2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 * @file policybucket.cpp
18 * @author Aleksander Zdyb <a.zdyb@partner.samsung.com>
20 * @brief Tests for Cynara::PolicyBucket
23 #include <gtest/gtest.h>
24 #include <gmock/gmock.h>
26 #include "common/types/PolicyBucket.h"
27 #include "common/types/PolicyKey.h"
28 #include "common/types/PolicyCollection.h"
30 #include "../../helpers.h"
35 using namespace Cynara;
37 class PolicyBucketFixture : public ::testing::Test {
39 virtual ~PolicyBucketFixture() {}
42 const PolicyKey pk1 = Helpers::generatePolicyKey("1");
43 const PolicyKey otherPk = Helpers::generatePolicyKey("_");
45 const PolicyCollection pk1Policies = {
46 Policy::simpleWithKey(pk1, PredefinedPolicyType::ALLOW),
47 Policy::simpleWithKey(pk1, PredefinedPolicyType::ALLOW),
48 Policy::simpleWithKey(pk1, PredefinedPolicyType::ALLOW)
51 const PolicyCollection wildcardPolicies = {
52 Policy::simpleWithKey(PolicyKey("c1", "u1", "p1"), PredefinedPolicyType::ALLOW),
53 Policy::simpleWithKey(PolicyKey("c1", "u1", "p2"), PredefinedPolicyType::ALLOW),
54 Policy::simpleWithKey(PolicyKey("c2", "u1", "p1"), PredefinedPolicyType::ALLOW)
57 PolicyCollection filterHelper(const PolicyCollection &original,
58 std::function<bool(const PolicyCollection::value_type &)> pred) {
59 PolicyCollection filtered(original.size());
60 auto endIt = std::copy_if(std::begin(original), std::end(original),
61 std::begin(filtered), pred);
62 filtered.resize(std::distance(std::begin(filtered), endIt));
67 TEST_F(PolicyBucketFixture, filtered) {
68 using ::testing::UnorderedElementsAreArray;
69 using ::testing::UnorderedElementsAre;
70 using ::testing::IsEmpty;
72 PolicyBucket bucket(pk1Policies);
73 bucket.setDefaultPolicy(PredefinedPolicyType::DENY);
74 auto filtered = bucket.filtered(pk1);
77 ASSERT_THAT(filtered.policyCollection(), UnorderedElementsAreArray(pk1Policies));
79 // default policy matches
80 ASSERT_EQ(PredefinedPolicyType::DENY, filtered.defaultPolicy());
83 TEST_F(PolicyBucketFixture, filtered_other) {
84 using ::testing::UnorderedElementsAreArray;
85 using ::testing::UnorderedElementsAre;
86 using ::testing::IsEmpty;
88 PolicyBucket bucket(pk1Policies);
89 bucket.setDefaultPolicy(PredefinedPolicyType::DENY);
90 auto filtered = bucket.filtered(otherPk);
92 // No policies should be found
93 ASSERT_THAT(filtered.policyCollection(), IsEmpty());
95 // default policy should be preserved
96 ASSERT_EQ(PredefinedPolicyType::DENY, filtered.defaultPolicy());
99 TEST_F(PolicyBucketFixture, filtered_wildcard_privilege) {
100 using ::testing::UnorderedElementsAreArray;
102 // Leave policies with given client, given user and any privilege
103 auto policiesToStay = filterHelper(wildcardPolicies,
104 [] (const PolicyCollection::value_type &privilege) {
105 const auto &key = privilege->key();
106 return std::tie("c1", "u1") == std::tie(key.client(), key.user());
109 PolicyBucket bucket(wildcardPolicies);
110 auto filtered = bucket.filtered(PolicyKey("c1", "u1", "*"));
111 ASSERT_THAT(filtered.policyCollection(), UnorderedElementsAreArray(policiesToStay));
114 TEST_F(PolicyBucketFixture, filtered_wildcard_client) {
115 using ::testing::UnorderedElementsAreArray;
117 // Leave policies with given client, given user and any privilege
118 auto policiesToStay = filterHelper(wildcardPolicies,
119 [] (const PolicyCollection::value_type &privilege) {
120 const auto &key = privilege->key();
121 return std::tie("u1", "p1") == std::tie(key.user(), key.privilege());
124 PolicyBucket bucket(wildcardPolicies);
125 auto filtered = bucket.filtered(PolicyKey("*", "u1", "p1"));
126 ASSERT_THAT(filtered.policyCollection(), UnorderedElementsAreArray(policiesToStay));
129 TEST_F(PolicyBucketFixture, filtered_wildcard_client_privilege) {
130 using ::testing::UnorderedElementsAreArray;
132 // Leave policies with given client, given user and any privilege
133 auto policiesToStay = filterHelper(wildcardPolicies,
134 [] (const PolicyCollection::value_type &privilege) {
135 const auto &key = privilege->key();
136 return key.user() == "u1";
139 PolicyBucket bucket(wildcardPolicies);
140 auto filtered = bucket.filtered(PolicyKey("*", "u1", "*"));
141 ASSERT_THAT(filtered.policyCollection(), UnorderedElementsAreArray(policiesToStay));
144 TEST_F(PolicyBucketFixture, filtered_wildcard_none) {
145 using ::testing::IsEmpty;
147 PolicyBucket bucket(wildcardPolicies);
148 auto filtered = bucket.filtered(PolicyKey("*", "u2", "*"));
149 ASSERT_THAT(filtered.policyCollection(), IsEmpty());