From: Pawel Wieczorek Date: Thu, 15 Jan 2015 07:19:15 +0000 (+0100) Subject: Introduce ChecksumStream X-Git-Tag: accepted/tizen/common/20150310.163254~11 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F12%2F33812%2F27;p=platform%2Fcore%2Fsecurity%2Fcynara.git Introduce ChecksumStream ChecksumStream will replace std::ofstream for saving database contents in storage. This way data will be not only written to the files, but also its checksums will be computed and stored in given stream (database index equivalent for storing checksums). Checksum computing is performed during stream destruction in order to be sure that all necessary data was already collected. Change-Id: I4a9ff2e29361f337cacd790d77364feca854a706 --- diff --git a/src/storage/CMakeLists.txt b/src/storage/CMakeLists.txt index 72f0ec2..f75bc06 100644 --- a/src/storage/CMakeLists.txt +++ b/src/storage/CMakeLists.txt @@ -24,6 +24,7 @@ SET(CYNARA_LIB_CYNARA_STORAGE_PATH ${CYNARA_PATH}/storage) SET(LIB_CYNARA_STORAGE_SOURCES ${CYNARA_LIB_CYNARA_STORAGE_PATH}/BucketDeserializer.cpp + ${CYNARA_LIB_CYNARA_STORAGE_PATH}/ChecksumStream.cpp ${CYNARA_LIB_CYNARA_STORAGE_PATH}/ChecksumValidator.cpp ${CYNARA_LIB_CYNARA_STORAGE_PATH}/InMemoryStorageBackend.cpp ${CYNARA_LIB_CYNARA_STORAGE_PATH}/Integrity.cpp diff --git a/src/storage/ChecksumStream.cpp b/src/storage/ChecksumStream.cpp new file mode 100644 index 0000000..551307e --- /dev/null +++ b/src/storage/ChecksumStream.cpp @@ -0,0 +1,71 @@ +/* + * Copyright (c) 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/ChecksumStream.cpp + * @author Pawel Wieczorek + * @version 1.0 + * @brief This file contains ChecksumStream implementation. + */ + +#include +#include + +#include + +#include + +#include "ChecksumStream.h" + +namespace Cynara { + +const char ChecksumStream::m_fieldSeparator(PathConfig::StoragePath::fieldSeparator); +const char ChecksumStream::m_recordSeparator(PathConfig::StoragePath::recordSeparator); + +ChecksumStream::~ChecksumStream() { + if (!std::uncaught_exception()) { + save(); + } +} + +ChecksumStream& ChecksumStream::operator<<(std::ostream& (*manip)(std::ostream&)) { + m_bufStream << manip; + m_outStream << manip; + return *this; +} + +void ChecksumStream::open(const std::string &filename, std::ios_base::openmode mode) { + m_outStream.open(filename, mode); +} + +bool ChecksumStream::is_open(void) const { + return m_outStream.is_open(); +} + +std::ios_base::fmtflags ChecksumStream::flags(void) const { + return m_outStream.flags(); +} + +std::ios_base::fmtflags ChecksumStream::flags(std::ios_base::fmtflags flags) { + return m_outStream.flags(flags); +} + +void ChecksumStream::save() { + m_outStream.close(); + *m_chsStream << m_filename << m_fieldSeparator << ChecksumValidator::generate(m_bufStream.str()) + << m_recordSeparator; +} + +} // namespace Cynara diff --git a/src/storage/ChecksumStream.h b/src/storage/ChecksumStream.h new file mode 100644 index 0000000..ddc6e6f --- /dev/null +++ b/src/storage/ChecksumStream.h @@ -0,0 +1,70 @@ +/* + * Copyright (c) 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/ChecksumStream.h + * @author Pawel Wieczorek + * @version 1.0 + * @brief This file contains ChecksumStream header. + */ + +#ifndef SRC_STORAGE_CHECKSUMSTREAM_H_ +#define SRC_STORAGE_CHECKSUMSTREAM_H_ + +#include +#include +#include +#include +#include + +namespace Cynara { + +class ChecksumStream { +public: + ChecksumStream(const std::string &filename, const std::shared_ptr &stream) + : m_chsStream(stream), m_filename(filename) { + } + ~ChecksumStream(); + + template + ChecksumStream& operator<<(const Type &item); + ChecksumStream& operator<<(std::ostream& (*manip)(std::ostream&)); + + void open(const std::string &filename, std::ios_base::openmode mode); + bool is_open(void) const; + std::ios_base::fmtflags flags(void) const; + std::ios_base::fmtflags flags(std::ios_base::fmtflags flags); + +private: + void save(void); + + std::shared_ptr m_chsStream; + std::ofstream m_outStream; + std::stringstream m_bufStream; + const std::string m_filename; + static const char m_fieldSeparator; + static const char m_recordSeparator; +}; + +template +ChecksumStream& ChecksumStream::operator<<(const Type& item) { + m_bufStream << item; + m_outStream << item; + return *this; +} + +} // namespace Cynara + +#endif // SRC_STORAGE_CHECKSUMSTREAM_H_ diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 9cf89d7..189268d 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -63,6 +63,7 @@ SET(CYNARA_SOURCES_FOR_TESTS ${CYNARA_SRC}/helpers/creds-commons/creds-commons.cpp ${CYNARA_SRC}/service/main/CmdlineParser.cpp ${CYNARA_SRC}/storage/BucketDeserializer.cpp + ${CYNARA_SRC}/storage/ChecksumStream.cpp ${CYNARA_SRC}/storage/ChecksumValidator.cpp ${CYNARA_SRC}/storage/InMemoryStorageBackend.cpp ${CYNARA_SRC}/storage/Integrity.cpp