/test/storage/serializer/dump_load.cpp

https://gitlab.com/admin-github-cloud/cynara · C++ · 70 lines · 35 code · 12 blank · 23 comment · 1 complexity · e0013d10c930921563488c5cb1e12fad MD5 · raw file

  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. #include <sstream>
  23. #include <gmock/gmock.h>
  24. #include <gtest/gtest.h>
  25. #include <storage/BucketDeserializer.h>
  26. #include <storage/StorageSerializer.h>
  27. #include <types/Policy.h>
  28. #include <types/PolicyBucket.h>
  29. #include "../../helpers.h"
  30. using namespace Cynara;
  31. // TODO: Move to helpers or other .h
  32. MATCHER_P(PolicyAtPtrEq, policy, "") {
  33. return std::tie(policy->key(), policy->result())
  34. == std::tie(arg->key(), arg->result());
  35. }
  36. // Test if dumping and loading yields the same result
  37. TEST(dump_load, bucket) {
  38. using ::testing::UnorderedElementsAre;
  39. PolicyKey pk1 = Helpers::generatePolicyKey("1");
  40. PolicyKey pk2 = Helpers::generatePolicyKey("2");
  41. PolicyKey pk3 = Helpers::generatePolicyKey("3");
  42. PolicyBucketId bucketId = Helpers::generateBucketId();
  43. PolicyBucket bucket = {"bucket", { Policy::bucketWithKey(pk1, bucketId),
  44. Policy::simpleWithKey(pk2, PredefinedPolicyType::DENY),
  45. Policy::bucketWithKey(pk3, bucketId) }};
  46. auto ioStream = std::make_shared<std::stringstream>();
  47. StorageSerializer<std::stringstream> serializer(ioStream);
  48. serializer.dump(bucket);
  49. BucketDeserializer deserializer(ioStream);
  50. const auto loadedPolicies = deserializer.loadPolicies();
  51. PolicyCollection expectedPolicies;
  52. std::copy(bucket.begin(), bucket.end(), std::back_inserter(expectedPolicies));
  53. ASSERT_THAT(loadedPolicies, UnorderedElementsAre(
  54. PolicyAtPtrEq(expectedPolicies.at(0)),
  55. PolicyAtPtrEq(expectedPolicies.at(1)),
  56. PolicyAtPtrEq(expectedPolicies.at(2))
  57. ));
  58. }