From: Slawek Figiel Date: Tue, 11 Apr 2023 12:32:34 +0000 (+0200) Subject: [#2815] Add unit test X-Git-Tag: Kea-2.3.8~71 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a80b9ef55ff9a8e597e01c25680ff4b141adfb6b;p=thirdparty%2Fkea.git [#2815] Add unit test --- 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 0754b3217e..4d1c4f0ebc 100644 --- a/src/lib/dhcpsrv/testutils/generic_host_data_source_unittest.cc +++ b/src/lib/dhcpsrv/testutils/generic_host_data_source_unittest.cc @@ -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(&data_source); + return ptr != nullptr; +} + void GenericHostDataSourceTest::testUpdate() { // Make sure the host data source is initialized. 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 c884776b83..8aea9a3e2f 100644 --- a/src/lib/dhcpsrv/testutils/generic_host_data_source_unittest.h +++ b/src/lib/dhcpsrv/testutils/generic_host_data_source_unittest.h @@ -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 hwaddrs_; /// @brief DUIDs to be used by the tests.