65087af6c846467642e4c58222781a4235bab484
[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_appId(nullptr)
27     , m_pkgId(nullptr)
28     , m_uid(false, 0)
29 {
30     int result = security_manager_app_inst_req_new(&m_req);
31     RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
32                       "creation of new request failed. Result: " << result);
33     RUNNER_ASSERT_MSG(m_req != nullptr, "creation of new request did not allocate memory");
34 }
35
36 InstallRequest::~InstallRequest()
37 {
38     security_manager_app_inst_req_free(m_req);
39 }
40
41 void InstallRequest::setAppTizenVersion(const char * tizenVer, lib_retcode expectedResult)
42 {
43     int result = security_manager_app_inst_req_set_target_version(m_req, tizenVer);
44     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
45                       "setting app id returned wrong value."
46                           << " Tizen version: " << tizenVer << ";"
47                           << " Result: " << result << ";"
48                           << " Expected result: " << expectedResult);
49     m_tizenVer = std::string(tizenVer);
50 }
51
52 void InstallRequest::setAppId(const char *appId, lib_retcode expectedResult)
53 {
54     int result = security_manager_app_inst_req_set_app_id(m_req, appId);
55     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
56                       "setting app id returned wrong value."
57                           << " App id: " << appId << ";"
58                           << " Result: " << result << ";"
59                           << " Expected result: " << expectedResult);
60     m_appId = appId;
61 }
62
63 void InstallRequest::setPkgId(const char *pkgId, lib_retcode expectedResult)
64 {
65     int result = security_manager_app_inst_req_set_pkg_id(m_req, pkgId);
66     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
67                       "setting pkg id returned wrong value."
68                           << " Pkg id: " << pkgId << ";"
69                           << " Result: " << result << ";"
70                           << " Expected result: " << expectedResult);
71     m_pkgId = pkgId;
72 }
73
74 void InstallRequest::addPrivilege(const char *privilege, lib_retcode expectedResult)
75 {
76     int result = security_manager_app_inst_req_add_privilege(m_req, privilege);
77     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
78                       "adding privilege returned wrong value."
79                           << " Privilege: " << privilege << ";"
80                           << " Result: " << result << ";"
81                           << " Expected result: " << expectedResult);
82     m_privileges.push_back(privilege);
83 }
84
85 void InstallRequest::addPath(const char *path, app_install_path_type pathType, lib_retcode expectedResult)
86 {
87     int result = security_manager_app_inst_req_add_path(m_req, path, pathType);
88     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
89                       "adding path returned wrong value."
90                           << " Path: " << path << ";"
91                           << " Path type: " << pathType << ";"
92                           << " Result: " << result << ";"
93                           << " Expected result: " << expectedResult);
94     m_paths.push_back(std::pair<std::string, app_install_path_type>(path, pathType));
95 }
96
97 void InstallRequest::setUid(const uid_t uid, lib_retcode expectedResult)
98 {
99     int result = security_manager_app_inst_req_set_uid(m_req, uid);
100     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
101                       "setting uid returned wrong value."
102                           << " Uid: " << uid << ";"
103                           << " Result: " << result << ";"
104                           << " Expected result: " << expectedResult);
105     m_uid.first = true;
106     m_uid.second = uid;
107 }
108
109 void InstallRequest::setAuthorId(const char *authorId, lib_retcode expectedResult)
110 {
111     m_authorId.clear();
112     if(authorId)
113         m_authorId.assign(authorId);
114
115     int result = security_manager_app_inst_req_set_author_id(m_req, authorId);
116     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
117                       "setting author id returned wrong value."
118                           << " Author id: " << m_authorId << ";"
119                           << " Result: " << result << ";"
120                           << " Expected result: " << expectedResult);
121
122 }
123
124 std::ostream& operator<<(std::ostream &os, const InstallRequest &request)
125 {
126     if (request.m_appId != nullptr)
127         os << "app id: " << request.m_appId << "; ";
128     if (request.m_pkgId != nullptr)
129         os << "pkg id: " << request.m_pkgId << "; ";
130     if (!request.m_privileges.empty()) {
131         os << "privileges: [ " << request.m_privileges[0];
132         for (size_t i=1; i < request.m_privileges.size(); ++i) {
133             os << "; " << request.m_privileges[i];
134         }
135         os << " ]";
136     }
137     if (!request.m_paths.empty()) {
138         os << "paths: [ " << "< " << request.m_paths[0].first << "; "
139                                   << request.m_paths[0].second << " >";
140         for (size_t i=1; i < request.m_paths.size(); ++i) {
141             os << "; < " << request.m_paths[i].first << "; "
142                          << request.m_paths[i].second << " >";
143         }
144         os << " ]";
145     }
146     if (request.m_uid.first)
147         os << "uid: " << request.m_uid.second << "; ";
148     if (!request.m_authorId.empty()) {
149         os << "author id: " << request.m_authorId << "; ";
150     }
151     return os;
152 }
153
154 } // namespace SecurityManagerTest