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