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