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 policies in Storage
23 #include <gtest/gtest.h>
24 #include <gmock/gmock.h>
26 #include "types/PolicyType.h"
27 #include "types/PolicyKey.h"
28 #include "types/PolicyResult.h"
29 #include "types/PolicyCollection.h"
30 #include "types/pointers.h"
31 #include "exceptions/DefaultBucketDeletionException.h"
32 #include "storage/Storage.h"
33 #include "storage/StorageBackend.h"
36 #include "fakestoragebackend.h"
37 #include "../../helpers.h"
44 using namespace Cynara;
46 TEST(storage, deletePolicies) {
49 using ::testing::TypedEq;
50 FakeStorageBackend backend;
51 Cynara::Storage storage(backend);
53 auto pk1 = Helpers::generatePolicyKey("1");
54 auto pk2 = Helpers::generatePolicyKey("2");
55 PolicyBucketId bucketId1 = "bucket-1";
56 PolicyBucketId bucketId2 = "bucket-2";
58 EXPECT_CALL(backend, deletePolicy(bucketId1, pk1));
59 EXPECT_CALL(backend, deletePolicy(bucketId2, pk1));
60 EXPECT_CALL(backend, deletePolicy(bucketId1, pk2));
62 std::map<PolicyBucketId, std::vector<PolicyKey>> policies;
63 policies[bucketId1].push_back(pk1);
64 policies[bucketId2].push_back(pk1);
65 policies[bucketId1].push_back(pk2);
67 storage.deletePolicies(policies);
70 // TODO: isn't it the same test as storage.deleteBucket?
71 TEST(storage, deleteBucketWithLinkedPolicies) {
72 FakeStorageBackend backend;
73 Cynara::Storage storage(backend);
75 PolicyBucketId bucketId = "test-bucket";
77 EXPECT_CALL(backend, deleteLinking(bucketId));
78 EXPECT_CALL(backend, deleteBucket(bucketId));
80 storage.deleteBucket(bucketId);
84 TEST(storage, insertPolicies) {
85 using ::testing::Return;
87 FakeStorageBackend backend;
88 Cynara::Storage storage(backend);
90 PolicyBucketId testBucket1 = "test-bucket-1";
91 PolicyBucketId testBucket2 = "test-bucket-2";
93 std::map<PolicyBucketId, std::vector<Policy>> policiesToInsert;
94 policiesToInsert[testBucket1].push_back(Policy(Helpers::generatePolicyKey("1"), PredefinedPolicyType::ALLOW));
95 policiesToInsert[testBucket1].push_back(Policy(Helpers::generatePolicyKey("2"), PredefinedPolicyType::DENY));
96 policiesToInsert[testBucket1].push_back(Policy(Helpers::generatePolicyKey("3"), PredefinedPolicyType::DENY));
97 policiesToInsert[testBucket2].push_back(Policy(Helpers::generatePolicyKey("4"), PredefinedPolicyType::ALLOW));
98 policiesToInsert[testBucket2].push_back(Policy(Helpers::generatePolicyKey("5"), PredefinedPolicyType::ALLOW));
100 for (const auto &bucket : policiesToInsert) {
101 const PolicyBucketId &bucketId = bucket.first;
102 for (const auto &policy : bucket.second) {
103 EXPECT_CALL(backend, searchBucket(bucketId, policy.key()))
104 .WillOnce(Return(PolicyBucket()));
105 EXPECT_CALL(backend, insertPolicy(bucketId, _));
109 storage.insertPolicies(policiesToInsert);
112 TEST(storage, updatePolicies) {
113 using ::testing::Return;
115 FakeStorageBackend backend;
116 Cynara::Storage storage(backend);
118 PolicyBucketId testBucket1 = "test-bucket-1";
119 PolicyBucketId testBucket2 = "test-bucket-2";
121 std::map<PolicyBucketId, std::vector<Policy>> policiesToInsert;
122 policiesToInsert[testBucket1].push_back(Policy(Helpers::generatePolicyKey("1"), PredefinedPolicyType::ALLOW));
123 policiesToInsert[testBucket1].push_back(Policy(Helpers::generatePolicyKey("2"), PredefinedPolicyType::DENY));
124 policiesToInsert[testBucket1].push_back(Policy(Helpers::generatePolicyKey("3"), PredefinedPolicyType::DENY));
125 policiesToInsert[testBucket2].push_back(Policy(Helpers::generatePolicyKey("4"), PredefinedPolicyType::ALLOW));
126 policiesToInsert[testBucket2].push_back(Policy(Helpers::generatePolicyKey("5"), PredefinedPolicyType::ALLOW));
128 storage.insertPolicies(policiesToInsert);
130 for (const auto &bucket : policiesToInsert) {
131 const PolicyBucketId &bucketId = bucket.first;
132 for (const auto &policy : bucket.second) {
133 EXPECT_CALL(backend, searchBucket(bucketId, policy.key()));
134 EXPECT_CALL(backend, deletePolicy(bucketId, policy.key()));
135 EXPECT_CALL(backend, insertPolicy(bucketId, _));
139 storage.insertPolicies(policiesToInsert);