From 64e29fd98666c0618d0d0b156938c40de6d203f6 Mon Sep 17 00:00:00 2001 From: Tomek Mrugalski Date: Tue, 20 Oct 2015 18:38:14 +0200 Subject: [PATCH] [3682] Host comparison implemented in unit-tests. --- src/lib/dhcpsrv/mysql_host_data_source.cc | 3 +- .../generic_host_data_source_unittest.cc | 63 ++++++++++++++++++- .../tests/generic_host_data_source_unittest.h | 18 ++++++ 3 files changed, 80 insertions(+), 4 deletions(-) diff --git a/src/lib/dhcpsrv/mysql_host_data_source.cc b/src/lib/dhcpsrv/mysql_host_data_source.cc index 7bcb80fc14..864cf9e513 100644 --- a/src/lib/dhcpsrv/mysql_host_data_source.cc +++ b/src/lib/dhcpsrv/mysql_host_data_source.cc @@ -796,9 +796,10 @@ MySqlHostDataSource::get4(const SubnetID& subnet_id, // Set up the WHERE clause value MYSQL_BIND inbind[2]; + uint32_t subnet = subnet_id; memset(inbind, 0, sizeof(inbind)); inbind[0].buffer_type = MYSQL_TYPE_LONG; - inbind[0].buffer = reinterpret_cast(subnet_id); + inbind[0].buffer = reinterpret_cast(&subnet); inbind[0].is_unsigned = MLM_TRUE; uint32_t addr4 = static_cast(address); 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 61da447a19..ebc9793138 100644 --- a/src/lib/dhcpsrv/tests/generic_host_data_source_unittest.cc +++ b/src/lib/dhcpsrv/tests/generic_host_data_source_unittest.cc @@ -114,13 +114,70 @@ void GenericHostDataSourceTest::compareHosts(const ConstHostPtr& host1, ASSERT_TRUE(host2); // Compare if both have or have not HWaddress set. - ASSERT_EQ(host1->getHWAddress(), host2->getHWAddress()); + if ((host1->getHWAddress() && !host2->getHWAddress()) || + (!host1->getHWAddress() && host2->getHWAddress())) { + ADD_FAILURE() << "Host comparison failed: host1 hwaddress=" + << host1->getHWAddress() << ", host2 hwaddress=" + << host2->getHWAddress(); + return; + } if (host1->getHWAddress()) { // Compare the actual address if they match. - EXPECT_EQ(*host1->getHWAddress(), *host2->getHWAddress()); + EXPECT_TRUE(*host1->getHWAddress() == *host2->getHWAddress()); + if (*host1->getHWAddress() != *host2->getHWAddress()) { + cout << host1->getHWAddress()->toText(true) << endl; + cout << host2->getHWAddress()->toText(true) << endl; + } + } + + // comapre if both have or have not DUID set + if ((host1->getDuid() && !host2->getDuid()) || + (!host1->getDuid() && host2->getDuid())) { + ADD_FAILURE() << "DUID comparison failed: host1 duid=" + << host1->getDuid() << ", host2 duid=" + << host2->getDuid(); + return; } + if (host1->getDuid()) { + EXPECT_TRUE(*host1->getDuid() == *host1->getDuid()); + if (*host1->getHWAddress() != *host2->getHWAddress()) { + cout << host1->getDuid()->toText() << endl; + cout << host2->getDuid()->toText() << endl; + } + } + + // Now check that the identifiers returned as vectors are the same + EXPECT_EQ(host1->getIdentifierType(), host2->getIdentifierType()); + EXPECT_EQ(host1->getIdentifier(), host2->getIdentifier()); + + // Check host parameters + EXPECT_EQ(host1->getIPv4SubnetID(), host2->getIPv4SubnetID()); + EXPECT_EQ(host1->getIPv6SubnetID(), host2->getIPv6SubnetID()); + EXPECT_EQ(host1->getIPv4Reservation(), host2->getIPv4Reservation()); + EXPECT_EQ(host1->getHostname(), host2->getHostname()); + + // Compare IPv6 reservations + compareReservations6(host1->getIPv6Reservations(), + host2->getIPv6Reservations()); + + // And compare client classification details + compareClientClasses(host1->getClientClasses4(), + host2->getClientClasses4()); + + compareClientClasses(host1->getClientClasses6(), + host2->getClientClasses6()); +} + +void +GenericHostDataSourceTest::compareReservations6(IPv6ResrvRange /*a*/, + IPv6ResrvRange /*b*/) { + /// @todo: Implement client classes comparison +} - /// @todo: Compare other fields +void +GenericHostDataSourceTest::compareClientClasses(const ClientClasses& /*classes1*/, + const ClientClasses& /*classes2*/) { + /// @todo: Implement client classes comparison } void GenericHostDataSourceTest::testBasic4() { 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 ccaaf1588d..1e97111d0a 100644 --- a/src/lib/dhcpsrv/tests/generic_host_data_source_unittest.h +++ b/src/lib/dhcpsrv/tests/generic_host_data_source_unittest.h @@ -16,6 +16,7 @@ #define GENERIC_HOST_DATA_SOURCE_UNITTEST_H #include +#include #include #include @@ -76,6 +77,23 @@ public: /// @param host2 second host to compare void compareHosts(const ConstHostPtr& host1, const ConstHostPtr& host2); + /// @brief Compares two IPv6 reservation lists. + /// + /// This method uses gtest macros to signal errors. + /// + /// @param resv1 first IPv6 reservations list + /// @param resv2 second IPv6 reservations list + void compareReservations6(IPv6ResrvRange resv1, IPv6ResrvRange resv2); + + /// @brief Compares two client classes + /// + /// This method uses gtest macros to signal errors. + /// + /// @param classes1 first list of client classes + /// @param classes2 second list of client classes + void compareClientClasses(const ClientClasses& classes1, + const ClientClasses& classes2); + /// @brief Pointer to the host data source BaseHostDataSource* hdsptr_; -- 2.47.3