From 3a1f01a8d2dbce8b5e5bcc2b80f8b0a2826afb61 Mon Sep 17 00:00:00 2001 From: Tomek Mrugalski Date: Mon, 16 Nov 2015 16:27:02 +0100 Subject: [PATCH] [3682] Another segfault fix + 2 tests implemented. --- src/lib/dhcpsrv/mysql_host_data_source.cc | 7 ++- .../generic_host_data_source_unittest.cc | 60 ++++++++++++++++++- .../tests/generic_host_data_source_unittest.h | 9 +++ .../tests/mysql_host_data_source_unittest.cc | 19 ++---- 4 files changed, 76 insertions(+), 19 deletions(-) diff --git a/src/lib/dhcpsrv/mysql_host_data_source.cc b/src/lib/dhcpsrv/mysql_host_data_source.cc index dba704c6c2..acd7ab6c4b 100644 --- a/src/lib/dhcpsrv/mysql_host_data_source.cc +++ b/src/lib/dhcpsrv/mysql_host_data_source.cc @@ -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(1); + dhcp_identifier_type = 1; // 1 = IDENT_DUID + inbind[1].buffer = reinterpret_cast(&dhcp_identifier_type); const vector& 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(0); + dhcp_identifier_type = 0; // 0 = IDENT_HWADDR + inbind[1].buffer = reinterpret_cast(&dhcp_identifier_type); const vector& hwaddr_vector = hwaddr->hwaddr_; unsigned long hwaddr_length = hwaddr_vector.size(); 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 132590a41e..555a21ff76 100644 --- a/src/lib/dhcpsrv/tests/generic_host_data_source_unittest.cc +++ b/src/lib/dhcpsrv/tests/generic_host_data_source_unittest.cc @@ -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 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 7455d9023e..fa3ee427f3 100644 --- a/src/lib/dhcpsrv/tests/generic_host_data_source_unittest.h +++ b/src/lib/dhcpsrv/tests/generic_host_data_source_unittest.h @@ -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 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 77862345b1..4fb825acae 100644 --- a/src/lib/dhcpsrv/tests/mysql_host_data_source_unittest.cc +++ b/src/lib/dhcpsrv/tests/mysql_host_data_source_unittest.cc @@ -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 -- 2.47.2