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