2012-11-23 18:23:21 +00:00
|
|
|
// Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
|
2012-10-16 16:39:32 +01:00
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
|
|
|
|
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
|
|
// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
|
|
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
|
|
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
|
|
|
|
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
// PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
|
|
|
#include <config.h>
|
2012-11-16 11:19:19 +00:00
|
|
|
|
|
|
|
#include <asiolink/io_address.h>
|
|
|
|
#include <dhcpsrv/lease_mgr_factory.h>
|
|
|
|
#include <dhcpsrv/mysql_lease_mgr.h>
|
|
|
|
|
2012-11-16 14:15:45 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2012-12-03 21:23:48 +00:00
|
|
|
#include <algorithm>
|
2012-10-16 16:39:32 +01:00
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
2012-10-20 22:52:25 +01:00
|
|
|
#include <string>
|
2012-11-16 14:15:45 +00:00
|
|
|
#include <utility>
|
2012-10-16 16:39:32 +01:00
|
|
|
|
|
|
|
using namespace isc;
|
|
|
|
using namespace isc::asiolink;
|
|
|
|
using namespace isc::dhcp;
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2012-11-23 18:23:21 +00:00
|
|
|
// This holds statements to create and destroy the schema.
|
2012-11-05 19:29:39 +00:00
|
|
|
#include "schema_copy.h"
|
|
|
|
|
2012-11-23 18:23:21 +00:00
|
|
|
// IPv4 and IPv6 addresses used in the tests
|
2012-11-21 12:44:18 +00:00
|
|
|
const char* ADDRESS4[] = {
|
|
|
|
"192.0.2.0", "192.0.2.1", "192.0.2.2", "192.0.2.3",
|
|
|
|
"192.0.2.4", "192.0.2.5", "192.0.2.6", "192.0.2.7",
|
|
|
|
NULL
|
|
|
|
};
|
2012-11-19 19:37:07 +00:00
|
|
|
const char* ADDRESS6[] = {
|
|
|
|
"2001:db8::0", "2001:db8::1", "2001:db8::2", "2001:db8::3",
|
|
|
|
"2001:db8::4", "2001:db8::5", "2001:db8::6", "2001:db8::7",
|
|
|
|
NULL
|
|
|
|
};
|
2012-10-29 19:19:36 +00:00
|
|
|
|
2012-11-23 18:23:21 +00:00
|
|
|
// Connection strings.
|
2012-10-24 22:14:42 +01:00
|
|
|
// Database: keatest
|
2012-11-23 18:23:21 +00:00
|
|
|
// Host: localhost
|
2012-10-24 22:14:42 +01:00
|
|
|
// Username: keatest
|
|
|
|
// Password: keatest
|
2012-10-16 16:39:32 +01:00
|
|
|
const char* VALID_TYPE = "type=mysql";
|
|
|
|
const char* INVALID_TYPE = "type=unknown";
|
2012-10-17 18:37:22 +01:00
|
|
|
const char* VALID_NAME = "name=keatest";
|
2012-10-16 16:39:32 +01:00
|
|
|
const char* INVALID_NAME = "name=invalidname";
|
|
|
|
const char* VALID_HOST = "host=localhost";
|
|
|
|
const char* INVALID_HOST = "host=invalidhost";
|
|
|
|
const char* VALID_USER = "user=keatest";
|
|
|
|
const char* INVALID_USER = "user=invaliduser";
|
|
|
|
const char* VALID_PASSWORD = "password=keatest";
|
|
|
|
const char* INVALID_PASSWORD = "password=invalid";
|
|
|
|
|
|
|
|
// Given a combination of strings above, produce a connection string.
|
|
|
|
string connectionString(const char* type, const char* name, const char* host,
|
|
|
|
const char* user, const char* password) {
|
|
|
|
const string space = " ";
|
2012-10-24 19:34:38 +01:00
|
|
|
string result = "";
|
|
|
|
|
|
|
|
if (type != NULL) {
|
|
|
|
result += string(type);
|
|
|
|
}
|
2012-12-03 15:35:36 +00:00
|
|
|
if (name != NULL) {
|
2012-10-24 19:34:38 +01:00
|
|
|
if (! result.empty()) {
|
|
|
|
result += space;
|
|
|
|
}
|
|
|
|
result += string(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (host != NULL) {
|
|
|
|
if (! result.empty()) {
|
|
|
|
result += space;
|
|
|
|
}
|
|
|
|
result += string(host);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (user != NULL) {
|
|
|
|
if (! result.empty()) {
|
|
|
|
result += space;
|
|
|
|
}
|
|
|
|
result += string(user);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (password != NULL) {
|
|
|
|
if (! result.empty()) {
|
|
|
|
result += space;
|
|
|
|
}
|
|
|
|
result += string(password);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (result);
|
2012-10-16 16:39:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return valid connection string
|
2012-10-24 22:14:42 +01:00
|
|
|
string
|
|
|
|
validConnectionString() {
|
2012-10-16 16:39:32 +01:00
|
|
|
return (connectionString(VALID_TYPE, VALID_NAME, VALID_HOST,
|
|
|
|
VALID_USER, VALID_PASSWORD));
|
|
|
|
}
|
|
|
|
|
2012-11-05 19:29:39 +00:00
|
|
|
// @brief Clear everything from the database
|
|
|
|
//
|
|
|
|
// There is no error checking in this code: if something fails, one of the
|
2012-11-23 18:23:21 +00:00
|
|
|
// tests will (should) fall over.
|
2012-11-05 19:29:39 +00:00
|
|
|
void destroySchema() {
|
|
|
|
// Initialise
|
|
|
|
MYSQL handle;
|
|
|
|
(void) mysql_init(&handle);
|
|
|
|
|
|
|
|
// Open database
|
|
|
|
(void) mysql_real_connect(&handle, "localhost", "keatest",
|
|
|
|
"keatest", "keatest", 0, NULL, 0);
|
|
|
|
|
|
|
|
// Get rid of everything in it.
|
|
|
|
for (int i = 0; destroy_statement[i] != NULL; ++i) {
|
|
|
|
(void) mysql_query(&handle, destroy_statement[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ... and close
|
|
|
|
(void) mysql_close(&handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
// @brief Create the Schema
|
|
|
|
//
|
|
|
|
// Creates all the tables in what is assumed to be an empty database.
|
|
|
|
//
|
|
|
|
// There is no error checking in this code: if it fails, one of the tests
|
|
|
|
// will fall over.
|
|
|
|
void createSchema() {
|
|
|
|
// Initialise
|
|
|
|
MYSQL handle;
|
|
|
|
(void) mysql_init(&handle);
|
|
|
|
|
|
|
|
// Open database
|
|
|
|
(void) mysql_real_connect(&handle, "localhost", "keatest",
|
|
|
|
"keatest", "keatest", 0, NULL, 0);
|
|
|
|
|
2012-11-23 18:23:21 +00:00
|
|
|
// Execute creation statements.
|
2012-11-05 19:29:39 +00:00
|
|
|
for (int i = 0; create_statement[i] != NULL; ++i) {
|
|
|
|
(void) mysql_query(&handle, create_statement[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ... and close
|
|
|
|
(void) mysql_close(&handle);
|
|
|
|
}
|
|
|
|
|
2012-11-23 18:23:21 +00:00
|
|
|
/// @brief Test fixture class for testing MySQL Lease Manager
|
|
|
|
///
|
|
|
|
/// Opens the database prior to each test and closes it afterwards.
|
|
|
|
/// All pending transactions are deleted prior to closure.
|
2012-10-20 22:52:25 +01:00
|
|
|
|
|
|
|
class MySqlLeaseMgrTest : public ::testing::Test {
|
|
|
|
public:
|
2012-11-23 18:23:21 +00:00
|
|
|
/// @brief Constructor
|
|
|
|
///
|
|
|
|
/// Deletes everything from the database and opens it.
|
2012-11-19 19:37:07 +00:00
|
|
|
MySqlLeaseMgrTest() {
|
|
|
|
// Initialize address strings and IOAddresses
|
2012-11-21 12:44:18 +00:00
|
|
|
for (int i = 0; ADDRESS4[i] != NULL; ++i) {
|
|
|
|
string addr(ADDRESS4[i]);
|
|
|
|
straddress4_.push_back(addr);
|
|
|
|
IOAddress ioaddr(addr);
|
|
|
|
ioaddress4_.push_back(ioaddr);
|
|
|
|
}
|
|
|
|
|
2012-11-19 19:37:07 +00:00
|
|
|
for (int i = 0; ADDRESS6[i] != NULL; ++i) {
|
|
|
|
string addr(ADDRESS6[i]);
|
|
|
|
straddress6_.push_back(addr);
|
|
|
|
IOAddress ioaddr(addr);
|
|
|
|
ioaddress6_.push_back(ioaddr);
|
|
|
|
}
|
2012-11-08 13:14:19 +00:00
|
|
|
|
2012-12-03 15:35:36 +00:00
|
|
|
// Ensure schema is the correct one.
|
2012-11-05 19:29:39 +00:00
|
|
|
destroySchema();
|
|
|
|
createSchema();
|
2012-11-21 12:44:18 +00:00
|
|
|
|
2012-12-03 15:35:36 +00:00
|
|
|
// Connect to the database
|
2012-11-08 13:14:19 +00:00
|
|
|
try {
|
|
|
|
LeaseMgrFactory::create(validConnectionString());
|
|
|
|
} catch (...) {
|
|
|
|
std::cerr << "*** ERROR: unable to open database. The test\n"
|
|
|
|
"*** environment is broken and must be fixed before\n"
|
|
|
|
"*** the MySQL tests will run correctly.\n"
|
|
|
|
"*** The reason for the problem is described in the\n"
|
|
|
|
"*** accompanying exception output.\n";
|
|
|
|
throw;
|
|
|
|
}
|
2012-10-24 22:14:42 +01:00
|
|
|
lmptr_ = &(LeaseMgrFactory::instance());
|
|
|
|
}
|
|
|
|
|
2012-11-23 18:23:21 +00:00
|
|
|
/// @brief Destructor
|
|
|
|
///
|
2012-12-03 15:35:36 +00:00
|
|
|
/// Rolls back all pending transactions. The deletion of lmptr_ will close
|
|
|
|
/// the database. Then reopen it and delete everything created by the test.
|
2012-10-29 19:19:36 +00:00
|
|
|
virtual ~MySqlLeaseMgrTest() {
|
|
|
|
lmptr_->rollback();
|
|
|
|
LeaseMgrFactory::destroy();
|
2012-11-05 19:29:39 +00:00
|
|
|
destroySchema();
|
2012-10-29 19:19:36 +00:00
|
|
|
}
|
|
|
|
|
2012-11-23 18:23:21 +00:00
|
|
|
/// @brief Reopen the database
|
|
|
|
///
|
|
|
|
/// Closes the database and re-open it. Anything committed should be
|
|
|
|
/// visible.
|
2012-10-24 22:14:42 +01:00
|
|
|
void reopen() {
|
|
|
|
LeaseMgrFactory::destroy();
|
2012-10-24 14:12:06 +01:00
|
|
|
LeaseMgrFactory::create(validConnectionString());
|
|
|
|
lmptr_ = &(LeaseMgrFactory::instance());
|
2012-10-20 22:52:25 +01:00
|
|
|
}
|
|
|
|
|
2012-11-23 18:23:21 +00:00
|
|
|
/// @brief Initialize Lease4 Fields
|
|
|
|
///
|
2012-12-03 15:35:36 +00:00
|
|
|
/// Returns a pointer to a Lease4 structure. Different values are put into
|
|
|
|
/// the lease according to the address passed.
|
2012-11-23 18:23:21 +00:00
|
|
|
///
|
|
|
|
/// This is just a convenience function for the test methods.
|
|
|
|
///
|
|
|
|
/// @param address Address to use for the initialization
|
|
|
|
///
|
2012-12-10 10:35:28 +00:00
|
|
|
/// @return Lease4Ptr. This will not point to anything if the
|
|
|
|
/// initialization failed (e.g. unknown address).
|
2012-11-21 12:44:18 +00:00
|
|
|
Lease4Ptr initializeLease4(std::string address) {
|
|
|
|
Lease4Ptr lease(new Lease4());
|
|
|
|
|
|
|
|
// Set the address of the lease
|
|
|
|
lease->addr_ = IOAddress(address);
|
|
|
|
|
|
|
|
// Initialize unused fields.
|
|
|
|
lease->ext_ = 0; // Not saved
|
|
|
|
lease->t1_ = 0; // Not saved
|
|
|
|
lease->t2_ = 0; // Not saved
|
|
|
|
lease->fixed_ = false; // Unused
|
|
|
|
lease->hostname_ = std::string(""); // Unused
|
|
|
|
lease->fqdn_fwd_ = false; // Unused
|
|
|
|
lease->fqdn_rev_ = false; // Unused
|
|
|
|
lease->comments_ = std::string(""); // Unused
|
|
|
|
|
|
|
|
// Set other parameters. For historical reasons, address 0 is not used.
|
|
|
|
if (address == straddress4_[0]) {
|
|
|
|
lease->hwaddr_ = vector<uint8_t>(6, 0x08);
|
2012-12-10 10:35:28 +00:00
|
|
|
lease->client_id_ = ClientIdPtr(
|
2012-11-23 12:47:40 +00:00
|
|
|
new ClientId(vector<uint8_t>(8, 0x42)));
|
|
|
|
lease->valid_lft_ = 8677;
|
|
|
|
lease->cltt_ = 168256;
|
|
|
|
lease->subnet_id_ = 23;
|
2012-11-21 12:44:18 +00:00
|
|
|
|
|
|
|
} else if (address == straddress4_[1]) {
|
|
|
|
lease->hwaddr_ = vector<uint8_t>(6, 0x19);
|
2012-12-10 10:35:28 +00:00
|
|
|
lease->client_id_ = ClientIdPtr(
|
2012-11-23 12:47:40 +00:00
|
|
|
new ClientId(vector<uint8_t>(8, 0x53)));
|
|
|
|
lease->valid_lft_ = 3677;
|
|
|
|
lease->cltt_ = 123456;
|
|
|
|
lease->subnet_id_ = 73;
|
2012-11-21 12:44:18 +00:00
|
|
|
|
|
|
|
} else if (address == straddress4_[2]) {
|
|
|
|
lease->hwaddr_ = vector<uint8_t>(6, 0x2a);
|
2012-12-10 10:35:28 +00:00
|
|
|
lease->client_id_ = ClientIdPtr(
|
2012-11-23 12:47:40 +00:00
|
|
|
new ClientId(vector<uint8_t>(8, 0x64)));
|
|
|
|
lease->valid_lft_ = 5412;
|
|
|
|
lease->cltt_ = 234567;
|
|
|
|
lease->subnet_id_ = 73; // Same as lease 1
|
2012-11-21 12:44:18 +00:00
|
|
|
|
|
|
|
} else if (address == straddress4_[3]) {
|
2012-11-23 12:47:40 +00:00
|
|
|
lease->hwaddr_ = vector<uint8_t>(6, 0x19); // Same as lease 1
|
2012-12-10 10:35:28 +00:00
|
|
|
lease->client_id_ = ClientIdPtr(
|
2012-11-23 12:47:40 +00:00
|
|
|
new ClientId(vector<uint8_t>(8, 0x75)));
|
2012-11-21 12:44:18 +00:00
|
|
|
|
|
|
|
// The times used in the next tests are deliberately restricted - we
|
|
|
|
// should be able to cope with valid lifetimes up to 0xffffffff.
|
|
|
|
// However, this will lead to overflows.
|
|
|
|
// @TODO: test overflow conditions when code has been fixed
|
2012-11-23 12:47:40 +00:00
|
|
|
lease->valid_lft_ = 7000;
|
|
|
|
lease->cltt_ = 234567;
|
|
|
|
lease->subnet_id_ = 37;
|
2012-11-21 12:44:18 +00:00
|
|
|
|
|
|
|
} else if (address == straddress4_[4]) {
|
|
|
|
lease->hwaddr_ = vector<uint8_t>(6, 0x4c);
|
|
|
|
// Same ClientId as straddr4_[1]
|
2012-12-10 10:35:28 +00:00
|
|
|
lease->client_id_ = ClientIdPtr(
|
2012-11-23 12:47:40 +00:00
|
|
|
new ClientId(vector<uint8_t>(8, 0x53))); // Same as lease 1
|
|
|
|
lease->valid_lft_ = 7736;
|
|
|
|
lease->cltt_ = 222456;
|
|
|
|
lease->subnet_id_ = 85;
|
2012-11-21 12:44:18 +00:00
|
|
|
|
|
|
|
} else if (address == straddress4_[5]) {
|
2012-11-23 12:47:40 +00:00
|
|
|
lease->hwaddr_ = vector<uint8_t>(6, 0x19); // Same as lease 1
|
2012-11-21 12:44:18 +00:00
|
|
|
// Same ClientId and IAID as straddress4_1
|
2012-12-10 10:35:28 +00:00
|
|
|
lease->client_id_ = ClientIdPtr(
|
2012-11-23 12:47:40 +00:00
|
|
|
new ClientId(vector<uint8_t>(8, 0x53))); // Same as lease 1
|
|
|
|
lease->valid_lft_ = 7832;
|
|
|
|
lease->cltt_ = 227476;
|
|
|
|
lease->subnet_id_ = 175;
|
2012-11-21 12:44:18 +00:00
|
|
|
|
|
|
|
} else if (address == straddress4_[6]) {
|
|
|
|
lease->hwaddr_ = vector<uint8_t>(6, 0x6e);
|
|
|
|
// Same ClientId as straddress4_1
|
2012-12-10 10:35:28 +00:00
|
|
|
lease->client_id_ = ClientIdPtr(
|
2012-11-23 12:47:40 +00:00
|
|
|
new ClientId(vector<uint8_t>(8, 0x53))); // Same as lease 1
|
|
|
|
lease->valid_lft_ = 1832;
|
|
|
|
lease->cltt_ = 627476;
|
|
|
|
lease->subnet_id_ = 112;
|
2012-11-21 12:44:18 +00:00
|
|
|
|
|
|
|
} else if (address == straddress4_[7]) {
|
2012-11-23 12:47:40 +00:00
|
|
|
lease->hwaddr_ = vector<uint8_t>(); // Empty
|
2012-12-10 10:35:28 +00:00
|
|
|
lease->client_id_ = ClientIdPtr(
|
2012-11-23 12:47:40 +00:00
|
|
|
new ClientId(vector<uint8_t>())); // Empty
|
|
|
|
lease->valid_lft_ = 7975;
|
|
|
|
lease->cltt_ = 213876;
|
|
|
|
lease->subnet_id_ = 19;
|
2012-11-21 12:44:18 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
// Unknown address, return an empty pointer.
|
|
|
|
lease.reset();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return (lease);
|
|
|
|
}
|
|
|
|
|
2012-11-23 18:23:21 +00:00
|
|
|
/// @brief Initialize Lease6 Fields
|
|
|
|
///
|
2012-12-03 15:35:36 +00:00
|
|
|
/// Returns a pointer to a Lease6 structure. Different values are put into
|
|
|
|
/// the lease according to the address passed.
|
2012-11-23 18:23:21 +00:00
|
|
|
///
|
|
|
|
/// This is just a convenience function for the test methods.
|
|
|
|
///
|
|
|
|
/// @param address Address to use for the initialization
|
|
|
|
///
|
|
|
|
/// @return Lease6Ptr. This will not point to anything if the initialization
|
|
|
|
/// failed (e.g. unknown address).
|
2012-10-29 19:19:36 +00:00
|
|
|
Lease6Ptr initializeLease6(std::string address) {
|
|
|
|
Lease6Ptr lease(new Lease6());
|
|
|
|
|
|
|
|
// Set the address of the lease
|
|
|
|
lease->addr_ = IOAddress(address);
|
|
|
|
|
|
|
|
// Initialize unused fields.
|
|
|
|
lease->t1_ = 0; // Not saved
|
|
|
|
lease->t2_ = 0; // Not saved
|
|
|
|
lease->fixed_ = false; // Unused
|
|
|
|
lease->hostname_ = std::string(""); // Unused
|
|
|
|
lease->fqdn_fwd_ = false; // Unused
|
|
|
|
lease->fqdn_rev_ = false; // Unused
|
|
|
|
lease->comments_ = std::string(""); // Unused
|
|
|
|
|
2012-11-19 19:37:07 +00:00
|
|
|
// Set other parameters. For historical reasons, address 0 is not used.
|
|
|
|
if (address == straddress6_[0]) {
|
2012-11-03 18:26:57 +00:00
|
|
|
lease->type_ = Lease6::LEASE_IA_TA;
|
|
|
|
lease->prefixlen_ = 4;
|
|
|
|
lease->iaid_ = 142;
|
2012-12-03 15:35:36 +00:00
|
|
|
lease->duid_ = DuidPtr(new DUID(vector<uint8_t>(8, 0x77)));
|
2012-11-23 18:23:21 +00:00
|
|
|
lease->preferred_lft_ = 900;
|
|
|
|
lease->valid_lft_ = 8677;
|
|
|
|
lease->cltt_ = 168256;
|
|
|
|
lease->subnet_id_ = 23;
|
2012-11-03 18:26:57 +00:00
|
|
|
|
2012-11-19 19:37:07 +00:00
|
|
|
} else if (address == straddress6_[1]) {
|
2012-10-29 19:19:36 +00:00
|
|
|
lease->type_ = Lease6::LEASE_IA_TA;
|
|
|
|
lease->prefixlen_ = 0;
|
|
|
|
lease->iaid_ = 42;
|
2012-12-03 15:35:36 +00:00
|
|
|
lease->duid_ = DuidPtr(new DUID(vector<uint8_t>(8, 0x42)));
|
2012-11-23 18:23:21 +00:00
|
|
|
lease->preferred_lft_ = 3600;
|
|
|
|
lease->valid_lft_ = 3677;
|
|
|
|
lease->cltt_ = 123456;
|
|
|
|
lease->subnet_id_ = 73;
|
2012-10-29 19:19:36 +00:00
|
|
|
|
2012-11-19 19:37:07 +00:00
|
|
|
} else if (address == straddress6_[2]) {
|
2012-10-29 19:19:36 +00:00
|
|
|
lease->type_ = Lease6::LEASE_IA_PD;
|
|
|
|
lease->prefixlen_ = 7;
|
|
|
|
lease->iaid_ = 89;
|
2012-12-03 15:35:36 +00:00
|
|
|
lease->duid_ = DuidPtr(new DUID(vector<uint8_t>(8, 0x3a)));
|
2012-11-23 18:23:21 +00:00
|
|
|
lease->preferred_lft_ = 1800;
|
|
|
|
lease->valid_lft_ = 5412;
|
|
|
|
lease->cltt_ = 234567;
|
|
|
|
lease->subnet_id_ = 73; // Same as lease 1
|
2012-10-29 19:19:36 +00:00
|
|
|
|
2012-11-19 19:37:07 +00:00
|
|
|
} else if (address == straddress6_[3]) {
|
2012-10-29 19:19:36 +00:00
|
|
|
lease->type_ = Lease6::LEASE_IA_NA;
|
|
|
|
lease->prefixlen_ = 28;
|
|
|
|
lease->iaid_ = 0xfffffffe;
|
|
|
|
vector<uint8_t> duid;
|
2012-11-03 18:26:57 +00:00
|
|
|
for (uint8_t i = 31; i < 126; ++i) {
|
|
|
|
duid.push_back(i);
|
2012-10-29 19:19:36 +00:00
|
|
|
}
|
2012-12-03 15:35:36 +00:00
|
|
|
lease->duid_ = DuidPtr(new DUID(duid));
|
2012-10-29 19:19:36 +00:00
|
|
|
|
|
|
|
// The times used in the next tests are deliberately restricted - we
|
|
|
|
// should be able to cope with valid lifetimes up to 0xffffffff.
|
|
|
|
// However, this will lead to overflows.
|
|
|
|
// @TODO: test overflow conditions when code has been fixed
|
2012-11-23 18:23:21 +00:00
|
|
|
lease->preferred_lft_ = 7200;
|
|
|
|
lease->valid_lft_ = 7000;
|
|
|
|
lease->cltt_ = 234567;
|
|
|
|
lease->subnet_id_ = 37;
|
2012-10-29 19:19:36 +00:00
|
|
|
|
2012-11-19 19:37:07 +00:00
|
|
|
} else if (address == straddress6_[4]) {
|
|
|
|
// Same DUID and IAID as straddress6_1
|
2012-11-03 18:26:57 +00:00
|
|
|
lease->type_ = Lease6::LEASE_IA_PD;
|
|
|
|
lease->prefixlen_ = 15;
|
|
|
|
lease->iaid_ = 42;
|
2012-12-03 15:35:36 +00:00
|
|
|
lease->duid_ = DuidPtr(new DUID(vector<uint8_t>(8, 0x42)));
|
2012-11-23 18:23:21 +00:00
|
|
|
lease->preferred_lft_ = 4800;
|
|
|
|
lease->valid_lft_ = 7736;
|
|
|
|
lease->cltt_ = 222456;
|
|
|
|
lease->subnet_id_ = 671;
|
2012-11-03 18:26:57 +00:00
|
|
|
|
2012-11-19 19:37:07 +00:00
|
|
|
} else if (address == straddress6_[5]) {
|
|
|
|
// Same DUID and IAID as straddress6_1
|
2012-11-03 18:26:57 +00:00
|
|
|
lease->type_ = Lease6::LEASE_IA_PD;
|
|
|
|
lease->prefixlen_ = 24;
|
2012-11-23 18:23:21 +00:00
|
|
|
lease->iaid_ = 42; // Same as lease 4
|
2012-12-03 15:35:36 +00:00
|
|
|
lease->duid_ = DuidPtr(new DUID(vector<uint8_t>(8, 0x42)));
|
|
|
|
// Same as lease 4
|
2012-11-23 18:23:21 +00:00
|
|
|
lease->preferred_lft_ = 5400;
|
|
|
|
lease->valid_lft_ = 7832;
|
|
|
|
lease->cltt_ = 227476;
|
|
|
|
lease->subnet_id_ = 175;
|
2012-11-03 18:26:57 +00:00
|
|
|
|
2012-11-19 19:37:07 +00:00
|
|
|
} else if (address == straddress6_[6]) {
|
|
|
|
// Same DUID as straddress6_1
|
2012-11-03 18:26:57 +00:00
|
|
|
lease->type_ = Lease6::LEASE_IA_PD;
|
|
|
|
lease->prefixlen_ = 24;
|
|
|
|
lease->iaid_ = 93;
|
2012-12-03 15:35:36 +00:00
|
|
|
lease->duid_ = DuidPtr(new DUID(vector<uint8_t>(8, 0x42)));
|
|
|
|
// Same as lease 4
|
2012-11-23 18:23:21 +00:00
|
|
|
lease->preferred_lft_ = 5400;
|
|
|
|
lease->valid_lft_ = 1832;
|
|
|
|
lease->cltt_ = 627476;
|
|
|
|
lease->subnet_id_ = 112;
|
2012-11-03 18:26:57 +00:00
|
|
|
|
2012-11-19 19:37:07 +00:00
|
|
|
} else if (address == straddress6_[7]) {
|
|
|
|
// Same IAID as straddress6_1
|
2012-11-03 18:26:57 +00:00
|
|
|
lease->type_ = Lease6::LEASE_IA_PD;
|
|
|
|
lease->prefixlen_ = 24;
|
|
|
|
lease->iaid_ = 42;
|
2012-12-03 15:35:36 +00:00
|
|
|
lease->duid_ = DuidPtr(new DUID(vector<uint8_t>(8, 0xe5)));
|
2012-11-23 18:23:21 +00:00
|
|
|
lease->preferred_lft_ = 5600;
|
|
|
|
lease->valid_lft_ = 7975;
|
|
|
|
lease->cltt_ = 213876;
|
|
|
|
lease->subnet_id_ = 19;
|
2012-11-03 18:26:57 +00:00
|
|
|
|
2012-10-29 19:19:36 +00:00
|
|
|
} else {
|
|
|
|
// Unknown address, return an empty pointer.
|
|
|
|
lease.reset();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return (lease);
|
|
|
|
}
|
|
|
|
|
2012-11-23 18:23:21 +00:00
|
|
|
/// @brief Check Leases present and different
|
|
|
|
///
|
|
|
|
/// Checks a vector of lease pointers and ensures that all the leases
|
|
|
|
/// they point to are present and different. If not, a GTest assertion
|
|
|
|
/// will fail.
|
|
|
|
///
|
|
|
|
/// @param leases Vector of pointers to leases
|
2012-11-21 12:44:18 +00:00
|
|
|
template <typename T>
|
|
|
|
void checkLeasesDifferent(const std::vector<T> leases) const {
|
2012-11-03 18:26:57 +00:00
|
|
|
|
|
|
|
// Check they were created
|
|
|
|
for (int i = 0; i < leases.size(); ++i) {
|
2012-11-21 19:38:26 +00:00
|
|
|
ASSERT_TRUE(leases[i]);
|
2012-11-03 18:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check they are different
|
|
|
|
for (int i = 0; i < (leases.size() - 1); ++i) {
|
|
|
|
for (int j = (i + 1); j < leases.size(); ++j) {
|
2012-12-03 15:35:36 +00:00
|
|
|
stringstream s;
|
|
|
|
s << "Comparing leases " << i << " & " << j << " for equality";
|
|
|
|
SCOPED_TRACE(s.str());
|
|
|
|
EXPECT_TRUE(*leases[i] != *leases[j]);
|
2012-11-03 18:26:57 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-21 12:44:18 +00:00
|
|
|
}
|
|
|
|
|
2012-11-23 18:23:21 +00:00
|
|
|
/// @brief Creates leases for the test
|
|
|
|
///
|
|
|
|
/// Creates all leases for the test and checks that they are different.
|
|
|
|
///
|
|
|
|
/// @return vector<Lease4Ptr> Vector of pointers to leases
|
2012-11-21 12:44:18 +00:00
|
|
|
vector<Lease4Ptr> createLeases4() {
|
|
|
|
|
|
|
|
// Create leases for each address
|
|
|
|
vector<Lease4Ptr> leases;
|
|
|
|
for (int i = 0; i < straddress4_.size(); ++i) {
|
|
|
|
leases.push_back(initializeLease4(straddress4_[i]));
|
|
|
|
}
|
|
|
|
EXPECT_EQ(8, leases.size());
|
|
|
|
|
|
|
|
// Check all were created and that they are different.
|
|
|
|
checkLeasesDifferent(leases);
|
|
|
|
|
|
|
|
return (leases);
|
|
|
|
}
|
|
|
|
|
2012-11-23 18:23:21 +00:00
|
|
|
/// @brief Creates leases for the test
|
|
|
|
///
|
|
|
|
/// Creates all leases for the test and checks that they are different.
|
|
|
|
///
|
|
|
|
/// @return vector<Lease6Ptr> Vector of pointers to leases
|
2012-11-21 12:44:18 +00:00
|
|
|
vector<Lease6Ptr> createLeases6() {
|
|
|
|
|
|
|
|
// Create leases for each address
|
|
|
|
vector<Lease6Ptr> leases;
|
|
|
|
for (int i = 0; i < straddress6_.size(); ++i) {
|
|
|
|
leases.push_back(initializeLease6(straddress6_[i]));
|
|
|
|
}
|
|
|
|
EXPECT_EQ(8, leases.size());
|
|
|
|
|
|
|
|
// Check all were created and that they are different.
|
|
|
|
checkLeasesDifferent(leases);
|
2012-11-03 18:26:57 +00:00
|
|
|
|
|
|
|
return (leases);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-29 19:19:36 +00:00
|
|
|
// Member variables
|
|
|
|
|
2012-11-23 18:23:21 +00:00
|
|
|
LeaseMgr* lmptr_; ///< Pointer to the lease manager
|
|
|
|
vector<string> straddress4_; ///< String forms of IPv4 addresses
|
|
|
|
vector<IOAddress> ioaddress4_; ///< IOAddress forms of IPv4 addresses
|
|
|
|
vector<string> straddress6_; ///< String forms of IPv6 addresses
|
|
|
|
vector<IOAddress> ioaddress6_; ///< IOAddress forms of IPv6 addresses
|
2012-10-20 22:52:25 +01:00
|
|
|
};
|
|
|
|
|
2012-12-03 15:35:36 +00:00
|
|
|
///@{
|
|
|
|
/// @brief Test Utilities
|
|
|
|
///
|
|
|
|
/// The follow are a set of functions used during the tests.
|
|
|
|
|
|
|
|
/// @brief Compare two Lease4 structures for equality
|
|
|
|
void
|
|
|
|
detailCompareLease(const Lease4Ptr& first, const Lease4Ptr& second) {
|
|
|
|
// Compare address strings. Comparison of address objects is not used, as
|
|
|
|
// odd things happen when they are different: the EXPECT_EQ macro appears to
|
|
|
|
// call the operator uint32_t() function, which causes an exception to be
|
|
|
|
// thrown for IPv6 addresses.
|
|
|
|
EXPECT_EQ(first->addr_.toText(), second->addr_.toText());
|
|
|
|
EXPECT_TRUE(first->hwaddr_ == second->hwaddr_);
|
|
|
|
EXPECT_TRUE(*first->client_id_ == *second->client_id_);
|
|
|
|
EXPECT_EQ(first->valid_lft_, second->valid_lft_);
|
|
|
|
EXPECT_EQ(first->cltt_, second->cltt_);
|
|
|
|
EXPECT_EQ(first->subnet_id_, second->subnet_id_);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @brief Compare two Lease6 structures for equality
|
|
|
|
void
|
|
|
|
detailCompareLease(const Lease6Ptr& first, const Lease6Ptr& second) {
|
|
|
|
EXPECT_EQ(first->type_, second->type_);
|
|
|
|
|
|
|
|
// Compare address strings. Comparison of address objects is not used, as
|
|
|
|
// odd things happen when they are different: the EXPECT_EQ macro appears to
|
|
|
|
// call the operator uint32_t() function, which causes an exception to be
|
|
|
|
// thrown for IPv6 addresses.
|
|
|
|
EXPECT_EQ(first->addr_.toText(), second->addr_.toText());
|
|
|
|
EXPECT_EQ(first->prefixlen_, second->prefixlen_);
|
|
|
|
EXPECT_EQ(first->iaid_, second->iaid_);
|
|
|
|
EXPECT_TRUE(*first->duid_ == *second->duid_);
|
|
|
|
EXPECT_EQ(first->preferred_lft_, second->preferred_lft_);
|
|
|
|
EXPECT_EQ(first->valid_lft_, second->valid_lft_);
|
|
|
|
EXPECT_EQ(first->cltt_, second->cltt_);
|
|
|
|
EXPECT_EQ(first->subnet_id_, second->subnet_id_);
|
|
|
|
}
|
|
|
|
|
|
|
|
///@}
|
|
|
|
|
2012-10-16 16:39:32 +01:00
|
|
|
|
2012-11-23 18:23:21 +00:00
|
|
|
/// @brief Check that database can be opened
|
|
|
|
///
|
|
|
|
/// This test checks if the MySqlLeaseMgr can be instantiated. This happens
|
|
|
|
/// only if the database can be opened. Note that this is not part of the
|
|
|
|
/// MySqlLeaseMgr test fixure set. This test checks that the database can be
|
|
|
|
/// opened: the fixtures assume that and check basic operations.
|
2012-10-20 22:52:25 +01:00
|
|
|
|
|
|
|
TEST(MySqlOpenTest, OpenDatabase) {
|
2012-11-05 19:29:39 +00:00
|
|
|
|
|
|
|
// Schema needs to be created for the test to work.
|
|
|
|
destroySchema();
|
|
|
|
createSchema();
|
|
|
|
|
|
|
|
// Check that lease manager open the database opens correctly and tidy up.
|
|
|
|
// If it fails, print the error message.
|
2012-10-24 21:18:13 +01:00
|
|
|
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"
|
2012-11-08 13:14:19 +00:00
|
|
|
<< " " << ex.what() << "\n"
|
2012-10-24 21:18:13 +01:00
|
|
|
<< "*** The test environment is broken and must be fixed\n"
|
|
|
|
<< "*** before the MySQL tests will run correctly.\n";
|
|
|
|
}
|
|
|
|
|
2012-10-24 14:12:06 +01:00
|
|
|
// Check that attempting to get an instance of the lease manager when
|
|
|
|
// none is set throws an exception.
|
|
|
|
EXPECT_THROW(LeaseMgrFactory::instance(), NoLeaseManager);
|
2012-10-16 16:39:32 +01:00
|
|
|
|
2012-10-17 18:37:22 +01:00
|
|
|
// Check that wrong specification of backend throws an exception.
|
|
|
|
// (This is really a check on LeaseMgrFactory, but is convenient to
|
|
|
|
// perform here.)
|
2012-10-24 19:34:38 +01:00
|
|
|
EXPECT_THROW(LeaseMgrFactory::create(connectionString(
|
|
|
|
NULL, VALID_NAME, VALID_HOST, INVALID_USER, VALID_PASSWORD)),
|
|
|
|
InvalidParameter);
|
2012-10-24 14:12:06 +01:00
|
|
|
EXPECT_THROW(LeaseMgrFactory::create(connectionString(
|
2012-10-16 16:39:32 +01:00
|
|
|
INVALID_TYPE, VALID_NAME, VALID_HOST, VALID_USER, VALID_PASSWORD)),
|
2012-10-24 14:12:06 +01:00
|
|
|
InvalidType);
|
2012-10-17 18:37:22 +01:00
|
|
|
|
|
|
|
// Check that invalid login data causes an exception.
|
2012-10-24 14:12:06 +01:00
|
|
|
EXPECT_THROW(LeaseMgrFactory::create(connectionString(
|
2012-10-16 16:39:32 +01:00
|
|
|
VALID_TYPE, INVALID_NAME, VALID_HOST, VALID_USER, VALID_PASSWORD)),
|
|
|
|
DbOpenError);
|
2012-10-24 14:12:06 +01:00
|
|
|
EXPECT_THROW(LeaseMgrFactory::create(connectionString(
|
2012-10-16 16:39:32 +01:00
|
|
|
VALID_TYPE, VALID_NAME, INVALID_HOST, VALID_USER, VALID_PASSWORD)),
|
|
|
|
DbOpenError);
|
2012-10-24 14:12:06 +01:00
|
|
|
EXPECT_THROW(LeaseMgrFactory::create(connectionString(
|
2012-10-16 16:39:32 +01:00
|
|
|
VALID_TYPE, VALID_NAME, VALID_HOST, INVALID_USER, VALID_PASSWORD)),
|
|
|
|
DbOpenError);
|
2012-10-24 14:12:06 +01:00
|
|
|
EXPECT_THROW(LeaseMgrFactory::create(connectionString(
|
2012-10-16 16:39:32 +01:00
|
|
|
VALID_TYPE, VALID_NAME, VALID_HOST, VALID_USER, INVALID_PASSWORD)),
|
|
|
|
DbOpenError);
|
|
|
|
|
2012-10-24 19:34:38 +01:00
|
|
|
// Check for missing parameters
|
|
|
|
EXPECT_THROW(LeaseMgrFactory::create(connectionString(
|
|
|
|
VALID_TYPE, NULL, VALID_HOST, INVALID_USER, VALID_PASSWORD)),
|
|
|
|
NoDatabaseName);
|
2012-11-05 19:29:39 +00:00
|
|
|
|
|
|
|
// Tidy up after the test
|
|
|
|
destroySchema();
|
2012-10-18 11:33:51 +01:00
|
|
|
}
|
|
|
|
|
2012-11-23 18:23:21 +00:00
|
|
|
/// @brief Check the getType() method
|
|
|
|
///
|
|
|
|
/// getType() returns a string giving the type of the backend, which should
|
|
|
|
/// always be "mysql".
|
2012-11-13 15:43:42 +00:00
|
|
|
TEST_F(MySqlLeaseMgrTest, getType) {
|
2012-11-13 12:14:55 +00:00
|
|
|
EXPECT_EQ(std::string("mysql"), lmptr_->getType());
|
|
|
|
}
|
|
|
|
|
2012-11-23 18:23:21 +00:00
|
|
|
/// @brief Check conversion functions
|
|
|
|
///
|
|
|
|
/// The server works using cltt and valid_filetime. In the database, the
|
|
|
|
/// information is stored as expire_time and valid-lifetime, which are
|
|
|
|
/// related by
|
|
|
|
///
|
|
|
|
/// expire_time = cltt + valid_lifetime
|
|
|
|
///
|
|
|
|
/// This test checks that the conversion is correct. It does not check that the
|
|
|
|
/// data is entered into the database correctly, only that the MYSQL_TIME
|
|
|
|
/// structure used for the entry is correctly set up.
|
2012-11-14 14:30:46 +00:00
|
|
|
TEST_F(MySqlLeaseMgrTest, checkTimeConversion) {
|
2012-10-20 22:52:25 +01:00
|
|
|
const time_t cltt = time(NULL);
|
|
|
|
const uint32_t valid_lft = 86400; // 1 day
|
2012-10-24 21:18:13 +01:00
|
|
|
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);
|
2012-10-16 16:39:32 +01:00
|
|
|
|
2012-10-20 22:52:25 +01:00
|
|
|
// Convert back
|
|
|
|
time_t converted_cltt = 0;
|
2012-10-24 21:18:13 +01:00
|
|
|
MySqlLeaseMgr::convertFromDatabaseTime(mysql_expire, valid_lft, converted_cltt);
|
2012-10-20 22:52:25 +01:00
|
|
|
EXPECT_EQ(cltt, converted_cltt);
|
|
|
|
}
|
|
|
|
|
2012-10-29 19:19:36 +00:00
|
|
|
|
2012-11-23 18:23:21 +00:00
|
|
|
/// @brief Check getName() returns correct database name
|
2012-10-29 19:19:36 +00:00
|
|
|
TEST_F(MySqlLeaseMgrTest, getName) {
|
|
|
|
EXPECT_EQ(std::string("keatest"), lmptr_->getName());
|
|
|
|
}
|
|
|
|
|
2012-11-23 18:23:21 +00:00
|
|
|
/// @brief Check that getVersion() returns the expected version
|
2012-11-14 14:30:46 +00:00
|
|
|
TEST_F(MySqlLeaseMgrTest, checkVersion) {
|
2012-10-18 11:33:51 +01:00
|
|
|
// Check version
|
2012-10-20 22:52:25 +01:00
|
|
|
pair<uint32_t, uint32_t> version;
|
|
|
|
ASSERT_NO_THROW(version = lmptr_->getVersion());
|
2012-10-24 21:18:13 +01:00
|
|
|
EXPECT_EQ(CURRENT_VERSION_VERSION, version.first);
|
|
|
|
EXPECT_EQ(CURRENT_VERSION_MINOR, version.second);
|
2012-10-20 22:52:25 +01:00
|
|
|
}
|
|
|
|
|
2012-12-03 15:35:36 +00:00
|
|
|
/// @brief Basic Lease4 Checks
|
2012-11-23 18:23:21 +00:00
|
|
|
///
|
2012-12-03 15:35:36 +00:00
|
|
|
/// Checks that the addLease, getLease4 (by address) and deleteLease4 works.
|
2012-11-21 12:44:18 +00:00
|
|
|
TEST_F(MySqlLeaseMgrTest, basicLease4) {
|
|
|
|
// Get the leases to be used for the test.
|
|
|
|
vector<Lease4Ptr> leases = createLeases4();
|
|
|
|
|
|
|
|
// Start the tests. Add three leases to the database, read them back and
|
|
|
|
// check they are what we think they are.
|
|
|
|
EXPECT_TRUE(lmptr_->addLease(leases[1]));
|
|
|
|
EXPECT_TRUE(lmptr_->addLease(leases[2]));
|
|
|
|
EXPECT_TRUE(lmptr_->addLease(leases[3]));
|
|
|
|
lmptr_->commit();
|
|
|
|
|
|
|
|
// Reopen the database to ensure that they actually got stored.
|
|
|
|
reopen();
|
|
|
|
|
|
|
|
Lease4Ptr l_returned = lmptr_->getLease4(ioaddress4_[1]);
|
2012-11-21 19:38:26 +00:00
|
|
|
ASSERT_TRUE(l_returned);
|
2012-11-21 12:44:18 +00:00
|
|
|
detailCompareLease(leases[1], l_returned);
|
|
|
|
|
|
|
|
l_returned = lmptr_->getLease4(ioaddress4_[2]);
|
2012-11-21 19:38:26 +00:00
|
|
|
ASSERT_TRUE(l_returned);
|
2012-11-21 12:44:18 +00:00
|
|
|
detailCompareLease(leases[2], l_returned);
|
|
|
|
|
|
|
|
l_returned = lmptr_->getLease4(ioaddress4_[3]);
|
2012-11-21 19:38:26 +00:00
|
|
|
ASSERT_TRUE(l_returned);
|
2012-11-21 12:44:18 +00:00
|
|
|
detailCompareLease(leases[3], l_returned);
|
|
|
|
|
|
|
|
// Check that we can't add a second lease with the same address
|
|
|
|
EXPECT_FALSE(lmptr_->addLease(leases[1]));
|
|
|
|
|
|
|
|
// Delete a lease, check that it's gone, and that we can't delete it
|
|
|
|
// a second time.
|
|
|
|
EXPECT_TRUE(lmptr_->deleteLease4(ioaddress4_[1]));
|
|
|
|
l_returned = lmptr_->getLease4(ioaddress4_[1]);
|
|
|
|
EXPECT_FALSE(l_returned);
|
|
|
|
EXPECT_FALSE(lmptr_->deleteLease4(ioaddress4_[1]));
|
|
|
|
|
|
|
|
// Check that the second address is still there.
|
|
|
|
l_returned = lmptr_->getLease4(ioaddress4_[2]);
|
2012-11-21 19:38:26 +00:00
|
|
|
ASSERT_TRUE(l_returned);
|
2012-11-21 12:44:18 +00:00
|
|
|
detailCompareLease(leases[2], l_returned);
|
|
|
|
}
|
|
|
|
|
2012-12-03 15:35:36 +00:00
|
|
|
/// @brief Basic Lease6 Checks
|
2012-11-23 18:23:21 +00:00
|
|
|
///
|
2012-12-03 15:35:36 +00:00
|
|
|
/// Checks that the addLease, getLease6 (by address) and deleteLease6 works.
|
2012-11-14 14:30:46 +00:00
|
|
|
TEST_F(MySqlLeaseMgrTest, basicLease6) {
|
2012-11-03 18:26:57 +00:00
|
|
|
// Get the leases to be used for the test.
|
|
|
|
vector<Lease6Ptr> leases = createLeases6();
|
2012-10-20 22:52:25 +01:00
|
|
|
|
2012-10-24 22:14:42 +01:00
|
|
|
// Start the tests. Add three leases to the database, read them back and
|
2012-10-22 16:07:34 +01:00
|
|
|
// check they are what we think they are.
|
2012-11-03 18:26:57 +00:00
|
|
|
EXPECT_TRUE(lmptr_->addLease(leases[1]));
|
|
|
|
EXPECT_TRUE(lmptr_->addLease(leases[2]));
|
|
|
|
EXPECT_TRUE(lmptr_->addLease(leases[3]));
|
2012-10-24 22:14:42 +01:00
|
|
|
lmptr_->commit();
|
|
|
|
|
|
|
|
// Reopen the database to ensure that they actually got stored.
|
|
|
|
reopen();
|
2012-10-22 16:07:34 +01:00
|
|
|
|
2012-11-19 19:37:07 +00:00
|
|
|
Lease6Ptr l_returned = lmptr_->getLease6(ioaddress6_[1]);
|
2012-11-21 19:38:26 +00:00
|
|
|
ASSERT_TRUE(l_returned);
|
2012-11-19 19:37:07 +00:00
|
|
|
detailCompareLease(leases[1], l_returned);
|
2012-10-20 22:52:25 +01:00
|
|
|
|
2012-11-19 19:37:07 +00:00
|
|
|
l_returned = lmptr_->getLease6(ioaddress6_[2]);
|
2012-11-21 19:38:26 +00:00
|
|
|
ASSERT_TRUE(l_returned);
|
2012-11-19 19:37:07 +00:00
|
|
|
detailCompareLease(leases[2], l_returned);
|
2012-10-20 22:52:25 +01:00
|
|
|
|
2012-11-19 19:37:07 +00:00
|
|
|
l_returned = lmptr_->getLease6(ioaddress6_[3]);
|
2012-11-21 19:38:26 +00:00
|
|
|
ASSERT_TRUE(l_returned);
|
2012-11-19 19:37:07 +00:00
|
|
|
detailCompareLease(leases[3], l_returned);
|
2012-10-24 22:14:42 +01:00
|
|
|
|
2012-10-22 16:07:34 +01:00
|
|
|
// Check that we can't add a second lease with the same address
|
2012-11-03 18:26:57 +00:00
|
|
|
EXPECT_FALSE(lmptr_->addLease(leases[1]));
|
2012-10-20 22:52:25 +01:00
|
|
|
|
2012-10-22 16:07:34 +01:00
|
|
|
// Delete a lease, check that it's gone, and that we can't delete it
|
|
|
|
// a second time.
|
2012-11-19 19:37:07 +00:00
|
|
|
EXPECT_TRUE(lmptr_->deleteLease6(ioaddress6_[1]));
|
|
|
|
l_returned = lmptr_->getLease6(ioaddress6_[1]);
|
2012-10-22 16:07:34 +01:00
|
|
|
EXPECT_FALSE(l_returned);
|
2012-11-19 19:37:07 +00:00
|
|
|
EXPECT_FALSE(lmptr_->deleteLease6(ioaddress6_[1]));
|
2012-10-22 16:07:34 +01:00
|
|
|
|
|
|
|
// Check that the second address is still there.
|
2012-11-19 19:37:07 +00:00
|
|
|
l_returned = lmptr_->getLease6(ioaddress6_[2]);
|
2012-11-21 19:38:26 +00:00
|
|
|
ASSERT_TRUE(l_returned);
|
2012-11-19 19:37:07 +00:00
|
|
|
detailCompareLease(leases[2], l_returned);
|
2012-10-20 22:52:25 +01:00
|
|
|
}
|
|
|
|
|
2012-11-23 18:23:21 +00:00
|
|
|
/// @brief Check GetLease4 methods - access by Hardware Address
|
|
|
|
///
|
|
|
|
/// Adds leases to the database and checks that they can be accessed via
|
|
|
|
/// a combination of DIUID and IAID.
|
2012-11-23 12:47:40 +00:00
|
|
|
TEST_F(MySqlLeaseMgrTest, getLease4Hwaddr) {
|
|
|
|
// Get the leases to be used for the test and add to the database
|
|
|
|
vector<Lease4Ptr> leases = createLeases4();
|
|
|
|
for (int i = 0; i < leases.size(); ++i) {
|
|
|
|
EXPECT_TRUE(lmptr_->addLease(leases[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the leases matching the hardware address of lease 1
|
|
|
|
Lease4Collection returned = lmptr_->getLease4(leases[1]->hwaddr_);
|
|
|
|
|
|
|
|
// Should be three leases, matching leases[1], [3] and [5].
|
|
|
|
ASSERT_EQ(3, returned.size());
|
|
|
|
|
|
|
|
// Easiest way to check is to look at the addresses.
|
|
|
|
vector<string> addresses;
|
|
|
|
for (Lease4Collection::const_iterator i = returned.begin();
|
|
|
|
i != returned.end(); ++i) {
|
|
|
|
addresses.push_back((*i)->addr_.toText());
|
|
|
|
}
|
|
|
|
sort(addresses.begin(), addresses.end());
|
|
|
|
EXPECT_EQ(straddress4_[1], addresses[0]);
|
|
|
|
EXPECT_EQ(straddress4_[3], addresses[1]);
|
|
|
|
EXPECT_EQ(straddress4_[5], addresses[2]);
|
|
|
|
|
|
|
|
// Repeat test with just one expected match
|
|
|
|
returned = lmptr_->getLease4(leases[2]->hwaddr_);
|
|
|
|
EXPECT_EQ(1, returned.size());
|
|
|
|
detailCompareLease(leases[2], *returned.begin());
|
|
|
|
|
|
|
|
// Check that an empty vector is valid
|
|
|
|
EXPECT_TRUE(leases[7]->hwaddr_.empty());
|
|
|
|
returned = lmptr_->getLease4(leases[7]->hwaddr_);
|
|
|
|
EXPECT_EQ(1, returned.size());
|
|
|
|
detailCompareLease(leases[7], *returned.begin());
|
|
|
|
|
|
|
|
// Try to get something with invalid hardware address
|
|
|
|
vector<uint8_t> invalid(6, 0);
|
|
|
|
returned = lmptr_->getLease4(invalid);
|
|
|
|
EXPECT_EQ(0, returned.size());
|
2012-12-03 21:23:48 +00:00
|
|
|
}
|
2012-11-23 12:47:40 +00:00
|
|
|
|
2012-12-03 21:23:48 +00:00
|
|
|
// @brief Get lease4 by hardware address (2)
|
|
|
|
//
|
|
|
|
// Check that the system can cope with getting a hardware address of
|
|
|
|
// any size.
|
|
|
|
TEST_F(MySqlLeaseMgrTest, getLease4HwaddrSize) {
|
|
|
|
|
|
|
|
// Create leases, although we need only one.
|
|
|
|
vector<Lease4Ptr> leases = createLeases4();
|
|
|
|
|
|
|
|
// Now add leases with increasing hardware address size.
|
|
|
|
for (uint8_t i = 0; i <= Lease4::HWADDR_MAX; ++i) {
|
|
|
|
leases[1]->hwaddr_.resize(i, i);
|
|
|
|
EXPECT_TRUE(lmptr_->addLease(leases[1]));
|
|
|
|
Lease4Collection returned = lmptr_->getLease4(leases[1]->hwaddr_);
|
|
|
|
ASSERT_EQ(1, returned.size());
|
|
|
|
detailCompareLease(leases[1], *returned.begin());
|
|
|
|
(void) lmptr_->deleteLease4(leases[1]->addr_);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expect some problem when accessing a lease that had too long a hardware
|
|
|
|
// address. (The 42 is a random value put in each byte of the address.)
|
|
|
|
// In fact the address is stored in a truncated form, so we won't find it
|
|
|
|
// when we look.
|
|
|
|
// @todo Check if there is some way of detecting that data added
|
|
|
|
// to the database is truncated. There does not appear to
|
|
|
|
// be any indication in the C API.
|
|
|
|
leases[1]->hwaddr_.resize(Lease4::HWADDR_MAX + 100, 42);
|
|
|
|
EXPECT_TRUE(lmptr_->addLease(leases[1]));
|
|
|
|
Lease4Collection returned = lmptr_->getLease4(leases[1]->hwaddr_);
|
2012-11-23 12:47:40 +00:00
|
|
|
EXPECT_EQ(0, returned.size());
|
|
|
|
}
|
|
|
|
|
2012-11-23 18:23:21 +00:00
|
|
|
/// @brief Check GetLease4 methods - access by Hardware Address & Subnet ID
|
|
|
|
///
|
|
|
|
/// Adds leases to the database and checks that they can be accessed via
|
|
|
|
/// a combination of hardware address and subnet ID
|
2012-11-23 12:23:17 +00:00
|
|
|
TEST_F(MySqlLeaseMgrTest, getLease4HwaddrSubnetId) {
|
|
|
|
// Get the leases to be used for the test and add to the database
|
|
|
|
vector<Lease4Ptr> leases = createLeases4();
|
|
|
|
for (int i = 0; i < leases.size(); ++i) {
|
|
|
|
EXPECT_TRUE(lmptr_->addLease(leases[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the leases matching the hardware address of lease 1 and
|
|
|
|
// subnet ID of lease 1. Result should be a single lease - lease 1.
|
|
|
|
Lease4Ptr returned = lmptr_->getLease4(leases[1]->hwaddr_,
|
|
|
|
leases[1]->subnet_id_);
|
|
|
|
ASSERT_TRUE(returned);
|
|
|
|
detailCompareLease(leases[1], returned);
|
|
|
|
|
|
|
|
// Try for a match to the hardware address of lease 1 and the wrong
|
|
|
|
// subnet ID.
|
|
|
|
returned = lmptr_->getLease4(leases[1]->hwaddr_,
|
|
|
|
leases[1]->subnet_id_ + 1);
|
|
|
|
EXPECT_FALSE(returned);
|
|
|
|
|
|
|
|
// Try for a match to the subnet ID of lease 1 (and lease 4) but
|
|
|
|
// the wrong hardware address.
|
|
|
|
vector<uint8_t> invalid_hwaddr(15, 0x77);
|
|
|
|
returned = lmptr_->getLease4(invalid_hwaddr,
|
|
|
|
leases[1]->subnet_id_);
|
|
|
|
EXPECT_FALSE(returned);
|
|
|
|
|
|
|
|
// Try for a match to an unknown hardware address and an unknown
|
|
|
|
// subnet ID.
|
|
|
|
returned = lmptr_->getLease4(invalid_hwaddr,
|
|
|
|
leases[1]->subnet_id_ + 1);
|
|
|
|
EXPECT_FALSE(returned);
|
2012-11-23 18:23:21 +00:00
|
|
|
|
|
|
|
// Add a second lease with the same values as the first and check that
|
|
|
|
// an attempt to access the database by these parameters throws a
|
|
|
|
// "multiple records" exception. (We expect there to be only one record
|
|
|
|
// with that combination, so getting them via getLeaseX() (as opposed
|
|
|
|
// to getLeaseXCollection() should throw an exception.)
|
|
|
|
EXPECT_TRUE(lmptr_->deleteLease4(leases[2]->addr_));
|
|
|
|
leases[1]->addr_ = leases[2]->addr_;
|
|
|
|
EXPECT_TRUE(lmptr_->addLease(leases[1]));
|
|
|
|
EXPECT_THROW(returned = lmptr_->getLease4(leases[1]->hwaddr_,
|
|
|
|
leases[1]->subnet_id_),
|
|
|
|
isc::dhcp::MultipleRecords);
|
2012-12-03 21:23:48 +00:00
|
|
|
|
|
|
|
// Delete all leases in the database
|
|
|
|
for (int i = 0; ADDRESS4[i] != NULL; ++i) {
|
|
|
|
IOAddress addr(ADDRESS4[i]);
|
|
|
|
(void) lmptr_->deleteLease4(addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// @brief Get lease4 by hardware address and subnet ID (2)
|
|
|
|
//
|
|
|
|
// Check that the system can cope with getting a hardware address of
|
|
|
|
// any size.
|
|
|
|
TEST_F(MySqlLeaseMgrTest, getLease4HwaddrSubnetIdSize) {
|
|
|
|
|
|
|
|
// Create leases, although we need only one.
|
|
|
|
vector<Lease4Ptr> leases = createLeases4();
|
|
|
|
|
|
|
|
// Now add leases with increasing hardware address size and check
|
|
|
|
// that they can be retrieved.
|
|
|
|
for (uint8_t i = 0; i <= Lease4::HWADDR_MAX; ++i) {
|
|
|
|
leases[1]->hwaddr_.resize(i, i);
|
|
|
|
EXPECT_TRUE(lmptr_->addLease(leases[1]));
|
|
|
|
Lease4Ptr returned = lmptr_->getLease4(leases[1]->hwaddr_,
|
|
|
|
leases[1]->subnet_id_);
|
|
|
|
ASSERT_TRUE(returned);
|
|
|
|
detailCompareLease(leases[1], returned);
|
|
|
|
(void) lmptr_->deleteLease4(leases[1]->addr_);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expect some error when getting a lease with too long a hardware
|
|
|
|
// address. Set the contents of each byte to 42, a random value.
|
|
|
|
// @todo Check if there is some way of detecting that data added
|
|
|
|
// to the database is truncated. There does not appear to
|
|
|
|
// be any indication in the C API.
|
|
|
|
leases[1]->hwaddr_.resize(Lease4::HWADDR_MAX + 100, 42);
|
|
|
|
EXPECT_TRUE(lmptr_->addLease(leases[1]));
|
|
|
|
Lease4Ptr returned = lmptr_->getLease4(leases[1]->hwaddr_,
|
|
|
|
leases[1]->subnet_id_);
|
|
|
|
EXPECT_FALSE(returned);
|
2012-11-23 12:23:17 +00:00
|
|
|
}
|
|
|
|
|
2012-11-23 18:23:21 +00:00
|
|
|
/// @brief Check GetLease4 methods - access by Client ID
|
|
|
|
///
|
|
|
|
/// Adds leases to the database and checks that they can be accessed via
|
|
|
|
/// the Client ID.
|
2012-11-23 12:47:40 +00:00
|
|
|
TEST_F(MySqlLeaseMgrTest, getLease4ClientId) {
|
2012-11-23 12:05:55 +00:00
|
|
|
// Get the leases to be used for the test and add to the database
|
2012-11-21 19:38:26 +00:00
|
|
|
vector<Lease4Ptr> leases = createLeases4();
|
|
|
|
for (int i = 0; i < leases.size(); ++i) {
|
|
|
|
EXPECT_TRUE(lmptr_->addLease(leases[i]));
|
|
|
|
}
|
|
|
|
|
2012-11-23 12:47:40 +00:00
|
|
|
// Get the leases matching the Client ID address of lease 1
|
|
|
|
Lease4Collection returned = lmptr_->getLease4(*leases[1]->client_id_);
|
2012-11-21 19:38:26 +00:00
|
|
|
|
2012-11-23 12:47:40 +00:00
|
|
|
// Should be four leases, matching leases[1], [4], [5] and [6].
|
|
|
|
ASSERT_EQ(4, returned.size());
|
2012-11-21 19:38:26 +00:00
|
|
|
|
2012-11-23 12:05:55 +00:00
|
|
|
// Easiest way to check is to look at the addresses.
|
|
|
|
vector<string> addresses;
|
|
|
|
for (Lease4Collection::const_iterator i = returned.begin();
|
|
|
|
i != returned.end(); ++i) {
|
|
|
|
addresses.push_back((*i)->addr_.toText());
|
|
|
|
}
|
|
|
|
sort(addresses.begin(), addresses.end());
|
|
|
|
EXPECT_EQ(straddress4_[1], addresses[0]);
|
2012-11-23 12:47:40 +00:00
|
|
|
EXPECT_EQ(straddress4_[4], addresses[1]);
|
2012-11-23 12:05:55 +00:00
|
|
|
EXPECT_EQ(straddress4_[5], addresses[2]);
|
2012-11-23 12:47:40 +00:00
|
|
|
EXPECT_EQ(straddress4_[6], addresses[3]);
|
2012-11-23 12:05:55 +00:00
|
|
|
|
|
|
|
// Repeat test with just one expected match
|
2012-11-23 12:47:40 +00:00
|
|
|
returned = lmptr_->getLease4(*leases[3]->client_id_);
|
2012-11-23 12:05:55 +00:00
|
|
|
EXPECT_EQ(1, returned.size());
|
2012-11-23 12:47:40 +00:00
|
|
|
detailCompareLease(leases[3], *returned.begin());
|
2012-11-23 12:05:55 +00:00
|
|
|
|
|
|
|
// Check that an empty vector is valid
|
2012-11-23 12:47:40 +00:00
|
|
|
EXPECT_TRUE(leases[7]->client_id_->getClientId().empty());
|
2012-11-23 12:05:55 +00:00
|
|
|
returned = lmptr_->getLease4(leases[7]->hwaddr_);
|
|
|
|
EXPECT_EQ(1, returned.size());
|
|
|
|
detailCompareLease(leases[7], *returned.begin());
|
|
|
|
|
2012-11-23 12:47:40 +00:00
|
|
|
// Try to get something with invalid client ID
|
|
|
|
const uint8_t invalid_data[] = {0, 0, 0};
|
|
|
|
ClientId invalid(invalid_data, sizeof(invalid_data));
|
2012-11-23 12:05:55 +00:00
|
|
|
returned = lmptr_->getLease4(invalid);
|
|
|
|
EXPECT_EQ(0, returned.size());
|
2012-11-21 19:38:26 +00:00
|
|
|
}
|
|
|
|
|
2012-12-03 21:23:48 +00:00
|
|
|
// @brief Get Lease4 by client ID (2)
|
|
|
|
//
|
|
|
|
// Check that the system can cope with a client ID of any size.
|
|
|
|
TEST_F(MySqlLeaseMgrTest, getLease4ClientIdSize) {
|
|
|
|
|
|
|
|
// Create leases, although we need only one.
|
|
|
|
vector<Lease4Ptr> leases = createLeases4();
|
|
|
|
|
|
|
|
// Now add leases with increasing Client ID size can be retrieved.
|
|
|
|
// For speed, go from 0 to 128 is steps of 16.
|
|
|
|
// Intermediate client_id_max is to overcome problem if
|
|
|
|
// ClientId::MAX_CLIENT_ID_LEN is used in an EXPECT_EQ.
|
|
|
|
int client_id_max = ClientId::MAX_CLIENT_ID_LEN;
|
|
|
|
EXPECT_EQ(128, client_id_max);
|
|
|
|
for (uint8_t i = 0; i <= client_id_max; i += 16) {
|
|
|
|
vector<uint8_t> clientid_vec(i, i);
|
|
|
|
leases[1]->client_id_.reset(new ClientId(clientid_vec));
|
|
|
|
EXPECT_TRUE(lmptr_->addLease(leases[1]));
|
|
|
|
Lease4Collection returned = lmptr_->getLease4(*leases[1]->client_id_);
|
|
|
|
ASSERT_TRUE(returned.size() == 1);
|
|
|
|
detailCompareLease(leases[1], *returned.begin());
|
|
|
|
(void) lmptr_->deleteLease4(leases[1]->addr_);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't bother to check client IDs longer than the maximum -
|
|
|
|
// these cannot be constructed, and that limitation is tested
|
|
|
|
// in the DUID/Client ID unit tests.
|
|
|
|
}
|
|
|
|
|
2012-11-23 18:23:21 +00:00
|
|
|
/// @brief Check GetLease4 methods - access by Client ID & Subnet ID
|
|
|
|
///
|
|
|
|
/// Adds leases to the database and checks that they can be accessed via
|
|
|
|
/// a combination of client and subnet IDs.
|
2012-11-23 13:48:42 +00:00
|
|
|
TEST_F(MySqlLeaseMgrTest, getLease4ClientIdSubnetId) {
|
|
|
|
// Get the leases to be used for the test and add to the database
|
|
|
|
vector<Lease4Ptr> leases = createLeases4();
|
|
|
|
for (int i = 0; i < leases.size(); ++i) {
|
|
|
|
EXPECT_TRUE(lmptr_->addLease(leases[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the leases matching the client ID of lease 1 and
|
|
|
|
// subnet ID of lease 1. Result should be a single lease - lease 1.
|
|
|
|
Lease4Ptr returned = lmptr_->getLease4(*leases[1]->client_id_,
|
|
|
|
leases[1]->subnet_id_);
|
|
|
|
ASSERT_TRUE(returned);
|
|
|
|
detailCompareLease(leases[1], returned);
|
|
|
|
|
|
|
|
// Try for a match to the client ID of lease 1 and the wrong
|
|
|
|
// subnet ID.
|
|
|
|
returned = lmptr_->getLease4(*leases[1]->client_id_,
|
|
|
|
leases[1]->subnet_id_ + 1);
|
|
|
|
EXPECT_FALSE(returned);
|
|
|
|
|
|
|
|
// Try for a match to the subnet ID of lease 1 (and lease 4) but
|
|
|
|
// the wrong client ID
|
|
|
|
const uint8_t invalid_data[] = {0, 0, 0};
|
|
|
|
ClientId invalid(invalid_data, sizeof(invalid_data));
|
|
|
|
returned = lmptr_->getLease4(invalid, leases[1]->subnet_id_);
|
|
|
|
EXPECT_FALSE(returned);
|
|
|
|
|
|
|
|
// Try for a match to an unknown hardware address and an unknown
|
|
|
|
// subnet ID.
|
|
|
|
returned = lmptr_->getLease4(invalid, leases[1]->subnet_id_ + 1);
|
|
|
|
EXPECT_FALSE(returned);
|
|
|
|
}
|
|
|
|
|
2012-11-23 18:23:21 +00:00
|
|
|
/// @brief Check GetLease6 methods - access by DUID/IAID
|
|
|
|
///
|
|
|
|
/// Adds leases to the database and checks that they can be accessed via
|
|
|
|
/// a combination of DIUID and IAID.
|
2012-11-21 19:38:26 +00:00
|
|
|
TEST_F(MySqlLeaseMgrTest, getLease6DuidIaid) {
|
2012-11-03 18:26:57 +00:00
|
|
|
// Get the leases to be used for the test.
|
|
|
|
vector<Lease6Ptr> leases = createLeases6();
|
2012-11-21 19:38:26 +00:00
|
|
|
ASSERT_LE(6, leases.size()); // Expect to access leases 0 through 5
|
2012-11-03 18:26:57 +00:00
|
|
|
|
|
|
|
// Add them to the database
|
|
|
|
for (int i = 0; i < leases.size(); ++i) {
|
|
|
|
EXPECT_TRUE(lmptr_->addLease(leases[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the leases matching the DUID and IAID of lease[1].
|
|
|
|
Lease6Collection returned = lmptr_->getLease6(*leases[1]->duid_,
|
|
|
|
leases[1]->iaid_);
|
|
|
|
|
|
|
|
// Should be three leases, matching leases[1], [4] and [5].
|
|
|
|
ASSERT_EQ(3, returned.size());
|
|
|
|
|
|
|
|
// Easiest way to check is to look at the addresses.
|
|
|
|
vector<string> addresses;
|
|
|
|
for (Lease6Collection::const_iterator i = returned.begin();
|
|
|
|
i != returned.end(); ++i) {
|
|
|
|
addresses.push_back((*i)->addr_.toText());
|
|
|
|
}
|
|
|
|
sort(addresses.begin(), addresses.end());
|
2012-11-19 19:37:07 +00:00
|
|
|
EXPECT_EQ(straddress6_[1], addresses[0]);
|
|
|
|
EXPECT_EQ(straddress6_[4], addresses[1]);
|
|
|
|
EXPECT_EQ(straddress6_[5], addresses[2]);
|
2012-11-04 19:57:05 +00:00
|
|
|
|
|
|
|
// Check that nothing is returned when either the IAID or DUID match
|
|
|
|
// nothing.
|
|
|
|
returned = lmptr_->getLease6(*leases[1]->duid_, leases[1]->iaid_ + 1);
|
|
|
|
EXPECT_EQ(0, returned.size());
|
|
|
|
|
|
|
|
// Alter the leases[1] DUID to match nothing in the database.
|
|
|
|
vector<uint8_t> duid_vector = leases[1]->duid_->getDuid();
|
|
|
|
++duid_vector[0];
|
|
|
|
DUID new_duid(duid_vector);
|
|
|
|
returned = lmptr_->getLease6(new_duid, leases[1]->iaid_);
|
|
|
|
EXPECT_EQ(0, returned.size());
|
|
|
|
}
|
|
|
|
|
2012-12-03 21:23:48 +00:00
|
|
|
// @brief Get Lease4 by DUID and IAID (2)
|
|
|
|
//
|
|
|
|
// Check that the system can cope with a DUID of any size.
|
|
|
|
TEST_F(MySqlLeaseMgrTest, getLease6DuidIaidSize) {
|
|
|
|
|
|
|
|
// Create leases, although we need only one.
|
|
|
|
vector<Lease6Ptr> leases = createLeases6();
|
|
|
|
|
|
|
|
// Now add leases with increasing DUID size can be retrieved.
|
|
|
|
// For speed, go from 0 to 128 is steps of 16.
|
|
|
|
int duid_max = DUID::MAX_DUID_LEN;
|
|
|
|
EXPECT_EQ(128, duid_max);
|
|
|
|
for (uint8_t i = 0; i <= duid_max; i += 16) {
|
|
|
|
vector<uint8_t> duid_vec(i, i);
|
|
|
|
leases[1]->duid_.reset(new DUID(duid_vec));
|
|
|
|
EXPECT_TRUE(lmptr_->addLease(leases[1]));
|
|
|
|
Lease6Collection returned = lmptr_->getLease6(*leases[1]->duid_,
|
|
|
|
leases[1]->iaid_);
|
|
|
|
EXPECT_EQ(1, returned.size());
|
|
|
|
detailCompareLease(leases[1], *returned.begin());
|
|
|
|
(void) lmptr_->deleteLease6(leases[1]->addr_);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't bother to check DUIDs longer than the maximum - these cannot be
|
|
|
|
// constructed, and that limitation is tested in the DUID/Client ID unit
|
|
|
|
// tests.
|
|
|
|
}
|
|
|
|
|
2012-11-23 18:23:21 +00:00
|
|
|
/// @brief Check GetLease6 methods - access by DUID/IAID/SubnetID
|
|
|
|
///
|
|
|
|
/// Adds leases to the database and checks that they can be accessed via
|
|
|
|
/// a combination of DIUID and IAID.
|
2012-11-21 19:38:26 +00:00
|
|
|
TEST_F(MySqlLeaseMgrTest, getLease6DuidIaidSubnetId) {
|
2012-11-23 14:08:00 +00:00
|
|
|
// Get the leases to be used for the test and add them to the database.
|
2012-11-04 19:57:05 +00:00
|
|
|
vector<Lease6Ptr> leases = createLeases6();
|
|
|
|
for (int i = 0; i < leases.size(); ++i) {
|
|
|
|
EXPECT_TRUE(lmptr_->addLease(leases[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the leases matching the DUID and IAID of lease[1].
|
|
|
|
Lease6Ptr returned = lmptr_->getLease6(*leases[1]->duid_,
|
|
|
|
leases[1]->iaid_,
|
|
|
|
leases[1]->subnet_id_);
|
|
|
|
ASSERT_TRUE(returned);
|
|
|
|
EXPECT_TRUE(*returned == *leases[1]);
|
|
|
|
|
|
|
|
// Modify each of the three parameters (DUID, IAID, Subnet ID) and
|
|
|
|
// check that nothing is returned.
|
|
|
|
returned = lmptr_->getLease6(*leases[1]->duid_, leases[1]->iaid_ + 1,
|
|
|
|
leases[1]->subnet_id_);
|
|
|
|
EXPECT_FALSE(returned);
|
|
|
|
|
|
|
|
returned = lmptr_->getLease6(*leases[1]->duid_, leases[1]->iaid_,
|
|
|
|
leases[1]->subnet_id_ + 1);
|
|
|
|
EXPECT_FALSE(returned);
|
|
|
|
|
|
|
|
// Alter the leases[1] DUID to match nothing in the database.
|
|
|
|
vector<uint8_t> duid_vector = leases[1]->duid_->getDuid();
|
|
|
|
++duid_vector[0];
|
|
|
|
DUID new_duid(duid_vector);
|
|
|
|
returned = lmptr_->getLease6(new_duid, leases[1]->iaid_,
|
|
|
|
leases[1]->subnet_id_);
|
|
|
|
EXPECT_FALSE(returned);
|
2012-11-03 18:26:57 +00:00
|
|
|
}
|
|
|
|
|
2012-12-03 21:23:48 +00:00
|
|
|
// @brief Get Lease4 by DUID, IAID & subnet ID (2)
|
|
|
|
//
|
|
|
|
// Check that the system can cope with a DUID of any size.
|
|
|
|
TEST_F(MySqlLeaseMgrTest, getLease6DuidIaidSubnetIdSize) {
|
|
|
|
|
|
|
|
// Create leases, although we need only one.
|
|
|
|
vector<Lease6Ptr> leases = createLeases6();
|
|
|
|
|
|
|
|
// Now add leases with increasing DUID size can be retrieved.
|
|
|
|
// For speed, go from 0 to 128 is steps of 16.
|
|
|
|
int duid_max = DUID::MAX_DUID_LEN;
|
|
|
|
EXPECT_EQ(128, duid_max);
|
|
|
|
for (uint8_t i = 0; i <= duid_max; i += 16) {
|
|
|
|
vector<uint8_t> duid_vec(i, i);
|
|
|
|
leases[1]->duid_.reset(new DUID(duid_vec));
|
|
|
|
EXPECT_TRUE(lmptr_->addLease(leases[1]));
|
|
|
|
Lease6Ptr returned = lmptr_->getLease6(*leases[1]->duid_,
|
|
|
|
leases[1]->iaid_,
|
|
|
|
leases[1]->subnet_id_);
|
|
|
|
ASSERT_TRUE(returned);
|
|
|
|
detailCompareLease(leases[1], returned);
|
|
|
|
(void) lmptr_->deleteLease6(leases[1]->addr_);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't bother to check DUIDs longer than the maximum - these cannot be
|
|
|
|
// constructed, and that limitation is tested in the DUID/Client ID unit
|
|
|
|
// tests.
|
|
|
|
}
|
|
|
|
|
2012-11-23 18:23:21 +00:00
|
|
|
/// @brief Lease4 update tests
|
|
|
|
///
|
|
|
|
/// Checks that we are able to update a lease in the database.
|
2012-11-23 14:08:00 +00:00
|
|
|
TEST_F(MySqlLeaseMgrTest, updateLease4) {
|
|
|
|
// Get the leases to be used for the test and add them to the database.
|
|
|
|
vector<Lease4Ptr> leases = createLeases4();
|
|
|
|
for (int i = 0; i < leases.size(); ++i) {
|
|
|
|
EXPECT_TRUE(lmptr_->addLease(leases[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Modify some fields in lease 1 (not the address) and update it.
|
|
|
|
++leases[1]->subnet_id_;
|
|
|
|
leases[1]->valid_lft_ *= 2;
|
|
|
|
lmptr_->updateLease4(leases[1]);
|
|
|
|
|
|
|
|
// ... and check what is returned is what is expected.
|
|
|
|
Lease4Ptr l_returned = lmptr_->getLease4(ioaddress4_[1]);
|
|
|
|
ASSERT_TRUE(l_returned);
|
|
|
|
detailCompareLease(leases[1], l_returned);
|
|
|
|
|
|
|
|
// Alter the lease again and check.
|
|
|
|
++leases[1]->subnet_id_;
|
|
|
|
leases[1]->cltt_ += 6;
|
|
|
|
lmptr_->updateLease4(leases[1]);
|
|
|
|
|
|
|
|
// Explicitly clear the returned pointer before getting new data to ensure
|
|
|
|
// that the new data is returned.
|
|
|
|
l_returned.reset();
|
|
|
|
l_returned = lmptr_->getLease4(ioaddress4_[1]);
|
|
|
|
ASSERT_TRUE(l_returned);
|
|
|
|
detailCompareLease(leases[1], l_returned);
|
|
|
|
|
|
|
|
// Check we can do an update without changing data.
|
|
|
|
lmptr_->updateLease4(leases[1]);
|
|
|
|
l_returned.reset();
|
|
|
|
l_returned = lmptr_->getLease4(ioaddress4_[1]);
|
|
|
|
ASSERT_TRUE(l_returned);
|
|
|
|
detailCompareLease(leases[1], l_returned);
|
|
|
|
|
|
|
|
// Try updating a lease not in the database.
|
|
|
|
lmptr_->deleteLease4(ioaddress4_[2]);
|
|
|
|
EXPECT_THROW(lmptr_->updateLease4(leases[2]), isc::dhcp::NoSuchLease);
|
|
|
|
}
|
|
|
|
|
2012-11-23 18:23:21 +00:00
|
|
|
/// @brief Lease6 update tests
|
|
|
|
///
|
|
|
|
/// Checks that we are able to update a lease in the database.
|
2012-11-14 14:30:46 +00:00
|
|
|
TEST_F(MySqlLeaseMgrTest, updateLease6) {
|
2012-11-03 18:26:57 +00:00
|
|
|
// Get the leases to be used for the test.
|
|
|
|
vector<Lease6Ptr> leases = createLeases6();
|
2012-11-21 19:38:26 +00:00
|
|
|
ASSERT_LE(3, leases.size()); // Expect to access leases 0 through 2
|
2012-10-29 19:19:36 +00:00
|
|
|
|
|
|
|
// Add a lease to the database and check that the lease is there.
|
2012-11-03 18:26:57 +00:00
|
|
|
EXPECT_TRUE(lmptr_->addLease(leases[1]));
|
2012-10-29 19:19:36 +00:00
|
|
|
lmptr_->commit();
|
|
|
|
|
2012-11-19 19:37:07 +00:00
|
|
|
Lease6Ptr l_returned = lmptr_->getLease6(ioaddress6_[1]);
|
2012-11-21 19:38:26 +00:00
|
|
|
ASSERT_TRUE(l_returned);
|
2012-11-19 19:37:07 +00:00
|
|
|
detailCompareLease(leases[1], l_returned);
|
2012-10-29 19:19:36 +00:00
|
|
|
|
|
|
|
// Modify some fields in lease 1 (not the address) and update it.
|
2012-11-03 18:26:57 +00:00
|
|
|
++leases[1]->iaid_;
|
|
|
|
leases[1]->type_ = Lease6::LEASE_IA_PD;
|
|
|
|
leases[1]->valid_lft_ *= 2;
|
|
|
|
lmptr_->updateLease6(leases[1]);
|
2012-10-29 19:19:36 +00:00
|
|
|
lmptr_->commit();
|
|
|
|
|
|
|
|
// ... and check what is returned is what is expected.
|
|
|
|
l_returned.reset();
|
2012-11-19 19:37:07 +00:00
|
|
|
l_returned = lmptr_->getLease6(ioaddress6_[1]);
|
2012-11-21 19:38:26 +00:00
|
|
|
ASSERT_TRUE(l_returned);
|
2012-11-19 19:37:07 +00:00
|
|
|
detailCompareLease(leases[1], l_returned);
|
2012-10-29 19:19:36 +00:00
|
|
|
|
|
|
|
// Alter the lease again and check.
|
2012-11-03 18:26:57 +00:00
|
|
|
++leases[1]->iaid_;
|
|
|
|
leases[1]->type_ = Lease6::LEASE_IA_TA;
|
|
|
|
leases[1]->cltt_ += 6;
|
|
|
|
leases[1]->prefixlen_ = 93;
|
|
|
|
lmptr_->updateLease6(leases[1]);
|
2012-10-29 19:19:36 +00:00
|
|
|
|
|
|
|
l_returned.reset();
|
2012-11-19 19:37:07 +00:00
|
|
|
l_returned = lmptr_->getLease6(ioaddress6_[1]);
|
2012-11-21 19:38:26 +00:00
|
|
|
ASSERT_TRUE(l_returned);
|
2012-11-19 19:37:07 +00:00
|
|
|
detailCompareLease(leases[1], l_returned);
|
2012-10-29 19:19:36 +00:00
|
|
|
|
|
|
|
// Check we can do an update without changing data.
|
2012-11-03 18:26:57 +00:00
|
|
|
lmptr_->updateLease6(leases[1]);
|
2012-10-29 19:19:36 +00:00
|
|
|
l_returned.reset();
|
2012-11-19 19:37:07 +00:00
|
|
|
l_returned = lmptr_->getLease6(ioaddress6_[1]);
|
2012-11-21 19:38:26 +00:00
|
|
|
ASSERT_TRUE(l_returned);
|
2012-11-19 19:37:07 +00:00
|
|
|
detailCompareLease(leases[1], l_returned);
|
2012-10-29 19:19:36 +00:00
|
|
|
|
2012-11-03 18:26:57 +00:00
|
|
|
// Try updating a lease not in the database.
|
|
|
|
EXPECT_THROW(lmptr_->updateLease6(leases[2]), isc::dhcp::NoSuchLease);
|
2012-10-24 19:18:33 +01:00
|
|
|
}
|
|
|
|
|
2012-11-05 19:29:39 +00:00
|
|
|
}; // Of anonymous namespace
|