Replace runtime production/test db choice with compile-time policy 76/191276/1
authorKonrad Lipinski <k.lipinski2@partner.samsung.com>
Mon, 15 Oct 2018 07:31:41 +0000 (09:31 +0200)
committerKonrad Lipinski <k.lipinski2@partner.samsung.com>
Mon, 15 Oct 2018 07:34:06 +0000 (09:34 +0200)
Change-Id: Ia13c7ec92f0ffdf4c2341b395a31b8097b4eeddd

src/common/include/config.h
src/common/include/privilege_db.h
src/common/privilege_db.cpp
src/server/CMakeLists.txt
src/server/rules-loader/security-manager-rules-loader.cpp
test/privilege_db_fixture.cpp
test/privilege_db_fixture.h
test/test_privilege_db_migration.cpp
test/test_privilege_db_privilege.cpp
test/test_privilege_db_transactions.cpp
test/testconfig.h [new file with mode: 0644]

index ec59824f3955d49d80d0344d08534ddfbd59d6d2..a71b9f9d1ad9fbe37cd5174ed208e405c8e3d666 100644 (file)
@@ -75,8 +75,3 @@ std::string getPrivilegeDbFallbackPath();
 #define DB_JOURNAL_SUFFIX "-journal"
 
 #define DB_OK_MARKER "/tmp/.security-manager.db.ok"
-#define RULES_LOADER_CMD "/usr/bin/security-manager-rules-loader"
-#define TEST_DB_OK_MARKER "/tmp/.security-manager-test.db.ok"
-#define TEST_RULES_LOADER_CMD "/usr/bin/security-manager-test-rules-loader"
-#define TEST_DB_PATH "/tmp/.security-manager-test.db"
-#define TEST_PRIVILEGE_FALLBACK_DB_PATH "/tmp/.security-manager-test.fallback.db"
index 17cb80411100ea1c41e8191cd53ea70a9568c4fa..8e2065013e3a7a151833919afe53e5561772ce73 100644 (file)
@@ -40,6 +40,7 @@
 #include <string>
 #include <vector>
 
+#include <config.h>
 #include <dpl/db/sql_connection.h>
 #include <utils.h>
 #include "security-manager-types.h"
@@ -140,6 +141,12 @@ private:
      */
     StatementWrapper getStatement(StmtType queryType);
 
+    struct DbStandard {
+        static auto path() { return Config::getPrivilegeDbPath(); }
+        static auto okMarkerPath() { return DB_OK_MARKER; }
+        static auto loaderCmd() { return "/usr/bin/security-manager-rules-loader"; }
+    };
+
 public:
     class Exception
     {
@@ -151,7 +158,6 @@ public:
     };
 
     enum class Offline : bool { no, yes };
-    enum class Db : bool { standard, test };
     /**
      * Constructor
      * @exception PrivilegeDb::Exception::IOError on problems with database access
@@ -159,8 +165,14 @@ public:
      *            configuration
      *
      */
-    explicit PrivilegeDb(Offline offline, Db db = Db::standard);
+    template <class Db = DbStandard>
+    explicit PrivilegeDb(Offline offline, const Db &db = Db())
+    : PrivilegeDb(offline, db.path(), db.okMarkerPath(), db.loaderCmd()) {}
 
