debda17bdb00b037f02818fbd4802096b16f65bd
[platform/core/test/security-tests.git] / src / 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 bool PolicyEntry::operator==(const PolicyEntry &other) const
125 {
126     auto cmp = [](const std::pair<bool, std::string> &a, const std::pair<bool, std::string> &b)->bool
127     {
128         return (a.first) ? (b.first && a.second == b.second) : !b.first;
129     };
130
131     return (
132         cmp(m_appId, other.m_appId) &&
133         cmp(m_user, other.m_user) &&
134         cmp(m_privilege, other.m_privilege) &&
135         cmp(m_currentLevel, other.m_currentLevel) &&
136         cmp(m_maxLevel, other.m_maxLevel));
137 }
138
139 std::string PolicyEntry::toString() const
140 {
141     std::stringstream ss;
142     auto append = [&](const std::pair<bool, std::string> &x)
143     {
144         if (x.first)
145             ss << x.second;
146         ss << '\0';
147     };
148
149     append(m_appId);
150     append(m_user);
151     append(m_privilege);
152     append(m_currentLevel);
153     append(m_maxLevel);
154
155     return ss.str();
156 }
157
158 PolicyRequest::PolicyRequest()
159     : m_req(nullptr),
160       m_entries()
161 {
162     int result = security_manager_policy_update_req_new(&m_req);
163     RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
164                       "creation of new policy request failed. Result: " << result);
165     RUNNER_ASSERT_MSG(m_req != nullptr, "creation of new policy request did not allocate memory");
166 }
167
168 PolicyRequest::~PolicyRequest()
169 {
170     for(std::vector<PolicyEntry>::iterator it = m_entries.begin(); it != m_entries.end(); ++it) {
171         it->free();
172     }
173     security_manager_policy_update_req_free(m_req);
174 }
175
176 void PolicyRequest::addEntry(PolicyEntry &entry,
177         lib_retcode expectedResult)
178 {
179     int result = 0;
180
181     result = security_manager_policy_update_req_add_entry(m_req, entry.get());
182
183     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
184                      "adding policy entry to request returned wrong value."
185                           << " entry: " << entry << ";"
186                           << " Result: " << result << ";"
187                           << " Expected result: " << expectedResult);
188
189     m_entries.push_back(entry);
190 }
191
192 std::ostream& operator<<(std::ostream &os, const PolicyRequest &request)
193 {
194     if (request.m_entries.size() != 0)
195     {
196         os << "PolicyRequest m_entries size: " << request.m_entries.size() << "; ";
197
198         for(unsigned int i = 0; i != request.m_entries.size(); i++) {
199            os << "entry " << i << ": " << request.m_entries[i] << "; ";
200        }
201     }
202
203     return os;
204 }
205
206 } // namespace SecurityManagerTest