Add wildcard tests for Cynara::PolicyBucket
authorAleksander Zdyb <a.zdyb@partner.samsung.com>
Mon, 23 Jun 2014 10:32:10 +0000 (12:32 +0200)
committerRafal Krypa <r.krypa@samsung.com>
Thu, 3 Jul 2014 12:19:09 +0000 (14:19 +0200)
Add some convenience functions to Cynara::PolicyKey

Change-Id: Iddd51ca16f85199e2f8993c95120dcdbc6b5d7eb

src/common/types/PolicyBucket.h
src/common/types/PolicyKey.cpp
src/common/types/PolicyKey.h
test/common/types/policybucket.cpp

index 30f4bb2..4607fd4 100644 (file)
@@ -45,10 +45,10 @@ const PolicyBucketId defaultPolicyBucketId("");
 class PolicyBucket {
 public:
 
-    PolicyBucket() : m_defaultPolicy(PolicyResult(PredefinedPolicyType::DENY)) {}
+    PolicyBucket() : m_defaultPolicy(PredefinedPolicyType::DENY) {}
     PolicyBucket(const PolicyCollection &policies)
         : m_policyCollection(policies),
-          m_defaultPolicy(PolicyResult(PredefinedPolicyType::DENY)) {}
+          m_defaultPolicy(PredefinedPolicyType::DENY) {}
 
     PolicyBucket filtered(const PolicyKey &key) const;
 
index b1c9e92..dc0d347 100644 (file)
@@ -41,4 +41,8 @@ std::string PolicyKey::toString() const {
     return ss.str();
 }
 
+bool operator ==(const PolicyKeyFeature::ValueType &pkf1, const PolicyKeyFeature &pkf2) {
+    return pkf2 == pkf1;
+}
+
 } /* namespace Cynara */
index 60d03f0..ae9774e 100644 (file)
@@ -52,10 +52,15 @@ public:
         return anyWildcard(*this, other) || valuesMatch(*this, other);
     }
 
+    bool operator==(const PolicyKeyFeature::ValueType &other) const {
+        return anyWildcard(*this, other) || valuesMatch(*this, other);
+    }
+
     const std::string &toString() const;
 
 protected:
-    PolicyKeyFeature(ValueType value) : m_value(value), m_isWildcard(false) {}
+    PolicyKeyFeature(const ValueType &value) : m_value(value),
+        m_isWildcard(value == PolicyKeyFeature::m_wildcardValue) {}
     PolicyKeyFeature() : m_value(m_wildcardValue), m_isWildcard(true) {}
 
     static bool anyWildcard(const PolicyKeyFeature &pkf1, const PolicyKeyFeature &pkf2) {
@@ -88,6 +93,11 @@ public:
             const PolicyKeyFeature &privilegeId)
         : m_client(clientId), m_user(userId), m_privilege(privilegeId) {};
 
+    PolicyKey(const PolicyKeyFeature::ValueType &clientId,
+            const PolicyKeyFeature::ValueType &userId,
+            const PolicyKeyFeature::ValueType &privilegeId)
+        : m_client(clientId), m_user(userId), m_privilege(privilegeId) {};
+
     bool operator==(const PolicyKey &other) const {
         return std::tie(m_client, m_user, m_privilege)
             == std::tie(other.m_client, other.m_user, other.m_privilege);
@@ -114,6 +124,8 @@ public:
     }
 };
 
+bool operator ==(const PolicyKeyFeature::ValueType &pkf1, const PolicyKeyFeature &pkf2);
+
 } /* namespace Cynara */
 
 
index 06afbbb..b48283f 100644 (file)
@@ -29,6 +29,9 @@
 
 #include "../../helpers.h"
 
+#include <algorithm>
+#include <tuple>
+
 using namespace Cynara;
 
 class PolicyBucketFixture : public ::testing::Test {
@@ -44,6 +47,21 @@ protected:
         Policy::simpleWithKey(pk1, PredefinedPolicyType::ALLOW),
         Policy::simpleWithKey(pk1, PredefinedPolicyType::ALLOW)
     };
+
+    const PolicyCollection wildcardPolicies = {
+        Policy::simpleWithKey(PolicyKey("c1", "u1", "p1"), PredefinedPolicyType::ALLOW),
+        Policy::simpleWithKey(PolicyKey("c1", "u1", "p2"), PredefinedPolicyType::ALLOW),
+        Policy::simpleWithKey(PolicyKey("c2", "u1", "p1"), PredefinedPolicyType::ALLOW)
+    };
+
+    PolicyCollection filterHelper(const PolicyCollection &original,
+            std::function<bool(const PolicyCollection::value_type &)> pred) {
+        PolicyCollection filtered(original.size());
+        auto endIt = std::copy_if(std::begin(original), std::end(original),
+            std::begin(filtered), pred);
+        filtered.resize(std::distance(std::begin(filtered), endIt));
+        return filtered;
+    }
 };
 
 TEST_F(PolicyBucketFixture, filtered) {
@@ -77,3 +95,56 @@ TEST_F(PolicyBucketFixture, filtered_other) {
     // default policy should be preserved
     ASSERT_EQ(PredefinedPolicyType::DENY, filtered.defaultPolicy());
 }
+
+TEST_F(PolicyBucketFixture, filtered_wildcard_privilege) {
+    using ::testing::UnorderedElementsAreArray;
+
+    // Leave policies with given client, given user and any privilege
+    auto policiesToStay = filterHelper(wildcardPolicies,
+        [] (const PolicyCollection::value_type &privilege) {
+            const auto &key = privilege->key();
+            return std::tie("c1", "u1") == std::tie(key.client(), key.user());
+        });
+
+    PolicyBucket bucket(wildcardPolicies);
+    auto filtered = bucket.filtered(PolicyKey("c1", "u1", "*"));
+    ASSERT_THAT(filtered.policyCollection(), UnorderedElementsAreArray(policiesToStay));
+}
+
+TEST_F(PolicyBucketFixture, filtered_wildcard_client) {
+    using ::testing::UnorderedElementsAreArray;
+
+    // Leave policies with given client, given user and any privilege
+    auto policiesToStay = filterHelper(wildcardPolicies,
+        [] (const PolicyCollection::value_type &privilege) {
+            const auto &key = privilege->key();
+            return std::tie("u1", "p1") == std::tie(key.user(), key.privilege());
+        });
+
+    PolicyBucket bucket(wildcardPolicies);
+    auto filtered = bucket.filtered(PolicyKey("*", "u1", "p1"));
+    ASSERT_THAT(filtered.policyCollection(), UnorderedElementsAreArray(policiesToStay));
+}
+
+TEST_F(PolicyBucketFixture, filtered_wildcard_client_privilege) {
+    using ::testing::UnorderedElementsAreArray;
+
+    // Leave policies with given client, given user and any privilege
+    auto policiesToStay = filterHelper(wildcardPolicies,
+        [] (const PolicyCollection::value_type &privilege) {
+            const auto &key = privilege->key();
+            return key.user() == "u1";
+        });
+
+    PolicyBucket bucket(wildcardPolicies);
+    auto filtered = bucket.filtered(PolicyKey("*", "u1", "*"));
+    ASSERT_THAT(filtered.policyCollection(), UnorderedElementsAreArray(policiesToStay));
+}
+
+TEST_F(PolicyBucketFixture, filtered_wildcard_none) {
+    using ::testing::IsEmpty;
+
+    PolicyBucket bucket(wildcardPolicies);
+    auto filtered = bucket.filtered(PolicyKey("*", "u2", "*"));
+    ASSERT_THAT(filtered.policyCollection(), IsEmpty());
+}