LCOV - code coverage report
Current view: top level - src/test - net_tests.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 98.9 % 939 929
Test Date: 2024-11-04 04:45:35 Functions: 100.0 % 58 58
Branches: 50.4 % 3980 2004

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

Generated by: LCOV version 2.0-1