| 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/internal_api/public/attachments/on_disk_attachment_store.h" | |
| 6 | |
| 7 #include <stdint.h> | |
| 8 | |
| 9 #include <string> | |
| 10 #include <utility> | |
| 11 | |
| 12 #include "base/files/file_path.h" | |
| 13 #include "base/files/file_util.h" | |
| 14 #include "base/files/scoped_temp_dir.h" | |
| 15 #include "base/memory/ptr_util.h" | |
| 16 #include "base/message_loop/message_loop.h" | |
| 17 #include "base/run_loop.h" | |
| 18 #include "base/threading/thread_task_runner_handle.h" | |
| 19 #include "base/time/time.h" | |
| 20 #include "sync/internal_api/attachments/attachment_store_test_template.h" | |
| 21 #include "sync/internal_api/attachments/proto/attachment_store.pb.h" | |
| 22 #include "testing/gmock/include/gmock/gmock.h" | |
| 23 #include "testing/gtest/include/gtest/gtest.h" | |
| 24 #include "third_party/leveldatabase/src/include/leveldb/db.h" | |
| 25 #include "third_party/leveldatabase/src/include/leveldb/options.h" | |
| 26 #include "third_party/leveldatabase/src/include/leveldb/slice.h" | |
| 27 #include "third_party/leveldatabase/src/include/leveldb/status.h" | |
| 28 | |
| 29 namespace syncer { | |
| 30 | |
| 31 namespace { | |
| 32 | |
| 33 void AttachmentStoreCreated(AttachmentStore::Result* result_dest, | |
| 34 const AttachmentStore::Result& result) { | |
| 35 *result_dest = result; | |
| 36 } | |
| 37 | |
| 38 } // namespace | |
| 39 | |
| 40 // Instantiation of common attachment store tests. | |
| 41 class OnDiskAttachmentStoreFactory { | |
| 42 public: | |
| 43 OnDiskAttachmentStoreFactory() {} | |
| 44 ~OnDiskAttachmentStoreFactory() {} | |
| 45 | |
| 46 std::unique_ptr<AttachmentStore> CreateAttachmentStore() { | |
| 47 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
| 48 std::unique_ptr<AttachmentStore> store; | |
| 49 AttachmentStore::Result result = AttachmentStore::UNSPECIFIED_ERROR; | |
| 50 store = AttachmentStore::CreateOnDiskStore( | |
| 51 temp_dir_.path(), base::ThreadTaskRunnerHandle::Get(), | |
| 52 base::Bind(&AttachmentStoreCreated, &result)); | |
| 53 base::RunLoop run_loop; | |
| 54 run_loop.RunUntilIdle(); | |
| 55 EXPECT_EQ(AttachmentStore::SUCCESS, result); | |
| 56 return store; | |
| 57 } | |
| 58 | |
| 59 private: | |
| 60 base::ScopedTempDir temp_dir_; | |
| 61 }; | |
| 62 | |
| 63 INSTANTIATE_TYPED_TEST_CASE_P(OnDisk, | |
| 64 AttachmentStoreTest, | |
| 65 OnDiskAttachmentStoreFactory); | |
| 66 | |
| 67 // Tests specific to OnDiskAttachmentStore. | |
| 68 class OnDiskAttachmentStoreSpecificTest : public testing::Test { | |
| 69 public: | |
| 70 base::ScopedTempDir temp_dir_; | |
| 71 base::FilePath db_path_; | |
| 72 base::MessageLoop message_loop_; | |
| 73 std::unique_ptr<AttachmentStore> store_; | |
| 74 | |
| 75 OnDiskAttachmentStoreSpecificTest() {} | |
| 76 | |
| 77 void SetUp() override { | |
| 78 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
| 79 db_path_ = temp_dir_.path().Append(FILE_PATH_LITERAL("leveldb")); | |
| 80 base::CreateDirectory(db_path_); | |
| 81 } | |
| 82 | |
| 83 void CopyResult(AttachmentStore::Result* destination_result, | |
| 84 const AttachmentStore::Result& source_result) { | |
| 85 *destination_result = source_result; | |
| 86 } | |
| 87 | |
| 88 void CopyResultAttachments( | |
| 89 AttachmentStore::Result* destination_result, | |
| 90 AttachmentIdList* destination_failed_attachment_ids, | |
| 91 const AttachmentStore::Result& source_result, | |
| 92 std::unique_ptr<AttachmentMap> source_attachments, | |
| 93 std::unique_ptr<AttachmentIdList> source_failed_attachment_ids) { | |
| 94 CopyResult(destination_result, source_result); | |
| 95 *destination_failed_attachment_ids = *source_failed_attachment_ids; | |
| 96 } | |
| 97 | |
| 98 void CopyResultMetadata( | |
| 99 AttachmentStore::Result* destination_result, | |
| 100 std::unique_ptr<AttachmentMetadataList>* destination_metadata, | |
| 101 const AttachmentStore::Result& source_result, | |
| 102 std::unique_ptr<AttachmentMetadataList> source_metadata) { | |
| 103 CopyResult(destination_result, source_result); | |
| 104 *destination_metadata = std::move(source_metadata); | |
| 105 } | |
| 106 | |
| 107 std::unique_ptr<leveldb::DB> OpenLevelDB() { | |
| 108 leveldb::DB* db; | |
| 109 leveldb::Options options; | |
| 110 options.create_if_missing = true; | |
| 111 leveldb::Status s = | |
| 112 leveldb::DB::Open(options, db_path_.AsUTF8Unsafe(), &db); | |
| 113 EXPECT_TRUE(s.ok()); | |
| 114 return base::WrapUnique(db); | |
| 115 } | |
| 116 | |
| 117 void UpdateRecord(const std::string& key, const std::string& content) { | |
| 118 std::unique_ptr<leveldb::DB> db = OpenLevelDB(); | |
| 119 leveldb::Status s = db->Put(leveldb::WriteOptions(), key, content); | |
| 120 EXPECT_TRUE(s.ok()); | |
| 121 } | |
| 122 | |
| 123 void UpdateStoreMetadataRecord(const std::string& content) { | |
| 124 UpdateRecord("database-metadata", content); | |
| 125 } | |
| 126 | |
| 127 void UpdateAttachmentMetadataRecord(const AttachmentId& attachment_id, | |
| 128 const std::string& content) { | |
| 129 std::string metadata_key = | |
| 130 OnDiskAttachmentStore::MakeMetadataKeyFromAttachmentId(attachment_id); | |
| 131 UpdateRecord(metadata_key, content); | |
| 132 } | |
| 133 | |
| 134 std::string ReadStoreMetadataRecord() { | |
| 135 std::unique_ptr<leveldb::DB> db = OpenLevelDB(); | |
| 136 std::string content; | |
| 137 leveldb::Status s = | |
| 138 db->Get(leveldb::ReadOptions(), "database-metadata", &content); | |
| 139 EXPECT_TRUE(s.ok()); | |
| 140 return content; | |
| 141 } | |
| 142 | |
| 143 void VerifyAttachmentRecordsPresent(const AttachmentId& attachment_id, | |
| 144 bool expect_records_present) { | |
| 145 std::unique_ptr<leveldb::DB> db = OpenLevelDB(); | |
| 146 | |
| 147 std::string metadata_key = | |
| 148 OnDiskAttachmentStore::MakeMetadataKeyFromAttachmentId(attachment_id); | |
| 149 std::string data_key = | |
| 150 OnDiskAttachmentStore::MakeDataKeyFromAttachmentId(attachment_id); | |
| 151 std::string data; | |
| 152 leveldb::Status s = db->Get(leveldb::ReadOptions(), data_key, &data); | |
| 153 if (expect_records_present) | |
| 154 EXPECT_TRUE(s.ok()); | |
| 155 else | |
| 156 EXPECT_TRUE(s.IsNotFound()); | |
| 157 s = db->Get(leveldb::ReadOptions(), metadata_key, &data); | |
| 158 if (expect_records_present) | |
| 159 EXPECT_TRUE(s.ok()); | |
| 160 else | |
| 161 EXPECT_TRUE(s.IsNotFound()); | |
| 162 } | |
| 163 | |
| 164 void RunLoop() { | |
| 165 base::RunLoop run_loop; | |
| 166 run_loop.RunUntilIdle(); | |
| 167 } | |
| 168 }; | |
| 169 | |
| 170 // Ensure that store can be closed and reopen while retaining stored | |
| 171 // attachments. | |
| 172 TEST_F(OnDiskAttachmentStoreSpecificTest, CloseAndReopen) { | |
| 173 AttachmentStore::Result result; | |
| 174 | |
| 175 result = AttachmentStore::UNSPECIFIED_ERROR; | |
| 176 store_ = AttachmentStore::CreateOnDiskStore( | |
| 177 temp_dir_.path(), base::ThreadTaskRunnerHandle::Get(), | |
| 178 base::Bind(&AttachmentStoreCreated, &result)); | |
| 179 RunLoop(); | |
| 180 EXPECT_EQ(AttachmentStore::SUCCESS, result); | |
| 181 | |
| 182 result = AttachmentStore::UNSPECIFIED_ERROR; | |
| 183 std::string some_data = "data"; | |
| 184 Attachment attachment = | |
| 185 Attachment::Create(base::RefCountedString::TakeString(&some_data)); | |
| 186 AttachmentList attachments; | |
| 187 attachments.push_back(attachment); | |
| 188 store_->Write(attachments, | |
| 189 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResult, | |
| 190 base::Unretained(this), | |
| 191 &result)); | |
| 192 RunLoop(); | |
| 193 EXPECT_EQ(AttachmentStore::SUCCESS, result); | |
| 194 | |
| 195 // Close and reopen attachment store. | |
| 196 store_ = nullptr; | |
| 197 result = AttachmentStore::UNSPECIFIED_ERROR; | |
| 198 store_ = AttachmentStore::CreateOnDiskStore( | |
| 199 temp_dir_.path(), base::ThreadTaskRunnerHandle::Get(), | |
| 200 base::Bind(&AttachmentStoreCreated, &result)); | |
| 201 RunLoop(); | |
| 202 EXPECT_EQ(AttachmentStore::SUCCESS, result); | |
| 203 | |
| 204 result = AttachmentStore::UNSPECIFIED_ERROR; | |
| 205 AttachmentIdList attachment_ids; | |
| 206 attachment_ids.push_back(attachment.GetId()); | |
| 207 AttachmentIdList failed_attachment_ids; | |
| 208 store_->Read( | |
| 209 attachment_ids, | |
| 210 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResultAttachments, | |
| 211 base::Unretained(this), &result, &failed_attachment_ids)); | |
| 212 RunLoop(); | |
| 213 EXPECT_EQ(AttachmentStore::SUCCESS, result); | |
| 214 EXPECT_TRUE(failed_attachment_ids.empty()); | |
| 215 } | |
| 216 | |
| 217 // Ensure loading corrupt attachment store fails. | |
| 218 TEST_F(OnDiskAttachmentStoreSpecificTest, FailToOpen) { | |
| 219 // To simulate corrupt database write empty CURRENT file. | |
| 220 std::string current_file_content = ""; | |
| 221 base::WriteFile(db_path_.Append(FILE_PATH_LITERAL("CURRENT")), | |
| 222 current_file_content.c_str(), current_file_content.size()); | |
| 223 | |
| 224 AttachmentStore::Result result = AttachmentStore::SUCCESS; | |
| 225 store_ = AttachmentStore::CreateOnDiskStore( | |
| 226 temp_dir_.path(), base::ThreadTaskRunnerHandle::Get(), | |
| 227 base::Bind(&AttachmentStoreCreated, &result)); | |
| 228 RunLoop(); | |
| 229 EXPECT_EQ(AttachmentStore::UNSPECIFIED_ERROR, result); | |
| 230 } | |
| 231 | |
| 232 // Ensure that attachment store works correctly when store metadata is missing, | |
| 233 // corrupt or has unknown schema version. | |
| 234 TEST_F(OnDiskAttachmentStoreSpecificTest, StoreMetadata) { | |
| 235 // Create and close empty database. | |
| 236 OpenLevelDB(); | |
| 237 // Open database with AttachmentStore. | |
| 238 AttachmentStore::Result result = AttachmentStore::UNSPECIFIED_ERROR; | |
| 239 store_ = AttachmentStore::CreateOnDiskStore( | |
| 240 temp_dir_.path(), base::ThreadTaskRunnerHandle::Get(), | |
| 241 base::Bind(&AttachmentStoreCreated, &result)); | |
| 242 RunLoop(); | |
| 243 EXPECT_EQ(AttachmentStore::SUCCESS, result); | |
| 244 // Close AttachmentStore so that test can check content. | |
| 245 store_ = nullptr; | |
| 246 RunLoop(); | |
| 247 | |
| 248 // AttachmentStore should create metadata record. | |
| 249 std::string data = ReadStoreMetadataRecord(); | |
| 250 attachment_store_pb::StoreMetadata metadata; | |
| 251 EXPECT_TRUE(metadata.ParseFromString(data)); | |
| 252 EXPECT_EQ(1, metadata.schema_version()); | |
| 253 | |
| 254 // Set unknown future schema version. | |
| 255 metadata.set_schema_version(2); | |
| 256 data = metadata.SerializeAsString(); | |
| 257 UpdateStoreMetadataRecord(data); | |
| 258 | |
| 259 // AttachmentStore should fail to load. | |
| 260 result = AttachmentStore::SUCCESS; | |
| 261 store_ = AttachmentStore::CreateOnDiskStore( | |
| 262 temp_dir_.path(), base::ThreadTaskRunnerHandle::Get(), | |
| 263 base::Bind(&AttachmentStoreCreated, &result)); | |
| 264 RunLoop(); | |
| 265 EXPECT_EQ(AttachmentStore::UNSPECIFIED_ERROR, result); | |
| 266 | |
| 267 // Write garbage into metadata record. | |
| 268 UpdateStoreMetadataRecord("abra.cadabra"); | |
| 269 | |
| 270 // AttachmentStore should fail to load. | |
| 271 result = AttachmentStore::SUCCESS; | |
| 272 store_ = AttachmentStore::CreateOnDiskStore( | |
| 273 temp_dir_.path(), base::ThreadTaskRunnerHandle::Get(), | |
| 274 base::Bind(&AttachmentStoreCreated, &result)); | |
| 275 RunLoop(); | |
| 276 EXPECT_EQ(AttachmentStore::UNSPECIFIED_ERROR, result); | |
| 277 } | |
| 278 | |
| 279 // Ensure that attachment store correctly maintains metadata records for | |
| 280 // attachments. | |
| 281 TEST_F(OnDiskAttachmentStoreSpecificTest, RecordMetadata) { | |
| 282 // Create attachment store. | |
| 283 AttachmentStore::Result create_result = AttachmentStore::UNSPECIFIED_ERROR; | |
| 284 store_ = AttachmentStore::CreateOnDiskStore( | |
| 285 temp_dir_.path(), base::ThreadTaskRunnerHandle::Get(), | |
| 286 base::Bind(&AttachmentStoreCreated, &create_result)); | |
| 287 | |
| 288 // Write two attachments. | |
| 289 AttachmentStore::Result write_result = AttachmentStore::UNSPECIFIED_ERROR; | |
| 290 std::string some_data; | |
| 291 AttachmentList attachments; | |
| 292 some_data = "data1"; | |
| 293 attachments.push_back( | |
| 294 Attachment::Create(base::RefCountedString::TakeString(&some_data))); | |
| 295 some_data = "data2"; | |
| 296 attachments.push_back( | |
| 297 Attachment::Create(base::RefCountedString::TakeString(&some_data))); | |
| 298 store_->Write(attachments, | |
| 299 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResult, | |
| 300 base::Unretained(this), &write_result)); | |
| 301 | |
| 302 // Delete one of written attachments. | |
| 303 AttachmentStore::Result drop_result = AttachmentStore::UNSPECIFIED_ERROR; | |
| 304 AttachmentIdList attachment_ids; | |
| 305 attachment_ids.push_back(attachments[0].GetId()); | |
| 306 store_->Drop(attachment_ids, | |
| 307 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResult, | |
| 308 base::Unretained(this), &drop_result)); | |
| 309 store_ = nullptr; | |
| 310 RunLoop(); | |
| 311 EXPECT_EQ(AttachmentStore::SUCCESS, create_result); | |
| 312 EXPECT_EQ(AttachmentStore::SUCCESS, write_result); | |
| 313 EXPECT_EQ(AttachmentStore::SUCCESS, drop_result); | |
| 314 | |
| 315 // Verify that attachment store contains only records for second attachment. | |
| 316 VerifyAttachmentRecordsPresent(attachments[0].GetId(), false); | |
| 317 VerifyAttachmentRecordsPresent(attachments[1].GetId(), true); | |
| 318 } | |
| 319 | |
| 320 // Ensure that attachment store fails to load attachment if the crc in the store | |
| 321 // does not match the data. | |
| 322 TEST_F(OnDiskAttachmentStoreSpecificTest, MismatchedCrcInStore) { | |
| 323 // Create attachment store. | |
| 324 AttachmentStore::Result create_result = AttachmentStore::UNSPECIFIED_ERROR; | |
| 325 store_ = AttachmentStore::CreateOnDiskStore( | |
| 326 temp_dir_.path(), base::ThreadTaskRunnerHandle::Get(), | |
| 327 base::Bind(&AttachmentStoreCreated, &create_result)); | |
| 328 | |
| 329 // Write attachment with incorrect crc32c. | |
| 330 AttachmentStore::Result write_result = AttachmentStore::UNSPECIFIED_ERROR; | |
| 331 const uint32_t intentionally_wrong_crc32c = 0; | |
| 332 | |
| 333 scoped_refptr<base::RefCountedString> some_data(new base::RefCountedString()); | |
| 334 some_data->data() = "data1"; | |
| 335 Attachment attachment = Attachment::CreateFromParts( | |
| 336 AttachmentId::Create(some_data->size(), intentionally_wrong_crc32c), | |
| 337 some_data); | |
| 338 AttachmentList attachments; | |
| 339 attachments.push_back(attachment); | |
| 340 store_->Write(attachments, | |
| 341 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResult, | |
| 342 base::Unretained(this), &write_result)); | |
| 343 | |
| 344 // Read attachment. | |
| 345 AttachmentStore::Result read_result = AttachmentStore::UNSPECIFIED_ERROR; | |
| 346 AttachmentIdList attachment_ids; | |
| 347 attachment_ids.push_back(attachment.GetId()); | |
| 348 AttachmentIdList failed_attachment_ids; | |
| 349 store_->Read( | |
| 350 attachment_ids, | |
| 351 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResultAttachments, | |
| 352 base::Unretained(this), &read_result, &failed_attachment_ids)); | |
| 353 RunLoop(); | |
| 354 EXPECT_EQ(AttachmentStore::SUCCESS, create_result); | |
| 355 EXPECT_EQ(AttachmentStore::SUCCESS, write_result); | |
| 356 EXPECT_EQ(AttachmentStore::UNSPECIFIED_ERROR, read_result); | |
| 357 EXPECT_THAT(failed_attachment_ids, testing::ElementsAre(attachment.GetId())); | |
| 358 } | |
| 359 | |
| 360 // Ensure that attachment store fails to load attachment if the crc in the id | |
| 361 // does not match the data. | |
| 362 TEST_F(OnDiskAttachmentStoreSpecificTest, MismatchedCrcInId) { | |
| 363 // Create attachment store. | |
| 364 AttachmentStore::Result create_result = AttachmentStore::UNSPECIFIED_ERROR; | |
| 365 store_ = AttachmentStore::CreateOnDiskStore( | |
| 366 temp_dir_.path(), base::ThreadTaskRunnerHandle::Get(), | |
| 367 base::Bind(&AttachmentStoreCreated, &create_result)); | |
| 368 | |
| 369 AttachmentStore::Result write_result = AttachmentStore::UNSPECIFIED_ERROR; | |
| 370 scoped_refptr<base::RefCountedString> some_data(new base::RefCountedString()); | |
| 371 some_data->data() = "data1"; | |
| 372 Attachment attachment = Attachment::Create(some_data); | |
| 373 AttachmentList attachments; | |
| 374 attachments.push_back(attachment); | |
| 375 store_->Write(attachments, | |
| 376 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResult, | |
| 377 base::Unretained(this), &write_result)); | |
| 378 | |
| 379 // Read, but with the wrong crc32c in the id. | |
| 380 AttachmentStore::Result read_result = AttachmentStore::SUCCESS; | |
| 381 | |
| 382 AttachmentId id_with_bad_crc32c = | |
| 383 AttachmentId::Create(attachment.GetId().GetSize(), 12345); | |
| 384 AttachmentIdList attachment_ids; | |
| 385 attachment_ids.push_back(id_with_bad_crc32c); | |
| 386 AttachmentIdList failed_attachment_ids; | |
| 387 store_->Read( | |
| 388 attachment_ids, | |
| 389 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResultAttachments, | |
| 390 base::Unretained(this), &read_result, &failed_attachment_ids)); | |
| 391 RunLoop(); | |
| 392 EXPECT_EQ(AttachmentStore::SUCCESS, create_result); | |
| 393 EXPECT_EQ(AttachmentStore::SUCCESS, write_result); | |
| 394 EXPECT_EQ(AttachmentStore::UNSPECIFIED_ERROR, read_result); | |
| 395 EXPECT_THAT(failed_attachment_ids, testing::ElementsAre(id_with_bad_crc32c)); | |
| 396 } | |
| 397 | |
| 398 // Ensure that after store initialization failure ReadWrite/Drop operations fail | |
| 399 // with correct error. | |
| 400 TEST_F(OnDiskAttachmentStoreSpecificTest, OpsAfterInitializationFailed) { | |
| 401 // To simulate corrupt database write empty CURRENT file. | |
| 402 std::string current_file_content = ""; | |
| 403 base::WriteFile(db_path_.Append(FILE_PATH_LITERAL("CURRENT")), | |
| 404 current_file_content.c_str(), current_file_content.size()); | |
| 405 | |
| 406 AttachmentStore::Result create_result = AttachmentStore::SUCCESS; | |
| 407 store_ = AttachmentStore::CreateOnDiskStore( | |
| 408 temp_dir_.path(), base::ThreadTaskRunnerHandle::Get(), | |
| 409 base::Bind(&AttachmentStoreCreated, &create_result)); | |
| 410 | |
| 411 // Reading from uninitialized store should result in | |
| 412 // STORE_INITIALIZATION_FAILED. | |
| 413 AttachmentStore::Result read_result = AttachmentStore::SUCCESS; | |
| 414 AttachmentIdList attachment_ids; | |
| 415 std::string some_data("data1"); | |
| 416 Attachment attachment = | |
| 417 Attachment::Create(base::RefCountedString::TakeString(&some_data)); | |
| 418 attachment_ids.push_back(attachment.GetId()); | |
| 419 AttachmentIdList failed_attachment_ids; | |
| 420 store_->Read( | |
| 421 attachment_ids, | |
| 422 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResultAttachments, | |
| 423 base::Unretained(this), &read_result, &failed_attachment_ids)); | |
| 424 | |
| 425 // Dropping from uninitialized store should result in | |
| 426 // STORE_INITIALIZATION_FAILED. | |
| 427 AttachmentStore::Result drop_result = AttachmentStore::SUCCESS; | |
| 428 store_->Drop(attachment_ids, | |
| 429 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResult, | |
| 430 base::Unretained(this), &drop_result)); | |
| 431 | |
| 432 // Writing to uninitialized store should result in | |
| 433 // STORE_INITIALIZATION_FAILED. | |
| 434 AttachmentStore::Result write_result = AttachmentStore::SUCCESS; | |
| 435 AttachmentList attachments; | |
| 436 attachments.push_back(attachment); | |
| 437 store_->Write(attachments, | |
| 438 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResult, | |
| 439 base::Unretained(this), &write_result)); | |
| 440 | |
| 441 RunLoop(); | |
| 442 EXPECT_EQ(AttachmentStore::UNSPECIFIED_ERROR, create_result); | |
| 443 EXPECT_EQ(AttachmentStore::STORE_INITIALIZATION_FAILED, read_result); | |
| 444 EXPECT_THAT(failed_attachment_ids, testing::ElementsAre(attachment_ids[0])); | |
| 445 EXPECT_EQ(AttachmentStore::STORE_INITIALIZATION_FAILED, drop_result); | |
| 446 EXPECT_EQ(AttachmentStore::STORE_INITIALIZATION_FAILED, write_result); | |
| 447 } | |
| 448 | |
| 449 // Ensure that attachment store handles the case of having an unexpected | |
| 450 // record at the end without crashing. | |
| 451 TEST_F(OnDiskAttachmentStoreSpecificTest, ReadMetadataWithUnexpectedRecord) { | |
| 452 // Write a bogus entry at the end of the database. | |
| 453 UpdateRecord("zzz", "foobar"); | |
| 454 | |
| 455 // Create attachment store. | |
| 456 AttachmentStore::Result create_result = AttachmentStore::UNSPECIFIED_ERROR; | |
| 457 store_ = AttachmentStore::CreateOnDiskStore( | |
| 458 temp_dir_.path(), base::ThreadTaskRunnerHandle::Get(), | |
| 459 base::Bind(&AttachmentStoreCreated, &create_result)); | |
| 460 | |
| 461 // Read all metadata. Should be getting no error and zero entries. | |
| 462 AttachmentStore::Result metadata_result = AttachmentStore::UNSPECIFIED_ERROR; | |
| 463 std::unique_ptr<AttachmentMetadataList> metadata_list; | |
| 464 store_->ReadMetadata( | |
| 465 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResultMetadata, | |
| 466 base::Unretained(this), &metadata_result, &metadata_list)); | |
| 467 RunLoop(); | |
| 468 EXPECT_EQ(AttachmentStore::SUCCESS, create_result); | |
| 469 EXPECT_EQ(AttachmentStore::SUCCESS, metadata_result); | |
| 470 EXPECT_EQ(0U, metadata_list->size()); | |
| 471 metadata_list.reset(); | |
| 472 | |
| 473 // Write 3 attachments to the store | |
| 474 AttachmentList attachments; | |
| 475 | |
| 476 for (int i = 0; i < 3; i++) { | |
| 477 std::string some_data = "data"; | |
| 478 Attachment attachment = | |
| 479 Attachment::Create(base::RefCountedString::TakeString(&some_data)); | |
| 480 attachments.push_back(attachment); | |
| 481 } | |
| 482 AttachmentStore::Result write_result = AttachmentStore::UNSPECIFIED_ERROR; | |
| 483 store_->Write(attachments, | |
| 484 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResult, | |
| 485 base::Unretained(this), &write_result)); | |
| 486 | |
| 487 // Read all metadata back. We should be getting 3 entries. | |
| 488 store_->ReadMetadata( | |
| 489 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResultMetadata, | |
| 490 base::Unretained(this), &metadata_result, &metadata_list)); | |
| 491 RunLoop(); | |
| 492 EXPECT_EQ(AttachmentStore::SUCCESS, write_result); | |
| 493 EXPECT_EQ(AttachmentStore::SUCCESS, metadata_result); | |
| 494 EXPECT_EQ(3U, metadata_list->size()); | |
| 495 // Get Id of the attachment in the middle. | |
| 496 AttachmentId id = (*metadata_list.get())[1].GetId(); | |
| 497 metadata_list.reset(); | |
| 498 | |
| 499 // Close the store. | |
| 500 store_ = nullptr; | |
| 501 RunLoop(); | |
| 502 | |
| 503 // Modify the middle attachment metadata entry so that it isn't valid anymore. | |
| 504 UpdateAttachmentMetadataRecord(id, "foobar"); | |
| 505 | |
| 506 // Reopen the store. | |
| 507 create_result = AttachmentStore::UNSPECIFIED_ERROR; | |
| 508 metadata_result = AttachmentStore::UNSPECIFIED_ERROR; | |
| 509 store_ = AttachmentStore::CreateOnDiskStore( | |
| 510 temp_dir_.path(), base::ThreadTaskRunnerHandle::Get(), | |
| 511 base::Bind(&AttachmentStoreCreated, &create_result)); | |
| 512 | |
| 513 // Read all metadata back. We should be getting a failure and | |
| 514 // only 2 entries now. | |
| 515 store_->ReadMetadata( | |
| 516 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResultMetadata, | |
| 517 base::Unretained(this), &metadata_result, &metadata_list)); | |
| 518 RunLoop(); | |
| 519 EXPECT_EQ(AttachmentStore::SUCCESS, create_result); | |
| 520 EXPECT_EQ(AttachmentStore::UNSPECIFIED_ERROR, metadata_result); | |
| 521 EXPECT_EQ(2U, metadata_list->size()); | |
| 522 } | |
| 523 | |
| 524 } // namespace syncer | |
| OLD | NEW |