+private:
+    explicit PrivilegeDb(Offline offline, const std::string &dbPath, const char *okMarkerPath, const char *loaderCmd);
+
+public:
     /**
      * Begin transaction
      * @exception PrivilegeDb::Exception::InternalError on internal error
index 62b09e2cca38f783b6fd11089d2c0acf8c9d41f3..2c98376e2ad8f294c448f2c46e09bb3a6dfe90cb 100644 (file)
@@ -36,7 +36,6 @@
 #include <sys/stat.h>
 
 #include <dpl/log/log.h>
-#include <config.h>
 #include "../gen/db.h"
 #include "privilege_db.h"
 #include "tzplatform-config.h"
@@ -136,27 +135,12 @@ void tryCatchDbInit(F &&f) {
 }
 } //namespace
 
-PrivilegeDb::PrivilegeDb(Offline offline, Db db) {
-    std::string path;
-    const char *okMarkerPath, *loaderCmd;
-    switch (db) {
-        case Db::standard:
-            path = Config::getPrivilegeDbPath();
-            okMarkerPath = DB_OK_MARKER;
-            loaderCmd = RULES_LOADER_CMD;
-            break;
-        case Db::test:
-            path = TEST_DB_PATH;
-            okMarkerPath = TEST_DB_OK_MARKER;
-            loaderCmd = TEST_RULES_LOADER_CMD;
-            break;
-    }
-
+PrivilegeDb::PrivilegeDb(Offline offline, const std::string &dbPath, const char *okMarkerPath, const char *loaderCmd) {
     bool didFallback = false;
-    if (!underlying(offline) && !FS::fileExists(okMarkerPath) && !(didFallback = FS::fileExists(path + DB_RECOVERED_SUFFIX)))
+    if (!underlying(offline) && !FS::fileExists(okMarkerPath) && !(didFallback = FS::fileExists(dbPath + DB_RECOVERED_SUFFIX)))
         throwDbInitEx("loader failed to initialize db - giving up");
 
-    tryCatchDbInit([&]{ mSqlConnection.Connect(path); });
+    tryCatchDbInit([&]{ mSqlConnection.Connect(dbPath); });
     try {
         initDataCommands();
     } catch (DB::SqlConnection::Exception::Base &e) {
@@ -169,7 +153,7 @@ PrivilegeDb::PrivilegeDb(Offline offline, Db db) {
             mSqlConnection.Disconnect();
             if (!forkExecWaitpid(loaderCmd, "fallback-only"))
                 throwDbInitEx("Failure rerunning the loader to apply fallback - giving up");
-            mSqlConnection.Connect(path);
+            mSqlConnection.Connect(dbPath);
             initDataCommands();
         });
     }
index 9db7821b7751b5c8e714472fa885b4c443224116..eb74ebd095b66f33c5573cdb3aa9253ed4c879a2 100644 (file)
@@ -20,6 +20,7 @@ INCLUDE_DIRECTORIES(
     ${DPL_PATH}/core/include
     ${DPL_PATH}/log/include
     ${DPL_PATH}/db/include
+    ${PROJECT_SOURCE_DIR}/test
     )
 
 SET(SERVER_SOURCES
index 8b2d1208633180009a8a6abb8dc5e64579ec543d..bf4225145e2d6b26f942b851b3effb28cf9f148d 100644 (file)
@@ -32,6 +32,7 @@
 #include <tzplatform_config.h>
 
 #include <config.h>
+#include <testconfig.h>
 #include <utils.h>
 
 namespace {
index 707e61f288fddc4d7448dfedb55080db48999b8e..4079dd5408f40d3c0fc037b93e176be8d29e9117 100644 (file)
@@ -30,6 +30,7 @@
 
 #include <config.h>
 #include <filesystem.h>
+#include <testconfig.h>
 #include <utils.h>
 
 #include "privilege_db.h"
@@ -101,7 +102,7 @@ PrivilegeDBFixture::PrivilegeDBFixture(const std::string &src, const std::string
         putFile(fallback, TEST_PRIVILEGE_FALLBACK_DB_PATH);
     forkExecWaitpid(TEST_RULES_LOADER_CMD, "no-load");
     checkMarker(preMgr);
-    testPrivDb = new PrivilegeDb(PrivilegeDb::Offline::no, PrivilegeDb::Db::test);
+    testPrivDb = new PrivilegeDb(PrivilegeDb::Offline::no, Config::DbTest());
     checkMarker(PostMgrMarker::unchanged == postMgr ? preMgr : Marker::fallback);
 }
 
index c13d13c6aac01b7cd2913029ec7c244aca0d5574..9b79e0dc61e71e1a0f69e61b6a37da1fa5d038c6 100644 (file)
@@ -21,6 +21,7 @@
 #include <string>
 #include <sys/types.h>
 
+#include "config.h"
 #include "privilege_db.h"
 
 #define PRIVILEGE_DB_TEMPLATE DB_TEST_DIR"/.security-manager-test.db"
index 266304749a5342088f08deaf48fd4a2795bdd378..ba450ac6b5b66a2db1215c56d5d0a4a8d4a43b36 100644 (file)
@@ -23,6 +23,7 @@
 
 #include <config.h>
 #include <filesystem.h>
+#include <testconfig.h>
 #include "privilege_db.h"
 #include "privilege_db_fixture.h"
 
index acff28c45ee50259e166979c73e59f7367daf61f..c121342dac5411da7312e9b9f2aac5f4b1d9bb16 100644 (file)
@@ -28,7 +28,7 @@
 #include <boost/test/results_reporter.hpp>
 #include <boost/test/utils/wrap_stringstream.hpp>
 
-#include <config.h> // TEST_DB_PATH
+#include <testconfig.h> // TEST_DB_PATH
 #include "privilege_db.h"
 #include "privilege_db_fixture.h"
 
index cbfa273022eabb234b437260b1320513c8684d51..272d346549de9c25984ab020e169733415c6072e 100644 (file)
@@ -29,6 +29,7 @@
 
 #include <config.h>
 #include <filesystem.h>
+#include <testconfig.h>
 
 #include "privilege_db.h"
 #include "privilege_db_fixture.h"
@@ -46,7 +47,7 @@ BOOST_FIXTURE_TEST_CASE(T100_privilegedb_constructor, Empty)
 
     purgeDb();
     // db init must fail w/ no loader having run beforehand
-    BOOST_REQUIRE_THROW(testPrivDb.reset(new PrivilegeDb(PrivilegeDb::Offline::no, PrivilegeDb::Db::test)),
+    BOOST_REQUIRE_THROW(testPrivDb.reset(new PrivilegeDb(PrivilegeDb::Offline::no, Config::DbTest())),
         PrivilegeDb::Exception::IOError);
     requireNoDb();
 }
diff --git a/test/testconfig.h b/test/testconfig.h
new file mode 100644 (file)
index 0000000..215e241
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ *  Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+#pragma once
+
+#define TEST_DB_OK_MARKER "/tmp/.security-manager-test.db.ok"
+#define TEST_RULES_LOADER_CMD "/usr/bin/security-manager-test-rules-loader"
+#define TEST_DB_PATH "/tmp/.security-manager-test.db"
+#define TEST_PRIVILEGE_FALLBACK_DB_PATH "/tmp/.security-manager-test.fallback.db"
+
+namespace SecurityManager {
+namespace Config {
+
+struct DbTest {
+    static auto path() { return TEST_DB_PATH; }
+    static auto okMarkerPath() { return TEST_DB_OK_MARKER; }
+    static auto loaderCmd() { return TEST_RULES_LOADER_CMD; }
+};
+
+} // namespace Config
+} // namespace SecurityManager