From: Slawek Figiel Date: Thu, 6 Apr 2023 10:49:38 +0000 (+0200) Subject: [#2815] Add debug message and negative tests X-Git-Tag: Kea-2.3.8~80 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1cd39e72f1db40c1403d637f42baa59b526a9bb3;p=thirdparty%2Fkea.git [#2815] Add debug message and negative tests --- diff --git a/src/lib/dhcpsrv/cfg_hosts.cc b/src/lib/dhcpsrv/cfg_hosts.cc index 46c36743ed..97fb3cb084 100644 --- a/src/lib/dhcpsrv/cfg_hosts.cc +++ b/src/lib/dhcpsrv/cfg_hosts.cc @@ -1087,28 +1087,35 @@ CfgHosts::add6(const HostPtr& host) { bool CfgHosts::del(const SubnetID& subnet_id, const asiolink::IOAddress& addr) { - size_t erased_host_count = 0; - size_t erased_reservation_count = 0; + size_t erased_hosts = 0; + size_t erased_addresses = 0; if (addr.isV4()) { + // Delete IPv4 reservation and host. HostContainerIndex1& idx = hosts_.get<1>(); - erased_host_count = idx.erase(addr); - erased_reservation_count = erased_host_count; + erased_hosts = idx.erase(addr); + erased_addresses = erased_hosts; } else { HostContainer6Index1& idx6 = hosts6_.get<1>(); HostContainerIndex4& idx = hosts_.get<4>(); + // Delete IPv6 reservations. const auto& range = idx6.equal_range(boost::make_tuple(subnet_id, addr)); idx6.erase(range.first, range.second); + erased_addresses = boost::distance(range.first, range.second); + // Delete hosts. for (auto key = range.first; key != range.second; ++key) { - erased_reservation_count++; - erased_host_count += idx.erase(key->host_->getHostId()); + erased_hosts += idx.erase(key->host_->getHostId()); } } - // ToDo: Log message. + LOG_DEBUG(hosts_logger, HOSTS_DBG_TRACE, HOSTS_CFG_DEL) + .arg(erased_hosts) + .arg(erased_addresses) + .arg(subnet_id) + .arg(addr.toText()); - return (erased_host_count != 0); + return (erased_hosts != 0); } size_t diff --git a/src/lib/dhcpsrv/hosts_messages.cc b/src/lib/dhcpsrv/hosts_messages.cc index 8ede6783b4..0eaec09048 100644 --- a/src/lib/dhcpsrv/hosts_messages.cc +++ b/src/lib/dhcpsrv/hosts_messages.cc @@ -13,6 +13,7 @@ extern const isc::log::MessageID HOSTS_BACKEND_REGISTER = "HOSTS_BACKEND_REGISTE extern const isc::log::MessageID HOSTS_CFG_ADD_HOST = "HOSTS_CFG_ADD_HOST"; extern const isc::log::MessageID HOSTS_CFG_CACHE_HOST_DATA_SOURCE = "HOSTS_CFG_CACHE_HOST_DATA_SOURCE"; extern const isc::log::MessageID HOSTS_CFG_CLOSE_HOST_DATA_SOURCE = "HOSTS_CFG_CLOSE_HOST_DATA_SOURCE"; +extern const isc::log::MessageID HOSTS_CFG_DEL = "HOSTS_CFG_DEL"; extern const isc::log::MessageID HOSTS_CFG_DEL_ALL_SUBNET4 = "HOSTS_CFG_DEL_ALL_SUBNET4"; extern const isc::log::MessageID HOSTS_CFG_DEL_ALL_SUBNET6 = "HOSTS_CFG_DEL_ALL_SUBNET6"; extern const isc::log::MessageID HOSTS_CFG_GET_ALL = "HOSTS_CFG_GET_ALL"; @@ -85,6 +86,7 @@ const char* values[] = { "HOSTS_CFG_ADD_HOST", "add the host for reservations: %1", "HOSTS_CFG_CACHE_HOST_DATA_SOURCE", "get host cache data source: %1", "HOSTS_CFG_CLOSE_HOST_DATA_SOURCE", "Closing host data source: %1", + "HOSTS_CFG_DEL", "deleted %1 host(s) having %2 IPv6 reservation(s) for subnet id %3 and address %4", "HOSTS_CFG_DEL_ALL_SUBNET4", "deleted all %1 host(s) for subnet id %2", "HOSTS_CFG_DEL_ALL_SUBNET6", "deleted all %1 host(s) having %2 IPv6 reservation(s) for subnet id %3", "HOSTS_CFG_GET_ALL", "get all hosts with reservations", diff --git a/src/lib/dhcpsrv/hosts_messages.h b/src/lib/dhcpsrv/hosts_messages.h index 962af62b2e..937572c83a 100644 --- a/src/lib/dhcpsrv/hosts_messages.h +++ b/src/lib/dhcpsrv/hosts_messages.h @@ -14,6 +14,7 @@ extern const isc::log::MessageID HOSTS_BACKEND_REGISTER; extern const isc::log::MessageID HOSTS_CFG_ADD_HOST; extern const isc::log::MessageID HOSTS_CFG_CACHE_HOST_DATA_SOURCE; extern const isc::log::MessageID HOSTS_CFG_CLOSE_HOST_DATA_SOURCE; +extern const isc::log::MessageID HOSTS_CFG_DEL; extern const isc::log::MessageID HOSTS_CFG_DEL_ALL_SUBNET4; extern const isc::log::MessageID HOSTS_CFG_DEL_ALL_SUBNET6; extern const isc::log::MessageID HOSTS_CFG_GET_ALL; diff --git a/src/lib/dhcpsrv/hosts_messages.mes b/src/lib/dhcpsrv/hosts_messages.mes index 043c6d0525..232845deea 100644 --- a/src/lib/dhcpsrv/hosts_messages.mes +++ b/src/lib/dhcpsrv/hosts_messages.mes @@ -31,6 +31,13 @@ detected by the host manager. This is a normal message being printed when the server closes host data source connection. +% HOSTS_CFG_DEL deleted %1 host(s) having %2 IPv6 reservation(s) for subnet id %3 and address %4 +This debug message is issued when reservations are deleted for the specified +subnet and address. The first argument specifies how many hosts have been +deleted. The second argument specifies how many reservations have been deleted. +The third argument is the subnet identifier. The fourth argument is the IP +address. + % HOSTS_CFG_DEL_ALL_SUBNET4 deleted all %1 host(s) for subnet id %2 This debug message is issued when all IPv4 reservations are deleted for the specified subnet. The first argument specifies how many reservations @@ -40,8 +47,8 @@ have been deleted. The second argument is the subnet identifier. This debug message is issued when all IPv6 reservations are deleted for the specified subnet. The first argument specifies how many hosts have been deleted. The second argument specifies how many IPv6 -(addresses and prefixes) have been deleted. The third argument is the -subnet identifier. +(addresses and prefixes) reservations have been deleted. The third argument is +the subnet identifier. % HOSTS_CFG_GET_ALL get all hosts with reservations This debug message is issued when starting to retrieve all hosts. diff --git a/src/lib/dhcpsrv/tests/cfg_hosts_unittest.cc b/src/lib/dhcpsrv/tests/cfg_hosts_unittest.cc index 8682ca28e1..efb170c490 100644 --- a/src/lib/dhcpsrv/tests/cfg_hosts_unittest.cc +++ b/src/lib/dhcpsrv/tests/cfg_hosts_unittest.cc @@ -467,13 +467,10 @@ TEST_F(CfgHostsTest, deleteForIPv4) { for (size_t i = 0; i < host_count; i++) { - std::ostringstream s; - s << "hostname" << i; - cfg.add(HostPtr(new Host(hwaddrs_[i]->toText(false), "hw-address", subnet_id, SUBNET_ID_UNUSED, - increase(address, i), s.str()))); + increase(address, i)))); } // Get all inserted hosts. @@ -504,13 +501,9 @@ TEST_F(CfgHostsTest, deleteForIPv6) { for (size_t i = 0; i < host_count; i++) { - std::ostringstream s; - s << "hostname" << i; - HostPtr host = HostPtr(new Host(duids_[i]->toText(), "duid", SUBNET_ID_UNUSED, subnet_id, - IOAddress("0.0.0.0"), - s.str())); + IOAddress("0.0.0.0"))); host->addReservation(IPv6Resrv(IPv6Resrv::TYPE_NA, increase(IOAddress(address), i))); cfg.add(host); @@ -521,7 +514,7 @@ TEST_F(CfgHostsTest, deleteForIPv6) { auto hostsBySubnetAndAddress = cfg.getAll6(subnet_id, address); auto hostsBySubnet = cfg.getAll6(subnet_id); // Make sure the hosts and IP reservations were added. - EXPECT_EQ(1, hostsBySubnetAndAddress.size()); + ASSERT_EQ(1, hostsBySubnetAndAddress.size()); ASSERT_EQ(host_count, hostsBySubnet.size()); // Delete one host. @@ -534,6 +527,24 @@ TEST_F(CfgHostsTest, deleteForIPv6) { EXPECT_EQ(host_count-1, hostsBySubnet.size()); } +// This test checks that false is returned for deleting the IPv4 reservation +// that doesn't exist. +TEST_F(CfgHostsTest, deleteForMissingIPv4) { + CfgHosts cfg; + + // Delete non-existent host. + EXPECT_FALSE(cfg.del(SubnetID(42), IOAddress(("10.0.0.42")))); +} + +// This test checks that false is returned for deleting the IPv6 reservation +// that doesn't exist. +TEST_F(CfgHostsTest, deleteForMissingIPv6) { + CfgHosts cfg; + + // Delete non-existent host. + EXPECT_FALSE(cfg.del(SubnetID(42), IOAddress(("2001:db8:1::1")))); +} + // This test checks that all reservations for the specified IPv4 subnet can // be deleted. TEST_F(CfgHostsTest, deleteAll4) {