Branch data Line data Source code
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 : 94 : static CNetAddr ResolveIP(const std::string& ip)
25 : : {
26 [ + - + - ]: 94 : return LookupHost(ip, false).value_or(CNetAddr{});
27 : : }
28 : :
29 : 4 : static CNetAddr CreateInternal(const std::string& host)
30 : : {
31 : 4 : CNetAddr addr;
32 [ + - ]: 4 : addr.SetInternal(host);
33 : 4 : return addr;
34 : 0 : }
35 : :
36 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(netbase_networks)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
37 : : {
38 [ + - + - : 2 : BOOST_CHECK(ResolveIP("127.0.0.1").GetNetwork() == NET_UNROUTABLE);
+ - + - ]
39 [ + - + - : 2 : BOOST_CHECK(ResolveIP("::1").GetNetwork() == NET_UNROUTABLE);
+ - + - ]
40 [ + - + - : 2 : BOOST_CHECK(ResolveIP("8.8.8.8").GetNetwork() == NET_IPV4);
+ - + - ]
41 [ + - + - : 2 : BOOST_CHECK(ResolveIP("2001::8888").GetNetwork() == NET_IPV6);
+ - + - ]
42 [ + - + - : 2 : BOOST_CHECK(ResolveIP("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion").GetNetwork() == NET_ONION);
+ - + - ]
43 [ + - + - : 2 : BOOST_CHECK(CreateInternal("foo.com").GetNetwork() == NET_INTERNAL);
+ - + - ]
44 : 1 : }
45 : :
46 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(netbase_properties)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
47 : : {
48 : :
49 [ + - + - : 2 : BOOST_CHECK(ResolveIP("127.0.0.1").IsIPv4());
+ - ]
50 [ + - + - : 2 : BOOST_CHECK(ResolveIP("::FFFF:192.168.1.1").IsIPv4());
+ - ]
51 [ + - + - : 2 : BOOST_CHECK(ResolveIP("::1").IsIPv6());
+ - ]
52 [ + - + - : 2 : BOOST_CHECK(ResolveIP("10.0.0.1").IsRFC1918());
+ - + - ]
53 [ + - + - : 2 : BOOST_CHECK(ResolveIP("192.168.1.1").IsRFC1918());
+ - + - ]
54 [ + - + - : 2 : BOOST_CHECK(ResolveIP("172.31.255.255").IsRFC1918());
+ - + - ]
55 [ + - + - : 2 : BOOST_CHECK(ResolveIP("198.18.0.0").IsRFC2544());
+ - + - ]
56 [ + - + - : 2 : BOOST_CHECK(ResolveIP("198.19.255.255").IsRFC2544());
+ - + - ]
57 [ + - + - : 2 : BOOST_CHECK(ResolveIP("2001:0DB8::").IsRFC3849());
+ - + - ]
58 [ + - + - : 2 : BOOST_CHECK(ResolveIP("169.254.1.1").IsRFC3927());
+ - + - ]
59 [ + - + - : 2 : BOOST_CHECK(ResolveIP("2002::1").IsRFC3964());
+ - + - ]
60 [ + - + - : 2 : BOOST_CHECK(ResolveIP("FC00::").IsRFC4193());
+ - + - ]
61 [ + - + - : 2 : BOOST_CHECK(ResolveIP("2001::2").IsRFC4380());
+ - + - ]
62 [ + - + - : 2 : BOOST_CHECK(ResolveIP("2001:10::").IsRFC4843());
+ - + - ]
63 [ + - + - : 2 : BOOST_CHECK(ResolveIP("2001:20::").IsRFC7343());
+ - + - ]
64 [ + - + - : 2 : BOOST_CHECK(ResolveIP("FE80::").IsRFC4862());
+ - + - ]
65 [ + - + - : 2 : BOOST_CHECK(ResolveIP("64:FF9B::").IsRFC6052());
+ - + - ]
66 [ + - + - : 2 : BOOST_CHECK(ResolveIP("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion").IsTor());
+ - ]
67 [ + - + - : 2 : BOOST_CHECK(ResolveIP("127.0.0.1").IsLocal());
+ - + - ]
68 [ + - + - : 2 : BOOST_CHECK(ResolveIP("::1").IsLocal());
+ - + - ]
69 [ + - + - : 2 : BOOST_CHECK(ResolveIP("8.8.8.8").IsRoutable());
+ - + - ]
70 [ + - + - : 2 : BOOST_CHECK(ResolveIP("2001::1").IsRoutable());
+ - + - ]
71 [ + - + - : 2 : BOOST_CHECK(ResolveIP("127.0.0.1").IsValid());
+ - + - ]
72 [ + - + - : 2 : BOOST_CHECK(CreateInternal("FD6B:88C0:8724:edb1:8e4:3588:e546:35ca").IsInternal());
+ - + - ]
73 [ + - + - : 2 : BOOST_CHECK(CreateInternal("bar.com").IsInternal());
+ - + - ]
74 : :
75 : 1 : }
76 : :
77 : 32 : bool static TestSplitHost(const std::string& test, const std::string& host, uint16_t port, bool validPort=true)
78 : : {
79 [ + - ]: 32 : std::string hostOut;
80 : 32 : uint16_t portOut{0};
81 [ + - ]: 32 : bool validPortOut = SplitHostPort(test, portOut, hostOut);
82 [ + - + - : 32 : return hostOut == host && portOut == port && validPortOut == validPort;
- + ]
83 : 32 : }
84 : :
85 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(netbase_splithost)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
86 : : {
87 [ + - + - : 2 : BOOST_CHECK(TestSplitHost("www.bitcoincore.org", "www.bitcoincore.org", 0));
+ - + - ]
88 [ + - + - : 2 : BOOST_CHECK(TestSplitHost("[www.bitcoincore.org]", "www.bitcoincore.org", 0));
+ - + - ]
89 [ + - + - : 2 : BOOST_CHECK(TestSplitHost("www.bitcoincore.org:80", "www.bitcoincore.org", 80));
+ - + - ]
90 [ + - + - : 2 : BOOST_CHECK(TestSplitHost("[www.bitcoincore.org]:80", "www.bitcoincore.org", 80));
+ - + - ]
91 [ + - + - : 2 : BOOST_CHECK(TestSplitHost("127.0.0.1", "127.0.0.1", 0));
+ - + - ]
92 [ + - + - : 2 : BOOST_CHECK(TestSplitHost("127.0.0.1:8333", "127.0.0.1", 8333));
+ - + - ]
93 [ + - + - : 2 : BOOST_CHECK(TestSplitHost("[127.0.0.1]", "127.0.0.1", 0));
+ - + - ]
94 [ + - + - : 2 : BOOST_CHECK(TestSplitHost("[127.0.0.1]:8333", "127.0.0.1", 8333));
+ - + - ]
95 [ + - + - : 2 : BOOST_CHECK(TestSplitHost("::ffff:127.0.0.1", "::ffff:127.0.0.1", 0));
+ - + - ]
96 [ + - + - : 2 : BOOST_CHECK(TestSplitHost("[::ffff:127.0.0.1]:8333", "::ffff:127.0.0.1", 8333));
+ - + - ]
97 [ + - + - : 2 : BOOST_CHECK(TestSplitHost("[::]:8333", "::", 8333));
+ - + - ]
98 [ + - + - : 2 : BOOST_CHECK(TestSplitHost("::8333", "::8333", 0));
+ - + - ]
99 [ + - + - : 2 : BOOST_CHECK(TestSplitHost(":8333", "", 8333));
+ - + - ]
100 [ + - + - : 2 : BOOST_CHECK(TestSplitHost("[]:8333", "", 8333));
+ - + - ]
101 [ + - + - : 2 : BOOST_CHECK(TestSplitHost("", "", 0));
+ - + - ]
102 [ + - + - : 2 : BOOST_CHECK(TestSplitHost(":65535", "", 65535));
+ - + - ]
103 [ + - + - : 2 : BOOST_CHECK(TestSplitHost(":65536", ":65536", 0, false));
+ - + - ]
104 [ + - + - : 2 : BOOST_CHECK(TestSplitHost(":-1", ":-1", 0, false));
+ - + - ]
105 [ + - + - : 2 : BOOST_CHECK(TestSplitHost("[]:70001", "[]:70001", 0, false));
+ - + - ]
106 [ + - + - : 2 : BOOST_CHECK(TestSplitHost("[]:-1", "[]:-1", 0, false));
+ - + - ]
107 [ + - + - : 2 : BOOST_CHECK(TestSplitHost("[]:-0", "[]:-0", 0, false));
+ - + - ]
108 [ + - + - : 2 : BOOST_CHECK(TestSplitHost("[]:0", "", 0, false));
+ - + - ]
109 [ + - + - : 2 : BOOST_CHECK(TestSplitHost("[]:1/2", "[]:1/2", 0, false));
+ - + - ]
110 [ + - + - : 2 : BOOST_CHECK(TestSplitHost("[]:1E2", "[]:1E2", 0, false));
+ - + - ]
111 [ + - + - : 2 : BOOST_CHECK(TestSplitHost("127.0.0.1:65536", "127.0.0.1:65536", 0, false));
+ - + - ]
112 [ + - + - : 2 : BOOST_CHECK(TestSplitHost("127.0.0.1:0", "127.0.0.1", 0, false));
+ - + - ]
113 [ + - + - : 2 : BOOST_CHECK(TestSplitHost("127.0.0.1:", "127.0.0.1:", 0, false));
+ - + - ]
114 [ + - + - : 2 : BOOST_CHECK(TestSplitHost("127.0.0.1:1/2", "127.0.0.1:1/2", 0, false));
+ - + - ]
115 [ + - + - : 2 : BOOST_CHECK(TestSplitHost("127.0.0.1:1E2", "127.0.0.1:1E2", 0, false));
+ - + - ]
116 [ + - + - : 2 : BOOST_CHECK(TestSplitHost("www.bitcoincore.org:65536", "www.bitcoincore.org:65536", 0, false));
+ - + - ]
117 [ + - + - : 2 : BOOST_CHECK(TestSplitHost("www.bitcoincore.org:0", "www.bitcoincore.org", 0, false));
+ - + - ]
118 [ + - + - : 2 : BOOST_CHECK(TestSplitHost("www.bitcoincore.org:", "www.bitcoincore.org:", 0, false));
+ - + - ]
119 : 1 : }
120 : :
121 : 9 : bool static TestParse(std::string src, std::string canon)
122 : : {
123 [ + - ]: 9 : CService addr(LookupNumeric(src, 65535));
124 [ + - ]: 9 : return canon == addr.ToStringAddrPort();
125 : 9 : }
126 : :
127 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(netbase_lookupnumeric)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
128 : : {
129 [ + - + - : 2 : BOOST_CHECK(TestParse("127.0.0.1", "127.0.0.1:65535"));
+ - + - ]
130 [ + - + - : 2 : BOOST_CHECK(TestParse("127.0.0.1:8333", "127.0.0.1:8333"));
+ - + - ]
131 [ + - + - : 2 : BOOST_CHECK(TestParse("::ffff:127.0.0.1", "127.0.0.1:65535"));
+ - + - ]
132 [ + - + - : 2 : BOOST_CHECK(TestParse("::", "[::]:65535"));
+ - + - ]
133 [ + - + - : 2 : BOOST_CHECK(TestParse("[::]:8333", "[::]:8333"));
+ - + - ]
134 [ + - + - : 2 : BOOST_CHECK(TestParse("[127.0.0.1]", "127.0.0.1:65535"));
+ - + - ]
135 [ + - + - : 2 : BOOST_CHECK(TestParse(":::", "[::]:0"));
+ - + - ]
136 : :
137 : : // verify that an internal address fails to resolve
138 [ + - + - : 2 : BOOST_CHECK(TestParse("[fd6b:88c0:8724:1:2:3:4:5]", "[::]:0"));
+ - + - ]
139 : : // and that a one-off resolves correctly
140 [ + - + - : 2 : BOOST_CHECK(TestParse("[fd6c:88c0:8724:1:2:3:4:5]", "[fd6c:88c0:8724:1:2:3:4:5]:65535"));
+ - + - ]
141 : 1 : }
142 : :
143 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(embedded_test)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
144 : : {
145 [ + - ]: 1 : CNetAddr addr1(ResolveIP("1.2.3.4"));
146 [ + - + - ]: 1 : CNetAddr addr2(ResolveIP("::FFFF:0102:0304"));
147 [ + - + - : 2 : BOOST_CHECK(addr2.IsIPv4());
+ - ]
148 [ + - + - : 1 : BOOST_CHECK_EQUAL(addr1.ToStringAddr(), addr2.ToStringAddr());
+ - + - ]
149 : 1 : }
150 : :
151 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(subnet_test)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
152 : : {
153 : :
154 [ + - + - : 2 : BOOST_CHECK(LookupSubNet("1.2.3.0/24") == LookupSubNet("1.2.3.0/255.255.255.0"));
+ - + - +
- + - ]
155 [ + - + - : 3 : BOOST_CHECK(LookupSubNet("1.2.3.0/24") != LookupSubNet("1.2.4.0/255.255.255.0"));
+ - + - +
- ]
156 [ + - + - : 2 : BOOST_CHECK(LookupSubNet("1.2.3.0/24").Match(ResolveIP("1.2.3.4")));
+ - + - +
- + - ]
157 [ + - + - : 2 : BOOST_CHECK(!LookupSubNet("1.2.2.0/24").Match(ResolveIP("1.2.3.4")));
+ - + - +
- + - ]
158 [ + - + - : 2 : BOOST_CHECK(LookupSubNet("1.2.3.4").Match(ResolveIP("1.2.3.4")));
+ - + - +
- + - ]
159 [ + - + - : 2 : BOOST_CHECK(LookupSubNet("1.2.3.4/32").Match(ResolveIP("1.2.3.4")));
+ - + - +
- + - ]
160 [ + - + - : 2 : BOOST_CHECK(!LookupSubNet("1.2.3.4").Match(ResolveIP("5.6.7.8")));
+ - + - +
- + - ]
161 [ + - + - : 3 : BOOST_CHECK(!LookupSubNet("1.2.3.4/32").Match(ResolveIP("5.6.7.8")));
+ - + - +
- ]
162 [ + - + - : 2 : BOOST_CHECK(LookupSubNet("::ffff:127.0.0.1").Match(ResolveIP("127.0.0.1")));
+ - + - +
- + - ]
163 [ + - + - : 2 : BOOST_CHECK(LookupSubNet("1:2:3:4:5:6:7:8").Match(ResolveIP("1:2:3:4:5:6:7:8")));
+ - + - +
- + - ]
164 [ + - + - : 2 : BOOST_CHECK(!LookupSubNet("1:2:3:4:5:6:7:8").Match(ResolveIP("1:2:3:4:5:6:7:9")));
+ - + - +
- + - ]
165 [ + - + - : 2 : BOOST_CHECK(LookupSubNet("1:2:3:4:5:6:7:0/112").Match(ResolveIP("1:2:3:4:5:6:7:1234")));
+ - + - +
- + - ]
166 [ + - + - : 2 : BOOST_CHECK(LookupSubNet("192.168.0.1/24").Match(ResolveIP("192.168.0.2")));
+ - + - +
- + - ]
167 [ + - + - : 2 : BOOST_CHECK(LookupSubNet("192.168.0.20/29").Match(ResolveIP("192.168.0.18")));
+ - + - +
- + - ]
168 [ + - + - : 2 : BOOST_CHECK(LookupSubNet("1.2.2.1/24").Match(ResolveIP("1.2.2.4")));
+ - + - +
- + - ]
169 [ + - + - : 2 : BOOST_CHECK(LookupSubNet("1.2.2.110/31").Match(ResolveIP("1.2.2.111")));
+ - + - +
- + - ]
170 [ + - + - : 2 : BOOST_CHECK(LookupSubNet("1.2.2.20/26").Match(ResolveIP("1.2.2.63")));
+ - + - +
- + - ]
171 : : // All-Matching IPv6 Matches arbitrary IPv6
172 [ + - + - : 2 : 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 [ + - + - : 2 : BOOST_CHECK(!LookupSubNet("::/0").Match(ResolveIP("::")));
+ - + - +
- + - ]
175 [ + - + - : 2 : 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 [ + - + - : 2 : BOOST_CHECK(!LookupSubNet("::/0").Match(ResolveIP("1.2.3.4")));
+ - + - +
- + - ]
178 : : // All-Matching IPv4 does not Match IPv6
179 [ + - + - : 2 : 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 [ + - + - : 2 : BOOST_CHECK(!CSubNet().Match(ResolveIP("1.2.3.4")));
+ - + - +
- ]
182 [ + - + - : 3 : BOOST_CHECK(!LookupSubNet("").Match(ResolveIP("4.5.6.7")));
+ - + - +
- + - ]
183 [ + - + - : 2 : BOOST_CHECK(!LookupSubNet("bloop").Match(ResolveIP("0.0.0.0")));
+ - + - +
- + - ]
184 [ + - + - : 2 : BOOST_CHECK(!LookupSubNet("bloop").Match(ResolveIP("hab")));
+ - + - +
- + - ]
185 : : // Check valid/invalid
186 [ + - + - : 2 : BOOST_CHECK(LookupSubNet("1.2.3.0/0").IsValid());
+ - + - ]
187 [ + - + - : 2 : BOOST_CHECK(!LookupSubNet("1.2.3.0/-1").IsValid());
+ - + - ]
188 [ + - + - : 2 : BOOST_CHECK(LookupSubNet("1.2.3.0/32").IsValid());
+ - + - ]
189 [ + - + - : 2 : BOOST_CHECK(!LookupSubNet("1.2.3.0/33").IsValid());
+ - + - ]
190 [ + - + - : 2 : BOOST_CHECK(!LookupSubNet("1.2.3.0/300").IsValid());
+ - + - ]
191 [ + - + - : 2 : BOOST_CHECK(LookupSubNet("1:2:3:4:5:6:7:8/0").IsValid());
+ - + - ]
192 [ + - + - : 2 : BOOST_CHECK(LookupSubNet("1:2:3:4:5:6:7:8/33").IsValid());
+ - + - ]
193 [ + - + - : 2 : BOOST_CHECK(!LookupSubNet("1:2:3:4:5:6:7:8/-1").IsValid());
+ - + - ]
194 [ + - + - : 2 : BOOST_CHECK(LookupSubNet("1:2:3:4:5:6:7:8/128").IsValid());
+ - + - ]
195 [ + - + - : 2 : BOOST_CHECK(!LookupSubNet("1:2:3:4:5:6:7:8/129").IsValid());
+ - + - ]
196 [ + - + - : 2 : BOOST_CHECK(!LookupSubNet("fuzzy").IsValid());
+ - + - ]
197 : :
198 : : //CNetAddr constructor test
199 [ + - + - : 2 : BOOST_CHECK(CSubNet(ResolveIP("127.0.0.1")).IsValid());
+ - + - +
- ]
200 [ + - + - : 2 : BOOST_CHECK(CSubNet(ResolveIP("127.0.0.1")).Match(ResolveIP("127.0.0.1")));
+ - + - +
- + - +
- ]
201 [ + - + - : 2 : BOOST_CHECK(!CSubNet(ResolveIP("127.0.0.1")).Match(ResolveIP("127.0.0.2")));
+ - + - +
- + - +
- ]
202 [ + - + - : 2 : BOOST_CHECK(CSubNet(ResolveIP("127.0.0.1")).ToString() == "127.0.0.1/32");
+ - + - +
- ]
203 : :
204 [ + - + - ]: 2 : CSubNet subnet = CSubNet(ResolveIP("1.2.3.4"), 32);
205 [ + - + - : 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/32");
+ - ]
206 [ + - + - : 1 : subnet = CSubNet(ResolveIP("1.2.3.4"), 8);
+ - ]
207 [ + - + - : 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/8");
+ - ]
208 [ + - + - : 1 : subnet = CSubNet(ResolveIP("1.2.3.4"), 0);
+ - ]
209 [ + - + - : 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/0");
+ - ]
210 : :
211 [ + - + - : 1 : subnet = CSubNet(ResolveIP("1.2.3.4"), ResolveIP("255.255.255.255"));
+ - + - +
- ]
212 [ + - + - : 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/32");
+ - ]
213 [ + - + - : 1 : subnet = CSubNet(ResolveIP("1.2.3.4"), ResolveIP("255.0.0.0"));
+ - + - +
- ]
214 [ + - + - : 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/8");
+ - ]
215 [ + - + - : 1 : subnet = CSubNet(ResolveIP("1.2.3.4"), ResolveIP("0.0.0.0"));
+ - + - +
- ]
216 [ + - + - : 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/0");
+ - ]
217 : :
218 [ + - + - : 2 : BOOST_CHECK(CSubNet(ResolveIP("1:2:3:4:5:6:7:8")).IsValid());
+ - + - +
- + - +
- ]
219 [ + - + - : 2 : BOOST_CHECK(CSubNet(ResolveIP("1:2:3:4:5:6:7:8")).Match(ResolveIP("1:2:3:4:5:6:7:8")));
+ - + - +
- + - + -
+ - + - ]
220 [ + - + - : 2 : BOOST_CHECK(!CSubNet(ResolveIP("1:2:3:4:5:6:7:8")).Match(ResolveIP("1:2:3:4:5:6:7:9")));
+ - + - +
- + - + -
+ - + - ]
221 [ + - + - : 2 : 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 [ + - + - : 2 : BOOST_CHECK(!CSubNet(ResolveIP("1.1.1.1"), ResolveIP("ffff::")).IsValid());
+ - + - +
- + - + -
+ - + - ]
224 [ + - + - : 2 : BOOST_CHECK(!CSubNet(ResolveIP("::1"), ResolveIP("255.0.0.0")).IsValid());
+ - + - +
- + - + -
+ - + - ]
225 : :
226 : : // Create Non-IP subnets.
227 : :
228 [ + - ]: 1 : const CNetAddr tor_addr{
229 [ + - + - ]: 1 : ResolveIP("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion")};
230 : :
231 [ + - ]: 1 : subnet = CSubNet(tor_addr);
232 [ + - + - : 2 : BOOST_CHECK(subnet.IsValid());
+ - + - ]
233 [ + - + - : 1 : BOOST_CHECK_EQUAL(subnet.ToString(), tor_addr.ToStringAddr());
+ - + - ]
234 [ + - + - : 2 : BOOST_CHECK(subnet.Match(tor_addr));
+ - + - ]
235 [ + - + - : 2 : BOOST_CHECK(
+ - + - +
- + - ]
236 : : !subnet.Match(ResolveIP("kpgvmscirrdqpekbqjsvw5teanhatztpp2gl6eee4zkowvwfxwenqaid.onion")));
237 [ + - + - : 2 : BOOST_CHECK(!subnet.Match(ResolveIP("1.2.3.4")));
+ - + - +
- + - ]
238 : :
239 [ + - + - : 2 : BOOST_CHECK(!CSubNet(tor_addr, 200).IsValid());
+ - + - +
- ]
240 [ + - + - : 2 : BOOST_CHECK(!CSubNet(tor_addr, ResolveIP("255.0.0.0")).IsValid());
+ - + - +
- + - +
- ]
241 : :
242 [ + - + - ]: 1 : subnet = LookupSubNet("1.2.3.4/255.255.255.255");
243 [ + - + - : 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/32");
+ - ]
244 [ + - + - ]: 1 : subnet = LookupSubNet("1.2.3.4/255.255.255.254");
245 [ + - + - : 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/31");
+ - ]
246 [ + - + - ]: 1 : subnet = LookupSubNet("1.2.3.4/255.255.255.252");
247 [ + - + - : 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/30");
+ - ]
248 [ + - + - ]: 1 : subnet = LookupSubNet("1.2.3.4/255.255.255.248");
249 [ + - + - : 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/29");
+ - ]
250 [ + - + - ]: 1 : subnet = LookupSubNet("1.2.3.4/255.255.255.240");
251 [ + - + - : 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/28");
+ - ]
252 [ + - + - ]: 1 : subnet = LookupSubNet("1.2.3.4/255.255.255.224");
253 [ + - + - : 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/27");
+ - ]
254 [ + - + - ]: 1 : subnet = LookupSubNet("1.2.3.4/255.255.255.192");
255 [ + - + - : 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/26");
+ - ]
256 [ + - + - ]: 1 : subnet = LookupSubNet("1.2.3.4/255.255.255.128");
257 [ + - + - : 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/25");
+ - ]
258 [ + - + - ]: 1 : subnet = LookupSubNet("1.2.3.4/255.255.255.0");
259 [ + - + - : 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/24");
+ - ]
260 [ + - + - ]: 1 : subnet = LookupSubNet("1.2.3.4/255.255.254.0");
261 [ + - + - : 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.2.0/23");
+ - ]
262 [ + - + - ]: 1 : subnet = LookupSubNet("1.2.3.4/255.255.252.0");
263 [ + - + - : 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/22");
+ - ]
264 [ + - + - ]: 1 : subnet = LookupSubNet("1.2.3.4/255.255.248.0");
265 [ + - + - : 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/21");
+ - ]
266 [ + - + - ]: 1 : subnet = LookupSubNet("1.2.3.4/255.255.240.0");
267 [ + - + - : 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/20");
+ - ]
268 [ + - + - ]: 1 : subnet = LookupSubNet("1.2.3.4/255.255.224.0");
269 [ + - + - : 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/19");
+ - ]
270 [ + - + - ]: 1 : subnet = LookupSubNet("1.2.3.4/255.255.192.0");
271 [ + - + - : 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/18");
+ - ]
272 [ + - + - ]: 1 : subnet = LookupSubNet("1.2.3.4/255.255.128.0");
273 [ + - + - : 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/17");
+ - ]
274 [ + - + - ]: 1 : subnet = LookupSubNet("1.2.3.4/255.255.0.0");
275 [ + - + - : 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/16");
+ - ]
276 [ + - + - ]: 1 : subnet = LookupSubNet("1.2.3.4/255.254.0.0");
277 [ + - + - : 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/15");
+ - ]
278 [ + - + - ]: 1 : subnet = LookupSubNet("1.2.3.4/255.252.0.0");
279 [ + - + - : 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/14");
+ - ]
280 [ + - + - ]: 1 : subnet = LookupSubNet("1.2.3.4/255.248.0.0");
281 [ + - + - : 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/13");
+ - ]
282 [ + - + - ]: 1 : subnet = LookupSubNet("1.2.3.4/255.240.0.0");
283 [ + - + - : 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/12");
+ - ]
284 [ + - + - ]: 1 : subnet = LookupSubNet("1.2.3.4/255.224.0.0");
285 [ + - + - : 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/11");
+ - ]
286 [ + - + - ]: 1 : subnet = LookupSubNet("1.2.3.4/255.192.0.0");
287 [ + - + - : 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/10");
+ - ]
288 [ + - + - ]: 1 : subnet = LookupSubNet("1.2.3.4/255.128.0.0");
289 [ + - + - : 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/9");
+ - ]
290 [ + - + - ]: 1 : subnet = LookupSubNet("1.2.3.4/255.0.0.0");
291 [ + - + - : 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/8");
+ - ]
292 [ + - + - ]: 1 : subnet = LookupSubNet("1.2.3.4/254.0.0.0");
293 [ + - + - : 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/7");
+ - ]
294 [ + - + - ]: 1 : subnet = LookupSubNet("1.2.3.4/252.0.0.0");
295 [ + - + - : 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/6");
+ - ]
296 [ + - + - ]: 1 : subnet = LookupSubNet("1.2.3.4/248.0.0.0");
297 [ + - + - : 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/5");
+ - ]
298 [ + - + - ]: 1 : subnet = LookupSubNet("1.2.3.4/240.0.0.0");
299 [ + - + - : 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/4");
+ - ]
300 [ + - + - ]: 1 : subnet = LookupSubNet("1.2.3.4/224.0.0.0");
301 [ + - + - : 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/3");
+ - ]
302 [ + - + - ]: 1 : subnet = LookupSubNet("1.2.3.4/192.0.0.0");
303 [ + - + - : 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/2");
+ - ]
304 [ + - + - ]: 1 : subnet = LookupSubNet("1.2.3.4/128.0.0.0");
305 [ + - + - : 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/1");
+ - ]
306 [ + - + - ]: 1 : subnet = LookupSubNet("1.2.3.4/0.0.0.0");
307 [ + - + - : 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/0");
+ - ]
308 : :
309 [ + - + - ]: 1 : subnet = LookupSubNet("1:2:3:4:5:6:7:8/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
310 [ + - + - : 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1:2:3:4:5:6:7:8/128");
+ - ]
311 [ + - + - ]: 1 : subnet = LookupSubNet("1:2:3:4:5:6:7:8/ffff:0000:0000:0000:0000:0000:0000:0000");
312 [ + - + - : 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "1::/16");
+ - ]
313 [ + - + - ]: 1 : subnet = LookupSubNet("1:2:3:4:5:6:7:8/0000:0000:0000:0000:0000:0000:0000:0000");
314 [ + - + - : 1 : BOOST_CHECK_EQUAL(subnet.ToString(), "::/0");
+ - ]
315 : : // Invalid netmasks (with 1-bits after 0-bits)
316 [ + - + - ]: 1 : subnet = LookupSubNet("1.2.3.4/255.255.232.0");
317 [ + - + - : 2 : BOOST_CHECK(!subnet.IsValid());
+ - + - ]
318 [ + - + - ]: 1 : subnet = LookupSubNet("1.2.3.4/255.0.255.255");
319 [ + - + - : 2 : BOOST_CHECK(!subnet.IsValid());
+ - + - ]
320 [ + - + - ]: 1 : subnet = LookupSubNet("1:2:3:4:5:6:7:8/ffff:ffff:ffff:fffe:ffff:ffff:ffff:ff0f");
321 [ + - + - : 2 : BOOST_CHECK(!subnet.IsValid());
+ - ]
322 : 1 : }
323 : :
324 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(netbase_getgroup)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
325 : : {
326 : 1 : NetGroupManager netgroupman{std::vector<bool>()}; // use /16
327 [ + - + - : 2 : BOOST_CHECK(netgroupman.GetGroup(ResolveIP("127.0.0.1")) == std::vector<unsigned char>({0})); // Local -> !Routable()
+ - + - +
- + - +
- ]
328 [ + - + - : 2 : BOOST_CHECK(netgroupman.GetGroup(ResolveIP("257.0.0.1")) == std::vector<unsigned char>({0})); // !Valid -> !Routable()
+ - + - +
- + - +
- ]
329 [ + - + - : 2 : BOOST_CHECK(netgroupman.GetGroup(ResolveIP("10.0.0.1")) == std::vector<unsigned char>({0})); // RFC1918 -> !Routable()
+ - + - +
- + - +
- ]
330 [ + - + - : 2 : BOOST_CHECK(netgroupman.GetGroup(ResolveIP("169.254.1.1")) == std::vector<unsigned char>({0})); // RFC3927 -> !Routable()
+ - + - +
- + - +
- ]
331 [ + - + - : 2 : BOOST_CHECK(netgroupman.GetGroup(ResolveIP("1.2.3.4")) == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // IPv4
+ - + - +
- + - +
- ]
332 [ + - + - : 2 : BOOST_CHECK(netgroupman.GetGroup(ResolveIP("::FFFF:0:102:304")) == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // RFC6145
+ - + - +
- + - +
- ]
333 [ + - + - : 2 : BOOST_CHECK(netgroupman.GetGroup(ResolveIP("64:FF9B::102:304")) == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // RFC6052
+ - + - +
- + - +
- ]
334 [ + - + - : 2 : 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 [ + - + - : 2 : 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 [ + - + - : 2 : 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 [ + - + - : 2 : 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 [ + - ]: 1 : std::vector<unsigned char> internal_group = {NET_INTERNAL, 0x12, 0x92, 0x94, 0x00, 0xeb, 0x46, 0x07, 0xc4, 0xac, 0x07};
341 [ + - + - : 2 : BOOST_CHECK(netgroupman.GetGroup(CreateInternal("baz.net")) == internal_group);
+ - + - +
- ]
342 : 1 : }
343 : :
344 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(netbase_parsenetwork)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
345 : : {
346 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ParseNetwork("ipv4"), NET_IPV4);
347 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ParseNetwork("ipv6"), NET_IPV6);
348 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ParseNetwork("onion"), NET_ONION);
349 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ParseNetwork("tor"), NET_ONION);
350 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ParseNetwork("cjdns"), NET_CJDNS);
351 : :
352 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ParseNetwork("IPv4"), NET_IPV4);
353 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ParseNetwork("IPv6"), NET_IPV6);
354 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ParseNetwork("ONION"), NET_ONION);
355 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ParseNetwork("TOR"), NET_ONION);
356 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ParseNetwork("CJDNS"), NET_CJDNS);
357 : :
358 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ParseNetwork(":)"), NET_UNROUTABLE);
359 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ParseNetwork("tÖr"), NET_UNROUTABLE);
360 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ParseNetwork("\xfe\xff"), NET_UNROUTABLE);
361 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ParseNetwork(""), NET_UNROUTABLE);
362 : 1 : }
363 : :
364 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(netpermissions_test)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
365 : : {
366 [ + - ]: 1 : bilingual_str error;
367 [ + - ]: 1 : NetWhitebindPermissions whitebindPermissions;
368 [ + - ]: 1 : NetWhitelistPermissions whitelistPermissions;
369 : 1 : ConnectionDirection connection_direction;
370 : :
371 : : // Detect invalid white bind
372 [ + - + - : 2 : BOOST_CHECK(!NetWhitebindPermissions::TryParse("", whitebindPermissions, error));
+ - + - +
- ]
373 [ + - + - : 2 : BOOST_CHECK(error.original.find("Cannot resolve -whitebind address") != std::string::npos);
+ - ]
374 [ + - + - : 2 : BOOST_CHECK(!NetWhitebindPermissions::TryParse("127.0.0.1", whitebindPermissions, error));
+ - + - +
- ]
375 [ + - + - : 2 : BOOST_CHECK(error.original.find("Need to specify a port with -whitebind") != std::string::npos);
+ - ]
376 [ + - + - : 2 : BOOST_CHECK(!NetWhitebindPermissions::TryParse("", whitebindPermissions, error));
+ - + - +
- ]
377 : :
378 : : // If no permission flags, assume backward compatibility
379 [ + - + - : 2 : BOOST_CHECK(NetWhitebindPermissions::TryParse("1.2.3.4:32", whitebindPermissions, error));
+ - + - +
- ]
380 [ + - + - : 2 : BOOST_CHECK(error.empty());
+ - ]
381 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, NetPermissionFlags::Implicit);
382 [ + - + - : 2 : BOOST_CHECK(NetPermissions::HasFlag(whitebindPermissions.m_flags, NetPermissionFlags::Implicit));
+ - ]
383 [ + - ]: 1 : NetPermissions::ClearFlag(whitebindPermissions.m_flags, NetPermissionFlags::Implicit);
384 [ + - + - : 2 : BOOST_CHECK(!NetPermissions::HasFlag(whitebindPermissions.m_flags, NetPermissionFlags::Implicit));
+ - ]
385 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, NetPermissionFlags::None);
386 [ + - ]: 1 : NetPermissions::AddFlag(whitebindPermissions.m_flags, NetPermissionFlags::Implicit);
387 [ + - + - : 2 : BOOST_CHECK(NetPermissions::HasFlag(whitebindPermissions.m_flags, NetPermissionFlags::Implicit));
+ - ]
388 : :
389 : : // Can set one permission
390 [ + - + - : 2 : BOOST_CHECK(NetWhitebindPermissions::TryParse("bloom@1.2.3.4:32", whitebindPermissions, error));
+ - + - +
- ]
391 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, NetPermissionFlags::BloomFilter);
392 [ + - + - : 2 : BOOST_CHECK(NetWhitebindPermissions::TryParse("@1.2.3.4:32", whitebindPermissions, error));
+ - + - +
- ]
393 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, NetPermissionFlags::None);
394 : :
395 [ + - + - : 1 : NetWhitebindPermissions noban, noban_download, download_noban, download;
+ - + - ]
396 : :
397 : : // "noban" implies "download"
398 [ + - + - : 2 : BOOST_REQUIRE(NetWhitebindPermissions::TryParse("noban@1.2.3.4:32", noban, error));
+ - + - +
- ]
399 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(noban.m_flags, NetPermissionFlags::NoBan);
400 [ + - + - : 2 : BOOST_CHECK(NetPermissions::HasFlag(noban.m_flags, NetPermissionFlags::Download));
+ - ]
401 [ + - + - : 2 : BOOST_CHECK(NetPermissions::HasFlag(noban.m_flags, NetPermissionFlags::NoBan));
+ - ]
402 : :
403 : : // "noban,download" is equivalent to "noban"
404 [ + - + - : 2 : BOOST_REQUIRE(NetWhitebindPermissions::TryParse("noban,download@1.2.3.4:32", noban_download, error));
+ - + - +
- ]
405 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(noban_download.m_flags, noban.m_flags);
406 : :
407 : : // "download,noban" is equivalent to "noban"
408 [ + - + - : 2 : BOOST_REQUIRE(NetWhitebindPermissions::TryParse("download,noban@1.2.3.4:32", download_noban, error));
+ - + - +
- ]
409 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(download_noban.m_flags, noban.m_flags);
410 : :
411 : : // "download" excludes (does not imply) "noban"
412 [ + - + - : 2 : BOOST_REQUIRE(NetWhitebindPermissions::TryParse("download@1.2.3.4:32", download, error));
+ - + - +
- ]
413 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(download.m_flags, NetPermissionFlags::Download);
414 [ + - + - : 2 : BOOST_CHECK(NetPermissions::HasFlag(download.m_flags, NetPermissionFlags::Download));
+ - ]
415 [ + - + - : 2 : BOOST_CHECK(!NetPermissions::HasFlag(download.m_flags, NetPermissionFlags::NoBan));
+ - ]
416 : :
417 : : // Happy path, can parse flags
418 [ + - + - : 2 : BOOST_CHECK(NetWhitebindPermissions::TryParse("bloom,forcerelay@1.2.3.4:32", whitebindPermissions, error));
+ - + - +
- ]
419 : : // forcerelay should also activate the relay permission
420 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, NetPermissionFlags::BloomFilter | NetPermissionFlags::ForceRelay | NetPermissionFlags::Relay);
421 [ + - + - : 2 : BOOST_CHECK(NetWhitebindPermissions::TryParse("bloom,relay,noban@1.2.3.4:32", whitebindPermissions, error));
+ - + - +
- ]
422 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, NetPermissionFlags::BloomFilter | NetPermissionFlags::Relay | NetPermissionFlags::NoBan);
423 [ + - + - : 2 : BOOST_CHECK(NetWhitebindPermissions::TryParse("bloom,forcerelay,noban@1.2.3.4:32", whitebindPermissions, error));
+ - + - +
- ]
424 [ + - + - : 2 : BOOST_CHECK(NetWhitebindPermissions::TryParse("all@1.2.3.4:32", whitebindPermissions, error));
+ - + - +
- ]
425 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, NetPermissionFlags::All);
426 : :
427 : : // Allow dups
428 [ + - + - : 2 : BOOST_CHECK(NetWhitebindPermissions::TryParse("bloom,relay,noban,noban@1.2.3.4:32", whitebindPermissions, error));
+ - + - +
- ]
429 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, NetPermissionFlags::BloomFilter | NetPermissionFlags::Relay | NetPermissionFlags::NoBan | NetPermissionFlags::Download); // "noban" implies "download"
430 : :
431 : : // Allow empty
432 [ + - + - : 2 : BOOST_CHECK(NetWhitebindPermissions::TryParse("bloom,relay,,noban@1.2.3.4:32", whitebindPermissions, error));
+ - + - +
- ]
433 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, NetPermissionFlags::BloomFilter | NetPermissionFlags::Relay | NetPermissionFlags::NoBan);
434 [ + - + - : 2 : BOOST_CHECK(NetWhitebindPermissions::TryParse(",@1.2.3.4:32", whitebindPermissions, error));
+ - + - +
- ]
435 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, NetPermissionFlags::None);
436 [ + - + - : 2 : BOOST_CHECK(NetWhitebindPermissions::TryParse(",,@1.2.3.4:32", whitebindPermissions, error));
+ - + - +
- ]
437 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, NetPermissionFlags::None);
438 : :
439 [ + - + - : 2 : BOOST_CHECK(!NetWhitebindPermissions::TryParse("out,forcerelay@1.2.3.4:32", whitebindPermissions, error));
+ - + - +
- ]
440 [ + - + - : 2 : 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 [ + - + - : 2 : BOOST_CHECK(!NetWhitebindPermissions::TryParse("bloom,forcerelay,oopsie@1.2.3.4:32", whitebindPermissions, error));
+ - + - +
- ]
444 [ + - + - : 2 : BOOST_CHECK(error.original.find("Invalid P2P permission") != std::string::npos);
+ - ]
445 : :
446 : : // Check netmask error
447 [ + - + - : 2 : BOOST_CHECK(!NetWhitelistPermissions::TryParse("bloom,forcerelay,noban@1.2.3.4:32", whitelistPermissions, connection_direction, error));
+ - + - +
- ]
448 [ + - + - : 2 : BOOST_CHECK(error.original.find("Invalid netmask specified in -whitelist") != std::string::npos);
+ - ]
449 : :
450 : : // Happy path for whitelist parsing
451 [ + - + - : 2 : BOOST_CHECK(NetWhitelistPermissions::TryParse("noban@1.2.3.4", whitelistPermissions, connection_direction, error));
+ - + - +
- ]
452 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(whitelistPermissions.m_flags, NetPermissionFlags::NoBan);
453 [ + - + - : 2 : BOOST_CHECK(NetPermissions::HasFlag(whitelistPermissions.m_flags, NetPermissionFlags::NoBan));
+ - ]
454 : :
455 [ + - + - : 2 : BOOST_CHECK(NetWhitelistPermissions::TryParse("bloom,forcerelay,noban,relay@1.2.3.4/32", whitelistPermissions, connection_direction, error));
+ - + - +
- ]
456 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(whitelistPermissions.m_flags, NetPermissionFlags::BloomFilter | NetPermissionFlags::ForceRelay | NetPermissionFlags::NoBan | NetPermissionFlags::Relay);
457 [ + - + - : 2 : BOOST_CHECK(error.empty());
+ - ]
458 [ + - + - : 1 : BOOST_CHECK_EQUAL(whitelistPermissions.m_subnet.ToString(), "1.2.3.4/32");
+ - ]
459 [ + - + - : 2 : BOOST_CHECK(NetWhitelistPermissions::TryParse("bloom,forcerelay,noban,relay,mempool@1.2.3.4/32", whitelistPermissions, connection_direction, error));
+ - + - +
- ]
460 [ + - + - : 2 : BOOST_CHECK(NetWhitelistPermissions::TryParse("in,relay@1.2.3.4", whitelistPermissions, connection_direction, error));
+ - + - +
- ]
461 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(connection_direction, ConnectionDirection::In);
462 [ + - + - : 2 : BOOST_CHECK(NetWhitelistPermissions::TryParse("out,bloom@1.2.3.4", whitelistPermissions, connection_direction, error));
+ - + - +
- ]
463 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(connection_direction, ConnectionDirection::Out);
464 [ + - + - : 2 : BOOST_CHECK(NetWhitelistPermissions::TryParse("in,out,bloom@1.2.3.4", whitelistPermissions, connection_direction, error));
+ - + - +
- ]
465 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(connection_direction, ConnectionDirection::Both);
466 : :
467 [ + - ]: 1 : const auto strings = NetPermissions::ToStrings(NetPermissionFlags::All);
468 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(strings.size(), 7U);
469 [ + - + - : 2 : BOOST_CHECK(std::find(strings.begin(), strings.end(), "bloomfilter") != strings.end());
+ - ]
470 [ + - + - : 2 : BOOST_CHECK(std::find(strings.begin(), strings.end(), "forcerelay") != strings.end());
+ - ]
471 [ + - + - : 2 : BOOST_CHECK(std::find(strings.begin(), strings.end(), "relay") != strings.end());
+ - ]
472 [ + - + - : 2 : BOOST_CHECK(std::find(strings.begin(), strings.end(), "noban") != strings.end());
+ - ]
473 [ + - + - : 2 : BOOST_CHECK(std::find(strings.begin(), strings.end(), "mempool") != strings.end());
+ - ]
474 [ + - + - : 2 : BOOST_CHECK(std::find(strings.begin(), strings.end(), "download") != strings.end());
+ - ]
475 [ + - + - ]: 2 : BOOST_CHECK(std::find(strings.begin(), strings.end(), "addr") != strings.end());
476 : 2 : }
477 : :
478 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(netbase_dont_resolve_strings_with_embedded_nul_characters)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
479 : : {
480 [ + - + - : 3 : BOOST_CHECK(LookupHost("127.0.0.1"s, false).has_value());
+ - ]
481 [ + - + - : 3 : BOOST_CHECK(!LookupHost("127.0.0.1\0"s, false).has_value());
+ - ]
482 [ + - + - : 3 : BOOST_CHECK(!LookupHost("127.0.0.1\0example.com"s, false).has_value());
+ - ]
483 [ + - + - : 3 : BOOST_CHECK(!LookupHost("127.0.0.1\0example.com\0"s, false).has_value());
+ - ]
484 : :
485 [ + - + - : 2 : BOOST_CHECK(LookupSubNet("1.2.3.0/24"s).IsValid());
+ - + - ]
486 [ + - + - : 2 : BOOST_CHECK(!LookupSubNet("1.2.3.0/24\0"s).IsValid());
+ - + - ]
487 [ + - + - : 2 : BOOST_CHECK(!LookupSubNet("1.2.3.0/24\0example.com"s).IsValid());
+ - + - ]
488 [ + - + - : 2 : BOOST_CHECK(!LookupSubNet("1.2.3.0/24\0example.com\0"s).IsValid());
+ - + - ]
489 [ + - + - : 2 : BOOST_CHECK(LookupSubNet("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion"s).IsValid());
+ - + - ]
490 [ + - + - : 2 : BOOST_CHECK(!LookupSubNet("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion\0"s).IsValid());
+ - + - ]
491 [ + - + - : 2 : BOOST_CHECK(!LookupSubNet("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion\0example.com"s).IsValid());
+ - + - ]
492 [ + - + - : 2 : BOOST_CHECK(!LookupSubNet("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion\0example.com\0"s).IsValid());
+ - + - ]
493 : 1 : }
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 */),
511 : : static_cast<ServiceFlags>(NODE_WITNESS | NODE_COMPACT_FILTERS | NODE_NETWORK_LIMITED),
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 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(caddress_serialize_v1)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
563 : : {
564 : 1 : DataStream s{};
565 : :
566 [ + - ]: 1 : s << CAddress::V1_NETWORK(fixture_addresses);
567 [ + - + - : 1 : BOOST_CHECK_EQUAL(HexStr(s), stream_addrv1_hex);
+ - ]
568 : 1 : }
569 : :
570 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(caddress_unserialize_v1)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
571 : : {
572 [ + - ]: 1 : DataStream s{ParseHex(stream_addrv1_hex)};
573 : 1 : std::vector<CAddress> addresses_unserialized;
574 : :
575 [ + - ]: 1 : s >> CAddress::V1_NETWORK(addresses_unserialized);
576 [ + - + - : 2 : BOOST_CHECK(fixture_addresses == addresses_unserialized);
+ - ]
577 : 1 : }
578 : :
579 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(caddress_serialize_v2)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
580 : : {
581 : 1 : DataStream s{};
582 : :
583 [ + - ]: 1 : s << CAddress::V2_NETWORK(fixture_addresses);
584 [ + - + - : 1 : BOOST_CHECK_EQUAL(HexStr(s), stream_addrv2_hex);
+ - ]
585 : 1 : }
586 : :
587 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(caddress_unserialize_v2)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
588 : : {
589 [ + - ]: 1 : DataStream s{ParseHex(stream_addrv2_hex)};
590 : 1 : std::vector<CAddress> addresses_unserialized;
591 : :
592 [ + - ]: 1 : s >> CAddress::V2_NETWORK(addresses_unserialized);
593 [ + - + - : 2 : BOOST_CHECK(fixture_addresses == addresses_unserialized);
+ - ]
594 : 1 : }
595 : :
596 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(isbadport)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
597 : : {
598 [ + - + - ]: 2 : BOOST_CHECK(IsBadPort(1));
599 [ + - + - ]: 2 : BOOST_CHECK(IsBadPort(22));
600 [ + - + - ]: 2 : BOOST_CHECK(IsBadPort(6000));
601 : :
602 [ + - + - ]: 2 : BOOST_CHECK(!IsBadPort(80));
603 [ + - + - ]: 2 : BOOST_CHECK(!IsBadPort(443));
604 [ + - + - ]: 2 : BOOST_CHECK(!IsBadPort(8333));
605 : :
606 : : // Check all ports, there must be 80 bad ports in total.
607 : 1 : size_t total_bad_ports{0};
608 [ + + ]: 65536 : for (uint16_t port = std::numeric_limits<uint16_t>::max(); port > 0; --port) {
609 [ + + ]: 65535 : if (IsBadPort(port)) {
610 : 80 : ++total_bad_ports;
611 : : }
612 : : }
613 [ + - ]: 1 : BOOST_CHECK_EQUAL(total_bad_ports, 80);
614 : 1 : }
615 : :
616 : : BOOST_AUTO_TEST_SUITE_END()
|