Merge branch 'tizen' into ckm
[platform/core/test/security-tests.git] / src / common / sm_api.cpp
1 /*
2  * Copyright (c) 2014-2020 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     int result = security_manager_app_install(request.get());
31
32     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
33                       "installing app returned wrong value."
34                       << " InstallRequest: [ " << request << "];"
35                       << " Result: " << result << ";"
36                       << " Expected result: " << expectedResult);
37 }
38
39 void update(const InstallRequest &request, lib_retcode expectedResult)
40 {
41     int result = security_manager_app_update(request.get());
42     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
43                       "installing app returned wrong value."
44                           << " InstallRequest: [ " << request << "];"
45                           << " Result: " << result << ";"
46                           << " Expected result: " << expectedResult);
47 }
48
49 void uninstall(const InstallRequest &request, lib_retcode expectedResult)
50 {
51     int result = security_manager_app_uninstall(request.get());
52     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
53                       "uninstalling app returned wrong value."
54                           << " InstallRequest: [ " << request << "];"
55                           << " Result: " << result << ";"
56                           << " Expected result: " << expectedResult);
57 }
58
59 std::string getPkgId(const std::string &appId, lib_retcode expectedResult)
60 {
61     char *pkgId = nullptr;
62     int result = security_manager_get_app_pkgid(&pkgId, appId.c_str());
63     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
64                       "getting pkg id from app id returned wrong value."
65                           << " App id: " << appId << ";"
66                           << " Result: " << result << ";"
67                           << " Expected result: " << expectedResult);
68     if (expectedResult != SECURITY_MANAGER_SUCCESS)
69         return std::string();
70
71     RUNNER_ASSERT_MSG(pkgId != nullptr, "getting pkg id did not allocate memory");
72     std::string str(pkgId);
73     free(pkgId);
74     return str;
75 }
76
77 void setProcessLabel(const std::string &appId, lib_retcode expectedResult)
78 {
79     int result = security_manager_set_process_label_from_appid(appId.c_str());
80     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
81                       "setting process label from app id returned wrong value."
82                           << " App id: " << appId << ";"
83                           << " Result: " << result << ";"
84                           << " Expected result: " << expectedResult);
85 }
86
87 void setProcessGroups(const std::string &appId, lib_retcode expectedResult)
88 {
89     int result = security_manager_set_process_groups_from_appid(appId.c_str());
90     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
91                       "setting process groups from app id returned wrong value."
92                           << " App id: " << appId << ";"
93                           << " Result: " << result << ";"
94                           << " Expected result: " << expectedResult);
95 }
96
97 void dropProcessPrivileges(lib_retcode expectedResult)
98 {
99     int result = security_manager_drop_process_privileges();
100     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
101                       "dropping process privileges returned wrong value."
102                           << " Result: " << result << ";"
103                           << " Expected result: " << expectedResult);
104 }
105
106 void prepareAppCandidate(lib_retcode expectedResult)
107 {
108     int result = security_manager_prepare_app_candidate();
109     RUNNER_ASSERT_MSG((lib_retcode) result == expectedResult,
110                       "preparing app candidate process returned wrong value."
111                           << " Result: " << result << ";"
112                           << " Expected result: " << expectedResult);
113 }
114
115 void prepareApp(const std::string &appId, lib_retcode expectedResult)
116 {
117     int result = security_manager_prepare_app(appId.c_str());
118     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
119                       "preparing app returned wrong value."
120                           << " App id: " << appId << ";"
121                           << " Result: " << result << ";"
122                           << " Expected result: " << expectedResult);
123 }
124
125 void cleanupApp(const std::string &appId, uid_t uid, pid_t pid, lib_retcode expectedResult)
126 {
127     int result = security_manager_cleanup_app(appId.c_str(), uid, pid);
128     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
129                       "cleanup app returned wrong value."
130                           << " App id: " << appId << ";"
131                           << " user id: " << uid << ";"
132                           << " process id: " << pid << ";"
133                           << " Result: " << result << ";"
134                           << " Expected result: " << expectedResult);
135 }
136
137 void addUser(const UserRequest &request, lib_retcode expectedResult)
138 {
139     int result = security_manager_user_add(request.get());
140     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
141                       "adding user returned wrong value."
142                           << " UserRequest: [ " << request << "];"
143                           << " Result: " << result << ";"
144                           << " Expected result: " << expectedResult);
145 }
146
147 void deleteUser(const UserRequest &request, lib_retcode expectedResult)
148 {
149     int result = security_manager_user_delete(request.get());
150     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
151                       "deleting user returned wrong value."
152                           << " UserRequest: [ " << request << "];"
153                           << " Result: " << result << ";"
154                           << " Expected result: " << expectedResult);
155 }
156
157 void sendPolicy(const PolicyRequest &request, lib_retcode expectedResult)
158 {
159     int result = security_manager_policy_update_send(request.get());
160     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
161                           "sending policy update for self returned wrong value."
162                           << " PolicyRequest: [ " << request << "];"
163                           << " Result: " << result << ";"
164                           << " Expected result: " << expectedResult);
165 }
166
167 void getConfiguredPolicy(const PolicyEntry &filter, std::vector<PolicyEntry> &policyEntries, lib_retcode expectedResult, bool forAdmin)
168 {
169     policy_entry **pp_privs_policy = NULL;
170     size_t policy_size = 0;
171     int result;
172
173     if (forAdmin) {
174         result = security_manager_get_configured_policy_for_admin(filter.get(), &pp_privs_policy, &policy_size);
175     } else {
176         result = security_manager_get_configured_policy_for_self(filter.get(), &pp_privs_policy, &policy_size);
177     };
178
179     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
180                       "Unexpected result for filter: " << filter << std::endl
181                           << " Result: " << result << ";");
182
183     for (unsigned int i = 0; i < policy_size; ++i) {
184         PolicyEntry pe(*pp_privs_policy[i]);
185         policyEntries.push_back(pe);
186     };
187 }
188
189 void getPolicy(const PolicyEntry &filter, std::vector<PolicyEntry> &policyEntries, lib_retcode expectedResult)
190 {
191     policy_entry **pp_privs_policy = NULL;
192     size_t policy_size = 0;
193     int result;
194
195     result = security_manager_get_policy(filter.get(), &pp_privs_policy, &policy_size);
196     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
197                       "Unexpected result" << std::endl
198                           << " Result: " << result << ";");
199     for (unsigned int i = 0; i < policy_size; ++i) {
200         PolicyEntry pe(*pp_privs_policy[i]);
201         policyEntries.push_back(pe);
202     };
203 }
204
205 void applySharing(const SharingRequest &req, lib_retcode expectedResult) {
206     int result = security_manager_private_sharing_apply(req.get());
207     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
208                       "Unexpected result from security_manager_private_sharing_apply" << std::endl
209                           << " Result: " << result << ";");
210 }
211
212 void dropSharing(const SharingRequest &req, lib_retcode expectedResult) {
213     int result = security_manager_private_sharing_drop(req.get());
214     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
215                       "Unexpected result from security_manager_private_sharing_drop" << std::endl
216                           << " Result: " << result << ";");
217 }
218
219 void getPolicyForSelf(const PolicyEntry &filter, std::vector<PolicyEntry> &policyEntries, lib_retcode expectedResult)
220 {
221     getConfiguredPolicy(filter, policyEntries, expectedResult, false);
222 }
223
224 void getPolicyForAdmin(const PolicyEntry &filter, std::vector<PolicyEntry> &policyEntries, lib_retcode expectedResult)
225 {
226     getConfiguredPolicy(filter, policyEntries, expectedResult, true);
227 }
228
229 void getPkgIdBySocket(int socketFd, std::string *pkgId, std::string *appId, lib_retcode expectedResult)
230 {
231     char *pkg_Id = nullptr;
232     char *app_Id = nullptr;
233
234     int result = security_manager_identify_app_from_socket(socketFd,
235                     pkgId == nullptr ? nullptr : &pkg_Id,
236                     appId == nullptr ? nullptr : &app_Id);
237
238     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
239                       "getting pkg id from socket returned wrong value."
240                           << " socket: " << socketFd << ";"
241                           << " Result: " << result << ";"
242                           << " Expected result: " << expectedResult);
243
244     if (pkg_Id) {
245         *pkgId = pkg_Id;
246         free(pkg_Id);
247     }
248
249     if (app_Id) {
250         *appId = app_Id;
251         free(app_Id);
252     }
253 }
254
255 void getPkgIdByPid(pid_t pid, std::string *pkgId, std::string *appId, lib_retcode expectedResult)
256 {
257     char *pkg_Id = nullptr;
258     char *app_Id = nullptr;
259
260     int result = security_manager_identify_app_from_pid(pid,
261                     pkgId == nullptr ? nullptr : &pkg_Id,
262                     appId == nullptr ? nullptr : &app_Id);
263
264     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
265                       "getting pkg id from pid returned wrong value."
266                           << " pid: " << pid << ";"
267                           << " Result: " << result << ";"
268                           << " Expected result: " << expectedResult);
269
270     if (pkg_Id) {
271         *pkgId = pkg_Id;
272         free(pkg_Id);
273     }
274
275     if (app_Id) {
276         *appId = app_Id;
277         free(app_Id);
278     }
279 }
280
281 void getPkgIdByCynaraClient(const std::string &client, std::string *pkgId, std::string *appId,
282                             lib_retcode expectedResult)
283 {
284     char *pkg_Id = nullptr;
285     char *app_Id = nullptr;
286
287     int result = security_manager_identify_app_from_cynara_client(client.c_str(),
288                     pkgId == nullptr ? nullptr : &pkg_Id,
289                     appId == nullptr ? nullptr : &app_Id);
290
291     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
292                       "getting pkg id from pid returned wrong value."
293                           << " client: " << client << ";"
294                           << " Result: " << result << ";"
295                           << " Expected result: " << expectedResult);
296
297     if (pkg_Id) {
298         *pkgId = pkg_Id;
299         free(pkg_Id);
300     }
301
302     if (app_Id) {
303         *appId = app_Id;
304         free(app_Id);
305     }
306 }
307
308 void appHasPrivilege(const std::string &appId, const std::string &privilege, uid_t user,
309                      int &value, lib_retcode expectedResult)
310 {
311     int result = security_manager_app_has_privilege(appId.c_str(), privilege.c_str(), user, &value);
312
313     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
314                       "checking application privilege returned wrong result."
315                           << " Result: " << result << ";"
316                           << " Expected result: " << expectedResult);
317 }
318
319 void getSecurityManagerGroups(gid_t **groups, size_t *groups_count, lib_retcode expectedResult)
320 {
321   int result = security_manager_groups_get(groups, groups_count);
322   RUNNER_ASSERT_MSG(static_cast<lib_retcode>(result) == expectedResult,
323                     "Unexpected result in security_manager_groups_get()" << std::endl
324                     << " Result: " << result << " Expected: " << expectedResult);
325 }
326
327 void registerPaths(const PathsRequest& req, lib_retcode expectedResult)
328 {
329     int result = security_manager_paths_register(req.get());
330     RUNNER_ASSERT_MSG(static_cast<lib_retcode>(result) == expectedResult,
331                       "Unexpected result in security_manager_paths_register()" << std::endl
332                       << " Result: " << result << " Expected: " << expectedResult);
333 }
334
335 void labelsMonitorGetFd(const LabelMonitor &monitor, int *fd, lib_retcode expectedResult)
336 {
337     int result = security_manager_app_labels_monitor_get_fd(monitor.get(), fd);
338     RUNNER_ASSERT_MSG(static_cast<lib_retcode>(result) == expectedResult,
339                       "Unexpected result in security_manager_app_labels_monitor_get_fd()"
340                           << std::endl << " Result: " << result << " Expected: "
341                           << expectedResult);
342 };
343
344 void labelsProcess(const LabelMonitor &monitor, lib_retcode expectedResult)
345 {
346     int result = security_manager_app_labels_monitor_process(monitor.get());
347     RUNNER_ASSERT_MSG(static_cast<lib_retcode>(result) == expectedResult,
348                       "Unexpected result in security_manager_app_labels_monitor_process()"
349                           << std::endl << " Result: " << result << " Expected: "
350                           << expectedResult);
351 }
352
353 } // namespace Api
354
355 } // namespace SecurityManagerTest