Bitcoin Core 29.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>
13#include <util/strencodings.h>
14#include <util/translation.h>
15
16#include <string>
17#include <numeric>
18
19#include <boost/test/unit_test.hpp>
20
21using namespace std::literals;
22
24
25static CNetAddr ResolveIP(const std::string& ip)
26{
27 return LookupHost(ip, false).value_or(CNetAddr{});
28}
29
30static CNetAddr CreateInternal(const std::string& host)
31{
32 CNetAddr addr;
33 addr.SetInternal(host);
34 return addr;
35}
36
37BOOST_AUTO_TEST_CASE(netbase_networks)
38{
39 BOOST_CHECK(ResolveIP("127.0.0.1").GetNetwork() == NET_UNROUTABLE);
40 BOOST_CHECK(ResolveIP("::1").GetNetwork() == NET_UNROUTABLE);
41 BOOST_CHECK(ResolveIP("8.8.8.8").GetNetwork() == NET_IPV4);
42 BOOST_CHECK(ResolveIP("2001::8888").GetNetwork() == NET_IPV6);
43 BOOST_CHECK(ResolveIP("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion").GetNetwork() == NET_ONION);
44 BOOST_CHECK(CreateInternal("foo.com").GetNetwork() == NET_INTERNAL);
45}
46
47BOOST_AUTO_TEST_CASE(netbase_properties)
48{
49
50 BOOST_CHECK(ResolveIP("127.0.0.1").IsIPv4());
51 BOOST_CHECK(ResolveIP("::FFFF:192.168.1.1").IsIPv4());
52 BOOST_CHECK(ResolveIP("::1").IsIPv6());
53 BOOST_CHECK(ResolveIP("10.0.0.1").IsRFC1918());
54 BOOST_CHECK(ResolveIP("192.168.1.1").IsRFC1918());
55 BOOST_CHECK(ResolveIP("172.31.255.255").IsRFC1918());
56 BOOST_CHECK(ResolveIP("198.18.0.0").IsRFC2544());
57 BOOST_CHECK(ResolveIP("198.19.255.255").IsRFC2544());
58 BOOST_CHECK(ResolveIP("2001:0DB8::").IsRFC3849());
59 BOOST_CHECK(ResolveIP("169.254.1.1").IsRFC3927());
60 BOOST_CHECK(ResolveIP("2002::1").IsRFC3964());
61 BOOST_CHECK(ResolveIP("FC00::").IsRFC4193());
62 BOOST_CHECK(ResolveIP("2001::2").IsRFC4380());
63 BOOST_CHECK(ResolveIP("2001:10::").IsRFC4843());
64 BOOST_CHECK(ResolveIP("2001:20::").IsRFC7343());
65 BOOST_CHECK(ResolveIP("FE80::").IsRFC4862());
66 BOOST_CHECK(ResolveIP("64:FF9B::").IsRFC6052());
67 BOOST_CHECK(ResolveIP("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion").IsTor());
68 BOOST_CHECK(ResolveIP("127.0.0.1").IsLocal());
70 BOOST_CHECK(ResolveIP("8.8.8.8").IsRoutable());
71 BOOST_CHECK(ResolveIP("2001::1").IsRoutable());
72 BOOST_CHECK(ResolveIP("127.0.0.1").IsValid());
73 BOOST_CHECK(CreateInternal("FD6B:88C0:8724:edb1:8e4:3588:e546:35ca").IsInternal());
74 BOOST_CHECK(CreateInternal("bar.com").IsInternal());
75
76}
77
78bool static TestSplitHost(const std::string& test, const std::string& host, uint16_t port, bool validPort=true)
79{
80 std::string hostOut;
81 uint16_t portOut{0};
82 bool validPortOut = SplitHostPort(test, portOut, hostOut);
83 return hostOut == host && portOut == port && validPortOut == validPort;
84}
85
86BOOST_AUTO_TEST_CASE(netbase_splithost)
87{
88 BOOST_CHECK(TestSplitHost("www.bitcoincore.org", "www.bitcoincore.org", 0));
89 BOOST_CHECK(TestSplitHost("[www.bitcoincore.org]", "www.bitcoincore.org", 0));
90 BOOST_CHECK(TestSplitHost("www.bitcoincore.org:80", "www.bitcoincore.org", 80));
91 BOOST_CHECK(TestSplitHost("[www.bitcoincore.org]:80", "www.bitcoincore.org", 80));
92 BOOST_CHECK(TestSplitHost("127.0.0.1", "127.0.0.1", 0));
93 BOOST_CHECK(TestSplitHost("127.0.0.1:8333", "127.0.0.1", 8333));
94 BOOST_CHECK(TestSplitHost("[127.0.0.1]", "127.0.0.1", 0));
95 BOOST_CHECK(TestSplitHost("[127.0.0.1]:8333", "127.0.0.1", 8333));
96 BOOST_CHECK(TestSplitHost("::ffff:127.0.0.1", "::ffff:127.0.0.1", 0));
97 BOOST_CHECK(TestSplitHost("[::ffff:127.0.0.1]:8333", "::ffff:127.0.0.1", 8333));
98 BOOST_CHECK(TestSplitHost("[::]:8333", "::", 8333));
99 BOOST_CHECK(TestSplitHost("::8333", "::8333", 0));
100 BOOST_CHECK(TestSplitHost(":8333", "", 8333));
101 BOOST_CHECK(TestSplitHost("[]:8333", "", 8333));
102 BOOST_CHECK(TestSplitHost("", "", 0));
103 BOOST_CHECK(TestSplitHost(":65535", "", 65535));
104 BOOST_CHECK(TestSplitHost(":65536", ":65536", 0, false));
105 BOOST_CHECK(TestSplitHost(":-1", ":-1", 0, false));
106 BOOST_CHECK(TestSplitHost("[]:70001", "[]:70001", 0, false));
107 BOOST_CHECK(TestSplitHost("[]:-1", "[]:-1", 0, false));
108 BOOST_CHECK(TestSplitHost("[]:-0", "[]:-0", 0, false));
109 BOOST_CHECK(TestSplitHost("[]:0", "", 0, false));
110 BOOST_CHECK(TestSplitHost("[]:1/2", "[]:1/2", 0, false));
111 BOOST_CHECK(TestSplitHost("[]:1E2", "[]:1E2", 0, false));
112 BOOST_CHECK(TestSplitHost("127.0.0.1:65536", "127.0.0.1:65536", 0, false));
113 BOOST_CHECK(TestSplitHost("127.0.0.1:0", "127.0.0.1", 0, false));
114 BOOST_CHECK(TestSplitHost("127.0.0.1:", "127.0.0.1:", 0, false));
115 BOOST_CHECK(TestSplitHost("127.0.0.1:1/2", "127.0.0.1:1/2", 0, false));
116 BOOST_CHECK(TestSplitHost("127.0.0.1:1E2", "127.0.0.1:1E2", 0, false));
117 BOOST_CHECK(TestSplitHost("www.bitcoincore.org:65536", "www.bitcoincore.org:65536", 0, false));
118 BOOST_CHECK(TestSplitHost("www.bitcoincore.org:0", "www.bitcoincore.org", 0, false));
119 BOOST_CHECK(TestSplitHost("www.bitcoincore.org:", "www.bitcoincore.org:", 0, false));
120}
121
122bool static TestParse(std::string src, std::string canon)
123{
124 CService addr(LookupNumeric(src, 65535));
125 return canon == addr.ToStringAddrPort();
126}
127
128BOOST_AUTO_TEST_CASE(netbase_lookupnumeric)
129{
130 BOOST_CHECK(TestParse("127.0.0.1", "127.0.0.1:65535"));
131 BOOST_CHECK(TestParse("127.0.0.1:8333", "127.0.0.1:8333"));
132 BOOST_CHECK(TestParse("::ffff:127.0.0.1", "127.0.0.1:65535"));
133 BOOST_CHECK(TestParse("::", "[::]:65535"));
134 BOOST_CHECK(TestParse("[::]:8333", "[::]:8333"));
135 BOOST_CHECK(TestParse("[127.0.0.1]", "127.0.0.1:65535"));
136 BOOST_CHECK(TestParse(":::", "[::]:0"));
137
138 // verify that an internal address fails to resolve
139 BOOST_CHECK(TestParse("[fd6b:88c0:8724:1:2:3:4:5]", "[::]:0"));
140 // and that a one-off resolves correctly
141 BOOST_CHECK(TestParse("[fd6c:88c0:8724:1:2:3:4:5]", "[fd6c:88c0:8724:1:2:3:4:5]:65535"));
142}
143
145{
146 CNetAddr addr1(ResolveIP("1.2.3.4"));
147 CNetAddr addr2(ResolveIP("::FFFF:0102:0304"));
148 BOOST_CHECK(addr2.IsIPv4());
149 BOOST_CHECK_EQUAL(addr1.ToStringAddr(), addr2.ToStringAddr());
150}
151
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/+24").IsValid());
189 BOOST_CHECK(LookupSubNet("1.2.3.0/32").IsValid());
190 BOOST_CHECK(!LookupSubNet("1.2.3.0/33").IsValid());
191 BOOST_CHECK(!LookupSubNet("1.2.3.0/300").IsValid());
192 BOOST_CHECK(LookupSubNet("1:2:3:4:5:6:7:8/0").IsValid());
193 BOOST_CHECK(LookupSubNet("1:2:3:4:5:6:7:8/33").IsValid());
194 BOOST_CHECK(!LookupSubNet("1:2:3:4:5:6:7:8/-1").IsValid());
195 BOOST_CHECK(LookupSubNet("1:2:3:4:5:6:7:8/128").IsValid());
196 BOOST_CHECK(!LookupSubNet("1:2:3:4:5:6:7:8/129").IsValid());
197 BOOST_CHECK(!LookupSubNet("fuzzy").IsValid());
198
199 //CNetAddr constructor test
200 BOOST_CHECK(CSubNet(ResolveIP("127.0.0.1")).IsValid());
201 BOOST_CHECK(CSubNet(ResolveIP("127.0.0.1")).Match(ResolveIP("127.0.0.1")));
202 BOOST_CHECK(!CSubNet(ResolveIP("127.0.0.1")).Match(ResolveIP("127.0.0.2")));
203 BOOST_CHECK(CSubNet(ResolveIP("127.0.0.1")).ToString() == "127.0.0.1/32");
204
205 CSubNet subnet = CSubNet(ResolveIP("1.2.3.4"), 32);
206 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/32");
207 subnet = CSubNet(ResolveIP("1.2.3.4"), 8);
208 BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/8");
209 subnet = CSubNet(ResolveIP("1.2.3.4"), 0);
210 BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/0");
211
212 subnet = CSubNet(ResolveIP("1.2.3.4"), ResolveIP("255.255.255.255"));
213 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/32");
214 subnet = CSubNet(ResolveIP("1.2.3.4"), ResolveIP("255.0.0.0"));
215 BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/8");
216 subnet = CSubNet(ResolveIP("1.2.3.4"), ResolveIP("0.0.0.0"));
217 BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/0");
218
219 BOOST_CHECK(CSubNet(ResolveIP("1:2:3:4:5:6:7:8")).IsValid());
220 BOOST_CHECK(CSubNet(ResolveIP("1:2:3:4:5:6:7:8")).Match(ResolveIP("1:2:3:4:5:6:7:8")));
221 BOOST_CHECK(!CSubNet(ResolveIP("1:2:3:4:5:6:7:8")).Match(ResolveIP("1:2:3:4:5:6:7:9")));
222 BOOST_CHECK(CSubNet(ResolveIP("1:2:3:4:5:6:7:8")).ToString() == "1:2:3:4:5:6:7:8/128");
223 // IPv4 address with IPv6 netmask or the other way around.
224 BOOST_CHECK(!CSubNet(ResolveIP("1.1.1.1"), ResolveIP("ffff::")).IsValid());
225 BOOST_CHECK(!CSubNet(ResolveIP("::1"), ResolveIP("255.0.0.0")).IsValid());
226
227 // Create Non-IP subnets.
228
229 const CNetAddr tor_addr{
230 ResolveIP("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion")};
231
232 subnet = CSubNet(tor_addr);
233 BOOST_CHECK(subnet.IsValid());
234 BOOST_CHECK_EQUAL(subnet.ToString(), tor_addr.ToStringAddr());
235 BOOST_CHECK(subnet.Match(tor_addr));
237 !subnet.Match(ResolveIP("kpgvmscirrdqpekbqjsvw5teanhatztpp2gl6eee4zkowvwfxwenqaid.onion")));
238 BOOST_CHECK(!subnet.Match(ResolveIP("1.2.3.4")));
239
240 BOOST_CHECK(!CSubNet(tor_addr, 200).IsValid());
241 BOOST_CHECK(!CSubNet(tor_addr, ResolveIP("255.0.0.0")).IsValid());
242
243 subnet = LookupSubNet("1.2.3.4/255.255.255.255");
244 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/32");
245 subnet = LookupSubNet("1.2.3.4/255.255.255.254");
246 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/31");
247 subnet = LookupSubNet("1.2.3.4/255.255.255.252");
248 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/30");
249 subnet = LookupSubNet("1.2.3.4/255.255.255.248");
250 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/29");
251 subnet = LookupSubNet("1.2.3.4/255.255.255.240");
252 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/28");
253 subnet = LookupSubNet("1.2.3.4/255.255.255.224");
254 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/27");
255 subnet = LookupSubNet("1.2.3.4/255.255.255.192");
256 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/26");
257 subnet = LookupSubNet("1.2.3.4/255.255.255.128");
258 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/25");
259 subnet = LookupSubNet("1.2.3.4/255.255.255.0");
260 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/24");
261 subnet = LookupSubNet("1.2.3.4/255.255.254.0");
262 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.2.0/23");
263 subnet = LookupSubNet("1.2.3.4/255.255.252.0");
264 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/22");
265 subnet = LookupSubNet("1.2.3.4/255.255.248.0");
266 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/21");
267 subnet = LookupSubNet("1.2.3.4/255.255.240.0");
268 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/20");
269 subnet = LookupSubNet("1.2.3.4/255.255.224.0");
270 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/19");
271 subnet = LookupSubNet("1.2.3.4/255.255.192.0");
272 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/18");
273 subnet = LookupSubNet("1.2.3.4/255.255.128.0");
274 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/17");
275 subnet = LookupSubNet("1.2.3.4/255.255.0.0");
276 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/16");
277 subnet = LookupSubNet("1.2.3.4/255.254.0.0");
278 BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/15");
279 subnet = LookupSubNet("1.2.3.4/255.252.0.0");
280 BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/14");
281 subnet = LookupSubNet("1.2.3.4/255.248.0.0");
282 BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/13");
283 subnet = LookupSubNet("1.2.3.4/255.240.0.0");
284 BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/12");
285 subnet = LookupSubNet("1.2.3.4/255.224.0.0");
286 BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/11");
287 subnet = LookupSubNet("1.2.3.4/255.192.0.0");
288 BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/10");
289 subnet = LookupSubNet("1.2.3.4/255.128.0.0");
290 BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/9");
291 subnet = LookupSubNet("1.2.3.4/255.0.0.0");
292 BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/8");
293 subnet = LookupSubNet("1.2.3.4/254.0.0.0");
294 BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/7");
295 subnet = LookupSubNet("1.2.3.4/252.0.0.0");
296 BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/6");
297 subnet = LookupSubNet("1.2.3.4/248.0.0.0");
298 BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/5");
299 subnet = LookupSubNet("1.2.3.4/240.0.0.0");
300 BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/4");
301 subnet = LookupSubNet("1.2.3.4/224.0.0.0");
302 BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/3");
303 subnet = LookupSubNet("1.2.3.4/192.0.0.0");
304 BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/2");
305 subnet = LookupSubNet("1.2.3.4/128.0.0.0");
306 BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/1");
307 subnet = LookupSubNet("1.2.3.4/0.0.0.0");
308 BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/0");
309
310 subnet = LookupSubNet("1:2:3:4:5:6:7:8/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
311 BOOST_CHECK_EQUAL(subnet.ToString(), "1:2:3:4:5:6:7:8/128");
312 subnet = LookupSubNet("1:2:3:4:5:6:7:8/ffff:0000:0000:0000:0000:0000:0000:0000");
313 BOOST_CHECK_EQUAL(subnet.ToString(), "1::/16");
314 subnet = LookupSubNet("1:2:3:4:5:6:7:8/0000:0000:0000:0000:0000:0000:0000:0000");
315 BOOST_CHECK_EQUAL(subnet.ToString(), "::/0");
316 // Invalid netmasks (with 1-bits after 0-bits)
317 subnet = LookupSubNet("1.2.3.4/255.255.232.0");
318 BOOST_CHECK(!subnet.IsValid());
319 subnet = LookupSubNet("1.2.3.4/255.0.255.255");
320 BOOST_CHECK(!subnet.IsValid());
321 subnet = LookupSubNet("1:2:3:4:5:6:7:8/ffff:ffff:ffff:fffe:ffff:ffff:ffff:ff0f");
322 BOOST_CHECK(!subnet.IsValid());
323}
324
325BOOST_AUTO_TEST_CASE(netbase_getgroup)
326{
327 NetGroupManager netgroupman{std::vector<bool>()}; // use /16
328 BOOST_CHECK(netgroupman.GetGroup(ResolveIP("127.0.0.1")) == std::vector<unsigned char>({0})); // Local -> !Routable()
329 BOOST_CHECK(netgroupman.GetGroup(ResolveIP("257.0.0.1")) == std::vector<unsigned char>({0})); // !Valid -> !Routable()
330 BOOST_CHECK(netgroupman.GetGroup(ResolveIP("10.0.0.1")) == std::vector<unsigned char>({0})); // RFC1918 -> !Routable()
331 BOOST_CHECK(netgroupman.GetGroup(ResolveIP("169.254.1.1")) == std::vector<unsigned char>({0})); // RFC3927 -> !Routable()
332 BOOST_CHECK(netgroupman.GetGroup(ResolveIP("1.2.3.4")) == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // IPv4
333 BOOST_CHECK(netgroupman.GetGroup(ResolveIP("::FFFF:0:102:304")) == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // RFC6145
334 BOOST_CHECK(netgroupman.GetGroup(ResolveIP("64:FF9B::102:304")) == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // RFC6052
335 BOOST_CHECK(netgroupman.GetGroup(ResolveIP("2002:102:304:9999:9999:9999:9999:9999")) == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // RFC3964
336 BOOST_CHECK(netgroupman.GetGroup(ResolveIP("2001:0:9999:9999:9999:9999:FEFD:FCFB")) == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // RFC4380
337 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
338 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
339
340 // baz.net sha256 hash: 12929400eb4607c4ac075f087167e75286b179c693eb059a01774b864e8fe505
341 std::vector<unsigned char> internal_group = {NET_INTERNAL, 0x12, 0x92, 0x94, 0x00, 0xeb, 0x46, 0x07, 0xc4, 0xac, 0x07};
342 BOOST_CHECK(netgroupman.GetGroup(CreateInternal("baz.net")) == internal_group);
343}
344
345BOOST_AUTO_TEST_CASE(netbase_parsenetwork)
346{
352
358
363}
364
365BOOST_AUTO_TEST_CASE(netpermissions_test)
366{
367 bilingual_str error;
368 NetWhitebindPermissions whitebindPermissions;
369 NetWhitelistPermissions whitelistPermissions;
370 ConnectionDirection connection_direction;
371
372 // Detect invalid white bind
373 BOOST_CHECK(!NetWhitebindPermissions::TryParse("", whitebindPermissions, error));
374 BOOST_CHECK(error.original.find("Cannot resolve -whitebind address") != std::string::npos);
375 BOOST_CHECK(!NetWhitebindPermissions::TryParse("127.0.0.1", whitebindPermissions, error));
376 BOOST_CHECK(error.original.find("Need to specify a port with -whitebind") != std::string::npos);
377 BOOST_CHECK(!NetWhitebindPermissions::TryParse("", whitebindPermissions, error));
378
379 // If no permission flags, assume backward compatibility
380 BOOST_CHECK(NetWhitebindPermissions::TryParse("1.2.3.4:32", whitebindPermissions, error));
381 BOOST_CHECK(error.empty());
389
390 // Can set one permission
391 BOOST_CHECK(NetWhitebindPermissions::TryParse("bloom@1.2.3.4:32", whitebindPermissions, error));
393 BOOST_CHECK(NetWhitebindPermissions::TryParse("@1.2.3.4:32", whitebindPermissions, error));
395
396 NetWhitebindPermissions noban, noban_download, download_noban, download;
397
398 // "noban" implies "download"
399 BOOST_REQUIRE(NetWhitebindPermissions::TryParse("noban@1.2.3.4:32", noban, error));
403
404 // "noban,download" is equivalent to "noban"
405 BOOST_REQUIRE(NetWhitebindPermissions::TryParse("noban,download@1.2.3.4:32", noban_download, error));
406 BOOST_CHECK_EQUAL(noban_download.m_flags, noban.m_flags);
407
408 // "download,noban" is equivalent to "noban"
409 BOOST_REQUIRE(NetWhitebindPermissions::TryParse("download,noban@1.2.3.4:32", download_noban, error));
410 BOOST_CHECK_EQUAL(download_noban.m_flags, noban.m_flags);
411
412 // "download" excludes (does not imply) "noban"
413 BOOST_REQUIRE(NetWhitebindPermissions::TryParse("download@1.2.3.4:32", download, error));
417
418 // Happy path, can parse flags
419 BOOST_CHECK(NetWhitebindPermissions::TryParse("bloom,forcerelay@1.2.3.4:32", whitebindPermissions, error));
420 // forcerelay should also activate the relay permission
422 BOOST_CHECK(NetWhitebindPermissions::TryParse("bloom,relay,noban@1.2.3.4:32", whitebindPermissions, error));
424 BOOST_CHECK(NetWhitebindPermissions::TryParse("bloom,forcerelay,noban@1.2.3.4:32", whitebindPermissions, error));
425 BOOST_CHECK(NetWhitebindPermissions::TryParse("all@1.2.3.4:32", whitebindPermissions, error));
427
428 // Allow dups
429 BOOST_CHECK(NetWhitebindPermissions::TryParse("bloom,relay,noban,noban@1.2.3.4:32", whitebindPermissions, error));
431
432 // Allow empty
433 BOOST_CHECK(NetWhitebindPermissions::TryParse("bloom,relay,,noban@1.2.3.4:32", whitebindPermissions, error));
435 BOOST_CHECK(NetWhitebindPermissions::TryParse(",@1.2.3.4:32", whitebindPermissions, error));
437 BOOST_CHECK(NetWhitebindPermissions::TryParse(",,@1.2.3.4:32", whitebindPermissions, error));
439
440 BOOST_CHECK(!NetWhitebindPermissions::TryParse("out,forcerelay@1.2.3.4:32", whitebindPermissions, error));
441 BOOST_CHECK(error.original.find("whitebind may only be used for incoming connections (\"out\" was passed)") != std::string::npos);
442
443 // Detect invalid flag
444 BOOST_CHECK(!NetWhitebindPermissions::TryParse("bloom,forcerelay,oopsie@1.2.3.4:32", whitebindPermissions, error));
445 BOOST_CHECK(error.original.find("Invalid P2P permission") != std::string::npos);
446
447 // Check netmask error
448 BOOST_CHECK(!NetWhitelistPermissions::TryParse("bloom,forcerelay,noban@1.2.3.4:32", whitelistPermissions, connection_direction, error));
449 BOOST_CHECK(error.original.find("Invalid netmask specified in -whitelist") != std::string::npos);
450
451 // Happy path for whitelist parsing
452 BOOST_CHECK(NetWhitelistPermissions::TryParse("noban@1.2.3.4", whitelistPermissions, connection_direction, error));
455
456 BOOST_CHECK(NetWhitelistPermissions::TryParse("bloom,forcerelay,noban,relay@1.2.3.4/32", whitelistPermissions, connection_direction, error));
458 BOOST_CHECK(error.empty());
459 BOOST_CHECK_EQUAL(whitelistPermissions.m_subnet.ToString(), "1.2.3.4/32");
460 BOOST_CHECK(NetWhitelistPermissions::TryParse("bloom,forcerelay,noban,relay,mempool@1.2.3.4/32", whitelistPermissions, connection_direction, error));
461 BOOST_CHECK(NetWhitelistPermissions::TryParse("in,relay@1.2.3.4", whitelistPermissions, connection_direction, error));
462 BOOST_CHECK_EQUAL(connection_direction, ConnectionDirection::In);
463 BOOST_CHECK(NetWhitelistPermissions::TryParse("out,bloom@1.2.3.4", whitelistPermissions, connection_direction, error));
464 BOOST_CHECK_EQUAL(connection_direction, ConnectionDirection::Out);
465 BOOST_CHECK(NetWhitelistPermissions::TryParse("in,out,bloom@1.2.3.4", whitelistPermissions, connection_direction, error));
466 BOOST_CHECK_EQUAL(connection_direction, ConnectionDirection::Both);
467
469 BOOST_CHECK_EQUAL(strings.size(), 7U);
470 BOOST_CHECK(std::find(strings.begin(), strings.end(), "bloomfilter") != strings.end());
471 BOOST_CHECK(std::find(strings.begin(), strings.end(), "forcerelay") != strings.end());
472 BOOST_CHECK(std::find(strings.begin(), strings.end(), "relay") != strings.end());
473 BOOST_CHECK(std::find(strings.begin(), strings.end(), "noban") != strings.end());
474 BOOST_CHECK(std::find(strings.begin(), strings.end(), "mempool") != strings.end());
475 BOOST_CHECK(std::find(strings.begin(), strings.end(), "download") != strings.end());
476 BOOST_CHECK(std::find(strings.begin(), strings.end(), "addr") != strings.end());
477}
478
479BOOST_AUTO_TEST_CASE(netbase_dont_resolve_strings_with_embedded_nul_characters)
480{
481 BOOST_CHECK(LookupHost("127.0.0.1"s, false).has_value());
482 BOOST_CHECK(!LookupHost("127.0.0.1\0"s, false).has_value());
483 BOOST_CHECK(!LookupHost("127.0.0.1\0example.com"s, false).has_value());
484 BOOST_CHECK(!LookupHost("127.0.0.1\0example.com\0"s, false).has_value());
485
486 BOOST_CHECK(LookupSubNet("1.2.3.0/24"s).IsValid());
487 BOOST_CHECK(!LookupSubNet("1.2.3.0/24\0"s).IsValid());
488 BOOST_CHECK(!LookupSubNet("1.2.3.0/24\0example.com"s).IsValid());
489 BOOST_CHECK(!LookupSubNet("1.2.3.0/24\0example.com\0"s).IsValid());
490 BOOST_CHECK(LookupSubNet("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion"s).IsValid());
491 BOOST_CHECK(!LookupSubNet("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion\0"s).IsValid());
492 BOOST_CHECK(!LookupSubNet("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion\0example.com"s).IsValid());
493 BOOST_CHECK(!LookupSubNet("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion\0example.com\0"s).IsValid());
494}
495
496// Since CNetAddr (un)ser is tested separately in net_tests.cpp here we only
497// try a few edge cases for port, service flags and time.
498
499static const std::vector<CAddress> fixture_addresses({
500 CAddress{
501 CService(CNetAddr(in6_addr(IN6ADDR_LOOPBACK_INIT)), 0 /* port */),
502 NODE_NONE,
503 NodeSeconds{0x4966bc61s}, /* Fri Jan 9 02:54:25 UTC 2009 */
504 },
505 CAddress{
506 CService(CNetAddr(in6_addr(IN6ADDR_LOOPBACK_INIT)), 0x00f1 /* port */),
508 NodeSeconds{0x83766279s}, /* Tue Nov 22 11:22:33 UTC 2039 */
509 },
510 CAddress{
511 CService(CNetAddr(in6_addr(IN6ADDR_LOOPBACK_INIT)), 0xf1f2 /* port */),
513 NodeSeconds{0xffffffffs}, /* Sun Feb 7 06:28:15 UTC 2106 */
514 },
515});
516
517// fixture_addresses should equal to this when serialized in V1 format.
518// When this is unserialized from V1 format it should equal to fixture_addresses.
519static constexpr const char* stream_addrv1_hex =
520 "03" // number of entries
521
522 "61bc6649" // time, Fri Jan 9 02:54:25 UTC 2009
523 "0000000000000000" // service flags, NODE_NONE
524 "00000000000000000000000000000001" // address, fixed 16 bytes (IPv4 embedded in IPv6)
525 "0000" // port
526
527 "79627683" // time, Tue Nov 22 11:22:33 UTC 2039
528 "0100000000000000" // service flags, NODE_NETWORK
529 "00000000000000000000000000000001" // address, fixed 16 bytes (IPv6)
530 "00f1" // port
531
532 "ffffffff" // time, Sun Feb 7 06:28:15 UTC 2106
533 "4804000000000000" // service flags, NODE_WITNESS | NODE_COMPACT_FILTERS | NODE_NETWORK_LIMITED
534 "00000000000000000000000000000001" // address, fixed 16 bytes (IPv6)
535 "f1f2"; // port
536
537// fixture_addresses should equal to this when serialized in V2 format.
538// When this is unserialized from V2 format it should equal to fixture_addresses.
539static constexpr const char* stream_addrv2_hex =
540 "03" // number of entries
541
542 "61bc6649" // time, Fri Jan 9 02:54:25 UTC 2009
543 "00" // service flags, COMPACTSIZE(NODE_NONE)
544 "02" // network id, IPv6
545 "10" // address length, COMPACTSIZE(16)
546 "00000000000000000000000000000001" // address
547 "0000" // port
548
549 "79627683" // time, Tue Nov 22 11:22:33 UTC 2039
550 "01" // service flags, COMPACTSIZE(NODE_NETWORK)
551 "02" // network id, IPv6
552 "10" // address length, COMPACTSIZE(16)
553 "00000000000000000000000000000001" // address
554 "00f1" // port
555
556 "ffffffff" // time, Sun Feb 7 06:28:15 UTC 2106
557 "fd4804" // service flags, COMPACTSIZE(NODE_WITNESS | NODE_COMPACT_FILTERS | NODE_NETWORK_LIMITED)
558 "02" // network id, IPv6
559 "10" // address length, COMPACTSIZE(16)
560 "00000000000000000000000000000001" // address
561 "f1f2"; // port
562
563BOOST_AUTO_TEST_CASE(caddress_serialize_v1)
564{
565 DataStream s{};
566
569}
570
571BOOST_AUTO_TEST_CASE(caddress_unserialize_v1)
572{
574 std::vector<CAddress> addresses_unserialized;
575
576 s >> CAddress::V1_NETWORK(addresses_unserialized);
577 BOOST_CHECK(fixture_addresses == addresses_unserialized);
578}
579
580BOOST_AUTO_TEST_CASE(caddress_serialize_v2)
581{
582 DataStream s{};
583
586}
587
588BOOST_AUTO_TEST_CASE(caddress_unserialize_v2)
589{
591 std::vector<CAddress> addresses_unserialized;
592
593 s >> CAddress::V2_NETWORK(addresses_unserialized);
594 BOOST_CHECK(fixture_addresses == addresses_unserialized);
595}
596
598{
601 BOOST_CHECK(IsBadPort(6000));
602
604 BOOST_CHECK(!IsBadPort(443));
605 BOOST_CHECK(!IsBadPort(8333));
606
607 // Check all possible ports and ensure we only flag the expected amount as bad
608 std::list<int> ports(std::numeric_limits<uint16_t>::max());
609 std::iota(ports.begin(), ports.end(), 1);
610 BOOST_CHECK_EQUAL(std::ranges::count_if(ports, IsBadPort), 85);
611}
612
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:172
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:531
std::string ToStringAddrPort() const
Definition: netaddress.cpp:907
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:130
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_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
static CService ip(uint32_t i)
std::string HexStr(const std::span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Definition: hex_base.cpp:29
static const std::string addr1
Definition: key_tests.cpp:30
static const std::string addr2
Definition: key_tests.cpp:31
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:245
bool IsLocal(const CService &addr)
check whether a given address is potentially local
Definition: net.cpp:328
@ 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:816
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:177
enum Network ParseNetwork(const std::string &net_in)
Definition: netbase.cpp:100
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:220
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:851
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 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 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 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:68
Basic testing setup.
Definition: setup_common.h:64
Bilingual messages:
Definition: translation.h:24
bool empty() const
Definition: translation.h:35
std::string original
Definition: translation.h:25
bool SplitHostPort(std::string_view in, uint16_t &portOut, std::string &hostOut)
Splits socket address string into host string and port value.
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
Definition: time.h:25