diff --git a/test/Makefile.am b/test/Makefile.am index f8f546c6..5432dcb5 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -51,6 +51,8 @@ rtorrent_Test_Rpc_SOURCES = $(rtorrent_Test_Common) \ rtorrent_Test_Src_SOURCES = $(rtorrent_Test_Common) \ src/test_command_dynamic.cc \ src/test_command_dynamic.h \ + src/test_command_ip.cc \ + src/test_command_ip.h \ src/test_command_system.cc \ src/test_command_system.h \ src/test_command_path.cc \ diff --git a/test/src/test_command_ip.cc b/test/src/test_command_ip.cc new file mode 100644 index 00000000..9b34f05f --- /dev/null +++ b/test/src/test_command_ip.cc @@ -0,0 +1,55 @@ +#include "config.h" + +#include "test/src/test_command_ip.h" + +#include + +CPPUNIT_TEST_SUITE_REGISTRATION(TestCommandIp); + +bool ipv4_range_parse(const char* address, uint32_t* address_start, uint32_t* address_end); + +static uint32_t +ipv4(uint32_t a, uint32_t b, uint32_t c, uint32_t d) { + return (a << 24) | (b << 16) | (c << 8) | d; +} + +#define RANGE_ASSERT(address, expected_start, expected_end) \ + { \ + uint32_t start = 0; \ + uint32_t end = 0; \ + \ + CPPUNIT_ASSERT(ipv4_range_parse(address, &start, &end)); \ + CPPUNIT_ASSERT_EQUAL(expected_start, start); \ + CPPUNIT_ASSERT_EQUAL(expected_end, end); \ + } + +void +TestCommandIp::test_single_address() { + RANGE_ASSERT("10.1.2.3", ipv4(10, 1, 2, 3), ipv4(10, 1, 2, 3)); +} + +void +TestCommandIp::test_explicit_range() { + RANGE_ASSERT("10.1.2.3-10.1.2.9", ipv4(10, 1, 2, 3), ipv4(10, 1, 2, 9)); +} + +void +TestCommandIp::test_cidr() { + RANGE_ASSERT("10.0.0.0/8", ipv4(10, 0, 0, 0), ipv4(10, 255, 255, 255)); + RANGE_ASSERT("10.1.2.0/24", ipv4(10, 1, 2, 0), ipv4(10, 1, 2, 255)); + RANGE_ASSERT("10.1.2.128/25", ipv4(10, 1, 2, 128), ipv4(10, 1, 2, 255)); + RANGE_ASSERT("10.1.2.3/31", ipv4(10, 1, 2, 2), ipv4(10, 1, 2, 3)); +} + +void +TestCommandIp::test_cidr_zero_mask() { + RANGE_ASSERT("0.0.0.0/0", ipv4(0, 0, 0, 0), ipv4(255, 255, 255, 255)); + RANGE_ASSERT("10.1.2.3/0", ipv4(0, 0, 0, 0), ipv4(255, 255, 255, 255)); +} + +void +TestCommandIp::test_cidr_full_mask() { + RANGE_ASSERT("10.1.2.3/32", ipv4(10, 1, 2, 3), ipv4(10, 1, 2, 3)); + RANGE_ASSERT("0.0.0.0/32", ipv4(0, 0, 0, 0), ipv4(0, 0, 0, 0)); + RANGE_ASSERT("255.255.255.255/32", ipv4(255, 255, 255, 255), ipv4(255, 255, 255, 255)); +} diff --git a/test/src/test_command_ip.h b/test/src/test_command_ip.h new file mode 100644 index 00000000..9d45a71b --- /dev/null +++ b/test/src/test_command_ip.h @@ -0,0 +1,25 @@ +#include +#include + +// ipv4_range_parse is a pure function, so this does not use test_fixture and +// the mock and logging setup that comes with it. +class TestCommandIp : public CppUnit::TestFixture { + CPPUNIT_TEST_SUITE(TestCommandIp); + + CPPUNIT_TEST(test_single_address); + CPPUNIT_TEST(test_explicit_range); + + CPPUNIT_TEST(test_cidr); + CPPUNIT_TEST(test_cidr_zero_mask); + CPPUNIT_TEST(test_cidr_full_mask); + + CPPUNIT_TEST_SUITE_END(); + +public: + void test_single_address(); + void test_explicit_range(); + + void test_cidr(); + void test_cidr_zero_mask(); + void test_cidr_full_mask(); +};