Changeset View
Changeset View
Standalone View
Standalone View
src/test/addrman_tests.cpp
Show First 20 Lines • Show All 83 Lines • ▼ Show 20 Lines | |||||
BOOST_AUTO_TEST_CASE(addrman_simple) { | BOOST_AUTO_TEST_CASE(addrman_simple) { | ||||
CAddrManTest addrman; | CAddrManTest addrman; | ||||
// Set addrman addr placement to be deterministic. | // Set addrman addr placement to be deterministic. | ||||
addrman.MakeDeterministic(); | addrman.MakeDeterministic(); | ||||
CNetAddr source = ResolveIP("252.2.2.2"); | CNetAddr source = ResolveIP("252.2.2.2"); | ||||
// Test 1: Does Addrman respond correctly when empty. | // Test: Does Addrman respond correctly when empty. | ||||
BOOST_CHECK(addrman.size() == 0); | BOOST_CHECK_EQUAL(addrman.size(), 0); | ||||
CAddrInfo addr_null = addrman.Select(); | CAddrInfo addr_null = addrman.Select(); | ||||
BOOST_CHECK(addr_null.ToString() == "[::]:0"); | BOOST_CHECK_EQUAL(addr_null.ToString(), "[::]:0"); | ||||
// Test 2: Does Addrman::Add work as expected. | // Test: Does Addrman::Add work as expected. | ||||
CService addr1 = ResolveService("250.1.1.1", 8333); | CService addr1 = ResolveService("250.1.1.1", 8333); | ||||
BOOST_CHECK(addrman.Add(CAddress(addr1, NODE_NONE), source)); | BOOST_CHECK(addrman.Add(CAddress(addr1, NODE_NONE), source)); | ||||
BOOST_CHECK(addrman.size() == 1); | BOOST_CHECK_EQUAL(addrman.size(), 1); | ||||
CAddrInfo addr_ret1 = addrman.Select(); | CAddrInfo addr_ret1 = addrman.Select(); | ||||
BOOST_CHECK(addr_ret1.ToString() == "250.1.1.1:8333"); | BOOST_CHECK_EQUAL(addr_ret1.ToString(), "250.1.1.1:8333"); | ||||
// Test 3: Does IP address deduplication work correctly. | // Test: Does IP address deduplication work correctly. | ||||
// Expected dup IP should not be added. | // Expected dup IP should not be added. | ||||
CService addr1_dup = ResolveService("250.1.1.1", 8333); | CService addr1_dup = ResolveService("250.1.1.1", 8333); | ||||
BOOST_CHECK(!addrman.Add(CAddress(addr1_dup, NODE_NONE), source)); | BOOST_CHECK(!addrman.Add(CAddress(addr1_dup, NODE_NONE), source)); | ||||
BOOST_CHECK(addrman.size() == 1); | BOOST_CHECK_EQUAL(addrman.size(), 1); | ||||
// Test 5: New table has one addr and we add a diff addr we should | // Test: New table has one addr and we add a diff addr we should | ||||
// have two addrs. | // have two addrs. | ||||
CService addr2 = ResolveService("250.1.1.2", 8333); | CService addr2 = ResolveService("250.1.1.2", 8333); | ||||
BOOST_CHECK(addrman.Add(CAddress(addr2, NODE_NONE), source)); | BOOST_CHECK(addrman.Add(CAddress(addr2, NODE_NONE), source)); | ||||
BOOST_CHECK(addrman.size() == 2); | BOOST_CHECK(addrman.size() == 2); | ||||
deadalnix: You forgot that one. | |||||
// Test 6: AddrMan::Clear() should empty the new table. | // Test: AddrMan::Clear() should empty the new table. | ||||
addrman.Clear(); | addrman.Clear(); | ||||
BOOST_CHECK(addrman.size() == 0); | BOOST_CHECK(addrman.size() == 0); | ||||
deadalnixUnsubmitted Not Done Inline Actionsdito deadalnix: dito | |||||
CAddrInfo addr_null2 = addrman.Select(); | CAddrInfo addr_null2 = addrman.Select(); | ||||
BOOST_CHECK(addr_null2.ToString() == "[::]:0"); | BOOST_CHECK(addr_null2.ToString() == "[::]:0"); | ||||
deadalnixUnsubmitted Not Done Inline Actionsdito deadalnix: dito | |||||
// Test 6.5: AddrMan::Add multiple addresses works as expected | // Test: AddrMan::Add multiple addresses works as expected | ||||
std::vector<CAddress> vAddr; | std::vector<CAddress> vAddr; | ||||
vAddr.push_back(CAddress(ResolveService("250.1.1.3", 8333), NODE_NONE)); | vAddr.push_back(CAddress(ResolveService("250.1.1.3", 8333), NODE_NONE)); | ||||
vAddr.push_back(CAddress(ResolveService("250.1.1.4", 8333), NODE_NONE)); | vAddr.push_back(CAddress(ResolveService("250.1.1.4", 8333), NODE_NONE)); | ||||
BOOST_CHECK(addrman.Add(vAddr, source)); | BOOST_CHECK(addrman.Add(vAddr, source)); | ||||
BOOST_CHECK(addrman.size() == 2); | BOOST_CHECK(addrman.size() == 2); | ||||
deadalnixUnsubmitted Not Done Inline Actionsdito deadalnix: dito | |||||
} | } | ||||
BOOST_AUTO_TEST_CASE(addrman_ports) { | BOOST_AUTO_TEST_CASE(addrman_ports) { | ||||
CAddrManTest addrman; | CAddrManTest addrman; | ||||
// Set addrman addr placement to be deterministic. | // Set addrman addr placement to be deterministic. | ||||
addrman.MakeDeterministic(); | addrman.MakeDeterministic(); | ||||
CNetAddr source = ResolveIP("252.2.2.2"); | CNetAddr source = ResolveIP("252.2.2.2"); | ||||
BOOST_CHECK(addrman.size() == 0); | BOOST_CHECK_EQUAL(addrman.size(), 0); | ||||
// Test 7; Addr with same IP but diff port does not replace existing addr. | // Test: Addr with same IP but diff port does not replace existing addr. | ||||
CService addr1 = ResolveService("250.1.1.1", 8333); | CService addr1 = ResolveService("250.1.1.1", 8333); | ||||
addrman.Add(CAddress(addr1, NODE_NONE), source); | addrman.Add(CAddress(addr1, NODE_NONE), source); | ||||
BOOST_CHECK(addrman.size() == 1); | BOOST_CHECK_EQUAL(addrman.size(), 1); | ||||
CService addr1_port = ResolveService("250.1.1.1", 8334); | CService addr1_port = ResolveService("250.1.1.1", 8334); | ||||
addrman.Add(CAddress(addr1_port, NODE_NONE), source); | addrman.Add(CAddress(addr1_port, NODE_NONE), source); | ||||
BOOST_CHECK(addrman.size() == 1); | BOOST_CHECK_EQUAL(addrman.size(), 1); | ||||
CAddrInfo addr_ret2 = addrman.Select(); | CAddrInfo addr_ret2 = addrman.Select(); | ||||
BOOST_CHECK(addr_ret2.ToString() == "250.1.1.1:8333"); | BOOST_CHECK_EQUAL(addr_ret2.ToString(), "250.1.1.1:8333"); | ||||
// Test 8: Add same IP but diff port to tried table, it doesn't get added. | // Test: 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. | // Perhaps this is not ideal behavior but it is the current behavior. | ||||
addrman.Good(CAddress(addr1_port, NODE_NONE)); | addrman.Good(CAddress(addr1_port, NODE_NONE)); | ||||
BOOST_CHECK(addrman.size() == 1); | BOOST_CHECK_EQUAL(addrman.size(), 1); | ||||
bool newOnly = true; | bool newOnly = true; | ||||
CAddrInfo addr_ret3 = addrman.Select(newOnly); | CAddrInfo addr_ret3 = addrman.Select(newOnly); | ||||
BOOST_CHECK(addr_ret3.ToString() == "250.1.1.1:8333"); | BOOST_CHECK_EQUAL(addr_ret3.ToString(), "250.1.1.1:8333"); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(addrman_select) { | BOOST_AUTO_TEST_CASE(addrman_select) { | ||||
CAddrManTest addrman; | CAddrManTest addrman; | ||||
// Set addrman addr placement to be deterministic. | // Set addrman addr placement to be deterministic. | ||||
addrman.MakeDeterministic(); | addrman.MakeDeterministic(); | ||||
CNetAddr source = ResolveIP("252.2.2.2"); | CNetAddr source = ResolveIP("252.2.2.2"); | ||||
// Test 9: Select from new with 1 addr in new. | // Test: Select from new with 1 addr in new. | ||||
CService addr1 = ResolveService("250.1.1.1", 8333); | CService addr1 = ResolveService("250.1.1.1", 8333); | ||||
addrman.Add(CAddress(addr1, NODE_NONE), source); | addrman.Add(CAddress(addr1, NODE_NONE), source); | ||||
BOOST_CHECK(addrman.size() == 1); | BOOST_CHECK_EQUAL(addrman.size(), 1); | ||||
bool newOnly = true; | bool newOnly = true; | ||||
CAddrInfo addr_ret1 = addrman.Select(newOnly); | CAddrInfo addr_ret1 = addrman.Select(newOnly); | ||||
BOOST_CHECK(addr_ret1.ToString() == "250.1.1.1:8333"); | BOOST_CHECK_EQUAL(addr_ret1.ToString(), "250.1.1.1:8333"); | ||||
// Test 10: move addr to tried, select from new expected nothing returned. | // Test: move addr to tried, select from new expected nothing returned. | ||||
addrman.Good(CAddress(addr1, NODE_NONE)); | addrman.Good(CAddress(addr1, NODE_NONE)); | ||||
BOOST_CHECK(addrman.size() == 1); | BOOST_CHECK_EQUAL(addrman.size(), 1); | ||||
CAddrInfo addr_ret2 = addrman.Select(newOnly); | CAddrInfo addr_ret2 = addrman.Select(newOnly); | ||||
BOOST_CHECK(addr_ret2.ToString() == "[::]:0"); | BOOST_CHECK_EQUAL(addr_ret2.ToString(), "[::]:0"); | ||||
CAddrInfo addr_ret3 = addrman.Select(); | CAddrInfo addr_ret3 = addrman.Select(); | ||||
BOOST_CHECK(addr_ret3.ToString() == "250.1.1.1:8333"); | BOOST_CHECK_EQUAL(addr_ret3.ToString(), "250.1.1.1:8333"); | ||||
BOOST_CHECK(addrman.size() == 1); | BOOST_CHECK_EQUAL(addrman.size(), 1); | ||||
// Add three addresses to new table. | // Add three addresses to new table. | ||||
CService addr2 = ResolveService("250.3.1.1", 8333); | CService addr2 = ResolveService("250.3.1.1", 8333); | ||||
CService addr3 = ResolveService("250.3.2.2", 9999); | CService addr3 = ResolveService("250.3.2.2", 9999); | ||||
CService addr4 = ResolveService("250.3.3.3", 9999); | CService addr4 = ResolveService("250.3.3.3", 9999); | ||||
addrman.Add(CAddress(addr2, NODE_NONE), ResolveService("250.3.1.1", 8333)); | addrman.Add(CAddress(addr2, NODE_NONE), ResolveService("250.3.1.1", 8333)); | ||||
addrman.Add(CAddress(addr3, NODE_NONE), ResolveService("250.3.1.1", 8333)); | addrman.Add(CAddress(addr3, NODE_NONE), ResolveService("250.3.1.1", 8333)); | ||||
addrman.Add(CAddress(addr4, NODE_NONE), ResolveService("250.4.1.1", 8333)); | addrman.Add(CAddress(addr4, NODE_NONE), ResolveService("250.4.1.1", 8333)); | ||||
// Add three addresses to tried table. | // Add three addresses to tried table. | ||||
CService addr5 = ResolveService("250.4.4.4", 8333); | CService addr5 = ResolveService("250.4.4.4", 8333); | ||||
CService addr6 = ResolveService("250.4.5.5", 7777); | CService addr6 = ResolveService("250.4.5.5", 7777); | ||||
CService addr7 = ResolveService("250.4.6.6", 8333); | CService addr7 = ResolveService("250.4.6.6", 8333); | ||||
addrman.Add(CAddress(addr5, NODE_NONE), ResolveService("250.3.1.1", 8333)); | addrman.Add(CAddress(addr5, NODE_NONE), ResolveService("250.3.1.1", 8333)); | ||||
addrman.Good(CAddress(addr5, NODE_NONE)); | addrman.Good(CAddress(addr5, NODE_NONE)); | ||||
addrman.Add(CAddress(addr6, NODE_NONE), ResolveService("250.3.1.1", 8333)); | addrman.Add(CAddress(addr6, NODE_NONE), ResolveService("250.3.1.1", 8333)); | ||||
addrman.Good(CAddress(addr6, NODE_NONE)); | addrman.Good(CAddress(addr6, NODE_NONE)); | ||||
addrman.Add(CAddress(addr7, NODE_NONE), ResolveService("250.1.1.3", 8333)); | addrman.Add(CAddress(addr7, NODE_NONE), ResolveService("250.1.1.3", 8333)); | ||||
addrman.Good(CAddress(addr7, NODE_NONE)); | addrman.Good(CAddress(addr7, NODE_NONE)); | ||||
// Test 11: 6 addrs + 1 addr from last test = 7. | // Test: 6 addrs + 1 addr from last test = 7. | ||||
BOOST_CHECK(addrman.size() == 7); | BOOST_CHECK_EQUAL(addrman.size(), 7); | ||||
// Test 12: Select pulls from new and tried regardless of port number. | // Test: Select pulls from new and tried regardless of port number. | ||||
std::set<uint16_t> ports; | std::set<uint16_t> ports; | ||||
for (int i = 0; i < 20; ++i) { | for (int i = 0; i < 20; ++i) { | ||||
ports.insert(addrman.Select().GetPort()); | ports.insert(addrman.Select().GetPort()); | ||||
} | } | ||||
BOOST_CHECK_EQUAL(ports.size(), 3); | BOOST_CHECK_EQUAL(ports.size(), 3); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(addrman_new_collisions) { | BOOST_AUTO_TEST_CASE(addrman_new_collisions) { | ||||
CAddrManTest addrman; | CAddrManTest addrman; | ||||
// Set addrman addr placement to be deterministic. | // Set addrman addr placement to be deterministic. | ||||
addrman.MakeDeterministic(); | addrman.MakeDeterministic(); | ||||
CNetAddr source = ResolveIP("252.2.2.2"); | CNetAddr source = ResolveIP("252.2.2.2"); | ||||
BOOST_CHECK(addrman.size() == 0); | BOOST_CHECK_EQUAL(addrman.size(), 0); | ||||
for (unsigned int i = 1; i < 18; i++) { | for (unsigned int i = 1; i < 18; i++) { | ||||
CService addr = ResolveService("250.1.1." + std::to_string(i)); | CService addr = ResolveService("250.1.1." + std::to_string(i)); | ||||
addrman.Add(CAddress(addr, NODE_NONE), source); | addrman.Add(CAddress(addr, NODE_NONE), source); | ||||
// Test 13: No collision in new table yet. | // Test: No collision in new table yet. | ||||
BOOST_CHECK(addrman.size() == i); | BOOST_CHECK_EQUAL(addrman.size(), i); | ||||
} | } | ||||
// Test 14: new table collision! | // Test: new table collision! | ||||
CService addr1 = ResolveService("250.1.1.18"); | CService addr1 = ResolveService("250.1.1.18"); | ||||
addrman.Add(CAddress(addr1, NODE_NONE), source); | addrman.Add(CAddress(addr1, NODE_NONE), source); | ||||
BOOST_CHECK(addrman.size() == 17); | BOOST_CHECK_EQUAL(addrman.size(), 17); | ||||
CService addr2 = ResolveService("250.1.1.19"); | CService addr2 = ResolveService("250.1.1.19"); | ||||
addrman.Add(CAddress(addr2, NODE_NONE), source); | addrman.Add(CAddress(addr2, NODE_NONE), source); | ||||
BOOST_CHECK(addrman.size() == 18); | BOOST_CHECK_EQUAL(addrman.size(), 18); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(addrman_tried_collisions) { | BOOST_AUTO_TEST_CASE(addrman_tried_collisions) { | ||||
CAddrManTest addrman; | CAddrManTest addrman; | ||||
// Set addrman addr placement to be deterministic. | // Set addrman addr placement to be deterministic. | ||||
addrman.MakeDeterministic(); | addrman.MakeDeterministic(); | ||||
CNetAddr source = ResolveIP("252.2.2.2"); | CNetAddr source = ResolveIP("252.2.2.2"); | ||||
BOOST_CHECK(addrman.size() == 0); | BOOST_CHECK_EQUAL(addrman.size(), 0); | ||||
for (unsigned int i = 1; i < 80; i++) { | for (unsigned int i = 1; i < 80; i++) { | ||||
CService addr = ResolveService("250.1.1." + std::to_string(i)); | CService addr = ResolveService("250.1.1." + std::to_string(i)); | ||||
addrman.Add(CAddress(addr, NODE_NONE), source); | addrman.Add(CAddress(addr, NODE_NONE), source); | ||||
addrman.Good(CAddress(addr, NODE_NONE)); | addrman.Good(CAddress(addr, NODE_NONE)); | ||||
// Test 15: No collision in tried table yet. | // Test: No collision in tried table yet. | ||||
BOOST_CHECK_EQUAL(addrman.size(), i); | BOOST_CHECK_EQUAL(addrman.size(), i); | ||||
} | } | ||||
// Test 16: tried table collision! | // Test: tried table collision! | ||||
CService addr1 = ResolveService("250.1.1.80"); | CService addr1 = ResolveService("250.1.1.80"); | ||||
addrman.Add(CAddress(addr1, NODE_NONE), source); | addrman.Add(CAddress(addr1, NODE_NONE), source); | ||||
BOOST_CHECK(addrman.size() == 79); | BOOST_CHECK_EQUAL(addrman.size(), 79); | ||||
CService addr2 = ResolveService("250.1.1.81"); | CService addr2 = ResolveService("250.1.1.81"); | ||||
addrman.Add(CAddress(addr2, NODE_NONE), source); | addrman.Add(CAddress(addr2, NODE_NONE), source); | ||||
BOOST_CHECK(addrman.size() == 80); | BOOST_CHECK_EQUAL(addrman.size(), 80); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(addrman_find) { | BOOST_AUTO_TEST_CASE(addrman_find) { | ||||
CAddrManTest addrman; | CAddrManTest addrman; | ||||
// Set addrman addr placement to be deterministic. | // Set addrman addr placement to be deterministic. | ||||
addrman.MakeDeterministic(); | addrman.MakeDeterministic(); | ||||
BOOST_CHECK(addrman.size() == 0); | BOOST_CHECK_EQUAL(addrman.size(), 0); | ||||
CAddress addr1 = CAddress(ResolveService("250.1.2.1", 8333), NODE_NONE); | CAddress addr1 = CAddress(ResolveService("250.1.2.1", 8333), NODE_NONE); | ||||
CAddress addr2 = CAddress(ResolveService("250.1.2.1", 9999), NODE_NONE); | CAddress addr2 = CAddress(ResolveService("250.1.2.1", 9999), NODE_NONE); | ||||
CAddress addr3 = CAddress(ResolveService("251.255.2.1", 8333), NODE_NONE); | CAddress addr3 = CAddress(ResolveService("251.255.2.1", 8333), NODE_NONE); | ||||
CNetAddr source1 = ResolveIP("250.1.2.1"); | CNetAddr source1 = ResolveIP("250.1.2.1"); | ||||
CNetAddr source2 = ResolveIP("250.1.2.2"); | CNetAddr source2 = ResolveIP("250.1.2.2"); | ||||
addrman.Add(addr1, source1); | addrman.Add(addr1, source1); | ||||
addrman.Add(addr2, source2); | addrman.Add(addr2, source2); | ||||
addrman.Add(addr3, source1); | addrman.Add(addr3, source1); | ||||
// Test 17: ensure Find returns an IP matching what we searched on. | // Test: ensure Find returns an IP matching what we searched on. | ||||
CAddrInfo *info1 = addrman.Find(addr1); | CAddrInfo *info1 = addrman.Find(addr1); | ||||
BOOST_CHECK(info1); | BOOST_REQUIRE(info1); | ||||
if (info1) BOOST_CHECK(info1->ToString() == "250.1.2.1:8333"); | BOOST_CHECK_EQUAL(info1->ToString(), "250.1.2.1:8333"); | ||||
// Test 18; Find does not discriminate by port number. | // Test: Find does not discriminate by port number. | ||||
CAddrInfo *info2 = addrman.Find(addr2); | CAddrInfo *info2 = addrman.Find(addr2); | ||||
BOOST_CHECK(info2); | BOOST_REQUIRE(info2); | ||||
if (info2 && info1) BOOST_CHECK(info2->ToString() == info1->ToString()); | BOOST_CHECK_EQUAL(info2->ToString(), info1->ToString()); | ||||
// Test 19: Find returns another IP matching what we searched on. | // Test: Find returns another IP matching what we searched on. | ||||
CAddrInfo *info3 = addrman.Find(addr3); | CAddrInfo *info3 = addrman.Find(addr3); | ||||
BOOST_CHECK(info3); | BOOST_REQUIRE(info3); | ||||
if (info3) BOOST_CHECK(info3->ToString() == "251.255.2.1:8333"); | BOOST_CHECK_EQUAL(info3->ToString(), "251.255.2.1:8333"); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(addrman_create) { | BOOST_AUTO_TEST_CASE(addrman_create) { | ||||
CAddrManTest addrman; | CAddrManTest addrman; | ||||
// Set addrman addr placement to be deterministic. | // Set addrman addr placement to be deterministic. | ||||
addrman.MakeDeterministic(); | addrman.MakeDeterministic(); | ||||
BOOST_CHECK(addrman.size() == 0); | BOOST_CHECK_EQUAL(addrman.size(), 0); | ||||
CAddress addr1 = CAddress(ResolveService("250.1.2.1", 8333), NODE_NONE); | CAddress addr1 = CAddress(ResolveService("250.1.2.1", 8333), NODE_NONE); | ||||
CNetAddr source1 = ResolveIP("250.1.2.1"); | CNetAddr source1 = ResolveIP("250.1.2.1"); | ||||
int nId; | int nId; | ||||
CAddrInfo *pinfo = addrman.Create(addr1, source1, &nId); | CAddrInfo *pinfo = addrman.Create(addr1, source1, &nId); | ||||
// Test 20: The result should be the same as the input addr. | // Test: The result should be the same as the input addr. | ||||
BOOST_CHECK(pinfo->ToString() == "250.1.2.1:8333"); | BOOST_CHECK_EQUAL(pinfo->ToString(), "250.1.2.1:8333"); | ||||
CAddrInfo *info2 = addrman.Find(addr1); | CAddrInfo *info2 = addrman.Find(addr1); | ||||
BOOST_CHECK(info2->ToString() == "250.1.2.1:8333"); | BOOST_CHECK_EQUAL(info2->ToString(), "250.1.2.1:8333"); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(addrman_delete) { | BOOST_AUTO_TEST_CASE(addrman_delete) { | ||||
CAddrManTest addrman; | CAddrManTest addrman; | ||||
// Set addrman addr placement to be deterministic. | // Set addrman addr placement to be deterministic. | ||||
addrman.MakeDeterministic(); | addrman.MakeDeterministic(); | ||||
BOOST_CHECK(addrman.size() == 0); | BOOST_CHECK_EQUAL(addrman.size(), 0); | ||||
CAddress addr1 = CAddress(ResolveService("250.1.2.1", 8333), NODE_NONE); | CAddress addr1 = CAddress(ResolveService("250.1.2.1", 8333), NODE_NONE); | ||||
CNetAddr source1 = ResolveIP("250.1.2.1"); | CNetAddr source1 = ResolveIP("250.1.2.1"); | ||||
int nId; | int nId; | ||||
addrman.Create(addr1, source1, &nId); | addrman.Create(addr1, source1, &nId); | ||||
// Test 21: Delete should actually delete the addr. | // Test: Delete should actually delete the addr. | ||||
BOOST_CHECK(addrman.size() == 1); | BOOST_CHECK_EQUAL(addrman.size(), 1); | ||||
addrman.Delete(nId); | addrman.Delete(nId); | ||||
BOOST_CHECK(addrman.size() == 0); | BOOST_CHECK_EQUAL(addrman.size(), 0); | ||||
CAddrInfo *info2 = addrman.Find(addr1); | CAddrInfo *info2 = addrman.Find(addr1); | ||||
BOOST_CHECK(info2 == nullptr); | BOOST_CHECK(info2 == nullptr); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(addrman_getaddr) { | BOOST_AUTO_TEST_CASE(addrman_getaddr) { | ||||
CAddrManTest addrman; | CAddrManTest addrman; | ||||
// Set addrman addr placement to be deterministic. | // Set addrman addr placement to be deterministic. | ||||
addrman.MakeDeterministic(); | addrman.MakeDeterministic(); | ||||
// Test 22: Sanity check, GetAddr should never return anything if addrman | // Test: Sanity check, GetAddr should never return anything if addrman | ||||
// is empty. | // is empty. | ||||
BOOST_CHECK(addrman.size() == 0); | BOOST_CHECK_EQUAL(addrman.size(), 0); | ||||
std::vector<CAddress> vAddr1 = addrman.GetAddr(); | std::vector<CAddress> vAddr1 = addrman.GetAddr(); | ||||
BOOST_CHECK(vAddr1.size() == 0); | BOOST_CHECK_EQUAL(vAddr1.size(), 0); | ||||
CAddress addr1 = CAddress(ResolveService("250.250.2.1", 8333), NODE_NONE); | CAddress addr1 = CAddress(ResolveService("250.250.2.1", 8333), NODE_NONE); | ||||
addr1.nTime = GetAdjustedTime(); // Set time so isTerrible = false | addr1.nTime = GetAdjustedTime(); // Set time so isTerrible = false | ||||
CAddress addr2 = CAddress(ResolveService("250.251.2.2", 9999), NODE_NONE); | CAddress addr2 = CAddress(ResolveService("250.251.2.2", 9999), NODE_NONE); | ||||
addr2.nTime = GetAdjustedTime(); | addr2.nTime = GetAdjustedTime(); | ||||
CAddress addr3 = CAddress(ResolveService("251.252.2.3", 8333), NODE_NONE); | CAddress addr3 = CAddress(ResolveService("251.252.2.3", 8333), NODE_NONE); | ||||
addr3.nTime = GetAdjustedTime(); | addr3.nTime = GetAdjustedTime(); | ||||
CAddress addr4 = CAddress(ResolveService("252.253.3.4", 8333), NODE_NONE); | CAddress addr4 = CAddress(ResolveService("252.253.3.4", 8333), NODE_NONE); | ||||
addr4.nTime = GetAdjustedTime(); | addr4.nTime = GetAdjustedTime(); | ||||
CAddress addr5 = CAddress(ResolveService("252.254.4.5", 8333), NODE_NONE); | CAddress addr5 = CAddress(ResolveService("252.254.4.5", 8333), NODE_NONE); | ||||
addr5.nTime = GetAdjustedTime(); | addr5.nTime = GetAdjustedTime(); | ||||
CNetAddr source1 = ResolveIP("250.1.2.1"); | CNetAddr source1 = ResolveIP("250.1.2.1"); | ||||
CNetAddr source2 = ResolveIP("250.2.3.3"); | CNetAddr source2 = ResolveIP("250.2.3.3"); | ||||
// Test 23: Ensure GetAddr works with new addresses. | // Test: Ensure GetAddr works with new addresses. | ||||
addrman.Add(addr1, source1); | addrman.Add(addr1, source1); | ||||
addrman.Add(addr2, source2); | addrman.Add(addr2, source2); | ||||
addrman.Add(addr3, source1); | addrman.Add(addr3, source1); | ||||
addrman.Add(addr4, source2); | addrman.Add(addr4, source2); | ||||
addrman.Add(addr5, source1); | addrman.Add(addr5, source1); | ||||
// GetAddr returns 23% of addresses, 23% of 5 is 1 rounded down. | // GetAddr returns 23% of addresses, 23% of 5 is 1 rounded down. | ||||
BOOST_CHECK(addrman.GetAddr().size() == 1); | BOOST_CHECK_EQUAL(addrman.GetAddr().size(), 1); | ||||
// Test 24: Ensure GetAddr works with new and tried addresses. | // Test: Ensure GetAddr works with new and tried addresses. | ||||
addrman.Good(CAddress(addr1, NODE_NONE)); | addrman.Good(CAddress(addr1, NODE_NONE)); | ||||
addrman.Good(CAddress(addr2, NODE_NONE)); | addrman.Good(CAddress(addr2, NODE_NONE)); | ||||
BOOST_CHECK(addrman.GetAddr().size() == 1); | BOOST_CHECK_EQUAL(addrman.GetAddr().size(), 1); | ||||
// Test 25: Ensure GetAddr still returns 23% when addrman has many addrs. | // Test: Ensure GetAddr still returns 23% when addrman has many addrs. | ||||
for (unsigned int i = 1; i < (8 * 256); i++) { | for (unsigned int i = 1; i < (8 * 256); i++) { | ||||
int octet1 = i % 256; | int octet1 = i % 256; | ||||
int octet2 = i >> 8 % 256; | int octet2 = i >> 8 % 256; | ||||
std::string strAddr = | std::string strAddr = | ||||
std::to_string(octet1) + "." + std::to_string(octet2) + ".1.23"; | std::to_string(octet1) + "." + std::to_string(octet2) + ".1.23"; | ||||
CAddress addr = CAddress(ResolveService(strAddr), NODE_NONE); | CAddress addr = CAddress(ResolveService(strAddr), NODE_NONE); | ||||
// Ensure that for all addrs in addrman, isTerrible == false. | // Ensure that for all addrs in addrman, isTerrible == false. | ||||
Show All 21 Lines | BOOST_AUTO_TEST_CASE(caddrinfo_get_tried_bucket) { | ||||
CNetAddr source1 = ResolveIP("250.1.1.1"); | CNetAddr source1 = ResolveIP("250.1.1.1"); | ||||
CAddrInfo info1 = CAddrInfo(addr1, source1); | CAddrInfo info1 = CAddrInfo(addr1, source1); | ||||
uint256 nKey1 = (uint256)(CHashWriter(SER_GETHASH, 0) << 1).GetHash(); | uint256 nKey1 = (uint256)(CHashWriter(SER_GETHASH, 0) << 1).GetHash(); | ||||
uint256 nKey2 = (uint256)(CHashWriter(SER_GETHASH, 0) << 2).GetHash(); | uint256 nKey2 = (uint256)(CHashWriter(SER_GETHASH, 0) << 2).GetHash(); | ||||
BOOST_CHECK(info1.GetTriedBucket(nKey1) == 40); | BOOST_CHECK_EQUAL(info1.GetTriedBucket(nKey1), 40); | ||||
// Test 26: Make sure key actually randomizes bucket placement. A fail on | // Test: Make sure key actually randomizes bucket placement. A fail on | ||||
// this test could be a security issue. | // this test could be a security issue. | ||||
BOOST_CHECK(info1.GetTriedBucket(nKey1) != info1.GetTriedBucket(nKey2)); | BOOST_CHECK(info1.GetTriedBucket(nKey1) != info1.GetTriedBucket(nKey2)); | ||||
// Test 27: Two addresses with same IP but different ports can map to | // Test: Two addresses with same IP but different ports can map to | ||||
// different buckets because they have different keys. | // different buckets because they have different keys. | ||||
CAddrInfo info2 = CAddrInfo(addr2, source1); | CAddrInfo info2 = CAddrInfo(addr2, source1); | ||||
BOOST_CHECK(info1.GetKey() != info2.GetKey()); | BOOST_CHECK(info1.GetKey() != info2.GetKey()); | ||||
BOOST_CHECK(info1.GetTriedBucket(nKey1) != info2.GetTriedBucket(nKey1)); | BOOST_CHECK(info1.GetTriedBucket(nKey1) != info2.GetTriedBucket(nKey1)); | ||||
std::set<int> buckets; | std::set<int> buckets; | ||||
for (int i = 0; i < 255; i++) { | for (int i = 0; i < 255; i++) { | ||||
CAddrInfo infoi = CAddrInfo( | CAddrInfo infoi = CAddrInfo( | ||||
CAddress(ResolveService("250.1.1." + std::to_string(i)), NODE_NONE), | CAddress(ResolveService("250.1.1." + std::to_string(i)), NODE_NONE), | ||||
ResolveIP("250.1.1." + std::to_string(i))); | ResolveIP("250.1.1." + std::to_string(i))); | ||||
int bucket = infoi.GetTriedBucket(nKey1); | int bucket = infoi.GetTriedBucket(nKey1); | ||||
buckets.insert(bucket); | buckets.insert(bucket); | ||||
} | } | ||||
// Test 28: IP addresses in the same group (\16 prefix for IPv4) should | // Test: IP addresses in the same group (\16 prefix for IPv4) should | ||||
// never get more than 8 buckets | // never get more than 8 buckets | ||||
BOOST_CHECK(buckets.size() == 8); | BOOST_CHECK_EQUAL(buckets.size(), 8); | ||||
buckets.clear(); | buckets.clear(); | ||||
for (int j = 0; j < 255; j++) { | for (int j = 0; j < 255; j++) { | ||||
CAddrInfo infoj = CAddrInfo( | CAddrInfo infoj = CAddrInfo( | ||||
CAddress(ResolveService("250." + std::to_string(j) + ".1.1"), | CAddress(ResolveService("250." + std::to_string(j) + ".1.1"), | ||||
NODE_NONE), | NODE_NONE), | ||||
ResolveIP("250." + std::to_string(j) + ".1.1")); | ResolveIP("250." + std::to_string(j) + ".1.1")); | ||||
int bucket = infoj.GetTriedBucket(nKey1); | int bucket = infoj.GetTriedBucket(nKey1); | ||||
buckets.insert(bucket); | buckets.insert(bucket); | ||||
} | } | ||||
// Test 29: IP addresses in the different groups should map to more than | // Test: IP addresses in the different groups should map to more than | ||||
// 8 buckets. | // 8 buckets. | ||||
BOOST_CHECK(buckets.size() == 160); | BOOST_CHECK_EQUAL(buckets.size(), 160); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket) { | BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket) { | ||||
CAddrManTest addrman; | CAddrManTest addrman; | ||||
// Set addrman addr placement to be deterministic. | // Set addrman addr placement to be deterministic. | ||||
addrman.MakeDeterministic(); | addrman.MakeDeterministic(); | ||||
CAddress addr1 = CAddress(ResolveService("250.1.2.1", 8333), NODE_NONE); | CAddress addr1 = CAddress(ResolveService("250.1.2.1", 8333), NODE_NONE); | ||||
CAddress addr2 = CAddress(ResolveService("250.1.2.1", 9999), NODE_NONE); | CAddress addr2 = CAddress(ResolveService("250.1.2.1", 9999), NODE_NONE); | ||||
CNetAddr source1 = ResolveIP("250.1.2.1"); | CNetAddr source1 = ResolveIP("250.1.2.1"); | ||||
CAddrInfo info1 = CAddrInfo(addr1, source1); | CAddrInfo info1 = CAddrInfo(addr1, source1); | ||||
uint256 nKey1 = (uint256)(CHashWriter(SER_GETHASH, 0) << 1).GetHash(); | uint256 nKey1 = (uint256)(CHashWriter(SER_GETHASH, 0) << 1).GetHash(); | ||||
uint256 nKey2 = (uint256)(CHashWriter(SER_GETHASH, 0) << 2).GetHash(); | uint256 nKey2 = (uint256)(CHashWriter(SER_GETHASH, 0) << 2).GetHash(); | ||||
// Test 29.5: Make sure the buckets are what we expect | // Test: Make sure the buckets are what we expect | ||||
BOOST_CHECK(info1.GetNewBucket(nKey1) == 786); | BOOST_CHECK_EQUAL(info1.GetNewBucket(nKey1), 786); | ||||
BOOST_CHECK(info1.GetNewBucket(nKey1, source1) == 786); | BOOST_CHECK_EQUAL(info1.GetNewBucket(nKey1, source1), 786); | ||||
// Test 30: Make sure key actually randomizes bucket placement. A fail on | // Test: Make sure key actually randomizes bucket placement. A fail on | ||||
// this test could be a security issue. | // this test could be a security issue. | ||||
BOOST_CHECK(info1.GetNewBucket(nKey1) != info1.GetNewBucket(nKey2)); | BOOST_CHECK(info1.GetNewBucket(nKey1) != info1.GetNewBucket(nKey2)); | ||||
// Test 31: Ports should not effect bucket placement in the addr | // Test: Ports should not effect bucket placement in the addr | ||||
CAddrInfo info2 = CAddrInfo(addr2, source1); | CAddrInfo info2 = CAddrInfo(addr2, source1); | ||||
BOOST_CHECK(info1.GetKey() != info2.GetKey()); | BOOST_CHECK(info1.GetKey() != info2.GetKey()); | ||||
BOOST_CHECK(info1.GetNewBucket(nKey1) == info2.GetNewBucket(nKey1)); | BOOST_CHECK_EQUAL(info1.GetNewBucket(nKey1), info2.GetNewBucket(nKey1)); | ||||
std::set<int> buckets; | std::set<int> buckets; | ||||
for (int i = 0; i < 255; i++) { | for (int i = 0; i < 255; i++) { | ||||
CAddrInfo infoi = CAddrInfo( | CAddrInfo infoi = CAddrInfo( | ||||
CAddress(ResolveService("250.1.1." + std::to_string(i)), NODE_NONE), | CAddress(ResolveService("250.1.1." + std::to_string(i)), NODE_NONE), | ||||
ResolveIP("250.1.1." + std::to_string(i))); | ResolveIP("250.1.1." + std::to_string(i))); | ||||
int bucket = infoi.GetNewBucket(nKey1); | int bucket = infoi.GetNewBucket(nKey1); | ||||
buckets.insert(bucket); | buckets.insert(bucket); | ||||
} | } | ||||
// Test 32: IP addresses in the same group (\16 prefix for IPv4) should | // Test: IP addresses in the same group (\16 prefix for IPv4) should | ||||
// always map to the same bucket. | // always map to the same bucket. | ||||
BOOST_CHECK(buckets.size() == 1); | BOOST_CHECK_EQUAL(buckets.size(), 1); | ||||
buckets.clear(); | buckets.clear(); | ||||
for (int j = 0; j < 4 * 255; j++) { | for (int j = 0; j < 4 * 255; j++) { | ||||
CAddrInfo infoj = CAddrInfo( | CAddrInfo infoj = CAddrInfo( | ||||
CAddress(ResolveService(std::to_string(250 + (j / 255)) + "." + | CAddress(ResolveService(std::to_string(250 + (j / 255)) + "." + | ||||
std::to_string(j % 256) + ".1.1"), | std::to_string(j % 256) + ".1.1"), | ||||
NODE_NONE), | NODE_NONE), | ||||
ResolveIP("251.4.1.1")); | ResolveIP("251.4.1.1")); | ||||
int bucket = infoj.GetNewBucket(nKey1); | int bucket = infoj.GetNewBucket(nKey1); | ||||
buckets.insert(bucket); | buckets.insert(bucket); | ||||
} | } | ||||
// Test 33: IP addresses in the same source groups should map to no more | // Test: IP addresses in the same source groups should map to no more | ||||
// than 64 buckets. | // than 64 buckets. | ||||
BOOST_CHECK(buckets.size() <= 64); | BOOST_CHECK(buckets.size() <= 64); | ||||
buckets.clear(); | buckets.clear(); | ||||
for (int p = 0; p < 255; p++) { | for (int p = 0; p < 255; p++) { | ||||
CAddrInfo infoj = | CAddrInfo infoj = | ||||
CAddrInfo(CAddress(ResolveService("250.1.1.1"), NODE_NONE), | CAddrInfo(CAddress(ResolveService("250.1.1.1"), NODE_NONE), | ||||
ResolveIP("250." + std::to_string(p) + ".1.1")); | ResolveIP("250." + std::to_string(p) + ".1.1")); | ||||
int bucket = infoj.GetNewBucket(nKey1); | int bucket = infoj.GetNewBucket(nKey1); | ||||
buckets.insert(bucket); | buckets.insert(bucket); | ||||
} | } | ||||
// Test 34: IP addresses in the different source groups should map to more | // Test: IP addresses in the different source groups should map to more | ||||
// than 64 buckets. | // than 64 buckets. | ||||
BOOST_CHECK(buckets.size() > 64); | BOOST_CHECK(buckets.size() > 64); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(addrman_selecttriedcollision) { | BOOST_AUTO_TEST_CASE(addrman_selecttriedcollision) { | ||||
CAddrManTest addrman; | CAddrManTest addrman; | ||||
// Set addrman addr placement to be deterministic. | // Set addrman addr placement to be deterministic. | ||||
▲ Show 20 Lines • Show All 144 Lines • Show Last 20 Lines |
You forgot that one.