From: Aleksander Zdyb Date: Wed, 16 Jul 2014 14:09:05 +0000 (+0200) Subject: Add serialization IO exception X-Git-Tag: accepted/tizen/common/20140722.142604~11 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=71bcac4c8ea72b3b93e1f73ea1749eac5a21a03e;p=platform%2Fcore%2Fsecurity%2Fcynara.git Add serialization IO exception BucketSerializationException is thrown, when bucket file stream could not be opened. Change-Id: If8d3ca06f708fbe4a67c82e43c967d56be30e8af --- diff --git a/src/common/exceptions/BucketSerializationException.h b/src/common/exceptions/BucketSerializationException.h new file mode 100644 index 0000000..8882ff4 --- /dev/null +++ b/src/common/exceptions/BucketSerializationException.h @@ -0,0 +1,52 @@ +/* + * 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 BucketSerializationException.h + * @author Aleksander Zdyb + * @version 1.0 + * @brief Implementation of BucketSerializationException + */ +#ifndef SRC_COMMON_EXCEPTIONS_BUCKETSERIALIZATIONEXCEPTION_H_ +#define SRC_COMMON_EXCEPTIONS_BUCKETSERIALIZATIONEXCEPTION_H_ + +#include +#include + +namespace Cynara { + +class BucketSerializationException : public DatabaseException { +public: + BucketSerializationException(const PolicyBucketId &bucketId) : m_bucketId(bucketId) {} + + const std::string message(void) const { + if (m_message.empty()) { + m_message = "Could not serialize bucket " + m_bucketId; + } + return m_message; + } + + const PolicyBucketId &bucketId(void) const { + return m_bucketId; + } + +private: + mutable std::string m_message; + PolicyBucketId m_bucketId; +}; + +} /* namespace Cynara */ + +#endif /* SRC_COMMON_EXCEPTIONS_BUCKETSERIALIZATIONEXCEPTION_H_ */ diff --git a/src/service/storage/StorageSerializer.cpp b/src/service/storage/StorageSerializer.cpp index e7857b6..be8b9d4 100644 --- a/src/service/storage/StorageSerializer.cpp +++ b/src/service/storage/StorageSerializer.cpp @@ -24,6 +24,7 @@ #include #include +#include #include "types/PolicyBucket.h" #include "types/PolicyCollection.h" @@ -54,7 +55,7 @@ void StorageSerializer::dump(const InMemoryStorageBackend::Buckets &buckets, if (bucketSerializer != nullptr) { bucketSerializer->dump(bucket); } else { - // TODO: Throw? + throw BucketSerializationException(bucketId); } } } diff --git a/test/storage/serializer/serialize.cpp b/test/storage/serializer/serialize.cpp index dddd30d..06b8cfb 100644 --- a/test/storage/serializer/serialize.cpp +++ b/test/storage/serializer/serialize.cpp @@ -29,6 +29,7 @@ #include #include +#include #include #include #include @@ -114,3 +115,24 @@ TEST_F(StorageSerializerFixture, dump_buckets) { ASSERT_THAT(actualRecords, UnorderedElementsAreArray(expectedRecords)); } + +TEST_F(StorageSerializerFixture, dump_buckets_io_error) { + using ::testing::_; + using ::testing::Return; + + buckets = { + { "bucket1", PolicyBucket("bucket1", PredefinedPolicyType::DENY) }, + }; + + std::stringstream outStream; + StorageSerializer dbSerializer(outStream); + + // Make sure stream was opened for each bucket + EXPECT_CALL(fakeStreamOpener, streamForBucketId(_)) + .Times(buckets.size()).WillRepeatedly(Return(nullptr)); + + ASSERT_THROW( + dbSerializer.dump(buckets, fakeStreamOpener.streamOpener()), + BucketSerializationException + ); +}