Provides macro for easy backup of libprivilege DB
authorPawel Broda <p.broda@partner.samsung.com>
Mon, 13 Jan 2014 15:51:32 +0000 (16:51 +0100)
committerMarcin Niesluchowski <m.niesluchow@samsung.com>
Thu, 23 Jan 2014 14:27:55 +0000 (15:27 +0100)
[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

tests/common/tests_common.cpp
tests/libprivilege-control-tests/common/libprivilege-control_test_common.h
tests/libprivilege-control-tests/libprivilege-control_test_common.cpp

index 8c70372..895a3c9 100644 (file)
@@ -52,7 +52,7 @@ int smack_check(void)
 
 void closeFdPtr(int *fd)
 {
-    close(*fd);
+    TEMP_FAILURE_RETRY(close(*fd));
 }
 
 /**
index cc040e6..e8d8e15 100644 (file)
@@ -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<typename T> struct list_deleter {
 };
 typedef std::unique_ptr<char*, list_deleter<char> > 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:
index 726142c..e15c978 100644 (file)
  * @brief   Main file for libprivilege-control unit tests.
  */
 
-#include <sys/smack.h>
-#include <sys/types.h>
-#include <unistd.h>
-
-#include <string>
+#include <fcntl.h>
+#include <iostream>
 #include <set>
+#include <string>
+#include <sys/sendfile.h>
 #include <libprivilege-control_test_common.h>
 #include <tests_common.h>
+#include <sys/smack.h>
 
 #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