2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file test/storage/serializer/dump.cpp
18 * @author Aleksander Zdyb <a.zdyb@samsung.com>
20 * @brief Tests for dumping feature of Cynara::StorageSerializer
27 #include <gmock/gmock.h>
28 #include <gtest/gtest.h>
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>
36 #include "../../helpers.h"
38 using namespace Cynara;
40 static std::string expectedPolicyKey(const PolicyKey &key) {
41 return key.client().toString() + ";" + key.user().toString() + ";" + key.privilege().toString();
44 static std::string expectedPolicyType(const PolicyType &type) {
45 std::ostringstream oss;
46 oss << std::hex << std::uppercase << "0x" << type;
50 TEST(serializer_dump, dump_empty_bucket) {
51 auto oss = std::make_shared<std::ostringstream>();
54 StorageSerializer serializer(oss);
55 serializer.dump(bucket);
57 ASSERT_EQ("", oss->str());
60 TEST(serializer_dump, dump_bucket) {
61 using ::testing::UnorderedElementsAreArray;
62 using PredefinedPolicyType::ALLOW;
63 using PredefinedPolicyType::DENY;
65 PolicyKey pk1 = Helpers::generatePolicyKey("1");
66 PolicyKey pk2 = Helpers::generatePolicyKey("2");
68 PolicyBucket bucket = {{ Policy::simpleWithKey(pk1, ALLOW),
69 Policy::simpleWithKey(pk2, DENY) }};
71 auto outStream = std::make_shared<std::stringstream>();
72 StorageSerializer serializer(outStream);
73 serializer.dump(bucket);
75 // Split stream into records
76 auto actualRecords = std::vector<std::string>(std::istream_iterator<std::string>(*outStream),
77 std::istream_iterator<std::string>());
79 std::vector<std::string> expectedRecords = {
80 expectedPolicyKey(pk1) + ";" + expectedPolicyType(ALLOW) + ";",
81 expectedPolicyKey(pk2) + ";" + expectedPolicyType(DENY) + ";"
84 ASSERT_THAT(actualRecords, UnorderedElementsAreArray(expectedRecords));
87 TEST(serializer_dump, dump_bucket_bucket) {
88 using ::testing::UnorderedElementsAreArray;
89 using PredefinedPolicyType::BUCKET;
90 using PredefinedPolicyType::DENY;
92 PolicyKey pk1 = Helpers::generatePolicyKey("1");
93 PolicyKey pk2 = Helpers::generatePolicyKey("2");
94 PolicyKey pk3 = Helpers::generatePolicyKey("3");
95 PolicyBucketId bucketId = Helpers::generateBucketId();
97 PolicyBucket bucket = {{ Policy::bucketWithKey(pk1, bucketId),
98 Policy::simpleWithKey(pk2, DENY),
99 Policy::bucketWithKey(pk3, bucketId) }};
101 auto outStream = std::make_shared<std::stringstream>();
102 StorageSerializer serializer(outStream);
103 serializer.dump(bucket);
105 // Split stream into records
106 auto actualRecords = std::vector<std::string>(std::istream_iterator<std::string>(*outStream),
107 std::istream_iterator<std::string>());
109 std::vector<std::string> expectedRecords = {
110 expectedPolicyKey(pk1) + ";" + expectedPolicyType(BUCKET) + ";" + bucketId,
111 expectedPolicyKey(pk2) + ";" + expectedPolicyType(DENY) + ";",
112 expectedPolicyKey(pk3) + ";" + expectedPolicyType(BUCKET) + ";" + bucketId
115 ASSERT_THAT(actualRecords, UnorderedElementsAreArray(expectedRecords));