Generic solution for onlycap issues
[platform/core/test/security-tests.git] / src / common / tests_common.cpp
index 6808b57..235b062 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2013 - 2019 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.
  */
 
 #include "tests_common.h"
+#include <fcntl.h>
 #include <sys/mman.h>
 #include <sys/stat.h>
 #include <sys/types.h>
+#include <sys/wait.h>
 #include <unistd.h>
 #include <grp.h>
 #include <errno.h>
 #include <vector>
 #include <algorithm>
 
-int DB::Transaction::db_result = PC_OPERATION_SUCCESS;
-
-const char *WGT_APP_ID = "QwCqJ0ttyS";
-
-int smack_runtime_check(void)
-{
-    static int smack_present = -1;
-    if (-1 == smack_present) {
-        if (smack_smackfs_path()) {
-            smack_present = 1;
-        } else {
-            smack_present = 0;
-        }
-    }
-    return smack_present;
-}
-
-int smack_check(void)
+bool smack_check(void)
 {
 #ifndef WRT_SMACK_ENABLED
-    return 0;
+    return false;
 #else
-    return smack_runtime_check();
+    static int smack_present = -1;
+    if (-1 == smack_present)
+        smack_present = smack_smackfs_path() == nullptr ? 0 : 1;
+    return smack_present == 1;
 #endif
 }
 
@@ -77,12 +65,6 @@ int drop_root_privileges(uid_t appUid, gid_t appGid)
     return 1;
 }
 
-void setLabelForSelf(const int line, const char *label)
-{
-    int ret = smack_set_label_for_self(label);
-    RUNNER_ASSERT_MSG(ret == 0, "Error in smack_set_label_for_self(): " << ret << ", line: " << line);
-}
-
 /*
  * Add a new group to the current process groups.
  */
@@ -183,3 +165,95 @@ int files_compare(int fd1, int fd2)
 
     return result;
 }
+
+void mkdirSafe(const std::string &path, mode_t mode)
+{
+    RUNNER_ASSERT_ERRNO_MSG(0 == mkdir(path.c_str(), mode) || errno == EEXIST,
+                            "mkdir for <" << path << "> with mode <" << mode << "> failed");
+}
+
+void mktreeSafe(const std::string &path, mode_t mode)
+{
+    // Create subsequent parent directories
+    // Assume that path is absolute - i.e. starts with '/'
+    for (size_t pos = 0; (pos = path.find("/", pos + 1)) != std::string::npos; )
+        mkdirSafe(path.substr(0, pos).c_str(), mode);
+
+    mkdirSafe(path, mode);
+}
+
+void creatSafe(const std::string &path, mode_t mode)
+{
+    RUNNER_ASSERT_ERRNO_MSG(-1 != creat(path.c_str(), mode),
+                            "creat for <" << path << "> with mode <" << mode << "> failed");
+}
+
+void symlinkSafe(const std::string &targetPath, const std::string &linkPath)
+{
+    RUNNER_ASSERT_ERRNO_MSG(0 == symlink(targetPath.c_str(), linkPath.c_str()),
+                            "symlink for <" << linkPath << "> to <" << targetPath << "> failed");
+}
+
+void removeDir(const std::string &path)
+{
+    DIR *d = opendir(path.c_str());
+
+    if (nullptr == d) {
+        RUNNER_ASSERT_ERRNO_MSG(errno == ENOENT, "opendir of <" << path << "> failed");
+        return;
+    }
+
+    struct dirent *dirEntry;
+    while (nullptr != (dirEntry = readdir(d))) {
+        std::string entryName(dirEntry->d_name);
+        if (entryName == "." || entryName == "..")
+            continue;
+
+        std::string entryPath(path + "/" + entryName);
+        struct stat st;
+
+        RUNNER_ASSERT_ERRNO_MSG(0 == lstat(entryPath.c_str(), &st),
+                                "stat for <" << entryPath << "> failed");
+        if (S_ISDIR(st.st_mode))
+            removeDir(entryPath);
+        else
+            RUNNER_ASSERT_ERRNO_MSG(0 == unlink(entryPath.c_str()),
+                                    "unlink for <" << entryPath << "> failed");
+    }
+
+    closedir(d);
+
+    RUNNER_ASSERT_ERRNO_MSG(0 == rmdir(path.c_str()), "rmdir for <" << path << "> failed");
+}
+
+void waitPid(pid_t pid)
+{
+    int status;
+    pid_t ret = waitpid(pid, &status, 0);
+    RUNNER_ASSERT_MSG((ret != -1) && WIFEXITED(status) && WEXITSTATUS(status) == 0,
+        "Child process exited abnormally" <<
+        ": ret=" << ret << ", errno=" << errno << ", status=" << status);
+}
+
+pid_t runInChild(const std::function<void(void)> &process) {
+    pid_t pid = fork();
+    RUNNER_ASSERT_ERRNO_MSG(pid >= 0, "fork failed");
+
+    if (pid == 0) {
+        process();
+        exit(EXIT_SUCCESS);
+    }
+    return pid;
+}
+
+void runInChildParentWait(const std::function<void(void)> &process) {
+    pid_t pid = fork();
+    RUNNER_ASSERT_ERRNO_MSG(pid >= 0, "fork failed");
+    if (pid == 0) {
+        process();
+        exit(EXIT_SUCCESS);
+    } else {
+        waitPid(pid);
+    }
+}
+