Parcourir la source

[5061] Added unit test and pgsql support

Francis Dupont il y a 8 ans
Parent
commit
1cc6085151

+ 2 - 1
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

+ 1 - 0
doc/examples/kea6/mysql-reservations.json

@@ -47,6 +47,7 @@
     "user": "kea",
     "password": "kea",
     "host": "localhost",
+    "port": 3306,
     "readonly": true
   },
 

+ 35 - 1
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<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");

+ 57 - 1
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};