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