b7e2bdd3e070c3f06f5aa6bb5e96e55695111ead
[platform/core/security/cynara.git] / test / storage / inmemorystoragebackend / buckets.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        test/storage/inmemorystoragebackend/buckets.cpp
18  * @author      Aleksander Zdyb <a.zdyb@samsung.com>
19  * @version     1.0
20  * @brief       Tests of buckets in InMemeoryStorageBackend
21  */
22
23 #include <gmock/gmock.h>
24 #include <gtest/gtest.h>
25
26 #include <exceptions/BucketNotExistsException.h>
27 #include <types/PolicyBucket.h>
28 #include <types/PolicyResult.h>
29
30 #include "fakeinmemorystoragebackend.h"
31 #include "inmemeorystoragebackendfixture.h"
32
33 using namespace Cynara;
34
35 TEST_F(InMemeoryStorageBackendFixture, createBucket) {
36     using ::testing::ReturnRef;
37     using ::testing::IsEmpty;
38
39     FakeInMemoryStorageBackend backend;
40     EXPECT_CALL(backend, buckets())
41         .WillRepeatedly(ReturnRef(m_buckets));
42
43     PolicyResult defaultPolicy(PredefinedPolicyType::ALLOW);
44     PolicyBucketId bucketId = "new-bucket";
45     backend.createBucket(bucketId, defaultPolicy);
46
47     ASSERT_EQ(1, m_buckets.size());
48     ASSERT_NE(m_buckets.end(), m_buckets.find(bucketId));
49     ASSERT_EQ(defaultPolicy, m_buckets.at(bucketId).defaultPolicy());
50     ASSERT_THAT(m_buckets.at(bucketId), IsEmpty());
51 }
52
53 TEST_F(InMemeoryStorageBackendFixture, updateBucket) {
54     using ::testing::ReturnRef;
55
56     FakeInMemoryStorageBackend backend;
57     EXPECT_CALL(backend, buckets())
58         .WillRepeatedly(ReturnRef(m_buckets));
59
60     PolicyBucketId bucketId = "bucket";
61     auto &bucket = this->createBucket(bucketId);
62     bucket.setDefaultPolicy(PredefinedPolicyType::ALLOW);
63
64     backend.updateBucket(bucketId, PredefinedPolicyType::DENY);
65
66     ASSERT_EQ(PredefinedPolicyType::DENY, bucket.defaultPolicy());
67 }
68
69 TEST_F(InMemeoryStorageBackendFixture, updateNonexistentBucket) {
70     using ::testing::ReturnRef;
71
72     FakeInMemoryStorageBackend backend;
73     EXPECT_CALL(backend, buckets())
74         .WillRepeatedly(ReturnRef(m_buckets));
75
76     EXPECT_THROW(backend.updateBucket("non-existent", PredefinedPolicyType::ALLOW),
77                  BucketNotExistsException);
78 }
79
80 TEST_F(InMemeoryStorageBackendFixture, deleteBucket) {
81     using ::testing::ReturnRef;
82     using ::testing::IsEmpty;
83
84     FakeInMemoryStorageBackend backend;
85     EXPECT_CALL(backend, buckets())
86         .WillRepeatedly(ReturnRef(m_buckets));
87
88     PolicyBucketId bucketId = "delete-bucket";
89     m_buckets.insert({ bucketId, PolicyBucket() });
90
91     backend.deleteBucket(bucketId);
92
93     ASSERT_THAT(m_buckets, IsEmpty());
94 }
95
96 TEST_F(InMemeoryStorageBackendFixture, hasBucket) {
97     using ::testing::ReturnRef;
98     using ::testing::IsEmpty;
99
100     FakeInMemoryStorageBackend backend;
101     EXPECT_CALL(backend, buckets())
102         .WillRepeatedly(ReturnRef(m_buckets));
103
104     PolicyBucketId bucketId = "bucket";
105     m_buckets.insert({ bucketId, PolicyBucket() });
106
107     ASSERT_TRUE(backend.hasBucket(bucketId));
108     ASSERT_FALSE(backend.hasBucket("non-existent"));
109 }
110
111 TEST_F(InMemeoryStorageBackendFixture, deleteNonexistentBucket) {
112     using ::testing::ReturnRef;
113
114     FakeInMemoryStorageBackend backend;
115     EXPECT_CALL(backend, buckets())
116         .WillRepeatedly(ReturnRef(m_buckets));
117
118     EXPECT_THROW(backend.deleteBucket("non-existent"), BucketNotExistsException);
119 }