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 % 938 928
Test Date: 2024-08-28 04:44:32 Functions: 100.0 % 58 58
Branches: 50.4 % 4038 2035

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

Generated by: LCOV version 2.0-1