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