#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;
};
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:
* @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"
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