• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // address_v6.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/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 #if defined(BOOST_ASIO_HAS_STD_HASH)
175     std::size_t hash1 = std::hash<ip::address_v6>()(addr1);
176     (void)hash1;
177 #endif // defined(BOOST_ASIO_HAS_STD_HASH)
178   }
179   catch (std::exception&)
180   {
181   }
182 }
183 
184 } // namespace ip_address_v6_compile
185 
186 //------------------------------------------------------------------------------
187 
188 // ip_address_v6_runtime test
189 // ~~~~~~~~~~~~~~~~~~~~~~~~~~
190 // The following test checks that the various public member functions meet the
191 // necessary postconditions.
192 
193 namespace ip_address_v6_runtime {
194 
test()195 void test()
196 {
197   using boost::asio::ip::address_v6;
198 
199   address_v6 a1;
200   BOOST_ASIO_CHECK(a1.is_unspecified());
201   BOOST_ASIO_CHECK(a1.scope_id() == 0);
202 
203   address_v6::bytes_type b1 = {{ 1, 2, 3, 4, 5,
204     6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }};
205   address_v6 a2(b1, 12345);
206   BOOST_ASIO_CHECK(a2.to_bytes()[0] == 1);
207   BOOST_ASIO_CHECK(a2.to_bytes()[1] == 2);
208   BOOST_ASIO_CHECK(a2.to_bytes()[2] == 3);
209   BOOST_ASIO_CHECK(a2.to_bytes()[3] == 4);
210   BOOST_ASIO_CHECK(a2.to_bytes()[4] == 5);
211   BOOST_ASIO_CHECK(a2.to_bytes()[5] == 6);
212   BOOST_ASIO_CHECK(a2.to_bytes()[6] == 7);
213   BOOST_ASIO_CHECK(a2.to_bytes()[7] == 8);
214   BOOST_ASIO_CHECK(a2.to_bytes()[8] == 9);
215   BOOST_ASIO_CHECK(a2.to_bytes()[9] == 10);
216   BOOST_ASIO_CHECK(a2.to_bytes()[10] == 11);
217   BOOST_ASIO_CHECK(a2.to_bytes()[11] == 12);
218   BOOST_ASIO_CHECK(a2.to_bytes()[12] == 13);
219   BOOST_ASIO_CHECK(a2.to_bytes()[13] == 14);
220   BOOST_ASIO_CHECK(a2.to_bytes()[14] == 15);
221   BOOST_ASIO_CHECK(a2.to_bytes()[15] == 16);
222   BOOST_ASIO_CHECK(a2.scope_id() == 12345);
223 
224   address_v6 a3;
225   a3.scope_id(12345);
226   BOOST_ASIO_CHECK(a3.scope_id() == 12345);
227 
228   address_v6 unspecified_address;
229   address_v6::bytes_type loopback_bytes = {{ 0 }};
230   loopback_bytes[15] = 1;
231   address_v6 loopback_address(loopback_bytes);
232   address_v6::bytes_type link_local_bytes = {{ 0xFE, 0x80, 1 }};
233   address_v6 link_local_address(link_local_bytes);
234   address_v6::bytes_type site_local_bytes = {{ 0xFE, 0xC0, 1 }};
235   address_v6 site_local_address(site_local_bytes);
236   address_v6::bytes_type v4_mapped_bytes = {{ 0 }};
237   v4_mapped_bytes[10] = 0xFF, v4_mapped_bytes[11] = 0xFF;
238   v4_mapped_bytes[12] = 1, v4_mapped_bytes[13] = 2;
239   v4_mapped_bytes[14] = 3, v4_mapped_bytes[15] = 4;
240   address_v6 v4_mapped_address(v4_mapped_bytes);
241   address_v6::bytes_type v4_compat_bytes = {{ 0 }};
242   v4_compat_bytes[12] = 1, v4_compat_bytes[13] = 2;
243   v4_compat_bytes[14] = 3, v4_compat_bytes[15] = 4;
244   address_v6 v4_compat_address(v4_compat_bytes);
245   address_v6::bytes_type mcast_global_bytes = {{ 0xFF, 0x0E, 1 }};
246   address_v6 mcast_global_address(mcast_global_bytes);
247   address_v6::bytes_type mcast_link_local_bytes = {{ 0xFF, 0x02, 1 }};
248   address_v6 mcast_link_local_address(mcast_link_local_bytes);
249   address_v6::bytes_type mcast_node_local_bytes = {{ 0xFF, 0x01, 1 }};
250   address_v6 mcast_node_local_address(mcast_node_local_bytes);
251   address_v6::bytes_type mcast_org_local_bytes = {{ 0xFF, 0x08, 1 }};
252   address_v6 mcast_org_local_address(mcast_org_local_bytes);
253   address_v6::bytes_type mcast_site_local_bytes = {{ 0xFF, 0x05, 1 }};
254   address_v6 mcast_site_local_address(mcast_site_local_bytes);
255 
256   BOOST_ASIO_CHECK(!unspecified_address.is_loopback());
257   BOOST_ASIO_CHECK(loopback_address.is_loopback());
258   BOOST_ASIO_CHECK(!link_local_address.is_loopback());
259   BOOST_ASIO_CHECK(!site_local_address.is_loopback());
260   BOOST_ASIO_CHECK(!v4_mapped_address.is_loopback());
261   BOOST_ASIO_CHECK(!v4_compat_address.is_loopback());
262   BOOST_ASIO_CHECK(!mcast_global_address.is_loopback());
263   BOOST_ASIO_CHECK(!mcast_link_local_address.is_loopback());
264   BOOST_ASIO_CHECK(!mcast_node_local_address.is_loopback());
265   BOOST_ASIO_CHECK(!mcast_org_local_address.is_loopback());
266   BOOST_ASIO_CHECK(!mcast_site_local_address.is_loopback());
267 
268   BOOST_ASIO_CHECK(unspecified_address.is_unspecified());
269   BOOST_ASIO_CHECK(!loopback_address.is_unspecified());
270   BOOST_ASIO_CHECK(!link_local_address.is_unspecified());
271   BOOST_ASIO_CHECK(!site_local_address.is_unspecified());
272   BOOST_ASIO_CHECK(!v4_mapped_address.is_unspecified());
273   BOOST_ASIO_CHECK(!v4_compat_address.is_unspecified());
274   BOOST_ASIO_CHECK(!mcast_global_address.is_unspecified());
275   BOOST_ASIO_CHECK(!mcast_link_local_address.is_unspecified());
276   BOOST_ASIO_CHECK(!mcast_node_local_address.is_unspecified());
277   BOOST_ASIO_CHECK(!mcast_org_local_address.is_unspecified());
278   BOOST_ASIO_CHECK(!mcast_site_local_address.is_unspecified());
279 
280   BOOST_ASIO_CHECK(!unspecified_address.is_link_local());
281   BOOST_ASIO_CHECK(!loopback_address.is_link_local());
282   BOOST_ASIO_CHECK(link_local_address.is_link_local());
283   BOOST_ASIO_CHECK(!site_local_address.is_link_local());
284   BOOST_ASIO_CHECK(!v4_mapped_address.is_link_local());
285   BOOST_ASIO_CHECK(!v4_compat_address.is_link_local());
286   BOOST_ASIO_CHECK(!mcast_global_address.is_link_local());
287   BOOST_ASIO_CHECK(!mcast_link_local_address.is_link_local());
288   BOOST_ASIO_CHECK(!mcast_node_local_address.is_link_local());
289   BOOST_ASIO_CHECK(!mcast_org_local_address.is_link_local());
290   BOOST_ASIO_CHECK(!mcast_site_local_address.is_link_local());
291 
292   BOOST_ASIO_CHECK(!unspecified_address.is_site_local());
293   BOOST_ASIO_CHECK(!loopback_address.is_site_local());
294   BOOST_ASIO_CHECK(!link_local_address.is_site_local());
295   BOOST_ASIO_CHECK(site_local_address.is_site_local());
296   BOOST_ASIO_CHECK(!v4_mapped_address.is_site_local());
297   BOOST_ASIO_CHECK(!v4_compat_address.is_site_local());
298   BOOST_ASIO_CHECK(!mcast_global_address.is_site_local());
299   BOOST_ASIO_CHECK(!mcast_link_local_address.is_site_local());
300   BOOST_ASIO_CHECK(!mcast_node_local_address.is_site_local());
301   BOOST_ASIO_CHECK(!mcast_org_local_address.is_site_local());
302   BOOST_ASIO_CHECK(!mcast_site_local_address.is_site_local());
303 
304   BOOST_ASIO_CHECK(!unspecified_address.is_v4_mapped());
305   BOOST_ASIO_CHECK(!loopback_address.is_v4_mapped());
306   BOOST_ASIO_CHECK(!link_local_address.is_v4_mapped());
307   BOOST_ASIO_CHECK(!site_local_address.is_v4_mapped());
308   BOOST_ASIO_CHECK(v4_mapped_address.is_v4_mapped());
309   BOOST_ASIO_CHECK(!v4_compat_address.is_v4_mapped());
310   BOOST_ASIO_CHECK(!mcast_global_address.is_v4_mapped());
311   BOOST_ASIO_CHECK(!mcast_link_local_address.is_v4_mapped());
312   BOOST_ASIO_CHECK(!mcast_node_local_address.is_v4_mapped());
313   BOOST_ASIO_CHECK(!mcast_org_local_address.is_v4_mapped());
314   BOOST_ASIO_CHECK(!mcast_site_local_address.is_v4_mapped());
315 
316 #if !defined(BOOST_ASIO_NO_DEPRECATED)
317   BOOST_ASIO_CHECK(!unspecified_address.is_v4_compatible());
318   BOOST_ASIO_CHECK(!loopback_address.is_v4_compatible());
319   BOOST_ASIO_CHECK(!link_local_address.is_v4_compatible());
320   BOOST_ASIO_CHECK(!site_local_address.is_v4_compatible());
321   BOOST_ASIO_CHECK(!v4_mapped_address.is_v4_compatible());
322   BOOST_ASIO_CHECK(v4_compat_address.is_v4_compatible());
323   BOOST_ASIO_CHECK(!mcast_global_address.is_v4_compatible());
324   BOOST_ASIO_CHECK(!mcast_link_local_address.is_v4_compatible());
325   BOOST_ASIO_CHECK(!mcast_node_local_address.is_v4_compatible());
326   BOOST_ASIO_CHECK(!mcast_org_local_address.is_v4_compatible());
327   BOOST_ASIO_CHECK(!mcast_site_local_address.is_v4_compatible());
328 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
329 
330   BOOST_ASIO_CHECK(!unspecified_address.is_multicast());
331   BOOST_ASIO_CHECK(!loopback_address.is_multicast());
332   BOOST_ASIO_CHECK(!link_local_address.is_multicast());
333   BOOST_ASIO_CHECK(!site_local_address.is_multicast());
334   BOOST_ASIO_CHECK(!v4_mapped_address.is_multicast());
335   BOOST_ASIO_CHECK(!v4_compat_address.is_multicast());
336   BOOST_ASIO_CHECK(mcast_global_address.is_multicast());
337   BOOST_ASIO_CHECK(mcast_link_local_address.is_multicast());
338   BOOST_ASIO_CHECK(mcast_node_local_address.is_multicast());
339   BOOST_ASIO_CHECK(mcast_org_local_address.is_multicast());
340   BOOST_ASIO_CHECK(mcast_site_local_address.is_multicast());
341 
342   BOOST_ASIO_CHECK(!unspecified_address.is_multicast_global());
343   BOOST_ASIO_CHECK(!loopback_address.is_multicast_global());
344   BOOST_ASIO_CHECK(!link_local_address.is_multicast_global());
345   BOOST_ASIO_CHECK(!site_local_address.is_multicast_global());
346   BOOST_ASIO_CHECK(!v4_mapped_address.is_multicast_global());
347   BOOST_ASIO_CHECK(!v4_compat_address.is_multicast_global());
348   BOOST_ASIO_CHECK(mcast_global_address.is_multicast_global());
349   BOOST_ASIO_CHECK(!mcast_link_local_address.is_multicast_global());
350   BOOST_ASIO_CHECK(!mcast_node_local_address.is_multicast_global());
351   BOOST_ASIO_CHECK(!mcast_org_local_address.is_multicast_global());
352   BOOST_ASIO_CHECK(!mcast_site_local_address.is_multicast_global());
353 
354   BOOST_ASIO_CHECK(!unspecified_address.is_multicast_link_local());
355   BOOST_ASIO_CHECK(!loopback_address.is_multicast_link_local());
356   BOOST_ASIO_CHECK(!link_local_address.is_multicast_link_local());
357   BOOST_ASIO_CHECK(!site_local_address.is_multicast_link_local());
358   BOOST_ASIO_CHECK(!v4_mapped_address.is_multicast_link_local());
359   BOOST_ASIO_CHECK(!v4_compat_address.is_multicast_link_local());
360   BOOST_ASIO_CHECK(!mcast_global_address.is_multicast_link_local());
361   BOOST_ASIO_CHECK(mcast_link_local_address.is_multicast_link_local());
362   BOOST_ASIO_CHECK(!mcast_node_local_address.is_multicast_link_local());
363   BOOST_ASIO_CHECK(!mcast_org_local_address.is_multicast_link_local());
364   BOOST_ASIO_CHECK(!mcast_site_local_address.is_multicast_link_local());
365 
366   BOOST_ASIO_CHECK(!unspecified_address.is_multicast_node_local());
367   BOOST_ASIO_CHECK(!loopback_address.is_multicast_node_local());
368   BOOST_ASIO_CHECK(!link_local_address.is_multicast_node_local());
369   BOOST_ASIO_CHECK(!site_local_address.is_multicast_node_local());
370   BOOST_ASIO_CHECK(!v4_mapped_address.is_multicast_node_local());
371   BOOST_ASIO_CHECK(!v4_compat_address.is_multicast_node_local());
372   BOOST_ASIO_CHECK(!mcast_global_address.is_multicast_node_local());
373   BOOST_ASIO_CHECK(!mcast_link_local_address.is_multicast_node_local());
374   BOOST_ASIO_CHECK(mcast_node_local_address.is_multicast_node_local());
375   BOOST_ASIO_CHECK(!mcast_org_local_address.is_multicast_node_local());
376   BOOST_ASIO_CHECK(!mcast_site_local_address.is_multicast_node_local());
377 
378   BOOST_ASIO_CHECK(!unspecified_address.is_multicast_org_local());
379   BOOST_ASIO_CHECK(!loopback_address.is_multicast_org_local());
380   BOOST_ASIO_CHECK(!link_local_address.is_multicast_org_local());
381   BOOST_ASIO_CHECK(!site_local_address.is_multicast_org_local());
382   BOOST_ASIO_CHECK(!v4_mapped_address.is_multicast_org_local());
383   BOOST_ASIO_CHECK(!v4_compat_address.is_multicast_org_local());
384   BOOST_ASIO_CHECK(!mcast_global_address.is_multicast_org_local());
385   BOOST_ASIO_CHECK(!mcast_link_local_address.is_multicast_org_local());
386   BOOST_ASIO_CHECK(!mcast_node_local_address.is_multicast_org_local());
387   BOOST_ASIO_CHECK(mcast_org_local_address.is_multicast_org_local());
388   BOOST_ASIO_CHECK(!mcast_site_local_address.is_multicast_org_local());
389 
390   BOOST_ASIO_CHECK(!unspecified_address.is_multicast_site_local());
391   BOOST_ASIO_CHECK(!loopback_address.is_multicast_site_local());
392   BOOST_ASIO_CHECK(!link_local_address.is_multicast_site_local());
393   BOOST_ASIO_CHECK(!site_local_address.is_multicast_site_local());
394   BOOST_ASIO_CHECK(!v4_mapped_address.is_multicast_site_local());
395   BOOST_ASIO_CHECK(!v4_compat_address.is_multicast_site_local());
396   BOOST_ASIO_CHECK(!mcast_global_address.is_multicast_site_local());
397   BOOST_ASIO_CHECK(!mcast_link_local_address.is_multicast_site_local());
398   BOOST_ASIO_CHECK(!mcast_node_local_address.is_multicast_site_local());
399   BOOST_ASIO_CHECK(!mcast_org_local_address.is_multicast_site_local());
400   BOOST_ASIO_CHECK(mcast_site_local_address.is_multicast_site_local());
401 
402   BOOST_ASIO_CHECK(address_v6::loopback().is_loopback());
403 }
404 
405 } // namespace ip_address_v6_runtime
406 
407 //------------------------------------------------------------------------------
408 
409 BOOST_ASIO_TEST_SUITE
410 (
411   "ip/address_v6",
412   BOOST_ASIO_TEST_CASE(ip_address_v6_compile::test)
413   BOOST_ASIO_TEST_CASE(ip_address_v6_runtime::test)
414 )
415