Merge remote-tracking branch 'origin/tizen' into security-manager
[platform/core/test/security-tests.git] / src / security-manager-tests / common / sm_request.cpp
1 /*
2  * Copyright (c) 2014-2016 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 InstallRequest::InstallRequest()
24     : m_req(nullptr)
25     , m_tizenVer("3.0")
26     , m_uid(false, 0)
27 {
28     int result = security_manager_app_inst_req_new(&m_req);
29     RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
30                       "creation of new request failed. Result: " << result);
31     RUNNER_ASSERT_MSG(m_req != nullptr, "creation of new request did not allocate memory");
32 }
33
34 InstallRequest::~InstallRequest()
35 {
36     security_manager_app_inst_req_free(m_req);
37 }
38
39 void InstallRequest::setAppTizenVersion(std::string tizenVer, lib_retcode expectedResult)
40 {
41     int result = security_manager_app_inst_req_set_target_version(m_req, tizenVer.c_str());
42     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
43                       "setting app id returned wrong value."
44                           << " Tizen version: " << tizenVer << ";"
45                           << " Result: " << result << ";"
46                           << " Expected result: " << expectedResult);
47     m_tizenVer = std::move(tizenVer);
48 }
49
50 void InstallRequest::setAppId(std::string appId, lib_retcode expectedResult)
51 {
52     int result = security_manager_app_inst_req_set_app_id(m_req, appId.c_str());
53     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
54                       "setting app id returned wrong value."
55                           << " App id: " << appId << ";"
56                           << " Result: " << result << ";"
57                           << " Expected result: " << expectedResult);
58     m_appId = std::move(appId);
59 }
60
61 void InstallRequest::setPkgId(std::string pkgId, lib_retcode expectedResult)
62 {
63     int result = security_manager_app_inst_req_set_pkg_id(m_req, pkgId.c_str());
64     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
65                       "setting pkg id returned wrong value."
66                           << " Pkg id: " << pkgId << ";"
67                           << " Result: " << result << ";"
68                           << " Expected result: " << expectedResult);
69     m_pkgId = std::move(pkgId);
70 }
71
72 void InstallRequest::addPrivilege(const char *privilege, lib_retcode expectedResult)
73 {
74     int result = security_manager_app_inst_req_add_privilege(m_req, privilege);
75     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
76                       "adding privilege returned wrong value."
77                           << " Privilege: " << privilege << ";"
78                           << " Result: " << result << ";"
79                           << " Expected result: " << expectedResult);
80     m_privileges.push_back(strdup(privilege));
81 }
82
83 void InstallRequest::addPath(std::string path, app_install_path_type pathType, lib_retcode expectedResult)
84 {
85     int result = security_manager_app_inst_req_add_path(m_req, path.c_str(), pathType);
86     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
87                       "adding path returned wrong value."
88                           << " Path: " << path << ";"
89                           << " Path type: " << pathType << ";"
90                           << " Result: " << result << ";"
91                           << " Expected result: " << expectedResult);
92     m_paths.emplace_back(std::pair<std::string, app_install_path_type>(std::move(path), pathType));
93 }
94
95 void InstallRequest::setUid(const uid_t uid, lib_retcode expectedResult)
96 {
97     int result = security_manager_app_inst_req_set_uid(m_req, uid);
98     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
99                       "setting uid returned wrong value."
100                           << " Uid: " << uid << ";"
101                           << " Result: " << result << ";"
102                           << " Expected result: " << expectedResult);
103     m_uid.first = true;
104     m_uid.second = uid;
105 }
106
107 void InstallRequest::setAuthorId(std::string authorId, lib_retcode expectedResult)
108 {
109     int result = security_manager_app_inst_req_set_author_id(m_req, authorId.c_str());
110     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
111                       "setting author id returned wrong value."
112                           << " Author id: " << m_authorId << ";"
113                           << " Result: " << result << ";"
114                           << " Expected result: " << expectedResult);
115     m_authorId = std::move(authorId);
116 }
117
118 void InstallRequest::setInstallType(const enum app_install_type &type, lib_retcode expectedResult)
119 {
120     int result = security_manager_app_inst_req_set_install_type(m_req, type);
121     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
122                       "setting install type returned wrong value."
123                           << " Install type: " << type << ";"
124                           << " Result: " << result << ";"
125                           << " Expected result: " << expectedResult);
126 }
127
128 std::ostream& operator<<(std::ostream &os, const InstallRequest &request)
129 {
130     if (!request.m_appId.empty())
131         os << "app id: " << request.m_appId << "; ";
132     if (!request.m_pkgId.empty())
133         os << "pkg id: " << request.m_pkgId << "; ";
134     if (!request.m_privileges.empty()) {
135         os << "privileges: [ " << request.m_privileges[0];
136         for (size_t i=1; i < request.m_privileges.size(); ++i) {
137             os << "; " << request.m_privileges[i];
138         }
139         os << " ]";
140     }
141     if (!request.m_paths.empty()) {
142         os << "paths: [ " << "< " << request.m_paths[0].first << "; "
143                                   << request.m_paths[0].second << " >";
144         for (size_t i=1; i < request.m_paths.size(); ++i) {
145             os << "; < " << request.m_paths[i].first << "; "
146                          << request.m_paths[i].second << " >";
147         }
148         os << " ]";
149     }
150     if (request.m_uid.first)
151         os << "uid: " << request.m_uid.second << "; ";
152     if (!request.m_authorId.empty()) {
153         os << "author id: " << request.m_authorId << "; ";
154     }
155     return os;
156 }
157
158 PathsRequest::PathsRequest()
159     : m_req(nullptr)
160     , m_uid(false, 0)
161 {
162     int result = security_manager_path_req_new(&m_req);
163     RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
164                       "creation of new request failed. Result: " << result);
165     RUNNER_ASSERT_MSG(m_req != nullptr, "creation of new request did not allocate memory");
166 }
167
168 PathsRequest::~PathsRequest()
169 {
170     security_manager_path_req_free(m_req);
171 }
172
173 void PathsRequest::setPkgId(std::string pkgId, lib_retcode expectedResult)
174 {
175     int result = security_manager_path_req_set_pkg_id(m_req, pkgId.c_str());
176     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
177                       "setting pkg id returned wrong value."
178                           << " Pkg id: " << pkgId << ";"
179                           << " Result: " << result << ";"
180                           << " Expected result: " << expectedResult);
181     m_pkgId = std::move(pkgId);
182 }
183
184 void PathsRequest::addPath(std::string path, app_install_path_type pathType, lib_retcode expectedResult)
185 {
186     int result = security_manager_path_req_add_path(m_req, path.c_str(), pathType);
187     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
188                       "adding path returned wrong value."
189                           << " Path: " << path << ";"
190                           << " Path type: " << pathType << ";"
191                           << " Result: " << result << ";"
192                           << " Expected result: " << expectedResult);
193     m_paths.emplace_back(std::pair<std::string, app_install_path_type>(std::move(path), pathType));
194 }
195
196 void PathsRequest::setUid(const uid_t uid, lib_retcode expectedResult)
197 {
198     int result = security_manager_path_req_set_uid(m_req, uid);
199     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
200                       "setting uid returned wrong value."
201                           << " Uid: " << uid << ";"
202                           << " Result: " << result << ";"
203                           << " Expected result: " << expectedResult);
204     m_uid.first = true;
205     m_uid.second = uid;
206 }
207
208 void PathsRequest::setInstallType(const enum app_install_type &type, lib_retcode expectedResult)
209 {
210     int result = security_manager_path_req_set_install_type(m_req, type);
211     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
212                       "setting install type returned wrong value."
213                           << " Install type: " << type << ";"
214                           << " Result: " << result << ";"
215                           << " Expected result: " << expectedResult);
216 }
217
218 std::ostream& operator<<(std::ostream &os, const PathsRequest &request)
219 {
220     if (!request.m_pkgId.empty())
221         os << "pkg id: " << request.m_pkgId << "; ";
222     if (!request.m_paths.empty()) {
223         os << "paths: [ " << "< " << request.m_paths[0].first << "; "
224                                   << request.m_paths[0].second << " >";
225         for (size_t i=1; i < request.m_paths.size(); ++i) {
226             os << "; < " << request.m_paths[i].first << "; "
227                          << request.m_paths[i].second << " >";
228         }
229         os << " ]";
230     }
231     if (request.m_uid.first)
232         os << "uid: " << request.m_uid.second << "; ";
233     return os;
234 }
235
236 } // namespace SecurityManagerTest