WAE: API changed due to changed installer cred
[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 std::ostream& operator<<(std::ostream &os, const InstallRequest &request)
119 {
120     if (!request.m_appId.empty())
121         os << "app id: " << request.m_appId << "; ";
122     if (!request.m_pkgId.empty())
123         os << "pkg id: " << request.m_pkgId << "; ";
124     if (!request.m_privileges.empty()) {
125         os << "privileges: [ " << request.m_privileges[0];
126         for (size_t i=1; i < request.m_privileges.size(); ++i) {
127             os << "; " << request.m_privileges[i];
128         }
129         os << " ]";
130     }
131     if (!request.m_paths.empty()) {
132         os << "paths: [ " << "< " << request.m_paths[0].first << "; "
133                                   << request.m_paths[0].second << " >";
134         for (size_t i=1; i < request.m_paths.size(); ++i) {
135             os << "; < " << request.m_paths[i].first << "; "
136                          << request.m_paths[i].second << " >";
137         }
138         os << " ]";
139     }
140     if (request.m_uid.first)
141         os << "uid: " << request.m_uid.second << "; ";
142     if (!request.m_authorId.empty()) {
143         os << "author id: " << request.m_authorId << "; ";
144     }
145     return os;
146 }
147
148 } // namespace SecurityManagerTest