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);
125 PolicyRequest::PolicyRequest()
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");
135 PolicyRequest::~PolicyRequest()
137 for(std::vector<PolicyEntry>::iterator it = m_entries.begin(); it != m_entries.end(); ++it) {
140 security_manager_policy_update_req_free(m_req);
143 void PolicyRequest::addEntry(PolicyEntry &entry,
144 lib_retcode expectedResult)
148 result = security_manager_policy_update_req_add_entry(m_req, entry.get());
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);
156 m_entries.push_back(entry);
159 std::ostream& operator<<(std::ostream &os, const PolicyRequest &request)
161 if (request.m_entries.size() != 0)
163 os << "PolicyRequest m_entries size: " << request.m_entries.size() << "; ";
165 for(unsigned int i = 0; i != request.m_entries.size(); i++) {
166 os << "entry " << i << ": " << request.m_entries[i] << "; ";
173 } // namespace SecurityManagerTest