Merge branch 'tizen' into security-manager
[platform/core/test/security-tests.git] / src / security-manager-tests / common / sm_request.cpp
1 /*
2  * Copyright (c) 2014 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_appId(nullptr)
26     , m_pkgId(nullptr)
27     , m_uid(false, 0)
28 {
29     int result = security_manager_app_inst_req_new(&m_req);
30     RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
31                       "creation of new request failed. Result: " << result);
32     RUNNER_ASSERT_MSG(m_req != nullptr, "creation of new request did not allocate memory");
33 }
34
35 InstallRequest::~InstallRequest()
36 {
37     security_manager_app_inst_req_free(m_req);
38 }
39
40 void InstallRequest::setAppId(const char *appId, lib_retcode expectedResult)
41 {
42     int result = security_manager_app_inst_req_set_app_id(m_req, appId);
43     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
44                       "setting app id returned wrong value."
45                           << " App id: " << appId << ";"
46                           << " Result: " << result << ";"
47                           << " Expected result: " << expectedResult);
48     m_appId = appId;
49 }
50
51 void InstallRequest::setPkgId(const char *pkgId, lib_retcode expectedResult)
52 {
53     int result = security_manager_app_inst_req_set_pkg_id(m_req, pkgId);
54     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
55                       "setting pkg id returned wrong value."
56                           << " Pkg id: " << pkgId << ";"
57                           << " Result: " << result << ";"
58                           << " Expected result: " << expectedResult);
59     m_pkgId = pkgId;
60 }
61
62 void InstallRequest::addPrivilege(const char *privilege, lib_retcode expectedResult)
63 {
64     int result = security_manager_app_inst_req_add_privilege(m_req, privilege);
65     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
66                       "adding privilege returned wrong value."
67                           << " Privilege: " << privilege << ";"
68                           << " Result: " << result << ";"
69                           << " Expected result: " << expectedResult);
70     m_privileges.push_back(privilege);
71 }
72
73 void InstallRequest::addPath(const char *path, app_install_path_type pathType, lib_retcode expectedResult)
74 {
75     int result = security_manager_app_inst_req_add_path(m_req, path, pathType);
76     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
77                       "adding path returned wrong value."
78                           << " Path: " << path << ";"
79                           << " Path type: " << pathType << ";"
80                           << " Result: " << result << ";"
81                           << " Expected result: " << expectedResult);
82     m_paths.push_back(std::pair<std::string, app_install_path_type>(path, pathType));
83 }
84
85 void InstallRequest::setUid(const uid_t uid, lib_retcode expectedResult)
86 {
87     int result = security_manager_app_inst_req_set_uid(m_req, uid);
88     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
89                       "setting uid returned wrong value."
90                           << " Uid: " << uid << ";"
91                           << " Result: " << result << ";"
92                           << " Expected result: " << expectedResult);
93     m_uid.first = true;
94     m_uid.second = uid;
95 }
96
97 std::ostream& operator<<(std::ostream &os, const InstallRequest &request)
98 {
99     if (request.m_appId != nullptr)
100         os << "app id: " << request.m_appId << "; ";
101     if (request.m_pkgId != nullptr)
102         os << "pkg id: " << request.m_pkgId << "; ";
103     if (!request.m_privileges.empty()) {
104         os << "privileges: [ " << request.m_privileges[0];
105         for (size_t i=1; i < request.m_privileges.size(); ++i) {
106             os << "; " << request.m_privileges[i];
107         }
108         os << " ]";
109     }
110     if (!request.m_paths.empty()) {
111         os << "paths: [ " << "< " << request.m_paths[0].first << "; "
112                                   << request.m_paths[0].second << " >";
113         for (size_t i=1; i < request.m_paths.size(); ++i) {
114             os << "; < " << request.m_paths[i].first << "; "
115                          << request.m_paths[i].second << " >";
116         }
117         os << " ]";
118     }
119     if (request.m_uid.first)
120         os << "uid: " << request.m_uid.second << "; ";
121     return os;
122 }
123
124 } // namespace SecurityManagerTest