2020-04-16 13:14:08 -04:00
|
|
|
// Copyright (c) 2012-2020 The Bitcoin Core developers
|
2015-09-22 16:24:16 -03:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2017-11-09 21:57:53 -03:00
|
|
|
#include <addrman.h>
|
2015-09-22 16:24:16 -03:00
|
|
|
#include <boost/test/unit_test.hpp>
|
2020-04-16 13:11:54 -04:00
|
|
|
#include <string>
|
|
|
|
#include <test/data/asmap.raw.h>
|
|
|
|
#include <test/util/setup_common.h>
|
2019-12-24 15:18:44 -03:00
|
|
|
#include <util/asmap.h>
|
2020-02-13 01:01:45 -03:00
|
|
|
#include <util/string.h>
|
2015-09-22 16:24:16 -03:00
|
|
|
|
2017-11-09 21:57:53 -03:00
|
|
|
#include <hash.h>
|
|
|
|
#include <netbase.h>
|
|
|
|
#include <random.h>
|
2015-09-22 16:24:16 -03:00
|
|
|
|
2015-12-13 00:34:08 -03:00
|
|
|
class CAddrManTest : public CAddrMan
|
|
|
|
{
|
2019-12-24 15:18:44 -03:00
|
|
|
private:
|
|
|
|
bool deterministic;
|
2015-12-13 00:34:08 -03:00
|
|
|
public:
|
2019-12-24 15:18:44 -03:00
|
|
|
explicit CAddrManTest(bool makeDeterministic = true,
|
|
|
|
std::vector<bool> asmap = std::vector<bool>())
|
2015-12-13 00:34:08 -03:00
|
|
|
{
|
2017-07-13 07:39:27 -04:00
|
|
|
if (makeDeterministic) {
|
|
|
|
// Set addrman addr placement to be deterministic.
|
|
|
|
MakeDeterministic();
|
|
|
|
}
|
2019-12-24 15:18:44 -03:00
|
|
|
deterministic = makeDeterministic;
|
|
|
|
m_asmap = asmap;
|
2015-12-13 00:34:08 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
//! Ensure that bucket placement is always the same for testing purposes.
|
|
|
|
void MakeDeterministic()
|
|
|
|
{
|
|
|
|
nKey.SetNull();
|
2016-10-13 11:19:20 -03:00
|
|
|
insecure_rand = FastRandomContext(true);
|
2015-12-13 00:34:08 -03:00
|
|
|
}
|
|
|
|
|
2017-08-07 01:36:37 -04:00
|
|
|
CAddrInfo* Find(const CNetAddr& addr, int* pnId = nullptr)
|
2015-12-13 00:34:08 -03:00
|
|
|
{
|
2018-10-08 10:50:09 -03:00
|
|
|
LOCK(cs);
|
2015-12-13 00:34:08 -03:00
|
|
|
return CAddrMan::Find(addr, pnId);
|
|
|
|
}
|
|
|
|
|
2017-08-07 01:36:37 -04:00
|
|
|
CAddrInfo* Create(const CAddress& addr, const CNetAddr& addrSource, int* pnId = nullptr)
|
2015-12-13 00:34:08 -03:00
|
|
|
{
|
2018-10-08 10:50:09 -03:00
|
|
|
LOCK(cs);
|
2015-12-13 00:34:08 -03:00
|
|
|
return CAddrMan::Create(addr, addrSource, pnId);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Delete(int nId)
|
|
|
|
{
|
2018-10-08 10:50:09 -03:00
|
|
|
LOCK(cs);
|
2015-12-13 00:34:08 -03:00
|
|
|
CAddrMan::Delete(nId);
|
|
|
|
}
|
2016-10-27 14:55:39 -03:00
|
|
|
|
2019-12-24 15:18:44 -03:00
|
|
|
// Used to test deserialization
|
|
|
|
std::pair<int, int> GetBucketAndEntry(const CAddress& addr)
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
int nId = mapAddr[addr];
|
|
|
|
for (int bucket = 0; bucket < ADDRMAN_NEW_BUCKET_COUNT; ++bucket) {
|
|
|
|
for (int entry = 0; entry < ADDRMAN_BUCKET_SIZE; ++entry) {
|
|
|
|
if (nId == vvNew[bucket][entry]) {
|
|
|
|
return std::pair<int, int>(bucket, entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return std::pair<int, int>(-1, -1);
|
|
|
|
}
|
|
|
|
|
2016-10-27 14:55:39 -03:00
|
|
|
// Simulates connection failure so that we can test eviction of offline nodes
|
|
|
|
void SimConnFail(CService& addr)
|
|
|
|
{
|
2018-10-08 10:50:09 -03:00
|
|
|
LOCK(cs);
|
2016-10-27 14:55:39 -03:00
|
|
|
int64_t nLastSuccess = 1;
|
|
|
|
Good_(addr, true, nLastSuccess); // Set last good connection in the deep past.
|
|
|
|
|
|
|
|
bool count_failure = false;
|
|
|
|
int64_t nLastTry = GetAdjustedTime()-61;
|
|
|
|
Attempt(addr, count_failure, nLastTry);
|
|
|
|
}
|
2019-12-24 15:18:44 -03:00
|
|
|
|
|
|
|
void Clear()
|
|
|
|
{
|
|
|
|
CAddrMan::Clear();
|
|
|
|
if (deterministic) {
|
|
|
|
nKey.SetNull();
|
|
|
|
insecure_rand = FastRandomContext(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-13 00:34:08 -03:00
|
|
|
};
|
2015-09-22 16:24:16 -03:00
|
|
|
|
2019-12-29 17:23:48 -03:00
|
|
|
static CNetAddr ResolveIP(const std::string& ip)
|
2016-05-31 13:05:52 -04:00
|
|
|
{
|
|
|
|
CNetAddr addr;
|
|
|
|
BOOST_CHECK_MESSAGE(LookupHost(ip, addr, false), strprintf("failed to resolve: %s", ip));
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
2019-12-29 17:23:48 -03:00
|
|
|
static CService ResolveService(const std::string& ip, const int port = 0)
|
2016-05-31 13:51:11 -04:00
|
|
|
{
|
|
|
|
CService serv;
|
|
|
|
BOOST_CHECK_MESSAGE(Lookup(ip, serv, port, false), strprintf("failed to resolve: %s:%i", ip, port));
|
|
|
|
return serv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-24 15:18:44 -03:00
|
|
|
static std::vector<bool> FromBytes(const unsigned char* source, int vector_size) {
|
|
|
|
std::vector<bool> result(vector_size);
|
|
|
|
for (int byte_i = 0; byte_i < vector_size / 8; ++byte_i) {
|
|
|
|
unsigned char cur_byte = source[byte_i];
|
|
|
|
for (int bit_i = 0; bit_i < 8; ++bit_i) {
|
|
|
|
result[byte_i * 8 + bit_i] = (cur_byte >> bit_i) & 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-22 16:24:16 -03:00
|
|
|
BOOST_FIXTURE_TEST_SUITE(addrman_tests, BasicTestingSetup)
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(addrman_simple)
|
|
|
|
{
|
|
|
|
CAddrManTest addrman;
|
|
|
|
|
2016-05-31 13:05:52 -04:00
|
|
|
CNetAddr source = ResolveIP("252.2.2.2");
|
2015-09-22 16:24:16 -03:00
|
|
|
|
2017-05-09 13:11:12 -03:00
|
|
|
// Test: Does Addrman respond correctly when empty.
|
2018-04-09 04:50:19 -03:00
|
|
|
BOOST_CHECK_EQUAL(addrman.size(), 0U);
|
2015-09-22 16:24:16 -03:00
|
|
|
CAddrInfo addr_null = addrman.Select();
|
2017-05-09 13:11:12 -03:00
|
|
|
BOOST_CHECK_EQUAL(addr_null.ToString(), "[::]:0");
|
2015-09-22 16:24:16 -03:00
|
|
|
|
2017-05-09 13:11:12 -03:00
|
|
|
// Test: Does Addrman::Add work as expected.
|
2016-05-31 13:51:11 -04:00
|
|
|
CService addr1 = ResolveService("250.1.1.1", 8333);
|
2017-04-26 22:15:07 -03:00
|
|
|
BOOST_CHECK(addrman.Add(CAddress(addr1, NODE_NONE), source));
|
2018-04-09 04:50:19 -03:00
|
|
|
BOOST_CHECK_EQUAL(addrman.size(), 1U);
|
2015-09-22 16:24:16 -03:00
|
|
|
CAddrInfo addr_ret1 = addrman.Select();
|
2017-05-09 13:11:12 -03:00
|
|
|
BOOST_CHECK_EQUAL(addr_ret1.ToString(), "250.1.1.1:8333");
|
2015-09-22 16:24:16 -03:00
|
|
|
|
2017-05-09 13:11:12 -03:00
|
|
|
// Test: Does IP address deduplication work correctly.
|
2015-09-22 16:24:16 -03:00
|
|
|
// Expected dup IP should not be added.
|
2016-05-31 13:51:11 -04:00
|
|
|
CService addr1_dup = ResolveService("250.1.1.1", 8333);
|
2017-04-26 22:15:07 -03:00
|
|
|
BOOST_CHECK(!addrman.Add(CAddress(addr1_dup, NODE_NONE), source));
|
2018-04-09 04:50:19 -03:00
|
|
|
BOOST_CHECK_EQUAL(addrman.size(), 1U);
|
2015-09-22 16:24:16 -03:00
|
|
|
|
|
|
|
|
2017-05-09 13:11:12 -03:00
|
|
|
// Test: New table has one addr and we add a diff addr we should
|
2017-05-19 00:00:39 -04:00
|
|
|
// have at least one addr.
|
|
|
|
// Note that addrman's size cannot be tested reliably after insertion, as
|
|
|
|
// hash collisions may occur. But we can always be sure of at least one
|
|
|
|
// success.
|
|
|
|
|
2016-05-31 13:51:11 -04:00
|
|
|
CService addr2 = ResolveService("250.1.1.2", 8333);
|
2017-04-26 22:15:07 -03:00
|
|
|
BOOST_CHECK(addrman.Add(CAddress(addr2, NODE_NONE), source));
|
2017-05-19 00:00:39 -04:00
|
|
|
BOOST_CHECK(addrman.size() >= 1);
|
2015-09-22 16:24:16 -03:00
|
|
|
|
2017-05-09 13:11:12 -03:00
|
|
|
// Test: AddrMan::Clear() should empty the new table.
|
2015-09-22 16:24:16 -03:00
|
|
|
addrman.Clear();
|
2018-04-09 04:50:19 -03:00
|
|
|
BOOST_CHECK_EQUAL(addrman.size(), 0U);
|
2015-09-22 16:24:16 -03:00
|
|
|
CAddrInfo addr_null2 = addrman.Select();
|
2017-05-09 13:11:12 -03:00
|
|
|
BOOST_CHECK_EQUAL(addr_null2.ToString(), "[::]:0");
|
2017-04-26 22:15:07 -03:00
|
|
|
|
2017-05-09 13:11:12 -03:00
|
|
|
// Test: AddrMan::Add multiple addresses works as expected
|
2017-04-26 22:15:07 -03:00
|
|
|
std::vector<CAddress> vAddr;
|
|
|
|
vAddr.push_back(CAddress(ResolveService("250.1.1.3", 8333), NODE_NONE));
|
|
|
|
vAddr.push_back(CAddress(ResolveService("250.1.1.4", 8333), NODE_NONE));
|
|
|
|
BOOST_CHECK(addrman.Add(vAddr, source));
|
2017-05-19 00:00:39 -04:00
|
|
|
BOOST_CHECK(addrman.size() >= 1);
|
2015-09-22 16:24:16 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(addrman_ports)
|
|
|
|
{
|
|
|
|
CAddrManTest addrman;
|
|
|
|
|
2016-05-31 13:05:52 -04:00
|
|
|
CNetAddr source = ResolveIP("252.2.2.2");
|
2015-09-22 16:24:16 -03:00
|
|
|
|
2018-04-09 04:50:19 -03:00
|
|
|
BOOST_CHECK_EQUAL(addrman.size(), 0U);
|
2015-09-22 16:24:16 -03:00
|
|
|
|
|
|
|
// Test 7; Addr with same IP but diff port does not replace existing addr.
|
2016-05-31 13:51:11 -04:00
|
|
|
CService addr1 = ResolveService("250.1.1.1", 8333);
|
2018-12-12 10:31:38 -03:00
|
|
|
BOOST_CHECK(addrman.Add(CAddress(addr1, NODE_NONE), source));
|
2018-04-09 04:50:19 -03:00
|
|
|
BOOST_CHECK_EQUAL(addrman.size(), 1U);
|
2015-09-22 16:24:16 -03:00
|
|
|
|
2016-05-31 13:51:11 -04:00
|
|
|
CService addr1_port = ResolveService("250.1.1.1", 8334);
|
2018-12-12 10:31:38 -03:00
|
|
|
BOOST_CHECK(!addrman.Add(CAddress(addr1_port, NODE_NONE), source));
|
2018-04-09 04:50:19 -03:00
|
|
|
BOOST_CHECK_EQUAL(addrman.size(), 1U);
|
2015-09-22 16:24:16 -03:00
|
|
|
CAddrInfo addr_ret2 = addrman.Select();
|
2017-05-09 13:11:12 -03:00
|
|
|
BOOST_CHECK_EQUAL(addr_ret2.ToString(), "250.1.1.1:8333");
|
2015-09-22 16:24:16 -03:00
|
|
|
|
2017-05-09 13:11:12 -03:00
|
|
|
// Test: Add same IP but diff port to tried table, it doesn't get added.
|
2015-09-22 16:24:16 -03:00
|
|
|
// Perhaps this is not ideal behavior but it is the current behavior.
|
2016-06-08 13:12:22 -04:00
|
|
|
addrman.Good(CAddress(addr1_port, NODE_NONE));
|
2018-04-09 04:50:19 -03:00
|
|
|
BOOST_CHECK_EQUAL(addrman.size(), 1U);
|
2015-09-22 16:24:16 -03:00
|
|
|
bool newOnly = true;
|
|
|
|
CAddrInfo addr_ret3 = addrman.Select(newOnly);
|
2017-05-09 13:11:12 -03:00
|
|
|
BOOST_CHECK_EQUAL(addr_ret3.ToString(), "250.1.1.1:8333");
|
2015-09-22 16:24:16 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(addrman_select)
|
|
|
|
{
|
|
|
|
CAddrManTest addrman;
|
|
|
|
|
2016-05-31 13:05:52 -04:00
|
|
|
CNetAddr source = ResolveIP("252.2.2.2");
|
2015-09-22 16:24:16 -03:00
|
|
|
|
2017-05-09 13:11:12 -03:00
|
|
|
// Test: Select from new with 1 addr in new.
|
2016-05-31 13:51:11 -04:00
|
|
|
CService addr1 = ResolveService("250.1.1.1", 8333);
|
2018-12-12 10:31:38 -03:00
|
|
|
BOOST_CHECK(addrman.Add(CAddress(addr1, NODE_NONE), source));
|
2018-04-09 04:50:19 -03:00
|
|
|
BOOST_CHECK_EQUAL(addrman.size(), 1U);
|
2015-09-22 16:24:16 -03:00
|
|
|
|
|
|
|
bool newOnly = true;
|
|
|
|
CAddrInfo addr_ret1 = addrman.Select(newOnly);
|
2017-05-09 13:11:12 -03:00
|
|
|
BOOST_CHECK_EQUAL(addr_ret1.ToString(), "250.1.1.1:8333");
|
2015-09-22 16:24:16 -03:00
|
|
|
|
2017-05-09 13:11:12 -03:00
|
|
|
// Test: move addr to tried, select from new expected nothing returned.
|
2016-06-08 13:12:22 -04:00
|
|
|
addrman.Good(CAddress(addr1, NODE_NONE));
|
2018-04-09 04:50:19 -03:00
|
|
|
BOOST_CHECK_EQUAL(addrman.size(), 1U);
|
2015-09-22 16:24:16 -03:00
|
|
|
CAddrInfo addr_ret2 = addrman.Select(newOnly);
|
2017-05-09 13:11:12 -03:00
|
|
|
BOOST_CHECK_EQUAL(addr_ret2.ToString(), "[::]:0");
|
2015-09-22 16:24:16 -03:00
|
|
|
|
|
|
|
CAddrInfo addr_ret3 = addrman.Select();
|
2017-05-09 13:11:12 -03:00
|
|
|
BOOST_CHECK_EQUAL(addr_ret3.ToString(), "250.1.1.1:8333");
|
2015-12-13 00:34:08 -03:00
|
|
|
|
2018-04-09 04:50:19 -03:00
|
|
|
BOOST_CHECK_EQUAL(addrman.size(), 1U);
|
2015-12-13 00:34:08 -03:00
|
|
|
|
|
|
|
|
|
|
|
// Add three addresses to new table.
|
2016-05-31 13:51:11 -04:00
|
|
|
CService addr2 = ResolveService("250.3.1.1", 8333);
|
|
|
|
CService addr3 = ResolveService("250.3.2.2", 9999);
|
|
|
|
CService addr4 = ResolveService("250.3.3.3", 9999);
|
2015-12-13 00:34:08 -03:00
|
|
|
|
2018-12-12 10:31:38 -03:00
|
|
|
BOOST_CHECK(addrman.Add(CAddress(addr2, NODE_NONE), ResolveService("250.3.1.1", 8333)));
|
|
|
|
BOOST_CHECK(addrman.Add(CAddress(addr3, NODE_NONE), ResolveService("250.3.1.1", 8333)));
|
|
|
|
BOOST_CHECK(addrman.Add(CAddress(addr4, NODE_NONE), ResolveService("250.4.1.1", 8333)));
|
2015-12-13 00:34:08 -03:00
|
|
|
|
|
|
|
// Add three addresses to tried table.
|
2016-05-31 13:51:11 -04:00
|
|
|
CService addr5 = ResolveService("250.4.4.4", 8333);
|
|
|
|
CService addr6 = ResolveService("250.4.5.5", 7777);
|
|
|
|
CService addr7 = ResolveService("250.4.6.6", 8333);
|
2015-12-13 00:34:08 -03:00
|
|
|
|
2018-12-12 10:31:38 -03:00
|
|
|
BOOST_CHECK(addrman.Add(CAddress(addr5, NODE_NONE), ResolveService("250.3.1.1", 8333)));
|
2016-06-08 13:12:22 -04:00
|
|
|
addrman.Good(CAddress(addr5, NODE_NONE));
|
2018-12-12 10:31:38 -03:00
|
|
|
BOOST_CHECK(addrman.Add(CAddress(addr6, NODE_NONE), ResolveService("250.3.1.1", 8333)));
|
2016-06-08 13:12:22 -04:00
|
|
|
addrman.Good(CAddress(addr6, NODE_NONE));
|
2018-12-12 10:31:38 -03:00
|
|
|
BOOST_CHECK(addrman.Add(CAddress(addr7, NODE_NONE), ResolveService("250.1.1.3", 8333)));
|
2016-06-08 13:12:22 -04:00
|
|
|
addrman.Good(CAddress(addr7, NODE_NONE));
|
2015-12-13 00:34:08 -03:00
|
|
|
|
2017-05-09 13:11:12 -03:00
|
|
|
// Test: 6 addrs + 1 addr from last test = 7.
|
2018-04-09 04:50:19 -03:00
|
|
|
BOOST_CHECK_EQUAL(addrman.size(), 7U);
|
2015-12-13 00:34:08 -03:00
|
|
|
|
2017-05-09 13:11:12 -03:00
|
|
|
// Test: Select pulls from new and tried regardless of port number.
|
2017-02-15 22:45:22 -03:00
|
|
|
std::set<uint16_t> ports;
|
|
|
|
for (int i = 0; i < 20; ++i) {
|
|
|
|
ports.insert(addrman.Select().GetPort());
|
|
|
|
}
|
2018-04-09 04:50:19 -03:00
|
|
|
BOOST_CHECK_EQUAL(ports.size(), 3U);
|
2015-09-22 16:24:16 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(addrman_new_collisions)
|
|
|
|
{
|
|
|
|
CAddrManTest addrman;
|
|
|
|
|
2016-05-31 13:05:52 -04:00
|
|
|
CNetAddr source = ResolveIP("252.2.2.2");
|
2015-09-22 16:24:16 -03:00
|
|
|
|
2018-04-09 04:50:19 -03:00
|
|
|
BOOST_CHECK_EQUAL(addrman.size(), 0U);
|
2015-09-22 16:24:16 -03:00
|
|
|
|
2015-12-13 00:34:08 -03:00
|
|
|
for (unsigned int i = 1; i < 18; i++) {
|
2020-02-13 01:01:45 -03:00
|
|
|
CService addr = ResolveService("250.1.1." + ToString(i));
|
2018-12-12 10:31:38 -03:00
|
|
|
BOOST_CHECK(addrman.Add(CAddress(addr, NODE_NONE), source));
|
2015-09-22 16:24:16 -03:00
|
|
|
|
2017-05-09 13:11:12 -03:00
|
|
|
//Test: No collision in new table yet.
|
|
|
|
BOOST_CHECK_EQUAL(addrman.size(), i);
|
2015-09-22 16:24:16 -03:00
|
|
|
}
|
|
|
|
|
2017-05-09 13:11:12 -03:00
|
|
|
//Test: new table collision!
|
2016-05-31 13:51:11 -04:00
|
|
|
CService addr1 = ResolveService("250.1.1.18");
|
2018-12-12 10:31:38 -03:00
|
|
|
BOOST_CHECK(addrman.Add(CAddress(addr1, NODE_NONE), source));
|
2018-04-09 04:50:19 -03:00
|
|
|
BOOST_CHECK_EQUAL(addrman.size(), 17U);
|
2015-09-22 16:24:16 -03:00
|
|
|
|
2016-05-31 13:51:11 -04:00
|
|
|
CService addr2 = ResolveService("250.1.1.19");
|
2018-12-12 10:31:38 -03:00
|
|
|
BOOST_CHECK(addrman.Add(CAddress(addr2, NODE_NONE), source));
|
2018-04-09 04:50:19 -03:00
|
|
|
BOOST_CHECK_EQUAL(addrman.size(), 18U);
|
2015-09-22 16:24:16 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(addrman_tried_collisions)
|
|
|
|
{
|
|
|
|
CAddrManTest addrman;
|
|
|
|
|
2016-05-31 13:05:52 -04:00
|
|
|
CNetAddr source = ResolveIP("252.2.2.2");
|
2015-09-22 16:24:16 -03:00
|
|
|
|
2018-04-09 04:50:19 -03:00
|
|
|
BOOST_CHECK_EQUAL(addrman.size(), 0U);
|
2015-09-22 16:24:16 -03:00
|
|
|
|
2015-12-13 00:34:08 -03:00
|
|
|
for (unsigned int i = 1; i < 80; i++) {
|
2020-02-13 01:01:45 -03:00
|
|
|
CService addr = ResolveService("250.1.1." + ToString(i));
|
2018-12-12 10:31:38 -03:00
|
|
|
BOOST_CHECK(addrman.Add(CAddress(addr, NODE_NONE), source));
|
2016-06-08 13:12:22 -04:00
|
|
|
addrman.Good(CAddress(addr, NODE_NONE));
|
2015-09-22 16:24:16 -03:00
|
|
|
|
2017-05-09 13:11:12 -03:00
|
|
|
//Test: No collision in tried table yet.
|
2016-09-22 07:51:50 -03:00
|
|
|
BOOST_CHECK_EQUAL(addrman.size(), i);
|
2015-09-22 16:24:16 -03:00
|
|
|
}
|
|
|
|
|
2017-05-09 13:11:12 -03:00
|
|
|
//Test: tried table collision!
|
2016-05-31 13:51:11 -04:00
|
|
|
CService addr1 = ResolveService("250.1.1.80");
|
2018-12-12 10:31:38 -03:00
|
|
|
BOOST_CHECK(addrman.Add(CAddress(addr1, NODE_NONE), source));
|
2018-04-09 04:50:19 -03:00
|
|
|
BOOST_CHECK_EQUAL(addrman.size(), 79U);
|
2015-09-22 16:24:16 -03:00
|
|
|
|
2016-05-31 13:51:11 -04:00
|
|
|
CService addr2 = ResolveService("250.1.1.81");
|
2018-12-12 10:31:38 -03:00
|
|
|
BOOST_CHECK(addrman.Add(CAddress(addr2, NODE_NONE), source));
|
2018-04-09 04:50:19 -03:00
|
|
|
BOOST_CHECK_EQUAL(addrman.size(), 80U);
|
2015-12-13 00:34:08 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(addrman_find)
|
|
|
|
{
|
|
|
|
CAddrManTest addrman;
|
|
|
|
|
2018-04-09 04:50:19 -03:00
|
|
|
BOOST_CHECK_EQUAL(addrman.size(), 0U);
|
2015-12-13 00:34:08 -03:00
|
|
|
|
2016-05-31 13:51:11 -04:00
|
|
|
CAddress addr1 = CAddress(ResolveService("250.1.2.1", 8333), NODE_NONE);
|
|
|
|
CAddress addr2 = CAddress(ResolveService("250.1.2.1", 9999), NODE_NONE);
|
|
|
|
CAddress addr3 = CAddress(ResolveService("251.255.2.1", 8333), NODE_NONE);
|
2015-12-13 00:34:08 -03:00
|
|
|
|
2016-05-31 13:05:52 -04:00
|
|
|
CNetAddr source1 = ResolveIP("250.1.2.1");
|
|
|
|
CNetAddr source2 = ResolveIP("250.1.2.2");
|
2015-12-13 00:34:08 -03:00
|
|
|
|
2018-12-12 10:31:38 -03:00
|
|
|
BOOST_CHECK(addrman.Add(addr1, source1));
|
|
|
|
BOOST_CHECK(!addrman.Add(addr2, source2));
|
|
|
|
BOOST_CHECK(addrman.Add(addr3, source1));
|
2015-12-13 00:34:08 -03:00
|
|
|
|
2017-05-09 13:11:12 -03:00
|
|
|
// Test: ensure Find returns an IP matching what we searched on.
|
2015-12-13 00:34:08 -03:00
|
|
|
CAddrInfo* info1 = addrman.Find(addr1);
|
2017-05-09 13:11:12 -03:00
|
|
|
BOOST_REQUIRE(info1);
|
|
|
|
BOOST_CHECK_EQUAL(info1->ToString(), "250.1.2.1:8333");
|
2015-12-13 00:34:08 -03:00
|
|
|
|
|
|
|
// Test 18; Find does not discriminate by port number.
|
|
|
|
CAddrInfo* info2 = addrman.Find(addr2);
|
2017-05-09 13:11:12 -03:00
|
|
|
BOOST_REQUIRE(info2);
|
|
|
|
BOOST_CHECK_EQUAL(info2->ToString(), info1->ToString());
|
2015-12-13 00:34:08 -03:00
|
|
|
|
2017-05-09 13:11:12 -03:00
|
|
|
// Test: Find returns another IP matching what we searched on.
|
2015-12-13 00:34:08 -03:00
|
|
|
CAddrInfo* info3 = addrman.Find(addr3);
|
2017-05-09 13:11:12 -03:00
|
|
|
BOOST_REQUIRE(info3);
|
|
|
|
BOOST_CHECK_EQUAL(info3->ToString(), "251.255.2.1:8333");
|
2015-12-13 00:34:08 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(addrman_create)
|
|
|
|
{
|
|
|
|
CAddrManTest addrman;
|
|
|
|
|
2018-04-09 04:50:19 -03:00
|
|
|
BOOST_CHECK_EQUAL(addrman.size(), 0U);
|
2015-12-13 00:34:08 -03:00
|
|
|
|
2016-05-31 13:51:11 -04:00
|
|
|
CAddress addr1 = CAddress(ResolveService("250.1.2.1", 8333), NODE_NONE);
|
2016-05-31 13:05:52 -04:00
|
|
|
CNetAddr source1 = ResolveIP("250.1.2.1");
|
2015-12-13 00:34:08 -03:00
|
|
|
|
|
|
|
int nId;
|
|
|
|
CAddrInfo* pinfo = addrman.Create(addr1, source1, &nId);
|
|
|
|
|
2017-05-09 13:11:12 -03:00
|
|
|
// Test: The result should be the same as the input addr.
|
|
|
|
BOOST_CHECK_EQUAL(pinfo->ToString(), "250.1.2.1:8333");
|
2015-12-13 00:34:08 -03:00
|
|
|
|
|
|
|
CAddrInfo* info2 = addrman.Find(addr1);
|
2017-05-09 13:11:12 -03:00
|
|
|
BOOST_CHECK_EQUAL(info2->ToString(), "250.1.2.1:8333");
|
2015-12-13 00:34:08 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(addrman_delete)
|
|
|
|
{
|
|
|
|
CAddrManTest addrman;
|
|
|
|
|
2018-04-09 04:50:19 -03:00
|
|
|
BOOST_CHECK_EQUAL(addrman.size(), 0U);
|
2015-12-13 00:34:08 -03:00
|
|
|
|
2016-05-31 13:51:11 -04:00
|
|
|
CAddress addr1 = CAddress(ResolveService("250.1.2.1", 8333), NODE_NONE);
|
2016-05-31 13:05:52 -04:00
|
|
|
CNetAddr source1 = ResolveIP("250.1.2.1");
|
2015-12-13 00:34:08 -03:00
|
|
|
|
|
|
|
int nId;
|
|
|
|
addrman.Create(addr1, source1, &nId);
|
|
|
|
|
2017-05-09 13:11:12 -03:00
|
|
|
// Test: Delete should actually delete the addr.
|
2018-04-09 04:50:19 -03:00
|
|
|
BOOST_CHECK_EQUAL(addrman.size(), 1U);
|
2015-12-13 00:34:08 -03:00
|
|
|
addrman.Delete(nId);
|
2018-04-09 04:50:19 -03:00
|
|
|
BOOST_CHECK_EQUAL(addrman.size(), 0U);
|
2015-12-13 00:34:08 -03:00
|
|
|
CAddrInfo* info2 = addrman.Find(addr1);
|
2017-08-07 01:36:37 -04:00
|
|
|
BOOST_CHECK(info2 == nullptr);
|
2015-12-13 00:34:08 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(addrman_getaddr)
|
|
|
|
{
|
|
|
|
CAddrManTest addrman;
|
|
|
|
|
2017-05-09 13:11:12 -03:00
|
|
|
// Test: Sanity check, GetAddr should never return anything if addrman
|
2015-12-13 00:34:08 -03:00
|
|
|
// is empty.
|
2018-04-09 04:50:19 -03:00
|
|
|
BOOST_CHECK_EQUAL(addrman.size(), 0U);
|
2016-12-05 04:03:53 -03:00
|
|
|
std::vector<CAddress> vAddr1 = addrman.GetAddr();
|
2018-04-09 04:50:19 -03:00
|
|
|
BOOST_CHECK_EQUAL(vAddr1.size(), 0U);
|
2015-12-13 00:34:08 -03:00
|
|
|
|
2016-05-31 13:51:11 -04:00
|
|
|
CAddress addr1 = CAddress(ResolveService("250.250.2.1", 8333), NODE_NONE);
|
2015-12-13 00:34:08 -03:00
|
|
|
addr1.nTime = GetAdjustedTime(); // Set time so isTerrible = false
|
2016-05-31 13:51:11 -04:00
|
|
|
CAddress addr2 = CAddress(ResolveService("250.251.2.2", 9999), NODE_NONE);
|
2015-12-13 00:34:08 -03:00
|
|
|
addr2.nTime = GetAdjustedTime();
|
2016-05-31 13:51:11 -04:00
|
|
|
CAddress addr3 = CAddress(ResolveService("251.252.2.3", 8333), NODE_NONE);
|
2015-12-13 00:34:08 -03:00
|
|
|
addr3.nTime = GetAdjustedTime();
|
2016-05-31 13:51:11 -04:00
|
|
|
CAddress addr4 = CAddress(ResolveService("252.253.3.4", 8333), NODE_NONE);
|
2015-12-13 00:34:08 -03:00
|
|
|
addr4.nTime = GetAdjustedTime();
|
2016-05-31 13:51:11 -04:00
|
|
|
CAddress addr5 = CAddress(ResolveService("252.254.4.5", 8333), NODE_NONE);
|
2015-12-13 00:34:08 -03:00
|
|
|
addr5.nTime = GetAdjustedTime();
|
2016-05-31 13:05:52 -04:00
|
|
|
CNetAddr source1 = ResolveIP("250.1.2.1");
|
|
|
|
CNetAddr source2 = ResolveIP("250.2.3.3");
|
2015-12-13 00:34:08 -03:00
|
|
|
|
2017-05-09 13:11:12 -03:00
|
|
|
// Test: Ensure GetAddr works with new addresses.
|
2018-12-12 10:31:38 -03:00
|
|
|
BOOST_CHECK(addrman.Add(addr1, source1));
|
|
|
|
BOOST_CHECK(addrman.Add(addr2, source2));
|
|
|
|
BOOST_CHECK(addrman.Add(addr3, source1));
|
|
|
|
BOOST_CHECK(addrman.Add(addr4, source2));
|
|
|
|
BOOST_CHECK(addrman.Add(addr5, source1));
|
2015-12-13 00:34:08 -03:00
|
|
|
|
|
|
|
// GetAddr returns 23% of addresses, 23% of 5 is 1 rounded down.
|
2018-04-09 04:50:19 -03:00
|
|
|
BOOST_CHECK_EQUAL(addrman.GetAddr().size(), 1U);
|
2015-12-13 00:34:08 -03:00
|
|
|
|
2017-05-09 13:11:12 -03:00
|
|
|
// Test: Ensure GetAddr works with new and tried addresses.
|
2016-06-08 13:12:22 -04:00
|
|
|
addrman.Good(CAddress(addr1, NODE_NONE));
|
|
|
|
addrman.Good(CAddress(addr2, NODE_NONE));
|
2018-04-09 04:50:19 -03:00
|
|
|
BOOST_CHECK_EQUAL(addrman.GetAddr().size(), 1U);
|
2015-12-13 00:34:08 -03:00
|
|
|
|
2017-05-09 13:11:12 -03:00
|
|
|
// Test: Ensure GetAddr still returns 23% when addrman has many addrs.
|
2015-12-13 00:34:08 -03:00
|
|
|
for (unsigned int i = 1; i < (8 * 256); i++) {
|
|
|
|
int octet1 = i % 256;
|
2017-04-26 22:15:07 -03:00
|
|
|
int octet2 = i >> 8 % 256;
|
2020-02-13 01:01:45 -03:00
|
|
|
std::string strAddr = ToString(octet1) + "." + ToString(octet2) + ".1.23";
|
2016-05-31 13:51:11 -04:00
|
|
|
CAddress addr = CAddress(ResolveService(strAddr), NODE_NONE);
|
2017-05-09 13:11:12 -03:00
|
|
|
|
2015-12-13 00:34:08 -03:00
|
|
|
// Ensure that for all addrs in addrman, isTerrible == false.
|
|
|
|
addr.nTime = GetAdjustedTime();
|
2016-05-31 13:05:52 -04:00
|
|
|
addrman.Add(addr, ResolveIP(strAddr));
|
2015-12-13 00:34:08 -03:00
|
|
|
if (i % 8 == 0)
|
|
|
|
addrman.Good(addr);
|
|
|
|
}
|
2016-12-05 04:03:53 -03:00
|
|
|
std::vector<CAddress> vAddr = addrman.GetAddr();
|
2015-12-13 00:34:08 -03:00
|
|
|
|
|
|
|
size_t percent23 = (addrman.size() * 23) / 100;
|
2017-04-26 22:15:07 -03:00
|
|
|
BOOST_CHECK_EQUAL(vAddr.size(), percent23);
|
2018-04-09 04:50:19 -03:00
|
|
|
BOOST_CHECK_EQUAL(vAddr.size(), 461U);
|
2017-03-21 15:49:08 -03:00
|
|
|
// (Addrman.size() < number of addresses added) due to address collisions.
|
2018-04-09 04:50:19 -03:00
|
|
|
BOOST_CHECK_EQUAL(addrman.size(), 2006U);
|
2015-09-22 16:24:16 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-24 15:18:44 -03:00
|
|
|
BOOST_AUTO_TEST_CASE(caddrinfo_get_tried_bucket_legacy)
|
2015-12-13 00:34:08 -03:00
|
|
|
{
|
|
|
|
CAddrManTest addrman;
|
|
|
|
|
2016-05-31 13:51:11 -04:00
|
|
|
CAddress addr1 = CAddress(ResolveService("250.1.1.1", 8333), NODE_NONE);
|
|
|
|
CAddress addr2 = CAddress(ResolveService("250.1.1.1", 9999), NODE_NONE);
|
2015-12-13 00:34:08 -03:00
|
|
|
|
2016-05-31 13:05:52 -04:00
|
|
|
CNetAddr source1 = ResolveIP("250.1.1.1");
|
2015-12-13 00:34:08 -03:00
|
|
|
|
|
|
|
|
|
|
|
CAddrInfo info1 = CAddrInfo(addr1, source1);
|
|
|
|
|
|
|
|
uint256 nKey1 = (uint256)(CHashWriter(SER_GETHASH, 0) << 1).GetHash();
|
|
|
|
uint256 nKey2 = (uint256)(CHashWriter(SER_GETHASH, 0) << 2).GetHash();
|
|
|
|
|
2019-12-24 15:18:44 -03:00
|
|
|
std::vector<bool> asmap; // use /16
|
2015-12-13 00:34:08 -03:00
|
|
|
|
2019-12-24 15:18:44 -03:00
|
|
|
BOOST_CHECK_EQUAL(info1.GetTriedBucket(nKey1, asmap), 40);
|
2015-12-13 00:34:08 -03:00
|
|
|
|
2017-05-09 13:11:12 -03:00
|
|
|
// Test: Make sure key actually randomizes bucket placement. A fail on
|
2015-12-13 00:34:08 -03:00
|
|
|
// this test could be a security issue.
|
2019-12-24 15:18:44 -03:00
|
|
|
BOOST_CHECK(info1.GetTriedBucket(nKey1, asmap) != info1.GetTriedBucket(nKey2, asmap));
|
2015-12-13 00:34:08 -03:00
|
|
|
|
2017-05-09 13:11:12 -03:00
|
|
|
// Test: Two addresses with same IP but different ports can map to
|
2015-12-13 00:34:08 -03:00
|
|
|
// different buckets because they have different keys.
|
|
|
|
CAddrInfo info2 = CAddrInfo(addr2, source1);
|
|
|
|
|
|
|
|
BOOST_CHECK(info1.GetKey() != info2.GetKey());
|
2019-12-24 15:18:44 -03:00
|
|
|
BOOST_CHECK(info1.GetTriedBucket(nKey1, asmap) != info2.GetTriedBucket(nKey1, asmap));
|
2015-12-13 00:34:08 -03:00
|
|
|
|
2016-12-05 04:03:53 -03:00
|
|
|
std::set<int> buckets;
|
2015-12-13 00:34:08 -03:00
|
|
|
for (int i = 0; i < 255; i++) {
|
|
|
|
CAddrInfo infoi = CAddrInfo(
|
2020-02-13 01:01:45 -03:00
|
|
|
CAddress(ResolveService("250.1.1." + ToString(i)), NODE_NONE),
|
|
|
|
ResolveIP("250.1.1." + ToString(i)));
|
2019-12-24 15:18:44 -03:00
|
|
|
int bucket = infoi.GetTriedBucket(nKey1, asmap);
|
2015-12-13 00:34:08 -03:00
|
|
|
buckets.insert(bucket);
|
|
|
|
}
|
2019-12-24 15:18:44 -03:00
|
|
|
// Test: IP addresses in the same /16 prefix should
|
|
|
|
// never get more than 8 buckets with legacy grouping
|
2018-04-09 04:50:19 -03:00
|
|
|
BOOST_CHECK_EQUAL(buckets.size(), 8U);
|
2015-12-13 00:34:08 -03:00
|
|
|
|
|
|
|
buckets.clear();
|
|
|
|
for (int j = 0; j < 255; j++) {
|
|
|
|
CAddrInfo infoj = CAddrInfo(
|
2020-02-13 01:01:45 -03:00
|
|
|
CAddress(ResolveService("250." + ToString(j) + ".1.1"), NODE_NONE),
|
|
|
|
ResolveIP("250." + ToString(j) + ".1.1"));
|
2019-12-24 15:18:44 -03:00
|
|
|
int bucket = infoj.GetTriedBucket(nKey1, asmap);
|
2015-12-13 00:34:08 -03:00
|
|
|
buckets.insert(bucket);
|
|
|
|
}
|
2019-12-24 15:18:44 -03:00
|
|
|
// Test: IP addresses in the different /16 prefix should map to more than
|
|
|
|
// 8 buckets with legacy grouping
|
2018-04-09 04:50:19 -03:00
|
|
|
BOOST_CHECK_EQUAL(buckets.size(), 160U);
|
2015-12-13 00:34:08 -03:00
|
|
|
}
|
|
|
|
|
2019-12-24 15:18:44 -03:00
|
|
|
BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket_legacy)
|
2015-12-13 00:34:08 -03:00
|
|
|
{
|
|
|
|
CAddrManTest addrman;
|
|
|
|
|
2016-05-31 13:51:11 -04:00
|
|
|
CAddress addr1 = CAddress(ResolveService("250.1.2.1", 8333), NODE_NONE);
|
|
|
|
CAddress addr2 = CAddress(ResolveService("250.1.2.1", 9999), NODE_NONE);
|
2015-12-13 00:34:08 -03:00
|
|
|
|
2016-05-31 13:05:52 -04:00
|
|
|
CNetAddr source1 = ResolveIP("250.1.2.1");
|
2015-12-13 00:34:08 -03:00
|
|
|
|
|
|
|
CAddrInfo info1 = CAddrInfo(addr1, source1);
|
|
|
|
|
|
|
|
uint256 nKey1 = (uint256)(CHashWriter(SER_GETHASH, 0) << 1).GetHash();
|
|
|
|
uint256 nKey2 = (uint256)(CHashWriter(SER_GETHASH, 0) << 2).GetHash();
|
|
|
|
|
2019-12-24 15:18:44 -03:00
|
|
|
std::vector<bool> asmap; // use /16
|
|
|
|
|
2017-05-09 13:11:12 -03:00
|
|
|
// Test: Make sure the buckets are what we expect
|
2019-12-24 15:18:44 -03:00
|
|
|
BOOST_CHECK_EQUAL(info1.GetNewBucket(nKey1, asmap), 786);
|
|
|
|
BOOST_CHECK_EQUAL(info1.GetNewBucket(nKey1, source1, asmap), 786);
|
2015-12-13 00:34:08 -03:00
|
|
|
|
2017-05-09 13:11:12 -03:00
|
|
|
// Test: Make sure key actually randomizes bucket placement. A fail on
|
2015-12-13 00:34:08 -03:00
|
|
|
// this test could be a security issue.
|
2019-12-24 15:18:44 -03:00
|
|
|
BOOST_CHECK(info1.GetNewBucket(nKey1, asmap) != info1.GetNewBucket(nKey2, asmap));
|
2015-12-13 00:34:08 -03:00
|
|
|
|
2018-03-18 11:26:45 -03:00
|
|
|
// Test: Ports should not affect bucket placement in the addr
|
2015-12-13 00:34:08 -03:00
|
|
|
CAddrInfo info2 = CAddrInfo(addr2, source1);
|
|
|
|
BOOST_CHECK(info1.GetKey() != info2.GetKey());
|
2019-12-24 15:18:44 -03:00
|
|
|
BOOST_CHECK_EQUAL(info1.GetNewBucket(nKey1, asmap), info2.GetNewBucket(nKey1, asmap));
|
2015-12-13 00:34:08 -03:00
|
|
|
|
2016-12-05 04:03:53 -03:00
|
|
|
std::set<int> buckets;
|
2015-12-13 00:34:08 -03:00
|
|
|
for (int i = 0; i < 255; i++) {
|
|
|
|
CAddrInfo infoi = CAddrInfo(
|
2020-02-13 01:01:45 -03:00
|
|
|
CAddress(ResolveService("250.1.1." + ToString(i)), NODE_NONE),
|
|
|
|
ResolveIP("250.1.1." + ToString(i)));
|
2019-12-24 15:18:44 -03:00
|
|
|
int bucket = infoi.GetNewBucket(nKey1, asmap);
|
2015-12-13 00:34:08 -03:00
|
|
|
buckets.insert(bucket);
|
|
|
|
}
|
2017-05-09 13:11:12 -03:00
|
|
|
// Test: IP addresses in the same group (\16 prefix for IPv4) should
|
2015-12-13 00:34:08 -03:00
|
|
|
// always map to the same bucket.
|
2018-04-09 04:50:19 -03:00
|
|
|
BOOST_CHECK_EQUAL(buckets.size(), 1U);
|
2015-12-13 00:34:08 -03:00
|
|
|
|
|
|
|
buckets.clear();
|
|
|
|
for (int j = 0; j < 4 * 255; j++) {
|
|
|
|
CAddrInfo infoj = CAddrInfo(CAddress(
|
2016-05-31 13:51:11 -04:00
|
|
|
ResolveService(
|
2020-02-13 01:01:45 -03:00
|
|
|
ToString(250 + (j / 255)) + "." + ToString(j % 256) + ".1.1"), NODE_NONE),
|
2016-05-31 13:05:52 -04:00
|
|
|
ResolveIP("251.4.1.1"));
|
2019-12-24 15:18:44 -03:00
|
|
|
int bucket = infoj.GetNewBucket(nKey1, asmap);
|
2015-12-13 00:34:08 -03:00
|
|
|
buckets.insert(bucket);
|
|
|
|
}
|
2019-12-24 15:18:44 -03:00
|
|
|
// Test: IP addresses in the same source groups should map to NO MORE
|
2015-12-13 00:34:08 -03:00
|
|
|
// than 64 buckets.
|
|
|
|
BOOST_CHECK(buckets.size() <= 64);
|
|
|
|
|
|
|
|
buckets.clear();
|
|
|
|
for (int p = 0; p < 255; p++) {
|
|
|
|
CAddrInfo infoj = CAddrInfo(
|
2016-05-31 13:51:11 -04:00
|
|
|
CAddress(ResolveService("250.1.1.1"), NODE_NONE),
|
2020-02-13 01:01:45 -03:00
|
|
|
ResolveIP("250." + ToString(p) + ".1.1"));
|
2019-12-24 15:18:44 -03:00
|
|
|
int bucket = infoj.GetNewBucket(nKey1, asmap);
|
2015-12-13 00:34:08 -03:00
|
|
|
buckets.insert(bucket);
|
|
|
|
}
|
2019-12-24 15:18:44 -03:00
|
|
|
// Test: IP addresses in the different source groups should map to MORE
|
2015-12-13 00:34:08 -03:00
|
|
|
// than 64 buckets.
|
|
|
|
BOOST_CHECK(buckets.size() > 64);
|
|
|
|
}
|
2016-10-27 14:55:39 -03:00
|
|
|
|
2019-12-24 15:18:44 -03:00
|
|
|
// The following three test cases use asmap.raw
|
|
|
|
// We use an artificial minimal mock mapping
|
|
|
|
// 250.0.0.0/8 AS1000
|
|
|
|
// 101.1.0.0/16 AS1
|
|
|
|
// 101.2.0.0/16 AS2
|
|
|
|
// 101.3.0.0/16 AS3
|
|
|
|
// 101.4.0.0/16 AS4
|
|
|
|
// 101.5.0.0/16 AS5
|
|
|
|
// 101.6.0.0/16 AS6
|
|
|
|
// 101.7.0.0/16 AS7
|
|
|
|
// 101.8.0.0/16 AS8
|
|
|
|
BOOST_AUTO_TEST_CASE(caddrinfo_get_tried_bucket)
|
|
|
|
{
|
|
|
|
CAddrManTest addrman;
|
|
|
|
|
|
|
|
CAddress addr1 = CAddress(ResolveService("250.1.1.1", 8333), NODE_NONE);
|
|
|
|
CAddress addr2 = CAddress(ResolveService("250.1.1.1", 9999), NODE_NONE);
|
|
|
|
|
|
|
|
CNetAddr source1 = ResolveIP("250.1.1.1");
|
|
|
|
|
|
|
|
|
|
|
|
CAddrInfo info1 = CAddrInfo(addr1, source1);
|
|
|
|
|
|
|
|
uint256 nKey1 = (uint256)(CHashWriter(SER_GETHASH, 0) << 1).GetHash();
|
|
|
|
uint256 nKey2 = (uint256)(CHashWriter(SER_GETHASH, 0) << 2).GetHash();
|
|
|
|
|
|
|
|
std::vector<bool> asmap = FromBytes(asmap_raw, sizeof(asmap_raw) * 8);
|
|
|
|
|
|
|
|
BOOST_CHECK_EQUAL(info1.GetTriedBucket(nKey1, asmap), 236);
|
|
|
|
|
|
|
|
// Test: Make sure key actually randomizes bucket placement. A fail on
|
|
|
|
// this test could be a security issue.
|
|
|
|
BOOST_CHECK(info1.GetTriedBucket(nKey1, asmap) != info1.GetTriedBucket(nKey2, asmap));
|
|
|
|
|
|
|
|
// Test: Two addresses with same IP but different ports can map to
|
|
|
|
// different buckets because they have different keys.
|
|
|
|
CAddrInfo info2 = CAddrInfo(addr2, source1);
|
|
|
|
|
|
|
|
BOOST_CHECK(info1.GetKey() != info2.GetKey());
|
|
|
|
BOOST_CHECK(info1.GetTriedBucket(nKey1, asmap) != info2.GetTriedBucket(nKey1, asmap));
|
|
|
|
|
|
|
|
std::set<int> buckets;
|
|
|
|
for (int j = 0; j < 255; j++) {
|
|
|
|
CAddrInfo infoj = CAddrInfo(
|
2020-02-13 01:01:45 -03:00
|
|
|
CAddress(ResolveService("101." + ToString(j) + ".1.1"), NODE_NONE),
|
|
|
|
ResolveIP("101." + ToString(j) + ".1.1"));
|
2019-12-24 15:18:44 -03:00
|
|
|
int bucket = infoj.GetTriedBucket(nKey1, asmap);
|
|
|
|
buckets.insert(bucket);
|
|
|
|
}
|
|
|
|
// Test: IP addresses in the different /16 prefix MAY map to more than
|
|
|
|
// 8 buckets.
|
|
|
|
BOOST_CHECK(buckets.size() > 8);
|
|
|
|
|
|
|
|
buckets.clear();
|
|
|
|
for (int j = 0; j < 255; j++) {
|
|
|
|
CAddrInfo infoj = CAddrInfo(
|
2020-02-13 01:01:45 -03:00
|
|
|
CAddress(ResolveService("250." + ToString(j) + ".1.1"), NODE_NONE),
|
|
|
|
ResolveIP("250." + ToString(j) + ".1.1"));
|
2019-12-24 15:18:44 -03:00
|
|
|
int bucket = infoj.GetTriedBucket(nKey1, asmap);
|
|
|
|
buckets.insert(bucket);
|
|
|
|
}
|
|
|
|
// Test: IP addresses in the different /16 prefix MAY NOT map to more than
|
|
|
|
// 8 buckets.
|
|
|
|
BOOST_CHECK(buckets.size() == 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket)
|
|
|
|
{
|
|
|
|
CAddrManTest addrman;
|
|
|
|
|
|
|
|
CAddress addr1 = CAddress(ResolveService("250.1.2.1", 8333), NODE_NONE);
|
|
|
|
CAddress addr2 = CAddress(ResolveService("250.1.2.1", 9999), NODE_NONE);
|
|
|
|
|
|
|
|
CNetAddr source1 = ResolveIP("250.1.2.1");
|
|
|
|
|
|
|
|
CAddrInfo info1 = CAddrInfo(addr1, source1);
|
|
|
|
|
|
|
|
uint256 nKey1 = (uint256)(CHashWriter(SER_GETHASH, 0) << 1).GetHash();
|
|
|
|
uint256 nKey2 = (uint256)(CHashWriter(SER_GETHASH, 0) << 2).GetHash();
|
|
|
|
|
|
|
|
std::vector<bool> asmap = FromBytes(asmap_raw, sizeof(asmap_raw) * 8);
|
|
|
|
|
|
|
|
// Test: Make sure the buckets are what we expect
|
|
|
|
BOOST_CHECK_EQUAL(info1.GetNewBucket(nKey1, asmap), 795);
|
|
|
|
BOOST_CHECK_EQUAL(info1.GetNewBucket(nKey1, source1, asmap), 795);
|
|
|
|
|
|
|
|
// Test: Make sure key actually randomizes bucket placement. A fail on
|
|
|
|
// this test could be a security issue.
|
|
|
|
BOOST_CHECK(info1.GetNewBucket(nKey1, asmap) != info1.GetNewBucket(nKey2, asmap));
|
|
|
|
|
|
|
|
// Test: Ports should not affect bucket placement in the addr
|
|
|
|
CAddrInfo info2 = CAddrInfo(addr2, source1);
|
|
|
|
BOOST_CHECK(info1.GetKey() != info2.GetKey());
|
|
|
|
BOOST_CHECK_EQUAL(info1.GetNewBucket(nKey1, asmap), info2.GetNewBucket(nKey1, asmap));
|
|
|
|
|
|
|
|
std::set<int> buckets;
|
|
|
|
for (int i = 0; i < 255; i++) {
|
|
|
|
CAddrInfo infoi = CAddrInfo(
|
2020-02-13 01:01:45 -03:00
|
|
|
CAddress(ResolveService("250.1.1." + ToString(i)), NODE_NONE),
|
|
|
|
ResolveIP("250.1.1." + ToString(i)));
|
2019-12-24 15:18:44 -03:00
|
|
|
int bucket = infoi.GetNewBucket(nKey1, asmap);
|
|
|
|
buckets.insert(bucket);
|
|
|
|
}
|
|
|
|
// Test: IP addresses in the same /16 prefix
|
|
|
|
// usually map to the same bucket.
|
|
|
|
BOOST_CHECK_EQUAL(buckets.size(), 1U);
|
|
|
|
|
|
|
|
buckets.clear();
|
|
|
|
for (int j = 0; j < 4 * 255; j++) {
|
|
|
|
CAddrInfo infoj = CAddrInfo(CAddress(
|
|
|
|
ResolveService(
|
2020-02-13 01:01:45 -03:00
|
|
|
ToString(250 + (j / 255)) + "." + ToString(j % 256) + ".1.1"), NODE_NONE),
|
2019-12-24 15:18:44 -03:00
|
|
|
ResolveIP("251.4.1.1"));
|
|
|
|
int bucket = infoj.GetNewBucket(nKey1, asmap);
|
|
|
|
buckets.insert(bucket);
|
|
|
|
}
|
|
|
|
// Test: IP addresses in the same source /16 prefix should not map to more
|
|
|
|
// than 64 buckets.
|
|
|
|
BOOST_CHECK(buckets.size() <= 64);
|
|
|
|
|
|
|
|
buckets.clear();
|
|
|
|
for (int p = 0; p < 255; p++) {
|
|
|
|
CAddrInfo infoj = CAddrInfo(
|
|
|
|
CAddress(ResolveService("250.1.1.1"), NODE_NONE),
|
2020-02-13 01:01:45 -03:00
|
|
|
ResolveIP("101." + ToString(p) + ".1.1"));
|
2019-12-24 15:18:44 -03:00
|
|
|
int bucket = infoj.GetNewBucket(nKey1, asmap);
|
|
|
|
buckets.insert(bucket);
|
|
|
|
}
|
|
|
|
// Test: IP addresses in the different source /16 prefixes usually map to MORE
|
|
|
|
// than 1 bucket.
|
|
|
|
BOOST_CHECK(buckets.size() > 1);
|
|
|
|
|
|
|
|
buckets.clear();
|
|
|
|
for (int p = 0; p < 255; p++) {
|
|
|
|
CAddrInfo infoj = CAddrInfo(
|
|
|
|
CAddress(ResolveService("250.1.1.1"), NODE_NONE),
|
2020-02-13 01:01:45 -03:00
|
|
|
ResolveIP("250." + ToString(p) + ".1.1"));
|
2019-12-24 15:18:44 -03:00
|
|
|
int bucket = infoj.GetNewBucket(nKey1, asmap);
|
|
|
|
buckets.insert(bucket);
|
|
|
|
}
|
|
|
|
// Test: IP addresses in the different source /16 prefixes sometimes map to NO MORE
|
|
|
|
// than 1 bucket.
|
|
|
|
BOOST_CHECK(buckets.size() == 1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(addrman_serialization)
|
|
|
|
{
|
|
|
|
std::vector<bool> asmap1 = FromBytes(asmap_raw, sizeof(asmap_raw) * 8);
|
|
|
|
|
|
|
|
CAddrManTest addrman_asmap1(true, asmap1);
|
|
|
|
CAddrManTest addrman_asmap1_dup(true, asmap1);
|
|
|
|
CAddrManTest addrman_noasmap;
|
|
|
|
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
|
|
|
|
|
|
|
|
CAddress addr = CAddress(ResolveService("250.1.1.1"), NODE_NONE);
|
|
|
|
CNetAddr default_source;
|
|
|
|
|
|
|
|
|
|
|
|
addrman_asmap1.Add(addr, default_source);
|
|
|
|
|
|
|
|
stream << addrman_asmap1;
|
|
|
|
// serizalizing/deserializing addrman with the same asmap
|
|
|
|
stream >> addrman_asmap1_dup;
|
|
|
|
|
|
|
|
std::pair<int, int> bucketAndEntry_asmap1 = addrman_asmap1.GetBucketAndEntry(addr);
|
|
|
|
std::pair<int, int> bucketAndEntry_asmap1_dup = addrman_asmap1_dup.GetBucketAndEntry(addr);
|
|
|
|
BOOST_CHECK(bucketAndEntry_asmap1.second != -1);
|
|
|
|
BOOST_CHECK(bucketAndEntry_asmap1_dup.second != -1);
|
|
|
|
|
|
|
|
BOOST_CHECK(bucketAndEntry_asmap1.first == bucketAndEntry_asmap1_dup.first);
|
|
|
|
BOOST_CHECK(bucketAndEntry_asmap1.second == bucketAndEntry_asmap1_dup.second);
|
|
|
|
|
|
|
|
// deserializing asmaped peers.dat to non-asmaped addrman
|
|
|
|
stream << addrman_asmap1;
|
|
|
|
stream >> addrman_noasmap;
|
|
|
|
std::pair<int, int> bucketAndEntry_noasmap = addrman_noasmap.GetBucketAndEntry(addr);
|
|
|
|
BOOST_CHECK(bucketAndEntry_noasmap.second != -1);
|
|
|
|
BOOST_CHECK(bucketAndEntry_asmap1.first != bucketAndEntry_noasmap.first);
|
|
|
|
BOOST_CHECK(bucketAndEntry_asmap1.second != bucketAndEntry_noasmap.second);
|
|
|
|
|
|
|
|
// deserializing non-asmaped peers.dat to asmaped addrman
|
|
|
|
addrman_asmap1.Clear();
|
|
|
|
addrman_noasmap.Clear();
|
|
|
|
addrman_noasmap.Add(addr, default_source);
|
|
|
|
stream << addrman_noasmap;
|
|
|
|
stream >> addrman_asmap1;
|
|
|
|
std::pair<int, int> bucketAndEntry_asmap1_deser = addrman_asmap1.GetBucketAndEntry(addr);
|
|
|
|
BOOST_CHECK(bucketAndEntry_asmap1_deser.second != -1);
|
|
|
|
BOOST_CHECK(bucketAndEntry_asmap1_deser.first != bucketAndEntry_noasmap.first);
|
|
|
|
BOOST_CHECK(bucketAndEntry_asmap1_deser.first == bucketAndEntry_asmap1_dup.first);
|
|
|
|
BOOST_CHECK(bucketAndEntry_asmap1_deser.second == bucketAndEntry_asmap1_dup.second);
|
|
|
|
|
|
|
|
// used to map to different buckets, now maps to the same bucket.
|
|
|
|
addrman_asmap1.Clear();
|
|
|
|
addrman_noasmap.Clear();
|
|
|
|
CAddress addr1 = CAddress(ResolveService("250.1.1.1"), NODE_NONE);
|
|
|
|
CAddress addr2 = CAddress(ResolveService("250.2.1.1"), NODE_NONE);
|
|
|
|
addrman_noasmap.Add(addr, default_source);
|
|
|
|
addrman_noasmap.Add(addr2, default_source);
|
|
|
|
std::pair<int, int> bucketAndEntry_noasmap_addr1 = addrman_noasmap.GetBucketAndEntry(addr1);
|
|
|
|
std::pair<int, int> bucketAndEntry_noasmap_addr2 = addrman_noasmap.GetBucketAndEntry(addr2);
|
|
|
|
BOOST_CHECK(bucketAndEntry_noasmap_addr1.first != bucketAndEntry_noasmap_addr2.first);
|
|
|
|
BOOST_CHECK(bucketAndEntry_noasmap_addr1.second != bucketAndEntry_noasmap_addr2.second);
|
|
|
|
stream << addrman_noasmap;
|
|
|
|
stream >> addrman_asmap1;
|
|
|
|
std::pair<int, int> bucketAndEntry_asmap1_deser_addr1 = addrman_asmap1.GetBucketAndEntry(addr1);
|
|
|
|
std::pair<int, int> bucketAndEntry_asmap1_deser_addr2 = addrman_asmap1.GetBucketAndEntry(addr2);
|
|
|
|
BOOST_CHECK(bucketAndEntry_asmap1_deser_addr1.first == bucketAndEntry_asmap1_deser_addr2.first);
|
|
|
|
BOOST_CHECK(bucketAndEntry_asmap1_deser_addr1.second != bucketAndEntry_asmap1_deser_addr2.second);
|
|
|
|
}
|
|
|
|
|
2016-10-27 14:55:39 -03:00
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(addrman_selecttriedcollision)
|
|
|
|
{
|
|
|
|
CAddrManTest addrman;
|
|
|
|
|
|
|
|
BOOST_CHECK(addrman.size() == 0);
|
|
|
|
|
|
|
|
// Empty addrman should return blank addrman info.
|
|
|
|
BOOST_CHECK(addrman.SelectTriedCollision().ToString() == "[::]:0");
|
|
|
|
|
|
|
|
// Add twenty two addresses.
|
|
|
|
CNetAddr source = ResolveIP("252.2.2.2");
|
|
|
|
for (unsigned int i = 1; i < 23; i++) {
|
2020-02-13 01:01:45 -03:00
|
|
|
CService addr = ResolveService("250.1.1."+ToString(i));
|
2018-12-12 10:31:38 -03:00
|
|
|
BOOST_CHECK(addrman.Add(CAddress(addr, NODE_NONE), source));
|
2016-10-27 14:55:39 -03:00
|
|
|
addrman.Good(addr);
|
|
|
|
|
|
|
|
// No collisions yet.
|
|
|
|
BOOST_CHECK(addrman.size() == i);
|
|
|
|
BOOST_CHECK(addrman.SelectTriedCollision().ToString() == "[::]:0");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure Good handles duplicates well.
|
|
|
|
for (unsigned int i = 1; i < 23; i++) {
|
2020-02-13 01:01:45 -03:00
|
|
|
CService addr = ResolveService("250.1.1."+ToString(i));
|
2016-10-27 14:55:39 -03:00
|
|
|
addrman.Good(addr);
|
|
|
|
|
|
|
|
BOOST_CHECK(addrman.size() == 22);
|
|
|
|
BOOST_CHECK(addrman.SelectTriedCollision().ToString() == "[::]:0");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(addrman_noevict)
|
|
|
|
{
|
|
|
|
CAddrManTest addrman;
|
|
|
|
|
|
|
|
// Add twenty two addresses.
|
|
|
|
CNetAddr source = ResolveIP("252.2.2.2");
|
|
|
|
for (unsigned int i = 1; i < 23; i++) {
|
2020-02-13 01:01:45 -03:00
|
|
|
CService addr = ResolveService("250.1.1."+ToString(i));
|
2018-12-12 10:31:38 -03:00
|
|
|
BOOST_CHECK(addrman.Add(CAddress(addr, NODE_NONE), source));
|
2016-10-27 14:55:39 -03:00
|
|
|
addrman.Good(addr);
|
|
|
|
|
|
|
|
// No collision yet.
|
|
|
|
BOOST_CHECK(addrman.size() == i);
|
|
|
|
BOOST_CHECK(addrman.SelectTriedCollision().ToString() == "[::]:0");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collision between 23 and 19.
|
|
|
|
CService addr23 = ResolveService("250.1.1.23");
|
2018-12-12 10:31:38 -03:00
|
|
|
BOOST_CHECK(addrman.Add(CAddress(addr23, NODE_NONE), source));
|
2016-10-27 14:55:39 -03:00
|
|
|
addrman.Good(addr23);
|
|
|
|
|
|
|
|
BOOST_CHECK(addrman.size() == 23);
|
|
|
|
BOOST_CHECK(addrman.SelectTriedCollision().ToString() == "250.1.1.19:0");
|
|
|
|
|
|
|
|
// 23 should be discarded and 19 not evicted.
|
|
|
|
addrman.ResolveCollisions();
|
|
|
|
BOOST_CHECK(addrman.SelectTriedCollision().ToString() == "[::]:0");
|
|
|
|
|
|
|
|
// Lets create two collisions.
|
|
|
|
for (unsigned int i = 24; i < 33; i++) {
|
2020-02-13 01:01:45 -03:00
|
|
|
CService addr = ResolveService("250.1.1."+ToString(i));
|
2018-12-12 10:31:38 -03:00
|
|
|
BOOST_CHECK(addrman.Add(CAddress(addr, NODE_NONE), source));
|
2016-10-27 14:55:39 -03:00
|
|
|
addrman.Good(addr);
|
|
|
|
|
|
|
|
BOOST_CHECK(addrman.size() == i);
|
|
|
|
BOOST_CHECK(addrman.SelectTriedCollision().ToString() == "[::]:0");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cause a collision.
|
|
|
|
CService addr33 = ResolveService("250.1.1.33");
|
2018-12-12 10:31:38 -03:00
|
|
|
BOOST_CHECK(addrman.Add(CAddress(addr33, NODE_NONE), source));
|
2016-10-27 14:55:39 -03:00
|
|
|
addrman.Good(addr33);
|
|
|
|
BOOST_CHECK(addrman.size() == 33);
|
|
|
|
|
|
|
|
BOOST_CHECK(addrman.SelectTriedCollision().ToString() == "250.1.1.27:0");
|
|
|
|
|
|
|
|
// Cause a second collision.
|
2018-12-12 10:31:38 -03:00
|
|
|
BOOST_CHECK(!addrman.Add(CAddress(addr23, NODE_NONE), source));
|
2016-10-27 14:55:39 -03:00
|
|
|
addrman.Good(addr23);
|
|
|
|
BOOST_CHECK(addrman.size() == 33);
|
|
|
|
|
|
|
|
BOOST_CHECK(addrman.SelectTriedCollision().ToString() != "[::]:0");
|
|
|
|
addrman.ResolveCollisions();
|
|
|
|
BOOST_CHECK(addrman.SelectTriedCollision().ToString() == "[::]:0");
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(addrman_evictionworks)
|
|
|
|
{
|
|
|
|
CAddrManTest addrman;
|
|
|
|
|
|
|
|
BOOST_CHECK(addrman.size() == 0);
|
|
|
|
|
|
|
|
// Empty addrman should return blank addrman info.
|
|
|
|
BOOST_CHECK(addrman.SelectTriedCollision().ToString() == "[::]:0");
|
|
|
|
|
|
|
|
// Add twenty two addresses.
|
|
|
|
CNetAddr source = ResolveIP("252.2.2.2");
|
|
|
|
for (unsigned int i = 1; i < 23; i++) {
|
2020-02-13 01:01:45 -03:00
|
|
|
CService addr = ResolveService("250.1.1."+ToString(i));
|
2018-12-12 10:31:38 -03:00
|
|
|
BOOST_CHECK(addrman.Add(CAddress(addr, NODE_NONE), source));
|
2016-10-27 14:55:39 -03:00
|
|
|
addrman.Good(addr);
|
|
|
|
|
|
|
|
// No collision yet.
|
|
|
|
BOOST_CHECK(addrman.size() == i);
|
|
|
|
BOOST_CHECK(addrman.SelectTriedCollision().ToString() == "[::]:0");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collision between 23 and 19.
|
|
|
|
CService addr = ResolveService("250.1.1.23");
|
2018-12-12 10:31:38 -03:00
|
|
|
BOOST_CHECK(addrman.Add(CAddress(addr, NODE_NONE), source));
|
2016-10-27 14:55:39 -03:00
|
|
|
addrman.Good(addr);
|
|
|
|
|
|
|
|
BOOST_CHECK(addrman.size() == 23);
|
|
|
|
CAddrInfo info = addrman.SelectTriedCollision();
|
|
|
|
BOOST_CHECK(info.ToString() == "250.1.1.19:0");
|
|
|
|
|
|
|
|
// Ensure test of address fails, so that it is evicted.
|
|
|
|
addrman.SimConnFail(info);
|
|
|
|
|
|
|
|
// Should swap 23 for 19.
|
|
|
|
addrman.ResolveCollisions();
|
|
|
|
BOOST_CHECK(addrman.SelectTriedCollision().ToString() == "[::]:0");
|
|
|
|
|
|
|
|
// If 23 was swapped for 19, then this should cause no collisions.
|
2018-12-12 10:31:38 -03:00
|
|
|
BOOST_CHECK(!addrman.Add(CAddress(addr, NODE_NONE), source));
|
2016-10-27 14:55:39 -03:00
|
|
|
addrman.Good(addr);
|
|
|
|
|
|
|
|
BOOST_CHECK(addrman.SelectTriedCollision().ToString() == "[::]:0");
|
|
|
|
|
|
|
|
// If we insert 19 is should collide with 23.
|
|
|
|
CService addr19 = ResolveService("250.1.1.19");
|
2018-12-12 10:31:38 -03:00
|
|
|
BOOST_CHECK(!addrman.Add(CAddress(addr19, NODE_NONE), source));
|
2016-10-27 14:55:39 -03:00
|
|
|
addrman.Good(addr19);
|
|
|
|
|
|
|
|
BOOST_CHECK(addrman.SelectTriedCollision().ToString() == "250.1.1.23:0");
|
|
|
|
|
|
|
|
addrman.ResolveCollisions();
|
|
|
|
BOOST_CHECK(addrman.SelectTriedCollision().ToString() == "[::]:0");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-22 07:51:50 -03:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|