Introduce CynaraAdminPolicies 75/32175/5
authorAleksander Zdyb <a.zdyb@samsung.com>
Tue, 9 Dec 2014 12:22:55 +0000 (13:22 +0100)
committerPawel Wieczorek <p.wieczorek2@samsung.com>
Mon, 29 Dec 2014 09:10:08 +0000 (10:10 +0100)
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
src/cyad/CynaraAdminPolicies.cpp [new file with mode: 0644]
src/cyad/CynaraAdminPolicies.h [new file with mode: 0644]
test/CMakeLists.txt
test/cyad/helpers.cpp [new file with mode: 0644]
test/cyad/helpers.h [new file with mode: 0644]
test/cyad/policy_collection.cpp [new file with mode: 0644]

index 1000a0a..d221c75 100644 (file)
@@ -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 (file)
index 0000000..4d83014
--- /dev/null
@@ -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 <a.zdyb@samsung.com>
+ * @version     1.0
+ * @brief       Collection of cynara_admin_policy structs
+ */
+
+#include <algorithm>
+#include <cstring>
+#include <new>
+#include <stdexcept>
+#include <string>
+
+#include <cynara-admin-types.h>
+
+#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 (file)
index 0000000..8541783
--- /dev/null
@@ -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 <a.zdyb@samsung.com>
+ * @version     1.0
+ * @brief       Collection of cynara_admin_policy structs
+ */
+
+#ifndef SRC_CYAD_CYNARAADMINPOLICIES_H_
+#define SRC_CYAD_CYNARAADMINPOLICIES_H_
+
+#include <vector>
+
+#include <types/PolicyBucketId.h>
+#include <types/PolicyKey.h>
+#include <types/PolicyResult.h>
+#include <types/PolicyType.h>
+
+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<cynara_admin_policy *> m_policies;
+    bool m_sealed;
+};
+
+} /* namespace Cynara */
+
+#endif /* SRC_CYAD_CYNARAADMINPOLICIES_H_ */
index 6f8ef37..a60837c 100644 (file)
@@ -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 (file)
index 0000000..1f600cb
--- /dev/null
@@ -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 <a.zdyb@samsung.com>
+ * @version     1.0
+ * @brief       Helper functions, matchers and operators
+ */
+
+#include <cstdlib>
+#include <cstring>
+
+#include <cynara-admin-types.h>
+
+#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 (file)
index 0000000..b4fd02c
--- /dev/null
@@ -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 <a.zdyb@samsung.com>
+ * @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 (file)
index 0000000..425e025
--- /dev/null
@@ -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 <a.zdyb@samsung.com>
+ * @version     1.0
+ * @brief       Tests for CynaraAdminPolicies
+ */
+
+#include <exception>
+#include <memory>
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+#include <cynara-admin-types.h>
+#include <cynara-policy-types.h>
+
+#include <cyad/CynaraAdminPolicies.h>
+
+#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);
+}