From f5c2d36446b40ed9dca8a3ef4c30161ca3545b5c Mon Sep 17 00:00:00 2001 From: Lukasz Wojciechowski Date: Mon, 21 Jul 2014 14:26:23 +0200 Subject: [PATCH 01/16] Fix badly inceremented pointers Change-Id: I40790d15f973a6e7bfacefb39b307b2a7bd77510 --- src/admin/api/admin-api.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/admin/api/admin-api.cpp b/src/admin/api/admin-api.cpp index 5c2e2e6..70c5283 100644 --- a/src/admin/api/admin-api.cpp +++ b/src/admin/api/admin-api.cpp @@ -93,27 +93,28 @@ int cynara_admin_set_policies(struct cynara_admin *p_cynara_admin, }); try { - for (auto i = policies[0]; i; i++) { - if(!i->bucket || !i->client || !i->user || !i->privilege) + for (auto i = policies; *i; i++) { + if(!(*i)->bucket || !(*i)->client || !(*i)->user || !(*i)->privilege) return CYNARA_ADMIN_API_INVALID_PARAM; - switch (i->result) { + switch ((*i)->result) { case CYNARA_ADMIN_DELETE: - remove[i->bucket].push_back(key(i)); + remove[(*i)->bucket].push_back(key(*i)); break; case CYNARA_ADMIN_DENY: - insertOrUpdate[i->bucket].push_back(Cynara::Policy(key(i), + insertOrUpdate[(*i)->bucket].push_back(Cynara::Policy(key(*i), Cynara::PredefinedPolicyType::DENY)); break; case CYNARA_ADMIN_ALLOW: - insertOrUpdate[i->bucket].push_back(Cynara::Policy(key(i), + insertOrUpdate[(*i)->bucket].push_back(Cynara::Policy(key(*i), Cynara::PredefinedPolicyType::ALLOW)); break; case CYNARA_ADMIN_BUCKET: - insertOrUpdate[i->bucket].push_back(Cynara::Policy(key(i), + insertOrUpdate[(*i)->bucket].push_back(Cynara::Policy(key(*i), Cynara::PolicyResult( Cynara::PredefinedPolicyType::BUCKET, - i->result_extra ? i->result_extra : ""))); + (*i)->result_extra ? (*i)->result_extra + : ""))); break; default: return CYNARA_ADMIN_API_INVALID_PARAM; -- 2.7.4 From fbb418d58e87c59d9948d8058c3447365b4f27cb Mon Sep 17 00:00:00 2001 From: Lukasz Wojciechowski Date: Mon, 21 Jul 2014 16:38:26 +0200 Subject: [PATCH 02/16] Fix problem with passing references to local objects References are changed to shared pointers. Change-Id: I8bdd52e1ba04ce6625cf42810c68d19cdf006ecd --- src/service/storage/BucketDeserializer.cpp | 4 ++-- src/service/storage/BucketDeserializer.h | 9 ++++++--- src/service/storage/InMemoryStorageBackend.cpp | 22 ++++++++++++---------- src/service/storage/InMemoryStorageBackend.h | 4 ++-- src/service/storage/StorageDeserializer.cpp | 10 ++++++---- src/service/storage/StorageDeserializer.h | 7 ++++--- src/service/storage/StorageSerializer.cpp | 16 +++++++++------- src/service/storage/StorageSerializer.h | 15 +++++---------- 8 files changed, 46 insertions(+), 41 deletions(-) diff --git a/src/service/storage/BucketDeserializer.cpp b/src/service/storage/BucketDeserializer.cpp index 1bfac50..7ba7d4b 100644 --- a/src/service/storage/BucketDeserializer.cpp +++ b/src/service/storage/BucketDeserializer.cpp @@ -41,9 +41,9 @@ PolicyCollection BucketDeserializer::loadPolicies(void) { PolicyCollection policies; // TODO: Get someone smart to do error checking on stream - for (std::size_t lineNum = 1; !m_inStream.eof(); ++lineNum) { + for (std::size_t lineNum = 1; !m_inStream->eof(); ++lineNum) { std::string line; - std::getline(m_inStream, line, StorageSerializer::recordSeparator()); + std::getline(*m_inStream, line, StorageSerializer::recordSeparator()); if (line.empty()) break; diff --git a/src/service/storage/BucketDeserializer.h b/src/service/storage/BucketDeserializer.h index 393991e..ad54100 100644 --- a/src/service/storage/BucketDeserializer.h +++ b/src/service/storage/BucketDeserializer.h @@ -22,7 +22,8 @@ #ifndef SRC_SERVICE_STORAGE_BUCKETDESERIALIZER_H_ #define SRC_SERVICE_STORAGE_BUCKETDESERIALIZER_H_ -#include +#include +#include #include #include @@ -33,14 +34,16 @@ namespace Cynara { class BucketDeserializer { public: - BucketDeserializer(std::istream &inStream) : m_inStream(inStream) {} + BucketDeserializer(std::shared_ptr inStream) : m_inStream(inStream) { + } + PolicyCollection loadPolicies(void); protected: static PolicyKey parseKey(const std::string &line, std::size_t &beginToken); private: - std::istream &m_inStream; + std::shared_ptr m_inStream; }; } /* namespace Cynara */ diff --git a/src/service/storage/InMemoryStorageBackend.cpp b/src/service/storage/InMemoryStorageBackend.cpp index 5d18219..d04a6ff 100644 --- a/src/service/storage/InMemoryStorageBackend.cpp +++ b/src/service/storage/InMemoryStorageBackend.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -50,7 +51,7 @@ void InMemoryStorageBackend::load(void) { std::string indexFilename = m_dbPath + m_indexFileName; try { - std::ifstream indexStream; + std::shared_ptr indexStream = std::make_shared(); openFileStream(indexStream, indexFilename); StorageDeserializer storageDeserializer(indexStream, @@ -81,7 +82,7 @@ void InMemoryStorageBackend::save(void) { } } - std::ofstream indexStream; + std::shared_ptr indexStream = std::make_shared(); openDumpFileStream(indexStream, m_dbPath + m_indexFileName); StorageSerializer storageSerializer(indexStream); @@ -176,20 +177,21 @@ void InMemoryStorageBackend::deleteLinking(const PolicyBucketId &bucketId) { } } -void InMemoryStorageBackend::openFileStream(std::ifstream &stream, const std::string &filename) { +void InMemoryStorageBackend::openFileStream(std::shared_ptr stream, + const std::string &filename) { // TODO: Consider adding exceptions to streams and handling them: // stream.exceptions(std::ifstream::failbit | std::ifstream::badbit); - stream.open(filename); + stream->open(filename); - if (!stream.is_open()) + if (!stream->is_open()) throw FileNotFoundException(filename); } -void InMemoryStorageBackend::openDumpFileStream(std::ofstream &stream, +void InMemoryStorageBackend::openDumpFileStream(std::shared_ptr stream, const std::string &filename) { - stream.open(filename, std::ofstream::out | std::ofstream::trunc); + stream->open(filename, std::ofstream::out | std::ofstream::trunc); - if (!stream.is_open()) { + if (!stream->is_open()) { throw CannotCreateFileException(filename); return; } @@ -198,7 +200,7 @@ void InMemoryStorageBackend::openDumpFileStream(std::ofstream &stream, std::shared_ptr InMemoryStorageBackend::bucketStreamOpener( const PolicyBucketId &bucketId) { std::string bucketFilename = m_dbPath + "_" + bucketId; - std::ifstream bucketStream; + std::shared_ptr bucketStream = std::make_shared(); try { openFileStream(bucketStream, bucketFilename); return std::make_shared(bucketStream); @@ -210,7 +212,7 @@ std::shared_ptr InMemoryStorageBackend::bucketStreamOpener( std::shared_ptr InMemoryStorageBackend::bucketDumpStreamOpener( const PolicyBucketId &bucketId) { std::string bucketFilename = m_dbPath + "_" + bucketId; - std::ofstream bucketStream; + std::shared_ptr bucketStream = std::make_shared(); openDumpFileStream(bucketStream, bucketFilename); return std::make_shared(bucketStream); diff --git a/src/service/storage/InMemoryStorageBackend.h b/src/service/storage/InMemoryStorageBackend.h index 5127bc6..be2ba54 100644 --- a/src/service/storage/InMemoryStorageBackend.h +++ b/src/service/storage/InMemoryStorageBackend.h @@ -60,10 +60,10 @@ public: virtual void deleteLinking(const PolicyBucketId &bucketId); protected: - void openFileStream(std::ifstream &stream, const std::string &filename); + void openFileStream(std::shared_ptr stream, const std::string &filename); std::shared_ptr bucketStreamOpener(const PolicyBucketId &bucketId); - void openDumpFileStream(std::ofstream &stream, const std::string &filename); + void openDumpFileStream(std::shared_ptr stream, const std::string &filename); std::shared_ptr bucketDumpStreamOpener(const PolicyBucketId &bucketId); private: diff --git a/src/service/storage/StorageDeserializer.cpp b/src/service/storage/StorageDeserializer.cpp index 66c8ff0..9b34e5f 100644 --- a/src/service/storage/StorageDeserializer.cpp +++ b/src/service/storage/StorageDeserializer.cpp @@ -21,6 +21,7 @@ */ #include +#include #include #include @@ -35,16 +36,17 @@ namespace Cynara { -StorageDeserializer::StorageDeserializer(std::istream &inStream, +StorageDeserializer::StorageDeserializer(std::shared_ptr inStream, BucketStreamOpener bucketStreamOpener) - : m_inStream(inStream), m_bucketStreamOpener(bucketStreamOpener) {} + : m_inStream(inStream), m_bucketStreamOpener(bucketStreamOpener) { +} void StorageDeserializer::initBuckets(Buckets &buckets) { buckets.clear(); - for (std::size_t lineNum = 1; !m_inStream.eof(); ++lineNum) { + for (std::size_t lineNum = 1; !m_inStream->eof(); ++lineNum) { std::string line; - std::getline(m_inStream, line, StorageSerializer::recordSeparator()); + std::getline(*m_inStream, line, StorageSerializer::recordSeparator()); if (line.empty()) break; diff --git a/src/service/storage/StorageDeserializer.h b/src/service/storage/StorageDeserializer.h index c3680c4..d186ff7 100644 --- a/src/service/storage/StorageDeserializer.h +++ b/src/service/storage/StorageDeserializer.h @@ -23,7 +23,7 @@ #define SRC_SERVICE_STORAGE_STORAGEDESERIALIZER_H_ #include -#include +#include #include #include @@ -40,7 +40,8 @@ class StorageDeserializer { public: typedef std::function(const std::string &)> BucketStreamOpener; - StorageDeserializer(std::istream &inStream, BucketStreamOpener m_bucketStreamOpener); + StorageDeserializer(std::shared_ptr inStream, + BucketStreamOpener m_bucketStreamOpener); void initBuckets(Buckets &buckets); void loadBuckets(Buckets &buckets); @@ -50,7 +51,7 @@ public: std::size_t &beginToken); private: - std::istream &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 f46d48e..2572a3b 100644 --- a/src/service/storage/StorageSerializer.cpp +++ b/src/service/storage/StorageSerializer.cpp @@ -20,8 +20,9 @@ * @brief Implementation of Cynara::StorageSerializer methods */ +#include +#include #include -#include #include #include @@ -37,7 +38,8 @@ namespace Cynara { char StorageSerializer::m_fieldSeparator = ';'; char StorageSerializer::m_recordSeparator = '\n'; -StorageSerializer::StorageSerializer(std::ostream &os) : m_outStream(os) {} +StorageSerializer::StorageSerializer(std::shared_ptr os) : m_outStream(os) { +} void StorageSerializer::dump(const Buckets &buckets, BucketStreamOpener streamOpener) { @@ -72,17 +74,17 @@ void StorageSerializer::dump(const PolicyBucket& bucket) { } void StorageSerializer::dump(const PolicyKey &key) { - outStream() << key.toString(); + *m_outStream << key.toString(); } void StorageSerializer::dump(const PolicyType &policyType) { - auto oldFormat = m_outStream.flags(); - outStream() << "0x" << std::uppercase << std::hex << policyType; - m_outStream.flags(oldFormat); + auto oldFormat = m_outStream->flags(); + *m_outStream << "0x" << std::uppercase << std::hex << policyType; + m_outStream->flags(oldFormat); } void StorageSerializer::dump(const PolicyResult::PolicyMetadata &metadata) { - outStream() << metadata; + *m_outStream << metadata; } void StorageSerializer::dump(const PolicyCollection::value_type &policy) { diff --git a/src/service/storage/StorageSerializer.h b/src/service/storage/StorageSerializer.h index 80966e9..f4397ba 100644 --- a/src/service/storage/StorageSerializer.h +++ b/src/service/storage/StorageSerializer.h @@ -24,8 +24,8 @@ #define SRC_SERVICE_STORAGE_STORAGESERIALIZER_H_ #include +#include #include -#include #include #include @@ -44,7 +44,7 @@ public: typedef std::function(const PolicyBucketId &)> BucketStreamOpener; - StorageSerializer(std::ostream &os); + StorageSerializer(std::shared_ptr os); virtual ~StorageSerializer() = default; virtual void dump(const Buckets &buckets, @@ -56,13 +56,13 @@ protected: inline void dumpFields(const Arg1 &arg1, const Args&... args) { dump(arg1); if (sizeof...(Args) > 0) { - outStream() << fieldSeparator(); + *m_outStream << fieldSeparator(); } dumpFields(args...); } inline void dumpFields(void) { - outStream() << recordSeparator(); + *m_outStream << recordSeparator(); } void dump(const PolicyKey &key); @@ -70,13 +70,8 @@ protected: void dump(const PolicyResult::PolicyMetadata &metadata); void dump(const PolicyCollection::value_type &policy); -protected: - std::ostream &outStream(void) { - return m_outStream; - } - private: - std::ostream &m_outStream; + std::shared_ptr m_outStream; static char m_fieldSeparator; static char m_recordSeparator; -- 2.7.4 From 9c59afbf788423139b359948874ced64ee314ccc Mon Sep 17 00:00:00 2001 From: Lukasz Wojciechowski Date: Mon, 21 Jul 2014 17:54:03 +0200 Subject: [PATCH 03/16] Fix bug - change reference to object in CheckResponse Change-Id: I1a7754669db74e747c069be70d5593b478e9b28d --- src/common/response/CheckResponse.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/response/CheckResponse.h b/src/common/response/CheckResponse.h index 7d3ebc4..1f49648 100644 --- a/src/common/response/CheckResponse.h +++ b/src/common/response/CheckResponse.h @@ -33,7 +33,7 @@ namespace Cynara { class CheckResponse : public Response { public: - const PolicyResult &m_resultRef; + const PolicyResult m_resultRef; CheckResponse(const PolicyResult &result, ProtocolFrameSequenceNumber sequenceNumber) : Response(sequenceNumber), m_resultRef(result) { -- 2.7.4 From 50837dad71bab7fc4582749bf5ce35bed4788149 Mon Sep 17 00:00:00 2001 From: Adam Malinowski Date: Mon, 21 Jul 2014 18:23:50 +0200 Subject: [PATCH 04/16] Fix cynara service and local state directory labeling with smack Change-Id: I08c3fdbc118d566a3e9fb4b4b340ba3c2323d71a --- packaging/cynara.manifest | 2 +- packaging/cynara.spec | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/packaging/cynara.manifest b/packaging/cynara.manifest index a76fdba..f31de52 100644 --- a/packaging/cynara.manifest +++ b/packaging/cynara.manifest @@ -1,5 +1,5 @@ - + diff --git a/packaging/cynara.spec b/packaging/cynara.spec index 11c630b..0b02d91 100644 --- a/packaging/cynara.spec +++ b/packaging/cynara.spec @@ -125,6 +125,8 @@ if [ $1 = 1 ]; then systemctl enable %{name}.service fi +chsmack -a System %{state_path} + systemctl restart %{name}.service /sbin/ldconfig -- 2.7.4 From 17d3725ca897beb78ddc9d2a9bac576e91c01240 Mon Sep 17 00:00:00 2001 From: Adam Malinowski Date: Mon, 21 Jul 2014 18:25:49 +0200 Subject: [PATCH 05/16] Release version 0.1.0 Change-Id: I5b4cc3fe87e4ea0af46e7a73a58e9b20b760f8d6 --- packaging/cynara.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/cynara.spec b/packaging/cynara.spec index 0b02d91..fce3588 100644 --- a/packaging/cynara.spec +++ b/packaging/cynara.spec @@ -1,6 +1,6 @@ Name: cynara Summary: Cynara service with client libraries -Version: 0.0.1 +Version: 0.1.0 Release: 1 Group: Security/Access Control License: Apache-2.0 -- 2.7.4 From 26e7c427a80ab231d15b683d21fec305ef412605 Mon Sep 17 00:00:00 2001 From: Adam Malinowski Date: Tue, 22 Jul 2014 10:52:36 +0200 Subject: [PATCH 06/16] Add 'default-ac-domains' package dependency in spec file This dependency is needed for using System domain for cynara service. Change-Id: Ib0c21e3375309f25c893289fd1271b84798fdb85 --- packaging/cynara.spec | 1 + 1 file changed, 1 insertion(+) diff --git a/packaging/cynara.spec b/packaging/cynara.spec index fce3588..b68e6dc 100644 --- a/packaging/cynara.spec +++ b/packaging/cynara.spec @@ -8,6 +8,7 @@ Source0: %{name}-%{version}.tar.gz Source1001: cynara.manifest Source1002: libcynara-client.manifest Source1003: libcynara-admin.manifest +Requires: default-ac-domains BuildRequires: cmake BuildRequires: zip BuildRequires: pkgconfig(libsystemd-daemon) -- 2.7.4 From c1f8603272ec4083fbe3f4fb267d1bd7654c7b36 Mon Sep 17 00:00:00 2001 From: Lukasz Wojciechowski Date: Tue, 22 Jul 2014 08:48:55 +0200 Subject: [PATCH 07/16] Fix parameter checking when setting bucket-pointing policy Field result_extra in cynara_admin_policy structure should contain another bucket's name in case of insertion or update of a policy pointing to another bucket. There was no check for that. In case a nullptr was passed, it was converted to an empty string "". Now cynara_admin_set_policies returns CYNARA_ADMIN_API_INVALID_PARAM when result is set to CYNARA_ADMIN_BUCKET and result_extra is nullptr. Change-Id: I24ff5ab662e88b7bc538368385e13d78f48f9e9a --- src/admin/api/admin-api.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/admin/api/admin-api.cpp b/src/admin/api/admin-api.cpp index 70c5283..4087e40 100644 --- a/src/admin/api/admin-api.cpp +++ b/src/admin/api/admin-api.cpp @@ -110,11 +110,12 @@ int cynara_admin_set_policies(struct cynara_admin *p_cynara_admin, Cynara::PredefinedPolicyType::ALLOW)); break; case CYNARA_ADMIN_BUCKET: + if (!(*i)->result_extra) + return CYNARA_ADMIN_API_INVALID_PARAM; insertOrUpdate[(*i)->bucket].push_back(Cynara::Policy(key(*i), Cynara::PolicyResult( Cynara::PredefinedPolicyType::BUCKET, - (*i)->result_extra ? (*i)->result_extra - : ""))); + (*i)->result_extra))); break; default: return CYNARA_ADMIN_API_INVALID_PARAM; -- 2.7.4 From ea25549b696469d62378a2334a06ffc12a934952 Mon Sep 17 00:00:00 2001 From: Adam Malinowski Date: Fri, 25 Jul 2014 11:01:14 +0200 Subject: [PATCH 08/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 09/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 10/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 11/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 12/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 13/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 14/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 15/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 16/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