Move test framework from wrt-commons to security-tests
[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 <unistd.h>
26 #include <grp.h>
27 #include <errno.h>
28 #include <vector>
29 #include <algorithm>
30
31 int DB::Transaction::db_result = PC_OPERATION_SUCCESS;
32
33 const char *WGT_APP_ID = "QwCqJ0ttyS";
34
35 int smack_runtime_check(void)
36 {
37     static int smack_present = -1;
38     if (-1 == smack_present) {
39         if (smack_smackfs_path()) {
40             smack_present = 1;
41         } else {
42             smack_present = 0;
43         }
44     }
45     return smack_present;
46 }
47
48 int smack_check(void)
49 {
50 #ifndef WRT_SMACK_ENABLED
51     return 0;
52 #else
53     return smack_runtime_check();
54 #endif
55 }
56
57 /**
58  * Dropping root privileges
59  * returns 0 on success, 1 on error
60  */
61 int drop_root_privileges(void)
62 {
63     if (getuid() == 0) {
64         /* process is running as root, drop privileges */
65         if (setgid(APP_GID) != 0)
66             return 1;
67         if (setuid(APP_UID) != 0)
68             return 1;
69     }
70     uid_t uid = getuid();
71     if (uid == APP_UID)
72         return 0;
73
74     return 1;
75 }
76
77 void setLabelForSelf(const int line, const char *label)
78 {
79     int ret = smack_set_label_for_self(label);
80     RUNNER_ASSERT_MSG_BT(ret == 0, "Error in smack_set_label_for_self(): " << ret << ", line: " << line);
81 }
82
83 /*
84  * Add a new group to the current process groups.
85  */
86 void add_process_group(const char* group_name)
87 {
88     // get group ID by group name
89     group *gr = getgrnam(group_name);
90     RUNNER_ASSERT_MSG_BT(gr != NULL, "Group '" << group_name << "' does not exist.");
91     const gid_t new_group_id = gr->gr_gid;
92
93     // get number of groups that the current process belongs to
94     int ngroups = getgroups(0, NULL);
95
96     //allocate groups table + space for new group entry
97     std::vector<gid_t> groups(ngroups + 1);
98     getgroups(ngroups, groups.data());
99
100     // check if the process already belongs to the group
101     if (std::find(groups.begin(), groups.end(), new_group_id) != groups.end()) return;
102
103     // add new group & apply change
104     groups[ngroups] = new_group_id;
105     int ret = setgroups(groups.size(), groups.data());
106     int error = errno;
107     RUNNER_ASSERT_MSG_BT(ret == 0, "setgroups() failed. " << strerror(error));
108 }
109
110 /*
111  * Remove specific group from the current process groups.
112  */
113 void remove_process_group(const char* group_name)
114 {
115     // get group ID by group name
116     group *gr = getgrnam(group_name);
117     RUNNER_ASSERT_MSG_BT(gr != NULL, "Group '" << group_name << "' does not exist.");
118     const gid_t new_group_id = gr->gr_gid;
119
120     int ngroups = getgroups(0, NULL);
121     std::vector<gid_t> groups(ngroups);
122     getgroups(ngroups, groups.data());
123
124     // remove group from the list
125     groups.erase(std::remove(groups.begin(), groups.end(), new_group_id), groups.end());
126
127     if (groups.size() != (size_t)ngroups) {
128         // apply change
129         int ret = setgroups(groups.size(), groups.data());
130         int error = errno;
131         RUNNER_ASSERT_MSG_BT(ret == 0, "setgroups() failed. " << strerror(error));
132     }
133 }
134
135 std::string formatCstr(const char *cstr)
136 {
137     if (!cstr)
138         return std::string("nullptr");
139     return std::string("\"") + cstr + "\"";
140 }