2 * Copyright (c) 2014-2019 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <sm_request.h>
19 #include <dpl/test/test_runner.h>
21 namespace SecurityManagerTest {
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,
30 request.setAppId(app_id);
31 request.setPkgId(pkg_id);
32 request.addPath(path, pathType);
38 InstallRequest::InstallRequest()
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");
49 InstallRequest::~InstallRequest()
51 security_manager_app_inst_req_free(m_req);
54 void InstallRequest::setAppTizenVersion(std::string tizenVer, lib_retcode expectedResult)
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);
65 void InstallRequest::setAppId(std::string appId, lib_retcode expectedResult)
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);
75 void InstallRequest::setPkgId(std::string pkgId, lib_retcode expectedResult)
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);
86 void InstallRequest::addPrivilege(Privilege privilege, lib_retcode expectedResult)
88 int result = security_manager_app_inst_req_add_client_privilege(m_req,
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));
99 void InstallRequest::addAppDefinedPrivilege(Privilege privilege, lib_retcode expectedResult)
101 int result = security_manager_app_inst_req_add_app_defined_privilege(
104 privilege.getSMType(),
105 privilege.getLicenseC());
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));
115 void InstallRequest::addPath(std::string path, app_install_path_type pathType, lib_retcode expectedResult)
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));
127 void InstallRequest::setUid(const uid_t uid, lib_retcode expectedResult)
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);
139 void InstallRequest::setAuthorId(std::string authorId, lib_retcode expectedResult)
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);
150 void InstallRequest::setInstallType(const enum app_install_type &type, lib_retcode expectedResult)
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);
160 void InstallRequest::setHybrid(lib_retcode expectedResult)
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);
169 void InstallRequest::nextApp(lib_retcode expectedResult)
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);
178 std::ostream& operator<<(std::ostream &os, const InstallRequest &request)
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() << " >";
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() << " >";
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() << " >";
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 << " >";
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 << "; ";
222 PathsRequest::PathsRequest()
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");
232 PathsRequest::~PathsRequest()
234 security_manager_path_req_free(m_req);
237 void PathsRequest::setPkgId(std::string pkgId, lib_retcode expectedResult)
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);
248 void PathsRequest::addPath(std::string path, app_install_path_type pathType, lib_retcode expectedResult)
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));
260 void PathsRequest::setUid(const uid_t uid, lib_retcode expectedResult)
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);
272 void PathsRequest::setInstallType(const enum app_install_type &type, lib_retcode expectedResult)
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);
282 std::ostream& operator<<(std::ostream &os, const PathsRequest &request)
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 << " >";
295 if (request.m_uid.first)
296 os << "uid: " << request.m_uid.second << "; ";
300 } // namespace SecurityManagerTest