Add wildcard tests for Cynara::PolicyBucket
[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 "common/types/PolicyBucket.h"
27 #include "common/types/PolicyKey.h"
28 #include "common/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 otherPk = Helpers::generatePolicyKey("_");
44
45     const PolicyCollection pk1Policies = {
46         Policy::simpleWithKey(pk1, PredefinedPolicyType::ALLOW),
47         Policy::simpleWithKey(pk1, PredefinedPolicyType::ALLOW),
48         Policy::simpleWithKey(pk1, PredefinedPolicyType::ALLOW)
49     };
50
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)
55     };
56
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));
63         return filtered;
64     }
65 };
66
67 TEST_F(PolicyBucketFixture, filtered) {
68     using ::testing::UnorderedElementsAreArray;
69     using ::testing::UnorderedElementsAre;
70     using ::testing::IsEmpty;
71
72     PolicyBucket bucket(pk1Policies);
73     bucket.setDefaultPolicy(PredefinedPolicyType::DENY);
74     auto filtered = bucket.filtered(pk1);
75
76     // Elements match
77     ASSERT_THAT(filtered.policyCollection(), UnorderedElementsAreArray(pk1Policies));
78
79     // default policy matches
80     ASSERT_EQ(PredefinedPolicyType::DENY, filtered.defaultPolicy());
81 }
82
83 TEST_F(PolicyBucketFixture, filtered_other) {
84     using ::testing::UnorderedElementsAreArray;
85     using ::testing::UnorderedElementsAre;
86     using ::testing::IsEmpty;
87
88     PolicyBucket bucket(pk1Policies);
89     bucket.setDefaultPolicy(PredefinedPolicyType::DENY);
90     auto filtered = bucket.filtered(otherPk);
91
92     // No policies should be found
93     ASSERT_THAT(filtered.policyCollection(), IsEmpty());
94
95     // default policy should be preserved
96     ASSERT_EQ(PredefinedPolicyType::DENY, filtered.defaultPolicy());
97 }
98
99 TEST_F(PolicyBucketFixture, filtered_wildcard_privilege) {
100     using ::testing::UnorderedElementsAreArray;
101
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());
107         });
108
109     PolicyBucket bucket(wildcardPolicies);
110     auto filtered = bucket.filtered(PolicyKey("c1", "u1", "*"));
111     ASSERT_THAT(filtered.policyCollection(), UnorderedElementsAreArray(policiesToStay));
112 }
113
114 TEST_F(PolicyBucketFixture, filtered_wildcard_client) {
115     using ::testing::UnorderedElementsAreArray;
116
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());
122         });
123
124     PolicyBucket bucket(wildcardPolicies);
125     auto filtered = bucket.filtered(PolicyKey("*", "u1", "p1"));
126     ASSERT_THAT(filtered.policyCollection(), UnorderedElementsAreArray(policiesToStay));
127 }
128
129 TEST_F(PolicyBucketFixture, filtered_wildcard_client_privilege) {
130     using ::testing::UnorderedElementsAreArray;
131
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";
137         });
138
139     PolicyBucket bucket(wildcardPolicies);
140     auto filtered = bucket.filtered(PolicyKey("*", "u1", "*"));
141     ASSERT_THAT(filtered.policyCollection(), UnorderedElementsAreArray(policiesToStay));
142 }
143
144 TEST_F(PolicyBucketFixture, filtered_wildcard_none) {
145     using ::testing::IsEmpty;
146
147     PolicyBucket bucket(wildcardPolicies);
148     auto filtered = bucket.filtered(PolicyKey("*", "u2", "*"));
149     ASSERT_THAT(filtered.policyCollection(), IsEmpty());
150 }