PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/test/storage/storage/check.cpp

https://gitlab.com/admin-github-cloud/cynara
C++ | 264 lines | 149 code | 67 blank | 48 comment | 0 complexity | 8510dfef8bc52eb11cebba79e953a168 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/storage/check.cpp
  18. * @author Aleksander Zdyb <a.zdyb@samsung.com>
  19. * @version 1.0
  20. * @brief Tests of check in Storage
  21. */
  22. #include <gtest/gtest.h>
  23. #include <gmock/gmock.h>
  24. #include "types/PolicyType.h"
  25. #include "types/PolicyKey.h"
  26. #include "types/PolicyResult.h"
  27. #include "types/PolicyCollection.h"
  28. #include "types/pointers.h"
  29. #include "exceptions/DefaultBucketDeletionException.h"
  30. #include "storage/Storage.h"
  31. #include "storage/StorageBackend.h"
  32. #include "fakestoragebackend.h"
  33. #include "../../helpers.h"
  34. #include <memory>
  35. #include <tuple>
  36. using namespace Cynara;
  37. TEST(storage, checkEmpty) {
  38. using ::testing::ReturnPointee;
  39. PolicyBucket emptyBucket("empty");
  40. FakeStorageBackend backend;
  41. Cynara::Storage storage(backend);
  42. PolicyKey pk = Helpers::generatePolicyKey();
  43. EXPECT_CALL(backend, searchBucket(defaultPolicyBucketId, pk))
  44. .WillOnce(ReturnPointee(&emptyBucket));
  45. // Default bucket empty -- return DENY
  46. auto policyAnswer = storage.checkPolicy(pk);
  47. ASSERT_EQ(PredefinedPolicyType::DENY, policyAnswer.policyType());
  48. }
  49. TEST(storage, checkSimple) {
  50. using ::testing::ReturnPointee;
  51. PolicyBucket bucket(defaultPolicyBucketId);
  52. FakeStorageBackend backend;
  53. Cynara::Storage storage(backend);
  54. PolicyKey pk = Helpers::generatePolicyKey();
  55. EXPECT_CALL(backend, searchBucket(defaultPolicyBucketId, pk))
  56. .WillRepeatedly(ReturnPointee(&bucket));
  57. // Default bucket empty -- return DENY
  58. ASSERT_EQ(PredefinedPolicyType::DENY, storage.checkPolicy(pk).policyType());
  59. // Add ALLOW to default bucket -- return ALLOW
  60. bucket.insertPolicy(Policy::simpleWithKey(pk, PredefinedPolicyType::ALLOW));
  61. ASSERT_EQ(PredefinedPolicyType::ALLOW, storage.checkPolicy(pk).policyType());
  62. // Add DENY to default bucket -- return DENY
  63. bucket.insertPolicy(Policy::simpleWithKey(pk, PredefinedPolicyType::DENY));
  64. ASSERT_EQ(PredefinedPolicyType::DENY, storage.checkPolicy(pk).policyType());
  65. }
  66. // TODO: Refactorize to resemble checkNonrecursive()
  67. TEST(storage, checkBucket) {
  68. using ::testing::ReturnPointee;
  69. const PolicyBucketId additionalBucketId = "additional-bucket";
  70. FakeStorageBackend backend;
  71. Cynara::Storage storage(backend);
  72. PolicyKey pk = Helpers::generatePolicyKey();
  73. PolicyBucket defaultBucket(defaultPolicyBucketId, PolicyCollection({
  74. Policy::simpleWithKey(pk, PredefinedPolicyType::ALLOW),
  75. Policy::bucketWithKey(pk, additionalBucketId)
  76. }));
  77. PolicyBucket additionalBucket("additional");
  78. EXPECT_CALL(backend, searchBucket(defaultPolicyBucketId, pk))
  79. .WillRepeatedly(ReturnPointee(&defaultBucket));
  80. EXPECT_CALL(backend, searchBucket(additionalBucketId, pk))
  81. .WillRepeatedly(ReturnPointee(&additionalBucket));
  82. // Bucket empty -- should return DENY as default bucket value
  83. ASSERT_EQ(PredefinedPolicyType::DENY, storage.checkPolicy(pk).policyType());
  84. // Add ALLOW to bucket, so return ALLOW
  85. additionalBucket.insertPolicy(Policy::simpleWithKey(pk, PredefinedPolicyType::ALLOW));
  86. ASSERT_EQ(PredefinedPolicyType::ALLOW, storage.checkPolicy(pk).policyType());
  87. // Add DENY to default bucket -- return DENY, even though ALLOW in other bucket
  88. defaultBucket.insertPolicy(Policy::simpleWithKey(pk, PredefinedPolicyType::DENY));
  89. ASSERT_EQ(PredefinedPolicyType::DENY, storage.checkPolicy(pk).policyType());
  90. }
  91. // Catch a bug, where consecutive buckets were filtered with wildcard policies' keys
  92. // instead of original key being checked
  93. TEST(storage, checkBucketWildcard) {
  94. using ::testing::Return;
  95. using ::testing::ReturnPointee;
  96. const PolicyBucketId additionalBucketId = "additional-bucket";
  97. const PolicyKey defaultBucketKey = PolicyKey("c", "*", "p");
  98. const PolicyKey checkKey = PolicyKey("c", "u1", "p");
  99. PolicyBucket defaultBucket(defaultPolicyBucketId, PolicyCollection({
  100. Policy::bucketWithKey(defaultBucketKey, additionalBucketId)
  101. }));
  102. FakeStorageBackend backend;
  103. Cynara::Storage storage(backend);
  104. EXPECT_CALL(backend, searchBucket(defaultPolicyBucketId, checkKey))
  105. .WillRepeatedly(ReturnPointee(&defaultBucket));
  106. // Check, if next bucket is filtered with original key
  107. EXPECT_CALL(backend, searchBucket(additionalBucketId, checkKey))
  108. .WillRepeatedly(Return(PolicyBucket("id"))); // additional bucket would yield no records
  109. // Should return additional bucket's default policy
  110. ASSERT_EQ(PredefinedPolicyType::DENY, storage.checkPolicy(checkKey));
  111. }
  112. TEST(storage, checkBucketWildcardOtherDefault) {
  113. using ::testing::ReturnPointee;
  114. const PolicyBucketId additionalBucketId = "additional-bucket";
  115. const PolicyKey defaultBucketKey = PolicyKey("c", "*", "p");
  116. const PolicyKey checkKey = PolicyKey("c", "u1", "p");
  117. PolicyBucket defaultBucket(defaultPolicyBucketId, PolicyCollection({
  118. Policy::bucketWithKey(defaultBucketKey, additionalBucketId)
  119. }));
  120. PolicyBucket additionalBucket(additionalBucketId, PredefinedPolicyType::ALLOW);
  121. FakeStorageBackend backend;
  122. Cynara::Storage storage(backend);
  123. EXPECT_CALL(backend, searchBucket(defaultPolicyBucketId, checkKey))
  124. .WillRepeatedly(ReturnPointee(&defaultBucket));
  125. // Check, if next bucket is filtered with original key
  126. EXPECT_CALL(backend, searchBucket(additionalBucketId, checkKey))
  127. .WillRepeatedly(ReturnPointee(&additionalBucket));
  128. // Should return additional bucket's default policy
  129. ASSERT_EQ(PredefinedPolicyType::ALLOW, storage.checkPolicy(checkKey));
  130. }
  131. TEST(storage, checkNonrecursive) {
  132. using ::testing::ReturnPointee;
  133. PolicyKey pk = Helpers::generatePolicyKey();
  134. PolicyBucketId bucketId = "a-bucket";
  135. PolicyBucket bucket(bucketId, PredefinedPolicyType::ALLOW,
  136. { Policy::bucketWithKey(pk, "must-not-be-touched") });
  137. FakeStorageBackend backend;
  138. Cynara::Storage storage(backend);
  139. EXPECT_CALL(backend, searchBucket(bucketId, pk))
  140. .WillOnce(ReturnPointee(&bucket));
  141. ASSERT_EQ(PredefinedPolicyType::ALLOW, storage.checkPolicy(pk, bucketId, false));
  142. }
  143. /*
  144. * bucket1 contains policy (with key pk) pointing to bucket2
  145. * bucket2 is empty and it's default policy is NONE
  146. * Because NONE policy in bucket2, check should yield default policy of bucket1 and not of bucket2
  147. */
  148. TEST(storage, noneBucket) {
  149. using ::testing::ReturnPointee;
  150. using PredefinedPolicyType::ALLOW;
  151. using PredefinedPolicyType::NONE;
  152. auto pk = Helpers::generatePolicyKey();
  153. PolicyBucket bucket2("bucket-2", NONE);
  154. PolicyBucket bucket1("bucket-1", ALLOW, { Policy::bucketWithKey(pk, bucket2.id()) });
  155. FakeStorageBackend backend;
  156. Cynara::Storage storage(backend);
  157. EXPECT_CALL(backend, searchBucket(bucket1.id(), pk))
  158. .WillOnce(ReturnPointee(&bucket1));
  159. EXPECT_CALL(backend, searchBucket(bucket2.id(), pk))
  160. .WillOnce(ReturnPointee(&bucket2));
  161. ASSERT_EQ(ALLOW, storage.checkPolicy(pk, bucket1.id(), true));
  162. }
  163. /*
  164. * Scenario similar to noneBucket, but bucket2 contains matching policy.
  165. * In this case this policy should be returned.
  166. */
  167. TEST(storage, noneBucketNotEmpty) {
  168. using ::testing::ReturnPointee;
  169. using PredefinedPolicyType::ALLOW;
  170. using PredefinedPolicyType::DENY;
  171. using PredefinedPolicyType::NONE;
  172. auto pk = Helpers::generatePolicyKey();
  173. PolicyBucket bucket2("bucket-2", NONE, { Policy::simpleWithKey(pk, DENY) });
  174. PolicyBucket bucket1("bucket-1", ALLOW, { Policy::bucketWithKey(pk, bucket2.id()) });
  175. FakeStorageBackend backend;
  176. Cynara::Storage storage(backend);
  177. EXPECT_CALL(backend, searchBucket(bucket1.id(), pk))
  178. .WillOnce(ReturnPointee(&bucket1));
  179. EXPECT_CALL(backend, searchBucket(bucket2.id(), pk))
  180. .WillOnce(ReturnPointee(&bucket2));
  181. ASSERT_EQ(DENY, storage.checkPolicy(pk, bucket1.id(), true));
  182. }
  183. /*
  184. * Single empty bucket with default policy of NONE
  185. * -- searching for any key should yield NONE
  186. */
  187. TEST(storage, singleNoneBucket) {
  188. using ::testing::ReturnPointee;
  189. using PredefinedPolicyType::NONE;
  190. auto pk = Helpers::generatePolicyKey();
  191. PolicyBucket bucket("bucket", NONE, {});
  192. FakeStorageBackend backend;
  193. Cynara::Storage storage(backend);
  194. EXPECT_CALL(backend, searchBucket(bucket.id(), pk))
  195. .WillOnce(ReturnPointee(&bucket));
  196. ASSERT_EQ(NONE, storage.checkPolicy(pk, bucket.id(), true));
  197. }