]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[3682] Two unit-tests implemented.
authorTomek Mrugalski <tomasz@isc.org>
Fri, 13 Nov 2015 08:47:11 +0000 (09:47 +0100)
committerTomek Mrugalski <tomasz@isc.org>
Fri, 13 Nov 2015 08:47:11 +0000 (09:47 +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 ebc9793138126efdb949c0aa0f5577240c5b6e44..a8e19540458e1af7d97bd3aacf54e0469c23edcd 100644 (file)
@@ -36,8 +36,27 @@ GenericHostDataSourceTest::~GenericHostDataSourceTest() {
 
 std::string
 GenericHostDataSourceTest::generateHWAddr() {
-    /// @todo: Make this code return different hwaddress every time.
-    return ("01:02:03:04:05:06");
+
+    static uint8_t hwaddr[] = {65, 66, 67, 68, 69, 70};
+
+
+    stringstream tmp;
+    for (int i = 0; i < sizeof(hwaddr); ++i) {
+        if (i) {
+            tmp << ":";
+        }
+        tmp << std::setw(2) << std::hex << std::setfill('0')
+            << static_cast<unsigned int>(hwaddr[i]);
+    }
+
+    // Increase the address for the next time we use it.
+    // This is primitive, but will work for 65k unique
+    // addresses.
+    hwaddr[0]++;
+    if (hwaddr[0] == 0) {
+        hwaddr[1]++;
+    }
+    return (tmp.str());
 }
 
 std::string
@@ -108,27 +127,49 @@ HostPtr GenericHostDataSourceTest::initializeHost6(std::string address,
     return (host);
 }
 
-void GenericHostDataSourceTest::compareHosts(const ConstHostPtr& host1,
-                                             const ConstHostPtr& host2) {
+void
+GenericHostDataSourceTest::compareHwaddrs(const ConstHostPtr& host1,
+                                          const ConstHostPtr& host2,
+                                          bool expect_match) {
     ASSERT_TRUE(host1);
     ASSERT_TRUE(host2);
 
     // Compare if both have or have not HWaddress set.
     if ((host1->getHWAddress() && !host2->getHWAddress()) ||
         (!host1->getHWAddress() && host2->getHWAddress())) {
-        ADD_FAILURE() << "Host comparison failed: host1 hwaddress="
-                      << host1->getHWAddress() << ", host2 hwaddress="
-                      << host2->getHWAddress();
+
+        // One host has hardware address set while the other has not.
+        // Let's see if it's a problem.
+        if (expect_match) {
+            ADD_FAILURE() << "Host comparison failed: host1 hwaddress="
+                          << host1->getHWAddress() << ", host2 hwaddress="
+                          << host2->getHWAddress();
+        }
         return;
     }
+
+    // Now we know that either both or neither have hw address set.
+    // If host1 has it, we can proceed to value comparison.
     if (host1->getHWAddress()) {
-        // Compare the actual address if they match.
-        EXPECT_TRUE(*host1->getHWAddress() == *host2->getHWAddress());
+
+        if (expect_match) {
+            // Compare the actual address if they match.
+            EXPECT_TRUE(*host1->getHWAddress() == *host2->getHWAddress());
+        } else {
+            EXPECT_FALSE(*host1->getHWAddress() == *host2->getHWAddress());
+        }
         if (*host1->getHWAddress() != *host2->getHWAddress()) {
             cout << host1->getHWAddress()->toText(true) << endl;
             cout << host2->getHWAddress()->toText(true) << endl;
         }
     }
+}
+
+void GenericHostDataSourceTest::compareHosts(const ConstHostPtr& host1,
+                                             const ConstHostPtr& host2) {
+
+    // Let's compare HW addresses and expect match.
+    compareHwaddrs(host1, host2, true);
 
     // comapre if both have or have not DUID set
     if ((host1->getDuid() && !host2->getDuid()) ||
@@ -202,6 +243,79 @@ void GenericHostDataSourceTest::testBasic4() {
     compareHosts(host, from_hds);
 }
 
+void GenericHostDataSourceTest::testGetByIPv4() {
+    // Make sure we have a pointer to the host data source.
+    ASSERT_TRUE(hdsptr_);
+
+    // Let's create a couple of hosts...
+    HostPtr host1 = initializeHost4("192.0.2.1", true);
+    HostPtr host2 = initializeHost4("192.0.2.2", true);
+    HostPtr host3 = initializeHost4("192.0.2.3", true);
+    HostPtr host4 = initializeHost4("192.0.2.4", true);
+
+    // ... and add them to the data source.
+    ASSERT_NO_THROW(hdsptr_->add(host1));
+    ASSERT_NO_THROW(hdsptr_->add(host2));
+    ASSERT_NO_THROW(hdsptr_->add(host3));
+    ASSERT_NO_THROW(hdsptr_->add(host4));
+
+    SubnetID subnet1 = host1->getIPv4SubnetID();
+    SubnetID subnet2 = host2->getIPv4SubnetID();
+    SubnetID subnet3 = host3->getIPv4SubnetID();
+    SubnetID subnet4 = host4->getIPv4SubnetID();
+
+    // And then try to retrieve them back.
+    ConstHostPtr from_hds1 = hdsptr_->get4(subnet1, IOAddress("192.0.2.1"));
+    ConstHostPtr from_hds2 = hdsptr_->get4(subnet2, IOAddress("192.0.2.2"));
+    ConstHostPtr from_hds3 = hdsptr_->get4(subnet3, IOAddress("192.0.2.3"));
+    ConstHostPtr from_hds4 = hdsptr_->get4(subnet4, IOAddress("192.0.2.4"));
+
+    // Make sure we got something back.
+    ASSERT_TRUE(from_hds1);
+    ASSERT_TRUE(from_hds2);
+    ASSERT_TRUE(from_hds3);
+    ASSERT_TRUE(from_hds4);
+
+    // Then let's check that what we got seems correct.
+    compareHosts(host1, from_hds1);
+    compareHosts(host2, from_hds2);
+    compareHosts(host3, from_hds3);
+    compareHosts(host4, from_hds4);
+
+    // Ok, finally let's check that getting by a different address
+    // will not work.
+    EXPECT_FALSE(hdsptr_->get4(subnet1, IOAddress("192.0.1.5")));
+}
+
+void GenericHostDataSourceTest::testGetByHWaddr() {
+    // Make sure we have a pointer to the host data source.
+    ASSERT_TRUE(hdsptr_);
+
+    HostPtr host1 = initializeHost4("192.0.2.1", true);
+    HostPtr host2 = initializeHost4("192.0.2.2", true);
+
+    // Sanity check: make sure the hosts have different HW addresses.
+    ASSERT_TRUE(host1->getHWAddress());
+    ASSERT_TRUE(host2->getHWAddress());
+    compareHwaddrs(host1, host2, false);
+
+    // Try to add both of the to the host data source.
+    ASSERT_NO_THROW(hdsptr_->add(host1));
+    ASSERT_NO_THROW(hdsptr_->add(host2));
+
+    SubnetID subnet1 = host1->getIPv4SubnetID();
+    SubnetID subnet2 = host2->getIPv4SubnetID();
+
+    ConstHostPtr from_hds1 = hdsptr_->get4(subnet1, host1->getHWAddress());
+    ConstHostPtr from_hds2 = hdsptr_->get4(subnet2, host2->getHWAddress());
+
+    // Now let's check if we got what we expected.
+    ASSERT_TRUE(from_hds1);
+    ASSERT_TRUE(from_hds2);
+    compareHosts(host1, from_hds1);
+    compareHosts(host2, from_hds2);
+}
+
 }; // namespace test
 }; // namespace dhcp
 }; // namespace isc
index 1e97111d0a4c6b1deb1ca83419739b1c60eaaa4b..d3bec5c2c47708f7bdb12dc29e1c71e1136cf361 100644 (file)
@@ -69,6 +69,16 @@ public:
     /// @return DUID in textual form acceptable by Host constructor
     std::string generateDuid();
 
+    /// @brief Compares hardware addresses of the two hosts.
+    ///
+    /// This method compares two hwardware address and uses gtest
+    /// macros to signal unexpected (mismatch if expect_match is true;
+    /// match if expect_match is false) values.
+    void
+    compareHwaddrs(const ConstHostPtr& host1,
+                   const ConstHostPtr& host2,
+                   bool expect_match);
+
     /// @brief Compares two hosts
     ///
     /// This method uses gtest macros to signal errors.
@@ -102,6 +112,17 @@ public:
     ///
     /// Uses gtest macros to report failures.
     void testBasic4();
+
+    /// @brief Test inserts several hosts with unique IPv4 address and
+    ///        checks that they can be retrieved properly.
+    ///
+    /// Uses gtest macros to report failures.
+    void testGetByIPv4();
+
+    /// @brief Test that hosts can be retrieved by hardware address.
+    ///
+    /// Uses gtest macros to report failures.
+    void testGetByHWaddr();
 };
 
 }; // namespace test
index d71aba46eb4eac1151775673a4a37e828224a67d..3ae5a101bfd9588c96969c3660e166885d37fce1 100644 (file)
@@ -304,11 +304,14 @@ TEST_F(MySqlHostDataSourceTest, basic4) {
     testBasic4();
 }
 
+TEST_F(MySqlHostDataSourceTest, getByIPv4) {
+    testGetByIPv4();
+}
+
 // Test verifies if a host reservation can be added and later retrieved by
 // hardware address.
-TEST_F(MySqlHostDataSourceTest, hwaddr) {
-    /// @todo: add host reservation with hardware address, retrieve it by
-    /// hardware address and make sure the values are correct.
+TEST_F(MySqlHostDataSourceTest, getByHWaddr) {
+    testGetByHWaddr();
 }
 
 // Test verifies if a host reservation can be added and later retrieved by