• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // address_v6.cpp
3 // ~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2020 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/address_v6.hpp>
18 
19 #include "../unit_test.hpp"
20 #include <sstream>
21 
22 //------------------------------------------------------------------------------
23 
24 // ip_address_v6_compile test
25 // ~~~~~~~~~~~~~~~~~~~~~~~~~~
26 // The following test checks that all public member functions on the class
27 // ip::address_v6 compile and link correctly. Runtime failures are ignored.
28 
29 namespace ip_address_v6_compile {
30 
test()31 void test()
32 {
33   using namespace boost::asio;
34   namespace ip = boost::asio::ip;
35 
36   try
37   {
38     boost::system::error_code ec;
39 
40     // address_v6 constructors.
41 
42     ip::address_v6 addr1;
43     const ip::address_v6::bytes_type const_bytes_value = { { 0 } };
44     ip::address_v6 addr2(const_bytes_value);
45 
46     // address_v6 functions.
47 
48     unsigned long scope_id = addr1.scope_id();
49     addr1.scope_id(scope_id);
50 
51     bool b = addr1.is_unspecified();
52     (void)b;
53 
54     b = addr1.is_loopback();
55     (void)b;
56 
57     b = addr1.is_multicast();
58     (void)b;
59 
60     b = addr1.is_link_local();
61     (void)b;
62 
63     b = addr1.is_site_local();
64     (void)b;
65 
66     b = addr1.is_v4_mapped();
67     (void)b;
68 
69 #if !defined(BOOST_ASIO_NO_DEPRECATED)
70     b = addr1.is_v4_compatible();
71     (void)b;
72 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
73 
74     b = addr1.is_multicast_node_local();
75     (void)b;
76 
77     b = addr1.is_multicast_link_local();
78     (void)b;
79 
80     b = addr1.is_multicast_site_local();
81     (void)b;
82 
83     b = addr1.is_multicast_org_local();
84     (void)b;
85 
86     b = addr1.is_multicast_global();
87     (void)b;
88 
89     ip::address_v6::bytes_type bytes_value = addr1.to_bytes();
90     (void)bytes_value;
91 
92     std::string string_value = addr1.to_string();
93 #if !defined(BOOST_ASIO_NO_DEPRECATED)
94     string_value = addr1.to_string(ec);
95 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
96 
97 #if !defined(BOOST_ASIO_NO_DEPRECATED)
98     ip::address_v4 addr3 = addr1.to_v4();
99 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
100 
101     // address_v6 static functions.
102 
103 #if !defined(BOOST_ASIO_NO_DEPRECATED)
104     addr1 = ip::address_v6::from_string("0::0");
105     addr1 = ip::address_v6::from_string("0::0", ec);
106     addr1 = ip::address_v6::from_string(string_value);
107     addr1 = ip::address_v6::from_string(string_value, ec);
108 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
109 
110     addr1 = ip::address_v6::any();
111 
112     addr1 = ip::address_v6::loopback();
113 
114 #if !defined(BOOST_ASIO_NO_DEPRECATED)
115     addr1 = ip::address_v6::v4_mapped(addr3);
116 
117     addr1 = ip::address_v6::v4_compatible(addr3);
118 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
119 
120     // address_v6 comparisons.
121 
122     b = (addr1 == addr2);
123     (void)b;
124 
125     b = (addr1 != addr2);
126     (void)b;
127 
128     b = (addr1 < addr2);
129     (void)b;
130 
131     b = (addr1 > addr2);
132     (void)b;
133 
134     b = (addr1 <= addr2);
135     (void)b;
136 
137     b = (addr1 >= addr2);
138     (void)b;
139 
140     // address_v6 creation functions.
141 
142     addr1 = ip::make_address_v6(const_bytes_value, scope_id);
143     addr1 = ip::make_address_v6("0::0");
144     addr1 = ip::make_address_v6("0::0", ec);
145     addr1 = ip::make_address_v6(string_value);
146     addr1 = ip::make_address_v6(string_value, ec);
147 #if defined(BOOST_ASIO_HAS_STRING_VIEW)
148 # if defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
149     std::string_view string_view_value("0::0");
150 # else // defined(BOOST_ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
151     std::experimental::string_view string_view_value("0::0");
152 # endif // defined(BOOST_ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
153     addr1 = ip::make_address_v6(string_view_value);
154     addr1 = ip::make_address_v6(string_view_value, ec);
155 #endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
156 
157     // address_v6 IPv4-mapped conversion.
158 #if defined(BOOST_ASIO_NO_DEPRECATED)
159     ip::address_v4 addr3;
160 #endif // defined(BOOST_ASIO_NO_DEPRECATED)
161     addr1 = ip::make_address_v6(ip::v4_mapped, addr3);
162     addr3 = ip::make_address_v4(ip::v4_mapped, addr1);
163 
164     // address_v6 I/O.
165 
166     std::ostringstream os;
167     os << addr1;
168 
169 #if !defined(BOOST_NO_STD_WSTREAMBUF)
170     std::wostringstream wos;
171     wos << addr1;
172 #endif // !defined(BOOST_NO_STD_WSTREAMBUF)
173   }
174   catch (std::exception&)
175   {
176   }
177 }
178 
179 } // namespace ip_address_v6_compile
180 
181 //------------------------------------------------------------------------------
182 
183 // ip_address_v6_runtime test
184 // ~~~~~~~~~~~~~~~~~~~~~~~~~~
185 // The following test checks that the various public member functions meet the
186 // necessary postconditions.
187 
188 namespace ip_address_v6_runtime {
189 
test()190 void test()
191 {
192   using boost::asio::ip::address_v6;
193 
194   address_v6 a1;
195   BOOST_ASIO_CHECK(a1.is_unspecified());
196   BOOST_ASIO_CHECK(a1.scope_id() == 0);
197 
198   address_v6::bytes_type b1 = {{ 1, 2, 3, 4, 5,
199     6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }};
200   address_v6 a2(b1, 12345);
201   BOOST_ASIO_CHECK(a2.to_bytes()[0] == 1);
202   BOOST_ASIO_CHECK(a2.to_bytes()[1] == 2);
203   BOOST_ASIO_CHECK(a2.to_bytes()[2] == 3);
204   BOOST_ASIO_CHECK(a2.to_bytes()[3] == 4);
205   BOOST_ASIO_CHECK(a2.to_bytes()[4] == 5);
206   BOOST_ASIO_CHECK(a2.to_bytes()[5] == 6);
207   BOOST_ASIO_CHECK(a2.to_bytes()[6] == 7);
208   BOOST_ASIO_CHECK(a2.to_bytes()[7] == 8);
209   BOOST_ASIO_CHECK(a2.to_bytes()[8] == 9);
210   BOOST_ASIO_CHECK(a2.to_bytes()[9] == 10);
211   BOOST_ASIO_CHECK(a2.to_bytes()[10] == 11);
212   BOOST_ASIO_CHECK(a2.to_bytes()[11] == 12);
213   BOOST_ASIO_CHECK(a2.to_bytes()[12] == 13);
214   BOOST_ASIO_CHECK(a2.to_bytes()[13] == 14);
215   BOOST_ASIO_CHECK(a2.to_bytes()[14] == 15);
216   BOOST_ASIO_CHECK(a2.to_bytes()[15] == 16);
217   BOOST_ASIO_CHECK(a2.scope_id() == 12345);
218 
219   address_v6 a3;
220   a3.scope_id(12345);
221   BOOST_ASIO_CHECK(a3.scope_id() == 12345);
222 
223   address_v6 unspecified_address;
224   address_v6::bytes_type loopback_bytes = {{ 0 }};
225   loopback_bytes[15] = 1;
226   address_v6 loopback_address(loopback_bytes);
227   address_v6::bytes_type link_local_bytes = {{ 0xFE, 0x80, 1 }};
228   address_v6 link_local_address(link_local_bytes);
229   address_v6::bytes_type site_local_bytes = {{ 0xFE, 0xC0, 1 }};
230   address_v6 site_local_address(site_local_bytes);
231   address_v6::bytes_type v4_mapped_bytes = {{ 0 }};
232   v4_mapped_bytes[10] = 0xFF, v4_mapped_bytes[11] = 0xFF;
233   v4_mapped_bytes[12] = 1, v4_mapped_bytes[13] = 2;
234   v4_mapped_bytes[14] = 3, v4_mapped_bytes[15] = 4;
235   address_v6 v4_mapped_address(v4_mapped_bytes);
236   address_v6::bytes_type v4_compat_bytes = {{ 0 }};
237   v4_compat_bytes[12] = 1, v4_compat_bytes[13] = 2;
238   v4_compat_bytes[14] = 3, v4_compat_bytes[15] = 4;
239   address_v6 v4_compat_address(v4_compat_bytes);
240   address_v6::bytes_type mcast_global_bytes = {{ 0xFF, 0x0E, 1 }};
241   address_v6 mcast_global_address(mcast_global_bytes);
242   address_v6::bytes_type mcast_link_local_bytes = {{ 0xFF, 0x02, 1 }};
243   address_v6 mcast_link_local_address(mcast_link_local_bytes);
244   address_v6::bytes_type mcast_node_local_bytes = {{ 0xFF, 0x01, 1 }};
245   address_v6 mcast_node_local_address(mcast_node_local_bytes);
246   address_v6::bytes_type mcast_org_local_bytes = {{ 0xFF, 0x08, 1 }};
247   address_v6 mcast_org_local_address(mcast_org_local_bytes);
248   address_v6::bytes_type mcast_site_local_bytes = {{ 0xFF, 0x05, 1 }};
249   address_v6 mcast_site_local_address(mcast_site_local_bytes);
250 
251   BOOST_ASIO_CHECK(!unspecified_address.is_loopback());
252   BOOST_ASIO_CHECK(loopback_address.is_loopback());
253   BOOST_ASIO_CHECK(!link_local_address.is_loopback());
254   BOOST_ASIO_CHECK(!site_local_address.is_loopback());
255   BOOST_ASIO_CHECK(!v4_mapped_address.is_loopback());
256   BOOST_ASIO_CHECK(!v4_compat_address.is_loopback());
257   BOOST_ASIO_CHECK(!mcast_global_address.is_loopback());
258   BOOST_ASIO_CHECK(!mcast_link_local_address.is_loopback());
259   BOOST_ASIO_CHECK(!mcast_node_local_address.is_loopback());
260   BOOST_ASIO_CHECK(!mcast_org_local_address.is_loopback());
261   BOOST_ASIO_CHECK(!mcast_site_local_address.is_loopback());
262 
263   BOOST_ASIO_CHECK(unspecified_address.is_unspecified());
264   BOOST_ASIO_CHECK(!loopback_address.is_unspecified());
265   BOOST_ASIO_CHECK(!link_local_address.is_unspecified());
266   BOOST_ASIO_CHECK(!site_local_address.is_unspecified());
267   BOOST_ASIO_CHECK(!v4_mapped_address.is_unspecified());
268   BOOST_ASIO_CHECK(!v4_compat_address.is_unspecified());
269   BOOST_ASIO_CHECK(!mcast_global_address.is_unspecified());
270   BOOST_ASIO_CHECK(!mcast_link_local_address.is_unspecified());
271   BOOST_ASIO_CHECK(!mcast_node_local_address.is_unspecified());
272   BOOST_ASIO_CHECK(!mcast_org_local_address.is_unspecified());
273   BOOST_ASIO_CHECK(!mcast_site_local_address.is_unspecified());
274 
275   BOOST_ASIO_CHECK(!unspecified_address.is_link_local());
276   BOOST_ASIO_CHECK(!loopback_address.is_link_local());
277   BOOST_ASIO_CHECK(link_local_address.is_link_local());
278   BOOST_ASIO_CHECK(!site_local_address.is_link_local());
279   BOOST_ASIO_CHECK(!v4_mapped_address.is_link_local());
280   BOOST_ASIO_CHECK(!v4_compat_address.is_link_local());
281   BOOST_ASIO_CHECK(!mcast_global_address.is_link_local());
282   BOOST_ASIO_CHECK(!mcast_link_local_address.is_link_local());
283   BOOST_ASIO_CHECK(!mcast_node_local_address.is_link_local());
284   BOOST_ASIO_CHECK(!mcast_org_local_address.is_link_local());
285   BOOST_ASIO_CHECK(!mcast_site_local_address.is_link_local());
286 
287   BOOST_ASIO_CHECK(!unspecified_address.is_site_local());
288   BOOST_ASIO_CHECK(!loopback_address.is_site_local());
289   BOOST_ASIO_CHECK(!link_local_address.is_site_local());
290   BOOST_ASIO_CHECK(site_local_address.is_site_local());
291   BOOST_ASIO_CHECK(!v4_mapped_address.is_site_local());
292   BOOST_ASIO_CHECK(!v4_compat_address.is_site_local());
293   BOOST_ASIO_CHECK(!mcast_global_address.is_site_local());
294   BOOST_ASIO_CHECK(!mcast_link_local_address.is_site_local());
295   BOOST_ASIO_CHECK(!mcast_node_local_address.is_site_local());
296   BOOST_ASIO_CHECK(!mcast_org_local_address.is_site_local());
297   BOOST_ASIO_CHECK(!mcast_site_local_address.is_site_local());
298 
299   BOOST_ASIO_CHECK(!unspecified_address.is_v4_mapped());
300   BOOST_ASIO_CHECK(!loopback_address.is_v4_mapped());
301   BOOST_ASIO_CHECK(!link_local_address.is_v4_mapped());
302   BOOST_ASIO_CHECK(!site_local_address.is_v4_mapped());
303   BOOST_ASIO_CHECK(v4_mapped_address.is_v4_mapped());
304   BOOST_ASIO_CHECK(!v4_compat_address.is_v4_mapped());
305   BOOST_ASIO_CHECK(!mcast_global_address.is_v4_mapped());
306   BOOST_ASIO_CHECK(!mcast_link_local_address.is_v4_mapped());
307   BOOST_ASIO_CHECK(!mcast_node_local_address.is_v4_mapped());
308   BOOST_ASIO_CHECK(!mcast_org_local_address.is_v4_mapped());
309   BOOST_ASIO_CHECK(!mcast_site_local_address.is_v4_mapped());
310 
311 #if !defined(BOOST_ASIO_NO_DEPRECATED)
312   BOOST_ASIO_CHECK(!unspecified_address.is_v4_compatible());
313   BOOST_ASIO_CHECK(!loopback_address.is_v4_compatible());
314   BOOST_ASIO_CHECK(!link_local_address.is_v4_compatible());
315   BOOST_ASIO_CHECK(!site_local_address.is_v4_compatible());
316   BOOST_ASIO_CHECK(!v4_mapped_address.is_v4_compatible());
317   BOOST_ASIO_CHECK(v4_compat_address.is_v4_compatible());
318   BOOST_ASIO_CHECK(!mcast_global_address.is_v4_compatible());
319   BOOST_ASIO_CHECK(!mcast_link_local_address.is_v4_compatible());
320   BOOST_ASIO_CHECK(!mcast_node_local_address.is_v4_compatible());
321   BOOST_ASIO_CHECK(!mcast_org_local_address.is_v4_compatible());
322   BOOST_ASIO_CHECK(!mcast_site_local_address.is_v4_compatible());
323 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
324 
325   BOOST_ASIO_CHECK(!unspecified_address.is_multicast());
326   BOOST_ASIO_CHECK(!loopback_address.is_multicast());
327   BOOST_ASIO_CHECK(!link_local_address.is_multicast());
328   BOOST_ASIO_CHECK(!site_local_address.is_multicast());
329   BOOST_ASIO_CHECK(!v4_mapped_address.is_multicast());
330   BOOST_ASIO_CHECK(!v4_compat_address.is_multicast());
331   BOOST_ASIO_CHECK(mcast_global_address.is_multicast());
332   BOOST_ASIO_CHECK(mcast_link_local_address.is_multicast());
333   BOOST_ASIO_CHECK(mcast_node_local_address.is_multicast());
334   BOOST_ASIO_CHECK(mcast_org_local_address.is_multicast());
335   BOOST_ASIO_CHECK(mcast_site_local_address.is_multicast());
336 
337   BOOST_ASIO_CHECK(!unspecified_address.is_multicast_global());
338   BOOST_ASIO_CHECK(!loopback_address.is_multicast_global());
339   BOOST_ASIO_CHECK(!link_local_address.is_multicast_global());
340   BOOST_ASIO_CHECK(!site_local_address.is_multicast_global());
341   BOOST_ASIO_CHECK(!v4_mapped_address.is_multicast_global());
342   BOOST_ASIO_CHECK(!v4_compat_address.is_multicast_global());
343   BOOST_ASIO_CHECK(mcast_global_address.is_multicast_global());
344   BOOST_ASIO_CHECK(!mcast_link_local_address.is_multicast_global());
345   BOOST_ASIO_CHECK(!mcast_node_local_address.is_multicast_global());
346   BOOST_ASIO_CHECK(!mcast_org_local_address.is_multicast_global());
347   BOOST_ASIO_CHECK(!mcast_site_local_address.is_multicast_global());
348 
349   BOOST_ASIO_CHECK(!unspecified_address.is_multicast_link_local());
350   BOOST_ASIO_CHECK(!loopback_address.is_multicast_link_local());
351   BOOST_ASIO_CHECK(!link_local_address.is_multicast_link_local());
352   BOOST_ASIO_CHECK(!site_local_address.is_multicast_link_local());
353   BOOST_ASIO_CHECK(!v4_mapped_address.is_multicast_link_local());
354   BOOST_ASIO_CHECK(!v4_compat_address.is_multicast_link_local());
355   BOOST_ASIO_CHECK(!mcast_global_address.is_multicast_link_local());
356   BOOST_ASIO_CHECK(mcast_link_local_address.is_multicast_link_local());
357   BOOST_ASIO_CHECK(!mcast_node_local_address.is_multicast_link_local());
358   BOOST_ASIO_CHECK(!mcast_org_local_address.is_multicast_link_local());
359   BOOST_ASIO_CHECK(!mcast_site_local_address.is_multicast_link_local());
360 
361   BOOST_ASIO_CHECK(!unspecified_address.is_multicast_node_local());
362   BOOST_ASIO_CHECK(!loopback_address.is_multicast_node_local());
363   BOOST_ASIO_CHECK(!link_local_address.is_multicast_node_local());
364   BOOST_ASIO_CHECK(!site_local_address.is_multicast_node_local());
365   BOOST_ASIO_CHECK(!v4_mapped_address.is_multicast_node_local());
366   BOOST_ASIO_CHECK(!v4_compat_address.is_multicast_node_local());
367   BOOST_ASIO_CHECK(!mcast_global_address.is_multicast_node_local());
368   BOOST_ASIO_CHECK(!mcast_link_local_address.is_multicast_node_local());
369   BOOST_ASIO_CHECK(mcast_node_local_address.is_multicast_node_local());
370   BOOST_ASIO_CHECK(!mcast_org_local_address.is_multicast_node_local());
371   BOOST_ASIO_CHECK(!mcast_site_local_address.is_multicast_node_local());
372 
373   BOOST_ASIO_CHECK(!unspecified_address.is_multicast_org_local());
374   BOOST_ASIO_CHECK(!loopback_address.is_multicast_org_local());
375   BOOST_ASIO_CHECK(!link_local_address.is_multicast_org_local());
376   BOOST_ASIO_CHECK(!site_local_address.is_multicast_org_local());
377   BOOST_ASIO_CHECK(!v4_mapped_address.is_multicast_org_local());
378   BOOST_ASIO_CHECK(!v4_compat_address.is_multicast_org_local());
379   BOOST_ASIO_CHECK(!mcast_global_address.is_multicast_org_local());
380   BOOST_ASIO_CHECK(!mcast_link_local_address.is_multicast_org_local());
381   BOOST_ASIO_CHECK(!mcast_node_local_address.is_multicast_org_local());
382   BOOST_ASIO_CHECK(mcast_org_local_address.is_multicast_org_local());
383   BOOST_ASIO_CHECK(!mcast_site_local_address.is_multicast_org_local());
384 
385   BOOST_ASIO_CHECK(!unspecified_address.is_multicast_site_local());
386   BOOST_ASIO_CHECK(!loopback_address.is_multicast_site_local());
387   BOOST_ASIO_CHECK(!link_local_address.is_multicast_site_local());
388   BOOST_ASIO_CHECK(!site_local_address.is_multicast_site_local());
389   BOOST_ASIO_CHECK(!v4_mapped_address.is_multicast_site_local());
390   BOOST_ASIO_CHECK(!v4_compat_address.is_multicast_site_local());
391   BOOST_ASIO_CHECK(!mcast_global_address.is_multicast_site_local());
392   BOOST_ASIO_CHECK(!mcast_link_local_address.is_multicast_site_local());
393   BOOST_ASIO_CHECK(!mcast_node_local_address.is_multicast_site_local());
394   BOOST_ASIO_CHECK(!mcast_org_local_address.is_multicast_site_local());
395   BOOST_ASIO_CHECK(mcast_site_local_address.is_multicast_site_local());
396 
397   BOOST_ASIO_CHECK(address_v6::loopback().is_loopback());
398 }
399 
400 } // namespace ip_address_v6_runtime
401 
402 //------------------------------------------------------------------------------
403 
404 BOOST_ASIO_TEST_SUITE
405 (
406   "ip/address_v6",
407   BOOST_ASIO_TEST_CASE(ip_address_v6_compile::test)
408   BOOST_ASIO_TEST_CASE(ip_address_v6_runtime::test)
409 )
410