]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/asio/test/buffered_stream.cpp
5 // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
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)
16 // Test that header file is self-contained.
17 #include <boost/asio/buffered_stream.hpp>
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"
27 #if defined(BOOST_ASIO_HAS_BOOST_ARRAY)
28 # include <boost/array.hpp>
29 #else // defined(BOOST_ASIO_HAS_BOOST_ARRAY)
31 #endif // defined(BOOST_ASIO_HAS_BOOST_ARRAY)
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)
39 typedef boost::asio::buffered_stream
<
40 boost::asio::ip::tcp::socket
> stream_type
;
42 void write_some_handler(const boost::system::error_code
&, std::size_t)
46 void flush_handler(const boost::system::error_code
&, std::size_t)
50 void fill_handler(const boost::system::error_code
&, std::size_t)
54 void read_some_handler(const boost::system::error_code
&, std::size_t)
60 #if defined(BOOST_ASIO_HAS_BOOST_ARRAY)
62 #else // defined(BOOST_ASIO_HAS_BOOST_ARRAY)
64 #endif // defined(BOOST_ASIO_HAS_BOOST_ARRAY)
66 using namespace boost::asio
;
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
;
82 stream_type
stream1(ioc
);
83 stream_type
stream2(ioc
, 1024, 1024);
85 stream_type::executor_type ex
= stream1
.get_executor();
88 stream_type::lowest_layer_type
& lowest_layer
= stream1
.lowest_layer();
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
);
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
);
109 int i2
= stream1
.async_write_some(buffer(const_char_buffer
), lazy
);
111 int i3
= stream1
.async_write_some(mutable_buffers
, lazy
);
113 int i4
= stream1
.async_write_some(const_buffers
, lazy
);
115 int i5
= stream1
.async_write_some(null_buffers(), lazy
);
121 stream1
.async_flush(&flush_handler
);
122 int i6
= stream1
.async_flush(lazy
);
128 stream1
.async_fill(&fill_handler
);
129 int i7
= stream1
.async_fill(lazy
);
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
);
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
);
144 int i9
= stream1
.async_read_some(mutable_buffers
, lazy
);
146 int i10
= stream1
.async_read_some(null_buffers(), lazy
);
149 catch (std::exception
&)
154 void test_sync_operations()
156 using namespace std
; // For memcmp.
158 boost::asio::io_context io_context
;
160 boost::asio::ip::tcp::acceptor
acceptor(io_context
,
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());
165 stream_type
client_socket(io_context
);
166 client_socket
.lowest_layer().connect(server_endpoint
);
168 stream_type
server_socket(io_context
);
169 acceptor
.accept(server_socket
.lowest_layer());
171 const char write_data
[]
172 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
173 const boost::asio::const_buffer write_buf
= boost::asio::buffer(write_data
);
175 std::size_t bytes_written
= 0;
176 while (bytes_written
< sizeof(write_data
))
178 bytes_written
+= client_socket
.write_some(
179 boost::asio::buffer(write_buf
+ bytes_written
));
180 client_socket
.flush();
183 char read_data
[sizeof(write_data
)];
184 const boost::asio::mutable_buffer read_buf
= boost::asio::buffer(read_data
);
186 std::size_t bytes_read
= 0;
187 while (bytes_read
< sizeof(read_data
))
189 bytes_read
+= server_socket
.read_some(
190 boost::asio::buffer(read_buf
+ bytes_read
));
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);
198 while (bytes_written
< sizeof(write_data
))
200 bytes_written
+= server_socket
.write_some(
201 boost::asio::buffer(write_buf
+ bytes_written
));
202 server_socket
.flush();
206 while (bytes_read
< sizeof(read_data
))
208 bytes_read
+= client_socket
.read_some(
209 boost::asio::buffer(read_buf
+ bytes_read
));
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);
216 server_socket
.close();
217 boost::system::error_code error
;
218 bytes_read
= client_socket
.read_some(
219 boost::asio::buffer(read_buf
), error
);
221 BOOST_ASIO_CHECK(bytes_read
== 0);
222 BOOST_ASIO_CHECK(error
== boost::asio::error::eof
);
224 client_socket
.close(error
);
227 void handle_accept(const boost::system::error_code
& e
)
229 BOOST_ASIO_CHECK(!e
);
232 void handle_write(const boost::system::error_code
& e
,
233 std::size_t bytes_transferred
,
234 std::size_t* total_bytes_written
)
236 BOOST_ASIO_CHECK(!e
);
238 throw boost::system::system_error(e
); // Terminate test.
239 *total_bytes_written
+= bytes_transferred
;
242 void handle_flush(const boost::system::error_code
& e
)
244 BOOST_ASIO_CHECK(!e
);
247 void handle_read(const boost::system::error_code
& e
,
248 std::size_t bytes_transferred
,
249 std::size_t* total_bytes_read
)
251 BOOST_ASIO_CHECK(!e
);
253 throw boost::system::system_error(e
); // Terminate test.
254 *total_bytes_read
+= bytes_transferred
;
257 void handle_read_eof(const boost::system::error_code
& e
,
258 std::size_t bytes_transferred
)
260 BOOST_ASIO_CHECK(e
== boost::asio::error::eof
);
261 BOOST_ASIO_CHECK(bytes_transferred
== 0);
264 void test_async_operations()
266 using namespace std
; // For memcmp.
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)
276 boost::asio::io_context io_context
;
278 boost::asio::ip::tcp::acceptor
acceptor(io_context
,
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());
283 stream_type
client_socket(io_context
);
284 client_socket
.lowest_layer().connect(server_endpoint
);
286 stream_type
server_socket(io_context
);
287 acceptor
.async_accept(server_socket
.lowest_layer(), &handle_accept
);
289 io_context
.restart();
291 const char write_data
[]
292 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
293 const boost::asio::const_buffer write_buf
= boost::asio::buffer(write_data
);
295 std::size_t bytes_written
= 0;
296 while (bytes_written
< sizeof(write_data
))
298 client_socket
.async_write_some(
299 boost::asio::buffer(write_buf
+ bytes_written
),
300 bindns::bind(handle_write
, _1
, _2
, &bytes_written
));
302 io_context
.restart();
303 client_socket
.async_flush(
304 bindns::bind(handle_flush
, _1
));
306 io_context
.restart();
309 char read_data
[sizeof(write_data
)];
310 const boost::asio::mutable_buffer read_buf
= boost::asio::buffer(read_data
);
312 std::size_t bytes_read
= 0;
313 while (bytes_read
< sizeof(read_data
))
315 server_socket
.async_read_some(
316 boost::asio::buffer(read_buf
+ bytes_read
),
317 bindns::bind(handle_read
, _1
, _2
, &bytes_read
));
319 io_context
.restart();
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);
327 while (bytes_written
< sizeof(write_data
))
329 server_socket
.async_write_some(
330 boost::asio::buffer(write_buf
+ bytes_written
),
331 bindns::bind(handle_write
, _1
, _2
, &bytes_written
));
333 io_context
.restart();
334 server_socket
.async_flush(
335 bindns::bind(handle_flush
, _1
));
337 io_context
.restart();
341 while (bytes_read
< sizeof(read_data
))
343 client_socket
.async_read_some(
344 boost::asio::buffer(read_buf
+ bytes_read
),
345 bindns::bind(handle_read
, _1
, _2
, &bytes_read
));
347 io_context
.restart();
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);
354 server_socket
.close();
355 client_socket
.async_read_some(boost::asio::buffer(read_buf
), handle_read_eof
);
358 BOOST_ASIO_TEST_SUITE
361 BOOST_ASIO_TEST_CASE(test_compile
)
362 BOOST_ASIO_TEST_CASE(test_sync_operations
)
363 BOOST_ASIO_TEST_CASE(test_async_operations
)