diff options
-rw-r--r-- | src/lib/database/database_connection.h | 9 | ||||
-rw-r--r-- | src/lib/database/testutils/schema.cc | 1 | ||||
-rw-r--r-- | src/lib/database/testutils/schema.h | 1 | ||||
-rw-r--r-- | src/lib/mysql/mysql_connection.cc | 7 | ||||
-rw-r--r-- | src/lib/mysql/tests/mysql_connection_unittest.cc | 10 | ||||
-rw-r--r-- | src/lib/pgsql/pgsql_connection.cc | 7 | ||||
-rw-r--r-- | src/lib/pgsql/tests/pgsql_connection_unittest.cc | 9 |
7 files changed, 42 insertions, 2 deletions
diff --git a/src/lib/database/database_connection.h b/src/lib/database/database_connection.h index 881bfbac3d..f28f326745 100644 --- a/src/lib/database/database_connection.h +++ b/src/lib/database/database_connection.h @@ -68,6 +68,15 @@ public: isc::Exception(file, line, what) {} }; +/// @brief Invalid port number +/// +/// Thrown when the port number specified for the database connection is invalid. +class DbInvalidPort : public Exception { +public: + DbInvalidPort(const char* file, size_t line, const char* what) : + isc::Exception(file, line, what) {} +}; + /// @brief Invalid 'readonly' value specification. /// /// Thrown when the value of the 'readonly' boolean parameter is invalid. diff --git a/src/lib/database/testutils/schema.cc b/src/lib/database/testutils/schema.cc index 80371e5abc..b396b9ec45 100644 --- a/src/lib/database/testutils/schema.cc +++ b/src/lib/database/testutils/schema.cc @@ -25,6 +25,7 @@ const char* INVALID_NAME = "name=invalidname"; const char* VALID_HOST = "host=localhost"; const char* VALID_HOST_TCP = "host=127.0.0.1"; const char* INVALID_HOST = "host=invalidhost"; +const char* INVALID_PORT_1 = "port=65536"; const char* VALID_USER = "user=keatest"; const char* VALID_READONLY_USER = "user=keatest_readonly"; const char* VALID_SECURE_USER = "user=keatest_secure"; diff --git a/src/lib/database/testutils/schema.h b/src/lib/database/testutils/schema.h index 55c7b77864..2c08bed334 100644 --- a/src/lib/database/testutils/schema.h +++ b/src/lib/database/testutils/schema.h @@ -21,6 +21,7 @@ extern const char* INVALID_NAME; extern const char* VALID_HOST; extern const char* VALID_HOST_TCP; extern const char* INVALID_HOST; +extern const char* INVALID_PORT_1; extern const char* VALID_USER; extern const char* VALID_READONLY_USER; extern const char* VALID_SECURE_USER; diff --git a/src/lib/mysql/mysql_connection.cc b/src/lib/mysql/mysql_connection.cc index 1b1e4fc7da..9db0da5416 100644 --- a/src/lib/mysql/mysql_connection.cc +++ b/src/lib/mysql/mysql_connection.cc @@ -65,7 +65,12 @@ MySqlConnection::openDatabase() { } unsigned int port = 0; - setIntParameterValue("port", 0, numeric_limits<uint16_t>::max(), port); + try { + setIntParameterValue("port", 0, numeric_limits<uint16_t>::max(), port); + + } catch (const std::exception& ex) { + isc_throw(DbInvalidPort, ex.what()); + } const char* user = NULL; string suser; diff --git a/src/lib/mysql/tests/mysql_connection_unittest.cc b/src/lib/mysql/tests/mysql_connection_unittest.cc index 46a8cb0abc..a6d4bbde49 100644 --- a/src/lib/mysql/tests/mysql_connection_unittest.cc +++ b/src/lib/mysql/tests/mysql_connection_unittest.cc @@ -609,6 +609,16 @@ TEST_F(MySqlConnectionTest, transactions) { EXPECT_THROW(conn_.rollback(), isc::Unexpected); } +// Tests that invalid port value causes an error. +TEST_F(MySqlConnectionTest, portInvalid) { + std::string conn_str = connectionString(MYSQL_VALID_TYPE, VALID_NAME, + VALID_HOST_TCP, VALID_USER, + VALID_PASSWORD, INVALID_PORT_1); + MySqlConnection conn(DatabaseConnection::parse(conn_str)); + EXPECT_THROW(conn.openDatabase(), DbInvalidPort); +} + + // Tests that valid connection timeout is accepted. TEST_F(MySqlConnectionTest, connectionTimeout) { std::string conn_str = connectionString(MYSQL_VALID_TYPE, VALID_NAME, diff --git a/src/lib/pgsql/pgsql_connection.cc b/src/lib/pgsql/pgsql_connection.cc index d7a08686c0..77b71260be 100644 --- a/src/lib/pgsql/pgsql_connection.cc +++ b/src/lib/pgsql/pgsql_connection.cc @@ -193,7 +193,12 @@ PgSqlConnection::getConnParameters() { dbconnparameters += "host = '" + shost + "'" ; unsigned int port = 0; - setIntParameterValue("port", 0, numeric_limits<uint16_t>::max(), port); + try { + setIntParameterValue("port", 0, numeric_limits<uint16_t>::max(), port); + + } catch (const std::exception& ex) { + isc_throw(DbInvalidPort, ex.what()); + } // Add port to connection parameters when not default. if (port > 0) { diff --git a/src/lib/pgsql/tests/pgsql_connection_unittest.cc b/src/lib/pgsql/tests/pgsql_connection_unittest.cc index 897904a7ac..906b3fc389 100644 --- a/src/lib/pgsql/tests/pgsql_connection_unittest.cc +++ b/src/lib/pgsql/tests/pgsql_connection_unittest.cc @@ -563,6 +563,15 @@ TEST_F(PgSqlConnectionTest, savepoints) { ASSERT_NO_THROW_LOG(testSelect(three_rows, 0, 10)); } +// Tests that invalid port value causes an error. +TEST_F(PgSqlConnectionTest, portInvalid) { + std::string conn_str = connectionString(PGSQL_VALID_TYPE, VALID_NAME, + VALID_USER, VALID_PASSWORD, + INVALID_PORT_1); + PgSqlConnection conn(DatabaseConnection::parse(conn_str)); + EXPECT_THROW(conn.getConnParameters(), DbInvalidPort); +} + // Tests that valid connection timeout is accepted. TEST_F(PgSqlConnectionTest, connectionTimeout) { std::string conn_str = connectionString(PGSQL_VALID_TYPE, VALID_NAME, |