Imported Upstream version 1.64.0
[platform/upstream/boost.git] / libs / asio / test / buffered_stream.cpp
1 //
2 // buffered_stream.cpp
3 // ~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2017 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_stream.hpp>
18
19 #include <cstring>
20 #include "archetypes/async_result.hpp"
21 #include <boost/asio/buffer.hpp>
22 #include <boost/asio/io_service.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.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_stream<
40     boost::asio::ip::tcp::socket> stream_type;
41
42 void write_some_handler(const boost::system::error_code&, std::size_t)
43 {
44 }
45
46 void flush_handler(const boost::system::error_code&, std::size_t)
47 {
48 }
49
50 void fill_handler(const boost::system::error_code&, std::size_t)
51 {
52 }
53
54 void read_some_handler(const boost::system::error_code&, std::size_t)
55 {
56 }
57
58 void test_compile()
59 {
60 #if defined(BOOST_ASIO_HAS_BOOST_ARRAY)
61   using boost::array;
62 #else // defined(BOOST_ASIO_HAS_BOOST_ARRAY)
63   using std::array;
64 #endif // defined(BOOST_ASIO_HAS_BOOST_ARRAY)
65
66   using namespace boost::asio;
67
68   try
69   {
70     io_service ios;
71     char mutable_char_buffer[128] = "";
72     const char const_char_buffer[128] = "";
73     array<boost::asio::mutable_buffer, 2> mutable_buffers = {{
74         boost::asio::buffer(mutable_char_buffer, 10),
75         boost::asio::buffer(mutable_char_buffer + 10, 10) }};
76     array<boost::asio::const_buffer, 2> const_buffers = {{
77         boost::asio::buffer(const_char_buffer, 10),
78         boost::asio::buffer(const_char_buffer + 10, 10) }};
79     archetypes::lazy_handler lazy;
80     boost::system::error_code ec;
81
82     stream_type stream1(ios);
83     stream_type stream2(ios, 1024, 1024);
84
85     io_service& ios_ref = stream1.get_io_service();
86     (void)ios_ref;
87
88     stream_type::lowest_layer_type& lowest_layer = stream1.lowest_layer();
89     (void)lowest_layer;
90
91     stream1.write_some(buffer(mutable_char_buffer));
92     stream1.write_some(buffer(const_char_buffer));
93     stream1.write_some(mutable_buffers);
94     stream1.write_some(const_buffers);
95     stream1.write_some(null_buffers());
96     stream1.write_some(buffer(mutable_char_buffer), ec);
97     stream1.write_some(buffer(const_char_buffer), ec);
98     stream1.write_some(mutable_buffers, ec);
99     stream1.write_some(const_buffers, ec);
100     stream1.write_some(null_buffers(), ec);
101
102     stream1.async_write_some(buffer(mutable_char_buffer), &write_some_handler);
103     stream1.async_write_some(buffer(const_char_buffer), &write_some_handler);
104     stream1.async_write_some(mutable_buffers, &write_some_handler);
105     stream1.async_write_some(const_buffers, &write_some_handler);
106     stream1.async_write_some(null_buffers(), &write_some_handler);
107     int i1 = stream1.async_write_some(buffer(mutable_char_buffer), lazy);
108     (void)i1;
109     int i2 = stream1.async_write_some(buffer(const_char_buffer), lazy);
110     (void)i2;
111     int i3 = stream1.async_write_some(mutable_buffers, lazy);
112     (void)i3;
113     int i4 = stream1.async_write_some(const_buffers, lazy);
114     (void)i4;
115     int i5 = stream1.async_write_some(null_buffers(), lazy);
116     (void)i5;
117
118     stream1.flush();
119     stream1.flush(ec);
120
121     stream1.async_flush(&flush_handler);
122     int i6 = stream1.async_flush(lazy);
123     (void)i6;
124
125     stream1.fill();
126     stream1.fill(ec);
127
128     stream1.async_fill(&fill_handler);
129     int i7 = stream1.async_fill(lazy);
130     (void)i7;
131
132     stream1.read_some(buffer(mutable_char_buffer));
133     stream1.read_some(mutable_buffers);
134     stream1.read_some(null_buffers());
135     stream1.read_some(buffer(mutable_char_buffer), ec);
136     stream1.read_some(mutable_buffers, ec);
137     stream1.read_some(null_buffers(), ec);
138
139     stream1.async_read_some(buffer(mutable_char_buffer), &read_some_handler);
140     stream1.async_read_some(mutable_buffers, &read_some_handler);
141     stream1.async_read_some(null_buffers(), &read_some_handler);
142     int i8 = stream1.async_read_some(buffer(mutable_char_buffer), lazy);
143     (void)i8;
144     int i9 = stream1.async_read_some(mutable_buffers, lazy);
145     (void)i9;
146     int i10 = stream1.async_read_some(null_buffers(), lazy);
147     (void)i10;
148   }
149   catch (std::exception&)
150   {
151   }
152 }
153
154 void test_sync_operations()
155 {
156   using namespace std; // For memcmp.
157
158   boost::asio::io_service io_service;
159
160   boost::asio::ip::tcp::acceptor acceptor(io_service,
161       boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 0));
162   boost::asio::ip::tcp::endpoint server_endpoint = acceptor.local_endpoint();
163   server_endpoint.address(boost::asio::ip::address_v4::loopback());
164
165   stream_type client_socket(io_service);
166   client_socket.lowest_layer().connect(server_endpoint);
167
168   stream_type server_socket(io_service);
169   acceptor.accept(server_socket.lowest_layer());
170
171   const char write_data[]
172     = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
173   const boost::asio::const_buffer write_buf = boost::asio::buffer(write_data);
174
175   std::size_t bytes_written = 0;
176   while (bytes_written < sizeof(write_data))
177   {
178     bytes_written += client_socket.write_some(
179         boost::asio::buffer(write_buf + bytes_written));
180     client_socket.flush();
181   }
182
183   char read_data[sizeof(write_data)];
184   const boost::asio::mutable_buffer read_buf = boost::asio::buffer(read_data);
185
186   std::size_t bytes_read = 0;
187   while (bytes_read < sizeof(read_data))
188   {
189     bytes_read += server_socket.read_some(
190         boost::asio::buffer(read_buf + bytes_read));
191   }
192
193   BOOST_ASIO_CHECK(bytes_written == sizeof(write_data));
194   BOOST_ASIO_CHECK(bytes_read == sizeof(read_data));
195   BOOST_ASIO_CHECK(memcmp(write_data, read_data, sizeof(write_data)) == 0);
196
197   bytes_written = 0;
198   while (bytes_written < sizeof(write_data))
199   {
200     bytes_written += server_socket.write_some(
201         boost::asio::buffer(write_buf + bytes_written));
202     server_socket.flush();
203   }
204
205   bytes_read = 0;
206   while (bytes_read < sizeof(read_data))
207   {
208     bytes_read += client_socket.read_some(
209         boost::asio::buffer(read_buf + bytes_read));
210   }
211
212   BOOST_ASIO_CHECK(bytes_written == sizeof(write_data));
213   BOOST_ASIO_CHECK(bytes_read == sizeof(read_data));
214   BOOST_ASIO_CHECK(memcmp(write_data, read_data, sizeof(write_data)) == 0);
215
216   server_socket.close();
217   boost::system::error_code error;
218   bytes_read = client_socket.read_some(
219       boost::asio::buffer(read_buf), error);
220
221   BOOST_ASIO_CHECK(bytes_read == 0);
222   BOOST_ASIO_CHECK(error == boost::asio::error::eof);
223
224   client_socket.close(error);
225 }
226
227 void handle_accept(const boost::system::error_code& e)
228 {
229   BOOST_ASIO_CHECK(!e);
230 }
231
232 void handle_write(const boost::system::error_code& e,
233     std::size_t bytes_transferred,
234     std::size_t* total_bytes_written)
235 {
236   BOOST_ASIO_CHECK(!e);
237   if (e)
238     throw boost::system::system_error(e); // Terminate test.
239   *total_bytes_written += bytes_transferred;
240 }
241
242 void handle_flush(const boost::system::error_code& e)
243 {
244   BOOST_ASIO_CHECK(!e);
245 }
246
247 void handle_read(const boost::system::error_code& e,
248     std::size_t bytes_transferred,
249     std::size_t* total_bytes_read)
250 {
251   BOOST_ASIO_CHECK(!e);
252   if (e)
253     throw boost::system::system_error(e); // Terminate test.
254   *total_bytes_read += bytes_transferred;
255 }
256
257 void handle_read_eof(const boost::system::error_code& e,
258     std::size_t bytes_transferred)
259 {
260   BOOST_ASIO_CHECK(e == boost::asio::error::eof);
261   BOOST_ASIO_CHECK(bytes_transferred == 0);
262 }
263
264 void test_async_operations()
265 {
266   using namespace std; // For memcmp.
267
268 #if defined(BOOST_ASIO_HAS_BOOST_BIND)
269   namespace bindns = boost;
270 #else // defined(BOOST_ASIO_HAS_BOOST_BIND)
271   namespace bindns = std;
272   using std::placeholders::_1;
273   using std::placeholders::_2;
274 #endif // defined(BOOST_ASIO_HAS_BOOST_BIND)
275
276   boost::asio::io_service io_service;
277
278   boost::asio::ip::tcp::acceptor acceptor(io_service,
279       boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 0));
280   boost::asio::ip::tcp::endpoint server_endpoint = acceptor.local_endpoint();
281   server_endpoint.address(boost::asio::ip::address_v4::loopback());
282
283   stream_type client_socket(io_service);
284   client_socket.lowest_layer().connect(server_endpoint);
285
286   stream_type server_socket(io_service);
287   acceptor.async_accept(server_socket.lowest_layer(), &handle_accept);
288   io_service.run();
289   io_service.reset();
290
291   const char write_data[]
292     = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
293   const boost::asio::const_buffer write_buf = boost::asio::buffer(write_data);
294
295   std::size_t bytes_written = 0;
296   while (bytes_written < sizeof(write_data))
297   {
298     client_socket.async_write_some(
299         boost::asio::buffer(write_buf + bytes_written),
300         bindns::bind(handle_write, _1, _2, &bytes_written));
301     io_service.run();
302     io_service.reset();
303     client_socket.async_flush(
304         bindns::bind(handle_flush, _1));
305     io_service.run();
306     io_service.reset();
307   }
308
309   char read_data[sizeof(write_data)];
310   const boost::asio::mutable_buffer read_buf = boost::asio::buffer(read_data);
311
312   std::size_t bytes_read = 0;
313   while (bytes_read < sizeof(read_data))
314   {
315     server_socket.async_read_some(
316         boost::asio::buffer(read_buf + bytes_read),
317         bindns::bind(handle_read, _1, _2, &bytes_read));
318     io_service.run();
319     io_service.reset();
320   }
321
322   BOOST_ASIO_CHECK(bytes_written == sizeof(write_data));
323   BOOST_ASIO_CHECK(bytes_read == sizeof(read_data));
324   BOOST_ASIO_CHECK(memcmp(write_data, read_data, sizeof(write_data)) == 0);
325
326   bytes_written = 0;
327   while (bytes_written < sizeof(write_data))
328   {
329     server_socket.async_write_some(
330         boost::asio::buffer(write_buf + bytes_written),
331         bindns::bind(handle_write, _1, _2, &bytes_written));
332     io_service.run();
333     io_service.reset();
334     server_socket.async_flush(
335         bindns::bind(handle_flush, _1));
336     io_service.run();
337     io_service.reset();
338   }
339
340   bytes_read = 0;
341   while (bytes_read < sizeof(read_data))
342   {
343     client_socket.async_read_some(
344         boost::asio::buffer(read_buf + bytes_read),
345         bindns::bind(handle_read, _1, _2, &bytes_read));
346     io_service.run();
347     io_service.reset();
348   }
349
350   BOOST_ASIO_CHECK(bytes_written == sizeof(write_data));
351   BOOST_ASIO_CHECK(bytes_read == sizeof(read_data));
352   BOOST_ASIO_CHECK(memcmp(write_data, read_data, sizeof(write_data)) == 0);
353
354   server_socket.close();
355   client_socket.async_read_some(boost::asio::buffer(read_buf), handle_read_eof);
356 }
357
358 BOOST_ASIO_TEST_SUITE
359 (
360   "buffered_stream",
361   BOOST_ASIO_TEST_CASE(test_compile)
362   BOOST_ASIO_TEST_CASE(test_sync_operations)
363   BOOST_ASIO_TEST_CASE(test_async_operations)
364 )