Spring cleaning
[platform/core/test/security-tests.git] / src / common / tests_common.cpp
1 /*
2  * Copyright (c) 2013-2020 Samsung Electronics Co., Ltd. All rights reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /*
18  * @file        tests_common.cpp
19  * @author      Lukasz Kostyra (l.kostyra@partner.samsung.com)
20  * @version     1.0
21  * @brief       Common functions and macros used in security-tests package.
22  */
23
24 #include "tests_common.h"
25 #include <sys/mman.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <sys/wait.h>
29 #include <unistd.h>
30 #include <errno.h>
31
32 bool smack_check(void)
33 {
34 #ifndef WRT_SMACK_ENABLED
35     return false;
36 #else
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;
41 #endif
42 }
43
44 /**
45  * Dropping root privileges
46  * returns 0 on success, 1 on error
47  */
48 int drop_root_privileges(uid_t appUid, gid_t appGid)
49 {
50     if (getuid() == 0) {
51         /* process is running as root, drop privileges */
52         if (setgid(appGid) != 0)
53             return 1;
54         if (setuid(appUid) != 0)
55             return 1;
56     }
57     uid_t uid = getuid();
58     if (uid == appUid)
59         return 0;
60
61     return 1;
62 }
63
64 std::string formatCstr(const char *cstr)
65 {
66     if (!cstr)
67         return std::string("nullptr");
68     return std::string("\"") + cstr + "\"";
69 }
70
71 int files_compare(int fd1, int fd2)
72 {
73     //for getting files sizes
74     struct stat fs1, fs2;
75
76     //handlers for mmap()
77     void *h1 = MAP_FAILED;
78     void *h2 = MAP_FAILED;
79
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");
83
84     if (fs1.st_size < fs2.st_size) {
85         return -1;
86     }
87
88     if (fs1.st_size > fs2.st_size) {
89         return 1;
90     }
91
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) {
95         return 0;
96     }
97
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);
101
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));
106     }
107
108     int result = memcmp(h1, h2, fs1.st_size);
109     munmap(h1, fs1.st_size);
110     munmap(h2, fs2.st_size);
111
112     return result;
113 }
114
115 static void mkdirSafe(const std::string &path, mode_t mode)
116 {
117     RUNNER_ASSERT_ERRNO_MSG(0 == mkdir(path.c_str(), mode) || errno == EEXIST,
118                             "mkdir for <" << path << "> with mode <" << mode << "> failed");
119 }
120
121 void mktreeSafe(const std::string &path, mode_t mode)
122 {
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);
127
128     mkdirSafe(path, mode);
129 }
130
131 void waitPid(pid_t pid)
132 {
133     int status;
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);
138 }
139
140 pid_t runInChild(const std::function<void(void)> &process) {
141     pid_t pid = fork();
142     RUNNER_ASSERT_ERRNO_MSG(pid >= 0, "fork failed");
143
144     if (pid == 0) {
145         process();
146         exit(EXIT_SUCCESS);
147     }
148     return pid;
149 }
150
151 void runInChildParentWait(const std::function<void(void)> &process) {
152     pid_t pid = fork();
153     RUNNER_ASSERT_ERRNO_MSG(pid >= 0, "fork failed");
154     if (pid == 0) {
155         process();
156         exit(EXIT_SUCCESS);
157     } else {
158         waitPid(pid);
159     }
160 }
161