]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#2815] Add unit test
authorSlawek Figiel <slawek@isc.org>
Wed, 5 Apr 2023 17:27:26 +0000 (19:27 +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/tests/cfg_hosts_unittest.cc

index 340d818009811f3bfe6e6d230d37fba0ef279995..46c36743ed05cb857baa11f1387cf078819a2c57 100644 (file)
@@ -1087,17 +1087,28 @@ CfgHosts::add6(const HostPtr& host) {
 
 bool
 CfgHosts::del(const SubnetID& subnet_id, const asiolink::IOAddress& addr) {
-    bool erased = false;
+    size_t erased_host_count = 0;
+    size_t erased_reservation_count = 0;
     if (addr.isV4()) {
         HostContainerIndex1& idx = hosts_.get<1>();
-        erased = idx.erase(addr) != 0;
+        erased_host_count = idx.erase(addr);
+        erased_reservation_count = erased_host_count;
     } else {
-        HostContainer6Index1& idx = hosts6_.get<1>();
-        auto range = idx.equal_range(boost::make_tuple(subnet_id, addr));
-        auto eraseIt = idx.erase(range.first, range.second);
-        erased = eraseIt != idx.end();
+        HostContainer6Index1& idx6 = hosts6_.get<1>();
+        HostContainerIndex4& idx = hosts_.get<4>();
+
+        const auto& range = idx6.equal_range(boost::make_tuple(subnet_id, addr));
+        idx6.erase(range.first, range.second);
+
+        for (auto key = range.first; key != range.second; ++key) {
+            erased_reservation_count++;
+            erased_host_count += idx.erase(key->host_->getHostId());
+        }
     }
-    return (erased);
+
+    // ToDo: Log message.
+
+    return (erased_host_count != 0);
 }
 
 size_t
index 7fdb2b042f35797669ca2fb10b9ba47ff18c4888..8682ca28e16a37afe48477eb59186c1505a4cb74 100644 (file)
@@ -456,6 +456,84 @@ TEST_F(CfgHostsTest, getAll4ByAddress) {
     EXPECT_EQ(25, *subnet_ids.rbegin());
 }
 
+// This test checks that the IPv4 reservation for the specified IPv4 address can
+// be deleted.
+TEST_F(CfgHostsTest, deleteForIPv4) {
+    CfgHosts cfg;
+    // Add hosts.
+    IOAddress address("10.0.0.42");
+    SubnetID subnet_id(42);
+    size_t host_count = 10;
+
+    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())));
+    }
+
+    // Get all inserted hosts.
+    HostCollection hostsBySubnet = cfg.getAll4(subnet_id);
+    HostCollection hostsByAddress = cfg.getAll4(address);
+    // Make sure the hosts and IP reservations were added.
+    ASSERT_EQ(host_count, hostsBySubnet.size());
+    ASSERT_EQ(1, hostsByAddress.size());
+    
+    // Delete one host.
+    EXPECT_TRUE(cfg.del(subnet_id, address));
+
+    // Check if the host is actually deleted.
+    hostsBySubnet = cfg.getAll4(subnet_id);
+    hostsByAddress = cfg.getAll4(address);
+    EXPECT_EQ(host_count-1, hostsBySubnet.size());
+    EXPECT_EQ(0, hostsByAddress.size());
+}
+
+// This test checks that the IPv6 reservation for the specified subnet ID and
+// IPv6 address can be deleted.
+TEST_F(CfgHostsTest, deleteForIPv6) {
+    CfgHosts cfg;
+    // Add hosts.
+    IOAddress address("2001:db8:1::1");
+    size_t host_count = 10;
+    SubnetID subnet_id(42);
+
+    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()));
+        host->addReservation(IPv6Resrv(IPv6Resrv::TYPE_NA,
+                                       increase(IOAddress(address), i)));
+        cfg.add(host);
+    }
+    
+
+    // Get all inserted hosts.
+    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(host_count, hostsBySubnet.size());
+    
+    // Delete one host.
+    EXPECT_TRUE(cfg.del(subnet_id, address));
+
+    // Check if the host is actually deleted.
+    hostsBySubnetAndAddress = cfg.getAll6(subnet_id, address);
+    hostsBySubnet = cfg.getAll6(subnet_id);
+    EXPECT_EQ(0, hostsBySubnetAndAddress.size());
+    EXPECT_EQ(host_count-1, hostsBySubnet.size());
+}
+
 // This test checks that all reservations for the specified IPv4 subnet can
 // be deleted.
 TEST_F(CfgHostsTest, deleteAll4) {
@@ -473,7 +551,7 @@ TEST_F(CfgHostsTest, deleteAll4) {
                                  "hw-address",
                                  SubnetID(1 + i % 2), SUBNET_ID_UNUSED,
                                  IOAddress::IPV4_ZERO_ADDRESS(),
-                                 "hostname")));
+                                 s.str())));
     }
 
     // Get all inserted hosts.
@@ -719,7 +797,7 @@ TEST_F(CfgHostsTest, deleteAll6) {
                       reservations.first->second.getPrefix());
 
         } else {
-            // All hosts for subnet id 2 should be gone.
+            // All hosts for subnet id 1 should be gone.
             EXPECT_FALSE(host);
         }
     }