From 3e93864b4f627fa369d9b54b6596f00d77c81d53 Mon Sep 17 00:00:00 2001 From: Francis Dupont Date: Mon, 10 Jun 2024 10:38:08 +0200 Subject: [PATCH] [#3375] Addressed comments --- src/lib/dhcpsrv/hosts_messages.mes | 2 +- src/lib/dhcpsrv/tests/cfg_hosts_unittest.cc | 40 ++++++++++++- .../tests/mysql_host_data_source_unittest.cc | 26 +++++++++ .../tests/pgsql_host_data_source_unittest.cc | 26 +++++++++ .../generic_host_data_source_unittest.cc | 56 +++++++++++++++++++ .../generic_host_data_source_unittest.h | 8 +++ 6 files changed, 155 insertions(+), 3 deletions(-) diff --git a/src/lib/dhcpsrv/hosts_messages.mes b/src/lib/dhcpsrv/hosts_messages.mes index 93e89b4d77..58ed525082 100644 --- a/src/lib/dhcpsrv/hosts_messages.mes +++ b/src/lib/dhcpsrv/hosts_messages.mes @@ -278,7 +278,7 @@ This debug message is issued when no host was found using the specified subnet id and host identifier. % HOSTS_CFG_UPDATE_ADD add the host for reservations: %1 -This debug message is issued when new host (with reservations) is +This debug message is issued when a new host (with reservations) is added to the server's configuration during an update. The argument describes the host and its reservations in detail. diff --git a/src/lib/dhcpsrv/tests/cfg_hosts_unittest.cc b/src/lib/dhcpsrv/tests/cfg_hosts_unittest.cc index 66ccd54e4d..2076a5cdf7 100644 --- a/src/lib/dhcpsrv/tests/cfg_hosts_unittest.cc +++ b/src/lib/dhcpsrv/tests/cfg_hosts_unittest.cc @@ -72,6 +72,7 @@ public: void testDeleteForIPv4(); void testDeleteForIPv6(); void testDelete2ForIPv6(); + void testDeleteBothForIPv6(); void testDel4(); void testDel6(); void testDeleteAll4(); @@ -719,7 +720,7 @@ TEST_F(CfgHostsTest, deleteForIPv6MultiThreading) { void CfgHostsTest::testDelete2ForIPv6() { CfgHosts cfg; - // Add host with two addresses. + // Add a host with two addresses. IOAddress address1("2001:db8:1::1"); IOAddress address2("2001:db8:2::2"); size_t host_count = 10; @@ -732,7 +733,7 @@ CfgHostsTest::testDelete2ForIPv6() { host->addReservation(IPv6Resrv(IPv6Resrv::TYPE_NA, address2)); cfg.add(host); - // Delete one host using first address. + // Delete the host using its first address. EXPECT_TRUE(cfg.del(subnet_id, address1)); // Check if all addresses were removed. @@ -749,6 +750,41 @@ TEST_F(CfgHostsTest, delete2ForIPv6MultiThreading) { testDelete2ForIPv6(); } +// This test checks that IPv6 address and prefix reservations for the specified +// subnet ID and IPv6 address can be deleted. +void +CfgHostsTest::testDeleteBothForIPv6() { + CfgHosts cfg; + // Add a host with two addresses. + IOAddress address1("2001:db8:1::1"); + IOAddress address2("2001:db8:2::"); + size_t host_count = 10; + SubnetID subnet_id(42); + + HostPtr host = HostPtr(new Host(duids_[0]->toText(), "duid", + SUBNET_ID_UNUSED, subnet_id, + IOAddress::IPV4_ZERO_ADDRESS())); + host->addReservation(IPv6Resrv(IPv6Resrv::TYPE_NA, address1)); + host->addReservation(IPv6Resrv(IPv6Resrv::TYPE_PD, address2, 64)); + cfg.add(host); + + // Delete the host using its address. + EXPECT_TRUE(cfg.del(subnet_id, address1)); + + // Check if all reservations were removed. + EXPECT_FALSE(cfg.get6(subnet_id, address2)); + EXPECT_FALSE(cfg.del(subnet_id, address2)); +} + +TEST_F(CfgHostsTest, deleteBothForIPv6) { + testDeleteBothForIPv6(); +} + +TEST_F(CfgHostsTest, deleteBothForIPv6MultiThreading) { + MultiThreadingTest mt(true); + testDeleteBothForIPv6(); +} + // This test checks that false is returned for deleting the IPv4 reservation // that doesn't exist. TEST_F(CfgHostsTest, deleteForMissingIPv4) { 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 fe1fa67a56..830a4dd96a 100644 --- a/src/lib/dhcpsrv/tests/mysql_host_data_source_unittest.cc +++ b/src/lib/dhcpsrv/tests/mysql_host_data_source_unittest.cc @@ -1443,6 +1443,32 @@ TEST_F(MySqlHostDataSourceTest, deleteById6OptionsMultiThreading) { testDeleteById6Options(); } +// This test verifies that all reservations can be deleted from database +// by providing subnet ID and one address. +TEST_F(MySqlHostDataSourceTest, del2) { + testDelete2ForIPv6(); +} + +// This test verifies that all reservations can be deleted from database +// by providing subnet ID and one address. +TEST_F(MySqlHostDataSourceTest, del2MultiThreading) { + MultiThreadingTest mt(true); + testDelete2ForIPv6(); +} + +// This test verifies that address and PD reservations can be deleted from database +// by providing subnet ID and the address. +TEST_F(MySqlHostDataSourceTest, delBoth) { + testDelete2ForIPv6(); +} + +// This test verifies that address and PD reservations can be deleted from database +// by providing subnet ID and the address. +TEST_F(MySqlHostDataSourceTest, delBothMultiThreading) { + MultiThreadingTest mt(true); + testDelete2ForIPv6(); +} + /// @brief Tests that multiple reservations without IPv4 addresses can be /// specified within a subnet. TEST_F(MySqlHostDataSourceTest, testMultipleHostsNoAddress4) { 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 543ba9f1a7..1f136cffb6 100644 --- a/src/lib/dhcpsrv/tests/pgsql_host_data_source_unittest.cc +++ b/src/lib/dhcpsrv/tests/pgsql_host_data_source_unittest.cc @@ -1411,6 +1411,32 @@ TEST_F(PgSqlHostDataSourceTest, deleteById6OptionsMultiThreading) { testDeleteById6Options(); } +// This test verifies that all reservations can be deleted from database +// by providing subnet ID and one address. +TEST_F(PgSqlHostDataSourceTest, del2) { + testDelete2ForIPv6(); +} + +// This test verifies that all reservations can be deleted from database +// by providing subnet ID and one address. +TEST_F(PgSqlHostDataSourceTest, del2MultiThreading) { + MultiThreadingTest mt(true); + testDelete2ForIPv6(); +} + +// This test verifies that address and PD reservations can be deleted from database +// by providing subnet ID and the address. +TEST_F(PgSqlHostDataSourceTest, delBoth) { + testDelete2ForIPv6(); +} + +// This test verifies that address and PD reservations can be deleted from database +// by providing subnet ID and the address. +TEST_F(PgSqlHostDataSourceTest, delBothMultiThreading) { + MultiThreadingTest mt(true); + testDelete2ForIPv6(); +} + /// @brief Tests that multiple reservations without IPv4 addresses can be /// specified within a subnet. TEST_F(PgSqlHostDataSourceTest, testMultipleHostsNoAddress4) { 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 3b612f4bd0..8fce65f216 100644 --- a/src/lib/dhcpsrv/testutils/generic_host_data_source_unittest.cc +++ b/src/lib/dhcpsrv/testutils/generic_host_data_source_unittest.cc @@ -2619,6 +2619,62 @@ GenericHostDataSourceTest::testDeleteById6Options() { EXPECT_FALSE(result); } +void +GenericHostDataSourceTest::testDelete2ForIPv6() { + // Make sure we have a pointer to the host data source. + ASSERT_TRUE(hdsptr_); + + // Let's create a v6 host... + IOAddress address1("2001:db8:1::1"); + IOAddress address2("2001:db8:2::2"); + SubnetID subnet_id(42); + auto ident = HostDataSourceUtils::generateIdentifier(Host::IDENT_DUID); + HostPtr host = HostPtr(new Host(&ident[0], ident.size(), Host::IDENT_DUID, + SUBNET_ID_UNUSED, subnet_id, + IOAddress::IPV4_ZERO_ADDRESS())); + host->addReservation(IPv6Resrv(IPv6Resrv::TYPE_NA, address1)); + host->addReservation(IPv6Resrv(IPv6Resrv::TYPE_NA, address2)); + // ... and add it to the data source. + ASSERT_NO_THROW(hdsptr_->add(host)); + EXPECT_EQ(2, countDBReservations6()); + + // Delete the host using its first address. + EXPECT_TRUE(hdsptr_->del(subnet_id, address1)); + + // Check if all addresses were removed. + EXPECT_EQ(0, countDBReservations6()); + EXPECT_FALSE(hdsptr_->get6(subnet_id, address2)); + EXPECT_FALSE(hdsptr_->del(subnet_id, address2)); +} + +void +GenericHostDataSourceTest::testDeleteBothForIPv6() { + // Make sure we have a pointer to the host data source. + ASSERT_TRUE(hdsptr_); + + // Let's create a v6 host... + IOAddress address1("2001:db8:1::1"); + IOAddress address2("2001:db8:2::"); + SubnetID subnet_id(42); + auto ident = HostDataSourceUtils::generateIdentifier(Host::IDENT_DUID); + HostPtr host = HostPtr(new Host(&ident[0], ident.size(), Host::IDENT_DUID, + SUBNET_ID_UNUSED, subnet_id, + IOAddress::IPV4_ZERO_ADDRESS())); + host->addReservation(IPv6Resrv(IPv6Resrv::TYPE_NA, address1)); + host->addReservation(IPv6Resrv(IPv6Resrv::TYPE_PD, address2, 64)); + // ... and add it to the data source. + ASSERT_NO_THROW(hdsptr_->add(host)); + EXPECT_EQ(2, countDBReservations6()); + + // Delete the host using its address. + EXPECT_TRUE(hdsptr_->del(subnet_id, address1)); + + // Check if all reservations were removed. + EXPECT_EQ(0, countDBReservations6()); + EXPECT_FALSE(hdsptr_->get6(subnet_id, address2)); + EXPECT_FALSE(hdsptr_->del(subnet_id, address2)); +} + void GenericHostDataSourceTest::testMultipleHostsNoAddress4() { // Make sure we have a pointer to the host data source. 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 8eba40a2c8..944daa602d 100644 --- a/src/lib/dhcpsrv/testutils/generic_host_data_source_unittest.h +++ b/src/lib/dhcpsrv/testutils/generic_host_data_source_unittest.h @@ -508,6 +508,14 @@ public: /// Uses gtest macros to report failures. void testDeleteById6Options(); + /// @brief Tests that two IPv6 reservations for the specified subnet ID + /// and IPv6 address can be deleted. + void testDelete2ForIPv6(); + + /// @brief Tests that IPv6 address and prefix reservations for the specified + /// subnet ID and IPv6 address can be deleted. + void testDeleteBothForIPv6(); + /// @brief Tests that multiple reservations without IPv4 addresses can be /// specified within a subnet. /// -- 2.47.2