Add tests for Cynara::PolicyBucket
authorAleksander Zdyb <a.zdyb@partner.samsung.com>
Fri, 20 Jun 2014 13:01:14 +0000 (15:01 +0200)
committerRafal Krypa <r.krypa@samsung.com>
Thu, 3 Jul 2014 12:19:09 +0000 (14:19 +0200)
Added some convenience methods to PolicyType and PolicyResult

Change-Id: Ibcc1200403132cd16879c6afbc859e851c2a3dac

src/common/types/PolicyResult.h
src/common/types/PolicyType.cpp [new file with mode: 0644]
src/common/types/PolicyType.h
test/CMakeLists.txt
test/common/types/policybucket.cpp [new file with mode: 0644]

index b0d0420..a8febc2 100644 (file)
@@ -25,6 +25,8 @@
 
 #include "types/PolicyType.h"
 
+#include <tuple>
+
 namespace Cynara {
 
 class PolicyResult {
@@ -56,6 +58,10 @@ public:
     bool operator ==(const PolicyResult &other) const {
         return std::tie(m_type, m_metadata) == std::tie(other.m_type, other.m_metadata);
     }
+
+    bool operator ==(const PolicyType &policyType) const {
+        return (m_type == policyType) && m_metadata.empty();
+    }
 };
 
 } // namespace Cynara
diff --git a/src/common/types/PolicyType.cpp b/src/common/types/PolicyType.cpp
new file mode 100644 (file)
index 0000000..bf022f5
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * 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        PolicyType.cpp
+ * @author      Aleksander Zdyb <a.zdyb@partner.samsung.com>
+ * @version     1.0
+ * @brief       Implementation of functions for Cynara::PolicyType
+ */
+
+#include "PolicyType.h"
+#include "PolicyResult.h"
+
+namespace Cynara {
+
+bool operator ==(const PolicyType &policyType, const PolicyResult &policyResult) {
+    return policyResult == policyType;
+}
+
+}  // namespace Cynara
index 82d4740..9131b1b 100644 (file)
@@ -37,6 +37,9 @@ namespace PredefinedPolicyType  {
     const PolicyType ALLOW = 0xFFFF;
 };
 
+class PolicyResult;
+bool operator ==(const PolicyType &policyType, const PolicyResult &policyResult);
+
 }  // namespace Cynara
 
 #endif /* SRC_COMMON_TYPES_POLICYTYPE_H_ */
index d9ad4bd..fddc664 100644 (file)
@@ -28,6 +28,7 @@ SET(CYNARA_SOURCES_FOR_TESTS
     ${CYNARA_SRC}/service/storage/InMemoryStorageBackend.cpp
     ${CYNARA_SRC}/service/storage/StorageSerializer.cpp
     ${CYNARA_SRC}/common/types/PolicyKey.cpp
+    ${CYNARA_SRC}/common/types/PolicyType.cpp
 )
 
 SET(CYNARA_TESTS_SOURCES
@@ -39,6 +40,7 @@ SET(CYNARA_TESTS_SOURCES
     storage/inmemorystoragebackend/search.cpp
     storage/inmemorystoragebackend/buckets.cpp
     storage/serializer/dump.cpp
+    common/types/policybucket.cpp
     helpers.cpp
 )
 
diff --git a/test/common/types/policybucket.cpp b/test/common/types/policybucket.cpp
new file mode 100644 (file)
index 0000000..06afbbb
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ * 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        policybucket.cpp
+ * @author      Aleksander Zdyb <a.zdyb@partner.samsung.com>
+ * @version     1.0
+ * @brief       Tests for Cynara::PolicyBucket
+ */
+
+#include <gtest/gtest.h>
+#include <gmock/gmock.h>
+
+#include "common/types/PolicyBucket.h"
+#include "common/types/PolicyKey.h"
+#include "common/types/PolicyCollection.h"
+
+#include "../../helpers.h"
+
+using namespace Cynara;
+
+class PolicyBucketFixture : public ::testing::Test {
+public:
+    virtual ~PolicyBucketFixture() {}
+
+protected:
+    const PolicyKey pk1 = Helpers::generatePolicyKey("1");
+    const PolicyKey otherPk = Helpers::generatePolicyKey("_");
+
+    const PolicyCollection pk1Policies = {
+        Policy::simpleWithKey(pk1, PredefinedPolicyType::ALLOW),
+        Policy::simpleWithKey(pk1, PredefinedPolicyType::ALLOW),
+        Policy::simpleWithKey(pk1, PredefinedPolicyType::ALLOW)
+    };
+};
+
+TEST_F(PolicyBucketFixture, filtered) {
+    using ::testing::UnorderedElementsAreArray;
+    using ::testing::UnorderedElementsAre;
+    using ::testing::IsEmpty;
+
+    PolicyBucket bucket(pk1Policies);
+    bucket.setDefaultPolicy(PredefinedPolicyType::DENY);
+    auto filtered = bucket.filtered(pk1);
+
+    // Elements match
+    ASSERT_THAT(filtered.policyCollection(), UnorderedElementsAreArray(pk1Policies));
+
+    // default policy matches
+    ASSERT_EQ(PredefinedPolicyType::DENY, filtered.defaultPolicy());
+}
+
+TEST_F(PolicyBucketFixture, filtered_other) {
+    using ::testing::UnorderedElementsAreArray;
+    using ::testing::UnorderedElementsAre;
+    using ::testing::IsEmpty;
+
+    PolicyBucket bucket(pk1Policies);
+    bucket.setDefaultPolicy(PredefinedPolicyType::DENY);
+    auto filtered = bucket.filtered(otherPk);
+
+    // No policies should be found
+    ASSERT_THAT(filtered.policyCollection(), IsEmpty());
+
+    // default policy should be preserved
+    ASSERT_EQ(PredefinedPolicyType::DENY, filtered.defaultPolicy());
+}