20 #include <boost/test/unit_test.hpp>
28 using namespace std::literals;
38 uint16_t altPort = 12345;
49 ipv4Addr.s_addr = 0xa0b0c001;
54 std::unique_ptr<CNode> pnode1 = std::make_unique<CNode>(
id++,
73 std::unique_ptr<CNode> pnode2 = std::make_unique<CNode>(
id++,
92 std::unique_ptr<CNode> pnode3 = std::make_unique<CNode>(
id++,
111 std::unique_ptr<CNode> pnode4 = std::make_unique<CNode>(
id++,
121 BOOST_CHECK(pnode4->IsFullOutboundConn() ==
false);
136 BOOST_REQUIRE(
LookupHost(
"0.0.0.0", addr,
false));
137 BOOST_REQUIRE(!addr.
IsValid());
138 BOOST_REQUIRE(addr.
IsIPv4());
145 BOOST_REQUIRE(
LookupHost(
"255.255.255.255", addr,
false));
146 BOOST_REQUIRE(!addr.
IsValid());
147 BOOST_REQUIRE(addr.
IsIPv4());
154 BOOST_REQUIRE(
LookupHost(
"12.34.56.78", addr,
false));
156 BOOST_REQUIRE(addr.
IsIPv4());
164 BOOST_REQUIRE(!addr.
IsValid());
165 BOOST_REQUIRE(addr.
IsIPv6());
172 BOOST_REQUIRE(
LookupHost(
"1122:3344:5566:7788:9900:aabb:ccdd:eeff", addr,
false));
174 BOOST_REQUIRE(addr.
IsIPv6());
185 const std::string link_local{
"fe80::1"};
186 const std::string scoped_addr{link_local +
"%32"};
187 BOOST_REQUIRE(
LookupHost(scoped_addr, addr,
false));
189 BOOST_REQUIRE(addr.
IsIPv6());
194 BOOST_REQUIRE(
LookupHost(link_local +
"%0", addr,
false));
196 BOOST_REQUIRE(addr.
IsIPv6());
204 const char* torv3_addr =
"pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion";
207 BOOST_REQUIRE(addr.
IsTor());
222 "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd\0wtf.onion", 66}));
231 const char* i2p_addr =
"UDHDrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v4jna.b32.I2P";
234 BOOST_REQUIRE(addr.
IsI2P());
254 !addr.
SetSpecial(
"pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscsad.b32.i2p"));
261 BOOST_REQUIRE(!addr.
IsValid());
276 const std::map<std::string, std::string> canonical_representations_ipv6{
277 {
"0000:0000:0000:0000:0000:0000:0000:0000",
"::"},
278 {
"000:0000:000:00:0:00:000:0000",
"::"},
279 {
"000:000:000:000:000:000:000:000",
"::"},
280 {
"00:00:00:00:00:00:00:00",
"::"},
281 {
"0:0:0:0:0:0:0:0",
"::"},
282 {
"0:0:0:0:0:0:0:1",
"::1"},
283 {
"2001:0:0:1:0:0:0:1",
"2001:0:0:1::1"},
284 {
"2001:0db8:0:0:1:0:0:1",
"2001:db8::1:0:0:1"},
285 {
"2001:0db8:85a3:0000:0000:8a2e:0370:7334",
"2001:db8:85a3::8a2e:370:7334"},
286 {
"2001:0db8::0001",
"2001:db8::1"},
287 {
"2001:0db8::0001:0000",
"2001:db8::1:0"},
288 {
"2001:0db8::1:0:0:1",
"2001:db8::1:0:0:1"},
289 {
"2001:db8:0000:0:1::1",
"2001:db8::1:0:0:1"},
290 {
"2001:db8:0000:1:1:1:1:1",
"2001:db8:0:1:1:1:1:1"},
291 {
"2001:db8:0:0:0:0:2:1",
"2001:db8::2:1"},
292 {
"2001:db8:0:0:0::1",
"2001:db8::1"},
293 {
"2001:db8:0:0:1:0:0:1",
"2001:db8::1:0:0:1"},
294 {
"2001:db8:0:0:1::1",
"2001:db8::1:0:0:1"},
295 {
"2001:DB8:0:0:1::1",
"2001:db8::1:0:0:1"},
296 {
"2001:db8:0:0::1",
"2001:db8::1"},
297 {
"2001:db8:0:0:aaaa::1",
"2001:db8::aaaa:0:0:1"},
298 {
"2001:db8:0:1:1:1:1:1",
"2001:db8:0:1:1:1:1:1"},
299 {
"2001:db8:0::1",
"2001:db8::1"},
300 {
"2001:db8:85a3:0:0:8a2e:370:7334",
"2001:db8:85a3::8a2e:370:7334"},
301 {
"2001:db8::0:1",
"2001:db8::1"},
302 {
"2001:db8::0:1:0:0:1",
"2001:db8::1:0:0:1"},
303 {
"2001:DB8::1",
"2001:db8::1"},
304 {
"2001:db8::1",
"2001:db8::1"},
305 {
"2001:db8::1:0:0:1",
"2001:db8::1:0:0:1"},
306 {
"2001:db8::1:1:1:1:1",
"2001:db8:0:1:1:1:1:1"},
307 {
"2001:db8::aaaa:0:0:1",
"2001:db8::aaaa:0:0:1"},
308 {
"2001:db8:aaaa:bbbb:cccc:dddd:0:1",
"2001:db8:aaaa:bbbb:cccc:dddd:0:1"},
309 {
"2001:db8:aaaa:bbbb:cccc:dddd::1",
"2001:db8:aaaa:bbbb:cccc:dddd:0:1"},
310 {
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:0001",
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:1"},
311 {
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:001",
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:1"},
312 {
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:01",
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:1"},
313 {
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:1",
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:1"},
314 {
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:aaaa",
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:aaaa"},
315 {
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:AAAA",
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:aaaa"},
316 {
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:AaAa",
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:aaaa"},
318 for (
const auto& [input_address, expected_canonical_representation_output] : canonical_representations_ipv6) {
320 BOOST_REQUIRE(
LookupHost(input_address, net_addr,
false));
321 BOOST_REQUIRE(net_addr.
IsIPv6());
335 BOOST_REQUIRE(
LookupHost(
"1.2.3.4", addr,
false));
340 BOOST_REQUIRE(
LookupHost(
"1a1b:2a2b:3a3b:4a4b:5a5b:6a6b:7a7b:8a8b", addr,
false));
346 BOOST_CHECK(!addr.SetSpecial(
"6hzph5hv6337r6p2.onion"));
348 BOOST_REQUIRE(addr.SetSpecial(
"pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion"));
353 addr.SetInternal(
"a");
371 BOOST_REQUIRE(
LookupHost(
"1.2.3.4", addr,
false));
376 BOOST_REQUIRE(
LookupHost(
"1a1b:2a2b:3a3b:4a4b:5a5b:6a6b:7a7b:8a8b", addr,
false));
382 BOOST_CHECK(!addr.SetSpecial(
"6hzph5hv6337r6p2.onion"));
384 BOOST_REQUIRE(addr.SetSpecial(
"kpgvmscirrdqpekbqjsvw5teanhatztpp2gl6eee4zkowvwfxwenqaid.onion"));
389 BOOST_REQUIRE(addr.SetInternal(
"a"));
412 BOOST_REQUIRE(s.
empty());
418 BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure,
HasReason(
"end of data"));
419 BOOST_REQUIRE(!s.
empty());
426 BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure,
427 HasReason(
"BIP155 IPv4 address with length 5 (should be 4)"));
428 BOOST_REQUIRE(!s.
empty());
435 BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure,
436 HasReason(
"Address too long: 513 > 512"));
437 BOOST_REQUIRE(!s.
empty());
443 "0102030405060708090a0b0c0d0e0f10")};
449 BOOST_REQUIRE(s.
empty());
455 "fd6b88c08724ca978112ca1bbdcafac2")};
461 BOOST_REQUIRE(s.
empty());
467 BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure,
468 HasReason(
"BIP155 IPv6 address with length 4 (should be 16)"));
469 BOOST_REQUIRE(!s.
empty());
475 "00000000000000000000ffff01020304")};
478 BOOST_REQUIRE(s.
empty());
483 "fd87d87eeb430102030405060708090a")};
486 BOOST_REQUIRE(s.
empty());
491 "f1f2f3f4f5f6f7f8f9fa")};
494 BOOST_REQUIRE(s.
empty());
499 "79bcc625184b05194975c28b66b66b04"
500 "69f7f6556fb1ac3189a79b40dda32f1f"
507 "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion");
508 BOOST_REQUIRE(s.
empty());
515 BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure,
516 HasReason(
"BIP155 TORv3 address with length 0 (should be 32)"));
517 BOOST_REQUIRE(!s.
empty());
523 "a2894dabaec08c0051a481a6dac88b64"
524 "f98232ae42d4b6fd2fa81952dfe36a87")};
530 "ukeu3k5oycgaauneqgtnvselmt4yemvoilkln7jpvamvfx7dnkdq.b32.i2p");
531 BOOST_REQUIRE(s.
empty());
538 BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure,
539 HasReason(
"BIP155 I2P address with length 3 (should be 32)"));
540 BOOST_REQUIRE(!s.
empty());
546 "fc000001000200030004000500060007"
553 BOOST_REQUIRE(s.
empty());
558 "aa000001000200030004000500060007"
563 BOOST_REQUIRE(s.
empty());
570 BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure,
571 HasReason(
"BIP155 CJDNS address with length 1 (should be 16)"));
572 BOOST_REQUIRE(!s.
empty());
580 BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure,
581 HasReason(
"Address too long: 33554432 > 512"));
582 BOOST_REQUIRE(!s.
empty());
592 BOOST_REQUIRE(s.
empty());
601 BOOST_REQUIRE(s.
empty());
612 in_addr ipv4AddrLocal;
613 ipv4AddrLocal.s_addr = 0x0100007f;
618 mapLocalHost[addr] = lsi;
622 in_addr ipv4AddrPeer;
623 ipv4AddrPeer.s_addr = 0xa0b0c001;
625 std::unique_ptr<CNode> pnode = std::make_unique<CNode>(0,
635 pnode->fSuccessfullyConnected.store(
true);
638 in6_addr ipv6AddrLocal;
639 memset(ipv6AddrLocal.s6_addr, 0, 16);
640 ipv6AddrLocal.s6_addr[0] = 0xcc;
642 pnode->SetAddrLocal(addrLocal);
694 unsigned char ip[] = {p1, p2, p3, p4};
696 struct sockaddr_in sa;
697 memset(&sa, 0,
sizeof(sockaddr_in));
698 memcpy(&(sa.sin_addr), &
ip,
sizeof(
ip));