2 * Copyright (c) 2013-2015 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 bool smack_check(void)
40 #ifndef WRT_SMACK_ENABLED
43 static int smack_present = -1;
44 if (-1 == smack_present)
45 smack_present = smack_smackfs_path() == nullptr ? 0 : 1;
46 return smack_present == 1;
51 * Dropping root privileges
52 * returns 0 on success, 1 on error
54 int drop_root_privileges(uid_t appUid, gid_t appGid)
57 /* process is running as root, drop privileges */
58 if (setgid(appGid) != 0)
60 if (setuid(appUid) != 0)
70 void setLabelForSelf(const int line, const char *label)
72 int ret = smack_set_label_for_self(label);
73 RUNNER_ASSERT_MSG(ret == 0, "Error in smack_set_label_for_self(): " << ret << ", line: " << line);
77 * Add a new group to the current process groups.
79 void add_process_group(const char* group_name)
81 // get group ID by group name
82 group *gr = getgrnam(group_name);
83 RUNNER_ASSERT_ERRNO_MSG(gr != nullptr, "getgrnam failed on '" << group_name << "' group");
84 const gid_t new_group_id = gr->gr_gid;
86 // get number of groups that the current process belongs to
87 int ngroups = getgroups(0, nullptr);
89 //allocate groups table + space for new group entry
90 std::vector<gid_t> groups(ngroups + 1);
91 getgroups(ngroups, groups.data());
93 // check if the process already belongs to the group
94 if (std::find(groups.begin(), groups.end(), new_group_id) != groups.end()) return;
96 // add new group & apply change
97 groups[ngroups] = new_group_id;
98 int ret = setgroups(groups.size(), groups.data());
99 RUNNER_ASSERT_ERRNO_MSG(ret == 0, "setgroups() failed");
103 * Remove specific group from the current process groups.
105 void remove_process_group(const char* group_name)
107 // get group ID by group name
108 group *gr = getgrnam(group_name);
109 RUNNER_ASSERT_ERRNO_MSG(gr != nullptr, "getgrnam failed on '" << group_name << "' group");
110 const gid_t new_group_id = gr->gr_gid;
112 int ngroups = getgroups(0, nullptr);
113 std::vector<gid_t> groups(ngroups);
114 getgroups(ngroups, groups.data());
116 // remove group from the list
117 groups.erase(std::remove(groups.begin(), groups.end(), new_group_id), groups.end());
119 if (groups.size() != (size_t)ngroups) {
121 int ret = setgroups(groups.size(), groups.data());
122 RUNNER_ASSERT_ERRNO_MSG(ret == 0, "setgroups() failed");
126 std::string formatCstr(const char *cstr)
129 return std::string("nullptr");
130 return std::string("\"") + cstr + "\"";
133 int files_compare(int fd1, int fd2)
135 //for getting files sizes
136 struct stat fs1, fs2;
138 //handlers for mmap()
139 void *h1 = MAP_FAILED;
140 void *h2 = MAP_FAILED;
142 //getting files information
143 RUNNER_ASSERT_ERRNO_MSG(fstat(fd1, &fs1) == 0, "fstat failed");
144 RUNNER_ASSERT_ERRNO_MSG(fstat(fd2, &fs2) == 0, "fstat failed");
146 if (fs1.st_size < fs2.st_size) {
150 if (fs1.st_size > fs2.st_size) {
154 //since Linux 2.6.12, mmap returns EINVAL if length is 0
155 //if both lengths are 0, files are actually the same
156 if (0 == fs1.st_size && 0 == fs2.st_size) {
160 //mapping files to process memory
161 RUNNER_ASSERT_ERRNO_MSG((h1 = mmap(0, fs1.st_size, PROT_READ, MAP_SHARED, fd1, 0 )) != MAP_FAILED,
162 "mmap failed for fd=" << fd1);
164 if ((h2 = mmap(0, fs2.st_size, PROT_READ, MAP_SHARED, fd2, 0 )) == MAP_FAILED) {
165 munmap(h1, fs1.st_size);
166 RUNNER_ASSERT_MSG(h2 != MAP_FAILED, "mmap failed for fd=" << fd2
167 << ". " << strerror(errno));
170 int result = memcmp(h1, h2, fs1.st_size);
171 munmap(h1, fs1.st_size);
172 munmap(h2, fs2.st_size);