| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/browser/download/base_file.h" | 5 #include "content/browser/download/base_file.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 // Make sure the mock BrowserThread outlives the BaseFile to satisfy | 81 // Make sure the mock BrowserThread outlives the BaseFile to satisfy |
| 82 // thread checks inside it. | 82 // thread checks inside it. |
| 83 base_file_.reset(); | 83 base_file_.reset(); |
| 84 | 84 |
| 85 EXPECT_EQ(expect_file_survives_, base::PathExists(full_path)); | 85 EXPECT_EQ(expect_file_survives_, base::PathExists(full_path)); |
| 86 } | 86 } |
| 87 | 87 |
| 88 bool InitializeFile() { | 88 bool InitializeFile() { |
| 89 DownloadInterruptReason result = base_file_->Initialize( | 89 DownloadInterruptReason result = base_file_->Initialize( |
| 90 base::FilePath(), temp_dir_.GetPath(), base::File(), 0, std::string(), | 90 base::FilePath(), temp_dir_.GetPath(), base::File(), 0, std::string(), |
| 91 std::unique_ptr<crypto::SecureHash>()); | 91 std::unique_ptr<crypto::SecureHash>(), BaseFile::EXCLUSIVE); |
| 92 EXPECT_EQ(expected_error_, result); | 92 EXPECT_EQ(expected_error_, result); |
| 93 return result == DOWNLOAD_INTERRUPT_REASON_NONE; | 93 return result == DOWNLOAD_INTERRUPT_REASON_NONE; |
| 94 } | 94 } |
| 95 | 95 |
| 96 bool AppendDataToFile(const std::string& data) { | 96 bool AppendDataToFile(const std::string& data) { |
| 97 EXPECT_EQ(expect_in_progress_, base_file_->in_progress()); | 97 EXPECT_EQ(expect_in_progress_, base_file_->in_progress()); |
| 98 DownloadInterruptReason result = | 98 DownloadInterruptReason result = |
| 99 base_file_->AppendDataToFile(data.data(), data.size()); | 99 base_file_->AppendDataToFile(data.data(), data.size()); |
| 100 if (result == DOWNLOAD_INTERRUPT_REASON_NONE) | 100 if (result == DOWNLOAD_INTERRUPT_REASON_NONE) |
| 101 EXPECT_TRUE(expect_in_progress_) << " result = " << result; | 101 EXPECT_TRUE(expect_in_progress_) << " result = " << result; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 115 | 115 |
| 116 // Helper functions. | 116 // Helper functions. |
| 117 // Create a file. Returns the complete file path. | 117 // Create a file. Returns the complete file path. |
| 118 base::FilePath CreateTestFile() { | 118 base::FilePath CreateTestFile() { |
| 119 base::FilePath file_name; | 119 base::FilePath file_name; |
| 120 BaseFile file((net::NetLogWithSource())); | 120 BaseFile file((net::NetLogWithSource())); |
| 121 | 121 |
| 122 EXPECT_EQ( | 122 EXPECT_EQ( |
| 123 DOWNLOAD_INTERRUPT_REASON_NONE, | 123 DOWNLOAD_INTERRUPT_REASON_NONE, |
| 124 file.Initialize(base::FilePath(), temp_dir_.GetPath(), base::File(), 0, | 124 file.Initialize(base::FilePath(), temp_dir_.GetPath(), base::File(), 0, |
| 125 std::string(), std::unique_ptr<crypto::SecureHash>())); | 125 std::string(), std::unique_ptr<crypto::SecureHash>(), |
| 126 BaseFile::EXCLUSIVE)); |
| 126 file_name = file.full_path(); | 127 file_name = file.full_path(); |
| 127 EXPECT_NE(base::FilePath::StringType(), file_name.value()); | 128 EXPECT_NE(base::FilePath::StringType(), file_name.value()); |
| 128 | 129 |
| 129 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | 130 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 130 file.AppendDataToFile(kTestData4, kTestDataLength4)); | 131 file.AppendDataToFile(kTestData4, kTestDataLength4)); |
| 131 | 132 |
| 132 // Keep the file from getting deleted when existing_file_name is deleted. | 133 // Keep the file from getting deleted when existing_file_name is deleted. |
| 133 file.Detach(); | 134 file.Detach(); |
| 134 | 135 |
| 135 return file_name; | 136 return file_name; |
| 136 } | 137 } |
| 137 | 138 |
| 138 // Create a file with the specified file name. | 139 // Create a file with the specified file name. |
| 139 void CreateFileWithName(const base::FilePath& file_name) { | 140 void CreateFileWithName(const base::FilePath& file_name) { |
| 140 EXPECT_NE(base::FilePath::StringType(), file_name.value()); | 141 EXPECT_NE(base::FilePath::StringType(), file_name.value()); |
| 141 BaseFile duplicate_file((net::NetLogWithSource())); | 142 BaseFile duplicate_file((net::NetLogWithSource())); |
| 142 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | 143 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 143 duplicate_file.Initialize(file_name, temp_dir_.GetPath(), | 144 duplicate_file.Initialize(file_name, temp_dir_.GetPath(), |
| 144 base::File(), 0, std::string(), | 145 base::File(), 0, std::string(), |
| 145 std::unique_ptr<crypto::SecureHash>())); | 146 std::unique_ptr<crypto::SecureHash>(), |
| 147 BaseFile::EXCLUSIVE)); |
| 146 // Write something into it. | 148 // Write something into it. |
| 147 duplicate_file.AppendDataToFile(kTestData4, kTestDataLength4); | 149 duplicate_file.AppendDataToFile(kTestData4, kTestDataLength4); |
| 148 // Detach the file so it isn't deleted on destruction of |duplicate_file|. | 150 // Detach the file so it isn't deleted on destruction of |duplicate_file|. |
| 149 duplicate_file.Detach(); | 151 duplicate_file.Detach(); |
| 150 } | 152 } |
| 151 | 153 |
| 152 int64_t CurrentSpeedAtTime(base::TimeTicks current_time) { | 154 int64_t CurrentSpeedAtTime(base::TimeTicks current_time) { |
| 153 EXPECT_TRUE(base_file_.get()); | 155 EXPECT_TRUE(base_file_.get()); |
| 154 return base_file_->CurrentSpeedAtTime(current_time); | 156 return base_file_->CurrentSpeedAtTime(current_time); |
| 155 } | 157 } |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 289 ASSERT_TRUE(base::CopyFile(base_file_->full_path(), new_file_path)); | 291 ASSERT_TRUE(base::CopyFile(base_file_->full_path(), new_file_path)); |
| 290 | 292 |
| 291 // Create another file | 293 // Create another file |
| 292 BaseFile second_file((net::NetLogWithSource())); | 294 BaseFile second_file((net::NetLogWithSource())); |
| 293 ASSERT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | 295 ASSERT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 294 second_file.Initialize(new_file_path, | 296 second_file.Initialize(new_file_path, |
| 295 base::FilePath(), | 297 base::FilePath(), |
| 296 base::File(), | 298 base::File(), |
| 297 base_file_->bytes_so_far(), | 299 base_file_->bytes_so_far(), |
| 298 std::string(), | 300 std::string(), |
| 299 std::move(hash_state))); | 301 std::move(hash_state), |
| 302 BaseFile::EXCLUSIVE)); |
| 300 std::string data(kTestData3); | 303 std::string data(kTestData3); |
| 301 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | 304 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 302 second_file.AppendDataToFile(data.data(), data.size())); | 305 second_file.AppendDataToFile(data.data(), data.size())); |
| 303 ExpectHashValue(kHashOfTestData1To3, second_file.Finish()); | 306 ExpectHashValue(kHashOfTestData1To3, second_file.Finish()); |
| 304 } | 307 } |
| 305 | 308 |
| 306 // Rename the file after all writes to it. | 309 // Rename the file after all writes to it. |
| 307 TEST_F(BaseFileTest, WriteThenRename) { | 310 TEST_F(BaseFileTest, WriteThenRename) { |
| 308 ASSERT_TRUE(InitializeFile()); | 311 ASSERT_TRUE(InitializeFile()); |
| 309 | 312 |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 411 base::FilePath path; | 414 base::FilePath path; |
| 412 ASSERT_TRUE(base::CreateTemporaryFile(&path)); | 415 ASSERT_TRUE(base::CreateTemporaryFile(&path)); |
| 413 | 416 |
| 414 // Pass a file handle which was opened without the WRITE flag. | 417 // Pass a file handle which was opened without the WRITE flag. |
| 415 // This should result in an error when writing. | 418 // This should result in an error when writing. |
| 416 base::File file(path, base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ); | 419 base::File file(path, base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ); |
| 417 base_file_.reset(new BaseFile(net::NetLogWithSource())); | 420 base_file_.reset(new BaseFile(net::NetLogWithSource())); |
| 418 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | 421 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 419 base_file_->Initialize(path, base::FilePath(), std::move(file), 0, | 422 base_file_->Initialize(path, base::FilePath(), std::move(file), 0, |
| 420 std::string(), | 423 std::string(), |
| 421 std::unique_ptr<crypto::SecureHash>())); | 424 std::unique_ptr<crypto::SecureHash>(), |
| 425 BaseFile::EXCLUSIVE)); |
| 422 #if defined(OS_WIN) | 426 #if defined(OS_WIN) |
| 423 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); | 427 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); |
| 424 #elif defined (OS_POSIX) | 428 #elif defined (OS_POSIX) |
| 425 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); | 429 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); |
| 426 #endif | 430 #endif |
| 427 ASSERT_FALSE(AppendDataToFile(kTestData1)); | 431 ASSERT_FALSE(AppendDataToFile(kTestData1)); |
| 428 base_file_->Finish(); | 432 base_file_->Finish(); |
| 429 } | 433 } |
| 430 | 434 |
| 431 // Try to write to uninitialized file. | 435 // Try to write to uninitialized file. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 453 // Create a new file. | 457 // Create a new file. |
| 454 base::FilePath existing_file_name = CreateTestFile(); | 458 base::FilePath existing_file_name = CreateTestFile(); |
| 455 set_expected_data(kTestData4); | 459 set_expected_data(kTestData4); |
| 456 | 460 |
| 457 // Use the file we've just created. | 461 // Use the file we've just created. |
| 458 base_file_.reset(new BaseFile(net::NetLogWithSource())); | 462 base_file_.reset(new BaseFile(net::NetLogWithSource())); |
| 459 ASSERT_EQ( | 463 ASSERT_EQ( |
| 460 DOWNLOAD_INTERRUPT_REASON_NONE, | 464 DOWNLOAD_INTERRUPT_REASON_NONE, |
| 461 base_file_->Initialize(existing_file_name, base::FilePath(), base::File(), | 465 base_file_->Initialize(existing_file_name, base::FilePath(), base::File(), |
| 462 kTestDataLength4, std::string(), | 466 kTestDataLength4, std::string(), |
| 463 std::unique_ptr<crypto::SecureHash>())); | 467 std::unique_ptr<crypto::SecureHash>(), |
| 468 BaseFile::EXCLUSIVE)); |
| 464 | 469 |
| 465 const base::FilePath file_name = base_file_->full_path(); | 470 const base::FilePath file_name = base_file_->full_path(); |
| 466 EXPECT_NE(base::FilePath::StringType(), file_name.value()); | 471 EXPECT_NE(base::FilePath::StringType(), file_name.value()); |
| 467 | 472 |
| 468 // Write into the file. | 473 // Write into the file. |
| 469 EXPECT_TRUE(AppendDataToFile(kTestData1)); | 474 EXPECT_TRUE(AppendDataToFile(kTestData1)); |
| 470 | 475 |
| 471 base_file_->Finish(); | 476 base_file_->Finish(); |
| 472 base_file_->Detach(); | 477 base_file_->Detach(); |
| 473 expect_file_survives_ = true; | 478 expect_file_survives_ = true; |
| 474 } | 479 } |
| 475 | 480 |
| 476 // Create a read-only file and attempt to write to it. | 481 // Create a read-only file and attempt to write to it. |
| 477 TEST_F(BaseFileTest, ReadonlyBaseFile) { | 482 TEST_F(BaseFileTest, ReadonlyBaseFile) { |
| 478 // Create a new file. | 483 // Create a new file. |
| 479 base::FilePath readonly_file_name = CreateTestFile(); | 484 base::FilePath readonly_file_name = CreateTestFile(); |
| 480 | 485 |
| 481 // Restore permissions to the file when we are done with this test. | 486 // Restore permissions to the file when we are done with this test. |
| 482 base::FilePermissionRestorer restore_permissions(readonly_file_name); | 487 base::FilePermissionRestorer restore_permissions(readonly_file_name); |
| 483 | 488 |
| 484 // Make it read-only. | 489 // Make it read-only. |
| 485 EXPECT_TRUE(base::MakeFileUnwritable(readonly_file_name)); | 490 EXPECT_TRUE(base::MakeFileUnwritable(readonly_file_name)); |
| 486 | 491 |
| 487 // Try to overwrite it. | 492 // Try to overwrite it. |
| 488 base_file_.reset(new BaseFile(net::NetLogWithSource())); | 493 base_file_.reset(new BaseFile(net::NetLogWithSource())); |
| 489 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED, | 494 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED, |
| 490 base_file_->Initialize(readonly_file_name, base::FilePath(), | 495 base_file_->Initialize(readonly_file_name, base::FilePath(), |
| 491 base::File(), 0, std::string(), | 496 base::File(), 0, std::string(), |
| 492 std::unique_ptr<crypto::SecureHash>())); | 497 std::unique_ptr<crypto::SecureHash>(), |
| 498 BaseFile::EXCLUSIVE)); |
| 493 | 499 |
| 494 expect_in_progress_ = false; | 500 expect_in_progress_ = false; |
| 495 | 501 |
| 496 const base::FilePath file_name = base_file_->full_path(); | 502 const base::FilePath file_name = base_file_->full_path(); |
| 497 EXPECT_NE(base::FilePath::StringType(), file_name.value()); | 503 EXPECT_NE(base::FilePath::StringType(), file_name.value()); |
| 498 | 504 |
| 499 // Write into the file. | 505 // Write into the file. |
| 500 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); | 506 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); |
| 501 EXPECT_FALSE(AppendDataToFile(kTestData1)); | 507 EXPECT_FALSE(AppendDataToFile(kTestData1)); |
| 502 | 508 |
| 503 base_file_->Finish(); | 509 base_file_->Finish(); |
| 504 base_file_->Detach(); | 510 base_file_->Detach(); |
| 505 expect_file_survives_ = true; | 511 expect_file_survives_ = true; |
| 506 } | 512 } |
| 507 | 513 |
| 508 // Open an existing file and continue writing to it. The hash of the partial | 514 // Open an existing file and continue writing to it. The hash of the partial |
| 509 // file is known and matches the existing contents. | 515 // file is known and matches the existing contents. |
| 510 TEST_F(BaseFileTest, ExistingBaseFileKnownHash) { | 516 TEST_F(BaseFileTest, ExistingBaseFileKnownHash) { |
| 511 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing"); | 517 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing"); |
| 512 ASSERT_EQ(kTestDataLength1, | 518 ASSERT_EQ(kTestDataLength1, |
| 513 base::WriteFile(file_path, kTestData1, kTestDataLength1)); | 519 base::WriteFile(file_path, kTestData1, kTestDataLength1)); |
| 514 | 520 |
| 515 std::string hash_so_far(std::begin(kHashOfTestData1), | 521 std::string hash_so_far(std::begin(kHashOfTestData1), |
| 516 std::end(kHashOfTestData1)); | 522 std::end(kHashOfTestData1)); |
| 517 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | 523 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 518 base_file_->Initialize(file_path, base::FilePath(), base::File(), | 524 base_file_->Initialize(file_path, base::FilePath(), base::File(), |
| 519 kTestDataLength1, hash_so_far, | 525 kTestDataLength1, hash_so_far, |
| 520 std::unique_ptr<crypto::SecureHash>())); | 526 std::unique_ptr<crypto::SecureHash>(), |
| 527 BaseFile::EXCLUSIVE)); |
| 521 set_expected_data(kTestData1); | 528 set_expected_data(kTestData1); |
| 522 ASSERT_TRUE(AppendDataToFile(kTestData2)); | 529 ASSERT_TRUE(AppendDataToFile(kTestData2)); |
| 523 ASSERT_TRUE(AppendDataToFile(kTestData3)); | 530 ASSERT_TRUE(AppendDataToFile(kTestData3)); |
| 524 ExpectHashValue(kHashOfTestData1To3, base_file_->Finish()); | 531 ExpectHashValue(kHashOfTestData1To3, base_file_->Finish()); |
| 525 } | 532 } |
| 526 | 533 |
| 527 // Open an existing file and continue writing to it. The hash of the partial | 534 // Open an existing file and continue writing to it. The hash of the partial |
| 528 // file is unknown. | 535 // file is unknown. |
| 529 TEST_F(BaseFileTest, ExistingBaseFileUnknownHash) { | 536 TEST_F(BaseFileTest, ExistingBaseFileUnknownHash) { |
| 530 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing"); | 537 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing"); |
| 531 ASSERT_EQ(kTestDataLength1, | 538 ASSERT_EQ(kTestDataLength1, |
| 532 base::WriteFile(file_path, kTestData1, kTestDataLength1)); | 539 base::WriteFile(file_path, kTestData1, kTestDataLength1)); |
| 533 | 540 |
| 534 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | 541 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 535 base_file_->Initialize(file_path, base::FilePath(), base::File(), | 542 base_file_->Initialize(file_path, base::FilePath(), base::File(), |
| 536 kTestDataLength1, std::string(), | 543 kTestDataLength1, std::string(), |
| 537 std::unique_ptr<crypto::SecureHash>())); | 544 std::unique_ptr<crypto::SecureHash>(), |
| 545 BaseFile::EXCLUSIVE)); |
| 538 set_expected_data(kTestData1); | 546 set_expected_data(kTestData1); |
| 539 ASSERT_TRUE(AppendDataToFile(kTestData2)); | 547 ASSERT_TRUE(AppendDataToFile(kTestData2)); |
| 540 ASSERT_TRUE(AppendDataToFile(kTestData3)); | 548 ASSERT_TRUE(AppendDataToFile(kTestData3)); |
| 541 ExpectHashValue(kHashOfTestData1To3, base_file_->Finish()); | 549 ExpectHashValue(kHashOfTestData1To3, base_file_->Finish()); |
| 542 } | 550 } |
| 543 | 551 |
| 544 // Open an existing file. The contentsof the file doesn't match the known hash. | 552 // Open an existing file. The contentsof the file doesn't match the known hash. |
| 545 TEST_F(BaseFileTest, ExistingBaseFileIncorrectHash) { | 553 TEST_F(BaseFileTest, ExistingBaseFileIncorrectHash) { |
| 546 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing"); | 554 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing"); |
| 547 ASSERT_EQ(kTestDataLength2, | 555 ASSERT_EQ(kTestDataLength2, |
| 548 base::WriteFile(file_path, kTestData2, kTestDataLength2)); | 556 base::WriteFile(file_path, kTestData2, kTestDataLength2)); |
| 549 | 557 |
| 550 std::string hash_so_far(std::begin(kHashOfTestData1), | 558 std::string hash_so_far(std::begin(kHashOfTestData1), |
| 551 std::end(kHashOfTestData1)); | 559 std::end(kHashOfTestData1)); |
| 552 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_HASH_MISMATCH, | 560 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_HASH_MISMATCH, |
| 553 base_file_->Initialize(file_path, base::FilePath(), base::File(), | 561 base_file_->Initialize(file_path, base::FilePath(), base::File(), |
| 554 kTestDataLength2, hash_so_far, | 562 kTestDataLength2, hash_so_far, |
| 555 std::unique_ptr<crypto::SecureHash>())); | 563 std::unique_ptr<crypto::SecureHash>(), |
| 564 BaseFile::EXCLUSIVE)); |
| 556 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_HASH_MISMATCH); | 565 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_HASH_MISMATCH); |
| 557 } | 566 } |
| 558 | 567 |
| 559 // Open a large existing file with a known hash and continue writing to it. | 568 // Open a large existing file with a known hash and continue writing to it. |
| 560 TEST_F(BaseFileTest, ExistingBaseFileLargeSizeKnownHash) { | 569 TEST_F(BaseFileTest, ExistingBaseFileLargeSizeKnownHash) { |
| 561 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing"); | 570 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing"); |
| 562 std::string big_buffer(1024 * 200, 'a'); | 571 std::string big_buffer(1024 * 200, 'a'); |
| 563 ASSERT_EQ(static_cast<int>(big_buffer.size()), | 572 ASSERT_EQ(static_cast<int>(big_buffer.size()), |
| 564 base::WriteFile(file_path, big_buffer.data(), big_buffer.size())); | 573 base::WriteFile(file_path, big_buffer.data(), big_buffer.size())); |
| 565 | 574 |
| 566 // Hash of partial file (1024*200 * 'a') | 575 // Hash of partial file (1024*200 * 'a') |
| 567 const uint8_t kExpectedPartialHash[] = { | 576 const uint8_t kExpectedPartialHash[] = { |
| 568 0x4b, 0x4f, 0x0f, 0x46, 0xac, 0x02, 0xd1, 0x77, 0xde, 0xa0, 0xab, | 577 0x4b, 0x4f, 0x0f, 0x46, 0xac, 0x02, 0xd1, 0x77, 0xde, 0xa0, 0xab, |
| 569 0x36, 0xa6, 0x6a, 0x65, 0x78, 0x40, 0xe2, 0xfb, 0x98, 0xb2, 0x0b, | 578 0x36, 0xa6, 0x6a, 0x65, 0x78, 0x40, 0xe2, 0xfb, 0x98, 0xb2, 0x0b, |
| 570 0xb2, 0x7a, 0x68, 0x8d, 0xb4, 0xd8, 0xea, 0x9c, 0xd2, 0x2c}; | 579 0xb2, 0x7a, 0x68, 0x8d, 0xb4, 0xd8, 0xea, 0x9c, 0xd2, 0x2c}; |
| 571 | 580 |
| 572 // Hash of entire file (1024*400 * 'a') | 581 // Hash of entire file (1024*400 * 'a') |
| 573 const uint8_t kExpectedFullHash[] = { | 582 const uint8_t kExpectedFullHash[] = { |
| 574 0x0c, 0xe9, 0xf6, 0x78, 0x6b, 0x0f, 0x58, 0x49, 0x36, 0xe8, 0x83, | 583 0x0c, 0xe9, 0xf6, 0x78, 0x6b, 0x0f, 0x58, 0x49, 0x36, 0xe8, 0x83, |
| 575 0xc5, 0x09, 0x16, 0xbc, 0x5e, 0x2d, 0x07, 0x95, 0xb9, 0x42, 0x20, | 584 0xc5, 0x09, 0x16, 0xbc, 0x5e, 0x2d, 0x07, 0x95, 0xb9, 0x42, 0x20, |
| 576 0x41, 0x7c, 0xb3, 0x38, 0xd3, 0xf4, 0xe0, 0x78, 0x89, 0x46}; | 585 0x41, 0x7c, 0xb3, 0x38, 0xd3, 0xf4, 0xe0, 0x78, 0x89, 0x46}; |
| 577 | 586 |
| 578 ASSERT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | 587 ASSERT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 579 base_file_->Initialize(file_path, base::FilePath(), base::File(), | 588 base_file_->Initialize(file_path, base::FilePath(), base::File(), |
| 580 big_buffer.size(), | 589 big_buffer.size(), |
| 581 std::string(std::begin(kExpectedPartialHash), | 590 std::string(std::begin(kExpectedPartialHash), |
| 582 std::end(kExpectedPartialHash)), | 591 std::end(kExpectedPartialHash)), |
| 583 std::unique_ptr<crypto::SecureHash>())); | 592 std::unique_ptr<crypto::SecureHash>(), |
| 593 BaseFile::EXCLUSIVE)); |
| 584 set_expected_data(big_buffer); // Contents of the file on Open. | 594 set_expected_data(big_buffer); // Contents of the file on Open. |
| 585 ASSERT_TRUE(AppendDataToFile(big_buffer)); | 595 ASSERT_TRUE(AppendDataToFile(big_buffer)); |
| 586 ExpectHashValue(kExpectedFullHash, base_file_->Finish()); | 596 ExpectHashValue(kExpectedFullHash, base_file_->Finish()); |
| 587 } | 597 } |
| 588 | 598 |
| 589 // Open a large existing file. The contents doesn't match the known hash. | 599 // Open a large existing file. The contents doesn't match the known hash. |
| 590 TEST_F(BaseFileTest, ExistingBaseFileLargeSizeIncorrectHash) { | 600 TEST_F(BaseFileTest, ExistingBaseFileLargeSizeIncorrectHash) { |
| 591 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing"); | 601 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing"); |
| 592 std::string big_buffer(1024 * 200, 'a'); | 602 std::string big_buffer(1024 * 200, 'a'); |
| 593 ASSERT_EQ(static_cast<int>(big_buffer.size()), | 603 ASSERT_EQ(static_cast<int>(big_buffer.size()), |
| 594 base::WriteFile(file_path, big_buffer.data(), big_buffer.size())); | 604 base::WriteFile(file_path, big_buffer.data(), big_buffer.size())); |
| 595 | 605 |
| 596 // Incorrect hash of partial file (1024*200 * 'a') | 606 // Incorrect hash of partial file (1024*200 * 'a') |
| 597 const uint8_t kExpectedPartialHash[] = { | 607 const uint8_t kExpectedPartialHash[] = { |
| 598 0xc2, 0xa9, 0x08, 0xd9, 0x8f, 0x5d, 0xf9, 0x87, 0xad, 0xe4, 0x1b, | 608 0xc2, 0xa9, 0x08, 0xd9, 0x8f, 0x5d, 0xf9, 0x87, 0xad, 0xe4, 0x1b, |
| 599 0x5f, 0xce, 0x21, 0x30, 0x67, 0xef, 0x6c, 0xc2, 0x1e, 0xf2, 0x24, | 609 0x5f, 0xce, 0x21, 0x30, 0x67, 0xef, 0x6c, 0xc2, 0x1e, 0xf2, 0x24, |
| 600 0x02, 0x12, 0xa4, 0x1e, 0x54, 0xb5, 0xe7, 0xc2, 0x8a, 0xe5}; | 610 0x02, 0x12, 0xa4, 0x1e, 0x54, 0xb5, 0xe7, 0xc2, 0x8a, 0xe5}; |
| 601 | 611 |
| 602 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_HASH_MISMATCH, | 612 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_HASH_MISMATCH, |
| 603 base_file_->Initialize(file_path, base::FilePath(), base::File(), | 613 base_file_->Initialize(file_path, base::FilePath(), base::File(), |
| 604 big_buffer.size(), | 614 big_buffer.size(), |
| 605 std::string(std::begin(kExpectedPartialHash), | 615 std::string(std::begin(kExpectedPartialHash), |
| 606 std::end(kExpectedPartialHash)), | 616 std::end(kExpectedPartialHash)), |
| 607 std::unique_ptr<crypto::SecureHash>())); | 617 std::unique_ptr<crypto::SecureHash>(), |
| 618 BaseFile::EXCLUSIVE)); |
| 608 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_HASH_MISMATCH); | 619 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_HASH_MISMATCH); |
| 609 } | 620 } |
| 610 | 621 |
| 611 // Open an existing file. The size of the file is too short. | 622 // Open an existing file. The size of the file is too short. |
| 612 TEST_F(BaseFileTest, ExistingBaseFileTooShort) { | 623 TEST_F(BaseFileTest, ExistingBaseFileTooShort) { |
| 613 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing"); | 624 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing"); |
| 614 ASSERT_EQ(kTestDataLength1, | 625 ASSERT_EQ(kTestDataLength1, |
| 615 base::WriteFile(file_path, kTestData1, kTestDataLength1)); | 626 base::WriteFile(file_path, kTestData1, kTestDataLength1)); |
| 616 | 627 |
| 617 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_TOO_SHORT, | 628 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_TOO_SHORT, |
| 618 base_file_->Initialize(file_path, base::FilePath(), base::File(), | 629 base_file_->Initialize(file_path, base::FilePath(), base::File(), |
| 619 kTestDataLength1 + 1, std::string(), | 630 kTestDataLength1 + 1, std::string(), |
| 620 std::unique_ptr<crypto::SecureHash>())); | 631 std::unique_ptr<crypto::SecureHash>(), |
| 632 BaseFile::EXCLUSIVE)); |
| 621 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_TOO_SHORT); | 633 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_TOO_SHORT); |
| 622 } | 634 } |
| 623 | 635 |
| 624 // Open an existing file. The size is larger than expected. | 636 // Open an existing file. The size is larger than expected. |
| 625 TEST_F(BaseFileTest, ExistingBaseFileKnownHashTooLong) { | 637 TEST_F(BaseFileTest, ExistingBaseFileKnownHashTooLong) { |
| 626 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing"); | 638 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing"); |
| 627 std::string contents; | 639 std::string contents; |
| 628 contents.append(kTestData1); | 640 contents.append(kTestData1); |
| 629 contents.append("Something extra"); | 641 contents.append("Something extra"); |
| 630 ASSERT_EQ(static_cast<int>(contents.size()), | 642 ASSERT_EQ(static_cast<int>(contents.size()), |
| 631 base::WriteFile(file_path, contents.data(), contents.size())); | 643 base::WriteFile(file_path, contents.data(), contents.size())); |
| 632 | 644 |
| 633 std::string hash_so_far(std::begin(kHashOfTestData1), | 645 std::string hash_so_far(std::begin(kHashOfTestData1), |
| 634 std::end(kHashOfTestData1)); | 646 std::end(kHashOfTestData1)); |
| 635 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | 647 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 636 base_file_->Initialize(file_path, base::FilePath(), base::File(), | 648 base_file_->Initialize(file_path, base::FilePath(), base::File(), |
| 637 kTestDataLength1, hash_so_far, | 649 kTestDataLength1, hash_so_far, |
| 638 std::unique_ptr<crypto::SecureHash>())); | 650 std::unique_ptr<crypto::SecureHash>(), |
| 651 BaseFile::EXCLUSIVE)); |
| 639 set_expected_data(kTestData1); // Our starting position. | 652 set_expected_data(kTestData1); // Our starting position. |
| 640 ASSERT_TRUE(AppendDataToFile(kTestData2)); | 653 ASSERT_TRUE(AppendDataToFile(kTestData2)); |
| 641 ASSERT_TRUE(AppendDataToFile(kTestData3)); | 654 ASSERT_TRUE(AppendDataToFile(kTestData3)); |
| 642 ExpectHashValue(kHashOfTestData1To3, base_file_->Finish()); | 655 ExpectHashValue(kHashOfTestData1To3, base_file_->Finish()); |
| 643 } | 656 } |
| 644 | 657 |
| 645 // Open an existing file. The size is large than expected and the hash is | 658 // Open an existing file. The size is large than expected and the hash is |
| 646 // unknown. | 659 // unknown. |
| 647 TEST_F(BaseFileTest, ExistingBaseFileUnknownHashTooLong) { | 660 TEST_F(BaseFileTest, ExistingBaseFileUnknownHashTooLong) { |
| 648 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing"); | 661 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing"); |
| 649 std::string contents; | 662 std::string contents; |
| 650 contents.append(kTestData1); | 663 contents.append(kTestData1); |
| 651 contents.append("Something extra"); | 664 contents.append("Something extra"); |
| 652 ASSERT_EQ(static_cast<int>(contents.size()), | 665 ASSERT_EQ(static_cast<int>(contents.size()), |
| 653 base::WriteFile(file_path, contents.data(), contents.size())); | 666 base::WriteFile(file_path, contents.data(), contents.size())); |
| 654 | 667 |
| 655 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | 668 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 656 base_file_->Initialize(file_path, base::FilePath(), base::File(), | 669 base_file_->Initialize(file_path, base::FilePath(), base::File(), |
| 657 kTestDataLength1, std::string(), | 670 kTestDataLength1, std::string(), |
| 658 std::unique_ptr<crypto::SecureHash>())); | 671 std::unique_ptr<crypto::SecureHash>(), |
| 672 BaseFile::EXCLUSIVE)); |
| 659 set_expected_data(kTestData1); | 673 set_expected_data(kTestData1); |
| 660 ASSERT_TRUE(AppendDataToFile(kTestData2)); | 674 ASSERT_TRUE(AppendDataToFile(kTestData2)); |
| 661 ASSERT_TRUE(AppendDataToFile(kTestData3)); | 675 ASSERT_TRUE(AppendDataToFile(kTestData3)); |
| 662 ExpectHashValue(kHashOfTestData1To3, base_file_->Finish()); | 676 ExpectHashValue(kHashOfTestData1To3, base_file_->Finish()); |
| 663 } | 677 } |
| 664 | 678 |
| 665 // Similar to ExistingBaseFileKnownHashTooLong test, but with a file large | 679 // Similar to ExistingBaseFileKnownHashTooLong test, but with a file large |
| 666 // enough to requre multiple Read()s to complete. This provides additional code | 680 // enough to requre multiple Read()s to complete. This provides additional code |
| 667 // coverage for the CalculatePartialHash() logic. | 681 // coverage for the CalculatePartialHash() logic. |
| 668 TEST_F(BaseFileTest, ExistingBaseFileUnknownHashTooLongForLargeFile) { | 682 TEST_F(BaseFileTest, ExistingBaseFileUnknownHashTooLongForLargeFile) { |
| 669 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing"); | 683 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing"); |
| 670 const size_t kFileSize = 1024 * 1024; | 684 const size_t kFileSize = 1024 * 1024; |
| 671 const size_t kIntermediateSize = kFileSize / 2 + 111; | 685 const size_t kIntermediateSize = kFileSize / 2 + 111; |
| 672 // |contents| is 100 bytes longer than kIntermediateSize. The latter is the | 686 // |contents| is 100 bytes longer than kIntermediateSize. The latter is the |
| 673 // expected size. | 687 // expected size. |
| 674 std::string contents(kIntermediateSize + 100, 'a'); | 688 std::string contents(kIntermediateSize + 100, 'a'); |
| 675 ASSERT_EQ(static_cast<int>(contents.size()), | 689 ASSERT_EQ(static_cast<int>(contents.size()), |
| 676 base::WriteFile(file_path, contents.data(), contents.size())); | 690 base::WriteFile(file_path, contents.data(), contents.size())); |
| 677 | 691 |
| 678 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, | 692 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 679 base_file_->Initialize(file_path, base::FilePath(), base::File(), | 693 base_file_->Initialize(file_path, base::FilePath(), base::File(), |
| 680 kIntermediateSize, std::string(), | 694 kIntermediateSize, std::string(), |
| 681 std::unique_ptr<crypto::SecureHash>())); | 695 std::unique_ptr<crypto::SecureHash>(), |
| 696 BaseFile::EXCLUSIVE)); |
| 682 // The extra bytes should be stripped during Initialize(). | 697 // The extra bytes should be stripped during Initialize(). |
| 683 contents.resize(kIntermediateSize, 'a'); | 698 contents.resize(kIntermediateSize, 'a'); |
| 684 set_expected_data(contents); | 699 set_expected_data(contents); |
| 685 std::string new_data(kFileSize - kIntermediateSize, 'a'); | 700 std::string new_data(kFileSize - kIntermediateSize, 'a'); |
| 686 ASSERT_TRUE(AppendDataToFile(new_data)); | 701 ASSERT_TRUE(AppendDataToFile(new_data)); |
| 687 const uint8_t kExpectedHash[] = { | 702 const uint8_t kExpectedHash[] = { |
| 688 0x9b, 0xc1, 0xb2, 0xa2, 0x88, 0xb2, 0x6a, 0xf7, 0x25, 0x7a, 0x36, | 703 0x9b, 0xc1, 0xb2, 0xa2, 0x88, 0xb2, 0x6a, 0xf7, 0x25, 0x7a, 0x36, |
| 689 0x27, 0x7a, 0xe3, 0x81, 0x6a, 0x7d, 0x4f, 0x16, 0xe8, 0x9c, 0x1e, | 704 0x27, 0x7a, 0xe3, 0x81, 0x6a, 0x7d, 0x4f, 0x16, 0xe8, 0x9c, 0x1e, |
| 690 0x7e, 0x77, 0xd0, 0xa5, 0xc4, 0x8b, 0xad, 0x62, 0xb3, 0x60, | 705 0x7e, 0x77, 0xd0, 0xa5, 0xc4, 0x8b, 0xad, 0x62, 0xb3, 0x60, |
| 691 }; | 706 }; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 721 ASSERT_FALSE(base::PathExists(full_path)); | 736 ASSERT_FALSE(base::PathExists(full_path)); |
| 722 | 737 |
| 723 const char kData[] = "hello"; | 738 const char kData[] = "hello"; |
| 724 const int kDataLength = static_cast<int>(arraysize(kData) - 1); | 739 const int kDataLength = static_cast<int>(arraysize(kData) - 1); |
| 725 ASSERT_EQ(kDataLength, base::WriteFile(full_path, kData, kDataLength)); | 740 ASSERT_EQ(kDataLength, base::WriteFile(full_path, kData, kDataLength)); |
| 726 // The file that we created here should stick around when the BaseFile is | 741 // The file that we created here should stick around when the BaseFile is |
| 727 // destroyed during TearDown. | 742 // destroyed during TearDown. |
| 728 expect_file_survives_ = true; | 743 expect_file_survives_ = true; |
| 729 } | 744 } |
| 730 | 745 |
| 746 // Test that writing data to a particular offset works. |
| 747 TEST_F(BaseFileTest, WriteDataAtOffset) { |
| 748 base::FilePath file_path = temp_dir_.GetPath().AppendASCII("existing"); |
| 749 std::string contents = kTestData1; |
| 750 ASSERT_EQ(static_cast<int>(contents.size()), |
| 751 base::WriteFile(file_path, contents.data(), contents.size())); |
| 752 |
| 753 base_file_->Initialize(file_path, base::FilePath(), base::File(), |
| 754 kTestDataLength1, |
| 755 std::string(), |
| 756 std::unique_ptr<crypto::SecureHash>(), |
| 757 BaseFile::SHARED); |
| 758 // This will create a hole in the file. |
| 759 base_file_->WriteDataToFile(kTestDataLength1 + kTestDataLength2, |
| 760 kTestData4, |
| 761 kTestDataLength4); |
| 762 // This should fill the hole. |
| 763 base_file_->WriteDataToFile(kTestDataLength1, |
| 764 kTestData2, |
| 765 kTestDataLength2); |
| 766 set_expected_data(contents + kTestData2 + kTestData4); |
| 767 base_file_->Finish(); |
| 768 } |
| 769 |
| 731 } // namespace content | 770 } // namespace content |
| OLD | NEW |