mirror of
https://gitlab.isc.org/isc-projects/kea
synced 2025-09-03 23:45:27 +00:00
[5061] Added unit test and pgsql support
This commit is contained in:
@@ -58,7 +58,8 @@
|
|||||||
"name": "kea",
|
"name": "kea",
|
||||||
"user": "kea",
|
"user": "kea",
|
||||||
"password": "kea",
|
"password": "kea",
|
||||||
"host": "localhost"
|
"host": "localhost",
|
||||||
|
"port": 3306
|
||||||
},
|
},
|
||||||
|
|
||||||
# Define a subnet with a single pool of dynamic addresses. Addresses from
|
# Define a subnet with a single pool of dynamic addresses. Addresses from
|
||||||
|
@@ -47,6 +47,7 @@
|
|||||||
"user": "kea",
|
"user": "kea",
|
||||||
"password": "kea",
|
"password": "kea",
|
||||||
"host": "localhost",
|
"host": "localhost",
|
||||||
|
"port": 3306,
|
||||||
"readonly": true
|
"readonly": true
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
// Copyright (C) 2016 Internet Systems Consortium, Inc. ("ISC")
|
// Copyright (C) 2016-2017 Internet Systems Consortium, Inc. ("ISC")
|
||||||
//
|
//
|
||||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
@@ -153,6 +153,40 @@ PgSqlConnection::openDatabase() {
|
|||||||
|
|
||||||
dbconnparameters += "host = '" + shost + "'" ;
|
dbconnparameters += "host = '" + shost + "'" ;
|
||||||
|
|
||||||
|
string sport;
|
||||||
|
try {
|
||||||
|
sport = getParameter("port");
|
||||||
|
} catch (...) {
|
||||||
|
// No port parameter, we are going to use the default port.
|
||||||
|
sport = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sport.size() > 0) {
|
||||||
|
unsigned int port = 0;
|
||||||
|
|
||||||
|
// Port was given, so try to convert it to an integer.
|
||||||
|
try {
|
||||||
|
port = boost::lexical_cast<unsigned int>(sport);
|
||||||
|
} catch (...) {
|
||||||
|
// Port given but could not be converted to an unsigned int.
|
||||||
|
// Just fall back to the default value.
|
||||||
|
port = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The port is only valid when it is in the 0..65535 range.
|
||||||
|
// Again fall back to the default when the given value is invalid.
|
||||||
|
if (port > numeric_limits<uint16_t>::max()) {
|
||||||
|
port = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add it to connection parameters when not default.
|
||||||
|
if (port > 0) {
|
||||||
|
std::ostringstream oss;
|
||||||
|
oss << port;
|
||||||
|
dbconnparameters += " port = " + oss.str();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
string suser;
|
string suser;
|
||||||
try {
|
try {
|
||||||
suser = getParameter("user");
|
suser = getParameter("user");
|
||||||
|
@@ -178,6 +178,7 @@ private:
|
|||||||
bool quoteValue(const std::string& parameter) const {
|
bool quoteValue(const std::string& parameter) const {
|
||||||
return ((parameter != "persist") && (parameter != "lfc-interval") &&
|
return ((parameter != "persist") && (parameter != "lfc-interval") &&
|
||||||
(parameter != "connect-timeout") &&
|
(parameter != "connect-timeout") &&
|
||||||
|
(parameter != "port") &&
|
||||||
(parameter != "readonly"));
|
(parameter != "readonly"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -402,10 +403,61 @@ TEST_F(DbAccessParserTest, largeTimeout) {
|
|||||||
EXPECT_THROW(parser.parse(json_elements), DhcpConfigError);
|
EXPECT_THROW(parser.parse(json_elements), DhcpConfigError);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This test checks that the parser accepts the valid value of the
|
||||||
|
// port parameter.
|
||||||
|
TEST_F(DbAccessParserTest, validPort) {
|
||||||
|
const char* config[] = {"type", "memfile",
|
||||||
|
"name", "/opt/kea/var/kea-leases6.csv",
|
||||||
|
"port", "3306",
|
||||||
|
NULL};
|
||||||
|
|
||||||
|
string json_config = toJson(config);
|
||||||
|
ConstElementPtr json_elements = Element::fromJSON(json_config);
|
||||||
|
EXPECT_TRUE(json_elements);
|
||||||
|
|
||||||
|
TestDbAccessParser parser(DbAccessParser::LEASE_DB);
|
||||||
|
EXPECT_NO_THROW(parser.parse(json_elements));
|
||||||
|
checkAccessString("Valid port", parser.getDbAccessParameters(),
|
||||||
|
config);
|
||||||
|
}
|
||||||
|
|
||||||
|
// This test checks that the parser rejects the negative value of the
|
||||||
|
// port parameter.
|
||||||
|
TEST_F(DbAccessParserTest, negativePort) {
|
||||||
|
const char* config[] = {"type", "memfile",
|
||||||
|
"name", "/opt/kea/var/kea-leases6.csv",
|
||||||
|
"port", "-1",
|
||||||
|
NULL};
|
||||||
|
|
||||||
|
string json_config = toJson(config);
|
||||||
|
ConstElementPtr json_elements = Element::fromJSON(json_config);
|
||||||
|
EXPECT_TRUE(json_elements);
|
||||||
|
|
||||||
|
TestDbAccessParser parser(DbAccessParser::LEASE_DB);
|
||||||
|
EXPECT_THROW(parser.parse(json_elements), DhcpConfigError);
|
||||||
|
}
|
||||||
|
|
||||||
|
// This test checks that the parser rejects a too large (greater than
|
||||||
|
// the max uint16_t) value of the timeout parameter.
|
||||||
|
TEST_F(DbAccessParserTest, largePort) {
|
||||||
|
const char* config[] = {"type", "memfile",
|
||||||
|
"name", "/opt/kea/var/kea-leases6.csv",
|
||||||
|
"port", "65536",
|
||||||
|
NULL};
|
||||||
|
|
||||||
|
string json_config = toJson(config);
|
||||||
|
ConstElementPtr json_elements = Element::fromJSON(json_config);
|
||||||
|
EXPECT_TRUE(json_elements);
|
||||||
|
|
||||||
|
TestDbAccessParser parser(DbAccessParser::LEASE_DB);
|
||||||
|
EXPECT_THROW(parser.parse(json_elements), DhcpConfigError);
|
||||||
|
}
|
||||||
|
|
||||||
// Check that the parser works with a valid MySQL configuration
|
// Check that the parser works with a valid MySQL configuration
|
||||||
TEST_F(DbAccessParserTest, validTypeMysql) {
|
TEST_F(DbAccessParserTest, validTypeMysql) {
|
||||||
const char* config[] = {"type", "mysql",
|
const char* config[] = {"type", "mysql",
|
||||||
"host", "erewhon",
|
"host", "erewhon",
|
||||||
|
"port", "3306",
|
||||||
"user", "kea",
|
"user", "kea",
|
||||||
"password", "keapassword",
|
"password", "keapassword",
|
||||||
"name", "keatest",
|
"name", "keatest",
|
||||||
@@ -423,6 +475,7 @@ TEST_F(DbAccessParserTest, validTypeMysql) {
|
|||||||
// A missing 'type' keyword should cause an exception to be thrown.
|
// A missing 'type' keyword should cause an exception to be thrown.
|
||||||
TEST_F(DbAccessParserTest, missingTypeKeyword) {
|
TEST_F(DbAccessParserTest, missingTypeKeyword) {
|
||||||
const char* config[] = {"host", "erewhon",
|
const char* config[] = {"host", "erewhon",
|
||||||
|
"port", "3306",
|
||||||
"user", "kea",
|
"user", "kea",
|
||||||
"password", "keapassword",
|
"password", "keapassword",
|
||||||
"name", "keatest",
|
"name", "keatest",
|
||||||
@@ -445,6 +498,7 @@ TEST_F(DbAccessParserTest, incrementalChanges) {
|
|||||||
// Applying config2 will cause a wholesale change.
|
// Applying config2 will cause a wholesale change.
|
||||||
const char* config2[] = {"type", "mysql",
|
const char* config2[] = {"type", "mysql",
|
||||||
"host", "erewhon",
|
"host", "erewhon",
|
||||||
|
"port", "3306",
|
||||||
"user", "kea",
|
"user", "kea",
|
||||||
"password", "keapassword",
|
"password", "keapassword",
|
||||||
"name", "keatest",
|
"name", "keatest",
|
||||||
@@ -456,6 +510,7 @@ TEST_F(DbAccessParserTest, incrementalChanges) {
|
|||||||
NULL};
|
NULL};
|
||||||
const char* config3[] = {"type", "mysql",
|
const char* config3[] = {"type", "mysql",
|
||||||
"host", "erewhon",
|
"host", "erewhon",
|
||||||
|
"port", "3306",
|
||||||
"user", "me",
|
"user", "me",
|
||||||
"password", "meagain",
|
"password", "meagain",
|
||||||
"name", "keatest",
|
"name", "keatest",
|
||||||
@@ -475,6 +530,7 @@ TEST_F(DbAccessParserTest, incrementalChanges) {
|
|||||||
NULL};
|
NULL};
|
||||||
const char* config4[] = {"type", "mysql",
|
const char* config4[] = {"type", "mysql",
|
||||||
"host", "erewhon",
|
"host", "erewhon",
|
||||||
|
"port", "3306",
|
||||||
"user", "them",
|
"user", "them",
|
||||||
"password", "",
|
"password", "",
|
||||||
"name", "keatest",
|
"name", "keatest",
|
||||||
@@ -536,7 +592,7 @@ TEST_F(DbAccessParserTest, incrementalChanges) {
|
|||||||
// Check that the database access string is constructed correctly.
|
// Check that the database access string is constructed correctly.
|
||||||
TEST_F(DbAccessParserTest, getDbAccessString) {
|
TEST_F(DbAccessParserTest, getDbAccessString) {
|
||||||
const char* config[] = {"type", "mysql",
|
const char* config[] = {"type", "mysql",
|
||||||
"host", "" ,
|
"host", "",
|
||||||
"name", "keatest",
|
"name", "keatest",
|
||||||
NULL};
|
NULL};
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user