From: Thomas Markwalder Date: Wed, 25 May 2016 13:32:13 +0000 (-0400) Subject: [4276] Addressed review comments #2 X-Git-Tag: trac4106_update_base~12^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=48a350e9ca1989e750034c681e27c28ce8ec3d50;p=thirdparty%2Fkea.git [4276] Addressed review comments #2 Moved PgSqlExchange unit tests into their own file. Improved time testing. --- diff --git a/src/lib/dhcpsrv/tests/Makefile.am b/src/lib/dhcpsrv/tests/Makefile.am index 2f450da1f1..b8c766f497 100755 --- a/src/lib/dhcpsrv/tests/Makefile.am +++ b/src/lib/dhcpsrv/tests/Makefile.am @@ -118,6 +118,7 @@ endif libdhcpsrv_unittests_SOURCES += ncr_generator_unittest.cc if HAVE_PGSQL +libdhcpsrv_unittests_SOURCES += pgsql_exchange_unittest.cc libdhcpsrv_unittests_SOURCES += pgsql_lease_mgr_unittest.cc endif libdhcpsrv_unittests_SOURCES += pool_unittest.cc diff --git a/src/lib/dhcpsrv/tests/pgsql_exchange_unittest.cc b/src/lib/dhcpsrv/tests/pgsql_exchange_unittest.cc new file mode 100755 index 0000000000..9cc51975ca --- /dev/null +++ b/src/lib/dhcpsrv/tests/pgsql_exchange_unittest.cc @@ -0,0 +1,73 @@ +// Copyright (C) 2016 Internet Systems Consortium, Inc. ("ISC") +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +#include + +#include + +#include + +#include + +using namespace isc; +using namespace isc::dhcp; + +namespace { + +/// @brief Converts a time_t into a string matching our Postgres input format +/// +/// @param time_val Time value to convert +/// @retrun A string containing the converted time +std::string timeToDbString(const time_t time_val) { + struct tm tinfo; + char buffer[20]; + + localtime_r(&time_val, &tinfo); + strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &tinfo); + return(std::string(buffer)); +} + +/// @brief Basic checks on time conversion functions in PgSqlExchange +/// We input timestamps as date/time strings and we output them as +/// an integer string of seconds since the epoch. There is no meangingful +/// way to test them round-trip without Postgres involved. +TEST(PgSqlExchangeTest, convertTimeTest) { + // Get a reference time and time string + time_t ref_time; + time(&ref_time); + + std::string ref_time_str(timeToDbString(ref_time)); + + // Verify convertToDatabaseTime gives us the expected localtime string + std::string time_str = PgSqlExchange::convertToDatabaseTime(ref_time); + EXPECT_EQ(time_str, ref_time_str); + + // Verify convertToDatabaseTime with valid_lifetime = 0 gives us the + // expected localtime string + time_str = PgSqlExchange::convertToDatabaseTime(ref_time, 0); + EXPECT_EQ(time_str, ref_time_str); + + // Verify we can add time by adding a day. + ref_time_str = timeToDbString(ref_time + (24*3600)); + ASSERT_NO_THROW(time_str = PgSqlExchange::convertToDatabaseTime(ref_time, + 24*3600)); + EXPECT_EQ(time_str, ref_time_str); + + // Verify too large of a value is detected. + ASSERT_THROW(PgSqlExchange::convertToDatabaseTime(DatabaseConnection:: + MAX_DB_TIME - 1, + 24*3600), + isc::BadValue); + + // Make sure Conversion "from" database time functions + std::string ref_secs_str = boost::lexical_cast(ref_time); + time_t from_time = PgSqlExchange::convertFromDatabaseTime(ref_secs_str); + from_time = PgSqlExchange::convertFromDatabaseTime(ref_secs_str); + EXPECT_EQ(ref_time, from_time); +} + +}; // namespace + diff --git a/src/lib/dhcpsrv/tests/pgsql_lease_mgr_unittest.cc b/src/lib/dhcpsrv/tests/pgsql_lease_mgr_unittest.cc index 8e6596de78..736495adbc 100755 --- a/src/lib/dhcpsrv/tests/pgsql_lease_mgr_unittest.cc +++ b/src/lib/dhcpsrv/tests/pgsql_lease_mgr_unittest.cc @@ -14,18 +14,8 @@ #include #include -#include - #include -#include -#include -#include -#include -#include - -#include - using namespace isc; using namespace isc::asiolink; using namespace isc::dhcp; @@ -388,49 +378,5 @@ TEST_F(PgSqlLeaseMgrTest, getExpiredLeases6) { testGetExpiredLeases6(); } -/// @brief Basic checks on time conversion functions in PgSqlExchange -/// We input timestamps as date/time strings and we output them as -/// an integer string of seconds since the epoch. There is no meangingful -/// way to test them round-trip without Postgres involved. -TEST(PgSqlExchange, convertTimeTest) { - // Get a reference time and time string - time_t ref_time; - struct tm tinfo; - char buffer[20]; - - time(&ref_time); - localtime_r(&ref_time, &tinfo); - strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &tinfo); - std::string ref_time_str(buffer); - - // Verify convertToDatabaseTime gives us the expected localtime string - std::string time_str = PgSqlExchange::convertToDatabaseTime(ref_time); - EXPECT_EQ(ref_time_str, time_str); - - // Verify convertToDatabaseTime with valid_lifetime = 0 gives us the - // expected localtime string - time_str = PgSqlExchange::convertToDatabaseTime(ref_time, 0); - EXPECT_EQ(time_str, ref_time_str); - - // Add a day, we should get a string that's greater than the reference - // string. Ok, maybe not the most exacting test, but you want I should - // parse this? - std::string time_str2; - ASSERT_NO_THROW(time_str2 = PgSqlExchange::convertToDatabaseTime(ref_time, - 24*3600)); - EXPECT_GT(time_str2, ref_time_str); - - // Verify too large of a value is detected. - ASSERT_THROW(PgSqlExchange::convertToDatabaseTime(DatabaseConnection:: - MAX_DB_TIME, 24*3600), - isc::BadValue); - - // Make sure Conversion "from" database time functions - std::string ref_secs_str = boost::lexical_cast(ref_time); - time_t from_time = PgSqlExchange::convertFromDatabaseTime(ref_secs_str); - from_time = PgSqlExchange::convertFromDatabaseTime(ref_secs_str); - EXPECT_EQ(ref_time, from_time); -} - }; // namespace