File: ipv6_address.cpp

package info (click to toggle)
actor-framework 0.17.6-3.2
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 9,008 kB
  • sloc: cpp: 77,684; sh: 674; python: 309; makefile: 13
file content (97 lines) | stat: -rw-r--r-- 4,129 bytes parent folder | download | duplicates (4)
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
/******************************************************************************
 *                       ____    _    _____                                   *
 *                      / ___|  / \  |  ___|    C++                           *
 *                     | |     / _ \ | |_       Actor                         *
 *                     | |___ / ___ \|  _|      Framework                     *
 *                      \____/_/   \_|_|                                      *
 *                                                                            *
 * Copyright 2011-2018 Dominik Charousset                                     *
 *                                                                            *
 * Distributed under the terms and conditions of the BSD 3-Clause License or  *
 * (at your option) under the terms and conditions of the Boost Software      *
 * License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE.       *
 *                                                                            *
 * If you did not receive a copy of the license files, see                    *
 * http://opensource.org/licenses/BSD-3-Clause and                            *
 * http://www.boost.org/LICENSE_1_0.txt.                                      *
 ******************************************************************************/

#include <initializer_list>

#include "caf/config.hpp"

#define CAF_SUITE ipv6_address
#include "caf/test/dsl.hpp"

#include "caf/ipv4_address.hpp"
#include "caf/ipv6_address.hpp"

using namespace caf;

namespace {

using array_type = ipv6_address::array_type;

ipv6_address addr(std::initializer_list<uint16_t> prefix,
                  std::initializer_list<uint16_t> suffix = {}) {
  return ipv6_address{prefix, suffix};
}

} // namespace

CAF_TEST(constructing) {
  ipv6_address::array_type localhost_bytes{{0, 0, 0, 0, 0, 0, 0, 0,
                                            0, 0, 0, 0, 0, 0, 0, 1}};
  ipv6_address localhost{localhost_bytes};
  CAF_CHECK_EQUAL(localhost.data(), localhost_bytes);
  CAF_CHECK_EQUAL(localhost, addr({}, {0x01}));
}

CAF_TEST(comparison) {
  CAF_CHECK_EQUAL(addr({1, 2, 3}), addr({1, 2, 3}));
  CAF_CHECK_NOT_EQUAL(addr({3, 2, 1}), addr({1, 2, 3}));
  CAF_CHECK_EQUAL(addr({}, {0xFFFF, 0x7F00, 0x0001}),
                  make_ipv4_address(127, 0, 0, 1));
}

CAF_TEST(from string) {
  auto from_string = [](string_view str) {
    ipv6_address result;
    auto err = parse(str, result);
    if (err)
      CAF_FAIL("error while parsing " << str << ": " << to_string(err));
    return result;
  };
  CAF_CHECK_EQUAL(from_string("::1"), addr({}, {0x01}));
  CAF_CHECK_EQUAL(from_string("::11"), addr({}, {0x11}));
  CAF_CHECK_EQUAL(from_string("::112"), addr({}, {0x0112}));
  CAF_CHECK_EQUAL(from_string("::1122"), addr({}, {0x1122}));
  CAF_CHECK_EQUAL(from_string("::1:2"), addr({}, {0x01, 0x02}));
  CAF_CHECK_EQUAL(from_string("::1:2"), addr({}, {0x01, 0x02}));
  CAF_CHECK_EQUAL(from_string("1::1"), addr({0x01}, {0x01}));
  CAF_CHECK_EQUAL(from_string("2a00:bdc0:e003::"),
                  addr({0x2a00, 0xbdc0, 0xe003}, {}));
  CAF_CHECK_EQUAL(from_string("1::"), addr({0x01}, {}));
  CAF_CHECK_EQUAL(from_string("0.1.0.1"), addr({}, {0xFFFF, 0x01, 0x01}));
  CAF_CHECK_EQUAL(from_string("::ffff:127.0.0.1"),
                  addr({}, {0xFFFF, 0x7F00, 0x0001}));
  CAF_CHECK_EQUAL(from_string("1:2:3:4:5:6:7:8"), addr({1,2,3,4,5,6,7,8}));
  CAF_CHECK_EQUAL(from_string("1:2:3:4::5:6:7:8"), addr({1,2,3,4,5,6,7,8}));
  CAF_CHECK_EQUAL(from_string("1:2:3:4:5:6:0.7.0.8"), addr({1,2,3,4,5,6,7,8}));
  auto invalid = [](string_view str) {
    ipv6_address result;
    auto err = parse(str, result);
    return err != none;
  };
  CAF_CHECK(invalid("1:2:3:4:5:6:7:8:9"));
  CAF_CHECK(invalid("1:2:3:4::5:6:7:8:9"));
  CAF_CHECK(invalid("1:2:3::4:5:6::7:8:9"));
}

CAF_TEST(to string) {
  CAF_CHECK_EQUAL(to_string(addr({}, {0x01})), "::1");
  CAF_CHECK_EQUAL(to_string(addr({0x01}, {0x01})), "1::1");
  CAF_CHECK_EQUAL(to_string(addr({0x01})), "1::");
  CAF_CHECK_EQUAL(to_string(addr({}, {0xFFFF, 0x01, 0x01})), "0.1.0.1");
}