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