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