address_v6.cpp 14.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
//
// address_v6.cpp
// ~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

// Disable autolinking for unit tests.
#if !defined(BOOST_ALL_NO_LIB)
#define BOOST_ALL_NO_LIB 1
#endif // !defined(BOOST_ALL_NO_LIB)

// Test that header file is self-contained.
#include "asio/ip/address_v6.hpp"

#include "../unit_test.hpp"
#include <sstream>

//------------------------------------------------------------------------------

// ip_address_v6_compile test
// ~~~~~~~~~~~~~~~~~~~~~~~~~~
// The following test checks that all public member functions on the class
// ip::address_v6 compile and link correctly. Runtime failures are ignored.

namespace ip_address_v6_compile {

void test()
{
  using namespace asio;
  namespace ip = asio::ip;

  try
  {
    asio::error_code ec;

    // address_v6 constructors.

    ip::address_v6 addr1;
    const ip::address_v6::bytes_type const_bytes_value = { { 0 } };
    ip::address_v6 addr2(const_bytes_value);

    // address_v6 functions.

    unsigned long scope_id = addr1.scope_id();
    addr1.scope_id(scope_id);

    bool b = addr1.is_unspecified();
    (void)b;

    b = addr1.is_loopback();
    (void)b;

    b = addr1.is_multicast();
    (void)b;

    b = addr1.is_link_local();
    (void)b;

    b = addr1.is_site_local();
    (void)b;

    b = addr1.is_v4_mapped();
    (void)b;

#if !defined(ASIO_NO_DEPRECATED)
    b = addr1.is_v4_compatible();
    (void)b;
#endif // !defined(ASIO_NO_DEPRECATED)

    b = addr1.is_multicast_node_local();
    (void)b;

    b = addr1.is_multicast_link_local();
    (void)b;

    b = addr1.is_multicast_site_local();
    (void)b;

    b = addr1.is_multicast_org_local();
    (void)b;

    b = addr1.is_multicast_global();
    (void)b;

    ip::address_v6::bytes_type bytes_value = addr1.to_bytes();
    (void)bytes_value;

    std::string string_value = addr1.to_string();
#if !defined(ASIO_NO_DEPRECATED)
    string_value = addr1.to_string(ec);
#endif // !defined(ASIO_NO_DEPRECATED)

#if !defined(ASIO_NO_DEPRECATED)
    ip::address_v4 addr3 = addr1.to_v4();
#endif // !defined(ASIO_NO_DEPRECATED)

    // address_v6 static functions.

#if !defined(ASIO_NO_DEPRECATED)
    addr1 = ip::address_v6::from_string("0::0");
    addr1 = ip::address_v6::from_string("0::0", ec);
    addr1 = ip::address_v6::from_string(string_value);
    addr1 = ip::address_v6::from_string(string_value, ec);
#endif // !defined(ASIO_NO_DEPRECATED)

    addr1 = ip::address_v6::any();

    addr1 = ip::address_v6::loopback();

#if !defined(ASIO_NO_DEPRECATED)
    addr1 = ip::address_v6::v4_mapped(addr3);

    addr1 = ip::address_v6::v4_compatible(addr3);
#endif // !defined(ASIO_NO_DEPRECATED)

    // address_v6 comparisons.

    b = (addr1 == addr2);
    (void)b;

    b = (addr1 != addr2);
    (void)b;

    b = (addr1 < addr2);
    (void)b;

    b = (addr1 > addr2);
    (void)b;

    b = (addr1 <= addr2);
    (void)b;

    b = (addr1 >= addr2);
    (void)b;

    // address_v6 creation functions.

    addr1 = ip::make_address_v6(const_bytes_value, scope_id);
    addr1 = ip::make_address_v6("0::0");
    addr1 = ip::make_address_v6("0::0", ec);
    addr1 = ip::make_address_v6(string_value);
    addr1 = ip::make_address_v6(string_value, ec);
#if defined(ASIO_HAS_STRING_VIEW)
# if defined(ASIO_HAS_STD_STRING_VIEW)
    std::string_view string_view_value("0::0");
# else // defined(ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
    std::experimental::string_view string_view_value("0::0");
# endif // defined(ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
    addr1 = ip::make_address_v6(string_view_value);
    addr1 = ip::make_address_v6(string_view_value, ec);
#endif // defined(ASIO_HAS_STRING_VIEW)

    // address_v6 IPv4-mapped conversion.
#if defined(ASIO_NO_DEPRECATED)
    ip::address_v4 addr3;
#endif // defined(ASIO_NO_DEPRECATED)
    addr1 = ip::make_address_v6(ip::v4_mapped, addr3);
    addr3 = ip::make_address_v4(ip::v4_mapped, addr1);

    // address_v6 I/O.

    std::ostringstream os;
    os << addr1;

#if !defined(BOOST_NO_STD_WSTREAMBUF)
    std::wostringstream wos;
    wos << addr1;
#endif // !defined(BOOST_NO_STD_WSTREAMBUF)
  }
  catch (std::exception&)
  {
  }
}

} // namespace ip_address_v6_compile

