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_load.cpp
18 * @author Aleksander Zdyb <a.zdyb@samsung.com>
20 * @brief Tests for dump => load routine
25 #include <gmock/gmock.h>
26 #include <gtest/gtest.h>
28 #include <storage/BucketDeserializer.h>
29 #include <storage/StorageSerializer.h>
30 #include <types/Policy.h>
31 #include <types/PolicyBucket.h>
33 #include "../../helpers.h"
35 using namespace Cynara;
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());
43 // Test if dumping and loading yields the same result
44 TEST(dump_load, bucket) {
45 using ::testing::UnorderedElementsAre;
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 = {{ Policy::bucketWithKey(pk1, bucketId),
52 Policy::simpleWithKey(pk2, PredefinedPolicyType::DENY),
53 Policy::bucketWithKey(pk3, bucketId) }};
55 auto ioStream = std::make_shared<std::stringstream>();
57 StorageSerializer serializer(ioStream);
58 serializer.dump(bucket);
60 BucketDeserializer deserializer(ioStream);
61 const auto loadedPolicies = deserializer.loadPolicies();
62 PolicyCollection expectedPolicies;
63 std::copy(bucket.begin(), bucket.end(), std::back_inserter(expectedPolicies));
65 ASSERT_THAT(loadedPolicies, UnorderedElementsAre(
66 PolicyAtPtrEq(expectedPolicies.at(0)),
67 PolicyAtPtrEq(expectedPolicies.at(1)),
68 PolicyAtPtrEq(expectedPolicies.at(2))