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