From ea25549b696469d62378a2334a06ffc12a934952 Mon Sep 17 00:00:00 2001 From: Adam Malinowski Date: Fri, 25 Jul 2014 11:01:14 +0200 Subject: [PATCH 01/16] Correct exception catching Exception should be caught by reference. Change-Id: I19e906aa892956b783f174f6d40c63b4320cda93 --- src/admin/api/admin-api.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/admin/api/admin-api.cpp b/src/admin/api/admin-api.cpp index 4087e40..ac10e26 100644 --- a/src/admin/api/admin-api.cpp +++ b/src/admin/api/admin-api.cpp @@ -121,7 +121,7 @@ int cynara_admin_set_policies(struct cynara_admin *p_cynara_admin, return CYNARA_ADMIN_API_INVALID_PARAM; } } - } catch (std::bad_alloc ex) { + } catch (const std::bad_alloc &ex) { return CYNARA_ADMIN_API_OUT_OF_MEMORY; } @@ -139,7 +139,7 @@ int cynara_admin_set_bucket(struct cynara_admin *p_cynara_admin, const char *buc std::string extraStr; try { extraStr = extra ? extra : ""; - } catch (std::bad_alloc ex) { + } catch (const std::bad_alloc &ex) { return CYNARA_ADMIN_API_OUT_OF_MEMORY; } switch (operation) { -- 2.7.4 From cea2cf9937ab5b8c6de3ec373f361bc805c12c21 Mon Sep 17 00:00:00 2001 From: Adam Malinowski Date: Fri, 25 Jul 2014 14:56:46 +0200 Subject: [PATCH 02/16] Fix implementation of what() method in Exception class Change-Id: Ib0259dbd85bc5526b474396ac82b819f0b03b54d --- src/common/exceptions/Exception.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/common/exceptions/Exception.h b/src/common/exceptions/Exception.h index 92546b2..b709060 100644 --- a/src/common/exceptions/Exception.h +++ b/src/common/exceptions/Exception.h @@ -36,13 +36,17 @@ public: virtual ~Exception() = default; virtual const char *what(void) const noexcept { - return (message() + " From: " + m_backtrace).c_str(); + if(m_whatMessage.empty()) { + m_whatMessage = message() + " From: " + m_backtrace; + } + return m_whatMessage.c_str(); } virtual const std::string message(void) const = 0; private: std::string m_backtrace; + mutable std::string m_whatMessage; }; } /* namespace Cynara */ -- 2.7.4 From 0b53fd2b841d05d0f588a60ae160385ed1d21f59 Mon Sep 17 00:00:00 2001 From: Adam Malinowski Date: Fri, 25 Jul 2014 11:17:36 +0200 Subject: [PATCH 03/16] Remove unneeded return statement Change-Id: Ib500dd5249cbe37adbab441bb6700fb8554e3c18 --- src/service/storage/InMemoryStorageBackend.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/service/storage/InMemoryStorageBackend.cpp b/src/service/storage/InMemoryStorageBackend.cpp index d04a6ff..bf1c1c9 100644 --- a/src/service/storage/InMemoryStorageBackend.cpp +++ b/src/service/storage/InMemoryStorageBackend.cpp @@ -193,7 +193,6 @@ void InMemoryStorageBackend::openDumpFileStream(std::shared_ptr s if (!stream->is_open()) { throw CannotCreateFileException(filename); - return; } } -- 2.7.4 From 2fe2569276fcf239fbc8a4177700598acaae7bd9 Mon Sep 17 00:00:00 2001 From: Adam Malinowski Date: Fri, 25 Jul 2014 11:31:28 +0200 Subject: [PATCH 04/16] Correct snprintf format string in Backtrace::buildBacktrace() method Additionally use static_cast and reduce scope of local variable. Change-Id: Idbaf8f6622ae8bf4e2610e378c6a001ea3a0acbf --- src/common/log/Backtrace.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/common/log/Backtrace.cpp b/src/common/log/Backtrace.cpp index d2e9550..36b3c27 100644 --- a/src/common/log/Backtrace.cpp +++ b/src/common/log/Backtrace.cpp @@ -62,7 +62,6 @@ const std::string Backtrace::buildBacktrace(void) { char proc_name[BUFSIZ]; char btstr[BUFSIZ]; unw_word_t offp; - char *realname; int status; unw_getcontext(&uc); @@ -73,11 +72,11 @@ const std::string Backtrace::buildBacktrace(void) { unw_get_reg(&cursor, UNW_REG_IP, &ip); unw_get_reg(&cursor, UNW_REG_SP, &sp); unw_get_proc_name(&cursor, proc_name, sizeof(proc_name), &offp); - realname = abi::__cxa_demangle(proc_name, 0, 0, &status); + char *realname = abi::__cxa_demangle(proc_name, 0, 0, &status); getSourceInfo(ip); - snprintf(btstr, sizeof(btstr), "ip = %lx, sp = %lx, %s, %s:%d\n", - (long) ip, (long) sp, realname ? realname : proc_name, + snprintf(btstr, sizeof(btstr), "ip = %p, sp = %p, %s, %s:%u\n", + ip, sp, realname ? realname : proc_name, m_fileName, m_lineNumber); free(realname); backtrace += btstr; -- 2.7.4 From e7c43f6d5493322520118501a7e88aeb2fbf83a2 Mon Sep 17 00:00:00 2001 From: Adam Malinowski Date: Fri, 25 Jul 2014 11:48:43 +0200 Subject: [PATCH 05/16] Reduce scope of variables Change-Id: Ib18cde12f9508cb17469bcef0bb38a6665d6297d --- src/common/sockets/Socket.cpp | 6 ++---- src/service/main/main.cpp | 4 +--- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/src/common/sockets/Socket.cpp b/src/common/sockets/Socket.cpp index 5149d17..e666262 100644 --- a/src/common/sockets/Socket.cpp +++ b/src/common/sockets/Socket.cpp @@ -143,9 +143,8 @@ bool Socket::connect(void) { int retval = TEMP_FAILURE_RETRY(::connect(m_sock, (struct sockaddr*)&clientAddr, SUN_LEN(&clientAddr))); - int err = 0; if (retval == -1) { - err = errno; + int err = errno; if (err == EINPROGRESS) { if (!waitForSocket(POLLOUT)) { return false; @@ -165,7 +164,6 @@ bool Socket::connect(void) { } bool Socket::sendToServer(BinaryQueue &queue) { - ssize_t done = 0; bool retry = false; RawBuffer buffer(queue.size()); @@ -178,7 +176,7 @@ bool Socket::sendToServer(BinaryQueue &queue) { } retry = false; - done = 0; + ssize_t done = 0; while ((buffer.size() - done) > 0) { if (! waitForSocket(POLLOUT)) { LOGE("Error in poll(POLLOUT)"); diff --git a/src/service/main/main.cpp b/src/service/main/main.cpp index ce7b07b..23362c2 100644 --- a/src/service/main/main.cpp +++ b/src/service/main/main.cpp @@ -34,8 +34,6 @@ #include "Cynara.h" int main(int argc UNUSED, char **argv UNUSED) { - int ret; - init_log(); try { @@ -44,7 +42,7 @@ int main(int argc UNUSED, char **argv UNUSED) { cynara.init(); LOGI("Cynara service is started"); - ret = sd_notify(0, "READY=1"); + int ret = sd_notify(0, "READY=1"); if (ret == 0) { LOGW("Cynara was not configured to notify its status"); } else if (ret < 0) { -- 2.7.4 From f49cae653af93878738f5143c97b4e7ebcab200b Mon Sep 17 00:00:00 2001 From: Aleksander Zdyb Date: Thu, 24 Jul 2014 12:00:24 +0200 Subject: [PATCH 06/16] 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 From 501d3b9fe8660c02e98711a82bd23b4989923b2c Mon Sep 17 00:00:00 2001 From: Aleksander Zdyb Date: Wed, 23 Jul 2014 08:10:59 +0200 Subject: [PATCH 07/16] Make (de)serializer classes more generic and easier testable StorageSerializer and StorageDeserializer now accept std::ostream and std::istream respectively, to enable unit testing. Storage::insertPolicies() body has been slightly rearranged to be more readable. Change-Id: I54296a679a5d1b736ae4f3829f718b6bf8aea66e --- src/service/storage/BucketDeserializer.h | 4 ++-- src/service/storage/InMemoryStorageBackend.cpp | 8 ++++---- src/service/storage/Storage.cpp | 24 ++++++++++++------------ src/service/storage/Storage.h | 4 ++-- src/service/storage/StorageDeserializer.cpp | 2 +- src/service/storage/StorageDeserializer.h | 4 ++-- src/service/storage/StorageSerializer.cpp | 2 +- src/service/storage/StorageSerializer.h | 4 ++-- 8 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/service/storage/BucketDeserializer.h b/src/service/storage/BucketDeserializer.h index ad54100..64adb51 100644 --- a/src/service/storage/BucketDeserializer.h +++ b/src/service/storage/BucketDeserializer.h @@ -34,7 +34,7 @@ namespace Cynara { class BucketDeserializer { public: - BucketDeserializer(std::shared_ptr inStream) : m_inStream(inStream) { + BucketDeserializer(std::shared_ptr inStream) : m_inStream(inStream) { } PolicyCollection loadPolicies(void); @@ -43,7 +43,7 @@ protected: static PolicyKey parseKey(const std::string &line, std::size_t &beginToken); private: - std::shared_ptr m_inStream; + std::shared_ptr m_inStream; }; } /* namespace Cynara */ diff --git a/src/service/storage/InMemoryStorageBackend.cpp b/src/service/storage/InMemoryStorageBackend.cpp index fa20744..a0283f7 100644 --- a/src/service/storage/InMemoryStorageBackend.cpp +++ b/src/service/storage/InMemoryStorageBackend.cpp @@ -52,7 +52,7 @@ void InMemoryStorageBackend::load(void) { std::string indexFilename = m_dbPath + m_indexFileName; try { - std::shared_ptr indexStream = std::make_shared(); + auto indexStream = std::make_shared(); openFileStream(indexStream, indexFilename); StorageDeserializer storageDeserializer(indexStream, @@ -83,7 +83,7 @@ void InMemoryStorageBackend::save(void) { } } - std::shared_ptr indexStream = std::make_shared(); + auto indexStream = std::make_shared(); openDumpFileStream(indexStream, m_dbPath + m_indexFileName); StorageSerializer storageSerializer(indexStream); @@ -200,7 +200,7 @@ void InMemoryStorageBackend::openDumpFileStream(std::shared_ptr s std::shared_ptr InMemoryStorageBackend::bucketStreamOpener( const PolicyBucketId &bucketId) { std::string bucketFilename = m_dbPath + "_" + bucketId; - std::shared_ptr bucketStream = std::make_shared(); + auto bucketStream = std::make_shared(); try { openFileStream(bucketStream, bucketFilename); return std::make_shared(bucketStream); @@ -212,7 +212,7 @@ std::shared_ptr InMemoryStorageBackend::bucketStreamOpener( std::shared_ptr InMemoryStorageBackend::bucketDumpStreamOpener( const PolicyBucketId &bucketId) { std::string bucketFilename = m_dbPath + "_" + bucketId; - std::shared_ptr bucketStream = std::make_shared(); + auto bucketStream = std::make_shared(); openDumpFileStream(bucketStream, bucketFilename); return std::make_shared(bucketStream); diff --git a/src/service/storage/Storage.cpp b/src/service/storage/Storage.cpp index cfbd82d..7bf3f9c 100644 --- a/src/service/storage/Storage.cpp +++ b/src/service/storage/Storage.cpp @@ -80,16 +80,16 @@ PolicyResult Storage::minimalPolicy(const PolicyBucket &bucket, const PolicyKey return minimal; } -void Storage::insertPolicies(const std::map> &policies) { - for (const auto &bucket : policies) { - const PolicyBucketId &bucketId = bucket.first; - for (const auto &policy : bucket.second) { - PolicyPtr policyPtr = std::make_shared(policy); - auto existingPolicies = m_backend.searchBucket(bucketId, policyPtr->key()); - for (auto existingPolicy : existingPolicies.policyCollection()) { - m_backend.deletePolicy(bucketId, existingPolicy->key()); - } - m_backend.insertPolicy(bucketId, policyPtr); +void Storage::insertPolicies(const std::map> &policiesByBucketId) { + for (const auto &group : policiesByBucketId) { + const PolicyBucketId &bucketId = group.first; + const auto &policies = group.second; + for (const auto &policy : policies) { + auto existingPolicies = m_backend.searchBucket(bucketId, policy.key()); + for (auto existingPolicy : existingPolicies.policyCollection()) { + m_backend.deletePolicy(bucketId, existingPolicy->key()); + } + m_backend.insertPolicy(bucketId, std::make_shared(policy)); } } } @@ -113,8 +113,8 @@ void Storage::deleteBucket(const PolicyBucketId &bucketId) { m_backend.deleteBucket(bucketId); } -void Storage::deletePolicies(const std::map> &policies) { - for (const auto &bucket : policies) { +void Storage::deletePolicies(const std::map> &keysByBucketId) { + for (const auto &bucket : keysByBucketId) { const PolicyBucketId &bucketId = bucket.first; for (const auto &policyKey : bucket.second) { m_backend.deletePolicy(bucketId, policyKey); diff --git a/src/service/storage/Storage.h b/src/service/storage/Storage.h index 23955ff..adee6ec 100644 --- a/src/service/storage/Storage.h +++ b/src/service/storage/Storage.h @@ -45,8 +45,8 @@ public: PolicyResult checkPolicy(const PolicyKey &key); - void insertPolicies(const std::map> &policies); - void deletePolicies(const std::map> &policies); + void insertPolicies(const std::map> &policiesByBucketId); + void deletePolicies(const std::map> &keysByBucketId); void addOrUpdateBucket(const PolicyBucketId &bucketId, const PolicyResult &defaultBucketPolicy); void deleteBucket(const PolicyBucketId &bucketId); diff --git a/src/service/storage/StorageDeserializer.cpp b/src/service/storage/StorageDeserializer.cpp index 9b34e5f..b0522e8 100644 --- a/src/service/storage/StorageDeserializer.cpp +++ b/src/service/storage/StorageDeserializer.cpp @@ -36,7 +36,7 @@ namespace Cynara { -StorageDeserializer::StorageDeserializer(std::shared_ptr inStream, +StorageDeserializer::StorageDeserializer(std::shared_ptr inStream, BucketStreamOpener bucketStreamOpener) : m_inStream(inStream), m_bucketStreamOpener(bucketStreamOpener) { } diff --git a/src/service/storage/StorageDeserializer.h b/src/service/storage/StorageDeserializer.h index d186ff7..3c9e3f7 100644 --- a/src/service/storage/StorageDeserializer.h +++ b/src/service/storage/StorageDeserializer.h @@ -40,7 +40,7 @@ class StorageDeserializer { public: typedef std::function(const std::string &)> BucketStreamOpener; - StorageDeserializer(std::shared_ptr inStream, + StorageDeserializer(std::shared_ptr inStream, BucketStreamOpener m_bucketStreamOpener); void initBuckets(Buckets &buckets); void loadBuckets(Buckets &buckets); @@ -51,7 +51,7 @@ public: std::size_t &beginToken); private: - std::shared_ptr m_inStream; + std::shared_ptr m_inStream; BucketStreamOpener m_bucketStreamOpener; }; diff --git a/src/service/storage/StorageSerializer.cpp b/src/service/storage/StorageSerializer.cpp index f6e42fe..db5f331 100644 --- a/src/service/storage/StorageSerializer.cpp +++ b/src/service/storage/StorageSerializer.cpp @@ -38,7 +38,7 @@ namespace Cynara { char StorageSerializer::m_fieldSeparator = ';'; char StorageSerializer::m_recordSeparator = '\n'; -StorageSerializer::StorageSerializer(std::shared_ptr os) : m_outStream(os) { +StorageSerializer::StorageSerializer(std::shared_ptr os) : m_outStream(os) { } void StorageSerializer::dump(const Buckets &buckets, diff --git a/src/service/storage/StorageSerializer.h b/src/service/storage/StorageSerializer.h index 214287b..a995265 100644 --- a/src/service/storage/StorageSerializer.h +++ b/src/service/storage/StorageSerializer.h @@ -44,7 +44,7 @@ public: typedef std::function(const PolicyBucketId &)> BucketStreamOpener; - StorageSerializer(std::shared_ptr os); + StorageSerializer(std::shared_ptr os); virtual ~StorageSerializer() = default; virtual void dump(const Buckets &buckets, @@ -71,7 +71,7 @@ protected: void dump(const PolicyCollection::value_type &policy); private: - std::shared_ptr m_outStream; + std::shared_ptr m_outStream; static char m_fieldSeparator; static char m_recordSeparator; -- 2.7.4 From 93103cf431c67c2204c711182ab5d666f251e7cd Mon Sep 17 00:00:00 2001 From: Aleksander Zdyb Date: Fri, 25 Jul 2014 11:00:53 +0200 Subject: [PATCH 08/16] 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 From 3b3b2ef8c183ba3ac743f13748ea70408158d157 Mon Sep 17 00:00:00 2001 From: Aleksander Zdyb Date: Fri, 25 Jul 2014 10:25:56 +0200 Subject: [PATCH 09/16] Fix Cynara::PolicyBucket tests Wildcards test has been inverted -- now wildcard policies are stored in buckets and checks are made only against concrete keys. Change-Id: I8b1a02a6648961403829564bf81d57dd42e1036d --- test/common/types/policybucket.cpp | 76 ++++++++++++++++++++++---------------- 1 file changed, 44 insertions(+), 32 deletions(-) diff --git a/test/common/types/policybucket.cpp b/test/common/types/policybucket.cpp index db6c761..9e47c21 100644 --- a/test/common/types/policybucket.cpp +++ b/test/common/types/policybucket.cpp @@ -40,18 +40,21 @@ public: protected: const PolicyKey pk1 = Helpers::generatePolicyKey("1"); + const PolicyKey pk2 = Helpers::generatePolicyKey("2"); + const PolicyKey pk3 = Helpers::generatePolicyKey("3"); const PolicyKey otherPk = Helpers::generatePolicyKey("_"); - const PolicyCollection pk1Policies = { + const PolicyCollection pkPolicies = { Policy::simpleWithKey(pk1, PredefinedPolicyType::ALLOW), - Policy::simpleWithKey(pk1, PredefinedPolicyType::ALLOW), - Policy::simpleWithKey(pk1, PredefinedPolicyType::ALLOW) + Policy::simpleWithKey(pk2, PredefinedPolicyType::ALLOW), + Policy::simpleWithKey(pk3, PredefinedPolicyType::ALLOW) }; const PolicyCollection wildcardPolicies = { - Policy::simpleWithKey(PolicyKey("c1", "u1", "p1"), PredefinedPolicyType::ALLOW), - Policy::simpleWithKey(PolicyKey("c1", "u1", "p2"), PredefinedPolicyType::ALLOW), - Policy::simpleWithKey(PolicyKey("c2", "u1", "p1"), PredefinedPolicyType::ALLOW) + Policy::simpleWithKey(PolicyKey("c1", "u1", "*"), PredefinedPolicyType::ALLOW), + Policy::simpleWithKey(PolicyKey("*", "u1", "p2"), PredefinedPolicyType::ALLOW), + Policy::simpleWithKey(PolicyKey("*", "*", "p1"), PredefinedPolicyType::ALLOW), + Policy::simpleWithKey(PolicyKey("*", "*", "*"), PredefinedPolicyType::ALLOW) }; PolicyCollection filterHelper(const PolicyCollection &original, @@ -62,6 +65,15 @@ protected: filtered.resize(std::distance(std::begin(filtered), endIt)); return filtered; } + + PolicyCollection filterHelper(const PolicyCollection &original, std::vector idx) { + PolicyCollection filtered; + filtered.reserve(idx.size()); + for (const auto &i : idx) { + filtered.push_back(original.at(i)); + } + return filtered; + } }; TEST_F(PolicyBucketFixture, filtered) { @@ -69,12 +81,12 @@ TEST_F(PolicyBucketFixture, filtered) { using ::testing::UnorderedElementsAre; using ::testing::IsEmpty; - PolicyBucket bucket(pk1Policies); + PolicyBucket bucket(pkPolicies); bucket.setDefaultPolicy(PredefinedPolicyType::DENY); auto filtered = bucket.filtered(pk1); // Elements match - ASSERT_THAT(filtered.policyCollection(), UnorderedElementsAreArray(pk1Policies)); + ASSERT_THAT(filtered.policyCollection(), UnorderedElementsAre(pkPolicies.at(0))); // default policy matches ASSERT_EQ(PredefinedPolicyType::DENY, filtered.defaultPolicy()); @@ -85,7 +97,7 @@ TEST_F(PolicyBucketFixture, filtered_other) { using ::testing::UnorderedElementsAre; using ::testing::IsEmpty; - PolicyBucket bucket(pk1Policies); + PolicyBucket bucket(pkPolicies); bucket.setDefaultPolicy(PredefinedPolicyType::DENY); auto filtered = bucket.filtered(otherPk); @@ -96,55 +108,55 @@ TEST_F(PolicyBucketFixture, filtered_other) { ASSERT_EQ(PredefinedPolicyType::DENY, filtered.defaultPolicy()); } -TEST_F(PolicyBucketFixture, filtered_wildcard_privilege) { +TEST_F(PolicyBucketFixture, filtered_wildcard_1) { + using ::testing::UnorderedElementsAreArray; + + // Leave policies with given client, given user and any privilege + auto policiesToStay = filterHelper(wildcardPolicies, { 0, 1, 3 }); + + PolicyBucket bucket(wildcardPolicies); + auto filtered = bucket.filtered(PolicyKey("c1", "u1", "p2")); + ASSERT_THAT(filtered.policyCollection(), UnorderedElementsAreArray(policiesToStay)); +} + +TEST_F(PolicyBucketFixture, filtered_wildcard_2) { using ::testing::UnorderedElementsAreArray; // Leave policies with given client, given user and any privilege - auto policiesToStay = filterHelper(wildcardPolicies, - [] (const PolicyCollection::value_type &privilege) { - const auto &key = privilege->key(); - return std::tie("c1", "u1") == std::tie(key.client(), key.user()); - }); + auto policiesToStay = filterHelper(wildcardPolicies, std::vector{ 2, 3 }); PolicyBucket bucket(wildcardPolicies); - auto filtered = bucket.filtered(PolicyKey("c1", "u1", "*")); + auto filtered = bucket.filtered(PolicyKey("cccc", "u1", "p1")); + ASSERT_THAT(filtered.policyCollection(), UnorderedElementsAreArray(policiesToStay)); } -TEST_F(PolicyBucketFixture, filtered_wildcard_client) { +TEST_F(PolicyBucketFixture, filtered_wildcard_3) { using ::testing::UnorderedElementsAreArray; // Leave policies with given client, given user and any privilege - auto policiesToStay = filterHelper(wildcardPolicies, - [] (const PolicyCollection::value_type &privilege) { - const auto &key = privilege->key(); - return std::tie("u1", "p1") == std::tie(key.user(), key.privilege()); - }); + auto policiesToStay = filterHelper(wildcardPolicies, std::vector{ 0, 3 }); PolicyBucket bucket(wildcardPolicies); - auto filtered = bucket.filtered(PolicyKey("*", "u1", "p1")); + auto filtered = bucket.filtered(PolicyKey("c1", "u1", "pppp")); ASSERT_THAT(filtered.policyCollection(), UnorderedElementsAreArray(policiesToStay)); } -TEST_F(PolicyBucketFixture, filtered_wildcard_client_privilege) { +TEST_F(PolicyBucketFixture, filtered_wildcard_4) { using ::testing::UnorderedElementsAreArray; // Leave policies with given client, given user and any privilege - auto policiesToStay = filterHelper(wildcardPolicies, - [] (const PolicyCollection::value_type &privilege) { - const auto &key = privilege->key(); - return key.user() == "u1"; - }); + auto policiesToStay = filterHelper(wildcardPolicies, std::vector{ 3 }); PolicyBucket bucket(wildcardPolicies); - auto filtered = bucket.filtered(PolicyKey("*", "u1", "*")); + auto filtered = bucket.filtered(PolicyKey("cccc", "uuuu", "pppp")); ASSERT_THAT(filtered.policyCollection(), UnorderedElementsAreArray(policiesToStay)); } TEST_F(PolicyBucketFixture, filtered_wildcard_none) { using ::testing::IsEmpty; - PolicyBucket bucket(wildcardPolicies); - auto filtered = bucket.filtered(PolicyKey("*", "u2", "*")); + PolicyBucket bucket({ wildcardPolicies.begin(), wildcardPolicies.begin() + 3 }); + auto filtered = bucket.filtered(PolicyKey("cccc", "uuuu", "pppp")); ASSERT_THAT(filtered.policyCollection(), IsEmpty()); } -- 2.7.4 From 1ce53fc8df74aa0b18af2954f1a7ae6bf4baaeee Mon Sep 17 00:00:00 2001 From: Adam Malinowski Date: Tue, 22 Jul 2014 11:27:30 +0200 Subject: [PATCH 10/16] Enable logs in admin library Change-Id: Ib54497540ce9bb61e98e9cb50317bc71ccbb8ffa --- src/admin/api/admin-api.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/admin/api/admin-api.cpp b/src/admin/api/admin-api.cpp index ac10e26..d6e6a12 100644 --- a/src/admin/api/admin-api.cpp +++ b/src/admin/api/admin-api.cpp @@ -26,6 +26,7 @@ #include #include +#include #include #include #include @@ -58,6 +59,10 @@ int cynara_admin_initialize(struct cynara_admin **pp_cynara_admin) { return CYNARA_ADMIN_API_OUT_OF_MEMORY; } + init_log(); + + LOGD("Cynara admin initialized"); + return CYNARA_ADMIN_API_SUCCESS; } -- 2.7.4 From 8cd4946364d609d810569a2074b0a81bdb8b6529 Mon Sep 17 00:00:00 2001 From: Adam Malinowski Date: Tue, 22 Jul 2014 11:11:02 +0200 Subject: [PATCH 11/16] Add SIGPIPE ignoring in client and admin libraries Change-Id: I485dee789cc3916256affea310b7bd288200eec6 --- src/admin/api/admin-api.cpp | 3 +++ src/client/api/client-api.cpp | 4 ++++ src/common/CMakeLists.txt | 1 + src/common/system/signals.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ src/common/system/signals.h | 30 ++++++++++++++++++++++++++++++ 5 files changed, 79 insertions(+) create mode 100644 src/common/system/signals.cpp create mode 100644 src/common/system/signals.h diff --git a/src/admin/api/admin-api.cpp b/src/admin/api/admin-api.cpp index d6e6a12..3c72744 100644 --- a/src/admin/api/admin-api.cpp +++ b/src/admin/api/admin-api.cpp @@ -27,6 +27,7 @@ #include #include +#include #include #include #include @@ -61,6 +62,8 @@ int cynara_admin_initialize(struct cynara_admin **pp_cynara_admin) { init_log(); + ignore_sigpipe(); + LOGD("Cynara admin initialized"); return CYNARA_ADMIN_API_SUCCESS; diff --git a/src/client/api/client-api.cpp b/src/client/api/client-api.cpp index 4fc1c1d..8b34981 100644 --- a/src/client/api/client-api.cpp +++ b/src/client/api/client-api.cpp @@ -25,6 +25,7 @@ #include #include +#include #include #include @@ -53,6 +54,9 @@ int cynara_initialize(cynara **pp_cynara, const cynara_configuration *p_conf UNU } init_log(); + + ignore_sigpipe(); + LOGD("Cynara client initialized"); return cynara_api_result::CYNARA_API_SUCCESS; diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index d2d5e03..92874a7 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -42,6 +42,7 @@ SET(COMMON_SOURCES ${COMMON_PATH}/response/ResponseTaker.cpp ${COMMON_PATH}/sockets/Socket.cpp ${COMMON_PATH}/sockets/SocketClient.cpp + ${COMMON_PATH}/system/signals.cpp ${COMMON_PATH}/types/PolicyBucket.cpp ${COMMON_PATH}/types/PolicyKey.cpp ) diff --git a/src/common/system/signals.cpp b/src/common/system/signals.cpp new file mode 100644 index 0000000..6d74f4b --- /dev/null +++ b/src/common/system/signals.cpp @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Lukasz Wojciechowski + * + * 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 signals.cpp + * @author Adam Malinowski + * @version 1.0 + * @brief Implementation of signal related functions + */ + +#include +#include + +#include + +#include "signals.h" + +void ignore_sigpipe(void) +{ + struct sigaction act; + + memset(&act, 0, sizeof(act)); + act.sa_handler = SIG_IGN; + + if (sigaction(SIGPIPE, &act, NULL)) + LOGE("sigaction failed during setting SIGPIPE handler to ignore"); +} diff --git a/src/common/system/signals.h b/src/common/system/signals.h new file mode 100644 index 0000000..ef21ebb --- /dev/null +++ b/src/common/system/signals.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Lukasz Wojciechowski + * + * 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 signals.h + * @author Adam Malinowski + * @version 1.0 + * @brief Declaration of signal related functions + */ + +#ifndef SRC_COMMON_SYSTEM_SIGNALS_H_ +#define SRC_COMMON_SYSTEM_SIGNALS_H_ + +void ignore_sigpipe(void); + +#endif /* SRC_COMMON_SYSTEM_SIGNALS_H_ */ -- 2.7.4 From 63a0d0b6d10f51e202cc78b731d9f4d012a9325c Mon Sep 17 00:00:00 2001 From: Adam Malinowski Date: Mon, 28 Jul 2014 10:31:17 +0200 Subject: [PATCH 12/16] Remove unused bucket visitor mechanism from BinaryQueue class Change-Id: I0601ad50a6bb3ab9e1eba6323ba74faa1f07f51f --- src/common/containers/BinaryQueue.cpp | 22 ------------------- src/common/containers/BinaryQueue.h | 40 ----------------------------------- 2 files changed, 62 deletions(-) diff --git a/src/common/containers/BinaryQueue.cpp b/src/common/containers/BinaryQueue.cpp index b56f950..beab980 100644 --- a/src/common/containers/BinaryQueue.cpp +++ b/src/common/containers/BinaryQueue.cpp @@ -248,26 +248,4 @@ BinaryQueue::Bucket::~Bucket() { deleter(buffer, size, param); } -BinaryQueue::BucketVisitor::~BucketVisitor() { -} - -BinaryQueue::BucketVisitorCall::BucketVisitorCall(BucketVisitor *visitor) : - m_visitor(visitor) { -} - -BinaryQueue::BucketVisitorCall::~BucketVisitorCall() { -} - -void BinaryQueue::BucketVisitorCall::operator()(Bucket *bucket) const { - m_visitor->onVisitBucket(bucket->ptr, bucket->left); -} - -void BinaryQueue::visitBuckets(BucketVisitor *visitor) const { - if(visitor == nullptr) - throw NullPointerException("visitor"); - - // Visit all buckets - std::for_each(m_buckets.begin(), m_buckets.end(), BucketVisitorCall(visitor)); -} - } // namespace Cynara diff --git a/src/common/containers/BinaryQueue.h b/src/common/containers/BinaryQueue.h index ccfbffe..962a132 100644 --- a/src/common/containers/BinaryQueue.h +++ b/src/common/containers/BinaryQueue.h @@ -48,24 +48,6 @@ class BinaryQueue size_t bufferSize, void *userParam); - class BucketVisitor - { - public: - /** - * Destructor - */ - virtual ~BucketVisitor(); - - /** - * Visit bucket - * - * @return none - * @param[in] buffer Constant pointer to bucket data buffer - * @param[in] bufferSize Number of bytes in bucket - */ - virtual void onVisitBucket(const void *buffer, size_t bufferSize) = 0; - }; - private: struct Bucket { @@ -96,18 +78,6 @@ class BinaryQueue static void deleteBucket(Bucket *bucket); - class BucketVisitorCall - { - private: - BucketVisitor *m_visitor; - - public: - BucketVisitorCall(BucketVisitor *visitor); - virtual ~BucketVisitorCall(); - - void operator()(Bucket *bucket) const; - }; - public: /** * Construct empty binary queue @@ -269,16 +239,6 @@ class BinaryQueue * is larger than available bytes in binary queue */ void flattenConsume(void *buffer, size_t bufferSize); - - /** - * Visit each buffer with data using visitor object - * - * @return none - * @param[in] visitor Pointer to bucket visitor - * @see BinaryQueue::BucketVisitor - * @exception Cynara::NullPointerException visitor is nullptr - */ - void visitBuckets(BucketVisitor *visitor) const; }; } // namespace Cynara -- 2.7.4 From 4194e21c1e29d6f627979dc4a4fcc48d0c107b5f Mon Sep 17 00:00:00 2001 From: Adam Malinowski Date: Mon, 28 Jul 2014 11:00:14 +0200 Subject: [PATCH 13/16] Make few methods static in Cynara and SocketManager classes This change is due to cppcheck report. Change-Id: Ib163bc4294d4afdf3742564a7fd0d4bf7a81304f --- src/service/main/Cynara.cpp | 2 +- src/service/main/Cynara.h | 2 +- src/service/sockets/SocketManager.h | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/service/main/Cynara.cpp b/src/service/main/Cynara.cpp index 8ad22e2..7772a37 100644 --- a/src/service/main/Cynara.cpp +++ b/src/service/main/Cynara.cpp @@ -44,7 +44,7 @@ Cynara::~Cynara() { finalize(); } -const std::string Cynara::storageDir(void) const { +const std::string Cynara::storageDir(void) { std::string dir("/var/lib/cynara/"); #ifdef CYNARA_STATE_PATH diff --git a/src/service/main/Cynara.h b/src/service/main/Cynara.h index 2e962c5..d76d9f3 100644 --- a/src/service/main/Cynara.h +++ b/src/service/main/Cynara.h @@ -34,7 +34,7 @@ private: StoragePtr m_storage; StorageBackendPtr m_storageBackend; - const std::string storageDir(void) const; + static const std::string storageDir(void); public: Cynara(); diff --git a/src/service/sockets/SocketManager.h b/src/service/sockets/SocketManager.h index 2acd810..ae0906b 100644 --- a/src/service/sockets/SocketManager.h +++ b/src/service/sockets/SocketManager.h @@ -76,8 +76,8 @@ private: bool handleRead(int fd, const RawBuffer &readbuffer); void createDomainSocket(ProtocolPtr protocol, const std::string &path, mode_t mask); - int createDomainSocketHelp(const std::string &path, mode_t mask); - int getSocketFromSystemD(const std::string &path); + static int createDomainSocketHelp(const std::string &path, mode_t mask); + static int getSocketFromSystemD(const std::string &path); void createSignalSocket(ProtocolPtr protocol); Descriptor &createDescriptor(int fd); -- 2.7.4 From f081b8ae44dd2c54f9c3fb941b4346e936c825b1 Mon Sep 17 00:00:00 2001 From: Adam Malinowski Date: Mon, 28 Jul 2014 11:24:21 +0200 Subject: [PATCH 14/16] Make protocol() method const in Descriptor class Change-Id: Ic4d14c09af552c9a675f1f2f7fe6c696277b7010 --- src/service/sockets/Descriptor.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/service/sockets/Descriptor.h b/src/service/sockets/Descriptor.h index 86ee4db..b49ef03 100644 --- a/src/service/sockets/Descriptor.h +++ b/src/service/sockets/Descriptor.h @@ -57,7 +57,7 @@ public: bool hasDataToWrite(void) const; - ProtocolPtr protocol(void) { + const ProtocolPtr protocol(void) const { return m_protocol; } -- 2.7.4 From 3f6c825b3e9a0e69034ce4a608d6407075963f6e Mon Sep 17 00:00:00 2001 From: Aleksander Zdyb Date: Thu, 31 Jul 2014 14:35:39 +0200 Subject: [PATCH 15/16] Add Cynara::PolicyKeyHelpers PolicyKeyHelpers is an utility class holding helper functions to manage policy keys. Change-Id: I2f9195d62fc22809989890b0914f385b1c05d0b6 --- src/common/types/PolicyKeyHelpers.cpp | 64 +++++++++++++++++++++++++++++++++++ src/common/types/PolicyKeyHelpers.h | 42 +++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 src/common/types/PolicyKeyHelpers.cpp create mode 100644 src/common/types/PolicyKeyHelpers.h diff --git a/src/common/types/PolicyKeyHelpers.cpp b/src/common/types/PolicyKeyHelpers.cpp new file mode 100644 index 0000000..4d7efc6 --- /dev/null +++ b/src/common/types/PolicyKeyHelpers.cpp @@ -0,0 +1,64 @@ +/* + * 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 PolicyKeyHelpers.cpp + * @author Aleksander Zdyb + * @version 1.0 + * @brief Helper functions to manage Cynara::PolicyKey + */ + +#include + +#include "PolicyKeyHelpers.h" + +namespace Cynara { + +std::string PolicyKeyHelpers::glueKey(const PolicyKey &key) { + return glueKey(key.client(), key.user(), key.privilege()); +} + +std::string PolicyKeyHelpers::glueKey(const PolicyKeyFeature &client, + const PolicyKeyFeature &user, + const PolicyKeyFeature &privilege) { + const std::string sep = ";"; + const std::string c = client.toString(); + const std::string u = user.toString(); + const std::string p = privilege.toString(); + + std::ostringstream glued; + glued << c << sep << u << sep << p << sep << c.size() << sep << u.size() << sep << p.size(); + return glued.str(); +}; + +std::vector PolicyKeyHelpers::keyVariants(const PolicyKey &key) { + const auto &client = key.client(); + const auto &user = key.user(); + const auto &privilege = key.privilege(); + const auto w = PolicyKeyFeature::createWildcard(); + + return { + glueKey(client, user, privilege), + glueKey(w, user, privilege), + glueKey(client, w, privilege), + glueKey(client, user, w), + glueKey(w, w, privilege), + glueKey(w, user, w), + glueKey(client, w, w), + glueKey(w, w, w), + }; +} + +} /* namespace Cynara */ diff --git a/src/common/types/PolicyKeyHelpers.h b/src/common/types/PolicyKeyHelpers.h new file mode 100644 index 0000000..4c4dc7f --- /dev/null +++ b/src/common/types/PolicyKeyHelpers.h @@ -0,0 +1,42 @@ +/* + * 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 PolicyKeyHelpers.h + * @author Aleksander Zdyb + * @version 1.0 + * @brief Helper functions to manage Cynara::PolicyKey + */ +#ifndef SRC_COMMON_TYPES_POLICYKEYHELPERS_H_ +#define SRC_COMMON_TYPES_POLICYKEYHELPERS_H_ + +#include +#include + +#include + +namespace Cynara { + +class PolicyKeyHelpers { +public: + static std::string glueKey(const PolicyKey &client); + static std::string glueKey(const PolicyKeyFeature &client, const PolicyKeyFeature &user, + const PolicyKeyFeature &privilege); + static std::vector keyVariants(const PolicyKey &key); +}; + +} /* namespace Cynara */ + +#endif /* SRC_COMMON_TYPES_POLICYKEYHELPERS_H_ */ -- 2.7.4 From 938defaf19d30419de088631bd2a25f0b78d677e Mon Sep 17 00:00:00 2001 From: Aleksander Zdyb Date: Thu, 31 Jul 2014 15:01:13 +0200 Subject: [PATCH 16/16] Implement a better data structure to store policies A new map-based structure was used to store policies in Cynara::PolicyBucket. This allows policy checks to be much faster. PolicyBucket interface was changed, so the operation required a lot of refactorings across many storage classes. Most of functions are now simpler, clearer and decoupled from each other. Tests has been adjusted. No public API changes were required. Change-Id: Ida9dc8eb31120b27cf9547e56840dd27390de247 --- src/common/CMakeLists.txt | 1 + src/common/types/PolicyBucket.cpp | 49 ++++++++++++++-- src/common/types/PolicyBucket.h | 63 ++++++++++++-------- src/common/types/PolicyCollection.h | 24 ++++++-- src/service/storage/InMemoryStorageBackend.cpp | 14 +---- src/service/storage/Storage.cpp | 10 +--- src/service/storage/StorageDeserializer.cpp | 5 +- src/service/storage/StorageSerializer.cpp | 4 +- test/CMakeLists.txt | 1 + test/common/types/policybucket.cpp | 55 ++++++------------ test/helpers.h | 14 +++++ test/storage/inmemorystoragebackend/buckets.cpp | 2 +- .../inmemeorystoragebackendfixture.h | 12 +++- .../inmemorystoragebackend.cpp | 42 +++++++------- test/storage/inmemorystoragebackend/search.cpp | 38 ++++++------ test/storage/serializer/deserialize.cpp | 2 +- test/storage/serializer/dump.cpp | 67 ++++++++++++---------- test/storage/serializer/dump_load.cpp | 3 +- test/storage/storage/check.cpp | 8 +-- test/storage/storage/policies.cpp | 45 --------------- 20 files changed, 237 insertions(+), 222 deletions(-) diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 92874a7..5f25aea 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -45,6 +45,7 @@ SET(COMMON_SOURCES ${COMMON_PATH}/system/signals.cpp ${COMMON_PATH}/types/PolicyBucket.cpp ${COMMON_PATH}/types/PolicyKey.cpp + ${COMMON_PATH}/types/PolicyKeyHelpers.cpp ) IF (CMAKE_BUILD_TYPE MATCHES "DEBUG") diff --git a/src/common/types/PolicyBucket.cpp b/src/common/types/PolicyBucket.cpp index 4aea491..ab60604 100644 --- a/src/common/types/PolicyBucket.cpp +++ b/src/common/types/PolicyBucket.cpp @@ -20,6 +20,10 @@ * @brief Implementation of Cynara::PolicyBucket methods */ +#include + +#include +#include #include "PolicyBucket.h" @@ -28,15 +32,50 @@ namespace Cynara { PolicyBucket PolicyBucket::filtered(const PolicyKey &key) const { PolicyBucket result; - const auto &policies = policyCollection(); - std::copy_if(policies.begin(), policies.end(), std::back_inserter(result.policyCollection()), - [=] (PolicyCollection::value_type policy) { - return policy->key() == key; - }); + const auto &policies = m_policyCollection; + const auto variants = PolicyKeyHelpers::keyVariants(key); + + for (const auto &variant : variants) { + const auto policyIter = policies.find(variant); + if (policyIter != policies.end()) { + result.m_policyCollection[policyIter->first] = policyIter->second; + } + } // Inherit original policy result.setDefaultPolicy(defaultPolicy()); return result; } +void PolicyBucket::insertPolicy(PolicyPtr policy) { + const auto gluedKey = PolicyKeyHelpers::glueKey(policy->key()); + m_policyCollection[gluedKey] = policy; +} + +void PolicyBucket::deletePolicy(const PolicyKey &key) { + const auto gluedKey = PolicyKeyHelpers::glueKey(key); + m_policyCollection.erase(gluedKey); +} + +void PolicyBucket::deletePolicy(std::function predicate) { + auto &policies = m_policyCollection; + + for (auto iter = policies.begin(); iter != policies.end(); ) { + if (predicate(iter->second)) { + policies.erase(iter++); + } else { + ++iter; + } + } +} + +PolicyMap PolicyBucket::makePolicyMap(const PolicyCollection &policies) { + PolicyMap result; + for (const auto &policy : policies) { + const auto gluedKey = PolicyKeyHelpers::glueKey(policy->key()); + result[gluedKey] = policy; + } + return result; +} + } // namespace Cynara diff --git a/src/common/types/PolicyBucket.h b/src/common/types/PolicyBucket.h index 120cf36..b865d9e 100644 --- a/src/common/types/PolicyBucket.h +++ b/src/common/types/PolicyBucket.h @@ -25,17 +25,17 @@ #ifndef SRC_COMMON_TYPES_POLICYBUCKET_H_ #define SRC_COMMON_TYPES_POLICYBUCKET_H_ -#include "PolicyCollection.h" -#include "PolicyKey.h" -#include "Policy.h" -#include "exceptions/NotImplementedException.h" -#include "types/pointers.h" -#include "types/PolicyType.h" -#include "PolicyBucketId.h" - -#include -#include #include +#include +#include + +#include +#include +#include +#include +#include +#include +#include namespace Cynara { @@ -45,45 +45,60 @@ const PolicyBucketId defaultPolicyBucketId(""); class PolicyBucket { public: + typedef PolicyCollection::value_type value_type; + typedef const_policy_iterator const_iterator; + PolicyBucket() : m_defaultPolicy(PredefinedPolicyType::DENY) {} PolicyBucket(const PolicyBucketId &id, const PolicyResult &defaultPolicy) : m_defaultPolicy(defaultPolicy), m_id(id) {} PolicyBucket(const PolicyCollection &policies) - : m_policyCollection(policies), + : m_policyCollection(makePolicyMap(policies)), m_defaultPolicy(PredefinedPolicyType::DENY) {} PolicyBucket filtered(const PolicyKey &key) const; + void insertPolicy(PolicyPtr policy); + void deletePolicy(const PolicyKey &key); + + // TODO: Try to change interface, so this method is not needed + void deletePolicy(std::function predicate); + + static PolicyMap makePolicyMap(const PolicyCollection &policies); private: - PolicyCollection m_policyCollection; + PolicyMap m_policyCollection; PolicyResult m_defaultPolicy; PolicyBucketId m_id; public: - const PolicyResult &defaultPolicy() const { - return m_defaultPolicy; + + const_policy_iterator begin(void) const { + return const_policy_iterator(m_policyCollection.begin()); } - const PolicyBucketId &id() const { - return m_id; + const_policy_iterator end(void) const { + return const_policy_iterator(m_policyCollection.end()); + } + + PolicyMap::size_type size(void) const noexcept { + return m_policyCollection.size(); + } + + bool empty(void) const noexcept { + return m_policyCollection.empty(); } - PolicyCollection &policyCollection() { - return m_policyCollection; + const PolicyResult &defaultPolicy(void) const { + return m_defaultPolicy; } - const PolicyCollection &policyCollection() const { - return m_policyCollection; + const PolicyBucketId &id(void) const { + return m_id; } // TODO: Consider StorageBackend to be only one to alter this property void setDefaultPolicy(const PolicyResult &defaultPolicy) { m_defaultPolicy = defaultPolicy; } - - void setPolicyCollection(const PolicyCollection &policies) { - m_policyCollection = policies; - } }; } /* namespace Cynara */ diff --git a/src/common/types/PolicyCollection.h b/src/common/types/PolicyCollection.h index 0c864d0..bb25ee0 100644 --- a/src/common/types/PolicyCollection.h +++ b/src/common/types/PolicyCollection.h @@ -21,17 +21,31 @@ * @brief This file defines a collection of policies */ -#ifndef CYNARA_COMMON_TYPES_POLICYCOLLECTION_H -#define CYNARA_COMMON_TYPES_POLICYCOLLECTION_H - -#include "types/pointers.h" +#ifndef SRC_COMMON_TYPES_POLICYCOLLECTION_H_ +#define SRC_COMMON_TYPES_POLICYCOLLECTION_H_ +#include #include +#include "types/pointers.h" + namespace Cynara { typedef std::vector PolicyCollection; +typedef std::unordered_map PolicyMap; + +class const_policy_iterator : public PolicyMap::const_iterator +{ + public: + const_policy_iterator() : PolicyMap::const_iterator() {}; + const_policy_iterator(const PolicyMap::const_iterator &other) + : PolicyMap::const_iterator(other) {}; + + PolicyPtr operator*(void) { + return PolicyMap::const_iterator::operator*().second; + } +}; } // namespace Cynara -#endif /* CYNARA_COMMON_TYPES_POLICYCOLLECTION_H */ +#endif /* SRC_COMMON_TYPES_POLICYCOLLECTION_H_ */ diff --git a/src/service/storage/InMemoryStorageBackend.cpp b/src/service/storage/InMemoryStorageBackend.cpp index a0283f7..e46e485 100644 --- a/src/service/storage/InMemoryStorageBackend.cpp +++ b/src/service/storage/InMemoryStorageBackend.cpp @@ -108,8 +108,7 @@ PolicyBucket InMemoryStorageBackend::searchBucket(const PolicyBucketId &bucketId void InMemoryStorageBackend::insertPolicy(const PolicyBucketId &bucketId, PolicyPtr policy) { try { auto &bucket = buckets().at(bucketId); - auto &policies = bucket.policyCollection(); - policies.push_back(policy); + bucket.insertPolicy(policy); } catch (const std::out_of_range &) { throw BucketNotExistsException(bucketId); } @@ -147,11 +146,7 @@ void InMemoryStorageBackend::deletePolicy(const PolicyBucketId &bucketId, const try { // TODO: Move the erase code to PolicyCollection maybe? auto &bucket = buckets().at(bucketId); - auto &policies = bucket.policyCollection(); - policies.erase(remove_if(policies.begin(), policies.end(), - [key](PolicyPtr policy) -> bool { - return policy->key() == key; - }), policies.end()); + bucket.deletePolicy(key); } catch (const std::out_of_range &) { throw BucketNotExistsException(bucketId); } @@ -170,11 +165,8 @@ void InMemoryStorageBackend::deleteLinking(const PolicyBucketId &bucketId) { }; for (auto &bucketIter : buckets()) { - // TODO: Move the erase code to PolicyCollection maybe? auto &bucket = bucketIter.second; - auto &policies = bucket.policyCollection(); - policies.erase(remove_if(policies.begin(), policies.end(), bucketIdMatches), - policies.end()); + bucket.deletePolicy(bucketIdMatches); } } diff --git a/src/service/storage/Storage.cpp b/src/service/storage/Storage.cpp index 7bf3f9c..f8e7b8a 100644 --- a/src/service/storage/Storage.cpp +++ b/src/service/storage/Storage.cpp @@ -46,8 +46,6 @@ PolicyResult Storage::minimalPolicy(const PolicyBucket &bucket, const PolicyKey bool hasMinimal = false; PolicyResult minimal = bucket.defaultPolicy(); - const auto &policies = bucket.policyCollection(); - auto proposeMinimal = [&minimal, &hasMinimal](const PolicyResult &candidate) { if (hasMinimal == false) { minimal = candidate; @@ -57,8 +55,8 @@ PolicyResult Storage::minimalPolicy(const PolicyBucket &bucket, const PolicyKey hasMinimal = true; }; - for (const auto &policyRecord : policies) { - const auto &policyResult = policyRecord->result(); + for (const auto policy : bucket) { + const auto &policyResult = policy->result(); switch (policyResult.policyType()) { case PredefinedPolicyType::DENY: @@ -85,10 +83,6 @@ void Storage::insertPolicies(const std::map> const PolicyBucketId &bucketId = group.first; const auto &policies = group.second; for (const auto &policy : policies) { - auto existingPolicies = m_backend.searchBucket(bucketId, policy.key()); - for (auto existingPolicy : existingPolicies.policyCollection()) { - m_backend.deletePolicy(bucketId, existingPolicy->key()); - } m_backend.insertPolicy(bucketId, std::make_shared(policy)); } } diff --git a/src/service/storage/StorageDeserializer.cpp b/src/service/storage/StorageDeserializer.cpp index b0522e8..1f2181d 100644 --- a/src/service/storage/StorageDeserializer.cpp +++ b/src/service/storage/StorageDeserializer.cpp @@ -67,7 +67,10 @@ void StorageDeserializer::loadBuckets(Buckets &buckets) { auto bucketDeserializer = m_bucketStreamOpener(bucketId); if (bucketDeserializer != nullptr) { - bucket.setPolicyCollection(bucketDeserializer->loadPolicies()); + const auto policies = bucketDeserializer->loadPolicies(); + for (const auto policy : policies) { + bucket.insertPolicy(policy); + } } else { throw BucketDeserializationException(bucketId); } diff --git a/src/service/storage/StorageSerializer.cpp b/src/service/storage/StorageSerializer.cpp index db5f331..5d458d4 100644 --- a/src/service/storage/StorageSerializer.cpp +++ b/src/service/storage/StorageSerializer.cpp @@ -65,9 +65,7 @@ void StorageSerializer::dump(const Buckets &buckets, } void StorageSerializer::dump(const PolicyBucket& bucket) { - const auto &policies = bucket.policyCollection(); - - for (auto it = std::begin(policies); it != std::end(policies); ++it) { + for (auto it = std::begin(bucket); it != std::end(bucket); ++it) { const auto &policy = *it; dump(policy); } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 72c77fd..b391a4c 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -50,6 +50,7 @@ SET(CYNARA_SRC ${PROJECT_SOURCE_DIR}/../src) SET(CYNARA_SOURCES_FOR_TESTS ${CYNARA_SRC}/service/storage/Storage.cpp ${CYNARA_SRC}/common/types/PolicyBucket.cpp + ${CYNARA_SRC}/common/types/PolicyKeyHelpers.cpp ${CYNARA_SRC}/service/storage/InMemoryStorageBackend.cpp ${CYNARA_SRC}/service/storage/BucketDeserializer.cpp ${CYNARA_SRC}/service/storage/StorageDeserializer.cpp diff --git a/test/common/types/policybucket.cpp b/test/common/types/policybucket.cpp index 9e47c21..52916e8 100644 --- a/test/common/types/policybucket.cpp +++ b/test/common/types/policybucket.cpp @@ -20,18 +20,19 @@ * @brief Tests for Cynara::PolicyBucket */ -#include +#include +#include +#include + #include +#include #include "types/PolicyBucket.h" -#include "types/PolicyKey.h" #include "types/PolicyCollection.h" +#include "types/PolicyKey.h" #include "../../helpers.h" -#include -#include - using namespace Cynara; class PolicyBucketFixture : public ::testing::Test { @@ -56,45 +57,23 @@ protected: Policy::simpleWithKey(PolicyKey("*", "*", "p1"), PredefinedPolicyType::ALLOW), Policy::simpleWithKey(PolicyKey("*", "*", "*"), PredefinedPolicyType::ALLOW) }; - - PolicyCollection filterHelper(const PolicyCollection &original, - std::function pred) { - PolicyCollection filtered(original.size()); - auto endIt = std::copy_if(std::begin(original), std::end(original), - std::begin(filtered), pred); - filtered.resize(std::distance(std::begin(filtered), endIt)); - return filtered; - } - - PolicyCollection filterHelper(const PolicyCollection &original, std::vector idx) { - PolicyCollection filtered; - filtered.reserve(idx.size()); - for (const auto &i : idx) { - filtered.push_back(original.at(i)); - } - return filtered; - } }; TEST_F(PolicyBucketFixture, filtered) { - using ::testing::UnorderedElementsAreArray; using ::testing::UnorderedElementsAre; - using ::testing::IsEmpty; PolicyBucket bucket(pkPolicies); bucket.setDefaultPolicy(PredefinedPolicyType::DENY); auto filtered = bucket.filtered(pk1); // Elements match - ASSERT_THAT(filtered.policyCollection(), UnorderedElementsAre(pkPolicies.at(0))); + ASSERT_THAT(filtered, UnorderedElementsAre(pkPolicies.at(0))); // default policy matches ASSERT_EQ(PredefinedPolicyType::DENY, filtered.defaultPolicy()); } TEST_F(PolicyBucketFixture, filtered_other) { - using ::testing::UnorderedElementsAreArray; - using ::testing::UnorderedElementsAre; using ::testing::IsEmpty; PolicyBucket bucket(pkPolicies); @@ -102,7 +81,7 @@ TEST_F(PolicyBucketFixture, filtered_other) { auto filtered = bucket.filtered(otherPk); // No policies should be found - ASSERT_THAT(filtered.policyCollection(), IsEmpty()); + ASSERT_THAT(filtered, IsEmpty()); // default policy should be preserved ASSERT_EQ(PredefinedPolicyType::DENY, filtered.defaultPolicy()); @@ -112,45 +91,45 @@ TEST_F(PolicyBucketFixture, filtered_wildcard_1) { using ::testing::UnorderedElementsAreArray; // Leave policies with given client, given user and any privilege - auto policiesToStay = filterHelper(wildcardPolicies, { 0, 1, 3 }); + auto policiesToStay = Helpers::pickFromCollection(wildcardPolicies, { 0, 1, 3 }); PolicyBucket bucket(wildcardPolicies); auto filtered = bucket.filtered(PolicyKey("c1", "u1", "p2")); - ASSERT_THAT(filtered.policyCollection(), UnorderedElementsAreArray(policiesToStay)); + ASSERT_THAT(filtered, UnorderedElementsAreArray(policiesToStay)); } TEST_F(PolicyBucketFixture, filtered_wildcard_2) { using ::testing::UnorderedElementsAreArray; // Leave policies with given client, given user and any privilege - auto policiesToStay = filterHelper(wildcardPolicies, std::vector{ 2, 3 }); + auto policiesToStay = Helpers::pickFromCollection(wildcardPolicies, { 2, 3 }); PolicyBucket bucket(wildcardPolicies); auto filtered = bucket.filtered(PolicyKey("cccc", "u1", "p1")); - ASSERT_THAT(filtered.policyCollection(), UnorderedElementsAreArray(policiesToStay)); + ASSERT_THAT(filtered, UnorderedElementsAreArray(policiesToStay)); } TEST_F(PolicyBucketFixture, filtered_wildcard_3) { using ::testing::UnorderedElementsAreArray; // Leave policies with given client, given user and any privilege - auto policiesToStay = filterHelper(wildcardPolicies, std::vector{ 0, 3 }); + auto policiesToStay = Helpers::pickFromCollection(wildcardPolicies, { 0, 3 }); PolicyBucket bucket(wildcardPolicies); auto filtered = bucket.filtered(PolicyKey("c1", "u1", "pppp")); - ASSERT_THAT(filtered.policyCollection(), UnorderedElementsAreArray(policiesToStay)); + ASSERT_THAT(filtered, UnorderedElementsAreArray(policiesToStay)); } TEST_F(PolicyBucketFixture, filtered_wildcard_4) { using ::testing::UnorderedElementsAreArray; // Leave policies with given client, given user and any privilege - auto policiesToStay = filterHelper(wildcardPolicies, std::vector{ 3 }); + auto policiesToStay = Helpers::pickFromCollection(wildcardPolicies, { 3 }); PolicyBucket bucket(wildcardPolicies); auto filtered = bucket.filtered(PolicyKey("cccc", "uuuu", "pppp")); - ASSERT_THAT(filtered.policyCollection(), UnorderedElementsAreArray(policiesToStay)); + ASSERT_THAT(filtered, UnorderedElementsAreArray(policiesToStay)); } TEST_F(PolicyBucketFixture, filtered_wildcard_none) { @@ -158,5 +137,5 @@ TEST_F(PolicyBucketFixture, filtered_wildcard_none) { PolicyBucket bucket({ wildcardPolicies.begin(), wildcardPolicies.begin() + 3 }); auto filtered = bucket.filtered(PolicyKey("cccc", "uuuu", "pppp")); - ASSERT_THAT(filtered.policyCollection(), IsEmpty()); + ASSERT_THAT(filtered, IsEmpty()); } diff --git a/test/helpers.h b/test/helpers.h index 1f678d5..69940ff 100644 --- a/test/helpers.h +++ b/test/helpers.h @@ -23,16 +23,30 @@ #ifndef HELPERS_H #define HELPERS_H +#include + #include "types/PolicyKey.h" #include "types/PolicyBucketId.h" namespace Cynara { + namespace Helpers { PolicyKey generatePolicyKey(const PolicyKeyFeature::ValueType &sufix = ""); PolicyBucketId generateBucketId(const PolicyBucketId &sufix = ""); +template +Collection pickFromCollection(const Collection &original, const std::vector &idx) { + Collection filtered; + filtered.reserve(idx.size()); + for (const auto &i : idx) { + filtered.push_back(original.at(i)); + } + return filtered; +} + } // namespace Helpers + } // namespace Cynara #endif // HELPERS_H diff --git a/test/storage/inmemorystoragebackend/buckets.cpp b/test/storage/inmemorystoragebackend/buckets.cpp index 9028d1a..860660f 100644 --- a/test/storage/inmemorystoragebackend/buckets.cpp +++ b/test/storage/inmemorystoragebackend/buckets.cpp @@ -47,7 +47,7 @@ TEST_F(InMemeoryStorageBackendFixture, createBucket) { ASSERT_EQ(1, m_buckets.size()); ASSERT_NE(m_buckets.end(), m_buckets.find(bucketId)); ASSERT_EQ(defaultPolicy, m_buckets.at(bucketId).defaultPolicy()); - ASSERT_THAT(m_buckets.at(bucketId).policyCollection(), IsEmpty()); + ASSERT_THAT(m_buckets.at(bucketId), IsEmpty()); } TEST_F(InMemeoryStorageBackendFixture, updateBucket) { diff --git a/test/storage/inmemorystoragebackend/inmemeorystoragebackendfixture.h b/test/storage/inmemorystoragebackend/inmemeorystoragebackendfixture.h index b194006..2c6e593 100644 --- a/test/storage/inmemorystoragebackend/inmemeorystoragebackendfixture.h +++ b/test/storage/inmemorystoragebackend/inmemeorystoragebackendfixture.h @@ -39,9 +39,17 @@ protected: return m_buckets.insert({ bucketId, bucket }).first->second; } + Cynara::Buckets::mapped_type & + createBucket(const Cynara::PolicyBucketId &bucketId, const Cynara::PolicyCollection &policies) { + auto bucket = Cynara::PolicyBucket(policies); + return m_buckets.insert({ bucketId, bucket }).first->second; + } + void addToBucket(Cynara::PolicyBucketId bucketId, const Cynara::PolicyCollection &policies) { - std::copy(policies.begin(), policies.end(), - std::back_inserter(m_buckets[bucketId].policyCollection())); + // TODO: Consider altering PolicyMap directly + for (const auto &policy : policies) { + m_buckets[bucketId].insertPolicy(policy); + } } virtual ~InMemeoryStorageBackendFixture() {} diff --git a/test/storage/inmemorystoragebackend/inmemorystoragebackend.cpp b/test/storage/inmemorystoragebackend/inmemorystoragebackend.cpp index 34a3c42..6e84475 100644 --- a/test/storage/inmemorystoragebackend/inmemorystoragebackend.cpp +++ b/test/storage/inmemorystoragebackend/inmemorystoragebackend.cpp @@ -20,17 +20,17 @@ * @brief Tests of InMemeoryStorageBackend */ -#include #include +#include -#include "types/PolicyType.h" -#include "types/PolicyKey.h" -#include "types/PolicyResult.h" -#include "types/PolicyCollection.h" #include "exceptions/DefaultBucketDeletionException.h" #include "exceptions/BucketNotExistsException.h" -#include "storage/StorageBackend.h" #include "storage/InMemoryStorageBackend.h" +#include "storage/StorageBackend.h" +#include "types/PolicyCollection.h" +#include "types/PolicyKey.h" +#include "types/PolicyResult.h" +#include "types/PolicyType.h" #include "../../helpers.h" #include "fakeinmemorystoragebackend.h" @@ -54,6 +54,7 @@ TEST_F(InMemeoryStorageBackendFixture, defaultPolicyIsDeny) { ASSERT_EQ(PredefinedPolicyType::DENY, defaultPolicy.policyType()); } +// TODO: Refactorize this test to be shorter and clearer TEST_F(InMemeoryStorageBackendFixture, deleteLinking) { using ::testing::ReturnRef; using ::testing::IsEmpty; @@ -102,17 +103,17 @@ TEST_F(InMemeoryStorageBackendFixture, deleteLinking) { // Should delete 1st and 2nd policy from 1st bucket backend.deleteLinking(testBucket2); - ASSERT_THAT(m_buckets.at(testBucket1).policyCollection(), + ASSERT_THAT(m_buckets.at(testBucket1), UnorderedElementsAre(policiesToStay.at(0))); - ASSERT_THAT(m_buckets.at(testBucket2).policyCollection(), + ASSERT_THAT(m_buckets.at(testBucket2), UnorderedElementsAre(policiesToStay.at(1), policiesToStay.at(2))); - ASSERT_THAT(m_buckets.at(testBucket3).policyCollection(), IsEmpty()); + ASSERT_THAT(m_buckets.at(testBucket3), IsEmpty()); } TEST_F(InMemeoryStorageBackendFixture, insertPolicy) { using ::testing::ReturnRef; - using ::testing::IsEmpty; using ::testing::UnorderedElementsAre; + using PredefinedPolicyType::ALLOW; FakeInMemoryStorageBackend backend; EXPECT_CALL(backend, buckets()) @@ -121,16 +122,14 @@ TEST_F(InMemeoryStorageBackendFixture, insertPolicy) { PolicyBucketId bucketId = "test-bucket"; createBucket(bucketId); - auto policyToAdd = Policy::simpleWithKey(Helpers::generatePolicyKey(), PredefinedPolicyType::ALLOW); + auto policyToAdd = Policy::simpleWithKey(Helpers::generatePolicyKey(), ALLOW); backend.insertPolicy(bucketId, policyToAdd); - ASSERT_THAT(m_buckets.at(bucketId).policyCollection(), UnorderedElementsAre(policyToAdd)); + ASSERT_THAT(m_buckets.at(bucketId), UnorderedElementsAre(policyToAdd)); } TEST_F(InMemeoryStorageBackendFixture, insertPolicyToNonexistentBucket) { using ::testing::ReturnRef; - using ::testing::IsEmpty; - using ::testing::UnorderedElementsAre; FakeInMemoryStorageBackend backend; EXPECT_CALL(backend, buckets()) @@ -143,7 +142,9 @@ TEST_F(InMemeoryStorageBackendFixture, deletePolicy) { using ::testing::ReturnRef; using ::testing::IsEmpty; using ::testing::UnorderedElementsAre; + using ::testing::UnorderedElementsAreArray; using ::testing::ContainerEq; + using PredefinedPolicyType::ALLOW; FakeInMemoryStorageBackend backend; EXPECT_CALL(backend, buckets()) @@ -152,12 +153,12 @@ TEST_F(InMemeoryStorageBackendFixture, deletePolicy) { PolicyBucketId bucketId = "test-bucket"; createBucket(bucketId); - auto policyToDelete = Policy::simpleWithKey(Helpers::generatePolicyKey(), PredefinedPolicyType::ALLOW); + auto policyToDelete = Policy::simpleWithKey(Helpers::generatePolicyKey(), ALLOW); PolicyCollection otherPolicies = { - Policy::simpleWithKey(Helpers::generatePolicyKey("other-policy-1"), PredefinedPolicyType::ALLOW), - Policy::simpleWithKey(Helpers::generatePolicyKey("other-policy-2"), PredefinedPolicyType::ALLOW), - Policy::simpleWithKey(Helpers::generatePolicyKey("other-policy-3"), PredefinedPolicyType::ALLOW), + Policy::simpleWithKey(Helpers::generatePolicyKey("other-policy-1"), ALLOW), + Policy::simpleWithKey(Helpers::generatePolicyKey("other-policy-2"), ALLOW), + Policy::simpleWithKey(Helpers::generatePolicyKey("other-policy-3"), ALLOW), }; addToBucket(bucketId, { @@ -170,7 +171,7 @@ TEST_F(InMemeoryStorageBackendFixture, deletePolicy) { backend.deletePolicy(bucketId, policyToDelete->key()); // Check if only policyToDelete has been deleted - EXPECT_THAT(m_buckets.at(bucketId).policyCollection(), ContainerEq(otherPolicies)); + EXPECT_THAT(m_buckets.at(bucketId), UnorderedElementsAreArray(otherPolicies)); } TEST_F(InMemeoryStorageBackendFixture, deletePolicyFromNonexistentBucket) { @@ -182,5 +183,6 @@ TEST_F(InMemeoryStorageBackendFixture, deletePolicyFromNonexistentBucket) { EXPECT_CALL(backend, buckets()) .WillOnce(ReturnRef(m_buckets)); - EXPECT_THROW(backend.deletePolicy("non-existent", Helpers::generatePolicyKey()), BucketNotExistsException); + EXPECT_THROW(backend.deletePolicy("non-existent", Helpers::generatePolicyKey()), + BucketNotExistsException); } diff --git a/test/storage/inmemorystoragebackend/search.cpp b/test/storage/inmemorystoragebackend/search.cpp index 65c9057..c9f9dfd 100644 --- a/test/storage/inmemorystoragebackend/search.cpp +++ b/test/storage/inmemorystoragebackend/search.cpp @@ -20,52 +20,46 @@ * @brief Tests of search in InMemeoryStorageBackend */ +#include + #include "gmock/gmock.h" #include "gtest/gtest.h" -#include "inmemeorystoragebackendfixture.h" -#include "fakeinmemorystoragebackend.h" - #include "types/PolicyBucket.h" #include "../../helpers.h" - -#include +#include "fakeinmemorystoragebackend.h" +#include "inmemeorystoragebackendfixture.h" using namespace Cynara; TEST_F(InMemeoryStorageBackendFixture, searchDefault) { using ::testing::ReturnRef; - using ::testing::UnorderedElementsAre; + using ::testing::UnorderedElementsAreArray; using ::testing::IsEmpty; auto pk1 = Helpers::generatePolicyKey("1"); auto pk2 = Helpers::generatePolicyKey("2"); + auto pk3 = PolicyKey(PolicyKeyFeature::createWildcard(), pk1.user(), pk1.privilege()); auto otherPk = Helpers::generatePolicyKey("-"); - const auto &defaultBucket = createBucket(defaultPolicyBucketId); - - addToBucket(defaultPolicyBucketId, { + PolicyCollection policies = { Policy::simpleWithKey(pk1, PredefinedPolicyType::ALLOW), Policy::simpleWithKey(pk2, PredefinedPolicyType::DENY), - Policy::simpleWithKey(pk1, PredefinedPolicyType::DENY), - }); + Policy::simpleWithKey(pk3, PredefinedPolicyType::DENY), + }; + + createBucket(defaultPolicyBucketId, policies); // Just override buckets() accessor FakeInMemoryStorageBackend backend; EXPECT_CALL(backend, buckets()) .WillRepeatedly(ReturnRef(m_buckets)); - auto searchDefaultBucket = [&backend](const PolicyKey &key) -> PolicyCollection { - return backend.searchDefaultBucket(key).policyCollection(); - }; - - auto defaultPolicyAt = [&defaultBucket] (PolicyCollection::size_type idx) { - return defaultBucket.policyCollection().at(idx); - }; + auto policiesToStay1 = Helpers::pickFromCollection(policies, { 0, 2 }); + auto policiesToStay2 = Helpers::pickFromCollection(policies, { 1 }); - ASSERT_THAT(searchDefaultBucket(pk1), - UnorderedElementsAre(defaultPolicyAt(0),defaultPolicyAt(2))); - ASSERT_THAT(searchDefaultBucket(pk2), UnorderedElementsAre(defaultPolicyAt(1))); - ASSERT_THAT(searchDefaultBucket(otherPk), IsEmpty()); + ASSERT_THAT(backend.searchDefaultBucket(pk1), UnorderedElementsAreArray(policiesToStay1)); + ASSERT_THAT(backend.searchDefaultBucket(pk2), UnorderedElementsAreArray(policiesToStay2)); + ASSERT_THAT(backend.searchDefaultBucket(otherPk), IsEmpty()); } diff --git a/test/storage/serializer/deserialize.cpp b/test/storage/serializer/deserialize.cpp index 140242b..931a5ab 100644 --- a/test/storage/serializer/deserialize.cpp +++ b/test/storage/serializer/deserialize.cpp @@ -147,7 +147,7 @@ TEST_F(StorageDeserializerFixture, load_buckets_plus_policies) { )); // Check policy was inserted into bucket - ASSERT_THAT(buckets.at("").policyCollection(), UnorderedElementsAre( + ASSERT_THAT(buckets.at(""), UnorderedElementsAre( Pointee(Policy(PolicyKey("c", "u", "p"), PolicyResult(PredefinedPolicyType::DENY, "meta"))) )); } diff --git a/test/storage/serializer/dump.cpp b/test/storage/serializer/dump.cpp index 2a0bfd0..90144e4 100644 --- a/test/storage/serializer/dump.cpp +++ b/test/storage/serializer/dump.cpp @@ -58,54 +58,59 @@ TEST(serializer_dump, dump_empty_bucket) { } TEST(serializer_dump, dump_bucket) { + using ::testing::UnorderedElementsAreArray; + using PredefinedPolicyType::ALLOW; + using PredefinedPolicyType::DENY; + PolicyKey pk1 = Helpers::generatePolicyKey("1"); PolicyKey pk2 = Helpers::generatePolicyKey("2"); - PolicyBucket bucket = {{ Policy::simpleWithKey(pk1, PredefinedPolicyType::ALLOW), - Policy::simpleWithKey(pk2, PredefinedPolicyType::DENY) }}; + PolicyBucket bucket = {{ Policy::simpleWithKey(pk1, ALLOW), + Policy::simpleWithKey(pk2, DENY) }}; - auto outputStream = std::make_shared(); - StorageSerializer serializer(outputStream); + auto outStream = std::make_shared(); + StorageSerializer serializer(outStream); serializer.dump(bucket); - // TODO: Cynara::PolicyCollection is a vector, but in future version this may change - // and so, we should not expect the exact order of records in serialized stream - // See: StorageSerializerFixture::dump_buckets in serialize.cpp - std::stringstream expected; - expected - << expectedPolicyKey(pk1) << ";" << expectedPolicyType(PredefinedPolicyType::ALLOW)<< ";" - << std::endl - << expectedPolicyKey(pk2) << ";" << expectedPolicyType(PredefinedPolicyType::DENY) << ";" - << std::endl; - - ASSERT_EQ(expected.str(), outputStream->str()); + // Split stream into records + auto actualRecords = std::vector(std::istream_iterator(*outStream), + std::istream_iterator()); + + std::vector expectedRecords = { + expectedPolicyKey(pk1) + ";" + expectedPolicyType(ALLOW) + ";", + expectedPolicyKey(pk2) + ";" + expectedPolicyType(DENY) + ";" + }; + + ASSERT_THAT(actualRecords, UnorderedElementsAreArray(expectedRecords)); } TEST(serializer_dump, dump_bucket_bucket) { + using ::testing::UnorderedElementsAreArray; + using PredefinedPolicyType::BUCKET; + using PredefinedPolicyType::DENY; + 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::simpleWithKey(pk2, DENY), Policy::bucketWithKey(pk3, bucketId) }}; - auto outputStream = std::make_shared(); - StorageSerializer serializer(outputStream); + auto outStream = std::make_shared(); + StorageSerializer serializer(outStream); serializer.dump(bucket); - // TODO: Cynara::PolicyCollection is a vector, but in future version this may change - // and so, we should not expect the exact order of records in serialized stream - // See: StorageSerializerFixture::dump_buckets in serialize.cpp - std::stringstream expected; - expected - << 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()); + // Split stream into records + auto actualRecords = std::vector(std::istream_iterator(*outStream), + std::istream_iterator()); + + std::vector expectedRecords = { + expectedPolicyKey(pk1) + ";" + expectedPolicyType(BUCKET) + ";" + bucketId, + expectedPolicyKey(pk2) + ";" + expectedPolicyType(DENY) + ";", + expectedPolicyKey(pk3) + ";" + expectedPolicyType(BUCKET) + ";" + bucketId + }; + + ASSERT_THAT(actualRecords, UnorderedElementsAreArray(expectedRecords)); } diff --git a/test/storage/serializer/dump_load.cpp b/test/storage/serializer/dump_load.cpp index 02d90fd..36468ff 100644 --- a/test/storage/serializer/dump_load.cpp +++ b/test/storage/serializer/dump_load.cpp @@ -59,7 +59,8 @@ TEST(dump_load, bucket) { BucketDeserializer deserializer(ioStream); const auto loadedPolicies = deserializer.loadPolicies(); - const auto &expectedPolicies = bucket.policyCollection(); + PolicyCollection expectedPolicies; + std::copy(bucket.begin(), bucket.end(), std::back_inserter(expectedPolicies)); ASSERT_THAT(loadedPolicies, UnorderedElementsAre( PolicyAtPtrEq(expectedPolicies.at(0)), diff --git a/test/storage/storage/check.cpp b/test/storage/storage/check.cpp index 492e7fb..e71e40f 100644 --- a/test/storage/storage/check.cpp +++ b/test/storage/storage/check.cpp @@ -73,11 +73,11 @@ TEST(storage, checkSimple) { ASSERT_EQ(PredefinedPolicyType::DENY, storage.checkPolicy(pk).policyType()); // Add ALLOW to default bucket -- return ALLOW - bucket.policyCollection().push_back(Policy::simpleWithKey(pk, PredefinedPolicyType::ALLOW)); + bucket.insertPolicy(Policy::simpleWithKey(pk, PredefinedPolicyType::ALLOW)); ASSERT_EQ(PredefinedPolicyType::ALLOW, storage.checkPolicy(pk).policyType()); // Add DENY to default bucket -- return DENY - bucket.policyCollection().push_back(Policy::simpleWithKey(pk, PredefinedPolicyType::DENY)); + bucket.insertPolicy(Policy::simpleWithKey(pk, PredefinedPolicyType::DENY)); ASSERT_EQ(PredefinedPolicyType::DENY, storage.checkPolicy(pk).policyType()); } @@ -111,11 +111,11 @@ TEST(storage, checkBucket) { ASSERT_EQ(PredefinedPolicyType::DENY, storage.checkPolicy(pk).policyType()); // Add ALLOW to bucket, so return ALLOW - additionalBucket.policyCollection().push_back(Policy::simpleWithKey(pk, PredefinedPolicyType::ALLOW)); + additionalBucket.insertPolicy(Policy::simpleWithKey(pk, PredefinedPolicyType::ALLOW)); ASSERT_EQ(PredefinedPolicyType::ALLOW, storage.checkPolicy(pk).policyType()); // Add DENY to default bucket -- return DENY, even though ALLOW in other bucket - defaultBucket.policyCollection().push_back(Policy::simpleWithKey(pk, PredefinedPolicyType::DENY)); + defaultBucket.insertPolicy(Policy::simpleWithKey(pk, PredefinedPolicyType::DENY)); ASSERT_EQ(PredefinedPolicyType::DENY, storage.checkPolicy(pk).policyType()); } diff --git a/test/storage/storage/policies.cpp b/test/storage/storage/policies.cpp index 4125a6d..fbe40fa 100644 --- a/test/storage/storage/policies.cpp +++ b/test/storage/storage/policies.cpp @@ -107,51 +107,6 @@ TEST(storage, insertPolicies) { 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, Pointee(policy))); - } - } - - storage.insertPolicies(policiesToInsert); -} - -TEST(storage, updatePolicies) { - using ::testing::Pointee; - using ::testing::Return; - FakeStorageBackend backend; - Storage storage(backend); - - PolicyBucketId testBucket1 = "test-bucket-1"; - PolicyBucketId testBucket2 = "test-bucket-2"; - - 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, Pointee(policy))); } } -- 2.7.4