diff options
author | Konrad Lipinski <k.lipinski2@partner.samsung.com> | 2018-10-15 09:31:41 +0200 |
---|---|---|
committer | Konrad Lipinski <k.lipinski2@partner.samsung.com> | 2018-10-15 09:34:06 +0200 |
commit | 3392b1bbbc4024f980913c1ae339d219ee88a932 (patch) | |
tree | 6edef8140a18b88f542678adae45be6d580ff14e | |
parent | 75293c9c70f3f04c86721039bedfd6e0bf0786a8 (diff) | |
download | security-manager-3392b1bbbc4024f980913c1ae339d219ee88a932.tar.gz security-manager-3392b1bbbc4024f980913c1ae339d219ee88a932.tar.bz2 security-manager-3392b1bbbc4024f980913c1ae339d219ee88a932.zip |
Replace runtime production/test db choice with compile-time policy
Change-Id: Ia13c7ec92f0ffdf4c2341b395a31b8097b4eeddd
-rw-r--r-- | src/common/include/config.h | 5 | ||||
-rw-r--r-- | src/common/include/privilege_db.h | 16 | ||||
-rw-r--r-- | src/common/privilege_db.cpp | 24 | ||||
-rw-r--r-- | src/server/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/server/rules-loader/security-manager-rules-loader.cpp | 1 | ||||
-rw-r--r-- | test/privilege_db_fixture.cpp | 3 | ||||
-rw-r--r-- | test/privilege_db_fixture.h | 1 | ||||
-rw-r--r-- | test/test_privilege_db_migration.cpp | 1 | ||||
-rw-r--r-- | test/test_privilege_db_privilege.cpp | 2 | ||||
-rw-r--r-- | test/test_privilege_db_transactions.cpp | 3 | ||||
-rw-r--r-- | test/testconfig.h | 33 |
11 files changed, 60 insertions, 30 deletions
diff --git a/src/common/include/config.h b/src/common/include/config.h index ec59824f..a71b9f9d 100644 --- a/src/common/include/config.h +++ b/src/common/include/config.h @@ -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" diff --git a/src/common/include/privilege_db.h b/src/common/include/privilege_db.h index 17cb8041..8e206501 100644 --- a/src/common/include/privilege_db.h +++ b/src/common/include/privilege_db.h @@ -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 diff --git a/src/common/privilege_db.cpp b/src/common/privilege_db.cpp index 62b09e2c..2c98376e 100644 --- a/src/common/privilege_db.cpp +++ b/src/common/privilege_db.cpp @@ -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(); }); } diff --git a/src/server/CMakeLists.txt b/src/server/CMakeLists.txt index 9db7821b..eb74ebd0 100644 --- a/src/server/CMakeLists.txt +++ b/src/server/CMakeLists.txt @@ -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 diff --git a/src/server/rules-loader/security-manager-rules-loader.cpp b/src/server/rules-loader/security-manager-rules-loader.cpp index 8b2d1208..bf422514 100644 --- a/src/server/rules-loader/security-manager-rules-loader.cpp +++ b/src/server/rules-loader/security-manager-rules-loader.cpp @@ -32,6 +32,7 @@ #include <tzplatform_config.h> #include <config.h> +#include <testconfig.h> #include <utils.h> namespace { diff --git a/test/privilege_db_fixture.cpp b/test/privilege_db_fixture.cpp index 707e61f2..4079dd54 100644 --- a/test/privilege_db_fixture.cpp +++ b/test/privilege_db_fixture.cpp @@ -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); } diff --git a/test/privilege_db_fixture.h b/test/privilege_db_fixture.h index c13d13c6..9b79e0dc 100644 --- a/test/privilege_db_fixture.h +++ b/test/privilege_db_fixture.h @@ -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" diff --git a/test/test_privilege_db_migration.cpp b/test/test_privilege_db_migration.cpp index 26630474..ba450ac6 100644 --- a/test/test_privilege_db_migration.cpp +++ b/test/test_privilege_db_migration.cpp @@ -23,6 +23,7 @@ #include <config.h> #include <filesystem.h> +#include <testconfig.h> #include "privilege_db.h" #include "privilege_db_fixture.h" diff --git a/test/test_privilege_db_privilege.cpp b/test/test_privilege_db_privilege.cpp index acff28c4..c121342d 100644 --- a/test/test_privilege_db_privilege.cpp +++ b/test/test_privilege_db_privilege.cpp @@ -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" diff --git a/test/test_privilege_db_transactions.cpp b/test/test_privilege_db_transactions.cpp index cbfa2730..272d3465 100644 --- a/test/test_privilege_db_transactions.cpp +++ b/test/test_privilege_db_transactions.cpp @@ -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 index 00000000..215e241e --- /dev/null +++ b/test/testconfig.h @@ -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 |