#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"
#include <string>
#include <vector>
+#include <config.h>
#include <dpl/db/sql_connection.h>
#include <utils.h>
#include "security-manager-types.h"
*/
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
{
};
enum class Offline : bool { no, yes };
- enum class Db : bool { standard, test };
/**
* Constructor
* @exception PrivilegeDb::Exception::IOError on problems with database access
* 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
#include <sys/stat.h>
#include <dpl/log/log.h>
-#include <config.h>
#include "../gen/db.h"
#include "privilege_db.h"
#include "tzplatform-config.h"
}
} //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) {
mSqlConnection.Disconnect();
if (!forkExecWaitpid(loaderCmd, "fallback-only"))
throwDbInitEx("Failure rerunning the loader to apply fallback - giving up");
- mSqlConnection.Connect(path);
+ mSqlConnection.Connect(dbPath);
initDataCommands();
});
}
${DPL_PATH}/core/include
${DPL_PATH}/log/include
${DPL_PATH}/db/include
+ ${PROJECT_SOURCE_DIR}/test
)
SET(SERVER_SOURCES
#include <tzplatform_config.h>
#include <config.h>
+#include <testconfig.h>
#include <utils.h>
namespace {
#include <config.h>
#include <filesystem.h>
+#include <testconfig.h>
#include <utils.h>
#include "privilege_db.h"
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);
}
#include <string>
#include <sys/types.h>
+#include "config.h"
#include "privilege_db.h"
#define PRIVILEGE_DB_TEMPLATE DB_TEST_DIR"/.security-manager-test.db"
#include <config.h>
#include <filesystem.h>
+#include <testconfig.h>
#include "privilege_db.h"
#include "privilege_db_fixture.h"
#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"
#include <config.h>
#include <filesystem.h>
+#include <testconfig.h>
#include "privilege_db.h"
#include "privilege_db_fixture.h"
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();
}
--- /dev/null
+/*
+ * 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