53034ce4554d5f5988788f0f181b6278cfd6e7b8
[platform/core/security/cynara.git] / test / storage / serializer / dump.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/serializer/dump.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
24 #include <sstream>
25 #include <memory>
26
27 #include <gmock/gmock.h>
28 #include <gtest/gtest.h>
29
30 #include <storage/StorageSerializer.h>
31 #include <types/Policy.h>
32 #include <types/PolicyBucket.h>
33 #include <types/PolicyKey.h>
34 #include <types/PolicyType.h>
35
36 #include "../../helpers.h"
37
38 using namespace Cynara;
39
40 static std::string expectedPolicyKey(const PolicyKey &key) {
41     return key.client().toString() + ";" + key.user().toString() + ";" + key.privilege().toString();
42 }
43
44 static std::string expectedPolicyType(const PolicyType &type) {
45     std::ostringstream oss;
46     oss << std::hex << std::uppercase << "0x" << type;
47     return oss.str();
48 }
49
50 TEST(serializer_dump, dump_empty_bucket) {
51     auto oss = std::make_shared<std::ostringstream>();
52     PolicyBucket bucket;
53
54     StorageSerializer serializer(oss);
55     serializer.dump(bucket);
56
57     ASSERT_EQ("", oss->str());
58 }
59
60 TEST(serializer_dump, dump_bucket) {
61     using ::testing::UnorderedElementsAreArray;
62     using PredefinedPolicyType::ALLOW;
63     using PredefinedPolicyType::DENY;
64
65     PolicyKey pk1 = Helpers::generatePolicyKey("1");
66     PolicyKey pk2 = Helpers::generatePolicyKey("2");
67
68     PolicyBucket bucket = {{ Policy::simpleWithKey(pk1, ALLOW),
69                              Policy::simpleWithKey(pk2, DENY) }};
70
71     auto outStream = std::make_shared<std::stringstream>();
72     StorageSerializer serializer(outStream);
73     serializer.dump(bucket);
74
75     // Split stream into records
76     auto actualRecords = std::vector<std::string>(std::istream_iterator<std::string>(*outStream),
77                                                   std::istream_iterator<std::string>());
78
79     std::vector<std::string> expectedRecords = {
80         expectedPolicyKey(pk1) + ";" + expectedPolicyType(ALLOW) + ";",
81         expectedPolicyKey(pk2) + ";" + expectedPolicyType(DENY) + ";"
82     };
83
84     ASSERT_THAT(actualRecords, UnorderedElementsAreArray(expectedRecords));
85 }
86
87 TEST(serializer_dump, dump_bucket_bucket) {
88     using ::testing::UnorderedElementsAreArray;
89     using PredefinedPolicyType::BUCKET;
90     using PredefinedPolicyType::DENY;
91
92     PolicyKey pk1 = Helpers::generatePolicyKey("1");
93     PolicyKey pk2 = Helpers::generatePolicyKey("2");
94     PolicyKey pk3 = Helpers::generatePolicyKey("3");
95     PolicyBucketId bucketId = Helpers::generateBucketId();
96
97     PolicyBucket bucket = {{ Policy::bucketWithKey(pk1, bucketId),
98                              Policy::simpleWithKey(pk2, DENY),
99                              Policy::bucketWithKey(pk3, bucketId) }};
100
101     auto outStream = std::make_shared<std::stringstream>();
102     StorageSerializer serializer(outStream);
103     serializer.dump(bucket);
104
105     // Split stream into records
106     auto actualRecords = std::vector<std::string>(std::istream_iterator<std::string>(*outStream),
107                                                   std::istream_iterator<std::string>());
108
109     std::vector<std::string> expectedRecords = {
110         expectedPolicyKey(pk1) + ";" + expectedPolicyType(BUCKET) + ";" + bucketId,
111         expectedPolicyKey(pk2) + ";" + expectedPolicyType(DENY) + ";",
112         expectedPolicyKey(pk3) + ";" + expectedPolicyType(BUCKET) + ";" + bucketId
113     };
114
115     ASSERT_THAT(actualRecords, UnorderedElementsAreArray(expectedRecords));
116 }