Skip to content

Commit

Permalink
Move tests for network functions to a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Mar 25, 2024
1 parent 53a1f95 commit 31e3f12
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 57 deletions.
1 change: 1 addition & 0 deletions nano/core_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
56 changes: 0 additions & 56 deletions nano/core_test/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
59 changes: 59 additions & 0 deletions nano/core_test/network_functions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <nano/node/transport/transport.hpp>
#include <nano/test_common/testutil.hpp>

#include <gtest/gtest.h>

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);
}
2 changes: 1 addition & 1 deletion nano/node/transport/transport.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 &);
}

0 comments on commit 31e3f12

Please sign in to comment.