seq_packet_protocol.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //
  2. // generic/seq_packet_protocol.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/generic/seq_packet_protocol.hpp>
  16. #include <cstring>
  17. #include <boost/asio/io_context.hpp>
  18. #include "../unit_test.hpp"
  19. #if defined(__cplusplus_cli) || defined(__cplusplus_winrt)
  20. # define generic cpp_generic
  21. #endif
  22. //------------------------------------------------------------------------------
  23. // generic_seq_packet_protocol_socket_compile test
  24. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  25. // The following test checks that all public member functions on the class
  26. // generic::seq_packet_socket::socket compile and link correctly. Runtime
  27. // failures are ignored.
  28. namespace generic_seq_packet_protocol_socket_compile {
  29. void connect_handler(const boost::system::error_code&)
  30. {
  31. }
  32. void send_handler(const boost::system::error_code&, std::size_t)
  33. {
  34. }
  35. void receive_handler(const boost::system::error_code&, std::size_t)
  36. {
  37. }
  38. void test()
  39. {
  40. using namespace boost::asio;
  41. namespace generic = boost::asio::generic;
  42. typedef generic::seq_packet_protocol spp;
  43. const int af_inet = BOOST_ASIO_OS_DEF(AF_INET);
  44. const int sock_seqpacket = BOOST_ASIO_OS_DEF(SOCK_SEQPACKET);
  45. try
  46. {
  47. io_context ioc;
  48. char mutable_char_buffer[128] = "";
  49. const char const_char_buffer[128] = "";
  50. const socket_base::message_flags in_flags = 0;
  51. socket_base::message_flags out_flags = 0;
  52. socket_base::send_buffer_size socket_option;
  53. socket_base::bytes_readable io_control_command;
  54. boost::system::error_code ec;
  55. // basic_seq_packet_socket constructors.
  56. spp::socket socket1(ioc);
  57. spp::socket socket2(ioc, spp(af_inet, 0));
  58. spp::socket socket3(ioc, spp::endpoint());
  59. #if !defined(BOOST_ASIO_WINDOWS_RUNTIME)
  60. spp::socket::native_handle_type native_socket1
  61. = ::socket(af_inet, sock_seqpacket, 0);
  62. spp::socket socket4(ioc, spp(af_inet, 0), native_socket1);
  63. #endif // !defined(BOOST_ASIO_WINDOWS_RUNTIME)
  64. #if defined(BOOST_ASIO_HAS_MOVE)
  65. spp::socket socket5(std::move(socket4));
  66. #endif // defined(BOOST_ASIO_HAS_MOVE)
  67. // basic_seq_packet_socket operators.
  68. #if defined(BOOST_ASIO_HAS_MOVE)
  69. socket1 = spp::socket(ioc);
  70. socket1 = std::move(socket2);
  71. #endif // defined(BOOST_ASIO_HAS_MOVE)
  72. // basic_io_object functions.
  73. spp::socket::executor_type ex = socket1.get_executor();
  74. (void)ex;
  75. // basic_socket functions.
  76. spp::socket::lowest_layer_type& lowest_layer = socket1.lowest_layer();
  77. (void)lowest_layer;
  78. socket1.open(spp(af_inet, 0));
  79. socket1.open(spp(af_inet, 0), ec);
  80. #if !defined(BOOST_ASIO_WINDOWS_RUNTIME)
  81. spp::socket::native_handle_type native_socket2
  82. = ::socket(af_inet, sock_seqpacket, 0);
  83. socket1.assign(spp(af_inet, 0), native_socket2);
  84. spp::socket::native_handle_type native_socket3
  85. = ::socket(af_inet, sock_seqpacket, 0);
  86. socket1.assign(spp(af_inet, 0), native_socket3, ec);
  87. #endif // !defined(BOOST_ASIO_WINDOWS_RUNTIME)
  88. bool is_open = socket1.is_open();
  89. (void)is_open;
  90. socket1.close();
  91. socket1.close(ec);
  92. spp::socket::native_handle_type native_socket4 = socket1.native_handle();
  93. (void)native_socket4;
  94. socket1.cancel();
  95. socket1.cancel(ec);
  96. bool at_mark1 = socket1.at_mark();
  97. (void)at_mark1;
  98. bool at_mark2 = socket1.at_mark(ec);
  99. (void)at_mark2;
  100. std::size_t available1 = socket1.available();
  101. (void)available1;
  102. std::size_t available2 = socket1.available(ec);
  103. (void)available2;
  104. socket1.bind(spp::endpoint());
  105. socket1.bind(spp::endpoint(), ec);
  106. socket1.connect(spp::endpoint());
  107. socket1.connect(spp::endpoint(), ec);
  108. socket1.async_connect(spp::endpoint(), connect_handler);
  109. socket1.set_option(socket_option);
  110. socket1.set_option(socket_option, ec);
  111. socket1.get_option(socket_option);
  112. socket1.get_option(socket_option, ec);
  113. socket1.io_control(io_control_command);
  114. socket1.io_control(io_control_command, ec);
  115. spp::endpoint endpoint1 = socket1.local_endpoint();
  116. (void)endpoint1;
  117. spp::endpoint endpoint2 = socket1.local_endpoint(ec);
  118. (void)endpoint2;
  119. spp::endpoint endpoint3 = socket1.remote_endpoint();
  120. (void)endpoint3;
  121. spp::endpoint endpoint4 = socket1.remote_endpoint(ec);
  122. (void)endpoint4;
  123. socket1.shutdown(socket_base::shutdown_both);
  124. socket1.shutdown(socket_base::shutdown_both, ec);
  125. // basic_seq_packet_socket functions.
  126. socket1.send(buffer(mutable_char_buffer), in_flags);
  127. socket1.send(buffer(const_char_buffer), in_flags);
  128. socket1.send(null_buffers(), in_flags);
  129. socket1.send(buffer(mutable_char_buffer), in_flags, ec);
  130. socket1.send(buffer(const_char_buffer), in_flags, ec);
  131. socket1.send(null_buffers(), in_flags, ec);
  132. socket1.async_send(buffer(mutable_char_buffer), in_flags, send_handler);
  133. socket1.async_send(buffer(const_char_buffer), in_flags, send_handler);
  134. socket1.async_send(null_buffers(), in_flags, send_handler);
  135. socket1.receive(buffer(mutable_char_buffer), out_flags);
  136. socket1.receive(null_buffers(), out_flags);
  137. socket1.receive(buffer(mutable_char_buffer), in_flags, out_flags);
  138. socket1.receive(null_buffers(), in_flags, out_flags);
  139. socket1.receive(buffer(mutable_char_buffer), in_flags, out_flags, ec);
  140. socket1.receive(null_buffers(), in_flags, out_flags, ec);
  141. socket1.async_receive(buffer(mutable_char_buffer), out_flags,
  142. receive_handler);
  143. socket1.async_receive(null_buffers(), out_flags, receive_handler);
  144. socket1.async_receive(buffer(mutable_char_buffer), in_flags,
  145. out_flags, receive_handler);
  146. socket1.async_receive(null_buffers(), in_flags, out_flags, receive_handler);
  147. }
  148. catch (std::exception&)
  149. {
  150. }
  151. }
  152. } // namespace generic_seq_packet_protocol_socket_compile
  153. //------------------------------------------------------------------------------
  154. BOOST_ASIO_TEST_SUITE
  155. (
  156. "generic/seq_packet_protocol",
  157. BOOST_ASIO_TEST_CASE(generic_seq_packet_protocol_socket_compile::test)
  158. )