--- /dev/null
+// Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
+// Use of this source code is governed by a apache 2.0 license that can be
+// found in the LICENSE file.
+
+#include <gtest/gtest.h>
+
+#include <boost/filesystem/operations.hpp>
+#include <boost/filesystem/path.hpp>
+#include <boost/system/error_code.hpp>
+
+#include <string>
+
+#include "common/capability.h"
+#include "common/db_manager.h"
+#include "common/remote_device.h"
+#include "common/utils/logging.h"
+
+namespace bf = boost::filesystem;
+namespace bs = boost::system;
+
+namespace {
+
+const char kDBPath[] = "/run/capmgr/capmgr.db";
+const char kJournalPath[] = "/run/capmgr/capmgr.db-journal";
+const ::testing::Environment* env = nullptr;
+
+static boost::filesystem::path GetTrashPath(
+ const boost::filesystem::path& path) {
+ return path.string() + ".trash";
+}
+
+bool BackupPath(const bf::path& path) {
+ bf::path trash_path = GetTrashPath(path);
+ if (bf::exists(trash_path)) {
+ LOG(ERROR) << trash_path << " exists. Please remove "
+ << trash_path << " manually!";
+ return false;
+ }
+ bf::path backup_path = path.string() + ".bck";
+ std::cout << "Backup path: " << path << " to " << backup_path << std::endl;
+ bs::error_code error;
+ bf::remove_all(backup_path, error);
+ if (error)
+ LOG(ERROR) << "Remove failed: " << backup_path
+ << " (" << error.message() << ")";
+ if (bf::exists(path)) {
+ bf::rename(path, backup_path, error);
+ if (error) {
+ LOG(ERROR) << "Failed to setup test environment. Does some previous"
+ << " test crashed? Path: "
+ << backup_path << " should not exist.";
+ return false;
+ }
+ assert(!error);
+ }
+ return true;
+}
+
+bool RestorePath(const bf::path& path) {
+ bf::path backup_path = path.string() + ".bck";
+ std::cout << "Restore path: " << path << " from " << backup_path << std::endl;
+ bs::error_code error;
+ bf::remove_all(path, error);
+ if (error) {
+ bf::path trash_path = GetTrashPath(path);
+ LOG(ERROR) << "Remove failed: " << path << " (" << error.message() << ")";
+ std::cout << "Moving " << path << " to " << trash_path << std::endl;
+ bf::rename(path, trash_path, error);
+ if (error)
+ LOG(ERROR) << "Failed to move " << path << " to " << trash_path
+ << " (" << error.message() << ")";
+ else
+ LOG(ERROR) << trash_path << " should be removed manually!";
+ }
+ if (bf::exists(backup_path)) {
+ bf::rename(backup_path, path, error);
+ if (error) {
+ LOG(ERROR) << "Failed to restore backup path: " << backup_path
+ << " (" << error.message() << ")";
+ return false;
+ }
+ }
+ return true;
+}
+
+} // namespace
+
+namespace capmgr {
+
+class SmokeEnvironment : public ::testing::Environment {
+ public:
+ void SetUp() override {
+ // backup original db
+ if (!BackupPath(kDBPath))
+ LOG(ERROR) << "Failed to backup db";
+ if (!BackupPath(kJournalPath))
+ LOG(ERROR) << "Failed to backup db";
+ }
+
+ void TearDown() override {
+ if (!RestorePath(kDBPath))
+ LOG(ERROR) << "Failed to restore db";
+ if (!RestorePath(kJournalPath))
+ LOG(ERROR) << "Failed to restore db";
+ }
+};
+
+class SmokeTest : public ::testing::Test {
+ protected:
+ virtual void SetUp() {}
+ virtual void TearDown() {}
+};
+
+TEST_F(SmokeTest, InsertDeviceTest) {
+ RemoteDevice dev("insertdev", "addr1234", "tm1", "TM1", "5.0", "mobile",
+ "5.0");
+ ASSERT_TRUE(DBManager::InsertDevice(dev));
+}
+
+TEST_F(SmokeTest, DeleteDeviceTest) {
+ RemoteDevice dev("deletedev", "addr1234", "tm1", "TM1", "5.0", "mobile",
+ "5.0");
+ ASSERT_TRUE(DBManager::InsertDevice(dev));
+ ASSERT_TRUE(DBManager::DeleteDevice(dev));
+}
+
+TEST_F(SmokeTest, InsertCapTest) {
+ RemoteDevice dev("insertcapdev", "addr1234", "tm1", "TM1", "5.0", "mobile",
+ "5.0");
+ ASSERT_TRUE(DBManager::InsertDevice(dev));
+ Capability cap("operation", "uri", "mime", "appid", "pkgid");
+ ASSERT_TRUE(DBManager::InsertCapability("insertcapdev", cap));
+}
+
+TEST_F(SmokeTest, DeleteCapTest) {
+ RemoteDevice dev("deletecapdev", "addr1234", "tm1", "TM1", "5.0", "mobile",
+ "5.0");
+ ASSERT_TRUE(DBManager::InsertDevice(dev));
+ Capability cap("operation", "uri", "mime", "appid", "pkgid");
+ ASSERT_TRUE(DBManager::InsertCapability("deletecapdev", cap));
+ ASSERT_TRUE(DBManager::DeleteCapability("deletecapdev", cap));
+}
+
+} // namespace capmgr
+
+int main(int argc, char* argv[]) {
+ ::testing::InitGoogleTest(&argc, argv);
+ ::env = testing::AddGlobalTestEnvironment(new capmgr::SmokeEnvironment);
+ return RUN_ALL_TESTS();
+}