• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // buffered_write_stream.cpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2021 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_write_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_write_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 
flush_handler(const boost::system::error_code &,std::size_t)46 void flush_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.flush();
115     stream1.flush(ec);
116 
117     stream1.async_flush(&flush_handler);
118     int i6 = stream1.async_flush(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     client_socket.flush();
170   }
171 
172   char read_data[sizeof(write_data)];
173   const boost::asio::mutable_buffer read_buf = boost::asio::buffer(read_data);
174 
175   std::size_t bytes_read = 0;
176   while (bytes_read < sizeof(read_data))
177   {
178     bytes_read += server_socket.read_some(
179         boost::asio::buffer(read_buf + bytes_read));
180   }
181 
182   BOOST_ASIO_CHECK(bytes_written == sizeof(write_data));
183   BOOST_ASIO_CHECK(bytes_read == sizeof(read_data));
184   BOOST_ASIO_CHECK(memcmp(write_data, read_data, sizeof(write_data)) == 0);
185 
186   bytes_written = 0;
187   while (bytes_written < sizeof(write_data))
188   {
189     bytes_written += server_socket.write_some(
190         boost::asio::buffer(write_buf + bytes_written));
191     server_socket.flush();
192   }
193 
194   bytes_read = 0;
195   while (bytes_read < sizeof(read_data))
196   {
197     bytes_read += client_socket.read_some(
198         boost::asio::buffer(read_buf + bytes_read));
199   }
200 
201   BOOST_ASIO_CHECK(bytes_written == sizeof(write_data));
202   BOOST_ASIO_CHECK(bytes_read == sizeof(read_data));
203   BOOST_ASIO_CHECK(memcmp(write_data, read_data, sizeof(write_data)) == 0);
204 
205   server_socket.close();
206   boost::system::error_code error;
207   bytes_read = client_socket.read_some(
208       boost::asio::buffer(read_buf), error);
209 
210   BOOST_ASIO_CHECK(bytes_read == 0);
211   BOOST_ASIO_CHECK(error == boost::asio::error::eof);
212 
213   client_socket.close(error);
214 }
215 
handle_accept(const boost::system::error_code & e)216 void handle_accept(const boost::system::error_code& e)
217 {
218   BOOST_ASIO_CHECK(!e);
219 }
220 
handle_write(const boost::system::error_code & e,std::size_t bytes_transferred,std::size_t * total_bytes_written)221 void handle_write(const boost::system::error_code& e,
222     std::size_t bytes_transferred,
223     std::size_t* total_bytes_written)
224 {
225   BOOST_ASIO_CHECK(!e);
226   if (e)
227     throw boost::system::system_error(e); // Terminate test.
228   *total_bytes_written += bytes_transferred;
229 }
230 
handle_flush(const boost::system::error_code & e)231 void handle_flush(const boost::system::error_code& e)
232 {
233   BOOST_ASIO_CHECK(!e);
234 }
235 
handle_read(const boost::system::error_code & e,std::size_t bytes_transferred,std::size_t * total_bytes_read)236 void handle_read(const boost::system::error_code& e,
237     std::size_t bytes_transferred,
238     std::size_t* total_bytes_read)
239 {
240   BOOST_ASIO_CHECK(!e);
241   if (e)
242     throw boost::system::system_error(e); // Terminate test.
243   *total_bytes_read += bytes_transferred;
244 }
245 
handle_read_eof(const boost::system::error_code & e,std::size_t bytes_transferred)246 void handle_read_eof(const boost::system::error_code& e,
247     std::size_t bytes_transferred)
248 {
249   BOOST_ASIO_CHECK(e == boost::asio::error::eof);
250   BOOST_ASIO_CHECK(bytes_transferred == 0);
251 }
252 
test_async_operations()253 void test_async_operations()
254 {
255   using namespace std; // For memcmp.
256 
257 #if defined(BOOST_ASIO_HAS_BOOST_BIND)
258   namespace bindns = boost;
259 #else // defined(BOOST_ASIO_HAS_BOOST_BIND)
260   namespace bindns = std;
261 #endif // defined(BOOST_ASIO_HAS_BOOST_BIND)
262   using bindns::placeholders::_1;
263   using bindns::placeholders::_2;
264 
265   boost::asio::io_context io_context;
266 
267   boost::asio::ip::tcp::acceptor acceptor(io_context,
268       boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 0));
269   boost::asio::ip::tcp::endpoint server_endpoint = acceptor.local_endpoint();
270   server_endpoint.address(boost::asio::ip::address_v4::loopback());
271 
272   stream_type client_socket(io_context);
273   client_socket.lowest_layer().connect(server_endpoint);
274 
275   stream_type server_socket(io_context);
276   acceptor.async_accept(server_socket.lowest_layer(), &handle_accept);
277   io_context.run();
278   io_context.restart();
279 
280   const char write_data[]
281     = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
282   const boost::asio::const_buffer write_buf = boost::asio::buffer(write_data);
283 
284   std::size_t bytes_written = 0;
285   while (bytes_written < sizeof(write_data))
286   {
287     client_socket.async_write_some(
288         boost::asio::buffer(write_buf + bytes_written),
289         bindns::bind(handle_write, _1, _2, &bytes_written));
290     io_context.run();
291     io_context.restart();
292     client_socket.async_flush(
293         bindns::bind(handle_flush, _1));
294     io_context.run();
295     io_context.restart();
296   }
297 
298   char read_data[sizeof(write_data)];
299   const boost::asio::mutable_buffer read_buf = boost::asio::buffer(read_data);
300 
301   std::size_t bytes_read = 0;
302   while (bytes_read < sizeof(read_data))
303   {
304     server_socket.async_read_some(
305         boost::asio::buffer(read_buf + bytes_read),
306         bindns::bind(handle_read, _1, _2, &bytes_read));
307     io_context.run();
308     io_context.restart();
309   }
310 
311   BOOST_ASIO_CHECK(bytes_written == sizeof(write_data));
312   BOOST_ASIO_CHECK(bytes_read == sizeof(read_data));
313   BOOST_ASIO_CHECK(memcmp(write_data, read_data, sizeof(write_data)) == 0);
314 
315   bytes_written = 0;
316   while (bytes_written < sizeof(write_data))
317   {
318     server_socket.async_write_some(
319         boost::asio::buffer(write_buf + bytes_written),
320         bindns::bind(handle_write, _1, _2, &bytes_written));
321     io_context.run();
322     io_context.restart();
323     server_socket.async_flush(
324         bindns::bind(handle_flush, _1));
325     io_context.run();
326     io_context.restart();
327   }
328 
329   bytes_read = 0;
330   while (bytes_read < sizeof(read_data))
331   {
332     client_socket.async_read_some(
333         boost::asio::buffer(read_buf + bytes_read),
334         bindns::bind(handle_read, _1, _2, &bytes_read));
335     io_context.run();
336     io_context.restart();
337   }
338 
339   BOOST_ASIO_CHECK(bytes_written == sizeof(write_data));
340   BOOST_ASIO_CHECK(bytes_read == sizeof(read_data));
341   BOOST_ASIO_CHECK(memcmp(write_data, read_data, sizeof(write_data)) == 0);
342 
343   server_socket.close();
344   client_socket.async_read_some(boost::asio::buffer(read_buf), handle_read_eof);
345 }
346 
347 BOOST_ASIO_TEST_SUITE
348 (
349   "buffered_write_stream",
350   BOOST_ASIO_TEST_CASE(test_compile)
351   BOOST_ASIO_TEST_CASE(test_sync_operations)
352   BOOST_ASIO_TEST_CASE(test_async_operations)
353 )
354