diff --git a/CMakeLists.txt b/CMakeLists.txt index 870cd252..999158f4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -55,6 +55,7 @@ add_executable (test rai/test/entry.cpp rai/test/ledger.cpp rai/test/processor_service.cpp + rai/test/peer_container.cpp rai/test/network.cpp rai/test/uint256_union.cpp rai/test/wallet_test.cpp diff --git a/rai/test/network.cpp b/rai/test/network.cpp index 40e29962..93b34ae3 100644 --- a/rai/test/network.cpp +++ b/rai/test/network.cpp @@ -63,50 +63,6 @@ TEST (network, self_discard) ASSERT_EQ (1, system.clients [0]->network.bad_sender_count); } -TEST (peer_container, empty_peers) -{ - rai::peer_container peers (rai::endpoint {}); - auto list (peers.purge_list (std::chrono::system_clock::now ())); - ASSERT_EQ (0, list.size ()); -} - -TEST (peer_container, no_recontact) -{ - rai::peer_container peers (rai::endpoint {}); - rai::endpoint endpoint1 (boost::asio::ip::address_v4 (0x7f000001), 10000); - ASSERT_EQ (0, peers.size ()); - ASSERT_FALSE (peers.contacting_peer (endpoint1)); - ASSERT_EQ (1, peers.size ()); - ASSERT_TRUE (peers.contacting_peer (endpoint1)); -} - -TEST (peer_container, no_self_incoming) -{ - rai::endpoint self (boost::asio::ip::address_v4 (0x7f000001), 10000); - rai::peer_container peers (self); - peers.incoming_from_peer (self); - ASSERT_TRUE (peers.peers.empty ()); -} - -TEST (peer_container, no_self_contacting) -{ - rai::endpoint self (boost::asio::ip::address_v4 (0x7f000001), 10000); - rai::peer_container peers (self); - peers.contacting_peer (self); - ASSERT_TRUE (peers.peers.empty ()); -} - -TEST (peer_container, old_known) -{ - rai::endpoint self (boost::asio::ip::address_v4 (0x7f000001), 10000); - rai::endpoint other (boost::asio::ip::address_v4 (0x7f000001), 10001); - rai::peer_container peers (self); - peers.contacting_peer (other); - ASSERT_FALSE (peers.known_peer (other)); - peers.incoming_from_peer (other); - ASSERT_TRUE (peers.known_peer (other)); -} - TEST (keepalive_req, deserialize) { rai::keepalive_req message1; @@ -139,69 +95,6 @@ TEST (keepalive_ack, deserialize) ASSERT_EQ (message1.peers, message2.peers); } -TEST (peer_container, reserved_peers_no_contact) -{ - rai::peer_container peers (rai::endpoint {}); - ASSERT_TRUE (peers.contacting_peer (rai::endpoint (boost::asio::ip::address_v4 (0x00000001), 10000))); - ASSERT_TRUE (peers.contacting_peer (rai::endpoint (boost::asio::ip::address_v4 (0xc0000201), 10000))); - ASSERT_TRUE (peers.contacting_peer (rai::endpoint (boost::asio::ip::address_v4 (0xc6336401), 10000))); - ASSERT_TRUE (peers.contacting_peer (rai::endpoint (boost::asio::ip::address_v4 (0xcb007101), 10000))); - ASSERT_TRUE (peers.contacting_peer (rai::endpoint (boost::asio::ip::address_v4 (0xe9fc0001), 10000))); - ASSERT_TRUE (peers.contacting_peer (rai::endpoint (boost::asio::ip::address_v4 (0xf0000001), 10000))); - ASSERT_TRUE (peers.contacting_peer (rai::endpoint (boost::asio::ip::address_v4 (0xffffffff), 10000))); - ASSERT_EQ (0, peers.size ()); -} - -TEST (peer_container, split) -{ - rai::peer_container peers (rai::endpoint {}); - auto now (std::chrono::system_clock::now ()); - rai::endpoint endpoint1 (boost::asio::ip::address_v4::any (), 100); - rai::endpoint endpoint2 (boost::asio::ip::address_v4::any (), 101); - peers.peers.insert ({endpoint1, now - std::chrono::seconds (1), now - std::chrono::seconds (1)}); - peers.peers.insert ({endpoint2, now + std::chrono::seconds (1), now + std::chrono::seconds (1)}); - auto list (peers.purge_list (now)); - ASSERT_EQ (1, list.size ()); - ASSERT_EQ (endpoint2, list [0].endpoint); -} - -TEST (peer_container, fill_random_clear) -{ - rai::peer_container peers (rai::endpoint {}); - std::array target; - std::fill (target.begin (), target.end (), rai::endpoint (boost::asio::ip::address_v4 (0x7f000001), 10000)); - peers.random_fill (target); - ASSERT_TRUE (std::all_of (target.begin (), target.end (), [] (rai::endpoint const & endpoint_a) {return endpoint_a == rai::endpoint (boost::asio::ip::address_v4 (0), 0); })); -} - -TEST (peer_container, fill_random_full) -{ - rai::peer_container peers (rai::endpoint {}); - for (auto i (0); i < 100; ++i) - { - peers.incoming_from_peer (rai::endpoint (boost::asio::ip::address_v4 (0x7f000001), i)); - } - std::array target; - std::fill (target.begin (), target.end (), rai::endpoint (boost::asio::ip::address_v4 (0x7f000001), 10000)); - peers.random_fill (target); - ASSERT_TRUE (std::none_of (target.begin (), target.end (), [] (rai::endpoint const & endpoint_a) {return endpoint_a == rai::endpoint (boost::asio::ip::address_v4 (0x7f000001), 10000); })); -} - -TEST (peer_container, fill_random_part) -{ - rai::peer_container peers (rai::endpoint {}); - for (auto i (0); i < 16; ++i) - { - peers.incoming_from_peer (rai::endpoint (boost::asio::ip::address_v4 (0x7f000001), i + 1)); - } - std::array target; - std::fill (target.begin (), target.end (), rai::endpoint (boost::asio::ip::address_v4 (0x7f000001), 10000)); - peers.random_fill (target); - ASSERT_TRUE (std::none_of (target.begin (), target.begin () + 16, [] (rai::endpoint const & endpoint_a) {return endpoint_a == rai::endpoint (boost::asio::ip::address_v4 (0x7f000001), 10000); })); - ASSERT_TRUE (std::none_of (target.begin (), target.begin () + 16, [] (rai::endpoint const & endpoint_a) {return endpoint_a == rai::endpoint (boost::asio::ip::address_v4 (0x7f000001), 0); })); - ASSERT_TRUE (std::all_of (target.begin () + 16, target.end (), [] (rai::endpoint const & endpoint_a) {return endpoint_a == rai::endpoint (boost::asio::ip::address_v4 (0), 0); })); -} - TEST (network, send_keepalive) { rai::system system (24000, 2); diff --git a/rai/test/peer_container.cpp b/rai/test/peer_container.cpp index dce3cc2a..c91cbec8 100644 --- a/rai/test/peer_container.cpp +++ b/rai/test/peer_container.cpp @@ -1,11 +1,28 @@ #include #include +TEST (peer_container, empty_peers) +{ + rai::peer_container peers (rai::endpoint {}); + auto list (peers.purge_list (std::chrono::system_clock::now ())); + ASSERT_EQ (0, list.size ()); +} + +TEST (peer_container, no_recontact) +{ + rai::peer_container peers (rai::endpoint {}); + rai::endpoint endpoint1 (boost::asio::ip::address_v4 (0x7f000001), 10000); + ASSERT_EQ (0, peers.size ()); + ASSERT_FALSE (peers.contacting_peer (endpoint1)); + ASSERT_EQ (1, peers.size ()); + ASSERT_TRUE (peers.contacting_peer (endpoint1)); +} + TEST (peer_container, no_self_incoming) { rai::endpoint self (boost::asio::ip::address_v4 (0x7f000001), 10000); rai::peer_container peers (self); - ASSERT_FALSE (peers.incoming_from_peer (self)); + peers.incoming_from_peer (self); ASSERT_TRUE (peers.peers.empty ()); } @@ -28,12 +45,65 @@ TEST (peer_container, old_known) ASSERT_TRUE (peers.known_peer (other)); } -TEST (peer_container, exists) +TEST (peer_container, reserved_peers_no_contact) { - rai::endpoint self (boost::asio::ip::address_v4 (0x7f000001), 10000); - rai::endpoint other (boost::asio::ip::address_v4 (0x7f000001), 10001); - rai::peer_container peers (self); - ASSERT_TRUE (peers.incoming_from_peer (other)); - ASSERT_TRUE (peers.known_peer (other)); - ASSERT_FALSE (peers.incoming_from_peer (other)); + rai::peer_container peers (rai::endpoint {}); + ASSERT_TRUE (peers.contacting_peer (rai::endpoint (boost::asio::ip::address_v4 (0x00000001), 10000))); + ASSERT_TRUE (peers.contacting_peer (rai::endpoint (boost::asio::ip::address_v4 (0xc0000201), 10000))); + ASSERT_TRUE (peers.contacting_peer (rai::endpoint (boost::asio::ip::address_v4 (0xc6336401), 10000))); + ASSERT_TRUE (peers.contacting_peer (rai::endpoint (boost::asio::ip::address_v4 (0xcb007101), 10000))); + ASSERT_TRUE (peers.contacting_peer (rai::endpoint (boost::asio::ip::address_v4 (0xe9fc0001), 10000))); + ASSERT_TRUE (peers.contacting_peer (rai::endpoint (boost::asio::ip::address_v4 (0xf0000001), 10000))); + ASSERT_TRUE (peers.contacting_peer (rai::endpoint (boost::asio::ip::address_v4 (0xffffffff), 10000))); + ASSERT_EQ (0, peers.size ()); +} + +TEST (peer_container, split) +{ + rai::peer_container peers (rai::endpoint {}); + auto now (std::chrono::system_clock::now ()); + rai::endpoint endpoint1 (boost::asio::ip::address_v4::any (), 100); + rai::endpoint endpoint2 (boost::asio::ip::address_v4::any (), 101); + peers.peers.insert ({endpoint1, now - std::chrono::seconds (1), now - std::chrono::seconds (1)}); + peers.peers.insert ({endpoint2, now + std::chrono::seconds (1), now + std::chrono::seconds (1)}); + auto list (peers.purge_list (now)); + ASSERT_EQ (1, list.size ()); + ASSERT_EQ (endpoint2, list [0].endpoint); +} + +TEST (peer_container, fill_random_clear) +{ + rai::peer_container peers (rai::endpoint {}); + std::array target; + std::fill (target.begin (), target.end (), rai::endpoint (boost::asio::ip::address_v4 (0x7f000001), 10000)); + peers.random_fill (target); + ASSERT_TRUE (std::all_of (target.begin (), target.end (), [] (rai::endpoint const & endpoint_a) {return endpoint_a == rai::endpoint (boost::asio::ip::address_v4 (0), 0); })); +} + +TEST (peer_container, fill_random_full) +{ + rai::peer_container peers (rai::endpoint {}); + for (auto i (0); i < 100; ++i) + { + peers.incoming_from_peer (rai::endpoint (boost::asio::ip::address_v4 (0x7f000001), i)); + } + std::array target; + std::fill (target.begin (), target.end (), rai::endpoint (boost::asio::ip::address_v4 (0x7f000001), 10000)); + peers.random_fill (target); + ASSERT_TRUE (std::none_of (target.begin (), target.end (), [] (rai::endpoint const & endpoint_a) {return endpoint_a == rai::endpoint (boost::asio::ip::address_v4 (0x7f000001), 10000); })); +} + +TEST (peer_container, fill_random_part) +{ + rai::peer_container peers (rai::endpoint {}); + for (auto i (0); i < 16; ++i) + { + peers.incoming_from_peer (rai::endpoint (boost::asio::ip::address_v4 (0x7f000001), i + 1)); + } + std::array target; + std::fill (target.begin (), target.end (), rai::endpoint (boost::asio::ip::address_v4 (0x7f000001), 10000)); + peers.random_fill (target); + ASSERT_TRUE (std::none_of (target.begin (), target.begin () + 16, [] (rai::endpoint const & endpoint_a) {return endpoint_a == rai::endpoint (boost::asio::ip::address_v4 (0x7f000001), 10000); })); + ASSERT_TRUE (std::none_of (target.begin (), target.begin () + 16, [] (rai::endpoint const & endpoint_a) {return endpoint_a == rai::endpoint (boost::asio::ip::address_v4 (0x7f000001), 0); })); + ASSERT_TRUE (std::all_of (target.begin () + 16, target.end (), [] (rai::endpoint const & endpoint_a) {return endpoint_a == rai::endpoint (boost::asio::ip::address_v4 (0), 0); })); } \ No newline at end of file