2 * Copyright (c) 2014-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.
19 #include <dpl/test/test_runner.h>
21 namespace SecurityManagerTest {
25 void install(const InstallRequest &request, lib_retcode expectedResult)
27 int result = security_manager_app_install(request.get());
28 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
29 "installing app returned wrong value."
30 << " InstallRequest: [ " << request << "];"
31 << " Result: " << result << ";"
32 << " Expected result: " << expectedResult);
35 void update(const InstallRequest &request, lib_retcode expectedResult)
37 int result = security_manager_app_update(request.get());
38 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
39 "installing app returned wrong value."
40 << " InstallRequest: [ " << request << "];"
41 << " Result: " << result << ";"
42 << " Expected result: " << expectedResult);
45 void uninstall(const InstallRequest &request, lib_retcode expectedResult)
47 int result = security_manager_app_uninstall(request.get());
48 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
49 "uninstalling app returned wrong value."
50 << " InstallRequest: [ " << request << "];"
51 << " Result: " << result << ";"
52 << " Expected result: " << expectedResult);
55 std::string getPkgId(const std::string &appId, lib_retcode expectedResult)
57 char *pkgId = nullptr;
58 int result = security_manager_get_app_pkgid(&pkgId, appId.c_str());
59 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
60 "getting pkg id from app id returned wrong value."
61 << " App id: " << appId << ";"
62 << " Result: " << result << ";"
63 << " Expected result: " << expectedResult);
64 if (expectedResult != SECURITY_MANAGER_SUCCESS)
67 RUNNER_ASSERT_MSG(pkgId != nullptr, "getting pkg id did not allocate memory");
68 std::string str(pkgId);
73 void setProcessLabel(const std::string &appId, lib_retcode expectedResult)
75 int result = security_manager_set_process_label_from_appid(appId.c_str());
76 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
77 "setting process label from app id returned wrong value."
78 << " App id: " << appId << ";"
79 << " Result: " << result << ";"
80 << " Expected result: " << expectedResult);
83 void setProcessGroups(const std::string &appId, lib_retcode expectedResult)
85 int result = security_manager_set_process_groups_from_appid(appId.c_str());
86 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
87 "setting process groups from app id returned wrong value."
88 << " App id: " << appId << ";"
89 << " Result: " << result << ";"
90 << " Expected result: " << expectedResult);
93 void dropProcessPrivileges(lib_retcode expectedResult)
95 int result = security_manager_drop_process_privileges();
96 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
97 "dropping process privileges returned wrong value."
98 << " Result: " << result << ";"
99 << " Expected result: " << expectedResult);
102 void prepareApp(const std::string &appId, lib_retcode expectedResult)
104 int result = security_manager_prepare_app(appId.c_str());
105 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
106 "preparing app returned wrong value."
107 << " App id: " << appId << ";"
108 << " Result: " << result << ";"
109 << " Expected result: " << expectedResult);
112 void cleanupApp(const std::string &appId, uid_t uid, pid_t pid, lib_retcode expectedResult)
114 int result = security_manager_cleanup_app(appId.c_str(), uid, pid);
115 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
116 "cleanup app returned wrong value."
117 << " App id: " << appId << ";"
118 << " user id: " << uid << ";"
119 << " process id: " << pid << ";"
120 << " Result: " << result << ";"
121 << " Expected result: " << expectedResult);
124 void addUser(const UserRequest &request, lib_retcode expectedResult)
126 int result = security_manager_user_add(request.get());
127 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
128 "adding user returned wrong value."
129 << " UserRequest: [ " << request << "];"
130 << " Result: " << result << ";"
131 << " Expected result: " << expectedResult);
134 void deleteUser(const UserRequest &request, lib_retcode expectedResult)
136 int result = security_manager_user_delete(request.get());
137 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
138 "deleting user returned wrong value."
139 << " UserRequest: [ " << request << "];"
140 << " Result: " << result << ";"
141 << " Expected result: " << expectedResult);
144 void sendPolicy(const PolicyRequest &request, lib_retcode expectedResult)
146 int result = security_manager_policy_update_send(request.get());
147 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
148 "sending policy update for self returned wrong value."
149 << " PolicyRequest: [ " << request << "];"
150 << " Result: " << result << ";"
151 << " Expected result: " << expectedResult);
154 void getConfiguredPolicy(const PolicyEntry &filter, std::vector<PolicyEntry> &policyEntries, lib_retcode expectedResult, bool forAdmin)
156 policy_entry **pp_privs_policy = NULL;
157 size_t policy_size = 0;
161 result = security_manager_get_configured_policy_for_admin(filter.get(), &pp_privs_policy, &policy_size);
163 result = security_manager_get_configured_policy_for_self(filter.get(), &pp_privs_policy, &policy_size);
166 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
167 "Unexpected result for filter: " << filter << std::endl
168 << " Result: " << result << ";");
170 for (unsigned int i = 0; i < policy_size; ++i) {
171 PolicyEntry pe(*pp_privs_policy[i]);
172 policyEntries.push_back(pe);
176 void getPolicy(const PolicyEntry &filter, std::vector<PolicyEntry> &policyEntries, lib_retcode expectedResult)
178 policy_entry **pp_privs_policy = NULL;
179 size_t policy_size = 0;
182 result = security_manager_get_policy(filter.get(), &pp_privs_policy, &policy_size);
183 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
184 "Unexpected result" << std::endl
185 << " Result: " << result << ";");
186 for (unsigned int i = 0; i < policy_size; ++i) {
187 PolicyEntry pe(*pp_privs_policy[i]);
188 policyEntries.push_back(pe);
192 void applySharing(const SharingRequest &req, lib_retcode expectedResult) {
193 int result = security_manager_private_sharing_apply(req.get());
194 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
195 "Unexpected result from security_manager_private_sharing_apply" << std::endl
196 << " Result: " << result << ";");
199 void dropSharing(const SharingRequest &req, lib_retcode expectedResult) {
200 int result = security_manager_private_sharing_drop(req.get());
201 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
202 "Unexpected result from security_manager_private_sharing_drop" << std::endl
203 << " Result: " << result << ";");
206 void getPolicyForSelf(const PolicyEntry &filter, std::vector<PolicyEntry> &policyEntries, lib_retcode expectedResult)
208 getConfiguredPolicy(filter, policyEntries, expectedResult, false);
211 void getPolicyForAdmin(const PolicyEntry &filter, std::vector<PolicyEntry> &policyEntries, lib_retcode expectedResult)
213 getConfiguredPolicy(filter, policyEntries, expectedResult, true);
216 void getPkgIdBySocket(int socketFd, std::string *pkgId, std::string *appId, lib_retcode expectedResult)
218 char *pkg_Id = nullptr;
219 char *app_Id = nullptr;
221 int result = security_manager_identify_app_from_socket(socketFd,
222 pkgId == nullptr ? nullptr : &pkg_Id,
223 appId == nullptr ? nullptr : &app_Id);
225 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
226 "getting pkg id from socket returned wrong value."
227 << " socket: " << socketFd << ";"
228 << " Result: " << result << ";"
229 << " Expected result: " << expectedResult);
242 void getPkgIdByPid(pid_t pid, std::string *pkgId, std::string *appId, lib_retcode expectedResult)
244 char *pkg_Id = nullptr;
245 char *app_Id = nullptr;
247 int result = security_manager_identify_app_from_pid(pid,
248 pkgId == nullptr ? nullptr : &pkg_Id,
249 appId == nullptr ? nullptr : &app_Id);
251 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
252 "getting pkg id from pid returned wrong value."
253 << " pid: " << pid << ";"
254 << " Result: " << result << ";"
255 << " Expected result: " << expectedResult);
268 void getPkgIdByCynaraClient(const std::string &client, std::string *pkgId, std::string *appId,
269 lib_retcode expectedResult)
271 char *pkg_Id = nullptr;
272 char *app_Id = nullptr;
274 int result = security_manager_identify_app_from_cynara_client(client.c_str(),
275 pkgId == nullptr ? nullptr : &pkg_Id,
276 appId == nullptr ? nullptr : &app_Id);
278 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
279 "getting pkg id from pid returned wrong value."
280 << " client: " << client << ";"
281 << " Result: " << result << ";"
282 << " Expected result: " << expectedResult);
295 void appHasPrivilege(const std::string &appId, const std::string &privilege, uid_t user,
296 int &value, lib_retcode expectedResult)
298 int result = security_manager_app_has_privilege(appId.c_str(), privilege.c_str(), user, &value);
300 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
301 "checking application privilege returned wrong result."
302 << " Result: " << result << ";"
303 << " Expected result: " << expectedResult);
306 void getSecurityManagerGroups(gid_t **groups, size_t *groups_count, lib_retcode expectedResult)
308 int result = security_manager_groups_get(groups, groups_count);
309 RUNNER_ASSERT_MSG(static_cast<lib_retcode>(result) == expectedResult,
310 "Unexpected result in security_manager_groups_get()" << std::endl
311 << " Result: " << result << " Expected: " << expectedResult);
314 void registerPaths(const PathsRequest& req, lib_retcode expectedResult)
316 int result = security_manager_paths_register(req.get());
317 RUNNER_ASSERT_MSG(static_cast<lib_retcode>(result) == expectedResult,
318 "Unexpected result in security_manager_paths_register()" << std::endl
319 << " Result: " << result << " Expected: " << expectedResult);
322 void labelsMonitorGetFd(const LabelMonitor &monitor, int *fd, lib_retcode expectedResult)
324 int result = security_manager_app_labels_monitor_get_fd(monitor.get(), fd);
325 RUNNER_ASSERT_MSG(static_cast<lib_retcode>(result) == expectedResult,
326 "Unexpected result in security_manager_app_labels_monitor_get_fd()"
327 << std::endl << " Result: " << result << " Expected: "
331 void labelsProcess(const LabelMonitor &monitor, lib_retcode expectedResult)
333 int result = security_manager_app_labels_monitor_process(monitor.get());
334 RUNNER_ASSERT_MSG(static_cast<lib_retcode>(result) == expectedResult,
335 "Unexpected result in security_manager_app_labels_monitor_process()"
336 << std::endl << " Result: " << result << " Expected: "
342 } // namespace SecurityManagerTest