2 * Copyright (c) 2014-2019 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)
31 std::string dbPath = uid == 0 ? "/opt/dbspace/" : "/opt/dbspace/user/" + std::to_string(uid) + "/";
33 std::ostringstream command;
34 command << "/usr/bin/sqlite3 "
35 << dbPath << ".pkgmgr_parser.db "
36 << " \"insert into package_info (package, package_type, package_api_version,"
37 << " install_location, mainapp_id, root_path, installed_storage)"
38 << " values ('" << request.getPkgId() << "', 'tpk', '" << request.getAppTizenVersion()
39 << "', 'auto', '" << request.getPkgId() << "', '/opt/usr/globallapps/"
40 << request.getPkgId() << "', 'installed_internal')\"";
41 int result = system(command.str().c_str());
42 RUNNER_ASSERT_MSG(result == 0, "failed to set package_info: " << result);
44 result = security_manager_app_install(request.get());
45 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
46 "installing app returned wrong value."
47 << " InstallRequest: [ " << request << "];"
48 << " Result: " << result << ";"
49 << " Expected result: " << expectedResult);
51 command = std::ostringstream();
52 command << "/usr/bin/sqlite3 "
53 << dbPath << ".pkgmgr_parser.db "
54 << " \"delete from package_info where package='" << request.getPkgId() << "'\"";
55 result = system(command.str().c_str());
56 RUNNER_ASSERT_MSG(result == 0, "failed to unset package_info: " << result);
59 void update(const InstallRequest &request, lib_retcode expectedResult)
61 int result = security_manager_app_update(request.get());
62 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
63 "installing app returned wrong value."
64 << " InstallRequest: [ " << request << "];"
65 << " Result: " << result << ";"
66 << " Expected result: " << expectedResult);
69 void uninstall(const InstallRequest &request, lib_retcode expectedResult)
71 int result = security_manager_app_uninstall(request.get());
72 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
73 "uninstalling app returned wrong value."
74 << " InstallRequest: [ " << request << "];"
75 << " Result: " << result << ";"
76 << " Expected result: " << expectedResult);
79 std::string getPkgId(const std::string &appId, lib_retcode expectedResult)
81 char *pkgId = nullptr;
82 int result = security_manager_get_app_pkgid(&pkgId, appId.c_str());
83 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
84 "getting pkg id from app id returned wrong value."
85 << " App id: " << appId << ";"
86 << " Result: " << result << ";"
87 << " Expected result: " << expectedResult);
88 if (expectedResult != SECURITY_MANAGER_SUCCESS)
91 RUNNER_ASSERT_MSG(pkgId != nullptr, "getting pkg id did not allocate memory");
92 std::string str(pkgId);
97 void setProcessLabel(const std::string &appId, lib_retcode expectedResult)
99 int result = security_manager_set_process_label_from_appid(appId.c_str());
100 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
101 "setting process label from app id returned wrong value."
102 << " App id: " << appId << ";"
103 << " Result: " << result << ";"
104 << " Expected result: " << expectedResult);
107 void setProcessGroups(const std::string &appId, lib_retcode expectedResult)
109 int result = security_manager_set_process_groups_from_appid(appId.c_str());
110 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
111 "setting process groups from app id returned wrong value."
112 << " App id: " << appId << ";"
113 << " Result: " << result << ";"
114 << " Expected result: " << expectedResult);
117 void dropProcessPrivileges(lib_retcode expectedResult)
119 int result = security_manager_drop_process_privileges();
120 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
121 "dropping process privileges returned wrong value."
122 << " Result: " << result << ";"
123 << " Expected result: " << expectedResult);
126 void prepareAppCandidate(lib_retcode expectedResult)
128 int result = security_manager_prepare_app_candidate();
129 RUNNER_ASSERT_MSG((lib_retcode) result == expectedResult,
130 "preparing app candidate process returned wrong value."
131 << " Result: " << result << ";"
132 << " Expected result: " << expectedResult);
135 void prepareApp(const std::string &appId, lib_retcode expectedResult)
137 int result = security_manager_prepare_app(appId.c_str());
138 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
139 "preparing app returned wrong value."
140 << " App id: " << appId << ";"
141 << " Result: " << result << ";"
142 << " Expected result: " << expectedResult);
145 void cleanupApp(const std::string &appId, uid_t uid, pid_t pid, lib_retcode expectedResult)
147 int result = security_manager_cleanup_app(appId.c_str(), uid, pid);
148 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
149 "cleanup app returned wrong value."
150 << " App id: " << appId << ";"
151 << " user id: " << uid << ";"
152 << " process id: " << pid << ";"
153 << " Result: " << result << ";"
154 << " Expected result: " << expectedResult);
157 void addUser(const UserRequest &request, lib_retcode expectedResult)
159 int result = security_manager_user_add(request.get());
160 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
161 "adding user returned wrong value."
162 << " UserRequest: [ " << request << "];"
163 << " Result: " << result << ";"
164 << " Expected result: " << expectedResult);
167 void deleteUser(const UserRequest &request, lib_retcode expectedResult)
169 int result = security_manager_user_delete(request.get());
170 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
171 "deleting user returned wrong value."
172 << " UserRequest: [ " << request << "];"
173 << " Result: " << result << ";"
174 << " Expected result: " << expectedResult);
177 void sendPolicy(const PolicyRequest &request, lib_retcode expectedResult)
179 int result = security_manager_policy_update_send(request.get());
180 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
181 "sending policy update for self returned wrong value."
182 << " PolicyRequest: [ " << request << "];"
183 << " Result: " << result << ";"
184 << " Expected result: " << expectedResult);
187 void getConfiguredPolicy(const PolicyEntry &filter, std::vector<PolicyEntry> &policyEntries, lib_retcode expectedResult, bool forAdmin)
189 policy_entry **pp_privs_policy = NULL;
190 size_t policy_size = 0;
194 result = security_manager_get_configured_policy_for_admin(filter.get(), &pp_privs_policy, &policy_size);
196 result = security_manager_get_configured_policy_for_self(filter.get(), &pp_privs_policy, &policy_size);
199 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
200 "Unexpected result for filter: " << filter << std::endl
201 << " Result: " << result << ";");
203 for (unsigned int i = 0; i < policy_size; ++i) {
204 PolicyEntry pe(*pp_privs_policy[i]);
205 policyEntries.push_back(pe);
209 void getPolicy(const PolicyEntry &filter, std::vector<PolicyEntry> &policyEntries, lib_retcode expectedResult)
211 policy_entry **pp_privs_policy = NULL;
212 size_t policy_size = 0;
215 result = security_manager_get_policy(filter.get(), &pp_privs_policy, &policy_size);
216 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
217 "Unexpected result" << std::endl
218 << " Result: " << result << ";");
219 for (unsigned int i = 0; i < policy_size; ++i) {
220 PolicyEntry pe(*pp_privs_policy[i]);
221 policyEntries.push_back(pe);
225 void applySharing(const SharingRequest &req, lib_retcode expectedResult) {
226 int result = security_manager_private_sharing_apply(req.get());
227 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
228 "Unexpected result from security_manager_private_sharing_apply" << std::endl
229 << " Result: " << result << ";");
232 void dropSharing(const SharingRequest &req, lib_retcode expectedResult) {
233 int result = security_manager_private_sharing_drop(req.get());
234 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
235 "Unexpected result from security_manager_private_sharing_drop" << std::endl
236 << " Result: " << result << ";");
239 void getPolicyForSelf(const PolicyEntry &filter, std::vector<PolicyEntry> &policyEntries, lib_retcode expectedResult)
241 getConfiguredPolicy(filter, policyEntries, expectedResult, false);
244 void getPolicyForAdmin(const PolicyEntry &filter, std::vector<PolicyEntry> &policyEntries, lib_retcode expectedResult)
246 getConfiguredPolicy(filter, policyEntries, expectedResult, true);
249 void getPkgIdBySocket(int socketFd, std::string *pkgId, std::string *appId, lib_retcode expectedResult)
251 char *pkg_Id = nullptr;
252 char *app_Id = nullptr;
254 int result = security_manager_identify_app_from_socket(socketFd,
255 pkgId == nullptr ? nullptr : &pkg_Id,
256 appId == nullptr ? nullptr : &app_Id);
258 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
259 "getting pkg id from socket returned wrong value."
260 << " socket: " << socketFd << ";"
261 << " Result: " << result << ";"
262 << " Expected result: " << expectedResult);
275 void getPkgIdByPid(pid_t pid, std::string *pkgId, std::string *appId, lib_retcode expectedResult)
277 char *pkg_Id = nullptr;
278 char *app_Id = nullptr;
280 int result = security_manager_identify_app_from_pid(pid,
281 pkgId == nullptr ? nullptr : &pkg_Id,
282 appId == nullptr ? nullptr : &app_Id);
284 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
285 "getting pkg id from pid returned wrong value."
286 << " pid: " << pid << ";"
287 << " Result: " << result << ";"
288 << " Expected result: " << expectedResult);
301 void getPkgIdByCynaraClient(const std::string &client, std::string *pkgId, std::string *appId,
302 lib_retcode expectedResult)
304 char *pkg_Id = nullptr;
305 char *app_Id = nullptr;
307 int result = security_manager_identify_app_from_cynara_client(client.c_str(),
308 pkgId == nullptr ? nullptr : &pkg_Id,
309 appId == nullptr ? nullptr : &app_Id);
311 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
312 "getting pkg id from pid returned wrong value."
313 << " client: " << client << ";"
314 << " Result: " << result << ";"
315 << " Expected result: " << expectedResult);
328 void appHasPrivilege(const std::string &appId, const std::string &privilege, uid_t user,
329 int &value, lib_retcode expectedResult)
331 int result = security_manager_app_has_privilege(appId.c_str(), privilege.c_str(), user, &value);
333 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
334 "checking application privilege returned wrong result."
335 << " Result: " << result << ";"
336 << " Expected result: " << expectedResult);
339 void getSecurityManagerGroups(gid_t **groups, size_t *groups_count, lib_retcode expectedResult)
341 int result = security_manager_groups_get(groups, groups_count);
342 RUNNER_ASSERT_MSG(static_cast<lib_retcode>(result) == expectedResult,
343 "Unexpected result in security_manager_groups_get()" << std::endl
344 << " Result: " << result << " Expected: " << expectedResult);
347 void registerPaths(const PathsRequest& req, lib_retcode expectedResult)
349 int result = security_manager_paths_register(req.get());
350 RUNNER_ASSERT_MSG(static_cast<lib_retcode>(result) == expectedResult,
351 "Unexpected result in security_manager_paths_register()" << std::endl
352 << " Result: " << result << " Expected: " << expectedResult);
355 void labelsMonitorGetFd(const LabelMonitor &monitor, int *fd, lib_retcode expectedResult)
357 int result = security_manager_app_labels_monitor_get_fd(monitor.get(), fd);
358 RUNNER_ASSERT_MSG(static_cast<lib_retcode>(result) == expectedResult,
359 "Unexpected result in security_manager_app_labels_monitor_get_fd()"
360 << std::endl << " Result: " << result << " Expected: "
364 void labelsProcess(const LabelMonitor &monitor, lib_retcode expectedResult)
366 int result = security_manager_app_labels_monitor_process(monitor.get());
367 RUNNER_ASSERT_MSG(static_cast<lib_retcode>(result) == expectedResult,
368 "Unexpected result in security_manager_app_labels_monitor_process()"
369 << std::endl << " Result: " << result << " Expected: "
375 } // namespace SecurityManagerTest