-// 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
// License, v. 2.0. If a copy of the MPL was not distributed with this
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;
try {
suser = getParameter("user");
bool quoteValue(const std::string& parameter) const {
return ((parameter != "persist") && (parameter != "lfc-interval") &&
(parameter != "connect-timeout") &&
+ (parameter != "port") &&
(parameter != "readonly"));
}
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
TEST_F(DbAccessParserTest, validTypeMysql) {
const char* config[] = {"type", "mysql",
"host", "erewhon",
+ "port", "3306",
"user", "kea",
"password", "keapassword",
"name", "keatest",
// A missing 'type' keyword should cause an exception to be thrown.
TEST_F(DbAccessParserTest, missingTypeKeyword) {
const char* config[] = {"host", "erewhon",
+ "port", "3306",
"user", "kea",
"password", "keapassword",
"name", "keatest",
// Applying config2 will cause a wholesale change.
const char* config2[] = {"type", "mysql",
"host", "erewhon",
+ "port", "3306",
"user", "kea",
"password", "keapassword",
"name", "keatest",
NULL};
const char* config3[] = {"type", "mysql",
"host", "erewhon",
+ "port", "3306",
"user", "me",
"password", "meagain",
"name", "keatest",
NULL};
const char* config4[] = {"type", "mysql",
"host", "erewhon",
+ "port", "3306",
"user", "them",
"password", "",
"name", "keatest",
// Check that the database access string is constructed correctly.
TEST_F(DbAccessParserTest, getDbAccessString) {
const char* config[] = {"type", "mysql",
- "host", "" ,
+ "host", "",
"name", "keatest",
NULL};