5 // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
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)
16 // Test that header file is self-contained.
17 #include <boost/asio/ip/address_v6.hpp>
19 #include "../unit_test.hpp"
22 //------------------------------------------------------------------------------
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.
29 namespace ip_address_v6_compile
{
33 using namespace boost::asio
;
34 namespace ip
= boost::asio::ip
;
38 boost::system::error_code ec
;
40 // address_v6 constructors.
43 const ip::address_v6::bytes_type const_bytes_value
= { { 0 } };
44 ip::address_v6
addr2(const_bytes_value
);
46 // address_v6 functions.
48 unsigned long scope_id
= addr1
.scope_id();
49 addr1
.scope_id(scope_id
);
51 bool b
= addr1
.is_unspecified();
54 b
= addr1
.is_loopback();
57 b
= addr1
.is_multicast();
60 b
= addr1
.is_link_local();
63 b
= addr1
.is_site_local();
66 b
= addr1
.is_v4_mapped();
69 #if !defined(BOOST_ASIO_NO_DEPRECATED)
70 b
= addr1
.is_v4_compatible();
72 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
74 b
= addr1
.is_multicast_node_local();
77 b
= addr1
.is_multicast_link_local();
80 b
= addr1
.is_multicast_site_local();
83 b
= addr1
.is_multicast_org_local();
86 b
= addr1
.is_multicast_global();
89 ip::address_v6::bytes_type bytes_value
= addr1
.to_bytes();
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)
97 #if !defined(BOOST_ASIO_NO_DEPRECATED)
98 ip::address_v4 addr3
= addr1
.to_v4();
99 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
101 // address_v6 static functions.
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)
110 addr1
= ip::address_v6::any();
112 addr1
= ip::address_v6::loopback();
114 #if !defined(BOOST_ASIO_NO_DEPRECATED)
115 addr1
= ip::address_v6::v4_mapped(addr3
);
117 addr1
= ip::address_v6::v4_compatible(addr3
);
118 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
120 // address_v6 comparisons.
122 b
= (addr1
== addr2
);
125 b
= (addr1
!= addr2
);
134 b
= (addr1
<= addr2
);
137 b
= (addr1
>= addr2
);
140 // address_v6 creation functions.
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)
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
);
166 std::ostringstream os
;
169 #if !defined(BOOST_NO_STD_WSTREAMBUF)
170 std::wostringstream wos
;
172 #endif // !defined(BOOST_NO_STD_WSTREAMBUF)
174 catch (std::exception
&)
179 } // namespace ip_address_v6_compile
181 //------------------------------------------------------------------------------
183 // ip_address_v6_runtime test
184 // ~~~~~~~~~~~~~~~~~~~~~~~~~~
185 // The following test checks that the various public member functions meet the
186 // necessary postconditions.
188 namespace ip_address_v6_runtime
{
192 using boost::asio::ip::address_v6
;
195 BOOST_ASIO_CHECK(a1
.is_unspecified());
196 BOOST_ASIO_CHECK(a1
.scope_id() == 0);
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);
221 BOOST_ASIO_CHECK(a3
.scope_id() == 12345);
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
);
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());
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());
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());
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());
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());
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)
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());
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());
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());
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());
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());
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());
397 BOOST_ASIO_CHECK(address_v6::loopback().is_loopback());
400 } // namespace ip_address_v6_runtime
402 //------------------------------------------------------------------------------
404 BOOST_ASIO_TEST_SUITE
407 BOOST_ASIO_TEST_CASE(ip_address_v6_compile::test
)
408 BOOST_ASIO_TEST_CASE(ip_address_v6_runtime::test
)