]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/asio/test/buffered_read_stream.cpp
2 // buffered_read_stream.cpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2022 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_read_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/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_read_stream
<
40 boost::asio::ip::tcp::socket
> stream_type
;
42 void write_some_handler(const boost::system::error_code
&, std::size_t)
46 void fill_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_fill(&fill_handler
);
118 int i6
= stream1
.async_fill(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
));
171 char read_data
[sizeof(write_data
)];
172 const boost::asio::mutable_buffer read_buf
= boost::asio::buffer(read_data
);
174 std::size_t bytes_read
= 0;
175 while (bytes_read
< sizeof(read_data
))
177 bytes_read
+= server_socket
.read_some(
178 boost::asio::buffer(read_buf
+ bytes_read
));
181 BOOST_ASIO_CHECK(bytes_written
== sizeof(write_data
));
182 BOOST_ASIO_CHECK(bytes_read
== sizeof(read_data
));
183 BOOST_ASIO_CHECK(memcmp(write_data
, read_data
, sizeof(write_data
)) == 0);
186 while (bytes_written
< sizeof(write_data
))
188 bytes_written
+= server_socket
.write_some(
189 boost::asio::buffer(write_buf
+ bytes_written
));
193 while (bytes_read
< sizeof(read_data
))
195 bytes_read
+= client_socket
.read_some(
196 boost::asio::buffer(read_buf
+ bytes_read
));
199 BOOST_ASIO_CHECK(bytes_written
== sizeof(write_data
));
200 BOOST_ASIO_CHECK(bytes_read
== sizeof(read_data
));
201 BOOST_ASIO_CHECK(memcmp(write_data
, read_data
, sizeof(write_data
)) == 0);
203 server_socket
.close();
204 boost::system::error_code error
;
205 bytes_read
= client_socket
.read_some(
206 boost::asio::buffer(read_buf
), error
);
208 BOOST_ASIO_CHECK(bytes_read
== 0);
209 BOOST_ASIO_CHECK(error
== boost::asio::error::eof
);
211 client_socket
.close(error
);
214 void handle_accept(const boost::system::error_code
& e
)
216 BOOST_ASIO_CHECK(!e
);
219 void handle_write(const boost::system::error_code
& e
,
220 std::size_t bytes_transferred
,
221 std::size_t* total_bytes_written
)
223 BOOST_ASIO_CHECK(!e
);
225 throw boost::system::system_error(e
); // Terminate test.
226 *total_bytes_written
+= bytes_transferred
;
229 void handle_read(const boost::system::error_code
& e
,
230 std::size_t bytes_transferred
,
231 std::size_t* total_bytes_read
)
233 BOOST_ASIO_CHECK(!e
);
235 throw boost::system::system_error(e
); // Terminate test.
236 *total_bytes_read
+= bytes_transferred
;
239 void handle_read_eof(const boost::system::error_code
& e
,
240 std::size_t bytes_transferred
)
242 BOOST_ASIO_CHECK(e
== boost::asio::error::eof
);
243 BOOST_ASIO_CHECK(bytes_transferred
== 0);
246 void test_async_operations()
248 using namespace std
; // For memcmp.
250 #if defined(BOOST_ASIO_HAS_BOOST_BIND)
251 namespace bindns
= boost
;
252 #else // defined(BOOST_ASIO_HAS_BOOST_BIND)
253 namespace bindns
= std
;
254 #endif // defined(BOOST_ASIO_HAS_BOOST_BIND)
255 using bindns::placeholders::_1
;
256 using bindns::placeholders::_2
;
258 boost::asio::io_context io_context
;
260 boost::asio::ip::tcp::acceptor
acceptor(io_context
,
261 boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 0));
262 boost::asio::ip::tcp::endpoint server_endpoint
= acceptor
.local_endpoint();
263 server_endpoint
.address(boost::asio::ip::address_v4::loopback());
265 stream_type
client_socket(io_context
);
266 client_socket
.lowest_layer().connect(server_endpoint
);
268 stream_type
server_socket(io_context
);
269 acceptor
.async_accept(server_socket
.lowest_layer(), &handle_accept
);
271 io_context
.restart();
273 const char write_data
[]
274 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
275 const boost::asio::const_buffer write_buf
= boost::asio::buffer(write_data
);
277 std::size_t bytes_written
= 0;
278 while (bytes_written
< sizeof(write_data
))
280 client_socket
.async_write_some(
281 boost::asio::buffer(write_buf
+ bytes_written
),
282 bindns::bind(handle_write
, _1
, _2
, &bytes_written
));
284 io_context
.restart();
287 char read_data
[sizeof(write_data
)];
288 const boost::asio::mutable_buffer read_buf
= boost::asio::buffer(read_data
);
290 std::size_t bytes_read
= 0;
291 while (bytes_read
< sizeof(read_data
))
293 server_socket
.async_read_some(
294 boost::asio::buffer(read_buf
+ bytes_read
),
295 bindns::bind(handle_read
, _1
, _2
, &bytes_read
));
297 io_context
.restart();
300 BOOST_ASIO_CHECK(bytes_written
== sizeof(write_data
));
301 BOOST_ASIO_CHECK(bytes_read
== sizeof(read_data
));
302 BOOST_ASIO_CHECK(memcmp(write_data
, read_data
, sizeof(write_data
)) == 0);
305 while (bytes_written
< sizeof(write_data
))
307 server_socket
.async_write_some(
308 boost::asio::buffer(write_buf
+ bytes_written
),
309 bindns::bind(handle_write
, _1
, _2
, &bytes_written
));
311 io_context
.restart();
315 while (bytes_read
< sizeof(read_data
))
317 client_socket
.async_read_some(
318 boost::asio::buffer(read_buf
+ bytes_read
),
319 bindns::bind(handle_read
, _1
, _2
, &bytes_read
));
321 io_context
.restart();
324 BOOST_ASIO_CHECK(bytes_written
== sizeof(write_data
));
325 BOOST_ASIO_CHECK(bytes_read
== sizeof(read_data
));
326 BOOST_ASIO_CHECK(memcmp(write_data
, read_data
, sizeof(write_data
)) == 0);
328 server_socket
.close();
329 client_socket
.async_read_some(boost::asio::buffer(read_buf
), handle_read_eof
);
332 BOOST_ASIO_TEST_SUITE
334 "buffered_read_stream",
335 BOOST_ASIO_TEST_CASE(test_compile
)
336 BOOST_ASIO_TEST_CASE(test_sync_operations
)
337 BOOST_ASIO_TEST_CASE(test_async_operations
)