Spring cleaning
[platform/core/test/security-tests.git] / src / common / tests_common.cpp
index 235b062..b4d0d33 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013 - 2019 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 "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>
 
 bool smack_check(void)
 {
@@ -65,56 +61,6 @@ int drop_root_privileges(uid_t appUid, gid_t appGid)
     return 1;
 }
 
-/*
- * 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)
@@ -166,7 +112,7 @@ int files_compare(int fd1, int fd2)
     return result;
 }
 
-void mkdirSafe(const std::string &path, mode_t mode)
+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");
@@ -182,50 +128,6 @@ void mktreeSafe(const std::string &path, mode_t 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;