]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[3682] Implemented unit-tests for distinguishing hw addr and DUID
authorTomek Mrugalski <tomasz@isc.org>
Fri, 13 Nov 2015 11:00:01 +0000 (12:00 +0100)
committerTomek Mrugalski <tomasz@isc.org>
Fri, 13 Nov 2015 11:00:01 +0000 (12:00 +0100)
src/lib/dhcpsrv/tests/generic_host_data_source_unittest.cc
src/lib/dhcpsrv/tests/generic_host_data_source_unittest.h
src/lib/dhcpsrv/tests/mysql_host_data_source_unittest.cc

index 13ed4252d9362b8446d35039d35052166e9d10a1..2d9933757235d6a837c752225e98df3985d47d8c 100644 (file)
@@ -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
index 228f6a4311c0661739ed6857a6a04276a3119d97..9ec22fffa4efc5f43deb0aeab5dc8710c85bfe08 100644 (file)
@@ -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
index dc7bd03b2b18a35f273b0ed5b04c5ff65c57eeaa..32e5f94610df535b59c4afd952a32b006b58db6d 100644 (file)
@@ -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.
 }