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