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"
42 using namespace Cynara;
44 TEST(storage, deletePolicies) {
47 using ::testing::TypedEq;
48 FakeStorageBackend backend;
49 Cynara::Storage storage(backend);
51 auto pk1 = Helpers::generatePolicyKey("1");
52 auto pk2 = Helpers::generatePolicyKey("2");
53 PolicyBucketId bucketId1 = "bucket";
54 PolicyBucketId bucketId2 = "bucket-2";
57 EXPECT_CALL(backend, deletePolicy(bucketId1, pk1));
58 EXPECT_CALL(backend, deletePolicy(bucketId2, pk1));
59 EXPECT_CALL(backend, deletePolicy(bucketId1, pk2));
61 storage.deletePolicies({
62 std::make_tuple(pk1, bucketId1),
63 std::make_tuple(pk1, bucketId2),
64 std::make_tuple(pk2, bucketId1),
68 // TODO: isn't it the same test as storage.deleteBucket?
69 TEST(storage, deleteBucketWithLinkedPolicies) {
70 FakeStorageBackend backend;
71 Cynara::Storage storage(backend);
73 PolicyBucketId bucketId = "test-bucket";
75 EXPECT_CALL(backend, deleteLinking(bucketId));
76 EXPECT_CALL(backend, deleteBucket(bucketId));
78 storage.deleteBucket(bucketId);
82 TEST(storage, insertPolicies) {
83 using ::testing::Return;
84 FakeStorageBackend backend;
85 Cynara::Storage storage(backend);
87 PolicyBucketId testBucket1 = "test-bucket-1";
88 PolicyBucketId testBucket2 = "test-bucket-2";
90 std::vector<Storage::PolicyPolicyBucket> policiesToInsert = {
91 std::make_tuple(Policy::simpleWithKey(Helpers::generatePolicyKey("1"), PredefinedPolicyType::ALLOW), testBucket1),
92 std::make_tuple(Policy::simpleWithKey(Helpers::generatePolicyKey("2"), PredefinedPolicyType::DENY), testBucket1),
93 std::make_tuple(Policy::simpleWithKey(Helpers::generatePolicyKey("3"), PredefinedPolicyType::DENY), testBucket1),
94 std::make_tuple(Policy::simpleWithKey(Helpers::generatePolicyKey("4"), PredefinedPolicyType::ALLOW), testBucket2),
95 std::make_tuple(Policy::simpleWithKey(Helpers::generatePolicyKey("5"), PredefinedPolicyType::ALLOW), testBucket2),
98 for (const auto &policy : policiesToInsert) {
99 PolicyBucketId bucketId;
101 std::tie(policyPtr, bucketId) = policy;
102 EXPECT_CALL(backend, searchBucket(bucketId, policyPtr->key()))
103 .WillOnce(Return(PolicyBucket()));
104 EXPECT_CALL(backend, insertPolicy(bucketId, policyPtr));
107 storage.insertPolicies(policiesToInsert);
110 TEST(storage, updatePolicies) {
111 using ::testing::Return;
112 FakeStorageBackend backend;
113 Cynara::Storage storage(backend);
115 PolicyBucketId testBucket1 = "test-bucket-1";
116 PolicyBucketId testBucket2 = "test-bucket-2";
118 std::vector<Storage::PolicyPolicyBucket> policiesToInsert = {
119 std::make_tuple(Policy::simpleWithKey(Helpers::generatePolicyKey("1"), PredefinedPolicyType::ALLOW), testBucket1),
120 std::make_tuple(Policy::simpleWithKey(Helpers::generatePolicyKey("2"), PredefinedPolicyType::DENY), testBucket1),
121 std::make_tuple(Policy::simpleWithKey(Helpers::generatePolicyKey("3"), PredefinedPolicyType::DENY), testBucket1),
122 std::make_tuple(Policy::simpleWithKey(Helpers::generatePolicyKey("4"), PredefinedPolicyType::ALLOW), testBucket2),
123 std::make_tuple(Policy::simpleWithKey(Helpers::generatePolicyKey("5"), PredefinedPolicyType::ALLOW), testBucket2),
127 PolicyCollection pc({std::get<0>(policiesToInsert.at(0))});
129 for (const auto &policy : policiesToInsert) {
130 PolicyBucketId bucketId;
132 std::tie(policyPtr, bucketId) = policy;
133 EXPECT_CALL(backend, searchBucket(bucketId, policyPtr->key()))
134 .WillOnce(Return(PolicyBucket(PolicyCollection({std::get<0>(policy)}))));
135 EXPECT_CALL(backend, deletePolicy(bucketId, policyPtr->key()));
136 EXPECT_CALL(backend, insertPolicy(bucketId, policyPtr));
139 storage.insertPolicies(policiesToInsert);