| 60 | } |
| 61 | |
| 62 | BOOST_AUTO_TEST_CASE(peer_protection_test) |
| 63 | { |
| 64 | FastRandomContext random_context{true}; |
| 65 | int num_peers{12}; |
| 66 | |
| 67 | // Expect half of the peers with greatest uptime (the lowest m_connected) |
| 68 | // to be protected from eviction. |
| 69 | BOOST_CHECK(IsProtected( |
| 70 | num_peers, [](NodeEvictionCandidate& c) { |
| 71 | c.m_connected = std::chrono::seconds{c.id}; |
| 72 | c.m_is_local = false; |
| 73 | c.m_network = NET_IPV4; |
| 74 | }, |
| 75 | /*protected_peer_ids=*/{0, 1, 2, 3, 4, 5}, |
| 76 | /*unprotected_peer_ids=*/{6, 7, 8, 9, 10, 11}, |
| 77 | random_context)); |
| 78 | |
| 79 | // Verify in the opposite direction. |
| 80 | BOOST_CHECK(IsProtected( |
| 81 | num_peers, [num_peers](NodeEvictionCandidate& c) { |
| 82 | c.m_connected = std::chrono::seconds{num_peers - c.id}; |
| 83 | c.m_is_local = false; |
| 84 | c.m_network = NET_IPV6; |
| 85 | }, |
| 86 | /*protected_peer_ids=*/{6, 7, 8, 9, 10, 11}, |
| 87 | /*unprotected_peer_ids=*/{0, 1, 2, 3, 4, 5}, |
| 88 | random_context)); |
| 89 | |
| 90 | // Test protection of onion, localhost, and I2P peers... |
| 91 | |
| 92 | // Expect 1/4 onion peers to be protected from eviction, |
| 93 | // if no localhost, I2P, or CJDNS peers. |
| 94 | BOOST_CHECK(IsProtected( |
| 95 | num_peers, [](NodeEvictionCandidate& c) { |
| 96 | c.m_is_local = false; |
| 97 | c.m_network = (c.id == 3 || c.id == 8 || c.id == 9) ? NET_ONION : NET_IPV4; |
| 98 | }, |
| 99 | /*protected_peer_ids=*/{3, 8, 9}, |
| 100 | /*unprotected_peer_ids=*/{}, |
| 101 | random_context)); |
| 102 | |
| 103 | // Expect 1/4 onion peers and 1/4 of the other peers to be protected, |
| 104 | // sorted by longest uptime (lowest m_connected), if no localhost, I2P or CJDNS peers. |
| 105 | BOOST_CHECK(IsProtected( |
| 106 | num_peers, [](NodeEvictionCandidate& c) { |
| 107 | c.m_connected = std::chrono::seconds{c.id}; |
| 108 | c.m_is_local = false; |
| 109 | c.m_network = (c.id == 3 || c.id > 7) ? NET_ONION : NET_IPV6; |
| 110 | }, |
| 111 | /*protected_peer_ids=*/{0, 1, 2, 3, 8, 9}, |
| 112 | /*unprotected_peer_ids=*/{4, 5, 6, 7, 10, 11}, |
| 113 | random_context)); |
| 114 | |
| 115 | // Expect 1/4 localhost peers to be protected from eviction, |
| 116 | // if no onion, I2P, or CJDNS peers. |
| 117 | BOOST_CHECK(IsProtected( |
| 118 | num_peers, [](NodeEvictionCandidate& c) { |
| 119 | c.m_is_local = (c.id == 1 || c.id == 9 || c.id == 11); |
nothing calls this directly
no test coverage detected