8fcf90078d05592d424a2576fcddba6e9642b852
[platform/core/security/cynara.git] / test / storage / storage / policies.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 policies in Storage
21  */
22
23 #include <gtest/gtest.h>
24 #include <gmock/gmock.h>
25
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"
34
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, deletePolicies) {
45     using ::testing::_;
46     using ::testing::Eq;
47     using ::testing::TypedEq;
48     FakeStorageBackend backend;
49     Cynara::Storage storage(backend);
50
51     auto pk1 = Helpers::generatePolicyKey("1");
52     auto pk2 = Helpers::generatePolicyKey("2");
53     PolicyBucketId bucketId1 = "bucket";
54     PolicyBucketId bucketId2 = "bucket-2";
55
56
57     EXPECT_CALL(backend, deletePolicy(bucketId1, pk1));
58     EXPECT_CALL(backend, deletePolicy(bucketId2, pk1));
59     EXPECT_CALL(backend, deletePolicy(bucketId1, pk2));
60
61     storage.deletePolicies({
62         std::make_tuple(pk1, bucketId1),
63         std::make_tuple(pk1, bucketId2),
64         std::make_tuple(pk2, bucketId1),
65     });
66 }
67
68 // TODO: isn't it the same test as storage.deleteBucket?
69 TEST(storage, deleteBucketWithLinkedPolicies) {
70     FakeStorageBackend backend;
71     Cynara::Storage storage(backend);
72
73     PolicyBucketId bucketId = "test-bucket";
74
75     EXPECT_CALL(backend, deleteLinking(bucketId));
76     EXPECT_CALL(backend, deleteBucket(bucketId));
77
78     storage.deleteBucket(bucketId);
79 }
80
81
82 TEST(storage, insertPolicies) {
83     using ::testing::Return;
84     FakeStorageBackend backend;
85     Cynara::Storage storage(backend);
86
87     PolicyBucketId testBucket1 = "test-bucket-1";
88     PolicyBucketId testBucket2 = "test-bucket-2";
89
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),
96     };
97
98     for (const auto &policy : policiesToInsert) {
99         PolicyBucketId bucketId;
100         PolicyPtr policyPtr;
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));
105     }
106
107     storage.insertPolicies(policiesToInsert);
108 }
109
110 TEST(storage, updatePolicies) {
111     using ::testing::Return;
112     FakeStorageBackend backend;
113     Cynara::Storage storage(backend);
114
115     PolicyBucketId testBucket1 = "test-bucket-1";
116     PolicyBucketId testBucket2 = "test-bucket-2";
117
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),
124     };
125
126
127     PolicyCollection pc({std::get<0>(policiesToInsert.at(0))});
128
129     for (const auto &policy : policiesToInsert) {
130         PolicyBucketId bucketId;
131         PolicyPtr policyPtr;
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));
137     }
138
139     storage.insertPolicies(policiesToInsert);
140 }