aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJon Atack <jon@atack.com>2022-01-26 10:30:35 +0100
committerJon Atack <jon@atack.com>2022-01-26 10:35:15 +0100
commitb7be28cac50046b9f2ddfe63ecafccc80649a36c (patch)
treeb5f97f04af4bdcffe57f4ca88db06ed27dac9511
parent0a1bb84770b403ab5cbd9d5474c76f91ce58e8f6 (diff)
downloadbitcoin-b7be28cac50046b9f2ddfe63ecafccc80649a36c.tar.xz
test: add combined CJDNS/I2P/localhost/onion eviction protection tests
-rw-r--r--src/test/net_peer_eviction_tests.cpp110
1 files changed, 110 insertions, 0 deletions
diff --git a/src/test/net_peer_eviction_tests.cpp b/src/test/net_peer_eviction_tests.cpp
index d064cc7028..6ec3fb0c6b 100644
--- a/src/test/net_peer_eviction_tests.cpp
+++ b/src/test/net_peer_eviction_tests.cpp
@@ -457,6 +457,116 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
/*protected_peer_ids=*/{0, 1, 2, 3, 4, 5, 7, 8, 11, 12, 16, 17},
/*unprotected_peer_ids=*/{6, 9, 10, 13, 14, 15, 18, 19, 20, 21, 22, 23},
random_context));
+
+ // Tests with 4 networks...
+
+ // Combined test: expect having 1 CJDNS, 1 I2P, 1 localhost and 1 onion peer
+ // out of 5 to protect 1 CJDNS, 0 I2P, 0 localhost, 0 onion and 1 other peer
+ // (2 total), sorted by longest uptime; stable sort breaks tie with array
+ // order of CJDNS first.
+ BOOST_CHECK(IsProtected(
+ 5, [](NodeEvictionCandidate& c) {
+ c.m_connected = std::chrono::seconds{c.id};
+ c.m_is_local = (c.id == 3);
+ if (c.id == 4) {
+ c.m_network = NET_CJDNS;
+ } else if (c.id == 1) {
+ c.m_network = NET_I2P;
+ } else if (c.id == 2) {
+ c.m_network = NET_ONION;
+ } else {
+ c.m_network = NET_IPV6;
+ }
+ },
+ /* protected_peer_ids */ {0, 4},
+ /* unprotected_peer_ids */ {1, 2, 3},
+ random_context));
+
+ // Combined test: expect having 1 CJDNS, 1 I2P, 1 localhost and 1 onion peer
+ // out of 7 to protect 1 CJDNS, 0, I2P, 0 localhost, 0 onion and 2 other
+ // peers (3 total) sorted by longest uptime; stable sort breaks tie with
+ // array order of CJDNS first.
+ BOOST_CHECK(IsProtected(
+ 7, [](NodeEvictionCandidate& c) {
+ c.m_connected = std::chrono::seconds{c.id};
+ c.m_is_local = (c.id == 4);
+ if (c.id == 6) {
+ c.m_network = NET_CJDNS;
+ } else if (c.id == 5) {
+ c.m_network = NET_I2P;
+ } else if (c.id == 3) {
+ c.m_network = NET_ONION;
+ } else {
+ c.m_network = NET_IPV4;
+ }
+ },
+ /*protected_peer_ids=*/{0, 1, 6},
+ /*unprotected_peer_ids=*/{2, 3, 4, 5},
+ random_context));
+
+ // Combined test: expect having 1 CJDNS, 1 I2P, 1 localhost and 1 onion peer
+ // out of 8 to protect 1 CJDNS, 1 I2P, 0 localhost, 0 onion and 2 other
+ // peers (4 total) sorted by longest uptime; stable sort breaks tie with
+ // array order of CJDNS first.
+ BOOST_CHECK(IsProtected(
+ 8, [](NodeEvictionCandidate& c) {
+ c.m_connected = std::chrono::seconds{c.id};
+ c.m_is_local = (c.id == 3);
+ if (c.id == 5) {
+ c.m_network = NET_CJDNS;
+ } else if (c.id == 6) {
+ c.m_network = NET_I2P;
+ } else if (c.id == 3) {
+ c.m_network = NET_ONION;
+ } else {
+ c.m_network = NET_IPV6;
+ }
+ },
+ /*protected_peer_ids=*/{0, 1, 5, 6},
+ /*unprotected_peer_ids=*/{2, 3, 4, 7},
+ random_context));
+
+ // Combined test: expect having 2 CJDNS, 2 I2P, 4 localhost, and 2 onion
+ // peers out of 16 to protect 1 CJDNS, 1 I2P, 1 localhost, 1 onion (4/16
+ // total), plus 4 others for 8 total, sorted by longest uptime.
+ BOOST_CHECK(IsProtected(
+ 16, [](NodeEvictionCandidate& c) {
+ c.m_connected = std::chrono::seconds{c.id};
+ c.m_is_local = (c.id > 5);
+ if (c.id == 11 || c.id == 15) {
+ c.m_network = NET_CJDNS;
+ } else if (c.id == 10 || c.id == 14) {
+ c.m_network = NET_I2P;
+ } else if (c.id == 8 || c.id == 9) {
+ c.m_network = NET_ONION;
+ } else {
+ c.m_network = NET_IPV4;
+ }
+ },
+ /*protected_peer_ids=*/{0, 1, 2, 3, 6, 8, 10, 11},
+ /*unprotected_peer_ids=*/{4, 5, 7, 9, 12, 13, 14, 15},
+ random_context));
+
+ // Combined test: expect having 6 CJDNS, 1 I2P, 1 localhost, and 4 onion
+ // peers out of 24 to protect 2 CJDNS, 1 I2P, 1 localhost, and 2 onions (6
+ // total), plus 6 others for 12/24 total, sorted by longest uptime.
+ BOOST_CHECK(IsProtected(
+ 24, [](NodeEvictionCandidate& c) {
+ c.m_connected = std::chrono::seconds{c.id};
+ c.m_is_local = (c.id == 13);
+ if (c.id > 17) {
+ c.m_network = NET_CJDNS;
+ } else if (c.id == 17) {
+ c.m_network = NET_I2P;
+ } else if (c.id == 12 || c.id == 14 || c.id == 15 || c.id == 16) {
+ c.m_network = NET_ONION;
+ } else {
+ c.m_network = NET_IPV6;
+ }
+ },
+ /*protected_peer_ids=*/{0, 1, 2, 3, 4, 5, 12, 13, 14, 17, 18, 19},
+ /*unprotected_peer_ids=*/{6, 7, 8, 9, 10, 11, 15, 16, 20, 21, 22, 23},
+ random_context));
}
// Returns true if any of the node ids in node_ids are selected for eviction.