]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[2342] Add check that database name cannot be NULL
authorStephen Morris <stephen@isc.org>
Wed, 24 Oct 2012 18:34:38 +0000 (19:34 +0100)
committerStephen Morris <stephen@isc.org>
Wed, 24 Oct 2012 18:34:38 +0000 (19:34 +0100)
src/lib/dhcp/lease_mgr.h
src/lib/dhcp/mysql_lease_mgr.cc
src/lib/dhcp/mysql_lease_mgr.h
src/lib/dhcp/tests/mysql_lease_mgr_unittest.cc

index 6d74e5e7037fa2218a21cc2cee9d947097de373f..c21b9969d8b9da7056e10cf623558404897272a8 100644 (file)
 namespace isc {
 namespace dhcp {
 
+/// @brief Exception thrown if name of database is not specified
+class NoDatabaseName : public Exception {
+public:
+    NoDatabaseName(const char* file, size_t line, const char* what) :
+        isc::Exception(file, line, what) {}
+};
+
 /// @brief Exception thrown on failure to open database
 class DbOpenError : public Exception {
 public:
index 8fb73bda0d07fcffa2c7ac9835f9b644450db9eb..5f5feddd86d5675e7d18ad1ce4e28f76b83fc509 100644 (file)
@@ -471,8 +471,8 @@ MySqlLeaseMgr::openDatabase() {
         sname = getParameter("name");
         name = sname.c_str();
     } catch (...) {
-        // No database name.  Fine, we'll use NULL
-        ;
+        // No database name.  Throw a "NoName" exception
+        isc_throw(NoDatabaseName, "must specified a name for the database");
     }
 
     // Open the database.  Use defaults for non-specified options.
index 349b3bb839034db0164a934763eb1c1790909f6c..9a86691b824ae0bc26d8eb53e06169a01ad8462d 100644 (file)
@@ -45,6 +45,7 @@ public:
     /// @param parameters A data structure relating keywords and values
     ///        concerned with the database.
     ///
+    /// @exception NoDatabaseName Mandatory database name not given
     /// @exception DbOpenError Error opening the database
     /// @exception DbOperationError An operation on the open database has
     ///            failed.
@@ -362,6 +363,7 @@ private:
     /// Opens the database using the information supplied in the parameters
     /// passed to the constructor.
     ///
+    /// @exception NoDatabaseName Mandatory database name not given
     /// @exception DbOpenError Error opening the database
     void openDatabase();
 
index e8305e4d9876f93da55c642556f5f7cb0a65e449..c0f12a485a29a0193340abb2ec944b29c0cd7574 100644 (file)
@@ -46,8 +46,41 @@ const char* INVALID_PASSWORD = "password=invalid";
 string connectionString(const char* type, const char* name, const char* host,
                         const char* user, const char* password) {
     const string space = " ";
-    return (string(type) + space + string(name) + space + string(host) + space +
-            string(user) + space + string(password));
+    string result = "";
+
+    if (type != NULL) {
+        result += string(type);
+    }
+
+    if (name != NULL) {
+        if (! result.empty()) {
+            result += space;
+        }
+        result += string(name);
+    }
+
+    if (host != NULL) {
+        if (! result.empty()) {
+            result += space;
+        }
+        result += string(host);
+    }
+
+    if (user != NULL) {
+        if (! result.empty()) {
+            result += space;
+        }
+        result += string(user);
+    }
+
+    if (password != NULL) {
+        if (! result.empty()) {
+            result += space;
+        }
+        result += string(password);
+    }
+
+    return (result);
 }
 
 // Return valid connection string
@@ -101,6 +134,9 @@ TEST(MySqlOpenTest, OpenDatabase) {
     // Check that wrong specification of backend throws an exception.
     // (This is really a check on LeaseMgrFactory, but is convenient to
     // perform here.)
+    EXPECT_THROW(LeaseMgrFactory::create(connectionString(
+        NULL, VALID_NAME, VALID_HOST, INVALID_USER, VALID_PASSWORD)),
+        InvalidParameter);
     EXPECT_THROW(LeaseMgrFactory::create(connectionString(
         INVALID_TYPE, VALID_NAME, VALID_HOST, VALID_USER, VALID_PASSWORD)),
         InvalidType);
@@ -119,6 +155,11 @@ TEST(MySqlOpenTest, OpenDatabase) {
         VALID_TYPE, VALID_NAME, VALID_HOST, VALID_USER, INVALID_PASSWORD)),
         DbOpenError);
 
+    // Check for missing parameters
+    EXPECT_THROW(LeaseMgrFactory::create(connectionString(
+        VALID_TYPE, NULL, VALID_HOST, INVALID_USER, VALID_PASSWORD)),
+        NoDatabaseName);
+
     // Check that database opens correctly.
     ASSERT_NO_THROW(LeaseMgrFactory::create(validConnectionString()));
     EXPECT_NO_THROW((void) LeaseMgrFactory::instance());