]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#2815] Add debug message and negative tests
authorSlawek Figiel <slawek@isc.org>
Thu, 6 Apr 2023 10:49:38 +0000 (12:49 +0200)
committerSlawek Figiel <slawek@isc.org>
Thu, 25 May 2023 11:29:28 +0000 (13:29 +0200)
src/lib/dhcpsrv/cfg_hosts.cc
src/lib/dhcpsrv/hosts_messages.cc
src/lib/dhcpsrv/hosts_messages.h
src/lib/dhcpsrv/hosts_messages.mes
src/lib/dhcpsrv/tests/cfg_hosts_unittest.cc

index 46c36743ed05cb857baa11f1387cf078819a2c57..97fb3cb084819898cc14e3aac7f29032aafdef2b 100644 (file)
@@ -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
index 8ede6783b42ff233be1c120fb71b73d57dba4045..0eaec09048864b6ad113eb9c2ee57d2507ef956e 100644 (file)
@@ -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",
index 962af62b2ebbf691eef7bfa9c94244179ffc24cc..937572c83a3ee38a39832fb4a448baed9984bd84 100644 (file)
@@ -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;
index 043c6d05258efd3119f18ef7bc9bff115f313de1..232845deea8923f7bbb86614e7416e0d069d3f18 100644 (file)
@@ -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.
index 8682ca28e16a37afe48477eb59186c1505a4cb74..efb170c490288284f11310aba3a05b2a59a0d11f 100644 (file)
@@ -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) {