PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/package/boost_1_58_0/libs/asio/test/buffered_stream.cpp

https://gitlab.com/cdeclare/intcrypt
C++ | 364 lines | 292 code | 61 blank | 11 comment | 26 complexity | a20e0ce70084aea02b42e75fc3964771 MD5 | raw file
  1. //
  2. // buffered_stream.cpp
  3. // ~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 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_stream.hpp>
  16. #include <cstring>
  17. #include "archetypes/async_result.hpp"
  18. #include <boost/asio/buffer.hpp>
  19. #include <boost/asio/io_service.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_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 flush_handler(const boost::system::error_code&, std::size_t)
  39. {
  40. }
  41. void fill_handler(const boost::system::error_code&, std::size_t)
  42. {
  43. }
  44. void read_some_handler(const boost::system::error_code&, std::size_t)
  45. {
  46. }
  47. void test_compile()
  48. {
  49. #if defined(BOOST_ASIO_HAS_BOOST_ARRAY)
  50. using boost::array;
  51. #else // defined(BOOST_ASIO_HAS_BOOST_ARRAY)
  52. using std::array;
  53. #endif // defined(BOOST_ASIO_HAS_BOOST_ARRAY)
  54. using namespace boost::asio;
  55. try
  56. {
  57. io_service ios;
  58. char mutable_char_buffer[128] = "";
  59. const char const_char_buffer[128] = "";
  60. array<boost::asio::mutable_buffer, 2> mutable_buffers = {{
  61. boost::asio::buffer(mutable_char_buffer, 10),
  62. boost::asio::buffer(mutable_char_buffer + 10, 10) }};
  63. array<boost::asio::const_buffer, 2> const_buffers = {{
  64. boost::asio::buffer(const_char_buffer, 10),
  65. boost::asio::buffer(const_char_buffer + 10, 10) }};
  66. archetypes::lazy_handler lazy;
  67. boost::system::error_code ec;
  68. stream_type stream1(ios);
  69. stream_type stream2(ios, 1024, 1024);
  70. io_service& ios_ref = stream1.get_io_service();
  71. (void)ios_ref;
  72. stream_type::lowest_layer_type& lowest_layer = stream1.lowest_layer();
  73. (void)lowest_layer;
  74. stream1.write_some(buffer(mutable_char_buffer));
  75. stream1.write_some(buffer(const_char_buffer));
  76. stream1.write_some(mutable_buffers);
  77. stream1.write_some(const_buffers);
  78. stream1.write_some(null_buffers());
  79. stream1.write_some(buffer(mutable_char_buffer), ec);
  80. stream1.write_some(buffer(const_char_buffer), ec);
  81. stream1.write_some(mutable_buffers, ec);
  82. stream1.write_some(const_buffers, ec);
  83. stream1.write_some(null_buffers(), ec);
  84. stream1.async_write_some(buffer(mutable_char_buffer), &write_some_handler);
  85. stream1.async_write_some(buffer(const_char_buffer), &write_some_handler);
  86. stream1.async_write_some(mutable_buffers, &write_some_handler);
  87. stream1.async_write_some(const_buffers, &write_some_handler);
  88. stream1.async_write_some(null_buffers(), &write_some_handler);
  89. int i1 = stream1.async_write_some(buffer(mutable_char_buffer), lazy);
  90. (void)i1;
  91. int i2 = stream1.async_write_some(buffer(const_char_buffer), lazy);
  92. (void)i2;
  93. int i3 = stream1.async_write_some(mutable_buffers, lazy);
  94. (void)i3;
  95. int i4 = stream1.async_write_some(const_buffers, lazy);
  96. (void)i4;
  97. int i5 = stream1.async_write_some(null_buffers(), lazy);
  98. (void)i5;
  99. stream1.flush();
  100. stream1.flush(ec);
  101. stream1.async_flush(&flush_handler);
  102. int i6 = stream1.async_flush(lazy);
  103. (void)i6;
  104. stream1.fill();
  105. stream1.fill(ec);
  106. stream1.async_fill(&fill_handler);
  107. int i7 = stream1.async_fill(lazy);
  108. (void)i7;
  109. stream1.read_some(buffer(mutable_char_buffer));
  110. stream1.read_some(mutable_buffers);
  111. stream1.read_some(null_buffers());
  112. stream1.read_some(buffer(mutable_char_buffer), ec);
  113. stream1.read_some(mutable_buffers, ec);
  114. stream1.read_some(null_buffers(), ec);
  115. stream1.async_read_some(buffer(mutable_char_buffer), &read_some_handler);
  116. stream1.async_read_some(mutable_buffers, &read_some_handler);
  117. stream1.async_read_some(null_buffers(), &read_some_handler);
  118. int i8 = stream1.async_read_some(buffer(mutable_char_buffer), lazy);
  119. (void)i8;
  120. int i9 = stream1.async_read_some(mutable_buffers, lazy);
  121. (void)i9;
  122. int i10 = stream1.async_read_some(null_buffers(), lazy);
  123. (void)i10;
  124. }
  125. catch (std::exception&)
  126. {
  127. }
  128. }
  129. void test_sync_operations()
  130. {
  131. using namespace std; // For memcmp.
  132. boost::asio::io_service io_service;
  133. boost::asio::ip::tcp::acceptor acceptor(io_service,
  134. boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 0));
  135. boost::asio::ip::tcp::endpoint server_endpoint = acceptor.local_endpoint();
  136. server_endpoint.address(boost::asio::ip::address_v4::loopback());
  137. stream_type client_socket(io_service);
  138. client_socket.lowest_layer().connect(server_endpoint);
  139. stream_type server_socket(io_service);
  140. acceptor.accept(server_socket.lowest_layer());
  141. const char write_data[]
  142. = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  143. const boost::asio::const_buffer write_buf = boost::asio::buffer(write_data);
  144. std::size_t bytes_written = 0;
  145. while (bytes_written < sizeof(write_data))
  146. {
  147. bytes_written += client_socket.write_some(
  148. boost::asio::buffer(write_buf + bytes_written));
  149. client_socket.flush();
  150. }
  151. char read_data[sizeof(write_data)];
  152. const boost::asio::mutable_buffer read_buf = boost::asio::buffer(read_data);
  153. std::size_t bytes_read = 0;
  154. while (bytes_read < sizeof(read_data))
  155. {
  156. bytes_read += server_socket.read_some(
  157. boost::asio::buffer(read_buf + bytes_read));
  158. }
  159. BOOST_ASIO_CHECK(bytes_written == sizeof(write_data));
  160. BOOST_ASIO_CHECK(bytes_read == sizeof(read_data));
  161. BOOST_ASIO_CHECK(memcmp(write_data, read_data, sizeof(write_data)) == 0);
  162. bytes_written = 0;
  163. while (bytes_written < sizeof(write_data))
  164. {
  165. bytes_written += server_socket.write_some(
  166. boost::asio::buffer(write_buf + bytes_written));
  167. server_socket.flush();
  168. }
  169. bytes_read = 0;
  170. while (bytes_read < sizeof(read_data))
  171. {
  172. bytes_read += client_socket.read_some(
  173. boost::asio::buffer(read_buf + bytes_read));
  174. }
  175. BOOST_ASIO_CHECK(bytes_written == sizeof(write_data));
  176. BOOST_ASIO_CHECK(bytes_read == sizeof(read_data));
  177. BOOST_ASIO_CHECK(memcmp(write_data, read_data, sizeof(write_data)) == 0);
  178. server_socket.close();
  179. boost::system::error_code error;
  180. bytes_read = client_socket.read_some(
  181. boost::asio::buffer(read_buf), error);
  182. BOOST_ASIO_CHECK(bytes_read == 0);
  183. BOOST_ASIO_CHECK(error == boost::asio::error::eof);
  184. client_socket.close(error);
  185. }
  186. void handle_accept(const boost::system::error_code& e)
  187. {
  188. BOOST_ASIO_CHECK(!e);
  189. }
  190. void handle_write(const boost::system::error_code& e,
  191. std::size_t bytes_transferred,
  192. std::size_t* total_bytes_written)
  193. {
  194. BOOST_ASIO_CHECK(!e);
  195. if (e)
  196. throw boost::system::system_error(e); // Terminate test.
  197. *total_bytes_written += bytes_transferred;
  198. }
  199. void handle_flush(const boost::system::error_code& e)
  200. {
  201. BOOST_ASIO_CHECK(!e);
  202. }
  203. void handle_read(const boost::system::error_code& e,
  204. std::size_t bytes_transferred,
  205. std::size_t* total_bytes_read)
  206. {
  207. BOOST_ASIO_CHECK(!e);
  208. if (e)
  209. throw boost::system::system_error(e); // Terminate test.
  210. *total_bytes_read += bytes_transferred;
  211. }
  212. void handle_read_eof(const boost::system::error_code& e,
  213. std::size_t bytes_transferred)
  214. {
  215. BOOST_ASIO_CHECK(e == boost::asio::error::eof);
  216. BOOST_ASIO_CHECK(bytes_transferred == 0);
  217. }
  218. void test_async_operations()
  219. {
  220. using namespace std; // For memcmp.
  221. #if defined(BOOST_ASIO_HAS_BOOST_BIND)
  222. namespace bindns = boost;
  223. #else // defined(BOOST_ASIO_HAS_BOOST_BIND)
  224. namespace bindns = std;
  225. using std::placeholders::_1;
  226. using std::placeholders::_2;
  227. #endif // defined(BOOST_ASIO_HAS_BOOST_BIND)
  228. boost::asio::io_service io_service;
  229. boost::asio::ip::tcp::acceptor acceptor(io_service,
  230. boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 0));
  231. boost::asio::ip::tcp::endpoint server_endpoint = acceptor.local_endpoint();
  232. server_endpoint.address(boost::asio::ip::address_v4::loopback());
  233. stream_type client_socket(io_service);
  234. client_socket.lowest_layer().connect(server_endpoint);
  235. stream_type server_socket(io_service);
  236. acceptor.async_accept(server_socket.lowest_layer(), &handle_accept);
  237. io_service.run();
  238. io_service.reset();
  239. const char write_data[]
  240. = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  241. const boost::asio::const_buffer write_buf = boost::asio::buffer(write_data);
  242. std::size_t bytes_written = 0;
  243. while (bytes_written < sizeof(write_data))
  244. {
  245. client_socket.async_write_some(
  246. boost::asio::buffer(write_buf + bytes_written),
  247. bindns::bind(handle_write, _1, _2, &bytes_written));
  248. io_service.run();
  249. io_service.reset();
  250. client_socket.async_flush(
  251. bindns::bind(handle_flush, _1));
  252. io_service.run();
  253. io_service.reset();
  254. }
  255. char read_data[sizeof(write_data)];
  256. const boost::asio::mutable_buffer read_buf = boost::asio::buffer(read_data);
  257. std::size_t bytes_read = 0;
  258. while (bytes_read < sizeof(read_data))
  259. {
  260. server_socket.async_read_some(
  261. boost::asio::buffer(read_buf + bytes_read),
  262. bindns::bind(handle_read, _1, _2, &bytes_read));
  263. io_service.run();
  264. io_service.reset();
  265. }
  266. BOOST_ASIO_CHECK(bytes_written == sizeof(write_data));
  267. BOOST_ASIO_CHECK(bytes_read == sizeof(read_data));
  268. BOOST_ASIO_CHECK(memcmp(write_data, read_data, sizeof(write_data)) == 0);
  269. bytes_written = 0;
  270. while (bytes_written < sizeof(write_data))
  271. {
  272. server_socket.async_write_some(
  273. boost::asio::buffer(write_buf + bytes_written),
  274. bindns::bind(handle_write, _1, _2, &bytes_written));
  275. io_service.run();
  276. io_service.reset();
  277. server_socket.async_flush(
  278. bindns::bind(handle_flush, _1));
  279. io_service.run();
  280. io_service.reset();
  281. }
  282. bytes_read = 0;
  283. while (bytes_read < sizeof(read_data))
  284. {
  285. client_socket.async_read_some(
  286. boost::asio::buffer(read_buf + bytes_read),
  287. bindns::bind(handle_read, _1, _2, &bytes_read));
  288. io_service.run();
  289. io_service.reset();
  290. }
  291. BOOST_ASIO_CHECK(bytes_written == sizeof(write_data));
  292. BOOST_ASIO_CHECK(bytes_read == sizeof(read_data));
  293. BOOST_ASIO_CHECK(memcmp(write_data, read_data, sizeof(write_data)) == 0);
  294. server_socket.close();
  295. client_socket.async_read_some(boost::asio::buffer(read_buf), handle_read_eof);
  296. }
  297. BOOST_ASIO_TEST_SUITE
  298. (
  299. "buffered_stream",
  300. BOOST_ASIO_TEST_CASE(test_compile)
  301. BOOST_ASIO_TEST_CASE(test_sync_operations)
  302. BOOST_ASIO_TEST_CASE(test_async_operations)
  303. )