Use list initialization (C++11) for maps/vectors instead of boost::assign::map_list_o...
[bitcoinplatinum.git] / src / test / netbase_tests.cpp
blobe4b4b857208640855cddb3c78bb8d34f3946720d
1 // Copyright (c) 2012-2016 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #include "netbase.h"
6 #include "test/test_bitcoin.h"
8 #include <string>
10 #include <boost/test/unit_test.hpp>
12 BOOST_FIXTURE_TEST_SUITE(netbase_tests, BasicTestingSetup)
14 static CNetAddr ResolveIP(const char* ip)
16 CNetAddr addr;
17 LookupHost(ip, addr, false);
18 return addr;
21 static CSubNet ResolveSubNet(const char* subnet)
23 CSubNet ret;
24 LookupSubNet(subnet, ret);
25 return ret;
28 BOOST_AUTO_TEST_CASE(netbase_networks)
30 BOOST_CHECK(ResolveIP("127.0.0.1").GetNetwork() == NET_UNROUTABLE);
31 BOOST_CHECK(ResolveIP("::1").GetNetwork() == NET_UNROUTABLE);
32 BOOST_CHECK(ResolveIP("8.8.8.8").GetNetwork() == NET_IPV4);
33 BOOST_CHECK(ResolveIP("2001::8888").GetNetwork() == NET_IPV6);
34 BOOST_CHECK(ResolveIP("FD87:D87E:EB43:edb1:8e4:3588:e546:35ca").GetNetwork() == NET_TOR);
38 BOOST_AUTO_TEST_CASE(netbase_properties)
41 BOOST_CHECK(ResolveIP("127.0.0.1").IsIPv4());
42 BOOST_CHECK(ResolveIP("::FFFF:192.168.1.1").IsIPv4());
43 BOOST_CHECK(ResolveIP("::1").IsIPv6());
44 BOOST_CHECK(ResolveIP("10.0.0.1").IsRFC1918());
45 BOOST_CHECK(ResolveIP("192.168.1.1").IsRFC1918());
46 BOOST_CHECK(ResolveIP("172.31.255.255").IsRFC1918());
47 BOOST_CHECK(ResolveIP("2001:0DB8::").IsRFC3849());
48 BOOST_CHECK(ResolveIP("169.254.1.1").IsRFC3927());
49 BOOST_CHECK(ResolveIP("2002::1").IsRFC3964());
50 BOOST_CHECK(ResolveIP("FC00::").IsRFC4193());
51 BOOST_CHECK(ResolveIP("2001::2").IsRFC4380());
52 BOOST_CHECK(ResolveIP("2001:10::").IsRFC4843());
53 BOOST_CHECK(ResolveIP("FE80::").IsRFC4862());
54 BOOST_CHECK(ResolveIP("64:FF9B::").IsRFC6052());
55 BOOST_CHECK(ResolveIP("FD87:D87E:EB43:edb1:8e4:3588:e546:35ca").IsTor());
56 BOOST_CHECK(ResolveIP("127.0.0.1").IsLocal());
57 BOOST_CHECK(ResolveIP("::1").IsLocal());
58 BOOST_CHECK(ResolveIP("8.8.8.8").IsRoutable());
59 BOOST_CHECK(ResolveIP("2001::1").IsRoutable());
60 BOOST_CHECK(ResolveIP("127.0.0.1").IsValid());
64 bool static TestSplitHost(std::string test, std::string host, int port)
66 std::string hostOut;
67 int portOut = -1;
68 SplitHostPort(test, portOut, hostOut);
69 return hostOut == host && port == portOut;
72 BOOST_AUTO_TEST_CASE(netbase_splithost)
74 BOOST_CHECK(TestSplitHost("www.bitcoin.org", "www.bitcoin.org", -1));
75 BOOST_CHECK(TestSplitHost("[www.bitcoin.org]", "www.bitcoin.org", -1));
76 BOOST_CHECK(TestSplitHost("www.bitcoin.org:80", "www.bitcoin.org", 80));
77 BOOST_CHECK(TestSplitHost("[www.bitcoin.org]:80", "www.bitcoin.org", 80));
78 BOOST_CHECK(TestSplitHost("127.0.0.1", "127.0.0.1", -1));
79 BOOST_CHECK(TestSplitHost("127.0.0.1:8333", "127.0.0.1", 8333));
80 BOOST_CHECK(TestSplitHost("[127.0.0.1]", "127.0.0.1", -1));
81 BOOST_CHECK(TestSplitHost("[127.0.0.1]:8333", "127.0.0.1", 8333));
82 BOOST_CHECK(TestSplitHost("::ffff:127.0.0.1", "::ffff:127.0.0.1", -1));
83 BOOST_CHECK(TestSplitHost("[::ffff:127.0.0.1]:8333", "::ffff:127.0.0.1", 8333));
84 BOOST_CHECK(TestSplitHost("[::]:8333", "::", 8333));
85 BOOST_CHECK(TestSplitHost("::8333", "::8333", -1));
86 BOOST_CHECK(TestSplitHost(":8333", "", 8333));
87 BOOST_CHECK(TestSplitHost("[]:8333", "", 8333));
88 BOOST_CHECK(TestSplitHost("", "", -1));
91 bool static TestParse(std::string src, std::string canon)
93 CService addr(LookupNumeric(src.c_str(), 65535));
94 return canon == addr.ToString();
97 BOOST_AUTO_TEST_CASE(netbase_lookupnumeric)
99 BOOST_CHECK(TestParse("127.0.0.1", "127.0.0.1:65535"));
100 BOOST_CHECK(TestParse("127.0.0.1:8333", "127.0.0.1:8333"));
101 BOOST_CHECK(TestParse("::ffff:127.0.0.1", "127.0.0.1:65535"));
102 BOOST_CHECK(TestParse("::", "[::]:65535"));
103 BOOST_CHECK(TestParse("[::]:8333", "[::]:8333"));
104 BOOST_CHECK(TestParse("[127.0.0.1]", "127.0.0.1:65535"));
105 BOOST_CHECK(TestParse(":::", "[::]:0"));
108 BOOST_AUTO_TEST_CASE(onioncat_test)
111 // values from https://web.archive.org/web/20121122003543/http://www.cypherpunk.at/onioncat/wiki/OnionCat
112 CNetAddr addr1(ResolveIP("5wyqrzbvrdsumnok.onion"));
113 CNetAddr addr2(ResolveIP("FD87:D87E:EB43:edb1:8e4:3588:e546:35ca"));
114 BOOST_CHECK(addr1 == addr2);
115 BOOST_CHECK(addr1.IsTor());
116 BOOST_CHECK(addr1.ToStringIP() == "5wyqrzbvrdsumnok.onion");
117 BOOST_CHECK(addr1.IsRoutable());
121 BOOST_AUTO_TEST_CASE(subnet_test)
124 BOOST_CHECK(ResolveSubNet("1.2.3.0/24") == ResolveSubNet("1.2.3.0/255.255.255.0"));
125 BOOST_CHECK(ResolveSubNet("1.2.3.0/24") != ResolveSubNet("1.2.4.0/255.255.255.0"));
126 BOOST_CHECK(ResolveSubNet("1.2.3.0/24").Match(ResolveIP("1.2.3.4")));
127 BOOST_CHECK(!ResolveSubNet("1.2.2.0/24").Match(ResolveIP("1.2.3.4")));
128 BOOST_CHECK(ResolveSubNet("1.2.3.4").Match(ResolveIP("1.2.3.4")));
129 BOOST_CHECK(ResolveSubNet("1.2.3.4/32").Match(ResolveIP("1.2.3.4")));
130 BOOST_CHECK(!ResolveSubNet("1.2.3.4").Match(ResolveIP("5.6.7.8")));
131 BOOST_CHECK(!ResolveSubNet("1.2.3.4/32").Match(ResolveIP("5.6.7.8")));
132 BOOST_CHECK(ResolveSubNet("::ffff:127.0.0.1").Match(ResolveIP("127.0.0.1")));
133 BOOST_CHECK(ResolveSubNet("1:2:3:4:5:6:7:8").Match(ResolveIP("1:2:3:4:5:6:7:8")));
134 BOOST_CHECK(!ResolveSubNet("1:2:3:4:5:6:7:8").Match(ResolveIP("1:2:3:4:5:6:7:9")));
135 BOOST_CHECK(ResolveSubNet("1:2:3:4:5:6:7:0/112").Match(ResolveIP("1:2:3:4:5:6:7:1234")));
136 BOOST_CHECK(ResolveSubNet("192.168.0.1/24").Match(ResolveIP("192.168.0.2")));
137 BOOST_CHECK(ResolveSubNet("192.168.0.20/29").Match(ResolveIP("192.168.0.18")));
138 BOOST_CHECK(ResolveSubNet("1.2.2.1/24").Match(ResolveIP("1.2.2.4")));
139 BOOST_CHECK(ResolveSubNet("1.2.2.110/31").Match(ResolveIP("1.2.2.111")));
140 BOOST_CHECK(ResolveSubNet("1.2.2.20/26").Match(ResolveIP("1.2.2.63")));
141 // All-Matching IPv6 Matches arbitrary IPv4 and IPv6
142 BOOST_CHECK(ResolveSubNet("::/0").Match(ResolveIP("1:2:3:4:5:6:7:1234")));
143 BOOST_CHECK(ResolveSubNet("::/0").Match(ResolveIP("1.2.3.4")));
144 // All-Matching IPv4 does not Match IPv6
145 BOOST_CHECK(!ResolveSubNet("0.0.0.0/0").Match(ResolveIP("1:2:3:4:5:6:7:1234")));
146 // Invalid subnets Match nothing (not even invalid addresses)
147 BOOST_CHECK(!CSubNet().Match(ResolveIP("1.2.3.4")));
148 BOOST_CHECK(!ResolveSubNet("").Match(ResolveIP("4.5.6.7")));
149 BOOST_CHECK(!ResolveSubNet("bloop").Match(ResolveIP("0.0.0.0")));
150 BOOST_CHECK(!ResolveSubNet("bloop").Match(ResolveIP("hab")));
151 // Check valid/invalid
152 BOOST_CHECK(ResolveSubNet("1.2.3.0/0").IsValid());
153 BOOST_CHECK(!ResolveSubNet("1.2.3.0/-1").IsValid());
154 BOOST_CHECK(ResolveSubNet("1.2.3.0/32").IsValid());
155 BOOST_CHECK(!ResolveSubNet("1.2.3.0/33").IsValid());
156 BOOST_CHECK(ResolveSubNet("1:2:3:4:5:6:7:8/0").IsValid());
157 BOOST_CHECK(ResolveSubNet("1:2:3:4:5:6:7:8/33").IsValid());
158 BOOST_CHECK(!ResolveSubNet("1:2:3:4:5:6:7:8/-1").IsValid());
159 BOOST_CHECK(ResolveSubNet("1:2:3:4:5:6:7:8/128").IsValid());
160 BOOST_CHECK(!ResolveSubNet("1:2:3:4:5:6:7:8/129").IsValid());
161 BOOST_CHECK(!ResolveSubNet("fuzzy").IsValid());
163 //CNetAddr constructor test
164 BOOST_CHECK(CSubNet(ResolveIP("127.0.0.1")).IsValid());
165 BOOST_CHECK(CSubNet(ResolveIP("127.0.0.1")).Match(ResolveIP("127.0.0.1")));
166 BOOST_CHECK(!CSubNet(ResolveIP("127.0.0.1")).Match(ResolveIP("127.0.0.2")));
167 BOOST_CHECK(CSubNet(ResolveIP("127.0.0.1")).ToString() == "127.0.0.1/32");
169 CSubNet subnet = CSubNet(ResolveIP("1.2.3.4"), 32);
170 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/32");
171 subnet = CSubNet(ResolveIP("1.2.3.4"), 8);
172 BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/8");
173 subnet = CSubNet(ResolveIP("1.2.3.4"), 0);
174 BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/0");
176 subnet = CSubNet(ResolveIP("1.2.3.4"), ResolveIP("255.255.255.255"));
177 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/32");
178 subnet = CSubNet(ResolveIP("1.2.3.4"), ResolveIP("255.0.0.0"));
179 BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/8");
180 subnet = CSubNet(ResolveIP("1.2.3.4"), ResolveIP("0.0.0.0"));
181 BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/0");
183 BOOST_CHECK(CSubNet(ResolveIP("1:2:3:4:5:6:7:8")).IsValid());
184 BOOST_CHECK(CSubNet(ResolveIP("1:2:3:4:5:6:7:8")).Match(ResolveIP("1:2:3:4:5:6:7:8")));
185 BOOST_CHECK(!CSubNet(ResolveIP("1:2:3:4:5:6:7:8")).Match(ResolveIP("1:2:3:4:5:6:7:9")));
186 BOOST_CHECK(CSubNet(ResolveIP("1:2:3:4:5:6:7:8")).ToString() == "1:2:3:4:5:6:7:8/128");
188 subnet = ResolveSubNet("1.2.3.4/255.255.255.255");
189 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/32");
190 subnet = ResolveSubNet("1.2.3.4/255.255.255.254");
191 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/31");
192 subnet = ResolveSubNet("1.2.3.4/255.255.255.252");
193 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/30");
194 subnet = ResolveSubNet("1.2.3.4/255.255.255.248");
195 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/29");
196 subnet = ResolveSubNet("1.2.3.4/255.255.255.240");
197 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/28");
198 subnet = ResolveSubNet("1.2.3.4/255.255.255.224");
199 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/27");
200 subnet = ResolveSubNet("1.2.3.4/255.255.255.192");
201 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/26");
202 subnet = ResolveSubNet("1.2.3.4/255.255.255.128");
203 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/25");
204 subnet = ResolveSubNet("1.2.3.4/255.255.255.0");
205 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/24");
206 subnet = ResolveSubNet("1.2.3.4/255.255.254.0");
207 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.2.0/23");
208 subnet = ResolveSubNet("1.2.3.4/255.255.252.0");
209 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/22");
210 subnet = ResolveSubNet("1.2.3.4/255.255.248.0");
211 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/21");
212 subnet = ResolveSubNet("1.2.3.4/255.255.240.0");
213 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/20");
214 subnet = ResolveSubNet("1.2.3.4/255.255.224.0");
215 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/19");
216 subnet = ResolveSubNet("1.2.3.4/255.255.192.0");
217 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/18");
218 subnet = ResolveSubNet("1.2.3.4/255.255.128.0");
219 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/17");
220 subnet = ResolveSubNet("1.2.3.4/255.255.0.0");
221 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/16");
222 subnet = ResolveSubNet("1.2.3.4/255.254.0.0");
223 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/15");
224 subnet = ResolveSubNet("1.2.3.4/255.252.0.0");
225 BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/14");
226 subnet = ResolveSubNet("1.2.3.4/255.248.0.0");
227 BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/13");
228 subnet = ResolveSubNet("1.2.3.4/255.240.0.0");
229 BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/12");
230 subnet = ResolveSubNet("1.2.3.4/255.224.0.0");
231 BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/11");
232 subnet = ResolveSubNet("1.2.3.4/255.192.0.0");
233 BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/10");
234 subnet = ResolveSubNet("1.2.3.4/255.128.0.0");
235 BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/9");
236 subnet = ResolveSubNet("1.2.3.4/255.0.0.0");
237 BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/8");
238 subnet = ResolveSubNet("1.2.3.4/254.0.0.0");
239 BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/7");
240 subnet = ResolveSubNet("1.2.3.4/252.0.0.0");
241 BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/6");
242 subnet = ResolveSubNet("1.2.3.4/248.0.0.0");
243 BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/5");
244 subnet = ResolveSubNet("1.2.3.4/240.0.0.0");
245 BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/4");
246 subnet = ResolveSubNet("1.2.3.4/224.0.0.0");
247 BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/3");
248 subnet = ResolveSubNet("1.2.3.4/192.0.0.0");
249 BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/2");
250 subnet = ResolveSubNet("1.2.3.4/128.0.0.0");
251 BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/1");
252 subnet = ResolveSubNet("1.2.3.4/0.0.0.0");
253 BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/0");
255 subnet = ResolveSubNet("1:2:3:4:5:6:7:8/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
256 BOOST_CHECK_EQUAL(subnet.ToString(), "1:2:3:4:5:6:7:8/128");
257 subnet = ResolveSubNet("1:2:3:4:5:6:7:8/ffff:0000:0000:0000:0000:0000:0000:0000");
258 BOOST_CHECK_EQUAL(subnet.ToString(), "1::/16");
259 subnet = ResolveSubNet("1:2:3:4:5:6:7:8/0000:0000:0000:0000:0000:0000:0000:0000");
260 BOOST_CHECK_EQUAL(subnet.ToString(), "::/0");
261 subnet = ResolveSubNet("1.2.3.4/255.255.232.0");
262 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/255.255.232.0");
263 subnet = ResolveSubNet("1:2:3:4:5:6:7:8/ffff:ffff:ffff:fffe:ffff:ffff:ffff:ff0f");
264 BOOST_CHECK_EQUAL(subnet.ToString(), "1:2:3:4:5:6:7:8/ffff:ffff:ffff:fffe:ffff:ffff:ffff:ff0f");
268 BOOST_AUTO_TEST_CASE(netbase_getgroup)
271 BOOST_CHECK(ResolveIP("127.0.0.1").GetGroup() == std::vector<unsigned char>({0})); // Local -> !Routable()
272 BOOST_CHECK(ResolveIP("257.0.0.1").GetGroup() == std::vector<unsigned char>({0})); // !Valid -> !Routable()
273 BOOST_CHECK(ResolveIP("10.0.0.1").GetGroup() == std::vector<unsigned char>({0})); // RFC1918 -> !Routable()
274 BOOST_CHECK(ResolveIP("169.254.1.1").GetGroup() == std::vector<unsigned char>({0})); // RFC3927 -> !Routable()
275 BOOST_CHECK(ResolveIP("1.2.3.4").GetGroup() == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // IPv4
276 BOOST_CHECK(ResolveIP("::FFFF:0:102:304").GetGroup() == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // RFC6145
277 BOOST_CHECK(ResolveIP("64:FF9B::102:304").GetGroup() == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // RFC6052
278 BOOST_CHECK(ResolveIP("2002:102:304:9999:9999:9999:9999:9999").GetGroup() == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // RFC3964
279 BOOST_CHECK(ResolveIP("2001:0:9999:9999:9999:9999:FEFD:FCFB").GetGroup() == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // RFC4380
280 BOOST_CHECK(ResolveIP("FD87:D87E:EB43:edb1:8e4:3588:e546:35ca").GetGroup() == std::vector<unsigned char>({(unsigned char)NET_TOR, 239})); // Tor
281 BOOST_CHECK(ResolveIP("2001:470:abcd:9999:9999:9999:9999:9999").GetGroup() == std::vector<unsigned char>({(unsigned char)NET_IPV6, 32, 1, 4, 112, 175})); //he.net
282 BOOST_CHECK(ResolveIP("2001:2001:9999:9999:9999:9999:9999:9999").GetGroup() == std::vector<unsigned char>({(unsigned char)NET_IPV6, 32, 1, 32, 1})); //IPv6
286 BOOST_AUTO_TEST_SUITE_END()