unicast.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // unicast.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/ip/unicast.hpp>
  16. #include <boost/asio/io_context.hpp>
  17. #include <boost/asio/ip/udp.hpp>
  18. #include "../unit_test.hpp"
  19. //------------------------------------------------------------------------------
  20. // ip_unicast_compile test
  21. // ~~~~~~~~~~~~~~~~~~~~~~~
  22. // The following test checks that all nested classes, enums and constants in
  23. // ip::unicast compile and link correctly. Runtime failures are ignored.
  24. namespace ip_unicast_compile {
  25. void test()
  26. {
  27. using namespace boost::asio;
  28. namespace ip = boost::asio::ip;
  29. try
  30. {
  31. io_context ioc;
  32. ip::udp::socket sock(ioc);
  33. // hops class.
  34. ip::unicast::hops hops1(1024);
  35. sock.set_option(hops1);
  36. ip::unicast::hops hops2;
  37. sock.get_option(hops2);
  38. hops1 = 1;
  39. (void)static_cast<int>(hops1.value());
  40. }
  41. catch (std::exception&)
  42. {
  43. }
  44. }
  45. } // namespace ip_unicast_compile
  46. //------------------------------------------------------------------------------
  47. // ip_unicast_runtime test
  48. // ~~~~~~~~~~~~~~~~~~~~~~~
  49. // The following test checks the runtime operation of the socket options defined
  50. // in the ip::unicast namespace.
  51. namespace ip_unicast_runtime {
  52. void test()
  53. {
  54. using namespace boost::asio;
  55. namespace ip = boost::asio::ip;
  56. io_context ioc;
  57. boost::system::error_code ec;
  58. ip::udp::endpoint ep_v4(ip::address_v4::loopback(), 0);
  59. ip::udp::socket sock_v4(ioc);
  60. sock_v4.open(ep_v4.protocol(), ec);
  61. sock_v4.bind(ep_v4, ec);
  62. bool have_v4 = !ec;
  63. ip::udp::endpoint ep_v6(ip::address_v6::loopback(), 0);
  64. ip::udp::socket sock_v6(ioc);
  65. sock_v6.open(ep_v6.protocol(), ec);
  66. sock_v6.bind(ep_v6, ec);
  67. bool have_v6 = !ec;
  68. BOOST_ASIO_CHECK(have_v4 || have_v6);
  69. // hops class.
  70. if (have_v4)
  71. {
  72. ip::unicast::hops hops1(1);
  73. BOOST_ASIO_CHECK(hops1.value() == 1);
  74. sock_v4.set_option(hops1, ec);
  75. #if defined(BOOST_ASIO_WINDOWS) && defined(UNDER_CE)
  76. // Option is not supported under Windows CE.
  77. BOOST_ASIO_CHECK_MESSAGE(ec == boost::asio::error::no_protocol_option,
  78. ec.value() << ", " << ec.message());
  79. #else // defined(BOOST_ASIO_WINDOWS) && defined(UNDER_CE)
  80. BOOST_ASIO_CHECK(!ec);
  81. #endif // defined(BOOST_ASIO_WINDOWS) && defined(UNDER_CE)
  82. ip::unicast::hops hops2;
  83. sock_v4.get_option(hops2, ec);
  84. #if defined(BOOST_ASIO_WINDOWS) && defined(UNDER_CE)
  85. // Option is not supported under Windows CE.
  86. BOOST_ASIO_CHECK_MESSAGE(ec == boost::asio::error::no_protocol_option,
  87. ec.value() << ", " << ec.message());
  88. #else // defined(BOOST_ASIO_WINDOWS) && defined(UNDER_CE)
  89. BOOST_ASIO_CHECK(!ec);
  90. BOOST_ASIO_CHECK(hops2.value() == 1);
  91. #endif // defined(BOOST_ASIO_WINDOWS) && defined(UNDER_CE)
  92. ip::unicast::hops hops3(255);
  93. BOOST_ASIO_CHECK(hops3.value() == 255);
  94. sock_v4.set_option(hops3, ec);
  95. #if defined(BOOST_ASIO_WINDOWS) && defined(UNDER_CE)
  96. // Option is not supported under Windows CE.
  97. BOOST_ASIO_CHECK_MESSAGE(ec == boost::asio::error::no_protocol_option,
  98. ec.value() << ", " << ec.message());
  99. #else // defined(BOOST_ASIO_WINDOWS) && defined(UNDER_CE)
  100. BOOST_ASIO_CHECK(!ec);
  101. #endif // defined(BOOST_ASIO_WINDOWS) && defined(UNDER_CE)
  102. ip::unicast::hops hops4;
  103. sock_v4.get_option(hops4, ec);
  104. #if defined(BOOST_ASIO_WINDOWS) && defined(UNDER_CE)
  105. // Option is not supported under Windows CE.
  106. BOOST_ASIO_CHECK_MESSAGE(ec == boost::asio::error::no_protocol_option,
  107. ec.value() << ", " << ec.message());
  108. #else // defined(BOOST_ASIO_WINDOWS) && defined(UNDER_CE)
  109. BOOST_ASIO_CHECK(!ec);
  110. BOOST_ASIO_CHECK(hops4.value() == 255);
  111. #endif // defined(BOOST_ASIO_WINDOWS) && defined(UNDER_CE)
  112. }
  113. if (have_v6)
  114. {
  115. ip::unicast::hops hops1(1);
  116. BOOST_ASIO_CHECK(hops1.value() == 1);
  117. sock_v6.set_option(hops1, ec);
  118. BOOST_ASIO_CHECK(!ec);
  119. ip::unicast::hops hops2;
  120. sock_v6.get_option(hops2, ec);
  121. BOOST_ASIO_CHECK(!ec);
  122. BOOST_ASIO_CHECK(hops2.value() == 1);
  123. ip::unicast::hops hops3(255);
  124. BOOST_ASIO_CHECK(hops3.value() == 255);
  125. sock_v6.set_option(hops3, ec);
  126. BOOST_ASIO_CHECK(!ec);
  127. ip::unicast::hops hops4;
  128. sock_v6.get_option(hops4, ec);
  129. BOOST_ASIO_CHECK(!ec);
  130. BOOST_ASIO_CHECK(hops4.value() == 255);
  131. }
  132. }
  133. } // namespace ip_unicast_runtime
  134. //------------------------------------------------------------------------------
  135. BOOST_ASIO_TEST_SUITE
  136. (
  137. "ip/unicast",
  138. BOOST_ASIO_TEST_CASE(ip_unicast_compile::test)
  139. BOOST_ASIO_TEST_CASE(ip_unicast_runtime::test)
  140. )