-# 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.
${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(
std::string indexFilename = m_dbPath + m_indexFilename;
openDumpFileStream(indexStream, indexFilename + m_backupFilenameSuffix);
- StorageSerializer storageSerializer(indexStream);
+ StorageSerializer<std::ofstream> storageSerializer(indexStream);
storageSerializer.dump(buckets(), std::bind(&InMemoryStorageBackend::bucketDumpStreamOpener,
this, std::placeholders::_1));
}
}
-std::shared_ptr<StorageSerializer> InMemoryStorageBackend::bucketDumpStreamOpener(
+std::shared_ptr<StorageSerializer<std::ofstream> > InMemoryStorageBackend::bucketDumpStreamOpener(
const PolicyBucketId &bucketId) {
std::string bucketFilename = m_dbPath + m_bucketFilenamePrefix +
bucketId + m_backupFilenameSuffix;
auto bucketStream = std::make_shared<std::ofstream>();
openDumpFileStream(bucketStream, bucketFilename);
- return std::make_shared<StorageSerializer>(bucketStream);
+ return std::make_shared<StorageSerializer<std::ofstream> >(bucketStream);
}
void InMemoryStorageBackend::postLoadCleanup(bool isBackupValid) {
virtual void openDumpFileStream(std::shared_ptr<std::ofstream> stream,
const std::string &filename);
- std::shared_ptr<StorageSerializer> bucketDumpStreamOpener(const PolicyBucketId &bucketId);
+ std::shared_ptr<StorageSerializer<std::ofstream> > bucketDumpStreamOpener(
+ const PolicyBucketId &bucketId);
virtual void postLoadCleanup(bool isBackupValid);
+++ /dev/null
-/*
- * 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 <a.zdyb@samsung.com>
- * @version 1.0
- * @brief Implementation of Cynara::StorageSerializer methods
- */
-
-#include <fstream>
-#include <memory>
-#include <ios>
-
-#include <exceptions/BucketSerializationException.h>
-#include <types/Policy.h>
-#include <types/PolicyBucket.h>
-#include <types/PolicyBucketId.h>
-#include <types/PolicyCollection.h>
-#include <types/PolicyType.h>
-
-#include "StorageSerializer.h"
-
-namespace Cynara {
-
-StorageSerializer::StorageSerializer(std::shared_ptr<std::ostream> 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 */
#include <functional>
#include <fstream>
+#include <ios>
#include <memory>
+#include <sstream>
#include <config/PathConfig.h>
+#include <exceptions/BucketSerializationException.h>
+#include <types/Policy.h>
#include <types/PolicyBucket.h>
#include <types/PolicyBucketId.h>
#include <types/PolicyCollection.h>
namespace Cynara {
+template<typename StreamType>
class StorageSerializer {
public:
typedef std::function<std::shared_ptr<StorageSerializer>(const PolicyBucketId &)>
BucketStreamOpener;
- StorageSerializer(std::shared_ptr<std::ostream> os);
+ StorageSerializer(std::shared_ptr<StreamType> os);
virtual ~StorageSerializer() {};
virtual void dump(const Buckets &buckets,
void dump(const PolicyCollection::value_type &policy);
private:
- std::shared_ptr<std::ostream> m_outStream;
+ std::shared_ptr<StreamType> m_outStream;
};
+template<typename StreamType>
+StorageSerializer<StreamType>::StorageSerializer(std::shared_ptr<StreamType> os) : m_outStream(os) {
+}
+
+template<typename StreamType>
+void StorageSerializer<StreamType>::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<typename StreamType>
+void StorageSerializer<StreamType>::dump(const PolicyBucket &bucket) {
+ for (auto it = std::begin(bucket); it != std::end(bucket); ++it) {
+ const auto &policy = *it;
+ dump(policy);
+ }
+}
+
+template<typename StreamType>
+void StorageSerializer<StreamType>::dump(const PolicyKeyFeature &keyFeature) {
+ *m_outStream << keyFeature.toString();
+}
+
+template<typename StreamType>
+void StorageSerializer<StreamType>::dump(const PolicyType &policyType) {
+ auto oldFormat = m_outStream->flags();
+ *m_outStream << "0x" << std::uppercase << std::hex << policyType;
+ m_outStream->flags(oldFormat);
+}
+
+template<typename StreamType>
+void StorageSerializer<StreamType>::dump(const PolicyResult::PolicyMetadata &metadata) {
+ *m_outStream << metadata;
+}
+
+template<typename StreamType>
+void StorageSerializer<StreamType>::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_ */
${CYNARA_SRC}/storage/Integrity.cpp
${CYNARA_SRC}/storage/Storage.cpp
${CYNARA_SRC}/storage/StorageDeserializer.cpp
- ${CYNARA_SRC}/storage/StorageSerializer.cpp
)
SET(CYNARA_TESTS_SOURCES
/*
- * 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.
auto oss = std::make_shared<std::ostringstream>();
PolicyBucket bucket("empty");
- StorageSerializer serializer(oss);
+ StorageSerializer<std::ostringstream> serializer(oss);
serializer.dump(bucket);
ASSERT_EQ("", oss->str());
Policy::simpleWithKey(pk2, DENY) }));
auto outStream = std::make_shared<std::stringstream>();
- StorageSerializer serializer(outStream);
+ StorageSerializer<std::stringstream> serializer(outStream);
serializer.dump(bucket);
// Split stream into records
Policy::bucketWithKey(pk3, bucketId) }};
auto outStream = std::make_shared<std::stringstream>();
- StorageSerializer serializer(outStream);
+ StorageSerializer<std::stringstream> serializer(outStream);
serializer.dump(bucket);
// Split stream into records
/*
- * 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.
auto ioStream = std::make_shared<std::stringstream>();
- StorageSerializer serializer(ioStream);
+ StorageSerializer<std::stringstream> serializer(ioStream);
serializer.dump(bucket);
BucketDeserializer deserializer(ioStream);
/*
- * 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.
class FakeStreamForBucketId {
public:
+ typedef std::shared_ptr<Cynara::StorageSerializer<std::stringstream> >
+ StringstreamStorageSerializerPtr;
+
MOCK_METHOD1(streamForBucketId,
- std::shared_ptr<Cynara::StorageSerializer>(const Cynara::PolicyBucketId &));
+ StringstreamStorageSerializerPtr(const Cynara::PolicyBucketId &));
- Cynara::StorageSerializer::BucketStreamOpener streamOpener() {
+ Cynara::StorageSerializer<std::stringstream>::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<std::stringstream> {
public:
- FakeStorageSerializer(std::shared_ptr<std::ostringstream> o) : Cynara::StorageSerializer(o),
- outStream(o) {}
+ FakeStorageSerializer(std::shared_ptr<std::stringstream> o)
+ : Cynara::StorageSerializer<std::stringstream>(o), outStream(o) {}
MOCK_METHOD1(dump, void(const Cynara::PolicyBucket &bucket));
- std::shared_ptr<std::ostringstream> outStream;
+ std::shared_ptr<std::stringstream> outStream;
};
class StorageSerializerFixture : public ::testing::Test {
// 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<std::ostringstream>();
- StorageSerializer serializer(outStream);
+ auto outStream = std::make_shared<std::stringstream>();
+ StorageSerializer<std::stringstream> serializer(outStream);
serializer.dump(Buckets(), fakeStreamOpener.streamOpener());
// Stream should be empty
// Will be returned as serializer for buckets
auto fakeBucketSerializer = std::make_shared<FakeStorageSerializer>(
- std::make_shared<std::ostringstream>());
+ std::make_shared<std::stringstream>());
buckets = {
{ "bucket1", PolicyBucket("bucket1", PredefinedPolicyType::DENY) },
};
auto outStream = std::make_shared<std::stringstream>();
- StorageSerializer dbSerializer(outStream);
+ StorageSerializer<std::stringstream> dbSerializer(outStream);
// Make sure stream was opened for each bucket
EXPECT_CALL(fakeStreamOpener, streamForBucketId(_))
};
auto outStream = std::make_shared<std::stringstream>();
- StorageSerializer dbSerializer(outStream);
+ StorageSerializer<std::stringstream> dbSerializer(outStream);
// Make sure stream was opened for each bucket
EXPECT_CALL(fakeStreamOpener, streamForBucketId(_))