7db63b1f5d52e441954592f86bcbe5a2f886ce41
[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_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 std::ostream& operator<<(std::ostream &os, const InstallRequest &request)
110 {
111     if (request.m_appId != nullptr)
112         os << "app id: " << request.m_appId << "; ";
113     if (request.m_pkgId != nullptr)
114         os << "pkg id: " << request.m_pkgId << "; ";
115     if (!request.m_privileges.empty()) {
116         os << "privileges: [ " << request.m_privileges[0];
117         for (size_t i=1; i < request.m_privileges.size(); ++i) {
118             os << "; " << request.m_privileges[i];
119         }
120         os << " ]";
121     }
122     if (!request.m_paths.empty()) {
123         os << "paths: [ " << "< " << request.m_paths[0].first << "; "
124                                   << request.m_paths[0].second << " >";
125         for (size_t i=1; i < request.m_paths.size(); ++i) {
126             os << "; < " << request.m_paths[i].first << "; "
127                          << request.m_paths[i].second << " >";
128         }
129         os << " ]";
130     }
131     if (request.m_uid.first)
132         os << "uid: " << request.m_uid.second << "; ";
133     return os;
134 }
135
136 } // namespace SecurityManagerTest