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 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(""))
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");
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());
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(""))
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");
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());
58 PolicyEntry::PolicyEntry(policy_entry &entry): m_entry(&entry)
61 m_appId.second = std::string(security_manager_policy_entry_get_application(m_entry));
64 m_user.second = std::string(security_manager_policy_entry_get_user(m_entry));
66 m_privilege.first = true;
67 m_privilege.second = std::string(security_manager_policy_entry_get_privilege(m_entry));
69 m_currentLevel.first = true;
70 m_currentLevel.second = std::string(security_manager_policy_entry_get_level(m_entry));
72 m_maxLevel.first = true;
73 m_maxLevel.second = std::string(security_manager_policy_entry_get_max_level(m_entry));
76 void PolicyEntry::setLevel(const std::string &level)
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));
85 void PolicyEntry::setMaxLevel(const std::string &level)
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));
95 std::ostream& operator<<(std::ostream &os, const PolicyEntry &request)
97 if (request.m_appId.first)
98 os << "appId: " << request.m_appId.second << "; ";
100 if (request.m_user.first)
101 os << "user: " << request.m_user.second << "; ";
103 if (request.m_privilege.first)
104 os << "privilege: " << request.m_privilege.second << "; ";
106 if (request.m_currentLevel.first)
107 os << "current: " << request.m_currentLevel.second << "; ";
109 if (request.m_maxLevel.first)
110 os << "max: " << request.m_maxLevel.second << "; ";
115 PolicyEntry::~PolicyEntry()
119 void PolicyEntry::free(void)
121 security_manager_policy_entry_free(m_entry);
124 bool PolicyEntry::operator==(const PolicyEntry &other) const
126 auto cmp = [](const std::pair<bool, std::string> &a, const std::pair<bool, std::string> &b)->bool
128 return (a.first) ? (b.first && a.second == b.second) : !b.first;
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));
139 std::string PolicyEntry::toString() const
141 std::stringstream ss;
142 auto append = [&](const std::pair<bool, std::string> &x)
152 append(m_currentLevel);
158 PolicyRequest::PolicyRequest()
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");
168 PolicyRequest::~PolicyRequest()
170 for(std::vector<PolicyEntry>::iterator it = m_entries.begin(); it != m_entries.end(); ++it) {
173 security_manager_policy_update_req_free(m_req);
176 void PolicyRequest::addEntry(PolicyEntry &entry,
177 lib_retcode expectedResult)
181 result = security_manager_policy_update_req_add_entry(m_req, entry.get());
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);
189 m_entries.push_back(entry);
192 std::ostream& operator<<(std::ostream &os, const PolicyRequest &request)
194 if (request.m_entries.size() != 0)
196 os << "PolicyRequest m_entries size: " << request.m_entries.size() << "; ";
198 for(unsigned int i = 0; i != request.m_entries.size(); i++) {
199 os << "entry " << i << ": " << request.m_entries[i] << "; ";
206 } // namespace SecurityManagerTest