X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=tests%2Fcommon%2Ftests_common.cpp;h=a8afc7ba908a51fcac8575a6bc2760ed84c2a3d1;hb=refs%2Fchanges%2F60%2F36060%2F3;hp=3f999461e4bf9000c654980e6df6d175fef83f90;hpb=4255d55fdb8d1731c6c90b102610e1b0771aed95;p=platform%2Fcore%2Ftest%2Fsecurity-tests.git diff --git a/tests/common/tests_common.cpp b/tests/common/tests_common.cpp index 3f99946..a8afc7b 100644 --- a/tests/common/tests_common.cpp +++ b/tests/common/tests_common.cpp @@ -22,10 +22,15 @@ */ #include "tests_common.h" +#include +#include +#include +#include #include #include #include #include +#include int DB::Transaction::db_result = PC_OPERATION_SUCCESS; @@ -53,26 +58,21 @@ int smack_check(void) #endif } -void closeFdPtr(int *fd) -{ - TEMP_FAILURE_RETRY(close(*fd)); -} - /** * Dropping root privileges * returns 0 on success, 1 on error */ -int drop_root_privileges(void) +int drop_root_privileges(uid_t appUid, gid_t appGid) { if (getuid() == 0) { /* process is running as root, drop privileges */ - if (setgid(APP_GID) != 0) + if (setgid(appGid) != 0) return 1; - if (setuid(APP_UID) != 0) + if (setuid(appUid) != 0) return 1; } uid_t uid = getuid(); - if (uid == APP_UID) + if (uid == appUid) return 0; return 1; @@ -81,7 +81,7 @@ int drop_root_privileges(void) void setLabelForSelf(const int line, const char *label) { int ret = smack_set_label_for_self(label); - RUNNER_ASSERT_MSG_BT(ret == 0, "Error in smack_set_label_for_self(): " << ret << ", line: " << line); + RUNNER_ASSERT_MSG(ret == 0, "Error in smack_set_label_for_self(): " << ret << ", line: " << line); } /* @@ -91,11 +91,11 @@ void add_process_group(const char* group_name) { // get group ID by group name group *gr = getgrnam(group_name); - RUNNER_ASSERT_MSG_BT(gr != NULL, "Group '" << group_name << "' does not exist."); + 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, NULL); + int ngroups = getgroups(0, nullptr); //allocate groups table + space for new group entry std::vector groups(ngroups + 1); @@ -107,8 +107,7 @@ void add_process_group(const char* group_name) // add new group & apply change groups[ngroups] = new_group_id; int ret = setgroups(groups.size(), groups.data()); - int error = errno; - RUNNER_ASSERT_MSG_BT(ret == 0, "setgroups() failed. " << strerror(error)); + RUNNER_ASSERT_ERRNO_MSG(ret == 0, "setgroups() failed"); } /* @@ -118,20 +117,130 @@ void remove_process_group(const char* group_name) { // get group ID by group name group *gr = getgrnam(group_name); - RUNNER_ASSERT_MSG_BT(gr != NULL, "Group '" << group_name << "' does not exist."); + 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, NULL); + int ngroups = getgroups(0, nullptr); std::vector groups(ngroups); getgroups(ngroups, groups.data()); // remove group from the list - groups.erase(std::remove(groups.begin(), groups.end(), new_group_id)); + 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()); - int error = errno; - RUNNER_ASSERT_MSG_BT(ret == 0, "setgroups() failed. " << strerror(error)); + RUNNER_ASSERT_ERRNO_MSG(ret == 0, "setgroups() failed"); } } + +std::string formatCstr(const char *cstr) +{ + if (!cstr) + return std::string("nullptr"); + return std::string("\"") + cstr + "\""; +} + +int files_compare(int fd1, int fd2) +{ + //for getting files sizes + struct stat fs1, fs2; + + //handlers for mmap() + void *h1 = MAP_FAILED; + void *h2 = MAP_FAILED; + + //getting files information + RUNNER_ASSERT_ERRNO_MSG(fstat(fd1, &fs1) == 0, "fstat failed"); + RUNNER_ASSERT_ERRNO_MSG(fstat(fd2, &fs2) == 0, "fstat failed"); + + if (fs1.st_size < fs2.st_size) { + return -1; + } + + if (fs1.st_size > fs2.st_size) { + return 1; + } + + //since Linux 2.6.12, mmap returns EINVAL if length is 0 + //if both lengths are 0, files are actually the same + if (0 == fs1.st_size && 0 == fs2.st_size) { + return 0; + } + + //mapping files to process memory + RUNNER_ASSERT_ERRNO_MSG((h1 = mmap(0, fs1.st_size, PROT_READ, MAP_SHARED, fd1, 0 )) != MAP_FAILED, + "mmap failed for fd=" << fd1); + + if ((h2 = mmap(0, fs2.st_size, PROT_READ, MAP_SHARED, fd2, 0 )) == MAP_FAILED) { + munmap(h1, fs1.st_size); + RUNNER_ASSERT_MSG(h2 != MAP_FAILED, "mmap failed for fd=" << fd2 + << ". " << strerror(errno)); + } + + int result = memcmp(h1, h2, fs1.st_size); + munmap(h1, fs1.st_size); + munmap(h2, fs2.st_size); + + 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"); +}