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