LCOV - code coverage report
Current view: top level - src/test - net_tests.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 99.1 % 1112 1102
Test Date: 2026-11-27 06:02:19 Functions: 100.0 % 69 69
Branches: 50.3 % 5102 2567

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2012-present 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 <addrman.h>
       6                 :             : #include <bip324.h>
       7                 :             : #include <chainparams.h>
       8                 :             : #include <clientversion.h>
       9                 :             : #include <common/args.h>
      10                 :             : #include <compat/compat.h>
      11                 :             : #include <net.h>
      12                 :             : #include <net_processing.h>
      13                 :             : #include <netaddress.h>
      14                 :             : #include <netbase.h>
      15                 :             : #include <netmessagemaker.h>
      16                 :             : #include <node/protocol_version.h>
      17                 :             : #include <serialize.h>
      18                 :             : #include <span.h>
      19                 :             : #include <streams.h>
      20                 :             : #include <test/util/common.h>
      21                 :             : #include <test/util/net.h>
      22                 :             : #include <test/util/random.h>
      23                 :             : #include <test/util/setup_common.h>
      24                 :             : #include <test/util/validation.h>
      25                 :             : #include <util/strencodings.h>
      26                 :             : #include <util/string.h>
      27                 :             : #include <validation.h>
      28                 :             : 
      29                 :             : #include <boost/test/unit_test.hpp>
      30                 :             : 
      31                 :             : #include <algorithm>
      32                 :             : #include <cstdint>
      33                 :             : #include <ios>
      34                 :             : #include <memory>
      35                 :             : #include <optional>
      36                 :             : #include <string>
      37                 :             : 
      38                 :             : using namespace std::literals;
      39                 :             : using namespace util::hex_literals;
      40                 :             : using util::ToString;
      41                 :             : 
      42                 :             : BOOST_FIXTURE_TEST_SUITE(net_tests, RegTestingSetup)
      43                 :             : 
      44   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(cnode_listen_port)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
      45                 :             : {
      46                 :             :     // test default
      47                 :           1 :     uint16_t port{GetListenPort()};
      48   [ +  -  +  - ]:           2 :     BOOST_CHECK(port == Params().GetDefaultPort());
      49                 :             :     // test set port
      50                 :           1 :     uint16_t altPort = 12345;
      51   [ +  -  +  -  :           2 :     BOOST_CHECK(gArgs.SoftSetArg("-port", ToString(altPort)));
             +  -  +  - ]
      52                 :           1 :     port = GetListenPort();
      53         [ +  - ]:           2 :     BOOST_CHECK(port == altPort);
      54                 :           1 : }
      55                 :             : 
      56   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(cnode_simple_test)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
      57                 :             : {
      58                 :           1 :     NodeId id = 0;
      59                 :             : 
      60                 :           1 :     in_addr ipv4Addr;
      61                 :           1 :     ipv4Addr.s_addr = 0xa0b0c001;
      62                 :             : 
      63                 :           1 :     CAddress addr = CAddress(CService(ipv4Addr, 7777), NODE_NETWORK);
      64         [ +  - ]:           1 :     std::string pszDest;
      65                 :             : 
      66                 :           2 :     std::unique_ptr<CNode> pnode1 = std::make_unique<CNode>(id++,
      67                 :           1 :                                                             /*sock=*/nullptr,
      68                 :             :                                                             addr,
      69                 :           1 :                                                             /*nKeyedNetGroupIn=*/0,
      70                 :           1 :                                                             /*nLocalHostNonceIn=*/0,
      71                 :           0 :                                                             CAddress(),
      72                 :             :                                                             pszDest,
      73                 :           1 :                                                             ConnectionType::OUTBOUND_FULL_RELAY,
      74                 :           1 :                                                             /*inbound_onion=*/false,
      75   [ +  -  +  - ]:           1 :                                                             /*network_key=*/0);
      76   [ +  -  +  -  :           2 :     BOOST_CHECK(pnode1->IsFullOutboundConn() == true);
                   +  - ]
      77   [ +  -  +  -  :           2 :     BOOST_CHECK(pnode1->IsManualConn() == false);
                   +  - ]
      78   [ +  -  +  -  :           2 :     BOOST_CHECK(pnode1->IsBlockOnlyConn() == false);
                   +  - ]
      79   [ +  -  +  -  :           2 :     BOOST_CHECK(pnode1->IsFeelerConn() == false);
                   +  - ]
      80   [ +  -  +  -  :           2 :     BOOST_CHECK(pnode1->IsAddrFetchConn() == false);
                   +  - ]
      81   [ +  -  +  -  :           2 :     BOOST_CHECK(pnode1->IsInboundConn() == false);
                   +  - ]
      82   [ +  -  +  -  :           2 :     BOOST_CHECK(pnode1->m_inbound_onion == false);
                   +  - ]
      83   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(pnode1->ConnectedThroughNetwork(), Network::NET_IPV4);
             +  -  +  - ]
      84                 :             : 
      85                 :           2 :     std::unique_ptr<CNode> pnode2 = std::make_unique<CNode>(id++,
      86                 :           1 :                                                             /*sock=*/nullptr,
      87                 :             :                                                             addr,
      88                 :           1 :                                                             /*nKeyedNetGroupIn=*/1,
      89                 :           1 :                                                             /*nLocalHostNonceIn=*/1,
      90                 :           0 :                                                             CAddress(),
      91                 :             :                                                             pszDest,
      92                 :           1 :                                                             ConnectionType::INBOUND,
      93                 :           1 :                                                             /*inbound_onion=*/false,
      94   [ +  -  +  - ]:           1 :                                                             /*network_key=*/1);
      95   [ +  -  +  -  :           2 :     BOOST_CHECK(pnode2->IsFullOutboundConn() == false);
                   +  - ]
      96   [ +  -  +  -  :           2 :     BOOST_CHECK(pnode2->IsManualConn() == false);
                   +  - ]
      97   [ +  -  +  -  :           2 :     BOOST_CHECK(pnode2->IsBlockOnlyConn() == false);
                   +  - ]
      98   [ +  -  +  -  :           2 :     BOOST_CHECK(pnode2->IsFeelerConn() == false);
                   +  - ]
      99   [ +  -  +  -  :           2 :     BOOST_CHECK(pnode2->IsAddrFetchConn() == false);
                   +  - ]
     100   [ +  -  +  -  :           2 :     BOOST_CHECK(pnode2->IsInboundConn() == true);
                   +  - ]
     101   [ +  -  +  -  :           2 :     BOOST_CHECK(pnode2->m_inbound_onion == false);
                   +  - ]
     102   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(pnode2->ConnectedThroughNetwork(), Network::NET_IPV4);
                   +  - ]
     103                 :             : 
     104                 :           2 :     std::unique_ptr<CNode> pnode3 = std::make_unique<CNode>(id++,
     105                 :           1 :                                                             /*sock=*/nullptr,
     106                 :             :                                                             addr,
     107                 :           1 :                                                             /*nKeyedNetGroupIn=*/0,
     108                 :           1 :                                                             /*nLocalHostNonceIn=*/0,
     109                 :           0 :                                                             CAddress(),
     110                 :             :                                                             pszDest,
     111                 :           1 :                                                             ConnectionType::OUTBOUND_FULL_RELAY,
     112                 :           1 :                                                             /*inbound_onion=*/false,
     113   [ +  -  +  - ]:           1 :                                                             /*network_key=*/2);
     114   [ +  -  +  -  :           2 :     BOOST_CHECK(pnode3->IsFullOutboundConn() == true);
                   +  - ]
     115   [ +  -  +  -  :           2 :     BOOST_CHECK(pnode3->IsManualConn() == false);
                   +  - ]
     116   [ +  -  +  -  :           2 :     BOOST_CHECK(pnode3->IsBlockOnlyConn() == false);
                   +  - ]
     117   [ +  -  +  -  :           2 :     BOOST_CHECK(pnode3->IsFeelerConn() == false);
                   +  - ]
     118   [ +  -  +  -  :           2 :     BOOST_CHECK(pnode3->IsAddrFetchConn() == false);
                   +  - ]
     119   [ +  -  +  -  :           2 :     BOOST_CHECK(pnode3->IsInboundConn() == false);
                   +  - ]
     120   [ +  -  +  -  :           2 :     BOOST_CHECK(pnode3->m_inbound_onion == false);
                   +  - ]
     121   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(pnode3->ConnectedThroughNetwork(), Network::NET_IPV4);
                   +  - ]
     122                 :             : 
     123                 :           2 :     std::unique_ptr<CNode> pnode4 = std::make_unique<CNode>(id++,
     124                 :           1 :                                                             /*sock=*/nullptr,
     125                 :             :                                                             addr,
     126                 :           1 :                                                             /*nKeyedNetGroupIn=*/1,
     127                 :           1 :                                                             /*nLocalHostNonceIn=*/1,
     128                 :           0 :                                                             CAddress(),
     129                 :             :                                                             pszDest,
     130                 :           1 :                                                             ConnectionType::INBOUND,
     131                 :           1 :                                                             /*inbound_onion=*/true,
     132   [ +  -  +  - ]:           1 :                                                             /*network_key=*/3);
     133   [ +  -  +  -  :           2 :     BOOST_CHECK(pnode4->IsFullOutboundConn() == false);
                   +  - ]
     134   [ +  -  +  -  :           2 :     BOOST_CHECK(pnode4->IsManualConn() == false);
                   +  - ]
     135   [ +  -  +  -  :           2 :     BOOST_CHECK(pnode4->IsBlockOnlyConn() == false);
                   +  - ]
     136   [ +  -  +  -  :           2 :     BOOST_CHECK(pnode4->IsFeelerConn() == false);
                   +  - ]
     137   [ +  -  +  -  :           2 :     BOOST_CHECK(pnode4->IsAddrFetchConn() == false);
                   +  - ]
     138   [ +  -  +  -  :           2 :     BOOST_CHECK(pnode4->IsInboundConn() == true);
                   +  - ]
     139   [ +  -  +  -  :           2 :     BOOST_CHECK(pnode4->m_inbound_onion == true);
                   +  - ]
     140   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(pnode4->ConnectedThroughNetwork(), Network::NET_ONION);
                   +  - ]
     141                 :           1 : }
     142                 :             : 
     143   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(cnetaddr_basic)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     144                 :             : {
     145                 :           1 :     CNetAddr addr;
     146                 :             : 
     147                 :             :     // IPv4, INADDR_ANY
     148   [ +  -  +  -  :           2 :     addr = LookupHost("0.0.0.0", false).value();
                   +  - ]
     149   [ +  -  +  -  :           2 :     BOOST_REQUIRE(!addr.IsValid());
             +  -  +  - ]
     150   [ +  -  +  -  :           2 :     BOOST_REQUIRE(addr.IsIPv4());
                   +  - ]
     151                 :             : 
     152   [ +  -  +  -  :           2 :     BOOST_CHECK(addr.IsBindAny());
             +  -  +  - ]
     153   [ +  -  +  -  :           2 :     BOOST_CHECK(addr.IsAddrV1Compatible());
             +  -  +  - ]
     154   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(addr.ToStringAddr(), "0.0.0.0");
                   +  - ]
     155                 :             : 
     156                 :             :     // IPv4, INADDR_NONE
     157   [ +  -  +  -  :           2 :     addr = LookupHost("255.255.255.255", false).value();
                   +  - ]
     158   [ +  -  +  -  :           2 :     BOOST_REQUIRE(!addr.IsValid());
             +  -  +  - ]
     159   [ +  -  +  -  :           2 :     BOOST_REQUIRE(addr.IsIPv4());
                   +  - ]
     160                 :             : 
     161   [ +  -  +  -  :           2 :     BOOST_CHECK(!addr.IsBindAny());
             +  -  +  - ]
     162   [ +  -  +  -  :           2 :     BOOST_CHECK(addr.IsAddrV1Compatible());
             +  -  +  - ]
     163   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(addr.ToStringAddr(), "255.255.255.255");
                   +  - ]
     164                 :             : 
     165                 :             :     // IPv4, casual
     166   [ +  -  +  -  :           2 :     addr = LookupHost("12.34.56.78", false).value();
                   +  - ]
     167   [ +  -  +  -  :           2 :     BOOST_REQUIRE(addr.IsValid());
             +  -  +  - ]
     168   [ +  -  +  -  :           2 :     BOOST_REQUIRE(addr.IsIPv4());
                   +  - ]
     169                 :             : 
     170   [ +  -  +  -  :           2 :     BOOST_CHECK(!addr.IsBindAny());
             +  -  +  - ]
     171   [ +  -  +  -  :           2 :     BOOST_CHECK(addr.IsAddrV1Compatible());
             +  -  +  - ]
     172   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(addr.ToStringAddr(), "12.34.56.78");
                   +  - ]
     173                 :             : 
     174                 :             :     // IPv6, in6addr_any
     175   [ +  -  +  -  :           2 :     addr = LookupHost("::", false).value();
                   +  - ]
     176   [ +  -  +  -  :           2 :     BOOST_REQUIRE(!addr.IsValid());
             +  -  +  - ]
     177   [ +  -  +  -  :           2 :     BOOST_REQUIRE(addr.IsIPv6());
                   +  - ]
     178                 :             : 
     179   [ +  -  +  -  :           2 :     BOOST_CHECK(addr.IsBindAny());
             +  -  +  - ]
     180   [ +  -  +  -  :           2 :     BOOST_CHECK(addr.IsAddrV1Compatible());
             +  -  +  - ]
     181   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(addr.ToStringAddr(), "::");
                   +  - ]
     182                 :             : 
     183                 :             :     // IPv6, casual
     184   [ +  -  +  -  :           2 :     addr = LookupHost("1122:3344:5566:7788:9900:aabb:ccdd:eeff", false).value();
                   +  - ]
     185   [ +  -  +  -  :           2 :     BOOST_REQUIRE(addr.IsValid());
             +  -  +  - ]
     186   [ +  -  +  -  :           2 :     BOOST_REQUIRE(addr.IsIPv6());
                   +  - ]
     187                 :             : 
     188   [ +  -  +  -  :           2 :     BOOST_CHECK(!addr.IsBindAny());
             +  -  +  - ]
     189   [ +  -  +  -  :           2 :     BOOST_CHECK(addr.IsAddrV1Compatible());
             +  -  +  - ]
     190   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(addr.ToStringAddr(), "1122:3344:5566:7788:9900:aabb:ccdd:eeff");
                   +  - ]
     191                 :             : 
     192                 :             :     // IPv6, scoped/link-local. See https://tools.ietf.org/html/rfc4007
     193                 :             :     // We support non-negative decimal integers (uint32_t) as zone id indices.
     194                 :             :     // Normal link-local scoped address functionality is to append "%" plus the
     195                 :             :     // zone id, for example, given a link-local address of "fe80::1" and a zone
     196                 :             :     // id of "32", return the address as "fe80::1%32".
     197         [ +  - ]:           1 :     const std::string link_local{"fe80::1"};
     198         [ +  - ]:           1 :     const std::string scoped_addr{link_local + "%32"};
     199   [ +  -  +  - ]:           2 :     addr = LookupHost(scoped_addr, false).value();
     200   [ +  -  +  -  :           2 :     BOOST_REQUIRE(addr.IsValid());
             +  -  +  - ]
     201   [ +  -  +  -  :           2 :     BOOST_REQUIRE(addr.IsIPv6());
                   +  - ]
     202   [ +  -  +  -  :           2 :     BOOST_CHECK(!addr.IsBindAny());
             +  -  +  - ]
     203   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(addr.ToStringAddr(), scoped_addr);
                   +  - ]
     204                 :             : 
     205                 :             :     // TORv2, no longer supported
     206   [ +  -  +  -  :           2 :     BOOST_CHECK(!addr.SetSpecial("6hzph5hv6337r6p2.onion"));
             +  -  +  - ]
     207                 :             : 
     208                 :             :     // TORv3
     209                 :           1 :     const char* torv3_addr = "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion";
     210   [ +  -  +  -  :           2 :     BOOST_REQUIRE(addr.SetSpecial(torv3_addr));
             +  -  +  - ]
     211   [ +  -  +  -  :           2 :     BOOST_REQUIRE(addr.IsValid());
             +  -  +  - ]
     212   [ +  -  +  -  :           2 :     BOOST_REQUIRE(addr.IsTor());
                   +  - ]
     213                 :             : 
     214   [ +  -  +  -  :           2 :     BOOST_CHECK(!addr.IsI2P());
                   +  - ]
     215   [ +  -  +  -  :           2 :     BOOST_CHECK(!addr.IsBindAny());
             +  -  +  - ]
     216   [ +  -  +  -  :           2 :     BOOST_CHECK(!addr.IsAddrV1Compatible());
             +  -  +  - ]
     217   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(addr.ToStringAddr(), torv3_addr);
                   +  - ]
     218                 :             : 
     219                 :             :     // TORv3, mixed case
     220                 :           1 :     const char* torv3_addr_mixed_case = "PG6Mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.ONION";
     221   [ +  -  +  -  :           2 :     BOOST_REQUIRE(addr.SetSpecial(torv3_addr_mixed_case));
             +  -  +  - ]
     222   [ +  -  +  -  :           2 :     BOOST_REQUIRE(addr.IsValid());
             +  -  +  - ]
     223   [ +  -  +  -  :           2 :     BOOST_REQUIRE(addr.IsTor());
                   +  - ]
     224   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(addr.ToStringAddr(), ToLower(torv3_addr_mixed_case));
             +  -  +  - ]
     225                 :             : 
     226                 :             :     // TORv3, broken, with wrong checksum
     227   [ +  -  +  -  :           2 :     BOOST_CHECK(!addr.SetSpecial("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscsad.onion"));
             +  -  +  - ]
     228                 :             : 
     229                 :             :     // TORv3, broken, with wrong version
     230   [ +  -  +  -  :           2 :     BOOST_CHECK(!addr.SetSpecial("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscrye.onion"));
             +  -  +  - ]
     231                 :             : 
     232                 :             :     // TORv3, malicious
     233   [ +  -  +  -  :           3 :     BOOST_CHECK(!addr.SetSpecial(std::string{
          +  -  +  -  +  
                      - ]
     234                 :             :         "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd\0wtf.onion", 66}));
     235                 :             : 
     236                 :             :     // TOR, bogus length
     237   [ +  -  +  -  :           2 :     BOOST_CHECK(!addr.SetSpecial("mfrggzak.onion"));
             +  -  +  - ]
     238                 :             : 
     239                 :             :     // TOR, invalid base32
     240   [ +  -  +  -  :           2 :     BOOST_CHECK(!addr.SetSpecial("mf*g zak.onion"));
             +  -  +  - ]
     241                 :             : 
     242                 :             :     // I2P
     243                 :           1 :     const char* i2p_addr = "UDHDrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v4jna.b32.I2P";
     244   [ +  -  +  -  :           2 :     BOOST_REQUIRE(addr.SetSpecial(i2p_addr));
             +  -  +  - ]
     245   [ +  -  +  -  :           2 :     BOOST_REQUIRE(addr.IsValid());
             +  -  +  - ]
     246   [ +  -  +  -  :           2 :     BOOST_REQUIRE(addr.IsI2P());
                   +  - ]
     247                 :             : 
     248   [ +  -  +  -  :           2 :     BOOST_CHECK(!addr.IsTor());
                   +  - ]
     249   [ +  -  +  -  :           2 :     BOOST_CHECK(!addr.IsBindAny());
             +  -  +  - ]
     250   [ +  -  +  -  :           2 :     BOOST_CHECK(!addr.IsAddrV1Compatible());
             +  -  +  - ]
     251   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(addr.ToStringAddr(), ToLower(i2p_addr));
             +  -  +  - ]
     252                 :             : 
     253                 :             :     // I2P, correct length, but decodes to less than the expected number of bytes.
     254   [ +  -  +  -  :           2 :     BOOST_CHECK(!addr.SetSpecial("udhdrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v4jn=.b32.i2p"));
             +  -  +  - ]
     255                 :             : 
     256                 :             :     // I2P, extra unnecessary padding
     257   [ +  -  +  -  :           2 :     BOOST_CHECK(!addr.SetSpecial("udhdrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v4jna=.b32.i2p"));
             +  -  +  - ]
     258                 :             : 
     259                 :             :     // I2P, malicious
     260   [ +  -  +  -  :           2 :     BOOST_CHECK(!addr.SetSpecial("udhdrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v\0wtf.b32.i2p"s));
          -  +  +  -  +  
                -  +  - ]
     261                 :             : 
     262                 :             :     // I2P, valid but unsupported (56 Base32 characters)
     263                 :             :     // See https://i2p.net/en/docs/specs/b32encrypted
     264   [ +  -  +  -  :           2 :     BOOST_CHECK(
             +  -  +  - ]
     265                 :             :         !addr.SetSpecial("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscsad.b32.i2p"));
     266                 :             : 
     267                 :             :     // I2P, invalid base32
     268   [ +  -  +  -  :           2 :     BOOST_CHECK(!addr.SetSpecial("tp*szydbh4dp.b32.i2p"));
             +  -  +  - ]
     269                 :             : 
     270                 :             :     // Internal
     271   [ +  -  +  - ]:           1 :     addr.SetInternal("esffpp");
     272   [ +  -  +  -  :           2 :     BOOST_REQUIRE(!addr.IsValid()); // "internal" is considered invalid
             +  -  +  - ]
     273   [ +  -  +  -  :           2 :     BOOST_REQUIRE(addr.IsInternal());
             +  -  +  - ]
     274                 :             : 
     275   [ +  -  +  -  :           2 :     BOOST_CHECK(!addr.IsBindAny());
             +  -  +  - ]
     276   [ +  -  +  -  :           2 :     BOOST_CHECK(addr.IsAddrV1Compatible());
             +  -  +  - ]
     277   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(addr.ToStringAddr(), "esffpvrt3wpeaygy.internal");
                   +  - ]
     278                 :             : 
     279                 :             :     // Totally bogus
     280   [ +  -  +  -  :           2 :     BOOST_CHECK(!addr.SetSpecial("totally bogus"));
                   +  - ]
     281                 :           1 : }
     282                 :             : 
     283   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(cnetaddr_tostring_canonical_ipv6)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     284                 :             : {
     285                 :             :     // Test that CNetAddr::ToString formats IPv6 addresses with zero compression as described in
     286                 :             :     // RFC 5952 ("A Recommendation for IPv6 Address Text Representation").
     287                 :           1 :     const std::map<std::string, std::string> canonical_representations_ipv6{
     288                 :             :         {"0000:0000:0000:0000:0000:0000:0000:0000", "::"},
     289                 :             :         {"000:0000:000:00:0:00:000:0000", "::"},
     290                 :             :         {"000:000:000:000:000:000:000:000", "::"},
     291                 :             :         {"00:00:00:00:00:00:00:00", "::"},
     292                 :             :         {"0:0:0:0:0:0:0:0", "::"},
     293                 :             :         {"0:0:0:0:0:0:0:1", "::1"},
     294                 :             :         {"2001:0:0:1:0:0:0:1", "2001:0:0:1::1"},
     295                 :             :         {"2001:0db8:0:0:1:0:0:1", "2001:db8::1:0:0:1"},
     296                 :             :         {"2001:0db8:85a3:0000:0000:8a2e:0370:7334", "2001:db8:85a3::8a2e:370:7334"},
     297                 :             :         {"2001:0db8::0001", "2001:db8::1"},
     298                 :             :         {"2001:0db8::0001:0000", "2001:db8::1:0"},
     299                 :             :         {"2001:0db8::1:0:0:1", "2001:db8::1:0:0:1"},
     300                 :             :         {"2001:db8:0000:0:1::1", "2001:db8::1:0:0:1"},
     301                 :             :         {"2001:db8:0000:1:1:1:1:1", "2001:db8:0:1:1:1:1:1"},
     302                 :             :         {"2001:db8:0:0:0:0:2:1", "2001:db8::2:1"},
     303                 :             :         {"2001:db8:0:0:0::1", "2001:db8::1"},
     304                 :             :         {"2001:db8:0:0:1:0:0:1", "2001:db8::1:0:0:1"},
     305                 :             :         {"2001:db8:0:0:1::1", "2001:db8::1:0:0:1"},
     306                 :             :         {"2001:DB8:0:0:1::1", "2001:db8::1:0:0:1"},
     307                 :             :         {"2001:db8:0:0::1", "2001:db8::1"},
     308                 :             :         {"2001:db8:0:0:aaaa::1", "2001:db8::aaaa:0:0:1"},
     309                 :             :         {"2001:db8:0:1:1:1:1:1", "2001:db8:0:1:1:1:1:1"},
     310                 :             :         {"2001:db8:0::1", "2001:db8::1"},
     311                 :             :         {"2001:db8:85a3:0:0:8a2e:370:7334", "2001:db8:85a3::8a2e:370:7334"},
     312                 :             :         {"2001:db8::0:1", "2001:db8::1"},
     313                 :             :         {"2001:db8::0:1:0:0:1", "2001:db8::1:0:0:1"},
     314                 :             :         {"2001:DB8::1", "2001:db8::1"},
     315                 :             :         {"2001:db8::1", "2001:db8::1"},
     316                 :             :         {"2001:db8::1:0:0:1", "2001:db8::1:0:0:1"},
     317                 :             :         {"2001:db8::1:1:1:1:1", "2001:db8:0:1:1:1:1:1"},
     318                 :             :         {"2001:db8::aaaa:0:0:1", "2001:db8::aaaa:0:0:1"},
     319                 :             :         {"2001:db8:aaaa:bbbb:cccc:dddd:0:1", "2001:db8:aaaa:bbbb:cccc:dddd:0:1"},
     320                 :             :         {"2001:db8:aaaa:bbbb:cccc:dddd::1", "2001:db8:aaaa:bbbb:cccc:dddd:0:1"},
     321                 :             :         {"2001:db8:aaaa:bbbb:cccc:dddd:eeee:0001", "2001:db8:aaaa:bbbb:cccc:dddd:eeee:1"},
     322                 :             :         {"2001:db8:aaaa:bbbb:cccc:dddd:eeee:001", "2001:db8:aaaa:bbbb:cccc:dddd:eeee:1"},
     323                 :             :         {"2001:db8:aaaa:bbbb:cccc:dddd:eeee:01", "2001:db8:aaaa:bbbb:cccc:dddd:eeee:1"},
     324                 :             :         {"2001:db8:aaaa:bbbb:cccc:dddd:eeee:1", "2001:db8:aaaa:bbbb:cccc:dddd:eeee:1"},
     325                 :             :         {"2001:db8:aaaa:bbbb:cccc:dddd:eeee:aaaa", "2001:db8:aaaa:bbbb:cccc:dddd:eeee:aaaa"},
     326                 :             :         {"2001:db8:aaaa:bbbb:cccc:dddd:eeee:AAAA", "2001:db8:aaaa:bbbb:cccc:dddd:eeee:aaaa"},
     327                 :             :         {"2001:db8:aaaa:bbbb:cccc:dddd:eeee:AaAa", "2001:db8:aaaa:bbbb:cccc:dddd:eeee:aaaa"},
     328   [ -  +  +  +  :          41 :     };
                   -  - ]
     329   [ +  -  +  + ]:          41 :     for (const auto& [input_address, expected_canonical_representation_output] : canonical_representations_ipv6) {
     330   [ +  -  +  - ]:          40 :         const std::optional<CNetAddr> net_addr{LookupHost(input_address, false)};
     331   [ +  -  +  -  :          80 :         BOOST_REQUIRE(net_addr.value().IsIPv6());
             +  -  +  - ]
     332   [ +  -  +  -  :          40 :         BOOST_CHECK_EQUAL(net_addr.value().ToStringAddr(), expected_canonical_representation_output);
             +  -  +  - ]
     333                 :          40 :     }
     334   [ +  -  +  -  :           2 : }
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                -  -  - ]
     335                 :             : 
     336   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(cnetaddr_serialize_v1)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     337                 :             : {
     338                 :           1 :     CNetAddr addr;
     339                 :           1 :     DataStream s{};
     340                 :           1 :     const auto ser_params{CAddress::V1_NETWORK};
     341                 :             : 
     342         [ +  - ]:           1 :     s << ser_params(addr);
     343   [ +  -  -  +  :           1 :     BOOST_CHECK_EQUAL(HexStr(s), "00000000000000000000000000000000");
             +  -  +  - ]
     344         [ +  - ]:           1 :     s.clear();
     345                 :             : 
     346   [ +  -  +  -  :           2 :     addr = LookupHost("1.2.3.4", false).value();
                   +  - ]
     347         [ +  - ]:           1 :     s << ser_params(addr);
     348   [ +  -  -  +  :           1 :     BOOST_CHECK_EQUAL(HexStr(s), "00000000000000000000ffff01020304");
             +  -  +  - ]
     349         [ +  - ]:           1 :     s.clear();
     350                 :             : 
     351   [ +  -  +  -  :           2 :     addr = LookupHost("1a1b:2a2b:3a3b:4a4b:5a5b:6a6b:7a7b:8a8b", false).value();
                   +  - ]
     352         [ +  - ]:           1 :     s << ser_params(addr);
     353   [ +  -  -  +  :           1 :     BOOST_CHECK_EQUAL(HexStr(s), "1a1b2a2b3a3b4a4b5a5b6a6b7a7b8a8b");
             +  -  +  - ]
     354         [ +  - ]:           1 :     s.clear();
     355                 :             : 
     356                 :             :     // TORv2, no longer supported
     357   [ +  -  +  -  :           2 :     BOOST_CHECK(!addr.SetSpecial("6hzph5hv6337r6p2.onion"));
             +  -  +  - ]
     358                 :             : 
     359   [ +  -  +  -  :           2 :     BOOST_REQUIRE(addr.SetSpecial("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion"));
             +  -  +  - ]
     360         [ +  - ]:           1 :     s << ser_params(addr);
     361   [ +  -  -  +  :           1 :     BOOST_CHECK_EQUAL(HexStr(s), "00000000000000000000000000000000");
             +  -  +  - ]
     362         [ +  - ]:           1 :     s.clear();
     363                 :             : 
     364   [ +  -  +  - ]:           1 :     addr.SetInternal("a");
     365         [ +  - ]:           1 :     s << ser_params(addr);
     366   [ +  -  -  +  :           1 :     BOOST_CHECK_EQUAL(HexStr(s), "fd6b88c08724ca978112ca1bbdcafac2");
             +  -  +  - ]
     367         [ +  - ]:           2 :     s.clear();
     368                 :           1 : }
     369                 :             : 
     370   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(cnetaddr_serialize_v2)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     371                 :             : {
     372                 :           1 :     CNetAddr addr;
     373                 :           1 :     DataStream s{};
     374                 :           1 :     const auto ser_params{CAddress::V2_NETWORK};
     375                 :             : 
     376         [ +  - ]:           1 :     s << ser_params(addr);
     377   [ +  -  -  +  :           1 :     BOOST_CHECK_EQUAL(HexStr(s), "021000000000000000000000000000000000");
             +  -  +  - ]
     378         [ +  - ]:           1 :     s.clear();
     379                 :             : 
     380   [ +  -  +  -  :           2 :     addr = LookupHost("1.2.3.4", false).value();
                   +  - ]
     381         [ +  - ]:           1 :     s << ser_params(addr);
     382   [ +  -  -  +  :           1 :     BOOST_CHECK_EQUAL(HexStr(s), "010401020304");
             +  -  +  - ]
     383         [ +  - ]:           1 :     s.clear();
     384                 :             : 
     385   [ +  -  +  -  :           2 :     addr = LookupHost("1a1b:2a2b:3a3b:4a4b:5a5b:6a6b:7a7b:8a8b", false).value();
                   +  - ]
     386         [ +  - ]:           1 :     s << ser_params(addr);
     387   [ +  -  -  +  :           1 :     BOOST_CHECK_EQUAL(HexStr(s), "02101a1b2a2b3a3b4a4b5a5b6a6b7a7b8a8b");
             +  -  +  - ]
     388         [ +  - ]:           1 :     s.clear();
     389                 :             : 
     390                 :             :     // TORv2, no longer supported
     391   [ +  -  +  -  :           2 :     BOOST_CHECK(!addr.SetSpecial("6hzph5hv6337r6p2.onion"));
             +  -  +  - ]
     392                 :             : 
     393   [ +  -  +  -  :           2 :     BOOST_REQUIRE(addr.SetSpecial("kpgvmscirrdqpekbqjsvw5teanhatztpp2gl6eee4zkowvwfxwenqaid.onion"));
             +  -  +  - ]
     394         [ +  - ]:           1 :     s << ser_params(addr);
     395   [ +  -  -  +  :           1 :     BOOST_CHECK_EQUAL(HexStr(s), "042053cd5648488c4707914182655b7664034e09e66f7e8cbf1084e654eb56c5bd88");
             +  -  +  - ]
     396         [ +  - ]:           1 :     s.clear();
     397                 :             : 
     398   [ +  -  +  -  :           2 :     BOOST_REQUIRE(addr.SetInternal("a"));
          +  -  +  -  +  
                      - ]
     399         [ +  - ]:           1 :     s << ser_params(addr);
     400   [ +  -  -  +  :           1 :     BOOST_CHECK_EQUAL(HexStr(s), "0210fd6b88c08724ca978112ca1bbdcafac2");
             +  -  +  - ]
     401         [ +  - ]:           2 :     s.clear();
     402                 :           1 : }
     403                 :             : 
     404   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(cnetaddr_unserialize_v2)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     405                 :             : {
     406                 :           1 :     CNetAddr addr;
     407                 :           1 :     DataStream s{};
     408                 :           1 :     const auto ser_params{CAddress::V2_NETWORK};
     409                 :             : 
     410                 :             :     // Valid IPv4.
     411         [ +  - ]:           1 :     s << "01"            // network type (IPv4)
     412                 :             :          "04"            // address length
     413                 :           1 :          "01020304"_hex; // address
     414         [ +  - ]:           1 :     s >> ser_params(addr);
     415   [ +  -  +  -  :           2 :     BOOST_CHECK(addr.IsValid());
             +  -  +  - ]
     416   [ +  -  +  -  :           2 :     BOOST_CHECK(addr.IsIPv4());
                   +  - ]
     417   [ +  -  +  -  :           2 :     BOOST_CHECK(addr.IsAddrV1Compatible());
             +  -  +  - ]
     418   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(addr.ToStringAddr(), "1.2.3.4");
                   +  - ]
     419   [ +  -  -  +  :           2 :     BOOST_REQUIRE(s.empty());
             +  -  +  - ]
     420                 :             : 
     421                 :             :     // Invalid IPv4, valid length but address itself is shorter.
     422         [ +  - ]:           1 :     s << "01"        // network type (IPv4)
     423                 :             :          "04"        // address length
     424                 :           1 :          "0102"_hex; // address
     425   [ +  -  -  +  :           2 :     BOOST_CHECK_EXCEPTION(s >> ser_params(addr), std::ios_base::failure, HasReason("end of data"));
          -  -  -  -  -  
          +  +  -  +  -  
                   +  - ]
     426   [ +  -  -  +  :           2 :     BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
             +  -  +  - ]
     427         [ +  - ]:           1 :     s.clear();
     428                 :             : 
     429                 :             :     // Invalid IPv4, with bogus length.
     430         [ +  - ]:           1 :     s << "01"            // network type (IPv4)
     431                 :             :          "05"            // address length
     432                 :           1 :          "01020304"_hex; // address
     433   [ +  -  -  +  :           2 :     BOOST_CHECK_EXCEPTION(s >> ser_params(addr), std::ios_base::failure,
          -  -  -  -  -  
          +  +  -  +  -  
                   +  - ]
     434                 :             :                           HasReason("BIP155 IPv4 address with length 5 (should be 4)"));
     435   [ +  -  -  +  :           2 :     BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
             +  -  +  - ]
     436         [ +  - ]:           1 :     s.clear();
     437                 :             : 
     438                 :             :     // Invalid IPv4, with extreme length.
     439         [ +  - ]:           1 :     s << "01"            // network type (IPv4)
     440                 :             :          "fd0102"        // address length (513 as CompactSize)
     441                 :           1 :          "01020304"_hex; // address
     442   [ +  -  -  +  :           2 :     BOOST_CHECK_EXCEPTION(s >> ser_params(addr), std::ios_base::failure,
          -  -  -  -  -  
          +  +  -  +  -  
                   +  - ]
     443                 :             :                           HasReason("Address too long: 513 > 512"));
     444   [ +  -  -  +  :           2 :     BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
             +  -  +  - ]
     445         [ +  - ]:           1 :     s.clear();
     446                 :             : 
     447                 :             :     // Valid IPv6.
     448         [ +  - ]:           1 :     s << "02"                                    // network type (IPv6)
     449                 :             :          "10"                                    // address length
     450                 :           1 :          "0102030405060708090a0b0c0d0e0f10"_hex; // address
     451         [ +  - ]:           1 :     s >> ser_params(addr);
     452   [ +  -  +  -  :           2 :     BOOST_CHECK(addr.IsValid());
             +  -  +  - ]
     453   [ +  -  +  -  :           2 :     BOOST_CHECK(addr.IsIPv6());
                   +  - ]
     454   [ +  -  +  -  :           2 :     BOOST_CHECK(addr.IsAddrV1Compatible());
             +  -  +  - ]
     455   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(addr.ToStringAddr(), "102:304:506:708:90a:b0c:d0e:f10");
                   +  - ]
     456   [ +  -  -  +  :           2 :     BOOST_REQUIRE(s.empty());
             +  -  +  - ]
     457                 :             : 
     458                 :             :     // Valid IPv6, contains embedded "internal".
     459         [ +  - ]:           1 :     s << "02"                                    // network type (IPv6)
     460                 :             :          "10"                                    // address length
     461                 :           1 :          "fd6b88c08724ca978112ca1bbdcafac2"_hex; // address: 0xfd + sha256("bitcoin")[0:5] +
     462                 :             :                                                  // sha256(name)[0:10]
     463         [ +  - ]:           1 :     s >> ser_params(addr);
     464   [ +  -  +  -  :           2 :     BOOST_CHECK(addr.IsInternal());
             +  -  +  - ]
     465   [ +  -  +  -  :           2 :     BOOST_CHECK(addr.IsAddrV1Compatible());
             +  -  +  - ]
     466   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(addr.ToStringAddr(), "zklycewkdo64v6wc.internal");
                   +  - ]
     467   [ +  -  -  +  :           2 :     BOOST_REQUIRE(s.empty());
             +  -  +  - ]
     468                 :             : 
     469                 :             :     // Invalid IPv6, with bogus length.
     470         [ +  - ]:           1 :     s << "02"      // network type (IPv6)
     471                 :             :          "04"      // address length
     472                 :           1 :          "00"_hex; // address
     473   [ +  -  -  +  :           2 :     BOOST_CHECK_EXCEPTION(s >> ser_params(addr), std::ios_base::failure,
          -  -  -  -  -  
          +  +  -  +  -  
                   +  - ]
     474                 :             :                           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         [ +  - ]:           1 :     s.clear();
     477                 :             : 
     478                 :             :     // Invalid IPv6, contains embedded IPv4.
     479         [ +  - ]:           1 :     s << "02"                                    // network type (IPv6)
     480                 :             :          "10"                                    // address length
     481                 :           1 :          "00000000000000000000ffff01020304"_hex; // address
     482         [ +  - ]:           1 :     s >> ser_params(addr);
     483   [ +  -  +  -  :           2 :     BOOST_CHECK(!addr.IsValid());
             +  -  +  - ]
     484   [ +  -  -  +  :           2 :     BOOST_REQUIRE(s.empty());
             +  -  +  - ]
     485                 :             : 
     486                 :             :     // Invalid IPv6, contains embedded TORv2.
     487         [ +  - ]:           1 :     s << "02"                                    // network type (IPv6)
     488                 :             :          "10"                                    // address length
     489                 :           1 :          "fd87d87eeb430102030405060708090a"_hex; // address
     490         [ +  - ]:           1 :     s >> ser_params(addr);
     491   [ +  -  +  -  :           2 :     BOOST_CHECK(!addr.IsValid());
             +  -  +  - ]
     492   [ +  -  -  +  :           2 :     BOOST_REQUIRE(s.empty());
             +  -  +  - ]
     493                 :             : 
     494                 :             :     // TORv2, no longer supported.
     495         [ +  - ]:           1 :     s << "03"                        // network type (TORv2)
     496                 :             :          "0a"                        // address length
     497                 :           1 :          "f1f2f3f4f5f6f7f8f9fa"_hex; // address
     498         [ +  - ]:           1 :     s >> ser_params(addr);
     499   [ +  -  +  -  :           2 :     BOOST_CHECK(!addr.IsValid());
             +  -  +  - ]
     500   [ +  -  -  +  :           2 :     BOOST_REQUIRE(s.empty());
             +  -  +  - ]
     501                 :             : 
     502                 :             :     // Valid TORv3.
     503         [ +  - ]:           1 :     s << "04"                               // network type (TORv3)
     504                 :             :          "20"                               // address length
     505                 :             :          "79bcc625184b05194975c28b66b66b04" // address
     506                 :           1 :          "69f7f6556fb1ac3189a79b40dda32f1f"_hex;
     507         [ +  - ]:           1 :     s >> ser_params(addr);
     508   [ +  -  +  -  :           2 :     BOOST_CHECK(addr.IsValid());
             +  -  +  - ]
     509   [ +  -  +  -  :           2 :     BOOST_CHECK(addr.IsTor());
                   +  - ]
     510   [ +  -  +  -  :           2 :     BOOST_CHECK(!addr.IsAddrV1Compatible());
             +  -  +  - ]
     511   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(addr.ToStringAddr(),
                   +  - ]
     512                 :             :                       "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion");
     513   [ +  -  -  +  :           2 :     BOOST_REQUIRE(s.empty());
             +  -  +  - ]
     514                 :             : 
     515                 :             :     // Invalid TORv3, with bogus length.
     516         [ +  - ]:           1 :     s << "04"      // network type (TORv3)
     517                 :             :          "00"      // address length
     518                 :           1 :          "00"_hex; // address
     519   [ +  -  -  +  :           2 :     BOOST_CHECK_EXCEPTION(s >> ser_params(addr), std::ios_base::failure,
          -  -  -  -  -  
          +  +  -  +  -  
                   +  - ]
     520                 :             :                           HasReason("BIP155 TORv3 address with length 0 (should be 32)"));
     521   [ +  -  -  +  :           2 :     BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
             +  -  +  - ]
     522         [ +  - ]:           1 :     s.clear();
     523                 :             : 
     524                 :             :     // Valid I2P.
     525         [ +  - ]:           1 :     s << "05"                               // network type (I2P)
     526                 :             :          "20"                               // address length
     527                 :             :          "a2894dabaec08c0051a481a6dac88b64" // address
     528                 :           1 :          "f98232ae42d4b6fd2fa81952dfe36a87"_hex;
     529         [ +  - ]:           1 :     s >> ser_params(addr);
     530   [ +  -  +  -  :           2 :     BOOST_CHECK(addr.IsValid());
             +  -  +  - ]
     531   [ +  -  +  -  :           2 :     BOOST_CHECK(addr.IsI2P());
                   +  - ]
     532   [ +  -  +  -  :           2 :     BOOST_CHECK(!addr.IsAddrV1Compatible());
             +  -  +  - ]
     533   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(addr.ToStringAddr(),
                   +  - ]
     534                 :             :                       "ukeu3k5oycgaauneqgtnvselmt4yemvoilkln7jpvamvfx7dnkdq.b32.i2p");
     535   [ +  -  -  +  :           2 :     BOOST_REQUIRE(s.empty());
             +  -  +  - ]
     536                 :             : 
     537                 :             :     // Invalid I2P, with bogus length.
     538         [ +  - ]:           1 :     s << "05"      // network type (I2P)
     539                 :             :          "03"      // address length
     540                 :           1 :          "00"_hex; // address
     541   [ +  -  -  +  :           2 :     BOOST_CHECK_EXCEPTION(s >> ser_params(addr), std::ios_base::failure,
          -  -  -  -  -  
          +  +  -  +  -  
                   +  - ]
     542                 :             :                           HasReason("BIP155 I2P address with length 3 (should be 32)"));
     543   [ +  -  -  +  :           2 :     BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
             +  -  +  - ]
     544         [ +  - ]:           1 :     s.clear();
     545                 :             : 
     546                 :             :     // Valid CJDNS.
     547         [ +  - ]:           1 :     s << "06"                                    // network type (CJDNS)
     548                 :             :          "10"                                    // address length
     549                 :           1 :          "fc000001000200030004000500060007"_hex; // address
     550         [ +  - ]:           1 :     s >> ser_params(addr);
     551   [ +  -  +  -  :           2 :     BOOST_CHECK(addr.IsValid());
             +  -  +  - ]
     552   [ +  -  +  -  :           2 :     BOOST_CHECK(addr.IsCJDNS());
                   +  - ]
     553   [ +  -  +  -  :           2 :     BOOST_CHECK(!addr.IsAddrV1Compatible());
             +  -  +  - ]
     554   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(addr.ToStringAddr(), "fc00:1:2:3:4:5:6:7");
                   +  - ]
     555   [ +  -  -  +  :           2 :     BOOST_REQUIRE(s.empty());
             +  -  +  - ]
     556                 :             : 
     557                 :             :     // Invalid CJDNS, wrong prefix.
     558         [ +  - ]:           1 :     s << "06"                                    // network type (CJDNS)
     559                 :             :          "10"                                    // address length
     560                 :           1 :          "aa000001000200030004000500060007"_hex; // address
     561         [ +  - ]:           1 :     s >> ser_params(addr);
     562   [ +  -  +  -  :           2 :     BOOST_CHECK(addr.IsCJDNS());
                   +  - ]
     563   [ +  -  +  -  :           2 :     BOOST_CHECK(!addr.IsValid());
             +  -  +  - ]
     564   [ +  -  -  +  :           2 :     BOOST_REQUIRE(s.empty());
             +  -  +  - ]
     565                 :             : 
     566                 :             :     // Invalid CJDNS, with bogus length.
     567         [ +  - ]:           1 :     s << "06"      // network type (CJDNS)
     568                 :             :          "01"      // address length
     569                 :           1 :          "00"_hex; // address
     570   [ +  -  -  +  :           2 :     BOOST_CHECK_EXCEPTION(s >> ser_params(addr), std::ios_base::failure,
          -  -  -  -  -  
          +  +  -  +  -  
                   +  - ]
     571                 :             :                           HasReason("BIP155 CJDNS address with length 1 (should be 16)"));
     572   [ +  -  -  +  :           2 :     BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
             +  -  +  - ]
     573         [ +  - ]:           1 :     s.clear();
     574                 :             : 
     575                 :             :     // Unknown, with extreme length.
     576         [ +  - ]:           1 :     s << "aa"                  // network type (unknown)
     577                 :             :          "fe00000002"          // address length (CompactSize's MAX_SIZE)
     578                 :           1 :          "01020304050607"_hex; // address
     579   [ +  -  -  +  :           2 :     BOOST_CHECK_EXCEPTION(s >> ser_params(addr), std::ios_base::failure,
          -  -  -  -  -  
          +  +  -  +  -  
                   +  - ]
     580                 :             :                           HasReason("Address too long: 33554432 > 512"));
     581   [ +  -  -  +  :           2 :     BOOST_REQUIRE(!s.empty()); // The stream is not consumed on invalid input.
             +  -  +  - ]
     582         [ +  - ]:           1 :     s.clear();
     583                 :             : 
     584                 :             :     // Unknown, with reasonable length.
     585         [ +  - ]:           1 :     s << "aa"            // network type (unknown)
     586                 :             :          "04"            // address length
     587                 :           1 :          "01020304"_hex; // address
     588         [ +  - ]:           1 :     s >> ser_params(addr);
     589   [ +  -  +  -  :           2 :     BOOST_CHECK(!addr.IsValid());
             +  -  +  - ]
     590   [ +  -  -  +  :           2 :     BOOST_REQUIRE(s.empty());
             +  -  +  - ]
     591                 :             : 
     592                 :             :     // Unknown, with zero length.
     593         [ +  - ]:           1 :     s << "aa"    // network type (unknown)
     594                 :             :          "00"    // address length
     595                 :           1 :          ""_hex; // address
     596         [ +  - ]:           1 :     s >> ser_params(addr);
     597   [ +  -  +  -  :           2 :     BOOST_CHECK(!addr.IsValid());
             +  -  +  - ]
     598   [ +  -  -  +  :           2 :     BOOST_REQUIRE(s.empty());
                   +  - ]
     599                 :           1 : }
     600                 :             : 
     601                 :             : // prior to PR #14728, this test triggers an undefined behavior
     602   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(ipv4_peer_with_ipv6_addrMe_test)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     603                 :             : {
     604                 :             :     // set up local addresses; all that's necessary to reproduce the bug is
     605                 :             :     // that a normal IPv4 address is among the entries, but if this address is
     606                 :             :     // !IsRoutable the undefined behavior is easier to trigger deterministically
     607                 :           1 :     in_addr raw_addr;
     608                 :           1 :     raw_addr.s_addr = htonl(0x7f000001);
     609                 :           1 :     const CNetAddr mapLocalHost_entry = CNetAddr(raw_addr);
     610                 :           1 :     {
     611         [ +  - ]:           1 :         LOCK(g_maplocalhost_mutex);
     612                 :           1 :         LocalServiceInfo lsi;
     613                 :           1 :         lsi.nScore = 23;
     614                 :           1 :         lsi.nPort = 42;
     615   [ +  -  +  - ]:           1 :         mapLocalHost[mapLocalHost_entry] = lsi;
     616                 :           0 :     }
     617                 :             : 
     618                 :             :     // create a peer with an IPv4 address
     619                 :           1 :     in_addr ipv4AddrPeer;
     620                 :           1 :     ipv4AddrPeer.s_addr = 0xa0b0c001;
     621         [ +  - ]:           2 :     CAddress addr = CAddress(CService(ipv4AddrPeer, 7777), NODE_NETWORK);
     622                 :           2 :     std::unique_ptr<CNode> pnode = std::make_unique<CNode>(/*id=*/0,
     623                 :           1 :                                                            /*sock=*/nullptr,
     624                 :             :                                                            addr,
     625                 :           1 :                                                            /*nKeyedNetGroupIn=*/0,
     626                 :           1 :                                                            /*nLocalHostNonceIn=*/0,
     627                 :           1 :                                                            CAddress{},
     628         [ +  - ]:           1 :                                                            /*pszDest=*/std::string{},
     629                 :           1 :                                                            ConnectionType::OUTBOUND_FULL_RELAY,
     630                 :           1 :                                                            /*inbound_onion=*/false,
     631   [ +  -  +  - ]:           1 :                                                            /*network_key=*/0);
     632         [ +  - ]:           1 :     pnode->fSuccessfullyConnected.store(true);
     633                 :             : 
     634                 :             :     // the peer claims to be reaching us via IPv6
     635                 :           1 :     in6_addr ipv6AddrLocal;
     636         [ +  - ]:           1 :     memset(ipv6AddrLocal.s6_addr, 0, 16);
     637                 :           1 :     ipv6AddrLocal.s6_addr[0] = 0xcc;
     638         [ +  - ]:           2 :     CAddress addrLocal = CAddress(CService(ipv6AddrLocal, 7777), NODE_NETWORK);
     639         [ +  - ]:           1 :     pnode->SetAddrLocal(addrLocal);
     640                 :             : 
     641                 :             :     // before patch, this causes undefined behavior detectable with clang's -fsanitize=memory
     642         [ +  - ]:           1 :     GetLocalAddrForPeer(*pnode);
     643                 :             : 
     644                 :             :     // suppress no-checks-run warning; if this test fails, it's by triggering a sanitizer
     645   [ +  -  +  -  :           2 :     BOOST_CHECK(1);
                   +  - ]
     646                 :             : 
     647                 :             :     // Cleanup, so that we don't confuse other tests.
     648                 :           1 :     {
     649         [ +  - ]:           1 :         LOCK(g_maplocalhost_mutex);
     650   [ +  -  +  - ]:           1 :         mapLocalHost.erase(mapLocalHost_entry);
     651                 :           1 :     }
     652                 :           1 : }
     653                 :             : 
     654   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(get_local_addr_for_peer_port)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     655                 :             : {
     656                 :             :     // Test that GetLocalAddrForPeer() properly selects the address to self-advertise:
     657                 :             :     //
     658                 :             :     // 1. GetLocalAddrForPeer() calls GetLocalAddress() which returns an address that is
     659                 :             :     //    not routable.
     660                 :             :     // 2. GetLocalAddrForPeer() overrides the address with whatever the peer has told us
     661                 :             :     //    he sees us as.
     662                 :             :     // 2.1. For inbound connections we must override both the address and the port.
     663                 :             :     // 2.2. For outbound connections we must override only the address.
     664                 :             :     // 3. A peer on a privacy network cannot see our address, so its view of us must not
     665                 :             :     //    be used, neither for inbound nor for outbound connections.
     666                 :             : 
     667                 :             :     // Pretend that we bound to this port.
     668                 :           1 :     const uint16_t bind_port = 20001;
     669   [ +  -  +  - ]:           2 :     m_node.args->ForceSetArg("-bind", strprintf("3.4.5.6:%u", bind_port));
     670                 :             : 
     671                 :             :     // Our address:port as seen from the peer, completely different from the above.
     672                 :           1 :     in_addr peer_us_addr;
     673                 :           1 :     peer_us_addr.s_addr = htonl(0x02030405);
     674                 :           1 :     const CService peer_us{peer_us_addr, 20002};
     675                 :             : 
     676                 :             :     // Create a peer with a routable IPv4 address (outbound).
     677                 :           1 :     in_addr peer_out_in_addr;
     678                 :           1 :     peer_out_in_addr.s_addr = htonl(0x01020304);
     679                 :           1 :     CNode peer_out{/*id=*/0,
     680                 :             :                    /*sock=*/nullptr,
     681         [ -  + ]:           1 :                    /*addrIn=*/CAddress{CService{peer_out_in_addr, 8333}, NODE_NETWORK},
     682                 :             :                    /*nKeyedNetGroupIn=*/0,
     683                 :             :                    /*nLocalHostNonceIn=*/0,
     684         [ +  - ]:           2 :                    /*addrBindIn=*/CService{},
     685                 :           1 :                    /*addrNameIn=*/std::string{},
     686                 :             :                    /*conn_type_in=*/ConnectionType::OUTBOUND_FULL_RELAY,
     687                 :             :                    /*inbound_onion=*/false,
     688   [ +  -  +  - ]:           2 :                    /*network_key=*/0};
     689         [ +  - ]:           1 :     peer_out.fSuccessfullyConnected = true;
     690         [ +  - ]:           1 :     peer_out.SetAddrLocal(peer_us);
     691                 :             : 
     692                 :             :     // Without the fix peer_us:8333 is chosen instead of the proper peer_us:bind_port.
     693         [ +  - ]:           1 :     auto chosen_local_addr = GetLocalAddrForPeer(peer_out);
     694   [ +  -  +  -  :           2 :     BOOST_REQUIRE(chosen_local_addr);
                   +  - ]
     695         [ +  - ]:           1 :     const CService expected{peer_us_addr, bind_port};
     696   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(*chosen_local_addr, expected);
     697                 :             : 
     698                 :             :     // Create a peer with a routable IPv4 address (inbound).
     699                 :           1 :     in_addr peer_in_in_addr;
     700                 :           1 :     peer_in_in_addr.s_addr = htonl(0x05060708);
     701                 :           1 :     CNode peer_in{/*id=*/0,
     702                 :             :                   /*sock=*/nullptr,
     703         [ -  + ]:           1 :                   /*addrIn=*/CAddress{CService{peer_in_in_addr, 8333}, NODE_NETWORK},
     704                 :             :                   /*nKeyedNetGroupIn=*/0,
     705                 :             :                   /*nLocalHostNonceIn=*/0,
     706         [ +  - ]:           2 :                   /*addrBindIn=*/CService{},
     707                 :           1 :                   /*addrNameIn=*/std::string{},
     708                 :             :                   /*conn_type_in=*/ConnectionType::INBOUND,
     709                 :             :                   /*inbound_onion=*/false,
     710   [ +  -  +  - ]:           2 :                   /*network_key=*/1};
     711         [ +  - ]:           1 :     peer_in.fSuccessfullyConnected = true;
     712         [ +  - ]:           1 :     peer_in.SetAddrLocal(peer_us);
     713                 :             : 
     714                 :             :     // Without the fix peer_us:8333 is chosen instead of the proper peer_us:peer_us.GetPort().
     715         [ +  - ]:           2 :     chosen_local_addr = GetLocalAddrForPeer(peer_in);
     716   [ +  -  +  -  :           2 :     BOOST_REQUIRE(chosen_local_addr);
                   +  - ]
     717   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(*chosen_local_addr, peer_us);
     718                 :             : 
     719                 :             :     // An inbound privacy network peer claims to see us at a routable IPv4 address. Nothing is advertised.
     720         [ +  - ]:           1 :     CNetAddr i2p_addr;
     721   [ +  -  +  -  :           2 :     BOOST_REQUIRE(i2p_addr.SetSpecial("udhdrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v4jna.b32.i2p"));
             +  -  +  - ]
     722                 :           1 :     CNode peer_in_i2p{/*id=*/0,
     723                 :             :                       /*sock=*/nullptr,
     724         [ -  + ]:           1 :                       /*addrIn=*/CAddress{CService{i2p_addr, I2P_SAM31_PORT}, NODE_NETWORK},
     725                 :             :                       /*nKeyedNetGroupIn=*/0,
     726                 :             :                       /*nLocalHostNonceIn=*/0,
     727         [ +  - ]:           2 :                       /*addrBindIn=*/CService{},
     728                 :           1 :                       /*addrNameIn=*/std::string{},
     729                 :             :                       /*conn_type_in=*/ConnectionType::INBOUND,
     730                 :             :                       /*inbound_onion=*/false,
     731   [ +  -  +  - ]:           2 :                       /*network_key=*/2};
     732         [ +  - ]:           1 :     peer_in_i2p.fSuccessfullyConnected = true;
     733         [ +  - ]:           1 :     peer_in_i2p.SetAddrLocal(peer_us);
     734   [ +  -  +  -  :           2 :     BOOST_CHECK(!GetLocalAddrForPeer(peer_in_i2p));
             +  -  +  - ]
     735                 :             : 
     736                 :             :     // Same for an outbound privacy network peer
     737         [ +  - ]:           1 :     CNetAddr onion_addr;
     738   [ +  -  +  -  :           2 :     BOOST_REQUIRE(onion_addr.SetSpecial("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion"));
             +  -  +  - ]
     739                 :           1 :     CNode peer_out_onion{/*id=*/0,
     740                 :             :                          /*sock=*/nullptr,
     741         [ -  + ]:           1 :                          /*addrIn=*/CAddress{CService{onion_addr, 8333}, NODE_NETWORK},
     742                 :             :                          /*nKeyedNetGroupIn=*/0,
     743                 :             :                          /*nLocalHostNonceIn=*/0,
     744         [ +  - ]:           2 :                          /*addrBindIn=*/CService{},
     745                 :           1 :                          /*addrNameIn=*/std::string{},
     746                 :             :                          /*conn_type_in=*/ConnectionType::OUTBOUND_FULL_RELAY,
     747                 :             :                          /*inbound_onion=*/false,
     748   [ +  -  +  - ]:           2 :                          /*network_key=*/3};
     749         [ +  - ]:           1 :     peer_out_onion.fSuccessfullyConnected = true;
     750         [ +  - ]:           1 :     peer_out_onion.SetAddrLocal(peer_us);
     751   [ +  -  +  -  :           2 :     BOOST_CHECK(!GetLocalAddrForPeer(peer_out_onion));
             +  -  +  - ]
     752                 :             : 
     753   [ +  -  +  -  :           2 :     m_node.args->ForceSetArg("-bind", "");
                   +  - ]
     754                 :           1 : }
     755                 :             : 
     756   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(LimitedAndReachable_Network)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     757                 :             : {
     758   [ +  -  +  - ]:           2 :     BOOST_CHECK(g_reachable_nets.Contains(NET_IPV4));
     759   [ +  -  +  - ]:           2 :     BOOST_CHECK(g_reachable_nets.Contains(NET_IPV6));
     760   [ +  -  +  - ]:           2 :     BOOST_CHECK(g_reachable_nets.Contains(NET_ONION));
     761   [ +  -  +  - ]:           2 :     BOOST_CHECK(g_reachable_nets.Contains(NET_I2P));
     762   [ +  -  +  - ]:           2 :     BOOST_CHECK(g_reachable_nets.Contains(NET_CJDNS));
     763                 :             : 
     764                 :           1 :     g_reachable_nets.Remove(NET_IPV4);
     765                 :           1 :     g_reachable_nets.Remove(NET_IPV6);
     766                 :           1 :     g_reachable_nets.Remove(NET_ONION);
     767                 :           1 :     g_reachable_nets.Remove(NET_I2P);
     768                 :           1 :     g_reachable_nets.Remove(NET_CJDNS);
     769                 :             : 
     770   [ +  -  +  - ]:           2 :     BOOST_CHECK(!g_reachable_nets.Contains(NET_IPV4));
     771   [ +  -  +  - ]:           2 :     BOOST_CHECK(!g_reachable_nets.Contains(NET_IPV6));
     772   [ +  -  +  - ]:           2 :     BOOST_CHECK(!g_reachable_nets.Contains(NET_ONION));
     773   [ +  -  +  - ]:           2 :     BOOST_CHECK(!g_reachable_nets.Contains(NET_I2P));
     774   [ +  -  +  - ]:           2 :     BOOST_CHECK(!g_reachable_nets.Contains(NET_CJDNS));
     775                 :             : 
     776                 :           1 :     g_reachable_nets.Add(NET_IPV4);
     777                 :           1 :     g_reachable_nets.Add(NET_IPV6);
     778                 :           1 :     g_reachable_nets.Add(NET_ONION);
     779                 :           1 :     g_reachable_nets.Add(NET_I2P);
     780                 :           1 :     g_reachable_nets.Add(NET_CJDNS);
     781                 :             : 
     782   [ +  -  +  - ]:           2 :     BOOST_CHECK(g_reachable_nets.Contains(NET_IPV4));
     783   [ +  -  +  - ]:           2 :     BOOST_CHECK(g_reachable_nets.Contains(NET_IPV6));
     784   [ +  -  +  - ]:           2 :     BOOST_CHECK(g_reachable_nets.Contains(NET_ONION));
     785   [ +  -  +  - ]:           2 :     BOOST_CHECK(g_reachable_nets.Contains(NET_I2P));
     786   [ +  -  +  - ]:           2 :     BOOST_CHECK(g_reachable_nets.Contains(NET_CJDNS));
     787                 :           1 : }
     788                 :             : 
     789   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(LimitedAndReachable_NetworkCaseUnroutableAndInternal)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     790                 :             : {
     791                 :             :     // Should be reachable by default.
     792   [ +  -  +  - ]:           2 :     BOOST_CHECK(g_reachable_nets.Contains(NET_UNROUTABLE));
     793   [ +  -  +  - ]:           2 :     BOOST_CHECK(g_reachable_nets.Contains(NET_INTERNAL));
     794                 :             : 
     795                 :           1 :     g_reachable_nets.RemoveAll();
     796                 :             : 
     797   [ +  -  +  - ]:           2 :     BOOST_CHECK(!g_reachable_nets.Contains(NET_UNROUTABLE));
     798   [ +  -  +  - ]:           2 :     BOOST_CHECK(!g_reachable_nets.Contains(NET_INTERNAL));
     799                 :             : 
     800                 :           1 :     g_reachable_nets.Add(NET_IPV4);
     801                 :           1 :     g_reachable_nets.Add(NET_IPV6);
     802                 :           1 :     g_reachable_nets.Add(NET_ONION);
     803                 :           1 :     g_reachable_nets.Add(NET_I2P);
     804                 :           1 :     g_reachable_nets.Add(NET_CJDNS);
     805                 :           1 :     g_reachable_nets.Add(NET_UNROUTABLE);
     806                 :           1 :     g_reachable_nets.Add(NET_INTERNAL);
     807                 :           1 : }
     808                 :             : 
     809                 :           3 : CNetAddr UtilBuildAddress(unsigned char p1, unsigned char p2, unsigned char p3, unsigned char p4)
     810                 :             : {
     811                 :           3 :     unsigned char ip[] = {p1, p2, p3, p4};
     812                 :             : 
     813                 :           3 :     struct sockaddr_in sa;
     814                 :           3 :     memset(&sa, 0, sizeof(sockaddr_in)); // initialize the memory block
     815                 :           3 :     memcpy(&(sa.sin_addr), &ip, sizeof(ip));
     816                 :           3 :     return CNetAddr(sa.sin_addr);
     817                 :             : }
     818                 :             : 
     819                 :             : 
     820   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(LimitedAndReachable_CNetAddr)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     821                 :             : {
     822                 :           1 :     CNetAddr addr = UtilBuildAddress(0x001, 0x001, 0x001, 0x001); // 1.1.1.1
     823                 :             : 
     824         [ +  - ]:           1 :     g_reachable_nets.Add(NET_IPV4);
     825   [ +  -  +  -  :           2 :     BOOST_CHECK(g_reachable_nets.Contains(addr));
             +  -  +  - ]
     826                 :             : 
     827         [ +  - ]:           1 :     g_reachable_nets.Remove(NET_IPV4);
     828   [ +  -  +  -  :           2 :     BOOST_CHECK(!g_reachable_nets.Contains(addr));
             +  -  +  - ]
     829                 :             : 
     830         [ +  - ]:           1 :     g_reachable_nets.Add(NET_IPV4); // have to reset this, because this is stateful.
     831                 :           1 : }
     832                 :             : 
     833                 :             : 
     834   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(LocalAddress_BasicLifecycle)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     835                 :             : {
     836         [ +  - ]:           1 :     CService addr = CService(UtilBuildAddress(0x002, 0x001, 0x001, 0x001), 1000); // 2.1.1.1:1000
     837                 :             : 
     838         [ +  - ]:           1 :     g_reachable_nets.Add(NET_IPV4);
     839                 :             : 
     840   [ +  -  +  -  :           2 :     BOOST_CHECK(!IsLocal(addr));
             +  -  +  - ]
     841   [ +  -  +  -  :           2 :     BOOST_CHECK(AddLocal(addr, 1000));
             +  -  +  - ]
     842   [ +  -  +  -  :           2 :     BOOST_CHECK(IsLocal(addr));
             +  -  +  - ]
     843                 :             : 
     844         [ +  - ]:           1 :     RemoveLocal(addr);
     845   [ +  -  +  -  :           2 :     BOOST_CHECK(!IsLocal(addr));
                   +  - ]
     846                 :           1 : }
     847                 :             : 
     848   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(LocalAddress_nScore_Overflow)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     849                 :             : {
     850                 :           1 :     g_reachable_nets.Add(NET_IPV4);
     851         [ +  - ]:           1 :     const CService addr{UtilBuildAddress(0x002, 0x001, 0x001, 0x001), 1000}; // 2.1.1.1:1000
     852                 :             : 
     853                 :           6 :     const auto get_score = [](const CService& service) -> int {
     854                 :           5 :         LOCK(g_maplocalhost_mutex);
     855         [ +  - ]:           5 :         const auto it = mapLocalHost.find(service);
     856         [ +  - ]:           5 :         return it != mapLocalHost.end() ? it->second.nScore : 0;
     857                 :           5 :     };
     858                 :             : 
     859                 :           1 :     const int initial_score = 1000;
     860   [ +  -  +  -  :           2 :     BOOST_REQUIRE(AddLocal(addr, initial_score));
             +  -  +  - ]
     861   [ +  -  +  -  :           2 :     BOOST_REQUIRE(IsLocal(addr));
             +  -  +  - ]
     862   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(get_score(addr), initial_score);
                   +  - ]
     863                 :             : 
     864                 :             :     // SeenLocal should increment nScore by 1.
     865   [ +  -  +  -  :           2 :     BOOST_CHECK(SeenLocal(addr));
             +  -  +  - ]
     866   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(get_score(addr), initial_score + 1);
                   +  - ]
     867                 :             : 
     868                 :             :     // AddLocal() saturates nScore when updating an existing entry at INT_MAX.
     869   [ +  -  +  -  :           2 :     BOOST_REQUIRE(AddLocal(addr, std::numeric_limits<int>::max()));
             +  -  +  - ]
     870   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(get_score(addr), std::numeric_limits<int>::max());
                   +  - ]
     871                 :             : 
     872   [ +  -  +  -  :           2 :     BOOST_CHECK(AddLocal(addr, std::numeric_limits<int>::max()));
             +  -  +  - ]
     873   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(get_score(addr), std::numeric_limits<int>::max());
                   +  - ]
     874                 :             : 
     875                 :             :     // SeenLocal() also saturates at INT_MAX.
     876   [ +  -  +  -  :           2 :     BOOST_CHECK(SeenLocal(addr));
             +  -  +  - ]
     877   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(get_score(addr), std::numeric_limits<int>::max());
                   +  - ]
     878                 :             : 
     879         [ +  - ]:           1 :     RemoveLocal(addr);
     880   [ +  -  +  -  :           2 :     BOOST_CHECK(!IsLocal(addr));
                   +  - ]
     881                 :           1 : }
     882                 :             : 
     883   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(initial_advertise_from_version_message)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     884                 :             : {
     885                 :           1 :     LOCK(NetEventsInterface::g_msgproc_mutex);
     886         [ +  - ]:           1 :     auto& connman{static_cast<ConnmanTestMsg&>(*m_node.connman)};
     887                 :             : 
     888                 :             :     // Tests the following scenario:
     889                 :             :     // * -bind=3.4.5.6:20001 is specified
     890                 :             :     // * we make an outbound connection to a peer
     891                 :             :     // * the peer reports he sees us as 2.3.4.5:20002 in the version message
     892                 :             :     //   (20002 is a random port assigned by our OS for the outgoing TCP connection,
     893                 :             :     //   we cannot accept connections to it)
     894                 :             :     // * we should self-advertise to that peer as 2.3.4.5:20001
     895                 :             : 
     896                 :             :     // Pretend that we bound to this port.
     897                 :           1 :     const uint16_t bind_port = 20001;
     898   [ +  -  +  -  :           2 :     m_node.args->ForceSetArg("-bind", strprintf("3.4.5.6:%u", bind_port));
                   +  - ]
     899         [ +  - ]:           1 :     m_node.connman->SetCaptureMessages(true);
     900                 :             : 
     901                 :             :     // Our address:port as seen from the peer - 2.3.4.5:20002 (different from the above).
     902                 :           1 :     in_addr peer_us_addr;
     903                 :           1 :     peer_us_addr.s_addr = htonl(0x02030405);
     904         [ +  - ]:           1 :     const CService peer_us{peer_us_addr, 20002};
     905                 :             : 
     906                 :             :     // Create a peer with a routable IPv4 address.
     907                 :           1 :     in_addr peer_in_addr;
     908                 :           1 :     peer_in_addr.s_addr = htonl(0x01020304);
     909                 :           1 :     CNode peer{/*id=*/0,
     910                 :             :                /*sock=*/nullptr,
     911         [ -  + ]:           1 :                /*addrIn=*/CAddress{CService{peer_in_addr, 8333}, NODE_NETWORK},
     912                 :             :                /*nKeyedNetGroupIn=*/0,
     913                 :             :                /*nLocalHostNonceIn=*/0,
     914         [ +  - ]:           2 :                /*addrBindIn=*/CService{},
     915                 :           1 :                /*addrNameIn=*/std::string{},
     916                 :             :                /*conn_type_in=*/ConnectionType::OUTBOUND_FULL_RELAY,
     917                 :             :                /*inbound_onion=*/false,
     918   [ +  -  +  - ]:           2 :                /*network_key=*/2};
     919                 :             : 
     920                 :           1 :     const uint64_t services{NODE_NETWORK | NODE_WITNESS};
     921                 :           1 :     const int64_t time{0};
     922                 :             : 
     923                 :             :     // Force ChainstateManager::IsInitialBlockDownload() to return false.
     924                 :             :     // Otherwise PushAddress() isn't called by PeerManager::ProcessMessage().
     925         [ +  - ]:           1 :     auto& chainman = static_cast<TestChainstateManager&>(*m_node.chainman);
     926         [ +  - ]:           1 :     chainman.JumpOutOfIbd();
     927                 :             : 
     928         [ +  - ]:           1 :     m_node.peerman->InitializeNode(peer, NODE_NETWORK);
     929                 :             : 
     930         [ +  - ]:           1 :     m_node.peerman->SendMessages(peer);
     931         [ +  - ]:           1 :     connman.FlushSendBuffer(peer); // Drop sent version message
     932                 :             : 
     933         [ +  - ]:           1 :     auto msg_version_receive =
     934   [ +  -  +  - ]:           1 :         NetMsg::Make(NetMsgType::VERSION, PROTOCOL_VERSION, services, time, services, CAddress::V1_NETWORK(peer_us));
     935   [ +  -  +  - ]:           1 :     Assert(connman.ReceiveMsgFrom(peer, std::move(msg_version_receive)));
     936         [ +  - ]:           1 :     peer.fPauseSend = false;
     937         [ +  - ]:           1 :     bool more_work{connman.ProcessMessagesOnce(peer)};
     938         [ +  - ]:           1 :     Assert(!more_work);
     939                 :             : 
     940         [ +  - ]:           1 :     m_node.peerman->SendMessages(peer);
     941         [ +  - ]:           1 :     connman.FlushSendBuffer(peer); // Drop sent verack message
     942                 :             : 
     943   [ +  -  +  -  :           2 :     Assert(connman.ReceiveMsgFrom(peer, NetMsg::Make(NetMsgType::VERACK)));
             +  -  +  - ]
     944         [ +  - ]:           1 :     peer.fPauseSend = false;
     945                 :             :     // Will set peer.fSuccessfullyConnected to true (necessary in SendMessages()).
     946         [ +  - ]:           1 :     more_work = connman.ProcessMessagesOnce(peer);
     947         [ +  - ]:           1 :     Assert(!more_work);
     948                 :             : 
     949                 :             :     // Ensure that peer_us_addr:bind_port is sent to the peer.
     950         [ +  - ]:           1 :     const CService expected{peer_us_addr, bind_port};
     951                 :           1 :     bool sent{false};
     952                 :             : 
     953         [ +  - ]:           1 :     const auto CaptureMessageOrig = CaptureMessage;
     954                 :           5 :     CaptureMessage = [&sent, &expected](const CAddress& addr,
     955                 :             :                                         const std::string& msg_type,
     956                 :             :                                         std::span<const unsigned char> data,
     957                 :             :                                         bool is_incoming) -> void {
     958   [ +  -  +  + ]:           4 :         if (!is_incoming && msg_type == "addr") {
     959                 :           1 :             std::vector<CAddress> addresses;
     960                 :             : 
     961         [ +  - ]:           1 :             SpanReader{data} >> CAddress::V1_NETWORK(addresses);
     962                 :             : 
     963         [ +  - ]:           1 :             for (const auto& addr : addresses) {
     964   [ +  -  +  - ]:           1 :                 if (addr == expected) {
     965                 :           1 :                     sent = true;
     966                 :           1 :                     return;
     967                 :             :                 }
     968                 :             :             }
     969                 :           1 :         }
     970                 :           1 :     };
     971                 :             : 
     972         [ +  - ]:           1 :     m_node.peerman->SendMessages(peer);
     973                 :             : 
     974   [ +  -  +  -  :           2 :     BOOST_CHECK(sent);
                   +  - ]
     975                 :             : 
     976         [ +  - ]:           1 :     CaptureMessage = CaptureMessageOrig;
     977         [ +  - ]:           1 :     chainman.ResetIbd();
     978         [ +  - ]:           1 :     m_node.connman->SetCaptureMessages(false);
     979   [ +  -  +  -  :           2 :     m_node.args->ForceSetArg("-bind", "");
                   +  - ]
     980         [ +  - ]:           3 : }
     981                 :             : 
     982                 :             : 
     983   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(advertise_local_address)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     984                 :             : {
     985                 :           8 :     auto CreatePeer = [](const CAddress& addr) {
     986                 :          14 :         return std::make_unique<CNode>(/*id=*/0,
     987                 :           7 :                                        /*sock=*/nullptr,
     988                 :             :                                        addr,
     989                 :           7 :                                        /*nKeyedNetGroupIn=*/0,
     990                 :           7 :                                        /*nLocalHostNonceIn=*/0,
     991                 :           7 :                                        CAddress{},
     992         [ +  - ]:           7 :                                        /*pszDest=*/std::string{},
     993                 :           7 :                                        ConnectionType::OUTBOUND_FULL_RELAY,
     994                 :           7 :                                        /*inbound_onion=*/false,
     995   [ +  -  +  - ]:          14 :                                        /*network_key=*/0);
     996                 :             :     };
     997                 :           1 :     g_reachable_nets.Add(NET_CJDNS);
     998                 :             : 
     999   [ +  -  +  - ]:           2 :     CAddress addr_ipv4{Lookup("1.2.3.4", 8333, false).value(), NODE_NONE};
    1000   [ +  -  +  -  :           2 :     BOOST_REQUIRE(addr_ipv4.IsValid());
             +  -  +  - ]
    1001   [ +  -  +  -  :           2 :     BOOST_REQUIRE(addr_ipv4.IsIPv4());
                   +  - ]
    1002                 :             : 
    1003   [ +  -  +  -  :           2 :     CAddress addr_ipv6{Lookup("1122:3344:5566:7788:9900:aabb:ccdd:eeff", 8333, false).value(), NODE_NONE};
                   +  - ]
    1004   [ +  -  +  -  :           2 :     BOOST_REQUIRE(addr_ipv6.IsValid());
             +  -  +  - ]
    1005   [ +  -  +  -  :           2 :     BOOST_REQUIRE(addr_ipv6.IsIPv6());
                   +  - ]
    1006                 :             : 
    1007   [ +  -  +  -  :           2 :     CAddress addr_ipv6_tunnel{Lookup("2002:3344:5566:7788:9900:aabb:ccdd:eeff", 8333, false).value(), NODE_NONE};
                   +  - ]
    1008   [ +  -  +  -  :           2 :     BOOST_REQUIRE(addr_ipv6_tunnel.IsValid());
             +  -  +  - ]
    1009   [ +  -  +  -  :           2 :     BOOST_REQUIRE(addr_ipv6_tunnel.IsIPv6());
                   +  - ]
    1010   [ +  -  +  -  :           2 :     BOOST_REQUIRE(addr_ipv6_tunnel.IsRFC3964());
             +  -  +  - ]
    1011                 :             : 
    1012   [ +  -  +  -  :           2 :     CAddress addr_teredo{Lookup("2001:0000:5566:7788:9900:aabb:ccdd:eeff", 8333, false).value(), NODE_NONE};
                   +  - ]
    1013   [ +  -  +  -  :           2 :     BOOST_REQUIRE(addr_teredo.IsValid());
             +  -  +  - ]
    1014   [ +  -  +  -  :           2 :     BOOST_REQUIRE(addr_teredo.IsIPv6());
                   +  - ]
    1015   [ +  -  +  -  :           2 :     BOOST_REQUIRE(addr_teredo.IsRFC4380());
             +  -  +  - ]
    1016                 :             : 
    1017         [ +  - ]:           1 :     CAddress addr_onion;
    1018   [ +  -  +  -  :           2 :     BOOST_REQUIRE(addr_onion.SetSpecial("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion"));
             +  -  +  - ]
    1019   [ +  -  +  -  :           2 :     BOOST_REQUIRE(addr_onion.IsValid());
             +  -  +  - ]
    1020   [ +  -  +  -  :           2 :     BOOST_REQUIRE(addr_onion.IsTor());
                   +  - ]
    1021                 :             : 
    1022         [ +  - ]:           1 :     CAddress addr_i2p;
    1023   [ +  -  +  -  :           2 :     BOOST_REQUIRE(addr_i2p.SetSpecial("udhdrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v4jna.b32.i2p"));
             +  -  +  - ]
    1024   [ +  -  +  -  :           2 :     BOOST_REQUIRE(addr_i2p.IsValid());
             +  -  +  - ]
    1025   [ +  -  +  -  :           2 :     BOOST_REQUIRE(addr_i2p.IsI2P());
                   +  - ]
    1026                 :             : 
    1027   [ +  -  +  -  :           3 :     CService service_cjdns{Lookup("fc00:3344:5566:7788:9900:aabb:ccdd:eeff", 8333, false).value(), NODE_NONE};
             +  -  +  - ]
    1028         [ +  - ]:           2 :     CAddress addr_cjdns{MaybeFlipIPv6toCJDNS(service_cjdns), NODE_NONE};
    1029   [ +  -  +  -  :           2 :     BOOST_REQUIRE(addr_cjdns.IsValid());
             +  -  +  - ]
    1030   [ +  -  +  -  :           2 :     BOOST_REQUIRE(addr_cjdns.IsCJDNS());
                   +  - ]
    1031                 :             : 
    1032         [ +  - ]:           1 :     const auto peer_ipv4{CreatePeer(addr_ipv4)};
    1033         [ +  - ]:           1 :     const auto peer_ipv6{CreatePeer(addr_ipv6)};
    1034         [ +  - ]:           1 :     const auto peer_ipv6_tunnel{CreatePeer(addr_ipv6_tunnel)};
    1035         [ +  - ]:           1 :     const auto peer_teredo{CreatePeer(addr_teredo)};
    1036         [ +  - ]:           1 :     const auto peer_onion{CreatePeer(addr_onion)};
    1037         [ +  - ]:           1 :     const auto peer_i2p{CreatePeer(addr_i2p)};
    1038         [ +  - ]:           1 :     const auto peer_cjdns{CreatePeer(addr_cjdns)};
    1039                 :             : 
    1040                 :             :     // one local clearnet address - advertise to all but privacy peers
    1041         [ +  - ]:           1 :     AddLocal(addr_ipv4);
    1042   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(GetLocalAddress(*peer_ipv4), addr_ipv4);
                   +  - ]
    1043   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(GetLocalAddress(*peer_ipv6), addr_ipv4);
                   +  - ]
    1044   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(GetLocalAddress(*peer_ipv6_tunnel), addr_ipv4);
                   +  - ]
    1045   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(GetLocalAddress(*peer_teredo), addr_ipv4);
                   +  - ]
    1046   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(GetLocalAddress(*peer_cjdns), addr_ipv4);
                   +  - ]
    1047   [ +  -  +  -  :           2 :     BOOST_CHECK(!GetLocalAddress(*peer_onion).IsValid());
          +  -  +  -  +  
                      - ]
    1048   [ +  -  +  -  :           2 :     BOOST_CHECK(!GetLocalAddress(*peer_i2p).IsValid());
          +  -  +  -  +  
                      - ]
    1049         [ +  - ]:           1 :     RemoveLocal(addr_ipv4);
    1050                 :             : 
    1051                 :             :     // local privacy addresses - don't advertise to clearnet peers
    1052         [ +  - ]:           1 :     AddLocal(addr_onion);
    1053         [ +  - ]:           1 :     AddLocal(addr_i2p);
    1054   [ +  -  +  -  :           2 :     BOOST_CHECK(!GetLocalAddress(*peer_ipv4).IsValid());
          +  -  +  -  +  
                      - ]
    1055   [ +  -  +  -  :           2 :     BOOST_CHECK(!GetLocalAddress(*peer_ipv6).IsValid());
          +  -  +  -  +  
                      - ]
    1056   [ +  -  +  -  :           2 :     BOOST_CHECK(!GetLocalAddress(*peer_ipv6_tunnel).IsValid());
          +  -  +  -  +  
                      - ]
    1057   [ +  -  +  -  :           2 :     BOOST_CHECK(!GetLocalAddress(*peer_teredo).IsValid());
          +  -  +  -  +  
                      - ]
    1058   [ +  -  +  -  :           2 :     BOOST_CHECK(!GetLocalAddress(*peer_cjdns).IsValid());
          +  -  +  -  +  
                      - ]
    1059   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(GetLocalAddress(*peer_onion), addr_onion);
                   +  - ]
    1060   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(GetLocalAddress(*peer_i2p), addr_i2p);
                   +  - ]
    1061         [ +  - ]:           1 :     RemoveLocal(addr_onion);
    1062         [ +  - ]:           1 :     RemoveLocal(addr_i2p);
    1063                 :             : 
    1064                 :             :     // local addresses from all networks
    1065         [ +  - ]:           1 :     AddLocal(addr_ipv4);
    1066         [ +  - ]:           1 :     AddLocal(addr_ipv6);
    1067         [ +  - ]:           1 :     AddLocal(addr_ipv6_tunnel);
    1068         [ +  - ]:           1 :     AddLocal(addr_teredo);
    1069         [ +  - ]:           1 :     AddLocal(addr_onion);
    1070         [ +  - ]:           1 :     AddLocal(addr_i2p);
    1071         [ +  - ]:           1 :     AddLocal(addr_cjdns);
    1072   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(GetLocalAddress(*peer_ipv4), addr_ipv4);
                   +  - ]
    1073   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(GetLocalAddress(*peer_ipv6), addr_ipv6);
                   +  - ]
    1074   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(GetLocalAddress(*peer_ipv6_tunnel), addr_ipv6);
                   +  - ]
    1075   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(GetLocalAddress(*peer_teredo), addr_ipv4);
                   +  - ]
    1076   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(GetLocalAddress(*peer_onion), addr_onion);
                   +  - ]
    1077   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(GetLocalAddress(*peer_i2p), addr_i2p);
                   +  - ]
    1078   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(GetLocalAddress(*peer_cjdns), addr_cjdns);
                   +  - ]
    1079         [ +  - ]:           1 :     RemoveLocal(addr_ipv4);
    1080         [ +  - ]:           1 :     RemoveLocal(addr_ipv6);
    1081         [ +  - ]:           1 :     RemoveLocal(addr_ipv6_tunnel);
    1082         [ +  - ]:           1 :     RemoveLocal(addr_teredo);
    1083         [ +  - ]:           1 :     RemoveLocal(addr_onion);
    1084         [ +  - ]:           1 :     RemoveLocal(addr_i2p);
    1085         [ +  - ]:           1 :     RemoveLocal(addr_cjdns);
    1086                 :           1 : }
    1087                 :             : 
    1088                 :             : namespace {
    1089                 :             : 
    1090                 :          75 : CKey GenerateRandomTestKey(FastRandomContext& rng) noexcept
    1091                 :             : {
    1092                 :          75 :     CKey key;
    1093                 :          75 :     uint256 key_data = rng.rand256();
    1094                 :          75 :     key.Set(key_data.begin(), key_data.end(), true);
    1095                 :          75 :     return key;
    1096                 :             : }
    1097                 :             : 
    1098                 :             : /** A class for scenario-based tests of V2Transport
    1099                 :             :  *
    1100                 :             :  * Each V2TransportTester encapsulates a V2Transport (the one being tested), and can be told to
    1101                 :             :  * interact with it. To do so, it also encapsulates a BIP324Cipher to act as the other side. A
    1102                 :             :  * second V2Transport is not used, as doing so would not permit scenarios that involve sending
    1103                 :             :  * invalid data, or ones using BIP324 features that are not implemented on the sending
    1104                 :             :  * side (like decoy packets).
    1105                 :             :  */
    1106                 :             : class V2TransportTester
    1107                 :             : {
    1108                 :             :     FastRandomContext& m_rng;
    1109                 :             :     V2Transport m_transport; //!< V2Transport being tested
    1110                 :             :     BIP324Cipher m_cipher; //!< Cipher to help with the other side
    1111                 :             :     bool m_test_initiator; //!< Whether m_transport is the initiator (true) or responder (false)
    1112                 :             : 
    1113                 :             :     std::vector<uint8_t> m_sent_garbage; //!< The garbage we've sent to m_transport.
    1114                 :             :     std::vector<uint8_t> m_recv_garbage; //!< The garbage we've received from m_transport.
    1115                 :             :     std::vector<uint8_t> m_to_send; //!< Bytes we have queued up to send to m_transport.
    1116                 :             :     std::vector<uint8_t> m_received; //!< Bytes we have received from m_transport.
    1117                 :             :     std::deque<CSerializedNetMsg> m_msg_to_send; //!< Messages to be sent *by* m_transport to us.
    1118                 :             :     bool m_sent_aad{false};
    1119                 :             : 
    1120                 :             : public:
    1121                 :             :     /** Construct a tester object. test_initiator: whether the tested transport is initiator. */
    1122                 :          75 :     explicit V2TransportTester(FastRandomContext& rng, bool test_initiator)
    1123                 :          75 :         : m_rng{rng},
    1124                 :          75 :           m_transport{0, test_initiator},
    1125                 :          75 :           m_cipher{GenerateRandomTestKey(m_rng), MakeByteSpan(m_rng.rand256())},
    1126         [ +  - ]:          75 :           m_test_initiator(test_initiator) {}
    1127                 :             : 
    1128                 :             :     /** Data type returned by Interact:
    1129                 :             :      *
    1130                 :             :      * - std::nullopt: transport error occurred
    1131                 :             :      * - otherwise: a vector of
    1132                 :             :      *   - std::nullopt: invalid message received
    1133                 :             :      *   - otherwise: a CNetMessage retrieved
    1134                 :             :      */
    1135                 :             :     using InteractResult = std::optional<std::vector<std::optional<CNetMessage>>>;
    1136                 :             : 
    1137                 :             :     /** Send/receive scheduled/available bytes and messages.
    1138                 :             :      *
    1139                 :             :      * This is the only function that interacts with the transport being tested; everything else is
    1140                 :             :      * scheduling things done by Interact(), or processing things learned by it.
    1141                 :             :      */
    1142                 :         239 :     InteractResult Interact()
    1143                 :             :     {
    1144                 :         239 :         std::vector<std::optional<CNetMessage>> ret;
    1145                 :        3052 :         while (true) {
    1146                 :        3052 :             bool progress{false};
    1147                 :             :             // Send bytes from m_to_send to the transport.
    1148         [ +  + ]:        3052 :             if (!m_to_send.empty()) {
    1149         [ -  + ]:        2347 :                 std::span<const uint8_t> to_send = std::span{m_to_send}.first(1 + m_rng.randrange(m_to_send.size()));
    1150                 :        2347 :                 size_t old_len = to_send.size();
    1151         [ +  + ]:        2347 :                 if (!m_transport.ReceivedBytes(to_send)) {
    1152                 :          23 :                     return std::nullopt; // transport error occurred
    1153                 :             :                 }
    1154         [ +  + ]:        2324 :                 if (old_len != to_send.size()) {
    1155                 :        2219 :                     progress = true;
    1156                 :        2219 :                     m_to_send.erase(m_to_send.begin(), m_to_send.begin() + (old_len - to_send.size()));
    1157                 :             :                 }
    1158                 :             :             }
    1159                 :             :             // Retrieve messages received by the transport.
    1160   [ +  +  +  +  :        3029 :             if (m_transport.ReceivedMessageComplete() && (!progress || m_rng.randbool())) {
                   +  + ]
    1161                 :         272 :                 bool reject{false};
    1162                 :         272 :                 auto msg = m_transport.GetReceivedMessage({}, reject);
    1163         [ +  + ]:         272 :                 if (reject) {
    1164         [ +  - ]:          71 :                     ret.emplace_back(std::nullopt);
    1165                 :             :                 } else {
    1166         [ +  - ]:         201 :                     ret.emplace_back(std::move(msg));
    1167                 :             :                 }
    1168                 :         272 :                 progress = true;
    1169                 :         272 :             }
    1170                 :             :             // Enqueue a message to be sent by the transport to us.
    1171   [ +  +  +  -  :        3029 :             if (!m_msg_to_send.empty() && (!progress || m_rng.randbool())) {
                   +  + ]
    1172         [ +  - ]:          51 :                 if (m_transport.SetMessageToSend(m_msg_to_send.front())) {
    1173                 :          51 :                     m_msg_to_send.pop_front();
    1174                 :          51 :                     progress = true;
    1175                 :             :                 }
    1176                 :             :             }
    1177                 :             :             // Receive bytes from the transport.
    1178         [ +  + ]:        3029 :             const auto& [recv_bytes, _more, _msg_type] = m_transport.GetBytesToSend(!m_msg_to_send.empty());
    1179   [ +  +  +  +  :        3029 :             if (!recv_bytes.empty() && (!progress || m_rng.randbool())) {
                   +  + ]
    1180                 :         916 :                 size_t to_receive = 1 + m_rng.randrange(recv_bytes.size());
    1181         [ +  - ]:         916 :                 m_received.insert(m_received.end(), recv_bytes.begin(), recv_bytes.begin() + to_receive);
    1182                 :         916 :                 progress = true;
    1183                 :         916 :                 m_transport.MarkBytesSent(to_receive);
    1184                 :             :             }
    1185         [ +  + ]:        3029 :             if (!progress) break;
    1186                 :             :         }
    1187                 :         216 :         return ret;
    1188                 :         239 :     }
    1189                 :             : 
    1190                 :             :     /** Expose the cipher. */
    1191                 :             :     BIP324Cipher& GetCipher() { return m_cipher; }
    1192                 :             : 
    1193                 :             :     /** Schedule bytes to be sent to the transport. */
    1194                 :       47572 :     void Send(std::span<const uint8_t> data)
    1195                 :             :     {
    1196                 :       47572 :         m_to_send.insert(m_to_send.end(), data.begin(), data.end());
    1197                 :       47572 :     }
    1198                 :             : 
    1199                 :             :     /** Send V1 version message header to the transport. */
    1200                 :           2 :     void SendV1Version(const MessageStartChars& magic)
    1201                 :             :     {
    1202                 :           2 :         CMessageHeader hdr(magic, "version", 126 + m_rng.randrange(11));
    1203                 :           2 :         DataStream ser{};
    1204         [ +  - ]:           2 :         ser << hdr;
    1205   [ -  +  +  - ]:           2 :         m_to_send.insert(m_to_send.end(), UCharCast(ser.data()), UCharCast(ser.data() + ser.size()));
    1206                 :           2 :     }
    1207                 :             : 
    1208                 :             :     /** Schedule bytes to be sent to the transport. */
    1209                 :       47499 :     void Send(std::span<const std::byte> data) { Send(MakeUCharSpan(data)); }
    1210                 :             : 
    1211                 :             :     /** Schedule our ellswift key to be sent to the transport. */
    1212                 :          73 :     void SendKey() { Send(m_cipher.GetOurPubKey()); }
    1213                 :             : 
    1214                 :             :     /** Schedule specified garbage to be sent to the transport. */
    1215                 :          73 :     void SendGarbage(std::span<const uint8_t> garbage)
    1216                 :             :     {
    1217                 :             :         // Remember the specified garbage (so we can use it as AAD).
    1218                 :          73 :         m_sent_garbage.assign(garbage.begin(), garbage.end());
    1219                 :             :         // Schedule it for sending.
    1220         [ -  + ]:          73 :         Send(m_sent_garbage);
    1221                 :          73 :     }
    1222                 :             : 
    1223                 :             :     /** Schedule garbage (of specified length) to be sent to the transport. */
    1224                 :          72 :     void SendGarbage(size_t garbage_len)
    1225                 :             :     {
    1226                 :             :         // Generate random garbage and send it.
    1227   [ -  +  +  - ]:          72 :         SendGarbage(m_rng.randbytes<uint8_t>(garbage_len));
    1228                 :          72 :     }
    1229                 :             : 
    1230                 :             :     /** Schedule garbage (with valid random length) to be sent to the transport. */
    1231                 :          20 :     void SendGarbage()
    1232                 :             :     {
    1233                 :          20 :          SendGarbage(m_rng.randrange(V2Transport::MAX_GARBAGE_LEN + 1));
    1234                 :          20 :     }
    1235                 :             : 
    1236                 :             :     /** Schedule a message to be sent to us by the transport. */
    1237                 :          51 :     void AddMessage(std::string m_type, std::vector<uint8_t> payload)
    1238                 :             :     {
    1239                 :          51 :         CSerializedNetMsg msg;
    1240                 :          51 :         msg.m_type = std::move(m_type);
    1241                 :          51 :         msg.data = std::move(payload);
    1242         [ +  - ]:          51 :         m_msg_to_send.push_back(std::move(msg));
    1243                 :          51 :     }
    1244                 :             : 
    1245                 :             :     /** Expect ellswift key to have been received from transport and process it.
    1246                 :             :      *
    1247                 :             :      * Many other V2TransportTester functions cannot be called until after ReceiveKey() has been
    1248                 :             :      * called, as no encryption keys are set up before that point.
    1249                 :             :      */
    1250                 :          73 :     void ReceiveKey()
    1251                 :             :     {
    1252                 :             :         // When processing a key, enough bytes need to have been received already.
    1253   [ -  +  +  - ]:         146 :         BOOST_REQUIRE(m_received.size() >= EllSwiftPubKey::size());
    1254                 :             :         // Initialize the cipher using it (acting as the opposite side of the tested transport).
    1255                 :          73 :         m_cipher.Initialize(MakeByteSpan(m_received).first(EllSwiftPubKey::size()), !m_test_initiator);
    1256                 :             :         // Strip the processed bytes off the front of the receive buffer.
    1257                 :          73 :         m_received.erase(m_received.begin(), m_received.begin() + EllSwiftPubKey::size());
    1258                 :          73 :     }
    1259                 :             : 
    1260                 :             :     /** Schedule an encrypted packet with specified content/aad/ignore to be sent to transport
    1261                 :             :      *  (only after ReceiveKey). */
    1262                 :       47353 :     void SendPacket(std::span<const uint8_t> content, std::span<const uint8_t> aad = {}, bool ignore = false)
    1263                 :             :     {
    1264                 :             :         // Use cipher to construct ciphertext.
    1265                 :       47353 :         std::vector<std::byte> ciphertext;
    1266         [ +  - ]:       47353 :         ciphertext.resize(content.size() + BIP324Cipher::EXPANSION);
    1267         [ -  + ]:       47353 :         m_cipher.Encrypt(
    1268                 :             :             /*contents=*/MakeByteSpan(content),
    1269                 :             :             /*aad=*/MakeByteSpan(aad),
    1270                 :             :             /*ignore=*/ignore,
    1271                 :       47353 :             /*output=*/ciphertext);
    1272                 :             :         // Schedule it for sending.
    1273   [ -  +  +  - ]:       47353 :         Send(ciphertext);
    1274                 :       47353 :     }
    1275                 :             : 
    1276                 :             :     /** Schedule garbage terminator to be sent to the transport (only after ReceiveKey). */
    1277                 :          73 :     void SendGarbageTerm()
    1278                 :             :     {
    1279                 :             :         // Schedule the garbage terminator to be sent.
    1280                 :          73 :         Send(m_cipher.GetSendGarbageTerminator());
    1281                 :          73 :     }
    1282                 :             : 
    1283                 :             :     /** Schedule version packet to be sent to the transport (only after ReceiveKey). */
    1284                 :         283 :     void SendVersion(std::span<const uint8_t> version_data = {}, bool vers_ignore = false)
    1285                 :             :     {
    1286                 :         283 :         std::span<const std::uint8_t> aad;
    1287                 :             :         // Set AAD to garbage only for first packet.
    1288   [ +  +  -  + ]:         283 :         if (!m_sent_aad) aad = m_sent_garbage;
    1289                 :         283 :         SendPacket(/*content=*/version_data, /*aad=*/aad, /*ignore=*/vers_ignore);
    1290                 :         283 :         m_sent_aad = true;
    1291                 :         283 :     }
    1292                 :             : 
    1293                 :             :     /** Expect a packet to have been received from transport, process it, and return its contents
    1294                 :             :      *  (only after ReceiveKey). Decoys are skipped. Optional associated authenticated data (AAD) is
    1295                 :             :      *  expected in the first received packet, no matter if that is a decoy or not. */
    1296                 :         122 :     std::vector<uint8_t> ReceivePacket(std::span<const std::byte> aad = {})
    1297                 :             :     {
    1298                 :         122 :         std::vector<uint8_t> contents;
    1299                 :             :         // Loop as long as there are ignored packets that are to be skipped.
    1300                 :         122 :         while (true) {
    1301                 :             :             // When processing a packet, at least enough bytes for its length descriptor must be received.
    1302   [ +  -  -  +  :         244 :             BOOST_REQUIRE(m_received.size() >= BIP324Cipher::LENGTH_LEN);
             +  -  -  + ]
    1303                 :             :             // Decrypt the content length.
    1304         [ -  + ]:         122 :             size_t size = m_cipher.DecryptLength(MakeByteSpan(std::span{m_received}.first(BIP324Cipher::LENGTH_LEN)));
    1305                 :             :             // Check that the full packet is in the receive buffer.
    1306   [ +  -  -  +  :         244 :             BOOST_REQUIRE(m_received.size() >= size + BIP324Cipher::EXPANSION);
             +  -  +  - ]
    1307                 :             :             // Decrypt the packet contents.
    1308         [ +  - ]:         122 :             contents.resize(size);
    1309                 :         122 :             bool ignore{false};
    1310                 :         122 :             bool ret = m_cipher.Decrypt(
    1311                 :             :                 /*input=*/MakeByteSpan(
    1312         [ -  + ]:         122 :                     std::span{m_received}.first(size + BIP324Cipher::EXPANSION).subspan(BIP324Cipher::LENGTH_LEN)),
    1313                 :             :                 /*aad=*/aad,
    1314                 :             :                 /*ignore=*/ignore,
    1315                 :             :                 /*contents=*/MakeWritableByteSpan(contents));
    1316   [ +  -  +  - ]:         244 :             BOOST_CHECK(ret);
    1317                 :             :             // Don't expect AAD in further packets.
    1318                 :         122 :             aad = {};
    1319                 :             :             // Strip the processed packet's bytes off the front of the receive buffer.
    1320                 :         122 :             m_received.erase(m_received.begin(), m_received.begin() + size + BIP324Cipher::EXPANSION);
    1321                 :             :             // Stop if the ignore bit is not set on this packet.
    1322         [ -  + ]:         122 :             if (!ignore) break;
    1323                 :             :         }
    1324                 :         122 :         return contents;
    1325                 :           0 :     }
    1326                 :             : 
    1327                 :             :     /** Expect garbage and garbage terminator to have been received, and process them (only after
    1328                 :             :      *  ReceiveKey). */
    1329                 :          71 :     void ReceiveGarbage()
    1330                 :             :     {
    1331                 :             :         // Figure out the garbage length.
    1332                 :          71 :         size_t garblen;
    1333         [ +  - ]:      154766 :         for (garblen = 0; garblen <= V2Transport::MAX_GARBAGE_LEN; ++garblen) {
    1334   [ -  +  +  -  :      309532 :             BOOST_REQUIRE(m_received.size() >= garblen + BIP324Cipher::GARBAGE_TERMINATOR_LEN);
                   -  + ]
    1335   [ -  +  +  + ]:      154766 :             auto term_span = MakeByteSpan(std::span{m_received}.subspan(garblen, BIP324Cipher::GARBAGE_TERMINATOR_LEN));
    1336         [ +  + ]:      154766 :             if (std::ranges::equal(term_span, m_cipher.GetReceiveGarbageTerminator())) break;
    1337                 :             :         }
    1338                 :             :         // Copy the garbage to a buffer.
    1339                 :          71 :         m_recv_garbage.assign(m_received.begin(), m_received.begin() + garblen);
    1340                 :             :         // Strip garbage + garbage terminator off the front of the receive buffer.
    1341                 :          71 :         m_received.erase(m_received.begin(), m_received.begin() + garblen + BIP324Cipher::GARBAGE_TERMINATOR_LEN);
    1342                 :          71 :     }
    1343                 :             : 
    1344                 :             :     /** Expect version packet to have been received, and process it (only after ReceiveKey). */
    1345                 :          71 :     void ReceiveVersion()
    1346                 :             :     {
    1347                 :          71 :         auto contents = ReceivePacket(/*aad=*/MakeByteSpan(m_recv_garbage));
    1348                 :             :         // Version packets from real BIP324 peers are expected to be empty, despite the fact that
    1349                 :             :         // this class supports *sending* non-empty version packets (to test that BIP324 peers
    1350                 :             :         // correctly ignore version packet contents).
    1351   [ +  -  +  - ]:         142 :         BOOST_CHECK(contents.empty());
    1352                 :          71 :     }
    1353                 :             : 
    1354                 :             :     /** Expect application packet to have been received, with specified short id and payload.
    1355                 :             :      *  (only after ReceiveKey). */
    1356                 :           1 :     void ReceiveMessage(uint8_t short_id, std::span<const uint8_t> payload)
    1357                 :             :     {
    1358                 :           1 :         auto ret = ReceivePacket();
    1359   [ +  -  -  +  :           2 :         BOOST_CHECK(ret.size() == payload.size() + 1);
             +  -  +  - ]
    1360   [ +  -  +  -  :           2 :         BOOST_CHECK(ret[0] == short_id);
                   +  - ]
    1361   [ +  -  -  +  :           2 :         BOOST_CHECK(std::ranges::equal(std::span{ret}.subspan(1), payload));
                   +  - ]
    1362                 :           1 :     }
    1363                 :             : 
    1364                 :             :     /** Expect application packet to have been received, with specified 12-char message type and
    1365                 :             :      *  payload (only after ReceiveKey). */
    1366                 :          50 :     void ReceiveMessage(const std::string& m_type, std::span<const uint8_t> payload)
    1367                 :             :     {
    1368                 :          50 :         auto ret = ReceivePacket();
    1369   [ +  -  -  +  :         100 :         BOOST_REQUIRE(ret.size() == payload.size() + 1 + CMessageHeader::MESSAGE_TYPE_SIZE);
             +  -  +  - ]
    1370   [ +  -  +  - ]:         100 :         BOOST_CHECK(ret[0] == 0);
    1371         [ +  + ]:         650 :         for (unsigned i = 0; i < 12; ++i) {
    1372   [ -  +  +  + ]:         600 :             if (i < m_type.size()) {
    1373   [ +  -  +  - ]:         600 :                 BOOST_CHECK(ret[1 + i] == uint8_t(m_type[i]));
    1374                 :             :             } else {
    1375   [ +  -  +  - ]:         900 :                 BOOST_CHECK(ret[1 + i] == 0);
    1376                 :             :             }
    1377                 :             :         }
    1378   [ +  -  -  +  :         100 :         BOOST_CHECK(std::ranges::equal(std::span{ret}.subspan(1 + CMessageHeader::MESSAGE_TYPE_SIZE), payload));
                   +  - ]
    1379                 :          50 :     }
    1380                 :             : 
    1381                 :             :     /** Schedule an encrypted packet with specified message type and payload to be sent to
    1382                 :             :      *  transport (only after ReceiveKey). */
    1383                 :         140 :     void SendMessage(std::string mtype, std::span<const uint8_t> payload)
    1384                 :             :     {
    1385                 :             :         // Construct contents consisting of 0x00 + 12-byte message type + payload.
    1386                 :         140 :         std::vector<uint8_t> contents(1 + CMessageHeader::MESSAGE_TYPE_SIZE + payload.size());
    1387         [ -  + ]:         140 :         std::copy(mtype.begin(), mtype.end(), contents.begin() + 1);
    1388                 :         140 :         std::copy(payload.begin(), payload.end(), contents.begin() + 1 + CMessageHeader::MESSAGE_TYPE_SIZE);
    1389                 :             :         // Send a packet with that as contents.
    1390   [ -  +  +  - ]:         140 :         SendPacket(contents);
    1391                 :         140 :     }
    1392                 :             : 
    1393                 :             :     /** Schedule an encrypted packet with specified short message id and payload to be sent to
    1394                 :             :      *  transport (only after ReceiveKey). */
    1395                 :         152 :     void SendMessage(uint8_t short_id, std::span<const uint8_t> payload)
    1396                 :             :     {
    1397                 :             :         // Construct contents consisting of short_id + payload.
    1398                 :         152 :         std::vector<uint8_t> contents(1 + payload.size());
    1399                 :         152 :         contents[0] = short_id;
    1400                 :         152 :         std::copy(payload.begin(), payload.end(), contents.begin() + 1);
    1401                 :             :         // Send a packet with that as contents.
    1402   [ -  +  +  - ]:         152 :         SendPacket(contents);
    1403                 :         152 :     }
    1404                 :             : 
    1405                 :             :     /** Test whether the transport's session ID matches the session ID we expect. */
    1406                 :          71 :     void CompareSessionIDs() const
    1407                 :             :     {
    1408                 :          71 :         auto info = m_transport.GetInfo();
    1409         [ +  - ]:         142 :         BOOST_CHECK(info.session_id);
    1410   [ -  +  +  - ]:         142 :         BOOST_CHECK(uint256(MakeUCharSpan(m_cipher.GetSessionID())) == *info.session_id);
    1411                 :          71 :     }
    1412                 :             : 
    1413                 :             :     /** Introduce a bit error in the data scheduled to be sent. */
    1414                 :          10 :     void Damage()
    1415                 :             :     {
    1416         [ -  + ]:          10 :         m_to_send[m_rng.randrange(m_to_send.size())] ^= (uint8_t{1} << m_rng.randrange(8));
    1417                 :          10 :     }
    1418                 :             : };
    1419                 :             : 
    1420                 :             : } // namespace
    1421                 :             : 
    1422   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(v2transport_test)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
    1423                 :             : {
    1424                 :             :     // A mostly normal scenario, testing a transport in initiator mode.
    1425         [ +  + ]:          11 :     for (int i = 0; i < 10; ++i) {
    1426                 :          10 :         V2TransportTester tester(m_rng, true);
    1427         [ +  - ]:          10 :         auto ret = tester.Interact();
    1428   [ +  -  +  -  :          20 :         BOOST_REQUIRE(ret);
                   +  - ]
    1429   [ +  -  +  -  :          20 :         BOOST_REQUIRE(ret->empty());
                   +  - ]
    1430         [ +  - ]:          10 :         tester.SendKey();
    1431         [ +  - ]:          10 :         tester.SendGarbage();
    1432         [ +  - ]:          10 :         tester.ReceiveKey();
    1433         [ +  - ]:          10 :         tester.SendGarbageTerm();
    1434         [ +  - ]:          10 :         tester.SendVersion();
    1435         [ +  - ]:          20 :         ret = tester.Interact();
    1436   [ +  -  +  -  :          20 :         BOOST_REQUIRE(ret);
                   +  - ]
    1437   [ +  -  +  -  :          20 :         BOOST_REQUIRE(ret->empty());
                   +  - ]
    1438         [ +  - ]:          10 :         tester.ReceiveGarbage();
    1439         [ +  - ]:          10 :         tester.ReceiveVersion();
    1440         [ +  - ]:          10 :         tester.CompareSessionIDs();
    1441                 :          10 :         auto msg_data_1 = m_rng.randbytes<uint8_t>(m_rng.randrange(100000));
    1442                 :          10 :         auto msg_data_2 = m_rng.randbytes<uint8_t>(m_rng.randrange(1000));
    1443   [ -  +  +  - ]:          10 :         tester.SendMessage(uint8_t(4), msg_data_1); // cmpctblock short id
    1444         [ +  - ]:          10 :         tester.SendMessage(0, {}); // Invalidly encoded message
    1445   [ -  +  +  -  :          10 :         tester.SendMessage("tx", msg_data_2); // 12-character encoded message type
                   +  - ]
    1446                 :             :         // Message type containing 0x7F (DEL): outside the printable-ASCII range
    1447                 :             :         // accepted by the v1 transport, so must be rejected here as well.
    1448   [ -  +  +  -  :          10 :         tester.SendMessage(std::string{"t\x7f"}, msg_data_2);
                   +  - ]
    1449                 :             :         // Message type containing 0x7E ('~'): at the boundary, still valid.
    1450   [ -  +  +  -  :          10 :         tester.SendMessage("t~", msg_data_2);
                   +  - ]
    1451         [ +  - ]:          20 :         ret = tester.Interact();
    1452   [ +  -  +  -  :          20 :         BOOST_REQUIRE(ret);
                   +  - ]
    1453   [ +  -  -  +  :          20 :         BOOST_REQUIRE(ret->size() == 5);
             +  -  +  - ]
    1454   [ +  -  +  -  :          20 :         BOOST_REQUIRE((*ret)[0]);
                   +  - ]
    1455   [ +  -  +  -  :          20 :         BOOST_CHECK((*ret)[0]->m_type == "cmpctblock");
                   +  - ]
    1456   [ +  -  +  -  :          20 :         BOOST_CHECK(std::ranges::equal((*ret)[0]->m_recv, MakeByteSpan(msg_data_1)));
                   +  - ]
    1457   [ +  -  +  -  :          20 :         BOOST_CHECK(!(*ret)[1]);
                   +  - ]
    1458   [ +  -  +  -  :          20 :         BOOST_REQUIRE((*ret)[2]);
                   +  - ]
    1459   [ +  -  +  -  :          20 :         BOOST_CHECK((*ret)[2]->m_type == "tx");
                   +  - ]
    1460   [ +  -  +  -  :          20 :         BOOST_CHECK(std::ranges::equal((*ret)[2]->m_recv, MakeByteSpan(msg_data_2)));
                   +  - ]
    1461   [ +  -  +  -  :          20 :         BOOST_CHECK(!(*ret)[3]);
                   +  - ]
    1462   [ +  -  +  -  :          20 :         BOOST_REQUIRE((*ret)[4]);
                   +  - ]
    1463   [ +  -  +  -  :          20 :         BOOST_CHECK((*ret)[4]->m_type == "t~");
                   +  - ]
    1464   [ +  -  +  -  :          20 :         BOOST_CHECK(std::ranges::equal((*ret)[4]->m_recv, MakeByteSpan(msg_data_2)));
                   -  + ]
    1465                 :             : 
    1466                 :             :         // Then send a message with a bit error, expecting failure. It's possible this failure does
    1467                 :             :         // not occur immediately (when the length descriptor was modified), but it should come
    1468                 :             :         // eventually, and no messages can be delivered anymore.
    1469   [ -  +  +  -  :          10 :         tester.SendMessage("bad", msg_data_1);
                   +  - ]
    1470                 :          10 :         tester.Damage();
    1471                 :          10 :         while (true) {
    1472         [ +  - ]:          20 :             ret = tester.Interact();
    1473         [ -  + ]:          10 :             if (!ret) break; // failure
    1474   [ #  #  #  #  :           0 :             BOOST_CHECK(ret->size() == 0); // no message can be delivered
                   #  # ]
    1475                 :             :             // Send another message.
    1476                 :           0 :             auto msg_data_3 = m_rng.randbytes<uint8_t>(m_rng.randrange(10000));
    1477   [ #  #  #  # ]:           0 :             tester.SendMessage(uint8_t(12), msg_data_3); // getheaders short id
    1478                 :           0 :         }
    1479                 :          10 :     }
    1480                 :             : 
    1481                 :             :     // Normal scenario, with a transport in responder node.
    1482         [ +  + ]:          11 :     for (int i = 0; i < 10; ++i) {
    1483                 :          10 :         V2TransportTester tester(m_rng, false);
    1484         [ +  - ]:          10 :         tester.SendKey();
    1485         [ +  - ]:          10 :         tester.SendGarbage();
    1486         [ +  - ]:          10 :         auto ret = tester.Interact();
    1487   [ +  -  +  -  :          20 :         BOOST_REQUIRE(ret);
                   +  - ]
    1488   [ +  -  +  -  :          20 :         BOOST_REQUIRE(ret->empty());
                   +  - ]
    1489         [ +  - ]:          10 :         tester.ReceiveKey();
    1490         [ +  - ]:          10 :         tester.SendGarbageTerm();
    1491         [ +  - ]:          10 :         tester.SendVersion();
    1492         [ +  - ]:          20 :         ret = tester.Interact();
    1493   [ +  -  +  -  :          20 :         BOOST_REQUIRE(ret);
                   +  - ]
    1494   [ +  -  +  -  :          20 :         BOOST_REQUIRE(ret->empty());
                   +  - ]
    1495         [ +  - ]:          10 :         tester.ReceiveGarbage();
    1496         [ +  - ]:          10 :         tester.ReceiveVersion();
    1497         [ +  - ]:          10 :         tester.CompareSessionIDs();
    1498                 :          10 :         auto msg_data_1 = m_rng.randbytes<uint8_t>(m_rng.randrange(100000));
    1499                 :          10 :         auto msg_data_2 = m_rng.randbytes<uint8_t>(m_rng.randrange(1000));
    1500   [ -  +  +  - ]:          10 :         tester.SendMessage(uint8_t(14), msg_data_1); // inv short id
    1501   [ -  +  +  - ]:          10 :         tester.SendMessage(uint8_t(19), msg_data_2); // pong short id
    1502         [ +  - ]:          20 :         ret = tester.Interact();
    1503   [ +  -  +  -  :          20 :         BOOST_REQUIRE(ret);
                   +  - ]
    1504   [ +  -  -  +  :          20 :         BOOST_REQUIRE(ret->size() == 2);
             +  -  +  - ]
    1505   [ +  -  +  -  :          20 :         BOOST_REQUIRE((*ret)[0]);
                   +  - ]
    1506   [ +  -  +  -  :          20 :         BOOST_CHECK((*ret)[0]->m_type == "inv");
                   +  - ]
    1507   [ +  -  +  -  :          20 :         BOOST_CHECK(std::ranges::equal((*ret)[0]->m_recv, MakeByteSpan(msg_data_1)));
                   +  - ]
    1508   [ +  -  +  -  :          20 :         BOOST_REQUIRE((*ret)[1]);
                   +  - ]
    1509   [ +  -  +  -  :          20 :         BOOST_CHECK((*ret)[1]->m_type == "pong");
                   +  - ]
    1510   [ +  -  +  - ]:          20 :         BOOST_CHECK(std::ranges::equal((*ret)[1]->m_recv, MakeByteSpan(msg_data_2)));
    1511                 :             : 
    1512                 :             :         // Then send a too-large message.
    1513                 :          10 :         auto msg_data_3 = m_rng.randbytes<uint8_t>(4005000);
    1514   [ -  +  +  - ]:          10 :         tester.SendMessage(uint8_t(11), msg_data_3); // getdata short id
    1515         [ +  - ]:          20 :         ret = tester.Interact();
    1516   [ +  -  +  - ]:          20 :         BOOST_CHECK(!ret);
    1517                 :          10 :     }
    1518                 :             : 
    1519                 :             :     // Various valid but unusual scenarios.
    1520         [ +  + ]:          51 :     for (int i = 0; i < 50; ++i) {
    1521                 :             :         /** Whether an initiator or responder is being tested. */
    1522                 :          50 :         bool initiator = m_rng.randbool();
    1523                 :             :         /** Use either 0 bytes or the maximum possible (4095 bytes) garbage length. */
    1524         [ +  + ]:          50 :         size_t garb_len = m_rng.randbool() ? 0 : V2Transport::MAX_GARBAGE_LEN;
    1525                 :             :         /** How many decoy packets to send before the version packet. */
    1526                 :          50 :         unsigned num_ignore_version = m_rng.randrange(10);
    1527                 :             :         /** What data to send in the version packet (ignored by BIP324 peers, but reserved for future extensions). */
    1528         [ +  + ]:          50 :         auto ver_data = m_rng.randbytes<uint8_t>(m_rng.randbool() ? 0 : m_rng.randrange(1000));
    1529                 :             :         /** Whether to immediately send key and garbage out (required for responders, optional otherwise). */
    1530   [ +  +  +  + ]:          50 :         bool send_immediately = !initiator || m_rng.randbool();
    1531                 :             :         /** How many decoy packets to send before the first and second real message. */
    1532                 :          50 :         unsigned num_decoys_1 = m_rng.randrange(1000), num_decoys_2 = m_rng.randrange(1000);
    1533         [ +  - ]:          50 :         V2TransportTester tester(m_rng, initiator);
    1534         [ +  + ]:          50 :         if (send_immediately) {
    1535         [ +  - ]:          40 :             tester.SendKey();
    1536         [ +  - ]:          40 :             tester.SendGarbage(garb_len);
    1537                 :             :         }
    1538         [ +  - ]:          50 :         auto ret = tester.Interact();
    1539   [ +  -  +  -  :         100 :         BOOST_REQUIRE(ret);
                   +  - ]
    1540   [ +  -  +  -  :         100 :         BOOST_REQUIRE(ret->empty());
                   +  + ]
    1541         [ +  + ]:          50 :         if (!send_immediately) {
    1542         [ +  - ]:          10 :             tester.SendKey();
    1543         [ +  - ]:          10 :             tester.SendGarbage(garb_len);
    1544                 :             :         }
    1545         [ +  - ]:          50 :         tester.ReceiveKey();
    1546         [ +  - ]:          50 :         tester.SendGarbageTerm();
    1547         [ +  + ]:         262 :         for (unsigned v = 0; v < num_ignore_version; ++v) {
    1548         [ +  + ]:         212 :             size_t ver_ign_data_len = m_rng.randbool() ? 0 : m_rng.randrange(1000);
    1549                 :         212 :             auto ver_ign_data = m_rng.randbytes<uint8_t>(ver_ign_data_len);
    1550   [ -  +  +  - ]:         212 :             tester.SendVersion(ver_ign_data, true);
    1551                 :         212 :         }
    1552   [ -  +  +  - ]:          50 :         tester.SendVersion(ver_data, false);
    1553         [ +  - ]:         100 :         ret = tester.Interact();
    1554   [ +  -  +  -  :         100 :         BOOST_REQUIRE(ret);
                   +  - ]
    1555   [ +  -  +  -  :         100 :         BOOST_REQUIRE(ret->empty());
                   +  - ]
    1556         [ +  - ]:          50 :         tester.ReceiveGarbage();
    1557         [ +  - ]:          50 :         tester.ReceiveVersion();
    1558         [ +  - ]:          50 :         tester.CompareSessionIDs();
    1559         [ +  + ]:       23767 :         for (unsigned d = 0; d < num_decoys_1; ++d) {
    1560                 :       23717 :             auto decoy_data = m_rng.randbytes<uint8_t>(m_rng.randrange(1000));
    1561   [ -  +  +  - ]:       23717 :             tester.SendPacket(/*content=*/decoy_data, /*aad=*/{}, /*ignore=*/true);
    1562                 :       23717 :         }
    1563                 :          50 :         auto msg_data_1 = m_rng.randbytes<uint8_t>(m_rng.randrange(4000000));
    1564   [ -  +  +  - ]:          50 :         tester.SendMessage(uint8_t(28), msg_data_1);
    1565         [ +  + ]:       23111 :         for (unsigned d = 0; d < num_decoys_2; ++d) {
    1566                 :       23061 :             auto decoy_data = m_rng.randbytes<uint8_t>(m_rng.randrange(1000));
    1567   [ -  +  +  - ]:       23061 :             tester.SendPacket(/*content=*/decoy_data, /*aad=*/{}, /*ignore=*/true);
    1568                 :       23061 :         }
    1569                 :          50 :         auto msg_data_2 = m_rng.randbytes<uint8_t>(m_rng.randrange(1000));
    1570   [ -  +  +  - ]:          50 :         tester.SendMessage(uint8_t(13), msg_data_2); // headers short id
    1571                 :             :         // Send invalidly-encoded message
    1572   [ +  -  +  - ]:          50 :         tester.SendMessage(std::string("blocktxn\x00\x00\x00a", CMessageHeader::MESSAGE_TYPE_SIZE), {});
    1573   [ +  -  +  - ]:          50 :         tester.SendMessage("foobar", {}); // test receiving unknown message type
    1574   [ +  -  +  - ]:         100 :         tester.AddMessage("barfoo", {}); // test sending unknown message type
    1575         [ +  - ]:         100 :         ret = tester.Interact();
    1576   [ +  -  +  -  :         100 :         BOOST_REQUIRE(ret);
                   +  - ]
    1577   [ +  -  -  +  :         100 :         BOOST_REQUIRE(ret->size() == 4);
             +  -  +  - ]
    1578   [ +  -  +  -  :         100 :         BOOST_REQUIRE((*ret)[0]);
                   +  - ]
    1579   [ +  -  +  -  :         100 :         BOOST_CHECK((*ret)[0]->m_type == "addrv2");
                   +  - ]
    1580   [ +  -  +  -  :         100 :         BOOST_CHECK(std::ranges::equal((*ret)[0]->m_recv, MakeByteSpan(msg_data_1)));
                   +  - ]
    1581   [ +  -  +  -  :         100 :         BOOST_REQUIRE((*ret)[1]);
                   +  - ]
    1582   [ +  -  +  -  :         100 :         BOOST_CHECK((*ret)[1]->m_type == "headers");
                   +  - ]
    1583   [ +  -  +  -  :         100 :         BOOST_CHECK(std::ranges::equal((*ret)[1]->m_recv, MakeByteSpan(msg_data_2)));
                   +  - ]
    1584   [ +  -  +  -  :         100 :         BOOST_CHECK(!(*ret)[2]);
                   +  - ]
    1585   [ +  -  +  -  :         100 :         BOOST_REQUIRE((*ret)[3]);
                   +  - ]
    1586   [ +  -  +  -  :         100 :         BOOST_CHECK((*ret)[3]->m_type == "foobar");
                   +  - ]
    1587   [ +  -  -  +  :         100 :         BOOST_CHECK((*ret)[3]->m_recv.empty());
             +  -  +  - ]
    1588   [ +  -  +  - ]:         100 :         tester.ReceiveMessage("barfoo", {});
    1589                 :          50 :     }
    1590                 :             : 
    1591                 :             :     // Too long garbage (initiator).
    1592                 :           1 :     {
    1593                 :           1 :         V2TransportTester tester(m_rng, true);
    1594         [ +  - ]:           1 :         auto ret = tester.Interact();
    1595   [ +  -  +  -  :           2 :         BOOST_REQUIRE(ret);
                   +  - ]
    1596   [ +  -  +  -  :           2 :         BOOST_REQUIRE(ret->empty());
                   +  - ]
    1597         [ +  - ]:           1 :         tester.SendKey();
    1598         [ +  - ]:           1 :         tester.SendGarbage(V2Transport::MAX_GARBAGE_LEN + 1);
    1599         [ +  - ]:           1 :         tester.ReceiveKey();
    1600         [ +  - ]:           1 :         tester.SendGarbageTerm();
    1601         [ +  - ]:           2 :         ret = tester.Interact();
    1602   [ +  -  +  - ]:           2 :         BOOST_CHECK(!ret);
    1603                 :           1 :     }
    1604                 :             : 
    1605                 :             :     // Too long garbage (responder).
    1606                 :           1 :     {
    1607                 :           1 :         V2TransportTester tester(m_rng, false);
    1608         [ +  - ]:           1 :         tester.SendKey();
    1609         [ +  - ]:           1 :         tester.SendGarbage(V2Transport::MAX_GARBAGE_LEN + 1);
    1610         [ +  - ]:           1 :         auto ret = tester.Interact();
    1611   [ +  -  +  -  :           2 :         BOOST_REQUIRE(ret);
                   +  - ]
    1612   [ +  -  +  -  :           2 :         BOOST_REQUIRE(ret->empty());
                   +  - ]
    1613         [ +  - ]:           1 :         tester.ReceiveKey();
    1614         [ +  - ]:           1 :         tester.SendGarbageTerm();
    1615         [ +  - ]:           2 :         ret = tester.Interact();
    1616   [ +  -  +  - ]:           2 :         BOOST_CHECK(!ret);
    1617                 :           1 :     }
    1618                 :             : 
    1619                 :             :     // Send garbage that includes the first 15 garbage terminator bytes somewhere.
    1620                 :           1 :     {
    1621                 :           1 :         V2TransportTester tester(m_rng, true);
    1622         [ +  - ]:           1 :         auto ret = tester.Interact();
    1623   [ +  -  +  -  :           2 :         BOOST_REQUIRE(ret);
                   +  - ]
    1624   [ +  -  +  -  :           2 :         BOOST_REQUIRE(ret->empty());
                   +  - ]
    1625         [ +  - ]:           1 :         tester.SendKey();
    1626         [ +  - ]:           1 :         tester.ReceiveKey();
    1627                 :             :         /** The number of random garbage bytes before the included first 15 bytes of terminator. */
    1628                 :           1 :         size_t len_before = m_rng.randrange(V2Transport::MAX_GARBAGE_LEN - 16 + 1);
    1629                 :             :         /** The number of random garbage bytes after it. */
    1630                 :           1 :         size_t len_after = m_rng.randrange(V2Transport::MAX_GARBAGE_LEN - 16 - len_before + 1);
    1631                 :             :         // Construct len_before + 16 + len_after random bytes.
    1632                 :           1 :         auto garbage = m_rng.randbytes<uint8_t>(len_before + 16 + len_after);
    1633                 :             :         // Replace the designed 16 bytes in the middle with the to-be-sent garbage terminator.
    1634                 :           1 :         auto garb_term = MakeUCharSpan(tester.GetCipher().GetSendGarbageTerminator());
    1635                 :           1 :         std::copy(garb_term.begin(), garb_term.begin() + 16, garbage.begin() + len_before);
    1636                 :             :         // Introduce a bit error in the last byte of that copied garbage terminator, making only
    1637                 :             :         // the first 15 of them match.
    1638         [ -  + ]:           1 :         garbage[len_before + 15] ^= (uint8_t(1) << m_rng.randrange(8));
    1639   [ -  +  +  - ]:           1 :         tester.SendGarbage(garbage);
    1640         [ +  - ]:           1 :         tester.SendGarbageTerm();
    1641         [ +  - ]:           1 :         tester.SendVersion();
    1642         [ +  - ]:           2 :         ret = tester.Interact();
    1643   [ +  -  +  -  :           2 :         BOOST_REQUIRE(ret);
                   +  - ]
    1644   [ +  -  +  -  :           2 :         BOOST_REQUIRE(ret->empty());
                   +  - ]
    1645         [ +  - ]:           1 :         tester.ReceiveGarbage();
    1646         [ +  - ]:           1 :         tester.ReceiveVersion();
    1647         [ +  - ]:           1 :         tester.CompareSessionIDs();
    1648                 :           1 :         auto msg_data_1 = m_rng.randbytes<uint8_t>(4000000); // test that receiving 4M payload works
    1649                 :           1 :         auto msg_data_2 = m_rng.randbytes<uint8_t>(4000000); // test that sending 4M payload works
    1650         [ +  - ]:           1 :         tester.SendMessage(uint8_t(m_rng.randrange(256 - BIP324_SHORTIDS_IMPLEMENTED) + BIP324_SHORTIDS_IMPLEMENTED), {}); // unknown short id
    1651   [ -  +  +  - ]:           1 :         tester.SendMessage(uint8_t(2), msg_data_1); // "block" short id
    1652   [ +  -  +  -  :           2 :         tester.AddMessage("blocktxn", msg_data_2); // schedule blocktxn to be sent to us
                   +  - ]
    1653         [ +  - ]:           2 :         ret = tester.Interact();
    1654   [ +  -  +  -  :           2 :         BOOST_REQUIRE(ret);
                   +  - ]
    1655   [ +  -  -  +  :           2 :         BOOST_REQUIRE(ret->size() == 2);
             +  -  +  - ]
    1656   [ +  -  +  -  :           2 :         BOOST_CHECK(!(*ret)[0]);
                   +  - ]
    1657   [ +  -  +  -  :           2 :         BOOST_REQUIRE((*ret)[1]);
                   +  - ]
    1658   [ +  -  +  -  :           2 :         BOOST_CHECK((*ret)[1]->m_type == "block");
                   +  - ]
    1659   [ +  -  +  -  :           2 :         BOOST_CHECK(std::ranges::equal((*ret)[1]->m_recv, MakeByteSpan(msg_data_1)));
                   -  + ]
    1660   [ -  +  +  - ]:           1 :         tester.ReceiveMessage(uint8_t(3), msg_data_2); // "blocktxn" short id
    1661                 :           1 :     }
    1662                 :             : 
    1663                 :             :     // Send correct network's V1 header
    1664                 :           1 :     {
    1665                 :           1 :         V2TransportTester tester(m_rng, false);
    1666   [ +  -  +  - ]:           1 :         tester.SendV1Version(Params().MessageStart());
    1667         [ +  - ]:           1 :         auto ret = tester.Interact();
    1668   [ +  -  +  - ]:           2 :         BOOST_CHECK(ret);
    1669                 :           1 :     }
    1670                 :             : 
    1671                 :             :     // Send wrong network's V1 header
    1672                 :           1 :     {
    1673                 :           1 :         V2TransportTester tester(m_rng, false);
    1674   [ +  -  +  - ]:           1 :         tester.SendV1Version(CChainParams::Main()->MessageStart());
    1675         [ +  - ]:           1 :         auto ret = tester.Interact();
    1676   [ +  -  +  - ]:           2 :         BOOST_CHECK(!ret);
    1677                 :           1 :     }
    1678                 :           1 : }
    1679                 :             : 
    1680   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(version_message_min_peer_proto_version)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
    1681                 :             : {
    1682                 :           1 :     LOCK(NetEventsInterface::g_msgproc_mutex);
    1683                 :           1 :     auto& connman{static_cast<ConnmanTestMsg&>(*m_node.connman)};
    1684                 :             : 
    1685                 :             :     // Peers below MIN_PEER_PROTO_VERSION are disconnected, peers at it are not.
    1686         [ +  + ]:           3 :     for (const bool too_old : {true, false}) {
    1687                 :           2 :         CNode node{/*id=*/0,
    1688                 :             :                    /*sock=*/nullptr,
    1689         [ -  + ]:           2 :                    /*addrIn=*/CAddress{},
    1690                 :             :                    /*nKeyedNetGroupIn=*/0,
    1691                 :             :                    /*nLocalHostNonceIn=*/0,
    1692         [ +  - ]:           4 :                    /*addrBindIn=*/CService{},
    1693         [ +  - ]:           4 :                    /*addrNameIn=*/"",
    1694                 :             :                    /*conn_type_in=*/ConnectionType::INBOUND,
    1695                 :             :                    /*inbound_onion=*/false,
    1696   [ +  -  +  - ]:           4 :                    /*network_key=*/0};
    1697         [ +  - ]:           2 :         connman.Handshake(node,
    1698                 :             :                           /*successfully_connected=*/false,
    1699                 :             :                           /*remote_services=*/NODE_NETWORK,
    1700                 :             :                           /*local_services=*/NODE_NONE,
    1701         [ +  + ]:           2 :                           /*version=*/MIN_PEER_PROTO_VERSION - (too_old ? 1 : 0),
    1702                 :             :                           /*relay_txs=*/true);
    1703   [ +  -  +  - ]:           2 :         BOOST_CHECK_EQUAL(node.fDisconnect.load(), too_old);
    1704         [ +  - ]:           2 :         m_node.peerman->FinalizeNode(node);
    1705                 :           2 :     }
    1706                 :           1 : }
    1707                 :             : 
    1708   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(private_broadcast_version_does_not_update_addrman_services)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
    1709                 :             : {
    1710                 :           1 :     LOCK(NetEventsInterface::g_msgproc_mutex);
    1711                 :             : 
    1712   [ +  -  +  -  :           2 :     const CNetAddr source{LookupHost("2.3.4.5", /*fAllowLookup=*/false).value()};
                   +  - ]
    1713   [ +  -  +  -  :           2 :     const CAddress addr{Lookup("1.2.3.4", 8333, /*fAllowLookup=*/false).value(), NODE_NONE};
                   +  - ]
    1714   [ +  -  +  -  :           3 :     BOOST_REQUIRE(m_node.addrman->Add({addr}, source));
          +  -  +  -  +  
                +  -  - ]
    1715                 :           1 :     CNode node{/*id=*/0,
    1716                 :             :                /*sock=*/nullptr,
    1717                 :             :                /*addrIn=*/addr,
    1718                 :             :                /*nKeyedNetGroupIn=*/0,
    1719                 :             :                /*nLocalHostNonceIn=*/0,
    1720         [ -  + ]:           1 :                /*addrBindIn=*/CService{},
    1721         [ +  - ]:           2 :                /*addrNameIn=*/"",
    1722                 :             :                /*conn_type_in=*/ConnectionType::PRIVATE_BROADCAST,
    1723                 :             :                /*inbound_onion=*/false,
    1724   [ +  -  +  - ]:           2 :                /*network_key=*/0};
    1725                 :             : 
    1726         [ +  - ]:           1 :     auto& connman = static_cast<ConnmanTestMsg&>(*m_node.connman);
    1727         [ +  - ]:           1 :     connman.Handshake(node,
    1728                 :             :                       /*successfully_connected=*/false,
    1729                 :             :                       /*remote_services=*/NODE_NETWORK,
    1730                 :             :                       /*local_services=*/NODE_NONE,
    1731                 :             :                       /*version=*/PROTOCOL_VERSION,
    1732                 :             :                       /*relay_txs=*/true);
    1733                 :             : 
    1734   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(m_node.addrman->Select().first.nServices, NODE_NONE);
                   +  - ]
    1735         [ +  - ]:           1 :     m_node.peerman->FinalizeNode(node);
    1736         [ +  - ]:           3 : }
    1737                 :             : 
    1738   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(only_non_inbound_version_message_promotes_addr_to_tried)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
    1739                 :             : {
    1740                 :           1 :     LOCK(NetEventsInterface::g_msgproc_mutex);
    1741                 :             : 
    1742   [ +  -  +  -  :           2 :     const CNetAddr source{LookupHost("2.3.4.5", /*fAllowLookup=*/false).value()};
                   +  - ]
    1743   [ +  -  +  -  :           2 :     const CAddress addr_outbound{Lookup("5.6.7.8", 8333, /*fAllowLookup=*/false).value(), NODE_NONE};
                   +  - ]
    1744   [ +  -  +  -  :           2 :     const CAddress addr_inbound{Lookup("6.7.8.9", 8333, /*fAllowLookup=*/false).value(), NODE_NONE};
                   +  - ]
    1745                 :             :     // Good() returns false for an address it does not already know, so each entry must
    1746                 :             :     // first exist before its handshake for the promotion to happen. Adding addr_inbound
    1747                 :             :     // only after addr_outbound has moved to tried avoids a possible collision in the new
    1748                 :             :     // table.
    1749   [ +  -  +  -  :           3 :     BOOST_REQUIRE(m_node.addrman->Add({addr_outbound}, source));
          +  -  +  -  +  
                +  -  - ]
    1750   [ +  -  +  -  :           1 :     BOOST_REQUIRE_EQUAL(m_node.addrman->Size(/*net=*/std::nullopt, /*in_new=*/true), 1U);
                   +  - ]
    1751   [ +  -  +  -  :           1 :     BOOST_REQUIRE_EQUAL(m_node.addrman->Size(/*net=*/std::nullopt, /*in_new=*/false), 0U);
                   +  - ]
    1752                 :             : 
    1753         [ +  - ]:           1 :     auto& connman = static_cast<ConnmanTestMsg&>(*m_node.connman);
    1754                 :             :     // An outbound peer's address is moved from the new to the tried table.
    1755                 :             :     // Good() is called when the VERSION message is processed, so the handshake
    1756                 :             :     // does not need to be completed with a VERACK.
    1757                 :           1 :     CNode node_outbound{/*id=*/0,
    1758                 :             :                         /*sock=*/nullptr,
    1759                 :             :                         /*addrIn=*/addr_outbound,
    1760                 :             :                         /*nKeyedNetGroupIn=*/0,
    1761                 :             :                         /*nLocalHostNonceIn=*/0,
    1762         [ -  + ]:           1 :                         /*addrBindIn=*/CService{},
    1763         [ +  - ]:           2 :                         /*addrNameIn=*/"",
    1764                 :             :                         /*conn_type_in=*/ConnectionType::OUTBOUND_FULL_RELAY,
    1765                 :             :                         /*inbound_onion=*/false,
    1766   [ +  -  +  - ]:           2 :                         /*network_key=*/0};
    1767         [ +  - ]:           1 :     connman.Handshake(node_outbound,
    1768                 :             :                       /*successfully_connected=*/false,
    1769                 :             :                       /*remote_services=*/ServiceFlags(NODE_NETWORK | NODE_WITNESS),
    1770                 :             :                       /*local_services=*/NODE_NONE,
    1771                 :             :                       /*version=*/PROTOCOL_VERSION,
    1772                 :             :                       /*relay_txs=*/true);
    1773                 :             : 
    1774   [ +  -  +  -  :           2 :     BOOST_REQUIRE(!node_outbound.fDisconnect);
                   +  - ]
    1775   [ +  -  +  -  :           3 :     BOOST_CHECK(m_node.addrman->FindAddressEntry(addr_outbound).value().tried);
             +  -  +  - ]
    1776         [ +  - ]:           1 :     m_node.peerman->FinalizeNode(node_outbound);
    1777                 :             : 
    1778                 :             :     // An inbound peer's address stays in the new table.
    1779   [ +  -  +  -  :           3 :     BOOST_REQUIRE(m_node.addrman->Add({addr_inbound}, source));
          +  -  +  -  +  
                +  -  - ]
    1780                 :           1 :     CNode node_inbound{/*id=*/1,
    1781                 :             :                        /*sock=*/nullptr,
    1782                 :             :                        /*addrIn=*/addr_inbound,
    1783                 :             :                        /*nKeyedNetGroupIn=*/0,
    1784                 :             :                        /*nLocalHostNonceIn=*/0,
    1785         [ -  + ]:           1 :                        /*addrBindIn=*/CService{},
    1786         [ +  - ]:           2 :                        /*addrNameIn=*/"",
    1787                 :             :                        /*conn_type_in=*/ConnectionType::INBOUND,
    1788                 :             :                        /*inbound_onion=*/false,
    1789   [ +  -  +  - ]:           2 :                        /*network_key=*/0};
    1790         [ +  - ]:           1 :     connman.Handshake(node_inbound,
    1791                 :             :                       /*successfully_connected=*/false,
    1792                 :             :                       /*remote_services=*/ServiceFlags(NODE_NETWORK | NODE_WITNESS),
    1793                 :             :                       /*local_services=*/NODE_NONE,
    1794                 :             :                       /*version=*/PROTOCOL_VERSION,
    1795                 :             :                       /*relay_txs=*/true);
    1796                 :             : 
    1797   [ +  -  +  -  :           2 :     BOOST_REQUIRE(!node_inbound.fDisconnect);
                   +  - ]
    1798   [ +  -  +  -  :           3 :     BOOST_CHECK(!m_node.addrman->FindAddressEntry(addr_inbound).value().tried);
             +  -  +  - ]
    1799         [ +  - ]:           1 :     m_node.peerman->FinalizeNode(node_inbound);
    1800         [ +  - ]:           4 : }
    1801                 :             : 
    1802   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(addlocal_onlynet_externalip)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
    1803                 :             : {
    1804                 :             :     // Test that `-externalip` addresses bypass `-onlynet`, but score alone does
    1805                 :             :     // not.
    1806                 :             : 
    1807                 :           1 :     CAddress addr_onion;
    1808   [ +  -  +  -  :           2 :     BOOST_REQUIRE(addr_onion.SetSpecial("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion"));
             +  -  +  - ]
    1809   [ +  -  +  -  :           2 :     BOOST_REQUIRE(addr_onion.IsValid());
             +  -  +  - ]
    1810   [ +  -  +  -  :           2 :     BOOST_REQUIRE(addr_onion.IsTor());
                   +  - ]
    1811                 :             : 
    1812         [ +  - ]:           1 :     const auto reachable_nets_at_start{g_reachable_nets.All()};
    1813                 :           1 :     const bool discover_orig{fDiscover};
    1814                 :             : 
    1815                 :             :     // Simulate using -onlynet=ipv4 -externalip=<onion>
    1816         [ +  - ]:           1 :     g_reachable_nets.RemoveAll();
    1817         [ +  - ]:           1 :     g_reachable_nets.Add(NET_IPV4);
    1818                 :           1 :     fDiscover = false;
    1819                 :             : 
    1820                 :             :     // Now AddLocal with a non-manual score should fail for an unreachable network.
    1821   [ +  -  +  -  :           2 :     BOOST_CHECK(!AddLocal(addr_onion, LOCAL_BIND));
             +  -  +  - ]
    1822   [ +  -  +  -  :           2 :     BOOST_CHECK(!IsLocal(addr_onion));
             +  -  +  - ]
    1823                 :             : 
    1824   [ +  -  +  -  :           2 :     BOOST_CHECK(!AddLocal(addr_onion, LOCAL_MANUAL));
             +  -  +  - ]
    1825   [ +  -  +  -  :           2 :     BOOST_CHECK(!IsLocal(addr_onion));
             +  -  +  - ]
    1826                 :             : 
    1827                 :             :     // Whereas AddLocal for -externalip should succeed.
    1828   [ +  -  +  -  :           2 :     BOOST_CHECK(AddLocal(addr_onion, LOCAL_MANUAL, /*add_even_if_unreachable=*/true));
             +  -  +  - ]
    1829   [ +  -  +  -  :           2 :     BOOST_CHECK(IsLocal(addr_onion));
             +  -  +  - ]
    1830                 :             : 
    1831                 :             :     // Normal AddLocal on a reachable network still works.
    1832   [ +  -  +  -  :           2 :     const CNetAddr addr_ipv4{LookupHost("1.2.3.4", false).value()};
                   +  - ]
    1833   [ +  -  +  -  :           2 :     BOOST_CHECK(AddLocal(addr_ipv4, LOCAL_MANUAL));
             +  -  +  - ]
    1834   [ +  -  +  -  :           2 :     BOOST_CHECK(IsLocal(CService{addr_ipv4, GetListenPort()}));
          +  -  +  -  +  
                -  +  - ]
    1835                 :             : 
    1836   [ +  -  +  -  :           1 :     RemoveLocal(CService{addr_ipv4, GetListenPort()});
                   +  - ]
    1837         [ +  - ]:           1 :     RemoveLocal(addr_onion);
    1838         [ +  - ]:           1 :     g_reachable_nets.RemoveAll();
    1839   [ +  +  +  - ]:           8 :     for (const auto& net : reachable_nets_at_start) {
    1840         [ +  - ]:           7 :         g_reachable_nets.Add(net);
    1841                 :             :     }
    1842                 :           1 :     fDiscover = discover_orig;
    1843                 :           1 : }
    1844                 :             : 
    1845                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1