2 * Copyright (c) 2013-2020 Samsung Electronics Co., Ltd. All rights reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * @file tests_common.cpp
19 * @author Lukasz Kostyra (l.kostyra@partner.samsung.com)
21 * @brief Common functions and macros used in security-tests package.
24 #include "tests_common.h"
27 #include <sys/types.h>
32 bool smack_check(void)
34 #ifndef WRT_SMACK_ENABLED
37 static int smack_present = -1;
38 if (-1 == smack_present)
39 smack_present = smack_smackfs_path() == nullptr ? 0 : 1;
40 return smack_present == 1;
45 * Dropping root privileges
46 * returns 0 on success, 1 on error
48 int drop_root_privileges(uid_t appUid, gid_t appGid)
51 /* process is running as root, drop privileges */
52 if (setgid(appGid) != 0)
54 if (setuid(appUid) != 0)
64 std::string formatCstr(const char *cstr)
67 return std::string("nullptr");
68 return std::string("\"") + cstr + "\"";
71 int files_compare(int fd1, int fd2)
73 //for getting files sizes
77 void *h1 = MAP_FAILED;
78 void *h2 = MAP_FAILED;
80 //getting files information
81 RUNNER_ASSERT_ERRNO_MSG(fstat(fd1, &fs1) == 0, "fstat failed");
82 RUNNER_ASSERT_ERRNO_MSG(fstat(fd2, &fs2) == 0, "fstat failed");
84 if (fs1.st_size < fs2.st_size) {
88 if (fs1.st_size > fs2.st_size) {
92 //since Linux 2.6.12, mmap returns EINVAL if length is 0
93 //if both lengths are 0, files are actually the same
94 if (0 == fs1.st_size && 0 == fs2.st_size) {
98 //mapping files to process memory
99 RUNNER_ASSERT_ERRNO_MSG((h1 = mmap(0, fs1.st_size, PROT_READ, MAP_SHARED, fd1, 0 )) != MAP_FAILED,
100 "mmap failed for fd=" << fd1);
102 if ((h2 = mmap(0, fs2.st_size, PROT_READ, MAP_SHARED, fd2, 0 )) == MAP_FAILED) {
103 munmap(h1, fs1.st_size);
104 RUNNER_ASSERT_MSG(h2 != MAP_FAILED, "mmap failed for fd=" << fd2
105 << ". " << strerror(errno));
108 int result = memcmp(h1, h2, fs1.st_size);
109 munmap(h1, fs1.st_size);
110 munmap(h2, fs2.st_size);
115 static void mkdirSafe(const std::string &path, mode_t mode)
117 RUNNER_ASSERT_ERRNO_MSG(0 == mkdir(path.c_str(), mode) || errno == EEXIST,
118 "mkdir for <" << path << "> with mode <" << mode << "> failed");
121 void mktreeSafe(const std::string &path, mode_t mode)
123 // Create subsequent parent directories
124 // Assume that path is absolute - i.e. starts with '/'
125 for (size_t pos = 0; (pos = path.find("/", pos + 1)) != std::string::npos; )
126 mkdirSafe(path.substr(0, pos).c_str(), mode);
128 mkdirSafe(path, mode);
131 void waitPid(pid_t pid)
134 pid_t ret = waitpid(pid, &status, 0);
135 RUNNER_ASSERT_MSG((ret != -1) && WIFEXITED(status) && WEXITSTATUS(status) == 0,
136 "Child process exited abnormally" <<
137 ": ret=" << ret << ", errno=" << errno << ", status=" << status);
140 pid_t runInChild(const std::function<void(void)> &process) {
142 RUNNER_ASSERT_ERRNO_MSG(pid >= 0, "fork failed");
151 void runInChildParentWait(const std::function<void(void)> &process) {
153 RUNNER_ASSERT_ERRNO_MSG(pid >= 0, "fork failed");