]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[3682] Another segfault fix + 2 tests implemented.
authorTomek Mrugalski <tomasz@isc.org>
Mon, 16 Nov 2015 15:27:02 +0000 (16:27 +0100)
committerTomek Mrugalski <tomasz@isc.org>
Mon, 16 Nov 2015 15:27:02 +0000 (16:27 +0100)
src/lib/dhcpsrv/mysql_host_data_source.cc
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 dba704c6c2446e539e2a23ecd06ce23b0e53f229..acd7ab6c4b9bde40143aeb8f01ac4b95ea6a31bf 100644 (file)
@@ -701,7 +701,8 @@ MySqlHostDataSource::getAll(const HWAddrPtr& hwaddr, const DuidPtr& duid) const
        if (duid){
             // DUID
             // set proper dhcp_identifier_type
-            inbind[1].buffer = reinterpret_cast<char*>(1);
+            dhcp_identifier_type = 1; // 1 = IDENT_DUID
+            inbind[1].buffer = reinterpret_cast<char*>(&dhcp_identifier_type);
 
             const vector<uint8_t>& duid_vector = duid->getDuid();
             unsigned long duid_length = duid_vector.size();
@@ -712,8 +713,8 @@ MySqlHostDataSource::getAll(const HWAddrPtr& hwaddr, const DuidPtr& duid) const
             inbind[0].length = &duid_length;
        } else if (hwaddr) {
             // HW Address
-           // set proper dhcp_identifier_type
-           inbind[1].buffer = reinterpret_cast<char*>(0);
+            dhcp_identifier_type = 0; // 0 = IDENT_HWADDR
+            inbind[1].buffer = reinterpret_cast<char*>(&dhcp_identifier_type);
 
             const vector<uint8_t>& hwaddr_vector = hwaddr->hwaddr_;
             unsigned long hwaddr_length = hwaddr_vector.size();
index 132590a41e7821846751599ddba18dde38863e95..555a21ff765bd58854d3f5d4a8eaee6a7151dab8 100644 (file)
@@ -208,9 +208,9 @@ GenericHostDataSourceTest::compareDuids(const ConstHostPtr& host1,
     if (host1->getDuid()) {
 
         if (expect_match) {
-            EXPECT_TRUE(*host1->getDuid() == *host1->getDuid());
+            EXPECT_TRUE(*host1->getDuid() == *host2->getDuid());
         } else {
-            EXPECT_FALSE(*host1->getDuid() == *host1->getDuid());
+            EXPECT_FALSE(*host1->getDuid() == *host2->getDuid());
         }
         if (*host1->getDuid() != *host2->getDuid()) {
             cout << host1->getDuid()->toText() << endl;
@@ -511,6 +511,62 @@ GenericHostDataSourceTest::testHostname(std::string name, int num) {
     }
 }
 
+void
+GenericHostDataSourceTest::testMultipleSubnets(int subnets, bool hwaddr) {
+
+    // Make sure we have a pointer to the host data source.
+    ASSERT_TRUE(hdsptr_);
+
+    HostPtr host = initializeHost4("192.0.2.1", hwaddr);
+
+    for (int i = 0; i < subnets; ++i) {
+        host->setIPv4SubnetID(i + 1000);
+
+        // Check that the same host can have reservations in multiple subnets.
+        EXPECT_NO_THROW(hdsptr_->add(host));
+    }
+
+    // Now check that the reservations can be retrieved by IPv4 address from
+    // each subnet separately.
+    for (int i = 0; i < subnets; ++i) {
+
+        // Try to retrieve the host by IPv4 address.
+        ConstHostPtr from_hds = hdsptr_->get4(i + 1000, host->getIPv4Reservation());
+
+        ASSERT_TRUE(from_hds);
+        EXPECT_EQ(i + 1000, from_hds->getIPv4SubnetID());
+
+        // Try to retrieve the host by either HW address of client-id
+        from_hds = hdsptr_->get4(i + 1000, host->getHWAddress(), host->getDuid());
+        ASSERT_TRUE(from_hds);
+        EXPECT_EQ(i + 1000, from_hds->getIPv4SubnetID());
+    }
+
+    // Now check that they can be retrieved all at once, by IPv4 address.
+    ConstHostCollection all_by_addr = hdsptr_->getAll4(IOAddress("192.0.2.1"));
+    ASSERT_EQ(subnets, all_by_addr.size());
+
+    // Verify that the values returned are proper.
+    int i = 0;
+    for (ConstHostCollection::const_iterator it = all_by_addr.begin();
+         it != all_by_addr.end(); ++it) {
+        EXPECT_EQ(IOAddress("192.0.2.1"), (*it)->getIPv4Reservation());
+        EXPECT_EQ(1000 + i++, (*it)->getIPv4SubnetID());
+    }
+
+    // Finally, check that the hosts can be retrived by HW address or DUID
+    ConstHostCollection all_by_id = hdsptr_->getAll(host->getHWAddress(), host->getDuid());
+    ASSERT_EQ(subnets, all_by_id.size());
+
+    // Check that the returned values are as expected.
+    i = 0;
+    for (ConstHostCollection::const_iterator it = all_by_id.begin();
+         it != all_by_id.end(); ++it) {
+        EXPECT_EQ(IOAddress("192.0.2.1"), (*it)->getIPv4Reservation());
+        EXPECT_EQ(1000 + i++, (*it)->getIPv4SubnetID());
+    }
+}
+
 }; // namespace test
 }; // namespace dhcp
 }; // namespace isc
