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 % 946 936
Test Date: 2026-02-25 05:21:33 Functions: 100.0 % 58 58
Branches: 50.3 % 4218 2122

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

Generated by: LCOV version 2.0-1