Line data Source code
1 : // Copyright (c) 2012-2020 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.
4 :
5 : #include <net_permissions.h>
6 : #include <netbase.h>
7 : #include <test/util/setup_common.h>
8 : #include <util/strencodings.h>
9 : #include <util/translation.h>
10 :
11 : #include <string>
12 :
13 : #include <boost/test/unit_test.hpp>
14 :
15 89 : BOOST_FIXTURE_TEST_SUITE(netbase_tests, BasicTestingSetup)
16 :
17 95 : static CNetAddr ResolveIP(const std::string& ip)
18 : {
19 95 : CNetAddr addr;
20 95 : LookupHost(ip, addr, false);
21 : return addr;
22 95 : }
23 :
24 77 : static CSubNet ResolveSubNet(const std::string& subnet)
25 : {
26 77 : CSubNet ret;
27 77 : LookupSubNet(subnet, ret);
28 : return ret;
29 77 : }
30 :
31 4 : static CNetAddr CreateInternal(const std::string& host)
32 : {
33 4 : CNetAddr addr;
34 4 : addr.SetInternal(host);
35 : return addr;
36 4 : }
37 :
38 95 : BOOST_AUTO_TEST_CASE(netbase_networks)
39 : {
40 1 : BOOST_CHECK(ResolveIP("127.0.0.1").GetNetwork() == NET_UNROUTABLE);
41 1 : BOOST_CHECK(ResolveIP("::1").GetNetwork() == NET_UNROUTABLE);
42 1 : BOOST_CHECK(ResolveIP("8.8.8.8").GetNetwork() == NET_IPV4);
43 1 : BOOST_CHECK(ResolveIP("2001::8888").GetNetwork() == NET_IPV6);
44 1 : BOOST_CHECK(ResolveIP("FD87:D87E:EB43:edb1:8e4:3588:e546:35ca").GetNetwork() == NET_ONION);
45 1 : BOOST_CHECK(CreateInternal("foo.com").GetNetwork() == NET_INTERNAL);
46 :
47 1 : }
48 :
49 95 : BOOST_AUTO_TEST_CASE(netbase_properties)
50 : {
51 :
52 1 : BOOST_CHECK(ResolveIP("127.0.0.1").IsIPv4());
53 1 : BOOST_CHECK(ResolveIP("::FFFF:192.168.1.1").IsIPv4());
54 1 : BOOST_CHECK(ResolveIP("::1").IsIPv6());
55 1 : BOOST_CHECK(ResolveIP("10.0.0.1").IsRFC1918());
56 1 : BOOST_CHECK(ResolveIP("192.168.1.1").IsRFC1918());
57 1 : BOOST_CHECK(ResolveIP("172.31.255.255").IsRFC1918());
58 1 : BOOST_CHECK(ResolveIP("198.18.0.0").IsRFC2544());
59 1 : BOOST_CHECK(ResolveIP("198.19.255.255").IsRFC2544());
60 1 : BOOST_CHECK(ResolveIP("2001:0DB8::").IsRFC3849());
61 1 : BOOST_CHECK(ResolveIP("169.254.1.1").IsRFC3927());
62 1 : BOOST_CHECK(ResolveIP("2002::1").IsRFC3964());
63 1 : BOOST_CHECK(ResolveIP("FC00::").IsRFC4193());
64 1 : BOOST_CHECK(ResolveIP("2001::2").IsRFC4380());
65 1 : BOOST_CHECK(ResolveIP("2001:10::").IsRFC4843());
66 1 : BOOST_CHECK(ResolveIP("2001:20::").IsRFC7343());
67 1 : BOOST_CHECK(ResolveIP("FE80::").IsRFC4862());
68 1 : BOOST_CHECK(ResolveIP("64:FF9B::").IsRFC6052());
69 1 : BOOST_CHECK(ResolveIP("FD87:D87E:EB43:edb1:8e4:3588:e546:35ca").IsTor());
70 1 : BOOST_CHECK(ResolveIP("127.0.0.1").IsLocal());
71 1 : BOOST_CHECK(ResolveIP("::1").IsLocal());
72 1 : BOOST_CHECK(ResolveIP("8.8.8.8").IsRoutable());
73 1 : BOOST_CHECK(ResolveIP("2001::1").IsRoutable());
74 1 : BOOST_CHECK(ResolveIP("127.0.0.1").IsValid());
75 1 : BOOST_CHECK(CreateInternal("FD6B:88C0:8724:edb1:8e4:3588:e546:35ca").IsInternal());
76 1 : BOOST_CHECK(CreateInternal("bar.com").IsInternal());
77 :
78 1 : }
79 :
80 15 : bool static TestSplitHost(std::string test, std::string host, int port)
81 : {
82 15 : std::string hostOut;
83 15 : int portOut = -1;
84 15 : SplitHostPort(test, portOut, hostOut);
85 15 : return hostOut == host && port == portOut;
86 15 : }
87 :
88 95 : BOOST_AUTO_TEST_CASE(netbase_splithost)
89 : {
90 1 : BOOST_CHECK(TestSplitHost("www.bitcoincore.org", "www.bitcoincore.org", -1));
91 1 : BOOST_CHECK(TestSplitHost("[www.bitcoincore.org]", "www.bitcoincore.org", -1));
92 1 : BOOST_CHECK(TestSplitHost("www.bitcoincore.org:80", "www.bitcoincore.org", 80));
93 1 : BOOST_CHECK(TestSplitHost("[www.bitcoincore.org]:80", "www.bitcoincore.org", 80));
94 1 : BOOST_CHECK(TestSplitHost("127.0.0.1", "127.0.0.1", -1));
95 1 : BOOST_CHECK(TestSplitHost("127.0.0.1:8333", "127.0.0.1", 8333));
96 1 : BOOST_CHECK(TestSplitHost("[127.0.0.1]", "127.0.0.1", -1));
97 1 : BOOST_CHECK(TestSplitHost("[127.0.0.1]:8333", "127.0.0.1", 8333));
98 1 : BOOST_CHECK(TestSplitHost("::ffff:127.0.0.1", "::ffff:127.0.0.1", -1));
99 1 : BOOST_CHECK(TestSplitHost("[::ffff:127.0.0.1]:8333", "::ffff:127.0.0.1", 8333));
100 1 : BOOST_CHECK(TestSplitHost("[::]:8333", "::", 8333));
101 1 : BOOST_CHECK(TestSplitHost("::8333", "::8333", -1));
102 1 : BOOST_CHECK(TestSplitHost(":8333", "", 8333));
103 1 : BOOST_CHECK(TestSplitHost("[]:8333", "", 8333));
104 1 : BOOST_CHECK(TestSplitHost("", "", -1));
105 1 : }
106 :
107 9 : bool static TestParse(std::string src, std::string canon)
108 : {
109 9 : CService addr(LookupNumeric(src, 65535));
110 9 : return canon == addr.ToString();
111 9 : }
112 :
113 95 : BOOST_AUTO_TEST_CASE(netbase_lookupnumeric)
114 : {
115 1 : BOOST_CHECK(TestParse("127.0.0.1", "127.0.0.1:65535"));
116 1 : BOOST_CHECK(TestParse("127.0.0.1:8333", "127.0.0.1:8333"));
117 1 : BOOST_CHECK(TestParse("::ffff:127.0.0.1", "127.0.0.1:65535"));
118 1 : BOOST_CHECK(TestParse("::", "[::]:65535"));
119 1 : BOOST_CHECK(TestParse("[::]:8333", "[::]:8333"));
120 1 : BOOST_CHECK(TestParse("[127.0.0.1]", "127.0.0.1:65535"));
121 1 : BOOST_CHECK(TestParse(":::", "[::]:0"));
122 :
123 : // verify that an internal address fails to resolve
124 1 : BOOST_CHECK(TestParse("[fd6b:88c0:8724:1:2:3:4:5]", "[::]:0"));
125 : // and that a one-off resolves correctly
126 1 : BOOST_CHECK(TestParse("[fd6c:88c0:8724:1:2:3:4:5]", "[fd6c:88c0:8724:1:2:3:4:5]:65535"));
127 1 : }
128 :
129 95 : BOOST_AUTO_TEST_CASE(onioncat_test)
130 : {
131 : // values from https://web.archive.org/web/20121122003543/http://www.cypherpunk.at/onioncat/wiki/OnionCat
132 1 : CNetAddr addr1(ResolveIP("5wyqrzbvrdsumnok.onion"));
133 1 : CNetAddr addr2(ResolveIP("FD87:D87E:EB43:edb1:8e4:3588:e546:35ca"));
134 1 : BOOST_CHECK(addr1 == addr2);
135 1 : BOOST_CHECK(addr1.IsTor());
136 1 : BOOST_CHECK(addr1.ToStringIP() == "5wyqrzbvrdsumnok.onion");
137 1 : BOOST_CHECK(addr1.IsRoutable());
138 :
139 1 : }
140 :
141 95 : BOOST_AUTO_TEST_CASE(embedded_test)
142 : {
143 1 : CNetAddr addr1(ResolveIP("1.2.3.4"));
144 1 : CNetAddr addr2(ResolveIP("::FFFF:0102:0304"));
145 1 : BOOST_CHECK(addr2.IsIPv4());
146 1 : BOOST_CHECK_EQUAL(addr1.ToString(), addr2.ToString());
147 1 : }
148 :
149 95 : BOOST_AUTO_TEST_CASE(subnet_test)
150 : {
151 :
152 1 : BOOST_CHECK(ResolveSubNet("1.2.3.0/24") == ResolveSubNet("1.2.3.0/255.255.255.0"));
153 1 : BOOST_CHECK(ResolveSubNet("1.2.3.0/24") != ResolveSubNet("1.2.4.0/255.255.255.0"));
154 1 : BOOST_CHECK(ResolveSubNet("1.2.3.0/24").Match(ResolveIP("1.2.3.4")));
155 1 : BOOST_CHECK(!ResolveSubNet("1.2.2.0/24").Match(ResolveIP("1.2.3.4")));
156 1 : BOOST_CHECK(ResolveSubNet("1.2.3.4").Match(ResolveIP("1.2.3.4")));
157 1 : BOOST_CHECK(ResolveSubNet("1.2.3.4/32").Match(ResolveIP("1.2.3.4")));
158 1 : BOOST_CHECK(!ResolveSubNet("1.2.3.4").Match(ResolveIP("5.6.7.8")));
159 1 : BOOST_CHECK(!ResolveSubNet("1.2.3.4/32").Match(ResolveIP("5.6.7.8")));
160 1 : BOOST_CHECK(ResolveSubNet("::ffff:127.0.0.1").Match(ResolveIP("127.0.0.1")));
161 1 : BOOST_CHECK(ResolveSubNet("1:2:3:4:5:6:7:8").Match(ResolveIP("1:2:3:4:5:6:7:8")));
162 1 : BOOST_CHECK(!ResolveSubNet("1:2:3:4:5:6:7:8").Match(ResolveIP("1:2:3:4:5:6:7:9")));
163 1 : BOOST_CHECK(ResolveSubNet("1:2:3:4:5:6:7:0/112").Match(ResolveIP("1:2:3:4:5:6:7:1234")));
164 1 : BOOST_CHECK(ResolveSubNet("192.168.0.1/24").Match(ResolveIP("192.168.0.2")));
165 1 : BOOST_CHECK(ResolveSubNet("192.168.0.20/29").Match(ResolveIP("192.168.0.18")));
166 1 : BOOST_CHECK(ResolveSubNet("1.2.2.1/24").Match(ResolveIP("1.2.2.4")));
167 1 : BOOST_CHECK(ResolveSubNet("1.2.2.110/31").Match(ResolveIP("1.2.2.111")));
168 1 : BOOST_CHECK(ResolveSubNet("1.2.2.20/26").Match(ResolveIP("1.2.2.63")));
169 : // All-Matching IPv6 Matches arbitrary IPv6
170 1 : BOOST_CHECK(ResolveSubNet("::/0").Match(ResolveIP("1:2:3:4:5:6:7:1234")));
171 : // But not `::` or `0.0.0.0` because they are considered invalid addresses
172 1 : BOOST_CHECK(!ResolveSubNet("::/0").Match(ResolveIP("::")));
173 1 : BOOST_CHECK(!ResolveSubNet("::/0").Match(ResolveIP("0.0.0.0")));
174 : // Addresses from one network (IPv4) don't belong to subnets of another network (IPv6)
175 1 : BOOST_CHECK(!ResolveSubNet("::/0").Match(ResolveIP("1.2.3.4")));
176 : // All-Matching IPv4 does not Match IPv6
177 1 : BOOST_CHECK(!ResolveSubNet("0.0.0.0/0").Match(ResolveIP("1:2:3:4:5:6:7:1234")));
178 : // Invalid subnets Match nothing (not even invalid addresses)
179 1 : BOOST_CHECK(!CSubNet().Match(ResolveIP("1.2.3.4")));
180 1 : BOOST_CHECK(!ResolveSubNet("").Match(ResolveIP("4.5.6.7")));
181 1 : BOOST_CHECK(!ResolveSubNet("bloop").Match(ResolveIP("0.0.0.0")));
182 1 : BOOST_CHECK(!ResolveSubNet("bloop").Match(ResolveIP("hab")));
183 : // Check valid/invalid
184 1 : BOOST_CHECK(ResolveSubNet("1.2.3.0/0").IsValid());
185 1 : BOOST_CHECK(!ResolveSubNet("1.2.3.0/-1").IsValid());
186 1 : BOOST_CHECK(ResolveSubNet("1.2.3.0/32").IsValid());
187 1 : BOOST_CHECK(!ResolveSubNet("1.2.3.0/33").IsValid());
188 1 : BOOST_CHECK(!ResolveSubNet("1.2.3.0/300").IsValid());
189 1 : BOOST_CHECK(ResolveSubNet("1:2:3:4:5:6:7:8/0").IsValid());
190 1 : BOOST_CHECK(ResolveSubNet("1:2:3:4:5:6:7:8/33").IsValid());
191 1 : BOOST_CHECK(!ResolveSubNet("1:2:3:4:5:6:7:8/-1").IsValid());
192 1 : BOOST_CHECK(ResolveSubNet("1:2:3:4:5:6:7:8/128").IsValid());
193 1 : BOOST_CHECK(!ResolveSubNet("1:2:3:4:5:6:7:8/129").IsValid());
194 1 : BOOST_CHECK(!ResolveSubNet("fuzzy").IsValid());
195 :
196 : //CNetAddr constructor test
197 1 : BOOST_CHECK(CSubNet(ResolveIP("127.0.0.1")).IsValid());
198 1 : BOOST_CHECK(CSubNet(ResolveIP("127.0.0.1")).Match(ResolveIP("127.0.0.1")));
199 1 : BOOST_CHECK(!CSubNet(ResolveIP("127.0.0.1")).Match(ResolveIP("127.0.0.2")));
200 1 : BOOST_CHECK(CSubNet(ResolveIP("127.0.0.1")).ToString() == "127.0.0.1/32");
201 :
202 1 : CSubNet subnet = CSubNet(ResolveIP("1.2.3.4"), 32);
203 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/32");
204 1 : subnet = CSubNet(ResolveIP("1.2.3.4"), 8);
205 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/8");
206 1 : subnet = CSubNet(ResolveIP("1.2.3.4"), 0);
207 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/0");
208 :
209 1 : subnet = CSubNet(ResolveIP("1.2.3.4"), ResolveIP("255.255.255.255"));
210 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/32");
211 1 : subnet = CSubNet(ResolveIP("1.2.3.4"), ResolveIP("255.0.0.0"));
212 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/8");
213 1 : subnet = CSubNet(ResolveIP("1.2.3.4"), ResolveIP("0.0.0.0"));
214 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/0");
215 :
216 1 : BOOST_CHECK(CSubNet(ResolveIP("1:2:3:4:5:6:7:8")).IsValid());
217 1 : BOOST_CHECK(CSubNet(ResolveIP("1:2:3:4:5:6:7:8")).Match(ResolveIP("1:2:3:4:5:6:7:8")));
218 1 : BOOST_CHECK(!CSubNet(ResolveIP("1:2:3:4:5:6:7:8")).Match(ResolveIP("1:2:3:4:5:6:7:9")));
219 1 : BOOST_CHECK(CSubNet(ResolveIP("1:2:3:4:5:6:7:8")).ToString() == "1:2:3:4:5:6:7:8/128");
220 : // IPv4 address with IPv6 netmask or the other way around.
221 1 : BOOST_CHECK(!CSubNet(ResolveIP("1.1.1.1"), ResolveIP("ffff::")).IsValid());
222 1 : BOOST_CHECK(!CSubNet(ResolveIP("::1"), ResolveIP("255.0.0.0")).IsValid());
223 : // Can't subnet TOR (or any other non-IPv4 and non-IPv6 network).
224 1 : BOOST_CHECK(!CSubNet(ResolveIP("5wyqrzbvrdsumnok.onion"), ResolveIP("255.0.0.0")).IsValid());
225 :
226 1 : subnet = ResolveSubNet("1.2.3.4/255.255.255.255");
227 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/32");
228 1 : subnet = ResolveSubNet("1.2.3.4/255.255.255.254");
229 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/31");
230 1 : subnet = ResolveSubNet("1.2.3.4/255.255.255.252");
231 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/30");
232 1 : subnet = ResolveSubNet("1.2.3.4/255.255.255.248");
233 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/29");
234 1 : subnet = ResolveSubNet("1.2.3.4/255.255.255.240");
235 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/28");
236 1 : subnet = ResolveSubNet("1.2.3.4/255.255.255.224");
237 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/27");
238 1 : subnet = ResolveSubNet("1.2.3.4/255.255.255.192");
239 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/26");
240 1 : subnet = ResolveSubNet("1.2.3.4/255.255.255.128");
241 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/25");
242 1 : subnet = ResolveSubNet("1.2.3.4/255.255.255.0");
243 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/24");
244 1 : subnet = ResolveSubNet("1.2.3.4/255.255.254.0");
245 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.2.0/23");
246 1 : subnet = ResolveSubNet("1.2.3.4/255.255.252.0");
247 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/22");
248 1 : subnet = ResolveSubNet("1.2.3.4/255.255.248.0");
249 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/21");
250 1 : subnet = ResolveSubNet("1.2.3.4/255.255.240.0");
251 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/20");
252 1 : subnet = ResolveSubNet("1.2.3.4/255.255.224.0");
253 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/19");
254 1 : subnet = ResolveSubNet("1.2.3.4/255.255.192.0");
255 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/18");
256 1 : subnet = ResolveSubNet("1.2.3.4/255.255.128.0");
257 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/17");
258 1 : subnet = ResolveSubNet("1.2.3.4/255.255.0.0");
259 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/16");
260 1 : subnet = ResolveSubNet("1.2.3.4/255.254.0.0");
261 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/15");
262 1 : subnet = ResolveSubNet("1.2.3.4/255.252.0.0");
263 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/14");
264 1 : subnet = ResolveSubNet("1.2.3.4/255.248.0.0");
265 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/13");
266 1 : subnet = ResolveSubNet("1.2.3.4/255.240.0.0");
267 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/12");
268 1 : subnet = ResolveSubNet("1.2.3.4/255.224.0.0");
269 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/11");
270 1 : subnet = ResolveSubNet("1.2.3.4/255.192.0.0");
271 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/10");
272 1 : subnet = ResolveSubNet("1.2.3.4/255.128.0.0");
273 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/9");
274 1 : subnet = ResolveSubNet("1.2.3.4/255.0.0.0");
275 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/8");
276 1 : subnet = ResolveSubNet("1.2.3.4/254.0.0.0");
277 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/7");
278 1 : subnet = ResolveSubNet("1.2.3.4/252.0.0.0");
279 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/6");
280 1 : subnet = ResolveSubNet("1.2.3.4/248.0.0.0");
281 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/5");
282 1 : subnet = ResolveSubNet("1.2.3.4/240.0.0.0");
283 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/4");
284 1 : subnet = ResolveSubNet("1.2.3.4/224.0.0.0");
285 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/3");
286 1 : subnet = ResolveSubNet("1.2.3.4/192.0.0.0");
287 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/2");
288 1 : subnet = ResolveSubNet("1.2.3.4/128.0.0.0");
289 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/1");
290 1 : subnet = ResolveSubNet("1.2.3.4/0.0.0.0");
291 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/0");
292 :
293 1 : subnet = ResolveSubNet("1:2:3:4:5:6:7:8/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
294 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1:2:3:4:5:6:7:8/128");
295 1 : subnet = ResolveSubNet("1:2:3:4:5:6:7:8/ffff:0000:0000:0000:0000:0000:0000:0000");
296 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1::/16");
297 1 : subnet = ResolveSubNet("1:2:3:4:5:6:7:8/0000:0000:0000:0000:0000:0000:0000:0000");
298 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "::/0");
299 : // Invalid netmasks (with 1-bits after 0-bits)
300 1 : subnet = ResolveSubNet("1.2.3.4/255.255.232.0");
301 1 : BOOST_CHECK(!subnet.IsValid());
302 1 : subnet = ResolveSubNet("1.2.3.4/255.0.255.255");
303 1 : BOOST_CHECK(!subnet.IsValid());
304 1 : subnet = ResolveSubNet("1:2:3:4:5:6:7:8/ffff:ffff:ffff:fffe:ffff:ffff:ffff:ff0f");
305 1 : BOOST_CHECK(!subnet.IsValid());
306 1 : }
307 :
308 95 : BOOST_AUTO_TEST_CASE(netbase_getgroup)
309 : {
310 1 : std::vector<bool> asmap; // use /16
311 1 : BOOST_CHECK(ResolveIP("127.0.0.1").GetGroup(asmap) == std::vector<unsigned char>({0})); // Local -> !Routable()
312 1 : BOOST_CHECK(ResolveIP("257.0.0.1").GetGroup(asmap) == std::vector<unsigned char>({0})); // !Valid -> !Routable()
313 1 : BOOST_CHECK(ResolveIP("10.0.0.1").GetGroup(asmap) == std::vector<unsigned char>({0})); // RFC1918 -> !Routable()
314 1 : BOOST_CHECK(ResolveIP("169.254.1.1").GetGroup(asmap) == std::vector<unsigned char>({0})); // RFC3927 -> !Routable()
315 1 : BOOST_CHECK(ResolveIP("1.2.3.4").GetGroup(asmap) == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // IPv4
316 1 : BOOST_CHECK(ResolveIP("::FFFF:0:102:304").GetGroup(asmap) == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // RFC6145
317 1 : BOOST_CHECK(ResolveIP("64:FF9B::102:304").GetGroup(asmap) == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // RFC6052
318 1 : BOOST_CHECK(ResolveIP("2002:102:304:9999:9999:9999:9999:9999").GetGroup(asmap) == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // RFC3964
319 1 : BOOST_CHECK(ResolveIP("2001:0:9999:9999:9999:9999:FEFD:FCFB").GetGroup(asmap) == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // RFC4380
320 1 : BOOST_CHECK(ResolveIP("FD87:D87E:EB43:edb1:8e4:3588:e546:35ca").GetGroup(asmap) == std::vector<unsigned char>({(unsigned char)NET_ONION, 239})); // Tor
321 1 : BOOST_CHECK(ResolveIP("2001:470:abcd:9999:9999:9999:9999:9999").GetGroup(asmap) == std::vector<unsigned char>({(unsigned char)NET_IPV6, 32, 1, 4, 112, 175})); //he.net
322 1 : BOOST_CHECK(ResolveIP("2001:2001:9999:9999:9999:9999:9999:9999").GetGroup(asmap) == std::vector<unsigned char>({(unsigned char)NET_IPV6, 32, 1, 32, 1})); //IPv6
323 :
324 : // baz.net sha256 hash: 12929400eb4607c4ac075f087167e75286b179c693eb059a01774b864e8fe505
325 1 : std::vector<unsigned char> internal_group = {NET_INTERNAL, 0x12, 0x92, 0x94, 0x00, 0xeb, 0x46, 0x07, 0xc4, 0xac, 0x07};
326 1 : BOOST_CHECK(CreateInternal("baz.net").GetGroup(asmap) == internal_group);
327 1 : }
328 :
329 95 : BOOST_AUTO_TEST_CASE(netbase_parsenetwork)
330 : {
331 1 : BOOST_CHECK_EQUAL(ParseNetwork("ipv4"), NET_IPV4);
332 1 : BOOST_CHECK_EQUAL(ParseNetwork("ipv6"), NET_IPV6);
333 1 : BOOST_CHECK_EQUAL(ParseNetwork("onion"), NET_ONION);
334 1 : BOOST_CHECK_EQUAL(ParseNetwork("tor"), NET_ONION);
335 :
336 1 : BOOST_CHECK_EQUAL(ParseNetwork("IPv4"), NET_IPV4);
337 1 : BOOST_CHECK_EQUAL(ParseNetwork("IPv6"), NET_IPV6);
338 1 : BOOST_CHECK_EQUAL(ParseNetwork("ONION"), NET_ONION);
339 1 : BOOST_CHECK_EQUAL(ParseNetwork("TOR"), NET_ONION);
340 :
341 1 : BOOST_CHECK_EQUAL(ParseNetwork(":)"), NET_UNROUTABLE);
342 1 : BOOST_CHECK_EQUAL(ParseNetwork("tÖr"), NET_UNROUTABLE);
343 1 : BOOST_CHECK_EQUAL(ParseNetwork("\xfe\xff"), NET_UNROUTABLE);
344 1 : BOOST_CHECK_EQUAL(ParseNetwork(""), NET_UNROUTABLE);
345 1 : }
346 :
347 95 : BOOST_AUTO_TEST_CASE(netpermissions_test)
348 : {
349 1 : bilingual_str error;
350 1 : NetWhitebindPermissions whitebindPermissions;
351 1 : NetWhitelistPermissions whitelistPermissions;
352 :
353 : // Detect invalid white bind
354 1 : BOOST_CHECK(!NetWhitebindPermissions::TryParse("", whitebindPermissions, error));
355 1 : BOOST_CHECK(error.original.find("Cannot resolve -whitebind address") != std::string::npos);
356 1 : BOOST_CHECK(!NetWhitebindPermissions::TryParse("127.0.0.1", whitebindPermissions, error));
357 1 : BOOST_CHECK(error.original.find("Need to specify a port with -whitebind") != std::string::npos);
358 1 : BOOST_CHECK(!NetWhitebindPermissions::TryParse("", whitebindPermissions, error));
359 :
360 : // If no permission flags, assume backward compatibility
361 1 : BOOST_CHECK(NetWhitebindPermissions::TryParse("1.2.3.4:32", whitebindPermissions, error));
362 1 : BOOST_CHECK(error.empty());
363 1 : BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, PF_ISIMPLICIT);
364 1 : BOOST_CHECK(NetPermissions::HasFlag(whitebindPermissions.m_flags, PF_ISIMPLICIT));
365 1 : NetPermissions::ClearFlag(whitebindPermissions.m_flags, PF_ISIMPLICIT);
366 1 : BOOST_CHECK(!NetPermissions::HasFlag(whitebindPermissions.m_flags, PF_ISIMPLICIT));
367 1 : BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, PF_NONE);
368 1 : NetPermissions::AddFlag(whitebindPermissions.m_flags, PF_ISIMPLICIT);
369 1 : BOOST_CHECK(NetPermissions::HasFlag(whitebindPermissions.m_flags, PF_ISIMPLICIT));
370 :
371 : // Can set one permission
372 1 : BOOST_CHECK(NetWhitebindPermissions::TryParse("bloom@1.2.3.4:32", whitebindPermissions, error));
373 1 : BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, PF_BLOOMFILTER);
374 1 : BOOST_CHECK(NetWhitebindPermissions::TryParse("@1.2.3.4:32", whitebindPermissions, error));
375 1 : BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, PF_NONE);
376 :
377 : // Happy path, can parse flags
378 1 : BOOST_CHECK(NetWhitebindPermissions::TryParse("bloom,forcerelay@1.2.3.4:32", whitebindPermissions, error));
379 : // forcerelay should also activate the relay permission
380 1 : BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, PF_BLOOMFILTER | PF_FORCERELAY | PF_RELAY);
381 1 : BOOST_CHECK(NetWhitebindPermissions::TryParse("bloom,relay,noban@1.2.3.4:32", whitebindPermissions, error));
382 1 : BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, PF_BLOOMFILTER | PF_RELAY | PF_NOBAN);
383 1 : BOOST_CHECK(NetWhitebindPermissions::TryParse("bloom,forcerelay,noban@1.2.3.4:32", whitebindPermissions, error));
384 1 : BOOST_CHECK(NetWhitebindPermissions::TryParse("all@1.2.3.4:32", whitebindPermissions, error));
385 1 : BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, PF_ALL);
386 :
387 : // Allow dups
388 1 : BOOST_CHECK(NetWhitebindPermissions::TryParse("bloom,relay,noban,noban@1.2.3.4:32", whitebindPermissions, error));
389 1 : BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, PF_BLOOMFILTER | PF_RELAY | PF_NOBAN);
390 :
391 : // Allow empty
392 1 : BOOST_CHECK(NetWhitebindPermissions::TryParse("bloom,relay,,noban@1.2.3.4:32", whitebindPermissions, error));
393 1 : BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, PF_BLOOMFILTER | PF_RELAY | PF_NOBAN);
394 1 : BOOST_CHECK(NetWhitebindPermissions::TryParse(",@1.2.3.4:32", whitebindPermissions, error));
395 1 : BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, PF_NONE);
396 1 : BOOST_CHECK(NetWhitebindPermissions::TryParse(",,@1.2.3.4:32", whitebindPermissions, error));
397 1 : BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, PF_NONE);
398 :
399 : // Detect invalid flag
400 1 : BOOST_CHECK(!NetWhitebindPermissions::TryParse("bloom,forcerelay,oopsie@1.2.3.4:32", whitebindPermissions, error));
401 1 : BOOST_CHECK(error.original.find("Invalid P2P permission") != std::string::npos);
402 :
403 : // Check netmask error
404 1 : BOOST_CHECK(!NetWhitelistPermissions::TryParse("bloom,forcerelay,noban@1.2.3.4:32", whitelistPermissions, error));
405 1 : BOOST_CHECK(error.original.find("Invalid netmask specified in -whitelist") != std::string::npos);
406 :
407 : // Happy path for whitelist parsing
408 1 : BOOST_CHECK(NetWhitelistPermissions::TryParse("noban@1.2.3.4", whitelistPermissions, error));
409 1 : BOOST_CHECK_EQUAL(whitelistPermissions.m_flags, PF_NOBAN);
410 1 : BOOST_CHECK(NetWhitelistPermissions::TryParse("bloom,forcerelay,noban,relay@1.2.3.4/32", whitelistPermissions, error));
411 1 : BOOST_CHECK_EQUAL(whitelistPermissions.m_flags, PF_BLOOMFILTER | PF_FORCERELAY | PF_NOBAN | PF_RELAY);
412 1 : BOOST_CHECK(error.empty());
413 1 : BOOST_CHECK_EQUAL(whitelistPermissions.m_subnet.ToString(), "1.2.3.4/32");
414 1 : BOOST_CHECK(NetWhitelistPermissions::TryParse("bloom,forcerelay,noban,relay,mempool@1.2.3.4/32", whitelistPermissions, error));
415 :
416 1 : const auto strings = NetPermissions::ToStrings(PF_ALL);
417 1 : BOOST_CHECK_EQUAL(strings.size(), 7U);
418 1 : BOOST_CHECK(std::find(strings.begin(), strings.end(), "bloomfilter") != strings.end());
419 1 : BOOST_CHECK(std::find(strings.begin(), strings.end(), "forcerelay") != strings.end());
420 1 : BOOST_CHECK(std::find(strings.begin(), strings.end(), "relay") != strings.end());
421 1 : BOOST_CHECK(std::find(strings.begin(), strings.end(), "noban") != strings.end());
422 1 : BOOST_CHECK(std::find(strings.begin(), strings.end(), "mempool") != strings.end());
423 1 : BOOST_CHECK(std::find(strings.begin(), strings.end(), "download") != strings.end());
424 1 : BOOST_CHECK(std::find(strings.begin(), strings.end(), "addr") != strings.end());
425 1 : }
426 :
427 95 : BOOST_AUTO_TEST_CASE(netbase_dont_resolve_strings_with_embedded_nul_characters)
428 : {
429 1 : CNetAddr addr;
430 1 : BOOST_CHECK(LookupHost(std::string("127.0.0.1", 9), addr, false));
431 1 : BOOST_CHECK(!LookupHost(std::string("127.0.0.1\0", 10), addr, false));
432 1 : BOOST_CHECK(!LookupHost(std::string("127.0.0.1\0example.com", 21), addr, false));
433 1 : BOOST_CHECK(!LookupHost(std::string("127.0.0.1\0example.com\0", 22), addr, false));
434 1 : CSubNet ret;
435 1 : BOOST_CHECK(LookupSubNet(std::string("1.2.3.0/24", 10), ret));
436 1 : BOOST_CHECK(!LookupSubNet(std::string("1.2.3.0/24\0", 11), ret));
437 1 : BOOST_CHECK(!LookupSubNet(std::string("1.2.3.0/24\0example.com", 22), ret));
438 1 : BOOST_CHECK(!LookupSubNet(std::string("1.2.3.0/24\0example.com\0", 23), ret));
439 : // We only do subnetting for IPv4 and IPv6
440 1 : BOOST_CHECK(!LookupSubNet(std::string("5wyqrzbvrdsumnok.onion", 22), ret));
441 1 : BOOST_CHECK(!LookupSubNet(std::string("5wyqrzbvrdsumnok.onion\0", 23), ret));
442 1 : BOOST_CHECK(!LookupSubNet(std::string("5wyqrzbvrdsumnok.onion\0example.com", 34), ret));
443 1 : BOOST_CHECK(!LookupSubNet(std::string("5wyqrzbvrdsumnok.onion\0example.com\0", 35), ret));
444 1 : }
445 :
446 89 : BOOST_AUTO_TEST_SUITE_END()
|