Make StorageSerializer a template class
[platform/core/security/cynara.git] / test / storage / serializer / dump_load.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/dump_load.cpp
18  * @author      Aleksander Zdyb <a.zdyb@samsung.com>
19  * @version     1.0
20  * @brief       Tests for dump => load routine
21  */
22
23 #include <sstream>
24
25 #include <gmock/gmock.h>
26 #include <gtest/gtest.h>
27
28 #include <storage/BucketDeserializer.h>
29 #include <storage/StorageSerializer.h>
30 #include <types/Policy.h>
31 #include <types/PolicyBucket.h>
32
33 #include "../../helpers.h"
34
35 using namespace Cynara;
36
37 // TODO: Move to helpers or other .h
38 MATCHER_P(PolicyAtPtrEq, policy, "") {
39     return std::tie(policy->key(), policy->result())
40         == std::tie(arg->key(), arg->result());
41 }
42
43 // Test if dumping and loading yields the same result
44 TEST(dump_load, bucket) {
45     using ::testing::UnorderedElementsAre;
46
47     PolicyKey pk1 = Helpers::generatePolicyKey("1");
48     PolicyKey pk2 = Helpers::generatePolicyKey("2");
49     PolicyKey pk3 = Helpers::generatePolicyKey("3");
50     PolicyBucketId bucketId = Helpers::generateBucketId();
51     PolicyBucket bucket = {"bucket", { Policy::bucketWithKey(pk1, bucketId),
52                                        Policy::simpleWithKey(pk2, PredefinedPolicyType::DENY),
53                                        Policy::bucketWithKey(pk3, bucketId) }};
54
55     auto ioStream = std::make_shared<std::stringstream>();
56
57     StorageSerializer<std::stringstream> serializer(ioStream);
58     serializer.dump(bucket);
59
60     BucketDeserializer deserializer(ioStream);
61     const auto loadedPolicies = deserializer.loadPolicies();
62     PolicyCollection expectedPolicies;
63     std::copy(bucket.begin(), bucket.end(), std::back_inserter(expectedPolicies));
64
65     ASSERT_THAT(loadedPolicies, UnorderedElementsAre(
66         PolicyAtPtrEq(expectedPolicies.at(0)),
67         PolicyAtPtrEq(expectedPolicies.at(1)),
68         PolicyAtPtrEq(expectedPolicies.at(2))
69     ));
70 }