security-manager-tests: use full proper path for applications
[platform/core/test/security-tests.git] / tests / common / tests_common.cpp
1 /*
2  * Copyright (c) 2013 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 <fcntl.h>
26 #include <sys/mman.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <unistd.h>
30 #include <grp.h>
31 #include <errno.h>
32 #include <vector>
33 #include <algorithm>
34
35 int DB::Transaction::db_result = PC_OPERATION_SUCCESS;
36
37 const char *WGT_APP_ID = "QwCqJ0ttyS";
38
39 int smack_runtime_check(void)
40 {
41     static int smack_present = -1;
42     if (-1 == smack_present) {
43         if (smack_smackfs_path()) {
44             smack_present = 1;
45         } else {
46             smack_present = 0;
47         }
48     }
49     return smack_present;
50 }
51
52 int smack_check(void)
53 {
54 #ifndef WRT_SMACK_ENABLED
55     return 0;
56 #else
57     return smack_runtime_check();
58 #endif
59 }
60
61 /**
62  * Dropping root privileges
63  * returns 0 on success, 1 on error
64  */
65 int drop_root_privileges(uid_t appUid, gid_t appGid)
66 {
67     if (getuid() == 0) {
68         /* process is running as root, drop privileges */
69         if (setgid(appGid) != 0)
70             return 1;
71         if (setuid(appUid) != 0)
72             return 1;
73     }
74     uid_t uid = getuid();
75     if (uid == appUid)
76         return 0;
77
78     return 1;
79 }
80
81 void setLabelForSelf(const int line, const char *label)
82 {
83     int ret = smack_set_label_for_self(label);
84     RUNNER_ASSERT_MSG(ret == 0, "Error in smack_set_label_for_self(): " << ret << ", line: " << line);
85 }
86
87 /*
88  * Add a new group to the current process groups.
89  */
90 void add_process_group(const char* group_name)
91 {
92     // get group ID by group name
93     group *gr = getgrnam(group_name);
94     RUNNER_ASSERT_ERRNO_MSG(gr != nullptr, "getgrnam failed on '" << group_name << "' group");
95     const gid_t new_group_id = gr->gr_gid;
96
97     // get number of groups that the current process belongs to
98     int ngroups = getgroups(0, nullptr);
99
100     //allocate groups table + space for new group entry
101     std::vector<gid_t> groups(ngroups + 1);
102     getgroups(ngroups, groups.data());
103
104     // check if the process already belongs to the group
105     if (std::find(groups.begin(), groups.end(), new_group_id) != groups.end()) return;
106
107     // add new group & apply change
108     groups[ngroups] = new_group_id;
109     int ret = setgroups(groups.size(), groups.data());
110     RUNNER_ASSERT_ERRNO_MSG(ret == 0, "setgroups() failed");
111 }
112
113 /*
114  * Remove specific group from the current process groups.
115  */
116 void remove_process_group(const char* group_name)
117 {
118     // get group ID by group name
119     group *gr = getgrnam(group_name);
120     RUNNER_ASSERT_ERRNO_MSG(gr != nullptr, "getgrnam failed on '" << group_name << "' group");
121     const gid_t new_group_id = gr->gr_gid;
122
123     int ngroups = getgroups(0, nullptr);
124     std::vector<gid_t> groups(ngroups);
125     getgroups(ngroups, groups.data());
126
127     // remove group from the list
128     groups.erase(std::remove(groups.begin(), groups.end(), new_group_id), groups.end());
129
130     if (groups.size() != (size_t)ngroups) {
131         // apply change
132         int ret = setgroups(groups.size(), groups.data());
133         RUNNER_ASSERT_ERRNO_MSG(ret == 0, "setgroups() failed");
134     }
135 }
136
137 std::string formatCstr(const char *cstr)
138 {
139     if (!cstr)
140         return std::string("nullptr");
141     return std::string("\"") + cstr + "\"";
142 }
143
144 int files_compare(int fd1, int fd2)
145 {
146     //for getting files sizes
147     struct stat fs1, fs2;
148
149     //handlers for mmap()
150     void *h1 = MAP_FAILED;
151     void *h2 = MAP_FAILED;
152
153     //getting files information
154     RUNNER_ASSERT_ERRNO_MSG(fstat(fd1, &fs1) == 0, "fstat failed");
155     RUNNER_ASSERT_ERRNO_MSG(fstat(fd2, &fs2) == 0, "fstat failed");
156
157     if (fs1.st_size < fs2.st_size) {
158         return -1;
159     }
160
161     if (fs1.st_size > fs2.st_size) {
162         return 1;
163     }
164
165     //since Linux 2.6.12, mmap returns EINVAL if length is 0
166     //if both lengths are 0, files are actually the same
167     if (0 == fs1.st_size && 0 == fs2.st_size) {
168         return 0;
169     }
170
171     //mapping files to process memory
172     RUNNER_ASSERT_ERRNO_MSG((h1 = mmap(0, fs1.st_size, PROT_READ, MAP_SHARED, fd1, 0 )) != MAP_FAILED,
173                                "mmap failed for fd=" << fd1);
174
175     if ((h2 = mmap(0, fs2.st_size, PROT_READ, MAP_SHARED, fd2, 0 )) == MAP_FAILED) {
176         munmap(h1, fs1.st_size);
177         RUNNER_ASSERT_MSG(h2 != MAP_FAILED, "mmap failed for fd=" << fd2
178                                             << ". " << strerror(errno));
179     }
180
181     int result = memcmp(h1, h2, fs1.st_size);
182     munmap(h1, fs1.st_size);
183     munmap(h2, fs2.st_size);
184
185     return result;
186 }
187
188 void mkdirSafe(const std::string &path, mode_t mode)
189 {
190     RUNNER_ASSERT_ERRNO_MSG(0 == mkdir(path.c_str(), mode) || errno == EEXIST,
191                             "mkdir for <" << path << "> with mode <" << mode << "> failed");
192 }
193
194 void mktreeSafe(const std::string &path, mode_t mode)
195 {
196     // Create subsequent parent directories
197     // Assume that path is absolute - i.e. starts with '/'
198     for (size_t pos = 0; (pos = path.find("/", pos + 1)) != std::string::npos; )
199         mkdirSafe(path.substr(0, pos).c_str(), mode);
200
201     mkdirSafe(path, mode);
202 }
203
204 void creatSafe(const std::string &path, mode_t mode)
205 {
206     RUNNER_ASSERT_ERRNO_MSG(-1 != creat(path.c_str(), mode),
207                             "creat for <" << path << "> with mode <" << mode << "> failed");
208 }
209
210 void symlinkSafe(const std::string &targetPath, const std::string &linkPath)
211 {
212     RUNNER_ASSERT_ERRNO_MSG(0 == symlink(targetPath.c_str(), linkPath.c_str()),
213                             "symlink for <" << linkPath << "> to <" << targetPath << "> failed");
214 }
215
216 void removeDir(const std::string &path)
217 {
218     DIR *d = opendir(path.c_str());
219
220     if (nullptr == d) {
221         RUNNER_ASSERT_ERRNO_MSG(errno == ENOENT, "opendir of <" << path << "> failed");
222         return;
223     }
224
225     struct dirent *dirEntry;
226     while (nullptr != (dirEntry = readdir(d))) {
227         std::string entryName(dirEntry->d_name);
228         if (entryName == "." || entryName == "..")
229             continue;
230
231         std::string entryPath(path + "/" + entryName);
232         struct stat st;
233
234         RUNNER_ASSERT_ERRNO_MSG(0 == lstat(entryPath.c_str(), &st),
235                                 "stat for <" << entryPath << "> failed");
236         if (S_ISDIR(st.st_mode))
237             removeDir(entryPath);
238         else
239             RUNNER_ASSERT_ERRNO_MSG(0 == unlink(entryPath.c_str()),
240                                     "unlink for <" << entryPath << "> failed");
241     }
242
243     closedir(d);
244
245     RUNNER_ASSERT_ERRNO_MSG(0 == rmdir(path.c_str()), "rmdir for <" << path << "> failed");
246 }