2 * Copyright (c) 2014-2020 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");
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);
57 InstallRequest::~InstallRequest()
59 security_manager_app_inst_req_free(m_req);
62 void InstallRequest::setAppTizenVersion(std::string tizenVer, lib_retcode expectedResult)
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);
73 void InstallRequest::setAppId(std::string appId, lib_retcode expectedResult)
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);
83 void InstallRequest::setPkgId(std::string pkgId, lib_retcode expectedResult)
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);
94 void InstallRequest::addPrivilege(Privilege privilege, lib_retcode expectedResult)
96 int result = security_manager_app_inst_req_add_client_privilege(m_req,
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));
107 void InstallRequest::addAppDefinedPrivilege(Privilege privilege, lib_retcode expectedResult)
109 int result = security_manager_app_inst_req_add_app_defined_privilege(
112 privilege.getSMType(),
113 privilege.getLicenseC());
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));
123 void InstallRequest::addPath(std::string path, app_install_path_type pathType, lib_retcode expectedResult)
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));
135 void InstallRequest::setUid(const uid_t uid, lib_retcode expectedResult)
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);
147 void InstallRequest::setAuthorId(std::string authorId, lib_retcode expectedResult)
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);
158 void InstallRequest::setInstallType(const enum app_install_type &type, lib_retcode expectedResult)
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);
168 void InstallRequest::setHybrid(lib_retcode expectedResult)
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);
177 void InstallRequest::nextApp(lib_retcode expectedResult)
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);
186 std::ostream& operator<<(std::ostream &os, const InstallRequest &request)
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() << " >";
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() << " >";
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() << " >";
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 << " >";
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 << "; ";
230 PathsRequest::PathsRequest()
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");
240 PathsRequest::~PathsRequest()
242 security_manager_path_req_free(m_req);
245 void PathsRequest::setPkgId(std::string pkgId, lib_retcode expectedResult)
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);
256 void PathsRequest::addPath(std::string path, app_install_path_type pathType, lib_retcode expectedResult)
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));
268 void PathsRequest::setUid(const uid_t uid, lib_retcode expectedResult)
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);
280 void PathsRequest::setInstallType(const enum app_install_type &type, lib_retcode expectedResult)
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);
290 std::ostream& operator<<(std::ostream &os, const PathsRequest &request)
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 << " >";
303 if (request.m_uid.first)
304 os << "uid: " << request.m_uid.second << "; ";
308 } // namespace SecurityManagerTest