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 pkgmgrResult = system(command.str().c_str());
42 RUNNER_ASSERT_MSG(pkgmgrResult == 0, "failed to set package_info: " << pkgmgrResult);
44 int result = security_manager_app_install(request.get());
46 command = std::ostringstream();
47 command << "/usr/bin/sqlite3 "
48 << dbPath << ".pkgmgr_parser.db "
49 << " \"delete from package_info where package='" << request.getPkgId() << "'\"";
50 pkgmgrResult = system(command.str().c_str());
52 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
53 "installing app returned wrong value."
54 << " InstallRequest: [ " << request << "];"
55 << " Result: " << result << ";"
56 << " Expected result: " << expectedResult);
58 RUNNER_ASSERT_MSG(pkgmgrResult == 0, "failed to unset package_info: " << pkgmgrResult);
61 void update(const InstallRequest &request, lib_retcode expectedResult)
63 int result = security_manager_app_update(request.get());
64 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
65 "installing app returned wrong value."
66 << " InstallRequest: [ " << request << "];"
67 << " Result: " << result << ";"
68 << " Expected result: " << expectedResult);
71 void uninstall(const InstallRequest &request, lib_retcode expectedResult)
73 int result = security_manager_app_uninstall(request.get());
74 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
75 "uninstalling app returned wrong value."
76 << " InstallRequest: [ " << request << "];"
77 << " Result: " << result << ";"
78 << " Expected result: " << expectedResult);
81 std::string getPkgId(const std::string &appId, lib_retcode expectedResult)
83 char *pkgId = nullptr;
84 int result = security_manager_get_app_pkgid(&pkgId, appId.c_str());
85 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
86 "getting pkg id from app id returned wrong value."
87 << " App id: " << appId << ";"
88 << " Result: " << result << ";"
89 << " Expected result: " << expectedResult);
90 if (expectedResult != SECURITY_MANAGER_SUCCESS)
93 RUNNER_ASSERT_MSG(pkgId != nullptr, "getting pkg id did not allocate memory");
94 std::string str(pkgId);
99 void setProcessLabel(const std::string &appId, lib_retcode expectedResult)
101 int result = security_manager_set_process_label_from_appid(appId.c_str());
102 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
103 "setting process label from app id returned wrong value."
104 << " App id: " << appId << ";"
105 << " Result: " << result << ";"
106 << " Expected result: " << expectedResult);
109 void setProcessGroups(const std::string &appId, lib_retcode expectedResult)
111 int result = security_manager_set_process_groups_from_appid(appId.c_str());
112 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
113 "setting process groups from app id returned wrong value."
114 << " App id: " << appId << ";"
115 << " Result: " << result << ";"
116 << " Expected result: " << expectedResult);
119 void dropProcessPrivileges(lib_retcode expectedResult)
121 int result = security_manager_drop_process_privileges();
122 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
123 "dropping process privileges returned wrong value."
124 << " Result: " << result << ";"
125 << " Expected result: " << expectedResult);
128 void prepareAppCandidate(lib_retcode expectedResult)
130 int result = security_manager_prepare_app_candidate();
131 RUNNER_ASSERT_MSG((lib_retcode) result == expectedResult,
132 "preparing app candidate process returned wrong value."
133 << " Result: " << result << ";"
134 << " Expected result: " << expectedResult);
137 void prepareApp(const std::string &appId, lib_retcode expectedResult)
139 int result = security_manager_prepare_app(appId.c_str());
140 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
141 "preparing app returned wrong value."
142 << " App id: " << appId << ";"
143 << " Result: " << result << ";"
144 << " Expected result: " << expectedResult);
147 void cleanupApp(const std::string &appId, uid_t uid, pid_t pid, lib_retcode expectedResult)
149 int result = security_manager_cleanup_app(appId.c_str(), uid, pid);
150 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
151 "cleanup app returned wrong value."
152 << " App id: " << appId << ";"
153 << " user id: " << uid << ";"
154 << " process id: " << pid << ";"
155 << " Result: " << result << ";"
156 << " Expected result: " << expectedResult);
159 void addUser(const UserRequest &request, lib_retcode expectedResult)
161 int result = security_manager_user_add(request.get());
162 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
163 "adding user returned wrong value."
164 << " UserRequest: [ " << request << "];"
165 << " Result: " << result << ";"
166 << " Expected result: " << expectedResult);
169 void deleteUser(const UserRequest &request, lib_retcode expectedResult)
171 int result = security_manager_user_delete(request.get());
172 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
173 "deleting user returned wrong value."
174 << " UserRequest: [ " << request << "];"
175 << " Result: " << result << ";"
176 << " Expected result: " << expectedResult);
179 void sendPolicy(const PolicyRequest &request, lib_retcode expectedResult)
181 int result = security_manager_policy_update_send(request.get());
182 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
183 "sending policy update for self returned wrong value."
184 << " PolicyRequest: [ " << request << "];"
185 << " Result: " << result << ";"
186 << " Expected result: " << expectedResult);
189 void getConfiguredPolicy(const PolicyEntry &filter, std::vector<PolicyEntry> &policyEntries, lib_retcode expectedResult, bool forAdmin)
191 policy_entry **pp_privs_policy = NULL;
192 size_t policy_size = 0;
196 result = security_manager_get_configured_policy_for_admin(filter.get(), &pp_privs_policy, &policy_size);
198 result = security_manager_get_configured_policy_for_self(filter.get(), &pp_privs_policy, &policy_size);
201 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
202 "Unexpected result for filter: " << filter << std::endl
203 << " Result: " << result << ";");
205 for (unsigned int i = 0; i < policy_size; ++i) {
206 PolicyEntry pe(*pp_privs_policy[i]);
207 policyEntries.push_back(pe);
211 void getPolicy(const PolicyEntry &filter, std::vector<PolicyEntry> &policyEntries, lib_retcode expectedResult)
213 policy_entry **pp_privs_policy = NULL;
214 size_t policy_size = 0;
217 result = security_manager_get_policy(filter.get(), &pp_privs_policy, &policy_size);
218 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
219 "Unexpected result" << std::endl
220 << " Result: " << result << ";");
221 for (unsigned int i = 0; i < policy_size; ++i) {
222 PolicyEntry pe(*pp_privs_policy[i]);
223 policyEntries.push_back(pe);
227 void applySharing(const SharingRequest &req, lib_retcode expectedResult) {
228 int result = security_manager_private_sharing_apply(req.get());
229 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
230 "Unexpected result from security_manager_private_sharing_apply" << std::endl
231 << " Result: " << result << ";");
234 void dropSharing(const SharingRequest &req, lib_retcode expectedResult) {
235 int result = security_manager_private_sharing_drop(req.get());
236 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
237 "Unexpected result from security_manager_private_sharing_drop" << std::endl
238 << " Result: " << result << ";");
241 void getPolicyForSelf(const PolicyEntry &filter, std::vector<PolicyEntry> &policyEntries, lib_retcode expectedResult)
243 getConfiguredPolicy(filter, policyEntries, expectedResult, false);
246 void getPolicyForAdmin(const PolicyEntry &filter, std::vector<PolicyEntry> &policyEntries, lib_retcode expectedResult)
248 getConfiguredPolicy(filter, policyEntries, expectedResult, true);
251 void getPkgIdBySocket(int socketFd, std::string *pkgId, std::string *appId, lib_retcode expectedResult)
253 char *pkg_Id = nullptr;
254 char *app_Id = nullptr;
256 int result = security_manager_identify_app_from_socket(socketFd,
257 pkgId == nullptr ? nullptr : &pkg_Id,
258 appId == nullptr ? nullptr : &app_Id);
260 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
261 "getting pkg id from socket returned wrong value."
262 << " socket: " << socketFd << ";"
263 << " Result: " << result << ";"
264 << " Expected result: " << expectedResult);
277 void getPkgIdByPid(pid_t pid, std::string *pkgId, std::string *appId, lib_retcode expectedResult)
279 char *pkg_Id = nullptr;
280 char *app_Id = nullptr;
282 int result = security_manager_identify_app_from_pid(pid,
283 pkgId == nullptr ? nullptr : &pkg_Id,
284 appId == nullptr ? nullptr : &app_Id);
286 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
287 "getting pkg id from pid returned wrong value."
288 << " pid: " << pid << ";"
289 << " Result: " << result << ";"
290 << " Expected result: " << expectedResult);
303 void getPkgIdByCynaraClient(const std::string &client, std::string *pkgId, std::string *appId,
304 lib_retcode expectedResult)
306 char *pkg_Id = nullptr;
307 char *app_Id = nullptr;
309 int result = security_manager_identify_app_from_cynara_client(client.c_str(),
310 pkgId == nullptr ? nullptr : &pkg_Id,
311 appId == nullptr ? nullptr : &app_Id);
313 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
314 "getting pkg id from pid returned wrong value."
315 << " client: " << client << ";"
316 << " Result: " << result << ";"
317 << " Expected result: " << expectedResult);
330 void appHasPrivilege(const std::string &appId, const std::string &privilege, uid_t user,
331 int &value, lib_retcode expectedResult)
333 int result = security_manager_app_has_privilege(appId.c_str(), privilege.c_str(), user, &value);
335 RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
336 "checking application privilege returned wrong result."
337 << " Result: " << result << ";"
338 << " Expected result: " << expectedResult);
341 void getSecurityManagerGroups(gid_t **groups, size_t *groups_count, lib_retcode expectedResult)
343 int result = security_manager_groups_get(groups, groups_count);
344 RUNNER_ASSERT_MSG(static_cast<lib_retcode>(result) == expectedResult,
345 "Unexpected result in security_manager_groups_get()" << std::endl
346 << " Result: " << result << " Expected: " << expectedResult);
349 void registerPaths(const PathsRequest& req, lib_retcode expectedResult)
351 int result = security_manager_paths_register(req.get());
352 RUNNER_ASSERT_MSG(static_cast<lib_retcode>(result) == expectedResult,
353 "Unexpected result in security_manager_paths_register()" << std::endl
354 << " Result: " << result << " Expected: " << expectedResult);
357 void labelsMonitorGetFd(const LabelMonitor &monitor, int *fd, lib_retcode expectedResult)
359 int result = security_manager_app_labels_monitor_get_fd(monitor.get(), fd);
360 RUNNER_ASSERT_MSG(static_cast<lib_retcode>(result) == expectedResult,
361 "Unexpected result in security_manager_app_labels_monitor_get_fd()"
362 << std::endl << " Result: " << result << " Expected: "
366 void labelsProcess(const LabelMonitor &monitor, lib_retcode expectedResult)
368 int result = security_manager_app_labels_monitor_process(monitor.get());
369 RUNNER_ASSERT_MSG(static_cast<lib_retcode>(result) == expectedResult,
370 "Unexpected result in security_manager_app_labels_monitor_process()"
371 << std::endl << " Result: " << result << " Expected: "
377 } // namespace SecurityManagerTest