Browse Source

defined a new exception class, Sqlite3Error, to throw an exception with
sqlite3 related errors.
used the new class when Sqlite3DataSrc::open() failed.
added a test case for this.


git-svn-id: svn://bind10.isc.org/svn/bind10/trunk@1188 e5f2f494-b856-4b98-b285-d166d9295462

JINMEI Tatuya 15 years ago
parent
commit
d08ed40d52

+ 5 - 7
src/lib/auth/data_source_sqlite3.cc

@@ -15,6 +15,7 @@
 // $Id$
 
 #include <string>
+#include <sstream>
 
 #include "data_source_sqlite3.h"
 
@@ -284,7 +285,7 @@ Sqlite3DataSrc::findClosest(const char* const name,
                             const char** position) const
 {
     const char* current = name;
-    
+
     while (*current != 0) {
         const int rc = hasExactZone(current);
         if (rc >= 0) {
@@ -683,15 +684,12 @@ Sqlite3DataSrc::findReferral(const Query& q,
 //
 void
 Sqlite3DataSrc::open(const string& name) {
-    database_name = name;
-    
-    if (sqlite3_open(database_name.c_str(), &db) != 0) {
-        cerr << "open database: " << sqlite3_errmsg(db) << "\n";
+    if (sqlite3_open(name.c_str(), &db) != 0) {
         // sqlite3_close() must be called even when open fails.
         sqlite3_close(db);
-        throw("Cannot open database");
+        isc_throw(Sqlite3Error, "Cannot open Sqlite3 database file: " << name);
     }
-    
+
     checkAndSetupSchema();
 }
 

+ 8 - 1
src/lib/auth/data_source_sqlite3.h

@@ -19,6 +19,8 @@
 
 #include <string>
 
+#include <exceptions/exceptions.h>
+
 #include <sqlite3.h>
 
 #include "data_source.h"
@@ -36,6 +38,12 @@ namespace auth {
 
 class Query;
 
+class Sqlite3Error : public Exception {
+public:
+    Sqlite3Error(const char* file, size_t line, const char* what) :
+        isc::Exception(file, line, what) {}
+};
+
 class Sqlite3DataSrc : public DataSrc {
     ///
     /// \name Constructors, Assignment Operator and Destructor.
@@ -119,7 +127,6 @@ private:
     void checkAndSetupSchema(void);
 
     sqlite3 *db;
-    std::string database_name;
     int database_version;
     
     //

+ 10 - 0
src/lib/auth/data_source_sqlite3_unittest.cc

@@ -42,6 +42,11 @@ using namespace isc::auth;
 namespace {
 static const char* SQLITE_DBFILE_EXAMPLE = "testdata/test.sqlite3";
 static const char* SQLITE_DBFILE_EXAMPLE2 = "testdata/test2.sqlite3";
+// The following file must be non existent and mutt be "creatable";
+// the sqlite3 library will try to create a new DB file if it doesn't exist,
+// so to test a failure case the create operation should also fail.
+// The "nodir", a non existent directory, is inserted for this purpose.
+static const char* SQLITE_DBFILE_NOTEXIST = "testdata/nodir/notexist";
 
 static const string sigdata_common(" 20100322084538 20100220084538 "
                                    "33495 example.com. FAKEFAKEFAKEFAKE");
@@ -353,6 +358,11 @@ TEST_F(Sqlite3DataSourceTest, reOpen) {
     EXPECT_EQ(NULL, name_match.bestDataSrc());
 }
 
+TEST_F(Sqlite3DataSourceTest, openFail) {
+    EXPECT_EQ(DataSrc::SUCCESS, data_source.close());
+    EXPECT_THROW(data_source.init(SQLITE_DBFILE_NOTEXIST), Sqlite3Error);
+}
+
 TEST_F(Sqlite3DataSourceTest, findClosestEnclosure) {
     NameMatch name_match(www_name);
     data_source.findClosestEnclosure(name_match);