2 * Copyright (c) 2013 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>
34 int DB::Transaction::db_result = PC_OPERATION_SUCCESS;
36 const char *WGT_APP_ID = "QwCqJ0ttyS";
38 int smack_runtime_check(void)
40 static int smack_present = -1;
41 if (-1 == smack_present) {
42 if (smack_smackfs_path()) {
53 #ifndef WRT_SMACK_ENABLED
56 return smack_runtime_check();
61 * Dropping root privileges
62 * returns 0 on success, 1 on error
64 int drop_root_privileges(uid_t appUid, gid_t appGid)
67 /* process is running as root, drop privileges */
68 if (setgid(appGid) != 0)
70 if (setuid(appUid) != 0)
80 void setLabelForSelf(const int line, const char *label)
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);
87 * Add a new group to the current process groups.
89 void add_process_group(const char* group_name)
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;
96 // get number of groups that the current process belongs to
97 int ngroups = getgroups(0, nullptr);
99 //allocate groups table + space for new group entry
100 std::vector<gid_t> groups(ngroups + 1);
101 getgroups(ngroups, groups.data());
103 // check if the process already belongs to the group
104 if (std::find(groups.begin(), groups.end(), new_group_id) != groups.end()) return;
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");
113 * Remove specific group from the current process groups.
115 void remove_process_group(const char* group_name)
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;
122 int ngroups = getgroups(0, nullptr);
123 std::vector<gid_t> groups(ngroups);
124 getgroups(ngroups, groups.data());
126 // remove group from the list
127 groups.erase(std::remove(groups.begin(), groups.end(), new_group_id), groups.end());
129 if (groups.size() != (size_t)ngroups) {
131 int ret = setgroups(groups.size(), groups.data());
132 RUNNER_ASSERT_ERRNO_MSG(ret == 0, "setgroups() failed");
136 std::string formatCstr(const char *cstr)
139 return std::string("nullptr");
140 return std::string("\"") + cstr + "\"";
143 int files_compare(int fd1, int fd2)
145 //for getting files sizes
146 struct stat fs1, fs2;
148 //handlers for mmap()
149 void *h1 = MAP_FAILED;
150 void *h2 = MAP_FAILED;
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");
156 if (fs1.st_size < fs2.st_size) {
160 if (fs1.st_size > fs2.st_size) {
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) {
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);
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));
180 int result = memcmp(h1, h2, fs1.st_size);
181 munmap(h1, fs1.st_size);
182 munmap(h2, fs2.st_size);