Rename /tests to /ckm to align with tizen branch
[platform/core/test/security-tests.git] / src / 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 <sys/mman.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29 #include <grp.h>
30 #include <errno.h>
31 #include <vector>
32 #include <algorithm>
33
34 int DB::Transaction::db_result = PC_OPERATION_SUCCESS;
35
36 const char *WGT_APP_ID = "QwCqJ0ttyS";
37
38 int smack_runtime_check(void)
39 {
40     static int smack_present = -1;
41     if (-1 == smack_present) {
42         if (smack_smackfs_path()) {
43             smack_present = 1;
44         } else {
45             smack_present = 0;
46         }
47     }
48     return smack_present;
49 }
50
51 int smack_check(void)
52 {
53 #ifndef WRT_SMACK_ENABLED
54     return 0;
55 #else
56     return smack_runtime_check();
57 #endif
58 }
59
60 /**
61  * Dropping root privileges
62  * returns 0 on success, 1 on error
63  */
64 int drop_root_privileges(uid_t appUid, gid_t appGid)
65 {
66     if (getuid() == 0) {
67         /* process is running as root, drop privileges */
68         if (setgid(appGid) != 0)
69             return 1;
70         if (setuid(appUid) != 0)
71             return 1;
72     }
73     uid_t uid = getuid();
74     if (uid == appUid)
75         return 0;
76
77     return 1;
78 }
79
80 void setLabelForSelf(const int line, const char *label)
81 {
82     int ret = smack_set_label_for_self(label);
83     RUNNER_ASSERT_MSG(ret == 0, "Error in smack_set_label_for_self(): " << ret << ", line: " << line);
84 }
85
86 /*
87  * Add a new group to the current process groups.
88  */
89 void add_process_group(const char* group_name)
90 {
91     // get group ID by group name
92     group *gr = getgrnam(group_name);
93     RUNNER_ASSERT_ERRNO_MSG(gr != nullptr, "getgrnam failed on '" << group_name << "' group");
94     const gid_t new_group_id = gr->gr_gid;
95
96     // get number of groups that the current process belongs to
97     int ngroups = getgroups(0, nullptr);
98
99     //allocate groups table + space for new group entry
100     std::vector<gid_t> groups(ngroups + 1);
101     getgroups(ngroups, groups.data());
102
103     // check if the process already belongs to the group
104     if (std::find(groups.begin(), groups.end(), new_group_id) != groups.end()) return;
105
106     // add new group & apply change
107     groups[ngroups] = new_group_id;
108     int ret = setgroups(groups.size(), groups.data());
109     RUNNER_ASSERT_ERRNO_MSG(ret == 0, "setgroups() failed");
110 }
111
112 /*
113  * Remove specific group from the current process groups.
114  */
115 void remove_process_group(const char* group_name)
116 {
117     // get group ID by group name
118     group *gr = getgrnam(group_name);
119     RUNNER_ASSERT_ERRNO_MSG(gr != nullptr, "getgrnam failed on '" << group_name << "' group");
120     const gid_t new_group_id = gr->gr_gid;
121
122     int ngroups = getgroups(0, nullptr);
123     std::vector<gid_t> groups(ngroups);
124     getgroups(ngroups, groups.data());
125
126     // remove group from the list
127     groups.erase(std::remove(groups.begin(), groups.end(), new_group_id), groups.end());
128
129     if (groups.size() != (size_t)ngroups) {
130         // apply change
131         int ret = setgroups(groups.size(), groups.data());
132         RUNNER_ASSERT_ERRNO_MSG(ret == 0, "setgroups() failed");
133     }
134 }
135
136 std::string formatCstr(const char *cstr)
137 {
138     if (!cstr)
139         return std::string("nullptr");
140     return std::string("\"") + cstr + "\"";
141 }
142
143 int files_compare(int fd1, int fd2)
144 {
145     //for getting files sizes
146     struct stat fs1, fs2;
147
148     //handlers for mmap()
149     void *h1 = MAP_FAILED;
150     void *h2 = MAP_FAILED;
151
152     //getting files information
153     RUNNER_ASSERT_ERRNO_MSG(fstat(fd1, &fs1) == 0, "fstat failed");
154     RUNNER_ASSERT_ERRNO_MSG(fstat(fd2, &fs2) == 0, "fstat failed");
155
156     if (fs1.st_size < fs2.st_size) {
157         return -1;
158     }
159
160     if (fs1.st_size > fs2.st_size) {
161         return 1;
162     }
163
164     //since Linux 2.6.12, mmap returns EINVAL if length is 0
165     //if both lengths are 0, files are actually the same
166     if (0 == fs1.st_size && 0 == fs2.st_size) {
167         return 0;
168     }
169
170     //mapping files to process memory
171     RUNNER_ASSERT_ERRNO_MSG((h1 = mmap(0, fs1.st_size, PROT_READ, MAP_SHARED, fd1, 0 )) != MAP_FAILED,
172                                "mmap failed for fd=" << fd1);
173
174     if ((h2 = mmap(0, fs2.st_size, PROT_READ, MAP_SHARED, fd2, 0 )) == MAP_FAILED) {
175         munmap(h1, fs1.st_size);
176         RUNNER_ASSERT_MSG(h2 != MAP_FAILED, "mmap failed for fd=" << fd2
177                                             << ". " << strerror(errno));
178     }
179
180     int result = memcmp(h1, h2, fs1.st_size);
181     munmap(h1, fs1.st_size);
182     munmap(h2, fs2.st_size);
183
184     return result;
185 }