OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "sync/api/attachments/attachment_store.h" |
| 6 |
| 7 #include "base/files/file_path.h" |
| 8 #include "base/files/file_util.h" |
| 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/run_loop.h" |
| 12 #include "base/thread_task_runner_handle.h" |
| 13 #include "base/time/time.h" |
| 14 #include "sync/internal_api/attachments/attachment_store_test_template.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 namespace syncer { |
| 18 |
| 19 namespace { |
| 20 |
| 21 void AttachmentStoreCreated(scoped_refptr<AttachmentStore>* store_dest, |
| 22 AttachmentStore::Result* result_dest, |
| 23 const AttachmentStore::Result& result, |
| 24 const scoped_refptr<AttachmentStore>& store) { |
| 25 *result_dest = result; |
| 26 *store_dest = store; |
| 27 } |
| 28 |
| 29 } // namespace |
| 30 |
| 31 // Instantiation of common attachment store tests. |
| 32 class OnDiskAttachmentStoreFactory { |
| 33 public: |
| 34 OnDiskAttachmentStoreFactory() {} |
| 35 ~OnDiskAttachmentStoreFactory() {} |
| 36 |
| 37 scoped_refptr<AttachmentStore> CreateAttachmentStore() { |
| 38 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 39 scoped_refptr<AttachmentStore> store; |
| 40 AttachmentStore::Result result = AttachmentStore::UNSPECIFIED_ERROR; |
| 41 AttachmentStore::CreateOnDiskStore( |
| 42 temp_dir_.path(), |
| 43 base::ThreadTaskRunnerHandle::Get(), |
| 44 base::Bind(&AttachmentStoreCreated, &store, &result)); |
| 45 base::RunLoop run_loop; |
| 46 run_loop.RunUntilIdle(); |
| 47 EXPECT_EQ(result, AttachmentStore::SUCCESS); |
| 48 return store; |
| 49 } |
| 50 |
| 51 private: |
| 52 base::ScopedTempDir temp_dir_; |
| 53 }; |
| 54 |
| 55 INSTANTIATE_TYPED_TEST_CASE_P(OnDisk, |
| 56 AttachmentStoreTest, |
| 57 OnDiskAttachmentStoreFactory); |
| 58 |
| 59 // Tests specific to OnDiskAttachmentStore. |
| 60 class OnDiskAttachmentStoreSpecificTest : public testing::Test { |
| 61 public: |
| 62 base::ScopedTempDir temp_dir_; |
| 63 base::MessageLoop message_loop_; |
| 64 scoped_refptr<AttachmentStore> store_; |
| 65 |
| 66 OnDiskAttachmentStoreSpecificTest() {} |
| 67 |
| 68 void CopyResult(AttachmentStore::Result* destination_result, |
| 69 const AttachmentStore::Result& source_result) { |
| 70 *destination_result = source_result; |
| 71 } |
| 72 |
| 73 void CopyResultAttachments( |
| 74 AttachmentStore::Result* destination_result, |
| 75 const AttachmentStore::Result& source_result, |
| 76 scoped_ptr<AttachmentMap> source_attachments, |
| 77 scoped_ptr<AttachmentIdList> source_failed_attachment_ids) { |
| 78 CopyResult(destination_result, source_result); |
| 79 } |
| 80 |
| 81 void RunLoop() { |
| 82 base::RunLoop run_loop; |
| 83 run_loop.RunUntilIdle(); |
| 84 } |
| 85 }; |
| 86 |
| 87 // Ensure that store can be closed and reopen while retaining stored |
| 88 // attachments. |
| 89 TEST_F(OnDiskAttachmentStoreSpecificTest, CloseAndReopen) { |
| 90 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 91 AttachmentStore::Result result; |
| 92 |
| 93 result = AttachmentStore::UNSPECIFIED_ERROR; |
| 94 AttachmentStore::CreateOnDiskStore( |
| 95 temp_dir_.path(), |
| 96 base::ThreadTaskRunnerHandle::Get(), |
| 97 base::Bind(&AttachmentStoreCreated, &store_, &result)); |
| 98 RunLoop(); |
| 99 EXPECT_EQ(result, AttachmentStore::SUCCESS); |
| 100 |
| 101 result = AttachmentStore::UNSPECIFIED_ERROR; |
| 102 std::string some_data = "data"; |
| 103 Attachment attachment = |
| 104 Attachment::Create(base::RefCountedString::TakeString(&some_data)); |
| 105 AttachmentList attachments; |
| 106 attachments.push_back(attachment); |
| 107 store_->Write(attachments, |
| 108 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResult, |
| 109 base::Unretained(this), |
| 110 &result)); |
| 111 RunLoop(); |
| 112 EXPECT_EQ(result, AttachmentStore::SUCCESS); |
| 113 |
| 114 // Close attachment store. |
| 115 store_ = nullptr; |
| 116 result = AttachmentStore::UNSPECIFIED_ERROR; |
| 117 AttachmentStore::CreateOnDiskStore( |
| 118 temp_dir_.path(), |
| 119 base::ThreadTaskRunnerHandle::Get(), |
| 120 base::Bind(&AttachmentStoreCreated, &store_, &result)); |
| 121 RunLoop(); |
| 122 EXPECT_EQ(result, AttachmentStore::SUCCESS); |
| 123 |
| 124 result = AttachmentStore::UNSPECIFIED_ERROR; |
| 125 AttachmentIdList attachment_ids; |
| 126 attachment_ids.push_back(attachment.GetId()); |
| 127 store_->Read( |
| 128 attachment_ids, |
| 129 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResultAttachments, |
| 130 base::Unretained(this), |
| 131 &result)); |
| 132 RunLoop(); |
| 133 EXPECT_EQ(result, AttachmentStore::SUCCESS); |
| 134 } |
| 135 |
| 136 // Ensure loading corrupt attachment store fails. |
| 137 TEST_F(OnDiskAttachmentStoreSpecificTest, FailToOpen) { |
| 138 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 139 base::FilePath db_path = |
| 140 temp_dir_.path().Append(FILE_PATH_LITERAL("leveldb")); |
| 141 base::CreateDirectory(db_path); |
| 142 |
| 143 // To simulate corrupt database write garbage to CURRENT file. |
| 144 std::string current_file_content = "abra.cadabra"; |
| 145 base::WriteFile(db_path.Append(FILE_PATH_LITERAL("CURRENT")), |
| 146 current_file_content.c_str(), |
| 147 current_file_content.size()); |
| 148 |
| 149 AttachmentStore::Result result = AttachmentStore::SUCCESS; |
| 150 AttachmentStore::CreateOnDiskStore( |
| 151 temp_dir_.path(), |
| 152 base::ThreadTaskRunnerHandle::Get(), |
| 153 base::Bind(&AttachmentStoreCreated, &store_, &result)); |
| 154 RunLoop(); |
| 155 EXPECT_EQ(result, AttachmentStore::UNSPECIFIED_ERROR); |
| 156 EXPECT_EQ(store_.get(), nullptr); |
| 157 } |
| 158 |
| 159 } // namespace syncer |
OLD | NEW |