miodragpop
4 years ago
19 changed files with 1562 additions and 119 deletions
@ -0,0 +1,15 @@ |
|||
#include "fs.h" |
|||
|
|||
namespace fsbridge { |
|||
|
|||
FILE *fopen(const fs::path& p, const char *mode) |
|||
{ |
|||
return ::fopen(p.string().c_str(), mode); |
|||
} |
|||
|
|||
FILE *freopen(const fs::path& p, const char *mode, FILE *stream) |
|||
{ |
|||
return ::freopen(p.string().c_str(), mode, stream); |
|||
} |
|||
|
|||
} // fsbridge
|
@ -0,0 +1,24 @@ |
|||
// Copyright (c) 2017 The Bitcoin Core developers
|
|||
// Distributed under the MIT software license, see the accompanying
|
|||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|||
|
|||
#ifndef KOMODO_FS_H |
|||
#define KOMODO_FS_H |
|||
|
|||
#include <stdio.h> |
|||
#include <string> |
|||
|
|||
#include <boost/filesystem.hpp> |
|||
#include <boost/filesystem/fstream.hpp> |
|||
#include <boost/filesystem/detail/utf8_codecvt_facet.hpp> |
|||
|
|||
/** Filesystem operations and types */ |
|||
namespace fs = boost::filesystem; |
|||
|
|||
/** Bridge operations to C stdio */ |
|||
namespace fsbridge { |
|||
FILE *fopen(const fs::path& p, const char *mode); |
|||
FILE *freopen(const fs::path& p, const char *mode, FILE *stream); |
|||
}; |
|||
|
|||
#endif // KOMODO_FS_H
|
@ -0,0 +1,862 @@ |
|||
#include <gtest/gtest.h> |
|||
|
|||
#include "addrman.h" |
|||
#include <boost/filesystem.hpp> |
|||
#include <boost/thread.hpp> |
|||
#include <string> |
|||
|
|||
#include "hash.h" |
|||
#include "random.h" |
|||
#include "util/asmap.h" |
|||
|
|||
#include "netbase.h" |
|||
#include "chainparams.h" |
|||
#include "tinyformat.h" |
|||
#include "utilstrencodings.h" |
|||
|
|||
#define NODE_NONE 0 |
|||
|
|||
// https://stackoverflow.com/questions/16491675/how-to-send-custom-message-in-google-c-testing-framework/29155677
|
|||
#define GTEST_COUT_NOCOLOR std::cerr << "[ ] [ INFO ] " |
|||
namespace testing |
|||
{ |
|||
namespace internal |
|||
{ |
|||
enum GTestColor { |
|||
COLOR_DEFAULT, |
|||
COLOR_RED, |
|||
COLOR_GREEN, |
|||
COLOR_YELLOW |
|||
}; |
|||
|
|||
extern void ColoredPrintf(GTestColor color, const char* fmt, ...); |
|||
} |
|||
} |
|||
#define PRINTF(...) do { testing::internal::ColoredPrintf(testing::internal::COLOR_GREEN, "[ ] "); testing::internal::ColoredPrintf(testing::internal::COLOR_YELLOW, __VA_ARGS__); } while(0) |
|||
|
|||
// C++ stream interface
|
|||
class TestCout : public std::stringstream |
|||
{ |
|||
public: |
|||
~TestCout() |
|||
{ |
|||
PRINTF("%s",str().c_str()); |
|||
} |
|||
}; |
|||
|
|||
#define GTEST_COUT_COLOR TestCout() |
|||
|
|||
using namespace std; |
|||
|
|||
/* xxd -i est-komodo/data/asmap.raw | sed 's/unsigned char/static unsigned const char/g' */ |
|||
static unsigned const char asmap_raw[] = { |
|||
0xfb, 0x03, 0xec, 0x0f, 0xb0, 0x3f, 0xc0, 0xfe, 0x00, 0xfb, 0x03, 0xec, |
|||
0x0f, 0xb0, 0x3f, 0xc0, 0xfe, 0x00, 0xfb, 0x03, 0xec, 0x0f, 0xb0, 0xff, |
|||
0xff, 0xfe, 0xff, 0xed, 0xb0, 0xff, 0xd4, 0x86, 0xe6, 0x28, 0x29, 0x00, |
|||
0x00, 0x40, 0x00, 0x00, 0x40, 0x00, 0x40, 0x99, 0x01, 0x00, 0x80, 0x01, |
|||
0x80, 0x04, 0x00, 0x00, 0x05, 0x00, 0x06, 0x00, 0x1c, 0xf0, 0x39 |
|||
}; |
|||
unsigned int asmap_raw_len = 59; |
|||
|
|||
class CAddrManTest : public CAddrMan |
|||
{ |
|||
private: |
|||
uint64_t state; |
|||
bool deterministic; |
|||
public: |
|||
|
|||
explicit CAddrManTest(bool makeDeterministic = true, |
|||
std::vector<bool> asmap = std::vector<bool>()) |
|||
{ |
|||
if (makeDeterministic) { |
|||
// Set addrman addr placement to be deterministic.
|
|||
MakeDeterministic(); |
|||
} |
|||
deterministic = makeDeterministic; |
|||
m_asmap = asmap; |
|||
state = 1; |
|||
} |
|||
|
|||
void PrintInternals() |
|||
{ |
|||
GTEST_COUT_NOCOLOR << "mapInfo.size() = " << mapInfo.size() << std::endl; |
|||
GTEST_COUT_NOCOLOR << "nNew = " << nNew << std::endl; |
|||
} |
|||
|
|||
//! Ensure that bucket placement is always the same for testing purposes.
|
|||
void MakeDeterministic() |
|||
{ |
|||
nKey.SetNull(); |
|||
seed_insecure_rand(true); |
|||
} |
|||
|
|||
int RandomInt(int nMax) |
|||
{ |
|||
state = (CHashWriter(SER_GETHASH, 0) << state).GetHash().GetCheapHash(); |
|||
return (unsigned int)(state % nMax); |
|||
} |
|||
|
|||
CAddrInfo* Find(const CNetAddr& addr, int* pnId = NULL) |
|||
{ |
|||
return CAddrMan::Find(addr, pnId); |
|||
} |
|||
|
|||
CAddrInfo* Create(const CAddress& addr, const CNetAddr& addrSource, int* pnId = NULL) |
|||
{ |
|||
return CAddrMan::Create(addr, addrSource, pnId); |
|||
} |
|||
|
|||
void Delete(int nId) |
|||
{ |
|||
CAddrMan::Delete(nId); |
|||
} |
|||
|
|||
// 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); |
|||
} |
|||
|
|||
void Clear() |
|||
{ |
|||
CAddrMan::Clear(); |
|||
if (deterministic) { |
|||
nKey.SetNull(); |
|||
seed_insecure_rand(true); |
|||
} |
|||
} |
|||
}; |
|||
|
|||
static CNetAddr ResolveIP(const std::string& ip) |
|||
{ |
|||
vector<CNetAddr> vIPs; |
|||
CNetAddr addr; |
|||
if (LookupHost(ip.c_str(), vIPs)) { |
|||
addr = vIPs[0]; |
|||
} else |
|||
{ |
|||
// it was BOOST_CHECK_MESSAGE, but we can't use ASSERT or EXPECT outside a test
|
|||
GTEST_COUT_COLOR << strprintf("failed to resolve: %s", ip) << std::endl; |
|||
} |
|||
return addr; |
|||
} |
|||
|
|||
static CService ResolveService(const std::string& ip, const int port = 0) |
|||
{ |
|||
CService serv; |
|||
if (!Lookup(ip.c_str(), serv, port, false)) |
|||
GTEST_COUT_COLOR << strprintf("failed to resolve: %s:%i", ip, port) << std::endl; |
|||
return serv; |
|||
} |
|||
|
|||
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; |
|||
} |
|||
|
|||
namespace TestAddrmanTests { |
|||
|
|||
TEST(TestAddrmanTests, display_constants) { |
|||
|
|||
// Not actually the test, just used to display constants
|
|||
GTEST_COUT_COLOR << "ADDRMAN_NEW_BUCKET_COUNT = " << ADDRMAN_NEW_BUCKET_COUNT << std::endl; |
|||
GTEST_COUT_COLOR << "ADDRMAN_TRIED_BUCKET_COUNT = " << ADDRMAN_TRIED_BUCKET_COUNT << std::endl; |
|||
GTEST_COUT_COLOR << "ADDRMAN_BUCKET_SIZE = " << ADDRMAN_BUCKET_SIZE << std::endl; |
|||
|
|||
} |
|||
|
|||
TEST(TestAddrmanTests, addrman_simple) { |
|||
|
|||
CAddrManTest addrman; |
|||
|
|||
// Set addrman addr placement to be deterministic.
|
|||
addrman.MakeDeterministic(); |
|||
|
|||
CNetAddr source = CNetAddr("252.2.2.2"); |
|||
|
|||
// Test 1: Does Addrman respond correctly when empty.
|
|||
ASSERT_TRUE(addrman.size() == 0); |
|||
CAddrInfo addr_null = addrman.Select(); |
|||
ASSERT_TRUE(addr_null.ToString() == "[::]:0"); |
|||
|
|||
// Test 2: Does Addrman::Add work as expected.
|
|||
CService addr1 = CService("250.1.1.1", 8333); |
|||
addrman.Add(CAddress(addr1, NODE_NONE), source); |
|||
ASSERT_TRUE(addrman.size() == 1); |
|||
CAddrInfo addr_ret1 = addrman.Select(); |
|||
ASSERT_TRUE(addr_ret1.ToString() == "250.1.1.1:8333"); |
|||
|
|||
// Test 3: Does IP address deduplication work correctly.
|
|||
// Expected dup IP should not be added.
|
|||
CService addr1_dup = CService("250.1.1.1", 8333); |
|||
addrman.Add(CAddress(addr1_dup, NODE_NONE), source); |
|||
ASSERT_TRUE(addrman.size() == 1); |
|||
|
|||
// Test 5: New table has one addr and we add a diff addr we should
|
|||
// have two addrs.
|
|||
CService addr2 = CService("250.1.1.2", 8333); |
|||
addrman.Add(CAddress(addr2, NODE_NONE), source); |
|||
ASSERT_TRUE(addrman.size() == 2); |
|||
|
|||
// Test 6: AddrMan::Clear() should empty the new table.
|
|||
addrman.Clear(); |
|||
ASSERT_TRUE(addrman.size() == 0); |
|||
CAddrInfo addr_null2 = addrman.Select(); |
|||
ASSERT_TRUE(addr_null2.ToString() == "[::]:0"); |
|||
|
|||
} |
|||
|
|||
TEST(TestAddrmanTests, addrman_ports) { |
|||
CAddrManTest addrman; |
|||
|
|||
// Set addrman addr placement to be deterministic.
|
|||
addrman.MakeDeterministic(); |
|||
|
|||
CNetAddr source = CNetAddr("252.2.2.2"); |
|||
|
|||
ASSERT_TRUE(addrman.size() == 0); |
|||
|
|||
// Test 7; Addr with same IP but diff port does not replace existing addr.
|
|||
CService addr1 = CService("250.1.1.1", 8333); |
|||
addrman.Add(CAddress(addr1, NODE_NONE), source); |
|||
ASSERT_TRUE(addrman.size() == 1); |
|||
|
|||
CService addr1_port = CService("250.1.1.1", 8334); |
|||
addrman.Add(CAddress(addr1_port, NODE_NONE), source); |
|||
ASSERT_TRUE(addrman.size() == 1); |
|||
CAddrInfo addr_ret2 = addrman.Select(); |
|||
ASSERT_TRUE(addr_ret2.ToString() == "250.1.1.1:8333"); |
|||
|
|||
// Test 8: Add same IP but diff port to tried table, it doesn't get added.
|
|||
// Perhaps this is not ideal behavior but it is the current behavior.
|
|||
addrman.Good(CAddress(addr1_port, NODE_NONE)); |
|||
ASSERT_TRUE(addrman.size() == 1); |
|||
bool newOnly = true; |
|||
CAddrInfo addr_ret3 = addrman.Select(newOnly); |
|||
ASSERT_TRUE(addr_ret3.ToString() == "250.1.1.1:8333"); |
|||
|
|||
} |
|||
|
|||
TEST(TestAddrmanTests, addrman_select) { |
|||
CAddrManTest addrman; |
|||
|
|||
// Set addrman addr placement to be deterministic.
|
|||
addrman.MakeDeterministic(); |
|||
|
|||
CNetAddr source = CNetAddr("252.2.2.2"); |
|||
|
|||
// Test 9: Select from new with 1 addr in new.
|
|||
CService addr1 = CService("250.1.1.1", 8333); |
|||
addrman.Add(CAddress(addr1, NODE_NONE), source); |
|||
ASSERT_TRUE(addrman.size() == 1); |
|||
|
|||
bool newOnly = true; |
|||
CAddrInfo addr_ret1 = addrman.Select(newOnly); |
|||
ASSERT_TRUE(addr_ret1.ToString() == "250.1.1.1:8333"); |
|||
|
|||
// Test 10: move addr to tried, select from new expected nothing returned.
|
|||
addrman.Good(CAddress(addr1, NODE_NONE)); |
|||
ASSERT_TRUE(addrman.size() == 1); |
|||
CAddrInfo addr_ret2 = addrman.Select(newOnly); |
|||
ASSERT_TRUE(addr_ret2.ToString() == "[::]:0"); |
|||
|
|||
CAddrInfo addr_ret3 = addrman.Select(); |
|||
ASSERT_TRUE(addr_ret3.ToString() == "250.1.1.1:8333"); |
|||
|
|||
ASSERT_TRUE(addrman.size() == 1); |
|||
|
|||
|
|||
// Add three addresses to new table.
|
|||
CService addr2 = CService("250.3.1.1", 8333); |
|||
CService addr3 = CService("250.3.2.2", 9999); |
|||
CService addr4 = CService("250.3.3.3", 9999); |
|||
|
|||
addrman.Add(CAddress(addr2, NODE_NONE), CService("250.3.1.1", 8333)); |
|||
addrman.Add(CAddress(addr3, NODE_NONE), CService("250.3.1.1", 8333)); |
|||
addrman.Add(CAddress(addr4, NODE_NONE), CService("250.4.1.1", 8333)); |
|||
|
|||
// Add three addresses to tried table.
|
|||
CService addr5 = CService("250.4.4.4", 8333); |
|||
CService addr6 = CService("250.4.5.5", 7777); |
|||
CService addr7 = CService("250.4.6.6", 8333); |
|||
|
|||
addrman.Add(CAddress(addr5, NODE_NONE), CService("250.3.1.1", 8333)); |
|||
addrman.Good(CAddress(addr5, NODE_NONE)); |
|||
addrman.Add(CAddress(addr6, NODE_NONE), CService("250.3.1.1", 8333)); |
|||
addrman.Good(CAddress(addr6, NODE_NONE)); |
|||
addrman.Add(CAddress(addr7, NODE_NONE), CService("250.1.1.3", 8333)); |
|||
addrman.Good(CAddress(addr7, NODE_NONE)); |
|||
|
|||
// Test 11: 6 addrs + 1 addr from last test = 7.
|
|||
ASSERT_TRUE(addrman.size() == 7); |
|||
|
|||
// Test 12: Select pulls from new and tried regardless of port number.
|
|||
ASSERT_TRUE(addrman.Select().ToString() == "250.4.6.6:8333"); |
|||
ASSERT_TRUE(addrman.Select().ToString() == "250.3.2.2:9999"); |
|||
ASSERT_TRUE(addrman.Select().ToString() == "250.3.3.3:9999"); |
|||
ASSERT_TRUE(addrman.Select().ToString() == "250.4.4.4:8333"); |
|||
|
|||
} |
|||
|
|||
TEST(TestAddrmanTests, addrman_new_collisions) |
|||
{ |
|||
CAddrManTest addrman; |
|||
|
|||
// Set addrman addr placement to be deterministic.
|
|||
addrman.MakeDeterministic(); |
|||
|
|||
CNetAddr source = CNetAddr("252.2.2.2"); |
|||
|
|||
ASSERT_TRUE(addrman.size() == 0); |
|||
|
|||
for (unsigned int i = 1; i < 18; i++) { |
|||
CService addr = CService("250.1.1." + boost::to_string(i)); |
|||
addrman.Add(CAddress(addr, NODE_NONE), source); |
|||
//Test 13: No collision in new table yet.
|
|||
ASSERT_TRUE(addrman.size() == i); |
|||
} |
|||
|
|||
//Test 14: new table collision!
|
|||
CService addr1 = CService("250.1.1.18"); |
|||
addrman.Add(CAddress(addr1, NODE_NONE), source); |
|||
ASSERT_TRUE(addrman.size() == 17); |
|||
|
|||
CService addr2 = CService("250.1.1.19"); |
|||
addrman.Add(CAddress(addr2, NODE_NONE), source); |
|||
ASSERT_TRUE(addrman.size() == 18); |
|||
} |
|||
|
|||
TEST(TestAddrmanTests, addrman_tried_collisions) |
|||
{ |
|||
CAddrManTest addrman; |
|||
|
|||
// Set addrman addr placement to be deterministic.
|
|||
addrman.MakeDeterministic(); |
|||
|
|||
CNetAddr source = CNetAddr("252.2.2.2"); |
|||
|
|||
ASSERT_TRUE(addrman.size() == 0); |
|||
|
|||
for (unsigned int i = 1; i < 80; i++) { |
|||
CService addr = CService("250.1.1." + boost::to_string(i)); |
|||
addrman.Add(CAddress(addr, NODE_NONE), source); |
|||
addrman.Good(CAddress(addr, NODE_NONE)); |
|||
|
|||
//Test 15: No collision in tried table yet.
|
|||
// GTEST_COUT << addrman.size() << std::endl;
|
|||
ASSERT_TRUE(addrman.size() == i); |
|||
} |
|||
|
|||
//Test 16: tried table collision!
|
|||
CService addr1 = CService("250.1.1.80"); |
|||
addrman.Add(CAddress(addr1, NODE_NONE), source); |
|||
ASSERT_TRUE(addrman.size() == 79); |
|||
|
|||
CService addr2 = CService("250.1.1.81"); |
|||
addrman.Add(CAddress(addr2, NODE_NONE), source); |
|||
ASSERT_TRUE(addrman.size() == 80); |
|||
} |
|||
|
|||
TEST(TestAddrmanTests, addrman_find) |
|||
{ |
|||
CAddrManTest addrman; |
|||
|
|||
// Set addrman addr placement to be deterministic.
|
|||
addrman.MakeDeterministic(); |
|||
|
|||
ASSERT_TRUE(addrman.size() == 0); |
|||
|
|||
CAddress addr1 = CAddress(CService("250.1.2.1", 8333), NODE_NONE); |
|||
CAddress addr2 = CAddress(CService("250.1.2.1", 9999), NODE_NONE); |
|||
CAddress addr3 = CAddress(CService("251.255.2.1", 8333), NODE_NONE); |
|||
|
|||
CNetAddr source1 = CNetAddr("250.1.2.1"); |
|||
CNetAddr source2 = CNetAddr("250.1.2.2"); |
|||
|
|||
addrman.Add(addr1, source1); |
|||
addrman.Add(addr2, source2); |
|||
addrman.Add(addr3, source1); |
|||
|
|||
// Test 17: ensure Find returns an IP matching what we searched on.
|
|||
CAddrInfo* info1 = addrman.Find(addr1); |
|||
ASSERT_TRUE(info1); |
|||
if (info1) |
|||
ASSERT_TRUE(info1->ToString() == "250.1.2.1:8333"); |
|||
|
|||
// Test 18; Find does not discriminate by port number.
|
|||
CAddrInfo* info2 = addrman.Find(addr2); |
|||
ASSERT_TRUE(info2); |
|||
if (info2) |
|||
ASSERT_TRUE(info2->ToString() == info1->ToString()); |
|||
|
|||
// Test 19: Find returns another IP matching what we searched on.
|
|||
CAddrInfo* info3 = addrman.Find(addr3); |
|||
ASSERT_TRUE(info3); |
|||
if (info3) |
|||
ASSERT_TRUE(info3->ToString() == "251.255.2.1:8333"); |
|||
} |
|||
|
|||
TEST(TestAddrmanTests, addrman_create) |
|||
{ |
|||
CAddrManTest addrman; |
|||
|
|||
// Set addrman addr placement to be deterministic.
|
|||
addrman.MakeDeterministic(); |
|||
|
|||
ASSERT_TRUE(addrman.size() == 0); |
|||
|
|||
CAddress addr1 = CAddress(CService("250.1.2.1", 8333), NODE_NONE); |
|||
CNetAddr source1 = CNetAddr("250.1.2.1"); |
|||
|
|||
int nId; |
|||
CAddrInfo* pinfo = addrman.Create(addr1, source1, &nId); |
|||
|
|||
// Test 20: The result should be the same as the input addr.
|
|||
ASSERT_TRUE(pinfo->ToString() == "250.1.2.1:8333"); |
|||
|
|||
CAddrInfo* info2 = addrman.Find(addr1); |
|||
ASSERT_TRUE(info2->ToString() == "250.1.2.1:8333"); |
|||
} |
|||
|
|||
|
|||
TEST(TestAddrmanTests, addrman_delete) |
|||
{ |
|||
CAddrManTest addrman; |
|||
|
|||
// Set addrman addr placement to be deterministic.
|
|||
addrman.MakeDeterministic(); |
|||
|
|||
ASSERT_TRUE(addrman.size() == 0); |
|||
|
|||
CAddress addr1 = CAddress(CService("250.1.2.1", 8333), NODE_NONE); |
|||
CNetAddr source1 = CNetAddr("250.1.2.1"); |
|||
|
|||
int nId; |
|||
addrman.Create(addr1, source1, &nId); |
|||
|
|||
// Test 21: Delete should actually delete the addr.
|
|||
ASSERT_TRUE(addrman.size() == 1); |
|||
addrman.Delete(nId); |
|||
ASSERT_TRUE(addrman.size() == 0); |
|||
CAddrInfo* info2 = addrman.Find(addr1); |
|||
ASSERT_TRUE(info2 == NULL); |
|||
} |
|||
|
|||
TEST(TestAddrmanTests, addrman_getaddr) |
|||
{ |
|||
CAddrManTest addrman; |
|||
|
|||
// Set addrman addr placement to be deterministic.
|
|||
addrman.MakeDeterministic(); |
|||
|
|||
// Test 22: Sanity check, GetAddr should never return anything if addrman
|
|||
// is empty.
|
|||
ASSERT_TRUE(addrman.size() == 0); |
|||
vector<CAddress> vAddr1 = addrman.GetAddr(); |
|||
ASSERT_TRUE(vAddr1.size() == 0); |
|||
|
|||
CAddress addr1 = CAddress(CService("250.250.2.1", 8333), NODE_NONE); |
|||
addr1.nTime = GetTime(); // Set time so isTerrible = false
|
|||
CAddress addr2 = CAddress(CService("250.251.2.2", 9999), NODE_NONE); |
|||
addr2.nTime = GetTime(); |
|||
CAddress addr3 = CAddress(CService("251.252.2.3", 8333), NODE_NONE); |
|||
addr3.nTime = GetTime(); |
|||
CAddress addr4 = CAddress(CService("252.253.3.4", 8333), NODE_NONE); |
|||
addr4.nTime = GetTime(); |
|||
CAddress addr5 = CAddress(CService("252.254.4.5", 8333), NODE_NONE); |
|||
addr5.nTime = GetTime(); |
|||
CNetAddr source1 = CNetAddr("250.1.2.1"); |
|||
CNetAddr source2 = CNetAddr("250.2.3.3"); |
|||
|
|||
// Test 23: Ensure GetAddr works with new addresses.
|
|||
addrman.Add(addr1, source1); |
|||
addrman.Add(addr2, source2); |
|||
addrman.Add(addr3, source1); |
|||
addrman.Add(addr4, source2); |
|||
addrman.Add(addr5, source1); |
|||
|
|||
// GetAddr returns 23% of addresses, 23% of 5 is 1 rounded down.
|
|||
ASSERT_TRUE(addrman.GetAddr().size() == 1); |
|||
|
|||
// Test 24: Ensure GetAddr works with new and tried addresses.
|
|||
addrman.Good(CAddress(addr1, NODE_NONE)); |
|||
addrman.Good(CAddress(addr2, NODE_NONE)); |
|||
ASSERT_TRUE(addrman.GetAddr().size() == 1); |
|||
|
|||
// Test 25: Ensure GetAddr still returns 23% when addrman has many addrs.
|
|||
for (unsigned int i = 1; i < (8 * 256); i++) { |
|||
int octet1 = i % 256; |
|||
int octet2 = (i / 256) % 256; |
|||
int octet3 = (i / (256 * 2)) % 256; |
|||
string strAddr = boost::to_string(octet1) + "." + boost::to_string(octet2) + "." + boost::to_string(octet3) + ".23"; |
|||
CAddress addr = CAddress(CService(strAddr), NODE_NONE); |
|||
|
|||
// Ensure that for all addrs in addrman, isTerrible == false.
|
|||
addr.nTime = GetTime(); |
|||
addrman.Add(addr, CNetAddr(strAddr)); |
|||
if (i % 8 == 0) |
|||
addrman.Good(addr); |
|||
} |
|||
vector<CAddress> vAddr = addrman.GetAddr(); |
|||
|
|||
size_t percent23 = (addrman.size() * 23) / 100; |
|||
ASSERT_TRUE(vAddr.size() == percent23); |
|||
ASSERT_TRUE(vAddr.size() == 461); |
|||
// (Addrman.size() < number of addresses added) due to address collisons.
|
|||
ASSERT_TRUE(addrman.size() == 2007); |
|||
} |
|||
|
|||
TEST(TestAddrmanTests, caddrinfo_get_tried_bucket_legacy) |
|||
{ |
|||
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; // use /16
|
|||
|
|||
ASSERT_EQ(info1.GetTriedBucket(nKey1, asmap), 40); |
|||
|
|||
// Test: Make sure key actually randomizes bucket placement. A fail on
|
|||
// this test could be a security issue.
|
|||
ASSERT_TRUE(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); |
|||
|
|||
ASSERT_TRUE(info1.GetKey() != info2.GetKey()); |
|||
ASSERT_TRUE(info1.GetTriedBucket(nKey1, asmap) != info2.GetTriedBucket(nKey1, asmap)); |
|||
|
|||
std::set<int> buckets; |
|||
for (int i = 0; i < 255; i++) { |
|||
CAddrInfo infoi = CAddrInfo( |
|||
CAddress(ResolveService("250.1.1." + boost::to_string(i)), NODE_NONE), |
|||
ResolveIP("250.1.1." + boost::to_string(i))); |
|||
int bucket = infoi.GetTriedBucket(nKey1, asmap); |
|||
buckets.insert(bucket); |
|||
} |
|||
// Test: IP addresses in the same /16 prefix should
|
|||
// never get more than 8 buckets with legacy grouping
|
|||
ASSERT_EQ(buckets.size(), 8U); |
|||
|
|||
buckets.clear(); |
|||
for (int j = 0; j < 255; j++) { |
|||
CAddrInfo infoj = CAddrInfo( |
|||
CAddress(ResolveService("250." + boost::to_string(j) + ".1.1"), NODE_NONE), |
|||
ResolveIP("250." + boost::to_string(j) + ".1.1")); |
|||
int bucket = infoj.GetTriedBucket(nKey1, asmap); |
|||
buckets.insert(bucket); |
|||
} |
|||
// Test: IP addresses in the different /16 prefix should map to more than
|
|||
// 8 buckets with legacy grouping
|
|||
ASSERT_EQ(buckets.size(), 160U); |
|||
} |
|||
|
|||
TEST(TestAddrmanTests, caddrinfo_get_new_bucket_legacy) |
|||
{ |
|||
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; // use /16
|
|||
|
|||
// Test: Make sure the buckets are what we expect
|
|||
ASSERT_EQ(info1.GetNewBucket(nKey1, asmap), 786); |
|||
ASSERT_EQ(info1.GetNewBucket(nKey1, source1, asmap), 786); |
|||
|
|||
// Test: Make sure key actually randomizes bucket placement. A fail on
|
|||
// this test could be a security issue.
|
|||
ASSERT_TRUE(info1.GetNewBucket(nKey1, asmap) != info1.GetNewBucket(nKey2, asmap)); |
|||
|
|||
// Test: Ports should not affect bucket placement in the addr
|
|||
CAddrInfo info2 = CAddrInfo(addr2, source1); |
|||
ASSERT_TRUE(info1.GetKey() != info2.GetKey()); |
|||
ASSERT_EQ(info1.GetNewBucket(nKey1, asmap), info2.GetNewBucket(nKey1, asmap)); |
|||
|
|||
std::set<int> buckets; |
|||
for (int i = 0; i < 255; i++) { |
|||
CAddrInfo infoi = CAddrInfo( |
|||
CAddress(ResolveService("250.1.1." + boost::to_string(i)), NODE_NONE), |
|||
ResolveIP("250.1.1." + boost::to_string(i))); |
|||
int bucket = infoi.GetNewBucket(nKey1, asmap); |
|||
buckets.insert(bucket); |
|||
} |
|||
// Test: IP addresses in the same group (\16 prefix for IPv4) should
|
|||
// always map to the same bucket.
|
|||
ASSERT_EQ(buckets.size(), 1U); |
|||
|
|||
buckets.clear(); |
|||
for (int j = 0; j < 4 * 255; j++) { |
|||
CAddrInfo infoj = CAddrInfo(CAddress( |
|||
ResolveService( |
|||
boost::to_string(250 + (j / 255)) + "." + boost::to_string(j % 256) + ".1.1"), NODE_NONE), |
|||
ResolveIP("251.4.1.1")); |
|||
int bucket = infoj.GetNewBucket(nKey1, asmap); |
|||
buckets.insert(bucket); |
|||
} |
|||
// Test: IP addresses in the same source groups should map to NO MORE
|
|||
// than 64 buckets.
|
|||
ASSERT_TRUE(buckets.size() <= 64); |
|||
|
|||
buckets.clear(); |
|||
for (int p = 0; p < 255; p++) { |
|||
CAddrInfo infoj = CAddrInfo( |
|||
CAddress(ResolveService("250.1.1.1"), NODE_NONE), |
|||
ResolveIP("250." + boost::to_string(p) + ".1.1")); |
|||
int bucket = infoj.GetNewBucket(nKey1, asmap); |
|||
buckets.insert(bucket); |
|||
} |
|||
// Test: IP addresses in the different source groups should map to MORE
|
|||
// than 64 buckets.
|
|||
ASSERT_TRUE(buckets.size() > 64); |
|||
|
|||
} |
|||
|
|||
// The following three test cases use asmap_raw[] from asmap.raw file
|
|||
// 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
|
|||
|
|||
TEST(TestAddrmanTests, 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); |
|||
|
|||
ASSERT_EQ(info1.GetTriedBucket(nKey1, asmap), 236); |
|||
|
|||
// Test: Make sure key actually randomizes bucket placement. A fail on
|
|||
// this test could be a security issue.
|
|||
ASSERT_TRUE(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); |
|||
|
|||
ASSERT_TRUE(info1.GetKey() != info2.GetKey()); |
|||
ASSERT_TRUE(info1.GetTriedBucket(nKey1, asmap) != info2.GetTriedBucket(nKey1, asmap)); |
|||
|
|||
std::set<int> buckets; |
|||
for (int j = 0; j < 255; j++) { |
|||
CAddrInfo infoj = CAddrInfo( |
|||
CAddress(ResolveService("101." + boost::to_string(j) + ".1.1"), NODE_NONE), |
|||
ResolveIP("101." + boost::to_string(j) + ".1.1")); |
|||
int bucket = infoj.GetTriedBucket(nKey1, asmap); |
|||
buckets.insert(bucket); |
|||
} |
|||
// Test: IP addresses in the different /16 prefix MAY map to more than
|
|||
// 8 buckets.
|
|||
ASSERT_TRUE(buckets.size() > 8); |
|||
|
|||
buckets.clear(); |
|||
for (int j = 0; j < 255; j++) { |
|||
CAddrInfo infoj = CAddrInfo( |
|||
CAddress(ResolveService("250." + boost::to_string(j) + ".1.1"), NODE_NONE), |
|||
ResolveIP("250." + boost::to_string(j) + ".1.1")); |
|||
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.
|
|||
ASSERT_TRUE(buckets.size() == 8); |
|||
} |
|||
|
|||
TEST(TestAddrmanTests, 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
|
|||
ASSERT_EQ(info1.GetNewBucket(nKey1, asmap), 795); |
|||
ASSERT_EQ(info1.GetNewBucket(nKey1, source1, asmap), 795); |
|||
|
|||
// Test: Make sure key actually randomizes bucket placement. A fail on
|
|||
// this test could be a security issue.
|
|||
ASSERT_TRUE(info1.GetNewBucket(nKey1, asmap) != info1.GetNewBucket(nKey2, asmap)); |
|||
|
|||
// Test: Ports should not affect bucket placement in the addr
|
|||
CAddrInfo info2 = CAddrInfo(addr2, source1); |
|||
ASSERT_TRUE(info1.GetKey() != info2.GetKey()); |
|||
ASSERT_EQ(info1.GetNewBucket(nKey1, asmap), info2.GetNewBucket(nKey1, asmap)); |
|||
|
|||
std::set<int> buckets; |
|||
for (int i = 0; i < 255; i++) { |
|||
CAddrInfo infoi = CAddrInfo( |
|||
CAddress(ResolveService("250.1.1." + boost::to_string(i)), NODE_NONE), |
|||
ResolveIP("250.1.1." + boost::to_string(i))); |
|||
int bucket = infoi.GetNewBucket(nKey1, asmap); |
|||
buckets.insert(bucket); |
|||
} |
|||
// Test: IP addresses in the same /16 prefix
|
|||
// usually map to the same bucket.
|
|||
ASSERT_EQ(buckets.size(), 1U); |
|||
|
|||
buckets.clear(); |
|||
for (int j = 0; j < 4 * 255; j++) { |
|||
CAddrInfo infoj = CAddrInfo(CAddress( |
|||
ResolveService( |
|||
boost::to_string(250 + (j / 255)) + "." + boost::to_string(j % 256) + ".1.1"), NODE_NONE), |
|||
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.
|
|||
ASSERT_TRUE(buckets.size() <= 64); |
|||
|
|||
buckets.clear(); |
|||
for (int p = 0; p < 255; p++) { |
|||
CAddrInfo infoj = CAddrInfo( |
|||
CAddress(ResolveService("250.1.1.1"), NODE_NONE), |
|||
ResolveIP("101." + boost::to_string(p) + ".1.1")); |
|||
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.
|
|||
ASSERT_TRUE(buckets.size() > 1); |
|||
|
|||
buckets.clear(); |
|||
for (int p = 0; p < 255; p++) { |
|||
CAddrInfo infoj = CAddrInfo( |
|||
CAddress(ResolveService("250.1.1.1"), NODE_NONE), |
|||
ResolveIP("250." + boost::to_string(p) + ".1.1")); |
|||
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.
|
|||
ASSERT_TRUE(buckets.size() == 1); |
|||
} |
|||
|
|||
TEST(TestAddrmanTests, 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); |
|||
ASSERT_TRUE(bucketAndEntry_asmap1.second != -1); |
|||
ASSERT_TRUE(bucketAndEntry_asmap1_dup.second != -1); |
|||
|
|||
ASSERT_TRUE(bucketAndEntry_asmap1.first == bucketAndEntry_asmap1_dup.first); |
|||
ASSERT_TRUE(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); |
|||
ASSERT_TRUE(bucketAndEntry_noasmap.second != -1); |
|||
ASSERT_TRUE(bucketAndEntry_asmap1.first != bucketAndEntry_noasmap.first); |
|||
ASSERT_TRUE(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); |
|||
// GTEST_COUT_COLOR << addr.ToString() << " - " << default_source.ToString() << " - " << addrman_noasmap.size() << std::endl;
|
|||
// addrman_noasmap.PrintInternals();
|
|||
stream << addrman_noasmap; |
|||
// std::string strHex = HexStr(stream.begin(), stream.end());
|
|||
// GTEST_COUT_COLOR << strHex << std::endl;
|
|||
|
|||
stream >> addrman_asmap1; |
|||
std::pair<int, int> bucketAndEntry_asmap1_deser = addrman_asmap1.GetBucketAndEntry(addr); |
|||
ASSERT_TRUE(bucketAndEntry_asmap1_deser.second != -1); |
|||
ASSERT_TRUE(bucketAndEntry_asmap1_deser.first != bucketAndEntry_noasmap.first); |
|||
ASSERT_TRUE(bucketAndEntry_asmap1_deser.first == bucketAndEntry_asmap1_dup.first); |
|||
ASSERT_TRUE(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); |
|||
ASSERT_TRUE(bucketAndEntry_noasmap_addr1.first != bucketAndEntry_noasmap_addr2.first); |
|||
ASSERT_TRUE(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); |
|||
ASSERT_TRUE(bucketAndEntry_asmap1_deser_addr1.first == bucketAndEntry_asmap1_deser_addr2.first); |
|||
ASSERT_TRUE(bucketAndEntry_asmap1_deser_addr1.second != bucketAndEntry_asmap1_deser_addr2.second); |
|||
} |
|||
|
|||
} |
@ -0,0 +1,78 @@ |
|||
#include <gtest/gtest.h> |
|||
|
|||
#include <boost/filesystem.hpp> |
|||
#include <boost/thread.hpp> |
|||
|
|||
#include "addrman.h" |
|||
#include <string> |
|||
#include "netbase.h" |
|||
|
|||
#define GTEST_COUT_NOCOLOR std::cerr << "[ ] [ INFO ] " |
|||
namespace testing |
|||
{ |
|||
namespace internal |
|||
{ |
|||
enum GTestColor { |
|||
COLOR_DEFAULT, |
|||
COLOR_RED, |
|||
COLOR_GREEN, |
|||
COLOR_YELLOW |
|||
}; |
|||
|
|||
extern void ColoredPrintf(GTestColor color, const char* fmt, ...); |
|||
} |
|||
} |
|||
#define PRINTF(...) do { testing::internal::ColoredPrintf(testing::internal::COLOR_GREEN, "[ ] "); testing::internal::ColoredPrintf(testing::internal::COLOR_YELLOW, __VA_ARGS__); } while(0) |
|||
|
|||
// C++ stream interface
|
|||
class TestCout : public std::stringstream |
|||
{ |
|||
public: |
|||
~TestCout() |
|||
{ |
|||
PRINTF("%s",str().c_str()); |
|||
} |
|||
}; |
|||
|
|||
#define GTEST_COUT_COLOR TestCout() |
|||
|
|||
using namespace std; |
|||
|
|||
static CNetAddr ResolveIP(const std::string& ip) |
|||
{ |
|||
vector<CNetAddr> vIPs; |
|||
CNetAddr addr; |
|||
if (LookupHost(ip.c_str(), vIPs)) { |
|||
addr = vIPs[0]; |
|||
} else |
|||
{ |
|||
// it was BOOST_CHECK_MESSAGE, but we can't use ASSERT outside a test
|
|||
GTEST_COUT_COLOR << strprintf("failed to resolve: %s", ip) << std::endl; |
|||
} |
|||
return addr; |
|||
} |
|||
|
|||
namespace TestNetBaseTests { |
|||
|
|||
TEST(TestAddrmanTests, netbase_getgroup) { |
|||
|
|||
std::vector<bool> asmap; // use /16
|
|||
ASSERT_TRUE(ResolveIP("127.0.0.1").GetGroup(asmap) == std::vector<unsigned char>({0})); // Local -> !Routable()
|
|||
ASSERT_TRUE(ResolveIP("257.0.0.1").GetGroup(asmap) == std::vector<unsigned char>({0})); // !Valid -> !Routable()
|
|||
ASSERT_TRUE(ResolveIP("10.0.0.1").GetGroup(asmap) == std::vector<unsigned char>({0})); // RFC1918 -> !Routable()
|
|||
ASSERT_TRUE(ResolveIP("169.254.1.1").GetGroup(asmap) == std::vector<unsigned char>({0})); // RFC3927 -> !Routable()
|
|||
ASSERT_TRUE(ResolveIP("1.2.3.4").GetGroup(asmap) == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // IPv4
|
|||
|
|||
// std::vector<unsigned char> vch = ResolveIP("4.3.2.1").GetGroup(asmap);
|
|||
// GTEST_COUT_COLOR << boost::to_string((int)vch[0]) << boost::to_string((int)vch[1]) << boost::to_string((int)vch[2]) << std::endl;
|
|||
|
|||
ASSERT_TRUE(ResolveIP("::FFFF:0:102:304").GetGroup(asmap) == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // RFC6145
|
|||
ASSERT_TRUE(ResolveIP("64:FF9B::102:304").GetGroup(asmap) == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // RFC6052
|
|||
ASSERT_TRUE(ResolveIP("2002:102:304:9999:9999:9999:9999:9999").GetGroup(asmap) == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // RFC3964
|
|||
ASSERT_TRUE(ResolveIP("2001:0:9999:9999:9999:9999:FEFD:FCFB").GetGroup(asmap) == std::vector<unsigned char>({(unsigned char)NET_IPV4, 1, 2})); // RFC4380
|
|||
ASSERT_TRUE(ResolveIP("FD87:D87E:EB43:edb1:8e4:3588:e546:35ca").GetGroup(asmap) == std::vector<unsigned char>({(unsigned char)NET_ONION, 239})); // Tor
|
|||
ASSERT_TRUE(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
|
|||
ASSERT_TRUE(ResolveIP("2001:2001:9999:9999:9999:9999:9999:9999").GetGroup(asmap) == std::vector<unsigned char>({(unsigned char)NET_IPV6, 32, 1, 32, 1})); //IPv6
|
|||
|
|||
} |
|||
} |
@ -0,0 +1,187 @@ |
|||
// Copyright (c) 2019-2020 The Bitcoin Core developers
|
|||
// Distributed under the MIT software license, see the accompanying
|
|||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|||
|
|||
#include <map> |
|||
#include <vector> |
|||
#include <assert.h> |
|||
#include <crypto/common.h> |
|||
|
|||
namespace { |
|||
|
|||
constexpr uint32_t INVALID = 0xFFFFFFFF; |
|||
|
|||
uint32_t DecodeBits(std::vector<bool>::const_iterator& bitpos, const std::vector<bool>::const_iterator& endpos, uint8_t minval, const std::vector<uint8_t> &bit_sizes) |
|||
{ |
|||
uint32_t val = minval; |
|||
bool bit; |
|||
for (std::vector<uint8_t>::const_iterator bit_sizes_it = bit_sizes.begin(); |
|||
bit_sizes_it != bit_sizes.end(); ++bit_sizes_it) { |
|||
if (bit_sizes_it + 1 != bit_sizes.end()) { |
|||
if (bitpos == endpos) break; |
|||
bit = *bitpos; |
|||
bitpos++; |
|||
} else { |
|||
bit = 0; |
|||
} |
|||
if (bit) { |
|||
val += (1 << *bit_sizes_it); |
|||
} else { |
|||
for (int b = 0; b < *bit_sizes_it; b++) { |
|||
if (bitpos == endpos) return INVALID; // Reached EOF in mantissa
|
|||
bit = *bitpos; |
|||
bitpos++; |
|||
val += bit << (*bit_sizes_it - 1 - b); |
|||
} |
|||
return val; |
|||
} |
|||
} |
|||
return INVALID; // Reached EOF in exponent
|
|||
} |
|||
|
|||
enum class Instruction : uint32_t |
|||
{ |
|||
RETURN = 0, |
|||
JUMP = 1, |
|||
MATCH = 2, |
|||
DEFAULT = 3, |
|||
}; |
|||
|
|||
const std::vector<uint8_t> TYPE_BIT_SIZES{0, 0, 1}; |
|||
Instruction DecodeType(std::vector<bool>::const_iterator& bitpos, const std::vector<bool>::const_iterator& endpos) |
|||
{ |
|||
return Instruction(DecodeBits(bitpos, endpos, 0, TYPE_BIT_SIZES)); |
|||
} |
|||
|
|||
const std::vector<uint8_t> ASN_BIT_SIZES{15, 16, 17, 18, 19, 20, 21, 22, 23, 24}; |
|||
uint32_t DecodeASN(std::vector<bool>::const_iterator& bitpos, const std::vector<bool>::const_iterator& endpos) |
|||
{ |
|||
return DecodeBits(bitpos, endpos, 1, ASN_BIT_SIZES); |
|||
} |
|||
|
|||
|
|||
const std::vector<uint8_t> MATCH_BIT_SIZES{1, 2, 3, 4, 5, 6, 7, 8}; |
|||
uint32_t DecodeMatch(std::vector<bool>::const_iterator& bitpos, const std::vector<bool>::const_iterator& endpos) |
|||
{ |
|||
return DecodeBits(bitpos, endpos, 2, MATCH_BIT_SIZES); |
|||
} |
|||
|
|||
|
|||
const std::vector<uint8_t> JUMP_BIT_SIZES{5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30}; |
|||
uint32_t DecodeJump(std::vector<bool>::const_iterator& bitpos, const std::vector<bool>::const_iterator& endpos) |
|||
{ |
|||
return DecodeBits(bitpos, endpos, 17, JUMP_BIT_SIZES); |
|||
} |
|||
|
|||
} |
|||
|
|||
uint32_t Interpret(const std::vector<bool> &asmap, const std::vector<bool> &ip) |
|||
{ |
|||
std::vector<bool>::const_iterator pos = asmap.begin(); |
|||
const std::vector<bool>::const_iterator endpos = asmap.end(); |
|||
uint8_t bits = ip.size(); |
|||
uint32_t default_asn = 0; |
|||
uint32_t jump, match, matchlen; |
|||
Instruction opcode; |
|||
while (pos != endpos) { |
|||
opcode = DecodeType(pos, endpos); |
|||
if (opcode == Instruction::RETURN) { |
|||
default_asn = DecodeASN(pos, endpos); |
|||
if (default_asn == INVALID) break; // ASN straddles EOF
|
|||
return default_asn; |
|||
} else if (opcode == Instruction::JUMP) { |
|||
jump = DecodeJump(pos, endpos); |
|||
if (jump == INVALID) break; // Jump offset straddles EOF
|
|||
if (bits == 0) break; // No input bits left
|
|||
if (pos + jump < pos) break; // overflow
|
|||
if (pos + jump >= endpos) break; // Jumping past EOF
|
|||
if (ip[ip.size() - bits]) { |
|||
pos += jump; |
|||
} |
|||
bits--; |
|||
} else if (opcode == Instruction::MATCH) { |
|||
match = DecodeMatch(pos, endpos); |
|||
if (match == INVALID) break; // Match bits straddle EOF
|
|||
matchlen = CountBits(match) - 1; |
|||
if (bits < matchlen) break; // Not enough input bits
|
|||
for (uint32_t bit = 0; bit < matchlen; bit++) { |
|||
if ((ip[ip.size() - bits]) != ((match >> (matchlen - 1 - bit)) & 1)) { |
|||
return default_asn; |
|||
} |
|||
bits--; |
|||
} |
|||
} else if (opcode == Instruction::DEFAULT) { |
|||
default_asn = DecodeASN(pos, endpos); |
|||
if (default_asn == INVALID) break; // ASN straddles EOF
|
|||
} else { |
|||
break; // Instruction straddles EOF
|
|||
} |
|||
} |
|||
assert(false); // Reached EOF without RETURN, or aborted (see any of the breaks above) - should have been caught by SanityCheckASMap below
|
|||
return 0; // 0 is not a valid ASN
|
|||
} |
|||
|
|||
bool SanityCheckASMap(const std::vector<bool>& asmap, int bits) |
|||
{ |
|||
const std::vector<bool>::const_iterator begin = asmap.begin(), endpos = asmap.end(); |
|||
std::vector<bool>::const_iterator pos = begin; |
|||
std::vector<std::pair<uint32_t, int>> jumps; // All future positions we may jump to (bit offset in asmap -> bits to consume left)
|
|||
jumps.reserve(bits); |
|||
Instruction prevopcode = Instruction::JUMP; |
|||
bool had_incomplete_match = false; |
|||
while (pos != endpos) { |
|||
uint32_t offset = pos - begin; |
|||
if (!jumps.empty() && offset >= jumps.back().first) return false; // There was a jump into the middle of the previous instruction
|
|||
Instruction opcode = DecodeType(pos, endpos); |
|||
if (opcode == Instruction::RETURN) { |
|||
if (prevopcode == Instruction::DEFAULT) return false; // There should not be any RETURN immediately after a DEFAULT (could be combined into just RETURN)
|
|||
uint32_t asn = DecodeASN(pos, endpos); |
|||
if (asn == INVALID) return false; // ASN straddles EOF
|
|||
if (jumps.empty()) { |
|||
// Nothing to execute anymore
|
|||
if (endpos - pos > 7) return false; // Excessive padding
|
|||
while (pos != endpos) { |
|||
if (*pos) return false; // Nonzero padding bit
|
|||
++pos; |
|||
} |
|||
return true; // Sanely reached EOF
|
|||
} else { |
|||
// Continue by pretending we jumped to the next instruction
|
|||
offset = pos - begin; |
|||
if (offset != jumps.back().first) return false; // Unreachable code
|
|||
bits = jumps.back().second; // Restore the number of bits we would have had left after this jump
|
|||
jumps.pop_back(); |
|||
prevopcode = Instruction::JUMP; |
|||
} |
|||
} else if (opcode == Instruction::JUMP) { |
|||
uint32_t jump = DecodeJump(pos, endpos); |
|||
if (jump == INVALID) return false; // Jump offset straddles EOF
|
|||
if (pos + jump < pos) return false; // overflow
|
|||
if (pos + jump > endpos) return false; // Jump out of range
|
|||
if (bits == 0) return false; // Consuming bits past the end of the input
|
|||
--bits; |
|||
uint32_t jump_offset = pos - begin + jump; |
|||
if (!jumps.empty() && jump_offset >= jumps.back().first) return false; // Intersecting jumps
|
|||
jumps.emplace_back(jump_offset, bits); |
|||
prevopcode = Instruction::JUMP; |
|||
} else if (opcode == Instruction::MATCH) { |
|||
uint32_t match = DecodeMatch(pos, endpos); |
|||
if (match == INVALID) return false; // Match bits straddle EOF
|
|||
int matchlen = CountBits(match) - 1; |
|||
if (prevopcode != Instruction::MATCH) had_incomplete_match = false; |
|||
if (matchlen < 8 && had_incomplete_match) return false; // Within a sequence of matches only at most one should be incomplete
|
|||
had_incomplete_match = (matchlen < 8); |
|||
if (bits < matchlen) return false; // Consuming bits past the end of the input
|
|||
bits -= matchlen; |
|||
prevopcode = Instruction::MATCH; |
|||
} else if (opcode == Instruction::DEFAULT) { |
|||
if (prevopcode == Instruction::DEFAULT) return false; // There should not be two successive DEFAULTs (they could be combined into one)
|
|||
uint32_t asn = DecodeASN(pos, endpos); |
|||
if (asn == INVALID) return false; // ASN straddles EOF
|
|||
prevopcode = Instruction::DEFAULT; |
|||
} else { |
|||
return false; // Instruction straddles EOF
|
|||
} |
|||
} |
|||
return false; // Reached EOF without RETURN instruction
|
|||
} |
@ -0,0 +1,15 @@ |
|||
// Copyright (c) 2019 The Bitcoin Core developers
|
|||
// Distributed under the MIT software license, see the accompanying
|
|||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|||
|
|||
#ifndef BITCOIN_UTIL_ASMAP_H |
|||
#define BITCOIN_UTIL_ASMAP_H |
|||
|
|||
#include <stdint.h> |
|||
#include <vector> |
|||
|
|||
uint32_t Interpret(const std::vector<bool> &asmap, const std::vector<bool> &ip); |
|||
|
|||
bool SanityCheckASMap(const std::vector<bool>& asmap, int bits); |
|||
|
|||
#endif // BITCOIN_UTIL_ASMAP_H
|
Loading…
Reference in new issue