451675531fa399ab88e135358b6daf7630cd3c28
[platform/core/test/security-tests.git] / src / common / sm_api.cpp
1 /*
2  * Copyright (c) 2014-2019 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 #include <unistd.h>
18 #include <sys/types.h>
19
20 #include <sm_api.h>
21
22 #include <dpl/test/test_runner.h>
23
24 namespace SecurityManagerTest {
25
26 namespace Api {
27
28 void install(const InstallRequest &request, lib_retcode expectedResult)
29 {
30     uid_t uid = getuid();
31     std::string dbPath = uid == 0 ? "/opt/dbspace/" : "/opt/dbspace/user/" + std::to_string(uid) + "/";
32
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);
43
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);
50
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);
57 }
58
59 void update(const InstallRequest &request, lib_retcode expectedResult)
60 {
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);
67 }
68
69 void uninstall(const InstallRequest &request, lib_retcode expectedResult)
70 {
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);
77 }
78
79 std::string getPkgId(const std::string &appId, lib_retcode expectedResult)
80 {
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)
89         return std::string();
90
91     RUNNER_ASSERT_MSG(pkgId != nullptr, "getting pkg id did not allocate memory");
92     std::string str(pkgId);
93     free(pkgId);
94     return str;
95 }
96
97 void setProcessLabel(const std::string &appId, lib_retcode expectedResult)
98 {
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);
105 }
106
107 void setProcessGroups(const std::string &appId, lib_retcode expectedResult)
108 {
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);
115 }
116
117 void dropProcessPrivileges(lib_retcode expectedResult)
118 {
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);
124 }
125
126 void prepareAppCandidate(lib_retcode expectedResult)
127 {
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);
133 }
134
135 void prepareApp(const std::string &appId, lib_retcode expectedResult)
136 {
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);
143 }
144
145 void cleanupApp(const std::string &appId, uid_t uid, pid_t pid, lib_retcode expectedResult)
146 {
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);
155 }
156
157 void addUser(const UserRequest &request, lib_retcode expectedResult)
158 {
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);
165 }
166
167 void deleteUser(const UserRequest &request, lib_retcode expectedResult)
168 {
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);
175 }
176
177 void sendPolicy(const PolicyRequest &request, lib_retcode expectedResult)
178 {
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);
185 }
186
187 void getConfiguredPolicy(const PolicyEntry &filter, std::vector<PolicyEntry> &policyEntries, lib_retcode expectedResult, bool forAdmin)
188 {
189     policy_entry **pp_privs_policy = NULL;
190     size_t policy_size = 0;
191     int result;
192
193     if (forAdmin) {
194         result = security_manager_get_configured_policy_for_admin(filter.get(), &pp_privs_policy, &policy_size);
195     } else {
196         result = security_manager_get_configured_policy_for_self(filter.get(), &pp_privs_policy, &policy_size);
197     };
198
199     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
200                       "Unexpected result for filter: " << filter << std::endl
201                           << " Result: " << result << ";");
202
203     for (unsigned int i = 0; i < policy_size; ++i) {
204         PolicyEntry pe(*pp_privs_policy[i]);
205         policyEntries.push_back(pe);
206     };
207 }
208
209 void getPolicy(const PolicyEntry &filter, std::vector<PolicyEntry> &policyEntries, lib_retcode expectedResult)
210 {
211     policy_entry **pp_privs_policy = NULL;
212     size_t policy_size = 0;
213     int result;
214
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);
222     };
223 }
224
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 << ";");
230 }
231
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 << ";");
237 }
238
239 void getPolicyForSelf(const PolicyEntry &filter, std::vector<PolicyEntry> &policyEntries, lib_retcode expectedResult)
240 {
241     getConfiguredPolicy(filter, policyEntries, expectedResult, false);
242 }
243
244 void getPolicyForAdmin(const PolicyEntry &filter, std::vector<PolicyEntry> &policyEntries, lib_retcode expectedResult)
245 {
246     getConfiguredPolicy(filter, policyEntries, expectedResult, true);
247 }
248
249 void getPkgIdBySocket(int socketFd, std::string *pkgId, std::string *appId, lib_retcode expectedResult)
250 {
251     char *pkg_Id = nullptr;
252     char *app_Id = nullptr;
253
254     int result = security_manager_identify_app_from_socket(socketFd,
255                     pkgId == nullptr ? nullptr : &pkg_Id,
256                     appId == nullptr ? nullptr : &app_Id);
257
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);
263
264     if (pkg_Id) {
265         *pkgId = pkg_Id;
266         free(pkg_Id);
267     }
268
269     if (app_Id) {
270         *appId = app_Id;
271         free(app_Id);
272     }
273 }
274
275 void getPkgIdByPid(pid_t pid, std::string *pkgId, std::string *appId, lib_retcode expectedResult)
276 {
277     char *pkg_Id = nullptr;
278     char *app_Id = nullptr;
279
280     int result = security_manager_identify_app_from_pid(pid,
281                     pkgId == nullptr ? nullptr : &pkg_Id,
282                     appId == nullptr ? nullptr : &app_Id);
283
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);
289
290     if (pkg_Id) {
291         *pkgId = pkg_Id;
292         free(pkg_Id);
293     }
294
295     if (app_Id) {
296         *appId = app_Id;
297         free(app_Id);
298     }
299 }
300
301 void getPkgIdByCynaraClient(const std::string &client, std::string *pkgId, std::string *appId,
302                             lib_retcode expectedResult)
303 {
304     char *pkg_Id = nullptr;
305     char *app_Id = nullptr;
306
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);
310
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);
316
317     if (pkg_Id) {
318         *pkgId = pkg_Id;
319         free(pkg_Id);
320     }
321
322     if (app_Id) {
323         *appId = app_Id;
324         free(app_Id);
325     }
326 }
327
328 void appHasPrivilege(const std::string &appId, const std::string &privilege, uid_t user,
329                      int &value, lib_retcode expectedResult)
330 {
331     int result = security_manager_app_has_privilege(appId.c_str(), privilege.c_str(), user, &value);
332
333     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
334                       "checking application privilege returned wrong result."
335                           << " Result: " << result << ";"
336                           << " Expected result: " << expectedResult);
337 }
338
339 void getSecurityManagerGroups(gid_t **groups, size_t *groups_count, lib_retcode expectedResult)
340 {
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);
345 }
346
347 void registerPaths(const PathsRequest& req, lib_retcode expectedResult)
348 {
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);
353 }
354
355 void labelsMonitorGetFd(const LabelMonitor &monitor, int *fd, lib_retcode expectedResult)
356 {
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: "
361                           << expectedResult);
362 };
363
364 void labelsProcess(const LabelMonitor &monitor, lib_retcode expectedResult)
365 {
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: "
370                           << expectedResult);
371 }
372
373 } // namespace Api
374
375 } // namespace SecurityManagerTest