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