From: Tomek Mrugalski Date: Fri, 13 Nov 2015 11:00:01 +0000 (+0100) Subject: [3682] Implemented unit-tests for distinguishing hw addr and DUID X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=248708c174bef9f0d0735a60c43631d62fe39bd7;p=thirdparty%2Fkea.git [3682] Implemented unit-tests for distinguishing hw addr and DUID --- diff --git a/src/lib/dhcpsrv/tests/generic_host_data_source_unittest.cc b/src/lib/dhcpsrv/tests/generic_host_data_source_unittest.cc index 13ed4252d9..2d99337572 100644 --- a/src/lib/dhcpsrv/tests/generic_host_data_source_unittest.cc +++ b/src/lib/dhcpsrv/tests/generic_host_data_source_unittest.cc @@ -250,6 +250,24 @@ void GenericHostDataSourceTest::compareHosts(const ConstHostPtr& host1, host2->getClientClasses6()); } +DuidPtr +GenericHostDataSourceTest::HWAddrToDuid(const HWAddrPtr& hwaddr) { + if (!hwaddr) { + return (DuidPtr()); + } + + return (DuidPtr(new DUID(hwaddr->hwaddr_))); +} + +HWAddrPtr +GenericHostDataSourceTest::DuidToHWAddr(const DuidPtr& duid) { + if (!duid) { + return (HWAddrPtr()); + } + + return (HWAddrPtr(new HWAddr(duid->getDuid(), HTYPE_ETHER))); +} + void GenericHostDataSourceTest::compareReservations6(IPv6ResrvRange /*a*/, IPv6ResrvRange /*b*/) { @@ -329,7 +347,7 @@ void GenericHostDataSourceTest::testGetByIPv4(bool hwaddr) { EXPECT_FALSE(hdsptr_->get4(subnet1, IOAddress("192.0.1.5"))); } -void GenericHostDataSourceTest::testGetByHWaddr() { +void GenericHostDataSourceTest::testGetByHWAddr() { // Make sure we have a pointer to the host data source. ASSERT_TRUE(hdsptr_); @@ -387,6 +405,61 @@ void GenericHostDataSourceTest::testGetByClientId() { compareHosts(host2, from_hds2); } +void GenericHostDataSourceTest::testHWAddrNotClientId() { + // Make sure we have a pointer to the host data source. + ASSERT_TRUE(hdsptr_); + + // Create a host with HW address + HostPtr host = initializeHost4("192.0.2.1", true); + ASSERT_TRUE(host->getHWAddress()); + ASSERT_FALSE(host->getDuid()); + + // Try to add both of the to the host data source. + ASSERT_NO_THROW(hdsptr_->add(host)); + + SubnetID subnet = host->getIPv4SubnetID(); + + DuidPtr duid = HWAddrToDuid(host->getHWAddress()); + + // Get the host by HW address (should succeed) + ConstHostPtr by_hwaddr = hdsptr_->get4(subnet, host->getHWAddress(), DuidPtr()); + + // Get the host by DUID (should fail) + ConstHostPtr by_duid = hdsptr_->get4(subnet, HWAddrPtr(), duid); + + // Now let's check if we got what we expected. + EXPECT_TRUE(by_hwaddr); + EXPECT_FALSE(by_duid); +} + +void GenericHostDataSourceTest::testClientIdNotHWAddr() { + // Make sure we have a pointer to the host data source. + ASSERT_TRUE(hdsptr_); + + // Create a host with client-id + HostPtr host = initializeHost4("192.0.2.1", false); + ASSERT_FALSE(host->getHWAddress()); + ASSERT_TRUE(host->getDuid()); + + // Try to add both of the to the host data source. + ASSERT_NO_THROW(hdsptr_->add(host)); + + SubnetID subnet = host->getIPv4SubnetID(); + + HWAddrPtr hwaddr = DuidToHWAddr(host->getDuid()); + + // Get the host by DUID (should succeed) + ConstHostPtr by_duid = hdsptr_->get4(subnet, HWAddrPtr(), host->getDuid()); + + // Get the host by HW address (should fail) + ConstHostPtr by_hwaddr = hdsptr_->get4(subnet, hwaddr, DuidPtr()); + + // Now let's check if we got what we expected. + EXPECT_TRUE(by_duid); + EXPECT_FALSE(by_hwaddr); +} + + }; // namespace test }; // namespace dhcp }; // namespace isc diff --git a/src/lib/dhcpsrv/tests/generic_host_data_source_unittest.h b/src/lib/dhcpsrv/tests/generic_host_data_source_unittest.h index 228f6a4311..9ec22fffa4 100644 --- a/src/lib/dhcpsrv/tests/generic_host_data_source_unittest.h +++ b/src/lib/dhcpsrv/tests/generic_host_data_source_unittest.h @@ -143,12 +143,44 @@ public: /// @brief Test that hosts can be retrieved by hardware address. /// /// Uses gtest macros to report failures. - void testGetByHWaddr(); + void testGetByHWAddr(); /// @brief Test that hosts can be retrieved by client-id /// /// Uses gtest macros to report failures. void testGetByClientId(); + + /// @brief Test that clients with stored HW address can't be retrieved + /// by DUID with the same value. + /// + /// Uses gtest macros to report failures. + void testHWAddrNotClientId(); + + /// @brief Test that clients with stored DUID can't be retrieved + /// by HW address of the same value. + /// + /// Uses gtest macros to report failures. + void testClientIdNotHWAddr(); + + /// @brief Returns DUID with identical content as specified HW address + /// + /// This method does not have any sense in real life and is only useful + /// in testing corner cases in the database backends (e.g. whether the DB + /// is able to tell the difference between hwaddr and duid) + /// + /// @param hwaddr hardware address to be copied + /// @return duid with the same value as specified HW address + DuidPtr HWAddrToDuid(const HWAddrPtr& hwaddr); + + /// @brief Returns HW address with identical content as specified DUID + /// + /// This method does not have any sense in real life and is only useful + /// in testing corner cases in the database backends (e.g. whether the DB + /// is able to tell the difference between hwaddr and duid) + /// + /// @param duid DUID to be copied + /// @return HW address with the same value as specified DUID + HWAddrPtr DuidToHWAddr(const DuidPtr& duid); }; }; // namespace test diff --git a/src/lib/dhcpsrv/tests/mysql_host_data_source_unittest.cc b/src/lib/dhcpsrv/tests/mysql_host_data_source_unittest.cc index dc7bd03b2b..32e5f94610 100644 --- a/src/lib/dhcpsrv/tests/mysql_host_data_source_unittest.cc +++ b/src/lib/dhcpsrv/tests/mysql_host_data_source_unittest.cc @@ -325,7 +325,7 @@ TEST_F(MySqlHostDataSourceTest, getByIPv4ClientId) { // Test verifies if a host reservation can be added and later retrieved by // hardware address. TEST_F(MySqlHostDataSourceTest, getByHWaddr) { - testGetByHWaddr(); + testGetByHWAddr(); } // Test verifies if a host reservation can be added and later retrieved by @@ -336,12 +336,14 @@ TEST_F(MySqlHostDataSourceTest, getByClientId) { // Test verifies if hardware address and client identifier are not confused. TEST_F(MySqlHostDataSourceTest, hwaddrNotClientId1) { + testHWAddrNotClientId(); /// @todo: add host reservation with hardware address X, try to retrieve /// host by client-identifier X, verify that the reservation is not returned. } // Test verifies if hardware address and client identifier are not confused. TEST_F(MySqlHostDataSourceTest, hwaddrNotClientId2) { + testClientIdNotHWAddr(); /// @todo: add host reservation with client identifier X, try to retrieve host /// by hardware address X, verify that the reservation is not returned. }