index 7455d9023ec7338c4067eefa61aa336112917f20..fa3ee427f30d05630ff5d7b11ea05434d69dc18a 100644 (file)
@@ -178,6 +178,15 @@ public:
     /// @param num number of hostnames to be added.
     void testHostname(std::string name, int num);
 
+    /// @brief Test inserts multiple reservations for the same host for different
+    /// subnets and check that they can be retrieved properly.
+    ///
+    /// Uses gtest macros to report failures.
+    ///
+    /// @param subnets number of subnets to test
+    /// @param hwaddr true = use HW address, false = use client-id
+    void testMultipleSubnets(int subnets, bool hwaddr);
+
     /// @brief Returns DUID with identical content as specified HW address
     ///
     /// This method does not have any sense in real life and is only useful
index 77862345b14ff8a6310367749404be79adec2999..4fb825acae03439e814300d12c506355360a5ee0 100644 (file)
@@ -421,15 +421,11 @@ TEST_F(MySqlHostDataSourceTest, DISABLED_multipleClientClassesBoth) {
     /// check that the classes are not confused.
 }
 
-// Test if retrieving hosts by hardware addresses is working correctly.
-TEST_F(MySqlHostDataSourceTest, uniqueHW) {
-    /// @todo: Insert 100 host reservations, each with unique hardware address,
-    /// try to retrieve each and make sure that the correct host is returned.
-}
-
 // Test if the same host can have reservations in different subnets (with the
 // same hardware address)
-TEST_F(MySqlHostDataSourceTest, MultipleSubnetsHWAddr) {
+TEST_F(MySqlHostDataSourceTest, multipleSubnetsHWAddr) {
+    testMultipleSubnets(10, true);
+
     /// @todo: Insert 10 host reservations for a given physical host (the same
     /// hardware address), but for different subnets (different subnet-ids).
     /// Make sure that getAll() returns them all correctly.
@@ -437,18 +433,13 @@ TEST_F(MySqlHostDataSourceTest, MultipleSubnetsHWAddr) {
 
 // Test if the same host can have reservations in different subnets (with the
 // same client identifier)
-TEST_F(MySqlHostDataSourceTest, MultipleSubnetsClientId) {
+TEST_F(MySqlHostDataSourceTest, multipleSubnetsClientId) {
+    testMultipleSubnets(10, false);
     /// @todo: Insert 10 host reservations for a given physical host (the same
     /// client-identifier), but for different subnets (different subnet-ids).
     /// Make sure that getAll() returns them correctly.
 }
 
-// Test if host reservations made for different IPv4 subnets are handled correctly.
-TEST_F(MySqlHostDataSourceTest, subnetId4) {
-    /// @todo: Insert 10 host reservations for different subnets. Make sure that
-    /// get4(subnet-id, ...) calls return correct reservation.
-}
-
 // Test if host reservations made for different IPv6 subnets are handled correctly.
 TEST_F(MySqlHostDataSourceTest, subnetId6) {
     /// @todo: Insert 10 host reservations for different subnets. Make sure that