X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=src%2Fcommon%2Ftests_common.cpp;h=235b062cb254180e499d205d7fb62a29b945f8c6;hb=refs%2Fchanges%2F08%2F200708%2F5;hp=470235dfd848c3a6e00c6f658ace8db3dac13e85;hpb=aa310f173ea26eda1741109e67eea3ad7378c0de;p=platform%2Fcore%2Ftest%2Fsecurity-tests.git diff --git a/src/common/tests_common.cpp b/src/common/tests_common.cpp index 470235d..235b062 100644 --- a/src/common/tests_common.cpp +++ b/src/common/tests_common.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013-2015 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. @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -64,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. */ @@ -230,3 +225,35 @@ void removeDir(const std::string &path) 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 &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 &process) { + pid_t pid = fork(); + RUNNER_ASSERT_ERRNO_MSG(pid >= 0, "fork failed"); + if (pid == 0) { + process(); + exit(EXIT_SUCCESS); + } else { + waitPid(pid); + } +} +