Chromium Code Reviews| Index: sync/internal_api/attachments/on_disk_attachment_store_unittest.cc |
| diff --git a/sync/internal_api/attachments/on_disk_attachment_store_unittest.cc b/sync/internal_api/attachments/on_disk_attachment_store_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..44f49bb908d27539f93109923bef141691971131 |
| --- /dev/null |
| +++ b/sync/internal_api/attachments/on_disk_attachment_store_unittest.cc |
| @@ -0,0 +1,136 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "sync/api/attachments/attachment_store.h" |
| + |
| +#include "base/files/file_path.h" |
| +#include "base/files/file_util.h" |
| +#include "base/files/scoped_temp_dir.h" |
| +#include "base/message_loop/message_loop.h" |
| +#include "base/run_loop.h" |
| +#include "base/time/time.h" |
| +#include "sync/internal_api/attachments/attachment_store_test_template.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace syncer { |
| + |
| +// Instantiation of common attachment store tests. |
| +class OnDiskAttachmentStoreFactory { |
| + public: |
| + OnDiskAttachmentStoreFactory() {} |
| + ~OnDiskAttachmentStoreFactory() {} |
| + |
| + scoped_refptr<AttachmentStore> CreateAttachmentStore() { |
| + EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| + return AttachmentStore::CreateOnDiskStore(temp_dir_.path()); |
| + } |
| + |
| + private: |
| + base::ScopedTempDir temp_dir_; |
| +}; |
| + |
| +INSTANTIATE_TYPED_TEST_CASE_P(OnDisk, |
| + AttachmentStoreTest, |
| + OnDiskAttachmentStoreFactory); |
| + |
| +// Tests specific to OnDiskAttachmentStore. |
| +class OnDiskAttachmentStoreSpecificTest : public testing::Test { |
| + public: |
| + base::ScopedTempDir temp_dir_; |
| + base::MessageLoop message_loop_; |
| + scoped_refptr<AttachmentStore> store_; |
| + |
| + OnDiskAttachmentStoreSpecificTest() {} |
| + |
| + void CopyResult(AttachmentStore::Result* destination_result, |
| + const AttachmentStore::Result& source_result) { |
| + *destination_result = source_result; |
| + } |
| + |
| + void CopyResultAttachments( |
| + AttachmentStore::Result* destination_result, |
| + const AttachmentStore::Result& source_result, |
| + scoped_ptr<AttachmentMap> source_attachments, |
| + scoped_ptr<AttachmentIdList> source_failed_attachment_ids) { |
| + CopyResult(destination_result, source_result); |
| + } |
| + |
| + void RunLoop() { |
| + base::RunLoop run_loop; |
| + run_loop.RunUntilIdle(); |
| + } |
| +}; |
| + |
| +// Ensure that store can be closed and reopen and keeps attachment stored in it. |
|
maniscalco
2014/10/14 21:03:19
How about "closed and reopened while retaining the
pavely
2014/10/16 22:13:30
Done.
|
| +TEST_F(OnDiskAttachmentStoreSpecificTest, CloseAndReopen) { |
| + ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| + AttachmentStore::Result result; |
| + |
| + store_ = AttachmentStore::CreateOnDiskStore(temp_dir_.path()); |
| + |
| + result = AttachmentStore::UNSPECIFIED_ERROR; |
| + store_->Load(base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResult, |
| + base::Unretained(this), |
| + &result)); |
| + RunLoop(); |
| + EXPECT_EQ(result, AttachmentStore::SUCCESS); |
| + |
| + result = AttachmentStore::UNSPECIFIED_ERROR; |
| + std::string some_data = "data"; |
| + Attachment attachment = |
| + Attachment::Create(base::RefCountedString::TakeString(&some_data)); |
| + AttachmentList attachments; |
| + attachments.push_back(attachment); |
| + store_->Write(attachments, |
| + base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResult, |
| + base::Unretained(this), |
| + &result)); |
| + RunLoop(); |
| + EXPECT_EQ(result, AttachmentStore::SUCCESS); |
| + |
| + store_ = nullptr; |
|
maniscalco
2014/10/14 21:03:19
Just doing this for emphasis? I ask because the n
pavely
2014/10/16 22:13:29
Yes, this is just to make it easier to read this t
maniscalco
2014/10/17 00:00:19
Acknowledged.
|
| + store_ = AttachmentStore::CreateOnDiskStore(temp_dir_.path()); |
| + |
| + result = AttachmentStore::UNSPECIFIED_ERROR; |
| + store_->Load(base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResult, |
| + base::Unretained(this), |
| + &result)); |
| + RunLoop(); |
| + EXPECT_EQ(result, AttachmentStore::SUCCESS); |
| + |
| + result = AttachmentStore::UNSPECIFIED_ERROR; |
| + AttachmentIdList attachment_ids; |
| + attachment_ids.push_back(attachment.GetId()); |
| + store_->Read( |
| + attachment_ids, |
| + base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResultAttachments, |
| + base::Unretained(this), |
| + &result)); |
| + RunLoop(); |
| + EXPECT_EQ(result, AttachmentStore::SUCCESS); |
| +} |
| + |
| +// Ensure loading corrupt attachment store fails. |
| +TEST_F(OnDiskAttachmentStoreSpecificTest, FailToOpen) { |
| + ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| + base::FilePath db_path = |
| + temp_dir_.path().Append(base::FilePath::FromUTF8Unsafe("leveldb")); |
| + base::CreateDirectory(db_path); |
| + |
| + // To simulate corrupt database write garbage to CURRENT file. |
| + std::string current_file_content = "abra.cadabra"; |
| + base::WriteFile(db_path.Append(base::FilePath::FromUTF8Unsafe("CURRENT")), |
| + current_file_content.c_str(), |
| + current_file_content.size()); |
| + |
| + AttachmentStore::Result result = AttachmentStore::SUCCESS; |
| + store_ = AttachmentStore::CreateOnDiskStore(db_path); |
| + store_->Load(base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResult, |
| + base::Unretained(this), |
| + &result)); |
| + RunLoop(); |
| + EXPECT_EQ(result, AttachmentStore::UNSPECIFIED_ERROR); |
| +} |
| + |
| +} // namespace syncer |