Bitcoin Core 22.99.0
P2P Digital Currency
net_tests.cpp
Go to the documentation of this file.
1// Copyright (c) 2012-2020 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <chainparams.h>
6#include <clientversion.h>
7#include <cstdint>
8#include <net.h>
9#include <netaddress.h>
10#include <netbase.h>
11#include <serialize.h>
12#include <span.h>
13#include <streams.h>
15#include <util/strencodings.h>
16#include <util/string.h>
17#include <util/system.h>
18#include <version.h>
19
20#include <boost/test/unit_test.hpp>
21
22#include <algorithm>
23#include <ios>
24#include <memory>
25#include <optional>
26#include <string>
27
28using namespace std::literals;
29
31
32BOOST_AUTO_TEST_CASE(cnode_listen_port)
33{
34 // test default
35 uint16_t port{GetListenPort()};
36 BOOST_CHECK(port == Params().GetDefaultPort());
37 // test set port
38 uint16_t altPort = 12345;
39 BOOST_CHECK(gArgs.SoftSetArg("-port", ToString(altPort)));
40 port = GetListenPort();
41 BOOST_CHECK(port == altPort);
42}
43
44BOOST_AUTO_TEST_CASE(cnode_simple_test)
45{
46 SOCKET hSocket = INVALID_SOCKET;
47 NodeId id = 0;
48
49 in_addr ipv4Addr;
50 ipv4Addr.s_addr = 0xa0b0c001;
51
52 CAddress addr = CAddress(CService(ipv4Addr, 7777), NODE_NETWORK);
53 std::string pszDest;
54
55 std::unique_ptr<CNode> pnode1 = std::make_unique<CNode>(
56 id++, NODE_NETWORK, hSocket, addr,
57 /* nKeyedNetGroupIn = */ 0,
58 /* nLocalHostNonceIn = */ 0,
60 /* inbound_onion = */ false);
61 BOOST_CHECK(pnode1->IsFullOutboundConn() == true);
62 BOOST_CHECK(pnode1->IsManualConn() == false);
63 BOOST_CHECK(pnode1->IsBlockOnlyConn() == false);
64 BOOST_CHECK(pnode1->IsFeelerConn() == false);
65 BOOST_CHECK(pnode1->IsAddrFetchConn() == false);
66 BOOST_CHECK(pnode1->IsInboundConn() == false);
67 BOOST_CHECK(pnode1->m_inbound_onion == false);
68 BOOST_CHECK_EQUAL(pnode1->ConnectedThroughNetwork(), Network::NET_IPV4);
69
70 std::unique_ptr<CNode> pnode2 = std::make_unique<CNode>(
71 id++, NODE_NETWORK, hSocket, addr,
72 /* nKeyedNetGroupIn = */ 1,
73 /* nLocalHostNonceIn = */ 1,
75 /* inbound_onion = */ false);
76 BOOST_CHECK(pnode2->IsFullOutboundConn() == false);
77 BOOST_CHECK(pnode2->IsManualConn() == false);
78 BOOST_CHECK(pnode2->IsBlockOnlyConn() == false);
79 BOOST_CHECK(pnode2->IsFeelerConn() == false);
80 BOOST_CHECK(pnode2->IsAddrFetchConn() == false);
81 BOOST_CHECK(pnode2->IsInboundConn() == true);
82 BOOST_CHECK(pnode2->m_inbound_onion == false);
83 BOOST_CHECK_EQUAL(pnode2->ConnectedThroughNetwork(), Network::NET_IPV4);
84
85 std::unique_ptr<CNode> pnode3 = std::make_unique<CNode>(
86 id++, NODE_NETWORK, hSocket, addr,
87 /* nKeyedNetGroupIn = */ 0,
88 /* nLocalHostNonceIn = */ 0,
90 /* inbound_onion = */ false);
91 BOOST_CHECK(pnode3->IsFullOutboundConn() == true);
92 BOOST_CHECK(pnode3->IsManualConn() == false);
93 BOOST_CHECK(pnode3->IsBlockOnlyConn() == false);
94 BOOST_CHECK(pnode3->IsFeelerConn() == false);
95 BOOST_CHECK(pnode3->IsAddrFetchConn() == false);
96 BOOST_CHECK(pnode3->IsInboundConn() == false);
97 BOOST_CHECK(pnode3->m_inbound_onion == false);
98 BOOST_CHECK_EQUAL(pnode3->ConnectedThroughNetwork(), Network::NET_IPV4);
99
100 std::unique_ptr<CNode> pnode4 = std::make_unique<CNode>(
101 id++, NODE_NETWORK, hSocket, addr,
102 /* nKeyedNetGroupIn = */ 1,
103 /* nLocalHostNonceIn = */ 1,
105 /* inbound_onion = */ true);
106 BOOST_CHECK(pnode4->IsFullOutboundConn() == false);
107 BOOST_CHECK(pnode4->IsManualConn() == false);
108 BOOST_CHECK(pnode4->IsBlockOnlyConn() == false);
109 BOOST_CHECK(pnode4->IsFeelerConn() == false);
110 BOOST_CHECK(pnode4->IsAddrFetchConn() == false);
111 BOOST_CHECK(pnode4->IsInboundConn() == true);
112 BOOST_CHECK(pnode4->m_inbound_onion == true);
113 BOOST_CHECK_EQUAL(pnode4->ConnectedThroughNetwork(), Network::NET_ONION);
114}
115
116BOOST_AUTO_TEST_CASE(cnetaddr_basic)
117{
118 CNetAddr addr;
119
120 // IPv4, INADDR_ANY
121 BOOST_REQUIRE(LookupHost("0.0.0.0", addr, false));
122 BOOST_REQUIRE(!addr.IsValid());
123 BOOST_REQUIRE(addr.IsIPv4());
124
125 BOOST_CHECK(addr.IsBindAny());
127 BOOST_CHECK_EQUAL(addr.ToString(), "0.0.0.0");
128
129 // IPv4, INADDR_NONE
130 BOOST_REQUIRE(LookupHost("255.255.255.255", addr, false));
131 BOOST_REQUIRE(!addr.IsValid());
132 BOOST_REQUIRE(addr.IsIPv4());
133
134 BOOST_CHECK(!addr.IsBindAny());
136 BOOST_CHECK_EQUAL(addr.ToString(), "255.255.255.255");
137
138 // IPv4, casual
139 BOOST_REQUIRE(LookupHost("12.34.56.78", addr, false));
140 BOOST_REQUIRE(addr.IsValid());
141 BOOST_REQUIRE(addr.IsIPv4());
142
143 BOOST_CHECK(!addr.IsBindAny());
145 BOOST_CHECK_EQUAL(addr.ToString(), "12.34.56.78");
146
147 // IPv6, in6addr_any
148 BOOST_REQUIRE(LookupHost("::", addr, false));
149 BOOST_REQUIRE(!addr.IsValid());
150 BOOST_REQUIRE(addr.IsIPv6());
151
152 BOOST_CHECK(addr.IsBindAny());
154 BOOST_CHECK_EQUAL(addr.ToString(), "::");
155
156 // IPv6, casual
157 BOOST_REQUIRE(LookupHost("1122:3344:5566:7788:9900:aabb:ccdd:eeff", addr, false));
158 BOOST_REQUIRE(addr.IsValid());
159 BOOST_REQUIRE(addr.IsIPv6());
160
161 BOOST_CHECK(!addr.IsBindAny());
163 BOOST_CHECK_EQUAL(addr.ToString(), "1122:3344:5566:7788:9900:aabb:ccdd:eeff");
164
165 // IPv6, scoped/link-local. See https://tools.ietf.org/html/rfc4007
166 // We support non-negative decimal integers (uint32_t) as zone id indices.
167 // Normal link-local scoped address functionality is to append "%" plus the
168 // zone id, for example, given a link-local address of "fe80::1" and a zone
169 // id of "32", return the address as "fe80::1%32".
170 const std::string link_local{"fe80::1"};
171 const std::string scoped_addr{link_local + "%32"};
172 BOOST_REQUIRE(LookupHost(scoped_addr, addr, false));
173 BOOST_REQUIRE(addr.IsValid());
174 BOOST_REQUIRE(addr.IsIPv6());
175 BOOST_CHECK(!addr.IsBindAny());
176 BOOST_CHECK_EQUAL(addr.ToString(), scoped_addr);
177
178 // Test that the delimiter "%" and default zone id of 0 can be omitted for the default scope.
179 BOOST_REQUIRE(LookupHost(link_local + "%0", addr, false));
180 BOOST_REQUIRE(addr.IsValid());
181 BOOST_REQUIRE(addr.IsIPv6());
182 BOOST_CHECK(!addr.IsBindAny());
183 BOOST_CHECK_EQUAL(addr.ToString(), link_local);
184
185 // TORv2, no longer supported
186 BOOST_CHECK(!addr.SetSpecial("6hzph5hv6337r6p2.onion"));
187
188 // TORv3
189 const char* torv3_addr = "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion";
190 BOOST_REQUIRE(addr.SetSpecial(torv3_addr));
191 BOOST_REQUIRE(addr.IsValid());
192 BOOST_REQUIRE(addr.IsTor());
193
194 BOOST_CHECK(!addr.IsI2P());
195 BOOST_CHECK(!addr.IsBindAny());
197 BOOST_CHECK_EQUAL(addr.ToString(), torv3_addr);
198
199 // TORv3, broken, with wrong checksum
200 BOOST_CHECK(!addr.SetSpecial("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscsad.onion"));
201
202 // TORv3, broken, with wrong version
203 BOOST_CHECK(!addr.SetSpecial("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscrye.onion"));
204
205 // TORv3, malicious
206 BOOST_CHECK(!addr.SetSpecial(std::string{
207 "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd\0wtf.onion", 66}));
208
209 // TOR, bogus length
210 BOOST_CHECK(!addr.SetSpecial(std::string{"mfrggzak.onion"}));
211
212 // TOR, invalid base32
213 BOOST_CHECK(!addr.SetSpecial(std::string{"mf*g zak.onion"}));
214
215 // I2P
216 const char* i2p_addr = "UDHDrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v4jna.b32.I2P";
217 BOOST_REQUIRE(addr.SetSpecial(i2p_addr));
218 BOOST_REQUIRE(addr.IsValid());
219 BOOST_REQUIRE(addr.IsI2P());
220
221 BOOST_CHECK(!addr.IsTor());
222 BOOST_CHECK(!addr.IsBindAny());
224 BOOST_CHECK_EQUAL(addr.ToString(), ToLower(i2p_addr));
225
226 // I2P, correct length, but decodes to less than the expected number of bytes.
227 BOOST_CHECK(!addr.SetSpecial("udhdrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v4jn=.b32.i2p"));
228
229 // I2P, extra unnecessary padding
230 BOOST_CHECK(!addr.SetSpecial("udhdrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v4jna=.b32.i2p"));
231
232 // I2P, malicious
233 BOOST_CHECK(!addr.SetSpecial("udhdrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v\0wtf.b32.i2p"s));
234
235 // I2P, valid but unsupported (56 Base32 characters)
236 // See "Encrypted LS with Base 32 Addresses" in
237 // https://geti2p.net/spec/encryptedleaseset.txt
239 !addr.SetSpecial("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscsad.b32.i2p"));
240
241 // I2P, invalid base32
242 BOOST_CHECK(!addr.SetSpecial(std::string{"tp*szydbh4dp.b32.i2p"}));
243
244 // Internal
245 addr.SetInternal("esffpp");
246 BOOST_REQUIRE(!addr.IsValid()); // "internal" is considered invalid
247 BOOST_REQUIRE(addr.IsInternal());
248
249 BOOST_CHECK(!addr.IsBindAny());
251 BOOST_CHECK_EQUAL(addr.ToString(), "esffpvrt3wpeaygy.internal");
252
253 // Totally bogus
254 BOOST_CHECK(!addr.SetSpecial("totally bogus"));
255}
256
257BOOST_AUTO_TEST_CASE(cnetaddr_tostring_canonical_ipv6)
258{
259 // Test that CNetAddr::ToString formats IPv6 addresses with zero compression as described in
260 // RFC 5952 ("A Recommendation for IPv6 Address Text Representation").
261 const std::map<std::string, std::string> canonical_representations_ipv6{
262 {"0000:0000:0000:0000:0000:0000:0000:0000", "::"},
263 {"000:0000:000:00:0:00:000:0000", "::"},
264 {"000:000:000:000:000:000:000:000", "::"},
265 {"00:00:00:00:00:00:00:00", "::"},
266 {"0:0:0:0:0:0:0:0", "::"},
267 {"0:0:0:0:0:0:0:1", "::1"},
268 {"2001:0:0:1:0:0:0:1", "2001:0:0:1::1"},
269 {"2001:0db8:0:0:1:0:0:1", "2001:db8::1:0:0:1"},
270 {"2001:0db8:85a3:0000:0000:8a2e:0370:7334", "2001:db8:85a3::8a2e:370:7334"},
271 {"2001:0db8::0001", "2001:db8::1"},
272 {"2001:0db8::0001:0000", "2001:db8::1:0"},
273 {"2001:0db8::1:0:0:1", "2001:db8::1:0:0:1"},
274 {"2001:db8:0000:0:1::1", "2001:db8::1:0:0:1"},
275 {"2001:db8:0000:1:1:1:1:1", "2001:db8:0:1:1:1:1:1"},
276 {"2001:db8:0:0:0:0:2:1", "2001:db8::2:1"},
277 {"2001:db8:0:0:0::1", "2001:db8::1"},
278 {"2001:db8:0:0:1:0:0:1", "2001:db8::1:0:0:1"},
279 {"2001:db8:0:0:1::1", "2001:db8::1:0:0:1"},
280 {"2001:DB8:0:0:1::1", "2001:db8::1:0:0:1"},
281 {"2001:db8:0:0::1", "2001:db8::1"},
282 {"2001:db8:0:0:aaaa::1", "2001:db8::aaaa:0:0:1"},
283 {"2001:db8:0:1:1:1:1:1", "2001:db8:0:1:1:1:1:1"},
284 {"2001:db8:0::1", "2001:db8::1"},
285 {"2001:db8:85a3:0:0:8a2e:370:7334", "2001:db8:85a3::8a2e:370:7334"},
286 {"2001:db8::0:1", "2001:db8::1"},
287 {"2001:db8::0:1:0:0:1", "2001:db8::1:0:0:1"},
288 {"2001:DB8::1", "2001:db8::1"},
289 {"2001:db8::1", "2001:db8::1"},
290 {"2001:db8::1:0:0:1", "2001:db8::1:0:0:1"},
291 {"2001:db8::1:1:1:1:1", "2001:db8:0:1:1:1:1:1"},
292 {"2001:db8::aaaa:0:0:1", "2001:db8::aaaa:0:0:1"},
293 {"2001:db8:aaaa:bbbb:cccc:dddd:0:1", "2001:db8:aaaa:bbbb:cccc:dddd:0:1"},
294 {"2001:db8:aaaa:bbbb:cccc:dddd::1", "2001:db8:aaaa:bbbb:cccc:dddd:0:1"},
295 {"2001:db8:aaaa:bbbb:cccc:dddd:eeee:0001", "2001:db8:aaaa:bbbb:cccc:dddd:eeee:1"},
296 {"2001:db8:aaaa:bbbb:cccc:dddd:eeee:001", "2001:db8:aaaa:bbbb:cccc:dddd:eeee:1"},
297 {"2001:db8:aaaa:bbbb:cccc:dddd:eeee:01", "2001:db8:aaaa:bbbb:cccc:dddd:eeee:1"},
298 {"2001:db8:aaaa:bbbb:cccc:dddd:eeee:1", "2001:db8:aaaa:bbbb:cccc:dddd:eeee:1"},
299 {"2001:db8:aaaa:bbbb:cccc:dddd:eeee:aaaa", "2001:db8:aaaa:bbbb:cccc:dddd:eeee:aaaa"},
300 {"2001:db8:aaaa:bbbb:cccc:dddd:eeee:AAAA", "2001:db8:aaaa:bbbb:cccc:dddd:eeee:aaaa"},
301 {"2001:db8:aaaa:bbbb:cccc:dddd:eeee:AaAa", "2001:db8:aaaa:bbbb:cccc:dddd:eeee:aaaa"},
302 };
303 for (const auto& [input_address, expected_canonical_representation_output] : canonical_representations_ipv6) {
304 CNetAddr net_addr;
305 BOOST_REQUIRE(LookupHost(input_address, net_addr, false));
306 BOOST_REQUIRE(net_addr.IsIPv6());
307 BOOST_CHECK_EQUAL(net_addr.ToString(), expected_canonical_representation_output);
308 }
309}
310
311BOOST_AUTO_TEST_CASE(cnetaddr_serialize_v1)
312{
313 CNetAddr addr;
315
316 s << addr;
317 BOOST_CHECK_EQUAL(HexStr(s), "00000000000000000000000000000000");
318 s.clear();
319
320 BOOST_REQUIRE(LookupHost("1.2.3.4", addr, false));
321 s << addr;
322 BOOST_CHECK_EQUAL(HexStr(s), "00000000000000000000ffff01020304");
323 s.clear();
324
325 BOOST_REQUIRE(LookupHost("1a1b:2a2b:3a3b:4a4b:5a5b:6a6b:7a7b:8a8b", addr, false));
326 s << addr;
327 BOOST_CHECK_EQUAL(HexStr(s), "1a1b2a2b3a3b4a4b5a5b6a6b7a7b8a8b");
328 s.clear();
329
330 // TORv2, no longer supported
331 BOOST_CHECK(!addr.SetSpecial("6hzph5hv6337r6p2.onion"));
332
333 BOOST_REQUIRE(addr.SetSpecial("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion"));
334 s << addr;
335 BOOST_CHECK_EQUAL(HexStr(s), "00000000000000000000000000000000");
336 s.clear();
337
338 addr.SetInternal("a");
339 s << addr;
340 BOOST_CHECK_EQUAL(HexStr(s), "fd6b88c08724ca978112ca1bbdcafac2");
341 s.clear();
342}
343
344BOOST_AUTO_TEST_CASE(cnetaddr_serialize_v2)
345{
346 CNetAddr addr;
348 // Add ADDRV2_FORMAT to the version so that the CNetAddr
349 // serialize method produces an address in v2 format.
351
352 s << addr;
353 BOOST_CHECK_EQUAL(HexStr(s), "021000000000000000000000000000000000");
354 s.clear();
355
356 BOOST_REQUIRE(LookupHost("1.2.3.4", addr, false));
357 s << addr;
358 BOOST_CHECK_EQUAL(HexStr(s), "010401020304");
359 s.clear();
360
361 BOOST_REQUIRE(LookupHost("1a1b:2a2b:3a3b:4a4b:5a5b:6a6b:7a7b:8a8b", addr, false));
362 s << addr;
363 BOOST_CHECK_EQUAL(HexStr(s), "02101a1b2a2b3a3b4a4b5a5b6a6b7a7b8a8b");
364 s.clear();
365
366 // TORv2, no longer supported
367 BOOST_CHECK(!addr.SetSpecial("6hzph5hv6337r6p2.onion"));
368
369 BOOST_REQUIRE(addr.SetSpecial("kpgvmscirrdqpekbqjsvw5teanhatztpp2gl6eee4zkowvwfxwenqaid.onion"));
370 s << addr;
371 BOOST_CHECK_EQUAL(HexStr(s), "042053cd5648488c4707914182655b7664034e09e66f7e8cbf1084e654eb56c5bd88");
372 s.clear();
373
374 BOOST_REQUIRE(addr.SetInternal("a"));
375 s << addr;
376 BOOST_CHECK_EQUAL(HexStr(s), "0210fd6b88c08724ca978112ca1bbdcafac2");
377 s.clear();
378}
379
380BOOST_AUTO_TEST_CASE(cnetaddr_unserialize_v2)
381{
382 CNetAddr addr;
384 // Add ADDRV2_FORMAT to the version so that the CNetAddr
385 // unserialize method expects an address in v2 format.
387
388 // Valid IPv4.
389 s << MakeSpan(ParseHex("01" // network type (IPv4)
390 "04" // address length
391 "01020304")); // address
392 s >> addr;
393 BOOST_CHECK(addr.IsValid());
394 BOOST_CHECK(addr.IsIPv4());
396 BOOST_CHECK_EQUAL(addr.ToString(), "1.2.3.4");
397 BOOST_REQUIRE(s.empty());
398
399 // Invalid IPv4, valid length but address itself is shorter.
400 s << MakeSpan(ParseHex("01" // network type (IPv4)
401 "04" // address length
402 "0102")); // address
403 BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure, HasReason("end of data"));
404 BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
405 s.clear();
406
407 // Invalid IPv4, with bogus length.
408 s << MakeSpan(ParseHex("01" // network type (IPv4)
409 "05" // address length
410 "01020304")); // address
411 BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure,
412 HasReason("BIP155 IPv4 address with length 5 (should be 4)"));
413 BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
414 s.clear();
415
416 // Invalid IPv4, with extreme length.
417 s << MakeSpan(ParseHex("01" // network type (IPv4)
418 "fd0102" // address length (513 as CompactSize)
419 "01020304")); // address
420 BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure,
421 HasReason("Address too long: 513 > 512"));
422 BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
423 s.clear();
424
425 // Valid IPv6.
426 s << MakeSpan(ParseHex("02" // network type (IPv6)
427 "10" // address length
428 "0102030405060708090a0b0c0d0e0f10")); // address
429 s >> addr;
430 BOOST_CHECK(addr.IsValid());
431 BOOST_CHECK(addr.IsIPv6());
433 BOOST_CHECK_EQUAL(addr.ToString(), "102:304:506:708:90a:b0c:d0e:f10");
434 BOOST_REQUIRE(s.empty());
435
436 // Valid IPv6, contains embedded "internal".
437 s << MakeSpan(ParseHex(
438 "02" // network type (IPv6)
439 "10" // address length
440 "fd6b88c08724ca978112ca1bbdcafac2")); // address: 0xfd + sha256("bitcoin")[0:5] +
441 // sha256(name)[0:10]
442 s >> addr;
443 BOOST_CHECK(addr.IsInternal());
445 BOOST_CHECK_EQUAL(addr.ToString(), "zklycewkdo64v6wc.internal");
446 BOOST_REQUIRE(s.empty());
447
448 // Invalid IPv6, with bogus length.
449 s << MakeSpan(ParseHex("02" // network type (IPv6)
450 "04" // address length
451 "00")); // address
452 BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure,
453 HasReason("BIP155 IPv6 address with length 4 (should be 16)"));
454 BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
455 s.clear();
456
457 // Invalid IPv6, contains embedded IPv4.
458 s << MakeSpan(ParseHex("02" // network type (IPv6)
459 "10" // address length
460 "00000000000000000000ffff01020304")); // address
461 s >> addr;
462 BOOST_CHECK(!addr.IsValid());
463 BOOST_REQUIRE(s.empty());
464
465 // Invalid IPv6, contains embedded TORv2.
466 s << MakeSpan(ParseHex("02" // network type (IPv6)
467 "10" // address length
468 "fd87d87eeb430102030405060708090a")); // address
469 s >> addr;
470 BOOST_CHECK(!addr.IsValid());
471 BOOST_REQUIRE(s.empty());
472
473 // TORv2, no longer supported.
474 s << MakeSpan(ParseHex("03" // network type (TORv2)
475 "0a" // address length
476 "f1f2f3f4f5f6f7f8f9fa")); // address
477 s >> addr;
478 BOOST_CHECK(!addr.IsValid());
479 BOOST_REQUIRE(s.empty());
480
481 // Valid TORv3.
482 s << MakeSpan(ParseHex("04" // network type (TORv3)
483 "20" // address length
484 "79bcc625184b05194975c28b66b66b04" // address
485 "69f7f6556fb1ac3189a79b40dda32f1f"
486 ));
487 s >> addr;
488 BOOST_CHECK(addr.IsValid());
489 BOOST_CHECK(addr.IsTor());
492 "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion");
493 BOOST_REQUIRE(s.empty());
494
495 // Invalid TORv3, with bogus length.
496 s << MakeSpan(ParseHex("04" // network type (TORv3)
497 "00" // address length
498 "00" // address
499 ));
500 BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure,
501 HasReason("BIP155 TORv3 address with length 0 (should be 32)"));
502 BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
503 s.clear();
504
505 // Valid I2P.
506 s << MakeSpan(ParseHex("05" // network type (I2P)
507 "20" // address length
508 "a2894dabaec08c0051a481a6dac88b64" // address
509 "f98232ae42d4b6fd2fa81952dfe36a87"));
510 s >> addr;
511 BOOST_CHECK(addr.IsValid());
512 BOOST_CHECK(addr.IsI2P());
515 "ukeu3k5oycgaauneqgtnvselmt4yemvoilkln7jpvamvfx7dnkdq.b32.i2p");
516 BOOST_REQUIRE(s.empty());
517
518 // Invalid I2P, with bogus length.
519 s << MakeSpan(ParseHex("05" // network type (I2P)
520 "03" // address length
521 "00" // address
522 ));
523 BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure,
524 HasReason("BIP155 I2P address with length 3 (should be 32)"));
525 BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
526 s.clear();
527
528 // Valid CJDNS.
529 s << MakeSpan(ParseHex("06" // network type (CJDNS)
530 "10" // address length
531 "fc000001000200030004000500060007" // address
532 ));
533 s >> addr;
534 BOOST_CHECK(addr.IsValid());
535 BOOST_CHECK(addr.IsCJDNS());
537 BOOST_CHECK_EQUAL(addr.ToString(), "fc00:1:2:3:4:5:6:7");
538 BOOST_REQUIRE(s.empty());
539
540 // Invalid CJDNS, wrong prefix.
541 s << MakeSpan(ParseHex("06" // network type (CJDNS)
542 "10" // address length
543 "aa000001000200030004000500060007" // address
544 ));
545 s >> addr;
546 BOOST_CHECK(addr.IsCJDNS());
547 BOOST_CHECK(!addr.IsValid());
548 BOOST_REQUIRE(s.empty());
549
550 // Invalid CJDNS, with bogus length.
551 s << MakeSpan(ParseHex("06" // network type (CJDNS)
552 "01" // address length
553 "00" // address
554 ));
555 BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure,
556 HasReason("BIP155 CJDNS address with length 1 (should be 16)"));
557 BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
558 s.clear();
559
560 // Unknown, with extreme length.
561 s << MakeSpan(ParseHex("aa" // network type (unknown)
562 "fe00000002" // address length (CompactSize's MAX_SIZE)
563 "01020304050607" // address
564 ));
565 BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure,
566 HasReason("Address too long: 33554432 > 512"));
567 BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
568 s.clear();
569
570 // Unknown, with reasonable length.
571 s << MakeSpan(ParseHex("aa" // network type (unknown)
572 "04" // address length
573 "01020304" // address
574 ));
575 s >> addr;
576 BOOST_CHECK(!addr.IsValid());
577 BOOST_REQUIRE(s.empty());
578
579 // Unknown, with zero length.
580 s << MakeSpan(ParseHex("aa" // network type (unknown)
581 "00" // address length
582 "" // address
583 ));
584 s >> addr;
585 BOOST_CHECK(!addr.IsValid());
586 BOOST_REQUIRE(s.empty());
587}
588
589// prior to PR #14728, this test triggers an undefined behavior
590BOOST_AUTO_TEST_CASE(ipv4_peer_with_ipv6_addrMe_test)
591{
592 // set up local addresses; all that's necessary to reproduce the bug is
593 // that a normal IPv4 address is among the entries, but if this address is
594 // !IsRoutable the undefined behavior is easier to trigger deterministically
595 {
597 in_addr ipv4AddrLocal;
598 ipv4AddrLocal.s_addr = 0x0100007f;
599 CNetAddr addr = CNetAddr(ipv4AddrLocal);
601 lsi.nScore = 23;
602 lsi.nPort = 42;
603 mapLocalHost[addr] = lsi;
604 }
605
606 // create a peer with an IPv4 address
607 in_addr ipv4AddrPeer;
608 ipv4AddrPeer.s_addr = 0xa0b0c001;
609 CAddress addr = CAddress(CService(ipv4AddrPeer, 7777), NODE_NETWORK);
610 std::unique_ptr<CNode> pnode = std::make_unique<CNode>(0, NODE_NETWORK, INVALID_SOCKET, addr, /* nKeyedNetGroupIn */ 0, /* nLocalHostNonceIn */ 0, CAddress{}, /* pszDest */ std::string{}, ConnectionType::OUTBOUND_FULL_RELAY, /* inbound_onion */ false);
611 pnode->fSuccessfullyConnected.store(true);
612
613 // the peer claims to be reaching us via IPv6
614 in6_addr ipv6AddrLocal;
615 memset(ipv6AddrLocal.s6_addr, 0, 16);
616 ipv6AddrLocal.s6_addr[0] = 0xcc;
617 CAddress addrLocal = CAddress(CService(ipv6AddrLocal, 7777), NODE_NETWORK);
618 pnode->SetAddrLocal(addrLocal);
619
620 // before patch, this causes undefined behavior detectable with clang's -fsanitize=memory
621 GetLocalAddrForPeer(&*pnode);
622
623 // suppress no-checks-run warning; if this test fails, it's by triggering a sanitizer
624 BOOST_CHECK(1);
625}
626
627
628BOOST_AUTO_TEST_CASE(LimitedAndReachable_Network)
629{
634
635 SetReachable(NET_IPV4, false);
636 SetReachable(NET_IPV6, false);
637 SetReachable(NET_ONION, false);
638 SetReachable(NET_I2P, false);
639
644
645 SetReachable(NET_IPV4, true);
646 SetReachable(NET_IPV6, true);
647 SetReachable(NET_ONION, true);
648 SetReachable(NET_I2P, true);
649
654}
655
656BOOST_AUTO_TEST_CASE(LimitedAndReachable_NetworkCaseUnroutableAndInternal)
657{
660
663
664 BOOST_CHECK(IsReachable(NET_UNROUTABLE)); // Ignored for both networks
666}
667
668CNetAddr UtilBuildAddress(unsigned char p1, unsigned char p2, unsigned char p3, unsigned char p4)
669{
670 unsigned char ip[] = {p1, p2, p3, p4};
671
672 struct sockaddr_in sa;
673 memset(&sa, 0, sizeof(sockaddr_in)); // initialize the memory block
674 memcpy(&(sa.sin_addr), &ip, sizeof(ip));
675 return CNetAddr(sa.sin_addr);
676}
677
678
679BOOST_AUTO_TEST_CASE(LimitedAndReachable_CNetAddr)
680{
681 CNetAddr addr = UtilBuildAddress(0x001, 0x001, 0x001, 0x001); // 1.1.1.1
682
683 SetReachable(NET_IPV4, true);
685
686 SetReachable(NET_IPV4, false);
687 BOOST_CHECK(!IsReachable(addr));
688
689 SetReachable(NET_IPV4, true); // have to reset this, because this is stateful.
690}
691
692
693BOOST_AUTO_TEST_CASE(LocalAddress_BasicLifecycle)
694{
695 CService addr = CService(UtilBuildAddress(0x002, 0x001, 0x001, 0x001), 1000); // 2.1.1.1:1000
696
697 SetReachable(NET_IPV4, true);
698
699 BOOST_CHECK(!IsLocal(addr));
700 BOOST_CHECK(AddLocal(addr, 1000));
701 BOOST_CHECK(IsLocal(addr));
702
703 RemoveLocal(addr);
704 BOOST_CHECK(!IsLocal(addr));
705}
706
const CChainParams & Params()
Return the currently selected parameters.
bool SoftSetArg(const std::string &strArg, const std::string &strValue)
Set an argument if it doesn't already have a value.
Definition: system.cpp:608
A CService with information about it as peer.
Definition: protocol.h:359
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:205
void SetVersion(int n)
Definition: streams.h:362
int GetVersion() const
Definition: streams.h:363
bool empty() const
Definition: streams.h:256
void clear()
Definition: streams.h:261
Network address.
Definition: netaddress.h:119
bool IsBindAny() const
Definition: netaddress.cpp:310
bool SetSpecial(const std::string &addr)
Parse a Tor or I2P address and set this object to it.
Definition: netaddress.cpp:212
std::string ToString() const
Definition: netaddress.cpp:631
bool IsCJDNS() const
Check whether this object represents a CJDNS address.
Definition: netaddress.cpp:423
bool IsTor() const
Check whether this object represents a TOR address.
Definition: netaddress.cpp:413
bool IsValid() const
Definition: netaddress.cpp:451
bool IsIPv4() const
Definition: netaddress.cpp:318
bool IsIPv6() const
Definition: netaddress.cpp:320
bool IsInternal() const
Definition: netaddress.cpp:500
bool SetInternal(const std::string &name)
Create an "internal" address that represents a name or FQDN.
Definition: netaddress.cpp:173
bool IsAddrV1Compatible() const
Check if the current object can be serialized in pre-ADDRv2/BIP155 format.
Definition: netaddress.cpp:505
bool IsI2P() const
Check whether this object represents an I2P address.
Definition: netaddress.cpp:418
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:523
BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
Definition: setup_common.h:218
#define INVALID_SOCKET
Definition: compat.h:53
unsigned int SOCKET
Definition: compat.h:41
BOOST_AUTO_TEST_SUITE_END()
static CService ip(uint32_t i)
uint16_t GetListenPort()
Definition: net.cpp:125
bool IsLocal(const CService &addr)
check whether a given address is potentially local
Definition: net.cpp:321
void RemoveLocal(const CService &addr)
Definition: net.cpp:283
bool AddLocal(const CService &addr_, int nScore)
Definition: net.cpp:250
RecursiveMutex cs_mapLocalHost
Definition: net.cpp:114
std::optional< CAddress > GetLocalAddrForPeer(CNode *pnode)
Returns a local address that we should advertise to this peer.
Definition: net.cpp:208
void SetReachable(enum Network net, bool reachable)
Mark a network as reachable or unreachable (no automatic connects to it)
Definition: net.cpp:290
bool IsReachable(enum Network net)
Definition: net.cpp:298
@ OUTBOUND_FULL_RELAY
These are the default connections that we use to connect with the network.
@ INBOUND
Inbound connections are those initiated by a peer.
int64_t NodeId
Definition: net.h:87
BOOST_AUTO_TEST_CASE(cnode_listen_port)
Definition: net_tests.cpp:32
CNetAddr UtilBuildAddress(unsigned char p1, unsigned char p2, unsigned char p3, unsigned char p4)
Definition: net_tests.cpp:668
static constexpr int ADDRV2_FORMAT
A flag that is ORed into the protocol version to designate that addresses should be serialized in (un...
Definition: netaddress.h:34
@ NET_I2P
I2P.
Definition: netaddress.h:59
@ NET_ONION
TOR (v2 or v3)
Definition: netaddress.h:56
@ NET_IPV6
IPv6.
Definition: netaddress.h:53
@ NET_IPV4
IPv4.
Definition: netaddress.h:50
@ NET_UNROUTABLE
Addresses from these networks are not publicly routable on the global Internet.
Definition: netaddress.h:47
@ NET_INTERNAL
A set of addresses that represent the hash of a string or FQDN.
Definition: netaddress.h:66
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
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
#define BOOST_CHECK(expr)
Definition: object.cpp:17
@ NODE_NETWORK
Definition: protocol.h:277
@ SER_NETWORK
Definition: serialize.h:138
constexpr Span< A > MakeSpan(A(&a)[N])
MakeSpan for arrays:
Definition: span.h:222
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
std::string ToLower(const std::string &str)
Returns the lowercase equivalent of the given string.
std::vector< unsigned char > ParseHex(const char *psz)
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:87
Basic testing setup.
Definition: setup_common.h:76
uint16_t nPort
Definition: net.h:229
int nScore
Definition: net.h:228
#define LOCK(cs)
Definition: sync.h:226
ArgsManager gArgs
Definition: system.cpp:85
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:12