--- /dev/null
+/*
+ * 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 BucketDeserializationException.h
+ * @author Aleksander Zdyb <a.zdyb@partner.samsung.com>
+ * @version 1.0
+ * @brief Implementation of BucketDeserializationException
+ */
+#ifndef SRC_COMMON_EXCEPTIONS_BUCKETDESERIALIZATIONEXCEPTION_H_
+#define SRC_COMMON_EXCEPTIONS_BUCKETDESERIALIZATIONEXCEPTION_H_
+
+#include <exceptions/DatabaseException.h>
+#include <types/PolicyBucketId.h>
+
+namespace Cynara {
+
+class BucketDeserializationException : public DatabaseException {
+public:
+ BucketDeserializationException(const PolicyBucketId &bucketId) : m_bucketId(bucketId) {}
+
+ const std::string message(void) const {
+ if (m_message.empty()) {
+ m_message = "Could not deserialize 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_BUCKETDESERIALIZATIONEXCEPTION_H_ */
--- /dev/null
+/*
+ * 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 DatabaseException.h
+ * @author Aleksander Zdyb <a.zdyb@partner.samsung.com>
+ * @version 1.0
+ * @brief Common class for database exceptions
+ */
+#ifndef SRC_COMMON_EXCEPTIONS_DATABASEEXCEPTION_H_
+#define SRC_COMMON_EXCEPTIONS_DATABASEEXCEPTION_H_
+
+#include <exceptions/Exception.h>
+
+namespace Cynara {
+
+class DatabaseException : public Exception {
+
+};
+
+} /* namespace Cynara */
+
+#endif /* SRC_COMMON_EXCEPTIONS_DATABASEEXCEPTION_H_ */
* @brief Tests for Cynara::StorageDeserializer
*/
+#include <istream>
+#include <memory>
+#include <tuple>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
+#include <exceptions/BucketDeserializationException.h>
#include <types/PolicyBucket.h>
#include <storage/StorageDeserializer.h>
-#include <tuple>
-#include <memory>
-#include <istream>
-
-
// TODO: Move to .h, because it's used also in bucket_load.cpp
MATCHER_P(PolicyPtrEq, policy, "") {
return std::tie(policy->key(), policy->result())
TEST_F(StorageDeserializerFixture, init_overwrite) {
using ::testing::UnorderedElementsAre;
- std::istringstream ss(";0");
+ std::istringstream ss(";0x0");
StorageDeserializer deserializer(ss, nullStreamOpener);
InMemoryStorageBackend::Buckets buckets;
+ // Default bucket has ALLOW policy as default
buckets.insert({ "", PolicyBucket("fakeId", PredefinedPolicyType::ALLOW) });
+
deserializer.initBuckets(buckets);
+ // Check, if default bucket has now DENY as default policy, which would be read from stream
ASSERT_THAT(buckets, UnorderedElementsAre(
COMPARE_BUCKETS("", PolicyBucket("", PredefinedPolicyType::DENY))
));
deserializer.loadBuckets(buckets);
- // Check if our bucket is still there
+ // Check if pre-inserted bucket is still there
ASSERT_THAT(buckets, UnorderedElementsAre(
COMPARE_BUCKETS("", PolicyBucket("", PredefinedPolicyType::DENY))
));
// Check, if streamOpener was called for each bucket
EXPECT_CALL(streamOpener, streamForBucketId(""))
- .WillOnce(Return(nullptr));
+ .WillOnce(Return(emptyBucketStream()));
EXPECT_CALL(streamOpener, streamForBucketId("bucket1"))
- .WillOnce(Return(nullptr));
+ .WillOnce(Return(emptyBucketStream()));
EXPECT_CALL(streamOpener, streamForBucketId("bucket2"))
- .WillOnce(Return(nullptr));
+ .WillOnce(Return(emptyBucketStream()));
deserializer.loadBuckets(buckets);
}
+
+TEST_F(StorageDeserializerFixture, load_buckets_io_error) {
+ using ::testing::_;
+ using ::testing::Return;
+ using ::testing::UnorderedElementsAre;
+
+ // Pre-insert some buckets
+ InMemoryStorageBackend::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);
+
+ // Check, if streamOpener was called for each bucket
+ EXPECT_CALL(streamOpener, streamForBucketId(""))
+ .WillOnce(Return(nullptr));
+
+ ASSERT_THROW(deserializer.loadBuckets(buckets), BucketDeserializationException);
+}