From: Pawel Broda Date: Mon, 13 Jan 2014 15:51:32 +0000 (+0100) Subject: Provides macro for easy backup of libprivilege DB X-Git-Tag: security-manager_5.5_testing~301 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=9c638419ddc9363bda7f349d647555840db5b7b5;p=platform%2Fcore%2Ftest%2Fsecurity-tests.git Provides macro for easy backup of libprivilege DB [Issue#] SSDWSSP-732 [Feature] Implement better DB management in our libprivilege-control tests. [Cause] It was not possible to withdraw changes done by some of the tests. As a result some of the DB changes were permanent. [Solution] Provide a macro which backups DB before each test and copies it back after the test. [Verification] Build and run on the target. Change-Id: I2c46c69ee3cc122bc8567e5f97ed9530940bb508 --- diff --git a/tests/common/tests_common.cpp b/tests/common/tests_common.cpp index 8c703720..895a3c9d 100644 --- a/tests/common/tests_common.cpp +++ b/tests/common/tests_common.cpp @@ -52,7 +52,7 @@ int smack_check(void) void closeFdPtr(int *fd) { - close(*fd); + TEMP_FAILURE_RETRY(close(*fd)); } /** diff --git a/tests/libprivilege-control-tests/common/libprivilege-control_test_common.h b/tests/libprivilege-control-tests/common/libprivilege-control_test_common.h index cc040e6d..e8d8e15f 100644 --- a/tests/libprivilege-control-tests/common/libprivilege-control_test_common.h +++ b/tests/libprivilege-control-tests/common/libprivilege-control_test_common.h @@ -96,6 +96,9 @@ #define APP_NPRUNTIME "app_np_test" #define APP_NPRUNTIME_FILE "/etc/smack/test_privilege_control_DIR/app_dir/exec" +const std::string RDB_PATH("/opt/dbspace/.rules-db.db3"); +const std::string RDB_PATH_BACKUP("/opt/dbspace/.rules-db.db3.backup"); + //correct and incorrect PID used in incorrect params test const pid_t PID_CORRECT = 0; const pid_t PID_INCORRECT = -1; @@ -135,6 +138,15 @@ template struct list_deleter { }; typedef std::unique_ptr > CStringListPtr; +class DBBackup { +private: + bool backupfile(const std::string& src, const std::string& dst); + bool restorefile(const std::string& src, const std::string& dst); +public: + DBBackup(); + ~DBBackup(); +}; + class Directory { public: diff --git a/tests/libprivilege-control-tests/libprivilege-control_test_common.cpp b/tests/libprivilege-control-tests/libprivilege-control_test_common.cpp index 726142ce..e15c9783 100644 --- a/tests/libprivilege-control-tests/libprivilege-control_test_common.cpp +++ b/tests/libprivilege-control-tests/libprivilege-control_test_common.cpp @@ -21,14 +21,14 @@ * @brief Main file for libprivilege-control unit tests. */ -#include -#include -#include - -#include +#include +#include #include +#include +#include #include #include +#include #define CANARY_LABEL "tiny_yellow_canary" @@ -46,6 +46,61 @@ const char* PRIV_APPSETTING[] {"org.tizen.privilege.appsetting", NULL}; const char* PRIVS_AV[] = { "org.tizen.privilege.antivirus", NULL }; +bool DBBackup::backupfile(const std::string& src, const std::string& dst) +{ + int fdsrc = TEMP_FAILURE_RETRY(open(src.c_str(), O_RDONLY)); + if (fdsrc == -1) + return false; + FDUniquePtr FdPtrSrc(&fdsrc, closeFdPtr); + + struct stat stat_source; + if (fstat(fdsrc, &stat_source) == -1) + return false; + + int fddst = TEMP_FAILURE_RETRY(open(dst.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644)); + if (fddst == -1) + return false; + FDUniquePtr FdPtrDst(&fddst, closeFdPtr); + + if (sendfile(fddst, fdsrc, 0, stat_source.st_size) == -1) + return false; + + return true; +} + +bool DBBackup::restorefile(const std::string& src, const std::string& dst) +{ + if (rename(src.c_str(), dst.c_str()) == -1) + return false; + + return true; +} + +DBBackup::DBBackup() +{ + RUNNER_ASSERT_MSG_BT(backupfile(RDB_PATH, RDB_PATH_BACKUP), + "libprivilege DB backup failed. Errno: " << strerror(errno)); +} + +DBBackup::~DBBackup() +{ + if (!restorefile(RDB_PATH_BACKUP, RDB_PATH)) { + + std::string fatal_error = + "\n\n" + "!!! !!!\n" + "!!! FATAL ERROR - libprivilege DB restoring failed. !!!\n" + "!!! libprivilege-control tests are not valid. !!!\n" + "!!! Reinstall libprivilege-control package. !!!\n" + "!!! !!!\n"; + + if (std::uncaught_exception()) // don't throw! + std::cerr << fatal_error << std::flush; + else + RUNNER_ASSERT_MSG_BT(false, fatal_error); + } +} + /** * Check if every rule is true. * @return 1 if ALL rules in SMACK, 0 if ANY rule isn't, -1 on failure