PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/test/storage/inmemorystoragebackend/buckets.cpp

https://gitlab.com/admin-github-cloud/cynara
C++ | 119 lines | 70 code | 28 blank | 21 comment | 0 complexity | 0efb757639b4586559a12e26bd199344 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/inmemorystoragebackend/buckets.cpp
  18. * @author Aleksander Zdyb <a.zdyb@samsung.com>
  19. * @version 1.0
  20. * @brief Tests of buckets in InMemoryStorageBackend
  21. */
  22. #include <gmock/gmock.h>
  23. #include <gtest/gtest.h>
  24. #include <exceptions/BucketNotExistsException.h>
  25. #include <types/PolicyBucket.h>
  26. #include <types/PolicyResult.h>
  27. #include "fakeinmemorystoragebackend.h"
  28. #include "inmemorystoragebackendfixture.h"
  29. using namespace Cynara;
  30. TEST_F(InMemoryStorageBackendFixture, createBucket) {
  31. using ::testing::ReturnRef;
  32. using ::testing::IsEmpty;
  33. FakeInMemoryStorageBackend backend(m_fakeDbPath);
  34. EXPECT_CALL(backend, buckets())
  35. .WillRepeatedly(ReturnRef(m_buckets));
  36. PolicyResult defaultPolicy(PredefinedPolicyType::ALLOW);
  37. PolicyBucketId bucketId = "new-bucket";
  38. backend.createBucket(bucketId, defaultPolicy);
  39. ASSERT_EQ(static_cast<size_t>(1), m_buckets.size());
  40. ASSERT_NE(m_buckets.end(), m_buckets.find(bucketId));
  41. ASSERT_EQ(defaultPolicy, m_buckets.at(bucketId).defaultPolicy());
  42. ASSERT_THAT(m_buckets.at(bucketId), IsEmpty());
  43. }
  44. TEST_F(InMemoryStorageBackendFixture, updateBucket) {
  45. using ::testing::ReturnRef;
  46. FakeInMemoryStorageBackend backend(m_fakeDbPath);
  47. EXPECT_CALL(backend, buckets())
  48. .WillRepeatedly(ReturnRef(m_buckets));
  49. PolicyBucketId bucketId = "bucket";
  50. auto &bucket = this->createBucket(bucketId);
  51. bucket.setDefaultPolicy(PredefinedPolicyType::ALLOW);
  52. backend.updateBucket(bucketId, PredefinedPolicyType::DENY);
  53. ASSERT_EQ(PredefinedPolicyType::DENY, bucket.defaultPolicy());
  54. }
  55. TEST_F(InMemoryStorageBackendFixture, updateNonexistentBucket) {
  56. using ::testing::ReturnRef;
  57. FakeInMemoryStorageBackend backend(m_fakeDbPath);
  58. EXPECT_CALL(backend, buckets())
  59. .WillRepeatedly(ReturnRef(m_buckets));
  60. EXPECT_THROW(backend.updateBucket("non-existent", PredefinedPolicyType::ALLOW),
  61. BucketNotExistsException);
  62. }
  63. TEST_F(InMemoryStorageBackendFixture, deleteBucket) {
  64. using ::testing::ReturnRef;
  65. using ::testing::IsEmpty;
  66. FakeInMemoryStorageBackend backend(m_fakeDbPath);
  67. EXPECT_CALL(backend, buckets())
  68. .WillRepeatedly(ReturnRef(m_buckets));
  69. PolicyBucketId bucketId = "delete-bucket";
  70. m_buckets.insert({ bucketId, PolicyBucket(bucketId) });
  71. backend.deleteBucket(bucketId);
  72. ASSERT_THAT(m_buckets, IsEmpty());
  73. }
  74. TEST_F(InMemoryStorageBackendFixture, hasBucket) {
  75. using ::testing::ReturnRef;
  76. using ::testing::IsEmpty;
  77. FakeInMemoryStorageBackend backend(m_fakeDbPath);
  78. EXPECT_CALL(backend, buckets())
  79. .WillRepeatedly(ReturnRef(m_buckets));
  80. PolicyBucketId bucketId = "bucket";
  81. m_buckets.insert({ bucketId, PolicyBucket(bucketId) });
  82. ASSERT_TRUE(backend.hasBucket(bucketId));
  83. ASSERT_FALSE(backend.hasBucket("non-existent"));
  84. }
  85. TEST_F(InMemoryStorageBackendFixture, deleteNonexistentBucket) {
  86. using ::testing::ReturnRef;
  87. FakeInMemoryStorageBackend backend(m_fakeDbPath);
  88. EXPECT_CALL(backend, buckets())
  89. .WillRepeatedly(ReturnRef(m_buckets));
  90. EXPECT_THROW(backend.deleteBucket("non-existent"), BucketNotExistsException);
  91. }