2020-04-16 13:14:08 -04:00
|
|
|
// Copyright (c) 2012-2020 The Bitcoin Core developers
|
2016-03-16 12:54:30 -04:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2019-06-24 17:22:28 +02:00
|
|
|
|
2020-04-16 13:11:54 -04:00
|
|
|
#include <chainparams.h>
|
|
|
|
#include <clientversion.h>
|
2019-12-29 13:04:02 -08:00
|
|
|
#include <cstdint>
|
2017-11-10 13:57:53 +13:00
|
|
|
#include <net.h>
|
2021-03-06 20:14:08 +01:00
|
|
|
#include <netaddress.h>
|
2017-11-10 13:57:53 +13:00
|
|
|
#include <netbase.h>
|
2020-04-16 13:11:54 -04:00
|
|
|
#include <serialize.h>
|
2020-05-19 15:01:50 +02:00
|
|
|
#include <span.h>
|
2020-04-16 13:11:54 -04:00
|
|
|
#include <streams.h>
|
|
|
|
#include <test/util/setup_common.h>
|
2020-08-24 21:34:26 +02:00
|
|
|
#include <util/strencodings.h>
|
2020-02-12 23:01:45 -05:00
|
|
|
#include <util/string.h>
|
2020-04-16 13:11:54 -04:00
|
|
|
#include <util/system.h>
|
2020-08-24 21:34:26 +02:00
|
|
|
#include <version.h>
|
2016-03-16 12:54:30 -04:00
|
|
|
|
2020-04-17 06:41:14 -04:00
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
2020-11-06 11:40:09 +00:00
|
|
|
#include <algorithm>
|
2020-05-19 15:01:50 +02:00
|
|
|
#include <ios>
|
2018-04-02 18:31:40 +00:00
|
|
|
#include <memory>
|
2021-03-15 11:59:05 +08:00
|
|
|
#include <optional>
|
2020-04-17 06:41:14 -04:00
|
|
|
#include <string>
|
2018-04-02 18:31:40 +00:00
|
|
|
|
2020-09-23 08:40:38 +02:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2016-03-16 12:54:30 -04:00
|
|
|
BOOST_FIXTURE_TEST_SUITE(net_tests, BasicTestingSetup)
|
|
|
|
|
2017-04-25 07:26:43 -07:00
|
|
|
BOOST_AUTO_TEST_CASE(cnode_listen_port)
|
|
|
|
{
|
|
|
|
// test default
|
2021-03-01 21:35:28 +01:00
|
|
|
uint16_t port{GetListenPort()};
|
2017-04-25 07:26:43 -07:00
|
|
|
BOOST_CHECK(port == Params().GetDefaultPort());
|
|
|
|
// test set port
|
2019-12-29 13:04:02 -08:00
|
|
|
uint16_t altPort = 12345;
|
2020-02-12 23:01:45 -05:00
|
|
|
BOOST_CHECK(gArgs.SoftSetArg("-port", ToString(altPort)));
|
2017-04-25 07:26:43 -07:00
|
|
|
port = GetListenPort();
|
|
|
|
BOOST_CHECK(port == altPort);
|
|
|
|
}
|
|
|
|
|
2016-06-17 00:10:07 -04:00
|
|
|
BOOST_AUTO_TEST_CASE(cnode_simple_test)
|
|
|
|
{
|
|
|
|
SOCKET hSocket = INVALID_SOCKET;
|
2016-04-17 20:20:34 -04:00
|
|
|
NodeId id = 0;
|
2016-06-17 00:10:07 -04:00
|
|
|
|
|
|
|
in_addr ipv4Addr;
|
|
|
|
ipv4Addr.s_addr = 0xa0b0c001;
|
2018-07-24 16:59:49 +01:00
|
|
|
|
2016-06-17 00:10:07 -04:00
|
|
|
CAddress addr = CAddress(CService(ipv4Addr, 7777), NODE_NETWORK);
|
2018-01-14 18:15:31 +01:00
|
|
|
std::string pszDest;
|
2016-06-17 00:10:07 -04:00
|
|
|
|
2021-03-10 17:28:08 +08:00
|
|
|
std::unique_ptr<CNode> pnode1 = std::make_unique<CNode>(
|
2021-01-02 10:25:05 +01:00
|
|
|
id++, NODE_NETWORK, hSocket, addr,
|
2020-10-03 12:18:36 +03:00
|
|
|
/* nKeyedNetGroupIn = */ 0,
|
|
|
|
/* nLocalHostNonceIn = */ 0,
|
2021-02-12 21:40:19 +01:00
|
|
|
CAddress(), pszDest, ConnectionType::OUTBOUND_FULL_RELAY,
|
|
|
|
/* inbound_onion = */ false);
|
2020-08-11 10:37:57 -07:00
|
|
|
BOOST_CHECK(pnode1->IsFullOutboundConn() == true);
|
|
|
|
BOOST_CHECK(pnode1->IsManualConn() == false);
|
|
|
|
BOOST_CHECK(pnode1->IsBlockOnlyConn() == false);
|
|
|
|
BOOST_CHECK(pnode1->IsFeelerConn() == false);
|
|
|
|
BOOST_CHECK(pnode1->IsAddrFetchConn() == false);
|
2020-07-28 13:39:38 -07:00
|
|
|
BOOST_CHECK(pnode1->IsInboundConn() == false);
|
2021-01-26 22:08:28 +01:00
|
|
|
BOOST_CHECK(pnode1->m_inbound_onion == false);
|
2020-10-03 12:18:36 +03:00
|
|
|
BOOST_CHECK_EQUAL(pnode1->ConnectedThroughNetwork(), Network::NET_IPV4);
|
|
|
|
|
2021-03-10 17:28:08 +08:00
|
|
|
std::unique_ptr<CNode> pnode2 = std::make_unique<CNode>(
|
2021-01-02 10:25:05 +01:00
|
|
|
id++, NODE_NETWORK, hSocket, addr,
|
2020-10-03 12:18:36 +03:00
|
|
|
/* nKeyedNetGroupIn = */ 1,
|
|
|
|
/* nLocalHostNonceIn = */ 1,
|
|
|
|
CAddress(), pszDest, ConnectionType::INBOUND,
|
|
|
|
/* inbound_onion = */ false);
|
2020-08-11 10:37:57 -07:00
|
|
|
BOOST_CHECK(pnode2->IsFullOutboundConn() == false);
|
|
|
|
BOOST_CHECK(pnode2->IsManualConn() == false);
|
|
|
|
BOOST_CHECK(pnode2->IsBlockOnlyConn() == false);
|
|
|
|
BOOST_CHECK(pnode2->IsFeelerConn() == false);
|
|
|
|
BOOST_CHECK(pnode2->IsAddrFetchConn() == false);
|
2020-07-28 13:39:38 -07:00
|
|
|
BOOST_CHECK(pnode2->IsInboundConn() == true);
|
2021-01-26 22:08:28 +01:00
|
|
|
BOOST_CHECK(pnode2->m_inbound_onion == false);
|
2020-10-03 12:18:36 +03:00
|
|
|
BOOST_CHECK_EQUAL(pnode2->ConnectedThroughNetwork(), Network::NET_IPV4);
|
|
|
|
|
2021-03-10 17:28:08 +08:00
|
|
|
std::unique_ptr<CNode> pnode3 = std::make_unique<CNode>(
|
2021-01-02 10:25:05 +01:00
|
|
|
id++, NODE_NETWORK, hSocket, addr,
|
2020-10-03 12:18:36 +03:00
|
|
|
/* nKeyedNetGroupIn = */ 0,
|
|
|
|
/* nLocalHostNonceIn = */ 0,
|
|
|
|
CAddress(), pszDest, ConnectionType::OUTBOUND_FULL_RELAY,
|
2020-12-17 16:33:56 +01:00
|
|
|
/* inbound_onion = */ false);
|
2020-10-03 12:18:36 +03:00
|
|
|
BOOST_CHECK(pnode3->IsFullOutboundConn() == true);
|
|
|
|
BOOST_CHECK(pnode3->IsManualConn() == false);
|
|
|
|
BOOST_CHECK(pnode3->IsBlockOnlyConn() == false);
|
|
|
|
BOOST_CHECK(pnode3->IsFeelerConn() == false);
|
|
|
|
BOOST_CHECK(pnode3->IsAddrFetchConn() == false);
|
|
|
|
BOOST_CHECK(pnode3->IsInboundConn() == false);
|
2021-01-26 22:08:28 +01:00
|
|
|
BOOST_CHECK(pnode3->m_inbound_onion == false);
|
2020-10-03 12:18:36 +03:00
|
|
|
BOOST_CHECK_EQUAL(pnode3->ConnectedThroughNetwork(), Network::NET_IPV4);
|
|
|
|
|
2021-03-10 17:28:08 +08:00
|
|
|
std::unique_ptr<CNode> pnode4 = std::make_unique<CNode>(
|
2021-01-02 10:25:05 +01:00
|
|
|
id++, NODE_NETWORK, hSocket, addr,
|
2020-10-03 12:18:36 +03:00
|
|
|
/* nKeyedNetGroupIn = */ 1,
|
|
|
|
/* nLocalHostNonceIn = */ 1,
|
|
|
|
CAddress(), pszDest, ConnectionType::INBOUND,
|
|
|
|
/* inbound_onion = */ true);
|
|
|
|
BOOST_CHECK(pnode4->IsFullOutboundConn() == false);
|
|
|
|
BOOST_CHECK(pnode4->IsManualConn() == false);
|
|
|
|
BOOST_CHECK(pnode4->IsBlockOnlyConn() == false);
|
|
|
|
BOOST_CHECK(pnode4->IsFeelerConn() == false);
|
|
|
|
BOOST_CHECK(pnode4->IsAddrFetchConn() == false);
|
|
|
|
BOOST_CHECK(pnode4->IsInboundConn() == true);
|
2021-01-26 22:08:28 +01:00
|
|
|
BOOST_CHECK(pnode4->m_inbound_onion == true);
|
2020-10-03 12:18:36 +03:00
|
|
|
BOOST_CHECK_EQUAL(pnode4->ConnectedThroughNetwork(), Network::NET_ONION);
|
2016-06-17 00:10:07 -04:00
|
|
|
}
|
|
|
|
|
2020-08-24 21:34:26 +02:00
|
|
|
BOOST_AUTO_TEST_CASE(cnetaddr_basic)
|
|
|
|
{
|
|
|
|
CNetAddr addr;
|
|
|
|
|
|
|
|
// IPv4, INADDR_ANY
|
|
|
|
BOOST_REQUIRE(LookupHost("0.0.0.0", addr, false));
|
|
|
|
BOOST_REQUIRE(!addr.IsValid());
|
|
|
|
BOOST_REQUIRE(addr.IsIPv4());
|
|
|
|
|
|
|
|
BOOST_CHECK(addr.IsBindAny());
|
2020-05-20 12:05:18 +02:00
|
|
|
BOOST_CHECK(addr.IsAddrV1Compatible());
|
2020-08-24 21:34:26 +02:00
|
|
|
BOOST_CHECK_EQUAL(addr.ToString(), "0.0.0.0");
|
|
|
|
|
|
|
|
// IPv4, INADDR_NONE
|
|
|
|
BOOST_REQUIRE(LookupHost("255.255.255.255", addr, false));
|
|
|
|
BOOST_REQUIRE(!addr.IsValid());
|
|
|
|
BOOST_REQUIRE(addr.IsIPv4());
|
|
|
|
|
|
|
|
BOOST_CHECK(!addr.IsBindAny());
|
2020-05-20 12:05:18 +02:00
|
|
|
BOOST_CHECK(addr.IsAddrV1Compatible());
|
2020-08-24 21:34:26 +02:00
|
|
|
BOOST_CHECK_EQUAL(addr.ToString(), "255.255.255.255");
|
|
|
|
|
|
|
|
// IPv4, casual
|
|
|
|
BOOST_REQUIRE(LookupHost("12.34.56.78", addr, false));
|
|
|
|
BOOST_REQUIRE(addr.IsValid());
|
|
|
|
BOOST_REQUIRE(addr.IsIPv4());
|
|
|
|
|
|
|
|
BOOST_CHECK(!addr.IsBindAny());
|
2020-05-20 12:05:18 +02:00
|
|
|
BOOST_CHECK(addr.IsAddrV1Compatible());
|
2020-08-24 21:34:26 +02:00
|
|
|
BOOST_CHECK_EQUAL(addr.ToString(), "12.34.56.78");
|
|
|
|
|
|
|
|
// IPv6, in6addr_any
|
|
|
|
BOOST_REQUIRE(LookupHost("::", addr, false));
|
|
|
|
BOOST_REQUIRE(!addr.IsValid());
|
|
|
|
BOOST_REQUIRE(addr.IsIPv6());
|
|
|
|
|
|
|
|
BOOST_CHECK(addr.IsBindAny());
|
2020-05-20 12:05:18 +02:00
|
|
|
BOOST_CHECK(addr.IsAddrV1Compatible());
|
2020-08-24 21:34:26 +02:00
|
|
|
BOOST_CHECK_EQUAL(addr.ToString(), "::");
|
|
|
|
|
|
|
|
// IPv6, casual
|
|
|
|
BOOST_REQUIRE(LookupHost("1122:3344:5566:7788:9900:aabb:ccdd:eeff", addr, false));
|
|
|
|
BOOST_REQUIRE(addr.IsValid());
|
|
|
|
BOOST_REQUIRE(addr.IsIPv6());
|
|
|
|
|
|
|
|
BOOST_CHECK(!addr.IsBindAny());
|
2020-05-20 12:05:18 +02:00
|
|
|
BOOST_CHECK(addr.IsAddrV1Compatible());
|
2020-08-24 21:34:26 +02:00
|
|
|
BOOST_CHECK_EQUAL(addr.ToString(), "1122:3344:5566:7788:9900:aabb:ccdd:eeff");
|
|
|
|
|
2020-09-13 11:04:50 +02:00
|
|
|
// IPv6, scoped/link-local. See https://tools.ietf.org/html/rfc4007
|
|
|
|
// We support non-negative decimal integers (uint32_t) as zone id indices.
|
2021-05-17 17:01:44 +02:00
|
|
|
// Normal link-local scoped address functionality is to append "%" plus the
|
|
|
|
// zone id, for example, given a link-local address of "fe80::1" and a zone
|
|
|
|
// id of "32", return the address as "fe80::1%32".
|
2020-09-13 11:04:50 +02:00
|
|
|
const std::string link_local{"fe80::1"};
|
|
|
|
const std::string scoped_addr{link_local + "%32"};
|
|
|
|
BOOST_REQUIRE(LookupHost(scoped_addr, addr, false));
|
|
|
|
BOOST_REQUIRE(addr.IsValid());
|
|
|
|
BOOST_REQUIRE(addr.IsIPv6());
|
|
|
|
BOOST_CHECK(!addr.IsBindAny());
|
2021-05-17 17:01:44 +02:00
|
|
|
BOOST_CHECK_EQUAL(addr.ToString(), scoped_addr);
|
|
|
|
|
2020-09-13 11:04:50 +02:00
|
|
|
// Test that the delimiter "%" and default zone id of 0 can be omitted for the default scope.
|
|
|
|
BOOST_REQUIRE(LookupHost(link_local + "%0", addr, false));
|
|
|
|
BOOST_REQUIRE(addr.IsValid());
|
|
|
|
BOOST_REQUIRE(addr.IsIPv6());
|
|
|
|
BOOST_CHECK(!addr.IsBindAny());
|
|
|
|
BOOST_CHECK_EQUAL(addr.ToString(), link_local);
|
|
|
|
|
2021-05-24 18:00:08 +02:00
|
|
|
// TORv2, no longer supported
|
|
|
|
BOOST_CHECK(!addr.SetSpecial("6hzph5hv6337r6p2.onion"));
|
2020-08-24 21:34:26 +02:00
|
|
|
|
2020-08-27 11:03:21 +02:00
|
|
|
// TORv3
|
|
|
|
const char* torv3_addr = "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion";
|
|
|
|
BOOST_REQUIRE(addr.SetSpecial(torv3_addr));
|
|
|
|
BOOST_REQUIRE(addr.IsValid());
|
|
|
|
BOOST_REQUIRE(addr.IsTor());
|
|
|
|
|
2020-10-21 11:35:19 +02:00
|
|
|
BOOST_CHECK(!addr.IsI2P());
|
2020-08-27 11:03:21 +02:00
|
|
|
BOOST_CHECK(!addr.IsBindAny());
|
2020-05-20 12:05:18 +02:00
|
|
|
BOOST_CHECK(!addr.IsAddrV1Compatible());
|
2020-08-27 11:03:21 +02:00
|
|
|
BOOST_CHECK_EQUAL(addr.ToString(), torv3_addr);
|
|
|
|
|
|
|
|
// TORv3, broken, with wrong checksum
|
|
|
|
BOOST_CHECK(!addr.SetSpecial("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscsad.onion"));
|
|
|
|
|
|
|
|
// TORv3, broken, with wrong version
|
|
|
|
BOOST_CHECK(!addr.SetSpecial("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscrye.onion"));
|
|
|
|
|
|
|
|
// TORv3, malicious
|
|
|
|
BOOST_CHECK(!addr.SetSpecial(std::string{
|
|
|
|
"pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd\0wtf.onion", 66}));
|
|
|
|
|
|
|
|
// TOR, bogus length
|
|
|
|
BOOST_CHECK(!addr.SetSpecial(std::string{"mfrggzak.onion"}));
|
|
|
|
|
|
|
|
// TOR, invalid base32
|
|
|
|
BOOST_CHECK(!addr.SetSpecial(std::string{"mf*g zak.onion"}));
|
|
|
|
|
2020-10-21 11:35:19 +02:00
|
|
|
// I2P
|
|
|
|
const char* i2p_addr = "UDHDrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v4jna.b32.I2P";
|
|
|
|
BOOST_REQUIRE(addr.SetSpecial(i2p_addr));
|
|
|
|
BOOST_REQUIRE(addr.IsValid());
|
|
|
|
BOOST_REQUIRE(addr.IsI2P());
|
|
|
|
|
|
|
|
BOOST_CHECK(!addr.IsTor());
|
|
|
|
BOOST_CHECK(!addr.IsBindAny());
|
|
|
|
BOOST_CHECK(!addr.IsAddrV1Compatible());
|
|
|
|
BOOST_CHECK_EQUAL(addr.ToString(), ToLower(i2p_addr));
|
|
|
|
|
|
|
|
// I2P, correct length, but decodes to less than the expected number of bytes.
|
|
|
|
BOOST_CHECK(!addr.SetSpecial("udhdrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v4jn=.b32.i2p"));
|
|
|
|
|
|
|
|
// I2P, extra unnecessary padding
|
|
|
|
BOOST_CHECK(!addr.SetSpecial("udhdrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v4jna=.b32.i2p"));
|
|
|
|
|
|
|
|
// I2P, malicious
|
|
|
|
BOOST_CHECK(!addr.SetSpecial("udhdrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v\0wtf.b32.i2p"s));
|
|
|
|
|
|
|
|
// I2P, valid but unsupported (56 Base32 characters)
|
|
|
|
// See "Encrypted LS with Base 32 Addresses" in
|
|
|
|
// https://geti2p.net/spec/encryptedleaseset.txt
|
|
|
|
BOOST_CHECK(
|
|
|
|
!addr.SetSpecial("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscsad.b32.i2p"));
|
|
|
|
|
|
|
|
// I2P, invalid base32
|
|
|
|
BOOST_CHECK(!addr.SetSpecial(std::string{"tp*szydbh4dp.b32.i2p"}));
|
|
|
|
|
2020-08-24 21:34:26 +02:00
|
|
|
// Internal
|
|
|
|
addr.SetInternal("esffpp");
|
|
|
|
BOOST_REQUIRE(!addr.IsValid()); // "internal" is considered invalid
|
|
|
|
BOOST_REQUIRE(addr.IsInternal());
|
|
|
|
|
|
|
|
BOOST_CHECK(!addr.IsBindAny());
|
2020-05-20 12:05:18 +02:00
|
|
|
BOOST_CHECK(addr.IsAddrV1Compatible());
|
2020-08-24 21:34:26 +02:00
|
|
|
BOOST_CHECK_EQUAL(addr.ToString(), "esffpvrt3wpeaygy.internal");
|
2020-08-27 11:03:21 +02:00
|
|
|
|
|
|
|
// Totally bogus
|
|
|
|
BOOST_CHECK(!addr.SetSpecial("totally bogus"));
|
2020-08-24 21:34:26 +02:00
|
|
|
}
|
|
|
|
|
2021-03-19 10:37:05 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(cnetaddr_tostring_canonical_ipv6)
|
|
|
|
{
|
|
|
|
// Test that CNetAddr::ToString formats IPv6 addresses with zero compression as described in
|
|
|
|
// RFC 5952 ("A Recommendation for IPv6 Address Text Representation").
|
|
|
|
const std::map<std::string, std::string> canonical_representations_ipv6{
|
|
|
|
{"0000:0000:0000:0000:0000:0000:0000:0000", "::"},
|
|
|
|
{"000:0000:000:00:0:00:000:0000", "::"},
|
|
|
|
{"000:000:000:000:000:000:000:000", "::"},
|
|
|
|
{"00:00:00:00:00:00:00:00", "::"},
|
|
|
|
{"0:0:0:0:0:0:0:0", "::"},
|
|
|
|
{"0:0:0:0:0:0:0:1", "::1"},
|
|
|
|
{"2001:0:0:1:0:0:0:1", "2001:0:0:1::1"},
|
|
|
|
{"2001:0db8:0:0:1:0:0:1", "2001:db8::1:0:0:1"},
|
|
|
|
{"2001:0db8:85a3:0000:0000:8a2e:0370:7334", "2001:db8:85a3::8a2e:370:7334"},
|
|
|
|
{"2001:0db8::0001", "2001:db8::1"},
|
|
|
|
{"2001:0db8::0001:0000", "2001:db8::1:0"},
|
|
|
|
{"2001:0db8::1:0:0:1", "2001:db8::1:0:0:1"},
|
|
|
|
{"2001:db8:0000:0:1::1", "2001:db8::1:0:0:1"},
|
|
|
|
{"2001:db8:0000:1:1:1:1:1", "2001:db8:0:1:1:1:1:1"},
|
|
|
|
{"2001:db8:0:0:0:0:2:1", "2001:db8::2:1"},
|
|
|
|
{"2001:db8:0:0:0::1", "2001:db8::1"},
|
|
|
|
{"2001:db8:0:0:1:0:0:1", "2001:db8::1:0:0:1"},
|
|
|
|
{"2001:db8:0:0:1::1", "2001:db8::1:0:0:1"},
|
|
|
|
{"2001:DB8:0:0:1::1", "2001:db8::1:0:0:1"},
|
|
|
|
{"2001:db8:0:0::1", "2001:db8::1"},
|
|
|
|
{"2001:db8:0:0:aaaa::1", "2001:db8::aaaa:0:0:1"},
|
|
|
|
{"2001:db8:0:1:1:1:1:1", "2001:db8:0:1:1:1:1:1"},
|
|
|
|
{"2001:db8:0::1", "2001:db8::1"},
|
|
|
|
{"2001:db8:85a3:0:0:8a2e:370:7334", "2001:db8:85a3::8a2e:370:7334"},
|
|
|
|
{"2001:db8::0:1", "2001:db8::1"},
|
|
|
|
{"2001:db8::0:1:0:0:1", "2001:db8::1:0:0:1"},
|
|
|
|
{"2001:DB8::1", "2001:db8::1"},
|
|
|
|
{"2001:db8::1", "2001:db8::1"},
|
|
|
|
{"2001:db8::1:0:0:1", "2001:db8::1:0:0:1"},
|
|
|
|
{"2001:db8::1:1:1:1:1", "2001:db8:0:1:1:1:1:1"},
|
|
|
|
{"2001:db8::aaaa:0:0:1", "2001:db8::aaaa:0:0:1"},
|
|
|
|
{"2001:db8:aaaa:bbbb:cccc:dddd:0:1", "2001:db8:aaaa:bbbb:cccc:dddd:0:1"},
|
|
|
|
{"2001:db8:aaaa:bbbb:cccc:dddd::1", "2001:db8:aaaa:bbbb:cccc:dddd:0:1"},
|
|
|
|
{"2001:db8:aaaa:bbbb:cccc:dddd:eeee:0001", "2001:db8:aaaa:bbbb:cccc:dddd:eeee:1"},
|
|
|
|
{"2001:db8:aaaa:bbbb:cccc:dddd:eeee:001", "2001:db8:aaaa:bbbb:cccc:dddd:eeee:1"},
|
|
|
|
{"2001:db8:aaaa:bbbb:cccc:dddd:eeee:01", "2001:db8:aaaa:bbbb:cccc:dddd:eeee:1"},
|
|
|
|
{"2001:db8:aaaa:bbbb:cccc:dddd:eeee:1", "2001:db8:aaaa:bbbb:cccc:dddd:eeee:1"},
|
|
|
|
{"2001:db8:aaaa:bbbb:cccc:dddd:eeee:aaaa", "2001:db8:aaaa:bbbb:cccc:dddd:eeee:aaaa"},
|
|
|
|
{"2001:db8:aaaa:bbbb:cccc:dddd:eeee:AAAA", "2001:db8:aaaa:bbbb:cccc:dddd:eeee:aaaa"},
|
|
|
|
{"2001:db8:aaaa:bbbb:cccc:dddd:eeee:AaAa", "2001:db8:aaaa:bbbb:cccc:dddd:eeee:aaaa"},
|
|
|
|
};
|
|
|
|
for (const auto& [input_address, expected_canonical_representation_output] : canonical_representations_ipv6) {
|
|
|
|
CNetAddr net_addr;
|
|
|
|
BOOST_REQUIRE(LookupHost(input_address, net_addr, false));
|
|
|
|
BOOST_REQUIRE(net_addr.IsIPv6());
|
|
|
|
BOOST_CHECK_EQUAL(net_addr.ToString(), expected_canonical_representation_output);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-19 15:01:50 +02:00
|
|
|
BOOST_AUTO_TEST_CASE(cnetaddr_serialize_v1)
|
2020-08-24 21:34:26 +02:00
|
|
|
{
|
|
|
|
CNetAddr addr;
|
|
|
|
CDataStream s(SER_NETWORK, PROTOCOL_VERSION);
|
|
|
|
|
2020-05-19 15:01:50 +02:00
|
|
|
s << addr;
|
|
|
|
BOOST_CHECK_EQUAL(HexStr(s), "00000000000000000000000000000000");
|
|
|
|
s.clear();
|
|
|
|
|
|
|
|
BOOST_REQUIRE(LookupHost("1.2.3.4", addr, false));
|
|
|
|
s << addr;
|
|
|
|
BOOST_CHECK_EQUAL(HexStr(s), "00000000000000000000ffff01020304");
|
|
|
|
s.clear();
|
|
|
|
|
|
|
|
BOOST_REQUIRE(LookupHost("1a1b:2a2b:3a3b:4a4b:5a5b:6a6b:7a7b:8a8b", addr, false));
|
|
|
|
s << addr;
|
|
|
|
BOOST_CHECK_EQUAL(HexStr(s), "1a1b2a2b3a3b4a4b5a5b6a6b7a7b8a8b");
|
|
|
|
s.clear();
|
|
|
|
|
2021-05-24 18:00:08 +02:00
|
|
|
// TORv2, no longer supported
|
|
|
|
BOOST_CHECK(!addr.SetSpecial("6hzph5hv6337r6p2.onion"));
|
2020-05-19 15:01:50 +02:00
|
|
|
|
2020-08-27 11:03:21 +02:00
|
|
|
BOOST_REQUIRE(addr.SetSpecial("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion"));
|
|
|
|
s << addr;
|
|
|
|
BOOST_CHECK_EQUAL(HexStr(s), "00000000000000000000000000000000");
|
|
|
|
s.clear();
|
|
|
|
|
2020-08-24 21:34:26 +02:00
|
|
|
addr.SetInternal("a");
|
|
|
|
s << addr;
|
|
|
|
BOOST_CHECK_EQUAL(HexStr(s), "fd6b88c08724ca978112ca1bbdcafac2");
|
|
|
|
s.clear();
|
|
|
|
}
|
|
|
|
|
2020-05-19 15:01:50 +02:00
|
|
|
BOOST_AUTO_TEST_CASE(cnetaddr_serialize_v2)
|
|
|
|
{
|
|
|
|
CNetAddr addr;
|
|
|
|
CDataStream s(SER_NETWORK, PROTOCOL_VERSION);
|
|
|
|
// Add ADDRV2_FORMAT to the version so that the CNetAddr
|
|
|
|
// serialize method produces an address in v2 format.
|
|
|
|
s.SetVersion(s.GetVersion() | ADDRV2_FORMAT);
|
|
|
|
|
|
|
|
s << addr;
|
|
|
|
BOOST_CHECK_EQUAL(HexStr(s), "021000000000000000000000000000000000");
|
|
|
|
s.clear();
|
|
|
|
|
|
|
|
BOOST_REQUIRE(LookupHost("1.2.3.4", addr, false));
|
|
|
|
s << addr;
|
|
|
|
BOOST_CHECK_EQUAL(HexStr(s), "010401020304");
|
|
|
|
s.clear();
|
|
|
|
|
|
|
|
BOOST_REQUIRE(LookupHost("1a1b:2a2b:3a3b:4a4b:5a5b:6a6b:7a7b:8a8b", addr, false));
|
|
|
|
s << addr;
|
|
|
|
BOOST_CHECK_EQUAL(HexStr(s), "02101a1b2a2b3a3b4a4b5a5b6a6b7a7b8a8b");
|
|
|
|
s.clear();
|
|
|
|
|
2021-05-24 18:00:08 +02:00
|
|
|
// TORv2, no longer supported
|
|
|
|
BOOST_CHECK(!addr.SetSpecial("6hzph5hv6337r6p2.onion"));
|
2020-05-19 15:01:50 +02:00
|
|
|
|
2020-08-27 11:03:21 +02:00
|
|
|
BOOST_REQUIRE(addr.SetSpecial("kpgvmscirrdqpekbqjsvw5teanhatztpp2gl6eee4zkowvwfxwenqaid.onion"));
|
|
|
|
s << addr;
|
|
|
|
BOOST_CHECK_EQUAL(HexStr(s), "042053cd5648488c4707914182655b7664034e09e66f7e8cbf1084e654eb56c5bd88");
|
|
|
|
s.clear();
|
|
|
|
|
2020-05-19 15:01:50 +02:00
|
|
|
BOOST_REQUIRE(addr.SetInternal("a"));
|
|
|
|
s << addr;
|
|
|
|
BOOST_CHECK_EQUAL(HexStr(s), "0210fd6b88c08724ca978112ca1bbdcafac2");
|
|
|
|
s.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(cnetaddr_unserialize_v2)
|
|
|
|
{
|
|
|
|
CNetAddr addr;
|
|
|
|
CDataStream s(SER_NETWORK, PROTOCOL_VERSION);
|
|
|
|
// Add ADDRV2_FORMAT to the version so that the CNetAddr
|
|
|
|
// unserialize method expects an address in v2 format.
|
|
|
|
s.SetVersion(s.GetVersion() | ADDRV2_FORMAT);
|
|
|
|
|
|
|
|
// Valid IPv4.
|
2021-11-01 16:32:53 -04:00
|
|
|
s << Span{ParseHex("01" // network type (IPv4)
|
|
|
|
"04" // address length
|
|
|
|
"01020304")}; // address
|
2020-05-19 15:01:50 +02:00
|
|
|
s >> addr;
|
|
|
|
BOOST_CHECK(addr.IsValid());
|
|
|
|
BOOST_CHECK(addr.IsIPv4());
|
2020-05-20 12:05:18 +02:00
|
|
|
BOOST_CHECK(addr.IsAddrV1Compatible());
|
2020-05-19 15:01:50 +02:00
|
|
|
BOOST_CHECK_EQUAL(addr.ToString(), "1.2.3.4");
|
|
|
|
BOOST_REQUIRE(s.empty());
|
|
|
|
|
|
|
|
// Invalid IPv4, valid length but address itself is shorter.
|
2021-11-01 16:32:53 -04:00
|
|
|
s << Span{ParseHex("01" // network type (IPv4)
|
|
|
|
"04" // address length
|
|
|
|
"0102")}; // address
|
2020-05-19 15:01:50 +02:00
|
|
|
BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure, HasReason("end of data"));
|
|
|
|
BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
|
|
|
|
s.clear();
|
|
|
|
|
|
|
|
// Invalid IPv4, with bogus length.
|
2021-11-01 16:32:53 -04:00
|
|
|
s << Span{ParseHex("01" // network type (IPv4)
|
|
|
|
"05" // address length
|
|
|
|
"01020304")}; // address
|
2020-05-19 15:01:50 +02:00
|
|
|
BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure,
|
|
|
|
HasReason("BIP155 IPv4 address with length 5 (should be 4)"));
|
|
|
|
BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
|
|
|
|
s.clear();
|
|
|
|
|
|
|
|
// Invalid IPv4, with extreme length.
|
2021-11-01 16:32:53 -04:00
|
|
|
s << Span{ParseHex("01" // network type (IPv4)
|
|
|
|
"fd0102" // address length (513 as CompactSize)
|
|
|
|
"01020304")}; // address
|
2020-05-19 15:01:50 +02:00
|
|
|
BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure,
|
|
|
|
HasReason("Address too long: 513 > 512"));
|
|
|
|
BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
|
|
|
|
s.clear();
|
|
|
|
|
|
|
|
// Valid IPv6.
|
2021-11-01 16:32:53 -04:00
|
|
|
s << Span{ParseHex("02" // network type (IPv6)
|
|
|
|
"10" // address length
|
|
|
|
"0102030405060708090a0b0c0d0e0f10")}; // address
|
2020-05-19 15:01:50 +02:00
|
|
|
s >> addr;
|
|
|
|
BOOST_CHECK(addr.IsValid());
|
|
|
|
BOOST_CHECK(addr.IsIPv6());
|
2020-05-20 12:05:18 +02:00
|
|
|
BOOST_CHECK(addr.IsAddrV1Compatible());
|
2020-05-19 15:01:50 +02:00
|
|
|
BOOST_CHECK_EQUAL(addr.ToString(), "102:304:506:708:90a:b0c:d0e:f10");
|
|
|
|
BOOST_REQUIRE(s.empty());
|
|
|
|
|
|
|
|
// Valid IPv6, contains embedded "internal".
|
2021-11-01 16:32:53 -04:00
|
|
|
s << Span{ParseHex(
|
2020-05-19 15:01:50 +02:00
|
|
|
"02" // network type (IPv6)
|
|
|
|
"10" // address length
|
2021-11-01 16:32:53 -04:00
|
|
|
"fd6b88c08724ca978112ca1bbdcafac2")}; // address: 0xfd + sha256("bitcoin")[0:5] +
|
2020-05-19 15:01:50 +02:00
|
|
|
// sha256(name)[0:10]
|
|
|
|
s >> addr;
|
|
|
|
BOOST_CHECK(addr.IsInternal());
|
2020-05-20 12:05:18 +02:00
|
|
|
BOOST_CHECK(addr.IsAddrV1Compatible());
|
2020-05-19 15:01:50 +02:00
|
|
|
BOOST_CHECK_EQUAL(addr.ToString(), "zklycewkdo64v6wc.internal");
|
|
|
|
BOOST_REQUIRE(s.empty());
|
|
|
|
|
|
|
|
// Invalid IPv6, with bogus length.
|
2021-11-01 16:32:53 -04:00
|
|
|
s << Span{ParseHex("02" // network type (IPv6)
|
|
|
|
"04" // address length
|
|
|
|
"00")}; // address
|
2020-05-19 15:01:50 +02:00
|
|
|
BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure,
|
|
|
|
HasReason("BIP155 IPv6 address with length 4 (should be 16)"));
|
|
|
|
BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
|
|
|
|
s.clear();
|
|
|
|
|
|
|
|
// Invalid IPv6, contains embedded IPv4.
|
2021-11-01 16:32:53 -04:00
|
|
|
s << Span{ParseHex("02" // network type (IPv6)
|
|
|
|
"10" // address length
|
|
|
|
"00000000000000000000ffff01020304")}; // address
|
2020-05-19 15:01:50 +02:00
|
|
|
s >> addr;
|
|
|
|
BOOST_CHECK(!addr.IsValid());
|
|
|
|
BOOST_REQUIRE(s.empty());
|
|
|
|
|
|
|
|
// Invalid IPv6, contains embedded TORv2.
|
2021-11-01 16:32:53 -04:00
|
|
|
s << Span{ParseHex("02" // network type (IPv6)
|
|
|
|
"10" // address length
|
|
|
|
"fd87d87eeb430102030405060708090a")}; // address
|
2020-05-19 15:01:50 +02:00
|
|
|
s >> addr;
|
|
|
|
BOOST_CHECK(!addr.IsValid());
|
|
|
|
BOOST_REQUIRE(s.empty());
|
|
|
|
|
2021-05-24 22:42:39 +02:00
|
|
|
// TORv2, no longer supported.
|
2021-11-01 16:32:53 -04:00
|
|
|
s << Span{ParseHex("03" // network type (TORv2)
|
|
|
|
"0a" // address length
|
|
|
|
"f1f2f3f4f5f6f7f8f9fa")}; // address
|
2020-05-19 15:01:50 +02:00
|
|
|
s >> addr;
|
2021-05-24 22:42:39 +02:00
|
|
|
BOOST_CHECK(!addr.IsValid());
|
2020-05-19 15:01:50 +02:00
|
|
|
BOOST_REQUIRE(s.empty());
|
|
|
|
|
2020-08-27 11:03:21 +02:00
|
|
|
// Valid TORv3.
|
2021-11-01 16:32:53 -04:00
|
|
|
s << Span{ParseHex("04" // network type (TORv3)
|
|
|
|
"20" // address length
|
|
|
|
"79bcc625184b05194975c28b66b66b04" // address
|
|
|
|
"69f7f6556fb1ac3189a79b40dda32f1f"
|
|
|
|
)};
|
2020-08-27 11:03:21 +02:00
|
|
|
s >> addr;
|
|
|
|
BOOST_CHECK(addr.IsValid());
|
|
|
|
BOOST_CHECK(addr.IsTor());
|
2020-05-20 12:05:18 +02:00
|
|
|
BOOST_CHECK(!addr.IsAddrV1Compatible());
|
2020-08-27 11:03:21 +02:00
|
|
|
BOOST_CHECK_EQUAL(addr.ToString(),
|
|
|
|
"pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion");
|
|
|
|
BOOST_REQUIRE(s.empty());
|
|
|
|
|
|
|
|
// Invalid TORv3, with bogus length.
|
2021-11-01 16:32:53 -04:00
|
|
|
s << Span{ParseHex("04" // network type (TORv3)
|
|
|
|
"00" // address length
|
|
|
|
"00" // address
|
|
|
|
)};
|
2020-08-27 11:03:21 +02:00
|
|
|
BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure,
|
|
|
|
HasReason("BIP155 TORv3 address with length 0 (should be 32)"));
|
|
|
|
BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
|
|
|
|
s.clear();
|
|
|
|
|
|
|
|
// Valid I2P.
|
2021-11-01 16:32:53 -04:00
|
|
|
s << Span{ParseHex("05" // network type (I2P)
|
|
|
|
"20" // address length
|
|
|
|
"a2894dabaec08c0051a481a6dac88b64" // address
|
|
|
|
"f98232ae42d4b6fd2fa81952dfe36a87")};
|
2020-08-27 11:03:21 +02:00
|
|
|
s >> addr;
|
|
|
|
BOOST_CHECK(addr.IsValid());
|
2020-05-20 12:05:18 +02:00
|
|
|
BOOST_CHECK(addr.IsI2P());
|
|
|
|
BOOST_CHECK(!addr.IsAddrV1Compatible());
|
2020-08-27 11:03:21 +02:00
|
|
|
BOOST_CHECK_EQUAL(addr.ToString(),
|
|
|
|
"ukeu3k5oycgaauneqgtnvselmt4yemvoilkln7jpvamvfx7dnkdq.b32.i2p");
|
|
|
|
BOOST_REQUIRE(s.empty());
|
|
|
|
|
|
|
|
// Invalid I2P, with bogus length.
|
2021-11-01 16:32:53 -04:00
|
|
|
s << Span{ParseHex("05" // network type (I2P)
|
|
|
|
"03" // address length
|
|
|
|
"00" // address
|
|
|
|
)};
|
2020-08-27 11:03:21 +02:00
|
|
|
BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure,
|
|
|
|
HasReason("BIP155 I2P address with length 3 (should be 32)"));
|
|
|
|
BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
|
|
|
|
s.clear();
|
|
|
|
|
|
|
|
// Valid CJDNS.
|
2021-11-01 16:32:53 -04:00
|
|
|
s << Span{ParseHex("06" // network type (CJDNS)
|
|
|
|
"10" // address length
|
|
|
|
"fc000001000200030004000500060007" // address
|
|
|
|
)};
|
2020-08-27 11:03:21 +02:00
|
|
|
s >> addr;
|
|
|
|
BOOST_CHECK(addr.IsValid());
|
2020-05-20 12:05:18 +02:00
|
|
|
BOOST_CHECK(addr.IsCJDNS());
|
|
|
|
BOOST_CHECK(!addr.IsAddrV1Compatible());
|
2020-08-27 11:03:21 +02:00
|
|
|
BOOST_CHECK_EQUAL(addr.ToString(), "fc00:1:2:3:4:5:6:7");
|
|
|
|
BOOST_REQUIRE(s.empty());
|
|
|
|
|
2020-12-10 14:26:58 -03:00
|
|
|
// Invalid CJDNS, wrong prefix.
|
2021-11-01 16:32:53 -04:00
|
|
|
s << Span{ParseHex("06" // network type (CJDNS)
|
|
|
|
"10" // address length
|
|
|
|
"aa000001000200030004000500060007" // address
|
|
|
|
)};
|
2020-12-10 14:26:58 -03:00
|
|
|
s >> addr;
|
|
|
|
BOOST_CHECK(addr.IsCJDNS());
|
|
|
|
BOOST_CHECK(!addr.IsValid());
|
|
|
|
BOOST_REQUIRE(s.empty());
|
|
|
|
|
2020-08-27 11:03:21 +02:00
|
|
|
// Invalid CJDNS, with bogus length.
|
2021-11-01 16:32:53 -04:00
|
|
|
s << Span{ParseHex("06" // network type (CJDNS)
|
|
|
|
"01" // address length
|
|
|
|
"00" // address
|
|
|
|
)};
|
2020-08-27 11:03:21 +02:00
|
|
|
BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure,
|
|
|
|
HasReason("BIP155 CJDNS address with length 1 (should be 16)"));
|
|
|
|
BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
|
|
|
|
s.clear();
|
|
|
|
|
2020-05-19 15:01:50 +02:00
|
|
|
// Unknown, with extreme length.
|
2021-11-01 16:32:53 -04:00
|
|
|
s << Span{ParseHex("aa" // network type (unknown)
|
|
|
|
"fe00000002" // address length (CompactSize's MAX_SIZE)
|
|
|
|
"01020304050607" // address
|
|
|
|
)};
|
2020-05-19 15:01:50 +02:00
|
|
|
BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure,
|
|
|
|
HasReason("Address too long: 33554432 > 512"));
|
|
|
|
BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
|
|
|
|
s.clear();
|
|
|
|
|
|
|
|
// Unknown, with reasonable length.
|
2021-11-01 16:32:53 -04:00
|
|
|
s << Span{ParseHex("aa" // network type (unknown)
|
|
|
|
"04" // address length
|
|
|
|
"01020304" // address
|
|
|
|
)};
|
2020-05-19 15:01:50 +02:00
|
|
|
s >> addr;
|
|
|
|
BOOST_CHECK(!addr.IsValid());
|
|
|
|
BOOST_REQUIRE(s.empty());
|
|
|
|
|
|
|
|
// Unknown, with zero length.
|
2021-11-01 16:32:53 -04:00
|
|
|
s << Span{ParseHex("aa" // network type (unknown)
|
|
|
|
"00" // address length
|
|
|
|
"" // address
|
|
|
|
)};
|
2020-05-19 15:01:50 +02:00
|
|
|
s >> addr;
|
|
|
|
BOOST_CHECK(!addr.IsValid());
|
|
|
|
BOOST_REQUIRE(s.empty());
|
|
|
|
}
|
|
|
|
|
2018-11-15 12:34:46 -08:00
|
|
|
// prior to PR #14728, this test triggers an undefined behavior
|
|
|
|
BOOST_AUTO_TEST_CASE(ipv4_peer_with_ipv6_addrMe_test)
|
|
|
|
{
|
|
|
|
// set up local addresses; all that's necessary to reproduce the bug is
|
|
|
|
// that a normal IPv4 address is among the entries, but if this address is
|
|
|
|
// !IsRoutable the undefined behavior is easier to trigger deterministically
|
|
|
|
{
|
|
|
|
LOCK(cs_mapLocalHost);
|
|
|
|
in_addr ipv4AddrLocal;
|
|
|
|
ipv4AddrLocal.s_addr = 0x0100007f;
|
|
|
|
CNetAddr addr = CNetAddr(ipv4AddrLocal);
|
|
|
|
LocalServiceInfo lsi;
|
|
|
|
lsi.nScore = 23;
|
|
|
|
lsi.nPort = 42;
|
|
|
|
mapLocalHost[addr] = lsi;
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a peer with an IPv4 address
|
|
|
|
in_addr ipv4AddrPeer;
|
|
|
|
ipv4AddrPeer.s_addr = 0xa0b0c001;
|
|
|
|
CAddress addr = CAddress(CService(ipv4AddrPeer, 7777), NODE_NETWORK);
|
2021-09-14 16:56:34 +02:00
|
|
|
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);
|
2018-11-15 12:34:46 -08:00
|
|
|
pnode->fSuccessfullyConnected.store(true);
|
|
|
|
|
|
|
|
// the peer claims to be reaching us via IPv6
|
|
|
|
in6_addr ipv6AddrLocal;
|
|
|
|
memset(ipv6AddrLocal.s6_addr, 0, 16);
|
|
|
|
ipv6AddrLocal.s6_addr[0] = 0xcc;
|
|
|
|
CAddress addrLocal = CAddress(CService(ipv6AddrLocal, 7777), NODE_NETWORK);
|
|
|
|
pnode->SetAddrLocal(addrLocal);
|
|
|
|
|
|
|
|
// before patch, this causes undefined behavior detectable with clang's -fsanitize=memory
|
2020-07-09 07:42:11 +01:00
|
|
|
GetLocalAddrForPeer(&*pnode);
|
2018-11-15 12:34:46 -08:00
|
|
|
|
|
|
|
// suppress no-checks-run warning; if this test fails, it's by triggering a sanitizer
|
|
|
|
BOOST_CHECK(1);
|
|
|
|
}
|
|
|
|
|
2018-12-28 16:20:30 +01:00
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(LimitedAndReachable_Network)
|
|
|
|
{
|
2021-08-06 19:59:14 +02:00
|
|
|
BOOST_CHECK(IsReachable(NET_IPV4));
|
|
|
|
BOOST_CHECK(IsReachable(NET_IPV6));
|
|
|
|
BOOST_CHECK(IsReachable(NET_ONION));
|
|
|
|
BOOST_CHECK(IsReachable(NET_I2P));
|
2018-12-28 16:20:30 +01:00
|
|
|
|
2019-01-09 16:41:37 -08:00
|
|
|
SetReachable(NET_IPV4, false);
|
|
|
|
SetReachable(NET_IPV6, false);
|
|
|
|
SetReachable(NET_ONION, false);
|
2021-08-06 19:59:14 +02:00
|
|
|
SetReachable(NET_I2P, false);
|
2018-12-28 16:20:30 +01:00
|
|
|
|
2021-08-06 19:59:14 +02:00
|
|
|
BOOST_CHECK(!IsReachable(NET_IPV4));
|
|
|
|
BOOST_CHECK(!IsReachable(NET_IPV6));
|
|
|
|
BOOST_CHECK(!IsReachable(NET_ONION));
|
|
|
|
BOOST_CHECK(!IsReachable(NET_I2P));
|
2018-12-28 16:20:30 +01:00
|
|
|
|
2019-01-09 16:41:37 -08:00
|
|
|
SetReachable(NET_IPV4, true);
|
|
|
|
SetReachable(NET_IPV6, true);
|
|
|
|
SetReachable(NET_ONION, true);
|
2021-08-06 19:59:14 +02:00
|
|
|
SetReachable(NET_I2P, true);
|
2018-12-28 16:20:30 +01:00
|
|
|
|
2021-08-06 19:59:14 +02:00
|
|
|
BOOST_CHECK(IsReachable(NET_IPV4));
|
|
|
|
BOOST_CHECK(IsReachable(NET_IPV6));
|
|
|
|
BOOST_CHECK(IsReachable(NET_ONION));
|
|
|
|
BOOST_CHECK(IsReachable(NET_I2P));
|
2018-12-28 16:20:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(LimitedAndReachable_NetworkCaseUnroutableAndInternal)
|
|
|
|
{
|
2021-08-06 19:59:14 +02:00
|
|
|
BOOST_CHECK(IsReachable(NET_UNROUTABLE));
|
|
|
|
BOOST_CHECK(IsReachable(NET_INTERNAL));
|
2018-12-28 16:20:30 +01:00
|
|
|
|
2019-01-09 16:41:37 -08:00
|
|
|
SetReachable(NET_UNROUTABLE, false);
|
|
|
|
SetReachable(NET_INTERNAL, false);
|
2018-12-28 16:20:30 +01:00
|
|
|
|
2021-08-06 19:59:14 +02:00
|
|
|
BOOST_CHECK(IsReachable(NET_UNROUTABLE)); // Ignored for both networks
|
|
|
|
BOOST_CHECK(IsReachable(NET_INTERNAL));
|
2018-12-28 16:20:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CNetAddr UtilBuildAddress(unsigned char p1, unsigned char p2, unsigned char p3, unsigned char p4)
|
|
|
|
{
|
|
|
|
unsigned char ip[] = {p1, p2, p3, p4};
|
|
|
|
|
|
|
|
struct sockaddr_in sa;
|
|
|
|
memset(&sa, 0, sizeof(sockaddr_in)); // initialize the memory block
|
|
|
|
memcpy(&(sa.sin_addr), &ip, sizeof(ip));
|
|
|
|
return CNetAddr(sa.sin_addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(LimitedAndReachable_CNetAddr)
|
|
|
|
{
|
|
|
|
CNetAddr addr = UtilBuildAddress(0x001, 0x001, 0x001, 0x001); // 1.1.1.1
|
|
|
|
|
2019-01-09 16:41:37 -08:00
|
|
|
SetReachable(NET_IPV4, true);
|
2021-08-06 19:59:14 +02:00
|
|
|
BOOST_CHECK(IsReachable(addr));
|
2018-12-28 16:20:30 +01:00
|
|
|
|
2019-01-09 16:41:37 -08:00
|
|
|
SetReachable(NET_IPV4, false);
|
2021-08-06 19:59:14 +02:00
|
|
|
BOOST_CHECK(!IsReachable(addr));
|
2018-12-28 16:20:30 +01:00
|
|
|
|
2019-01-09 16:41:37 -08:00
|
|
|
SetReachable(NET_IPV4, true); // have to reset this, because this is stateful.
|
2018-12-28 16:20:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(LocalAddress_BasicLifecycle)
|
|
|
|
{
|
|
|
|
CService addr = CService(UtilBuildAddress(0x002, 0x001, 0x001, 0x001), 1000); // 2.1.1.1:1000
|
|
|
|
|
2019-01-09 16:41:37 -08:00
|
|
|
SetReachable(NET_IPV4, true);
|
2018-12-28 16:20:30 +01:00
|
|
|
|
2021-08-06 19:59:14 +02:00
|
|
|
BOOST_CHECK(!IsLocal(addr));
|
|
|
|
BOOST_CHECK(AddLocal(addr, 1000));
|
|
|
|
BOOST_CHECK(IsLocal(addr));
|
2018-12-28 16:20:30 +01:00
|
|
|
|
|
|
|
RemoveLocal(addr);
|
2021-08-06 19:59:14 +02:00
|
|
|
BOOST_CHECK(!IsLocal(addr));
|
2018-12-28 16:20:30 +01:00
|
|
|
}
|
|
|
|
|
2016-03-16 12:54:30 -04:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|