2 * Copyright (c) 2014-2020 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 #include <sys/types.h>
22 #include <dpl/test/test_runner.h>
24 namespace SecurityManagerTest {
28 void install(const InstallRequest &request, lib_retcode expectedResult)
30 int result = security_manager_app_install(request.get());
32 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
33 "installing app returned wrong value."
34 << " InstallRequest: [ " << request << "];"
35 << " Result: " << result << ";"
36 << " Expected result: " << expectedResult);
39 void update(const InstallRequest &request, lib_retcode expectedResult)
41 int result = security_manager_app_update(request.get());
42 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
43 "installing app returned wrong value."
44 << " InstallRequest: [ " << request << "];"
45 << " Result: " << result << ";"
46 << " Expected result: " << expectedResult);
49 void uninstall(const InstallRequest &request, lib_retcode expectedResult)
51 int result = security_manager_app_uninstall(request.get());
52 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
53 "uninstalling app returned wrong value."
54 << " InstallRequest: [ " << request << "];"
55 << " Result: " << result << ";"
56 << " Expected result: " << expectedResult);
59 std::string getPkgId(const std::string &appId, lib_retcode expectedResult)
61 char *pkgId = nullptr;
62 int result = security_manager_get_app_pkgid(&pkgId, appId.c_str());
63 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
64 "getting pkg id from app id returned wrong value."
65 << " App id: " << appId << ";"
66 << " Result: " << result << ";"
67 << " Expected result: " << expectedResult);
68 if (expectedResult != SECURITY_MANAGER_SUCCESS)
71 RUNNER_ASSERT_MSG(pkgId != nullptr, "getting pkg id did not allocate memory");
72 std::string str(pkgId);
77 void setProcessLabel(const std::string &appId, lib_retcode expectedResult)
79 int result = security_manager_set_process_label_from_appid(appId.c_str());
80 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
81 "setting process label from app id returned wrong value."
82 << " App id: " << appId << ";"
83 << " Result: " << result << ";"
84 << " Expected result: " << expectedResult);
87 void setProcessGroups(const std::string &appId, lib_retcode expectedResult)
89 int result = security_manager_set_process_groups_from_appid(appId.c_str());
90 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
91 "setting process groups from app id returned wrong value."
92 << " App id: " << appId << ";"
93 << " Result: " << result << ";"
94 << " Expected result: " << expectedResult);
97 void dropProcessPrivileges(lib_retcode expectedResult)
99 int result = security_manager_drop_process_privileges();
100 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
101 "dropping process privileges returned wrong value."
102 << " Result: " << result << ";"
103 << " Expected result: " << expectedResult);
106 void prepareAppCandidate(lib_retcode expectedResult)
108 int result = security_manager_prepare_app_candidate();
109 RUNNER_ASSERT_MSG((lib_retcode) result == expectedResult,
110 "preparing app candidate process returned wrong value."
111 << " Result: " << result << ";"
112 << " Expected result: " << expectedResult);
115 void prepareApp(const std::string &appId, lib_retcode expectedResult)
117 int result = security_manager_prepare_app(appId.c_str());
118 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
119 "preparing app returned wrong value."
120 << " App id: " << appId << ";"
121 << " Result: " << result << ";"
122 << " Expected result: " << expectedResult);
125 void cleanupApp(const std::string &appId, uid_t uid, pid_t pid, lib_retcode expectedResult)
127 int result = security_manager_cleanup_app(appId.c_str(), uid, pid);
128 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
129 "cleanup app returned wrong value."
130 << " App id: " << appId << ";"
131 << " user id: " << uid << ";"
132 << " process id: " << pid << ";"
133 << " Result: " << result << ";"
134 << " Expected result: " << expectedResult);
137 void addUser(const UserRequest &request, lib_retcode expectedResult)
139 int result = security_manager_user_add(request.get());
140 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
141 "adding user returned wrong value."
142 << " UserRequest: [ " << request << "];"
143 << " Result: " << result << ";"
144 << " Expected result: " << expectedResult);
147 void deleteUser(const UserRequest &request, lib_retcode expectedResult)
149 int result = security_manager_user_delete(request.get());
150 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
151 "deleting user returned wrong value."
152 << " UserRequest: [ " << request << "];"
153 << " Result: " << result << ";"
154 << " Expected result: " << expectedResult);
157 void sendPolicy(const PolicyRequest &request, lib_retcode expectedResult)
159 int result = security_manager_policy_update_send(request.get());
160 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
161 "sending policy update for self returned wrong value."
162 << " PolicyRequest: [ " << request << "];"
163 << " Result: " << result << ";"
164 << " Expected result: " << expectedResult);
167 void getConfiguredPolicy(const PolicyEntry &filter, std::vector<PolicyEntry> &policyEntries, lib_retcode expectedResult, bool forAdmin)
169 policy_entry **pp_privs_policy = NULL;
170 size_t policy_size = 0;
174 result = security_manager_get_configured_policy_for_admin(filter.get(), &pp_privs_policy, &policy_size);
176 result = security_manager_get_configured_policy_for_self(filter.get(), &pp_privs_policy, &policy_size);
179 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
180 "Unexpected result for filter: " << filter << std::endl
181 << " Result: " << result << ";");
183 for (unsigned int i = 0; i < policy_size; ++i) {
184 PolicyEntry pe(*pp_privs_policy[i]);
185 policyEntries.push_back(pe);
189 void getPolicy(const PolicyEntry &filter, std::vector<PolicyEntry> &policyEntries, lib_retcode expectedResult)
191 policy_entry **pp_privs_policy = NULL;
192 size_t policy_size = 0;
195 result = security_manager_get_policy(filter.get(), &pp_privs_policy, &policy_size);
196 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
197 "Unexpected result" << std::endl
198 << " Result: " << result << ";");
199 for (unsigned int i = 0; i < policy_size; ++i) {
200 PolicyEntry pe(*pp_privs_policy[i]);
201 policyEntries.push_back(pe);
205 void applySharing(const SharingRequest &req, lib_retcode expectedResult) {
206 int result = security_manager_private_sharing_apply(req.get());
207 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
208 "Unexpected result from security_manager_private_sharing_apply" << std::endl
209 << " Result: " << result << ";");
212 void dropSharing(const SharingRequest &req, lib_retcode expectedResult) {
213 int result = security_manager_private_sharing_drop(req.get());
214 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
215 "Unexpected result from security_manager_private_sharing_drop" << std::endl
216 << " Result: " << result << ";");
219 void getPolicyForSelf(const PolicyEntry &filter, std::vector<PolicyEntry> &policyEntries, lib_retcode expectedResult)
221 getConfiguredPolicy(filter, policyEntries, expectedResult, false);
224 void getPolicyForAdmin(const PolicyEntry &filter, std::vector<PolicyEntry> &policyEntries, lib_retcode expectedResult)
226 getConfiguredPolicy(filter, policyEntries, expectedResult, true);
229 void getPkgIdBySocket(int socketFd, std::string *pkgId, std::string *appId, lib_retcode expectedResult)
231 char *pkg_Id = nullptr;
232 char *app_Id = nullptr;
234 int result = security_manager_identify_app_from_socket(socketFd,
235 pkgId == nullptr ? nullptr : &pkg_Id,
236 appId == nullptr ? nullptr : &app_Id);
238 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
239 "getting pkg id from socket returned wrong value."
240 << " socket: " << socketFd << ";"
241 << " Result: " << result << ";"
242 << " Expected result: " << expectedResult);
255 void getPkgIdByPid(pid_t pid, std::string *pkgId, std::string *appId, lib_retcode expectedResult)
257 char *pkg_Id = nullptr;
258 char *app_Id = nullptr;
260 int result = security_manager_identify_app_from_pid(pid,
261 pkgId == nullptr ? nullptr : &pkg_Id,
262 appId == nullptr ? nullptr : &app_Id);
264 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
265 "getting pkg id from pid returned wrong value."
266 << " pid: " << pid << ";"
267 << " Result: " << result << ";"
268 << " Expected result: " << expectedResult);
281 void getPkgIdByCynaraClient(const std::string &client, std::string *pkgId, std::string *appId,
282 lib_retcode expectedResult)
284 char *pkg_Id = nullptr;
285 char *app_Id = nullptr;
287 int result = security_manager_identify_app_from_cynara_client(client.c_str(),
288 pkgId == nullptr ? nullptr : &pkg_Id,
289 appId == nullptr ? nullptr : &app_Id);
291 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
292 "getting pkg id from pid returned wrong value."
293 << " client: " << client << ";"
294 << " Result: " << result << ";"
295 << " Expected result: " << expectedResult);
308 void appHasPrivilege(const std::string &appId, const std::string &privilege, uid_t user,
309 int &value, lib_retcode expectedResult)
311 int result = security_manager_app_has_privilege(appId.c_str(), privilege.c_str(), user, &value);
313 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
314 "checking application privilege returned wrong result."
315 << " Result: " << result << ";"
316 << " Expected result: " << expectedResult);
319 void getSecurityManagerGroups(gid_t **groups, size_t *groups_count, lib_retcode expectedResult)
321 int result = security_manager_groups_get(groups, groups_count);
322 RUNNER_ASSERT_MSG(static_cast<lib_retcode>(result) == expectedResult,
323 "Unexpected result in security_manager_groups_get()" << std::endl
324 << " Result: " << result << " Expected: " << expectedResult);
327 void registerPaths(const PathsRequest& req, lib_retcode expectedResult)
329 int result = security_manager_paths_register(req.get());
330 RUNNER_ASSERT_MSG(static_cast<lib_retcode>(result) == expectedResult,
331 "Unexpected result in security_manager_paths_register()" << std::endl
332 << " Result: " << result << " Expected: " << expectedResult);
335 void labelsMonitorGetFd(const LabelMonitor &monitor, int *fd, lib_retcode expectedResult)
337 int result = security_manager_app_labels_monitor_get_fd(monitor.get(), fd);
338 RUNNER_ASSERT_MSG(static_cast<lib_retcode>(result) == expectedResult,
339 "Unexpected result in security_manager_app_labels_monitor_get_fd()"
340 << std::endl << " Result: " << result << " Expected: "
344 void labelsProcess(const LabelMonitor &monitor, lib_retcode expectedResult)
346 int result = security_manager_app_labels_monitor_process(monitor.get());
347 RUNNER_ASSERT_MSG(static_cast<lib_retcode>(result) == expectedResult,
348 "Unexpected result in security_manager_app_labels_monitor_process()"
349 << std::endl << " Result: " << result << " Expected: "
355 } // namespace SecurityManagerTest