PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/test/common/types/policybucket.cpp

https://gitlab.com/github-cloud-corporation/cynara
C++ | 179 lines | 102 code | 36 blank | 41 comment | 4 complexity | 5d15c45f1c19665363ff7288bf4cbff5 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/common/types/policybucket.cpp
  18. * @author Aleksander Zdyb <a.zdyb@samsung.com>
  19. * @version 1.0
  20. * @brief Tests for Cynara::PolicyBucket
  21. */
  22. #include <algorithm>
  23. #include <tuple>
  24. #include <vector>
  25. #include <gmock/gmock.h>
  26. #include <gtest/gtest.h>
  27. #include "exceptions/InvalidBucketIdException.h"
  28. #include "types/PolicyBucket.h"
  29. #include "types/PolicyCollection.h"
  30. #include "types/PolicyKey.h"
  31. #include "../../helpers.h"
  32. using namespace Cynara;
  33. class PolicyBucketFixture : public ::testing::Test {
  34. public:
  35. virtual ~PolicyBucketFixture() {}
  36. protected:
  37. const PolicyKey pk1 = Helpers::generatePolicyKey("1");
  38. const PolicyKey pk2 = Helpers::generatePolicyKey("2");
  39. const PolicyKey pk3 = Helpers::generatePolicyKey("3");
  40. const PolicyKey otherPk = Helpers::generatePolicyKey("_");
  41. const PolicyCollection pkPolicies = {
  42. Policy::simpleWithKey(pk1, PredefinedPolicyType::ALLOW),
  43. Policy::simpleWithKey(pk2, PredefinedPolicyType::ALLOW),
  44. Policy::simpleWithKey(pk3, PredefinedPolicyType::ALLOW)
  45. };
  46. const PolicyCollection wildcardPolicies = {
  47. Policy::simpleWithKey(PolicyKey("c1", "u1", "*"), PredefinedPolicyType::ALLOW),
  48. Policy::simpleWithKey(PolicyKey("*", "u1", "p2"), PredefinedPolicyType::ALLOW),
  49. Policy::simpleWithKey(PolicyKey("*", "*", "p1"), PredefinedPolicyType::ALLOW),
  50. Policy::simpleWithKey(PolicyKey("*", "*", "*"), PredefinedPolicyType::ALLOW)
  51. };
  52. const std::vector<PolicyBucketId> goodIds = {
  53. "_goodid", "good_id", "goodid_", "-goodid", "good-id", "goodid-"
  54. };
  55. const std::vector<PolicyBucketId> badIds = {
  56. "{badid", "bad[id", "badid~", "/badid", "bad*id", "badid|", "badid;", "\tbadid", "badid\n",
  57. " badid", "bad id", "badid "
  58. };
  59. };
  60. TEST_F(PolicyBucketFixture, filtered) {
  61. using ::testing::UnorderedElementsAre;
  62. PolicyBucket bucket("filtered", pkPolicies);
  63. bucket.setDefaultPolicy(PredefinedPolicyType::DENY);
  64. auto filtered = bucket.filtered(pk1);
  65. // Elements match
  66. ASSERT_THAT(filtered, UnorderedElementsAre(pkPolicies.at(0)));
  67. // default policy matches
  68. ASSERT_EQ(PredefinedPolicyType::DENY, filtered.defaultPolicy());
  69. }
  70. TEST_F(PolicyBucketFixture, filtered_other) {
  71. using ::testing::IsEmpty;
  72. PolicyBucket bucket("filtered_other", pkPolicies);
  73. bucket.setDefaultPolicy(PredefinedPolicyType::DENY);
  74. auto filtered = bucket.filtered(otherPk);
  75. // No policies should be found
  76. ASSERT_THAT(filtered, IsEmpty());
  77. // default policy should be preserved
  78. ASSERT_EQ(PredefinedPolicyType::DENY, filtered.defaultPolicy());
  79. }
  80. TEST_F(PolicyBucketFixture, filtered_wildcard_1) {
  81. using ::testing::UnorderedElementsAreArray;
  82. // Leave policies with given client, given user and any privilege
  83. auto policiesToStay = Helpers::pickFromCollection(wildcardPolicies, { 0, 1, 3 });
  84. PolicyBucket bucket("filtered_wildcard_1", wildcardPolicies);
  85. auto filtered = bucket.filtered(PolicyKey("c1", "u1", "p2"));
  86. ASSERT_THAT(filtered, UnorderedElementsAreArray(policiesToStay));
  87. }
  88. TEST_F(PolicyBucketFixture, filtered_wildcard_2) {
  89. using ::testing::UnorderedElementsAreArray;
  90. // Leave policies with given client, given user and any privilege
  91. auto policiesToStay = Helpers::pickFromCollection(wildcardPolicies, { 2, 3 });
  92. PolicyBucket bucket("filtered_wildcard_2", wildcardPolicies);
  93. auto filtered = bucket.filtered(PolicyKey("cccc", "u1", "p1"));
  94. ASSERT_THAT(filtered, UnorderedElementsAreArray(policiesToStay));
  95. }
  96. TEST_F(PolicyBucketFixture, filtered_wildcard_3) {
  97. using ::testing::UnorderedElementsAreArray;
  98. // Leave policies with given client, given user and any privilege
  99. auto policiesToStay = Helpers::pickFromCollection(wildcardPolicies, { 0, 3 });
  100. PolicyBucket bucket("filtered_wildcard_3", wildcardPolicies);
  101. auto filtered = bucket.filtered(PolicyKey("c1", "u1", "pppp"));
  102. ASSERT_THAT(filtered, UnorderedElementsAreArray(policiesToStay));
  103. }
  104. TEST_F(PolicyBucketFixture, filtered_wildcard_4) {
  105. using ::testing::UnorderedElementsAreArray;
  106. // Leave policies with given client, given user and any privilege
  107. auto policiesToStay = Helpers::pickFromCollection(wildcardPolicies, { 3 });
  108. PolicyBucket bucket("filtered_wildcard_4", wildcardPolicies);
  109. auto filtered = bucket.filtered(PolicyKey("cccc", "uuuu", "pppp"));
  110. ASSERT_THAT(filtered, UnorderedElementsAreArray(policiesToStay));
  111. }
  112. TEST_F(PolicyBucketFixture, filtered_wildcard_none) {
  113. using ::testing::IsEmpty;
  114. PolicyBucket bucket("filtered_wildcard_none",
  115. PolicyCollection({ wildcardPolicies.begin(),
  116. wildcardPolicies.begin() + 3 }));
  117. auto filtered = bucket.filtered(PolicyKey("cccc", "uuuu", "pppp"));
  118. ASSERT_THAT(filtered, IsEmpty());
  119. }
  120. /**
  121. * @brief Validate PolicyBucketIds during creation - passing bucket ids
  122. * @test Scenario:
  123. * - Iterate through vector of valid bucket ids and create them normally
  124. * - PolicyBucket constructor should not throw any exception
  125. */
  126. TEST_F(PolicyBucketFixture, bucket_id_validation_passes) {
  127. for (auto it = goodIds.begin(); it != goodIds.end(); ++it) {
  128. SCOPED_TRACE(*it);
  129. ASSERT_NO_THROW(PolicyBucket(PolicyBucketId(*it)));
  130. }
  131. }
  132. /**
  133. * @brief Validate PolicyBucketIds during creation - failing bucket ids
  134. * @test Scenario:
  135. * - Iterate through vector of bucket ids containing forbidden characters
  136. * - PolicyBucket constructor should throw an exception every time it is called
  137. */
  138. TEST_F(PolicyBucketFixture, bucket_id_validation_fails) {
  139. for (auto it = badIds.begin(); it != badIds.end(); ++it) {
  140. SCOPED_TRACE(*it);
  141. ASSERT_THROW(PolicyBucket(PolicyBucketId(*it)), InvalidBucketIdException);
  142. }
  143. }