LCOV - code coverage report
Current view: top level - src/test - net_tests.cpp (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 637 637 100.0 %
Date: 2022-04-21 14:51:19 Functions: 16 16 100.0 %
Legend: Modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed

Not modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed
Branches: 7 10 70.0 %

           Branch data     Line data    Source code
#       1                 :            : // Copyright (c) 2012-2021 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 <compat.h>
#       8                 :            : #include <cstdint>
#       9                 :            : #include <net.h>
#      10                 :            : #include <net_processing.h>
#      11                 :            : #include <netaddress.h>
#      12                 :            : #include <netbase.h>
#      13                 :            : #include <netmessagemaker.h>
#      14                 :            : #include <serialize.h>
#      15                 :            : #include <span.h>
#      16                 :            : #include <streams.h>
#      17                 :            : #include <test/util/setup_common.h>
#      18                 :            : #include <test/util/validation.h>
#      19                 :            : #include <timedata.h>
#      20                 :            : #include <util/strencodings.h>
#      21                 :            : #include <util/string.h>
#      22                 :            : #include <util/system.h>
#      23                 :            : #include <validation.h>
#      24                 :            : #include <version.h>
#      25                 :            : 
#      26                 :            : #include <boost/test/unit_test.hpp>
#      27                 :            : 
#      28                 :            : #include <algorithm>
#      29                 :            : #include <ios>
#      30                 :            : #include <memory>
#      31                 :            : #include <optional>
#      32                 :            : #include <string>
#      33                 :            : 
#      34                 :            : using namespace std::literals;
#      35                 :            : 
#      36                 :            : BOOST_FIXTURE_TEST_SUITE(net_tests, RegTestingSetup)
#      37                 :            : 
#      38                 :            : BOOST_AUTO_TEST_CASE(cnode_listen_port)
#      39                 :          2 : {
#      40                 :            :     // test default
#      41                 :          2 :     uint16_t port{GetListenPort()};
#      42                 :          2 :     BOOST_CHECK(port == Params().GetDefaultPort());
#      43                 :            :     // test set port
#      44                 :          2 :     uint16_t altPort = 12345;
#      45                 :          2 :     BOOST_CHECK(gArgs.SoftSetArg("-port", ToString(altPort)));
#      46                 :          2 :     port = GetListenPort();
#      47                 :          2 :     BOOST_CHECK(port == altPort);
#      48                 :          2 : }
#      49                 :            : 
#      50                 :            : BOOST_AUTO_TEST_CASE(cnode_simple_test)
#      51                 :          2 : {
#      52                 :          2 :     NodeId id = 0;
#      53                 :            : 
#      54                 :          2 :     in_addr ipv4Addr;
#      55                 :          2 :     ipv4Addr.s_addr = 0xa0b0c001;
#      56                 :            : 
#      57                 :          2 :     CAddress addr = CAddress(CService(ipv4Addr, 7777), NODE_NETWORK);
#      58                 :          2 :     std::string pszDest;
#      59                 :            : 
#      60                 :          2 :     std::unique_ptr<CNode> pnode1 = std::make_unique<CNode>(id++,
#      61                 :          2 :                                                             NODE_NETWORK,
#      62                 :          2 :                                                             /*sock=*/nullptr,
#      63                 :          2 :                                                             addr,
#      64                 :          2 :                                                             /*nKeyedNetGroupIn=*/0,
#      65                 :          2 :                                                             /*nLocalHostNonceIn=*/0,
#      66                 :          2 :                                                             CAddress(),
#      67                 :          2 :                                                             pszDest,
#      68                 :          2 :                                                             ConnectionType::OUTBOUND_FULL_RELAY,
#      69                 :          2 :                                                             /*inbound_onion=*/false);
#      70                 :          2 :     BOOST_CHECK(pnode1->IsFullOutboundConn() == true);
#      71                 :          2 :     BOOST_CHECK(pnode1->IsManualConn() == false);
#      72                 :          2 :     BOOST_CHECK(pnode1->IsBlockOnlyConn() == false);
#      73                 :          2 :     BOOST_CHECK(pnode1->IsFeelerConn() == false);
#      74                 :          2 :     BOOST_CHECK(pnode1->IsAddrFetchConn() == false);
#      75                 :          2 :     BOOST_CHECK(pnode1->IsInboundConn() == false);
#      76                 :          2 :     BOOST_CHECK(pnode1->m_inbound_onion == false);
#      77                 :          2 :     BOOST_CHECK_EQUAL(pnode1->ConnectedThroughNetwork(), Network::NET_IPV4);
#      78                 :            : 
#      79                 :          2 :     std::unique_ptr<CNode> pnode2 = std::make_unique<CNode>(id++,
#      80                 :          2 :                                                             NODE_NETWORK,
#      81                 :          2 :                                                             /*sock=*/nullptr,
#      82                 :          2 :                                                             addr,
#      83                 :          2 :                                                             /*nKeyedNetGroupIn=*/1,
#      84                 :          2 :                                                             /*nLocalHostNonceIn=*/1,
#      85                 :          2 :                                                             CAddress(),
#      86                 :          2 :                                                             pszDest,
#      87                 :          2 :                                                             ConnectionType::INBOUND,
#      88                 :          2 :                                                             /*inbound_onion=*/false);
#      89                 :          2 :     BOOST_CHECK(pnode2->IsFullOutboundConn() == false);
#      90                 :          2 :     BOOST_CHECK(pnode2->IsManualConn() == false);
#      91                 :          2 :     BOOST_CHECK(pnode2->IsBlockOnlyConn() == false);
#      92                 :          2 :     BOOST_CHECK(pnode2->IsFeelerConn() == false);
#      93                 :          2 :     BOOST_CHECK(pnode2->IsAddrFetchConn() == false);
#      94                 :          2 :     BOOST_CHECK(pnode2->IsInboundConn() == true);
#      95                 :          2 :     BOOST_CHECK(pnode2->m_inbound_onion == false);
#      96                 :          2 :     BOOST_CHECK_EQUAL(pnode2->ConnectedThroughNetwork(), Network::NET_IPV4);
#      97                 :            : 
#      98                 :          2 :     std::unique_ptr<CNode> pnode3 = std::make_unique<CNode>(id++,
#      99                 :          2 :                                                             NODE_NETWORK,
#     100                 :          2 :                                                             /*sock=*/nullptr,
#     101                 :          2 :                                                             addr,
#     102                 :          2 :                                                             /*nKeyedNetGroupIn=*/0,
#     103                 :          2 :                                                             /*nLocalHostNonceIn=*/0,
#     104                 :          2 :                                                             CAddress(),
#     105                 :          2 :                                                             pszDest,
#     106                 :          2 :                                                             ConnectionType::OUTBOUND_FULL_RELAY,
#     107                 :          2 :                                                             /*inbound_onion=*/false);
#     108                 :          2 :     BOOST_CHECK(pnode3->IsFullOutboundConn() == true);
#     109                 :          2 :     BOOST_CHECK(pnode3->IsManualConn() == false);
#     110                 :          2 :     BOOST_CHECK(pnode3->IsBlockOnlyConn() == false);
#     111                 :          2 :     BOOST_CHECK(pnode3->IsFeelerConn() == false);
#     112                 :          2 :     BOOST_CHECK(pnode3->IsAddrFetchConn() == false);
#     113                 :          2 :     BOOST_CHECK(pnode3->IsInboundConn() == false);
#     114                 :          2 :     BOOST_CHECK(pnode3->m_inbound_onion == false);
#     115                 :          2 :     BOOST_CHECK_EQUAL(pnode3->ConnectedThroughNetwork(), Network::NET_IPV4);
#     116                 :            : 
#     117                 :          2 :     std::unique_ptr<CNode> pnode4 = std::make_unique<CNode>(id++,
#     118                 :          2 :                                                             NODE_NETWORK,
#     119                 :          2 :                                                             /*sock=*/nullptr,
#     120                 :          2 :                                                             addr,
#     121                 :          2 :                                                             /*nKeyedNetGroupIn=*/1,
#     122                 :          2 :                                                             /*nLocalHostNonceIn=*/1,
#     123                 :          2 :                                                             CAddress(),
#     124                 :          2 :                                                             pszDest,
#     125                 :          2 :                                                             ConnectionType::INBOUND,
#     126                 :          2 :                                                             /*inbound_onion=*/true);
#     127                 :          2 :     BOOST_CHECK(pnode4->IsFullOutboundConn() == false);
#     128                 :          2 :     BOOST_CHECK(pnode4->IsManualConn() == false);
#     129                 :          2 :     BOOST_CHECK(pnode4->IsBlockOnlyConn() == false);
#     130                 :          2 :     BOOST_CHECK(pnode4->IsFeelerConn() == false);
#     131                 :          2 :     BOOST_CHECK(pnode4->IsAddrFetchConn() == false);
#     132                 :          2 :     BOOST_CHECK(pnode4->IsInboundConn() == true);
#     133                 :          2 :     BOOST_CHECK(pnode4->m_inbound_onion == true);
#     134                 :          2 :     BOOST_CHECK_EQUAL(pnode4->ConnectedThroughNetwork(), Network::NET_ONION);
#     135                 :          2 : }
#     136                 :            : 
#     137                 :            : BOOST_AUTO_TEST_CASE(cnetaddr_basic)
#     138                 :          2 : {
#     139                 :          2 :     CNetAddr addr;
#     140                 :            : 
#     141                 :            :     // IPv4, INADDR_ANY
#     142                 :          2 :     BOOST_REQUIRE(LookupHost("0.0.0.0", addr, false));
#     143                 :          2 :     BOOST_REQUIRE(!addr.IsValid());
#     144                 :          2 :     BOOST_REQUIRE(addr.IsIPv4());
#     145                 :            : 
#     146                 :          2 :     BOOST_CHECK(addr.IsBindAny());
#     147                 :          2 :     BOOST_CHECK(addr.IsAddrV1Compatible());
#     148                 :          2 :     BOOST_CHECK_EQUAL(addr.ToString(), "0.0.0.0");
#     149                 :            : 
#     150                 :            :     // IPv4, INADDR_NONE
#     151                 :          2 :     BOOST_REQUIRE(LookupHost("255.255.255.255", addr, false));
#     152                 :          2 :     BOOST_REQUIRE(!addr.IsValid());
#     153                 :          2 :     BOOST_REQUIRE(addr.IsIPv4());
#     154                 :            : 
#     155                 :          2 :     BOOST_CHECK(!addr.IsBindAny());
#     156                 :          2 :     BOOST_CHECK(addr.IsAddrV1Compatible());
#     157                 :          2 :     BOOST_CHECK_EQUAL(addr.ToString(), "255.255.255.255");
#     158                 :            : 
#     159                 :            :     // IPv4, casual
#     160                 :          2 :     BOOST_REQUIRE(LookupHost("12.34.56.78", addr, false));
#     161                 :          2 :     BOOST_REQUIRE(addr.IsValid());
#     162                 :          2 :     BOOST_REQUIRE(addr.IsIPv4());
#     163                 :            : 
#     164                 :          2 :     BOOST_CHECK(!addr.IsBindAny());
#     165                 :          2 :     BOOST_CHECK(addr.IsAddrV1Compatible());
#     166                 :          2 :     BOOST_CHECK_EQUAL(addr.ToString(), "12.34.56.78");
#     167                 :            : 
#     168                 :            :     // IPv6, in6addr_any
#     169                 :          2 :     BOOST_REQUIRE(LookupHost("::", addr, false));
#     170                 :          2 :     BOOST_REQUIRE(!addr.IsValid());
#     171                 :          2 :     BOOST_REQUIRE(addr.IsIPv6());
#     172                 :            : 
#     173                 :          2 :     BOOST_CHECK(addr.IsBindAny());
#     174                 :          2 :     BOOST_CHECK(addr.IsAddrV1Compatible());
#     175                 :          2 :     BOOST_CHECK_EQUAL(addr.ToString(), "::");
#     176                 :            : 
#     177                 :            :     // IPv6, casual
#     178                 :          2 :     BOOST_REQUIRE(LookupHost("1122:3344:5566:7788:9900:aabb:ccdd:eeff", addr, false));
#     179                 :          2 :     BOOST_REQUIRE(addr.IsValid());
#     180                 :          2 :     BOOST_REQUIRE(addr.IsIPv6());
#     181                 :            : 
#     182                 :          2 :     BOOST_CHECK(!addr.IsBindAny());
#     183                 :          2 :     BOOST_CHECK(addr.IsAddrV1Compatible());
#     184                 :          2 :     BOOST_CHECK_EQUAL(addr.ToString(), "1122:3344:5566:7788:9900:aabb:ccdd:eeff");
#     185                 :            : 
#     186                 :            :     // IPv6, scoped/link-local. See https://tools.ietf.org/html/rfc4007
#     187                 :            :     // We support non-negative decimal integers (uint32_t) as zone id indices.
#     188                 :            :     // Normal link-local scoped address functionality is to append "%" plus the
#     189                 :            :     // zone id, for example, given a link-local address of "fe80::1" and a zone
#     190                 :            :     // id of "32", return the address as "fe80::1%32".
#     191                 :          2 :     const std::string link_local{"fe80::1"};
#     192                 :          2 :     const std::string scoped_addr{link_local + "%32"};
#     193                 :          2 :     BOOST_REQUIRE(LookupHost(scoped_addr, addr, false));
#     194                 :          2 :     BOOST_REQUIRE(addr.IsValid());
#     195                 :          2 :     BOOST_REQUIRE(addr.IsIPv6());
#     196                 :          2 :     BOOST_CHECK(!addr.IsBindAny());
#     197                 :          2 :     BOOST_CHECK_EQUAL(addr.ToString(), scoped_addr);
#     198                 :            : 
#     199                 :            :     // Test that the delimiter "%" and default zone id of 0 can be omitted for the default scope.
#     200                 :          2 :     BOOST_REQUIRE(LookupHost(link_local + "%0", addr, false));
#     201                 :          2 :     BOOST_REQUIRE(addr.IsValid());
#     202                 :          2 :     BOOST_REQUIRE(addr.IsIPv6());
#     203                 :          2 :     BOOST_CHECK(!addr.IsBindAny());
#     204                 :          2 :     BOOST_CHECK_EQUAL(addr.ToString(), link_local);
#     205                 :            : 
#     206                 :            :     // TORv2, no longer supported
#     207                 :          2 :     BOOST_CHECK(!addr.SetSpecial("6hzph5hv6337r6p2.onion"));
#     208                 :            : 
#     209                 :            :     // TORv3
#     210                 :          2 :     const char* torv3_addr = "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion";
#     211                 :          2 :     BOOST_REQUIRE(addr.SetSpecial(torv3_addr));
#     212                 :          2 :     BOOST_REQUIRE(addr.IsValid());
#     213                 :          2 :     BOOST_REQUIRE(addr.IsTor());
#     214                 :            : 
#     215                 :          2 :     BOOST_CHECK(!addr.IsI2P());
#     216                 :          2 :     BOOST_CHECK(!addr.IsBindAny());
#     217                 :          2 :     BOOST_CHECK(!addr.IsAddrV1Compatible());
#     218                 :          2 :     BOOST_CHECK_EQUAL(addr.ToString(), torv3_addr);
#     219                 :            : 
#     220                 :            :     // TORv3, broken, with wrong checksum
#     221                 :          2 :     BOOST_CHECK(!addr.SetSpecial("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscsad.onion"));
#     222                 :            : 
#     223                 :            :     // TORv3, broken, with wrong version
#     224                 :          2 :     BOOST_CHECK(!addr.SetSpecial("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscrye.onion"));
#     225                 :            : 
#     226                 :            :     // TORv3, malicious
#     227                 :          2 :     BOOST_CHECK(!addr.SetSpecial(std::string{
#     228                 :          2 :         "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd\0wtf.onion", 66}));
#     229                 :            : 
#     230                 :            :     // TOR, bogus length
#     231                 :          2 :     BOOST_CHECK(!addr.SetSpecial(std::string{"mfrggzak.onion"}));
#     232                 :            : 
#     233                 :            :     // TOR, invalid base32
#     234                 :          2 :     BOOST_CHECK(!addr.SetSpecial(std::string{"mf*g zak.onion"}));
#     235                 :            : 
#     236                 :            :     // I2P
#     237                 :          2 :     const char* i2p_addr = "UDHDrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v4jna.b32.I2P";
#     238                 :          2 :     BOOST_REQUIRE(addr.SetSpecial(i2p_addr));
#     239                 :          2 :     BOOST_REQUIRE(addr.IsValid());
#     240                 :          2 :     BOOST_REQUIRE(addr.IsI2P());
#     241                 :            : 
#     242                 :          2 :     BOOST_CHECK(!addr.IsTor());
#     243                 :          2 :     BOOST_CHECK(!addr.IsBindAny());
#     244                 :          2 :     BOOST_CHECK(!addr.IsAddrV1Compatible());
#     245                 :          2 :     BOOST_CHECK_EQUAL(addr.ToString(), ToLower(i2p_addr));
#     246                 :            : 
#     247                 :            :     // I2P, correct length, but decodes to less than the expected number of bytes.
#     248                 :          2 :     BOOST_CHECK(!addr.SetSpecial("udhdrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v4jn=.b32.i2p"));
#     249                 :            : 
#     250                 :            :     // I2P, extra unnecessary padding
#     251                 :          2 :     BOOST_CHECK(!addr.SetSpecial("udhdrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v4jna=.b32.i2p"));
#     252                 :            : 
#     253                 :            :     // I2P, malicious
#     254                 :          2 :     BOOST_CHECK(!addr.SetSpecial("udhdrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v\0wtf.b32.i2p"s));
#     255                 :            : 
#     256                 :            :     // I2P, valid but unsupported (56 Base32 characters)
#     257                 :            :     // See "Encrypted LS with Base 32 Addresses" in
#     258                 :            :     // https://geti2p.net/spec/encryptedleaseset.txt
#     259                 :          2 :     BOOST_CHECK(
#     260                 :          2 :         !addr.SetSpecial("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscsad.b32.i2p"));
#     261                 :            : 
#     262                 :            :     // I2P, invalid base32
#     263                 :          2 :     BOOST_CHECK(!addr.SetSpecial(std::string{"tp*szydbh4dp.b32.i2p"}));
#     264                 :            : 
#     265                 :            :     // Internal
#     266                 :          2 :     addr.SetInternal("esffpp");
#     267                 :          2 :     BOOST_REQUIRE(!addr.IsValid()); // "internal" is considered invalid
#     268                 :          2 :     BOOST_REQUIRE(addr.IsInternal());
#     269                 :            : 
#     270                 :          2 :     BOOST_CHECK(!addr.IsBindAny());
#     271                 :          2 :     BOOST_CHECK(addr.IsAddrV1Compatible());
#     272                 :          2 :     BOOST_CHECK_EQUAL(addr.ToString(), "esffpvrt3wpeaygy.internal");
#     273                 :            : 
#     274                 :            :     // Totally bogus
#     275                 :          2 :     BOOST_CHECK(!addr.SetSpecial("totally bogus"));
#     276                 :          2 : }
#     277                 :            : 
#     278                 :            : BOOST_AUTO_TEST_CASE(cnetaddr_tostring_canonical_ipv6)
#     279                 :          2 : {
#     280                 :            :     // Test that CNetAddr::ToString formats IPv6 addresses with zero compression as described in
#     281                 :            :     // RFC 5952 ("A Recommendation for IPv6 Address Text Representation").
#     282                 :          2 :     const std::map<std::string, std::string> canonical_representations_ipv6{
#     283                 :          2 :         {"0000:0000:0000:0000:0000:0000:0000:0000", "::"},
#     284                 :          2 :         {"000:0000:000:00:0:00:000:0000", "::"},
#     285                 :          2 :         {"000:000:000:000:000:000:000:000", "::"},
#     286                 :          2 :         {"00:00:00:00:00:00:00:00", "::"},
#     287                 :          2 :         {"0:0:0:0:0:0:0:0", "::"},
#     288                 :          2 :         {"0:0:0:0:0:0:0:1", "::1"},
#     289                 :          2 :         {"2001:0:0:1:0:0:0:1", "2001:0:0:1::1"},
#     290                 :          2 :         {"2001:0db8:0:0:1:0:0:1", "2001:db8::1:0:0:1"},
#     291                 :          2 :         {"2001:0db8:85a3:0000:0000:8a2e:0370:7334", "2001:db8:85a3::8a2e:370:7334"},
#     292                 :          2 :         {"2001:0db8::0001", "2001:db8::1"},
#     293                 :          2 :         {"2001:0db8::0001:0000", "2001:db8::1:0"},
#     294                 :          2 :         {"2001:0db8::1:0:0:1", "2001:db8::1:0:0:1"},
#     295                 :          2 :         {"2001:db8:0000:0:1::1", "2001:db8::1:0:0:1"},
#     296                 :          2 :         {"2001:db8:0000:1:1:1:1:1", "2001:db8:0:1:1:1:1:1"},
#     297                 :          2 :         {"2001:db8:0:0:0:0:2:1", "2001:db8::2:1"},
#     298                 :          2 :         {"2001:db8:0:0:0::1", "2001:db8::1"},
#     299                 :          2 :         {"2001:db8:0:0:1:0:0:1", "2001:db8::1:0:0:1"},
#     300                 :          2 :         {"2001:db8:0:0:1::1", "2001:db8::1:0:0:1"},
#     301                 :          2 :         {"2001:DB8:0:0:1::1", "2001:db8::1:0:0:1"},
#     302                 :          2 :         {"2001:db8:0:0::1", "2001:db8::1"},
#     303                 :          2 :         {"2001:db8:0:0:aaaa::1", "2001:db8::aaaa:0:0:1"},
#     304                 :          2 :         {"2001:db8:0:1:1:1:1:1", "2001:db8:0:1:1:1:1:1"},
#     305                 :          2 :         {"2001:db8:0::1", "2001:db8::1"},
#     306                 :          2 :         {"2001:db8:85a3:0:0:8a2e:370:7334", "2001:db8:85a3::8a2e:370:7334"},
#     307                 :          2 :         {"2001:db8::0:1", "2001:db8::1"},
#     308                 :          2 :         {"2001:db8::0:1:0:0:1", "2001:db8::1:0:0:1"},
#     309                 :          2 :         {"2001:DB8::1", "2001:db8::1"},
#     310                 :          2 :         {"2001:db8::1", "2001:db8::1"},
#     311                 :          2 :         {"2001:db8::1:0:0:1", "2001:db8::1:0:0:1"},
#     312                 :          2 :         {"2001:db8::1:1:1:1:1", "2001:db8:0:1:1:1:1:1"},
#     313                 :          2 :         {"2001:db8::aaaa:0:0:1", "2001:db8::aaaa:0:0:1"},
#     314                 :          2 :         {"2001:db8:aaaa:bbbb:cccc:dddd:0:1", "2001:db8:aaaa:bbbb:cccc:dddd:0:1"},
#     315                 :          2 :         {"2001:db8:aaaa:bbbb:cccc:dddd::1", "2001:db8:aaaa:bbbb:cccc:dddd:0:1"},
#     316                 :          2 :         {"2001:db8:aaaa:bbbb:cccc:dddd:eeee:0001", "2001:db8:aaaa:bbbb:cccc:dddd:eeee:1"},
#     317                 :          2 :         {"2001:db8:aaaa:bbbb:cccc:dddd:eeee:001", "2001:db8:aaaa:bbbb:cccc:dddd:eeee:1"},
#     318                 :          2 :         {"2001:db8:aaaa:bbbb:cccc:dddd:eeee:01", "2001:db8:aaaa:bbbb:cccc:dddd:eeee:1"},
#     319                 :          2 :         {"2001:db8:aaaa:bbbb:cccc:dddd:eeee:1", "2001:db8:aaaa:bbbb:cccc:dddd:eeee:1"},
#     320                 :          2 :         {"2001:db8:aaaa:bbbb:cccc:dddd:eeee:aaaa", "2001:db8:aaaa:bbbb:cccc:dddd:eeee:aaaa"},
#     321                 :          2 :         {"2001:db8:aaaa:bbbb:cccc:dddd:eeee:AAAA", "2001:db8:aaaa:bbbb:cccc:dddd:eeee:aaaa"},
#     322                 :          2 :         {"2001:db8:aaaa:bbbb:cccc:dddd:eeee:AaAa", "2001:db8:aaaa:bbbb:cccc:dddd:eeee:aaaa"},
#     323                 :          2 :     };
#     324         [ +  + ]:         80 :     for (const auto& [input_address, expected_canonical_representation_output] : canonical_representations_ipv6) {
#     325                 :         80 :         CNetAddr net_addr;
#     326                 :         80 :         BOOST_REQUIRE(LookupHost(input_address, net_addr, false));
#     327                 :         80 :         BOOST_REQUIRE(net_addr.IsIPv6());
#     328                 :         80 :         BOOST_CHECK_EQUAL(net_addr.ToString(), expected_canonical_representation_output);
#     329                 :         80 :     }
#     330                 :          2 : }
#     331                 :            : 
#     332                 :            : BOOST_AUTO_TEST_CASE(cnetaddr_serialize_v1)
#     333                 :          2 : {
#     334                 :          2 :     CNetAddr addr;
#     335                 :          2 :     CDataStream s(SER_NETWORK, PROTOCOL_VERSION);
#     336                 :            : 
#     337                 :          2 :     s << addr;
#     338                 :          2 :     BOOST_CHECK_EQUAL(HexStr(s), "00000000000000000000000000000000");
#     339                 :          2 :     s.clear();
#     340                 :            : 
#     341                 :          2 :     BOOST_REQUIRE(LookupHost("1.2.3.4", addr, false));
#     342                 :          2 :     s << addr;
#     343                 :          2 :     BOOST_CHECK_EQUAL(HexStr(s), "00000000000000000000ffff01020304");
#     344                 :          2 :     s.clear();
#     345                 :            : 
#     346                 :          2 :     BOOST_REQUIRE(LookupHost("1a1b:2a2b:3a3b:4a4b:5a5b:6a6b:7a7b:8a8b", addr, false));
#     347                 :          2 :     s << addr;
#     348                 :          2 :     BOOST_CHECK_EQUAL(HexStr(s), "1a1b2a2b3a3b4a4b5a5b6a6b7a7b8a8b");
#     349                 :          2 :     s.clear();
#     350                 :            : 
#     351                 :            :     // TORv2, no longer supported
#     352                 :          2 :     BOOST_CHECK(!addr.SetSpecial("6hzph5hv6337r6p2.onion"));
#     353                 :            : 
#     354                 :          2 :     BOOST_REQUIRE(addr.SetSpecial("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion"));
#     355                 :          2 :     s << addr;
#     356                 :          2 :     BOOST_CHECK_EQUAL(HexStr(s), "00000000000000000000000000000000");
#     357                 :          2 :     s.clear();
#     358                 :            : 
#     359                 :          2 :     addr.SetInternal("a");
#     360                 :          2 :     s << addr;
#     361                 :          2 :     BOOST_CHECK_EQUAL(HexStr(s), "fd6b88c08724ca978112ca1bbdcafac2");
#     362                 :          2 :     s.clear();
#     363                 :          2 : }
#     364                 :            : 
#     365                 :            : BOOST_AUTO_TEST_CASE(cnetaddr_serialize_v2)
#     366                 :          2 : {
#     367                 :          2 :     CNetAddr addr;
#     368                 :          2 :     CDataStream s(SER_NETWORK, PROTOCOL_VERSION);
#     369                 :            :     // Add ADDRV2_FORMAT to the version so that the CNetAddr
#     370                 :            :     // serialize method produces an address in v2 format.
#     371                 :          2 :     s.SetVersion(s.GetVersion() | ADDRV2_FORMAT);
#     372                 :            : 
#     373                 :          2 :     s << addr;
#     374                 :          2 :     BOOST_CHECK_EQUAL(HexStr(s), "021000000000000000000000000000000000");
#     375                 :          2 :     s.clear();
#     376                 :            : 
#     377                 :          2 :     BOOST_REQUIRE(LookupHost("1.2.3.4", addr, false));
#     378                 :          2 :     s << addr;
#     379                 :          2 :     BOOST_CHECK_EQUAL(HexStr(s), "010401020304");
#     380                 :          2 :     s.clear();
#     381                 :            : 
#     382                 :          2 :     BOOST_REQUIRE(LookupHost("1a1b:2a2b:3a3b:4a4b:5a5b:6a6b:7a7b:8a8b", addr, false));
#     383                 :          2 :     s << addr;
#     384                 :          2 :     BOOST_CHECK_EQUAL(HexStr(s), "02101a1b2a2b3a3b4a4b5a5b6a6b7a7b8a8b");
#     385                 :          2 :     s.clear();
#     386                 :            : 
#     387                 :            :     // TORv2, no longer supported
#     388                 :          2 :     BOOST_CHECK(!addr.SetSpecial("6hzph5hv6337r6p2.onion"));
#     389                 :            : 
#     390                 :          2 :     BOOST_REQUIRE(addr.SetSpecial("kpgvmscirrdqpekbqjsvw5teanhatztpp2gl6eee4zkowvwfxwenqaid.onion"));
#     391                 :          2 :     s << addr;
#     392                 :          2 :     BOOST_CHECK_EQUAL(HexStr(s), "042053cd5648488c4707914182655b7664034e09e66f7e8cbf1084e654eb56c5bd88");
#     393                 :          2 :     s.clear();
#     394                 :            : 
#     395                 :          2 :     BOOST_REQUIRE(addr.SetInternal("a"));
#     396                 :          2 :     s << addr;
#     397                 :          2 :     BOOST_CHECK_EQUAL(HexStr(s), "0210fd6b88c08724ca978112ca1bbdcafac2");
#     398                 :          2 :     s.clear();
#     399                 :          2 : }
#     400                 :            : 
#     401                 :            : BOOST_AUTO_TEST_CASE(cnetaddr_unserialize_v2)
#     402                 :          2 : {
#     403                 :          2 :     CNetAddr addr;
#     404                 :          2 :     CDataStream s(SER_NETWORK, PROTOCOL_VERSION);
#     405                 :            :     // Add ADDRV2_FORMAT to the version so that the CNetAddr
#     406                 :            :     // unserialize method expects an address in v2 format.
#     407                 :          2 :     s.SetVersion(s.GetVersion() | ADDRV2_FORMAT);
#     408                 :            : 
#     409                 :            :     // Valid IPv4.
#     410                 :          2 :     s << Span{ParseHex("01"          // network type (IPv4)
#     411                 :          2 :                        "04"          // address length
#     412                 :          2 :                        "01020304")}; // address
#     413                 :          2 :     s >> addr;
#     414                 :          2 :     BOOST_CHECK(addr.IsValid());
#     415                 :          2 :     BOOST_CHECK(addr.IsIPv4());
#     416                 :          2 :     BOOST_CHECK(addr.IsAddrV1Compatible());
#     417                 :          2 :     BOOST_CHECK_EQUAL(addr.ToString(), "1.2.3.4");
#     418                 :          2 :     BOOST_REQUIRE(s.empty());
#     419                 :            : 
#     420                 :            :     // Invalid IPv4, valid length but address itself is shorter.
#     421                 :          2 :     s << Span{ParseHex("01"      // network type (IPv4)
#     422                 :          2 :                        "04"      // address length
#     423                 :          2 :                        "0102")}; // address
#     424                 :          2 :     BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure, HasReason("end of data"));
#     425                 :          2 :     BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
#     426                 :          2 :     s.clear();
#     427                 :            : 
#     428                 :            :     // Invalid IPv4, with bogus length.
#     429                 :          2 :     s << Span{ParseHex("01"          // network type (IPv4)
#     430                 :          2 :                        "05"          // address length
#     431                 :          2 :                        "01020304")}; // address
#     432                 :          2 :     BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure,
#     433                 :          2 :                           HasReason("BIP155 IPv4 address with length 5 (should be 4)"));
#     434                 :          2 :     BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
#     435                 :          2 :     s.clear();
#     436                 :            : 
#     437                 :            :     // Invalid IPv4, with extreme length.
#     438                 :          2 :     s << Span{ParseHex("01"          // network type (IPv4)
#     439                 :          2 :                        "fd0102"      // address length (513 as CompactSize)
#     440                 :          2 :                        "01020304")}; // address
#     441                 :          2 :     BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure,
#     442                 :          2 :                           HasReason("Address too long: 513 > 512"));
#     443                 :          2 :     BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
#     444                 :          2 :     s.clear();
#     445                 :            : 
#     446                 :            :     // Valid IPv6.
#     447                 :          2 :     s << Span{ParseHex("02"                                  // network type (IPv6)
#     448                 :          2 :                        "10"                                  // address length
#     449                 :          2 :                        "0102030405060708090a0b0c0d0e0f10")}; // address
#     450                 :          2 :     s >> addr;
#     451                 :          2 :     BOOST_CHECK(addr.IsValid());
#     452                 :          2 :     BOOST_CHECK(addr.IsIPv6());
#     453                 :          2 :     BOOST_CHECK(addr.IsAddrV1Compatible());
#     454                 :          2 :     BOOST_CHECK_EQUAL(addr.ToString(), "102:304:506:708:90a:b0c:d0e:f10");
#     455                 :          2 :     BOOST_REQUIRE(s.empty());
#     456                 :            : 
#     457                 :            :     // Valid IPv6, contains embedded "internal".
#     458                 :          2 :     s << Span{ParseHex(
#     459                 :          2 :         "02"                                  // network type (IPv6)
#     460                 :          2 :         "10"                                  // address length
#     461                 :          2 :         "fd6b88c08724ca978112ca1bbdcafac2")}; // address: 0xfd + sha256("bitcoin")[0:5] +
#     462                 :            :                                               // sha256(name)[0:10]
#     463                 :          2 :     s >> addr;
#     464                 :          2 :     BOOST_CHECK(addr.IsInternal());
#     465                 :          2 :     BOOST_CHECK(addr.IsAddrV1Compatible());
#     466                 :          2 :     BOOST_CHECK_EQUAL(addr.ToString(), "zklycewkdo64v6wc.internal");
#     467                 :          2 :     BOOST_REQUIRE(s.empty());
#     468                 :            : 
#     469                 :            :     // Invalid IPv6, with bogus length.
#     470                 :          2 :     s << Span{ParseHex("02"    // network type (IPv6)
#     471                 :          2 :                        "04"    // address length
#     472                 :          2 :                        "00")}; // address
#     473                 :          2 :     BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure,
#     474                 :          2 :                           HasReason("BIP155 IPv6 address with length 4 (should be 16)"));
#     475                 :          2 :     BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
#     476                 :          2 :     s.clear();
#     477                 :            : 
#     478                 :            :     // Invalid IPv6, contains embedded IPv4.
#     479                 :          2 :     s << Span{ParseHex("02"                                  // network type (IPv6)
#     480                 :          2 :                        "10"                                  // address length
#     481                 :          2 :                        "00000000000000000000ffff01020304")}; // address
#     482                 :          2 :     s >> addr;
#     483                 :          2 :     BOOST_CHECK(!addr.IsValid());
#     484                 :          2 :     BOOST_REQUIRE(s.empty());
#     485                 :            : 
#     486                 :            :     // Invalid IPv6, contains embedded TORv2.
#     487                 :          2 :     s << Span{ParseHex("02"                                  // network type (IPv6)
#     488                 :          2 :                        "10"                                  // address length
#     489                 :          2 :                        "fd87d87eeb430102030405060708090a")}; // address
#     490                 :          2 :     s >> addr;
#     491                 :          2 :     BOOST_CHECK(!addr.IsValid());
#     492                 :          2 :     BOOST_REQUIRE(s.empty());
#     493                 :            : 
#     494                 :            :     // TORv2, no longer supported.
#     495                 :          2 :     s << Span{ParseHex("03"                      // network type (TORv2)
#     496                 :          2 :                        "0a"                      // address length
#     497                 :          2 :                        "f1f2f3f4f5f6f7f8f9fa")}; // address
#     498                 :          2 :     s >> addr;
#     499                 :          2 :     BOOST_CHECK(!addr.IsValid());
#     500                 :          2 :     BOOST_REQUIRE(s.empty());
#     501                 :            : 
#     502                 :            :     // Valid TORv3.
#     503                 :          2 :     s << Span{ParseHex("04"                               // network type (TORv3)
#     504                 :          2 :                        "20"                               // address length
#     505                 :          2 :                        "79bcc625184b05194975c28b66b66b04" // address
#     506                 :          2 :                        "69f7f6556fb1ac3189a79b40dda32f1f"
#     507                 :          2 :                        )};
#     508                 :          2 :     s >> addr;
#     509                 :          2 :     BOOST_CHECK(addr.IsValid());
#     510                 :          2 :     BOOST_CHECK(addr.IsTor());
#     511                 :          2 :     BOOST_CHECK(!addr.IsAddrV1Compatible());
#     512                 :          2 :     BOOST_CHECK_EQUAL(addr.ToString(),
#     513                 :          2 :                       "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion");
#     514                 :          2 :     BOOST_REQUIRE(s.empty());
#     515                 :            : 
#     516                 :            :     // Invalid TORv3, with bogus length.
#     517                 :          2 :     s << Span{ParseHex("04" // network type (TORv3)
#     518                 :          2 :                        "00" // address length
#     519                 :          2 :                        "00" // address
#     520                 :          2 :                        )};
#     521                 :          2 :     BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure,
#     522                 :          2 :                           HasReason("BIP155 TORv3 address with length 0 (should be 32)"));
#     523                 :          2 :     BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
#     524                 :          2 :     s.clear();
#     525                 :            : 
#     526                 :            :     // Valid I2P.
#     527                 :          2 :     s << Span{ParseHex("05"                               // network type (I2P)
#     528                 :          2 :                        "20"                               // address length
#     529                 :          2 :                        "a2894dabaec08c0051a481a6dac88b64" // address
#     530                 :          2 :                        "f98232ae42d4b6fd2fa81952dfe36a87")};
#     531                 :          2 :     s >> addr;
#     532                 :          2 :     BOOST_CHECK(addr.IsValid());
#     533                 :          2 :     BOOST_CHECK(addr.IsI2P());
#     534                 :          2 :     BOOST_CHECK(!addr.IsAddrV1Compatible());
#     535                 :          2 :     BOOST_CHECK_EQUAL(addr.ToString(),
#     536                 :          2 :                       "ukeu3k5oycgaauneqgtnvselmt4yemvoilkln7jpvamvfx7dnkdq.b32.i2p");
#     537                 :          2 :     BOOST_REQUIRE(s.empty());
#     538                 :            : 
#     539                 :            :     // Invalid I2P, with bogus length.
#     540                 :          2 :     s << Span{ParseHex("05" // network type (I2P)
#     541                 :          2 :                        "03" // address length
#     542                 :          2 :                        "00" // address
#     543                 :          2 :                        )};
#     544                 :          2 :     BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure,
#     545                 :          2 :                           HasReason("BIP155 I2P address with length 3 (should be 32)"));
#     546                 :          2 :     BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
#     547                 :          2 :     s.clear();
#     548                 :            : 
#     549                 :            :     // Valid CJDNS.
#     550                 :          2 :     s << Span{ParseHex("06"                               // network type (CJDNS)
#     551                 :          2 :                        "10"                               // address length
#     552                 :          2 :                        "fc000001000200030004000500060007" // address
#     553                 :          2 :                        )};
#     554                 :          2 :     s >> addr;
#     555                 :          2 :     BOOST_CHECK(addr.IsValid());
#     556                 :          2 :     BOOST_CHECK(addr.IsCJDNS());
#     557                 :          2 :     BOOST_CHECK(!addr.IsAddrV1Compatible());
#     558                 :          2 :     BOOST_CHECK_EQUAL(addr.ToString(), "fc00:1:2:3:4:5:6:7");
#     559                 :          2 :     BOOST_REQUIRE(s.empty());
#     560                 :            : 
#     561                 :            :     // Invalid CJDNS, wrong prefix.
#     562                 :          2 :     s << Span{ParseHex("06"                               // network type (CJDNS)
#     563                 :          2 :                        "10"                               // address length
#     564                 :          2 :                        "aa000001000200030004000500060007" // address
#     565                 :          2 :                        )};
#     566                 :          2 :     s >> addr;
#     567                 :          2 :     BOOST_CHECK(addr.IsCJDNS());
#     568                 :          2 :     BOOST_CHECK(!addr.IsValid());
#     569                 :          2 :     BOOST_REQUIRE(s.empty());
#     570                 :            : 
#     571                 :            :     // Invalid CJDNS, with bogus length.
#     572                 :          2 :     s << Span{ParseHex("06" // network type (CJDNS)
#     573                 :          2 :                        "01" // address length
#     574                 :          2 :                        "00" // address
#     575                 :          2 :                        )};
#     576                 :          2 :     BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure,
#     577                 :          2 :                           HasReason("BIP155 CJDNS address with length 1 (should be 16)"));
#     578                 :          2 :     BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
#     579                 :          2 :     s.clear();
#     580                 :            : 
#     581                 :            :     // Unknown, with extreme length.
#     582                 :          2 :     s << Span{ParseHex("aa"             // network type (unknown)
#     583                 :          2 :                        "fe00000002"     // address length (CompactSize's MAX_SIZE)
#     584                 :          2 :                        "01020304050607" // address
#     585                 :          2 :                        )};
#     586                 :          2 :     BOOST_CHECK_EXCEPTION(s >> addr, std::ios_base::failure,
#     587                 :          2 :                           HasReason("Address too long: 33554432 > 512"));
#     588                 :          2 :     BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
#     589                 :          2 :     s.clear();
#     590                 :            : 
#     591                 :            :     // Unknown, with reasonable length.
#     592                 :          2 :     s << Span{ParseHex("aa"       // network type (unknown)
#     593                 :          2 :                        "04"       // address length
#     594                 :          2 :                        "01020304" // address
#     595                 :          2 :                        )};
#     596                 :          2 :     s >> addr;
#     597                 :          2 :     BOOST_CHECK(!addr.IsValid());
#     598                 :          2 :     BOOST_REQUIRE(s.empty());
#     599                 :            : 
#     600                 :            :     // Unknown, with zero length.
#     601                 :          2 :     s << Span{ParseHex("aa" // network type (unknown)
#     602                 :          2 :                        "00" // address length
#     603                 :          2 :                        ""   // address
#     604                 :          2 :                        )};
#     605                 :          2 :     s >> addr;
#     606                 :          2 :     BOOST_CHECK(!addr.IsValid());
#     607                 :          2 :     BOOST_REQUIRE(s.empty());
#     608                 :          2 : }
#     609                 :            : 
#     610                 :            : // prior to PR #14728, this test triggers an undefined behavior
#     611                 :            : BOOST_AUTO_TEST_CASE(ipv4_peer_with_ipv6_addrMe_test)
#     612                 :          2 : {
#     613                 :            :     // set up local addresses; all that's necessary to reproduce the bug is
#     614                 :            :     // that a normal IPv4 address is among the entries, but if this address is
#     615                 :            :     // !IsRoutable the undefined behavior is easier to trigger deterministically
#     616                 :          2 :     in_addr raw_addr;
#     617                 :          2 :     raw_addr.s_addr = htonl(0x7f000001);
#     618                 :          2 :     const CNetAddr mapLocalHost_entry = CNetAddr(raw_addr);
#     619                 :          2 :     {
#     620                 :          2 :         LOCK(g_maplocalhost_mutex);
#     621                 :          2 :         LocalServiceInfo lsi;
#     622                 :          2 :         lsi.nScore = 23;
#     623                 :          2 :         lsi.nPort = 42;
#     624                 :          2 :         mapLocalHost[mapLocalHost_entry] = lsi;
#     625                 :          2 :     }
#     626                 :            : 
#     627                 :            :     // create a peer with an IPv4 address
#     628                 :          2 :     in_addr ipv4AddrPeer;
#     629                 :          2 :     ipv4AddrPeer.s_addr = 0xa0b0c001;
#     630                 :          2 :     CAddress addr = CAddress(CService(ipv4AddrPeer, 7777), NODE_NETWORK);
#     631                 :          2 :     std::unique_ptr<CNode> pnode = std::make_unique<CNode>(/*id=*/0,
#     632                 :          2 :                                                            NODE_NETWORK,
#     633                 :          2 :                                                            /*sock=*/nullptr,
#     634                 :          2 :                                                            addr,
#     635                 :          2 :                                                            /*nKeyedNetGroupIn=*/0,
#     636                 :          2 :                                                            /*nLocalHostNonceIn=*/0,
#     637                 :          2 :                                                            CAddress{},
#     638                 :          2 :                                                            /*pszDest=*/std::string{},
#     639                 :          2 :                                                            ConnectionType::OUTBOUND_FULL_RELAY,
#     640                 :          2 :                                                            /*inbound_onion=*/false);
#     641                 :          2 :     pnode->fSuccessfullyConnected.store(true);
#     642                 :            : 
#     643                 :            :     // the peer claims to be reaching us via IPv6
#     644                 :          2 :     in6_addr ipv6AddrLocal;
#     645                 :          2 :     memset(ipv6AddrLocal.s6_addr, 0, 16);
#     646                 :          2 :     ipv6AddrLocal.s6_addr[0] = 0xcc;
#     647                 :          2 :     CAddress addrLocal = CAddress(CService(ipv6AddrLocal, 7777), NODE_NETWORK);
#     648                 :          2 :     pnode->SetAddrLocal(addrLocal);
#     649                 :            : 
#     650                 :            :     // before patch, this causes undefined behavior detectable with clang's -fsanitize=memory
#     651                 :          2 :     GetLocalAddrForPeer(&*pnode);
#     652                 :            : 
#     653                 :            :     // suppress no-checks-run warning; if this test fails, it's by triggering a sanitizer
#     654                 :          2 :     BOOST_CHECK(1);
#     655                 :            : 
#     656                 :            :     // Cleanup, so that we don't confuse other tests.
#     657                 :          2 :     {
#     658                 :          2 :         LOCK(g_maplocalhost_mutex);
#     659                 :          2 :         mapLocalHost.erase(mapLocalHost_entry);
#     660                 :          2 :     }
#     661                 :          2 : }
#     662                 :            : 
#     663                 :            : BOOST_AUTO_TEST_CASE(get_local_addr_for_peer_port)
#     664                 :          2 : {
#     665                 :            :     // Test that GetLocalAddrForPeer() properly selects the address to self-advertise:
#     666                 :            :     //
#     667                 :            :     // 1. GetLocalAddrForPeer() calls GetLocalAddress() which returns an address that is
#     668                 :            :     //    not routable.
#     669                 :            :     // 2. GetLocalAddrForPeer() overrides the address with whatever the peer has told us
#     670                 :            :     //    he sees us as.
#     671                 :            :     // 2.1. For inbound connections we must override both the address and the port.
#     672                 :            :     // 2.2. For outbound connections we must override only the address.
#     673                 :            : 
#     674                 :            :     // Pretend that we bound to this port.
#     675                 :          2 :     const uint16_t bind_port = 20001;
#     676                 :          2 :     m_node.args->ForceSetArg("-bind", strprintf("3.4.5.6:%u", bind_port));
#     677                 :            : 
#     678                 :            :     // Our address:port as seen from the peer, completely different from the above.
#     679                 :          2 :     in_addr peer_us_addr;
#     680                 :          2 :     peer_us_addr.s_addr = htonl(0x02030405);
#     681                 :          2 :     const CAddress peer_us{CService{peer_us_addr, 20002}, NODE_NETWORK};
#     682                 :            : 
#     683                 :            :     // Create a peer with a routable IPv4 address (outbound).
#     684                 :          2 :     in_addr peer_out_in_addr;
#     685                 :          2 :     peer_out_in_addr.s_addr = htonl(0x01020304);
#     686                 :          2 :     CNode peer_out{/*id=*/0,
#     687                 :          2 :                    /*nLocalServicesIn=*/NODE_NETWORK,
#     688                 :          2 :                    /*sock=*/nullptr,
#     689                 :          2 :                    /*addrIn=*/CAddress{CService{peer_out_in_addr, 8333}, NODE_NETWORK},
#     690                 :          2 :                    /*nKeyedNetGroupIn=*/0,
#     691                 :          2 :                    /*nLocalHostNonceIn=*/0,
#     692                 :          2 :                    /*addrBindIn=*/CAddress{},
#     693                 :          2 :                    /*addrNameIn=*/std::string{},
#     694                 :          2 :                    /*conn_type_in=*/ConnectionType::OUTBOUND_FULL_RELAY,
#     695                 :          2 :                    /*inbound_onion=*/false};
#     696                 :          2 :     peer_out.fSuccessfullyConnected = true;
#     697                 :          2 :     peer_out.SetAddrLocal(peer_us);
#     698                 :            : 
#     699                 :            :     // Without the fix peer_us:8333 is chosen instead of the proper peer_us:bind_port.
#     700                 :          2 :     auto chosen_local_addr = GetLocalAddrForPeer(&peer_out);
#     701                 :          2 :     BOOST_REQUIRE(chosen_local_addr);
#     702                 :          2 :     const CService expected{peer_us_addr, bind_port};
#     703                 :          2 :     BOOST_CHECK(*chosen_local_addr == expected);
#     704                 :            : 
#     705                 :            :     // Create a peer with a routable IPv4 address (inbound).
#     706                 :          2 :     in_addr peer_in_in_addr;
#     707                 :          2 :     peer_in_in_addr.s_addr = htonl(0x05060708);
#     708                 :          2 :     CNode peer_in{/*id=*/0,
#     709                 :          2 :                   /*nLocalServicesIn=*/NODE_NETWORK,
#     710                 :          2 :                   /*sock=*/nullptr,
#     711                 :          2 :                   /*addrIn=*/CAddress{CService{peer_in_in_addr, 8333}, NODE_NETWORK},
#     712                 :          2 :                   /*nKeyedNetGroupIn=*/0,
#     713                 :          2 :                   /*nLocalHostNonceIn=*/0,
#     714                 :          2 :                   /*addrBindIn=*/CAddress{},
#     715                 :          2 :                   /*addrNameIn=*/std::string{},
#     716                 :          2 :                   /*conn_type_in=*/ConnectionType::INBOUND,
#     717                 :          2 :                   /*inbound_onion=*/false};
#     718                 :          2 :     peer_in.fSuccessfullyConnected = true;
#     719                 :          2 :     peer_in.SetAddrLocal(peer_us);
#     720                 :            : 
#     721                 :            :     // Without the fix peer_us:8333 is chosen instead of the proper peer_us:peer_us.GetPort().
#     722                 :          2 :     chosen_local_addr = GetLocalAddrForPeer(&peer_in);
#     723                 :          2 :     BOOST_REQUIRE(chosen_local_addr);
#     724                 :          2 :     BOOST_CHECK(*chosen_local_addr == peer_us);
#     725                 :            : 
#     726                 :          2 :     m_node.args->ForceSetArg("-bind", "");
#     727                 :          2 : }
#     728                 :            : 
#     729                 :            : BOOST_AUTO_TEST_CASE(LimitedAndReachable_Network)
#     730                 :          2 : {
#     731                 :          2 :     BOOST_CHECK(IsReachable(NET_IPV4));
#     732                 :          2 :     BOOST_CHECK(IsReachable(NET_IPV6));
#     733                 :          2 :     BOOST_CHECK(IsReachable(NET_ONION));
#     734                 :          2 :     BOOST_CHECK(IsReachable(NET_I2P));
#     735                 :          2 :     BOOST_CHECK(IsReachable(NET_CJDNS));
#     736                 :            : 
#     737                 :          2 :     SetReachable(NET_IPV4, false);
#     738                 :          2 :     SetReachable(NET_IPV6, false);
#     739                 :          2 :     SetReachable(NET_ONION, false);
#     740                 :          2 :     SetReachable(NET_I2P, false);
#     741                 :          2 :     SetReachable(NET_CJDNS, false);
#     742                 :            : 
#     743                 :          2 :     BOOST_CHECK(!IsReachable(NET_IPV4));
#     744                 :          2 :     BOOST_CHECK(!IsReachable(NET_IPV6));
#     745                 :          2 :     BOOST_CHECK(!IsReachable(NET_ONION));
#     746                 :          2 :     BOOST_CHECK(!IsReachable(NET_I2P));
#     747                 :          2 :     BOOST_CHECK(!IsReachable(NET_CJDNS));
#     748                 :            : 
#     749                 :          2 :     SetReachable(NET_IPV4, true);
#     750                 :          2 :     SetReachable(NET_IPV6, true);
#     751                 :          2 :     SetReachable(NET_ONION, true);
#     752                 :          2 :     SetReachable(NET_I2P, true);
#     753                 :          2 :     SetReachable(NET_CJDNS, true);
#     754                 :            : 
#     755                 :          2 :     BOOST_CHECK(IsReachable(NET_IPV4));
#     756                 :          2 :     BOOST_CHECK(IsReachable(NET_IPV6));
#     757                 :          2 :     BOOST_CHECK(IsReachable(NET_ONION));
#     758                 :          2 :     BOOST_CHECK(IsReachable(NET_I2P));
#     759                 :          2 :     BOOST_CHECK(IsReachable(NET_CJDNS));
#     760                 :          2 : }
#     761                 :            : 
#     762                 :            : BOOST_AUTO_TEST_CASE(LimitedAndReachable_NetworkCaseUnroutableAndInternal)
#     763                 :          2 : {
#     764                 :          2 :     BOOST_CHECK(IsReachable(NET_UNROUTABLE));
#     765                 :          2 :     BOOST_CHECK(IsReachable(NET_INTERNAL));
#     766                 :            : 
#     767                 :          2 :     SetReachable(NET_UNROUTABLE, false);
#     768                 :          2 :     SetReachable(NET_INTERNAL, false);
#     769                 :            : 
#     770                 :          2 :     BOOST_CHECK(IsReachable(NET_UNROUTABLE)); // Ignored for both networks
#     771                 :          2 :     BOOST_CHECK(IsReachable(NET_INTERNAL));
#     772                 :          2 : }
#     773                 :            : 
#     774                 :            : CNetAddr UtilBuildAddress(unsigned char p1, unsigned char p2, unsigned char p3, unsigned char p4)
#     775                 :          4 : {
#     776                 :          4 :     unsigned char ip[] = {p1, p2, p3, p4};
#     777                 :            : 
#     778                 :          4 :     struct sockaddr_in sa;
#     779                 :          4 :     memset(&sa, 0, sizeof(sockaddr_in)); // initialize the memory block
#     780                 :          4 :     memcpy(&(sa.sin_addr), &ip, sizeof(ip));
#     781                 :          4 :     return CNetAddr(sa.sin_addr);
#     782                 :          4 : }
#     783                 :            : 
#     784                 :            : 
#     785                 :            : BOOST_AUTO_TEST_CASE(LimitedAndReachable_CNetAddr)
#     786                 :          2 : {
#     787                 :          2 :     CNetAddr addr = UtilBuildAddress(0x001, 0x001, 0x001, 0x001); // 1.1.1.1
#     788                 :            : 
#     789                 :          2 :     SetReachable(NET_IPV4, true);
#     790                 :          2 :     BOOST_CHECK(IsReachable(addr));
#     791                 :            : 
#     792                 :          2 :     SetReachable(NET_IPV4, false);
#     793                 :          2 :     BOOST_CHECK(!IsReachable(addr));
#     794                 :            : 
#     795                 :          2 :     SetReachable(NET_IPV4, true); // have to reset this, because this is stateful.
#     796                 :          2 : }
#     797                 :            : 
#     798                 :            : 
#     799                 :            : BOOST_AUTO_TEST_CASE(LocalAddress_BasicLifecycle)
#     800                 :          2 : {
#     801                 :          2 :     CService addr = CService(UtilBuildAddress(0x002, 0x001, 0x001, 0x001), 1000); // 2.1.1.1:1000
#     802                 :            : 
#     803                 :          2 :     SetReachable(NET_IPV4, true);
#     804                 :            : 
#     805                 :          2 :     BOOST_CHECK(!IsLocal(addr));
#     806                 :          2 :     BOOST_CHECK(AddLocal(addr, 1000));
#     807                 :          2 :     BOOST_CHECK(IsLocal(addr));
#     808                 :            : 
#     809                 :          2 :     RemoveLocal(addr);
#     810                 :          2 :     BOOST_CHECK(!IsLocal(addr));
#     811                 :          2 : }
#     812                 :            : 
#     813                 :            : BOOST_AUTO_TEST_CASE(initial_advertise_from_version_message)
#     814                 :          2 : {
#     815                 :            :     // Tests the following scenario:
#     816                 :            :     // * -bind=3.4.5.6:20001 is specified
#     817                 :            :     // * we make an outbound connection to a peer
#     818                 :            :     // * the peer reports he sees us as 2.3.4.5:20002 in the version message
#     819                 :            :     //   (20002 is a random port assigned by our OS for the outgoing TCP connection,
#     820                 :            :     //   we cannot accept connections to it)
#     821                 :            :     // * we should self-advertise to that peer as 2.3.4.5:20001
#     822                 :            : 
#     823                 :            :     // Pretend that we bound to this port.
#     824                 :          2 :     const uint16_t bind_port = 20001;
#     825                 :          2 :     m_node.args->ForceSetArg("-bind", strprintf("3.4.5.6:%u", bind_port));
#     826                 :          2 :     m_node.args->ForceSetArg("-capturemessages", "1");
#     827                 :            : 
#     828                 :            :     // Our address:port as seen from the peer - 2.3.4.5:20002 (different from the above).
#     829                 :          2 :     in_addr peer_us_addr;
#     830                 :          2 :     peer_us_addr.s_addr = htonl(0x02030405);
#     831                 :          2 :     const CService peer_us{peer_us_addr, 20002};
#     832                 :            : 
#     833                 :            :     // Create a peer with a routable IPv4 address.
#     834                 :          2 :     in_addr peer_in_addr;
#     835                 :          2 :     peer_in_addr.s_addr = htonl(0x01020304);
#     836                 :          2 :     CNode peer{/*id=*/0,
#     837                 :          2 :                /*nLocalServicesIn=*/NODE_NETWORK,
#     838                 :          2 :                /*sock=*/nullptr,
#     839                 :          2 :                /*addrIn=*/CAddress{CService{peer_in_addr, 8333}, NODE_NETWORK},
#     840                 :          2 :                /*nKeyedNetGroupIn=*/0,
#     841                 :          2 :                /*nLocalHostNonceIn=*/0,
#     842                 :          2 :                /*addrBindIn=*/CAddress{},
#     843                 :          2 :                /*addrNameIn=*/std::string{},
#     844                 :          2 :                /*conn_type_in=*/ConnectionType::OUTBOUND_FULL_RELAY,
#     845                 :          2 :                /*inbound_onion=*/false};
#     846                 :            : 
#     847                 :          2 :     const uint64_t services{NODE_NETWORK | NODE_WITNESS};
#     848                 :          2 :     const int64_t time{0};
#     849                 :          2 :     const CNetMsgMaker msg_maker{PROTOCOL_VERSION};
#     850                 :            : 
#     851                 :            :     // Force CChainState::IsInitialBlockDownload() to return false.
#     852                 :            :     // Otherwise PushAddress() isn't called by PeerManager::ProcessMessage().
#     853                 :          2 :     TestChainState& chainstate =
#     854                 :          2 :         *static_cast<TestChainState*>(&m_node.chainman->ActiveChainstate());
#     855                 :          2 :     chainstate.JumpOutOfIbd();
#     856                 :            : 
#     857                 :          2 :     m_node.peerman->InitializeNode(&peer);
#     858                 :            : 
#     859                 :          2 :     std::atomic<bool> interrupt_dummy{false};
#     860                 :          2 :     std::chrono::microseconds time_received_dummy{0};
#     861                 :            : 
#     862                 :          2 :     const auto msg_version =
#     863                 :          2 :         msg_maker.Make(NetMsgType::VERSION, PROTOCOL_VERSION, services, time, services, peer_us);
#     864                 :          2 :     CDataStream msg_version_stream{msg_version.data, SER_NETWORK, PROTOCOL_VERSION};
#     865                 :            : 
#     866                 :          2 :     m_node.peerman->ProcessMessage(
#     867                 :          2 :         peer, NetMsgType::VERSION, msg_version_stream, time_received_dummy, interrupt_dummy);
#     868                 :            : 
#     869                 :          2 :     const auto msg_verack = msg_maker.Make(NetMsgType::VERACK);
#     870                 :          2 :     CDataStream msg_verack_stream{msg_verack.data, SER_NETWORK, PROTOCOL_VERSION};
#     871                 :            : 
#     872                 :            :     // Will set peer.fSuccessfullyConnected to true (necessary in SendMessages()).
#     873                 :          2 :     m_node.peerman->ProcessMessage(
#     874                 :          2 :         peer, NetMsgType::VERACK, msg_verack_stream, time_received_dummy, interrupt_dummy);
#     875                 :            : 
#     876                 :            :     // Ensure that peer_us_addr:bind_port is sent to the peer.
#     877                 :          2 :     const CService expected{peer_us_addr, bind_port};
#     878                 :          2 :     bool sent{false};
#     879                 :            : 
#     880                 :          2 :     const auto CaptureMessageOrig = CaptureMessage;
#     881                 :          2 :     CaptureMessage = [&sent, &expected](const CAddress& addr,
#     882                 :          2 :                                         const std::string& msg_type,
#     883                 :          2 :                                         Span<const unsigned char> data,
#     884                 :          8 :                                         bool is_incoming) -> void {
#     885 [ +  - ][ +  + ]:          8 :         if (!is_incoming && msg_type == "addr") {
#     886                 :          2 :             CDataStream s(data, SER_NETWORK, PROTOCOL_VERSION);
#     887                 :          2 :             std::vector<CAddress> addresses;
#     888                 :            : 
#     889                 :          2 :             s >> addresses;
#     890                 :            : 
#     891         [ +  - ]:          2 :             for (const auto& addr : addresses) {
#     892         [ +  - ]:          2 :                 if (addr == expected) {
#     893                 :          2 :                     sent = true;
#     894                 :          2 :                     return;
#     895                 :          2 :                 }
#     896                 :          2 :             }
#     897                 :          2 :         }
#     898                 :          8 :     };
#     899                 :            : 
#     900                 :          2 :     {
#     901                 :          2 :         LOCK(peer.cs_sendProcessing);
#     902                 :          2 :         m_node.peerman->SendMessages(&peer);
#     903                 :          2 :     }
#     904                 :            : 
#     905                 :          2 :     BOOST_CHECK(sent);
#     906                 :            : 
#     907                 :          2 :     CaptureMessage = CaptureMessageOrig;
#     908                 :          2 :     chainstate.ResetIbd();
#     909                 :          2 :     m_node.args->ForceSetArg("-capturemessages", "0");
#     910                 :          2 :     m_node.args->ForceSetArg("-bind", "");
#     911                 :            :     // PeerManager::ProcessMessage() calls AddTimeData() which changes the internal state
#     912                 :            :     // in timedata.cpp and later confuses the test "timedata_tests/addtimedata". Thus reset
#     913                 :            :     // that state as it was before our test was run.
#     914                 :          2 :     TestOnlyResetTimeData();
#     915                 :          2 : }
#     916                 :            : 
#     917                 :            : BOOST_AUTO_TEST_SUITE_END()

Generated by: LCOV version 0-eol-96201-ge66f56f4af6a