2020-04-16 13:14:08 -04:00
|
|
|
// Copyright (c) 2012-2020 The Bitcoin Core developers
|
2014-12-13 12:09:33 +08:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2014-03-18 10:11:00 +01:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2019-06-20 18:37:51 +09:00
|
|
|
#include <net_permissions.h>
|
2021-03-06 20:14:08 +01:00
|
|
|
#include <netaddress.h>
|
2020-04-16 13:11:54 -04:00
|
|
|
#include <netbase.h>
|
2020-05-19 17:39:05 +02:00
|
|
|
#include <protocol.h>
|
|
|
|
#include <serialize.h>
|
|
|
|
#include <streams.h>
|
2019-11-05 15:18:59 -05:00
|
|
|
#include <test/util/setup_common.h>
|
2018-10-22 15:51:11 -07:00
|
|
|
#include <util/strencodings.h>
|
2020-06-05 14:26:16 +02:00
|
|
|
#include <util/translation.h>
|
2020-05-19 17:39:05 +02:00
|
|
|
#include <version.h>
|
2012-06-08 18:42:05 +02:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
2013-04-13 00:13:08 -05:00
|
|
|
#include <boost/test/unit_test.hpp>
|
2012-06-08 18:42:05 +02:00
|
|
|
|
2020-09-23 08:40:38 +02:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2015-03-12 09:34:42 +01:00
|
|
|
BOOST_FIXTURE_TEST_SUITE(netbase_tests, BasicTestingSetup)
|
2012-06-08 18:42:05 +02:00
|
|
|
|
2019-12-29 20:23:48 +00:00
|
|
|
static CNetAddr ResolveIP(const std::string& ip)
|
2016-05-31 13:05:52 -04:00
|
|
|
{
|
|
|
|
CNetAddr addr;
|
|
|
|
LookupHost(ip, addr, false);
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
2019-12-29 20:23:48 +00:00
|
|
|
static CSubNet ResolveSubNet(const std::string& subnet)
|
2016-05-31 15:50:24 -04:00
|
|
|
{
|
|
|
|
CSubNet ret;
|
|
|
|
LookupSubNet(subnet, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-12-29 20:23:48 +00:00
|
|
|
static CNetAddr CreateInternal(const std::string& host)
|
2017-05-23 20:04:38 -04:00
|
|
|
{
|
|
|
|
CNetAddr addr;
|
|
|
|
addr.SetInternal(host);
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
2012-06-08 18:42:05 +02:00
|
|
|
BOOST_AUTO_TEST_CASE(netbase_networks)
|
|
|
|
{
|
2016-05-31 13:05:52 -04:00
|
|
|
BOOST_CHECK(ResolveIP("127.0.0.1").GetNetwork() == NET_UNROUTABLE);
|
|
|
|
BOOST_CHECK(ResolveIP("::1").GetNetwork() == NET_UNROUTABLE);
|
|
|
|
BOOST_CHECK(ResolveIP("8.8.8.8").GetNetwork() == NET_IPV4);
|
|
|
|
BOOST_CHECK(ResolveIP("2001::8888").GetNetwork() == NET_IPV6);
|
2018-07-02 11:37:59 +02:00
|
|
|
BOOST_CHECK(ResolveIP("FD87:D87E:EB43:edb1:8e4:3588:e546:35ca").GetNetwork() == NET_ONION);
|
2017-05-23 20:04:38 -04:00
|
|
|
BOOST_CHECK(CreateInternal("foo.com").GetNetwork() == NET_INTERNAL);
|
2016-05-31 13:05:52 -04:00
|
|
|
|
2012-06-08 18:42:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(netbase_properties)
|
|
|
|
{
|
2016-05-31 13:05:52 -04:00
|
|
|
|
|
|
|
BOOST_CHECK(ResolveIP("127.0.0.1").IsIPv4());
|
|
|
|
BOOST_CHECK(ResolveIP("::FFFF:192.168.1.1").IsIPv4());
|
|
|
|
BOOST_CHECK(ResolveIP("::1").IsIPv6());
|
|
|
|
BOOST_CHECK(ResolveIP("10.0.0.1").IsRFC1918());
|
|
|
|
BOOST_CHECK(ResolveIP("192.168.1.1").IsRFC1918());
|
|
|
|
BOOST_CHECK(ResolveIP("172.31.255.255").IsRFC1918());
|
2019-12-16 19:43:15 -08:00
|
|
|
BOOST_CHECK(ResolveIP("198.18.0.0").IsRFC2544());
|
|
|
|
BOOST_CHECK(ResolveIP("198.19.255.255").IsRFC2544());
|
2016-05-31 13:05:52 -04:00
|
|
|
BOOST_CHECK(ResolveIP("2001:0DB8::").IsRFC3849());
|
|
|
|
BOOST_CHECK(ResolveIP("169.254.1.1").IsRFC3927());
|
|
|
|
BOOST_CHECK(ResolveIP("2002::1").IsRFC3964());
|
|
|
|
BOOST_CHECK(ResolveIP("FC00::").IsRFC4193());
|
|
|
|
BOOST_CHECK(ResolveIP("2001::2").IsRFC4380());
|
|
|
|
BOOST_CHECK(ResolveIP("2001:10::").IsRFC4843());
|
2019-03-28 15:21:31 -04:00
|
|
|
BOOST_CHECK(ResolveIP("2001:20::").IsRFC7343());
|
2016-05-31 13:05:52 -04:00
|
|
|
BOOST_CHECK(ResolveIP("FE80::").IsRFC4862());
|
|
|
|
BOOST_CHECK(ResolveIP("64:FF9B::").IsRFC6052());
|
|
|
|
BOOST_CHECK(ResolveIP("FD87:D87E:EB43:edb1:8e4:3588:e546:35ca").IsTor());
|
|
|
|
BOOST_CHECK(ResolveIP("127.0.0.1").IsLocal());
|
|
|
|
BOOST_CHECK(ResolveIP("::1").IsLocal());
|
|
|
|
BOOST_CHECK(ResolveIP("8.8.8.8").IsRoutable());
|
|
|
|
BOOST_CHECK(ResolveIP("2001::1").IsRoutable());
|
|
|
|
BOOST_CHECK(ResolveIP("127.0.0.1").IsValid());
|
2017-05-23 20:04:38 -04:00
|
|
|
BOOST_CHECK(CreateInternal("FD6B:88C0:8724:edb1:8e4:3588:e546:35ca").IsInternal());
|
|
|
|
BOOST_CHECK(CreateInternal("bar.com").IsInternal());
|
2016-05-31 13:05:52 -04:00
|
|
|
|
2012-06-08 18:42:05 +02:00
|
|
|
}
|
|
|
|
|
2021-03-01 21:35:28 +01:00
|
|
|
bool static TestSplitHost(const std::string& test, const std::string& host, uint16_t port)
|
2012-06-08 18:42:05 +02:00
|
|
|
{
|
2016-12-05 16:03:53 +09:00
|
|
|
std::string hostOut;
|
2021-03-01 21:35:28 +01:00
|
|
|
uint16_t portOut{0};
|
2012-06-08 18:42:05 +02:00
|
|
|
SplitHostPort(test, portOut, hostOut);
|
|
|
|
return hostOut == host && port == portOut;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(netbase_splithost)
|
|
|
|
{
|
2021-03-01 21:35:28 +01:00
|
|
|
BOOST_CHECK(TestSplitHost("www.bitcoincore.org", "www.bitcoincore.org", 0));
|
|
|
|
BOOST_CHECK(TestSplitHost("[www.bitcoincore.org]", "www.bitcoincore.org", 0));
|
2018-07-22 10:43:57 -04:00
|
|
|
BOOST_CHECK(TestSplitHost("www.bitcoincore.org:80", "www.bitcoincore.org", 80));
|
|
|
|
BOOST_CHECK(TestSplitHost("[www.bitcoincore.org]:80", "www.bitcoincore.org", 80));
|
2021-03-01 21:35:28 +01:00
|
|
|
BOOST_CHECK(TestSplitHost("127.0.0.1", "127.0.0.1", 0));
|
2012-06-08 18:42:05 +02:00
|
|
|
BOOST_CHECK(TestSplitHost("127.0.0.1:8333", "127.0.0.1", 8333));
|
2021-03-01 21:35:28 +01:00
|
|
|
BOOST_CHECK(TestSplitHost("[127.0.0.1]", "127.0.0.1", 0));
|
2012-06-08 18:42:05 +02:00
|
|
|
BOOST_CHECK(TestSplitHost("[127.0.0.1]:8333", "127.0.0.1", 8333));
|
2021-03-01 21:35:28 +01:00
|
|
|
BOOST_CHECK(TestSplitHost("::ffff:127.0.0.1", "::ffff:127.0.0.1", 0));
|
2012-06-08 18:42:05 +02:00
|
|
|
BOOST_CHECK(TestSplitHost("[::ffff:127.0.0.1]:8333", "::ffff:127.0.0.1", 8333));
|
|
|
|
BOOST_CHECK(TestSplitHost("[::]:8333", "::", 8333));
|
2021-03-01 21:35:28 +01:00
|
|
|
BOOST_CHECK(TestSplitHost("::8333", "::8333", 0));
|
2012-06-08 18:42:05 +02:00
|
|
|
BOOST_CHECK(TestSplitHost(":8333", "", 8333));
|
|
|
|
BOOST_CHECK(TestSplitHost("[]:8333", "", 8333));
|
2021-03-01 21:35:28 +01:00
|
|
|
BOOST_CHECK(TestSplitHost("", "", 0));
|
2012-06-08 18:42:05 +02:00
|
|
|
}
|
|
|
|
|
2016-12-05 16:03:53 +09:00
|
|
|
bool static TestParse(std::string src, std::string canon)
|
2012-06-08 18:42:05 +02:00
|
|
|
{
|
2019-12-11 16:39:29 +00:00
|
|
|
CService addr(LookupNumeric(src, 65535));
|
2012-06-08 18:42:05 +02:00
|
|
|
return canon == addr.ToString();
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(netbase_lookupnumeric)
|
|
|
|
{
|
|
|
|
BOOST_CHECK(TestParse("127.0.0.1", "127.0.0.1:65535"));
|
|
|
|
BOOST_CHECK(TestParse("127.0.0.1:8333", "127.0.0.1:8333"));
|
|
|
|
BOOST_CHECK(TestParse("::ffff:127.0.0.1", "127.0.0.1:65535"));
|
|
|
|
BOOST_CHECK(TestParse("::", "[::]:65535"));
|
|
|
|
BOOST_CHECK(TestParse("[::]:8333", "[::]:8333"));
|
|
|
|
BOOST_CHECK(TestParse("[127.0.0.1]", "127.0.0.1:65535"));
|
2016-08-04 16:37:49 -04:00
|
|
|
BOOST_CHECK(TestParse(":::", "[::]:0"));
|
2017-06-13 17:26:50 -04:00
|
|
|
|
|
|
|
// verify that an internal address fails to resolve
|
|
|
|
BOOST_CHECK(TestParse("[fd6b:88c0:8724:1:2:3:4:5]", "[::]:0"));
|
|
|
|
// and that a one-off resolves correctly
|
|
|
|
BOOST_CHECK(TestParse("[fd6c:88c0:8724:1:2:3:4:5]", "[fd6c:88c0:8724:1:2:3:4:5]:65535"));
|
2012-06-08 18:42:05 +02:00
|
|
|
}
|
|
|
|
|
2012-04-29 02:11:56 +02:00
|
|
|
BOOST_AUTO_TEST_CASE(onioncat_test)
|
|
|
|
{
|
2014-03-24 20:26:02 +00:00
|
|
|
// values from https://web.archive.org/web/20121122003543/http://www.cypherpunk.at/onioncat/wiki/OnionCat
|
2016-05-31 13:05:52 -04:00
|
|
|
CNetAddr addr1(ResolveIP("5wyqrzbvrdsumnok.onion"));
|
|
|
|
CNetAddr addr2(ResolveIP("FD87:D87E:EB43:edb1:8e4:3588:e546:35ca"));
|
2012-04-29 02:11:56 +02:00
|
|
|
BOOST_CHECK(addr1 == addr2);
|
|
|
|
BOOST_CHECK(addr1.IsTor());
|
|
|
|
BOOST_CHECK(addr1.ToStringIP() == "5wyqrzbvrdsumnok.onion");
|
|
|
|
BOOST_CHECK(addr1.IsRoutable());
|
2016-05-31 13:05:52 -04:00
|
|
|
|
2012-04-29 02:11:56 +02:00
|
|
|
}
|
|
|
|
|
2020-05-14 17:03:11 +02:00
|
|
|
BOOST_AUTO_TEST_CASE(embedded_test)
|
|
|
|
{
|
|
|
|
CNetAddr addr1(ResolveIP("1.2.3.4"));
|
|
|
|
CNetAddr addr2(ResolveIP("::FFFF:0102:0304"));
|
|
|
|
BOOST_CHECK(addr2.IsIPv4());
|
|
|
|
BOOST_CHECK_EQUAL(addr1.ToString(), addr2.ToString());
|
|
|
|
}
|
|
|
|
|
2014-04-28 11:08:57 +02:00
|
|
|
BOOST_AUTO_TEST_CASE(subnet_test)
|
|
|
|
{
|
2016-05-31 13:05:52 -04:00
|
|
|
|
2016-05-31 15:50:24 -04:00
|
|
|
BOOST_CHECK(ResolveSubNet("1.2.3.0/24") == ResolveSubNet("1.2.3.0/255.255.255.0"));
|
|
|
|
BOOST_CHECK(ResolveSubNet("1.2.3.0/24") != ResolveSubNet("1.2.4.0/255.255.255.0"));
|
|
|
|
BOOST_CHECK(ResolveSubNet("1.2.3.0/24").Match(ResolveIP("1.2.3.4")));
|
|
|
|
BOOST_CHECK(!ResolveSubNet("1.2.2.0/24").Match(ResolveIP("1.2.3.4")));
|
|
|
|
BOOST_CHECK(ResolveSubNet("1.2.3.4").Match(ResolveIP("1.2.3.4")));
|
|
|
|
BOOST_CHECK(ResolveSubNet("1.2.3.4/32").Match(ResolveIP("1.2.3.4")));
|
|
|
|
BOOST_CHECK(!ResolveSubNet("1.2.3.4").Match(ResolveIP("5.6.7.8")));
|
|
|
|
BOOST_CHECK(!ResolveSubNet("1.2.3.4/32").Match(ResolveIP("5.6.7.8")));
|
|
|
|
BOOST_CHECK(ResolveSubNet("::ffff:127.0.0.1").Match(ResolveIP("127.0.0.1")));
|
|
|
|
BOOST_CHECK(ResolveSubNet("1:2:3:4:5:6:7:8").Match(ResolveIP("1:2:3:4:5:6:7:8")));
|
|
|
|
BOOST_CHECK(!ResolveSubNet("1:2:3:4:5:6:7:8").Match(ResolveIP("1:2:3:4:5:6:7:9")));
|
|
|
|
BOOST_CHECK(ResolveSubNet("1:2:3:4:5:6:7:0/112").Match(ResolveIP("1:2:3:4:5:6:7:1234")));
|
|
|
|
BOOST_CHECK(ResolveSubNet("192.168.0.1/24").Match(ResolveIP("192.168.0.2")));
|
|
|
|
BOOST_CHECK(ResolveSubNet("192.168.0.20/29").Match(ResolveIP("192.168.0.18")));
|
|
|
|
BOOST_CHECK(ResolveSubNet("1.2.2.1/24").Match(ResolveIP("1.2.2.4")));
|
|
|
|
BOOST_CHECK(ResolveSubNet("1.2.2.110/31").Match(ResolveIP("1.2.2.111")));
|
|
|
|
BOOST_CHECK(ResolveSubNet("1.2.2.20/26").Match(ResolveIP("1.2.2.63")));
|
2020-05-14 17:03:11 +02:00
|
|
|
// All-Matching IPv6 Matches arbitrary IPv6
|
2016-05-31 15:50:24 -04:00
|
|
|
BOOST_CHECK(ResolveSubNet("::/0").Match(ResolveIP("1:2:3:4:5:6:7:1234")));
|
2020-05-15 17:02:05 +02:00
|
|
|
// But not `::` or `0.0.0.0` because they are considered invalid addresses
|
|
|
|
BOOST_CHECK(!ResolveSubNet("::/0").Match(ResolveIP("::")));
|
|
|
|
BOOST_CHECK(!ResolveSubNet("::/0").Match(ResolveIP("0.0.0.0")));
|
2020-05-14 17:03:11 +02:00
|
|
|
// Addresses from one network (IPv4) don't belong to subnets of another network (IPv6)
|
|
|
|
BOOST_CHECK(!ResolveSubNet("::/0").Match(ResolveIP("1.2.3.4")));
|
2014-04-28 11:08:57 +02:00
|
|
|
// All-Matching IPv4 does not Match IPv6
|
2016-05-31 15:50:24 -04:00
|
|
|
BOOST_CHECK(!ResolveSubNet("0.0.0.0/0").Match(ResolveIP("1:2:3:4:5:6:7:1234")));
|
2014-04-28 11:08:57 +02:00
|
|
|
// Invalid subnets Match nothing (not even invalid addresses)
|
2016-05-31 13:05:52 -04:00
|
|
|
BOOST_CHECK(!CSubNet().Match(ResolveIP("1.2.3.4")));
|
2016-05-31 15:50:24 -04:00
|
|
|
BOOST_CHECK(!ResolveSubNet("").Match(ResolveIP("4.5.6.7")));
|
|
|
|
BOOST_CHECK(!ResolveSubNet("bloop").Match(ResolveIP("0.0.0.0")));
|
|
|
|
BOOST_CHECK(!ResolveSubNet("bloop").Match(ResolveIP("hab")));
|
2014-04-28 11:08:57 +02:00
|
|
|
// Check valid/invalid
|
2016-05-31 15:50:24 -04:00
|
|
|
BOOST_CHECK(ResolveSubNet("1.2.3.0/0").IsValid());
|
|
|
|
BOOST_CHECK(!ResolveSubNet("1.2.3.0/-1").IsValid());
|
|
|
|
BOOST_CHECK(ResolveSubNet("1.2.3.0/32").IsValid());
|
|
|
|
BOOST_CHECK(!ResolveSubNet("1.2.3.0/33").IsValid());
|
2020-08-24 21:34:26 +02:00
|
|
|
BOOST_CHECK(!ResolveSubNet("1.2.3.0/300").IsValid());
|
2016-05-31 15:50:24 -04:00
|
|
|
BOOST_CHECK(ResolveSubNet("1:2:3:4:5:6:7:8/0").IsValid());
|
|
|
|
BOOST_CHECK(ResolveSubNet("1:2:3:4:5:6:7:8/33").IsValid());
|
|
|
|
BOOST_CHECK(!ResolveSubNet("1:2:3:4:5:6:7:8/-1").IsValid());
|
|
|
|
BOOST_CHECK(ResolveSubNet("1:2:3:4:5:6:7:8/128").IsValid());
|
|
|
|
BOOST_CHECK(!ResolveSubNet("1:2:3:4:5:6:7:8/129").IsValid());
|
|
|
|
BOOST_CHECK(!ResolveSubNet("fuzzy").IsValid());
|
2015-06-29 20:37:22 +02:00
|
|
|
|
|
|
|
//CNetAddr constructor test
|
2016-05-31 13:05:52 -04:00
|
|
|
BOOST_CHECK(CSubNet(ResolveIP("127.0.0.1")).IsValid());
|
|
|
|
BOOST_CHECK(CSubNet(ResolveIP("127.0.0.1")).Match(ResolveIP("127.0.0.1")));
|
|
|
|
BOOST_CHECK(!CSubNet(ResolveIP("127.0.0.1")).Match(ResolveIP("127.0.0.2")));
|
|
|
|
BOOST_CHECK(CSubNet(ResolveIP("127.0.0.1")).ToString() == "127.0.0.1/32");
|
2015-06-29 20:37:22 +02:00
|
|
|
|
2016-05-31 18:18:41 -04:00
|
|
|
CSubNet subnet = CSubNet(ResolveIP("1.2.3.4"), 32);
|
|
|
|
BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/32");
|
|
|
|
subnet = CSubNet(ResolveIP("1.2.3.4"), 8);
|
|
|
|
BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/8");
|
|
|
|
subnet = CSubNet(ResolveIP("1.2.3.4"), 0);
|
|
|
|
BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/0");
|
|
|
|
|
|
|
|
subnet = CSubNet(ResolveIP("1.2.3.4"), ResolveIP("255.255.255.255"));
|
|
|
|
BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/32");
|
|
|
|
subnet = CSubNet(ResolveIP("1.2.3.4"), ResolveIP("255.0.0.0"));
|
|
|
|
BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/8");
|
|
|
|
subnet = CSubNet(ResolveIP("1.2.3.4"), ResolveIP("0.0.0.0"));
|
|
|
|
BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/0");
|
|
|
|
|
2016-05-31 13:05:52 -04:00
|
|
|
BOOST_CHECK(CSubNet(ResolveIP("1:2:3:4:5:6:7:8")).IsValid());
|
|
|
|
BOOST_CHECK(CSubNet(ResolveIP("1:2:3:4:5:6:7:8")).Match(ResolveIP("1:2:3:4:5:6:7:8")));
|
|
|
|
BOOST_CHECK(!CSubNet(ResolveIP("1:2:3:4:5:6:7:8")).Match(ResolveIP("1:2:3:4:5:6:7:9")));
|
|
|
|
BOOST_CHECK(CSubNet(ResolveIP("1:2:3:4:5:6:7:8")).ToString() == "1:2:3:4:5:6:7:8/128");
|
2020-08-24 21:34:26 +02:00
|
|
|
// IPv4 address with IPv6 netmask or the other way around.
|
|
|
|
BOOST_CHECK(!CSubNet(ResolveIP("1.1.1.1"), ResolveIP("ffff::")).IsValid());
|
|
|
|
BOOST_CHECK(!CSubNet(ResolveIP("::1"), ResolveIP("255.0.0.0")).IsValid());
|
2021-01-04 18:37:52 +01:00
|
|
|
|
|
|
|
// Create Non-IP subnets.
|
|
|
|
|
|
|
|
const CNetAddr tor_addr{
|
|
|
|
ResolveIP("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion")};
|
|
|
|
|
|
|
|
subnet = CSubNet(tor_addr);
|
|
|
|
BOOST_CHECK(subnet.IsValid());
|
|
|
|
BOOST_CHECK_EQUAL(subnet.ToString(), tor_addr.ToString());
|
|
|
|
BOOST_CHECK(subnet.Match(tor_addr));
|
|
|
|
BOOST_CHECK(
|
|
|
|
!subnet.Match(ResolveIP("kpgvmscirrdqpekbqjsvw5teanhatztpp2gl6eee4zkowvwfxwenqaid.onion")));
|
|
|
|
BOOST_CHECK(!subnet.Match(ResolveIP("1.2.3.4")));
|
|
|
|
|
|
|
|
BOOST_CHECK(!CSubNet(tor_addr, 200).IsValid());
|
|
|
|
BOOST_CHECK(!CSubNet(tor_addr, ResolveIP("255.0.0.0")).IsValid());
|
2015-06-23 17:20:00 +02:00
|
|
|
|
2016-05-31 18:18:41 -04:00
|
|
|
subnet = ResolveSubNet("1.2.3.4/255.255.255.255");
|
2015-06-23 17:20:00 +02:00
|
|
|
BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/32");
|
2016-05-31 15:50:24 -04:00
|
|
|
subnet = ResolveSubNet("1.2.3.4/255.255.255.254");
|
2015-06-23 17:20:00 +02:00
|
|
|
BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/31");
|
2016-05-31 15:50:24 -04:00
|
|
|
subnet = ResolveSubNet("1.2.3.4/255.255.255.252");
|
2015-06-23 17:20:00 +02:00
|
|
|
BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/30");
|
2016-05-31 15:50:24 -04:00
|
|
|
subnet = ResolveSubNet("1.2.3.4/255.255.255.248");
|
2015-06-23 17:20:00 +02:00
|
|
|
BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/29");
|
2016-05-31 15:50:24 -04:00
|
|
|
subnet = ResolveSubNet("1.2.3.4/255.255.255.240");
|
2015-06-23 17:20:00 +02:00
|
|
|
BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/28");
|
2016-05-31 15:50:24 -04:00
|
|
|
subnet = ResolveSubNet("1.2.3.4/255.255.255.224");
|
2015-06-23 17:20:00 +02:00
|
|
|
BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/27");
|
2016-05-31 15:50:24 -04:00
|
|
|
subnet = ResolveSubNet("1.2.3.4/255.255.255.192");
|
2015-06-23 17:20:00 +02:00
|
|
|
BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/26");
|
2016-05-31 15:50:24 -04:00
|
|
|
subnet = ResolveSubNet("1.2.3.4/255.255.255.128");
|
2015-06-23 17:20:00 +02:00
|
|
|
BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/25");
|
2016-05-31 15:50:24 -04:00
|
|
|
subnet = ResolveSubNet("1.2.3.4/255.255.255.0");
|
2015-06-23 17:20:00 +02:00
|
|
|
BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.0/24");
|
2016-05-31 15:50:24 -04:00
|
|
|
subnet = ResolveSubNet("1.2.3.4/255.255.254.0");
|
2015-06-23 17:20:00 +02:00
|
|
|
BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.2.0/23");
|
2016-05-31 15:50:24 -04:00
|
|
|
subnet = ResolveSubNet("1.2.3.4/255.255.252.0");
|
2015-06-23 17:20:00 +02:00
|
|
|
BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/22");
|
2016-05-31 15:50:24 -04:00
|
|
|
subnet = ResolveSubNet("1.2.3.4/255.255.248.0");
|
2015-06-23 17:20:00 +02:00
|
|
|
BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/21");
|
2016-05-31 15:50:24 -04:00
|
|
|
subnet = ResolveSubNet("1.2.3.4/255.255.240.0");
|
2015-06-23 17:20:00 +02:00
|
|
|
BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/20");
|
2016-05-31 15:50:24 -04:00
|
|
|
subnet = ResolveSubNet("1.2.3.4/255.255.224.0");
|
2015-06-23 17:20:00 +02:00
|
|
|
BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/19");
|
2016-05-31 15:50:24 -04:00
|
|
|
subnet = ResolveSubNet("1.2.3.4/255.255.192.0");
|
2015-06-23 17:20:00 +02:00
|
|
|
BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/18");
|
2016-05-31 15:50:24 -04:00
|
|
|
subnet = ResolveSubNet("1.2.3.4/255.255.128.0");
|
2015-06-23 17:20:00 +02:00
|
|
|
BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/17");
|
2016-05-31 15:50:24 -04:00
|
|
|
subnet = ResolveSubNet("1.2.3.4/255.255.0.0");
|
2015-06-23 17:20:00 +02:00
|
|
|
BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/16");
|
2016-05-31 15:50:24 -04:00
|
|
|
subnet = ResolveSubNet("1.2.3.4/255.254.0.0");
|
2015-06-23 17:20:00 +02:00
|
|
|
BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/15");
|
2016-05-31 15:50:24 -04:00
|
|
|
subnet = ResolveSubNet("1.2.3.4/255.252.0.0");
|
2015-06-23 17:20:00 +02:00
|
|
|
BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/14");
|
2016-05-31 15:50:24 -04:00
|
|
|
subnet = ResolveSubNet("1.2.3.4/255.248.0.0");
|
2015-06-23 17:20:00 +02:00
|
|
|
BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/13");
|
2016-05-31 15:50:24 -04:00
|
|
|
subnet = ResolveSubNet("1.2.3.4/255.240.0.0");
|
2015-06-23 17:20:00 +02:00
|
|
|
BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/12");
|
2016-05-31 15:50:24 -04:00
|
|
|
subnet = ResolveSubNet("1.2.3.4/255.224.0.0");
|
2015-06-23 17:20:00 +02:00
|
|
|
BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/11");
|
2016-05-31 15:50:24 -04:00
|
|
|
subnet = ResolveSubNet("1.2.3.4/255.192.0.0");
|
2015-06-23 17:20:00 +02:00
|
|
|
BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/10");
|
2016-05-31 15:50:24 -04:00
|
|
|
subnet = ResolveSubNet("1.2.3.4/255.128.0.0");
|
2015-06-23 17:20:00 +02:00
|
|
|
BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/9");
|
2016-05-31 15:50:24 -04:00
|
|
|
subnet = ResolveSubNet("1.2.3.4/255.0.0.0");
|
2015-06-23 17:20:00 +02:00
|
|
|
BOOST_CHECK_EQUAL(subnet.ToString(), "1.0.0.0/8");
|
2016-05-31 15:50:24 -04:00
|
|
|
subnet = ResolveSubNet("1.2.3.4/254.0.0.0");
|
2015-06-23 17:20:00 +02:00
|
|
|
BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/7");
|
2016-05-31 15:50:24 -04:00
|
|
|
subnet = ResolveSubNet("1.2.3.4/252.0.0.0");
|
2015-06-23 17:20:00 +02:00
|
|
|
BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/6");
|
2016-05-31 15:50:24 -04:00
|
|
|
subnet = ResolveSubNet("1.2.3.4/248.0.0.0");
|
2015-06-23 17:20:00 +02:00
|
|
|
BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/5");
|
2016-05-31 15:50:24 -04:00
|
|
|
subnet = ResolveSubNet("1.2.3.4/240.0.0.0");
|
2015-06-23 17:20:00 +02:00
|
|
|
BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/4");
|
2016-05-31 15:50:24 -04:00
|
|
|
subnet = ResolveSubNet("1.2.3.4/224.0.0.0");
|
2015-06-23 17:20:00 +02:00
|
|
|
BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/3");
|
2016-05-31 15:50:24 -04:00
|
|
|
subnet = ResolveSubNet("1.2.3.4/192.0.0.0");
|
2015-06-23 17:20:00 +02:00
|
|
|
BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/2");
|
2016-05-31 15:50:24 -04:00
|
|
|
subnet = ResolveSubNet("1.2.3.4/128.0.0.0");
|
2015-06-23 17:20:00 +02:00
|
|
|
BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/1");
|
2016-05-31 15:50:24 -04:00
|
|
|
subnet = ResolveSubNet("1.2.3.4/0.0.0.0");
|
2015-06-23 17:20:00 +02:00
|
|
|
BOOST_CHECK_EQUAL(subnet.ToString(), "0.0.0.0/0");
|
|
|
|
|
2016-05-31 15:50:24 -04:00
|
|
|
subnet = ResolveSubNet("1:2:3:4:5:6:7:8/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
|
2015-06-23 17:20:00 +02:00
|
|
|
BOOST_CHECK_EQUAL(subnet.ToString(), "1:2:3:4:5:6:7:8/128");
|
2016-05-31 15:50:24 -04:00
|
|
|
subnet = ResolveSubNet("1:2:3:4:5:6:7:8/ffff:0000:0000:0000:0000:0000:0000:0000");
|
2015-06-23 17:20:00 +02:00
|
|
|
BOOST_CHECK_EQUAL(subnet.ToString(), "1::/16");
|
2016-05-31 15:50:24 -04:00
|
|
|
subnet = ResolveSubNet("1:2:3:4:5:6:7:8/0000:0000:0000:0000:0000:0000:0000:0000");
|
2015-06-23 17:20:00 +02:00
|
|
|
BOOST_CHECK_EQUAL(subnet.ToString(), "::/0");
|
2020-08-24 21:03:31 +02:00
|
|
|
// Invalid netmasks (with 1-bits after 0-bits)
|
2016-05-31 15:50:24 -04:00
|
|
|
subnet = ResolveSubNet("1.2.3.4/255.255.232.0");
|
2020-08-24 21:03:31 +02:00
|
|
|
BOOST_CHECK(!subnet.IsValid());
|
|
|
|
subnet = ResolveSubNet("1.2.3.4/255.0.255.255");
|
|
|
|
BOOST_CHECK(!subnet.IsValid());
|
2016-05-31 15:50:24 -04:00
|
|
|
subnet = ResolveSubNet("1:2:3:4:5:6:7:8/ffff:ffff:ffff:fffe:ffff:ffff:ffff:ff0f");
|
2020-08-24 21:03:31 +02:00
|
|
|
BOOST_CHECK(!subnet.IsValid());
|
2014-04-28 11:08:57 +02:00
|
|
|
}
|
|
|
|
|
2015-08-19 14:34:56 -04:00
|
|
|
BOOST_AUTO_TEST_CASE(netbase_getgroup)
|
|
|
|
{
|
2019-12-24 13:18:44 -05:00
|
|
|
std::vector<bool> asmap; // use /16
|
|
|
|
BOOST_CHECK(ResolveIP("127.0.0.1").GetGroup(asmap) == std::vector<unsigned char>({0})); // Local -> !Routable()
|
|
|
|
BOOST_CHECK(ResolveIP("257.0.0.1").GetGroup(asmap) == std::vector<unsigned char>({0})); // !Valid -> !Routable()
|
|
|
|
BOOST_CHECK(ResolveIP("10.0.0.1").GetGroup(asmap) == std::vector<unsigned char>({0})); // RFC1918 -> !Routable()
|
|
|
|
BOOST_CHECK(ResolveIP("169.254.1.1").GetGroup(asmap) == std::vector<unsigned char>({0})); // RFC3927 -> !Routable()
|
|
|
|
BOOST_CHECK(ResolveIP("1.2.3.4").GetGroup(asmap) == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // IPv4
|
|
|
|
BOOST_CHECK(ResolveIP("::FFFF:0:102:304").GetGroup(asmap) == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // RFC6145
|
|
|
|
BOOST_CHECK(ResolveIP("64:FF9B::102:304").GetGroup(asmap) == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // RFC6052
|
|
|
|
BOOST_CHECK(ResolveIP("2002:102:304:9999:9999:9999:9999:9999").GetGroup(asmap) == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // RFC3964
|
|
|
|
BOOST_CHECK(ResolveIP("2001:0:9999:9999:9999:9999:FEFD:FCFB").GetGroup(asmap) == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // RFC4380
|
|
|
|
BOOST_CHECK(ResolveIP("FD87:D87E:EB43:edb1:8e4:3588:e546:35ca").GetGroup(asmap) == std::vector<unsigned char>({(unsigned char)NET_ONION, 239})); // Tor
|
|
|
|
BOOST_CHECK(ResolveIP("2001:470:abcd:9999:9999:9999:9999:9999").GetGroup(asmap) == std::vector<unsigned char>({(unsigned char)NET_IPV6, 32, 1, 4, 112, 175})); //he.net
|
|
|
|
BOOST_CHECK(ResolveIP("2001:2001:9999:9999:9999:9999:9999:9999").GetGroup(asmap) == std::vector<unsigned char>({(unsigned char)NET_IPV6, 32, 1, 32, 1})); //IPv6
|
2016-05-31 13:05:52 -04:00
|
|
|
|
2017-05-23 20:04:38 -04:00
|
|
|
// baz.net sha256 hash: 12929400eb4607c4ac075f087167e75286b179c693eb059a01774b864e8fe505
|
|
|
|
std::vector<unsigned char> internal_group = {NET_INTERNAL, 0x12, 0x92, 0x94, 0x00, 0xeb, 0x46, 0x07, 0xc4, 0xac, 0x07};
|
2019-12-24 13:18:44 -05:00
|
|
|
BOOST_CHECK(CreateInternal("baz.net").GetGroup(asmap) == internal_group);
|
2015-08-19 14:34:56 -04:00
|
|
|
}
|
|
|
|
|
2018-07-15 21:45:30 +02:00
|
|
|
BOOST_AUTO_TEST_CASE(netbase_parsenetwork)
|
|
|
|
{
|
|
|
|
BOOST_CHECK_EQUAL(ParseNetwork("ipv4"), NET_IPV4);
|
|
|
|
BOOST_CHECK_EQUAL(ParseNetwork("ipv6"), NET_IPV6);
|
|
|
|
BOOST_CHECK_EQUAL(ParseNetwork("onion"), NET_ONION);
|
|
|
|
BOOST_CHECK_EQUAL(ParseNetwork("tor"), NET_ONION);
|
|
|
|
|
|
|
|
BOOST_CHECK_EQUAL(ParseNetwork("IPv4"), NET_IPV4);
|
|
|
|
BOOST_CHECK_EQUAL(ParseNetwork("IPv6"), NET_IPV6);
|
|
|
|
BOOST_CHECK_EQUAL(ParseNetwork("ONION"), NET_ONION);
|
|
|
|
BOOST_CHECK_EQUAL(ParseNetwork("TOR"), NET_ONION);
|
|
|
|
|
|
|
|
BOOST_CHECK_EQUAL(ParseNetwork(":)"), NET_UNROUTABLE);
|
|
|
|
BOOST_CHECK_EQUAL(ParseNetwork("tÖr"), NET_UNROUTABLE);
|
|
|
|
BOOST_CHECK_EQUAL(ParseNetwork("\xfe\xff"), NET_UNROUTABLE);
|
|
|
|
BOOST_CHECK_EQUAL(ParseNetwork(""), NET_UNROUTABLE);
|
|
|
|
}
|
|
|
|
|
2019-06-20 18:37:51 +09:00
|
|
|
BOOST_AUTO_TEST_CASE(netpermissions_test)
|
|
|
|
{
|
2020-06-05 14:26:16 +02:00
|
|
|
bilingual_str error;
|
2019-06-20 18:37:51 +09:00
|
|
|
NetWhitebindPermissions whitebindPermissions;
|
|
|
|
NetWhitelistPermissions whitelistPermissions;
|
|
|
|
|
|
|
|
// Detect invalid white bind
|
|
|
|
BOOST_CHECK(!NetWhitebindPermissions::TryParse("", whitebindPermissions, error));
|
2020-06-05 14:26:16 +02:00
|
|
|
BOOST_CHECK(error.original.find("Cannot resolve -whitebind address") != std::string::npos);
|
2019-06-20 18:37:51 +09:00
|
|
|
BOOST_CHECK(!NetWhitebindPermissions::TryParse("127.0.0.1", whitebindPermissions, error));
|
2020-06-05 14:26:16 +02:00
|
|
|
BOOST_CHECK(error.original.find("Need to specify a port with -whitebind") != std::string::npos);
|
2019-06-20 18:37:51 +09:00
|
|
|
BOOST_CHECK(!NetWhitebindPermissions::TryParse("", whitebindPermissions, error));
|
|
|
|
|
|
|
|
// If no permission flags, assume backward compatibility
|
|
|
|
BOOST_CHECK(NetWhitebindPermissions::TryParse("1.2.3.4:32", whitebindPermissions, error));
|
|
|
|
BOOST_CHECK(error.empty());
|
2021-03-22 19:19:05 +01:00
|
|
|
BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, NetPermissionFlags::PF_ISIMPLICIT);
|
|
|
|
BOOST_CHECK(NetPermissions::HasFlag(whitebindPermissions.m_flags, NetPermissionFlags::PF_ISIMPLICIT));
|
|
|
|
NetPermissions::ClearFlag(whitebindPermissions.m_flags, NetPermissionFlags::PF_ISIMPLICIT);
|
|
|
|
BOOST_CHECK(!NetPermissions::HasFlag(whitebindPermissions.m_flags, NetPermissionFlags::PF_ISIMPLICIT));
|
|
|
|
BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, NetPermissionFlags::PF_NONE);
|
|
|
|
NetPermissions::AddFlag(whitebindPermissions.m_flags, NetPermissionFlags::PF_ISIMPLICIT);
|
|
|
|
BOOST_CHECK(NetPermissions::HasFlag(whitebindPermissions.m_flags, NetPermissionFlags::PF_ISIMPLICIT));
|
2019-06-20 18:37:51 +09:00
|
|
|
|
|
|
|
// Can set one permission
|
|
|
|
BOOST_CHECK(NetWhitebindPermissions::TryParse("bloom@1.2.3.4:32", whitebindPermissions, error));
|
2021-03-22 19:19:05 +01:00
|
|
|
BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, NetPermissionFlags::PF_BLOOMFILTER);
|
2019-06-20 18:37:51 +09:00
|
|
|
BOOST_CHECK(NetWhitebindPermissions::TryParse("@1.2.3.4:32", whitebindPermissions, error));
|
2021-03-22 19:19:05 +01:00
|
|
|
BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, NetPermissionFlags::PF_NONE);
|
2019-06-20 18:37:51 +09:00
|
|
|
|
2021-04-09 20:32:40 +02:00
|
|
|
NetWhitebindPermissions noban, noban_download, download_noban, download;
|
|
|
|
|
|
|
|
// "noban" implies "download"
|
|
|
|
BOOST_REQUIRE(NetWhitebindPermissions::TryParse("noban@1.2.3.4:32", noban, error));
|
|
|
|
BOOST_CHECK_EQUAL(noban.m_flags, NetPermissionFlags::PF_NOBAN);
|
|
|
|
BOOST_CHECK(NetPermissions::HasFlag(noban.m_flags, NetPermissionFlags::PF_DOWNLOAD));
|
|
|
|
BOOST_CHECK(NetPermissions::HasFlag(noban.m_flags, NetPermissionFlags::PF_NOBAN));
|
|
|
|
|
|
|
|
// "noban,download" is equivalent to "noban"
|
|
|
|
BOOST_REQUIRE(NetWhitebindPermissions::TryParse("noban,download@1.2.3.4:32", noban_download, error));
|
|
|
|
BOOST_CHECK_EQUAL(noban_download.m_flags, noban.m_flags);
|
|
|
|
|
|
|
|
// "download,noban" is equivalent to "noban"
|
|
|
|
BOOST_REQUIRE(NetWhitebindPermissions::TryParse("download,noban@1.2.3.4:32", download_noban, error));
|
|
|
|
BOOST_CHECK_EQUAL(download_noban.m_flags, noban.m_flags);
|
|
|
|
|
|
|
|
// "download" excludes (does not imply) "noban"
|
|
|
|
BOOST_REQUIRE(NetWhitebindPermissions::TryParse("download@1.2.3.4:32", download, error));
|
|
|
|
BOOST_CHECK_EQUAL(download.m_flags, NetPermissionFlags::PF_DOWNLOAD);
|
|
|
|
BOOST_CHECK(NetPermissions::HasFlag(download.m_flags, NetPermissionFlags::PF_DOWNLOAD));
|
|
|
|
BOOST_CHECK(!NetPermissions::HasFlag(download.m_flags, NetPermissionFlags::PF_NOBAN));
|
|
|
|
|
2019-06-20 18:37:51 +09:00
|
|
|
// Happy path, can parse flags
|
|
|
|
BOOST_CHECK(NetWhitebindPermissions::TryParse("bloom,forcerelay@1.2.3.4:32", whitebindPermissions, error));
|
|
|
|
// forcerelay should also activate the relay permission
|
2021-03-22 19:19:05 +01:00
|
|
|
BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, NetPermissionFlags::PF_BLOOMFILTER | NetPermissionFlags::PF_FORCERELAY | NetPermissionFlags::PF_RELAY);
|
2019-06-20 18:37:51 +09:00
|
|
|
BOOST_CHECK(NetWhitebindPermissions::TryParse("bloom,relay,noban@1.2.3.4:32", whitebindPermissions, error));
|
2021-03-22 19:19:05 +01:00
|
|
|
BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, NetPermissionFlags::PF_BLOOMFILTER | NetPermissionFlags::PF_RELAY | NetPermissionFlags::PF_NOBAN);
|
2019-06-20 18:37:51 +09:00
|
|
|
BOOST_CHECK(NetWhitebindPermissions::TryParse("bloom,forcerelay,noban@1.2.3.4:32", whitebindPermissions, error));
|
|
|
|
BOOST_CHECK(NetWhitebindPermissions::TryParse("all@1.2.3.4:32", whitebindPermissions, error));
|
2021-03-22 19:19:05 +01:00
|
|
|
BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, NetPermissionFlags::PF_ALL);
|
2019-06-20 18:37:51 +09:00
|
|
|
|
|
|
|
// Allow dups
|
|
|
|
BOOST_CHECK(NetWhitebindPermissions::TryParse("bloom,relay,noban,noban@1.2.3.4:32", whitebindPermissions, error));
|
2021-03-22 19:19:05 +01:00
|
|
|
BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, NetPermissionFlags::PF_BLOOMFILTER | NetPermissionFlags::PF_RELAY | NetPermissionFlags::PF_NOBAN | NetPermissionFlags::PF_DOWNLOAD); // "noban" implies "download"
|
2019-06-20 18:37:51 +09:00
|
|
|
|
|
|
|
// Allow empty
|
|
|
|
BOOST_CHECK(NetWhitebindPermissions::TryParse("bloom,relay,,noban@1.2.3.4:32", whitebindPermissions, error));
|
2021-03-22 19:19:05 +01:00
|
|
|
BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, NetPermissionFlags::PF_BLOOMFILTER | NetPermissionFlags::PF_RELAY | NetPermissionFlags::PF_NOBAN);
|
2019-06-20 18:37:51 +09:00
|
|
|
BOOST_CHECK(NetWhitebindPermissions::TryParse(",@1.2.3.4:32", whitebindPermissions, error));
|
2021-03-22 19:19:05 +01:00
|
|
|
BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, NetPermissionFlags::PF_NONE);
|
2019-06-20 18:37:51 +09:00
|
|
|
BOOST_CHECK(NetWhitebindPermissions::TryParse(",,@1.2.3.4:32", whitebindPermissions, error));
|
2021-03-22 19:19:05 +01:00
|
|
|
BOOST_CHECK_EQUAL(whitebindPermissions.m_flags, NetPermissionFlags::PF_NONE);
|
2019-06-20 18:37:51 +09:00
|
|
|
|
|
|
|
// Detect invalid flag
|
|
|
|
BOOST_CHECK(!NetWhitebindPermissions::TryParse("bloom,forcerelay,oopsie@1.2.3.4:32", whitebindPermissions, error));
|
2020-06-05 14:26:16 +02:00
|
|
|
BOOST_CHECK(error.original.find("Invalid P2P permission") != std::string::npos);
|
2019-06-20 18:37:51 +09:00
|
|
|
|
2020-07-09 17:59:54 +02:00
|
|
|
// Check netmask error
|
2019-06-20 18:37:51 +09:00
|
|
|
BOOST_CHECK(!NetWhitelistPermissions::TryParse("bloom,forcerelay,noban@1.2.3.4:32", whitelistPermissions, error));
|
2020-06-05 14:26:16 +02:00
|
|
|
BOOST_CHECK(error.original.find("Invalid netmask specified in -whitelist") != std::string::npos);
|
2019-06-20 18:37:51 +09:00
|
|
|
|
|
|
|
// Happy path for whitelist parsing
|
|
|
|
BOOST_CHECK(NetWhitelistPermissions::TryParse("noban@1.2.3.4", whitelistPermissions, error));
|
2021-03-22 19:19:05 +01:00
|
|
|
BOOST_CHECK_EQUAL(whitelistPermissions.m_flags, NetPermissionFlags::PF_NOBAN);
|
2021-04-09 20:32:40 +02:00
|
|
|
BOOST_CHECK(NetPermissions::HasFlag(whitelistPermissions.m_flags, NetPermissionFlags::PF_NOBAN));
|
|
|
|
|
2019-06-20 18:37:51 +09:00
|
|
|
BOOST_CHECK(NetWhitelistPermissions::TryParse("bloom,forcerelay,noban,relay@1.2.3.4/32", whitelistPermissions, error));
|
2021-03-22 19:19:05 +01:00
|
|
|
BOOST_CHECK_EQUAL(whitelistPermissions.m_flags, NetPermissionFlags::PF_BLOOMFILTER | NetPermissionFlags::PF_FORCERELAY | NetPermissionFlags::PF_NOBAN | NetPermissionFlags::PF_RELAY);
|
2019-06-20 18:37:51 +09:00
|
|
|
BOOST_CHECK(error.empty());
|
|
|
|
BOOST_CHECK_EQUAL(whitelistPermissions.m_subnet.ToString(), "1.2.3.4/32");
|
|
|
|
BOOST_CHECK(NetWhitelistPermissions::TryParse("bloom,forcerelay,noban,relay,mempool@1.2.3.4/32", whitelistPermissions, error));
|
|
|
|
|
2021-03-22 19:19:05 +01:00
|
|
|
const auto strings = NetPermissions::ToStrings(NetPermissionFlags::PF_ALL);
|
2020-06-03 18:51:34 +03:00
|
|
|
BOOST_CHECK_EQUAL(strings.size(), 7U);
|
2019-06-20 18:37:51 +09:00
|
|
|
BOOST_CHECK(std::find(strings.begin(), strings.end(), "bloomfilter") != strings.end());
|
|
|
|
BOOST_CHECK(std::find(strings.begin(), strings.end(), "forcerelay") != strings.end());
|
|
|
|
BOOST_CHECK(std::find(strings.begin(), strings.end(), "relay") != strings.end());
|
|
|
|
BOOST_CHECK(std::find(strings.begin(), strings.end(), "noban") != strings.end());
|
|
|
|
BOOST_CHECK(std::find(strings.begin(), strings.end(), "mempool") != strings.end());
|
2020-06-06 17:07:25 +02:00
|
|
|
BOOST_CHECK(std::find(strings.begin(), strings.end(), "download") != strings.end());
|
2020-06-03 18:51:34 +03:00
|
|
|
BOOST_CHECK(std::find(strings.begin(), strings.end(), "addr") != strings.end());
|
2019-06-20 18:37:51 +09:00
|
|
|
}
|
|
|
|
|
2019-12-16 11:46:19 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(netbase_dont_resolve_strings_with_embedded_nul_characters)
|
|
|
|
{
|
|
|
|
CNetAddr addr;
|
2020-09-23 08:40:38 +02:00
|
|
|
BOOST_CHECK(LookupHost("127.0.0.1"s, addr, false));
|
|
|
|
BOOST_CHECK(!LookupHost("127.0.0.1\0"s, addr, false));
|
|
|
|
BOOST_CHECK(!LookupHost("127.0.0.1\0example.com"s, addr, false));
|
|
|
|
BOOST_CHECK(!LookupHost("127.0.0.1\0example.com\0"s, addr, false));
|
2019-12-16 11:46:19 +00:00
|
|
|
CSubNet ret;
|
2020-09-23 08:40:38 +02:00
|
|
|
BOOST_CHECK(LookupSubNet("1.2.3.0/24"s, ret));
|
|
|
|
BOOST_CHECK(!LookupSubNet("1.2.3.0/24\0"s, ret));
|
|
|
|
BOOST_CHECK(!LookupSubNet("1.2.3.0/24\0example.com"s, ret));
|
|
|
|
BOOST_CHECK(!LookupSubNet("1.2.3.0/24\0example.com\0"s, ret));
|
2021-01-04 18:37:52 +01:00
|
|
|
BOOST_CHECK(LookupSubNet("5wyqrzbvrdsumnok.onion"s, ret));
|
2020-09-23 08:40:38 +02:00
|
|
|
BOOST_CHECK(!LookupSubNet("5wyqrzbvrdsumnok.onion\0"s, ret));
|
|
|
|
BOOST_CHECK(!LookupSubNet("5wyqrzbvrdsumnok.onion\0example.com"s, ret));
|
|
|
|
BOOST_CHECK(!LookupSubNet("5wyqrzbvrdsumnok.onion\0example.com\0"s, ret));
|
2019-12-16 11:46:19 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 17:39:05 +02:00
|
|
|
// Since CNetAddr (un)ser is tested separately in net_tests.cpp here we only
|
|
|
|
// try a few edge cases for port, service flags and time.
|
|
|
|
|
|
|
|
static const std::vector<CAddress> fixture_addresses({
|
|
|
|
CAddress(
|
2020-10-12 10:25:08 +02:00
|
|
|
CService(CNetAddr(in6_addr(IN6ADDR_LOOPBACK_INIT)), 0 /* port */),
|
2020-05-19 17:39:05 +02:00
|
|
|
NODE_NONE,
|
|
|
|
0x4966bc61U /* Fri Jan 9 02:54:25 UTC 2009 */
|
|
|
|
),
|
|
|
|
CAddress(
|
2020-10-12 10:25:08 +02:00
|
|
|
CService(CNetAddr(in6_addr(IN6ADDR_LOOPBACK_INIT)), 0x00f1 /* port */),
|
2020-05-19 17:39:05 +02:00
|
|
|
NODE_NETWORK,
|
|
|
|
0x83766279U /* Tue Nov 22 11:22:33 UTC 2039 */
|
|
|
|
),
|
|
|
|
CAddress(
|
2020-10-12 10:25:08 +02:00
|
|
|
CService(CNetAddr(in6_addr(IN6ADDR_LOOPBACK_INIT)), 0xf1f2 /* port */),
|
2020-05-19 17:39:05 +02:00
|
|
|
static_cast<ServiceFlags>(NODE_WITNESS | NODE_COMPACT_FILTERS | NODE_NETWORK_LIMITED),
|
|
|
|
0xffffffffU /* Sun Feb 7 06:28:15 UTC 2106 */
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
|
|
|
// fixture_addresses should equal to this when serialized in V1 format.
|
|
|
|
// When this is unserialized from V1 format it should equal to fixture_addresses.
|
|
|
|
static constexpr const char* stream_addrv1_hex =
|
|
|
|
"03" // number of entries
|
|
|
|
|
|
|
|
"61bc6649" // time, Fri Jan 9 02:54:25 UTC 2009
|
|
|
|
"0000000000000000" // service flags, NODE_NONE
|
|
|
|
"00000000000000000000000000000001" // address, fixed 16 bytes (IPv4 embedded in IPv6)
|
|
|
|
"0000" // port
|
|
|
|
|
|
|
|
"79627683" // time, Tue Nov 22 11:22:33 UTC 2039
|
|
|
|
"0100000000000000" // service flags, NODE_NETWORK
|
|
|
|
"00000000000000000000000000000001" // address, fixed 16 bytes (IPv6)
|
|
|
|
"00f1" // port
|
|
|
|
|
|
|
|
"ffffffff" // time, Sun Feb 7 06:28:15 UTC 2106
|
|
|
|
"4804000000000000" // service flags, NODE_WITNESS | NODE_COMPACT_FILTERS | NODE_NETWORK_LIMITED
|
|
|
|
"00000000000000000000000000000001" // address, fixed 16 bytes (IPv6)
|
|
|
|
"f1f2"; // port
|
|
|
|
|
|
|
|
// fixture_addresses should equal to this when serialized in V2 format.
|
|
|
|
// When this is unserialized from V2 format it should equal to fixture_addresses.
|
|
|
|
static constexpr const char* stream_addrv2_hex =
|
|
|
|
"03" // number of entries
|
|
|
|
|
|
|
|
"61bc6649" // time, Fri Jan 9 02:54:25 UTC 2009
|
|
|
|
"00" // service flags, COMPACTSIZE(NODE_NONE)
|
|
|
|
"02" // network id, IPv6
|
|
|
|
"10" // address length, COMPACTSIZE(16)
|
|
|
|
"00000000000000000000000000000001" // address
|
|
|
|
"0000" // port
|
|
|
|
|
|
|
|
"79627683" // time, Tue Nov 22 11:22:33 UTC 2039
|
|
|
|
"01" // service flags, COMPACTSIZE(NODE_NETWORK)
|
|
|
|
"02" // network id, IPv6
|
|
|
|
"10" // address length, COMPACTSIZE(16)
|
|
|
|
"00000000000000000000000000000001" // address
|
|
|
|
"00f1" // port
|
|
|
|
|
|
|
|
"ffffffff" // time, Sun Feb 7 06:28:15 UTC 2106
|
|
|
|
"fd4804" // service flags, COMPACTSIZE(NODE_WITNESS | NODE_COMPACT_FILTERS | NODE_NETWORK_LIMITED)
|
|
|
|
"02" // network id, IPv6
|
|
|
|
"10" // address length, COMPACTSIZE(16)
|
|
|
|
"00000000000000000000000000000001" // address
|
|
|
|
"f1f2"; // port
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(caddress_serialize_v1)
|
|
|
|
{
|
|
|
|
CDataStream s(SER_NETWORK, PROTOCOL_VERSION);
|
|
|
|
|
|
|
|
s << fixture_addresses;
|
|
|
|
BOOST_CHECK_EQUAL(HexStr(s), stream_addrv1_hex);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(caddress_unserialize_v1)
|
|
|
|
{
|
|
|
|
CDataStream s(ParseHex(stream_addrv1_hex), SER_NETWORK, PROTOCOL_VERSION);
|
|
|
|
std::vector<CAddress> addresses_unserialized;
|
|
|
|
|
|
|
|
s >> addresses_unserialized;
|
|
|
|
BOOST_CHECK(fixture_addresses == addresses_unserialized);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(caddress_serialize_v2)
|
|
|
|
{
|
|
|
|
CDataStream s(SER_NETWORK, PROTOCOL_VERSION | ADDRV2_FORMAT);
|
|
|
|
|
|
|
|
s << fixture_addresses;
|
|
|
|
BOOST_CHECK_EQUAL(HexStr(s), stream_addrv2_hex);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(caddress_unserialize_v2)
|
|
|
|
{
|
|
|
|
CDataStream s(ParseHex(stream_addrv2_hex), SER_NETWORK, PROTOCOL_VERSION | ADDRV2_FORMAT);
|
|
|
|
std::vector<CAddress> addresses_unserialized;
|
|
|
|
|
|
|
|
s >> addresses_unserialized;
|
|
|
|
BOOST_CHECK(fixture_addresses == addresses_unserialized);
|
|
|
|
}
|
|
|
|
|
2012-06-08 18:42:05 +02:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|