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 test/common/types/policybucket.cpp
18 * @author Aleksander Zdyb <a.zdyb@samsung.com>
20 * @brief Tests for Cynara::PolicyBucket
27 #include <gmock/gmock.h>
28 #include <gtest/gtest.h>
30 #include "types/PolicyBucket.h"
31 #include "types/PolicyCollection.h"
32 #include "types/PolicyKey.h"
34 #include "../../helpers.h"
36 using namespace Cynara;
38 class PolicyBucketFixture : public ::testing::Test {
40 virtual ~PolicyBucketFixture() {}
43 const PolicyKey pk1 = Helpers::generatePolicyKey("1");
44 const PolicyKey pk2 = Helpers::generatePolicyKey("2");
45 const PolicyKey pk3 = Helpers::generatePolicyKey("3");
46 const PolicyKey otherPk = Helpers::generatePolicyKey("_");
48 const PolicyCollection pkPolicies = {
49 Policy::simpleWithKey(pk1, PredefinedPolicyType::ALLOW),
50 Policy::simpleWithKey(pk2, PredefinedPolicyType::ALLOW),
51 Policy::simpleWithKey(pk3, PredefinedPolicyType::ALLOW)
54 const PolicyCollection wildcardPolicies = {
55 Policy::simpleWithKey(PolicyKey("c1", "u1", "*"), PredefinedPolicyType::ALLOW),
56 Policy::simpleWithKey(PolicyKey("*", "u1", "p2"), PredefinedPolicyType::ALLOW),
57 Policy::simpleWithKey(PolicyKey("*", "*", "p1"), PredefinedPolicyType::ALLOW),
58 Policy::simpleWithKey(PolicyKey("*", "*", "*"), PredefinedPolicyType::ALLOW)
62 TEST_F(PolicyBucketFixture, filtered) {
63 using ::testing::UnorderedElementsAre;
65 PolicyBucket bucket("filtered", pkPolicies);
66 bucket.setDefaultPolicy(PredefinedPolicyType::DENY);
67 auto filtered = bucket.filtered(pk1);
70 ASSERT_THAT(filtered, UnorderedElementsAre(pkPolicies.at(0)));
72 // default policy matches
73 ASSERT_EQ(PredefinedPolicyType::DENY, filtered.defaultPolicy());
76 TEST_F(PolicyBucketFixture, filtered_other) {
77 using ::testing::IsEmpty;
79 PolicyBucket bucket("filtered_other", pkPolicies);
80 bucket.setDefaultPolicy(PredefinedPolicyType::DENY);
81 auto filtered = bucket.filtered(otherPk);
83 // No policies should be found
84 ASSERT_THAT(filtered, IsEmpty());
86 // default policy should be preserved
87 ASSERT_EQ(PredefinedPolicyType::DENY, filtered.defaultPolicy());
90 TEST_F(PolicyBucketFixture, filtered_wildcard_1) {
91 using ::testing::UnorderedElementsAreArray;
93 // Leave policies with given client, given user and any privilege
94 auto policiesToStay = Helpers::pickFromCollection(wildcardPolicies, { 0, 1, 3 });
96 PolicyBucket bucket("filtered_wildcard_1", wildcardPolicies);
97 auto filtered = bucket.filtered(PolicyKey("c1", "u1", "p2"));
98 ASSERT_THAT(filtered, UnorderedElementsAreArray(policiesToStay));
101 TEST_F(PolicyBucketFixture, filtered_wildcard_2) {
102 using ::testing::UnorderedElementsAreArray;
104 // Leave policies with given client, given user and any privilege
105 auto policiesToStay = Helpers::pickFromCollection(wildcardPolicies, { 2, 3 });
107 PolicyBucket bucket("filtered_wildcard_2", wildcardPolicies);
108 auto filtered = bucket.filtered(PolicyKey("cccc", "u1", "p1"));
110 ASSERT_THAT(filtered, UnorderedElementsAreArray(policiesToStay));
113 TEST_F(PolicyBucketFixture, filtered_wildcard_3) {
114 using ::testing::UnorderedElementsAreArray;
116 // Leave policies with given client, given user and any privilege
117 auto policiesToStay = Helpers::pickFromCollection(wildcardPolicies, { 0, 3 });
119 PolicyBucket bucket("filtered_wildcard_3", wildcardPolicies);
120 auto filtered = bucket.filtered(PolicyKey("c1", "u1", "pppp"));
121 ASSERT_THAT(filtered, UnorderedElementsAreArray(policiesToStay));
124 TEST_F(PolicyBucketFixture, filtered_wildcard_4) {
125 using ::testing::UnorderedElementsAreArray;
127 // Leave policies with given client, given user and any privilege
128 auto policiesToStay = Helpers::pickFromCollection(wildcardPolicies, { 3 });
130 PolicyBucket bucket("filtered_wildcard_4", wildcardPolicies);
131 auto filtered = bucket.filtered(PolicyKey("cccc", "uuuu", "pppp"));
132 ASSERT_THAT(filtered, UnorderedElementsAreArray(policiesToStay));
135 TEST_F(PolicyBucketFixture, filtered_wildcard_none) {
136 using ::testing::IsEmpty;
138 PolicyBucket bucket("filtered_wildcard_none",
139 PolicyCollection({ wildcardPolicies.begin(),
140 wildcardPolicies.begin() + 3 }));
141 auto filtered = bucket.filtered(PolicyKey("cccc", "uuuu", "pppp"));
142 ASSERT_THAT(filtered, IsEmpty());