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