f463db3404fee0a2c7c734fc9d60d499f95fa0b5
[platform/core/security/cynara.git] / test / storage / storage / check.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        buckets.cpp
18  * @author      Aleksander Zdyb <a.zdyb@partner.samsung.com>
19  * @version     1.0
20  * @brief       Tests of check in Storage
21  */
22
23 #include <gtest/gtest.h>
24 #include <gmock/gmock.h>
25
26 #include "common/types/PolicyType.h"
27 #include "common/types/PolicyKey.h"
28 #include "common/types/PolicyResult.h"
29 #include "common/types/PolicyCollection.h"
30 #include "common/types/pointers.h"
31 #include "common/exceptions/DefaultBucketDeletionException.h"
32 #include "common/exceptions/BucketAlreadyExistsException.h"
33 #include "service/storage/Storage.h"
34 #include "service/storage/StorageBackend.h"
35
36 #include "fakestoragebackend.h"
37 #include "../../helpers.h"
38
39 #include <memory>
40 #include <tuple>
41
42 using namespace Cynara;
43
44 TEST(storage, checkEmpty) {
45     using ::testing::ReturnPointee;
46
47     PolicyBucket emptyBucket;
48
49     FakeStorageBackend backend;
50     Cynara::Storage storage(backend);
51     PolicyKey pk = Helpers::generatePolicyKey();
52
53     EXPECT_CALL(backend, searchDefaultBucket(pk))
54         .WillOnce(ReturnPointee(&emptyBucket));
55
56     // Default bucket empty -- return DENY
57     auto policyAnswer = storage.checkPolicy(pk);
58     ASSERT_EQ(PolicyType::DENY, policyAnswer.policyType());
59 }
60
61 TEST(storage, checkSimple) {
62     using ::testing::ReturnPointee;
63
64     PolicyBucket bucket;
65     FakeStorageBackend backend;
66
67     Cynara::Storage storage(backend);
68     PolicyKey pk = Helpers::generatePolicyKey();
69
70     EXPECT_CALL(backend, searchDefaultBucket(pk))
71         .WillRepeatedly(ReturnPointee(&bucket));
72
73     // Default bucket empty -- return DENY
74     ASSERT_EQ(PolicyType::DENY, storage.checkPolicy(pk).policyType());
75
76     // Add ALLOW to default bucket -- return ALLOW
77     bucket.policyCollection().push_back(Policy::simpleWithKey(pk, PolicyType::ALLOW));
78     ASSERT_EQ(PolicyType::ALLOW, storage.checkPolicy(pk).policyType());
79
80     // Add DENY to default bucket -- return DENY
81     bucket.policyCollection().push_back(Policy::simpleWithKey(pk, PolicyType::DENY));
82     ASSERT_EQ(PolicyType::DENY, storage.checkPolicy(pk).policyType());
83 }
84
85 TEST(storage, checkBucket) {
86     using ::testing::ReturnPointee;
87
88     const PolicyBucketId additionalBucketId = "additional-bucket";
89
90     FakeStorageBackend backend;
91     Cynara::Storage storage(backend);
92     PolicyKey pk = Helpers::generatePolicyKey();
93
94     PolicyBucket defaultBucket(PolicyCollection({
95         Policy::simpleWithKey(pk, PolicyType::ALLOW),
96         Policy::bucketWithKey(pk, additionalBucketId)
97     }));
98
99     PolicyBucket additionalBucket;
100
101     EXPECT_CALL(backend, searchDefaultBucket(pk))
102         .WillRepeatedly(ReturnPointee(&defaultBucket));
103
104     EXPECT_CALL(backend, searchBucket(defaultPolicyBucketId, pk))
105         .WillRepeatedly(ReturnPointee(&defaultBucket));
106
107     EXPECT_CALL(backend, searchBucket(additionalBucketId, pk))
108         .WillRepeatedly(ReturnPointee(&additionalBucket));
109
110
111     // Bucket empty -- should return DENY as default bucket value
112     ASSERT_EQ(PolicyType::DENY, storage.checkPolicy(pk).policyType());
113
114     // Add ALLOW to bucket, so return ALLOW
115     additionalBucket.policyCollection().push_back(Policy::simpleWithKey(pk, PolicyType::ALLOW));
116     ASSERT_EQ(PolicyType::ALLOW, storage.checkPolicy(pk).policyType());
117
118     // Add DENY to default bucket -- return DENY, even though ALLOW in other bucket
119     defaultBucket.policyCollection().push_back(Policy::simpleWithKey(pk, PolicyType::DENY));
120     ASSERT_EQ(PolicyType::DENY, storage.checkPolicy(pk).policyType());
121 }