From f49cae653af93878738f5143c97b4e7ebcab200b Mon Sep 17 00:00:00 2001 From: Aleksander Zdyb Date: Thu, 24 Jul 2014 12:00:24 +0200 Subject: [PATCH] Fix dumping PolicyKey in StorageSerializer StorageSerializer no longer uses PolicyKey::toString(), which was meant only for logging purposes. Change-Id: I14214319ce7d2c3a7c79b60a44bb56ae79c2dad4 --- src/service/storage/InMemoryStorageBackend.cpp | 1 + src/service/storage/StorageSerializer.cpp | 6 +-- src/service/storage/StorageSerializer.h | 2 +- test/CMakeLists.txt | 1 + test/storage/serializer/bucket_load.cpp | 22 ++++---- test/storage/serializer/dump.cpp | 22 +++++--- test/storage/serializer/dump_load.cpp | 69 ++++++++++++++++++++++++++ 7 files changed, 101 insertions(+), 22 deletions(-) create mode 100644 test/storage/serializer/dump_load.cpp diff --git a/src/service/storage/InMemoryStorageBackend.cpp b/src/service/storage/InMemoryStorageBackend.cpp index bf1c1c9..fa20744 100644 --- a/src/service/storage/InMemoryStorageBackend.cpp +++ b/src/service/storage/InMemoryStorageBackend.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include diff --git a/src/service/storage/StorageSerializer.cpp b/src/service/storage/StorageSerializer.cpp index 2572a3b..f6e42fe 100644 --- a/src/service/storage/StorageSerializer.cpp +++ b/src/service/storage/StorageSerializer.cpp @@ -73,8 +73,8 @@ void StorageSerializer::dump(const PolicyBucket& bucket) { } } -void StorageSerializer::dump(const PolicyKey &key) { - *m_outStream << key.toString(); +void StorageSerializer::dump(const PolicyKeyFeature &keyFeature) { + *m_outStream << keyFeature.toString(); } void StorageSerializer::dump(const PolicyType &policyType) { @@ -91,7 +91,7 @@ void StorageSerializer::dump(const PolicyCollection::value_type &policy) { const auto &key = policy->key(); const auto &result = policy->result(); - dumpFields(key, result.policyType(), result.metadata()); + dumpFields(key.client(), key.user(), key.privilege(), result.policyType(), result.metadata()); } } /* namespace Cynara */ diff --git a/src/service/storage/StorageSerializer.h b/src/service/storage/StorageSerializer.h index f4397ba..214287b 100644 --- a/src/service/storage/StorageSerializer.h +++ b/src/service/storage/StorageSerializer.h @@ -65,7 +65,7 @@ protected: *m_outStream << recordSeparator(); } - void dump(const PolicyKey &key); + void dump(const PolicyKeyFeature &keyFeature); void dump(const PolicyType &policyType); void dump(const PolicyResult::PolicyMetadata &metadata); void dump(const PolicyCollection::value_type &policy); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index f3eee4b..72c77fd 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -70,6 +70,7 @@ SET(CYNARA_TESTS_SOURCES storage/serializer/bucket_load.cpp storage/serializer/deserialize.cpp storage/serializer/dump.cpp + storage/serializer/dump_load.cpp storage/serializer/serialize.cpp common/types/policybucket.cpp helpers.cpp diff --git a/test/storage/serializer/bucket_load.cpp b/test/storage/serializer/bucket_load.cpp index b43d12f..aa7d290 100644 --- a/test/storage/serializer/bucket_load.cpp +++ b/test/storage/serializer/bucket_load.cpp @@ -37,7 +37,7 @@ using namespace Cynara; -MATCHER_P(PolicyPtrEq, policy, "") { +MATCHER_P(PolicyAtPtrEq, policy, "") { return std::tie(policy->key(), policy->result()) == std::tie(arg->key(), arg->result()); } @@ -85,7 +85,7 @@ TEST_F(BucketDeserializerFixture, load_1) { auto policies = deserializer.loadPolicies(); auto expectedPolicy = createPolicy({ "c", "u", "p" }, { PredefinedPolicyType::DENY, "meta" }); - ASSERT_THAT(policies, UnorderedElementsAre(PolicyPtrEq(expectedPolicy))); + ASSERT_THAT(policies, UnorderedElementsAre(PolicyAtPtrEq(expectedPolicy))); } TEST_F(BucketDeserializerFixture, load_1_allow) { @@ -97,7 +97,7 @@ TEST_F(BucketDeserializerFixture, load_1_allow) { auto policies = deserializer.loadPolicies(); auto expectedPolicy = createPolicy({ "c", "u", "p" }, { PredefinedPolicyType::ALLOW, "meta" }); - ASSERT_THAT(policies, UnorderedElementsAre(PolicyPtrEq(expectedPolicy))); + ASSERT_THAT(policies, UnorderedElementsAre(PolicyAtPtrEq(expectedPolicy))); } TEST_F(BucketDeserializerFixture, load_1_no_meta_sep) { @@ -109,7 +109,7 @@ TEST_F(BucketDeserializerFixture, load_1_no_meta_sep) { auto policies = deserializer.loadPolicies(); auto expectedPolicy = createPolicy({ "c", "u", "p" }, { PredefinedPolicyType::DENY, "" }); - ASSERT_THAT(policies, UnorderedElementsAre(PolicyPtrEq(expectedPolicy))); + ASSERT_THAT(policies, UnorderedElementsAre(PolicyAtPtrEq(expectedPolicy))); } TEST_F(BucketDeserializerFixture, load_1_no_meta_no_sep) { @@ -121,7 +121,7 @@ TEST_F(BucketDeserializerFixture, load_1_no_meta_no_sep) { auto policies = deserializer.loadPolicies(); auto expectedPolicy = createPolicy({ "c", "u", "p" }, { PredefinedPolicyType::DENY, "" }); - ASSERT_THAT(policies, UnorderedElementsAre(PolicyPtrEq(expectedPolicy))); + ASSERT_THAT(policies, UnorderedElementsAre(PolicyAtPtrEq(expectedPolicy))); } TEST_F(BucketDeserializerFixture, load_2) { @@ -134,8 +134,8 @@ TEST_F(BucketDeserializerFixture, load_2) { auto policies = deserializer.loadPolicies(); auto expectedPolicy = createPolicy({ "c", "u", "p" }, { PredefinedPolicyType::DENY, "meta" }); - ASSERT_THAT(policies, UnorderedElementsAre(PolicyPtrEq(expectedPolicy), - PolicyPtrEq(expectedPolicy))); + ASSERT_THAT(policies, UnorderedElementsAre(PolicyAtPtrEq(expectedPolicy), + PolicyAtPtrEq(expectedPolicy))); } TEST_F(BucketDeserializerFixture, load_mixed) { @@ -158,10 +158,10 @@ TEST_F(BucketDeserializerFixture, load_mixed) { // How to do it more elegantly? ASSERT_THAT(policies, UnorderedElementsAre( - PolicyPtrEq(expectedPolices.at(0)), - PolicyPtrEq(expectedPolices.at(1)), - PolicyPtrEq(expectedPolices.at(2)), - PolicyPtrEq(expectedPolices.at(3)) + PolicyAtPtrEq(expectedPolices.at(0)), + PolicyAtPtrEq(expectedPolices.at(1)), + PolicyAtPtrEq(expectedPolices.at(2)), + PolicyAtPtrEq(expectedPolices.at(3)) )); } diff --git a/test/storage/serializer/dump.cpp b/test/storage/serializer/dump.cpp index d85950f..157a9f2 100644 --- a/test/storage/serializer/dump.cpp +++ b/test/storage/serializer/dump.cpp @@ -35,6 +35,16 @@ using namespace Cynara; +static std::string expectedPolicyKey(const PolicyKey &key) { + return key.client().toString() + ";" + key.user().toString() + ";" + key.privilege().toString(); +} + +static std::string expectedPolicyType(const PolicyType &type) { + std::ostringstream oss; + oss << std::hex << std::uppercase << "0x" << type; + return oss.str(); +} + TEST(serializer_dump, dump_empty_bucket) { std::ostringstream oss; PolicyBucket bucket; @@ -61,9 +71,8 @@ TEST(serializer_dump, dump_bucket) { // See: StorageSerializerFixture::dump_buckets in serialize.cpp std::stringstream expected; expected - << std::hex << std::uppercase - << pk1.toString() << ";" << "0x" << PredefinedPolicyType::ALLOW << ";" << "\n" - << pk2.toString() << ";" << "0x" << PredefinedPolicyType::DENY << ";" << "\n"; + << expectedPolicyKey(pk1) << ";" << expectedPolicyType(PredefinedPolicyType::ALLOW) << ";" << "\n" + << expectedPolicyKey(pk2) << ";" << expectedPolicyType(PredefinedPolicyType::DENY) << ";" << "\n"; ASSERT_EQ(expected.str(), outputStream.str()); } @@ -87,10 +96,9 @@ TEST(serializer_dump, dump_bucket_bucket) { // See: StorageSerializerFixture::dump_buckets in serialize.cpp std::stringstream expected; expected - << std::hex << std::uppercase - << pk1.toString() << ";" << "0x" << PredefinedPolicyType::BUCKET << ";" << bucketId << "\n" - << pk2.toString() << ";" << "0x" << PredefinedPolicyType::DENY << ";" << "\n" - << pk3.toString() << ";" << "0x" << PredefinedPolicyType::BUCKET << ";" << bucketId << "\n"; + << 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()); } diff --git a/test/storage/serializer/dump_load.cpp b/test/storage/serializer/dump_load.cpp new file mode 100644 index 0000000..02d90fd --- /dev/null +++ b/test/storage/serializer/dump_load.cpp @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * @file dump_load.cpp + * @author Aleksander Zdyb + * @version 1.0 + * @brief Tests for dump => load routine + */ + +#include + +#include +#include + +#include +#include +#include +#include + +#include "../../helpers.h" + +using namespace Cynara; + +// TODO: Move to helpers or other .h +MATCHER_P(PolicyAtPtrEq, policy, "") { + return std::tie(policy->key(), policy->result()) + == std::tie(arg->key(), arg->result()); +} + +// Test if dumping and loading yields the same result +TEST(dump_load, bucket) { + using ::testing::UnorderedElementsAre; + + PolicyKey pk1 = Helpers::generatePolicyKey("1"); + PolicyKey pk2 = Helpers::generatePolicyKey("2"); + PolicyKey pk3 = Helpers::generatePolicyKey("3"); + PolicyBucketId bucketId = Helpers::generateBucketId(); + PolicyBucket bucket = {{ Policy::bucketWithKey(pk1, bucketId), + Policy::simpleWithKey(pk2, PredefinedPolicyType::DENY), + Policy::bucketWithKey(pk3, bucketId) }}; + + auto ioStream = std::make_shared(); + + StorageSerializer serializer(ioStream); + serializer.dump(bucket); + + BucketDeserializer deserializer(ioStream); + const auto loadedPolicies = deserializer.loadPolicies(); + const auto &expectedPolicies = bucket.policyCollection(); + + ASSERT_THAT(loadedPolicies, UnorderedElementsAre( + PolicyAtPtrEq(expectedPolicies.at(0)), + PolicyAtPtrEq(expectedPolicies.at(1)), + PolicyAtPtrEq(expectedPolicies.at(2)) + )); +} -- 2.7.4