Bitcoin Core  22.99.0
P2P Digital Currency
netbase_tests.cpp
Go to the documentation of this file.
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 <netaddress.h>
7 #include <netbase.h>
8 #include <protocol.h>
9 #include <serialize.h>
10 #include <streams.h>
11 #include <test/util/setup_common.h>
12 #include <util/strencodings.h>
13 #include <util/translation.h>
14 #include <version.h>
15 
16 #include <string>
17 
18 #include <boost/test/unit_test.hpp>
19 
20 using namespace std::literals;
21 
23 
24 static CNetAddr ResolveIP(const std::string& ip)
25 {
26  CNetAddr addr;
27  LookupHost(ip, addr, false);
28  return addr;
29 }
30 
31 static CSubNet ResolveSubNet(const std::string& subnet)
32 {
33  CSubNet ret;
34  LookupSubNet(subnet, ret);
35  return ret;
36 }
37 
38 static CNetAddr CreateInternal(const std::string& host)
39 {
40  CNetAddr addr;
41  addr.SetInternal(host);
42  return addr;
43 }
44 
45 BOOST_AUTO_TEST_CASE(netbase_networks)
46 {
47  BOOST_CHECK(ResolveIP("127.0.0.1").GetNetwork() == NET_UNROUTABLE);
48  BOOST_CHECK(ResolveIP("::1").GetNetwork() == NET_UNROUTABLE);
49  BOOST_CHECK(ResolveIP("8.8.8.8").GetNetwork() == NET_IPV4);
50  BOOST_CHECK(ResolveIP("2001::8888").GetNetwork() == NET_IPV6);
51  BOOST_CHECK(ResolveIP("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion").GetNetwork() == NET_ONION);
52  BOOST_CHECK(CreateInternal("foo.com").GetNetwork() == NET_INTERNAL);
53 }
54 
55 BOOST_AUTO_TEST_CASE(netbase_properties)
56 {
57 
58  BOOST_CHECK(ResolveIP("127.0.0.1").IsIPv4());
59  BOOST_CHECK(ResolveIP("::FFFF:192.168.1.1").IsIPv4());
60  BOOST_CHECK(ResolveIP("::1").IsIPv6());
61  BOOST_CHECK(ResolveIP("10.0.0.1").IsRFC1918());
62  BOOST_CHECK(ResolveIP("192.168.1.1").IsRFC1918());
63  BOOST_CHECK(ResolveIP("172.31.255.255").IsRFC1918());
64  BOOST_CHECK(ResolveIP("198.18.0.0").IsRFC2544());
65  BOOST_CHECK(ResolveIP("198.19.255.255").IsRFC2544());
66  BOOST_CHECK(ResolveIP("2001:0DB8::").IsRFC3849());
67  BOOST_CHECK(ResolveIP("169.254.1.1").IsRFC3927());
68  BOOST_CHECK(ResolveIP("2002::1").IsRFC3964());
69  BOOST_CHECK(ResolveIP("FC00::").IsRFC4193());
70  BOOST_CHECK(ResolveIP("2001::2").IsRFC4380());
71  BOOST_CHECK(ResolveIP("2001:10::").IsRFC4843());
72  BOOST_CHECK(ResolveIP("2001:20::").IsRFC7343());
73  BOOST_CHECK(ResolveIP("FE80::").IsRFC4862());
74  BOOST_CHECK(ResolveIP("64:FF9B::").IsRFC6052());
75  BOOST_CHECK(ResolveIP("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion").IsTor());
76  BOOST_CHECK(ResolveIP("127.0.0.1").IsLocal());
77  BOOST_CHECK(ResolveIP("::1").IsLocal());
78  BOOST_CHECK(ResolveIP("8.8.8.8").IsRoutable());
79  BOOST_CHECK(ResolveIP("2001::1").IsRoutable());
80  BOOST_CHECK(ResolveIP("127.0.0.1").IsValid());
81  BOOST_CHECK(CreateInternal("FD6B:88C0:8724:edb1:8e4:3588:e546:35ca").IsInternal());
82  BOOST_CHECK(CreateInternal("bar.com").IsInternal());
83 
84 }
85 
86 bool static TestSplitHost(const std::string& test, const std::string& host, uint16_t port)
87 {
88  std::string hostOut;
89  uint16_t portOut{0};
90  SplitHostPort(test, portOut, hostOut);
91  return hostOut == host && port == portOut;
92 }
93 
94 BOOST_AUTO_TEST_CASE(netbase_splithost)
95 {
96  BOOST_CHECK(TestSplitHost("www.bitcoincore.org", "www.bitcoincore.org", 0));
97  BOOST_CHECK(TestSplitHost("[www.bitcoincore.org]", "www.bitcoincore.org", 0));
98  BOOST_CHECK(TestSplitHost("www.bitcoincore.org:80", "www.bitcoincore.org", 80));
99  BOOST_CHECK(TestSplitHost("[www.bitcoincore.org]:80", "www.bitcoincore.org", 80));
100  BOOST_CHECK(TestSplitHost("127.0.0.1", "127.0.0.1", 0));
101  BOOST_CHECK(TestSplitHost("127.0.0.1:8333", "127.0.0.1", 8333));
102  BOOST_CHECK(TestSplitHost("[127.0.0.1]", "127.0.0.1", 0));
103  BOOST_CHECK(TestSplitHost("[127.0.0.1]:8333", "127.0.0.1", 8333));
104  BOOST_CHECK(TestSplitHost("::ffff:127.0.0.1", "::ffff:127.0.0.1", 0));
105  BOOST_CHECK(TestSplitHost("[::ffff:127.0.0.1]:8333", "::ffff:127.0.0.1", 8333));
106  BOOST_CHECK(TestSplitHost("[::]:8333", "::", 8333));
107  BOOST_CHECK(TestSplitHost("::8333", "::8333", 0));
108  BOOST_CHECK(TestSplitHost(":8333", "", 8333));
109  BOOST_CHECK(TestSplitHost("[]:8333", "", 8333));
110  BOOST_CHECK(TestSplitHost("", "", 0));
111 }
112 
113 bool static TestParse(std::string src, std::string canon)
114 {
115  CService addr(LookupNumeric(src, 65535));
116  return canon == addr.ToString();
117 }
118 
119 BOOST_AUTO_TEST_CASE(netbase_lookupnumeric)
120 {
121  BOOST_CHECK(TestParse("127.0.0.1", "127.0.0.1:65535"));
122  BOOST_CHECK(TestParse("127.0.0.1:8333", "127.0.0.1:8333"));
123  BOOST_CHECK(TestParse("::ffff:127.0.0.1", "127.0.0.1:65535"));
124  BOOST_CHECK(TestParse("::", "[::]:65535"));
125  BOOST_CHECK(TestParse("[::]:8333", "[::]:8333"));
126  BOOST_CHECK(TestParse("[127.0.0.1]", "127.0.0.1:65535"));
127  BOOST_CHECK(TestParse(":::", "[::]:0"));
128 
129  // verify that an internal address fails to resolve
130  BOOST_CHECK(TestParse("[fd6b:88c0:8724:1:2:3:4:5]", "[::]:0"));
131  // and that a one-off resolves correctly
132  BOOST_CHECK(TestParse("[fd6c:88c0:8724:1:2:3:4:5]", "[fd6c:88c0:8724:1:2:3:4:5]:65535"));
133 }
134 
135 BOOST_AUTO_TEST_CASE(embedded_test)
136 {
137  CNetAddr addr1(ResolveIP("1.2.3.4"));
138  CNetAddr addr2(ResolveIP("::FFFF:0102:0304"));
139  BOOST_CHECK(addr2.IsIPv4());
140  BOOST_CHECK_EQUAL(addr1.ToString(), addr2.ToString());
141 }
142 
144 {
145 
146  BOOST_CHECK(ResolveSubNet("1.2.3.0/24") == ResolveSubNet("1.2.3.0/255.255.255.0"));
147  BOOST_CHECK(ResolveSubNet("1.2.3.0/24") != ResolveSubNet("1.2.4.0/255.255.255.0"));
148  BOOST_CHECK(ResolveSubNet("1.2.3.0/24").Match(ResolveIP("1.2.3.4")));
149  BOOST_CHECK(!ResolveSubNet("1.2.2.0/24").Match(ResolveIP("1.2.3.4")));
150  BOOST_CHECK(ResolveSubNet("1.2.3.4").Match(ResolveIP("1.2.3.4")));
151  BOOST_CHECK(ResolveSubNet("1.2.3.4/32").Match(ResolveIP("1.2.3.4")));
152  BOOST_CHECK(!ResolveSubNet("1.2.3.4").Match(ResolveIP("5.6.7.8")));
153  BOOST_CHECK(!ResolveSubNet("1.2.3.4/32").Match(ResolveIP("5.6.7.8")));
154  BOOST_CHECK(ResolveSubNet("::ffff:127.0.0.1").Match(ResolveIP("127.0.0.1")));
155  BOOST_CHECK(ResolveSubNet("1:2:3:4:5:6:7:8").Match(ResolveIP("1:2:3:4:5:6:7:8")));
156  BOOST_CHECK(!ResolveSubNet("1:2:3:4:5:6:7:8").Match(ResolveIP("1:2:3:4:5:6:7:9")));
157  BOOST_CHECK(ResolveSubNet("1:2:3:4:5:6:7:0/112").Match(ResolveIP("1:2:3:4:5:6:7:1234")));
158  BOOST_CHECK(ResolveSubNet("192.168.0.1/24").Match(ResolveIP("192.168.0.2")));
159  BOOST_CHECK(ResolveSubNet("192.168.0.20/29").Match(ResolveIP("192.168.0.18")));
160  BOOST_CHECK(ResolveSubNet("1.2.2.1/24").Match(ResolveIP("1.2.2.4")));
161  BOOST_CHECK(ResolveSubNet("1.2.2.110/31").Match(ResolveIP("1.2.2.111")));
162  BOOST_CHECK(ResolveSubNet("1.2.2.20/26").Match(ResolveIP("1.2.2.63")));
163  // All-Matching IPv6 Matches arbitrary IPv6
164  BOOST_CHECK(ResolveSubNet("::/0").Match(ResolveIP("1:2:3:4:5:6:7:1234")));
165  // But not `::` or `0.0.0.0` because they are considered invalid addresses
166  BOOST_CHECK(!ResolveSubNet("::/0").Match(ResolveIP("::")));
167  BOOST_CHECK(!ResolveSubNet("::/0").Match(ResolveIP("0.0.0.0")));
168  // Addresses from one network (IPv4) don't belong to subnets of another network (IPv6)
169  BOOST_CHECK(!ResolveSubNet("::/0").Match(ResolveIP("1.2.3.4")));
170  // All-Matching IPv4 does not Match IPv6
171  BOOST_CHECK(!ResolveSubNet("0.0.0.0/0").Match(ResolveIP("1:2:3:4:5:6:7:1234")));
172  // Invalid subnets Match nothing (not even invalid addresses)
173  BOOST_CHECK(!CSubNet().Match(ResolveIP("1.2.3.4")));
174  BOOST_CHECK(!ResolveSubNet("").Match(ResolveIP("4.5.6.7")));
175  BOOST_CHECK(!ResolveSubNet("bloop").Match(ResolveIP("0.0.0.0")));
176  BOOST_CHECK(!ResolveSubNet("bloop").Match(ResolveIP("hab")));
177  // Check valid/invalid
178  BOOST_CHECK(ResolveSubNet("1.2.3.0/0").IsValid());
179  BOOST_CHECK(!ResolveSubNet("1.2.3.0/-1").IsValid());
180  BOOST_CHECK(ResolveSubNet("1.2.3.0/32").IsValid());
181  BOOST_CHECK(!ResolveSubNet("1.2.3.0/33").IsValid());
182  BOOST_CHECK(!ResolveSubNet("1.2.3.0/300").IsValid());
183  BOOST_CHECK(ResolveSubNet("1:2:3:4:5:6:7:8/0").IsValid());
184  BOOST_CHECK(ResolveSubNet("1:2:3:4:5:6:7:8/33").IsValid());
185  BOOST_CHECK(!ResolveSubNet("1:2:3:4:5:6:7:8/-1").IsValid());
186  BOOST_CHECK(ResolveSubNet("1:2:3:4:5:6:7:8/128").IsValid());
187  BOOST_CHECK(!ResolveSubNet("1:2:3:4:5:6:7:8/129").IsValid());
188  BOOST_CHECK(!ResolveSubNet("fuzzy").IsValid());
189 
190  //CNetAddr constructor test
191  BOOST_CHECK(CSubNet(ResolveIP("127.0.0.1")).IsValid());
192  BOOST_CHECK(CSubNet(ResolveIP("127.0.0.1")).Match(ResolveIP("127.0.0.1")));
193  BOOST_CHECK(!CSubNet(ResolveIP("127.0.0.1")).Match(ResolveIP("127.0.0.2")));
194  BOOST_CHECK(CSubNet(ResolveIP("127.0.0.1")).ToString() == "127.0.0.1/32");
195 
196  CSubNet subnet = CSubNet(ResolveIP("1.2.3.4"), 32);
197  BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/32");
198  subnet = CSubNet(ResolveIP("1.2.3.4"), 8);
199  BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/8");
200  subnet = CSubNet(ResolveIP("1.2.3.4"), 0);
201  BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/0");
202 
203  subnet = CSubNet(ResolveIP("1.2.3.4"), ResolveIP("255.255.255.255"));
204  BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/32");
205  subnet = CSubNet(ResolveIP("1.2.3.4"), ResolveIP("255.0.0.0"));
206  BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/8");
207  subnet = CSubNet(ResolveIP("1.2.3.4"), ResolveIP("0.0.0.0"));
208  BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/0");
209 
210  BOOST_CHECK(CSubNet(ResolveIP("1:2:3:4:5:6:7:8")).IsValid());
211  BOOST_CHECK(CSubNet(ResolveIP("1:2:3:4:5:6:7:8")).Match(ResolveIP("1:2:3:4:5:6:7:8")));
212  BOOST_CHECK(!CSubNet(ResolveIP("1:2:3:4:5:6:7:8")).Match(ResolveIP("1:2:3:4:5:6:7:9")));
213  BOOST_CHECK(CSubNet(ResolveIP("1:2:3:4:5:6:7:8")).ToString() == "1:2:3:4:5:6:7:8/128");
214  // IPv4 address with IPv6 netmask or the other way around.
215  BOOST_CHECK(!CSubNet(ResolveIP("1.1.1.1"), ResolveIP("ffff::")).IsValid());
216  BOOST_CHECK(!CSubNet(ResolveIP("::1"), ResolveIP("255.0.0.0")).IsValid());
217 
218  // Create Non-IP subnets.
219 
220  const CNetAddr tor_addr{
221  ResolveIP("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion")};
222 
223  subnet = CSubNet(tor_addr);
224  BOOST_CHECK(subnet.IsValid());
225  BOOST_CHECK_EQUAL(subnet.ToString(), tor_addr.ToString());
226  BOOST_CHECK(subnet.Match(tor_addr));
227  BOOST_CHECK(
228  !subnet.Match(ResolveIP("kpgvmscirrdqpekbqjsvw5teanhatztpp2gl6eee4zkowvwfxwenqaid.onion")));
229  BOOST_CHECK(!subnet.Match(ResolveIP("1.2.3.4")));
230 
231  BOOST_CHECK(!CSubNet(tor_addr, 200).IsValid());
232  BOOST_CHECK(!CSubNet(tor_addr, ResolveIP("255.0.0.0")).IsValid());
233 
234  subnet = ResolveSubNet("1.2.3.4/255.255.255.255");
235  BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/32");
236  subnet = ResolveSubNet("1.2.3.4/255.255.255.254");
237  BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/31");
238  subnet = ResolveSubNet("1.2.3.4/255.255.255.252");
239  BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/30");
240  subnet = ResolveSubNet("1.2.3.4/255.255.255.248");
241  BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/29");
242  subnet = ResolveSubNet("1.2.3.4/255.255.255.240");
243  BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/28");
244  subnet = ResolveSubNet("1.2.3.4/255.255.255.224");
245  BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/27");
246  subnet = ResolveSubNet("1.2.3.4/255.255.255.192");
247  BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/26");
248  subnet = ResolveSubNet("1.2.3.4/255.255.255.128");
249  BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/25");
250  subnet = ResolveSubNet("1.2.3.4/255.255.255.0");
251  BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/24");
252  subnet = ResolveSubNet("1.2.3.4/255.255.254.0");
253  BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.2.0/23");
254  subnet = ResolveSubNet("1.2.3.4/255.255.252.0");
255  BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/22");
256  subnet = ResolveSubNet("1.2.3.4/255.255.248.0");
257  BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/21");
258  subnet = ResolveSubNet("1.2.3.4/255.255.240.0");
259  BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/20");
260  subnet = ResolveSubNet("1.2.3.4/255.255.224.0");
261  BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/19");
262  subnet = ResolveSubNet("1.2.3.4/255.255.192.0");
263  BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/18");
264  subnet = ResolveSubNet("1.2.3.4/255.255.128.0");
265  BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/17");
266  subnet = ResolveSubNet("1.2.3.4/255.255.0.0");
267  BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/16");
268  subnet = ResolveSubNet("1.2.3.4/255.254.0.0");
269  BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/15");
270  subnet = ResolveSubNet("1.2.3.4/255.252.0.0");
271  BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/14");
272  subnet = ResolveSubNet("1.2.3.4/255.248.0.0");
273  BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/13");
274  subnet = ResolveSubNet("1.2.3.4/255.240.0.0");
275  BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/12");
276  subnet = ResolveSubNet("1.2.3.4/255.224.0.0");
277  BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/11");
278  subnet = ResolveSubNet("1.2.3.4/255.192.0.0");
279  BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/10");
280  subnet = ResolveSubNet("1.2.3.4/255.128.0.0");
281  BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/9");
282  subnet = ResolveSubNet("1.2.3.4/255.0.0.0");
283  BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/8");
284  subnet = ResolveSubNet("1.2.3.4/254.0.0.0");
285  BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/7");
286  subnet = ResolveSubNet("1.2.3.4/252.0.0.0");
287  BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/6");
288  subnet = ResolveSubNet("1.2.3.4/248.0.0.0");
289  BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/5");
290  subnet = ResolveSubNet("1.2.3.4/240.0.0.0");
291  BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/4");
292  subnet = ResolveSubNet("1.2.3.4/224.0.0.0");
293  BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/3");
294  subnet = ResolveSubNet("1.2.3.4/192.0.0.0");
295  BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/2");
296  subnet = ResolveSubNet("1.2.3.4/128.0.0.0");
297  BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/1");
298  subnet = ResolveSubNet("1.2.3.4/0.0.0.0");
299  BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/0");
300 
301  subnet = ResolveSubNet("1:2:3:4:5:6:7:8/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
302  BOOST_CHECK_EQUAL(subnet.ToString(), "1:2:3:4:5:6:7:8/128");
303  subnet = ResolveSubNet("1:2:3:4:5:6:7:8/ffff:0000:0000:0000:0000:0000:0000:0000");
304  BOOST_CHECK_EQUAL(subnet.ToString(), "1::/16");
305  subnet = ResolveSubNet("1:2:3:4:5:6:7:8/0000:0000:0000:0000:0000:0000:0000:0000");
306  BOOST_CHECK_EQUAL(subnet.ToString(), "::/0");
307  // Invalid netmasks (with 1-bits after 0-bits)
308  subnet = ResolveSubNet("1.2.3.4/255.255.232.0");
309  BOOST_CHECK(!subnet.IsValid());
310  subnet = ResolveSubNet("1.2.3.4/255.0.255.255");
311  BOOST_CHECK(!subnet.IsValid());
312  subnet = ResolveSubNet("1:2:3:4:5:6:7:8/ffff:ffff:ffff:fffe:ffff:ffff:ffff:ff0f");
313  BOOST_CHECK(!subnet.IsValid());
314 }
315 
316 BOOST_AUTO_TEST_CASE(netbase_getgroup)
317 {
318  std::vector<bool> asmap; // use /16
319  BOOST_CHECK(ResolveIP("127.0.0.1").GetGroup(asmap) == std::vector<unsigned char>({0})); // Local -> !Routable()
320  BOOST_CHECK(ResolveIP("257.0.0.1").GetGroup(asmap) == std::vector<unsigned char>({0})); // !Valid -> !Routable()
321  BOOST_CHECK(ResolveIP("10.0.0.1").GetGroup(asmap) == std::vector<unsigned char>({0})); // RFC1918 -> !Routable()
322  BOOST_CHECK(ResolveIP("169.254.1.1").GetGroup(asmap) == std::vector<unsigned char>({0})); // RFC3927 -> !Routable()
323  BOOST_CHECK(ResolveIP("1.2.3.4").GetGroup(asmap) == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // IPv4
324  BOOST_CHECK(ResolveIP("::FFFF:0:102:304").GetGroup(asmap) == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // RFC6145
325  BOOST_CHECK(ResolveIP("64:FF9B::102:304").GetGroup(asmap) == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // RFC6052
326  BOOST_CHECK(ResolveIP("2002:102:304:9999:9999:9999:9999:9999").GetGroup(asmap) == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // RFC3964
327  BOOST_CHECK(ResolveIP("2001:0:9999:9999:9999:9999:FEFD:FCFB").GetGroup(asmap) == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // RFC4380
328  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
329  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
330 
331  // baz.net sha256 hash: 12929400eb4607c4ac075f087167e75286b179c693eb059a01774b864e8fe505
332  std::vector<unsigned char> internal_group = {NET_INTERNAL, 0x12, 0x92, 0x94, 0x00, 0xeb, 0x46, 0x07, 0xc4, 0xac, 0x07};
333  BOOST_CHECK(CreateInternal("baz.net").GetGroup(asmap) == internal_group);
334 }
335 
336 BOOST_AUTO_TEST_CASE(netbase_parsenetwork)
337 {
342 
347 
352 }
353 
354 BOOST_AUTO_TEST_CASE(netpermissions_test)
355 {
357  NetWhitebindPermissions whitebindPermissions;
358  NetWhitelistPermissions whitelistPermissions;
359 
360  // Detect invalid white bind
361  BOOST_CHECK(!NetWhitebindPermissions::TryParse("", whitebindPermissions, error));
362  BOOST_CHECK(error.original.find("Cannot resolve -whitebind address") != std::string::npos);
363  BOOST_CHECK(!NetWhitebindPermissions::TryParse("127.0.0.1", whitebindPermissions, error));
364  BOOST_CHECK(error.original.find("Need to specify a port with -whitebind") != std::string::npos);
365  BOOST_CHECK(!NetWhitebindPermissions::TryParse("", whitebindPermissions, error));
366 
367  // If no permission flags, assume backward compatibility
368  BOOST_CHECK(NetWhitebindPermissions::TryParse("1.2.3.4:32", whitebindPermissions, error));
369  BOOST_CHECK(error.empty());
374  BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, NetPermissionFlags::None);
377 
378  // Can set one permission
379  BOOST_CHECK(NetWhitebindPermissions::TryParse("bloom@1.2.3.4:32", whitebindPermissions, error));
381  BOOST_CHECK(NetWhitebindPermissions::TryParse("@1.2.3.4:32", whitebindPermissions, error));
382  BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, NetPermissionFlags::None);
383 
384  NetWhitebindPermissions noban, noban_download, download_noban, download;
385 
386  // "noban" implies "download"
387  BOOST_REQUIRE(NetWhitebindPermissions::TryParse("noban@1.2.3.4:32", noban, error));
391 
392  // "noban,download" is equivalent to "noban"
393  BOOST_REQUIRE(NetWhitebindPermissions::TryParse("noban,download@1.2.3.4:32", noban_download, error));
394  BOOST_CHECK_EQUAL(noban_download.m_flags, noban.m_flags);
395 
396  // "download,noban" is equivalent to "noban"
397  BOOST_REQUIRE(NetWhitebindPermissions::TryParse("download,noban@1.2.3.4:32", download_noban, error));
398  BOOST_CHECK_EQUAL(download_noban.m_flags, noban.m_flags);
399 
400  // "download" excludes (does not imply) "noban"
401  BOOST_REQUIRE(NetWhitebindPermissions::TryParse("download@1.2.3.4:32", download, error));
405 
406  // Happy path, can parse flags
407  BOOST_CHECK(NetWhitebindPermissions::TryParse("bloom,forcerelay@1.2.3.4:32", whitebindPermissions, error));
408  // forcerelay should also activate the relay permission
410  BOOST_CHECK(NetWhitebindPermissions::TryParse("bloom,relay,noban@1.2.3.4:32", whitebindPermissions, error));
412  BOOST_CHECK(NetWhitebindPermissions::TryParse("bloom,forcerelay,noban@1.2.3.4:32", whitebindPermissions, error));
413  BOOST_CHECK(NetWhitebindPermissions::TryParse("all@1.2.3.4:32", whitebindPermissions, error));
414  BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, NetPermissionFlags::All);
415 
416  // Allow dups
417  BOOST_CHECK(NetWhitebindPermissions::TryParse("bloom,relay,noban,noban@1.2.3.4:32", whitebindPermissions, error));
419 
420  // Allow empty
421  BOOST_CHECK(NetWhitebindPermissions::TryParse("bloom,relay,,noban@1.2.3.4:32", whitebindPermissions, error));
423  BOOST_CHECK(NetWhitebindPermissions::TryParse(",@1.2.3.4:32", whitebindPermissions, error));
424  BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, NetPermissionFlags::None);
425  BOOST_CHECK(NetWhitebindPermissions::TryParse(",,@1.2.3.4:32", whitebindPermissions, error));
426  BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, NetPermissionFlags::None);
427 
428  // Detect invalid flag
429  BOOST_CHECK(!NetWhitebindPermissions::TryParse("bloom,forcerelay,oopsie@1.2.3.4:32", whitebindPermissions, error));
430  BOOST_CHECK(error.original.find("Invalid P2P permission") != std::string::npos);
431 
432  // Check netmask error
433  BOOST_CHECK(!NetWhitelistPermissions::TryParse("bloom,forcerelay,noban@1.2.3.4:32", whitelistPermissions, error));
434  BOOST_CHECK(error.original.find("Invalid netmask specified in -whitelist") != std::string::npos);
435 
436  // Happy path for whitelist parsing
437  BOOST_CHECK(NetWhitelistPermissions::TryParse("noban@1.2.3.4", whitelistPermissions, error));
438  BOOST_CHECK_EQUAL(whitelistPermissions.m_flags, NetPermissionFlags::NoBan);
440 
441  BOOST_CHECK(NetWhitelistPermissions::TryParse("bloom,forcerelay,noban,relay@1.2.3.4/32", whitelistPermissions, error));
443  BOOST_CHECK(error.empty());
444  BOOST_CHECK_EQUAL(whitelistPermissions.m_subnet.ToString(), "1.2.3.4/32");
445  BOOST_CHECK(NetWhitelistPermissions::TryParse("bloom,forcerelay,noban,relay,mempool@1.2.3.4/32", whitelistPermissions, error));
446 
448  BOOST_CHECK_EQUAL(strings.size(), 7U);
449  BOOST_CHECK(std::find(strings.begin(), strings.end(), "bloomfilter") != strings.end());
450  BOOST_CHECK(std::find(strings.begin(), strings.end(), "forcerelay") != strings.end());
451  BOOST_CHECK(std::find(strings.begin(), strings.end(), "relay") != strings.end());
452  BOOST_CHECK(std::find(strings.begin(), strings.end(), "noban") != strings.end());
453  BOOST_CHECK(std::find(strings.begin(), strings.end(), "mempool") != strings.end());
454  BOOST_CHECK(std::find(strings.begin(), strings.end(), "download") != strings.end());
455  BOOST_CHECK(std::find(strings.begin(), strings.end(), "addr") != strings.end());
456 }
457 
458 BOOST_AUTO_TEST_CASE(netbase_dont_resolve_strings_with_embedded_nul_characters)
459 {
460  CNetAddr addr;
461  BOOST_CHECK(LookupHost("127.0.0.1"s, addr, false));
462  BOOST_CHECK(!LookupHost("127.0.0.1\0"s, addr, false));
463  BOOST_CHECK(!LookupHost("127.0.0.1\0example.com"s, addr, false));
464  BOOST_CHECK(!LookupHost("127.0.0.1\0example.com\0"s, addr, false));
465  CSubNet ret;
466  BOOST_CHECK(LookupSubNet("1.2.3.0/24"s, ret));
467  BOOST_CHECK(!LookupSubNet("1.2.3.0/24\0"s, ret));
468  BOOST_CHECK(!LookupSubNet("1.2.3.0/24\0example.com"s, ret));
469  BOOST_CHECK(!LookupSubNet("1.2.3.0/24\0example.com\0"s, ret));
470  BOOST_CHECK(LookupSubNet("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion"s, ret));
471  BOOST_CHECK(!LookupSubNet("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion\0"s, ret));
472  BOOST_CHECK(!LookupSubNet("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion\0example.com"s, ret));
473  BOOST_CHECK(!LookupSubNet("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion\0example.com\0"s, ret));
474 }
475 
476 // Since CNetAddr (un)ser is tested separately in net_tests.cpp here we only
477 // try a few edge cases for port, service flags and time.
478 
479 static const std::vector<CAddress> fixture_addresses({
480  CAddress(
481  CService(CNetAddr(in6_addr(IN6ADDR_LOOPBACK_INIT)), 0 /* port */),
482  NODE_NONE,
483  0x4966bc61U /* Fri Jan 9 02:54:25 UTC 2009 */
484  ),
485  CAddress(
486  CService(CNetAddr(in6_addr(IN6ADDR_LOOPBACK_INIT)), 0x00f1 /* port */),
487  NODE_NETWORK,
488  0x83766279U /* Tue Nov 22 11:22:33 UTC 2039 */
489  ),
490  CAddress(
491  CService(CNetAddr(in6_addr(IN6ADDR_LOOPBACK_INIT)), 0xf1f2 /* port */),
493  0xffffffffU /* Sun Feb 7 06:28:15 UTC 2106 */
494  )
495 });
496 
497 // fixture_addresses should equal to this when serialized in V1 format.
498 // When this is unserialized from V1 format it should equal to fixture_addresses.
499 static constexpr const char* stream_addrv1_hex =
500  "03" // number of entries
501 
502  "61bc6649" // time, Fri Jan 9 02:54:25 UTC 2009
503  "0000000000000000" // service flags, NODE_NONE
504  "00000000000000000000000000000001" // address, fixed 16 bytes (IPv4 embedded in IPv6)
505  "0000" // port
506 
507  "79627683" // time, Tue Nov 22 11:22:33 UTC 2039
508  "0100000000000000" // service flags, NODE_NETWORK
509  "00000000000000000000000000000001" // address, fixed 16 bytes (IPv6)
510  "00f1" // port
511 
512  "ffffffff" // time, Sun Feb 7 06:28:15 UTC 2106
513  "4804000000000000" // service flags, NODE_WITNESS | NODE_COMPACT_FILTERS | NODE_NETWORK_LIMITED
514  "00000000000000000000000000000001" // address, fixed 16 bytes (IPv6)
515  "f1f2"; // port
516 
517 // fixture_addresses should equal to this when serialized in V2 format.
518 // When this is unserialized from V2 format it should equal to fixture_addresses.
519 static constexpr const char* stream_addrv2_hex =
520  "03" // number of entries
521 
522  "61bc6649" // time, Fri Jan 9 02:54:25 UTC 2009
523  "00" // service flags, COMPACTSIZE(NODE_NONE)
524  "02" // network id, IPv6
525  "10" // address length, COMPACTSIZE(16)
526  "00000000000000000000000000000001" // address
527  "0000" // port
528 
529  "79627683" // time, Tue Nov 22 11:22:33 UTC 2039
530  "01" // service flags, COMPACTSIZE(NODE_NETWORK)
531  "02" // network id, IPv6
532  "10" // address length, COMPACTSIZE(16)
533  "00000000000000000000000000000001" // address
534  "00f1" // port
535 
536  "ffffffff" // time, Sun Feb 7 06:28:15 UTC 2106
537  "fd4804" // service flags, COMPACTSIZE(NODE_WITNESS | NODE_COMPACT_FILTERS | NODE_NETWORK_LIMITED)
538  "02" // network id, IPv6
539  "10" // address length, COMPACTSIZE(16)
540  "00000000000000000000000000000001" // address
541  "f1f2"; // port
542 
543 BOOST_AUTO_TEST_CASE(caddress_serialize_v1)
544 {
546 
547  s << fixture_addresses;
549 }
550 
551 BOOST_AUTO_TEST_CASE(caddress_unserialize_v1)
552 {
554  std::vector<CAddress> addresses_unserialized;
555 
556  s >> addresses_unserialized;
557  BOOST_CHECK(fixture_addresses == addresses_unserialized);
558 }
559 
560 BOOST_AUTO_TEST_CASE(caddress_serialize_v2)
561 {
563 
564  s << fixture_addresses;
566 }
567 
568 BOOST_AUTO_TEST_CASE(caddress_unserialize_v2)
569 {
571  std::vector<CAddress> addresses_unserialized;
572 
573  s >> addresses_unserialized;
574  BOOST_CHECK(fixture_addresses == addresses_unserialized);
575 }
576 
ADDRV2_FORMAT
static constexpr int ADDRV2_FORMAT
A flag that is ORed into the protocol version to designate that addresses should be serialized in (un...
Definition: netaddress.h:34
CService
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:539
LookupHost
bool LookupHost(const std::string &name, std::vector< CNetAddr > &vIP, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function)
Resolve a host string to its corresponding network addresses.
Definition: netbase.cpp:167
ParseHex
std::vector< unsigned char > ParseHex(const char *psz)
Definition: strencodings.cpp:84
ToString
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:87
NET_UNROUTABLE
@ NET_UNROUTABLE
Addresses from these networks are not publicly routable on the global Internet.
Definition: netaddress.h:47
ParseNetwork
enum Network ParseNetwork(const std::string &net_in)
Definition: netbase.cpp:87
NetPermissionFlags::None
@ None
NET_IPV4
@ NET_IPV4
IPv4.
Definition: netaddress.h:50
LookupSubNet
bool LookupSubNet(const std::string &strSubnet, CSubNet &ret, DNSLookupFn dns_lookup_function)
Parse and resolve a specified subnet string into the appropriate internal representation.
Definition: netbase.cpp:676
NetPermissions::ToStrings
static std::vector< std::string > ToStrings(NetPermissionFlags flags)
Definition: net_permissions.cpp:71
streams.h
setup_common.h
NET_INTERNAL
@ NET_INTERNAL
A set of addresses that represent the hash of a string or FQDN.
Definition: netaddress.h:66
TestParse
static bool TestParse(std::string src, std::string canon)
Definition: netbase_tests.cpp:113
ResolveSubNet
static CSubNet ResolveSubNet(const std::string &subnet)
Definition: netbase_tests.cpp:31
bilingual_str
Bilingual messages:
Definition: translation.h:16
CNetAddr
Network address.
Definition: netaddress.h:118
NetPermissionFlags::NoBan
@ NoBan
NetPermissions::ClearFlag
static void ClearFlag(NetPermissionFlags &flags, NetPermissionFlags f)
ClearFlag is only called with f == NetPermissionFlags::Implicit.
Definition: net_permissions.h:67
NetPermissions::HasFlag
static bool HasFlag(NetPermissionFlags flags, NetPermissionFlags f)
Definition: net_permissions.h:53
ServiceFlags
ServiceFlags
nServices flags
Definition: protocol.h:271
NODE_NETWORK_LIMITED
@ NODE_NETWORK_LIMITED
Definition: protocol.h:291
NODE_NETWORK
@ NODE_NETWORK
Definition: protocol.h:277
NetWhitelistPermissions::TryParse
static bool TryParse(const std::string &str, NetWhitelistPermissions &output, bilingual_str &error)
Definition: net_permissions.cpp:107
NetWhitelistPermissions::m_subnet
CSubNet m_subnet
Definition: net_permissions.h:86
BOOST_FIXTURE_TEST_SUITE
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
NODE_COMPACT_FILTERS
@ NODE_COMPACT_FILTERS
Definition: protocol.h:287
version.h
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
CreateInternal
static CNetAddr CreateInternal(const std::string &host)
Definition: netbase_tests.cpp:38
CSubNet::Match
bool Match(const CNetAddr &addr) const
Definition: netaddress.cpp:1147
NetPermissionFlags::BloomFilter
@ BloomFilter
NetWhitelistPermissions
Definition: net_permissions.h:82
strencodings.h
CService::ToString
std::string ToString() const
Definition: netaddress.cpp:1037
SplitHostPort
void SplitHostPort(std::string in, uint16_t &portOut, std::string &hostOut)
Definition: strencodings.cpp:110
stream_addrv1_hex
static constexpr const char * stream_addrv1_hex
Definition: netbase_tests.cpp:499
NetPermissionFlags::Download
@ Download
ip
static CService ip(uint32_t i)
Definition: denialofservice_tests.cpp:30
netaddress.h
stream_addrv2_hex
static constexpr const char * stream_addrv2_hex
Definition: netbase_tests.cpp:519
NetPermissionFlags::ForceRelay
@ ForceRelay
CSubNet
Definition: netaddress.h:486
BasicTestingSetup
Basic testing setup.
Definition: setup_common.h:76
ResolveIP
static CNetAddr ResolveIP(const std::string &ip)
Definition: netbase_tests.cpp:24
addr1
static const std::string addr1
Definition: key_tests.cpp:24
NetPermissionFlags::Relay
@ Relay
NetPermissionFlags::All
@ All
NODE_NONE
@ NODE_NONE
Definition: protocol.h:274
TestSplitHost
static bool TestSplitHost(const std::string &test, const std::string &host, uint16_t port)
Definition: netbase_tests.cpp:86
NODE_WITNESS
@ NODE_WITNESS
Definition: protocol.h:284
NetWhitebindPermissions::TryParse
static bool TryParse(const std::string &str, NetWhitebindPermissions &output, bilingual_str &error)
Definition: net_permissions.cpp:84
CAddress
A CService with information about it as peer.
Definition: protocol.h:358
addr2
static const std::string addr2
Definition: key_tests.cpp:25
NetPermissionFlags::Implicit
@ Implicit
NetPermissions::AddFlag
static void AddFlag(NetPermissionFlags &flags, NetPermissionFlags f)
Definition: net_permissions.h:58
translation.h
fixture_addresses
static const std::vector< CAddress > fixture_addresses({ CAddress(CService(CNetAddr(in6_addr(IN6ADDR_LOOPBACK_INIT)), 0), NODE_NONE, 0x4966bc61U), CAddress(CService(CNetAddr(in6_addr(IN6ADDR_LOOPBACK_INIT)), 0x00f1), NODE_NETWORK, 0x83766279U), CAddress(CService(CNetAddr(in6_addr(IN6ADDR_LOOPBACK_INIT)), 0xf1f2), static_cast< ServiceFlags >(NODE_WITNESS|NODE_COMPACT_FILTERS|NODE_NETWORK_LIMITED), 0xffffffffU) })
NetPermissions::m_flags
NetPermissionFlags m_flags
Definition: net_permissions.h:51
CSubNet::ToString
std::string ToString() const
Definition: netaddress.cpp:1175
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(netbase_networks)
Definition: netbase_tests.cpp:45
NET_ONION
@ NET_ONION
TOR (v2 or v3)
Definition: netaddress.h:56
CSubNet::IsValid
bool IsValid() const
Definition: netaddress.cpp:1208
serialize.h
CDataStream
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:204
NET_IPV6
@ NET_IPV6
IPv6.
Definition: netaddress.h:53
SER_NETWORK
@ SER_NETWORK
Definition: serialize.h:138
CNetAddr::SetInternal
bool SetInternal(const std::string &name)
Create an "internal" address that represents a name or FQDN.
Definition: netaddress.cpp:173
netbase.h
LookupNumeric
CService LookupNumeric(const std::string &name, uint16_t portDefault, DNSLookupFn dns_lookup_function)
Resolve a service string with a numeric IP to its first corresponding service.
Definition: netbase.cpp:227
error
bool error(const char *fmt, const Args &... args)
Definition: system.h:49
HexStr
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Definition: strencodings.cpp:594
NetWhitebindPermissions
Definition: net_permissions.h:75
IsLocal
bool IsLocal(const CService &addr)
check whether a given address is potentially local
Definition: net.cpp:302
BOOST_CHECK
#define BOOST_CHECK(expr)
Definition: object.cpp:17
BOOST_CHECK_EQUAL
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
net_permissions.h
PROTOCOL_VERSION
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:12