1 //
2 // network_v6.cpp
3 // ~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 // Copyright (c) 2014 Oliver Kowalke (oliver dot kowalke at gmail dot com)
7 //
8 // Distributed under the Boost Software License, Version 1.0. (See accompanying
9 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
10 //
11
12 // Disable autolinking for unit tests.
13 #if !defined(BOOST_ALL_NO_LIB)
14 #define BOOST_ALL_NO_LIB 1
15 #endif // !defined(BOOST_ALL_NO_LIB)
16
17 // Test that header file is self-contained.
18 #include "asio/ip/network_v6.hpp"
19
20 #include "../unit_test.hpp"
21
22 //------------------------------------------------------------------------------
23
24 // ip_network_v6_compile test
25 // ~~~~~~~~~~~~~~~~~~~~~~~~~~
26 // The following test checks that all public member functions on the class
27 // ip::network_v6 compile and link correctly. Runtime failures are ignored.
28
29 namespace ip_network_v6_compile {
30
test()31 void test()
32 {
33 using namespace asio;
34 namespace ip = asio::ip;
35
36 try
37 {
38 asio::error_code ec;
39
40 // network_v6 constructors.
41
42 ip::network_v6 net1(ip::make_address_v6("2001:370::10:7344"), 64);
43 ip::network_v6 net2(ip::make_address_v6("2001:370::10:7345"), 96);
44
45 // network_v6 functions.
46
47 ip::address_v6 addr1 = net1.address();
48 (void)addr1;
49
50 unsigned short prefix_len = net1.prefix_length();
51 (void)prefix_len;
52
53 ip::address_v6 addr3 = net1.network();
54 (void)addr3;
55
56 ip::address_v6_range hosts = net1.hosts();
57 (void)hosts;
58
59 ip::network_v6 net3 = net1.canonical();
60 (void)net3;
61
62 bool b1 = net1.is_host();
63 (void)b1;
64
65 bool b2 = net1.is_subnet_of(net2);
66 (void)b2;
67
68 std::string s1 = net1.to_string();
69 (void)s1;
70
71 std::string s2 = net1.to_string(ec);
72 (void)s2;
73
74 // network_v6 comparisons.
75
76 bool b3 = (net1 == net2);
77 (void)b3;
78
79 bool b4 = (net1 != net2);
80 (void)b4;
81
82 }
83 catch (std::exception&)
84 {
85 }
86 }
87
88 } // namespace ip_network_v6_compile
89
90 //------------------------------------------------------------------------------
91
92 // ip_network_v6_runtime test
93 // ~~~~~~~~~~~~~~~~~~~~~~~~~~
94 // The following test checks that the various public member functions meet the
95 // necessary postconditions.
96
97 namespace ip_network_v6_runtime {
98
test()99 void test()
100 {
101 using asio::ip::address_v6;
102 using asio::ip::make_address_v6;
103 using asio::ip::network_v6;
104 using asio::ip::make_network_v6;
105
106 address_v6 addr = make_address_v6("2001:370::10:7344");
107
108 std::string msg;
109 try
110 {
111 make_network_v6(addr, 129);
112 }
113 catch(std::out_of_range& ex)
114 {
115 msg = ex.what();
116 }
117 ASIO_CHECK(msg == std::string("prefix length too large"));
118
119 // construct address range from address and prefix length
120 ASIO_CHECK(network_v6(address_v6::from_string("2001:370::10:7344"), 128).network() == address_v6::from_string("2001:370::10:7344"));
121 ASIO_CHECK(network_v6(address_v6::from_string("2001:370::10:7344"), 64).network() == address_v6::from_string("2001:370::"));
122 ASIO_CHECK(network_v6(address_v6::from_string("2001:370::10:7344"), 27).network() == address_v6::from_string("2001:360::"));
123
124 // construct address range from string in CIDR notation
125 ASIO_CHECK(make_network_v6("2001:370::10:7344/128").network() == address_v6::from_string("2001:370::10:7344"));
126 ASIO_CHECK(make_network_v6("2001:370::10:7344/64").network() == address_v6::from_string("2001:370::"));
127 ASIO_CHECK(make_network_v6("2001:370::10:7344/27").network() == address_v6::from_string("2001:360::"));
128
129 // prefix length
130 ASIO_CHECK(make_network_v6("2001:370::10:7344/128").prefix_length() == 128);
131 ASIO_CHECK(network_v6(address_v6::from_string("2001:370::10:7344"), 27).prefix_length() == 27);
132
133 // to string
134 std::string a("2001:370::10:7344/64");
135 ASIO_CHECK(make_network_v6(a.c_str()).to_string() == a);
136 ASIO_CHECK(network_v6(address_v6::from_string("2001:370::10:7344"), 27).to_string() == std::string("2001:370::10:7344/27"));
137
138 // return host part
139 ASIO_CHECK(make_network_v6("2001:370::10:7344/64").address() == address_v6::from_string("2001:370::10:7344"));
140 ASIO_CHECK(make_network_v6("2001:370::10:7344/27").address().to_string() == "2001:370::10:7344");
141
142 // return network in CIDR notation
143 ASIO_CHECK(make_network_v6("2001:370::10:7344/27").canonical().to_string() == "2001:360::/27");
144
145 // is host
146 ASIO_CHECK(make_network_v6("2001:370::10:7344/128").is_host());
147 ASIO_CHECK(!make_network_v6("2001:370::10:7344/127").is_host());
148
149 // is real subnet of
150 ASIO_CHECK(make_network_v6("2001:370::10:3744/64").is_subnet_of(make_network_v6("2001:370::/16")));
151 ASIO_CHECK(make_network_v6("2001:370::/64").is_subnet_of(make_network_v6("2001:370::/16")));
152 ASIO_CHECK(make_network_v6("2001:0db8:85a3::/64").is_subnet_of(make_network_v6("2001:0d00::/24")));
153
154 ASIO_CHECK(!make_network_v6("2001:370::10:3744/128").is_subnet_of(make_network_v6("2001:370::10:3744/128")));
155 ASIO_CHECK(make_network_v6("2001:0db8:85a3::/64").is_subnet_of(make_network_v6("2001:0dc0::/24")));
156
157 network_v6 r(make_network_v6("2001:370::/64"));
158 ASIO_CHECK(!r.is_subnet_of(r));
159
160 network_v6 net12(make_network_v6("2001:370::10:7344/64"));
161 network_v6 net13(make_network_v6("2001:0db8::/127"));
162 network_v6 net14(make_network_v6("2001:0db8::/125"));
163 network_v6 net15(make_network_v6("2001:0db8::/119"));
164
165 // network
166 ASIO_CHECK(net12.network() == address_v6::from_string("2001:370::"));
167 ASIO_CHECK(net13.network() == address_v6::from_string("2001:0db8::"));
168 ASIO_CHECK(net14.network() == address_v6::from_string("2001:0db8::"));
169 ASIO_CHECK(net15.network() == address_v6::from_string("2001:0db8::"));
170
171 // iterator
172 //ASIO_CHECK(std::distance(net12.hosts().begin(),net12.hosts().end()) == 18446744073709552000);
173 ASIO_CHECK(std::distance(net13.hosts().begin(),net13.hosts().end()) == 2);
174 ASIO_CHECK(std::distance(net14.hosts().begin(),net14.hosts().end()) == 8);
175 ASIO_CHECK(std::distance(net15.hosts().begin(),net15.hosts().end()) == 512);
176 ASIO_CHECK(*net12.hosts().begin() == address_v6::from_string("2001:0370::"));
177 ASIO_CHECK(net12.hosts().end() != net12.hosts().find(address_v6::from_string("2001:0370::ffff:ffff:ffff:ffff")));
178 ASIO_CHECK(*net13.hosts().begin() == address_v6::from_string("2001:0db8::"));
179 ASIO_CHECK(net13.hosts().end() != net13.hosts().find(address_v6::from_string("2001:0db8::1")));
180 ASIO_CHECK(net13.hosts().end() == net13.hosts().find(address_v6::from_string("2001:0db8::2")));
181 ASIO_CHECK(*net14.hosts().begin() == address_v6::from_string("2001:0db8::"));
182 ASIO_CHECK(net14.hosts().end() != net14.hosts().find(address_v6::from_string("2001:0db8::7")));
183 ASIO_CHECK(net14.hosts().end() == net14.hosts().find(address_v6::from_string("2001:0db8::8")));
184 ASIO_CHECK(*net15.hosts().begin() == address_v6::from_string("2001:0db8::"));
185 ASIO_CHECK(net15.hosts().end() != net15.hosts().find(address_v6::from_string("2001:0db8::01ff")));
186 ASIO_CHECK(net15.hosts().end() == net15.hosts().find(address_v6::from_string("2001:0db8::0200")));
187 }
188
189 } // namespace ip_network_v6_runtime
190
191 //------------------------------------------------------------------------------
192
193 ASIO_TEST_SUITE
194 (
195 "ip/network_v6",
196 ASIO_TEST_CASE(ip_network_v6_compile::test)
197 ASIO_TEST_CASE(ip_network_v6_runtime::test)
198 )
199