Replace magic policy level strings with constexpr
[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 constexpr char PolicyEntry::LEVEL_ALLOW[];
24 constexpr char PolicyEntry::LEVEL_DENY[];
25 constexpr char PolicyEntry::LEVEL_ASK_USER[];
26
27 PolicyEntry::PolicyEntry()
28     : m_appId(true, std::string(SECURITY_MANAGER_ANY))
29     , m_user(true, std::string(SECURITY_MANAGER_ANY))
30     , m_privilege(true, std::string(SECURITY_MANAGER_ANY))
31     , m_currentLevel(false, std::string(""))
32     , m_maxLevel(false, std::string(""))
33 {
34     int result = security_manager_policy_entry_new(&m_entry);
35     RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
36                       "creation of new policy entry failed. Result: " << result);
37     RUNNER_ASSERT_MSG(m_entry != nullptr, "creation of new policy entry did not allocate memory");
38
39     security_manager_policy_entry_set_application(m_entry, m_appId.second.c_str());
40     security_manager_policy_entry_set_user(m_entry, m_user.second.c_str());
41     security_manager_policy_entry_set_privilege(m_entry, m_privilege.second.c_str());
42 }
43
44 PolicyEntry::PolicyEntry(const std::string &appId, const std::string &user,
45         const std::string &privilege)
46     : m_appId(true, std::string(appId))
47     , m_user(true, std::string(user))
48     , m_privilege(true, std::string(privilege))
49     , m_currentLevel(false, std::string(""))
50     , m_maxLevel(false, std::string(""))
51 {
52     int result = security_manager_policy_entry_new(&m_entry);
53     RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
54                       "creation of new policy entry failed. Result: " << result);
55     RUNNER_ASSERT_MSG(m_entry != nullptr, "creation of new policy entry did not allocate memory");
56
57     security_manager_policy_entry_set_user(m_entry, m_user.second.c_str());
58     security_manager_policy_entry_set_application(m_entry, m_appId.second.c_str());
59     security_manager_policy_entry_set_privilege(m_entry, m_privilege.second.c_str());
60 }
61
62 PolicyEntry::PolicyEntry(policy_entry &entry): m_entry(&entry)
63 {
64     m_appId.first = true;
65     m_appId.second = std::string(security_manager_policy_entry_get_application(m_entry));
66
67     m_user.first = true;
68     m_user.second = std::string(security_manager_policy_entry_get_user(m_entry));
69
70     m_privilege.first = true;
71     m_privilege.second = std::string(security_manager_policy_entry_get_privilege(m_entry));
72
73     m_currentLevel.first = true;
74     m_currentLevel.second = std::string(security_manager_policy_entry_get_level(m_entry));
75
76     m_maxLevel.first = true;
77     m_maxLevel.second = std::string(security_manager_policy_entry_get_max_level(m_entry));
78 };
79
80 void PolicyEntry::setLevel(const std::string &level)
81 {
82     m_currentLevel.first = true;
83     m_currentLevel.second = level;
84     security_manager_policy_entry_set_level(m_entry, level.c_str());
85     m_maxLevel.first = true;
86     m_maxLevel.second = std::string(security_manager_policy_entry_get_max_level(m_entry));
87 };
88
89 void PolicyEntry::setMaxLevel(const std::string &level)
90 {
91     m_maxLevel.first = true;
92     m_maxLevel.second = level;
93     security_manager_policy_entry_admin_set_level(m_entry, level.c_str());
94     m_currentLevel.first = true;
95     m_currentLevel.second = std::string(security_manager_policy_entry_get_level(m_entry));
96 };
97
98
99 std::ostream& operator<<(std::ostream &os, const PolicyEntry &request)
100 {
101     if (request.m_appId.first)
102             os << "appId: " << request.m_appId.second << "; ";
103
104     if (request.m_user.first)
105             os << "user: " << request.m_user.second << "; ";
106
107     if (request.m_privilege.first)
108             os << "privilege: " << request.m_privilege.second << "; ";
109
110     if (request.m_currentLevel.first)
111             os << "current: " << request.m_currentLevel.second << "; ";
112
113     if (request.m_maxLevel.first)
114             os << "max: " << request.m_maxLevel.second << "; ";
115
116     return os;
117 }
118
119 PolicyEntry::~PolicyEntry()
120 {
121 }
122
123 void PolicyEntry::free(void)
124 {
125     security_manager_policy_entry_free(m_entry);
126 }
127
128 bool PolicyEntry::operator==(const PolicyEntry &other) const
129 {
130     auto cmp = [](const std::pair<bool, std::string> &a, const std::pair<bool, std::string> &b)->bool
131     {
132         return (a.first) ? (b.first && a.second == b.second) : !b.first;
133     };
134
135     return (
136         cmp(m_appId, other.m_appId) &&
137         cmp(m_user, other.m_user) &&
138         cmp(m_privilege, other.m_privilege) &&
139         cmp(m_currentLevel, other.m_currentLevel) &&
140         cmp(m_maxLevel, other.m_maxLevel));
141 }
142
143 std::string PolicyEntry::toString() const
144 {
145     std::stringstream ss;
146     auto append = [&](const std::pair<bool, std::string> &x)
147     {
148         if (x.first)
149             ss << x.second;
150         ss << '\0';
151     };
152
153     append(m_appId);
154     append(m_user);
155     append(m_privilege);
156     append(m_currentLevel);
157     append(m_maxLevel);
158
159     return ss.str();
160 }
161
162 PolicyRequest::PolicyRequest()
163     : m_req(nullptr),
164       m_entries()
165 {
166     int result = security_manager_policy_update_req_new(&m_req);
167     RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
168                       "creation of new policy request failed. Result: " << result);
169     RUNNER_ASSERT_MSG(m_req != nullptr, "creation of new policy request did not allocate memory");
170 }
171
172 PolicyRequest::~PolicyRequest()
173 {
174     for(std::vector<PolicyEntry>::iterator it = m_entries.begin(); it != m_entries.end(); ++it) {
175         it->free();
176     }
177     security_manager_policy_update_req_free(m_req);
178 }
179
180 void PolicyRequest::addEntry(PolicyEntry &entry,
181         lib_retcode expectedResult)
182 {
183     int result = 0;
184
185     result = security_manager_policy_update_req_add_entry(m_req, entry.get());
186
187     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
188                      "adding policy entry to request returned wrong value."
189                           << " entry: " << entry << ";"
190                           << " Result: " << result << ";"
191                           << " Expected result: " << expectedResult);
192
193     m_entries.push_back(entry);
194 }
195
196 std::ostream& operator<<(std::ostream &os, const PolicyRequest &request)
197 {
198     if (request.m_entries.size() != 0)
199     {
200         os << "PolicyRequest m_entries size: " << request.m_entries.size() << "; ";
201
202         for(unsigned int i = 0; i != request.m_entries.size(); i++) {
203            os << "entry " << i << ": " << request.m_entries[i] << "; ";
204        }
205     }
206
207     return os;
208 }
209
210 } // namespace SecurityManagerTest