• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // buffered_read_stream.cpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10 
11 // Disable autolinking for unit tests.
12 #if !defined(BOOST_ALL_NO_LIB)
13 #define BOOST_ALL_NO_LIB 1
14 #endif // !defined(BOOST_ALL_NO_LIB)
15 
16 // Test that header file is self-contained.
17 #include <boost/asio/buffered_read_stream.hpp>
18 
19 #include <cstring>
20 #include "archetypes/async_result.hpp"
21 #include <boost/asio/buffer.hpp>
22 #include <boost/asio/io_context.hpp>
23 #include <boost/asio/ip/tcp.hpp>
24 #include <boost/system/system_error.hpp>
25 #include "unit_test.hpp"
26 
27 #if defined(BOOST_ASIO_HAS_BOOST_ARRAY)
28 # include <boost/array.hpp>
29 #else // defined(BOOST_ASIO_HAS_BOOST_ARRAY)
30 # include <array>
31 #endif // defined(BOOST_ASIO_HAS_BOOST_ARRAY)
32 
33 #if defined(BOOST_ASIO_HAS_BOOST_BIND)
34 # include <boost/bind/bind.hpp>
35 #else // defined(BOOST_ASIO_HAS_BOOST_BIND)
36 # include <functional>
37 #endif // defined(BOOST_ASIO_HAS_BOOST_BIND)
38 
39 typedef boost::asio::buffered_read_stream<
40     boost::asio::ip::tcp::socket> stream_type;
41 
write_some_handler(const boost::system::error_code &,std::size_t)42 void write_some_handler(const boost::system::error_code&, std::size_t)
43 {
44 }
45 
fill_handler(const boost::system::error_code &,std::size_t)46 void fill_handler(const boost::system::error_code&, std::size_t)
47 {
48 }
49 
read_some_handler(const boost::system::error_code &,std::size_t)50 void read_some_handler(const boost::system::error_code&, std::size_t)
51 {
52 }
53 
test_compile()54 void test_compile()
55 {
56 #if defined(BOOST_ASIO_HAS_BOOST_ARRAY)
57   using boost::array;
58 #else // defined(BOOST_ASIO_HAS_BOOST_ARRAY)
59   using std::array;
60 #endif // defined(BOOST_ASIO_HAS_BOOST_ARRAY)
61 
62   using namespace boost::asio;
63 
64   try
65   {
66     io_context ioc;
67     char mutable_char_buffer[128] = "";
68     const char const_char_buffer[128] = "";
69     array<boost::asio::mutable_buffer, 2> mutable_buffers = {{
70         boost::asio::buffer(mutable_char_buffer, 10),
71         boost::asio::buffer(mutable_char_buffer + 10, 10) }};
72     array<boost::asio::const_buffer, 2> const_buffers = {{
73         boost::asio::buffer(const_char_buffer, 10),
74         boost::asio::buffer(const_char_buffer + 10, 10) }};
75     archetypes::lazy_handler lazy;
76     boost::system::error_code ec;
77 
78     stream_type stream1(ioc);
79     stream_type stream2(ioc, 1024);
80 
81     stream_type::executor_type ex = stream1.get_executor();
82     (void)ex;
83 
84     stream_type::lowest_layer_type& lowest_layer = stream1.lowest_layer();
85     (void)lowest_layer;
86 
87     stream1.write_some(buffer(mutable_char_buffer));
88     stream1.write_some(buffer(const_char_buffer));
89     stream1.write_some(mutable_buffers);
90     stream1.write_some(const_buffers);
91     stream1.write_some(null_buffers());
92     stream1.write_some(buffer(mutable_char_buffer), ec);
93     stream1.write_some(buffer(const_char_buffer), ec);
94     stream1.write_some(mutable_buffers, ec);
95     stream1.write_some(const_buffers, ec);
96     stream1.write_some(null_buffers(), ec);
97 
98     stream1.async_write_some(buffer(mutable_char_buffer), &write_some_handler);
99     stream1.async_write_some(buffer(const_char_buffer), &write_some_handler);
100     stream1.async_write_some(mutable_buffers, &write_some_handler);
101     stream1.async_write_some(const_buffers, &write_some_handler);
102     stream1.async_write_some(null_buffers(), &write_some_handler);
103     int i1 = stream1.async_write_some(buffer(mutable_char_buffer), lazy);
104     (void)i1;
105     int i2 = stream1.async_write_some(buffer(const_char_buffer), lazy);
106     (void)i2;
107     int i3 = stream1.async_write_some(mutable_buffers, lazy);
108     (void)i3;
109     int i4 = stream1.async_write_some(const_buffers, lazy);
110     (void)i4;
111     int i5 = stream1.async_write_some(null_buffers(), lazy);
112     (void)i5;
113 
114     stream1.fill();
115     stream1.fill(ec);
116 
117     stream1.async_fill(&fill_handler);
118     int i6 = stream1.async_fill(lazy);
119     (void)i6;
120 
121     stream1.read_some(buffer(mutable_char_buffer));
122     stream1.read_some(mutable_buffers);
123     stream1.read_some(null_buffers());
124     stream1.read_some(buffer(mutable_char_buffer), ec);
125     stream1.read_some(mutable_buffers, ec);
126     stream1.read_some(null_buffers(), ec);
127 
128     stream1.async_read_some(buffer(mutable_char_buffer), &read_some_handler);
129     stream1.async_read_some(mutable_buffers, &read_some_handler);
130     stream1.async_read_some(null_buffers(), &read_some_handler);
131     int i7 = stream1.async_read_some(buffer(mutable_char_buffer), lazy);
132     (void)i7;
133     int i8 = stream1.async_read_some(mutable_buffers, lazy);
134     (void)i8;
135     int i9 = stream1.async_read_some(null_buffers(), lazy);
136     (void)i9;
137   }
138   catch (std::exception&)
139   {
140   }
141 }
142 
test_sync_operations()143 void test_sync_operations()
144 {
145   using namespace std; // For memcmp.
146 
147   boost::asio::io_context io_context;
148 
149   boost::asio::ip::tcp::acceptor acceptor(io_context,
150       boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 0));
151   boost::asio::ip::tcp::endpoint server_endpoint = acceptor.local_endpoint();
152   server_endpoint.address(boost::asio::ip::address_v4::loopback());
153 
154   stream_type client_socket(io_context);
155   client_socket.lowest_layer().connect(server_endpoint);
156 
157   stream_type server_socket(io_context);
158   acceptor.accept(server_socket.lowest_layer());
159 
160   const char write_data[]
161     = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
162   const boost::asio::const_buffer write_buf = boost::asio::buffer(write_data);
163 
164   std::size_t bytes_written = 0;
165   while (bytes_written < sizeof(write_data))
166   {
167     bytes_written += client_socket.write_some(
168         boost::asio::buffer(write_buf + bytes_written));
169   }
170 
171   char read_data[sizeof(write_data)];
172   const boost::asio::mutable_buffer read_buf = boost::asio::buffer(read_data);
173 
174   std::size_t bytes_read = 0;
175   while (bytes_read < sizeof(read_data))
176   {
177     bytes_read += server_socket.read_some(
178         boost::asio::buffer(read_buf + bytes_read));
179   }
180 
181   BOOST_ASIO_CHECK(bytes_written == sizeof(write_data));
182   BOOST_ASIO_CHECK(bytes_read == sizeof(read_data));
183   BOOST_ASIO_CHECK(memcmp(write_data, read_data, sizeof(write_data)) == 0);
184 
185   bytes_written = 0;
186   while (bytes_written < sizeof(write_data))
187   {
188     bytes_written += server_socket.write_some(
189         boost::asio::buffer(write_buf + bytes_written));
190   }
191 
192   bytes_read = 0;
193   while (bytes_read < sizeof(read_data))
194   {
195     bytes_read += client_socket.read_some(
196         boost::asio::buffer(read_buf + bytes_read));
197   }
198 
199   BOOST_ASIO_CHECK(bytes_written == sizeof(write_data));
200   BOOST_ASIO_CHECK(bytes_read == sizeof(read_data));
201   BOOST_ASIO_CHECK(memcmp(write_data, read_data, sizeof(write_data)) == 0);
202 
203   server_socket.close();
204   boost::system::error_code error;
205   bytes_read = client_socket.read_some(
206       boost::asio::buffer(read_buf), error);
207 
208   BOOST_ASIO_CHECK(bytes_read == 0);
209   BOOST_ASIO_CHECK(error == boost::asio::error::eof);
210 
211   client_socket.close(error);
212 }
213 
handle_accept(const boost::system::error_code & e)214 void handle_accept(const boost::system::error_code& e)
215 {
216   BOOST_ASIO_CHECK(!e);
217 }
218 
handle_write(const boost::system::error_code & e,std::size_t bytes_transferred,std::size_t * total_bytes_written)219 void handle_write(const boost::system::error_code& e,
220     std::size_t bytes_transferred,
221     std::size_t* total_bytes_written)
222 {
223   BOOST_ASIO_CHECK(!e);
224   if (e)
225     throw boost::system::system_error(e); // Terminate test.
226   *total_bytes_written += bytes_transferred;
227 }
228 
handle_read(const boost::system::error_code & e,std::size_t bytes_transferred,std::size_t * total_bytes_read)229 void handle_read(const boost::system::error_code& e,
230     std::size_t bytes_transferred,
231     std::size_t* total_bytes_read)
232 {
233   BOOST_ASIO_CHECK(!e);
234   if (e)
235     throw boost::system::system_error(e); // Terminate test.
236   *total_bytes_read += bytes_transferred;
237 }
238 
handle_read_eof(const boost::system::error_code & e,std::size_t bytes_transferred)239 void handle_read_eof(const boost::system::error_code& e,
240     std::size_t bytes_transferred)
241 {
242   BOOST_ASIO_CHECK(e == boost::asio::error::eof);
243   BOOST_ASIO_CHECK(bytes_transferred == 0);
244 }
245 
test_async_operations()246 void test_async_operations()
247 {
248   using namespace std; // For memcmp.
249 
250 #if defined(BOOST_ASIO_HAS_BOOST_BIND)
251   namespace bindns = boost;
252 #else // defined(BOOST_ASIO_HAS_BOOST_BIND)
253   namespace bindns = std;
254 #endif // defined(BOOST_ASIO_HAS_BOOST_BIND)
255   using bindns::placeholders::_1;
256   using bindns::placeholders::_2;
257 
258   boost::asio::io_context io_context;
259 
260   boost::asio::ip::tcp::acceptor acceptor(io_context,
261       boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 0));
262   boost::asio::ip::tcp::endpoint server_endpoint = acceptor.local_endpoint();
263   server_endpoint.address(boost::asio::ip::address_v4::loopback());
264 
265   stream_type client_socket(io_context);
266   client_socket.lowest_layer().connect(server_endpoint);
267 
268   stream_type server_socket(io_context);
269   acceptor.async_accept(server_socket.lowest_layer(), &handle_accept);
270   io_context.run();
271   io_context.restart();
272 
273   const char write_data[]
274     = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
275   const boost::asio::const_buffer write_buf = boost::asio::buffer(write_data);
276 
277   std::size_t bytes_written = 0;
278   while (bytes_written < sizeof(write_data))
279   {
280     client_socket.async_write_some(
281         boost::asio::buffer(write_buf + bytes_written),
282         bindns::bind(handle_write, _1, _2, &bytes_written));
283     io_context.run();
284     io_context.restart();
285   }
286 
287   char read_data[sizeof(write_data)];
288   const boost::asio::mutable_buffer read_buf = boost::asio::buffer(read_data);
289 
290   std::size_t bytes_read = 0;
291   while (bytes_read < sizeof(read_data))
292   {
293     server_socket.async_read_some(
294         boost::asio::buffer(read_buf + bytes_read),
295         bindns::bind(handle_read, _1, _2, &bytes_read));
296     io_context.run();
297     io_context.restart();
298   }
299 
300   BOOST_ASIO_CHECK(bytes_written == sizeof(write_data));
301   BOOST_ASIO_CHECK(bytes_read == sizeof(read_data));
302   BOOST_ASIO_CHECK(memcmp(write_data, read_data, sizeof(write_data)) == 0);
303 
304   bytes_written = 0;
305   while (bytes_written < sizeof(write_data))
306   {
307     server_socket.async_write_some(
308         boost::asio::buffer(write_buf + bytes_written),
309         bindns::bind(handle_write, _1, _2, &bytes_written));
310     io_context.run();
311     io_context.restart();
312   }
313 
314   bytes_read = 0;
315   while (bytes_read < sizeof(read_data))
316   {
317     client_socket.async_read_some(
318         boost::asio::buffer(read_buf + bytes_read),
319         bindns::bind(handle_read, _1, _2, &bytes_read));
320     io_context.run();
321     io_context.restart();
322   }
323 
324   BOOST_ASIO_CHECK(bytes_written == sizeof(write_data));
325   BOOST_ASIO_CHECK(bytes_read == sizeof(read_data));
326   BOOST_ASIO_CHECK(memcmp(write_data, read_data, sizeof(write_data)) == 0);
327 
328   server_socket.close();
329   client_socket.async_read_some(boost::asio::buffer(read_buf), handle_read_eof);
330 }
331 
332 BOOST_ASIO_TEST_SUITE
333 (
334   "buffered_read_stream",
335   BOOST_ASIO_TEST_CASE(test_compile)
336   BOOST_ASIO_TEST_CASE(test_sync_operations)
337   BOOST_ASIO_TEST_CASE(test_async_operations)
338 )
339