diff options
| author | luigi1111 <luigi1111w@gmail.com> | 2024-12-23 11:04:59 -0500 |
|---|---|---|
| committer | luigi1111 <luigi1111w@gmail.com> | 2024-12-23 11:04:59 -0500 |
| commit | a6f21ca4bd88f87a9167b40ecb53374db969be60 (patch) | |
| tree | 4ca84a7dbb787a55f17b82af1fd6615358e8b204 /tests/unit_tests/node_server.cpp | |
| parent | f498b4d10d7e9a9a01b881c56dd671a312038729 (diff) | |
| parent | a1b545a2f7b2ac48d66e7fd846ca5093286525c7 (diff) | |
| download | monzero-core-a6f21ca4bd88f87a9167b40ecb53374db969be60.tar.gz monzero-core-a6f21ca4bd88f87a9167b40ecb53374db969be60.tar.xz monzero-core-a6f21ca4bd88f87a9167b40ecb53374db969be60.zip | |
Merge pull request #9616
a1b545a p2p: allow comments in banlist files (jeffro256)
Diffstat (limited to 'tests/unit_tests/node_server.cpp')
| -rw-r--r-- | tests/unit_tests/node_server.cpp | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/tests/unit_tests/node_server.cpp b/tests/unit_tests/node_server.cpp index 584f98f7a..6a564e7ed 100644 --- a/tests/unit_tests/node_server.cpp +++ b/tests/unit_tests/node_server.cpp @@ -35,6 +35,7 @@ #include "cryptonote_core/i_core_events.h" #include "cryptonote_protocol/cryptonote_protocol_handler.h" #include "cryptonote_protocol/cryptonote_protocol_handler.inl" +#include "unit_tests_utils.h" #include <condition_variable> #define MAKE_IPV4_ADDRESS(a,b,c,d) epee::net_utils::ipv4_network_address{MAKE_IP(a,b,c,d),0} @@ -114,6 +115,18 @@ static bool is_blocked(Server &server, const epee::net_utils::network_address &a return true; } } + + if (address.get_type_id() != epee::net_utils::address_type::ipv4) + return false; + + const epee::net_utils::ipv4_network_address ipv4_address = address.as<epee::net_utils::ipv4_network_address>(); + + // check if in a blocked ipv4 subnet + const std::map<epee::net_utils::ipv4_network_subnet, time_t> subnets = server.get_blocked_subnets(); + for (const auto &subnet : subnets) + if (subnet.first.matches(ipv4_address)) + return true; + return false; } @@ -266,6 +279,78 @@ TEST(ban, ignores_port) ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS_PORT(1,2,3,4,6))); } +TEST(ban, file_banlist) +{ + test_core pr_core; + cryptonote::t_cryptonote_protocol_handler<test_core> cprotocol(pr_core, NULL); + Server server(cprotocol); + cprotocol.set_p2p_endpoint(&server); + + auto create_node_dir = [](){ + boost::system::error_code ec; + auto path = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path("daemon-%%%%%%%%%%%%%%%%", ec); + if (ec) + return boost::filesystem::path{}; + auto success = boost::filesystem::create_directory(path, ec); + if (!ec && success) + return path; + return boost::filesystem::path{}; + }; + const auto node_dir = create_node_dir(); + ASSERT_TRUE(!node_dir.empty()); + auto auto_remove_node_dir = epee::misc_utils::create_scope_leave_handler([&node_dir](){ + boost::filesystem::remove_all(node_dir); + }); + + boost::program_options::variables_map vm; + boost::program_options::store( + boost::program_options::command_line_parser({ + "--data-dir", + node_dir.string(), + "--ban-list", + (unit_test::data_dir / "node" / "banlist_1.txt").string() + }).options([]{ + boost::program_options::options_description options_description{}; + cryptonote::core::init_options(options_description); + Server::init_options(options_description); + return options_description; + }()).run(), + vm + ); + + ASSERT_TRUE(server.init(vm)); + + // Test cases (look in the banlist_1.txt file) + + // magicfolk + EXPECT_TRUE( is_blocked(server, MAKE_IPV4_ADDRESS_PORT(255,255,255,0,9999)) ); + EXPECT_TRUE( is_blocked(server, MAKE_IPV4_ADDRESS_PORT(128,128,128,0,9999)) ); + EXPECT_TRUE( is_blocked(server, MAKE_IPV4_ADDRESS_PORT(150,75,0,0,9999)) ); + EXPECT_TRUE( is_blocked(server, MAKE_IPV4_ADDRESS_PORT(99,98,0,0,9999)) ); + EXPECT_TRUE( is_blocked(server, MAKE_IPV4_ADDRESS_PORT(99,98,0,255,9999)) ); + EXPECT_TRUE( is_blocked(server, MAKE_IPV4_ADDRESS_PORT(99,98,1,0,9999)) ); + EXPECT_TRUE( is_blocked(server, MAKE_IPV4_ADDRESS_PORT(99,98,1,0,9999)) ); + EXPECT_TRUE( is_blocked(server, MAKE_IPV4_ADDRESS_PORT(99,98,255,255,9999)) ); + EXPECT_FALSE( is_blocked(server, MAKE_IPV4_ADDRESS_PORT(99,99,0,0,9999)) ); + + // personal enemies + EXPECT_TRUE( is_blocked(server, MAKE_IPV4_ADDRESS_PORT(1,2,3,4,9999)) ); + EXPECT_TRUE( is_blocked(server, MAKE_IPV4_ADDRESS_PORT(6,7,8,9,9999)) ); + EXPECT_TRUE( is_blocked(server, MAKE_IPV4_ADDRESS_PORT(1,0,0,7,9999)) ); + EXPECT_TRUE( is_blocked(server, MAKE_IPV4_ADDRESS_PORT(1,0,0,7,9999)) ); + EXPECT_TRUE( is_blocked(server, MAKE_IPV4_ADDRESS_PORT(100,98,1,13,9999)) ); + EXPECT_TRUE( is_blocked(server, MAKE_IPV4_ADDRESS_PORT(100,98,1,0,9999)) ); + EXPECT_TRUE( is_blocked(server, MAKE_IPV4_ADDRESS_PORT(100,98,1,255,9999)) ); + EXPECT_FALSE( is_blocked(server, MAKE_IPV4_ADDRESS_PORT(100,98,2,0,9999)) ); + EXPECT_FALSE( is_blocked(server, MAKE_IPV4_ADDRESS_PORT(100,98,0,255,9999)) ); + + // angel + EXPECT_FALSE( is_blocked(server, MAKE_IPV4_ADDRESS_PORT(007,007,007,007,9999)) ); + + // random IP + EXPECT_FALSE( is_blocked(server, MAKE_IPV4_ADDRESS_PORT(145,036,205,235,9999)) ); +} + TEST(node_server, bind_same_p2p_port) { struct test_data_t |
