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 "types/PolicyBucket.h"
27 #include "types/PolicyKey.h"
28 #include "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 pk2 = Helpers::generatePolicyKey("2");
44 const PolicyKey pk3 = Helpers::generatePolicyKey("3");
45 const PolicyKey otherPk = Helpers::generatePolicyKey("_");
47 const PolicyCollection pkPolicies = {
48 Policy::simpleWithKey(pk1, PredefinedPolicyType::ALLOW),
49 Policy::simpleWithKey(pk2, PredefinedPolicyType::ALLOW),
50 Policy::simpleWithKey(pk3, PredefinedPolicyType::ALLOW)
53 const PolicyCollection wildcardPolicies = {
54 Policy::simpleWithKey(PolicyKey("c1", "u1", "*"), PredefinedPolicyType::ALLOW),
55 Policy::simpleWithKey(PolicyKey("*", "u1", "p2"), PredefinedPolicyType::ALLOW),
56 Policy::simpleWithKey(PolicyKey("*", "*", "p1"), PredefinedPolicyType::ALLOW),
57 Policy::simpleWithKey(PolicyKey("*", "*", "*"), PredefinedPolicyType::ALLOW)
60 PolicyCollection filterHelper(const PolicyCollection &original,
61 std::function<bool(const PolicyCollection::value_type &)> pred) {
62 PolicyCollection filtered(original.size());
63 auto endIt = std::copy_if(std::begin(original), std::end(original),
64 std::begin(filtered), pred);
65 filtered.resize(std::distance(std::begin(filtered), endIt));
69 PolicyCollection filterHelper(const PolicyCollection &original, std::vector<unsigned> idx) {
70 PolicyCollection filtered;
71 filtered.reserve(idx.size());
72 for (const auto &i : idx) {
73 filtered.push_back(original.at(i));
79 TEST_F(PolicyBucketFixture, filtered) {
80 using ::testing::UnorderedElementsAreArray;
81 using ::testing::UnorderedElementsAre;
82 using ::testing::IsEmpty;
84 PolicyBucket bucket(pkPolicies);
85 bucket.setDefaultPolicy(PredefinedPolicyType::DENY);
86 auto filtered = bucket.filtered(pk1);
89 ASSERT_THAT(filtered.policyCollection(), UnorderedElementsAre(pkPolicies.at(0)));
91 // default policy matches
92 ASSERT_EQ(PredefinedPolicyType::DENY, filtered.defaultPolicy());
95 TEST_F(PolicyBucketFixture, filtered_other) {
96 using ::testing::UnorderedElementsAreArray;
97 using ::testing::UnorderedElementsAre;
98 using ::testing::IsEmpty;
100 PolicyBucket bucket(pkPolicies);
101 bucket.setDefaultPolicy(PredefinedPolicyType::DENY);
102 auto filtered = bucket.filtered(otherPk);
104 // No policies should be found
105 ASSERT_THAT(filtered.policyCollection(), IsEmpty());
107 // default policy should be preserved
108 ASSERT_EQ(PredefinedPolicyType::DENY, filtered.defaultPolicy());
111 TEST_F(PolicyBucketFixture, filtered_wildcard_1) {
112 using ::testing::UnorderedElementsAreArray;
114 // Leave policies with given client, given user and any privilege
115 auto policiesToStay = filterHelper(wildcardPolicies, { 0, 1, 3 });
117 PolicyBucket bucket(wildcardPolicies);
118 auto filtered = bucket.filtered(PolicyKey("c1", "u1", "p2"));
119 ASSERT_THAT(filtered.policyCollection(), UnorderedElementsAreArray(policiesToStay));
122 TEST_F(PolicyBucketFixture, filtered_wildcard_2) {
123 using ::testing::UnorderedElementsAreArray;
125 // Leave policies with given client, given user and any privilege
126 auto policiesToStay = filterHelper(wildcardPolicies, std::vector<unsigned>{ 2, 3 });
128 PolicyBucket bucket(wildcardPolicies);
129 auto filtered = bucket.filtered(PolicyKey("cccc", "u1", "p1"));
131 ASSERT_THAT(filtered.policyCollection(), UnorderedElementsAreArray(policiesToStay));
134 TEST_F(PolicyBucketFixture, filtered_wildcard_3) {
135 using ::testing::UnorderedElementsAreArray;
137 // Leave policies with given client, given user and any privilege
138 auto policiesToStay = filterHelper(wildcardPolicies, std::vector<unsigned>{ 0, 3 });
140 PolicyBucket bucket(wildcardPolicies);
141 auto filtered = bucket.filtered(PolicyKey("c1", "u1", "pppp"));
142 ASSERT_THAT(filtered.policyCollection(), UnorderedElementsAreArray(policiesToStay));
145 TEST_F(PolicyBucketFixture, filtered_wildcard_4) {
146 using ::testing::UnorderedElementsAreArray;
148 // Leave policies with given client, given user and any privilege
149 auto policiesToStay = filterHelper(wildcardPolicies, std::vector<unsigned>{ 3 });
151 PolicyBucket bucket(wildcardPolicies);
152 auto filtered = bucket.filtered(PolicyKey("cccc", "uuuu", "pppp"));
153 ASSERT_THAT(filtered.policyCollection(), UnorderedElementsAreArray(policiesToStay));
156 TEST_F(PolicyBucketFixture, filtered_wildcard_none) {
157 using ::testing::IsEmpty;
159 PolicyBucket bucket({ wildcardPolicies.begin(), wildcardPolicies.begin() + 3 });
160 auto filtered = bucket.filtered(PolicyKey("cccc", "uuuu", "pppp"));
161 ASSERT_THAT(filtered.policyCollection(), IsEmpty());