LCOV - code coverage report
Current view: top level - src/test - netbase_tests.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 0 426 0.0 %
Date: 2023-10-05 12:38:51 Functions: 0 85 0.0 %
Branches: 0 0 -

           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 <net_permissions.h>
       6                 :            : #include <netaddress.h>
       7                 :            : #include <netbase.h>
       8                 :            : #include <netgroup.h>
       9                 :            : #include <protocol.h>
      10                 :            : #include <serialize.h>
      11                 :            : #include <streams.h>
      12                 :            : #include <test/util/setup_common.h>
      13                 :            : #include <util/strencodings.h>
      14                 :            : #include <util/translation.h>
      15                 :            : #include <version.h>
      16                 :            : 
      17                 :            : #include <string>
      18                 :            : 
      19                 :            : #include <boost/test/unit_test.hpp>
      20                 :            : 
      21                 :            : using namespace std::literals;
      22                 :            : 
      23                 :          0 : BOOST_FIXTURE_TEST_SUITE(netbase_tests, BasicTestingSetup)
      24                 :            : 
      25                 :          0 : static CNetAddr ResolveIP(const std::string& ip)
      26                 :            : {
      27                 :          0 :     return LookupHost(ip, false).value_or(CNetAddr{});
      28                 :          0 : }
      29                 :            : 
      30                 :          0 : static CSubNet ResolveSubNet(const std::string& subnet)
      31                 :            : {
      32                 :          0 :     CSubNet ret;
      33                 :          0 :     LookupSubNet(subnet, ret);
      34                 :          0 :     return ret;
      35                 :          0 : }
      36                 :            : 
      37                 :          0 : static CNetAddr CreateInternal(const std::string& host)
      38                 :            : {
      39                 :          0 :     CNetAddr addr;
      40                 :          0 :     addr.SetInternal(host);
      41                 :          0 :     return addr;
      42                 :          0 : }
      43                 :            : 
      44                 :          0 : BOOST_AUTO_TEST_CASE(netbase_networks)
      45                 :            : {
      46                 :          0 :     BOOST_CHECK(ResolveIP("127.0.0.1").GetNetwork() == NET_UNROUTABLE);
      47                 :          0 :     BOOST_CHECK(ResolveIP("::1").GetNetwork() == NET_UNROUTABLE);
      48                 :          0 :     BOOST_CHECK(ResolveIP("8.8.8.8").GetNetwork() == NET_IPV4);
      49                 :          0 :     BOOST_CHECK(ResolveIP("2001::8888").GetNetwork() == NET_IPV6);
      50                 :          0 :     BOOST_CHECK(ResolveIP("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion").GetNetwork() == NET_ONION);
      51                 :          0 :     BOOST_CHECK(CreateInternal("foo.com").GetNetwork() == NET_INTERNAL);
      52                 :          0 : }
      53                 :            : 
      54                 :          0 : BOOST_AUTO_TEST_CASE(netbase_properties)
      55                 :            : {
      56                 :            : 
      57                 :          0 :     BOOST_CHECK(ResolveIP("127.0.0.1").IsIPv4());
      58                 :          0 :     BOOST_CHECK(ResolveIP("::FFFF:192.168.1.1").IsIPv4());
      59                 :          0 :     BOOST_CHECK(ResolveIP("::1").IsIPv6());
      60                 :          0 :     BOOST_CHECK(ResolveIP("10.0.0.1").IsRFC1918());
      61                 :          0 :     BOOST_CHECK(ResolveIP("192.168.1.1").IsRFC1918());
      62                 :          0 :     BOOST_CHECK(ResolveIP("172.31.255.255").IsRFC1918());
      63                 :          0 :     BOOST_CHECK(ResolveIP("198.18.0.0").IsRFC2544());
      64                 :          0 :     BOOST_CHECK(ResolveIP("198.19.255.255").IsRFC2544());
      65                 :          0 :     BOOST_CHECK(ResolveIP("2001:0DB8::").IsRFC3849());
      66                 :          0 :     BOOST_CHECK(ResolveIP("169.254.1.1").IsRFC3927());
      67                 :          0 :     BOOST_CHECK(ResolveIP("2002::1").IsRFC3964());
      68                 :          0 :     BOOST_CHECK(ResolveIP("FC00::").IsRFC4193());
      69                 :          0 :     BOOST_CHECK(ResolveIP("2001::2").IsRFC4380());
      70                 :          0 :     BOOST_CHECK(ResolveIP("2001:10::").IsRFC4843());
      71                 :          0 :     BOOST_CHECK(ResolveIP("2001:20::").IsRFC7343());
      72                 :          0 :     BOOST_CHECK(ResolveIP("FE80::").IsRFC4862());
      73                 :          0 :     BOOST_CHECK(ResolveIP("64:FF9B::").IsRFC6052());
      74                 :          0 :     BOOST_CHECK(ResolveIP("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion").IsTor());
      75                 :          0 :     BOOST_CHECK(ResolveIP("127.0.0.1").IsLocal());
      76                 :          0 :     BOOST_CHECK(ResolveIP("::1").IsLocal());
      77                 :          0 :     BOOST_CHECK(ResolveIP("8.8.8.8").IsRoutable());
      78                 :          0 :     BOOST_CHECK(ResolveIP("2001::1").IsRoutable());
      79                 :          0 :     BOOST_CHECK(ResolveIP("127.0.0.1").IsValid());
      80                 :          0 :     BOOST_CHECK(CreateInternal("FD6B:88C0:8724:edb1:8e4:3588:e546:35ca").IsInternal());
      81                 :          0 :     BOOST_CHECK(CreateInternal("bar.com").IsInternal());
      82                 :            : 
      83                 :          0 : }
      84                 :            : 
      85                 :          0 : bool static TestSplitHost(const std::string& test, const std::string& host, uint16_t port, bool validPort=true)
      86                 :            : {
      87                 :          0 :     std::string hostOut;
      88                 :          0 :     uint16_t portOut{0};
      89                 :          0 :     bool validPortOut = SplitHostPort(test, portOut, hostOut);
      90                 :          0 :     return hostOut == host && portOut == port && validPortOut == validPort;
      91                 :          0 : }
      92                 :            : 
      93                 :          0 : BOOST_AUTO_TEST_CASE(netbase_splithost)
      94                 :            : {
      95                 :          0 :     BOOST_CHECK(TestSplitHost("www.bitcoincore.org", "www.bitcoincore.org", 0));
      96                 :          0 :     BOOST_CHECK(TestSplitHost("[www.bitcoincore.org]", "www.bitcoincore.org", 0));
      97                 :          0 :     BOOST_CHECK(TestSplitHost("www.bitcoincore.org:80", "www.bitcoincore.org", 80));
      98                 :          0 :     BOOST_CHECK(TestSplitHost("[www.bitcoincore.org]:80", "www.bitcoincore.org", 80));
      99                 :          0 :     BOOST_CHECK(TestSplitHost("127.0.0.1", "127.0.0.1", 0));
     100                 :          0 :     BOOST_CHECK(TestSplitHost("127.0.0.1:8333", "127.0.0.1", 8333));
     101                 :          0 :     BOOST_CHECK(TestSplitHost("[127.0.0.1]", "127.0.0.1", 0));
     102                 :          0 :     BOOST_CHECK(TestSplitHost("[127.0.0.1]:8333", "127.0.0.1", 8333));
     103                 :          0 :     BOOST_CHECK(TestSplitHost("::ffff:127.0.0.1", "::ffff:127.0.0.1", 0));
     104                 :          0 :     BOOST_CHECK(TestSplitHost("[::ffff:127.0.0.1]:8333", "::ffff:127.0.0.1", 8333));
     105                 :          0 :     BOOST_CHECK(TestSplitHost("[::]:8333", "::", 8333));
     106                 :          0 :     BOOST_CHECK(TestSplitHost("::8333", "::8333", 0));
     107                 :          0 :     BOOST_CHECK(TestSplitHost(":8333", "", 8333));
     108                 :          0 :     BOOST_CHECK(TestSplitHost("[]:8333", "", 8333));
     109                 :          0 :     BOOST_CHECK(TestSplitHost("", "", 0));
     110                 :          0 :     BOOST_CHECK(TestSplitHost(":65535", "", 65535));
     111                 :          0 :     BOOST_CHECK(TestSplitHost(":65536", ":65536", 0, false));
     112                 :          0 :     BOOST_CHECK(TestSplitHost(":-1", ":-1", 0, false));
     113                 :          0 :     BOOST_CHECK(TestSplitHost("[]:70001", "[]:70001", 0, false));
     114                 :          0 :     BOOST_CHECK(TestSplitHost("[]:-1", "[]:-1", 0, false));
     115                 :          0 :     BOOST_CHECK(TestSplitHost("[]:-0", "[]:-0", 0, false));
     116                 :          0 :     BOOST_CHECK(TestSplitHost("[]:0", "", 0, false));
     117                 :          0 :     BOOST_CHECK(TestSplitHost("[]:1/2", "[]:1/2", 0, false));
     118                 :          0 :     BOOST_CHECK(TestSplitHost("[]:1E2", "[]:1E2", 0, false));
     119                 :          0 :     BOOST_CHECK(TestSplitHost("127.0.0.1:65536", "127.0.0.1:65536", 0, false));
     120                 :          0 :     BOOST_CHECK(TestSplitHost("127.0.0.1:0", "127.0.0.1", 0, false));
     121                 :          0 :     BOOST_CHECK(TestSplitHost("127.0.0.1:", "127.0.0.1:", 0, false));
     122                 :          0 :     BOOST_CHECK(TestSplitHost("127.0.0.1:1/2", "127.0.0.1:1/2", 0, false));
     123                 :          0 :     BOOST_CHECK(TestSplitHost("127.0.0.1:1E2", "127.0.0.1:1E2", 0, false));
     124                 :          0 :     BOOST_CHECK(TestSplitHost("www.bitcoincore.org:65536", "www.bitcoincore.org:65536", 0, false));
     125                 :          0 :     BOOST_CHECK(TestSplitHost("www.bitcoincore.org:0", "www.bitcoincore.org", 0, false));
     126                 :          0 :     BOOST_CHECK(TestSplitHost("www.bitcoincore.org:", "www.bitcoincore.org:", 0, false));
     127                 :          0 : }
     128                 :            : 
     129                 :          0 : bool static TestParse(std::string src, std::string canon)
     130                 :            : {
     131                 :          0 :     CService addr(LookupNumeric(src, 65535));
     132                 :          0 :     return canon == addr.ToStringAddrPort();
     133                 :          0 : }
     134                 :            : 
     135                 :          0 : BOOST_AUTO_TEST_CASE(netbase_lookupnumeric)
     136                 :            : {
     137                 :          0 :     BOOST_CHECK(TestParse("127.0.0.1", "127.0.0.1:65535"));
     138                 :          0 :     BOOST_CHECK(TestParse("127.0.0.1:8333", "127.0.0.1:8333"));
     139                 :          0 :     BOOST_CHECK(TestParse("::ffff:127.0.0.1", "127.0.0.1:65535"));
     140                 :          0 :     BOOST_CHECK(TestParse("::", "[::]:65535"));
     141                 :          0 :     BOOST_CHECK(TestParse("[::]:8333", "[::]:8333"));
     142                 :          0 :     BOOST_CHECK(TestParse("[127.0.0.1]", "127.0.0.1:65535"));
     143                 :          0 :     BOOST_CHECK(TestParse(":::", "[::]:0"));
     144                 :            : 
     145                 :            :     // verify that an internal address fails to resolve
     146                 :          0 :     BOOST_CHECK(TestParse("[fd6b:88c0:8724:1:2:3:4:5]", "[::]:0"));
     147                 :            :     // and that a one-off resolves correctly
     148                 :          0 :     BOOST_CHECK(TestParse("[fd6c:88c0:8724:1:2:3:4:5]", "[fd6c:88c0:8724:1:2:3:4:5]:65535"));
     149                 :          0 : }
     150                 :            : 
     151                 :          0 : BOOST_AUTO_TEST_CASE(embedded_test)
     152                 :            : {
     153                 :          0 :     CNetAddr addr1(ResolveIP("1.2.3.4"));
     154                 :          0 :     CNetAddr addr2(ResolveIP("::FFFF:0102:0304"));
     155                 :          0 :     BOOST_CHECK(addr2.IsIPv4());
     156                 :          0 :     BOOST_CHECK_EQUAL(addr1.ToStringAddr(), addr2.ToStringAddr());
     157                 :          0 : }
     158                 :            : 
     159                 :          0 : BOOST_AUTO_TEST_CASE(subnet_test)
     160                 :            : {
     161                 :            : 
     162                 :          0 :     BOOST_CHECK(ResolveSubNet("1.2.3.0/24") == ResolveSubNet("1.2.3.0/255.255.255.0"));
     163                 :          0 :     BOOST_CHECK(ResolveSubNet("1.2.3.0/24") != ResolveSubNet("1.2.4.0/255.255.255.0"));
     164                 :          0 :     BOOST_CHECK(ResolveSubNet("1.2.3.0/24").Match(ResolveIP("1.2.3.4")));
     165                 :          0 :     BOOST_CHECK(!ResolveSubNet("1.2.2.0/24").Match(ResolveIP("1.2.3.4")));
     166                 :          0 :     BOOST_CHECK(ResolveSubNet("1.2.3.4").Match(ResolveIP("1.2.3.4")));
     167                 :          0 :     BOOST_CHECK(ResolveSubNet("1.2.3.4/32").Match(ResolveIP("1.2.3.4")));
     168                 :          0 :     BOOST_CHECK(!ResolveSubNet("1.2.3.4").Match(ResolveIP("5.6.7.8")));
     169                 :          0 :     BOOST_CHECK(!ResolveSubNet("1.2.3.4/32").Match(ResolveIP("5.6.7.8")));
     170                 :          0 :     BOOST_CHECK(ResolveSubNet("::ffff:127.0.0.1").Match(ResolveIP("127.0.0.1")));
     171                 :          0 :     BOOST_CHECK(ResolveSubNet("1:2:3:4:5:6:7:8").Match(ResolveIP("1:2:3:4:5:6:7:8")));
     172                 :          0 :     BOOST_CHECK(!ResolveSubNet("1:2:3:4:5:6:7:8").Match(ResolveIP("1:2:3:4:5:6:7:9")));
     173                 :          0 :     BOOST_CHECK(ResolveSubNet("1:2:3:4:5:6:7:0/112").Match(ResolveIP("1:2:3:4:5:6:7:1234")));
     174                 :          0 :     BOOST_CHECK(ResolveSubNet("192.168.0.1/24").Match(ResolveIP("192.168.0.2")));
     175                 :          0 :     BOOST_CHECK(ResolveSubNet("192.168.0.20/29").Match(ResolveIP("192.168.0.18")));
     176                 :          0 :     BOOST_CHECK(ResolveSubNet("1.2.2.1/24").Match(ResolveIP("1.2.2.4")));
     177                 :          0 :     BOOST_CHECK(ResolveSubNet("1.2.2.110/31").Match(ResolveIP("1.2.2.111")));
     178                 :          0 :     BOOST_CHECK(ResolveSubNet("1.2.2.20/26").Match(ResolveIP("1.2.2.63")));
     179                 :            :     // All-Matching IPv6 Matches arbitrary IPv6
     180                 :          0 :     BOOST_CHECK(ResolveSubNet("::/0").Match(ResolveIP("1:2:3:4:5:6:7:1234")));
     181                 :            :     // But not `::` or `0.0.0.0` because they are considered invalid addresses
     182                 :          0 :     BOOST_CHECK(!ResolveSubNet("::/0").Match(ResolveIP("::")));
     183                 :          0 :     BOOST_CHECK(!ResolveSubNet("::/0").Match(ResolveIP("0.0.0.0")));
     184                 :            :     // Addresses from one network (IPv4) don't belong to subnets of another network (IPv6)
     185                 :          0 :     BOOST_CHECK(!ResolveSubNet("::/0").Match(ResolveIP("1.2.3.4")));
     186                 :            :     // All-Matching IPv4 does not Match IPv6
     187                 :          0 :     BOOST_CHECK(!ResolveSubNet("0.0.0.0/0").Match(ResolveIP("1:2:3:4:5:6:7:1234")));
     188                 :            :     // Invalid subnets Match nothing (not even invalid addresses)
     189                 :          0 :     BOOST_CHECK(!CSubNet().Match(ResolveIP("1.2.3.4")));
     190                 :          0 :     BOOST_CHECK(!ResolveSubNet("").Match(ResolveIP("4.5.6.7")));
     191                 :          0 :     BOOST_CHECK(!ResolveSubNet("bloop").Match(ResolveIP("0.0.0.0")));
     192                 :          0 :     BOOST_CHECK(!ResolveSubNet("bloop").Match(ResolveIP("hab")));
     193                 :            :     // Check valid/invalid
     194                 :          0 :     BOOST_CHECK(ResolveSubNet("1.2.3.0/0").IsValid());
     195                 :          0 :     BOOST_CHECK(!ResolveSubNet("1.2.3.0/-1").IsValid());
     196                 :          0 :     BOOST_CHECK(ResolveSubNet("1.2.3.0/32").IsValid());
     197                 :          0 :     BOOST_CHECK(!ResolveSubNet("1.2.3.0/33").IsValid());
     198                 :          0 :     BOOST_CHECK(!ResolveSubNet("1.2.3.0/300").IsValid());
     199                 :          0 :     BOOST_CHECK(ResolveSubNet("1:2:3:4:5:6:7:8/0").IsValid());
     200                 :          0 :     BOOST_CHECK(ResolveSubNet("1:2:3:4:5:6:7:8/33").IsValid());
     201                 :          0 :     BOOST_CHECK(!ResolveSubNet("1:2:3:4:5:6:7:8/-1").IsValid());
     202                 :          0 :     BOOST_CHECK(ResolveSubNet("1:2:3:4:5:6:7:8/128").IsValid());
     203                 :          0 :     BOOST_CHECK(!ResolveSubNet("1:2:3:4:5:6:7:8/129").IsValid());
     204                 :          0 :     BOOST_CHECK(!ResolveSubNet("fuzzy").IsValid());
     205                 :            : 
     206                 :            :     //CNetAddr constructor test
     207                 :          0 :     BOOST_CHECK(CSubNet(ResolveIP("127.0.0.1")).IsValid());
     208                 :          0 :     BOOST_CHECK(CSubNet(ResolveIP("127.0.0.1")).Match(ResolveIP("127.0.0.1")));
     209                 :          0 :     BOOST_CHECK(!CSubNet(ResolveIP("127.0.0.1")).Match(ResolveIP("127.0.0.2")));
     210                 :          0 :     BOOST_CHECK(CSubNet(ResolveIP("127.0.0.1")).ToString() == "127.0.0.1/32");
     211                 :            : 
     212                 :          0 :     CSubNet subnet = CSubNet(ResolveIP("1.2.3.4"), 32);
     213                 :          0 :     BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/32");
     214                 :          0 :     subnet = CSubNet(ResolveIP("1.2.3.4"), 8);
     215                 :          0 :     BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/8");
     216                 :          0 :     subnet = CSubNet(ResolveIP("1.2.3.4"), 0);
     217                 :          0 :     BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/0");
     218                 :            : 
     219                 :          0 :     subnet = CSubNet(ResolveIP("1.2.3.4"), ResolveIP("255.255.255.255"));
     220                 :          0 :     BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/32");
     221                 :          0 :     subnet = CSubNet(ResolveIP("1.2.3.4"), ResolveIP("255.0.0.0"));
     222                 :          0 :     BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/8");
     223                 :          0 :     subnet = CSubNet(ResolveIP("1.2.3.4"), ResolveIP("0.0.0.0"));
     224                 :          0 :     BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/0");
     225                 :            : 
     226                 :          0 :     BOOST_CHECK(CSubNet(ResolveIP("1:2:3:4:5:6:7:8")).IsValid());
     227                 :          0 :     BOOST_CHECK(CSubNet(ResolveIP("1:2:3:4:5:6:7:8")).Match(ResolveIP("1:2:3:4:5:6:7:8")));
     228                 :          0 :     BOOST_CHECK(!CSubNet(ResolveIP("1:2:3:4:5:6:7:8")).Match(ResolveIP("1:2:3:4:5:6:7:9")));
     229                 :          0 :     BOOST_CHECK(CSubNet(ResolveIP("1:2:3:4:5:6:7:8")).ToString() == "1:2:3:4:5:6:7:8/128");
     230                 :            :     // IPv4 address with IPv6 netmask or the other way around.
     231                 :          0 :     BOOST_CHECK(!CSubNet(ResolveIP("1.1.1.1"), ResolveIP("ffff::")).IsValid());
     232                 :          0 :     BOOST_CHECK(!CSubNet(ResolveIP("::1"), ResolveIP("255.0.0.0")).IsValid());
     233                 :            : 
     234                 :            :     // Create Non-IP subnets.
     235                 :            : 
     236                 :          0 :     const CNetAddr tor_addr{
     237                 :          0 :         ResolveIP("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion")};
     238                 :            : 
     239                 :          0 :     subnet = CSubNet(tor_addr);
     240                 :          0 :     BOOST_CHECK(subnet.IsValid());
     241                 :          0 :     BOOST_CHECK_EQUAL(subnet.ToString(), tor_addr.ToStringAddr());
     242                 :          0 :     BOOST_CHECK(subnet.Match(tor_addr));
     243                 :          0 :     BOOST_CHECK(
     244                 :            :         !subnet.Match(ResolveIP("kpgvmscirrdqpekbqjsvw5teanhatztpp2gl6eee4zkowvwfxwenqaid.onion")));
     245                 :          0 :     BOOST_CHECK(!subnet.Match(ResolveIP("1.2.3.4")));
     246                 :            : 
     247                 :          0 :     BOOST_CHECK(!CSubNet(tor_addr, 200).IsValid());
     248                 :          0 :     BOOST_CHECK(!CSubNet(tor_addr, ResolveIP("255.0.0.0")).IsValid());
     249                 :            : 
     250                 :          0 :     subnet = ResolveSubNet("1.2.3.4/255.255.255.255");
     251                 :          0 :     BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/32");
     252                 :          0 :     subnet = ResolveSubNet("1.2.3.4/255.255.255.254");
     253                 :          0 :     BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/31");
     254                 :          0 :     subnet = ResolveSubNet("1.2.3.4/255.255.255.252");
     255                 :          0 :     BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/30");
     256                 :          0 :     subnet = ResolveSubNet("1.2.3.4/255.255.255.248");
     257                 :          0 :     BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/29");
     258                 :          0 :     subnet = ResolveSubNet("1.2.3.4/255.255.255.240");
     259                 :          0 :     BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/28");
     260                 :          0 :     subnet = ResolveSubNet("1.2.3.4/255.255.255.224");
     261                 :          0 :     BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/27");
     262                 :          0 :     subnet = ResolveSubNet("1.2.3.4/255.255.255.192");
     263                 :          0 :     BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/26");
     264                 :          0 :     subnet = ResolveSubNet("1.2.3.4/255.255.255.128");
     265                 :          0 :     BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/25");
     266                 :          0 :     subnet = ResolveSubNet("1.2.3.4/255.255.255.0");
     267                 :          0 :     BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/24");
     268                 :          0 :     subnet = ResolveSubNet("1.2.3.4/255.255.254.0");
     269                 :          0 :     BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.2.0/23");
     270                 :          0 :     subnet = ResolveSubNet("1.2.3.4/255.255.252.0");
     271                 :          0 :     BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/22");
     272                 :          0 :     subnet = ResolveSubNet("1.2.3.4/255.255.248.0");
     273                 :          0 :     BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/21");
     274                 :          0 :     subnet = ResolveSubNet("1.2.3.4/255.255.240.0");
     275                 :          0 :     BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/20");
     276                 :          0 :     subnet = ResolveSubNet("1.2.3.4/255.255.224.0");
     277                 :          0 :     BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/19");
     278                 :          0 :     subnet = ResolveSubNet("1.2.3.4/255.255.192.0");
     279                 :          0 :     BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/18");
     280                 :          0 :     subnet = ResolveSubNet("1.2.3.4/255.255.128.0");
     281                 :          0 :     BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/17");
     282                 :          0 :     subnet = ResolveSubNet("1.2.3.4/255.255.0.0");
     283                 :          0 :     BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/16");
     284                 :          0 :     subnet = ResolveSubNet("1.2.3.4/255.254.0.0");
     285                 :          0 :     BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/15");
     286                 :          0 :     subnet = ResolveSubNet("1.2.3.4/255.252.0.0");
     287                 :          0 :     BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/14");
     288                 :          0 :     subnet = ResolveSubNet("1.2.3.4/255.248.0.0");
     289                 :          0 :     BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/13");
     290                 :          0 :     subnet = ResolveSubNet("1.2.3.4/255.240.0.0");
     291                 :          0 :     BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/12");
     292                 :          0 :     subnet = ResolveSubNet("1.2.3.4/255.224.0.0");
     293                 :          0 :     BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/11");
     294                 :          0 :     subnet = ResolveSubNet("1.2.3.4/255.192.0.0");
     295                 :          0 :     BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/10");
     296                 :          0 :     subnet = ResolveSubNet("1.2.3.4/255.128.0.0");
     297                 :          0 :     BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/9");
     298                 :          0 :     subnet = ResolveSubNet("1.2.3.4/255.0.0.0");
     299                 :          0 :     BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/8");
     300                 :          0 :     subnet = ResolveSubNet("1.2.3.4/254.0.0.0");
     301                 :          0 :     BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/7");
     302                 :          0 :     subnet = ResolveSubNet("1.2.3.4/252.0.0.0");
     303                 :          0 :     BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/6");
     304                 :          0 :     subnet = ResolveSubNet("1.2.3.4/248.0.0.0");
     305                 :          0 :     BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/5");
     306                 :          0 :     subnet = ResolveSubNet("1.2.3.4/240.0.0.0");
     307                 :          0 :     BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/4");
     308                 :          0 :     subnet = ResolveSubNet("1.2.3.4/224.0.0.0");
     309                 :          0 :     BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/3");
     310                 :          0 :     subnet = ResolveSubNet("1.2.3.4/192.0.0.0");
     311                 :          0 :     BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/2");
     312                 :          0 :     subnet = ResolveSubNet("1.2.3.4/128.0.0.0");
     313                 :          0 :     BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/1");
     314                 :          0 :     subnet = ResolveSubNet("1.2.3.4/0.0.0.0");
     315                 :          0 :     BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/0");
     316                 :            : 
     317                 :          0 :     subnet = ResolveSubNet("1:2:3:4:5:6:7:8/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
     318                 :          0 :     BOOST_CHECK_EQUAL(subnet.ToString(), "1:2:3:4:5:6:7:8/128");
     319                 :          0 :     subnet = ResolveSubNet("1:2:3:4:5:6:7:8/ffff:0000:0000:0000:0000:0000:0000:0000");
     320                 :          0 :     BOOST_CHECK_EQUAL(subnet.ToString(), "1::/16");
     321                 :          0 :     subnet = ResolveSubNet("1:2:3:4:5:6:7:8/0000:0000:0000:0000:0000:0000:0000:0000");
     322                 :          0 :     BOOST_CHECK_EQUAL(subnet.ToString(), "::/0");
     323                 :            :     // Invalid netmasks (with 1-bits after 0-bits)
     324                 :          0 :     subnet = ResolveSubNet("1.2.3.4/255.255.232.0");
     325                 :          0 :     BOOST_CHECK(!subnet.IsValid());
     326                 :          0 :     subnet = ResolveSubNet("1.2.3.4/255.0.255.255");
     327                 :          0 :     BOOST_CHECK(!subnet.IsValid());
     328                 :          0 :     subnet = ResolveSubNet("1:2:3:4:5:6:7:8/ffff:ffff:ffff:fffe:ffff:ffff:ffff:ff0f");
     329                 :          0 :     BOOST_CHECK(!subnet.IsValid());
     330                 :          0 : }
     331                 :            : 
     332                 :          0 : BOOST_AUTO_TEST_CASE(netbase_getgroup)
     333                 :            : {
     334                 :          0 :     NetGroupManager netgroupman{std::vector<bool>()}; // use /16
     335                 :          0 :     BOOST_CHECK(netgroupman.GetGroup(ResolveIP("127.0.0.1")) == std::vector<unsigned char>({0})); // Local -> !Routable()
     336                 :          0 :     BOOST_CHECK(netgroupman.GetGroup(ResolveIP("257.0.0.1")) == std::vector<unsigned char>({0})); // !Valid -> !Routable()
     337                 :          0 :     BOOST_CHECK(netgroupman.GetGroup(ResolveIP("10.0.0.1")) == std::vector<unsigned char>({0})); // RFC1918 -> !Routable()
     338                 :          0 :     BOOST_CHECK(netgroupman.GetGroup(ResolveIP("169.254.1.1")) == std::vector<unsigned char>({0})); // RFC3927 -> !Routable()
     339                 :          0 :     BOOST_CHECK(netgroupman.GetGroup(ResolveIP("1.2.3.4")) == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // IPv4
     340                 :          0 :     BOOST_CHECK(netgroupman.GetGroup(ResolveIP("::FFFF:0:102:304")) == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // RFC6145
     341                 :          0 :     BOOST_CHECK(netgroupman.GetGroup(ResolveIP("64:FF9B::102:304")) == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // RFC6052
     342                 :          0 :     BOOST_CHECK(netgroupman.GetGroup(ResolveIP("2002:102:304:9999:9999:9999:9999:9999")) == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // RFC3964
     343                 :          0 :     BOOST_CHECK(netgroupman.GetGroup(ResolveIP("2001:0:9999:9999:9999:9999:FEFD:FCFB")) == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // RFC4380
     344                 :          0 :     BOOST_CHECK(netgroupman.GetGroup(ResolveIP("2001:470:abcd:9999:9999:9999:9999:9999")) == std::vector<unsigned char>({(unsigned char)NET_IPV6, 32, 1, 4, 112, 175})); //he.net
     345                 :          0 :     BOOST_CHECK(netgroupman.GetGroup(ResolveIP("2001:2001:9999:9999:9999:9999:9999:9999")) == std::vector<unsigned char>({(unsigned char)NET_IPV6, 32, 1, 32, 1})); //IPv6
     346                 :            : 
     347                 :            :     // baz.net sha256 hash: 12929400eb4607c4ac075f087167e75286b179c693eb059a01774b864e8fe505
     348                 :          0 :     std::vector<unsigned char> internal_group = {NET_INTERNAL, 0x12, 0x92, 0x94, 0x00, 0xeb, 0x46, 0x07, 0xc4, 0xac, 0x07};
     349                 :          0 :     BOOST_CHECK(netgroupman.GetGroup(CreateInternal("baz.net")) == internal_group);
     350                 :          0 : }
     351                 :            : 
     352                 :          0 : BOOST_AUTO_TEST_CASE(netbase_parsenetwork)
     353                 :            : {
     354                 :          0 :     BOOST_CHECK_EQUAL(ParseNetwork("ipv4"), NET_IPV4);
     355                 :          0 :     BOOST_CHECK_EQUAL(ParseNetwork("ipv6"), NET_IPV6);
     356                 :          0 :     BOOST_CHECK_EQUAL(ParseNetwork("onion"), NET_ONION);
     357                 :          0 :     BOOST_CHECK_EQUAL(ParseNetwork("tor"), NET_ONION);
     358                 :          0 :     BOOST_CHECK_EQUAL(ParseNetwork("cjdns"), NET_CJDNS);
     359                 :            : 
     360                 :          0 :     BOOST_CHECK_EQUAL(ParseNetwork("IPv4"), NET_IPV4);
     361                 :          0 :     BOOST_CHECK_EQUAL(ParseNetwork("IPv6"), NET_IPV6);
     362                 :          0 :     BOOST_CHECK_EQUAL(ParseNetwork("ONION"), NET_ONION);
     363                 :          0 :     BOOST_CHECK_EQUAL(ParseNetwork("TOR"), NET_ONION);
     364                 :          0 :     BOOST_CHECK_EQUAL(ParseNetwork("CJDNS"), NET_CJDNS);
     365                 :            : 
     366                 :          0 :     BOOST_CHECK_EQUAL(ParseNetwork(":)"), NET_UNROUTABLE);
     367                 :          0 :     BOOST_CHECK_EQUAL(ParseNetwork("tÖr"), NET_UNROUTABLE);
     368                 :          0 :     BOOST_CHECK_EQUAL(ParseNetwork("\xfe\xff"), NET_UNROUTABLE);
     369                 :          0 :     BOOST_CHECK_EQUAL(ParseNetwork(""), NET_UNROUTABLE);
     370                 :          0 : }
     371                 :            : 
     372                 :          0 : BOOST_AUTO_TEST_CASE(netpermissions_test)
     373                 :            : {
     374                 :          0 :     bilingual_str error;
     375                 :          0 :     NetWhitebindPermissions whitebindPermissions;
     376                 :          0 :     NetWhitelistPermissions whitelistPermissions;
     377                 :            : 
     378                 :            :     // Detect invalid white bind
     379                 :          0 :     BOOST_CHECK(!NetWhitebindPermissions::TryParse("", whitebindPermissions, error));
     380                 :          0 :     BOOST_CHECK(error.original.find("Cannot resolve -whitebind address") != std::string::npos);
     381                 :          0 :     BOOST_CHECK(!NetWhitebindPermissions::TryParse("127.0.0.1", whitebindPermissions, error));
     382                 :          0 :     BOOST_CHECK(error.original.find("Need to specify a port with -whitebind") != std::string::npos);
     383                 :          0 :     BOOST_CHECK(!NetWhitebindPermissions::TryParse("", whitebindPermissions, error));
     384                 :            : 
     385                 :            :     // If no permission flags, assume backward compatibility
     386                 :          0 :     BOOST_CHECK(NetWhitebindPermissions::TryParse("1.2.3.4:32", whitebindPermissions, error));
     387                 :          0 :     BOOST_CHECK(error.empty());
     388                 :          0 :     BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, NetPermissionFlags::Implicit);
     389                 :          0 :     BOOST_CHECK(NetPermissions::HasFlag(whitebindPermissions.m_flags, NetPermissionFlags::Implicit));
     390                 :          0 :     NetPermissions::ClearFlag(whitebindPermissions.m_flags, NetPermissionFlags::Implicit);
     391                 :          0 :     BOOST_CHECK(!NetPermissions::HasFlag(whitebindPermissions.m_flags, NetPermissionFlags::Implicit));
     392                 :          0 :     BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, NetPermissionFlags::None);
     393                 :          0 :     NetPermissions::AddFlag(whitebindPermissions.m_flags, NetPermissionFlags::Implicit);
     394                 :          0 :     BOOST_CHECK(NetPermissions::HasFlag(whitebindPermissions.m_flags, NetPermissionFlags::Implicit));
     395                 :            : 
     396                 :            :     // Can set one permission
     397                 :          0 :     BOOST_CHECK(NetWhitebindPermissions::TryParse("bloom@1.2.3.4:32", whitebindPermissions, error));
     398                 :          0 :     BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, NetPermissionFlags::BloomFilter);
     399                 :          0 :     BOOST_CHECK(NetWhitebindPermissions::TryParse("@1.2.3.4:32", whitebindPermissions, error));
     400                 :          0 :     BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, NetPermissionFlags::None);
     401                 :            : 
     402                 :          0 :     NetWhitebindPermissions noban, noban_download, download_noban, download;
     403                 :            : 
     404                 :            :     // "noban" implies "download"
     405                 :          0 :     BOOST_REQUIRE(NetWhitebindPermissions::TryParse("noban@1.2.3.4:32", noban, error));
     406                 :          0 :     BOOST_CHECK_EQUAL(noban.m_flags, NetPermissionFlags::NoBan);
     407                 :          0 :     BOOST_CHECK(NetPermissions::HasFlag(noban.m_flags, NetPermissionFlags::Download));
     408                 :          0 :     BOOST_CHECK(NetPermissions::HasFlag(noban.m_flags, NetPermissionFlags::NoBan));
     409                 :            : 
     410                 :            :     // "noban,download" is equivalent to "noban"
     411                 :          0 :     BOOST_REQUIRE(NetWhitebindPermissions::TryParse("noban,download@1.2.3.4:32", noban_download, error));
     412                 :          0 :     BOOST_CHECK_EQUAL(noban_download.m_flags, noban.m_flags);
     413                 :            : 
     414                 :            :     // "download,noban" is equivalent to "noban"
     415                 :          0 :     BOOST_REQUIRE(NetWhitebindPermissions::TryParse("download,noban@1.2.3.4:32", download_noban, error));
     416                 :          0 :     BOOST_CHECK_EQUAL(download_noban.m_flags, noban.m_flags);
     417                 :            : 
     418                 :            :     // "download" excludes (does not imply) "noban"
     419                 :          0 :     BOOST_REQUIRE(NetWhitebindPermissions::TryParse("download@1.2.3.4:32", download, error));
     420                 :          0 :     BOOST_CHECK_EQUAL(download.m_flags, NetPermissionFlags::Download);
     421                 :          0 :     BOOST_CHECK(NetPermissions::HasFlag(download.m_flags, NetPermissionFlags::Download));
     422                 :          0 :     BOOST_CHECK(!NetPermissions::HasFlag(download.m_flags, NetPermissionFlags::NoBan));
     423                 :            : 
     424                 :            :     // Happy path, can parse flags
     425                 :          0 :     BOOST_CHECK(NetWhitebindPermissions::TryParse("bloom,forcerelay@1.2.3.4:32", whitebindPermissions, error));
     426                 :            :     // forcerelay should also activate the relay permission
     427                 :          0 :     BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, NetPermissionFlags::BloomFilter | NetPermissionFlags::ForceRelay | NetPermissionFlags::Relay);
     428                 :          0 :     BOOST_CHECK(NetWhitebindPermissions::TryParse("bloom,relay,noban@1.2.3.4:32", whitebindPermissions, error));
     429                 :          0 :     BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, NetPermissionFlags::BloomFilter | NetPermissionFlags::Relay | NetPermissionFlags::NoBan);
     430                 :          0 :     BOOST_CHECK(NetWhitebindPermissions::TryParse("bloom,forcerelay,noban@1.2.3.4:32", whitebindPermissions, error));
     431                 :          0 :     BOOST_CHECK(NetWhitebindPermissions::TryParse("all@1.2.3.4:32", whitebindPermissions, error));
     432                 :          0 :     BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, NetPermissionFlags::All);
     433                 :            : 
     434                 :            :     // Allow dups
     435                 :          0 :     BOOST_CHECK(NetWhitebindPermissions::TryParse("bloom,relay,noban,noban@1.2.3.4:32", whitebindPermissions, error));
     436                 :          0 :     BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, NetPermissionFlags::BloomFilter | NetPermissionFlags::Relay | NetPermissionFlags::NoBan | NetPermissionFlags::Download); // "noban" implies "download"
     437                 :            : 
     438                 :            :     // Allow empty
     439                 :          0 :     BOOST_CHECK(NetWhitebindPermissions::TryParse("bloom,relay,,noban@1.2.3.4:32", whitebindPermissions, error));
     440                 :          0 :     BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, NetPermissionFlags::BloomFilter | NetPermissionFlags::Relay | NetPermissionFlags::NoBan);
     441                 :          0 :     BOOST_CHECK(NetWhitebindPermissions::TryParse(",@1.2.3.4:32", whitebindPermissions, error));
     442                 :          0 :     BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, NetPermissionFlags::None);
     443                 :          0 :     BOOST_CHECK(NetWhitebindPermissions::TryParse(",,@1.2.3.4:32", whitebindPermissions, error));
     444                 :          0 :     BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, NetPermissionFlags::None);
     445                 :            : 
     446                 :            :     // Detect invalid flag
     447                 :          0 :     BOOST_CHECK(!NetWhitebindPermissions::TryParse("bloom,forcerelay,oopsie@1.2.3.4:32", whitebindPermissions, error));
     448                 :          0 :     BOOST_CHECK(error.original.find("Invalid P2P permission") != std::string::npos);
     449                 :            : 
     450                 :            :     // Check netmask error
     451                 :          0 :     BOOST_CHECK(!NetWhitelistPermissions::TryParse("bloom,forcerelay,noban@1.2.3.4:32", whitelistPermissions, error));
     452                 :          0 :     BOOST_CHECK(error.original.find("Invalid netmask specified in -whitelist") != std::string::npos);
     453                 :            : 
     454                 :            :     // Happy path for whitelist parsing
     455                 :          0 :     BOOST_CHECK(NetWhitelistPermissions::TryParse("noban@1.2.3.4", whitelistPermissions, error));
     456                 :          0 :     BOOST_CHECK_EQUAL(whitelistPermissions.m_flags, NetPermissionFlags::NoBan);
     457                 :          0 :     BOOST_CHECK(NetPermissions::HasFlag(whitelistPermissions.m_flags, NetPermissionFlags::NoBan));
     458                 :            : 
     459                 :          0 :     BOOST_CHECK(NetWhitelistPermissions::TryParse("bloom,forcerelay,noban,relay@1.2.3.4/32", whitelistPermissions, error));
     460                 :          0 :     BOOST_CHECK_EQUAL(whitelistPermissions.m_flags, NetPermissionFlags::BloomFilter | NetPermissionFlags::ForceRelay | NetPermissionFlags::NoBan | NetPermissionFlags::Relay);
     461                 :          0 :     BOOST_CHECK(error.empty());
     462                 :          0 :     BOOST_CHECK_EQUAL(whitelistPermissions.m_subnet.ToString(), "1.2.3.4/32");
     463                 :          0 :     BOOST_CHECK(NetWhitelistPermissions::TryParse("bloom,forcerelay,noban,relay,mempool@1.2.3.4/32", whitelistPermissions, error));
     464                 :            : 
     465                 :          0 :     const auto strings = NetPermissions::ToStrings(NetPermissionFlags::All);
     466                 :          0 :     BOOST_CHECK_EQUAL(strings.size(), 7U);
     467                 :          0 :     BOOST_CHECK(std::find(strings.begin(), strings.end(), "bloomfilter") != strings.end());
     468                 :          0 :     BOOST_CHECK(std::find(strings.begin(), strings.end(), "forcerelay") != strings.end());
     469                 :          0 :     BOOST_CHECK(std::find(strings.begin(), strings.end(), "relay") != strings.end());
     470                 :          0 :     BOOST_CHECK(std::find(strings.begin(), strings.end(), "noban") != strings.end());
     471                 :          0 :     BOOST_CHECK(std::find(strings.begin(), strings.end(), "mempool") != strings.end());
     472                 :          0 :     BOOST_CHECK(std::find(strings.begin(), strings.end(), "download") != strings.end());
     473                 :          0 :     BOOST_CHECK(std::find(strings.begin(), strings.end(), "addr") != strings.end());
     474                 :          0 : }
     475                 :            : 
     476                 :          0 : BOOST_AUTO_TEST_CASE(netbase_dont_resolve_strings_with_embedded_nul_characters)
     477                 :            : {
     478                 :          0 :     BOOST_CHECK(LookupHost("127.0.0.1"s, false).has_value());
     479                 :          0 :     BOOST_CHECK(!LookupHost("127.0.0.1\0"s, false).has_value());
     480                 :          0 :     BOOST_CHECK(!LookupHost("127.0.0.1\0example.com"s, false).has_value());
     481                 :          0 :     BOOST_CHECK(!LookupHost("127.0.0.1\0example.com\0"s, false).has_value());
     482                 :          0 :     CSubNet ret;
     483                 :          0 :     BOOST_CHECK(LookupSubNet("1.2.3.0/24"s, ret));
     484                 :          0 :     BOOST_CHECK(!LookupSubNet("1.2.3.0/24\0"s, ret));
     485                 :          0 :     BOOST_CHECK(!LookupSubNet("1.2.3.0/24\0example.com"s, ret));
     486                 :          0 :     BOOST_CHECK(!LookupSubNet("1.2.3.0/24\0example.com\0"s, ret));
     487                 :          0 :     BOOST_CHECK(LookupSubNet("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion"s, ret));
     488                 :          0 :     BOOST_CHECK(!LookupSubNet("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion\0"s, ret));
     489                 :          0 :     BOOST_CHECK(!LookupSubNet("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion\0example.com"s, ret));
     490                 :          0 :     BOOST_CHECK(!LookupSubNet("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion\0example.com\0"s, ret));
     491                 :          0 : }
     492                 :            : 
     493                 :            : // Since CNetAddr (un)ser is tested separately in net_tests.cpp here we only
     494                 :            : // try a few edge cases for port, service flags and time.
     495                 :            : 
     496                 :          0 : static const std::vector<CAddress> fixture_addresses({
     497                 :          0 :     CAddress{
     498                 :          0 :         CService(CNetAddr(in6_addr(IN6ADDR_LOOPBACK_INIT)), 0 /* port */),
     499                 :            :         NODE_NONE,
     500                 :          0 :         NodeSeconds{0x4966bc61s}, /* Fri Jan  9 02:54:25 UTC 2009 */
     501                 :            :     },
     502                 :          0 :     CAddress{
     503                 :          0 :         CService(CNetAddr(in6_addr(IN6ADDR_LOOPBACK_INIT)), 0x00f1 /* port */),
     504                 :            :         NODE_NETWORK,
     505                 :          0 :         NodeSeconds{0x83766279s}, /* Tue Nov 22 11:22:33 UTC 2039 */
     506                 :            :     },
     507                 :          0 :     CAddress{
     508                 :          0 :         CService(CNetAddr(in6_addr(IN6ADDR_LOOPBACK_INIT)), 0xf1f2 /* port */),
     509                 :            :         static_cast<ServiceFlags>(NODE_WITNESS | NODE_COMPACT_FILTERS | NODE_NETWORK_LIMITED),
     510                 :          0 :         NodeSeconds{0xffffffffs}, /* Sun Feb  7 06:28:15 UTC 2106 */
     511                 :            :     },
     512                 :            : });
     513                 :            : 
     514                 :            : // fixture_addresses should equal to this when serialized in V1 format.
     515                 :            : // When this is unserialized from V1 format it should equal to fixture_addresses.
     516                 :            : static constexpr const char* stream_addrv1_hex =
     517                 :            :     "03" // number of entries
     518                 :            : 
     519                 :            :     "61bc6649"                         // time, Fri Jan  9 02:54:25 UTC 2009
     520                 :            :     "0000000000000000"                 // service flags, NODE_NONE
     521                 :            :     "00000000000000000000000000000001" // address, fixed 16 bytes (IPv4 embedded in IPv6)
     522                 :            :     "0000"                             // port
     523                 :            : 
     524                 :            :     "79627683"                         // time, Tue Nov 22 11:22:33 UTC 2039
     525                 :            :     "0100000000000000"                 // service flags, NODE_NETWORK
     526                 :            :     "00000000000000000000000000000001" // address, fixed 16 bytes (IPv6)
     527                 :            :     "00f1"                             // port
     528                 :            : 
     529                 :            :     "ffffffff"                         // time, Sun Feb  7 06:28:15 UTC 2106
     530                 :            :     "4804000000000000"                 // service flags, NODE_WITNESS | NODE_COMPACT_FILTERS | NODE_NETWORK_LIMITED
     531                 :            :     "00000000000000000000000000000001" // address, fixed 16 bytes (IPv6)
     532                 :            :     "f1f2";                            // port
     533                 :            : 
     534                 :            : // fixture_addresses should equal to this when serialized in V2 format.
     535                 :            : // When this is unserialized from V2 format it should equal to fixture_addresses.
     536                 :            : static constexpr const char* stream_addrv2_hex =
     537                 :            :     "03" // number of entries
     538                 :            : 
     539                 :            :     "61bc6649"                         // time, Fri Jan  9 02:54:25 UTC 2009
     540                 :            :     "00"                               // service flags, COMPACTSIZE(NODE_NONE)
     541                 :            :     "02"                               // network id, IPv6
     542                 :            :     "10"                               // address length, COMPACTSIZE(16)
     543                 :            :     "00000000000000000000000000000001" // address
     544                 :            :     "0000"                             // port
     545                 :            : 
     546                 :            :     "79627683"                         // time, Tue Nov 22 11:22:33 UTC 2039
     547                 :            :     "01"                               // service flags, COMPACTSIZE(NODE_NETWORK)
     548                 :            :     "02"                               // network id, IPv6
     549                 :            :     "10"                               // address length, COMPACTSIZE(16)
     550                 :            :     "00000000000000000000000000000001" // address
     551                 :            :     "00f1"                             // port
     552                 :            : 
     553                 :            :     "ffffffff"                         // time, Sun Feb  7 06:28:15 UTC 2106
     554                 :            :     "fd4804"                           // service flags, COMPACTSIZE(NODE_WITNESS | NODE_COMPACT_FILTERS | NODE_NETWORK_LIMITED)
     555                 :            :     "02"                               // network id, IPv6
     556                 :            :     "10"                               // address length, COMPACTSIZE(16)
     557                 :            :     "00000000000000000000000000000001" // address
     558                 :            :     "f1f2";                            // port
     559                 :            : 
     560                 :          0 : BOOST_AUTO_TEST_CASE(caddress_serialize_v1)
     561                 :            : {
     562                 :          0 :     DataStream s{};
     563                 :            : 
     564                 :          0 :     s << CAddress::V1_NETWORK(fixture_addresses);
     565                 :          0 :     BOOST_CHECK_EQUAL(HexStr(s), stream_addrv1_hex);
     566                 :          0 : }
     567                 :            : 
     568                 :          0 : BOOST_AUTO_TEST_CASE(caddress_unserialize_v1)
     569                 :            : {
     570                 :          0 :     DataStream s{ParseHex(stream_addrv1_hex)};
     571                 :          0 :     std::vector<CAddress> addresses_unserialized;
     572                 :            : 
     573                 :          0 :     s >> CAddress::V1_NETWORK(addresses_unserialized);
     574                 :          0 :     BOOST_CHECK(fixture_addresses == addresses_unserialized);
     575                 :          0 : }
     576                 :            : 
     577                 :          0 : BOOST_AUTO_TEST_CASE(caddress_serialize_v2)
     578                 :            : {
     579                 :          0 :     DataStream s{};
     580                 :            : 
     581                 :          0 :     s << CAddress::V2_NETWORK(fixture_addresses);
     582                 :          0 :     BOOST_CHECK_EQUAL(HexStr(s), stream_addrv2_hex);
     583                 :          0 : }
     584                 :            : 
     585                 :          0 : BOOST_AUTO_TEST_CASE(caddress_unserialize_v2)
     586                 :            : {
     587                 :          0 :     DataStream s{ParseHex(stream_addrv2_hex)};
     588                 :          0 :     std::vector<CAddress> addresses_unserialized;
     589                 :            : 
     590                 :          0 :     s >> CAddress::V2_NETWORK(addresses_unserialized);
     591                 :          0 :     BOOST_CHECK(fixture_addresses == addresses_unserialized);
     592                 :          0 : }
     593                 :            : 
     594                 :          0 : BOOST_AUTO_TEST_CASE(isbadport)
     595                 :            : {
     596                 :          0 :     BOOST_CHECK(IsBadPort(1));
     597                 :          0 :     BOOST_CHECK(IsBadPort(22));
     598                 :          0 :     BOOST_CHECK(IsBadPort(6000));
     599                 :            : 
     600                 :          0 :     BOOST_CHECK(!IsBadPort(80));
     601                 :          0 :     BOOST_CHECK(!IsBadPort(443));
     602                 :          0 :     BOOST_CHECK(!IsBadPort(8333));
     603                 :            : 
     604                 :            :     // Check all ports, there must be 80 bad ports in total.
     605                 :          0 :     size_t total_bad_ports{0};
     606                 :          0 :     for (uint16_t port = std::numeric_limits<uint16_t>::max(); port > 0; --port) {
     607                 :          0 :         if (IsBadPort(port)) {
     608                 :          0 :             ++total_bad_ports;
     609                 :          0 :         }
     610                 :          0 :     }
     611                 :          0 :     BOOST_CHECK_EQUAL(total_bad_ports, 80);
     612                 :          0 : }
     613                 :            : 
     614                 :          0 : BOOST_AUTO_TEST_SUITE_END()

Generated by: LCOV version 1.14