Make StorageSerializer a template class
[platform/core/security/cynara.git] / test / storage / serializer / serialize.cpp
1 /*
2  * Copyright (c) 2014-2015 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/serializer/serialize.cpp
18  * @author      Aleksander Zdyb <a.zdyb@samsung.com>
19  * @version     1.0
20  * @brief       Tests for dumping feature of Cynara::StorageSerializer
21  */
22
23 #include <iterator>
24 #include <memory>
25 #include <sstream>
26 #include <string>
27 #include <vector>
28
29 #include <gmock/gmock.h>
30 #include <gtest/gtest.h>
31
32 #include <exceptions/BucketSerializationException.h>
33 #include <storage/Buckets.h>
34 #include <storage/InMemoryStorageBackend.h>
35 #include <storage/StorageSerializer.h>
36 #include <types/PolicyBucketId.h>
37
38 class FakeStreamForBucketId {
39 public:
40     typedef std::shared_ptr<Cynara::StorageSerializer<std::stringstream> >
41             StringstreamStorageSerializerPtr;
42
43     MOCK_METHOD1(streamForBucketId,
44                  StringstreamStorageSerializerPtr(const Cynara::PolicyBucketId &));
45
46     Cynara::StorageSerializer<std::stringstream>::BucketStreamOpener streamOpener() {
47         return std::bind(&FakeStreamForBucketId::streamForBucketId, this, std::placeholders::_1);
48     }
49 };
50
51 // Fake StorageSerializer for Cynara::PolicyBucket
52 class FakeStorageSerializer : public Cynara::StorageSerializer<std::stringstream> {
53 public:
54     FakeStorageSerializer(std::shared_ptr<std::stringstream> o)
55     : Cynara::StorageSerializer<std::stringstream>(o), outStream(o) {}
56     MOCK_METHOD1(dump, void(const Cynara::PolicyBucket &bucket));
57     std::shared_ptr<std::stringstream> outStream;
58 };
59
60 class StorageSerializerFixture : public ::testing::Test {
61 public:
62     virtual ~StorageSerializerFixture() {};
63
64     Cynara::Buckets buckets;
65     FakeStreamForBucketId fakeStreamOpener;
66 };
67
68 using namespace Cynara;
69
70 // Be sure no calls to streamForBucketId() are made
71 // and output stream is not touched
72 TEST_F(StorageSerializerFixture, dump_buckets_empty) {
73     auto outStream = std::make_shared<std::stringstream>();
74     StorageSerializer<std::stringstream> serializer(outStream);
75     serializer.dump(Buckets(), fakeStreamOpener.streamOpener());
76
77     // Stream should be empty
78     ASSERT_EQ(0, outStream->tellp());
79 }
80
81 TEST_F(StorageSerializerFixture, dump_buckets) {
82     using ::testing::_;
83     using ::testing::Property;
84     using ::testing::Return;
85     using ::testing::UnorderedElementsAreArray;
86
87     // Will be returned as serializer for buckets
88     auto fakeBucketSerializer = std::make_shared<FakeStorageSerializer>(
89             std::make_shared<std::stringstream>());
90
91     buckets = {
92         { "bucket1", PolicyBucket("bucket1", PredefinedPolicyType::DENY) },
93         { "bucket2", PolicyBucket("bucket2", PredefinedPolicyType::DENY) },
94         { "bucket3",
95             PolicyBucket("bucket3", PolicyResult(PredefinedPolicyType::BUCKET, "bucket2")) }
96     };
97
98     auto outStream = std::make_shared<std::stringstream>();
99     StorageSerializer<std::stringstream> dbSerializer(outStream);
100
101     // Make sure stream was opened for each bucket
102     EXPECT_CALL(fakeStreamOpener, streamForBucketId(_))
103         .Times(buckets.size()).WillRepeatedly(Return(fakeBucketSerializer));
104
105     // Make sure every bucket was dumped
106     for (const auto &bucket : buckets) {
107         EXPECT_CALL(*fakeBucketSerializer, dump(Property(&PolicyBucket::id, bucket.first)));
108     }
109
110     dbSerializer.dump(buckets, fakeStreamOpener.streamOpener());
111
112     std::vector<std::string> expectedRecords = {
113         "bucket1;0x0;",
114         "bucket2;0x0;",
115         "bucket3;0xFFFE;bucket2"
116     };
117
118     // Split stream into records
119     auto actualRecords = std::vector<std::string>(std::istream_iterator<std::string>(*outStream),
120                                                   std::istream_iterator<std::string>());
121
122     ASSERT_THAT(actualRecords, UnorderedElementsAreArray(expectedRecords));
123 }
124
125 TEST_F(StorageSerializerFixture, dump_buckets_io_error) {
126     using ::testing::_;
127     using ::testing::Return;
128
129     buckets = {
130         { "bucket1", PolicyBucket("bucket1", PredefinedPolicyType::DENY) },
131     };
132
133     auto outStream = std::make_shared<std::stringstream>();
134     StorageSerializer<std::stringstream> dbSerializer(outStream);
135
136     // Make sure stream was opened for each bucket
137     EXPECT_CALL(fakeStreamOpener, streamForBucketId(_))
138         .Times(buckets.size()).WillRepeatedly(Return(nullptr));
139
140     ASSERT_THROW(
141         dbSerializer.dump(buckets, fakeStreamOpener.streamOpener()),
142         BucketSerializationException
143     );
144 }