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