From: Piotrek Zadroga Date: Wed, 21 Jun 2023 23:23:25 +0000 (+0200) Subject: [#2795] adding new UT for IPv6 prefixes check X-Git-Tag: Kea-2.4.0~90 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0d236b55a5ce40f88f4449f8f9bc52f9e6e04bd0;p=thirdparty%2Fkea.git [#2795] adding new UT for IPv6 prefixes check --- diff --git a/src/lib/dhcpsrv/tests/host_mgr_unittest.cc b/src/lib/dhcpsrv/tests/host_mgr_unittest.cc index 2ade444c06..28ed24ee78 100644 --- a/src/lib/dhcpsrv/tests/host_mgr_unittest.cc +++ b/src/lib/dhcpsrv/tests/host_mgr_unittest.cc @@ -63,6 +63,13 @@ TEST_F(HostMgrTest, getAll6ByIP) { testGetAll6ByIP(*getCfgHosts(), *getCfgHosts()); } +// This test verifies that HostMgr returns all reservations for the +// IPv6 reserved prefix. The reservations are specified in the +// server's configuration. +TEST_F(HostMgrTest, getAll6ByIpPrefix) { + testGetAll6ByIpPrefix(*getCfgHosts(), *getCfgHosts()); +} + // This test verifies that HostMgr returns all reservations for the // specified DHCPv6 subnet. The reservations are defined in the server's // configuration. diff --git a/src/lib/dhcpsrv/tests/mysql_host_data_source_unittest.cc b/src/lib/dhcpsrv/tests/mysql_host_data_source_unittest.cc index 7ab3bc3791..8ad593c936 100644 --- a/src/lib/dhcpsrv/tests/mysql_host_data_source_unittest.cc +++ b/src/lib/dhcpsrv/tests/mysql_host_data_source_unittest.cc @@ -1583,6 +1583,12 @@ TEST_F(MySQLHostMgrTest, getAll6ByIP) { testGetAll6ByIP(*getCfgHosts(), *getCfgHosts()); } +// This test verifies that HostMgr returns all reservations for the +// IPv6 reserved prefix. +TEST_F(MySQLHostMgrTest, getAll6ByIpPrefix) { + testGetAll6ByIpPrefix(*getCfgHosts(), *getCfgHosts()); +} + // This test verifies that reservations for a particular hostname can be // retrieved from the configuration file and a database simultaneously. TEST_F(MySQLHostMgrTest, getAllbyHostname) { diff --git a/src/lib/dhcpsrv/tests/pgsql_host_data_source_unittest.cc b/src/lib/dhcpsrv/tests/pgsql_host_data_source_unittest.cc index 2d30b239ad..561e059089 100644 --- a/src/lib/dhcpsrv/tests/pgsql_host_data_source_unittest.cc +++ b/src/lib/dhcpsrv/tests/pgsql_host_data_source_unittest.cc @@ -1551,6 +1551,12 @@ TEST_F(PgSQLHostMgrTest, getAll6ByIP) { testGetAll6ByIP(*getCfgHosts(), *getCfgHosts()); } +// This test verifies that HostMgr returns all reservations for the +// IPv6 reserved prefix. +TEST_F(PgSQLHostMgrTest, getAll6ByIpPrefix) { + testGetAll6ByIpPrefix(*getCfgHosts(), *getCfgHosts()); +} + // This test verifies that reservations for a particular hostname can be // retrieved from the configuration file and a database simultaneously. TEST_F(PgSQLHostMgrTest, getAllbyHostname) { diff --git a/src/lib/dhcpsrv/testutils/generic_host_data_source_unittest.cc b/src/lib/dhcpsrv/testutils/generic_host_data_source_unittest.cc index 80a83b7359..562109d03b 100644 --- a/src/lib/dhcpsrv/testutils/generic_host_data_source_unittest.cc +++ b/src/lib/dhcpsrv/testutils/generic_host_data_source_unittest.cc @@ -4512,6 +4512,110 @@ HostMgrTest::testGetAll6ByIP(BaseHostDataSource& data_source1, BaseHostDataSourc EXPECT_EQ(0, hosts.size()); } +void +HostMgrTest::testGetAll6ByIpPrefix(BaseHostDataSource& data_source1, + BaseHostDataSource& data_source2) { + // Set the mode of operation with multiple reservations for the same + // IP address. + ASSERT_TRUE(HostMgr::instance().setIPReservationsUnique(false)); + CfgMgr::instance().getStagingCfg()->getCfgHosts()->setIPReservationsUnique(false); + + // Initially, no reservations should be present. + ConstHostCollection hosts = HostMgr::instance().getAll6(SubnetID(1), + IOAddress("2001:db8:1:10::")); + ASSERT_TRUE(hosts.empty()); + + // Prepare vectors of IPv6 prefix reservations for new hosts. + std::vector addresses1; + std::vector addresses2; + addresses1.push_back(IOAddress("2001:db8:1:10::")); + addresses1.push_back(IOAddress("2001:db8:1:11::")); + addresses2.push_back(IOAddress("2001:db8:1:10::")); + addresses2.push_back(IOAddress("2001:db8:1:12::")); + + // Add two hosts for the same subnet with 2 IPv6 prefix reservations per host. + addHost6(data_source1, duids_[0], SubnetID(1), addresses1, 64); + addHost6(data_source2, duids_[1], SubnetID(1), addresses2, 64); + + CfgMgr::instance().commit(); + + // If a non-matching subnet is specified, nothing should be returned. + hosts = HostMgr::instance().getAll6(SubnetID(100), IOAddress("2001:db8:1:10::")); + ASSERT_TRUE(hosts.empty()); + + // For given IP prefix there should be one reservation. + hosts = HostMgr::instance().getAll6(IOAddress("2001:db8:1:11::")); + ASSERT_EQ(1, hosts.size()); + + // For given IP prefix there should be one reservation. + hosts = HostMgr::instance().getAll6(IOAddress("2001:db8:1:12::")); + ASSERT_EQ(1, hosts.size()); + + // For given IP prefix there should be two reservations. + hosts = HostMgr::instance().getAll6(IOAddress("2001:db8:1:10::")); + ASSERT_EQ(2, hosts.size()); + + // Make sure that subnet is correct. + EXPECT_EQ(1, hosts[0]->getIPv6SubnetID()); + EXPECT_EQ(1, hosts[1]->getIPv6SubnetID()); + + // Make sure that all hosts were returned with different identifiers, and + // they have expected reservations. + EXPECT_NE(hosts[0]->getIdentifierAsText(), hosts[1]->getIdentifierAsText()); + EXPECT_TRUE( + hosts[0]->hasReservation(IPv6Resrv(IPv6Resrv::TYPE_PD, IOAddress("2001:db8:1:10::"), 64))); + EXPECT_TRUE( + hosts[0]->hasReservation(IPv6Resrv(IPv6Resrv::TYPE_PD, IOAddress("2001:db8:1:11::"), 64))); + EXPECT_TRUE( + hosts[1]->hasReservation(IPv6Resrv(IPv6Resrv::TYPE_PD, IOAddress("2001:db8:1:10::"), 64))); + EXPECT_TRUE( + hosts[1]->hasReservation(IPv6Resrv(IPv6Resrv::TYPE_PD, IOAddress("2001:db8:1:12::"), 64))); + + // Make sure that the operation target is supported. + bool is_first_source_primary = isPrimaryDataSource(data_source1); + bool is_second_source_primary = isPrimaryDataSource(data_source2); + size_t hosts_in_primary_source = is_first_source_primary + is_second_source_primary; + + // Select hosts only from the primary source. + hosts = HostMgr::instance().getAll6(IOAddress("2001:db8:1:10::"), + HostMgrOperationTarget::PRIMARY_SOURCE); + EXPECT_EQ(hosts_in_primary_source, hosts.size()); + if (is_first_source_primary) { + EXPECT_TRUE(hosts[0]->hasReservation( + IPv6Resrv(IPv6Resrv::TYPE_PD, IOAddress("2001:db8:1:10::"), 64))); + EXPECT_TRUE(hosts[0]->hasReservation( + IPv6Resrv(IPv6Resrv::TYPE_PD, IOAddress("2001:db8:1:11::"), 64))); + } + if (is_second_source_primary) { + EXPECT_TRUE(hosts[hosts_in_primary_source - 1]->hasReservation( + IPv6Resrv(IPv6Resrv::TYPE_PD, IOAddress("2001:db8:1:10::"), 64))); + EXPECT_TRUE(hosts[hosts_in_primary_source - 1]->hasReservation( + IPv6Resrv(IPv6Resrv::TYPE_PD, IOAddress("2001:db8:1:12::"), 64))); + } + + // Select hosts only from the alternate sources. + hosts = HostMgr::instance().getAll6(IOAddress("2001:db8:1:10::"), + HostMgrOperationTarget::ALTERNATE_SOURCES); + EXPECT_EQ(2 - hosts_in_primary_source, hosts.size()); + if (!is_first_source_primary) { + EXPECT_TRUE(hosts[0]->hasReservation( + IPv6Resrv(IPv6Resrv::TYPE_PD, IOAddress("2001:db8:1:10::"), 64))); + EXPECT_TRUE(hosts[0]->hasReservation( + IPv6Resrv(IPv6Resrv::TYPE_PD, IOAddress("2001:db8:1:11::"), 64))); + } + if (!is_second_source_primary) { + EXPECT_TRUE(hosts[2 - hosts_in_primary_source - 1]->hasReservation( + IPv6Resrv(IPv6Resrv::TYPE_PD, IOAddress("2001:db8:1:10::"), 64))); + EXPECT_TRUE(hosts[2 - hosts_in_primary_source - 1]->hasReservation( + IPv6Resrv(IPv6Resrv::TYPE_PD, IOAddress("2001:db8:1:11::"), 64))); + } + + // Select hosts for an unspecified source. + hosts = HostMgr::instance().getAll4(IOAddress("2001:db8:1:10::"), + HostMgrOperationTarget::UNSPECIFIED_SOURCE); + EXPECT_EQ(0, hosts.size()); +} + void HostMgrTest::testAdd(BaseHostDataSource& data_source1, BaseHostDataSource& data_source2) { diff --git a/src/lib/dhcpsrv/testutils/generic_host_data_source_unittest.h b/src/lib/dhcpsrv/testutils/generic_host_data_source_unittest.h index b804e29cdb..6717bbbf7c 100644 --- a/src/lib/dhcpsrv/testutils/generic_host_data_source_unittest.h +++ b/src/lib/dhcpsrv/testutils/generic_host_data_source_unittest.h @@ -965,6 +965,18 @@ protected: void testGetAll6ByIP(BaseHostDataSource& data_source1, BaseHostDataSource& data_source2); + /// @brief This test verifies that HostMgr returns all reservations for the + /// specified IPv6 prefix/es. + /// + /// If reservations are added to different host data sources, it is expected + /// that the @c HostMgr will retrieve reservations from both of them. + /// + /// @param data_source1 Host data source to which first reservation is + /// inserted. + /// @param data_source2 Host data source to which second reservation is + /// inserted. + void testGetAll6ByIpPrefix(BaseHostDataSource& data_source1, BaseHostDataSource& data_source2); + /// @brief This test verifies that HostMgr adds the reservations to any /// data source. ///