From 6d58a7c2c18f159ab5b6606509f25df7c8e2b54c Mon Sep 17 00:00:00 2001 From: Aleksander Zdyb Date: Tue, 9 Dec 2014 13:22:55 +0100 Subject: [PATCH] Introduce CynaraAdminPolicies This is a collection of cynara_admin_policy structs. It helps to manage memory and creates a convenient wrapper while still allowing to pass it to API calls. Change-Id: I61e39dbab88cbbbef07a0bd5ace4967d20336b17 --- src/cyad/CMakeLists.txt | 1 + src/cyad/CynaraAdminPolicies.cpp | 97 ++++++++++++++++++++++++++++++++++++++++ src/cyad/CynaraAdminPolicies.h | 60 +++++++++++++++++++++++++ test/CMakeLists.txt | 3 ++ test/cyad/helpers.cpp | 64 ++++++++++++++++++++++++++ test/cyad/helpers.h | 41 +++++++++++++++++ test/cyad/policy_collection.cpp | 68 ++++++++++++++++++++++++++++ 7 files changed, 334 insertions(+) create mode 100644 src/cyad/CynaraAdminPolicies.cpp create mode 100644 src/cyad/CynaraAdminPolicies.h create mode 100644 test/cyad/helpers.cpp create mode 100644 test/cyad/helpers.h create mode 100644 test/cyad/policy_collection.cpp diff --git a/src/cyad/CMakeLists.txt b/src/cyad/CMakeLists.txt index 1000a0a..d221c75 100644 --- a/src/cyad/CMakeLists.txt +++ b/src/cyad/CMakeLists.txt @@ -20,6 +20,7 @@ SET(CYAD_PATH ${CYNARA_PATH}/cyad) SET(CYAD_SOURCES ${CYAD_PATH}/AdminApiWrapper.cpp + ${CYAD_PATH}/CynaraAdminPolicies.cpp ${CYAD_PATH}/main.cpp ) diff --git a/src/cyad/CynaraAdminPolicies.cpp b/src/cyad/CynaraAdminPolicies.cpp new file mode 100644 index 0000000..4d83014 --- /dev/null +++ b/src/cyad/CynaraAdminPolicies.cpp @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2014 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. + */ +/** + * @file src/cyad/CynaraAdminPolicies.cpp + * @author Aleksander Zdyb + * @version 1.0 + * @brief Collection of cynara_admin_policy structs + */ + +#include +#include +#include +#include +#include + +#include + +#include "CynaraAdminPolicies.h" + +namespace Cynara { + +CynaraAdminPolicies::CynaraAdminPolicies() : m_sealed(false) {} + +CynaraAdminPolicies::~CynaraAdminPolicies() { + auto freePolicy = [] (cynara_admin_policy *admin_policy) { + if (admin_policy == nullptr) + return; + + free(admin_policy->bucket); + free(admin_policy->client); + free(admin_policy->user); + free(admin_policy->privilege); + free(admin_policy->result_extra); + + delete admin_policy; + }; + + std::for_each(m_policies.begin(), m_policies.end(), freePolicy); +} + +void CynaraAdminPolicies::add(const PolicyBucketId &bucketId, const PolicyResult &policyResult, + const PolicyKey &policyKey) { + if (sealed()) { + throw std::logic_error("Collection is sealed"); + } + + // TODO: Optimize -- try not to malloc every item + cynara_admin_policy *policy = new cynara_admin_policy(); + + auto duplicateString = [] (const std::string &str) -> char * { + auto ret = strdup(str.c_str()); + if (ret == nullptr) + throw std::bad_alloc(); + return ret; + }; + + policy->bucket = duplicateString(bucketId); + policy->client = duplicateString(policyKey.client().toString()); + policy->user = duplicateString(policyKey.user().toString()); + policy->privilege = duplicateString(policyKey.privilege().toString()); + policy->result = policyResult.policyType(); + + if (policyResult.metadata().empty()) + policy->result_extra = nullptr; + else + policy->result_extra = duplicateString(policyResult.metadata()); + + m_policies.push_back(policy); +} + +void CynaraAdminPolicies::seal(void) { + m_policies.push_back(nullptr); + m_sealed = true; +} + +cynara_admin_policy* const *CynaraAdminPolicies::data(void) const { + if (sealed() == false) { + throw std::logic_error("Collection is not sealed"); + } + + return m_policies.data(); +} + +} /* namespace Cynara */ diff --git a/src/cyad/CynaraAdminPolicies.h b/src/cyad/CynaraAdminPolicies.h new file mode 100644 index 0000000..8541783 --- /dev/null +++ b/src/cyad/CynaraAdminPolicies.h @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2014 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. + */ +/** + * @file src/cyad/CynaraAdminPolicies.h + * @author Aleksander Zdyb + * @version 1.0 + * @brief Collection of cynara_admin_policy structs + */ + +#ifndef SRC_CYAD_CYNARAADMINPOLICIES_H_ +#define SRC_CYAD_CYNARAADMINPOLICIES_H_ + +#include + +#include +#include +#include +#include + +struct cynara_admin_policy; + +namespace Cynara { + +class CynaraAdminPolicies { +public: + CynaraAdminPolicies(); + virtual ~CynaraAdminPolicies(); + + void add(const PolicyBucketId &bucketId, const PolicyResult &policyResult, + const PolicyKey &policyKey); + + void seal(void); + + cynara_admin_policy* const *data(void) const; + + bool sealed(void) const { + return m_sealed; + } + +private: + std::vector m_policies; + bool m_sealed; +}; + +} /* namespace Cynara */ + +#endif /* SRC_CYAD_CYNARAADMINPOLICIES_H_ */ diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 6f8ef37..a60837c 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -48,6 +48,7 @@ SET(CYNARA_SOURCES_FOR_TESTS ${CYNARA_SRC}/common/types/PolicyDescription.cpp ${CYNARA_SRC}/common/types/PolicyResult.cpp ${CYNARA_SRC}/common/types/PolicyType.cpp + ${CYNARA_SRC}/cyad/CynaraAdminPolicies.cpp ${CYNARA_SRC}/helpers/creds-commons/CredsCommonsInner.cpp ${CYNARA_SRC}/helpers/creds-commons/creds-commons.cpp ${CYNARA_SRC}/storage/BucketDeserializer.cpp @@ -70,6 +71,8 @@ SET(CYNARA_TESTS_SOURCES common/protocols/admin/listresponse.cpp common/types/policybucket.cpp credsCommons/parser/Parser.cpp + cyad/helpers.cpp + cyad/policy_collection.cpp helpers.cpp storage/performance/bucket.cpp storage/storage/policies.cpp diff --git a/test/cyad/helpers.cpp b/test/cyad/helpers.cpp new file mode 100644 index 0000000..1f600cb --- /dev/null +++ b/test/cyad/helpers.cpp @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2014 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. + */ +/** + * @file test/cyad/helpers.cpp + * @author Aleksander Zdyb + * @version 1.0 + * @brief Helper functions, matchers and operators + */ + +#include +#include + +#include + +#include "helpers.h" + +bool operator==(const cynara_admin_policy &lhs, const cynara_admin_policy &rhs) { + auto strEq = [] (const char *lhs, const char *rhs) -> bool { + if (lhs != nullptr && rhs != nullptr) + return strcmp(lhs, rhs) == 0; + else + return lhs == rhs; + }; + + return lhs.result == rhs.result + && strEq(lhs.bucket, rhs.bucket) + && strEq(lhs.client, rhs.client) + && strEq(lhs.user, rhs.user) + && strEq(lhs.privilege, rhs.privilege) + && strEq(lhs.result_extra, rhs.result_extra); +} + +bool operator!=(const cynara_admin_policy &lhs, const cynara_admin_policy &rhs) { + return !(lhs == rhs); +} + +namespace Cynara { + +namespace Helpers { + +void freeAdminPolicyMembers(cynara_admin_policy *admin_policy) { + free(admin_policy->bucket); + free(admin_policy->client); + free(admin_policy->user); + free(admin_policy->privilege); + free(admin_policy->result_extra); +} + +} /* namespace Helpers */ + +} /* namespace Cynara */ diff --git a/test/cyad/helpers.h b/test/cyad/helpers.h new file mode 100644 index 0000000..b4fd02c --- /dev/null +++ b/test/cyad/helpers.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2014 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. + */ +/** + * @file test/cyad/helpers.h + * @author Aleksander Zdyb + * @version 1.0 + * @brief Helper functions, matchers and operators + */ + +#ifndef TEST_CYAD_HELPERS_H_ +#define TEST_CYAD_HELPERS_H_ + +struct cynara_admin_policy; + +bool operator==(const cynara_admin_policy &lhs, const cynara_admin_policy &rhs); +bool operator!=(const cynara_admin_policy &lhs, const cynara_admin_policy &rhs); + +namespace Cynara { + +namespace Helpers { + +void freeAdminPolicyMembers(cynara_admin_policy *admin_policy); + +} /* namespace Helpers */ + +} /* namespace Cynara */ + +#endif /* TEST_CYAD_HELPERS_H_ */ diff --git a/test/cyad/policy_collection.cpp b/test/cyad/policy_collection.cpp new file mode 100644 index 0000000..425e025 --- /dev/null +++ b/test/cyad/policy_collection.cpp @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2014 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. + */ +/** + * @file test/cyad/policy_collection.cpp + * @author Aleksander Zdyb + * @version 1.0 + * @brief Tests for CynaraAdminPolicies + */ + +#include +#include + +#include +#include + +#include +#include + +#include + +#include "helpers.h" + +TEST(CynaraAdminPolicies, notSealed) { + Cynara::CynaraAdminPolicies policies; + ASSERT_THROW(policies.data(), std::logic_error); +} + +TEST(CynaraAdminPolicies, sealEmpty) { + Cynara::CynaraAdminPolicies policies; + policies.seal(); + ASSERT_EQ(nullptr, policies.data()[0]); +} + +TEST(CynaraAdminPolicies, addToSealed) { + Cynara::CynaraAdminPolicies policies; + policies.seal(); + ASSERT_THROW(policies.add("", { CYNARA_ADMIN_ALLOW, "" }, { "", "", ""} ), std::logic_error); +} + +TEST(CynaraAdminPolicies, addOne) { + using ::testing::ElementsAreArray; + + Cynara::CynaraAdminPolicies policies; + policies.add("test-bucket", { CYNARA_ADMIN_ALLOW, "" }, { "client", "user", "privilege"} ); + policies.seal(); + ASSERT_NO_THROW(policies.data()); + + cynara_admin_policy policy = { strdup("test-bucket"), strdup("client"), strdup("user"), + strdup("privilege"), CYNARA_ADMIN_ALLOW, nullptr }; + + ASSERT_EQ(policy, *policies.data()[0]); + ASSERT_EQ(nullptr, policies.data()[1]); + + Cynara::Helpers::freeAdminPolicyMembers(&policy); +} -- 2.7.4