5 #include <chainparams.h>
26 #include <boost/test/unit_test.hpp>
34 using namespace std::literals;
44 uint16_t altPort = 12345;
55 ipv4Addr.s_addr = 0xa0b0c001;
60 std::unique_ptr<CNode> pnode1 = std::make_unique<CNode>(
id++,
78 std::unique_ptr<CNode> pnode2 = std::make_unique<CNode>(
id++,
96 std::unique_ptr<CNode> pnode3 = std::make_unique<CNode>(
id++,
114 std::unique_ptr<CNode> pnode4 = std::make_unique<CNode>(
id++,
123 BOOST_CHECK(pnode4->IsFullOutboundConn() ==
false);
138 BOOST_REQUIRE(
LookupHost(
"0.0.0.0", addr,
false));
139 BOOST_REQUIRE(!addr.
IsValid());
140 BOOST_REQUIRE(addr.
IsIPv4());
147 BOOST_REQUIRE(
LookupHost(
"255.255.255.255", addr,
false));
148 BOOST_REQUIRE(!addr.
IsValid());
149 BOOST_REQUIRE(addr.
IsIPv4());
156 BOOST_REQUIRE(
LookupHost(
"12.34.56.78", addr,
false));
158 BOOST_REQUIRE(addr.
IsIPv4());
166 BOOST_REQUIRE(!addr.
IsValid());
167 BOOST_REQUIRE(addr.
IsIPv6());
174 BOOST_REQUIRE(
LookupHost(
"1122:3344:5566:7788:9900:aabb:ccdd:eeff", addr,
false));
176 BOOST_REQUIRE(addr.
IsIPv6());
187 const std::string link_local{
"fe80::1"};
188 const std::string scoped_addr{link_local +
"%32"};
189 BOOST_REQUIRE(
LookupHost(scoped_addr, addr,
false));
191 BOOST_REQUIRE(addr.
IsIPv6());
196 BOOST_REQUIRE(
LookupHost(link_local +
"%0", addr,
false));
198 BOOST_REQUIRE(addr.
IsIPv6());
206 const char* torv3_addr =
"pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion";
209 BOOST_REQUIRE(addr.
IsTor());
224 "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd\0wtf.onion", 66}));
233 const char* i2p_addr =
"UDHDrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v4jna.b32.I2P";
236 BOOST_REQUIRE(addr.
IsI2P());
256 !addr.
SetSpecial(
"pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscsad.b32.i2p"));
263 BOOST_REQUIRE(!addr.
IsValid());
278 const std::map<std::string, std::string> canonical_representations_ipv6{
279 {
"0000:0000:0000:0000:0000:0000:0000:0000",
"::"},
280 {
"000:0000:000:00:0:00:000:0000",
"::"},
281 {
"000:000:000:000:000:000:000:000",
"::"},
282 {
"00:00:00:00:00:00:00:00",
"::"},
283 {
"0:0:0:0:0:0:0:0",
"::"},
284 {
"0:0:0:0:0:0:0:1",
"::1"},
285 {
"2001:0:0:1:0:0:0:1",
"2001:0:0:1::1"},
286 {
"2001:0db8:0:0:1:0:0:1",
"2001:db8::1:0:0:1"},
287 {
"2001:0db8:85a3:0000:0000:8a2e:0370:7334",
"2001:db8:85a3::8a2e:370:7334"},
288 {
"2001:0db8::0001",
"2001:db8::1"},
289 {
"2001:0db8::0001:0000",
"2001:db8::1:0"},
290 {
"2001:0db8::1:0:0:1",
"2001:db8::1:0:0:1"},
291 {
"2001:db8:0000:0:1::1",
"2001:db8::1:0:0:1"},
292 {
"2001:db8:0000:1:1:1:1:1",
"2001:db8:0:1:1:1:1:1"},
293 {
"2001:db8:0:0:0:0:2:1",
"2001:db8::2:1"},
294 {
"2001:db8:0:0:0::1",
"2001:db8::1"},
295 {
"2001:db8:0:0:1:0:0:1",
"2001:db8::1:0:0:1"},
296 {
"2001:db8:0:0:1::1",
"2001:db8::1:0:0:1"},
297 {
"2001:DB8:0:0:1::1",
"2001:db8::1:0:0:1"},
298 {
"2001:db8:0:0::1",
"2001:db8::1"},
299 {
"2001:db8:0:0:aaaa::1",
"2001:db8::aaaa:0:0:1"},
300 {
"2001:db8:0:1:1:1:1:1",
"2001:db8:0:1:1:1:1:1"},
301 {
"2001:db8:0::1",
"2001:db8::1"},
302 {
"2001:db8:85a3:0:0:8a2e:370:7334",
"2001:db8:85a3::8a2e:370:7334"},
303 {
"2001:db8::0:1",
"2001:db8::1"},
304 {
"2001:db8::0:1:0:0:1",
"2001:db8::1:0:0:1"},
305 {
"2001:DB8::1",
"2001:db8::1"},
306 {
"2001:db8::1",
"2001:db8::1"},
307 {
"2001:db8::1:0:0:1",
"2001:db8::1:0:0:1"},
308 {
"2001:db8::1:1:1:1:1",
"2001:db8:0:1:1:1:1:1"},
309 {
"2001:db8::aaaa:0:0:1",
"2001:db8::aaaa:0:0:1"},
310 {
"2001:db8:aaaa:bbbb:cccc:dddd:0:1",
"2001:db8:aaaa:bbbb:cccc:dddd:0:1"},
311 {
"2001:db8:aaaa:bbbb:cccc:dddd::1",
"2001:db8:aaaa:bbbb:cccc:dddd:0:1"},
312 {
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:0001",
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:1"},
313 {
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:001",
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:1"},
314 {
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:01",
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:1"},
315 {
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:1",
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:1"},
316 {
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:aaaa",
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:aaaa"},
317 {
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:AAAA",
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:aaaa"},
318 {
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:AaAa",
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:aaaa"},
320 for (
const auto& [input_address, expected_canonical_representation_output] : canonical_representations_ipv6) {
322 BOOST_REQUIRE(
LookupHost(input_address, net_addr,
false));
323 BOOST_REQUIRE(net_addr.
IsIPv6());
337 BOOST_REQUIRE(
LookupHost(
"1.2.3.4", addr,
false));
342 BOOST_REQUIRE(
LookupHost(
"1a1b:2a2b:3a3b:4a4b:5a5b:6a6b:7a7b:8a8b", addr,
false));
350 BOOST_REQUIRE(addr.
SetSpecial(
"pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion"));
373 BOOST_REQUIRE(
LookupHost(
"1.2.3.4", addr,
false));
378 BOOST_REQUIRE(
LookupHost(
"1a1b:2a2b:3a3b:4a4b:5a5b:6a6b:7a7b:8a8b", addr,
false));
386 BOOST_REQUIRE(addr.
SetSpecial(
"kpgvmscirrdqpekbqjsvw5teanhatztpp2gl6eee4zkowvwfxwenqaid.onion"));
414 BOOST_REQUIRE(s.
empty());
420 BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure,
HasReason(
"end of data"));
421 BOOST_REQUIRE(!s.
empty());
428 BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure,
429 HasReason(
"BIP155 IPv4 address with length 5 (should be 4)"));
430 BOOST_REQUIRE(!s.
empty());
437 BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure,
438 HasReason(
"Address too long: 513 > 512"));
439 BOOST_REQUIRE(!s.
empty());
445 "0102030405060708090a0b0c0d0e0f10")};
451 BOOST_REQUIRE(s.
empty());
457 "fd6b88c08724ca978112ca1bbdcafac2")};
463 BOOST_REQUIRE(s.
empty());
469 BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure,
470 HasReason(
"BIP155 IPv6 address with length 4 (should be 16)"));
471 BOOST_REQUIRE(!s.
empty());
477 "00000000000000000000ffff01020304")};
480 BOOST_REQUIRE(s.
empty());
485 "fd87d87eeb430102030405060708090a")};
488 BOOST_REQUIRE(s.
empty());
493 "f1f2f3f4f5f6f7f8f9fa")};
496 BOOST_REQUIRE(s.
empty());
501 "79bcc625184b05194975c28b66b66b04"
502 "69f7f6556fb1ac3189a79b40dda32f1f"
509 "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion");
510 BOOST_REQUIRE(s.
empty());
517 BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure,
518 HasReason(
"BIP155 TORv3 address with length 0 (should be 32)"));
519 BOOST_REQUIRE(!s.
empty());
525 "a2894dabaec08c0051a481a6dac88b64"
526 "f98232ae42d4b6fd2fa81952dfe36a87")};
532 "ukeu3k5oycgaauneqgtnvselmt4yemvoilkln7jpvamvfx7dnkdq.b32.i2p");
533 BOOST_REQUIRE(s.
empty());
540 BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure,
541 HasReason(
"BIP155 I2P address with length 3 (should be 32)"));
542 BOOST_REQUIRE(!s.
empty());
548 "fc000001000200030004000500060007"
555 BOOST_REQUIRE(s.
empty());
560 "aa000001000200030004000500060007"
565 BOOST_REQUIRE(s.
empty());
572 BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure,
573 HasReason(
"BIP155 CJDNS address with length 1 (should be 16)"));
574 BOOST_REQUIRE(!s.
empty());
582 BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure,
583 HasReason(
"Address too long: 33554432 > 512"));
584 BOOST_REQUIRE(!s.
empty());
594 BOOST_REQUIRE(s.
empty());
603 BOOST_REQUIRE(s.
empty());
613 raw_addr.s_addr = htonl(0x7f000001);
620 mapLocalHost[mapLocalHost_entry] = lsi;
624 in_addr ipv4AddrPeer;
625 ipv4AddrPeer.s_addr = 0xa0b0c001;
627 std::unique_ptr<CNode> pnode = std::make_unique<CNode>(0,
636 pnode->fSuccessfullyConnected.store(
true);
639 in6_addr ipv6AddrLocal;
640 memset(ipv6AddrLocal.s6_addr, 0, 16);
641 ipv6AddrLocal.s6_addr[0] = 0xcc;
643 pnode->SetAddrLocal(addrLocal);
654 mapLocalHost.erase(mapLocalHost_entry);
670 const uint16_t bind_port = 20001;
674 in_addr peer_us_addr;
675 peer_us_addr.s_addr = htonl(0x02030405);
676 const CService peer_us{peer_us_addr, 20002};
679 in_addr peer_out_in_addr;
680 peer_out_in_addr.s_addr = htonl(0x01020304);
690 peer_out.fSuccessfullyConnected =
true;
691 peer_out.SetAddrLocal(peer_us);
695 BOOST_REQUIRE(chosen_local_addr);
696 const CService expected{peer_us_addr, bind_port};
700 in_addr peer_in_in_addr;
701 peer_in_in_addr.s_addr = htonl(0x05060708);
711 peer_in.fSuccessfullyConnected =
true;
712 peer_in.SetAddrLocal(peer_us);
716 BOOST_REQUIRE(chosen_local_addr);
769 unsigned char ip[] = {p1, p2, p3, p4};
771 struct sockaddr_in sa;
772 memset(&sa, 0,
sizeof(sockaddr_in));
773 memcpy(&(sa.sin_addr), &
ip,
sizeof(
ip));
819 const uint16_t bind_port = 20001;
824 in_addr peer_us_addr;
825 peer_us_addr.s_addr = htonl(0x02030405);
826 const CService peer_us{peer_us_addr, 20002};
829 in_addr peer_in_addr;
830 peer_in_addr.s_addr = htonl(0x01020304);
842 const int64_t time{0};
853 std::atomic<bool> interrupt_dummy{
false};
854 std::chrono::microseconds time_received_dummy{0};
856 const auto msg_version =
871 const CService expected{peer_us_addr, bind_port};
876 const std::string& msg_type,
878 bool is_incoming) ->
void {
879 if (!is_incoming && msg_type ==
"addr") {
881 std::vector<CAddress> addresses;
885 for (
const auto& addr : addresses) {
886 if (addr == expected) {
const CChainParams & Params()
Return the currently selected parameters.
void ForceSetArg(const std::string &strArg, const std::string &strValue)
bool SoftSetArg(const std::string &strArg, const std::string &strValue)
Set an argument if it doesn't already have a value.
A CService with information about it as peer.
std::string ToStringAddr() const
bool SetSpecial(const std::string &addr)
Parse a Tor or I2P address and set this object to it.
bool IsCJDNS() const
Check whether this object represents a CJDNS address.
bool IsTor() const
Check whether this object represents a TOR address.
bool SetInternal(const std::string &name)
Create an "internal" address that represents a name or FQDN.
bool IsAddrV1Compatible() const
Check if the current object can be serialized in pre-ADDRv2/BIP155 format.
bool IsI2P() const
Check whether this object represents an I2P address.
Information about a peer.
A combination of a network address (CNetAddr) and a (TCP) port.
BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
static Mutex g_msgproc_mutex
Mutex for anything that is only accessed via the msg processing thread.
A Span is an object that can refer to a contiguous sequence of objects.
@ 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.
BOOST_AUTO_TEST_SUITE_END()
static CService ip(uint32_t i)
const char * VERSION
The version message provides information about the transmitting node to the receiving node at the beg...
const char * VERACK
The verack message acknowledges a previously-received version message, informing the connecting node ...
bool IsLocal(const CService &addr)
check whether a given address is potentially local
void RemoveLocal(const CService &addr)
std::optional< CService > GetLocalAddrForPeer(CNode &node)
Returns a local address that we should advertise to this peer.
bool AddLocal(const CService &addr_, int nScore)
void SetReachable(enum Network net, bool reachable)
Mark a network as reachable or unreachable (no automatic connects to it)
GlobalMutex g_maplocalhost_mutex
std::function< void(const CAddress &addr, const std::string &msg_type, Span< const unsigned char > data, bool is_incoming)> CaptureMessage
Defaults to CaptureMessageToFile(), but can be overridden by unit tests.
bool IsReachable(enum Network net)
BOOST_AUTO_TEST_CASE(cnode_listen_port)
CNetAddr UtilBuildAddress(unsigned char p1, unsigned char p2, unsigned char p3, unsigned char p4)
static constexpr int ADDRV2_FORMAT
A flag that is ORed into the protocol version to designate that addresses should be serialized in (un...
@ NET_ONION
TOR (v2 or v3)
@ NET_UNROUTABLE
Addresses from these networks are not publicly routable on the global Internet.
@ NET_INTERNAL
A set of addresses that represent the hash of a string or FQDN.
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.
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
std::vector< Byte > ParseHex(std::string_view hex_str)
Like TryParseHex, but returns an empty vector on invalid input.
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Identical to TestingSetup, but chain set to regtest.
void ResetIbd()
Reset the ibd cache to its initial state.
void JumpOutOfIbd()
Toggle IsInitialBlockDownload from true to false.
std::unique_ptr< ChainstateManager > chainman
std::unique_ptr< PeerManager > peerman
void TestOnlyResetTimeData()
Reset the internal state of GetTimeOffset(), GetAdjustedTime() and AddTimeData().
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
std::string ToLower(std::string_view str)
Returns the lowercase equivalent of the given string.
static const int PROTOCOL_VERSION
network protocol versioning