From 93103cf431c67c2204c711182ab5d666f251e7cd Mon Sep 17 00:00:00 2001 From: Aleksander Zdyb Date: Fri, 25 Jul 2014 11:00:53 +0200 Subject: [PATCH] Adjust tests for new storage and serialization API Tests need to be adjusted because of changes in Storage::insertPolicies() and Storage::deletePolicies(). They also make use of pointers on I/O streams instead of references (in serializers and deserializers). Change-Id: Ief7e256f0848097caa66365ffd063289c50ec26c --- src/common/types/Policy.h | 5 + src/service/storage/InMemoryStorageBackend.h | 1 + test/storage/inmemorystoragebackend/buckets.cpp | 12 +- .../fakeinmemorystoragebackend.h | 6 +- .../inmemeorystoragebackendfixture.h | 13 ++- test/storage/serializer/bucket_load.cpp | 35 +++--- test/storage/serializer/deserialize.cpp | 62 ++++------ test/storage/serializer/dump.cpp | 47 ++++---- test/storage/serializer/serialize.cpp | 20 ++-- test/storage/storage/fakestoragebackend.h | 2 + test/storage/storage/policies.cpp | 130 ++++++++++++--------- 11 files changed, 179 insertions(+), 154 deletions(-) diff --git a/src/common/types/Policy.h b/src/common/types/Policy.h index 3b4737f..06db2f4 100644 --- a/src/common/types/Policy.h +++ b/src/common/types/Policy.h @@ -32,6 +32,7 @@ #include "types/PolicyBucketId.h" #include +#include namespace Cynara { @@ -50,6 +51,10 @@ public: return std::make_shared(key, result); } + bool operator==(const Policy &other) const { + return std::tie(m_key, m_result) == std::tie(other.m_key, other.m_result); + } + private: PolicyKey m_key; PolicyResult m_result; diff --git a/src/service/storage/InMemoryStorageBackend.h b/src/service/storage/InMemoryStorageBackend.h index be2ba54..68d042e 100644 --- a/src/service/storage/InMemoryStorageBackend.h +++ b/src/service/storage/InMemoryStorageBackend.h @@ -60,6 +60,7 @@ public: virtual void deleteLinking(const PolicyBucketId &bucketId); protected: + InMemoryStorageBackend() {} void openFileStream(std::shared_ptr stream, const std::string &filename); std::shared_ptr bucketStreamOpener(const PolicyBucketId &bucketId); diff --git a/test/storage/inmemorystoragebackend/buckets.cpp b/test/storage/inmemorystoragebackend/buckets.cpp index bc9fcf6..9028d1a 100644 --- a/test/storage/inmemorystoragebackend/buckets.cpp +++ b/test/storage/inmemorystoragebackend/buckets.cpp @@ -20,13 +20,15 @@ * @brief Tests of buckets in InMemeoryStorageBackend */ -#include "gmock/gmock.h" +#include +#include -#include "inmemeorystoragebackendfixture.h" -#include "fakeinmemorystoragebackend.h" +#include +#include +#include -#include "types/PolicyResult.h" -#include "types/PolicyBucket.h" +#include "fakeinmemorystoragebackend.h" +#include "inmemeorystoragebackendfixture.h" using namespace Cynara; diff --git a/test/storage/inmemorystoragebackend/fakeinmemorystoragebackend.h b/test/storage/inmemorystoragebackend/fakeinmemorystoragebackend.h index 52e046f..a53a24a 100644 --- a/test/storage/inmemorystoragebackend/fakeinmemorystoragebackend.h +++ b/test/storage/inmemorystoragebackend/fakeinmemorystoragebackend.h @@ -23,11 +23,13 @@ #ifndef FAKEINMEMORYSTORAGEBACKEND_H_ #define FAKEINMEMORYSTORAGEBACKEND_H_ -#include "storage/InMemoryStorageBackend.h" +#include +#include class FakeInMemoryStorageBackend : public Cynara::InMemoryStorageBackend { public: - MOCK_METHOD0(buckets, Cynara::InMemoryStorageBackend::Buckets&()); + using Cynara::InMemoryStorageBackend::InMemoryStorageBackend; + MOCK_METHOD0(buckets, Cynara::Buckets&()); }; diff --git a/test/storage/inmemorystoragebackend/inmemeorystoragebackendfixture.h b/test/storage/inmemorystoragebackend/inmemeorystoragebackendfixture.h index 21ae4fc..b194006 100644 --- a/test/storage/inmemorystoragebackend/inmemeorystoragebackendfixture.h +++ b/test/storage/inmemorystoragebackend/inmemeorystoragebackendfixture.h @@ -23,16 +23,17 @@ #ifndef INMEMEORYSTORAGEBACKENDFIXTURE_H_ #define INMEMEORYSTORAGEBACKENDFIXTURE_H_ -#include "gmock/gmock.h" +#include -#include "types/PolicyBucket.h" -#include "types/PolicyCollection.h" -#include "storage/InMemoryStorageBackend.h" +#include +#include +#include +#include class InMemeoryStorageBackendFixture : public ::testing::Test { protected: - Cynara::InMemoryStorageBackend::Buckets::mapped_type & + Cynara::Buckets::mapped_type & createBucket(const Cynara::PolicyBucketId &bucketId) { auto bucket = Cynara::PolicyBucket(); return m_buckets.insert({ bucketId, bucket }).first->second; @@ -46,7 +47,7 @@ protected: virtual ~InMemeoryStorageBackendFixture() {} // TODO: consider defaulting accessor with ON_CALL - Cynara::InMemoryStorageBackend::Buckets m_buckets; + Cynara::Buckets m_buckets; }; diff --git a/test/storage/serializer/bucket_load.cpp b/test/storage/serializer/bucket_load.cpp index aa7d290..3c2096e 100644 --- a/test/storage/serializer/bucket_load.cpp +++ b/test/storage/serializer/bucket_load.cpp @@ -20,6 +20,10 @@ * @brief Tests for Cynara::BucketDeserializer */ +#include +#include +#include +#include #include #include @@ -30,11 +34,6 @@ #include "../../helpers.h" -#include -#include -#include -#include - using namespace Cynara; MATCHER_P(PolicyAtPtrEq, policy, "") { @@ -52,11 +51,11 @@ public: void checkCorruptedData(const std::string &data, const std::string &corruptedLine, size_t corruptedLineNumber) { - std::istringstream bucketStream(data); + auto bucketStream = std::make_shared(data); BucketDeserializer deserializer(bucketStream); EXPECT_THROW(deserializer.loadPolicies(), BucketRecordCorruptedException); - bucketStream.seekg(0); + bucketStream->seekg(0); try { deserializer.loadPolicies(); } catch (const BucketRecordCorruptedException &ex) { @@ -69,7 +68,7 @@ public: TEST_F(BucketDeserializerFixture, load_empty) { using ::testing::IsEmpty; - std::istringstream bucketStream; + auto bucketStream = std::make_shared(); BucketDeserializer deserializer(bucketStream); auto policies = deserializer.loadPolicies(); @@ -79,7 +78,7 @@ TEST_F(BucketDeserializerFixture, load_empty) { TEST_F(BucketDeserializerFixture, load_1) { using ::testing::UnorderedElementsAre; - std::istringstream bucketStream("c;u;p;0;meta"); + auto bucketStream = std::make_shared("c;u;p;0;meta"); BucketDeserializer deserializer(bucketStream); auto policies = deserializer.loadPolicies(); @@ -91,7 +90,7 @@ TEST_F(BucketDeserializerFixture, load_1) { TEST_F(BucketDeserializerFixture, load_1_allow) { using ::testing::UnorderedElementsAre; - std::istringstream bucketStream("c;u;p;0xFFFF;meta"); + auto bucketStream = std::make_shared("c;u;p;0xFFFF;meta"); BucketDeserializer deserializer(bucketStream); auto policies = deserializer.loadPolicies(); @@ -103,7 +102,7 @@ TEST_F(BucketDeserializerFixture, load_1_allow) { TEST_F(BucketDeserializerFixture, load_1_no_meta_sep) { using ::testing::UnorderedElementsAre; - std::istringstream bucketStream("c;u;p;0;"); + auto bucketStream = std::make_shared("c;u;p;0;"); BucketDeserializer deserializer(bucketStream); auto policies = deserializer.loadPolicies(); @@ -115,7 +114,7 @@ TEST_F(BucketDeserializerFixture, load_1_no_meta_sep) { TEST_F(BucketDeserializerFixture, load_1_no_meta_no_sep) { using ::testing::UnorderedElementsAre; - std::istringstream bucketStream("c;u;p;0"); + auto bucketStream = std::make_shared("c;u;p;0"); BucketDeserializer deserializer(bucketStream); auto policies = deserializer.loadPolicies(); @@ -127,8 +126,8 @@ TEST_F(BucketDeserializerFixture, load_1_no_meta_no_sep) { TEST_F(BucketDeserializerFixture, load_2) { using ::testing::UnorderedElementsAre; - std::istringstream bucketStream("c;u;p;0;meta\n" - "c;u;p;0;meta\n"); + auto bucketStream = std::make_shared("c;u;p;0;meta\n" + "c;u;p;0;meta\n"); BucketDeserializer deserializer(bucketStream); auto policies = deserializer.loadPolicies(); @@ -142,10 +141,10 @@ TEST_F(BucketDeserializerFixture, load_mixed) { using ::testing::UnorderedElementsAre; using ::testing::UnorderedElementsAreArray; - std::istringstream bucketStream("c1;u1;p1;0;meta\n" - "c2;u2;p2;0xFFFF;meta2\n" - "c2;u2;p2;0xFFFF;\n" - "c1;u1;p3;0xFFFE;bucket\n"); + auto bucketStream = std::make_shared("c1;u1;p1;0;meta\n" + "c2;u2;p2;0xFFFF;meta2\n" + "c2;u2;p2;0xFFFF;\n" + "c1;u1;p3;0xFFFE;bucket\n"); BucketDeserializer deserializer(bucketStream); auto policies = deserializer.loadPolicies(); diff --git a/test/storage/serializer/deserialize.cpp b/test/storage/serializer/deserialize.cpp index 1fd6ad3..140242b 100644 --- a/test/storage/serializer/deserialize.cpp +++ b/test/storage/serializer/deserialize.cpp @@ -28,14 +28,9 @@ #include #include -#include +#include #include - -// TODO: Move to .h, because it's used also in bucket_load.cpp -MATCHER_P(PolicyPtrEq, policy, "") { - return std::tie(policy->key(), policy->result()) - == std::tie(arg->key(), arg->result()); -} +#include MATCHER_P(PolicyBucketIdPolicyEq, expected, "") { auto bucket1 = expected.second; @@ -50,15 +45,14 @@ MATCHER_P(PolicyBucketIdPolicyEq, expected, "") { class FakeStreamForBucketId { public: - MOCK_METHOD1(streamForBucketId, std::shared_ptr(const std::string &)); + MOCK_METHOD1(streamForBucketId, + std::shared_ptr(const std::string &)); }; class EmptyBucketDeserializer : public Cynara::BucketDeserializer { public: - EmptyBucketDeserializer() : Cynara::BucketDeserializer(m_emptyStream), - m_emptyStream("") {} -private: - std::istringstream m_emptyStream; + EmptyBucketDeserializer() + : Cynara::BucketDeserializer(std::make_shared("")) {} }; class StorageDeserializerFixture : public ::testing::Test { @@ -78,10 +72,10 @@ using namespace Cynara; TEST_F(StorageDeserializerFixture, init_default_only) { using ::testing::UnorderedElementsAre; - std::istringstream ss(";0"); + auto ss = std::make_shared(";0"); StorageDeserializer deserializer(ss, nullStreamOpener); - InMemoryStorageBackend::Buckets buckets; + Buckets buckets; deserializer.initBuckets(buckets); ASSERT_THAT(buckets, UnorderedElementsAre( @@ -92,12 +86,12 @@ TEST_F(StorageDeserializerFixture, init_default_only) { TEST_F(StorageDeserializerFixture, init_more) { using ::testing::UnorderedElementsAre; - std::istringstream ss(";0\n" - "bucket2;0\n" - "bucket3;0xFFFE;bucket2\n"); + auto ss = std::make_shared(";0\n" + "bucket2;0\n" + "bucket3;0xFFFE;bucket2\n"); StorageDeserializer deserializer(ss, nullStreamOpener); - InMemoryStorageBackend::Buckets buckets; + Buckets buckets; deserializer.initBuckets(buckets); ASSERT_THAT(buckets, UnorderedElementsAre( @@ -112,10 +106,10 @@ TEST_F(StorageDeserializerFixture, init_more) { TEST_F(StorageDeserializerFixture, init_overwrite) { using ::testing::UnorderedElementsAre; - std::istringstream ss(";0x0"); + auto ss = std::make_shared(";0x0"); StorageDeserializer deserializer(ss, nullStreamOpener); - InMemoryStorageBackend::Buckets buckets; + Buckets buckets; // Default bucket has ALLOW policy as default buckets.insert({ "", PolicyBucket("fakeId", PredefinedPolicyType::ALLOW) }); @@ -128,20 +122,19 @@ TEST_F(StorageDeserializerFixture, init_overwrite) { } TEST_F(StorageDeserializerFixture, load_buckets_plus_policies) { - using ::testing::_; + using ::testing::Pointee; using ::testing::Return; using ::testing::UnorderedElementsAre; - InMemoryStorageBackend::Buckets buckets; + Buckets buckets; buckets.insert({ "", PolicyBucket("", PredefinedPolicyType::DENY) }); - std::istringstream bucketsStream; // Won't be used; buckets are pre-inserted above FakeStreamForBucketId streamOpener; auto streamOpenerFunc = std::bind(&FakeStreamForBucketId::streamForBucketId, &streamOpener, std::placeholders::_1); - StorageDeserializer deserializer(bucketsStream, streamOpenerFunc); + StorageDeserializer deserializer(nullptr, streamOpenerFunc); - std::istringstream defaultBucketStream("c;u;p;0;meta"); + auto defaultBucketStream = std::make_shared("c;u;p;0;meta"); auto bucketDeserializer = std::make_shared(defaultBucketStream); EXPECT_CALL(streamOpener, streamForBucketId("")) .WillOnce(Return(bucketDeserializer)); @@ -153,31 +146,25 @@ TEST_F(StorageDeserializerFixture, load_buckets_plus_policies) { COMPARE_BUCKETS("", PolicyBucket("", PredefinedPolicyType::DENY)) )); - auto expectedPolicy = std::make_shared(PolicyKey("c", "u", "p"), - PolicyResult(PredefinedPolicyType::DENY, "meta")); - // Check policy was inserted into bucket ASSERT_THAT(buckets.at("").policyCollection(), UnorderedElementsAre( - PolicyPtrEq(expectedPolicy) + Pointee(Policy(PolicyKey("c", "u", "p"), PolicyResult(PredefinedPolicyType::DENY, "meta"))) )); } TEST_F(StorageDeserializerFixture, load_buckets) { - using ::testing::_; using ::testing::Return; - using ::testing::UnorderedElementsAre; // Pre-insert some buckets - InMemoryStorageBackend::Buckets buckets; + Buckets buckets; buckets.insert({ "", PolicyBucket("", PredefinedPolicyType::DENY) }); buckets.insert({ "bucket1", PolicyBucket("bucket1", PredefinedPolicyType::DENY) }); buckets.insert({ "bucket2", PolicyBucket("bucket2", PredefinedPolicyType::DENY) }); - std::istringstream bucketsStream; // Won't be used; buckets are pre-inserted above FakeStreamForBucketId streamOpener; auto streamOpenerFunc = std::bind(&FakeStreamForBucketId::streamForBucketId, &streamOpener, std::placeholders::_1); - StorageDeserializer deserializer(bucketsStream, streamOpenerFunc); + StorageDeserializer deserializer(nullptr, streamOpenerFunc); // Check, if streamOpener was called for each bucket EXPECT_CALL(streamOpener, streamForBucketId("")) @@ -193,19 +180,16 @@ TEST_F(StorageDeserializerFixture, load_buckets) { } TEST_F(StorageDeserializerFixture, load_buckets_io_error) { - using ::testing::_; using ::testing::Return; - using ::testing::UnorderedElementsAre; // Pre-insert some buckets - InMemoryStorageBackend::Buckets buckets; + Buckets buckets; buckets.insert({ "", PolicyBucket("", PredefinedPolicyType::DENY) }); - std::istringstream bucketsStream; // Won't be used; buckets are pre-inserted above FakeStreamForBucketId streamOpener; auto streamOpenerFunc = std::bind(&FakeStreamForBucketId::streamForBucketId, &streamOpener, std::placeholders::_1); - StorageDeserializer deserializer(bucketsStream, streamOpenerFunc); + StorageDeserializer deserializer(nullptr, streamOpenerFunc); // Check, if streamOpener was called for each bucket EXPECT_CALL(streamOpener, streamForBucketId("")) diff --git a/test/storage/serializer/dump.cpp b/test/storage/serializer/dump.cpp index 157a9f2..2a0bfd0 100644 --- a/test/storage/serializer/dump.cpp +++ b/test/storage/serializer/dump.cpp @@ -20,19 +20,21 @@ * @brief Tests for dumping feature of Cynara::StorageSerializer */ -#include + +#include +#include + #include +#include -#include "storage/StorageSerializer.h" -#include "types/PolicyBucket.h" -#include "types/PolicyType.h" -#include "types/PolicyKey.h" -#include "types/Policy.h" +#include +#include +#include +#include +#include #include "../../helpers.h" -#include - using namespace Cynara; static std::string expectedPolicyKey(const PolicyKey &key) { @@ -46,13 +48,13 @@ static std::string expectedPolicyType(const PolicyType &type) { } TEST(serializer_dump, dump_empty_bucket) { - std::ostringstream oss; + auto oss = std::make_shared(); PolicyBucket bucket; StorageSerializer serializer(oss); serializer.dump(bucket); - ASSERT_EQ("", oss.str()); + ASSERT_EQ("", oss->str()); } TEST(serializer_dump, dump_bucket) { @@ -62,7 +64,7 @@ TEST(serializer_dump, dump_bucket) { PolicyBucket bucket = {{ Policy::simpleWithKey(pk1, PredefinedPolicyType::ALLOW), Policy::simpleWithKey(pk2, PredefinedPolicyType::DENY) }}; - std::ostringstream outputStream; + auto outputStream = std::make_shared(); StorageSerializer serializer(outputStream); serializer.dump(bucket); @@ -71,10 +73,12 @@ TEST(serializer_dump, dump_bucket) { // See: StorageSerializerFixture::dump_buckets in serialize.cpp std::stringstream expected; expected - << expectedPolicyKey(pk1) << ";" << expectedPolicyType(PredefinedPolicyType::ALLOW) << ";" << "\n" - << expectedPolicyKey(pk2) << ";" << expectedPolicyType(PredefinedPolicyType::DENY) << ";" << "\n"; + << expectedPolicyKey(pk1) << ";" << expectedPolicyType(PredefinedPolicyType::ALLOW)<< ";" + << std::endl + << expectedPolicyKey(pk2) << ";" << expectedPolicyType(PredefinedPolicyType::DENY) << ";" + << std::endl; - ASSERT_EQ(expected.str(), outputStream.str()); + ASSERT_EQ(expected.str(), outputStream->str()); } TEST(serializer_dump, dump_bucket_bucket) { @@ -87,7 +91,7 @@ TEST(serializer_dump, dump_bucket_bucket) { Policy::simpleWithKey(pk2, PredefinedPolicyType::DENY), Policy::bucketWithKey(pk3, bucketId) }}; - std::ostringstream outputStream; + auto outputStream = std::make_shared(); StorageSerializer serializer(outputStream); serializer.dump(bucket); @@ -96,9 +100,12 @@ TEST(serializer_dump, dump_bucket_bucket) { // See: StorageSerializerFixture::dump_buckets in serialize.cpp std::stringstream expected; expected - << expectedPolicyKey(pk1) << ";" << expectedPolicyType(PredefinedPolicyType::BUCKET) << ";" << bucketId << "\n" - << expectedPolicyKey(pk2) << ";" << expectedPolicyType(PredefinedPolicyType::DENY) << ";" << "\n" - << expectedPolicyKey(pk3) << ";" << expectedPolicyType(PredefinedPolicyType::BUCKET) << ";" << bucketId << "\n"; - - ASSERT_EQ(expected.str(), outputStream.str()); + << expectedPolicyKey(pk1) << ";" << expectedPolicyType(PredefinedPolicyType::BUCKET) << ";" + << bucketId << std::endl + << expectedPolicyKey(pk2) << ";" << expectedPolicyType(PredefinedPolicyType::DENY) << ";" + << std::endl + << expectedPolicyKey(pk3) << ";" << expectedPolicyType(PredefinedPolicyType::BUCKET) << ";" + << bucketId << std::endl; + + ASSERT_EQ(expected.str(), outputStream->str()); } diff --git a/test/storage/serializer/serialize.cpp b/test/storage/serializer/serialize.cpp index 06b8cfb..07804f4 100644 --- a/test/storage/serializer/serialize.cpp +++ b/test/storage/serializer/serialize.cpp @@ -30,6 +30,7 @@ #include #include +#include #include #include #include @@ -47,16 +48,17 @@ public: // Fake StorageSerializer for Cynara::PolicyBucket class FakeStorageSerializer : public Cynara::StorageSerializer { public: - FakeStorageSerializer() : Cynara::StorageSerializer(outStream) {} + FakeStorageSerializer() : Cynara::StorageSerializer(outStream), + outStream(new std::ostringstream()) {} MOCK_METHOD1(dump, void(const Cynara::PolicyBucket &bucket)); - std::ostringstream outStream; + std::shared_ptr outStream; }; class StorageSerializerFixture : public ::testing::Test { public: virtual ~StorageSerializerFixture() = default; - Cynara::InMemoryStorageBackend::Buckets buckets; + Cynara::Buckets buckets; FakeStreamForBucketId fakeStreamOpener; }; @@ -65,12 +67,12 @@ using namespace Cynara; // Be sure no calls to streamForBucketId() are made // and output stream is not touched TEST_F(StorageSerializerFixture, dump_buckets_empty) { - std::ostringstream outStream; + auto outStream = std::make_shared(); StorageSerializer serializer(outStream); - serializer.dump(InMemoryStorageBackend::Buckets(), fakeStreamOpener.streamOpener()); + serializer.dump(Buckets(), fakeStreamOpener.streamOpener()); // Stream should be empty - ASSERT_EQ(0, outStream.tellp()); + ASSERT_EQ(0, outStream->tellp()); } TEST_F(StorageSerializerFixture, dump_buckets) { @@ -89,7 +91,7 @@ TEST_F(StorageSerializerFixture, dump_buckets) { PolicyBucket("bucket3", PolicyResult(PredefinedPolicyType::BUCKET, "bucket2")) } }; - std::stringstream outStream; + auto outStream = std::make_shared(); StorageSerializer dbSerializer(outStream); // Make sure stream was opened for each bucket @@ -110,7 +112,7 @@ TEST_F(StorageSerializerFixture, dump_buckets) { }; // Split stream into records - auto actualRecords = std::vector(std::istream_iterator(outStream), + auto actualRecords = std::vector(std::istream_iterator(*outStream), std::istream_iterator()); ASSERT_THAT(actualRecords, UnorderedElementsAreArray(expectedRecords)); @@ -124,7 +126,7 @@ TEST_F(StorageSerializerFixture, dump_buckets_io_error) { { "bucket1", PolicyBucket("bucket1", PredefinedPolicyType::DENY) }, }; - std::stringstream outStream; + auto outStream = std::make_shared(); StorageSerializer dbSerializer(outStream); // Make sure stream was opened for each bucket diff --git a/test/storage/storage/fakestoragebackend.h b/test/storage/storage/fakestoragebackend.h index bfa212d..b1e8b69 100644 --- a/test/storage/storage/fakestoragebackend.h +++ b/test/storage/storage/fakestoragebackend.h @@ -27,6 +27,8 @@ using namespace Cynara; class FakeStorageBackend : public StorageBackend { public: + MOCK_METHOD0(load, void(void)); + MOCK_METHOD0(save, void(void)); MOCK_METHOD1(searchDefaultBucket, PolicyBucket(const PolicyKey &key)); MOCK_METHOD2(searchBucket, PolicyBucket(const PolicyBucketId &bucket, const PolicyKey &key)); MOCK_METHOD2(createBucket, void(const PolicyBucketId &bucketId, diff --git a/test/storage/storage/policies.cpp b/test/storage/storage/policies.cpp index 5c593b5..4125a6d 100644 --- a/test/storage/storage/policies.cpp +++ b/test/storage/storage/policies.cpp @@ -20,33 +20,28 @@ * @brief Tests of policies in Storage */ -#include -#include - -#include "types/PolicyType.h" -#include "types/PolicyKey.h" -#include "types/PolicyResult.h" -#include "types/PolicyCollection.h" -#include "types/pointers.h" -#include "exceptions/DefaultBucketDeletionException.h" -#include "storage/Storage.h" -#include "storage/StorageBackend.h" - - -#include "fakestoragebackend.h" -#include "../../helpers.h" - #include #include #include #include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "../../helpers.h" +#include "fakestoragebackend.h" + using namespace Cynara; TEST(storage, deletePolicies) { - using ::testing::_; - using ::testing::Eq; - using ::testing::TypedEq; FakeStorageBackend backend; Cynara::Storage storage(backend); @@ -56,15 +51,15 @@ TEST(storage, deletePolicies) { PolicyBucketId bucketId2 = "bucket-2"; EXPECT_CALL(backend, deletePolicy(bucketId1, pk1)); - EXPECT_CALL(backend, deletePolicy(bucketId2, pk1)); EXPECT_CALL(backend, deletePolicy(bucketId1, pk2)); + EXPECT_CALL(backend, deletePolicy(bucketId2, pk1)); - std::map> policies; - policies[bucketId1].push_back(pk1); - policies[bucketId2].push_back(pk1); - policies[bucketId1].push_back(pk2); + typedef std::pair> BucketPoliciesPair; - storage.deletePolicies(policies); + storage.deletePolicies({ + BucketPoliciesPair(bucketId1, { pk1, pk2 }), + BucketPoliciesPair(bucketId2, { pk1 }), + }); } // TODO: isn't it the same test as storage.deleteBucket? @@ -80,29 +75,41 @@ TEST(storage, deleteBucketWithLinkedPolicies) { storage.deleteBucket(bucketId); } - TEST(storage, insertPolicies) { + using ::testing::Pointee; using ::testing::Return; - using ::testing::_; FakeStorageBackend backend; - Cynara::Storage storage(backend); + Storage storage(backend); PolicyBucketId testBucket1 = "test-bucket-1"; PolicyBucketId testBucket2 = "test-bucket-2"; - std::map> policiesToInsert; - policiesToInsert[testBucket1].push_back(Policy(Helpers::generatePolicyKey("1"), PredefinedPolicyType::ALLOW)); - policiesToInsert[testBucket1].push_back(Policy(Helpers::generatePolicyKey("2"), PredefinedPolicyType::DENY)); - policiesToInsert[testBucket1].push_back(Policy(Helpers::generatePolicyKey("3"), PredefinedPolicyType::DENY)); - policiesToInsert[testBucket2].push_back(Policy(Helpers::generatePolicyKey("4"), PredefinedPolicyType::ALLOW)); - policiesToInsert[testBucket2].push_back(Policy(Helpers::generatePolicyKey("5"), PredefinedPolicyType::ALLOW)); - - for (const auto &bucket : policiesToInsert) { - const PolicyBucketId &bucketId = bucket.first; - for (const auto &policy : bucket.second) { + typedef std::pair> BucketPolicyPair; + + auto createPolicy = [] (const std::string &keySuffix, const PolicyType &type) -> Policy { + return Policy(Helpers::generatePolicyKey(keySuffix), type); + }; + + std::map> policiesToInsert = { + BucketPolicyPair(testBucket1, { + createPolicy("1", PredefinedPolicyType::ALLOW), + createPolicy("2", PredefinedPolicyType::DENY), + createPolicy("3", PredefinedPolicyType::DENY) + }), + BucketPolicyPair(testBucket2, { + createPolicy("4", PredefinedPolicyType::ALLOW), + createPolicy("5", PredefinedPolicyType::ALLOW) + }) + }; + + for (const auto &group : policiesToInsert) { + const auto &bucketId = group.first; + const auto &policies = group.second; + + for (const auto &policy : policies) { EXPECT_CALL(backend, searchBucket(bucketId, policy.key())) .WillOnce(Return(PolicyBucket())); - EXPECT_CALL(backend, insertPolicy(bucketId, _)); + EXPECT_CALL(backend, insertPolicy(bucketId, Pointee(policy))); } } @@ -110,29 +117,42 @@ TEST(storage, insertPolicies) { } TEST(storage, updatePolicies) { + using ::testing::Pointee; using ::testing::Return; - using ::testing::_; FakeStorageBackend backend; - Cynara::Storage storage(backend); + Storage storage(backend); PolicyBucketId testBucket1 = "test-bucket-1"; PolicyBucketId testBucket2 = "test-bucket-2"; - std::map> policiesToInsert; - policiesToInsert[testBucket1].push_back(Policy(Helpers::generatePolicyKey("1"), PredefinedPolicyType::ALLOW)); - policiesToInsert[testBucket1].push_back(Policy(Helpers::generatePolicyKey("2"), PredefinedPolicyType::DENY)); - policiesToInsert[testBucket1].push_back(Policy(Helpers::generatePolicyKey("3"), PredefinedPolicyType::DENY)); - policiesToInsert[testBucket2].push_back(Policy(Helpers::generatePolicyKey("4"), PredefinedPolicyType::ALLOW)); - policiesToInsert[testBucket2].push_back(Policy(Helpers::generatePolicyKey("5"), PredefinedPolicyType::ALLOW)); - - storage.insertPolicies(policiesToInsert); - - for (const auto &bucket : policiesToInsert) { - const PolicyBucketId &bucketId = bucket.first; - for (const auto &policy : bucket.second) { - EXPECT_CALL(backend, searchBucket(bucketId, policy.key())); + typedef std::pair> BucketPolicyPair; + + auto createPolicy = [] (const std::string &keySuffix, const PolicyType &type) -> Policy { + return Policy(Helpers::generatePolicyKey(keySuffix), type); + }; + + std::map> policiesToInsert = { + BucketPolicyPair(testBucket1, { + createPolicy("1", PredefinedPolicyType::ALLOW), + createPolicy("2", PredefinedPolicyType::DENY), + createPolicy("3", PredefinedPolicyType::DENY) + }), + BucketPolicyPair(testBucket2, { + createPolicy("4", PredefinedPolicyType::ALLOW), + createPolicy("5", PredefinedPolicyType::ALLOW) + }) + }; + + for (const auto &group : policiesToInsert) { + const auto &bucketId = group.first; + const auto &policies = group.second; + + for (const auto &policy : policies) { + PolicyBucket searchResult(PolicyCollection { std::make_shared(policy) }); + EXPECT_CALL(backend, searchBucket(bucketId, policy.key())) + .WillOnce(Return(searchResult)); EXPECT_CALL(backend, deletePolicy(bucketId, policy.key())); - EXPECT_CALL(backend, insertPolicy(bucketId, _)); + EXPECT_CALL(backend, insertPolicy(bucketId, Pointee(policy))); } } -- 2.7.4