PageRenderTime 27ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/test/storage/serializer/bucket_load.cpp

https://gitlab.com/github-cloud-corporation/cynara
C++ | 192 lines | 128 code | 42 blank | 22 comment | 1 complexity | 2a13311c1fd7e4292e8bb80b4d83efa9 MD5 | raw file
  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/bucket_load.cpp
  18. * @author Aleksander Zdyb <a.zdyb@samsung.com>
  19. * @version 1.0
  20. * @brief Tests for Cynara::BucketDeserializer
  21. */
  22. #include <memory>
  23. #include <sstream>
  24. #include <tuple>
  25. #include <vector>
  26. #include <gmock/gmock.h>
  27. #include <gtest/gtest.h>
  28. #include <exceptions/BucketRecordCorruptedException.h>
  29. #include <storage/BucketDeserializer.h>
  30. #include <types/Policy.h>
  31. #include "../../helpers.h"
  32. using namespace Cynara;
  33. MATCHER_P(PolicyAtPtrEq, policy, "") {
  34. return std::tie(policy->key(), policy->result())
  35. == std::tie(arg->key(), arg->result());
  36. }
  37. class BucketDeserializerFixture : public ::testing::Test {
  38. public:
  39. virtual ~BucketDeserializerFixture() {};
  40. PolicyPtr createPolicy(const PolicyKey &pk, const PolicyResult &pr) {
  41. return std::make_shared<Policy>(pk, pr);
  42. }
  43. void checkCorruptedData(const std::string &data, const std::string &corruptedLine,
  44. size_t corruptedLineNumber) {
  45. auto bucketStream = std::make_shared<std::istringstream>(data);
  46. BucketDeserializer deserializer(bucketStream);
  47. EXPECT_THROW(deserializer.loadPolicies(), BucketRecordCorruptedException);
  48. bucketStream->seekg(0);
  49. try {
  50. deserializer.loadPolicies();
  51. } catch (const BucketRecordCorruptedException &ex) {
  52. ASSERT_EQ(corruptedLine, ex.line());
  53. ASSERT_EQ(corruptedLineNumber, ex.lineNumber());
  54. }
  55. }
  56. };
  57. TEST_F(BucketDeserializerFixture, load_empty) {
  58. using ::testing::IsEmpty;
  59. auto bucketStream = std::make_shared<std::istringstream>();
  60. BucketDeserializer deserializer(bucketStream);
  61. auto policies = deserializer.loadPolicies();
  62. ASSERT_THAT(policies, IsEmpty());
  63. }
  64. TEST_F(BucketDeserializerFixture, load_1) {
  65. using ::testing::UnorderedElementsAre;
  66. auto bucketStream = std::make_shared<std::istringstream>("c;u;p;0;meta");
  67. BucketDeserializer deserializer(bucketStream);
  68. auto policies = deserializer.loadPolicies();
  69. auto expectedPolicy = createPolicy({ "c", "u", "p" }, { PredefinedPolicyType::DENY, "meta" });
  70. ASSERT_THAT(policies, UnorderedElementsAre(PolicyAtPtrEq(expectedPolicy)));
  71. }
  72. TEST_F(BucketDeserializerFixture, load_1_allow) {
  73. using ::testing::UnorderedElementsAre;
  74. auto bucketStream = std::make_shared<std::istringstream>("c;u;p;0xFFFF;meta");
  75. BucketDeserializer deserializer(bucketStream);
  76. auto policies = deserializer.loadPolicies();
  77. auto expectedPolicy = createPolicy({ "c", "u", "p" }, { PredefinedPolicyType::ALLOW, "meta" });
  78. ASSERT_THAT(policies, UnorderedElementsAre(PolicyAtPtrEq(expectedPolicy)));
  79. }
  80. TEST_F(BucketDeserializerFixture, load_1_no_meta_sep) {
  81. using ::testing::UnorderedElementsAre;
  82. auto bucketStream = std::make_shared<std::istringstream>("c;u;p;0;");
  83. BucketDeserializer deserializer(bucketStream);
  84. auto policies = deserializer.loadPolicies();
  85. auto expectedPolicy = createPolicy({ "c", "u", "p" }, { PredefinedPolicyType::DENY, "" });
  86. ASSERT_THAT(policies, UnorderedElementsAre(PolicyAtPtrEq(expectedPolicy)));
  87. }
  88. TEST_F(BucketDeserializerFixture, load_1_no_meta_no_sep) {
  89. using ::testing::UnorderedElementsAre;
  90. auto bucketStream = std::make_shared<std::istringstream>("c;u;p;0");
  91. BucketDeserializer deserializer(bucketStream);
  92. auto policies = deserializer.loadPolicies();
  93. auto expectedPolicy = createPolicy({ "c", "u", "p" }, { PredefinedPolicyType::DENY, "" });
  94. ASSERT_THAT(policies, UnorderedElementsAre(PolicyAtPtrEq(expectedPolicy)));
  95. }
  96. TEST_F(BucketDeserializerFixture, load_2) {
  97. using ::testing::UnorderedElementsAre;
  98. auto bucketStream = std::make_shared<std::istringstream>("c;u;p;0;meta\n"
  99. "c;u;p;0;meta\n");
  100. BucketDeserializer deserializer(bucketStream);
  101. auto policies = deserializer.loadPolicies();
  102. auto expectedPolicy = createPolicy({ "c", "u", "p" }, { PredefinedPolicyType::DENY, "meta" });
  103. ASSERT_THAT(policies, UnorderedElementsAre(PolicyAtPtrEq(expectedPolicy),
  104. PolicyAtPtrEq(expectedPolicy)));
  105. }
  106. TEST_F(BucketDeserializerFixture, load_mixed) {
  107. using ::testing::UnorderedElementsAre;
  108. using ::testing::UnorderedElementsAreArray;
  109. auto bucketStream = std::make_shared<std::istringstream>("c1;u1;p1;0;meta\n"
  110. "c2;u2;p2;0xFFFF;meta2\n"
  111. "c2;u2;p2;0xFFFF;\n"
  112. "c1;u1;p3;0xFFFE;bucket\n");
  113. BucketDeserializer deserializer(bucketStream);
  114. auto policies = deserializer.loadPolicies();
  115. PolicyCollection expectedPolices = {
  116. createPolicy({ "c1", "u1", "p1" }, { PredefinedPolicyType::DENY, "meta" }),
  117. createPolicy({ "c2", "u2", "p2" }, { PredefinedPolicyType::ALLOW, "meta2" }),
  118. createPolicy({ "c2", "u2", "p2" }, { PredefinedPolicyType::ALLOW, "" }),
  119. createPolicy({ "c1", "u1", "p3" }, { PredefinedPolicyType::BUCKET, "bucket" })
  120. };
  121. // How to do it more elegantly?
  122. ASSERT_THAT(policies, UnorderedElementsAre(
  123. PolicyAtPtrEq(expectedPolices.at(0)),
  124. PolicyAtPtrEq(expectedPolices.at(1)),
  125. PolicyAtPtrEq(expectedPolices.at(2)),
  126. PolicyAtPtrEq(expectedPolices.at(3))
  127. ));
  128. }
  129. TEST_F(BucketDeserializerFixture, load_no_client) {
  130. auto data = "u;p;0;meta";
  131. checkCorruptedData(data, data, 1);
  132. }
  133. TEST_F(BucketDeserializerFixture, load_no_type) {
  134. auto data = "c;u;p;meta";
  135. checkCorruptedData(data, data, 1);
  136. }
  137. TEST_F(BucketDeserializerFixture, load_all_missing) {
  138. auto data = ";;;";
  139. checkCorruptedData(data, data, 1);
  140. }
  141. TEST_F(BucketDeserializerFixture, load_invalid_type) {
  142. auto data = "c;u;p;X";
  143. checkCorruptedData(data, data, 1);
  144. }
  145. TEST_F(BucketDeserializerFixture, load_invalid_multiline) {
  146. auto data = "c1;u1;p1;0;meta\n"
  147. "c;u;p;X\n"
  148. "c1;u1;p1;0;meta\n";
  149. checkCorruptedData(data, "c;u;p;X", 2);
  150. }