Update tests and remove unused Storage methods
[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 <map>
40 #include <memory>
41 #include <tuple>
42 #include <vector>
43
44 using namespace Cynara;
45
46 TEST(storage, deletePolicies) {
47     using ::testing::_;
48     using ::testing::Eq;
49     using ::testing::TypedEq;
50     FakeStorageBackend backend;
51     Cynara::Storage storage(backend);
52
53     auto pk1 = Helpers::generatePolicyKey("1");
54     auto pk2 = Helpers::generatePolicyKey("2");
55     PolicyBucketId bucketId1 = "bucket-1";
56     PolicyBucketId bucketId2 = "bucket-2";
57
58     EXPECT_CALL(backend, deletePolicy(bucketId1, pk1));
59     EXPECT_CALL(backend, deletePolicy(bucketId2, pk1));
60     EXPECT_CALL(backend, deletePolicy(bucketId1, pk2));
61
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);
66
67     storage.deletePolicies(policies);
68 }
69
70 // TODO: isn't it the same test as storage.deleteBucket?
71 TEST(storage, deleteBucketWithLinkedPolicies) {
72     FakeStorageBackend backend;
73     Cynara::Storage storage(backend);
74
75     PolicyBucketId bucketId = "test-bucket";
76
77     EXPECT_CALL(backend, deleteLinking(bucketId));
78     EXPECT_CALL(backend, deleteBucket(bucketId));
79
80     storage.deleteBucket(bucketId);
81 }
82
83
84 TEST(storage, insertPolicies) {
85     using ::testing::Return;
86     using ::testing::_;
87     FakeStorageBackend backend;
88     Cynara::Storage storage(backend);
89
90     PolicyBucketId testBucket1 = "test-bucket-1";
91     PolicyBucketId testBucket2 = "test-bucket-2";
92
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));
99
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, _));
106         }
107     }
108
109     storage.insertPolicies(policiesToInsert);
110 }
111
112 TEST(storage, updatePolicies) {
113     using ::testing::Return;
114     using ::testing::_;
115     FakeStorageBackend backend;
116     Cynara::Storage storage(backend);
117
118     PolicyBucketId testBucket1 = "test-bucket-1";
119     PolicyBucketId testBucket2 = "test-bucket-2";
120
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));
127
128     storage.insertPolicies(policiesToInsert);
129
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, _));
136         }
137     }
138
139     storage.insertPolicies(policiesToInsert);
140 }