Commit 6adb8b73 authored by Thomas Markwalder's avatar Thomas Markwalder
Browse files

[3080] Fix build and unit test errors

Initial review and testing revealed build issues, unit test failures,
and memory leaks.  With these changes it should build and pass unit testing.
There are still runtime issues to address.

Changed expire column type in lease tables to be "TIMESTAMP WITH TIME ZONE",
and added methods to convert to and from such fields to LeaseExchange. This
corrects mismatched time conversion to and from database which was causing unit tests to fail.

Added constructors to PgSqlParam to eliminate use of ".value" initializers and
to provide a safe, uniform way to create parameters for binary data. Prior to
this valgrind was reporting invalid reads when vectors were statically cast
to char*.

Removed superflous BOOST_STATIC_ASSERT and corrected values tested in remaining
check.

Removed use of "SET AUTOCOMMIT TO" as it is no longer supported in PostgreSQL.

Altered failure logic in PgSqlLeaseMgr::openDatabase() to release connection
if it is not NULL. This was causing memory leak in unit tests.

Added PQfinish call to createSchema() function to release the connection to fix
memory leaks during unit testing.

Cleaned most cppcheck complaints.
parent efbde0ad
......@@ -35,7 +35,7 @@ CREATE TABLE lease4 (
hwaddr BYTEA, -- Hardware address
client_id BYTEA, -- Client ID
valid_lifetime BIGINT, -- Length of the lease (seconds)
expire TIMESTAMP, -- Expiration time of the lease
expire TIMESTAMP WITH TIME ZONE, -- Expiration time of the lease
subnet_id BIGINT, -- Subnet identification
fqdn_fwd BOOLEAN, -- Has forward DNS update been performed by a server
fqdn_rev BOOLEAN, -- Has reverse DNS update been performed by a server
......@@ -57,7 +57,7 @@ CREATE TABLE lease6 (
address VARCHAR(39) PRIMARY KEY NOT NULL, -- IPv6 address
duid BYTEA, -- DUID
valid_lifetime BIGINT, -- Length of the lease (seconds)
expire TIMESTAMP, -- Expiration time of the lease
expire TIMESTAMP WITH TIME ZONE, -- Expiration time of the lease
subnet_id BIGINT, -- Subnet identification
pref_lifetime BIGINT, -- Preferred lifetime
lease_type SMALLINT, -- Lease type (see lease6_types
......
This diff is collapsed.
......@@ -22,6 +22,8 @@
#include <boost/utility.hpp>
#include <libpq-fe.h>
#include <vector>
namespace isc {
namespace dhcp {
......@@ -31,8 +33,28 @@ namespace dhcp {
/// or UPDATE clauses).
struct PgSqlParam {
std::string value; ///< The actual value represented as text
bool isbinary; ///< Boolean flag that controls whether the data is binary
bool isbinary; ///< Boolean flag that indicates if data is binary
int binarylen; ///< Specified binary length
/// @brief Constructor for text parameters
///
/// Constructs a text (i.e. non-binary) instance given a string value.
/// @param val string containing the text value of the parameter. The
/// default is an empty string which serves as the default or empty
/// parameter constructor.
PgSqlParam (const std::string& val = "")
: value(val), isbinary(false), binarylen(0) {
}
/// @brief Constructor for binary data parameters
///
/// Constructs a binary data instance given a vector of binary data.
/// @param data vector of binary data from which to set the parameter's
/// value.
PgSqlParam (const std::vector<uint8_t>& data)
: value(data.begin(), data.end()), isbinary(true),
binarylen(data.size()) {
}
};
/// @brief Defines all parameters for binding a compiled statement
......
......@@ -142,6 +142,8 @@ void createSchema() {
r = PQexec(conn, create_statement[i]);
PQclear(r);
}
PQfinish(conn);
}
/// @brief Test fixture class for testing PostgreSQL Lease Manager
......@@ -221,7 +223,6 @@ TEST(PgSqlOpenTest, OpenDatabase) {
<< "*** The test environment is broken and must be fixed\n"
<< "*** before the PostgreSQL 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);
......@@ -232,6 +233,7 @@ TEST(PgSqlOpenTest, OpenDatabase) {
EXPECT_THROW(LeaseMgrFactory::create(connectionString(
NULL, VALID_NAME, VALID_HOST, INVALID_USER, VALID_PASSWORD)),
InvalidParameter);
EXPECT_THROW(LeaseMgrFactory::create(connectionString(
INVALID_TYPE, VALID_NAME, VALID_HOST, VALID_USER, VALID_PASSWORD)),
InvalidType);
......@@ -240,12 +242,15 @@ TEST(PgSqlOpenTest, OpenDatabase) {
EXPECT_THROW(LeaseMgrFactory::create(connectionString(
VALID_TYPE, INVALID_NAME, VALID_HOST, VALID_USER, VALID_PASSWORD)),
DbOpenError);
EXPECT_THROW(LeaseMgrFactory::create(connectionString(
VALID_TYPE, VALID_NAME, INVALID_HOST, VALID_USER, VALID_PASSWORD)),
DbOpenError);
EXPECT_THROW(LeaseMgrFactory::create(connectionString(
VALID_TYPE, VALID_NAME, VALID_HOST, INVALID_USER, VALID_PASSWORD)),
DbOpenError);
EXPECT_THROW(LeaseMgrFactory::create(connectionString(
VALID_TYPE, VALID_NAME, VALID_HOST, VALID_USER, INVALID_PASSWORD)),
DbOpenError);
......
......@@ -48,7 +48,7 @@ const char* create_statement[] = {
"hwaddr BYTEA,"
"client_id BYTEA,"
"valid_lifetime BIGINT,"
"expire TIMESTAMP,"
"expire TIMESTAMP WITH TIME ZONE,"
"subnet_id BIGINT,"
"fqdn_fwd BOOLEAN,"
"fqdn_rev BOOLEAN,"
......@@ -59,7 +59,7 @@ const char* create_statement[] = {
"address VARCHAR(39) PRIMARY KEY NOT NULL,"
"duid BYTEA,"
"valid_lifetime BIGINT,"
"expire TIMESTAMP,"
"expire TIMESTAMP WITH TIME ZONE,"
"subnet_id BIGINT,"
"pref_lifetime BIGINT,"
"lease_type SMALLINT,"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment