983a0409ac8a97136eee5b8788c5c33c7bd4081e
[platform/core/security/cynara.git] / test / common / types / policybucket.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /**
17  * @file        test/common/types/policybucket.cpp
18  * @author      Aleksander Zdyb <a.zdyb@samsung.com>
19  * @version     1.0
20  * @brief       Tests for Cynara::PolicyBucket
21  */
22
23 #include <algorithm>
24 #include <tuple>
25 #include <vector>
26
27 #include <gmock/gmock.h>
28 #include <gtest/gtest.h>
29
30 #include "types/PolicyBucket.h"
31 #include "types/PolicyCollection.h"
32 #include "types/PolicyKey.h"
33
34 #include "../../helpers.h"
35
36 using namespace Cynara;
37
38 class PolicyBucketFixture : public ::testing::Test {
39 public:
40     virtual ~PolicyBucketFixture() {}
41
42 protected:
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("_");
47
48     const PolicyCollection pkPolicies = {
49         Policy::simpleWithKey(pk1, PredefinedPolicyType::ALLOW),
50         Policy::simpleWithKey(pk2, PredefinedPolicyType::ALLOW),
51         Policy::simpleWithKey(pk3, PredefinedPolicyType::ALLOW)
52     };
53
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)
59     };
60 };
61
62 TEST_F(PolicyBucketFixture, filtered) {
63     using ::testing::UnorderedElementsAre;
64
65     PolicyBucket bucket("filtered", pkPolicies);
66     bucket.setDefaultPolicy(PredefinedPolicyType::DENY);
67     auto filtered = bucket.filtered(pk1);
68
69     // Elements match
70     ASSERT_THAT(filtered, UnorderedElementsAre(pkPolicies.at(0)));
71
72     // default policy matches
73     ASSERT_EQ(PredefinedPolicyType::DENY, filtered.defaultPolicy());
74 }
75
76 TEST_F(PolicyBucketFixture, filtered_other) {
77     using ::testing::IsEmpty;
78
79     PolicyBucket bucket("filtered_other", pkPolicies);
80     bucket.setDefaultPolicy(PredefinedPolicyType::DENY);
81     auto filtered = bucket.filtered(otherPk);
82
83     // No policies should be found
84     ASSERT_THAT(filtered, IsEmpty());
85
86     // default policy should be preserved
87     ASSERT_EQ(PredefinedPolicyType::DENY, filtered.defaultPolicy());
88 }
89
90 TEST_F(PolicyBucketFixture, filtered_wildcard_1) {
91     using ::testing::UnorderedElementsAreArray;
92
93     // Leave policies with given client, given user and any privilege
94     auto policiesToStay = Helpers::pickFromCollection(wildcardPolicies, { 0, 1, 3 });
95
96     PolicyBucket bucket("filtered_wildcard_1", wildcardPolicies);
97     auto filtered = bucket.filtered(PolicyKey("c1", "u1", "p2"));
98     ASSERT_THAT(filtered, UnorderedElementsAreArray(policiesToStay));
99 }
100
101 TEST_F(PolicyBucketFixture, filtered_wildcard_2) {
102     using ::testing::UnorderedElementsAreArray;
103
104     // Leave policies with given client, given user and any privilege
105     auto policiesToStay = Helpers::pickFromCollection(wildcardPolicies, { 2, 3 });
106
107     PolicyBucket bucket("filtered_wildcard_2", wildcardPolicies);
108     auto filtered = bucket.filtered(PolicyKey("cccc", "u1", "p1"));
109
110     ASSERT_THAT(filtered, UnorderedElementsAreArray(policiesToStay));
111 }
112
113 TEST_F(PolicyBucketFixture, filtered_wildcard_3) {
114     using ::testing::UnorderedElementsAreArray;
115
116     // Leave policies with given client, given user and any privilege
117     auto policiesToStay = Helpers::pickFromCollection(wildcardPolicies, { 0, 3 });
118
119     PolicyBucket bucket("filtered_wildcard_3", wildcardPolicies);
120     auto filtered = bucket.filtered(PolicyKey("c1", "u1", "pppp"));
121     ASSERT_THAT(filtered, UnorderedElementsAreArray(policiesToStay));
122 }
123
124 TEST_F(PolicyBucketFixture, filtered_wildcard_4) {
125     using ::testing::UnorderedElementsAreArray;
126
127     // Leave policies with given client, given user and any privilege
128     auto policiesToStay = Helpers::pickFromCollection(wildcardPolicies, { 3 });
129
130     PolicyBucket bucket("filtered_wildcard_4", wildcardPolicies);
131     auto filtered = bucket.filtered(PolicyKey("cccc", "uuuu", "pppp"));
132     ASSERT_THAT(filtered, UnorderedElementsAreArray(policiesToStay));
133 }
134
135 TEST_F(PolicyBucketFixture, filtered_wildcard_none) {
136     using ::testing::IsEmpty;
137
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());
143 }