Fix Cynara::PolicyBucket tests
[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        policybucket.cpp
18  * @author      Aleksander Zdyb <a.zdyb@partner.samsung.com>
19  * @version     1.0
20  * @brief       Tests for Cynara::PolicyBucket
21  */
22
23 #include <gtest/gtest.h>
24 #include <gmock/gmock.h>
25
26 #include "types/PolicyBucket.h"
27 #include "types/PolicyKey.h"
28 #include "types/PolicyCollection.h"
29
30 #include "../../helpers.h"
31
32 #include <algorithm>
33 #include <tuple>
34
35 using namespace Cynara;
36
37 class PolicyBucketFixture : public ::testing::Test {
38 public:
39     virtual ~PolicyBucketFixture() {}
40
41 protected:
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("_");
46
47     const PolicyCollection pkPolicies = {
48         Policy::simpleWithKey(pk1, PredefinedPolicyType::ALLOW),
49         Policy::simpleWithKey(pk2, PredefinedPolicyType::ALLOW),
50         Policy::simpleWithKey(pk3, PredefinedPolicyType::ALLOW)
51     };
52
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)
58     };
59
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));
66         return filtered;
67     }
68
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));
74         }
75         return filtered;
76     }
77 };
78
79 TEST_F(PolicyBucketFixture, filtered) {
80     using ::testing::UnorderedElementsAreArray;
81     using ::testing::UnorderedElementsAre;
82     using ::testing::IsEmpty;
83
84     PolicyBucket bucket(pkPolicies);
85     bucket.setDefaultPolicy(PredefinedPolicyType::DENY);
86     auto filtered = bucket.filtered(pk1);
87
88     // Elements match
89     ASSERT_THAT(filtered.policyCollection(), UnorderedElementsAre(pkPolicies.at(0)));
90
91     // default policy matches
92     ASSERT_EQ(PredefinedPolicyType::DENY, filtered.defaultPolicy());
93 }
94
95 TEST_F(PolicyBucketFixture, filtered_other) {
96     using ::testing::UnorderedElementsAreArray;
97     using ::testing::UnorderedElementsAre;
98     using ::testing::IsEmpty;
99
100     PolicyBucket bucket(pkPolicies);
101     bucket.setDefaultPolicy(PredefinedPolicyType::DENY);
102     auto filtered = bucket.filtered(otherPk);
103
104     // No policies should be found
105     ASSERT_THAT(filtered.policyCollection(), IsEmpty());
106
107     // default policy should be preserved
108     ASSERT_EQ(PredefinedPolicyType::DENY, filtered.defaultPolicy());
109 }
110
111 TEST_F(PolicyBucketFixture, filtered_wildcard_1) {
112     using ::testing::UnorderedElementsAreArray;
113
114     // Leave policies with given client, given user and any privilege
115     auto policiesToStay = filterHelper(wildcardPolicies, { 0, 1, 3 });
116
117     PolicyBucket bucket(wildcardPolicies);
118     auto filtered = bucket.filtered(PolicyKey("c1", "u1", "p2"));
119     ASSERT_THAT(filtered.policyCollection(), UnorderedElementsAreArray(policiesToStay));
120 }
121
122 TEST_F(PolicyBucketFixture, filtered_wildcard_2) {
123     using ::testing::UnorderedElementsAreArray;
124
125     // Leave policies with given client, given user and any privilege
126     auto policiesToStay = filterHelper(wildcardPolicies, std::vector<unsigned>{ 2, 3 });
127
128     PolicyBucket bucket(wildcardPolicies);
129     auto filtered = bucket.filtered(PolicyKey("cccc", "u1", "p1"));
130
131     ASSERT_THAT(filtered.policyCollection(), UnorderedElementsAreArray(policiesToStay));
132 }
133
134 TEST_F(PolicyBucketFixture, filtered_wildcard_3) {
135     using ::testing::UnorderedElementsAreArray;
136
137     // Leave policies with given client, given user and any privilege
138     auto policiesToStay = filterHelper(wildcardPolicies, std::vector<unsigned>{ 0, 3 });
139
140     PolicyBucket bucket(wildcardPolicies);
141     auto filtered = bucket.filtered(PolicyKey("c1", "u1", "pppp"));
142     ASSERT_THAT(filtered.policyCollection(), UnorderedElementsAreArray(policiesToStay));
143 }
144
145 TEST_F(PolicyBucketFixture, filtered_wildcard_4) {
146     using ::testing::UnorderedElementsAreArray;
147
148     // Leave policies with given client, given user and any privilege
149     auto policiesToStay = filterHelper(wildcardPolicies, std::vector<unsigned>{ 3 });
150
151     PolicyBucket bucket(wildcardPolicies);
152     auto filtered = bucket.filtered(PolicyKey("cccc", "uuuu", "pppp"));
153     ASSERT_THAT(filtered.policyCollection(), UnorderedElementsAreArray(policiesToStay));
154 }
155
156 TEST_F(PolicyBucketFixture, filtered_wildcard_none) {
157     using ::testing::IsEmpty;
158
159     PolicyBucket bucket({ wildcardPolicies.begin(), wildcardPolicies.begin() + 3 });
160     auto filtered = bucket.filtered(PolicyKey("cccc", "uuuu", "pppp"));
161     ASSERT_THAT(filtered.policyCollection(), IsEmpty());
162 }