buffered_read_stream.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. //
  2. // buffered_read_stream.cpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  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. // Disable autolinking for unit tests.
  11. #if !defined(BOOST_ALL_NO_LIB)
  12. #define BOOST_ALL_NO_LIB 1
  13. #endif // !defined(BOOST_ALL_NO_LIB)
  14. // Test that header file is self-contained.
  15. #include <boost/asio/buffered_read_stream.hpp>
  16. #include <cstring>
  17. #include "archetypes/async_result.hpp"
  18. #include <boost/asio/buffer.hpp>
  19. #include <boost/asio/io_context.hpp>
  20. #include <boost/asio/ip/tcp.hpp>
  21. #include <boost/system/system_error.hpp>
  22. #include "unit_test.hpp"
  23. #if defined(BOOST_ASIO_HAS_BOOST_ARRAY)
  24. # include <boost/array.hpp>
  25. #else // defined(BOOST_ASIO_HAS_BOOST_ARRAY)
  26. # include <array>
  27. #endif // defined(BOOST_ASIO_HAS_BOOST_ARRAY)
  28. #if defined(BOOST_ASIO_HAS_BOOST_BIND)
  29. # include <boost/bind.hpp>
  30. #else // defined(BOOST_ASIO_HAS_BOOST_BIND)
  31. # include <functional>
  32. #endif // defined(BOOST_ASIO_HAS_BOOST_BIND)
  33. typedef boost::asio::buffered_read_stream<
  34. boost::asio::ip::tcp::socket> stream_type;
  35. void write_some_handler(const boost::system::error_code&, std::size_t)
  36. {
  37. }
  38. void fill_handler(const boost::system::error_code&, std::size_t)
  39. {
  40. }
  41. void read_some_handler(const boost::system::error_code&, std::size_t)
  42. {
  43. }
  44. void test_compile()
  45. {
  46. #if defined(BOOST_ASIO_HAS_BOOST_ARRAY)
  47. using boost::array;
  48. #else // defined(BOOST_ASIO_HAS_BOOST_ARRAY)
  49. using std::array;
  50. #endif // defined(BOOST_ASIO_HAS_BOOST_ARRAY)
  51. using namespace boost::asio;
  52. try
  53. {
  54. io_context ioc;
  55. char mutable_char_buffer[128] = "";
  56. const char const_char_buffer[128] = "";
  57. array<boost::asio::mutable_buffer, 2> mutable_buffers = {{
  58. boost::asio::buffer(mutable_char_buffer, 10),
  59. boost::asio::buffer(mutable_char_buffer + 10, 10) }};
  60. array<boost::asio::const_buffer, 2> const_buffers = {{
  61. boost::asio::buffer(const_char_buffer, 10),
  62. boost::asio::buffer(const_char_buffer + 10, 10) }};
  63. archetypes::lazy_handler lazy;
  64. boost::system::error_code ec;
  65. stream_type stream1(ioc);
  66. stream_type stream2(ioc, 1024);
  67. stream_type::executor_type ex = stream1.get_executor();
  68. (void)ex;
  69. stream_type::lowest_layer_type& lowest_layer = stream1.lowest_layer();
  70. (void)lowest_layer;
  71. stream1.write_some(buffer(mutable_char_buffer));
  72. stream1.write_some(buffer(const_char_buffer));
  73. stream1.write_some(mutable_buffers);
  74. stream1.write_some(const_buffers);
  75. stream1.write_some(null_buffers());
  76. stream1.write_some(buffer(mutable_char_buffer), ec);
  77. stream1.write_some(buffer(const_char_buffer), ec);
  78. stream1.write_some(mutable_buffers, ec);
  79. stream1.write_some(const_buffers, ec);
  80. stream1.write_some(null_buffers(), ec);
  81. stream1.async_write_some(buffer(mutable_char_buffer), &write_some_handler);
  82. stream1.async_write_some(buffer(const_char_buffer), &write_some_handler);
  83. stream1.async_write_some(mutable_buffers, &write_some_handler);
  84. stream1.async_write_some(const_buffers, &write_some_handler);
  85. stream1.async_write_some(null_buffers(), &write_some_handler);
  86. int i1 = stream1.async_write_some(buffer(mutable_char_buffer), lazy);
  87. (void)i1;
  88. int i2 = stream1.async_write_some(buffer(const_char_buffer), lazy);
  89. (void)i2;
  90. int i3 = stream1.async_write_some(mutable_buffers, lazy);
  91. (void)i3;
  92. int i4 = stream1.async_write_some(const_buffers, lazy);
  93. (void)i4;
  94. int i5 = stream1.async_write_some(null_buffers(), lazy);
  95. (void)i5;
  96. stream1.fill();
  97. stream1.fill(ec);
  98. stream1.async_fill(&fill_handler);
  99. int i6 = stream1.async_fill(lazy);
  100. (void)i6;
  101. stream1.read_some(buffer(mutable_char_buffer));
  102. stream1.read_some(mutable_buffers);
  103. stream1.read_some(null_buffers());
  104. stream1.read_some(buffer(mutable_char_buffer), ec);
  105. stream1.read_some(mutable_buffers, ec);
  106. stream1.read_some(null_buffers(), ec);
  107. stream1.async_read_some(buffer(mutable_char_buffer), &read_some_handler);
  108. stream1.async_read_some(mutable_buffers, &read_some_handler);
  109. stream1.async_read_some(null_buffers(), &read_some_handler);
  110. int i7 = stream1.async_read_some(buffer(mutable_char_buffer), lazy);
  111. (void)i7;
  112. int i8 = stream1.async_read_some(mutable_buffers, lazy);
  113. (void)i8;
  114. int i9 = stream1.async_read_some(null_buffers(), lazy);
  115. (void)i9;
  116. }
  117. catch (std::exception&)
  118. {
  119. }
  120. }
  121. void test_sync_operations()
  122. {
  123. using namespace std; // For memcmp.
  124. boost::asio::io_context io_context;
  125. boost::asio::ip::tcp::acceptor acceptor(io_context,
  126. boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 0));
  127. boost::asio::ip::tcp::endpoint server_endpoint = acceptor.local_endpoint();
  128. server_endpoint.address(boost::asio::ip::address_v4::loopback());
  129. stream_type client_socket(io_context);
  130. client_socket.lowest_layer().connect(server_endpoint);
  131. stream_type server_socket(io_context);
  132. acceptor.accept(server_socket.lowest_layer());
  133. const char write_data[]
  134. = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  135. const boost::asio::const_buffer write_buf = boost::asio::buffer(write_data);
  136. std::size_t bytes_written = 0;
  137. while (bytes_written < sizeof(write_data))
  138. {
  139. bytes_written += client_socket.write_some(
  140. boost::asio::buffer(write_buf + bytes_written));
  141. }
  142. char read_data[sizeof(write_data)];
  143. const boost::asio::mutable_buffer read_buf = boost::asio::buffer(read_data);
  144. std::size_t bytes_read = 0;
  145. while (bytes_read < sizeof(read_data))
  146. {
  147. bytes_read += server_socket.read_some(
  148. boost::asio::buffer(read_buf + bytes_read));
  149. }
  150. BOOST_ASIO_CHECK(bytes_written == sizeof(write_data));
  151. BOOST_ASIO_CHECK(bytes_read == sizeof(read_data));
  152. BOOST_ASIO_CHECK(memcmp(write_data, read_data, sizeof(write_data)) == 0);
  153. bytes_written = 0;
  154. while (bytes_written < sizeof(write_data))
  155. {
  156. bytes_written += server_socket.write_some(
  157. boost::asio::buffer(write_buf + bytes_written));
  158. }
  159. bytes_read = 0;
  160. while (bytes_read < sizeof(read_data))
  161. {
  162. bytes_read += client_socket.read_some(
  163. boost::asio::buffer(read_buf + bytes_read));
  164. }
  165. BOOST_ASIO_CHECK(bytes_written == sizeof(write_data));
  166. BOOST_ASIO_CHECK(bytes_read == sizeof(read_data));
  167. BOOST_ASIO_CHECK(memcmp(write_data, read_data, sizeof(write_data)) == 0);
  168. server_socket.close();
  169. boost::system::error_code error;
  170. bytes_read = client_socket.read_some(
  171. boost::asio::buffer(read_buf), error);
  172. BOOST_ASIO_CHECK(bytes_read == 0);
  173. BOOST_ASIO_CHECK(error == boost::asio::error::eof);
  174. client_socket.close(error);
  175. }
  176. void handle_accept(const boost::system::error_code& e)
  177. {
  178. BOOST_ASIO_CHECK(!e);
  179. }
  180. void handle_write(const boost::system::error_code& e,
  181. std::size_t bytes_transferred,
  182. std::size_t* total_bytes_written)
  183. {
  184. BOOST_ASIO_CHECK(!e);
  185. if (e)
  186. throw boost::system::system_error(e); // Terminate test.
  187. *total_bytes_written += bytes_transferred;
  188. }
  189. void handle_read(const boost::system::error_code& e,
  190. std::size_t bytes_transferred,
  191. std::size_t* total_bytes_read)
  192. {
  193. BOOST_ASIO_CHECK(!e);
  194. if (e)
  195. throw boost::system::system_error(e); // Terminate test.
  196. *total_bytes_read += bytes_transferred;
  197. }
  198. void handle_read_eof(const boost::system::error_code& e,
  199. std::size_t bytes_transferred)
  200. {
  201. BOOST_ASIO_CHECK(e == boost::asio::error::eof);
  202. BOOST_ASIO_CHECK(bytes_transferred == 0);
  203. }
  204. void test_async_operations()
  205. {
  206. using namespace std; // For memcmp.
  207. #if defined(BOOST_ASIO_HAS_BOOST_BIND)
  208. namespace bindns = boost;
  209. #else // defined(BOOST_ASIO_HAS_BOOST_BIND)
  210. namespace bindns = std;
  211. using std::placeholders::_1;
  212. using std::placeholders::_2;
  213. #endif // defined(BOOST_ASIO_HAS_BOOST_BIND)
  214. boost::asio::io_context io_context;
  215. boost::asio::ip::tcp::acceptor acceptor(io_context,
  216. boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 0));
  217. boost::asio::ip::tcp::endpoint server_endpoint = acceptor.local_endpoint();
  218. server_endpoint.address(boost::asio::ip::address_v4::loopback());
  219. stream_type client_socket(io_context);
  220. client_socket.lowest_layer().connect(server_endpoint);
  221. stream_type server_socket(io_context);
  222. acceptor.async_accept(server_socket.lowest_layer(), &handle_accept);
  223. io_context.run();
  224. io_context.restart();
  225. const char write_data[]
  226. = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  227. const boost::asio::const_buffer write_buf = boost::asio::buffer(write_data);
  228. std::size_t bytes_written = 0;
  229. while (bytes_written < sizeof(write_data))
  230. {
  231. client_socket.async_write_some(
  232. boost::asio::buffer(write_buf + bytes_written),
  233. bindns::bind(handle_write, _1, _2, &bytes_written));
  234. io_context.run();
  235. io_context.restart();
  236. }
  237. char read_data[sizeof(write_data)];
  238. const boost::asio::mutable_buffer read_buf = boost::asio::buffer(read_data);
  239. std::size_t bytes_read = 0;
  240. while (bytes_read < sizeof(read_data))
  241. {
  242. server_socket.async_read_some(
  243. boost::asio::buffer(read_buf + bytes_read),
  244. bindns::bind(handle_read, _1, _2, &bytes_read));
  245. io_context.run();
  246. io_context.restart();
  247. }
  248. BOOST_ASIO_CHECK(bytes_written == sizeof(write_data));
  249. BOOST_ASIO_CHECK(bytes_read == sizeof(read_data));
  250. BOOST_ASIO_CHECK(memcmp(write_data, read_data, sizeof(write_data)) == 0);
  251. bytes_written = 0;
  252. while (bytes_written < sizeof(write_data))
  253. {
  254. server_socket.async_write_some(
  255. boost::asio::buffer(write_buf + bytes_written),
  256. bindns::bind(handle_write, _1, _2, &bytes_written));
  257. io_context.run();
  258. io_context.restart();
  259. }
  260. bytes_read = 0;
  261. while (bytes_read < sizeof(read_data))
  262. {
  263. client_socket.async_read_some(
  264. boost::asio::buffer(read_buf + bytes_read),
  265. bindns::bind(handle_read, _1, _2, &bytes_read));
  266. io_context.run();
  267. io_context.restart();
  268. }
  269. BOOST_ASIO_CHECK(bytes_written == sizeof(write_data));
  270. BOOST_ASIO_CHECK(bytes_read == sizeof(read_data));
  271. BOOST_ASIO_CHECK(memcmp(write_data, read_data, sizeof(write_data)) == 0);
  272. server_socket.close();
  273. client_socket.async_read_some(boost::asio::buffer(read_buf), handle_read_eof);
  274. }
  275. BOOST_ASIO_TEST_SUITE
  276. (
  277. "buffered_read_stream",
  278. BOOST_ASIO_TEST_CASE(test_compile)
  279. BOOST_ASIO_TEST_CASE(test_sync_operations)
  280. BOOST_ASIO_TEST_CASE(test_async_operations)
  281. )