Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(188)

Side by Side Diff: content/browser/download/base_file_unittest.cc

Issue 1887873002: [Downloads] BaseFile shouldn't read past the expected EOF for a stub file. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/browser/download/base_file.cc ('k') | content/browser/download/download_browsertest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 499 matching lines...) Expand 10 before | Expand all | Expand 10 after
510 510
511 base_file_->Finish(); 511 base_file_->Finish();
512 base_file_->Detach(); 512 base_file_->Detach();
513 expect_file_survives_ = true; 513 expect_file_survives_ = true;
514 } 514 }
515 515
516 // Open an existing file and continue writing to it. The hash of the partial 516 // Open an existing file and continue writing to it. The hash of the partial
517 // file is known and matches the existing contents. 517 // file is known and matches the existing contents.
518 TEST_F(BaseFileTest, ExistingBaseFileKnownHash) { 518 TEST_F(BaseFileTest, ExistingBaseFileKnownHash) {
519 base::FilePath file_path = temp_dir_.path().AppendASCII("existing"); 519 base::FilePath file_path = temp_dir_.path().AppendASCII("existing");
520 ASSERT_TRUE(base::WriteFile(file_path, kTestData1, kTestDataLength1)); 520 ASSERT_EQ(kTestDataLength1,
521 base::WriteFile(file_path, kTestData1, kTestDataLength1));
521 522
522 std::string hash_so_far(std::begin(kHashOfTestData1), 523 std::string hash_so_far(std::begin(kHashOfTestData1),
523 std::end(kHashOfTestData1)); 524 std::end(kHashOfTestData1));
524 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, 525 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE,
525 base_file_->Initialize(file_path, base::FilePath(), base::File(), 526 base_file_->Initialize(file_path, base::FilePath(), base::File(),
526 kTestDataLength1, hash_so_far, 527 kTestDataLength1, hash_so_far,
527 std::unique_ptr<crypto::SecureHash>())); 528 std::unique_ptr<crypto::SecureHash>()));
528 set_expected_data(kTestData1); 529 set_expected_data(kTestData1);
529 ASSERT_TRUE(AppendDataToFile(kTestData2)); 530 ASSERT_TRUE(AppendDataToFile(kTestData2));
530 ASSERT_TRUE(AppendDataToFile(kTestData3)); 531 ASSERT_TRUE(AppendDataToFile(kTestData3));
531 ExpectHashValue(kHashOfTestData1To3, base_file_->Finish()); 532 ExpectHashValue(kHashOfTestData1To3, base_file_->Finish());
532 } 533 }
533 534
534 // Open an existing file and continue writing to it. The hash of the partial 535 // Open an existing file and continue writing to it. The hash of the partial
535 // file is unknown. 536 // file is unknown.
536 TEST_F(BaseFileTest, ExistingBaseFileUnknownHash) { 537 TEST_F(BaseFileTest, ExistingBaseFileUnknownHash) {
537 base::FilePath file_path = temp_dir_.path().AppendASCII("existing"); 538 base::FilePath file_path = temp_dir_.path().AppendASCII("existing");
538 ASSERT_TRUE(base::WriteFile(file_path, kTestData1, kTestDataLength1)); 539 ASSERT_EQ(kTestDataLength1,
540 base::WriteFile(file_path, kTestData1, kTestDataLength1));
539 541
540 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, 542 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE,
541 base_file_->Initialize(file_path, base::FilePath(), base::File(), 543 base_file_->Initialize(file_path, base::FilePath(), base::File(),
542 kTestDataLength1, std::string(), 544 kTestDataLength1, std::string(),
543 std::unique_ptr<crypto::SecureHash>())); 545 std::unique_ptr<crypto::SecureHash>()));
544 set_expected_data(kTestData1); 546 set_expected_data(kTestData1);
545 ASSERT_TRUE(AppendDataToFile(kTestData2)); 547 ASSERT_TRUE(AppendDataToFile(kTestData2));
546 ASSERT_TRUE(AppendDataToFile(kTestData3)); 548 ASSERT_TRUE(AppendDataToFile(kTestData3));
547 ExpectHashValue(kHashOfTestData1To3, base_file_->Finish()); 549 ExpectHashValue(kHashOfTestData1To3, base_file_->Finish());
548 } 550 }
549 551
550 // 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.
551 TEST_F(BaseFileTest, ExistingBaseFileIncorrectHash) { 553 TEST_F(BaseFileTest, ExistingBaseFileIncorrectHash) {
552 base::FilePath file_path = temp_dir_.path().AppendASCII("existing"); 554 base::FilePath file_path = temp_dir_.path().AppendASCII("existing");
553 ASSERT_TRUE(base::WriteFile(file_path, kTestData2, kTestDataLength2)); 555 ASSERT_EQ(kTestDataLength2,
556 base::WriteFile(file_path, kTestData2, kTestDataLength2));
554 557
555 std::string hash_so_far(std::begin(kHashOfTestData1), 558 std::string hash_so_far(std::begin(kHashOfTestData1),
556 std::end(kHashOfTestData1)); 559 std::end(kHashOfTestData1));
557 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_HASH_MISMATCH, 560 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_HASH_MISMATCH,
558 base_file_->Initialize(file_path, base::FilePath(), base::File(), 561 base_file_->Initialize(file_path, base::FilePath(), base::File(),
559 kTestDataLength2, hash_so_far, 562 kTestDataLength2, hash_so_far,
560 std::unique_ptr<crypto::SecureHash>())); 563 std::unique_ptr<crypto::SecureHash>()));
561 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_HASH_MISMATCH); 564 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_HASH_MISMATCH);
562 } 565 }
563 566
564 // Open a large existing file with a known hash and continue writing to it. 567 // Open a large existing file with a known hash and continue writing to it.
565 TEST_F(BaseFileTest, ExistingBaseFileLargeSizeKnownHash) { 568 TEST_F(BaseFileTest, ExistingBaseFileLargeSizeKnownHash) {
566 base::FilePath file_path = temp_dir_.path().AppendASCII("existing"); 569 base::FilePath file_path = temp_dir_.path().AppendASCII("existing");
567 std::string big_buffer(1024 * 200, 'a'); 570 std::string big_buffer(1024 * 200, 'a');
568 ASSERT_TRUE(base::WriteFile(file_path, big_buffer.data(), big_buffer.size())); 571 ASSERT_EQ(static_cast<int>(big_buffer.size()),
572 base::WriteFile(file_path, big_buffer.data(), big_buffer.size()));
569 573
570 // Hash of partial file (1024*200 * 'a') 574 // Hash of partial file (1024*200 * 'a')
571 const uint8_t kExpectedPartialHash[] = { 575 const uint8_t kExpectedPartialHash[] = {
572 0x4b, 0x4f, 0x0f, 0x46, 0xac, 0x02, 0xd1, 0x77, 0xde, 0xa0, 0xab, 576 0x4b, 0x4f, 0x0f, 0x46, 0xac, 0x02, 0xd1, 0x77, 0xde, 0xa0, 0xab,
573 0x36, 0xa6, 0x6a, 0x65, 0x78, 0x40, 0xe2, 0xfb, 0x98, 0xb2, 0x0b, 577 0x36, 0xa6, 0x6a, 0x65, 0x78, 0x40, 0xe2, 0xfb, 0x98, 0xb2, 0x0b,
574 0xb2, 0x7a, 0x68, 0x8d, 0xb4, 0xd8, 0xea, 0x9c, 0xd2, 0x2c}; 578 0xb2, 0x7a, 0x68, 0x8d, 0xb4, 0xd8, 0xea, 0x9c, 0xd2, 0x2c};
575 579
576 // Hash of entire file (1024*400 * 'a') 580 // Hash of entire file (1024*400 * 'a')
577 const uint8_t kExpectedFullHash[] = { 581 const uint8_t kExpectedFullHash[] = {
578 0x0c, 0xe9, 0xf6, 0x78, 0x6b, 0x0f, 0x58, 0x49, 0x36, 0xe8, 0x83, 582 0x0c, 0xe9, 0xf6, 0x78, 0x6b, 0x0f, 0x58, 0x49, 0x36, 0xe8, 0x83,
579 0xc5, 0x09, 0x16, 0xbc, 0x5e, 0x2d, 0x07, 0x95, 0xb9, 0x42, 0x20, 583 0xc5, 0x09, 0x16, 0xbc, 0x5e, 0x2d, 0x07, 0x95, 0xb9, 0x42, 0x20,
580 0x41, 0x7c, 0xb3, 0x38, 0xd3, 0xf4, 0xe0, 0x78, 0x89, 0x46}; 584 0x41, 0x7c, 0xb3, 0x38, 0xd3, 0xf4, 0xe0, 0x78, 0x89, 0x46};
581 585
582 ASSERT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, 586 ASSERT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE,
583 base_file_->Initialize(file_path, base::FilePath(), base::File(), 587 base_file_->Initialize(file_path, base::FilePath(), base::File(),
584 big_buffer.size(), 588 big_buffer.size(),
585 std::string(std::begin(kExpectedPartialHash), 589 std::string(std::begin(kExpectedPartialHash),
586 std::end(kExpectedPartialHash)), 590 std::end(kExpectedPartialHash)),
587 std::unique_ptr<crypto::SecureHash>())); 591 std::unique_ptr<crypto::SecureHash>()));
588 set_expected_data(big_buffer); // Contents of the file on Open. 592 set_expected_data(big_buffer); // Contents of the file on Open.
589 ASSERT_TRUE(AppendDataToFile(big_buffer)); 593 ASSERT_TRUE(AppendDataToFile(big_buffer));
590 ExpectHashValue(kExpectedFullHash, base_file_->Finish()); 594 ExpectHashValue(kExpectedFullHash, base_file_->Finish());
591 } 595 }
592 596
593 // Open a large existing file. The contents doesn't match the known hash. 597 // Open a large existing file. The contents doesn't match the known hash.
594 TEST_F(BaseFileTest, ExistingBaseFileLargeSizeIncorrectHash) { 598 TEST_F(BaseFileTest, ExistingBaseFileLargeSizeIncorrectHash) {
595 base::FilePath file_path = temp_dir_.path().AppendASCII("existing"); 599 base::FilePath file_path = temp_dir_.path().AppendASCII("existing");
596 std::string big_buffer(1024 * 200, 'a'); 600 std::string big_buffer(1024 * 200, 'a');
597 ASSERT_TRUE(base::WriteFile(file_path, big_buffer.data(), big_buffer.size())); 601 ASSERT_EQ(static_cast<int>(big_buffer.size()),
602 base::WriteFile(file_path, big_buffer.data(), big_buffer.size()));
598 603
599 // Incorrect hash of partial file (1024*200 * 'a') 604 // Incorrect hash of partial file (1024*200 * 'a')
600 const uint8_t kExpectedPartialHash[] = { 605 const uint8_t kExpectedPartialHash[] = {
601 0xc2, 0xa9, 0x08, 0xd9, 0x8f, 0x5d, 0xf9, 0x87, 0xad, 0xe4, 0x1b, 606 0xc2, 0xa9, 0x08, 0xd9, 0x8f, 0x5d, 0xf9, 0x87, 0xad, 0xe4, 0x1b,
602 0x5f, 0xce, 0x21, 0x30, 0x67, 0xef, 0x6c, 0xc2, 0x1e, 0xf2, 0x24, 607 0x5f, 0xce, 0x21, 0x30, 0x67, 0xef, 0x6c, 0xc2, 0x1e, 0xf2, 0x24,
603 0x02, 0x12, 0xa4, 0x1e, 0x54, 0xb5, 0xe7, 0xc2, 0x8a, 0xe5}; 608 0x02, 0x12, 0xa4, 0x1e, 0x54, 0xb5, 0xe7, 0xc2, 0x8a, 0xe5};
604 609
605 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_HASH_MISMATCH, 610 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_HASH_MISMATCH,
606 base_file_->Initialize(file_path, base::FilePath(), base::File(), 611 base_file_->Initialize(file_path, base::FilePath(), base::File(),
607 big_buffer.size(), 612 big_buffer.size(),
608 std::string(std::begin(kExpectedPartialHash), 613 std::string(std::begin(kExpectedPartialHash),
609 std::end(kExpectedPartialHash)), 614 std::end(kExpectedPartialHash)),
610 std::unique_ptr<crypto::SecureHash>())); 615 std::unique_ptr<crypto::SecureHash>()));
611 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_HASH_MISMATCH); 616 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_HASH_MISMATCH);
612 } 617 }
613 618
614 // Open an existing file. The size of the file is too short. 619 // Open an existing file. The size of the file is too short.
615 TEST_F(BaseFileTest, ExistingBaseFileTooShort) { 620 TEST_F(BaseFileTest, ExistingBaseFileTooShort) {
616 base::FilePath file_path = temp_dir_.path().AppendASCII("existing"); 621 base::FilePath file_path = temp_dir_.path().AppendASCII("existing");
617 ASSERT_TRUE(base::WriteFile(file_path, kTestData1, kTestDataLength1)); 622 ASSERT_EQ(kTestDataLength1,
623 base::WriteFile(file_path, kTestData1, kTestDataLength1));
618 624
619 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_TOO_SHORT, 625 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_TOO_SHORT,
620 base_file_->Initialize(file_path, base::FilePath(), base::File(), 626 base_file_->Initialize(file_path, base::FilePath(), base::File(),
621 kTestDataLength1 + 1, std::string(), 627 kTestDataLength1 + 1, std::string(),
622 std::unique_ptr<crypto::SecureHash>())); 628 std::unique_ptr<crypto::SecureHash>()));
623 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_TOO_SHORT); 629 set_expected_error(DOWNLOAD_INTERRUPT_REASON_FILE_TOO_SHORT);
624 } 630 }
625 631
626 // Open an existing file. The size is larger than expected. 632 // Open an existing file. The size is larger than expected.
627 TEST_F(BaseFileTest, ExistingBaseFileKnownHashTooLong) { 633 TEST_F(BaseFileTest, ExistingBaseFileKnownHashTooLong) {
628 base::FilePath file_path = temp_dir_.path().AppendASCII("existing"); 634 base::FilePath file_path = temp_dir_.path().AppendASCII("existing");
629 std::string contents; 635 std::string contents;
630 contents.append(kTestData1); 636 contents.append(kTestData1);
631 contents.append("Something extra"); 637 contents.append("Something extra");
632 ASSERT_TRUE(base::WriteFile(file_path, contents.data(), contents.size())); 638 ASSERT_EQ(static_cast<int>(contents.size()),
639 base::WriteFile(file_path, contents.data(), contents.size()));
633 640
634 std::string hash_so_far(std::begin(kHashOfTestData1), 641 std::string hash_so_far(std::begin(kHashOfTestData1),
635 std::end(kHashOfTestData1)); 642 std::end(kHashOfTestData1));
636 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, 643 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE,
637 base_file_->Initialize(file_path, base::FilePath(), base::File(), 644 base_file_->Initialize(file_path, base::FilePath(), base::File(),
638 kTestDataLength1, hash_so_far, 645 kTestDataLength1, hash_so_far,
639 std::unique_ptr<crypto::SecureHash>())); 646 std::unique_ptr<crypto::SecureHash>()));
640 set_expected_data(kTestData1); // Our starting position. 647 set_expected_data(kTestData1); // Our starting position.
641 ASSERT_TRUE(AppendDataToFile(kTestData2)); 648 ASSERT_TRUE(AppendDataToFile(kTestData2));
642 ASSERT_TRUE(AppendDataToFile(kTestData3)); 649 ASSERT_TRUE(AppendDataToFile(kTestData3));
643 ExpectHashValue(kHashOfTestData1To3, base_file_->Finish()); 650 ExpectHashValue(kHashOfTestData1To3, base_file_->Finish());
644 } 651 }
645 652
646 // Open an existing file. The size is large than expected and the hash is 653 // Open an existing file. The size is large than expected and the hash is
647 // unknown. 654 // unknown.
648 TEST_F(BaseFileTest, ExistingBaseFileUnknownHashTooLong) { 655 TEST_F(BaseFileTest, ExistingBaseFileUnknownHashTooLong) {
649 base::FilePath file_path = temp_dir_.path().AppendASCII("existing"); 656 base::FilePath file_path = temp_dir_.path().AppendASCII("existing");
650 std::string contents; 657 std::string contents;
651 contents.append(kTestData1); 658 contents.append(kTestData1);
652 contents.append("Something extra"); 659 contents.append("Something extra");
653 ASSERT_TRUE(base::WriteFile(file_path, contents.data(), contents.size())); 660 ASSERT_EQ(static_cast<int>(contents.size()),
661 base::WriteFile(file_path, contents.data(), contents.size()));
654 662
655 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, 663 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE,
656 base_file_->Initialize(file_path, base::FilePath(), base::File(), 664 base_file_->Initialize(file_path, base::FilePath(), base::File(),
657 kTestDataLength1, std::string(), 665 kTestDataLength1, std::string(),
658 std::unique_ptr<crypto::SecureHash>())); 666 std::unique_ptr<crypto::SecureHash>()));
659 set_expected_data(kTestData1); 667 set_expected_data(kTestData1);
660 ASSERT_TRUE(AppendDataToFile(kTestData2)); 668 ASSERT_TRUE(AppendDataToFile(kTestData2));
661 ASSERT_TRUE(AppendDataToFile(kTestData3)); 669 ASSERT_TRUE(AppendDataToFile(kTestData3));
662 ExpectHashValue(kHashOfTestData1To3, base_file_->Finish()); 670 ExpectHashValue(kHashOfTestData1To3, base_file_->Finish());
663 } 671 }
664 672
673 // Similar to ExistingBaseFileKnownHashTooLong test, but with a file large
674 // enough to requre multiple Read()s to complete. This provides additional code
675 // coverage for the CalculatePartialHash() logic.
676 TEST_F(BaseFileTest, ExistingBaseFileUnknownHashTooLongForLargeFile) {
677 base::FilePath file_path = temp_dir_.path().AppendASCII("existing");
678 const size_t kFileSize = 1024 * 1024;
679 const size_t kIntermediateSize = kFileSize / 2 + 111;
680 // |contents| is 100 bytes longer than kIntermediateSize. The latter is the
681 // expected size.
682 std::string contents(kIntermediateSize + 100, 'a');
683 ASSERT_EQ(static_cast<int>(contents.size()),
684 base::WriteFile(file_path, contents.data(), contents.size()));
685
686 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE,
687 base_file_->Initialize(file_path, base::FilePath(), base::File(),
688 kIntermediateSize, std::string(),
689 std::unique_ptr<crypto::SecureHash>()));
690 // The extra bytes should be stripped during Initialize().
691 contents.resize(kIntermediateSize, 'a');
692 set_expected_data(contents);
693 std::string new_data(kFileSize - kIntermediateSize, 'a');
694 ASSERT_TRUE(AppendDataToFile(new_data));
695 const uint8_t kExpectedHash[] = {
696 0x9b, 0xc1, 0xb2, 0xa2, 0x88, 0xb2, 0x6a, 0xf7, 0x25, 0x7a, 0x36,
697 0x27, 0x7a, 0xe3, 0x81, 0x6a, 0x7d, 0x4f, 0x16, 0xe8, 0x9c, 0x1e,
698 0x7e, 0x77, 0xd0, 0xa5, 0xc4, 0x8b, 0xad, 0x62, 0xb3, 0x60,
699 };
700 ExpectHashValue(kExpectedHash, base_file_->Finish());
701 }
702
665 // Test that a temporary file is created in the default download directory. 703 // Test that a temporary file is created in the default download directory.
666 TEST_F(BaseFileTest, CreatedInDefaultDirectory) { 704 TEST_F(BaseFileTest, CreatedInDefaultDirectory) {
667 ASSERT_TRUE(base_file_->full_path().empty()); 705 ASSERT_TRUE(base_file_->full_path().empty());
668 ASSERT_TRUE(InitializeFile()); 706 ASSERT_TRUE(InitializeFile());
669 EXPECT_FALSE(base_file_->full_path().empty()); 707 EXPECT_FALSE(base_file_->full_path().empty());
670 708
671 // On Windows, CreateTemporaryFileInDir() will cause a path with short names 709 // On Windows, CreateTemporaryFileInDir() will cause a path with short names
672 // to be expanded into a path with long names. Thus temp_dir.path() might not 710 // to be expanded into a path with long names. Thus temp_dir.path() might not
673 // be a string-wise match to base_file_->full_path().DirName() even though 711 // be a string-wise match to base_file_->full_path().DirName() even though
674 // they are in the same directory. 712 // they are in the same directory.
(...skipping 16 matching lines...) Expand all
691 729
692 const char kData[] = "hello"; 730 const char kData[] = "hello";
693 const int kDataLength = static_cast<int>(arraysize(kData) - 1); 731 const int kDataLength = static_cast<int>(arraysize(kData) - 1);
694 ASSERT_EQ(kDataLength, base::WriteFile(full_path, kData, kDataLength)); 732 ASSERT_EQ(kDataLength, base::WriteFile(full_path, kData, kDataLength));
695 // The file that we created here should stick around when the BaseFile is 733 // The file that we created here should stick around when the BaseFile is
696 // destroyed during TearDown. 734 // destroyed during TearDown.
697 expect_file_survives_ = true; 735 expect_file_survives_ = true;
698 } 736 }
699 737
700 } // namespace content 738 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/download/base_file.cc ('k') | content/browser/download/download_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698