2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <sm_policy_request.h>
19 #include <dpl/test/test_runner.h>
21 namespace SecurityManagerTest {
23 constexpr char PolicyEntry::LEVEL_ALLOW[];
24 constexpr char PolicyEntry::LEVEL_DENY[];
25 constexpr char PolicyEntry::LEVEL_ASK_USER[];
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(""))
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");
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());
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(""))
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");
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());
62 PolicyEntry::PolicyEntry(policy_entry &entry): m_entry(&entry)
65 m_appId.second = std::string(security_manager_policy_entry_get_application(m_entry));
68 m_user.second = std::string(security_manager_policy_entry_get_user(m_entry));
70 m_privilege.first = true;
71 m_privilege.second = std::string(security_manager_policy_entry_get_privilege(m_entry));
73 m_currentLevel.first = true;
74 m_currentLevel.second = std::string(security_manager_policy_entry_get_level(m_entry));
76 m_maxLevel.first = true;
77 m_maxLevel.second = std::string(security_manager_policy_entry_get_max_level(m_entry));
80 void PolicyEntry::setLevel(const std::string &level)
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));
89 void PolicyEntry::setMaxLevel(const std::string &level)
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));
99 std::ostream& operator<<(std::ostream &os, const PolicyEntry &request)
101 if (request.m_appId.first)
102 os << "appId: " << request.m_appId.second << "; ";
104 if (request.m_user.first)
105 os << "user: " << request.m_user.second << "; ";
107 if (request.m_privilege.first)
108 os << "privilege: " << request.m_privilege.second << "; ";
110 if (request.m_currentLevel.first)
111 os << "current: " << request.m_currentLevel.second << "; ";
113 if (request.m_maxLevel.first)
114 os << "max: " << request.m_maxLevel.second << "; ";
119 PolicyEntry::~PolicyEntry()
123 void PolicyEntry::free(void)
125 security_manager_policy_entry_free(m_entry);
128 bool PolicyEntry::operator==(const PolicyEntry &other) const
130 auto cmp = [](const std::pair<bool, std::string> &a, const std::pair<bool, std::string> &b)->bool
132 return (a.first) ? (b.first && a.second == b.second) : !b.first;
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));
143 std::string PolicyEntry::toString() const
145 std::stringstream ss;
146 auto append = [&](const std::pair<bool, std::string> &x)
156 append(m_currentLevel);
162 PolicyRequest::PolicyRequest()
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");
172 PolicyRequest::~PolicyRequest()
174 for(std::vector<PolicyEntry>::iterator it = m_entries.begin(); it != m_entries.end(); ++it) {
177 security_manager_policy_update_req_free(m_req);
180 void PolicyRequest::addEntry(PolicyEntry &entry,
181 lib_retcode expectedResult)
185 result = security_manager_policy_update_req_add_entry(m_req, entry.get());
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);
193 m_entries.push_back(entry);
196 std::ostream& operator<<(std::ostream &os, const PolicyRequest &request)
198 if (request.m_entries.size() != 0)
200 os << "PolicyRequest m_entries size: " << request.m_entries.size() << "; ";
202 for(unsigned int i = 0; i != request.m_entries.size(); i++) {
203 os << "entry " << i << ": " << request.m_entries[i] << "; ";
210 } // namespace SecurityManagerTest