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