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"
30 int DB::Transaction::db_result = PC_OPERATION_SUCCESS;
32 const char *WGT_APP_ID = "QwCqJ0ttyS";
34 int smack_runtime_check(void)
36 static int smack_present = -1;
37 if (-1 == smack_present) {
38 if (smack_smackfs_path()) {
49 #ifndef WRT_SMACK_ENABLED
52 return smack_runtime_check();
56 void closeFdPtr(int *fd)
58 TEMP_FAILURE_RETRY(close(*fd));
62 * Dropping root privileges
63 * returns 0 on success, 1 on error
65 int drop_root_privileges(void)
68 /* process is running as root, drop privileges */
69 if (setgid(APP_GID) != 0)
71 if (setuid(APP_UID) != 0)
81 void setLabelForSelf(const int line, const char *label)
83 int ret = smack_set_label_for_self(label);
84 RUNNER_ASSERT_MSG_BT(ret == 0, "Error in smack_set_label_for_self(): " << ret << ", line: " << line);
88 * Add a new group to the current process groups.
90 void add_process_group(const char* group_name)
92 // get group ID by group name
93 group *gr = getgrnam(group_name);
94 RUNNER_ASSERT_MSG_BT(gr != NULL, "Group '" << group_name << "' does not exist.");
95 const gid_t new_group_id = gr->gr_gid;
97 // get number of groups that the current process belongs to
98 int ngroups = getgroups(0, NULL);
100 //allocate groups table + space for new group entry
101 std::vector<gid_t> groups(ngroups + 1);
102 getgroups(ngroups, groups.data());
104 // check if the process already belongs to the group
105 if (std::find(groups.begin(), groups.end(), new_group_id) != groups.end()) return;
107 // add new group & apply change
108 groups[ngroups] = new_group_id;
109 int ret = setgroups(groups.size(), groups.data());
111 RUNNER_ASSERT_MSG_BT(ret == 0, "setgroups() failed. " << strerror(error));
115 * Remove specific group from the current process groups.
117 void remove_process_group(const char* group_name)
119 // get group ID by group name
120 group *gr = getgrnam(group_name);
121 RUNNER_ASSERT_MSG_BT(gr != NULL, "Group '" << group_name << "' does not exist.");
122 const gid_t new_group_id = gr->gr_gid;
124 int ngroups = getgroups(0, NULL);
125 std::vector<gid_t> groups(ngroups);
126 getgroups(ngroups, groups.data());
128 // remove group from the list
129 groups.erase(std::remove(groups.begin(), groups.end(), new_group_id), groups.end());
131 if (groups.size() != (size_t)ngroups) {
133 int ret = setgroups(groups.size(), groups.data());
135 RUNNER_ASSERT_MSG_BT(ret == 0, "setgroups() failed. " << strerror(error));