Fix security_manager_02_app_install_uninstall_full test
[platform/core/test/security-tests.git] / tests / security-manager-tests / common / sm_policy_request.cpp
1 /*
2  * Copyright (c) 2015 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_policy_request.h>
18
19 #include <dpl/test/test_runner.h>
20
21 namespace SecurityManagerTest {
22
23 PolicyEntry::PolicyEntry()
24     : m_appId(true, std::string(SECURITY_MANAGER_ANY))
25     , m_user(true, std::string(SECURITY_MANAGER_ANY))
26     , m_privilege(true, std::string(SECURITY_MANAGER_ANY))
27     , m_currentLevel(false, std::string(""))
28     , m_maxLevel(false, std::string(""))
29 {
30     int result = security_manager_policy_entry_new(&m_entry);
31     RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
32                       "creation of new policy entry failed. Result: " << result);
33     RUNNER_ASSERT_MSG(m_entry != nullptr, "creation of new policy entry did not allocate memory");
34
35     security_manager_policy_entry_set_application(m_entry, m_appId.second.c_str());
36     security_manager_policy_entry_set_user(m_entry, m_user.second.c_str());
37     security_manager_policy_entry_set_privilege(m_entry, m_privilege.second.c_str());
38 }
39
40 PolicyEntry::PolicyEntry(const std::string &appId, const std::string &user,
41         const std::string &privilege)
42     : m_appId(true, std::string(appId))
43     , m_user(true, std::string(user))
44     , m_privilege(true, std::string(privilege))
45     , m_currentLevel(false, std::string(""))
46     , m_maxLevel(false, std::string(""))
47 {
48     int result = security_manager_policy_entry_new(&m_entry);
49     RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
50                       "creation of new policy entry failed. Result: " << result);
51     RUNNER_ASSERT_MSG(m_entry != nullptr, "creation of new policy entry did not allocate memory");
52
53     security_manager_policy_entry_set_user(m_entry, m_user.second.c_str());
54     security_manager_policy_entry_set_application(m_entry, m_appId.second.c_str());
55     security_manager_policy_entry_set_privilege(m_entry, m_privilege.second.c_str());
56 }
57
58 PolicyEntry::PolicyEntry(policy_entry &entry): m_entry(&entry)
59 {
60     m_appId.first = true;
61     m_appId.second = std::string(security_manager_policy_entry_get_application(m_entry));
62
63     m_user.first = true;
64     m_user.second = std::string(security_manager_policy_entry_get_user(m_entry));
65
66     m_privilege.first = true;
67     m_privilege.second = std::string(security_manager_policy_entry_get_privilege(m_entry));
68
69     m_currentLevel.first = true;
70     m_currentLevel.second = std::string(security_manager_policy_entry_get_level(m_entry));
71
72     m_maxLevel.first = true;
73     m_maxLevel.second = std::string(security_manager_policy_entry_get_max_level(m_entry));
74 };
75
76 void PolicyEntry::setLevel(const std::string &level)
77 {
78     m_currentLevel.first = true;
79     m_currentLevel.second = level;
80     security_manager_policy_entry_set_level(m_entry, level.c_str());
81     m_maxLevel.first = true;
82     m_maxLevel.second = std::string(security_manager_policy_entry_get_max_level(m_entry));
83 };
84
85 void PolicyEntry::setMaxLevel(const std::string &level)
86 {
87     m_maxLevel.first = true;
88     m_maxLevel.second = level;
89     security_manager_policy_entry_admin_set_level(m_entry, level.c_str());
90     m_currentLevel.first = true;
91     m_currentLevel.second = std::string(security_manager_policy_entry_get_level(m_entry));
92 };
93
94
95 std::ostream& operator<<(std::ostream &os, const PolicyEntry &request)
96 {
97     if (request.m_appId.first)
98             os << "appId: " << request.m_appId.second << "; ";
99
100     if (request.m_user.first)
101             os << "user: " << request.m_user.second << "; ";
102
103     if (request.m_privilege.first)
104             os << "privilege: " << request.m_privilege.second << "; ";
105
106     if (request.m_currentLevel.first)
107             os << "current: " << request.m_currentLevel.second << "; ";
108
109     if (request.m_maxLevel.first)
110             os << "max: " << request.m_maxLevel.second << "; ";
111
112     return os;
113 }
114
115 PolicyEntry::~PolicyEntry()
116 {
117 }
118
119 void PolicyEntry::free(void)
120 {
121     security_manager_policy_entry_free(m_entry);
122 }
123
124
125 PolicyRequest::PolicyRequest()
126     : m_req(nullptr),
127       m_entries()
128 {
129     int result = security_manager_policy_update_req_new(&m_req);
130     RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
131                       "creation of new policy request failed. Result: " << result);
132     RUNNER_ASSERT_MSG(m_req != nullptr, "creation of new policy request did not allocate memory");
133 }
134
135 PolicyRequest::~PolicyRequest()
136 {
137     for(std::vector<PolicyEntry>::iterator it = m_entries.begin(); it != m_entries.end(); ++it) {
138         it->free();
139     }
140     security_manager_policy_update_req_free(m_req);
141 }
142
143 void PolicyRequest::addEntry(PolicyEntry &entry,
144         lib_retcode expectedResult)
145 {
146     int result = 0;
147
148     result = security_manager_policy_update_req_add_entry(m_req, entry.get());
149
150     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
151                      "adding policy entry to request returned wrong value."
152                           << " entry: " << entry << ";"
153                           << " Result: " << result << ";"
154                           << " Expected result: " << expectedResult);
155
156     m_entries.push_back(entry);
157 }
158
159 std::ostream& operator<<(std::ostream &os, const PolicyRequest &request)
160 {
161     if (request.m_entries.size() != 0)
162     {
163         os << "PolicyRequest m_entries size: " << request.m_entries.size() << "; ";
164
165         for(unsigned int i = 0; i != request.m_entries.size(); i++) {
166            os << "entry " << i << ": " << request.m_entries[i] << "; ";
167        }
168     }
169
170     return os;
171 }
172
173 } // namespace SecurityManagerTest