bc70ea06086fe82bbc6175e7ee167e3f1b916bdb
[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 pkgmgrResult = system(command.str().c_str());
42     RUNNER_ASSERT_MSG(pkgmgrResult == 0, "failed to set package_info: " << pkgmgrResult);
43
44     int result = security_manager_app_install(request.get());
45
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());
51
52     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
53                       "installing app returned wrong value."
54                       << " InstallRequest: [ " << request << "];"
55                       << " Result: " << result << ";"
56                       << " Expected result: " << expectedResult);
57
58     RUNNER_ASSERT_MSG(pkgmgrResult == 0, "failed to unset package_info: " << pkgmgrResult);
59 }
60
61 void update(const InstallRequest &request, lib_retcode expectedResult)
62 {
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);
69 }
70
71 void uninstall(const InstallRequest &request, lib_retcode expectedResult)
72 {
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);
79 }
80
81 std::string getPkgId(const std::string &appId, lib_retcode expectedResult)
82 {
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)
91         return std::string();
92
93     RUNNER_ASSERT_MSG(pkgId != nullptr, "getting pkg id did not allocate memory");
94     std::string str(pkgId);
95     free(pkgId);
96     return str;
97 }
98
99 void setProcessLabel(const std::string &appId, lib_retcode expectedResult)
100 {
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);
107 }
108
109 void setProcessGroups(const std::string &appId, lib_retcode expectedResult)
110 {
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);
117 }
118
119 void dropProcessPrivileges(lib_retcode expectedResult)
120 {
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);
126 }
127
128 void prepareAppCandidate(lib_retcode expectedResult)
129 {
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);
135 }
136
137 void prepareApp(const std::string &appId, lib_retcode expectedResult)
138 {
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);
145 }
146
147 void cleanupApp(const std::string &appId, uid_t uid, pid_t pid, lib_retcode expectedResult)
148 {
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);
157 }
158
159 void addUser(const UserRequest &request, lib_retcode expectedResult)
160 {
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);
167 }
168
169 void deleteUser(const UserRequest &request, lib_retcode expectedResult)
170 {
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);
177 }
178
179 void sendPolicy(const PolicyRequest &request, lib_retcode expectedResult)
180 {
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);
187 }
188
189 void getConfiguredPolicy(const PolicyEntry &filter, std::vector<PolicyEntry> &policyEntries, lib_retcode expectedResult, bool forAdmin)
190 {
191     policy_entry **pp_privs_policy = NULL;
192     size_t policy_size = 0;
193     int result;
194
195     if (forAdmin) {
196         result = security_manager_get_configured_policy_for_admin(filter.get(), &pp_privs_policy, &policy_size);
197     } else {
198         result = security_manager_get_configured_policy_for_self(filter.get(), &pp_privs_policy, &policy_size);
199     };
200
201     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
202                       "Unexpected result for filter: " << filter << std::endl
203                           << " Result: " << result << ";");
204
205     for (unsigned int i = 0; i < policy_size; ++i) {
206         PolicyEntry pe(*pp_privs_policy[i]);
207         policyEntries.push_back(pe);
208     };
209 }
210
211 void getPolicy(const PolicyEntry &filter, std::vector<PolicyEntry> &policyEntries, lib_retcode expectedResult)
212 {
213     policy_entry **pp_privs_policy = NULL;
214     size_t policy_size = 0;
215     int result;
216
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);
224     };
225 }
226
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 << ";");
232 }
233
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 << ";");
239 }
240
241 void getPolicyForSelf(const PolicyEntry &filter, std::vector<PolicyEntry> &policyEntries, lib_retcode expectedResult)
242 {
243     getConfiguredPolicy(filter, policyEntries, expectedResult, false);
244 }
245
246 void getPolicyForAdmin(const PolicyEntry &filter, std::vector<PolicyEntry> &policyEntries, lib_retcode expectedResult)
247 {
248     getConfiguredPolicy(filter, policyEntries, expectedResult, true);
249 }
250
251 void getPkgIdBySocket(int socketFd, std::string *pkgId, std::string *appId, lib_retcode expectedResult)
252 {
253     char *pkg_Id = nullptr;
254     char *app_Id = nullptr;
255
256     int result = security_manager_identify_app_from_socket(socketFd,
257                     pkgId == nullptr ? nullptr : &pkg_Id,
258                     appId == nullptr ? nullptr : &app_Id);
259
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);
265
266     if (pkg_Id) {
267         *pkgId = pkg_Id;
268         free(pkg_Id);
269     }
270
271     if (app_Id) {
272         *appId = app_Id;
273         free(app_Id);
274     }
275 }
276
277 void getPkgIdByPid(pid_t pid, std::string *pkgId, std::string *appId, lib_retcode expectedResult)
278 {
279     char *pkg_Id = nullptr;
280     char *app_Id = nullptr;
281
282     int result = security_manager_identify_app_from_pid(pid,
283                     pkgId == nullptr ? nullptr : &pkg_Id,
284                     appId == nullptr ? nullptr : &app_Id);
285
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);
291
292     if (pkg_Id) {
293         *pkgId = pkg_Id;
294         free(pkg_Id);
295     }
296
297     if (app_Id) {
298         *appId = app_Id;
299         free(app_Id);
300     }
301 }
302
303 void getPkgIdByCynaraClient(const std::string &client, std::string *pkgId, std::string *appId,
304                             lib_retcode expectedResult)
305 {
306     char *pkg_Id = nullptr;
307     char *app_Id = nullptr;
308
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);
312
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);
318
319     if (pkg_Id) {
320         *pkgId = pkg_Id;
321         free(pkg_Id);
322     }
323
324     if (app_Id) {
325         *appId = app_Id;
326         free(app_Id);
327     }
328 }
329
330 void appHasPrivilege(const std::string &appId, const std::string &privilege, uid_t user,
331                      int &value, lib_retcode expectedResult)
332 {
333     int result = security_manager_app_has_privilege(appId.c_str(), privilege.c_str(), user, &value);
334
335     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
336                       "checking application privilege returned wrong result."
337                           << " Result: " << result << ";"
338                           << " Expected result: " << expectedResult);
339 }
340
341 void getSecurityManagerGroups(gid_t **groups, size_t *groups_count, lib_retcode expectedResult)
342 {
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);
347 }
348
349 void registerPaths(const PathsRequest& req, lib_retcode expectedResult)
350 {
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);
355 }
356
357 void labelsMonitorGetFd(const LabelMonitor &monitor, int *fd, lib_retcode expectedResult)
358 {
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: "
363                           << expectedResult);
364 };
365
366 void labelsProcess(const LabelMonitor &monitor, lib_retcode expectedResult)
367 {
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: "
372                           << expectedResult);
373 }
374
375 } // namespace Api
376
377 } // namespace SecurityManagerTest