Add 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 using namespace Cynara;
33
34 class PolicyBucketFixture : public ::testing::Test {
35 public:
36     virtual ~PolicyBucketFixture() {}
37
38 protected:
39     const PolicyKey pk1 = Helpers::generatePolicyKey("1");
40     const PolicyKey otherPk = Helpers::generatePolicyKey("_");
41
42     const PolicyCollection pk1Policies = {
43         Policy::simpleWithKey(pk1, PredefinedPolicyType::ALLOW),
44         Policy::simpleWithKey(pk1, PredefinedPolicyType::ALLOW),
45         Policy::simpleWithKey(pk1, PredefinedPolicyType::ALLOW)
46     };
47 };
48
49 TEST_F(PolicyBucketFixture, filtered) {
50     using ::testing::UnorderedElementsAreArray;
51     using ::testing::UnorderedElementsAre;
52     using ::testing::IsEmpty;
53
54     PolicyBucket bucket(pk1Policies);
55     bucket.setDefaultPolicy(PredefinedPolicyType::DENY);
56     auto filtered = bucket.filtered(pk1);
57
58     // Elements match
59     ASSERT_THAT(filtered.policyCollection(), UnorderedElementsAreArray(pk1Policies));
60
61     // default policy matches
62     ASSERT_EQ(PredefinedPolicyType::DENY, filtered.defaultPolicy());
63 }
64
65 TEST_F(PolicyBucketFixture, filtered_other) {
66     using ::testing::UnorderedElementsAreArray;
67     using ::testing::UnorderedElementsAre;
68     using ::testing::IsEmpty;
69
70     PolicyBucket bucket(pk1Policies);
71     bucket.setDefaultPolicy(PredefinedPolicyType::DENY);
72     auto filtered = bucket.filtered(otherPk);
73
74     // No policies should be found
75     ASSERT_THAT(filtered.policyCollection(), IsEmpty());
76
77     // default policy should be preserved
78     ASSERT_EQ(PredefinedPolicyType::DENY, filtered.defaultPolicy());
79 }