From: Pawel Wieczorek
Date: Mon, 12 Jan 2015 14:01:54 +0000 (+0100)
Subject: Make StorageSerializer a template class
X-Git-Tag: accepted/tizen/common/20150310.163254~12
X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fsecurity%2Fcynara.git;a=commitdiff_plain;h=58d92c8f005be204b6d0cb990d8276aed1f3b76a
Make StorageSerializer a template class
This patch modifies StorageSerializer so that it will be able to use
other streams than std::ostream and its derivatives. Within current
class hierarchy custom output streams with overloaded insertion operator
(operator<<) cannot be used, as it is non-virtual in std::ostream.
Change-Id: I3e713329c55aacfbb8daa23a5c4579d4c5db9f52
---
diff --git a/src/storage/CMakeLists.txt b/src/storage/CMakeLists.txt
index 996b37a..72f0ec2 100644
--- a/src/storage/CMakeLists.txt
+++ b/src/storage/CMakeLists.txt
@@ -1,4 +1,4 @@
-# Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+# Copyright (c) 2014-2015 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.
@@ -29,7 +29,6 @@ SET(LIB_CYNARA_STORAGE_SOURCES
${CYNARA_LIB_CYNARA_STORAGE_PATH}/Integrity.cpp
${CYNARA_LIB_CYNARA_STORAGE_PATH}/Storage.cpp
${CYNARA_LIB_CYNARA_STORAGE_PATH}/StorageDeserializer.cpp
- ${CYNARA_LIB_CYNARA_STORAGE_PATH}/StorageSerializer.cpp
)
INCLUDE_DIRECTORIES(
diff --git a/src/storage/InMemoryStorageBackend.cpp b/src/storage/InMemoryStorageBackend.cpp
index a5e08b1..d6655e2 100644
--- a/src/storage/InMemoryStorageBackend.cpp
+++ b/src/storage/InMemoryStorageBackend.cpp
@@ -100,7 +100,7 @@ void InMemoryStorageBackend::save(void) {
std::string indexFilename = m_dbPath + m_indexFilename;
openDumpFileStream(indexStream, indexFilename + m_backupFilenameSuffix);
- StorageSerializer storageSerializer(indexStream);
+ StorageSerializer storageSerializer(indexStream);
storageSerializer.dump(buckets(), std::bind(&InMemoryStorageBackend::bucketDumpStreamOpener,
this, std::placeholders::_1));
@@ -253,14 +253,14 @@ std::shared_ptr InMemoryStorageBackend::bucketStreamOpener(
}
}
-std::shared_ptr InMemoryStorageBackend::bucketDumpStreamOpener(
+std::shared_ptr > InMemoryStorageBackend::bucketDumpStreamOpener(
const PolicyBucketId &bucketId) {
std::string bucketFilename = m_dbPath + m_bucketFilenamePrefix +
bucketId + m_backupFilenameSuffix;
auto bucketStream = std::make_shared();
openDumpFileStream(bucketStream, bucketFilename);
- return std::make_shared(bucketStream);
+ return std::make_shared >(bucketStream);
}
void InMemoryStorageBackend::postLoadCleanup(bool isBackupValid) {
diff --git a/src/storage/InMemoryStorageBackend.h b/src/storage/InMemoryStorageBackend.h
index c9e63c2..10251c7 100644
--- a/src/storage/InMemoryStorageBackend.h
+++ b/src/storage/InMemoryStorageBackend.h
@@ -71,7 +71,8 @@ protected:
virtual void openDumpFileStream(std::shared_ptr stream,
const std::string &filename);
- std::shared_ptr bucketDumpStreamOpener(const PolicyBucketId &bucketId);
+ std::shared_ptr > bucketDumpStreamOpener(
+ const PolicyBucketId &bucketId);
virtual void postLoadCleanup(bool isBackupValid);
diff --git a/src/storage/StorageSerializer.cpp b/src/storage/StorageSerializer.cpp
deleted file mode 100644
index 9f701dd..0000000
--- a/src/storage/StorageSerializer.cpp
+++ /dev/null
@@ -1,92 +0,0 @@
-/*
- * Copyright (c) 2014-2015 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 src/storage/StorageSerializer.cpp
- * @author Aleksander Zdyb
- * @version 1.0
- * @brief Implementation of Cynara::StorageSerializer methods
- */
-
-#include
-#include
-#include
-
-#include
-#include
-#include
-#include
-#include
-#include
-
-#include "StorageSerializer.h"
-
-namespace Cynara {
-
-StorageSerializer::StorageSerializer(std::shared_ptr os) : m_outStream(os) {
-}
-
-void StorageSerializer::dump(const Buckets &buckets,
- BucketStreamOpener streamOpener) {
-
- for (const auto bucketIter : buckets) {
- const auto &bucket = bucketIter.second;
-
- dumpFields(bucket.id(), bucket.defaultPolicy().policyType(),
- bucket.defaultPolicy().metadata());
- }
-
- for (const auto bucketIter : buckets) {
- const auto &bucketId = bucketIter.first;
- const auto &bucket = bucketIter.second;
- auto bucketSerializer = streamOpener(bucketId);
-
- if (bucketSerializer != nullptr) {
- bucketSerializer->dump(bucket);
- } else {
- throw BucketSerializationException(bucketId);
- }
- }
-}
-
-void StorageSerializer::dump(const PolicyBucket& bucket) {
- for (auto it = std::begin(bucket); it != std::end(bucket); ++it) {
- const auto &policy = *it;
- dump(policy);
- }
-}
-
-void StorageSerializer::dump(const PolicyKeyFeature &keyFeature) {
- *m_outStream << keyFeature.toString();
-}
-
-void StorageSerializer::dump(const PolicyType &policyType) {
- auto oldFormat = m_outStream->flags();
- *m_outStream << "0x" << std::uppercase << std::hex << policyType;
- m_outStream->flags(oldFormat);
-}
-
-void StorageSerializer::dump(const PolicyResult::PolicyMetadata &metadata) {
- *m_outStream << metadata;
-}
-
-void StorageSerializer::dump(const PolicyCollection::value_type &policy) {
- const auto &key = policy->key();
- const auto &result = policy->result();
-
- dumpFields(key.client(), key.user(), key.privilege(), result.policyType(), result.metadata());
-}
-
-} /* namespace Cynara */
diff --git a/src/storage/StorageSerializer.h b/src/storage/StorageSerializer.h
index 42982b2..40eb784 100644
--- a/src/storage/StorageSerializer.h
+++ b/src/storage/StorageSerializer.h
@@ -25,9 +25,13 @@
#include
#include
+#include
#include
+#include
#include
+#include
+#include
#include
#include
#include
@@ -39,13 +43,14 @@
namespace Cynara {
+template
class StorageSerializer {
public:
typedef std::function(const PolicyBucketId &)>
BucketStreamOpener;
- StorageSerializer(std::shared_ptr os);
+ StorageSerializer(std::shared_ptr os);
virtual ~StorageSerializer() {};
virtual void dump(const Buckets &buckets,
@@ -72,9 +77,68 @@ protected:
void dump(const PolicyCollection::value_type &policy);
private:
- std::shared_ptr m_outStream;
+ std::shared_ptr m_outStream;
};
+template
+StorageSerializer::StorageSerializer(std::shared_ptr os) : m_outStream(os) {
+}
+
+template
+void StorageSerializer::dump(const Buckets &buckets, BucketStreamOpener streamOpener) {
+ for (const auto bucketIter : buckets) {
+ const auto &bucket = bucketIter.second;
+
+ dumpFields(bucket.id(), bucket.defaultPolicy().policyType(),
+ bucket.defaultPolicy().metadata());
+ }
+
+ for (const auto bucketIter : buckets) {
+ const auto &bucketId = bucketIter.first;
+ const auto &bucket = bucketIter.second;
+ auto bucketSerializer = streamOpener(bucketId);
+
+ if (bucketSerializer != nullptr) {
+ bucketSerializer->dump(bucket);
+ } else {
+ throw BucketSerializationException(bucketId);
+ }
+ }
+}
+
+template
+void StorageSerializer::dump(const PolicyBucket &bucket) {
+ for (auto it = std::begin(bucket); it != std::end(bucket); ++it) {
+ const auto &policy = *it;
+ dump(policy);
+ }
+}
+
+template
+void StorageSerializer::dump(const PolicyKeyFeature &keyFeature) {
+ *m_outStream << keyFeature.toString();
+}
+
+template
+void StorageSerializer::dump(const PolicyType &policyType) {
+ auto oldFormat = m_outStream->flags();
+ *m_outStream << "0x" << std::uppercase << std::hex << policyType;
+ m_outStream->flags(oldFormat);
+}
+
+template
+void StorageSerializer::dump(const PolicyResult::PolicyMetadata &metadata) {
+ *m_outStream << metadata;
+}
+
+template
+void StorageSerializer::dump(const PolicyCollection::value_type &policy) {
+ const auto &key = policy->key();
+ const auto &result = policy->result();
+
+ dumpFields(key.client(), key.user(), key.privilege(), result.policyType(), result.metadata());
+}
+
} /* namespace Cynara */
#endif /* SRC_STORAGE_STORAGESERIALIZER_H_ */
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 5050634..9cf89d7 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -68,7 +68,6 @@ SET(CYNARA_SOURCES_FOR_TESTS
${CYNARA_SRC}/storage/Integrity.cpp
${CYNARA_SRC}/storage/Storage.cpp
${CYNARA_SRC}/storage/StorageDeserializer.cpp
- ${CYNARA_SRC}/storage/StorageSerializer.cpp
)
SET(CYNARA_TESTS_SOURCES
diff --git a/test/storage/serializer/dump.cpp b/test/storage/serializer/dump.cpp
index 2eafcf7..1daaf32 100644
--- a/test/storage/serializer/dump.cpp
+++ b/test/storage/serializer/dump.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2014-2015 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.
@@ -51,7 +51,7 @@ TEST(serializer_dump, dump_empty_bucket) {
auto oss = std::make_shared();
PolicyBucket bucket("empty");
- StorageSerializer serializer(oss);
+ StorageSerializer serializer(oss);
serializer.dump(bucket);
ASSERT_EQ("", oss->str());
@@ -69,7 +69,7 @@ TEST(serializer_dump, dump_bucket) {
Policy::simpleWithKey(pk2, DENY) }));
auto outStream = std::make_shared();
- StorageSerializer serializer(outStream);
+ StorageSerializer serializer(outStream);
serializer.dump(bucket);
// Split stream into records
@@ -99,7 +99,7 @@ TEST(serializer_dump, dump_bucket_bucket) {
Policy::bucketWithKey(pk3, bucketId) }};
auto outStream = std::make_shared();
- StorageSerializer serializer(outStream);
+ StorageSerializer serializer(outStream);
serializer.dump(bucket);
// Split stream into records
diff --git a/test/storage/serializer/dump_load.cpp b/test/storage/serializer/dump_load.cpp
index f3eff9b..cd4ce5c 100644
--- a/test/storage/serializer/dump_load.cpp
+++ b/test/storage/serializer/dump_load.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2014-2015 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.
@@ -54,7 +54,7 @@ TEST(dump_load, bucket) {
auto ioStream = std::make_shared();
- StorageSerializer serializer(ioStream);
+ StorageSerializer serializer(ioStream);
serializer.dump(bucket);
BucketDeserializer deserializer(ioStream);
diff --git a/test/storage/serializer/serialize.cpp b/test/storage/serializer/serialize.cpp
index 938283d..1681a5b 100644
--- a/test/storage/serializer/serialize.cpp
+++ b/test/storage/serializer/serialize.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2014-2015 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.
@@ -37,21 +37,24 @@
class FakeStreamForBucketId {
public:
+ typedef std::shared_ptr >
+ StringstreamStorageSerializerPtr;
+
MOCK_METHOD1(streamForBucketId,
- std::shared_ptr(const Cynara::PolicyBucketId &));
+ StringstreamStorageSerializerPtr(const Cynara::PolicyBucketId &));
- Cynara::StorageSerializer::BucketStreamOpener streamOpener() {
+ Cynara::StorageSerializer::BucketStreamOpener streamOpener() {
return std::bind(&FakeStreamForBucketId::streamForBucketId, this, std::placeholders::_1);
}
};
// Fake StorageSerializer for Cynara::PolicyBucket
-class FakeStorageSerializer : public Cynara::StorageSerializer {
+class FakeStorageSerializer : public Cynara::StorageSerializer {
public:
- FakeStorageSerializer(std::shared_ptr o) : Cynara::StorageSerializer(o),
- outStream(o) {}
+ FakeStorageSerializer(std::shared_ptr o)
+ : Cynara::StorageSerializer(o), outStream(o) {}
MOCK_METHOD1(dump, void(const Cynara::PolicyBucket &bucket));
- std::shared_ptr outStream;
+ std::shared_ptr outStream;
};
class StorageSerializerFixture : public ::testing::Test {
@@ -67,8 +70,8 @@ using namespace Cynara;
// Be sure no calls to streamForBucketId() are made
// and output stream is not touched
TEST_F(StorageSerializerFixture, dump_buckets_empty) {
- auto outStream = std::make_shared();
- StorageSerializer serializer(outStream);
+ auto outStream = std::make_shared();
+ StorageSerializer serializer(outStream);
serializer.dump(Buckets(), fakeStreamOpener.streamOpener());
// Stream should be empty
@@ -83,7 +86,7 @@ TEST_F(StorageSerializerFixture, dump_buckets) {
// Will be returned as serializer for buckets
auto fakeBucketSerializer = std::make_shared(
- std::make_shared());
+ std::make_shared());
buckets = {
{ "bucket1", PolicyBucket("bucket1", PredefinedPolicyType::DENY) },
@@ -93,7 +96,7 @@ TEST_F(StorageSerializerFixture, dump_buckets) {
};
auto outStream = std::make_shared();
- StorageSerializer dbSerializer(outStream);
+ StorageSerializer dbSerializer(outStream);
// Make sure stream was opened for each bucket
EXPECT_CALL(fakeStreamOpener, streamForBucketId(_))
@@ -128,7 +131,7 @@ TEST_F(StorageSerializerFixture, dump_buckets_io_error) {
};
auto outStream = std::make_shared();
- StorageSerializer dbSerializer(outStream);
+ StorageSerializer dbSerializer(outStream);
// Make sure stream was opened for each bucket
EXPECT_CALL(fakeStreamOpener, streamForBucketId(_))