Spring cleaning
[platform/core/test/security-tests.git] / src / common / tests_common.cpp
index 6808b57..b4d0d33 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2013-2020 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 <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,62 +61,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.
- */
-void add_process_group(const char* group_name)
-{
-    // get group ID by group name
-    group *gr = getgrnam(group_name);
-    RUNNER_ASSERT_ERRNO_MSG(gr != nullptr, "getgrnam failed on '" << group_name << "' group");
-    const gid_t new_group_id = gr->gr_gid;
-
-    // get number of groups that the current process belongs to
-    int ngroups = getgroups(0, nullptr);
-
-    //allocate groups table + space for new group entry
-    std::vector<gid_t> groups(ngroups + 1);
-    getgroups(ngroups, groups.data());
-
-    // check if the process already belongs to the group
-    if (std::find(groups.begin(), groups.end(), new_group_id) != groups.end()) return;
-
-    // add new group & apply change
-    groups[ngroups] = new_group_id;
-    int ret = setgroups(groups.size(), groups.data());
-    RUNNER_ASSERT_ERRNO_MSG(ret == 0, "setgroups() failed");
-}
-
-/*
- * Remove specific group from the current process groups.
- */
-void remove_process_group(const char* group_name)
-{
-    // get group ID by group name
-    group *gr = getgrnam(group_name);
-    RUNNER_ASSERT_ERRNO_MSG(gr != nullptr, "getgrnam failed on '" << group_name << "' group");
-    const gid_t new_group_id = gr->gr_gid;
-
-    int ngroups = getgroups(0, nullptr);
-    std::vector<gid_t> groups(ngroups);
-    getgroups(ngroups, groups.data());
-
-    // remove group from the list
-    groups.erase(std::remove(groups.begin(), groups.end(), new_group_id), groups.end());
-
-    if (groups.size() != (size_t)ngroups) {
-        // apply change
-        int ret = setgroups(groups.size(), groups.data());
-        RUNNER_ASSERT_ERRNO_MSG(ret == 0, "setgroups() failed");
-    }
-}
-
 std::string formatCstr(const char *cstr)
 {
     if (!cstr)
@@ -183,3 +111,51 @@ int files_compare(int fd1, int fd2)
 
     return result;
 }
+
+static 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 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);
+    }
+}
+