From 6d71401279adbcc5b29f5e7a7f35ebd0b3d24183 Mon Sep 17 00:00:00 2001 From: Michal Eljasiewicz Date: Fri, 30 Jan 2015 13:45:28 +0100 Subject: [PATCH 1/1] Add PolicyRequest class for testing updating policies API Change-Id: I7857d01b0fe8091ba0b55e2cd9e5d503eca07221 Signed-off-by: Michal Eljasiewicz Signed-off-by: Krzysztof Sasiak --- tests/security-manager-tests/CMakeLists.txt | 1 + .../common/sm_policy_request.cpp | 173 +++++++++++++++++++++ .../common/sm_policy_request.h | 87 +++++++++++ 3 files changed, 261 insertions(+) create mode 100644 tests/security-manager-tests/common/sm_policy_request.cpp create mode 100644 tests/security-manager-tests/common/sm_policy_request.h diff --git a/tests/security-manager-tests/CMakeLists.txt b/tests/security-manager-tests/CMakeLists.txt index eb3ed39..e0254c6 100644 --- a/tests/security-manager-tests/CMakeLists.txt +++ b/tests/security-manager-tests/CMakeLists.txt @@ -42,6 +42,7 @@ SET(SEC_MGR_SOURCES ${PROJECT_SOURCE_DIR}/tests/security-manager-tests/common/sm_db.cpp ${PROJECT_SOURCE_DIR}/tests/security-manager-tests/common/sm_request.cpp ${PROJECT_SOURCE_DIR}/tests/security-manager-tests/common/sm_user_request.cpp + ${PROJECT_SOURCE_DIR}/tests/security-manager-tests/common/sm_policy_request.cpp ${PROJECT_SOURCE_DIR}/tests/cynara-tests/common/cynara_test_client.cpp ${PROJECT_SOURCE_DIR}/tests/libprivilege-control-tests/libprivilege-control_test_common.cpp ) diff --git a/tests/security-manager-tests/common/sm_policy_request.cpp b/tests/security-manager-tests/common/sm_policy_request.cpp new file mode 100644 index 0000000..043b8d1 --- /dev/null +++ b/tests/security-manager-tests/common/sm_policy_request.cpp @@ -0,0 +1,173 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include + +namespace SecurityManagerTest { + +PolicyEntry::PolicyEntry() + : m_appId(true, std::string(SECURITY_MANAGER_ANY)) + , m_user(true, std::string(SECURITY_MANAGER_ANY)) + , m_privilege(true, std::string(SECURITY_MANAGER_ANY)) + , m_currentLevel(false, std::string("")) + , m_maxLevel(false, std::string("")) +{ + int result = security_manager_policy_entry_new(&m_entry); + RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS, + "creation of new policy entry failed. Result: " << result); + RUNNER_ASSERT_MSG(m_entry != nullptr, "creation of new policy entry did not allocate memory"); + + security_manager_policy_entry_set_application(m_entry, m_appId.second.c_str()); + security_manager_policy_entry_set_user(m_entry, m_user.second.c_str()); + security_manager_policy_entry_set_privilege(m_entry, m_privilege.second.c_str()); +} + +PolicyEntry::PolicyEntry(const std::string &appId, const std::string &user, + const std::string &privilege) + : m_appId(true, std::string(appId)) + , m_user(true, std::string(user)) + , m_privilege(true, std::string(privilege)) + , m_currentLevel(false, std::string("")) + , m_maxLevel(false, std::string("")) +{ + int result = security_manager_policy_entry_new(&m_entry); + RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS, + "creation of new policy entry failed. Result: " << result); + RUNNER_ASSERT_MSG(m_entry != nullptr, "creation of new policy entry did not allocate memory"); + + security_manager_policy_entry_set_user(m_entry, m_user.second.c_str()); + security_manager_policy_entry_set_application(m_entry, m_appId.second.c_str()); + security_manager_policy_entry_set_privilege(m_entry, m_privilege.second.c_str()); +} + +PolicyEntry::PolicyEntry(policy_entry &entry): m_entry(&entry) +{ + m_appId.first = true; + m_appId.second = std::string(security_manager_policy_entry_get_application(m_entry)); + + m_user.first = true; + m_user.second = std::string(security_manager_policy_entry_get_user(m_entry)); + + m_privilege.first = true; + m_privilege.second = std::string(security_manager_policy_entry_get_privilege(m_entry)); + + m_currentLevel.first = true; + m_currentLevel.second = std::string(security_manager_policy_entry_get_level(m_entry)); + + m_maxLevel.first = true; + m_maxLevel.second = std::string(security_manager_policy_entry_get_max_level(m_entry)); +}; + +void PolicyEntry::setLevel(const std::string &level) +{ + m_currentLevel.first = true; + m_currentLevel.second = level; + security_manager_policy_entry_set_level(m_entry, level.c_str()); + m_maxLevel.first = true; + m_maxLevel.second = std::string(security_manager_policy_entry_get_max_level(m_entry)); +}; + +void PolicyEntry::setMaxLevel(const std::string &level) +{ + m_maxLevel.first = true; + m_maxLevel.second = level; + security_manager_policy_entry_admin_set_level(m_entry, level.c_str()); + m_currentLevel.first = true; + m_currentLevel.second = std::string(security_manager_policy_entry_get_level(m_entry)); +}; + + +std::ostream& operator<<(std::ostream &os, const PolicyEntry &request) +{ + if (request.m_appId.first) + os << "appId: " << request.m_appId.second << "; "; + + if (request.m_user.first) + os << "user: " << request.m_user.second << "; "; + + if (request.m_privilege.first) + os << "privilege: " << request.m_privilege.second << "; "; + + if (request.m_currentLevel.first) + os << "current: " << request.m_currentLevel.second << "; "; + + if (request.m_maxLevel.first) + os << "max: " << request.m_maxLevel.second << "; "; + + return os; +} + +PolicyEntry::~PolicyEntry() +{ +} + +void PolicyEntry::free(void) +{ + security_manager_policy_entry_free(m_entry); +} + + +PolicyRequest::PolicyRequest() + : m_req(nullptr), + m_entries() +{ + int result = security_manager_policy_update_req_new(&m_req); + RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS, + "creation of new policy request failed. Result: " << result); + RUNNER_ASSERT_MSG(m_req != nullptr, "creation of new policy request did not allocate memory"); +} + +PolicyRequest::~PolicyRequest() +{ + for(std::vector::iterator it = m_entries.begin(); it != m_entries.end(); ++it) { + it->free(); + } + security_manager_policy_update_req_free(m_req); +} + +void PolicyRequest::addEntry(PolicyEntry &entry, + lib_retcode expectedResult) +{ + int result = 0; + + result = security_manager_policy_update_req_add_entry(m_req, entry.get()); + + RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult, + "adding policy entry to request returned wrong value." + << " entry: " << entry << ";" + << " Result: " << result << ";" + << " Expected result: " << expectedResult); + + m_entries.push_back(entry); +} + +std::ostream& operator<<(std::ostream &os, const PolicyRequest &request) +{ + if (request.m_entries.size() != 0) + { + os << "PolicyRequest m_entries size: " << request.m_entries.size() << "; "; + + for(unsigned int i = 0; i != request.m_entries.size(); i++) { + os << "entry " << i << ": " << request.m_entries[i] << "; "; + } + } + + return os; +} + +} // namespace SecurityManagerTest diff --git a/tests/security-manager-tests/common/sm_policy_request.h b/tests/security-manager-tests/common/sm_policy_request.h new file mode 100644 index 0000000..bd31329 --- /dev/null +++ b/tests/security-manager-tests/common/sm_policy_request.h @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef SECURITY_MANAGER_TEST_POLICYREQUEST +#define SECURITY_MANAGER_TEST_POLICYREQUEST + +#include +#include +#include +#include + +#include + +namespace SecurityManagerTest { + +class PolicyEntry +{ +public: + PolicyEntry(); + + PolicyEntry(const std::string &appId, + const std::string &user, + const std::string &privilege + ); + ~PolicyEntry(); + + PolicyEntry(policy_entry &entry); + + policy_entry *get() const { return m_entry; } + std::string getUser() const { return m_user.second; } + std::string getAppId() const { return m_appId.second; } + std::string getPrivilege() const { return m_privilege.second; } + std::string getCurrentLevel() const { return m_currentLevel.second; } + std::string getMaxLevel() const { return m_maxLevel.second; } + void setLevel(const std::string &level); + void setMaxLevel(const std::string &level); + void free(void); + + friend std::ostream& operator<<(std::ostream &, const PolicyEntry&); + +private: + policy_entry *m_entry; + std::pair m_appId; + std::pair m_user; + std::pair m_privilege; + std::pair m_currentLevel; + std::pair m_maxLevel; +}; + +std::ostream& operator<<(std::ostream &os, const SecurityManagerTest::PolicyEntry &request); + +class PolicyRequest +{ +public: + PolicyRequest(); + PolicyRequest(const PolicyRequest&) = delete; + PolicyRequest& operator=(const PolicyRequest&) = delete; + ~PolicyRequest(); + + void addEntry(PolicyEntry &entry, lib_retcode expectedResult = SECURITY_MANAGER_SUCCESS); + + policy_update_req *get() const { return m_req; } + friend std::ostream& operator<<(std::ostream &, const PolicyRequest&); + +private: + policy_update_req *m_req; + std::vector m_entries; +}; + +std::ostream& operator<<(std::ostream &os, const SecurityManagerTest::PolicyRequest &request); + +} // namespace SecurityManagerTest + +#endif // SECURITY_MANAGER_TEST_USERREQUEST -- 2.7.4