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