2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @author Aleksander Zdyb <a.zdyb@partner.samsung.com>
20 * @brief Tests of check in Storage
23 #include <gtest/gtest.h>
24 #include <gmock/gmock.h>
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"
36 #include "fakestoragebackend.h"
37 #include "../../helpers.h"
42 using namespace Cynara;
44 TEST(storage, checkEmpty) {
45 using ::testing::ReturnPointee;
47 PolicyBucket emptyBucket;
49 FakeStorageBackend backend;
50 Cynara::Storage storage(backend);
51 PolicyKey pk = Helpers::generatePolicyKey();
53 EXPECT_CALL(backend, searchDefaultBucket(pk))
54 .WillOnce(ReturnPointee(&emptyBucket));
56 // Default bucket empty -- return DENY
57 auto policyAnswer = storage.checkPolicy(pk);
58 ASSERT_EQ(PolicyType::DENY, policyAnswer.policyType());
61 TEST(storage, checkSimple) {
62 using ::testing::ReturnPointee;
65 FakeStorageBackend backend;
67 Cynara::Storage storage(backend);
68 PolicyKey pk = Helpers::generatePolicyKey();
70 EXPECT_CALL(backend, searchDefaultBucket(pk))
71 .WillRepeatedly(ReturnPointee(&bucket));
73 // Default bucket empty -- return DENY
74 ASSERT_EQ(PolicyType::DENY, storage.checkPolicy(pk).policyType());
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());
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());
85 TEST(storage, checkBucket) {
86 using ::testing::ReturnPointee;
88 const PolicyBucketId additionalBucketId = "additional-bucket";
90 FakeStorageBackend backend;
91 Cynara::Storage storage(backend);
92 PolicyKey pk = Helpers::generatePolicyKey();
94 PolicyBucket defaultBucket(PolicyCollection({
95 Policy::simpleWithKey(pk, PolicyType::ALLOW),
96 Policy::bucketWithKey(pk, additionalBucketId)
99 PolicyBucket additionalBucket;
101 EXPECT_CALL(backend, searchDefaultBucket(pk))
102 .WillRepeatedly(ReturnPointee(&defaultBucket));
104 EXPECT_CALL(backend, searchBucket(defaultPolicyBucketId, pk))
105 .WillRepeatedly(ReturnPointee(&defaultBucket));
107 EXPECT_CALL(backend, searchBucket(additionalBucketId, pk))
108 .WillRepeatedly(ReturnPointee(&additionalBucket));
111 // Bucket empty -- should return DENY as default bucket value
112 ASSERT_EQ(PolicyType::DENY, storage.checkPolicy(pk).policyType());
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());
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());