From: Tomek Mrugalski Date: Fri, 13 Nov 2015 10:29:04 +0000 (+0100) Subject: [3682] Several new unit-tests implemented. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=102d9c83b7984e7426b953b5ebdaf2ea6aa11f5d;p=thirdparty%2Fkea.git [3682] Several new unit-tests implemented. --- 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 a8e1954045..13ed4252d9 100644 --- a/src/lib/dhcpsrv/tests/generic_host_data_source_unittest.cc +++ b/src/lib/dhcpsrv/tests/generic_host_data_source_unittest.cc @@ -37,9 +37,10 @@ GenericHostDataSourceTest::~GenericHostDataSourceTest() { std::string GenericHostDataSourceTest::generateHWAddr() { + // Let's use something that is easily printable. That's convenient + // if you need to enter MySQL queries by hand. static uint8_t hwaddr[] = {65, 66, 67, 68, 69, 70}; - stringstream tmp; for (int i = 0; i < sizeof(hwaddr); ++i) { if (i) { @@ -52,17 +53,33 @@ GenericHostDataSourceTest::generateHWAddr() { // 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]++; + hwaddr[sizeof(hwaddr) - 1]++; + if (hwaddr[sizeof(hwaddr) - 1] == 0) { + hwaddr[sizeof(hwaddr) - 2]++; } return (tmp.str()); } std::string GenericHostDataSourceTest::generateDuid() { - /// @todo: Make this code return different duid every time. - return ("010203040506abcd"); + // Let's use something that is easily printable. That's convenient + // if you need to enter MySQL queries by hand. + static uint8_t duid[] = { 65, 66, 67, 68, 69, 70, 71, 72, 73, 74 }; + + stringstream tmp; + for (int i = 0; i < sizeof(duid); ++i) { + tmp << std::setw(2) << std::hex << std::setfill('0') + << static_cast(duid[i]); + } + + // Increase the DUID for the next time we use it. + // This is primitive, but will work for 65k unique + // DUIDs. + duid[sizeof(duid) - 1]++; + if (duid[sizeof(duid) - 1] == 0) { + duid[sizeof(duid) - 2]++; + } + return (tmp.str()); } HostPtr GenericHostDataSourceTest::initializeHost4(std::string address, @@ -165,27 +182,51 @@ GenericHostDataSourceTest::compareHwaddrs(const ConstHostPtr& host1, } } -void GenericHostDataSourceTest::compareHosts(const ConstHostPtr& host1, - const ConstHostPtr& host2) { - - // Let's compare HW addresses and expect match. - compareHwaddrs(host1, host2, true); +void +GenericHostDataSourceTest::compareDuids(const ConstHostPtr& host1, + const ConstHostPtr& host2, + bool expect_match) { + ASSERT_TRUE(host1); + ASSERT_TRUE(host2); // 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(); + + // One host has a DUID and the other doesn't. + // Let's see if it's a problem. + if (expect_match) { + ADD_FAILURE() << "DUID comparison failed: host1 duid=" + << host1->getDuid() << ", host2 duid=" + << host2->getDuid(); + } 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->getDuid()) { - EXPECT_TRUE(*host1->getDuid() == *host1->getDuid()); - if (*host1->getHWAddress() != *host2->getHWAddress()) { + + if (expect_match) { + EXPECT_TRUE(*host1->getDuid() == *host1->getDuid()); + } else { + EXPECT_FALSE(*host1->getDuid() == *host1->getDuid()); + } + if (*host1->getDuid() != *host2->getDuid()) { cout << host1->getDuid()->toText() << endl; cout << host2->getDuid()->toText() << endl; } } +} + +void GenericHostDataSourceTest::compareHosts(const ConstHostPtr& host1, + const ConstHostPtr& host2) { + + // Let's compare HW addresses and expect match. + compareHwaddrs(host1, host2, true); + + // Now compare DUIDs + compareDuids(host1, host2, true); // Now check that the identifiers returned as vectors are the same EXPECT_EQ(host1->getIdentifierType(), host2->getIdentifierType()); @@ -221,12 +262,12 @@ GenericHostDataSourceTest::compareClientClasses(const ClientClasses& /*classes1* /// @todo: Implement client classes comparison } -void GenericHostDataSourceTest::testBasic4() { +void GenericHostDataSourceTest::testBasic4(bool hwaddr) { // Make sure we have the pointer to the host data source. ASSERT_TRUE(hdsptr_); // Create a host reservation. - HostPtr host = initializeHost4("192.0.2.1", true); + HostPtr host = initializeHost4("192.0.2.1", hwaddr); ASSERT_TRUE(host); // Make sure the host is generate properly. SubnetID subnet = host->getIPv4SubnetID(); @@ -243,15 +284,16 @@ void GenericHostDataSourceTest::testBasic4() { compareHosts(host, from_hds); } -void GenericHostDataSourceTest::testGetByIPv4() { + +void GenericHostDataSourceTest::testGetByIPv4(bool hwaddr) { // 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); + HostPtr host1 = initializeHost4("192.0.2.1", hwaddr); + HostPtr host2 = initializeHost4("192.0.2.2", hwaddr); + HostPtr host3 = initializeHost4("192.0.2.3", hwaddr); + HostPtr host4 = initializeHost4("192.0.2.4", hwaddr); // ... and add them to the data source. ASSERT_NO_THROW(hdsptr_->add(host1)); @@ -316,6 +358,35 @@ void GenericHostDataSourceTest::testGetByHWaddr() { compareHosts(host2, from_hds2); } +void GenericHostDataSourceTest::testGetByClientId() { + // Make sure we have a pointer to the host data source. + ASSERT_TRUE(hdsptr_); + + HostPtr host1 = initializeHost4("192.0.2.1", false); + HostPtr host2 = initializeHost4("192.0.2.2", false); + + // Sanity check: make sure the hosts have different client-ids. + ASSERT_TRUE(host1->getDuid()); + ASSERT_TRUE(host2->getDuid()); + compareDuids(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, HWAddrPtr(), host1->getDuid()); + ConstHostPtr from_hds2 = hdsptr_->get4(subnet2, HWAddrPtr(), host2->getDuid()); + + // 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 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 d3bec5c2c4..228f6a4311 100644 --- a/src/lib/dhcpsrv/tests/generic_host_data_source_unittest.h +++ b/src/lib/dhcpsrv/tests/generic_host_data_source_unittest.h @@ -74,11 +74,30 @@ public: /// 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. + /// + /// @param host1 first host to be compared + /// @param host2 second host to be compared + /// @param expect_match true = HW addresses expected to be the same, + /// false = HW addresses expected to be different void - compareHwaddrs(const ConstHostPtr& host1, - const ConstHostPtr& host2, + compareHwaddrs(const ConstHostPtr& host1, const ConstHostPtr& host2, bool expect_match); + /// @brief Compares DUIDs of the two hosts. + /// + /// This method compares two DUIDs (client-ids) and uses gtest + /// macros to signal unexpected (mismatch if expect_match is true; + /// match if expect_match is false) values. + /// + /// @param host1 first host to be compared + /// @param host2 second host to be compared + /// @param expect_match true = DUIDs expected to be the same, + /// false = DUIDs expected to be different + void + compareDuids(const ConstHostPtr& host1, const ConstHostPtr& host2, + bool expect_match); + + /// @brief Compares two hosts /// /// This method uses gtest macros to signal errors. @@ -111,18 +130,25 @@ public: /// can be inserted and later retrieved. /// /// Uses gtest macros to report failures. - void testBasic4(); + /// @param hwaddr true = use HW address as identifier, false = use client-id(DUID) + void testBasic4(bool hwaddr); /// @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(); + /// @param hwaddr true = use HW address as identifier, false = use client-id(DUID) + void testGetByIPv4(bool hwaddr); /// @brief Test that hosts can be retrieved by hardware address. /// /// Uses gtest macros to report failures. void testGetByHWaddr(); + + /// @brief Test that hosts can be retrieved by client-id + /// + /// Uses gtest macros to report failures. + void testGetByClientId(); }; }; // namespace test 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 3ae5a101bf..dc7bd03b2b 100644 --- a/src/lib/dhcpsrv/tests/mysql_host_data_source_unittest.cc +++ b/src/lib/dhcpsrv/tests/mysql_host_data_source_unittest.cc @@ -299,13 +299,27 @@ TEST(MySqlConnection, checkTimeConversion) { } // Test verifies if a host reservation can be added and later retrieved by IPv4 -// address. -TEST_F(MySqlHostDataSourceTest, basic4) { - testBasic4(); +// address. Host uses hw address as identifier. +TEST_F(MySqlHostDataSourceTest, basic4HWAddr) { + testBasic4(true); +} + +// Test verifies if a host reservation can be added and later retrieved by IPv4 +// address. Host uses client-id (DUID) as identifier. +TEST_F(MySqlHostDataSourceTest, basic4ClientId) { + testBasic4(false); +} + +// Test verifies that multiple hosts can be added and later retrived by their +// reserved IPv4 address. This test uses HW addresses as identifiers. +TEST_F(MySqlHostDataSourceTest, getByIPv4HWaddr) { + testGetByIPv4(true); } -TEST_F(MySqlHostDataSourceTest, getByIPv4) { - testGetByIPv4(); +// Test verifies that multiple hosts can be added and later retrived by their +// reserved IPv4 address. This test uses client-id (DUID) as identifiers. +TEST_F(MySqlHostDataSourceTest, getByIPv4ClientId) { + testGetByIPv4(false); } // Test verifies if a host reservation can be added and later retrieved by @@ -316,9 +330,8 @@ TEST_F(MySqlHostDataSourceTest, getByHWaddr) { // Test verifies if a host reservation can be added and later retrieved by // client identifier. -TEST_F(MySqlHostDataSourceTest, clientId) { - /// @todo: add host reservation with hardware address, retrieve it by - /// hardware address and make sure the values are correct. +TEST_F(MySqlHostDataSourceTest, getByClientId) { + testGetByClientId(); } // Test verifies if hardware address and client identifier are not confused.