/test/storage/serializer/dump.cpp

https://gitlab.com/admin-github-cloud/cynara · C++ · 116 lines · 68 code · 25 blank · 23 comment · 0 complexity · 9b774f64dffacaa55ddb13211f9ffc5e 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.cpp
  18. * @author Aleksander Zdyb <a.zdyb@samsung.com>
  19. * @version 1.0
  20. * @brief Tests for dumping feature of Cynara::StorageSerializer
  21. */
  22. #include <sstream>
  23. #include <memory>
  24. #include <gmock/gmock.h>
  25. #include <gtest/gtest.h>
  26. #include <storage/StorageSerializer.h>
  27. #include <types/Policy.h>
  28. #include <types/PolicyBucket.h>
  29. #include <types/PolicyKey.h>
  30. #include <types/PolicyType.h>
  31. #include "../../helpers.h"
  32. using namespace Cynara;
  33. static std::string expectedPolicyKey(const PolicyKey &key) {
  34. return key.client().toString() + ";" + key.user().toString() + ";" + key.privilege().toString();
  35. }
  36. static std::string expectedPolicyType(const PolicyType &type) {
  37. std::ostringstream oss;
  38. oss << std::hex << std::uppercase << "0x" << type;
  39. return oss.str();
  40. }
  41. TEST(serializer_dump, dump_empty_bucket) {
  42. auto oss = std::make_shared<std::ostringstream>();
  43. PolicyBucket bucket("empty");
  44. StorageSerializer<std::ostringstream> serializer(oss);
  45. serializer.dump(bucket);
  46. ASSERT_EQ("", oss->str());
  47. }
  48. TEST(serializer_dump, dump_bucket) {
  49. using ::testing::UnorderedElementsAreArray;
  50. using PredefinedPolicyType::ALLOW;
  51. using PredefinedPolicyType::DENY;
  52. PolicyKey pk1 = Helpers::generatePolicyKey("1");
  53. PolicyKey pk2 = Helpers::generatePolicyKey("2");
  54. PolicyBucket bucket("dump_bucket", PolicyCollection({ Policy::simpleWithKey(pk1, ALLOW),
  55. Policy::simpleWithKey(pk2, DENY) }));
  56. auto outStream = std::make_shared<std::stringstream>();
  57. StorageSerializer<std::stringstream> serializer(outStream);
  58. serializer.dump(bucket);
  59. // Split stream into records
  60. auto actualRecords = std::vector<std::string>(std::istream_iterator<std::string>(*outStream),
  61. std::istream_iterator<std::string>());
  62. std::vector<std::string> expectedRecords = {
  63. expectedPolicyKey(pk1) + ";" + expectedPolicyType(ALLOW) + ";",
  64. expectedPolicyKey(pk2) + ";" + expectedPolicyType(DENY) + ";"
  65. };
  66. ASSERT_THAT(actualRecords, UnorderedElementsAreArray(expectedRecords));
  67. }
  68. TEST(serializer_dump, dump_bucket_bucket) {
  69. using ::testing::UnorderedElementsAreArray;
  70. using PredefinedPolicyType::BUCKET;
  71. using PredefinedPolicyType::DENY;
  72. PolicyKey pk1 = Helpers::generatePolicyKey("1");
  73. PolicyKey pk2 = Helpers::generatePolicyKey("2");
  74. PolicyKey pk3 = Helpers::generatePolicyKey("3");
  75. PolicyBucketId bucketId = Helpers::generateBucketId();
  76. PolicyBucket bucket = {"dump_bucket_bucket", { Policy::bucketWithKey(pk1, bucketId),
  77. Policy::simpleWithKey(pk2, DENY),
  78. Policy::bucketWithKey(pk3, bucketId) }};
  79. auto outStream = std::make_shared<std::stringstream>();
  80. StorageSerializer<std::stringstream> serializer(outStream);
  81. serializer.dump(bucket);
  82. // Split stream into records
  83. auto actualRecords = std::vector<std::string>(std::istream_iterator<std::string>(*outStream),
  84. std::istream_iterator<std::string>());
  85. std::vector<std::string> expectedRecords = {
  86. expectedPolicyKey(pk1) + ";" + expectedPolicyType(BUCKET) + ";" + bucketId,
  87. expectedPolicyKey(pk2) + ";" + expectedPolicyType(DENY) + ";",
  88. expectedPolicyKey(pk3) + ";" + expectedPolicyType(BUCKET) + ";" + bucketId
  89. };
  90. ASSERT_THAT(actualRecords, UnorderedElementsAreArray(expectedRecords));
  91. }