BIND 10 trac2342, updated. 89c8076272fb81a9e49b3bdbcd704103707db83f [2342] Extend MySQL tests
BIND 10 source code commits
bind10-changes at lists.isc.org
Wed Oct 24 21:16:21 UTC 2012
The branch, trac2342 has been updated
via 89c8076272fb81a9e49b3bdbcd704103707db83f (commit)
via 45feb03e15fa555b846d0b8dfa4f8c88a1d50d64 (commit)
via df141d203ebfefd11828290020db8f9177ef9908 (commit)
from 08daa03beac5d16305734fc434edcc28962205e9 (commit)
Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.
- Log -----------------------------------------------------------------
commit 89c8076272fb81a9e49b3bdbcd704103707db83f
Author: Stephen Morris <stephen at isc.org>
Date: Wed Oct 24 22:14:42 2012 +0100
[2342] Extend MySQL tests
Clear up after every test and check that commit works.
commit 45feb03e15fa555b846d0b8dfa4f8c88a1d50d64
Author: Stephen Morris <stephen at isc.org>
Date: Wed Oct 24 21:28:24 2012 +0100
[2342] Remove hwaddr from lease6
commit df141d203ebfefd11828290020db8f9177ef9908
Author: Stephen Morris <stephen at isc.org>
Date: Wed Oct 24 21:18:13 2012 +0100
[2342] Corrections based on first part for third section of review.
-----------------------------------------------------------------------
Summary of changes:
src/lib/dhcp/dhcpdb_create.mysql | 1 -
src/lib/dhcp/lease_mgr.cc | 46 +++++-
src/lib/dhcp/lease_mgr.h | 29 ++--
src/lib/dhcp/mysql_lease_mgr.cc | 155 +++++++++-----------
src/lib/dhcp/mysql_lease_mgr.h | 5 +
src/lib/dhcp/tests/mysql_lease_mgr_unittest.cc | 185 ++++++++++++++----------
6 files changed, 250 insertions(+), 171 deletions(-)
-----------------------------------------------------------------------
diff --git a/src/lib/dhcp/dhcpdb_create.mysql b/src/lib/dhcp/dhcpdb_create.mysql
index b888b59..7a292ec 100644
--- a/src/lib/dhcp/dhcpdb_create.mysql
+++ b/src/lib/dhcp/dhcpdb_create.mysql
@@ -44,7 +44,6 @@ CREATE TABLE lease4 (
# it will eventually be replaced by BINARY(16).
CREATE TABLE lease6 (
address VARCHAR(40) PRIMARY KEY NOT NULL, # IPv6 address
- hwaddr VARBINARY(20), # Hardware address
duid VARBINARY(128), # DUID
valid_lifetime INT UNSIGNED, # Length of the lease (seconds)
expire TIMESTAMP, # Expiration time of the lease
diff --git a/src/lib/dhcp/lease_mgr.cc b/src/lib/dhcp/lease_mgr.cc
index 1fd64d8..c86bca3 100644
--- a/src/lib/dhcp/lease_mgr.cc
+++ b/src/lib/dhcp/lease_mgr.cc
@@ -34,6 +34,9 @@ LeaseMgr::LeaseMgr(const LeaseMgr::ParameterMap& parameters)
: parameters_(parameters) {
}
+LeaseMgr::~LeaseMgr() {
+}
+
std::string LeaseMgr::getParameter(const std::string& name) const {
ParameterMap::const_iterator param = parameters_.find(name);
if (param == parameters_.end()) {
@@ -42,5 +45,46 @@ std::string LeaseMgr::getParameter(const std::string& name) const {
return (param->second);
}
-LeaseMgr::~LeaseMgr() {
+std::string
+Lease6::toText() {
+ ostringstream stream;
+
+ stream << "Type: " << static_cast<int>(type_) << " (";
+ switch (type_) {
+ case Lease6::LEASE_IA_NA:
+ stream << "IA_NA)\n";
+ break;
+ case Lease6::LEASE_IA_TA:
+ stream << "IA_TA)\n";
+ break;
+ case Lease6::LEASE_IA_PD:
+ stream << "IA_PD)\n";
+ break;
+ default:
+ stream << "unknown)\n";
+ }
+ stream << "Address: " << addr_.toText() << "\n"
+ << "Prefix length: " << static_cast<int>(prefixlen_) << "\n"
+ << "IAID: " << iaid_ << "\n"
+ << "Pref life: " << preferred_lft_ << "\n"
+ << "Valid life: " << valid_lft_ << "\n"
+ << "Cltt: " << cltt_ << "\n"
+ << "Subnet ID: " << subnet_id_ << "\n";
+
+ return (stream.str());
+}
+
+bool
+Lease6::operator==(const Lease6& other) const {
+ return (
+ type_ == other.type_ &&
+ addr_ == other.addr_ &&
+ prefixlen_ == other.prefixlen_ &&
+ iaid_ == other.iaid_ &&
+ *duid_ == *other.duid_ &&
+ preferred_lft_ == other.preferred_lft_ &&
+ valid_lft_ == other.valid_lft_ &&
+ cltt_ == other.cltt_ &&
+ subnet_id_ == other.subnet_id_
+ );
}
diff --git a/src/lib/dhcp/lease_mgr.h b/src/lib/dhcp/lease_mgr.h
index c21b996..fbb1584 100644
--- a/src/lib/dhcp/lease_mgr.h
+++ b/src/lib/dhcp/lease_mgr.h
@@ -58,6 +58,8 @@
/// Nevertheless, we hope to have failover protocol eventually implemented in
/// the Kea.
+#include <iostream>
+
namespace isc {
namespace dhcp {
@@ -210,15 +212,6 @@ struct Lease6 {
/// than once in a message. To differentiate between them, IAID field is present
uint32_t iaid_;
- /// @brief hardware address
- ///
- /// This field is not really used and is optional at best. The concept of identifying
- /// clients by their hardware address was replaced in DHCPv6 by DUID concept. Each
- /// client has its own unique DUID (DHCP Unique IDentifier). Furthermore, client's
- /// HW address is not always available, because client may be behind a relay (relay
- /// stores only link-local address).
- std::vector<uint8_t> hwaddr_;
-
/// @brief client identifier
boost::shared_ptr<DUID> duid_;
@@ -289,6 +282,24 @@ struct Lease6 {
///
/// Initialize fields that don't have a default constructor.
Lease6() : addr_("::") {}
+
+ /// @brief Convert Lease6 to Printable Form
+ ///
+ /// @return String form of the lease
+ std::string toText();
+
+ /// @brief Compare two leases for equality
+ ///
+ /// @param other lease6 object with which to compare
+ bool operator==(const Lease6& other) const;
+
+ /// @brief Compare two leases for inequality
+ ///
+ /// @param other lease6 object with which to compare
+ bool operator!=(const Lease6& other) const {
+ return (!operator==(other));
+ }
+
};
/// @brief Pointer to a Lease6 structure.
diff --git a/src/lib/dhcp/mysql_lease_mgr.cc b/src/lib/dhcp/mysql_lease_mgr.cc
index 5f5fedd..6627dfb 100644
--- a/src/lib/dhcp/mysql_lease_mgr.cc
+++ b/src/lib/dhcp/mysql_lease_mgr.cc
@@ -40,7 +40,6 @@ namespace {
const size_t ADDRESS6_TEXT_MAX_LEN = 42; // Max size of a IPv6 text buffer
const size_t DUID_MAX_LEN = 128; // Max size of a DUID
-const size_t HWADDR_MAX_LEN = 20; // Max size of a hardware address
///@}
};
@@ -91,6 +90,7 @@ public:
memset(bind_, 0, sizeof(bind_));
// address: varchar(40)
+
addr6_ = lease_->addr_.toText();
addr6_length_ = addr6_.size();
@@ -99,27 +99,19 @@ public:
bind_[0].buffer_length = addr6_length_;
bind_[0].length = &addr6_length_;
- // hwaddr: binary(20)
- hwaddr_length_ = lease_->hwaddr_.size();
-
- bind_[1].buffer_type = MYSQL_TYPE_BLOB;
- bind_[1].buffer = reinterpret_cast<char*>(&(lease_->hwaddr_[0]));
- bind_[1].buffer_length = hwaddr_length_;
- bind_[1].length = &hwaddr_length_;
-
// duid: varchar(128)
duid_ = lease_->duid_->getDuid();
duid_length_ = duid_.size();
- bind_[2].buffer_type = MYSQL_TYPE_BLOB;
- bind_[2].buffer = reinterpret_cast<char*>(&(duid_[0]));
- bind_[2].buffer_length = duid_length_;
- bind_[2].length = &duid_length_;
+ bind_[1].buffer_type = MYSQL_TYPE_BLOB;
+ bind_[1].buffer = reinterpret_cast<char*>(&(duid_[0]));
+ bind_[1].buffer_length = duid_length_;
+ bind_[1].length = &duid_length_;
// lease_time: unsigned int
- bind_[3].buffer_type = MYSQL_TYPE_LONG;
- bind_[3].buffer = reinterpret_cast<char*>(&lease->valid_lft_);
- bind_[3].is_unsigned = true_;
+ bind_[2].buffer_type = MYSQL_TYPE_LONG;
+ bind_[2].buffer = reinterpret_cast<char*>(&lease->valid_lft_);
+ bind_[2].is_unsigned = true_;
// expire: timestamp
// The lease structure holds the client last transmission time (cltt_)
@@ -129,40 +121,40 @@ public:
// expire = cltt_ + valid_lft_
MySqlLeaseMgr::convertToDatabaseTime(lease_->cltt_, lease_->valid_lft_,
expire_);
- bind_[4].buffer_type = MYSQL_TYPE_TIMESTAMP;
- bind_[4].buffer = reinterpret_cast<char*>(&expire_);
- bind_[4].buffer_length = sizeof(expire_);
+ bind_[3].buffer_type = MYSQL_TYPE_TIMESTAMP;
+ bind_[3].buffer = reinterpret_cast<char*>(&expire_);
+ bind_[3].buffer_length = sizeof(expire_);
// subnet_id: unsigned int
// Can use lease_->subnet_id_ directly as it is of type uint32_t.
- bind_[5].buffer_type = MYSQL_TYPE_LONG;
- bind_[5].buffer = reinterpret_cast<char*>(&lease_->subnet_id_);
- bind_[5].is_unsigned = true_;
+ bind_[4].buffer_type = MYSQL_TYPE_LONG;
+ bind_[4].buffer = reinterpret_cast<char*>(&lease_->subnet_id_);
+ bind_[4].is_unsigned = true_;
// pref_lifetime: unsigned int
// Can use lease_->preferred_lft_ directly as it is of type uint32_t.
- bind_[6].buffer_type = MYSQL_TYPE_LONG;
- bind_[6].buffer = reinterpret_cast<char*>(&lease_->preferred_lft_);
- bind_[6].is_unsigned = true_;
+ bind_[5].buffer_type = MYSQL_TYPE_LONG;
+ bind_[5].buffer = reinterpret_cast<char*>(&lease_->preferred_lft_);
+ bind_[5].is_unsigned = true_;
// lease_type: tinyint
// Must convert to uint8_t as lease_->type_ is a LeaseType variable
lease_type_ = lease_->type_;
- bind_[7].buffer_type = MYSQL_TYPE_TINY;
- bind_[7].buffer = reinterpret_cast<char*>(&lease_type_);
- bind_[7].is_unsigned = true_;
+ bind_[6].buffer_type = MYSQL_TYPE_TINY;
+ bind_[6].buffer = reinterpret_cast<char*>(&lease_type_);
+ bind_[6].is_unsigned = true_;
// iaid: unsigned int
// Can use lease_->iaid_ directly as it is of type uint32_t.
- bind_[8].buffer_type = MYSQL_TYPE_LONG;
- bind_[8].buffer = reinterpret_cast<char*>(&lease_->iaid_);
- bind_[8].is_unsigned = true_;
+ bind_[7].buffer_type = MYSQL_TYPE_LONG;
+ bind_[7].buffer = reinterpret_cast<char*>(&lease_->iaid_);
+ bind_[7].is_unsigned = true_;
// prefix_len: unsigned tinyint
// Can use lease_->prefixlen_ directly as it is uint32_t.
- bind_[9].buffer_type = MYSQL_TYPE_TINY;
- bind_[9].buffer = reinterpret_cast<char*>(&lease_->prefixlen_);
- bind_[9].is_unsigned = true_;
+ bind_[8].buffer_type = MYSQL_TYPE_TINY;
+ bind_[8].buffer = reinterpret_cast<char*>(&lease_->prefixlen_);
+ bind_[8].is_unsigned = true_;
return(bind_);
}
@@ -192,64 +184,56 @@ public:
bind_[0].buffer_length = addr6_length_;
bind_[0].length = &addr6_length_;
bind_[0].error = &error_[0];
-
- // hwaddr: varbinary(20)
- hwaddr_length_ = sizeof(hwaddr_buffer_);
- bind_[1].buffer_type = MYSQL_TYPE_BLOB;
- bind_[1].buffer = reinterpret_cast<char*>(hwaddr_buffer_);
- bind_[1].buffer_length = hwaddr_length_;
- bind_[1].length = &hwaddr_length_;
- bind_[2].error = &error_[1];
// client_id: varbinary(128)
duid_length_ = sizeof(duid_buffer_);
- bind_[2].buffer_type = MYSQL_TYPE_BLOB;
- bind_[2].buffer = reinterpret_cast<char*>(duid_buffer_);
- bind_[2].buffer_length = duid_length_;
- bind_[2].length = &duid_length_;
- bind_[2].error = &error_[2];
+ bind_[1].buffer_type = MYSQL_TYPE_BLOB;
+ bind_[1].buffer = reinterpret_cast<char*>(duid_buffer_);
+ bind_[1].buffer_length = duid_length_;
+ bind_[1].length = &duid_length_;
+ bind_[1].error = &error_[1];
// lease_time: unsigned int
- bind_[3].buffer_type = MYSQL_TYPE_LONG;
- bind_[3].buffer = reinterpret_cast<char*>(&valid_lifetime_);
- bind_[3].is_unsigned = true_;
- bind_[3].error = &error_[3];
+ bind_[2].buffer_type = MYSQL_TYPE_LONG;
+ bind_[2].buffer = reinterpret_cast<char*>(&valid_lifetime_);
+ bind_[2].is_unsigned = true_;
+ bind_[2].error = &error_[2];
// expire: timestamp
- bind_[4].buffer_type = MYSQL_TYPE_TIMESTAMP;
- bind_[4].buffer = reinterpret_cast<char*>(&expire_);
- bind_[4].buffer_length = sizeof(expire_);
- bind_[4].error = &error_[4];
+ bind_[3].buffer_type = MYSQL_TYPE_TIMESTAMP;
+ bind_[3].buffer = reinterpret_cast<char*>(&expire_);
+ bind_[3].buffer_length = sizeof(expire_);
+ bind_[3].error = &error_[3];
// subnet_id: unsigned int
+ bind_[4].buffer_type = MYSQL_TYPE_LONG;
+ bind_[4].buffer = reinterpret_cast<char*>(&subnet_id_);
+ bind_[4].is_unsigned = true_;
+ bind_[4].error = &error_[4];
+
+ // pref_lifetime: unsigned int
bind_[5].buffer_type = MYSQL_TYPE_LONG;
- bind_[5].buffer = reinterpret_cast<char*>(&subnet_id_);
+ bind_[5].buffer = reinterpret_cast<char*>(&pref_lifetime_);
bind_[5].is_unsigned = true_;
bind_[5].error = &error_[5];
- // pref_lifetime: unsigned int
- bind_[6].buffer_type = MYSQL_TYPE_LONG;
- bind_[6].buffer = reinterpret_cast<char*>(&pref_lifetime_);
+ // lease_type: tinyint
+ bind_[6].buffer_type = MYSQL_TYPE_TINY;
+ bind_[6].buffer = reinterpret_cast<char*>(&lease_type_);
bind_[6].is_unsigned = true_;
bind_[6].error = &error_[6];
- // lease_type: tinyint
- bind_[7].buffer_type = MYSQL_TYPE_TINY;
- bind_[7].buffer = reinterpret_cast<char*>(&lease_type_);
+ // iaid: unsigned int
+ bind_[7].buffer_type = MYSQL_TYPE_LONG;
+ bind_[7].buffer = reinterpret_cast<char*>(&iaid_);
bind_[7].is_unsigned = true_;
bind_[7].error = &error_[7];
-
- // iaid: unsigned int
- bind_[8].buffer_type = MYSQL_TYPE_LONG;
- bind_[8].buffer = reinterpret_cast<char*>(&iaid_);
- bind_[8].is_unsigned = true_;
- bind_[8].error = &error_[8];
// prefix_len: unsigned tinyint
- bind_[9].buffer_type = MYSQL_TYPE_TINY;
- bind_[9].buffer = reinterpret_cast<char*>(&prefixlen_);
- bind_[9].is_unsigned = true_;
- bind_[9].error = &error_[9];
+ bind_[8].buffer_type = MYSQL_TYPE_TINY;
+ bind_[8].buffer = reinterpret_cast<char*>(&prefixlen_);
+ bind_[8].is_unsigned = true_;
+ bind_[8].error = &error_[8];
return (bind_);
}
@@ -278,8 +262,6 @@ public:
// Set the other data, converting time as needed.
result->addr_ = isc::asiolink::IOAddress(address);
- result->hwaddr_ = vector<uint8_t>(&hwaddr_buffer_[0],
- &hwaddr_buffer_[hwaddr_length_]);
result->duid_.reset(new DUID(duid_buffer_, duid_length_));
MySqlLeaseMgr::convertFromDatabaseTime(expire_, valid_lifetime_,
result->cltt_);
@@ -320,15 +302,13 @@ private:
char addr6_buffer_[ADDRESS6_TEXT_MAX_LEN]; ///< Character
///< array form of V6 address
unsigned long addr6_length_; ///< Length of the address
- MYSQL_BIND bind_[10]; ///< Static array for speed of access
+ MYSQL_BIND bind_[9]; ///< Static array for speed of access
std::vector<uint8_t> duid_; ///< Client identification
uint8_t duid_buffer_[DUID_MAX_LEN]; ///< Buffer form of DUID
unsigned long duid_length_; ///< Length of the DUID
- my_bool error_[10]; ///< For error reporting
+ my_bool error_[9]; ///< For error reporting
MYSQL_TIME expire_; ///< Lease expiry time
const my_bool false_; ///< "false" for MySql
- uint8_t hwaddr_buffer_[HWADDR_MAX_LEN]; ///< Hardware address buffer
- unsigned long hwaddr_length_; ///< Length of hardware address
uint32_t iaid_; ///< Identity association ID
Lease6Ptr lease_; ///< Pointer to lease object
uint32_t valid_lifetime_; ///< Lease time
@@ -475,6 +455,15 @@ MySqlLeaseMgr::openDatabase() {
isc_throw(NoDatabaseName, "must specified a name for the database");
}
+ // Set options for the connection:
+ // - automatic reconnection
+ my_bool auto_reconnect = 1;
+ int result = mysql_options(mysql_, MYSQL_OPT_RECONNECT, &auto_reconnect);
+ if (result != 0) {
+ isc_throw(DbOpenError, "unable to set auto-reconnect option: " <<
+ mysql_error(mysql_));
+ }
+
// Open the database. Use defaults for non-specified options.
MYSQL* status = mysql_real_connect(mysql_, host, user, password, name,
0, NULL, 0);
@@ -522,17 +511,17 @@ MySqlLeaseMgr::prepareStatements() {
prepareStatement(DELETE_LEASE6,
"DELETE FROM lease6 WHERE address = ?");
prepareStatement(GET_LEASE6,
- "SELECT address, hwaddr, duid, "
- "valid_lifetime, expire, subnet_id, pref_lifetime, "
+ "SELECT address, duid, valid_lifetime, "
+ "expire, subnet_id, pref_lifetime, "
"lease_type, iaid, prefix_len "
"FROM lease6 WHERE address = ?");
prepareStatement(GET_VERSION,
"SELECT version, minor FROM schema_version");
prepareStatement(INSERT_LEASE6,
- "INSERT INTO lease6(address, hwaddr, duid, "
- "valid_lifetime, expire, subnet_id, pref_lifetime, "
+ "INSERT INTO lease6(address, duid, valid_lifetime, "
+ "expire, subnet_id, pref_lifetime, "
"lease_type, iaid, prefix_len) "
- "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
}
bool
diff --git a/src/lib/dhcp/mysql_lease_mgr.h b/src/lib/dhcp/mysql_lease_mgr.h
index 9a86691..9e28390 100644
--- a/src/lib/dhcp/mysql_lease_mgr.h
+++ b/src/lib/dhcp/mysql_lease_mgr.h
@@ -22,6 +22,11 @@
namespace isc {
namespace dhcp {
+// Define the current database schema values
+
+const uint32_t CURRENT_VERSION_VERSION = 0;
+const uint32_t CURRENT_VERSION_MINOR = 1;
+
/// @brief MySQL Lease Manager
///
/// This is a concrete API for the backend for the MySQL database.
diff --git a/src/lib/dhcp/tests/mysql_lease_mgr_unittest.cc b/src/lib/dhcp/tests/mysql_lease_mgr_unittest.cc
index c0f12a4..b267bc3 100644
--- a/src/lib/dhcp/tests/mysql_lease_mgr_unittest.cc
+++ b/src/lib/dhcp/tests/mysql_lease_mgr_unittest.cc
@@ -30,7 +30,10 @@ using namespace std;
namespace {
-// Connection strings
+// Connection strings. Assume:
+// Database: keatest
+// Username: keatest
+// Password: keatest
const char* VALID_TYPE = "type=mysql";
const char* INVALID_TYPE = "type=unknown";
const char* VALID_NAME = "name=keatest";
@@ -84,11 +87,31 @@ string connectionString(const char* type, const char* name, const char* host,
}
// Return valid connection string
-string validConnectionString() {
+string
+validConnectionString() {
return (connectionString(VALID_TYPE, VALID_NAME, VALID_HOST,
VALID_USER, VALID_PASSWORD));
}
+// Clear everything from the database tables
+void
+clearAll() {
+ // Initialise
+ MYSQL handle;
+ (void) mysql_init(&handle);
+
+ // Open database
+ (void) mysql_real_connect(&handle, "localhost", "keatest", "keatest",
+ "keatest", 0, NULL, 0);
+
+ // Clear the database
+ (void) mysql_query(&handle, "DELETE FROM lease4");
+ (void) mysql_query(&handle, "DELETE FROM lease6");
+
+ // ... and close
+ (void) mysql_close(&handle);
+}
+
/// @brief Test Fixture Class
///
/// Opens the database prior to each test and closes it afterwards.
@@ -98,9 +121,19 @@ class MySqlLeaseMgrTest : public ::testing::Test {
public:
/// @brief Constructor
///
- /// Open the database.
-
+ /// Deletes everything from the database and opens it.
MySqlLeaseMgrTest() {
+ clearAll();
+ LeaseMgrFactory::create(validConnectionString());
+ lmptr_ = &(LeaseMgrFactory::instance());
+ }
+
+ /// @brief Reopen the database
+ ///
+ /// Closes the database and re-open it. Anything committed should be
+ /// visible.
+ void reopen() {
+ LeaseMgrFactory::destroy();
LeaseMgrFactory::create(validConnectionString());
lmptr_ = &(LeaseMgrFactory::instance());
}
@@ -108,11 +141,12 @@ public:
/// @brief Destructor
///
/// Rolls back all pending transactions. The deletion of the
- /// lmptr_ member variable will close the database.
-
+ /// lmptr_ member variable will close the database. Then
+ /// reopen it and delete everything created by the test.
virtual ~MySqlLeaseMgrTest() {
lmptr_->rollback();
LeaseMgrFactory::destroy();
+ clearAll();
}
LeaseMgr* lmptr_; // Pointer to the lease manager
@@ -127,6 +161,19 @@ public:
/// opened: the fixtures assume that and check basic operations.
TEST(MySqlOpenTest, OpenDatabase) {
+ // Check that database opens correctly and tidy up. If it fails, print
+ // the error message.
+ try {
+ LeaseMgrFactory::create(validConnectionString());
+ EXPECT_NO_THROW((void) LeaseMgrFactory::instance());
+ LeaseMgrFactory::destroy();
+ } catch (const isc::Exception& ex) {
+ FAIL() << "*** ERROR: unable to open database, reason:\n"
+ << " " << ex.what()
+ << "*** The test environment is broken and must be fixed\n"
+ << "*** before the MySQL tests will run correctly.\n";
+ }
+
// Check that attempting to get an instance of the lease manager when
// none is set throws an exception.
EXPECT_THROW(LeaseMgrFactory::instance(), NoLeaseManager);
@@ -159,29 +206,36 @@ TEST(MySqlOpenTest, OpenDatabase) {
EXPECT_THROW(LeaseMgrFactory::create(connectionString(
VALID_TYPE, NULL, VALID_HOST, INVALID_USER, VALID_PASSWORD)),
NoDatabaseName);
-
- // Check that database opens correctly.
- ASSERT_NO_THROW(LeaseMgrFactory::create(validConnectionString()));
- EXPECT_NO_THROW((void) LeaseMgrFactory::instance());
-
- // ... and tidy up.
- LeaseMgrFactory::destroy();
}
/// @brief Check conversion functions
TEST_F(MySqlLeaseMgrTest, CheckTimeConversion) {
const time_t cltt = time(NULL);
const uint32_t valid_lft = 86400; // 1 day
- MYSQL_TIME expire;
-
- MySqlLeaseMgr::convertToDatabaseTime(cltt, valid_lft, expire);
- EXPECT_LE(2012, expire.year); // Code was written in 2012
- EXPECT_EQ(0, expire.second_part);
- EXPECT_EQ(0, expire.neg);
+ struct tm tm_expire;
+ MYSQL_TIME mysql_expire;
+
+ // Work out what the broken-down time will be for one day
+ // after the current time.
+ time_t expire_time = cltt + valid_lft;
+ (void) localtime_r(&expire_time, &tm_expire);
+
+ // Convert to the database time
+ MySqlLeaseMgr::convertToDatabaseTime(cltt, valid_lft, mysql_expire);
+
+ // Are the times the same?
+ EXPECT_EQ(tm_expire.tm_year + 1900, mysql_expire.year);
+ EXPECT_EQ(tm_expire.tm_mon + 1, mysql_expire.month);
+ EXPECT_EQ(tm_expire.tm_mday, mysql_expire.day);
+ EXPECT_EQ(tm_expire.tm_hour, mysql_expire.hour);
+ EXPECT_EQ(tm_expire.tm_min, mysql_expire.minute);
+ EXPECT_EQ(tm_expire.tm_sec, mysql_expire.second);
+ EXPECT_EQ(0, mysql_expire.second_part);
+ EXPECT_EQ(0, mysql_expire.neg);
// Convert back
time_t converted_cltt = 0;
- MySqlLeaseMgr::convertFromDatabaseTime(expire, valid_lft, converted_cltt);
+ MySqlLeaseMgr::convertFromDatabaseTime(mysql_expire, valid_lft, converted_cltt);
EXPECT_EQ(cltt, converted_cltt);
}
@@ -190,58 +244,8 @@ TEST_F(MySqlLeaseMgrTest, CheckVersion) {
// Check version
pair<uint32_t, uint32_t> version;
ASSERT_NO_THROW(version = lmptr_->getVersion());
- EXPECT_EQ(0, version.first);
- EXPECT_EQ(1, version.second);
-}
-
-/// @brief Print Elements of Lease6 Structure
-///
-/// @param lease Pointer to lease to print
-/// @param title Title to print before the lease information
-void
-printLease6(const Lease6Ptr& lease, const char* title = NULL) {
- if (title != NULL) {
- cout << title << "\n";
- }
-
- cout << " Type: ";
- switch (lease->type_) {
- case Lease6::LEASE_IA_NA:
- cout << "IA_NA\n";
- break;
- case Lease6::LEASE_IA_TA:
- cout << "IA_TA\n";
- break;
- case Lease6::LEASE_IA_PD:
- cout << "IA_PD\n";
- break;
- default:
- cout << "unknown (" << static_cast<int>(lease->type_) << ")\n";
- }
- cout << " Address: " << lease->addr_.toText() << "\n";
- cout << " Prefix length: " << static_cast<int>(lease->prefixlen_) << "\n";
- cout << " IAID: " << lease->iaid_ << "\n";
- cout << " Pref life: " << lease->preferred_lft_ << "\n";
- cout << " Valid life: " << lease->valid_lft_ << "\n";
- cout << " Cltt: " << lease->cltt_ << "\n";
- cout << " Subnet ID: " << lease->subnet_id_ << "\n";
-}
-
-/// @brief Compare Lease4 Structure
-bool
-compareLease6(const Lease6Ptr& first, const Lease6Ptr& second) {
- return (
- first->type_ == second->type_ &&
- first->addr_ == second->addr_ &&
- first->prefixlen_ == second->prefixlen_ &&
- first->iaid_ == second->iaid_ &&
- first->hwaddr_ == second->hwaddr_ &&
- *first->duid_ == *second->duid_ &&
- first->preferred_lft_ == second->preferred_lft_ &&
- first->valid_lft_ == second->valid_lft_ &&
- first->cltt_ == second->cltt_ &&
- first->subnet_id_ == second->subnet_id_
- );
+ EXPECT_EQ(CURRENT_VERSION_VERSION, version.first);
+ EXPECT_EQ(CURRENT_VERSION_MINOR, version.second);
}
void
@@ -254,7 +258,6 @@ detailCompareLease6(const Lease6Ptr& first, const Lease6Ptr& second) {
EXPECT_EQ(first->addr_.toText(), second->addr_.toText());
EXPECT_EQ(first->prefixlen_, second->prefixlen_);
EXPECT_EQ(first->iaid_, second->iaid_);
- EXPECT_TRUE(first->hwaddr_ == second->hwaddr_);
EXPECT_TRUE(*first->duid_ == *second->duid_);
EXPECT_EQ(first->preferred_lft_, second->preferred_lft_);
EXPECT_EQ(first->valid_lft_, second->valid_lft_);
@@ -295,7 +298,6 @@ TEST_F(MySqlLeaseMgrTest, BasicLease6) {
l1->addr_ = L1_ADDRESS;
l1->prefixlen_ = 0;
l1->iaid_ = 42;
- l1->hwaddr_ = std::vector<uint8_t>(6, 0x42); // Six hex 42's
l1->duid_ = boost::shared_ptr<DUID>(new DUID(vector<uint8_t>(8, 0x42)));
l1->preferred_lft_ = 3600; // Preferred lifetime
l1->valid_lft_ = 3677; // Actual lifetime
@@ -310,23 +312,48 @@ TEST_F(MySqlLeaseMgrTest, BasicLease6) {
l2->addr_ = L2_ADDRESS;
l2->prefixlen_ = 7;
l2->iaid_ = 89;
- l2->hwaddr_ = std::vector<uint8_t>(6, 0xf43); // Six hex 42's
l2->duid_ = boost::shared_ptr<DUID>(new DUID(vector<uint8_t>(8, 0x3a)));
l2->preferred_lft_ = 1800; // Preferred lifetime
l2->valid_lft_ = 5412; // Actual lifetime
l2->cltt_ = 234567; // Current time of day
l2->subnet_id_ = l1->subnet_id_; // Same as l1
+ const IOAddress L3_ADDRESS(std::string("2001:db8::3"));
+ Lease6Ptr l3(new Lease6());
+ initializeUnusedLease6(l3);
+
+ l3->type_ = Lease6::LEASE_IA_NA;
+ l3->addr_ = L3_ADDRESS;
+ l3->prefixlen_ = 28;
+ l3->iaid_ = 0xfffffffe;
+ vector<uint8_t> duid;
+ for (uint8_t i = 0; i < 128; ++i) {
+ duid.push_back(i + 5);
+ }
+ l3->duid_ = boost::shared_ptr<DUID>(new DUID(duid));
+ l3->preferred_lft_ = 0xfffffffc; // Preferred lifetime
+ l3->valid_lft_ = 0xfffffffd; // Actual lifetime
+ l3->cltt_ = 234567; // Current time of day
+ l3->subnet_id_ = l1->subnet_id_; // Same as l1
+
// Sanity check that the leases are different
- ASSERT_FALSE(compareLease6(l1, l2));
+ ASSERT_TRUE(*l1 != *l2);
+ ASSERT_TRUE(*l1 != *l3);
+ ASSERT_TRUE(*l2 != *l3);
- // Start the tests. Add two leases to the database, read them back and
+ // Start the tests. Add three leases to the database, read them back and
// check they are what we think they are.
Lease6Ptr l_returned;
EXPECT_TRUE(lmptr_->addLease(l1));
EXPECT_TRUE(lmptr_->addLease(l2));
+ EXPECT_TRUE(lmptr_->addLease(l3));
+ lmptr_->commit();
+
+ // Reopen the database to ensure that they actually got stored.
+ reopen();
+ // check that the values returned are as expected.
l_returned = lmptr_->getLease6(L1_ADDRESS);
EXPECT_TRUE(l_returned);
detailCompareLease6(l1, l_returned);
@@ -335,6 +362,10 @@ TEST_F(MySqlLeaseMgrTest, BasicLease6) {
EXPECT_TRUE(l_returned);
detailCompareLease6(l2, l_returned);
+ l_returned = lmptr_->getLease6(L3_ADDRESS);
+ EXPECT_TRUE(l_returned);
+ detailCompareLease6(l3, l_returned);
+
// Check that we can't add a second lease with the same address
EXPECT_FALSE(lmptr_->addLease(l1));
More information about the bind10-changes
mailing list