From: Francis Dupont Date: Sat, 28 Jan 2017 21:34:55 +0000 (+0100) Subject: [5061] Added unit test and pgsql support X-Git-Tag: trac5137_base~6^2~9 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=1cc60851511c7a227cded3dbee28b6b59e338d6a;p=thirdparty%2Fkea.git [5061] Added unit test and pgsql support --- diff --git a/doc/examples/kea4/mysql-reservations.json b/doc/examples/kea4/mysql-reservations.json index b72e5e43b6..4f2e307625 100644 --- a/doc/examples/kea4/mysql-reservations.json +++ b/doc/examples/kea4/mysql-reservations.json @@ -58,7 +58,8 @@ "name": "kea", "user": "kea", "password": "kea", - "host": "localhost" + "host": "localhost", + "port": 3306 }, # Define a subnet with a single pool of dynamic addresses. Addresses from diff --git a/doc/examples/kea6/mysql-reservations.json b/doc/examples/kea6/mysql-reservations.json index e1a57266b7..e31443a705 100644 --- a/doc/examples/kea6/mysql-reservations.json +++ b/doc/examples/kea6/mysql-reservations.json @@ -47,6 +47,7 @@ "user": "kea", "password": "kea", "host": "localhost", + "port": 3306, "readonly": true }, diff --git a/src/lib/dhcpsrv/pgsql_connection.cc b/src/lib/dhcpsrv/pgsql_connection.cc index cea224e57b..f443ba19ea 100644 --- a/src/lib/dhcpsrv/pgsql_connection.cc +++ b/src/lib/dhcpsrv/pgsql_connection.cc @@ -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 // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -153,6 +153,40 @@ PgSqlConnection::openDatabase() { 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(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::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"); diff --git a/src/lib/dhcpsrv/tests/dbaccess_parser_unittest.cc b/src/lib/dhcpsrv/tests/dbaccess_parser_unittest.cc index 5fa1081dcc..7216351e3f 100644 --- a/src/lib/dhcpsrv/tests/dbaccess_parser_unittest.cc +++ b/src/lib/dhcpsrv/tests/dbaccess_parser_unittest.cc @@ -178,6 +178,7 @@ private: bool quoteValue(const std::string& parameter) const { return ((parameter != "persist") && (parameter != "lfc-interval") && (parameter != "connect-timeout") && + (parameter != "port") && (parameter != "readonly")); } @@ -402,10 +403,61 @@ TEST_F(DbAccessParserTest, largeTimeout) { 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", @@ -423,6 +475,7 @@ TEST_F(DbAccessParserTest, validTypeMysql) { // 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", @@ -445,6 +498,7 @@ TEST_F(DbAccessParserTest, incrementalChanges) { // Applying config2 will cause a wholesale change. const char* config2[] = {"type", "mysql", "host", "erewhon", + "port", "3306", "user", "kea", "password", "keapassword", "name", "keatest", @@ -456,6 +510,7 @@ TEST_F(DbAccessParserTest, incrementalChanges) { NULL}; const char* config3[] = {"type", "mysql", "host", "erewhon", + "port", "3306", "user", "me", "password", "meagain", "name", "keatest", @@ -475,6 +530,7 @@ TEST_F(DbAccessParserTest, incrementalChanges) { NULL}; const char* config4[] = {"type", "mysql", "host", "erewhon", + "port", "3306", "user", "them", "password", "", "name", "keatest", @@ -536,7 +592,7 @@ TEST_F(DbAccessParserTest, incrementalChanges) { // Check that the database access string is constructed correctly. TEST_F(DbAccessParserTest, getDbAccessString) { const char* config[] = {"type", "mysql", - "host", "" , + "host", "", "name", "keatest", NULL};