b6fa1a1d6ccf95f95516111c7632f6d782d90a20
[platform/core/test/security-tests.git] / src / common / sm_request.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 <sm_request.h>
18
19 #include <dpl/test/test_runner.h>
20
21 namespace SecurityManagerTest {
22
23 void prepare_request(InstallRequest &request,
24                      const std::string &app_id,
25                      const std::string &pkg_id,
26                      app_install_path_type pathType,
27                      const std::string &path,
28                      uid_t uid)
29 {
30     request.setAppId(app_id);
31     request.setPkgId(pkg_id);
32     request.addPath(path, pathType);
33
34     if (uid != 0)
35         request.setUid(uid);
36 }
37
38 InstallRequest::InstallRequest()
39     : m_req(nullptr)
40     , m_tizenVer("3.0")
41     , m_uid(false, 0)
42 {
43     int result = security_manager_app_inst_req_new(&m_req);
44     RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
45                       "creation of new request failed. Result: " << result);
46     RUNNER_ASSERT_MSG(m_req != nullptr, "creation of new request did not allocate memory");
47 }
48
49 InstallRequest::~InstallRequest()
50 {
51     security_manager_app_inst_req_free(m_req);
52 }
53
54 void InstallRequest::setAppTizenVersion(std::string tizenVer, lib_retcode expectedResult)
55 {
56     int result = security_manager_app_inst_req_set_target_version(m_req, tizenVer.c_str());
57     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
58                       "setting app id returned wrong value."
59                           << " Tizen version: " << tizenVer << ";"
60                           << " Result: " << result << ";"
61                           << " Expected result: " << expectedResult);
62     m_tizenVer = std::move(tizenVer);
63 }
64
65 void InstallRequest::setAppId(std::string appId, lib_retcode expectedResult)
66 {
67     int result = security_manager_app_inst_req_set_app_id(m_req, appId.c_str());
68     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
69                       "setting app id returned wrong value."
70                           << " App id: " << appId << ";"
71                           << " Result: " << result << ";"
72                           << " Expected result: " << expectedResult);
73 }
74
75 void InstallRequest::setPkgId(std::string pkgId, lib_retcode expectedResult)
76 {
77     int result = security_manager_app_inst_req_set_pkg_id(m_req, pkgId.c_str());
78     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
79                       "setting pkg id returned wrong value."
80                           << " Pkg id: " << pkgId << ";"
81                           << " Result: " << result << ";"
82                           << " Expected result: " << expectedResult);
83     m_pkgId = std::move(pkgId);
84 }
85
86 void InstallRequest::addPrivilege(Privilege privilege, lib_retcode expectedResult)
87 {
88     int result = security_manager_app_inst_req_add_client_privilege(m_req,
89                                                                     privilege,
90                                                                     privilege.getLicenseC());
91     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
92                       "adding privilege returned wrong value."
93                           << " Privilege: " << privilege.getName() << ";"
94                           << " Result: " << result << ";"
95                           << " Expected result: " << expectedResult);
96     m_privileges.push_back(std::move(privilege));
97 }
98
99 void InstallRequest::addAppDefinedPrivilege(Privilege privilege, lib_retcode expectedResult)
100 {
101     int result = security_manager_app_inst_req_add_app_defined_privilege(
102                     m_req,
103                     privilege,
104                     privilege.getSMType(),
105                     privilege.getLicenseC());
106
107     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
108                       "adding app defined privilege returned wrong value."
109                           << " Privilege: " << privilege.getName() << ";"
110                           << " Result: " << result << ";"
111                           << " Expected result: " << expectedResult);
112     m_appDefinedPrivileges.push_back(std::move(privilege));
113 }
114
115 void InstallRequest::addPath(std::string path, app_install_path_type pathType, lib_retcode expectedResult)
116 {
117     int result = security_manager_app_inst_req_add_path(m_req, path.c_str(), pathType);
118     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
119                       "adding path returned wrong value."
120                           << " Path: " << path << ";"
121                           << " Path type: " << pathType << ";"
122                           << " Result: " << result << ";"
123                           << " Expected result: " << expectedResult);
124     m_paths.emplace_back(std::pair<std::string, app_install_path_type>(std::move(path), pathType));
125 }
126
127 void InstallRequest::setUid(const uid_t uid, lib_retcode expectedResult)
128 {
129     int result = security_manager_app_inst_req_set_uid(m_req, uid);
130     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
131                       "setting uid returned wrong value."
132                           << " Uid: " << uid << ";"
133                           << " Result: " << result << ";"
134                           << " Expected result: " << expectedResult);
135     m_uid.first = true;
136     m_uid.second = uid;
137 }
138
139 void InstallRequest::setAuthorId(std::string authorId, lib_retcode expectedResult)
140 {
141     int result = security_manager_app_inst_req_set_author_id(m_req, authorId.c_str());
142     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
143                       "setting author id returned wrong value."
144                           << " Author id: " << m_authorId << ";"
145                           << " Result: " << result << ";"
146                           << " Expected result: " << expectedResult);
147     m_authorId = std::move(authorId);
148 }
149
150 void InstallRequest::setInstallType(const enum app_install_type &type, lib_retcode expectedResult)
151 {
152     int result = security_manager_app_inst_req_set_install_type(m_req, type);
153     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
154                       "setting install type returned wrong value."
155                           << " Install type: " << type << ";"
156                           << " Result: " << result << ";"
157                           << " Expected result: " << expectedResult);
158 }
159
160 void InstallRequest::setHybrid(lib_retcode expectedResult)
161 {
162     int result = security_manager_app_inst_req_set_hybrid(m_req);
163     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
164                        "setting security_manager_app_inst_req_set_hybrid returned wrong value."
165                        << " Result: " << result << ";"
166                        << " Expected result: " << expectedResult);
167 }
168
169 void InstallRequest::nextApp(lib_retcode expectedResult)
170 {
171     int result = security_manager_app_inst_req_next(m_req);
172     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
173                         "security_manager_app_inst_req_next() returned wrong value."
174                         << " Result: " << result << ";"
175                         << " Expected result: " << expectedResult);
176 }
177
178 std::ostream& operator<<(std::ostream &os, const InstallRequest &request)
179 {
180     if (!request.m_pkgId.empty())
181         os << "pkg id: " << request.m_pkgId << "; ";
182     if (!request.m_privileges.empty()) {
183         os << "privileges: [ "  << "< " << request.m_privileges[0].getName() << "; "
184                                         << request.m_privileges[0].getLicense() << " >";
185         for (size_t i = 1; i < request.m_privileges.size(); ++i) {
186             os << "; <" << request.m_privileges[i].getName() << "; "
187                         << request.m_privileges[i].getLicense() << " >";
188         }
189         os << " ]";
190     }
191     if (!request.m_appDefinedPrivileges.empty()) {
192         os << "app defined privileges: [ "  << "< "
193            << request.m_appDefinedPrivileges[0].getName() << "; "
194            << request.m_appDefinedPrivileges[0].getType() << "; "
195            << request.m_appDefinedPrivileges[0].getLicense() << " >";
196
197         for (size_t i = 1; i < request.m_appDefinedPrivileges.size(); ++i) {
198             os << "; <" << request.m_appDefinedPrivileges[i].getName() << "; "
199                         << request.m_appDefinedPrivileges[i].getType() << "; "
200                         << request.m_appDefinedPrivileges[i].getLicense() << " >";
201         }
202         os << " ]";
203     }
204
205     if (!request.m_paths.empty()) {
206         os << "paths: [ " << "< " << request.m_paths[0].first << "; "
207                                   << request.m_paths[0].second << " >";
208         for (size_t i=1; i < request.m_paths.size(); ++i) {
209             os << "; < " << request.m_paths[i].first << "; "
210                          << request.m_paths[i].second << " >";
211         }
212         os << " ]";
213     }
214     if (request.m_uid.first)
215         os << "uid: " << request.m_uid.second << "; ";
216     if (!request.m_authorId.empty()) {
217         os << "author id: " << request.m_authorId << "; ";
218     }
219     return os;
220 }
221
222 PathsRequest::PathsRequest()
223     : m_req(nullptr)
224     , m_uid(false, 0)
225 {
226     int result = security_manager_path_req_new(&m_req);
227     RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
228                       "creation of new request failed. Result: " << result);
229     RUNNER_ASSERT_MSG(m_req != nullptr, "creation of new request did not allocate memory");
230 }
231
232 PathsRequest::~PathsRequest()
233 {
234     security_manager_path_req_free(m_req);
235 }
236
237 void PathsRequest::setPkgId(std::string pkgId, lib_retcode expectedResult)
238 {
239     int result = security_manager_path_req_set_pkg_id(m_req, pkgId.c_str());
240     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
241                       "setting pkg id returned wrong value."
242                           << " Pkg id: " << pkgId << ";"
243                           << " Result: " << result << ";"
244                           << " Expected result: " << expectedResult);
245     m_pkgId = std::move(pkgId);
246 }
247
248 void PathsRequest::addPath(std::string path, app_install_path_type pathType, lib_retcode expectedResult)
249 {
250     int result = security_manager_path_req_add_path(m_req, path.c_str(), pathType);
251     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
252                       "adding path returned wrong value."
253                           << " Path: " << path << ";"
254                           << " Path type: " << pathType << ";"
255                           << " Result: " << result << ";"
256                           << " Expected result: " << expectedResult);
257     m_paths.emplace_back(std::pair<std::string, app_install_path_type>(std::move(path), pathType));
258 }
259
260 void PathsRequest::setUid(const uid_t uid, lib_retcode expectedResult)
261 {
262     int result = security_manager_path_req_set_uid(m_req, uid);
263     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
264                       "setting uid returned wrong value."
265                           << " Uid: " << uid << ";"
266                           << " Result: " << result << ";"
267                           << " Expected result: " << expectedResult);
268     m_uid.first = true;
269     m_uid.second = uid;
270 }
271
272 void PathsRequest::setInstallType(const enum app_install_type &type, lib_retcode expectedResult)
273 {
274     int result = security_manager_path_req_set_install_type(m_req, type);
275     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
276                       "setting install type returned wrong value."
277                           << " Install type: " << type << ";"
278                           << " Result: " << result << ";"
279                           << " Expected result: " << expectedResult);
280 }
281
282 std::ostream& operator<<(std::ostream &os, const PathsRequest &request)
283 {
284     if (!request.m_pkgId.empty())
285         os << "pkg id: " << request.m_pkgId << "; ";
286     if (!request.m_paths.empty()) {
287         os << "paths: [ " << "< " << request.m_paths[0].first << "; "
288                                   << request.m_paths[0].second << " >";
289         for (size_t i=1; i < request.m_paths.size(); ++i) {
290             os << "; < " << request.m_paths[i].first << "; "
291                          << request.m_paths[i].second << " >";
292         }
293         os << " ]";
294     }
295     if (request.m_uid.first)
296         os << "uid: " << request.m_uid.second << "; ";
297     return os;
298 }
299
300 } // namespace SecurityManagerTest