]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/asio/test/buffered_write_stream.cpp
2 // buffered_write_stream.cpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~
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_write_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_write_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 read_some_handler(const boost::system::error_code
&, std::size_t)
56 #if defined(BOOST_ASIO_HAS_BOOST_ARRAY)
58 #else // defined(BOOST_ASIO_HAS_BOOST_ARRAY)
60 #endif // defined(BOOST_ASIO_HAS_BOOST_ARRAY)
62 using namespace boost::asio
;
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
;
78 stream_type
stream1(ioc
);
79 stream_type
stream2(ioc
, 1024);
81 stream_type::executor_type ex
= stream1
.get_executor();
84 stream_type::lowest_layer_type
& lowest_layer
= stream1
.lowest_layer();
87 stream1
.write_some(buffer(mutable_char_buffer
));
88 stream1
.write_some(buffer(const_char_buffer
));
89 stream1
.write_some(mutable_buffers
);
90 stream1
.write_some(const_buffers
);
91 stream1
.write_some(null_buffers());
92 stream1
.write_some(buffer(mutable_char_buffer
), ec
);
93 stream1
.write_some(buffer(const_char_buffer
), ec
);
94 stream1
.write_some(mutable_buffers
, ec
);
95 stream1
.write_some(const_buffers
, ec
);
96 stream1
.write_some(null_buffers(), ec
);
98 stream1
.async_write_some(buffer(mutable_char_buffer
), &write_some_handler
);
99 stream1
.async_write_some(buffer(const_char_buffer
), &write_some_handler
);
100 stream1
.async_write_some(mutable_buffers
, &write_some_handler
);
101 stream1
.async_write_some(const_buffers
, &write_some_handler
);
102 stream1
.async_write_some(null_buffers(), &write_some_handler
);
103 int i1
= stream1
.async_write_some(buffer(mutable_char_buffer
), lazy
);
105 int i2
= stream1
.async_write_some(buffer(const_char_buffer
), lazy
);
107 int i3
= stream1
.async_write_some(mutable_buffers
, lazy
);
109 int i4
= stream1
.async_write_some(const_buffers
, lazy
);
111 int i5
= stream1
.async_write_some(null_buffers(), lazy
);
117 stream1
.async_flush(&flush_handler
);
118 int i6
= stream1
.async_flush(lazy
);
121 stream1
.read_some(buffer(mutable_char_buffer
));
122 stream1
.read_some(mutable_buffers
);
123 stream1
.read_some(null_buffers());
124 stream1
.read_some(buffer(mutable_char_buffer
), ec
);
125 stream1
.read_some(mutable_buffers
, ec
);
126 stream1
.read_some(null_buffers(), ec
);
128 stream1
.async_read_some(buffer(mutable_char_buffer
), &read_some_handler
);
129 stream1
.async_read_some(mutable_buffers
, &read_some_handler
);
130 stream1
.async_read_some(null_buffers(), &read_some_handler
);
131 int i7
= stream1
.async_read_some(buffer(mutable_char_buffer
), lazy
);
133 int i8
= stream1
.async_read_some(mutable_buffers
, lazy
);
135 int i9
= stream1
.async_read_some(null_buffers(), lazy
);
138 catch (std::exception
&)
143 void test_sync_operations()
145 using namespace std
; // For memcmp.
147 boost::asio::io_context io_context
;
149 boost::asio::ip::tcp::acceptor
acceptor(io_context
,
150 boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 0));
151 boost::asio::ip::tcp::endpoint server_endpoint
= acceptor
.local_endpoint();
152 server_endpoint
.address(boost::asio::ip::address_v4::loopback());
154 stream_type
client_socket(io_context
);
155 client_socket
.lowest_layer().connect(server_endpoint
);
157 stream_type
server_socket(io_context
);
158 acceptor
.accept(server_socket
.lowest_layer());
160 const char write_data
[]
161 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
162 const boost::asio::const_buffer write_buf
= boost::asio::buffer(write_data
);
164 std::size_t bytes_written
= 0;
165 while (bytes_written
< sizeof(write_data
))
167 bytes_written
+= client_socket
.write_some(
168 boost::asio::buffer(write_buf
+ bytes_written
));
169 client_socket
.flush();
172 char read_data
[sizeof(write_data
)];
173 const boost::asio::mutable_buffer read_buf
= boost::asio::buffer(read_data
);
175 std::size_t bytes_read
= 0;
176 while (bytes_read
< sizeof(read_data
))
178 bytes_read
+= server_socket
.read_some(
179 boost::asio::buffer(read_buf
+ bytes_read
));
182 BOOST_ASIO_CHECK(bytes_written
== sizeof(write_data
));
183 BOOST_ASIO_CHECK(bytes_read
== sizeof(read_data
));
184 BOOST_ASIO_CHECK(memcmp(write_data
, read_data
, sizeof(write_data
)) == 0);
187 while (bytes_written
< sizeof(write_data
))
189 bytes_written
+= server_socket
.write_some(
190 boost::asio::buffer(write_buf
+ bytes_written
));
191 server_socket
.flush();
195 while (bytes_read
< sizeof(read_data
))
197 bytes_read
+= client_socket
.read_some(
198 boost::asio::buffer(read_buf
+ bytes_read
));
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);
205 server_socket
.close();
206 boost::system::error_code error
;
207 bytes_read
= client_socket
.read_some(
208 boost::asio::buffer(read_buf
), error
);
210 BOOST_ASIO_CHECK(bytes_read
== 0);
211 BOOST_ASIO_CHECK(error
== boost::asio::error::eof
);
213 client_socket
.close(error
);
216 void handle_accept(const boost::system::error_code
& e
)
218 BOOST_ASIO_CHECK(!e
);
221 void handle_write(const boost::system::error_code
& e
,
222 std::size_t bytes_transferred
,
223 std::size_t* total_bytes_written
)
225 BOOST_ASIO_CHECK(!e
);
227 throw boost::system::system_error(e
); // Terminate test.
228 *total_bytes_written
+= bytes_transferred
;
231 void handle_flush(const boost::system::error_code
& e
)
233 BOOST_ASIO_CHECK(!e
);
236 void handle_read(const boost::system::error_code
& e
,
237 std::size_t bytes_transferred
,
238 std::size_t* total_bytes_read
)
240 BOOST_ASIO_CHECK(!e
);
242 throw boost::system::system_error(e
); // Terminate test.
243 *total_bytes_read
+= bytes_transferred
;
246 void handle_read_eof(const boost::system::error_code
& e
,
247 std::size_t bytes_transferred
)
249 BOOST_ASIO_CHECK(e
== boost::asio::error::eof
);
250 BOOST_ASIO_CHECK(bytes_transferred
== 0);
253 void test_async_operations()
255 using namespace std
; // For memcmp.
257 #if defined(BOOST_ASIO_HAS_BOOST_BIND)
258 namespace bindns
= boost
;
259 #else // defined(BOOST_ASIO_HAS_BOOST_BIND)
260 namespace bindns
= std
;
261 using std::placeholders::_1
;
262 using std::placeholders::_2
;
263 #endif // defined(BOOST_ASIO_HAS_BOOST_BIND)
265 boost::asio::io_context io_context
;
267 boost::asio::ip::tcp::acceptor
acceptor(io_context
,
268 boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 0));
269 boost::asio::ip::tcp::endpoint server_endpoint
= acceptor
.local_endpoint();
270 server_endpoint
.address(boost::asio::ip::address_v4::loopback());
272 stream_type
client_socket(io_context
);
273 client_socket
.lowest_layer().connect(server_endpoint
);
275 stream_type
server_socket(io_context
);
276 acceptor
.async_accept(server_socket
.lowest_layer(), &handle_accept
);
278 io_context
.restart();
280 const char write_data
[]
281 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
282 const boost::asio::const_buffer write_buf
= boost::asio::buffer(write_data
);
284 std::size_t bytes_written
= 0;
285 while (bytes_written
< sizeof(write_data
))
287 client_socket
.async_write_some(
288 boost::asio::buffer(write_buf
+ bytes_written
),
289 bindns::bind(handle_write
, _1
, _2
, &bytes_written
));
291 io_context
.restart();
292 client_socket
.async_flush(
293 bindns::bind(handle_flush
, _1
));
295 io_context
.restart();
298 char read_data
[sizeof(write_data
)];
299 const boost::asio::mutable_buffer read_buf
= boost::asio::buffer(read_data
);
301 std::size_t bytes_read
= 0;
302 while (bytes_read
< sizeof(read_data
))
304 server_socket
.async_read_some(
305 boost::asio::buffer(read_buf
+ bytes_read
),
306 bindns::bind(handle_read
, _1
, _2
, &bytes_read
));
308 io_context
.restart();
311 BOOST_ASIO_CHECK(bytes_written
== sizeof(write_data
));
312 BOOST_ASIO_CHECK(bytes_read
== sizeof(read_data
));
313 BOOST_ASIO_CHECK(memcmp(write_data
, read_data
, sizeof(write_data
)) == 0);
316 while (bytes_written
< sizeof(write_data
))
318 server_socket
.async_write_some(
319 boost::asio::buffer(write_buf
+ bytes_written
),
320 bindns::bind(handle_write
, _1
, _2
, &bytes_written
));
322 io_context
.restart();
323 server_socket
.async_flush(
324 bindns::bind(handle_flush
, _1
));
326 io_context
.restart();
330 while (bytes_read
< sizeof(read_data
))
332 client_socket
.async_read_some(
333 boost::asio::buffer(read_buf
+ bytes_read
),
334 bindns::bind(handle_read
, _1
, _2
, &bytes_read
));
336 io_context
.restart();
339 BOOST_ASIO_CHECK(bytes_written
== sizeof(write_data
));
340 BOOST_ASIO_CHECK(bytes_read
== sizeof(read_data
));
341 BOOST_ASIO_CHECK(memcmp(write_data
, read_data
, sizeof(write_data
)) == 0);
343 server_socket
.close();
344 client_socket
.async_read_some(boost::asio::buffer(read_buf
), handle_read_eof
);
347 BOOST_ASIO_TEST_SUITE
349 "buffered_write_stream",
350 BOOST_ASIO_TEST_CASE(test_compile
)
351 BOOST_ASIO_TEST_CASE(test_sync_operations
)
352 BOOST_ASIO_TEST_CASE(test_async_operations
)