Merge #12079: Improve prioritisetransaction test coverage
[bitcoinplatinum.git] / src / test / netbase_tests.cpp
blob027214e512639d95962559f8ef5a57e0449ea031
1 // Copyright (c) 2012-2017 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>
7 #include <utilstrencodings.h>
9 #include <string>
11 #include <boost/test/unit_test.hpp>
13 BOOST_FIXTURE_TEST_SUITE(netbase_tests, BasicTestingSetup)
15 static CNetAddr ResolveIP(const char* ip)
17 CNetAddr addr;
18 LookupHost(ip, addr, false);
19 return addr;
22 static CSubNet ResolveSubNet(const char* subnet)
24 CSubNet ret;
25 LookupSubNet(subnet, ret);
26 return ret;
29 static CNetAddr CreateInternal(const char* host)
31 CNetAddr addr;
32 addr.SetInternal(host);
33 return addr;
36 BOOST_AUTO_TEST_CASE(netbase_networks)
38 BOOST_CHECK(ResolveIP("127.0.0.1").GetNetwork() == NET_UNROUTABLE);
39 BOOST_CHECK(ResolveIP("::1").GetNetwork() == NET_UNROUTABLE);
40 BOOST_CHECK(ResolveIP("8.8.8.8").GetNetwork() == NET_IPV4);
41 BOOST_CHECK(ResolveIP("2001::8888").GetNetwork() == NET_IPV6);
42 BOOST_CHECK(ResolveIP("FD87:D87E:EB43:edb1:8e4:3588:e546:35ca").GetNetwork() == NET_TOR);
43 BOOST_CHECK(CreateInternal("foo.com").GetNetwork() == NET_INTERNAL);
47 BOOST_AUTO_TEST_CASE(netbase_properties)
50 BOOST_CHECK(ResolveIP("127.0.0.1").IsIPv4());
51 BOOST_CHECK(ResolveIP("::FFFF:192.168.1.1").IsIPv4());
52 BOOST_CHECK(ResolveIP("::1").IsIPv6());
53 BOOST_CHECK(ResolveIP("10.0.0.1").IsRFC1918());
54 BOOST_CHECK(ResolveIP("192.168.1.1").IsRFC1918());
55 BOOST_CHECK(ResolveIP("172.31.255.255").IsRFC1918());
56 BOOST_CHECK(ResolveIP("2001:0DB8::").IsRFC3849());
57 BOOST_CHECK(ResolveIP("169.254.1.1").IsRFC3927());
58 BOOST_CHECK(ResolveIP("2002::1").IsRFC3964());
59 BOOST_CHECK(ResolveIP("FC00::").IsRFC4193());
60 BOOST_CHECK(ResolveIP("2001::2").IsRFC4380());
61 BOOST_CHECK(ResolveIP("2001:10::").IsRFC4843());
62 BOOST_CHECK(ResolveIP("FE80::").IsRFC4862());
63 BOOST_CHECK(ResolveIP("64:FF9B::").IsRFC6052());
64 BOOST_CHECK(ResolveIP("FD87:D87E:EB43:edb1:8e4:3588:e546:35ca").IsTor());
65 BOOST_CHECK(ResolveIP("127.0.0.1").IsLocal());
66 BOOST_CHECK(ResolveIP("::1").IsLocal());
67 BOOST_CHECK(ResolveIP("8.8.8.8").IsRoutable());
68 BOOST_CHECK(ResolveIP("2001::1").IsRoutable());
69 BOOST_CHECK(ResolveIP("127.0.0.1").IsValid());
70 BOOST_CHECK(CreateInternal("FD6B:88C0:8724:edb1:8e4:3588:e546:35ca").IsInternal());
71 BOOST_CHECK(CreateInternal("bar.com").IsInternal());
75 bool static TestSplitHost(std::string test, std::string host, int port)
77 std::string hostOut;
78 int portOut = -1;
79 SplitHostPort(test, portOut, hostOut);
80 return hostOut == host && port == portOut;
83 BOOST_AUTO_TEST_CASE(netbase_splithost)
85 BOOST_CHECK(TestSplitHost("www.bitcoin.org", "www.bitcoin.org", -1));
86 BOOST_CHECK(TestSplitHost("[www.bitcoin.org]", "www.bitcoin.org", -1));
87 BOOST_CHECK(TestSplitHost("www.bitcoin.org:80", "www.bitcoin.org", 80));
88 BOOST_CHECK(TestSplitHost("[www.bitcoin.org]:80", "www.bitcoin.org", 80));
89 BOOST_CHECK(TestSplitHost("127.0.0.1", "127.0.0.1", -1));
90 BOOST_CHECK(TestSplitHost("127.0.0.1:8333", "127.0.0.1", 8333));
91 BOOST_CHECK(TestSplitHost("[127.0.0.1]", "127.0.0.1", -1));
92 BOOST_CHECK(TestSplitHost("[127.0.0.1]:8333", "127.0.0.1", 8333));
93 BOOST_CHECK(TestSplitHost("::ffff:127.0.0.1", "::ffff:127.0.0.1", -1));
94 BOOST_CHECK(TestSplitHost("[::ffff:127.0.0.1]:8333", "::ffff:127.0.0.1", 8333));
95 BOOST_CHECK(TestSplitHost("[::]:8333", "::", 8333));
96 BOOST_CHECK(TestSplitHost("::8333", "::8333", -1));
97 BOOST_CHECK(TestSplitHost(":8333", "", 8333));
98 BOOST_CHECK(TestSplitHost("[]:8333", "", 8333));
99 BOOST_CHECK(TestSplitHost("", "", -1));
102 bool static TestParse(std::string src, std::string canon)
104 CService addr(LookupNumeric(src.c_str(), 65535));
105 return canon == addr.ToString();
108 BOOST_AUTO_TEST_CASE(netbase_lookupnumeric)
110 BOOST_CHECK(TestParse("127.0.0.1", "127.0.0.1:65535"));
111 BOOST_CHECK(TestParse("127.0.0.1:8333", "127.0.0.1:8333"));
112 BOOST_CHECK(TestParse("::ffff:127.0.0.1", "127.0.0.1:65535"));
113 BOOST_CHECK(TestParse("::", "[::]:65535"));
114 BOOST_CHECK(TestParse("[::]:8333", "[::]:8333"));
115 BOOST_CHECK(TestParse("[127.0.0.1]", "127.0.0.1:65535"));
116 BOOST_CHECK(TestParse(":::", "[::]:0"));
118 // verify that an internal address fails to resolve
119 BOOST_CHECK(TestParse("[fd6b:88c0:8724:1:2:3:4:5]", "[::]:0"));
120 // and that a one-off resolves correctly
121 BOOST_CHECK(TestParse("[fd6c:88c0:8724:1:2:3:4:5]", "[fd6c:88c0:8724:1:2:3:4:5]:65535"));
124 BOOST_AUTO_TEST_CASE(onioncat_test)
127 // values from https://web.archive.org/web/20121122003543/http://www.cypherpunk.at/onioncat/wiki/OnionCat
128 CNetAddr addr1(ResolveIP("5wyqrzbvrdsumnok.onion"));
129 CNetAddr addr2(ResolveIP("FD87:D87E:EB43:edb1:8e4:3588:e546:35ca"));
130 BOOST_CHECK(addr1 == addr2);
131 BOOST_CHECK(addr1.IsTor());
132 BOOST_CHECK(addr1.ToStringIP() == "5wyqrzbvrdsumnok.onion");
133 BOOST_CHECK(addr1.IsRoutable());
137 BOOST_AUTO_TEST_CASE(subnet_test)
140 BOOST_CHECK(ResolveSubNet("1.2.3.0/24") == ResolveSubNet("1.2.3.0/255.255.255.0"));
141 BOOST_CHECK(ResolveSubNet("1.2.3.0/24") != ResolveSubNet("1.2.4.0/255.255.255.0"));
142 BOOST_CHECK(ResolveSubNet("1.2.3.0/24").Match(ResolveIP("1.2.3.4")));
143 BOOST_CHECK(!ResolveSubNet("1.2.2.0/24").Match(ResolveIP("1.2.3.4")));
144 BOOST_CHECK(ResolveSubNet("1.2.3.4").Match(ResolveIP("1.2.3.4")));
145 BOOST_CHECK(ResolveSubNet("1.2.3.4/32").Match(ResolveIP("1.2.3.4")));
146 BOOST_CHECK(!ResolveSubNet("1.2.3.4").Match(ResolveIP("5.6.7.8")));
147 BOOST_CHECK(!ResolveSubNet("1.2.3.4/32").Match(ResolveIP("5.6.7.8")));
148 BOOST_CHECK(ResolveSubNet("::ffff:127.0.0.1").Match(ResolveIP("127.0.0.1")));
149 BOOST_CHECK(ResolveSubNet("1:2:3:4:5:6:7:8").Match(ResolveIP("1:2:3:4:5:6:7:8")));
150 BOOST_CHECK(!ResolveSubNet("1:2:3:4:5:6:7:8").Match(ResolveIP("1:2:3:4:5:6:7:9")));
151 BOOST_CHECK(ResolveSubNet("1:2:3:4:5:6:7:0/112").Match(ResolveIP("1:2:3:4:5:6:7:1234")));
152 BOOST_CHECK(ResolveSubNet("192.168.0.1/24").Match(ResolveIP("192.168.0.2")));
153 BOOST_CHECK(ResolveSubNet("192.168.0.20/29").Match(ResolveIP("192.168.0.18")));
154 BOOST_CHECK(ResolveSubNet("1.2.2.1/24").Match(ResolveIP("1.2.2.4")));
155 BOOST_CHECK(ResolveSubNet("1.2.2.110/31").Match(ResolveIP("1.2.2.111")));
156 BOOST_CHECK(ResolveSubNet("1.2.2.20/26").Match(ResolveIP("1.2.2.63")));
157 // All-Matching IPv6 Matches arbitrary IPv4 and IPv6
158 BOOST_CHECK(ResolveSubNet("::/0").Match(ResolveIP("1:2:3:4:5:6:7:1234")));
159 BOOST_CHECK(ResolveSubNet("::/0").Match(ResolveIP("1.2.3.4")));
160 // All-Matching IPv4 does not Match IPv6
161 BOOST_CHECK(!ResolveSubNet("0.0.0.0/0").Match(ResolveIP("1:2:3:4:5:6:7:1234")));
162 // Invalid subnets Match nothing (not even invalid addresses)
163 BOOST_CHECK(!CSubNet().Match(ResolveIP("1.2.3.4")));
164 BOOST_CHECK(!ResolveSubNet("").Match(ResolveIP("4.5.6.7")));
165 BOOST_CHECK(!ResolveSubNet("bloop").Match(ResolveIP("0.0.0.0")));
166 BOOST_CHECK(!ResolveSubNet("bloop").Match(ResolveIP("hab")));
167 // Check valid/invalid
168 BOOST_CHECK(ResolveSubNet("1.2.3.0/0").IsValid());
169 BOOST_CHECK(!ResolveSubNet("1.2.3.0/-1").IsValid());
170 BOOST_CHECK(ResolveSubNet("1.2.3.0/32").IsValid());
171 BOOST_CHECK(!ResolveSubNet("1.2.3.0/33").IsValid());
172 BOOST_CHECK(ResolveSubNet("1:2:3:4:5:6:7:8/0").IsValid());
173 BOOST_CHECK(ResolveSubNet("1:2:3:4:5:6:7:8/33").IsValid());
174 BOOST_CHECK(!ResolveSubNet("1:2:3:4:5:6:7:8/-1").IsValid());
175 BOOST_CHECK(ResolveSubNet("1:2:3:4:5:6:7:8/128").IsValid());
176 BOOST_CHECK(!ResolveSubNet("1:2:3:4:5:6:7:8/129").IsValid());
177 BOOST_CHECK(!ResolveSubNet("fuzzy").IsValid());
179 //CNetAddr constructor test
180 BOOST_CHECK(CSubNet(ResolveIP("127.0.0.1")).IsValid());
181 BOOST_CHECK(CSubNet(ResolveIP("127.0.0.1")).Match(ResolveIP("127.0.0.1")));
182 BOOST_CHECK(!CSubNet(ResolveIP("127.0.0.1")).Match(ResolveIP("127.0.0.2")));
183 BOOST_CHECK(CSubNet(ResolveIP("127.0.0.1")).ToString() == "127.0.0.1/32");
185 CSubNet subnet = CSubNet(ResolveIP("1.2.3.4"), 32);
186 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/32");
187 subnet = CSubNet(ResolveIP("1.2.3.4"), 8);
188 BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/8");
189 subnet = CSubNet(ResolveIP("1.2.3.4"), 0);
190 BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/0");
192 subnet = CSubNet(ResolveIP("1.2.3.4"), ResolveIP("255.255.255.255"));
193 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/32");
194 subnet = CSubNet(ResolveIP("1.2.3.4"), ResolveIP("255.0.0.0"));
195 BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/8");
196 subnet = CSubNet(ResolveIP("1.2.3.4"), ResolveIP("0.0.0.0"));
197 BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/0");
199 BOOST_CHECK(CSubNet(ResolveIP("1:2:3:4:5:6:7:8")).IsValid());
200 BOOST_CHECK(CSubNet(ResolveIP("1:2:3:4:5:6:7:8")).Match(ResolveIP("1:2:3:4:5:6:7:8")));
201 BOOST_CHECK(!CSubNet(ResolveIP("1:2:3:4:5:6:7:8")).Match(ResolveIP("1:2:3:4:5:6:7:9")));
202 BOOST_CHECK(CSubNet(ResolveIP("1:2:3:4:5:6:7:8")).ToString() == "1:2:3:4:5:6:7:8/128");
204 subnet = ResolveSubNet("1.2.3.4/255.255.255.255");
205 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/32");
206 subnet = ResolveSubNet("1.2.3.4/255.255.255.254");
207 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/31");
208 subnet = ResolveSubNet("1.2.3.4/255.255.255.252");
209 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/30");
210 subnet = ResolveSubNet("1.2.3.4/255.255.255.248");
211 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/29");
212 subnet = ResolveSubNet("1.2.3.4/255.255.255.240");
213 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/28");
214 subnet = ResolveSubNet("1.2.3.4/255.255.255.224");
215 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/27");
216 subnet = ResolveSubNet("1.2.3.4/255.255.255.192");
217 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/26");
218 subnet = ResolveSubNet("1.2.3.4/255.255.255.128");
219 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/25");
220 subnet = ResolveSubNet("1.2.3.4/255.255.255.0");
221 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/24");
222 subnet = ResolveSubNet("1.2.3.4/255.255.254.0");
223 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.2.0/23");
224 subnet = ResolveSubNet("1.2.3.4/255.255.252.0");
225 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/22");
226 subnet = ResolveSubNet("1.2.3.4/255.255.248.0");
227 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/21");
228 subnet = ResolveSubNet("1.2.3.4/255.255.240.0");
229 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/20");
230 subnet = ResolveSubNet("1.2.3.4/255.255.224.0");
231 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/19");
232 subnet = ResolveSubNet("1.2.3.4/255.255.192.0");
233 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/18");
234 subnet = ResolveSubNet("1.2.3.4/255.255.128.0");
235 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/17");
236 subnet = ResolveSubNet("1.2.3.4/255.255.0.0");
237 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/16");
238 subnet = ResolveSubNet("1.2.3.4/255.254.0.0");
239 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/15");
240 subnet = ResolveSubNet("1.2.3.4/255.252.0.0");
241 BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/14");
242 subnet = ResolveSubNet("1.2.3.4/255.248.0.0");
243 BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/13");
244 subnet = ResolveSubNet("1.2.3.4/255.240.0.0");
245 BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/12");
246 subnet = ResolveSubNet("1.2.3.4/255.224.0.0");
247 BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/11");
248 subnet = ResolveSubNet("1.2.3.4/255.192.0.0");
249 BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/10");
250 subnet = ResolveSubNet("1.2.3.4/255.128.0.0");
251 BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/9");
252 subnet = ResolveSubNet("1.2.3.4/255.0.0.0");
253 BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/8");
254 subnet = ResolveSubNet("1.2.3.4/254.0.0.0");
255 BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/7");
256 subnet = ResolveSubNet("1.2.3.4/252.0.0.0");
257 BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/6");
258 subnet = ResolveSubNet("1.2.3.4/248.0.0.0");
259 BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/5");
260 subnet = ResolveSubNet("1.2.3.4/240.0.0.0");
261 BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/4");
262 subnet = ResolveSubNet("1.2.3.4/224.0.0.0");
263 BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/3");
264 subnet = ResolveSubNet("1.2.3.4/192.0.0.0");
265 BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/2");
266 subnet = ResolveSubNet("1.2.3.4/128.0.0.0");
267 BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/1");
268 subnet = ResolveSubNet("1.2.3.4/0.0.0.0");
269 BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/0");
271 subnet = ResolveSubNet("1:2:3:4:5:6:7:8/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
272 BOOST_CHECK_EQUAL(subnet.ToString(), "1:2:3:4:5:6:7:8/128");
273 subnet = ResolveSubNet("1:2:3:4:5:6:7:8/ffff:0000:0000:0000:0000:0000:0000:0000");
274 BOOST_CHECK_EQUAL(subnet.ToString(), "1::/16");
275 subnet = ResolveSubNet("1:2:3:4:5:6:7:8/0000:0000:0000:0000:0000:0000:0000:0000");
276 BOOST_CHECK_EQUAL(subnet.ToString(), "::/0");
277 subnet = ResolveSubNet("1.2.3.4/255.255.232.0");
278 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/255.255.232.0");
279 subnet = ResolveSubNet("1:2:3:4:5:6:7:8/ffff:ffff:ffff:fffe:ffff:ffff:ffff:ff0f");
280 BOOST_CHECK_EQUAL(subnet.ToString(), "1:2:3:4:5:6:7:8/ffff:ffff:ffff:fffe:ffff:ffff:ffff:ff0f");
284 BOOST_AUTO_TEST_CASE(netbase_getgroup)
287 BOOST_CHECK(ResolveIP("127.0.0.1").GetGroup() == std::vector<unsigned char>({0})); // Local -> !Routable()
288 BOOST_CHECK(ResolveIP("257.0.0.1").GetGroup() == std::vector<unsigned char>({0})); // !Valid -> !Routable()
289 BOOST_CHECK(ResolveIP("10.0.0.1").GetGroup() == std::vector<unsigned char>({0})); // RFC1918 -> !Routable()
290 BOOST_CHECK(ResolveIP("169.254.1.1").GetGroup() == std::vector<unsigned char>({0})); // RFC3927 -> !Routable()
291 BOOST_CHECK(ResolveIP("1.2.3.4").GetGroup() == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // IPv4
292 BOOST_CHECK(ResolveIP("::FFFF:0:102:304").GetGroup() == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // RFC6145
293 BOOST_CHECK(ResolveIP("64:FF9B::102:304").GetGroup() == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // RFC6052
294 BOOST_CHECK(ResolveIP("2002:102:304:9999:9999:9999:9999:9999").GetGroup() == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // RFC3964
295 BOOST_CHECK(ResolveIP("2001:0:9999:9999:9999:9999:FEFD:FCFB").GetGroup() == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // RFC4380
296 BOOST_CHECK(ResolveIP("FD87:D87E:EB43:edb1:8e4:3588:e546:35ca").GetGroup() == std::vector<unsigned char>({(unsigned char)NET_TOR, 239})); // Tor
297 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
298 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
300 // baz.net sha256 hash: 12929400eb4607c4ac075f087167e75286b179c693eb059a01774b864e8fe505
301 std::vector<unsigned char> internal_group = {NET_INTERNAL, 0x12, 0x92, 0x94, 0x00, 0xeb, 0x46, 0x07, 0xc4, 0xac, 0x07};
302 BOOST_CHECK(CreateInternal("baz.net").GetGroup() == internal_group);
305 BOOST_AUTO_TEST_SUITE_END()