]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#2815] Add unit test
authorSlawek Figiel <slawek@isc.org>
Tue, 11 Apr 2023 12:32:34 +0000 (14:32 +0200)
committerSlawek Figiel <slawek@isc.org>
Thu, 25 May 2023 11:29:29 +0000 (13:29 +0200)
src/lib/dhcpsrv/testutils/generic_host_data_source_unittest.cc
src/lib/dhcpsrv/testutils/generic_host_data_source_unittest.h

index 0754b3217e87bce3c9402ea733e0f41a88ef9bee..4d1c4f0ebc011a69e13c9f4b34ba9589953bbb80 100644 (file)
@@ -3032,8 +3032,8 @@ HostMgrTest::testGetAll(BaseHostDataSource& data_source1,
     // Look for the first reservation.
     bool found = false;
     for (unsigned i = 0; i < 2; ++i) {
-        if (hosts[0]->getIPv4Reservation() == IOAddress("192.0.2.5")) {
-            ASSERT_EQ(1, hosts[0]->getIPv4SubnetID());
+        if (hosts[i]->getIPv4Reservation() == IOAddress("192.0.2.5")) {
+            ASSERT_EQ(1, hosts[i]->getIPv4SubnetID());
             found = true;
         }
     }
@@ -3045,8 +3045,8 @@ HostMgrTest::testGetAll(BaseHostDataSource& data_source1,
     // Look for the second reservation.
     found = false;
     for (unsigned i = 0; i < 2; ++i) {
-        if (hosts[1]->getIPv4Reservation() == IOAddress("192.0.3.10")) {
-            ASSERT_EQ(10, hosts[1]->getIPv4SubnetID());
+        if (hosts[i]->getIPv4Reservation() == IOAddress("192.0.3.10")) {
+            ASSERT_EQ(10, hosts[i]->getIPv4SubnetID());
             found = true;
         }
     }
@@ -3054,6 +3054,95 @@ HostMgrTest::testGetAll(BaseHostDataSource& data_source1,
         ADD_FAILURE() << "Reservation for the IPv4 address 192.0.3.10"
             " not found using getAll method";
     }
+
+    // Check handling of operation target.
+    bool is_first_source_primary = isPrimaryDataSource(data_source1);
+    bool is_second_source_primary = isPrimaryDataSource(data_source2);
+    size_t reservations_in_primary_source = 0;
+    if (is_first_source_primary) {
+        reservations_in_primary_source += 1;
+    }
+    if (is_second_source_primary) {
+        reservations_in_primary_source += 1;
+    }
+
+    // Primary source target.
+    hosts = HostMgr::instance().getAll(Host::IDENT_HWADDR,
+                                       &hwaddrs_[0]->hwaddr_[0],
+                                       hwaddrs_[0]->hwaddr_.size(),
+                                       HostMgrOperationTarget::PRIMARY_SOURCE);
+    EXPECT_EQ(reservations_in_primary_source, hosts.size());
+    if (is_first_source_primary) {
+        found = false;
+        for (unsigned i = 0; i < 2; ++i) {
+            if (hosts[i]->getIPv4Reservation() == IOAddress("192.0.2.5")) {
+                ASSERT_EQ(1, hosts[i]->getIPv4SubnetID());
+                found = true;
+            }
+        }
+        if (!found) {
+            ADD_FAILURE() << "Reservation for the IPv4 address 192.0.2.5"
+                " not found using getAll method with PRIMARY_SOURCE operation"
+                " target";
+        }
+    }
+    if (is_second_source_primary) {
+        found = false;
+        for (unsigned i = 0; i < 2; ++i) {
+            if (hosts[i]->getIPv4Reservation() == IOAddress("192.0.3.10")) {
+                ASSERT_EQ(10, hosts[i]->getIPv4SubnetID());
+                found = true;
+            }
+        }
+        if (!found) {
+            ADD_FAILURE() << "Reservation for the IPv4 address 192.0.3.10"
+                " not found using getAll method with PRIMARY_SOURCE operation"
+                " target";
+        }
+    }
+
+    // Alternate sources target.
+    hosts = HostMgr::instance().getAll(Host::IDENT_HWADDR,
+                                       &hwaddrs_[0]->hwaddr_[0],
+                                       hwaddrs_[0]->hwaddr_.size(),
+                                       HostMgrOperationTarget::ALTERNATE_SOURCES);
+    EXPECT_EQ(2 - reservations_in_primary_source, hosts.size());
+
+    if (!is_first_source_primary) {
+        found = false;
+        for (unsigned i = 0; i < 2; ++i) {
+            if (hosts[i]->getIPv4Reservation() == IOAddress("192.0.2.5")) {
+                ASSERT_EQ(1, hosts[i]->getIPv4SubnetID());
+                found = true;
+            }
+        }
+        if (!found) {
+            ADD_FAILURE() << "Reservation for the IPv4 address 192.0.2.5"
+                " not found using getAll method with PRIMARY_SOURCE operation"
+                " target";
+        }
+    }
+    if (!is_second_source_primary) {
+        found = false;
+        for (unsigned i = 0; i < 2; ++i) {
+            if (hosts[i]->getIPv4Reservation() == IOAddress("192.0.3.10")) {
+                ASSERT_EQ(10, hosts[i]->getIPv4SubnetID());
+                found = true;
+            }
+        }
+        if (!found) {
+            ADD_FAILURE() << "Reservation for the IPv4 address 192.0.3.10"
+                " not found using getAll method with PRIMARY_SOURCE operation"
+                " target";
+        }
+    }
+
+    // Unspecified source target.
+    hosts = HostMgr::instance().getAll(Host::IDENT_HWADDR,
+                                       &hwaddrs_[0]->hwaddr_[0],
+                                       hwaddrs_[0]->hwaddr_.size(),
+                                       HostMgrOperationTarget::UNSPECIFIED_SOURCE);
+    EXPECT_EQ(0, hosts.size());
 }
 
 void
@@ -3891,6 +3980,11 @@ HostMgrTest::testGetAll6BySubnetIP(BaseHostDataSource& data_source1,
                 IPv6Resrv(IPv6Resrv::TYPE_NA, IOAddress("2001:db8:1::5"))));
 }
 
+bool HostMgrTest::isPrimaryDataSource(const BaseHostDataSource& data_source) const {
+    const auto ptr = dynamic_cast<const CfgHosts*>(&data_source);
+    return ptr != nullptr;
+}
+
 void
 GenericHostDataSourceTest::testUpdate() {
     // Make sure the host data source is initialized.
index c884776b83c9b848f80c9e02ff66dfbac1038d02..8aea9a3e2fad2bbb28d54c6379abbd76b3086480 100644 (file)
@@ -929,6 +929,12 @@ protected:
     void testGetAll6BySubnetIP(BaseHostDataSource& data_source1,
                                BaseHostDataSource& data_source2);
 
+    /// @brief Utility function that returns true if a given data source
+    /// is primary (it isn't an alternate source).
+    /// @param data_source  Host data source to check.
+    /// @return True if the data source is primary. Otherwise, false.
+    bool isPrimaryDataSource(const BaseHostDataSource& data_source) const;
+
     /// @brief HW addresses to be used by the tests.
     std::vector<HWAddrPtr> hwaddrs_;
     /// @brief DUIDs to be used by the tests.