//------------------------------------------------------------------------------

// ip_address_v6_runtime test
// ~~~~~~~~~~~~~~~~~~~~~~~~~~
// The following test checks that the various public member functions meet the
// necessary postconditions.

namespace ip_address_v6_runtime {

void test()
{
  using asio::ip::address_v6;

  address_v6 a1;
  ASIO_CHECK(a1.is_unspecified());
  ASIO_CHECK(a1.scope_id() == 0);

  address_v6::bytes_type b1 = {{ 1, 2, 3, 4, 5,
    6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }};
  address_v6 a2(b1, 12345);
  ASIO_CHECK(a2.to_bytes()[0] == 1);
  ASIO_CHECK(a2.to_bytes()[1] == 2);
  ASIO_CHECK(a2.to_bytes()[2] == 3);
  ASIO_CHECK(a2.to_bytes()[3] == 4);
  ASIO_CHECK(a2.to_bytes()[4] == 5);
  ASIO_CHECK(a2.to_bytes()[5] == 6);
  ASIO_CHECK(a2.to_bytes()[6] == 7);
  ASIO_CHECK(a2.to_bytes()[7] == 8);
  ASIO_CHECK(a2.to_bytes()[8] == 9);
  ASIO_CHECK(a2.to_bytes()[9] == 10);
  ASIO_CHECK(a2.to_bytes()[10] == 11);
  ASIO_CHECK(a2.to_bytes()[11] == 12);
  ASIO_CHECK(a2.to_bytes()[12] == 13);
  ASIO_CHECK(a2.to_bytes()[13] == 14);
  ASIO_CHECK(a2.to_bytes()[14] == 15);
  ASIO_CHECK(a2.to_bytes()[15] == 16);
  ASIO_CHECK(a2.scope_id() == 12345);

  address_v6 a3;
  a3.scope_id(12345);
  ASIO_CHECK(a3.scope_id() == 12345);

  address_v6 unspecified_address;
  address_v6::bytes_type loopback_bytes = {{ 0 }};
  loopback_bytes[15] = 1;
  address_v6 loopback_address(loopback_bytes);
  address_v6::bytes_type link_local_bytes = {{ 0xFE, 0x80, 1 }};
  address_v6 link_local_address(link_local_bytes);
  address_v6::bytes_type site_local_bytes = {{ 0xFE, 0xC0, 1 }};
  address_v6 site_local_address(site_local_bytes);
  address_v6::bytes_type v4_mapped_bytes = {{ 0 }};
  v4_mapped_bytes[10] = 0xFF, v4_mapped_bytes[11] = 0xFF;
  v4_mapped_bytes[12] = 1, v4_mapped_bytes[13] = 2;
  v4_mapped_bytes[14] = 3, v4_mapped_bytes[15] = 4;
  address_v6 v4_mapped_address(v4_mapped_bytes);
  address_v6::bytes_type v4_compat_bytes = {{ 0 }};
  v4_compat_bytes[12] = 1, v4_compat_bytes[13] = 2;
  v4_compat_bytes[14] = 3, v4_compat_bytes[15] = 4;
  address_v6 v4_compat_address(v4_compat_bytes);
  address_v6::bytes_type mcast_global_bytes = {{ 0xFF, 0x0E, 1 }};
  address_v6 mcast_global_address(mcast_global_bytes);
  address_v6::bytes_type mcast_link_local_bytes = {{ 0xFF, 0x02, 1 }};
  address_v6 mcast_link_local_address(mcast_link_local_bytes);
  address_v6::bytes_type mcast_node_local_bytes = {{ 0xFF, 0x01, 1 }};
  address_v6 mcast_node_local_address(mcast_node_local_bytes);
  address_v6::bytes_type mcast_org_local_bytes = {{ 0xFF, 0x08, 1 }};
  address_v6 mcast_org_local_address(mcast_org_local_bytes);
  address_v6::bytes_type mcast_site_local_bytes = {{ 0xFF, 0x05, 1 }};
  address_v6 mcast_site_local_address(mcast_site_local_bytes);

  ASIO_CHECK(!unspecified_address.is_loopback());
  ASIO_CHECK(loopback_address.is_loopback());
  ASIO_CHECK(!link_local_address.is_loopback());
  ASIO_CHECK(!site_local_address.is_loopback());
  ASIO_CHECK(!v4_mapped_address.is_loopback());
  ASIO_CHECK(!v4_compat_address.is_loopback());
  ASIO_CHECK(!mcast_global_address.is_loopback());
  ASIO_CHECK(!mcast_link_local_address.is_loopback());
  ASIO_CHECK(!mcast_node_local_address.is_loopback());
  ASIO_CHECK(!mcast_org_local_address.is_loopback());
  ASIO_CHECK(!mcast_site_local_address.is_loopback());

  ASIO_CHECK(unspecified_address.is_unspecified());
  ASIO_CHECK(!loopback_address.is_unspecified());
  ASIO_CHECK(!link_local_address.is_unspecified());
  ASIO_CHECK(!site_local_address.is_unspecified());
  ASIO_CHECK(!v4_mapped_address.is_unspecified());
  ASIO_CHECK(!v4_compat_address.is_unspecified());
  ASIO_CHECK(!mcast_global_address.is_unspecified());
  ASIO_CHECK(!mcast_link_local_address.is_unspecified());
  ASIO_CHECK(!mcast_node_local_address.is_unspecified());
  ASIO_CHECK(!mcast_org_local_address.is_unspecified());
  ASIO_CHECK(!mcast_site_local_address.is_unspecified());

  ASIO_CHECK(!unspecified_address.is_link_local());
  ASIO_CHECK(!loopback_address.is_link_local());
  ASIO_CHECK(link_local_address.is_link_local());
  ASIO_CHECK(!site_local_address.is_link_local());
  ASIO_CHECK(!v4_mapped_address.is_link_local());
  ASIO_CHECK(!v4_compat_address.is_link_local());
  ASIO_CHECK(!mcast_global_address.is_link_local());
  ASIO_CHECK(!mcast_link_local_address.is_link_local());
  ASIO_CHECK(!mcast_node_local_address.is_link_local());
  ASIO_CHECK(!mcast_org_local_address.is_link_local());
  ASIO_CHECK(!mcast_site_local_address.is_link_local());

  ASIO_CHECK(!unspecified_address.is_site_local());
  ASIO_CHECK(!loopback_address.is_site_local());
  ASIO_CHECK(!link_local_address.is_site_local());
  ASIO_CHECK(site_local_address.is_site_local());
  ASIO_CHECK(!v4_mapped_address.is_site_local());
  ASIO_CHECK(!v4_compat_address.is_site_local());
  ASIO_CHECK(!mcast_global_address.is_site_local());
  ASIO_CHECK(!mcast_link_local_address.is_site_local());
  ASIO_CHECK(!mcast_node_local_address.is_site_local());
  ASIO_CHECK(!mcast_org_local_address.is_site_local());
  ASIO_CHECK(!mcast_site_local_address.is_site_local());

  ASIO_CHECK(!unspecified_address.is_v4_mapped());
  ASIO_CHECK(!loopback_address.is_v4_mapped());
  ASIO_CHECK(!link_local_address.is_v4_mapped());
  ASIO_CHECK(!site_local_address.is_v4_mapped());
  ASIO_CHECK(v4_mapped_address.is_v4_mapped());
  ASIO_CHECK(!v4_compat_address.is_v4_mapped());
  ASIO_CHECK(!mcast_global_address.is_v4_mapped());
  ASIO_CHECK(!mcast_link_local_address.is_v4_mapped());
  ASIO_CHECK(!mcast_node_local_address.is_v4_mapped());
  ASIO_CHECK(!mcast_org_local_address.is_v4_mapped());
  ASIO_CHECK(!mcast_site_local_address.is_v4_mapped());

#if !defined(ASIO_NO_DEPRECATED)
  ASIO_CHECK(!unspecified_address.is_v4_compatible());
  ASIO_CHECK(!loopback_address.is_v4_compatible());
  ASIO_CHECK(!link_local_address.is_v4_compatible());
  ASIO_CHECK(!site_local_address.is_v4_compatible());
  ASIO_CHECK(!v4_mapped_address.is_v4_compatible());
  ASIO_CHECK(v4_compat_address.is_v4_compatible());
  ASIO_CHECK(!mcast_global_address.is_v4_compatible());
  ASIO_CHECK(!mcast_link_local_address.is_v4_compatible());
  ASIO_CHECK(!mcast_node_local_address.is_v4_compatible());
  ASIO_CHECK(!mcast_org_local_address.is_v4_compatible());
  ASIO_CHECK(!mcast_site_local_address.is_v4_compatible());
#endif // !defined(ASIO_NO_DEPRECATED)

  ASIO_CHECK(!unspecified_address.is_multicast());
  ASIO_CHECK(!loopback_address.is_multicast());
  ASIO_CHECK(!link_local_address.is_multicast());
  ASIO_CHECK(!site_local_address.is_multicast());
  ASIO_CHECK(!v4_mapped_address.is_multicast());
  ASIO_CHECK(!v4_compat_address.is_multicast());
  ASIO_CHECK(mcast_global_address.is_multicast());
  ASIO_CHECK(mcast_link_local_address.is_multicast());
  ASIO_CHECK(mcast_node_local_address.is_multicast());
  ASIO_CHECK(mcast_org_local_address.is_multicast());
  ASIO_CHECK(mcast_site_local_address.is_multicast());

  ASIO_CHECK(!unspecified_address.is_multicast_global());
  ASIO_CHECK(!loopback_address.is_multicast_global());
  ASIO_CHECK(!link_local_address.is_multicast_global());
  ASIO_CHECK(!site_local_address.is_multicast_global());
  ASIO_CHECK(!v4_mapped_address.is_multicast_global());
  ASIO_CHECK(!v4_compat_address.is_multicast_global());
  ASIO_CHECK(mcast_global_address.is_multicast_global());
  ASIO_CHECK(!mcast_link_local_address.is_multicast_global());
  ASIO_CHECK(!mcast_node_local_address.is_multicast_global());
  ASIO_CHECK(!mcast_org_local_address.is_multicast_global());
  ASIO_CHECK(!mcast_site_local_address.is_multicast_global());

  ASIO_CHECK(!unspecified_address.is_multicast_link_local());
  ASIO_CHECK(!loopback_address.is_multicast_link_local());
  ASIO_CHECK(!link_local_address.is_multicast_link_local());
  ASIO_CHECK(!site_local_address.is_multicast_link_local());
  ASIO_CHECK(!v4_mapped_address.is_multicast_link_local());
  ASIO_CHECK(!v4_compat_address.is_multicast_link_local());
  ASIO_CHECK(!mcast_global_address.is_multicast_link_local());
  ASIO_CHECK(mcast_link_local_address.is_multicast_link_local());
  ASIO_CHECK(!mcast_node_local_address.is_multicast_link_local());
  ASIO_CHECK(!mcast_org_local_address.is_multicast_link_local());
  ASIO_CHECK(!mcast_site_local_address.is_multicast_link_local());

  ASIO_CHECK(!unspecified_address.is_multicast_node_local());
  ASIO_CHECK(!loopback_address.is_multicast_node_local());
  ASIO_CHECK(!link_local_address.is_multicast_node_local());
  ASIO_CHECK(!site_local_address.is_multicast_node_local());
  ASIO_CHECK(!v4_mapped_address.is_multicast_node_local());
  ASIO_CHECK(!v4_compat_address.is_multicast_node_local());
  ASIO_CHECK(!mcast_global_address.is_multicast_node_local());
  ASIO_CHECK(!mcast_link_local_address.is_multicast_node_local());
  ASIO_CHECK(mcast_node_local_address.is_multicast_node_local());
  ASIO_CHECK(!mcast_org_local_address.is_multicast_node_local());
  ASIO_CHECK(!mcast_site_local_address.is_multicast_node_local());

  ASIO_CHECK(!unspecified_address.is_multicast_org_local());
  ASIO_CHECK(!loopback_address.is_multicast_org_local());
  ASIO_CHECK(!link_local_address.is_multicast_org_local());
  ASIO_CHECK(!site_local_address.is_multicast_org_local());
  ASIO_CHECK(!v4_mapped_address.is_multicast_org_local());
  ASIO_CHECK(!v4_compat_address.is_multicast_org_local());
  ASIO_CHECK(!mcast_global_address.is_multicast_org_local());
  ASIO_CHECK(!mcast_link_local_address.is_multicast_org_local());
  ASIO_CHECK(!mcast_node_local_address.is_multicast_org_local());
  ASIO_CHECK(mcast_org_local_address.is_multicast_org_local());
  ASIO_CHECK(!mcast_site_local_address.is_multicast_org_local());

  ASIO_CHECK(!unspecified_address.is_multicast_site_local());
  ASIO_CHECK(!loopback_address.is_multicast_site_local());
  ASIO_CHECK(!link_local_address.is_multicast_site_local());
  ASIO_CHECK(!site_local_address.is_multicast_site_local());
  ASIO_CHECK(!v4_mapped_address.is_multicast_site_local());
  ASIO_CHECK(!v4_compat_address.is_multicast_site_local());
  ASIO_CHECK(!mcast_global_address.is_multicast_site_local());
  ASIO_CHECK(!mcast_link_local_address.is_multicast_site_local());
  ASIO_CHECK(!mcast_node_local_address.is_multicast_site_local());
  ASIO_CHECK(!mcast_org_local_address.is_multicast_site_local());
  ASIO_CHECK(mcast_site_local_address.is_multicast_site_local());

  ASIO_CHECK(address_v6::loopback().is_loopback());
}

} // namespace ip_address_v6_runtime

//------------------------------------------------------------------------------

ASIO_TEST_SUITE
(
  "ip/address_v6",
  ASIO_TEST_CASE(ip_address_v6_compile::test)
  ASIO_TEST_CASE(ip_address_v6_runtime::test)
)