diff --git a/nano/core_test/CMakeLists.txt b/nano/core_test/CMakeLists.txt index 23c2274428..ca3c44efee 100644 --- a/nano/core_test/CMakeLists.txt +++ b/nano/core_test/CMakeLists.txt @@ -31,6 +31,7 @@ add_executable( memory_pool.cpp network.cpp network_filter.cpp + network_functions.cpp node.cpp object_stream.cpp optimistic_scheduler.cpp diff --git a/nano/core_test/network.cpp b/nano/core_test/network.cpp index 085dee927a..1cfa357f27 100644 --- a/nano/core_test/network.cpp +++ b/nano/core_test/network.cpp @@ -577,62 +577,6 @@ TEST (network, endpoint_bad_fd) ASSERT_TIMELY_EQ (10s, system.nodes[0]->network.endpoint ().port (), 0); } -TEST (network, reserved_address) -{ - nano::test::system system (1); - // 0 port test - ASSERT_TRUE (nano::transport::reserved_address (nano::endpoint (boost::asio::ip::make_address_v6 ("2001::"), 0))); - // Valid address test - ASSERT_FALSE (nano::transport::reserved_address (nano::endpoint (boost::asio::ip::make_address_v6 ("2001::"), 1))); - nano::endpoint loopback (boost::asio::ip::make_address_v6 ("::1"), 1); - ASSERT_FALSE (nano::transport::reserved_address (loopback)); - nano::endpoint private_network_peer (boost::asio::ip::make_address_v6 ("::ffff:10.0.0.0"), 1); - ASSERT_TRUE (nano::transport::reserved_address (private_network_peer, false)); - ASSERT_FALSE (nano::transport::reserved_address (private_network_peer, true)); -} - -TEST (network, ipv6_bind_subnetwork) -{ - auto address1 (boost::asio::ip::make_address_v6 ("a41d:b7b2:8298:cf45:672e:bd1a:e7fb:f713")); - auto subnet1 (boost::asio::ip::make_network_v6 (address1, 48)); - ASSERT_EQ (boost::asio::ip::make_address_v6 ("a41d:b7b2:8298::"), subnet1.network ()); - auto address1_subnet (nano::transport::ipv4_address_or_ipv6_subnet (address1)); - ASSERT_EQ (subnet1.network (), address1_subnet); - // Ipv4 should return initial address - auto address2 (boost::asio::ip::make_address_v6 ("::ffff:192.168.1.1")); - auto address2_subnet (nano::transport::ipv4_address_or_ipv6_subnet (address2)); - ASSERT_EQ (address2, address2_subnet); -} - -TEST (network, network_range_ipv6) -{ - auto address1 (boost::asio::ip::make_address_v6 ("a41d:b7b2:8298:cf45:672e:bd1a:e7fb:f713")); - auto subnet1 (boost::asio::ip::make_network_v6 (address1, 58)); - ASSERT_EQ (boost::asio::ip::make_address_v6 ("a41d:b7b2:8298:cf40::"), subnet1.network ()); - auto address2 (boost::asio::ip::make_address_v6 ("520d:2402:3d:5e65:11:f8:7c54:3f")); - auto subnet2 (boost::asio::ip::make_network_v6 (address2, 33)); - ASSERT_EQ (boost::asio::ip::make_address_v6 ("520d:2402:0::"), subnet2.network ()); - // Default settings test - auto address3 (boost::asio::ip::make_address_v6 ("a719:0f12:536e:d88a:1331:ba53:4598:04e5")); - auto subnet3 (boost::asio::ip::make_network_v6 (address3, 32)); - ASSERT_EQ (boost::asio::ip::make_address_v6 ("a719:0f12::"), subnet3.network ()); - auto address3_subnet (nano::transport::map_address_to_subnetwork (address3)); - ASSERT_EQ (subnet3.network (), address3_subnet); -} - -TEST (network, network_range_ipv4) -{ - auto address1 (boost::asio::ip::make_address_v6 ("::ffff:192.168.1.1")); - auto subnet1 (boost::asio::ip::make_network_v6 (address1, 96 + 16)); - ASSERT_EQ (boost::asio::ip::make_address_v6 ("::ffff:192.168.0.0"), subnet1.network ()); - // Default settings test - auto address2 (boost::asio::ip::make_address_v6 ("::ffff:80.67.148.225")); - auto subnet2 (boost::asio::ip::make_network_v6 (address2, 96 + 24)); - ASSERT_EQ (boost::asio::ip::make_address_v6 ("::ffff:80.67.148.0"), subnet2.network ()); - auto address2_subnet (nano::transport::map_address_to_subnetwork (address2)); - ASSERT_EQ (subnet2.network (), address2_subnet); -} - TEST (node, port_mapping) { nano::test::system system (1); diff --git a/nano/core_test/network_functions.cpp b/nano/core_test/network_functions.cpp new file mode 100644 index 0000000000..d0a8ae5b22 --- /dev/null +++ b/nano/core_test/network_functions.cpp @@ -0,0 +1,59 @@ +#include +#include + +#include + +TEST (network_functions, reserved_address) +{ + // 0 port test + ASSERT_TRUE (nano::transport::reserved_address (nano::endpoint (boost::asio::ip::make_address_v6 ("2001::"), 0))); + // Valid address test + ASSERT_FALSE (nano::transport::reserved_address (nano::endpoint (boost::asio::ip::make_address_v6 ("2001::"), 1))); + nano::endpoint loopback (boost::asio::ip::make_address_v6 ("::1"), 1); + ASSERT_FALSE (nano::transport::reserved_address (loopback)); + nano::endpoint private_network_peer (boost::asio::ip::make_address_v6 ("::ffff:10.0.0.0"), 1); + ASSERT_TRUE (nano::transport::reserved_address (private_network_peer, false)); + ASSERT_FALSE (nano::transport::reserved_address (private_network_peer, true)); +} + +TEST (network_functions, ipv6_bind_subnetwork) +{ + auto address1 (boost::asio::ip::make_address_v6 ("a41d:b7b2:8298:cf45:672e:bd1a:e7fb:f713")); + auto subnet1 (boost::asio::ip::make_network_v6 (address1, 48)); + ASSERT_EQ (boost::asio::ip::make_address_v6 ("a41d:b7b2:8298::"), subnet1.network ()); + auto address1_subnet (nano::transport::ipv4_address_or_ipv6_subnet (address1)); + ASSERT_EQ (subnet1.network (), address1_subnet); + // Ipv4 should return initial address + auto address2 (boost::asio::ip::make_address_v6 ("::ffff:192.168.1.1")); + auto address2_subnet (nano::transport::ipv4_address_or_ipv6_subnet (address2)); + ASSERT_EQ (address2, address2_subnet); +} + +TEST (network_functions, network_range_ipv6) +{ + auto address1 (boost::asio::ip::make_address_v6 ("a41d:b7b2:8298:cf45:672e:bd1a:e7fb:f713")); + auto subnet1 (boost::asio::ip::make_network_v6 (address1, 58)); + ASSERT_EQ (boost::asio::ip::make_address_v6 ("a41d:b7b2:8298:cf40::"), subnet1.network ()); + auto address2 (boost::asio::ip::make_address_v6 ("520d:2402:3d:5e65:11:f8:7c54:3f")); + auto subnet2 (boost::asio::ip::make_network_v6 (address2, 33)); + ASSERT_EQ (boost::asio::ip::make_address_v6 ("520d:2402:0::"), subnet2.network ()); + // Default settings test + auto address3 (boost::asio::ip::make_address_v6 ("a719:0f12:536e:d88a:1331:ba53:4598:04e5")); + auto subnet3 (boost::asio::ip::make_network_v6 (address3, 32)); + ASSERT_EQ (boost::asio::ip::make_address_v6 ("a719:0f12::"), subnet3.network ()); + auto address3_subnet (nano::transport::map_address_to_subnetwork (address3)); + ASSERT_EQ (subnet3.network (), address3_subnet); +} + +TEST (network_functions, network_range_ipv4) +{ + auto address1 (boost::asio::ip::make_address_v6 ("::ffff:192.168.1.1")); + auto subnet1 (boost::asio::ip::make_network_v6 (address1, 96 + 16)); + ASSERT_EQ (boost::asio::ip::make_address_v6 ("::ffff:192.168.0.0"), subnet1.network ()); + // Default settings test + auto address2 (boost::asio::ip::make_address_v6 ("::ffff:80.67.148.225")); + auto subnet2 (boost::asio::ip::make_network_v6 (address2, 96 + 24)); + ASSERT_EQ (boost::asio::ip::make_address_v6 ("::ffff:80.67.148.0"), subnet2.network ()); + auto address2_subnet (nano::transport::map_address_to_subnetwork (address2)); + ASSERT_EQ (subnet2.network (), address2_subnet); +} diff --git a/nano/node/transport/transport.hpp b/nano/node/transport/transport.hpp index 523e9d5e37..88a225b64e 100644 --- a/nano/node/transport/transport.hpp +++ b/nano/node/transport/transport.hpp @@ -23,7 +23,7 @@ bool is_same_ip (boost::asio::ip::address const &, boost::asio::ip::address cons bool is_same_subnetwork (boost::asio::ip::address const &, boost::asio::ip::address const &); // Unassigned, reserved, self -bool reserved_address (nano::endpoint const &, bool = false); +bool reserved_address (nano::endpoint const &, bool allow_local_peers = false); bool is_temporary_error (boost::system::error_code const &); } \ No newline at end of file