20#include <boost/test/unit_test.hpp>
28using namespace std::literals;
38 uint16_t altPort = 12345;
50 ipv4Addr.s_addr = 0xa0b0c001;
55 std::unique_ptr<CNode> pnode1 = std::make_unique<CNode>(
70 std::unique_ptr<CNode> pnode2 = std::make_unique<CNode>(
85 std::unique_ptr<CNode> pnode3 = std::make_unique<CNode>(
100 std::unique_ptr<CNode> pnode4 = std::make_unique<CNode>(
106 BOOST_CHECK(pnode4->IsFullOutboundConn() ==
false);
121 BOOST_REQUIRE(
LookupHost(
"0.0.0.0", addr,
false));
122 BOOST_REQUIRE(!addr.
IsValid());
123 BOOST_REQUIRE(addr.
IsIPv4());
130 BOOST_REQUIRE(
LookupHost(
"255.255.255.255", addr,
false));
131 BOOST_REQUIRE(!addr.
IsValid());
132 BOOST_REQUIRE(addr.
IsIPv4());
139 BOOST_REQUIRE(
LookupHost(
"12.34.56.78", addr,
false));
141 BOOST_REQUIRE(addr.
IsIPv4());
149 BOOST_REQUIRE(!addr.
IsValid());
150 BOOST_REQUIRE(addr.
IsIPv6());
157 BOOST_REQUIRE(
LookupHost(
"1122:3344:5566:7788:9900:aabb:ccdd:eeff", addr,
false));
159 BOOST_REQUIRE(addr.
IsIPv6());
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));
174 BOOST_REQUIRE(addr.
IsIPv6());
179 BOOST_REQUIRE(
LookupHost(link_local +
"%0", addr,
false));
181 BOOST_REQUIRE(addr.
IsIPv6());
189 const char* torv3_addr =
"pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion";
192 BOOST_REQUIRE(addr.
IsTor());
207 "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd\0wtf.onion", 66}));
216 const char* i2p_addr =
"UDHDrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v4jna.b32.I2P";
219 BOOST_REQUIRE(addr.
IsI2P());
239 !addr.
SetSpecial(
"pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscsad.b32.i2p"));
246 BOOST_REQUIRE(!addr.
IsValid());
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"},
303 for (
const auto& [input_address, expected_canonical_representation_output] : canonical_representations_ipv6) {
305 BOOST_REQUIRE(
LookupHost(input_address, net_addr,
false));
306 BOOST_REQUIRE(net_addr.
IsIPv6());
320 BOOST_REQUIRE(
LookupHost(
"1.2.3.4", addr,
false));
325 BOOST_REQUIRE(
LookupHost(
"1a1b:2a2b:3a3b:4a4b:5a5b:6a6b:7a7b:8a8b", addr,
false));
333 BOOST_REQUIRE(addr.
SetSpecial(
"pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion"));
356 BOOST_REQUIRE(
LookupHost(
"1.2.3.4", addr,
false));
361 BOOST_REQUIRE(
LookupHost(
"1a1b:2a2b:3a3b:4a4b:5a5b:6a6b:7a7b:8a8b", addr,
false));
369 BOOST_REQUIRE(addr.
SetSpecial(
"kpgvmscirrdqpekbqjsvw5teanhatztpp2gl6eee4zkowvwfxwenqaid.onion"));
397 BOOST_REQUIRE(s.
empty());
403 BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure,
HasReason(
"end of data"));
404 BOOST_REQUIRE(!s.
empty());
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());
420 BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure,
421 HasReason(
"Address too long: 513 > 512"));
422 BOOST_REQUIRE(!s.
empty());
428 "0102030405060708090a0b0c0d0e0f10"));
434 BOOST_REQUIRE(s.
empty());
440 "fd6b88c08724ca978112ca1bbdcafac2"));
446 BOOST_REQUIRE(s.
empty());
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());
460 "00000000000000000000ffff01020304"));
463 BOOST_REQUIRE(s.
empty());
468 "fd87d87eeb430102030405060708090a"));
471 BOOST_REQUIRE(s.
empty());
476 "f1f2f3f4f5f6f7f8f9fa"));
479 BOOST_REQUIRE(s.
empty());
484 "79bcc625184b05194975c28b66b66b04"
485 "69f7f6556fb1ac3189a79b40dda32f1f"
492 "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion");
493 BOOST_REQUIRE(s.
empty());
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());
508 "a2894dabaec08c0051a481a6dac88b64"
509 "f98232ae42d4b6fd2fa81952dfe36a87"));
515 "ukeu3k5oycgaauneqgtnvselmt4yemvoilkln7jpvamvfx7dnkdq.b32.i2p");
516 BOOST_REQUIRE(s.
empty());
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());
531 "fc000001000200030004000500060007"
538 BOOST_REQUIRE(s.
empty());
543 "aa000001000200030004000500060007"
548 BOOST_REQUIRE(s.
empty());
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());
565 BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure,
566 HasReason(
"Address too long: 33554432 > 512"));
567 BOOST_REQUIRE(!s.
empty());
577 BOOST_REQUIRE(s.
empty());
586 BOOST_REQUIRE(s.
empty());
597 in_addr ipv4AddrLocal;
598 ipv4AddrLocal.s_addr = 0x0100007f;
603 mapLocalHost[addr] = lsi;
607 in_addr ipv4AddrPeer;
608 ipv4AddrPeer.s_addr = 0xa0b0c001;
611 pnode->fSuccessfullyConnected.store(
true);
614 in6_addr ipv6AddrLocal;
615 memset(ipv6AddrLocal.s6_addr, 0, 16);
616 ipv6AddrLocal.s6_addr[0] = 0xcc;
618 pnode->SetAddrLocal(addrLocal);
670 unsigned char ip[] = {p1, p2, p3, p4};
672 struct sockaddr_in sa;
673 memset(&sa, 0,
sizeof(sockaddr_in));
674 memcpy(&(sa.sin_addr), &
ip,
sizeof(
ip));
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.
A CService with information about it as peer.
Double ended buffer combining vector and stream-like interfaces.
bool SetSpecial(const std::string &addr)
Parse a Tor or I2P address and set this object to it.
std::string ToString() const
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.
A combination of a network address (CNetAddr) and a (TCP) port.
BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
BOOST_AUTO_TEST_SUITE_END()
static CService ip(uint32_t i)
bool IsLocal(const CService &addr)
check whether a given address is potentially local
void RemoveLocal(const CService &addr)
bool AddLocal(const CService &addr_, int nScore)
RecursiveMutex cs_mapLocalHost
std::optional< CAddress > GetLocalAddrForPeer(CNode *pnode)
Returns a local address that we should advertise to this peer.
void SetReachable(enum Network net, bool reachable)
Mark a network as reachable or unreachable (no automatic connects to it)
bool IsReachable(enum Network net)
@ 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_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_FIXTURE_TEST_SUITE(a, b)
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
constexpr Span< A > MakeSpan(A(&a)[N])
MakeSpan for arrays:
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.
static const int PROTOCOL_VERSION
network protocol versioning