OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "sync/internal_api/public/attachments/on_disk_attachment_store.h" | 5 #include "sync/internal_api/public/attachments/on_disk_attachment_store.h" |
6 | 6 |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
(...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
304 RunLoop(); | 304 RunLoop(); |
305 EXPECT_EQ(AttachmentStore::SUCCESS, create_result); | 305 EXPECT_EQ(AttachmentStore::SUCCESS, create_result); |
306 EXPECT_EQ(AttachmentStore::SUCCESS, write_result); | 306 EXPECT_EQ(AttachmentStore::SUCCESS, write_result); |
307 EXPECT_EQ(AttachmentStore::SUCCESS, drop_result); | 307 EXPECT_EQ(AttachmentStore::SUCCESS, drop_result); |
308 | 308 |
309 // Verify that attachment store contains only records for second attachment. | 309 // Verify that attachment store contains only records for second attachment. |
310 VerifyAttachmentRecordsPresent(attachments[0].GetId(), false); | 310 VerifyAttachmentRecordsPresent(attachments[0].GetId(), false); |
311 VerifyAttachmentRecordsPresent(attachments[1].GetId(), true); | 311 VerifyAttachmentRecordsPresent(attachments[1].GetId(), true); |
312 } | 312 } |
313 | 313 |
314 // Ensure that attachment store fails to load attachment if the crc in the store | 314 // Ensure that attachment store fails to load attachment with mismatched crc. |
315 // does not match the data. | 315 TEST_F(OnDiskAttachmentStoreSpecificTest, MismatchedCrc) { |
316 TEST_F(OnDiskAttachmentStoreSpecificTest, MismatchedCrcInStore) { | |
317 // Create attachment store. | 316 // Create attachment store. |
318 AttachmentStore::Result create_result = AttachmentStore::UNSPECIFIED_ERROR; | 317 AttachmentStore::Result create_result = AttachmentStore::UNSPECIFIED_ERROR; |
319 store_ = AttachmentStore::CreateOnDiskStore( | 318 store_ = AttachmentStore::CreateOnDiskStore( |
320 temp_dir_.path(), base::ThreadTaskRunnerHandle::Get(), | 319 temp_dir_.path(), base::ThreadTaskRunnerHandle::Get(), |
321 base::Bind(&AttachmentStoreCreated, &create_result)); | 320 base::Bind(&AttachmentStoreCreated, &create_result)); |
322 | 321 |
323 // Write attachment with incorrect crc32c. | 322 // Write attachment with incorrect crc32c. |
324 AttachmentStore::Result write_result = AttachmentStore::UNSPECIFIED_ERROR; | 323 AttachmentStore::Result write_result = AttachmentStore::UNSPECIFIED_ERROR; |
325 const uint32_t intentionally_wrong_crc32c = 0; | 324 const uint32_t intentionally_wrong_crc32c = 0; |
326 | 325 std::string some_data("data1"); |
327 scoped_refptr<base::RefCountedString> some_data(new base::RefCountedString()); | |
328 some_data->data() = "data1"; | |
329 Attachment attachment = Attachment::CreateFromParts( | 326 Attachment attachment = Attachment::CreateFromParts( |
330 AttachmentId::Create(some_data->size(), intentionally_wrong_crc32c), | 327 AttachmentId::Create(), base::RefCountedString::TakeString(&some_data), |
331 some_data); | 328 intentionally_wrong_crc32c); |
332 AttachmentList attachments; | 329 AttachmentList attachments; |
333 attachments.push_back(attachment); | 330 attachments.push_back(attachment); |
334 store_->Write(attachments, | 331 store_->Write(attachments, |
335 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResult, | 332 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResult, |
336 base::Unretained(this), &write_result)); | 333 base::Unretained(this), &write_result)); |
337 | 334 |
338 // Read attachment. | 335 // Read attachment. |
339 AttachmentStore::Result read_result = AttachmentStore::UNSPECIFIED_ERROR; | 336 AttachmentStore::Result read_result = AttachmentStore::UNSPECIFIED_ERROR; |
340 AttachmentIdList attachment_ids; | 337 AttachmentIdList attachment_ids; |
341 attachment_ids.push_back(attachment.GetId()); | 338 attachment_ids.push_back(attachment.GetId()); |
342 AttachmentIdList failed_attachment_ids; | 339 AttachmentIdList failed_attachment_ids; |
343 store_->Read( | 340 store_->Read( |
344 attachment_ids, | 341 attachment_ids, |
345 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResultAttachments, | 342 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResultAttachments, |
346 base::Unretained(this), &read_result, &failed_attachment_ids)); | 343 base::Unretained(this), &read_result, &failed_attachment_ids)); |
347 RunLoop(); | 344 RunLoop(); |
348 EXPECT_EQ(AttachmentStore::SUCCESS, create_result); | 345 EXPECT_EQ(AttachmentStore::SUCCESS, create_result); |
349 EXPECT_EQ(AttachmentStore::SUCCESS, write_result); | 346 EXPECT_EQ(AttachmentStore::SUCCESS, write_result); |
350 EXPECT_EQ(AttachmentStore::UNSPECIFIED_ERROR, read_result); | 347 EXPECT_EQ(AttachmentStore::UNSPECIFIED_ERROR, read_result); |
351 EXPECT_THAT(failed_attachment_ids, testing::ElementsAre(attachment.GetId())); | 348 EXPECT_THAT(failed_attachment_ids, testing::ElementsAre(attachment.GetId())); |
352 } | 349 } |
353 | 350 |
354 // Ensure that attachment store fails to load attachment if the crc in the id | |
355 // does not match the data. | |
356 TEST_F(OnDiskAttachmentStoreSpecificTest, MismatchedCrcInId) { | |
357 // Create attachment store. | |
358 AttachmentStore::Result create_result = AttachmentStore::UNSPECIFIED_ERROR; | |
359 store_ = AttachmentStore::CreateOnDiskStore( | |
360 temp_dir_.path(), base::ThreadTaskRunnerHandle::Get(), | |
361 base::Bind(&AttachmentStoreCreated, &create_result)); | |
362 | |
363 AttachmentStore::Result write_result = AttachmentStore::UNSPECIFIED_ERROR; | |
364 scoped_refptr<base::RefCountedString> some_data(new base::RefCountedString()); | |
365 some_data->data() = "data1"; | |
366 Attachment attachment = Attachment::Create(some_data); | |
367 AttachmentList attachments; | |
368 attachments.push_back(attachment); | |
369 store_->Write(attachments, | |
370 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResult, | |
371 base::Unretained(this), &write_result)); | |
372 | |
373 // Read, but with the wrong crc32c in the id. | |
374 AttachmentStore::Result read_result = AttachmentStore::SUCCESS; | |
375 | |
376 AttachmentId id_with_bad_crc32c = | |
377 AttachmentId::Create(attachment.GetId().GetSize(), 12345); | |
378 AttachmentIdList attachment_ids; | |
379 attachment_ids.push_back(id_with_bad_crc32c); | |
380 AttachmentIdList failed_attachment_ids; | |
381 store_->Read( | |
382 attachment_ids, | |
383 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResultAttachments, | |
384 base::Unretained(this), &read_result, &failed_attachment_ids)); | |
385 RunLoop(); | |
386 EXPECT_EQ(AttachmentStore::SUCCESS, create_result); | |
387 EXPECT_EQ(AttachmentStore::SUCCESS, write_result); | |
388 EXPECT_EQ(AttachmentStore::UNSPECIFIED_ERROR, read_result); | |
389 EXPECT_THAT(failed_attachment_ids, testing::ElementsAre(id_with_bad_crc32c)); | |
390 } | |
391 | |
392 // Ensure that after store initialization failure ReadWrite/Drop operations fail | 351 // Ensure that after store initialization failure ReadWrite/Drop operations fail |
393 // with correct error. | 352 // with correct error. |
394 TEST_F(OnDiskAttachmentStoreSpecificTest, OpsAfterInitializationFailed) { | 353 TEST_F(OnDiskAttachmentStoreSpecificTest, OpsAfterInitializationFailed) { |
395 // To simulate corrupt database write empty CURRENT file. | 354 // To simulate corrupt database write empty CURRENT file. |
396 std::string current_file_content = ""; | 355 std::string current_file_content = ""; |
397 base::WriteFile(db_path_.Append(FILE_PATH_LITERAL("CURRENT")), | 356 base::WriteFile(db_path_.Append(FILE_PATH_LITERAL("CURRENT")), |
398 current_file_content.c_str(), current_file_content.size()); | 357 current_file_content.c_str(), current_file_content.size()); |
399 | 358 |
400 AttachmentStore::Result create_result = AttachmentStore::SUCCESS; | 359 AttachmentStore::Result create_result = AttachmentStore::SUCCESS; |
401 store_ = AttachmentStore::CreateOnDiskStore( | 360 store_ = AttachmentStore::CreateOnDiskStore( |
402 temp_dir_.path(), base::ThreadTaskRunnerHandle::Get(), | 361 temp_dir_.path(), base::ThreadTaskRunnerHandle::Get(), |
403 base::Bind(&AttachmentStoreCreated, &create_result)); | 362 base::Bind(&AttachmentStoreCreated, &create_result)); |
404 | 363 |
405 // Reading from uninitialized store should result in | 364 // Reading from uninitialized store should result in |
406 // STORE_INITIALIZATION_FAILED. | 365 // STORE_INITIALIZATION_FAILED. |
407 AttachmentStore::Result read_result = AttachmentStore::SUCCESS; | 366 AttachmentStore::Result read_result = AttachmentStore::SUCCESS; |
408 AttachmentIdList attachment_ids; | 367 AttachmentIdList attachment_ids; |
409 std::string some_data("data1"); | 368 attachment_ids.push_back(AttachmentId::Create()); |
410 Attachment attachment = | |
411 Attachment::Create(base::RefCountedString::TakeString(&some_data)); | |
412 attachment_ids.push_back(attachment.GetId()); | |
413 AttachmentIdList failed_attachment_ids; | 369 AttachmentIdList failed_attachment_ids; |
414 store_->Read( | 370 store_->Read( |
415 attachment_ids, | 371 attachment_ids, |
416 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResultAttachments, | 372 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResultAttachments, |
417 base::Unretained(this), &read_result, &failed_attachment_ids)); | 373 base::Unretained(this), &read_result, &failed_attachment_ids)); |
418 | 374 |
419 // Dropping from uninitialized store should result in | 375 // Dropping from uninitialized store should result in |
420 // STORE_INITIALIZATION_FAILED. | 376 // STORE_INITIALIZATION_FAILED. |
421 AttachmentStore::Result drop_result = AttachmentStore::SUCCESS; | 377 AttachmentStore::Result drop_result = AttachmentStore::SUCCESS; |
422 store_->Drop(attachment_ids, | 378 store_->Drop(attachment_ids, |
423 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResult, | 379 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResult, |
424 base::Unretained(this), &drop_result)); | 380 base::Unretained(this), &drop_result)); |
425 | 381 |
426 // Writing to uninitialized store should result in | 382 // Writing to uninitialized store should result in |
427 // STORE_INITIALIZATION_FAILED. | 383 // STORE_INITIALIZATION_FAILED. |
428 AttachmentStore::Result write_result = AttachmentStore::SUCCESS; | 384 AttachmentStore::Result write_result = AttachmentStore::SUCCESS; |
| 385 std::string some_data; |
429 AttachmentList attachments; | 386 AttachmentList attachments; |
430 attachments.push_back(attachment); | 387 some_data = "data1"; |
| 388 attachments.push_back( |
| 389 Attachment::Create(base::RefCountedString::TakeString(&some_data))); |
431 store_->Write(attachments, | 390 store_->Write(attachments, |
432 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResult, | 391 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResult, |
433 base::Unretained(this), &write_result)); | 392 base::Unretained(this), &write_result)); |
434 | 393 |
435 RunLoop(); | 394 RunLoop(); |
436 EXPECT_EQ(AttachmentStore::UNSPECIFIED_ERROR, create_result); | 395 EXPECT_EQ(AttachmentStore::UNSPECIFIED_ERROR, create_result); |
437 EXPECT_EQ(AttachmentStore::STORE_INITIALIZATION_FAILED, read_result); | 396 EXPECT_EQ(AttachmentStore::STORE_INITIALIZATION_FAILED, read_result); |
438 EXPECT_THAT(failed_attachment_ids, testing::ElementsAre(attachment_ids[0])); | 397 EXPECT_THAT(failed_attachment_ids, testing::ElementsAre(attachment_ids[0])); |
439 EXPECT_EQ(AttachmentStore::STORE_INITIALIZATION_FAILED, drop_result); | 398 EXPECT_EQ(AttachmentStore::STORE_INITIALIZATION_FAILED, drop_result); |
440 EXPECT_EQ(AttachmentStore::STORE_INITIALIZATION_FAILED, write_result); | 399 EXPECT_EQ(AttachmentStore::STORE_INITIALIZATION_FAILED, write_result); |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
509 store_->ReadAllMetadata( | 468 store_->ReadAllMetadata( |
510 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResultMetadata, | 469 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResultMetadata, |
511 base::Unretained(this), &metadata_result, &metadata_list)); | 470 base::Unretained(this), &metadata_result, &metadata_list)); |
512 RunLoop(); | 471 RunLoop(); |
513 EXPECT_EQ(AttachmentStore::SUCCESS, create_result); | 472 EXPECT_EQ(AttachmentStore::SUCCESS, create_result); |
514 EXPECT_EQ(AttachmentStore::UNSPECIFIED_ERROR, metadata_result); | 473 EXPECT_EQ(AttachmentStore::UNSPECIFIED_ERROR, metadata_result); |
515 EXPECT_EQ(2U, metadata_list->size()); | 474 EXPECT_EQ(2U, metadata_list->size()); |
516 } | 475 } |
517 | 476 |
518 } // namespace syncer | 477 } // namespace syncer |
OLD | NEW |