OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "third_party/zlib/google/zip_reader.h" | 5 #include "third_party/zlib/google/zip_reader.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 #include <string.h> | 9 #include <string.h> |
10 | 10 |
(...skipping 552 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
563 | 563 |
564 for (size_t i = 0; i < 8; i++) { | 564 for (size_t i = 0; i < 8; i++) { |
565 SCOPED_TRACE(base::StringPrintf("Processing %d.txt", static_cast<int>(i))); | 565 SCOPED_TRACE(base::StringPrintf("Processing %d.txt", static_cast<int>(i))); |
566 | 566 |
567 base::FilePath file_name = base::FilePath::FromUTF8Unsafe( | 567 base::FilePath file_name = base::FilePath::FromUTF8Unsafe( |
568 base::StringPrintf("%d.txt", static_cast<int>(i))); | 568 base::StringPrintf("%d.txt", static_cast<int>(i))); |
569 ASSERT_TRUE(reader.LocateAndOpenEntry(file_name)); | 569 ASSERT_TRUE(reader.LocateAndOpenEntry(file_name)); |
570 | 570 |
571 if (i > 1) { | 571 if (i > 1) { |
572 // Off by one byte read limit: must fail. | 572 // Off by one byte read limit: must fail. |
573 EXPECT_FALSE(reader.ExtractCurrentEntryToString(i - 1, &contents)); | 573 EXPECT_FALSE(reader.ExtractCurrentEntryToString(i - 1, true, &contents)); |
574 } | 574 } |
575 | 575 |
576 if (i > 0) { | 576 if (i > 0) { |
577 // Exact byte read limit: must pass. | 577 // Exact byte read limit: must pass. |
578 EXPECT_TRUE(reader.ExtractCurrentEntryToString(i, &contents)); | 578 EXPECT_TRUE(reader.ExtractCurrentEntryToString(i, true, &contents)); |
579 EXPECT_EQ(i, contents.size()); | 579 EXPECT_EQ(i, contents.size()); |
580 EXPECT_EQ(0, memcmp(contents.c_str(), "0123456", i)); | 580 EXPECT_EQ(0, memcmp(contents.c_str(), "0123456", i)); |
581 } | 581 } |
582 | 582 |
583 // More than necessary byte read limit: must pass. | 583 // More than necessary byte read limit: must pass. |
584 EXPECT_TRUE(reader.ExtractCurrentEntryToString(16, &contents)); | 584 EXPECT_TRUE(reader.ExtractCurrentEntryToString(16, true, &contents)); |
585 EXPECT_EQ(i, contents.size()); | 585 EXPECT_EQ(i, contents.size()); |
586 EXPECT_EQ(0, memcmp(contents.c_str(), "0123456", i)); | 586 EXPECT_EQ(0, memcmp(contents.c_str(), "0123456", i)); |
587 } | 587 } |
588 reader.Close(); | 588 reader.Close(); |
589 } | 589 } |
590 | 590 |
| 591 TEST_F(ZipReaderTest, ExtractPartOfCurrentEntry) { |
| 592 // test_mismatch_size.zip contains files with names from 0.txt to 7.txt with |
| 593 // sizes from 0 to 7 bytes respectively, being the contents of each file a |
| 594 // substring of "0123456" starting at '0'. |
| 595 base::FilePath test_zip_file = |
| 596 test_data_dir_.AppendASCII("test_mismatch_size.zip"); |
| 597 |
| 598 ZipReader reader; |
| 599 std::string contents; |
| 600 ASSERT_TRUE(reader.Open(test_zip_file)); |
| 601 |
| 602 base::FilePath file_name0 = base::FilePath::FromUTF8Unsafe("0.txt"); |
| 603 ASSERT_TRUE(reader.LocateAndOpenEntry(file_name0)); |
| 604 EXPECT_TRUE(reader.ExtractCurrentEntryToString(0, false, &contents)); |
| 605 EXPECT_EQ(0, memcmp(contents.c_str(), "", 0)); |
| 606 EXPECT_FALSE(reader.ExtractCurrentEntryToString(1, false, &contents)); |
| 607 EXPECT_EQ(0, memcmp(contents.c_str(), "", 0)); |
| 608 |
| 609 base::FilePath file_name1 = base::FilePath::FromUTF8Unsafe("1.txt"); |
| 610 ASSERT_TRUE(reader.LocateAndOpenEntry(file_name1)); |
| 611 EXPECT_TRUE(reader.ExtractCurrentEntryToString(0, false, &contents)); |
| 612 EXPECT_EQ(0, memcmp(contents.c_str(), "", 0)); |
| 613 EXPECT_TRUE(reader.ExtractCurrentEntryToString(1, false, &contents)); |
| 614 EXPECT_EQ(0, memcmp(contents.c_str(), "0", 1)); |
| 615 EXPECT_FALSE(reader.ExtractCurrentEntryToString(2, false, &contents)); |
| 616 EXPECT_EQ(0, memcmp(contents.c_str(), "", 0)); |
| 617 |
| 618 base::FilePath file_name4 = base::FilePath::FromUTF8Unsafe("4.txt"); |
| 619 ASSERT_TRUE(reader.LocateAndOpenEntry(file_name4)); |
| 620 EXPECT_TRUE(reader.ExtractCurrentEntryToString(0, false, &contents)); |
| 621 EXPECT_EQ(0, memcmp(contents.c_str(), "", 0)); |
| 622 EXPECT_TRUE(reader.ExtractCurrentEntryToString(2, false, &contents)); |
| 623 EXPECT_EQ(0, memcmp(contents.c_str(), "01", 2)); |
| 624 EXPECT_TRUE(reader.ExtractCurrentEntryToString(4, false, &contents)); |
| 625 EXPECT_EQ(0, memcmp(contents.c_str(), "0123", 4)); |
| 626 EXPECT_FALSE(reader.ExtractCurrentEntryToString(5, false, &contents)); |
| 627 EXPECT_EQ(0, memcmp(contents.c_str(), "", 0)); |
| 628 |
| 629 reader.Close(); |
| 630 } |
| 631 |
591 // This test exposes http://crbug.com/430959, at least on OS X | 632 // This test exposes http://crbug.com/430959, at least on OS X |
592 TEST_F(ZipReaderTest, DISABLED_LeakDetectionTest) { | 633 TEST_F(ZipReaderTest, DISABLED_LeakDetectionTest) { |
593 for (int i = 0; i < 100000; ++i) { | 634 for (int i = 0; i < 100000; ++i) { |
594 FileWrapper zip_fd_wrapper(test_zip_file_, FileWrapper::READ_ONLY); | 635 FileWrapper zip_fd_wrapper(test_zip_file_, FileWrapper::READ_ONLY); |
595 ZipReader reader; | 636 ZipReader reader; |
596 ASSERT_TRUE(reader.OpenFromPlatformFile(zip_fd_wrapper.platform_file())); | 637 ASSERT_TRUE(reader.OpenFromPlatformFile(zip_fd_wrapper.platform_file())); |
597 } | 638 } |
598 } | 639 } |
599 | 640 |
600 // Test that when WriterDelegate::PrepareMock returns false, no other methods on | 641 // Test that when WriterDelegate::PrepareMock returns false, no other methods on |
601 // the delegate are called and the extraction fails. | 642 // the delegate are called and the extraction fails. |
602 TEST_F(ZipReaderTest, ExtractCurrentEntryPrepareFailure) { | 643 TEST_F(ZipReaderTest, ExtractCurrentEntryPrepareFailure) { |
603 testing::StrictMock<MockWriterDelegate> mock_writer; | 644 testing::StrictMock<MockWriterDelegate> mock_writer; |
604 | 645 |
605 EXPECT_CALL(mock_writer, PrepareOutput()) | 646 EXPECT_CALL(mock_writer, PrepareOutput()) |
606 .WillOnce(Return(false)); | 647 .WillOnce(Return(false)); |
607 | 648 |
608 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); | 649 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); |
609 ZipReader reader; | 650 ZipReader reader; |
610 | 651 |
611 ASSERT_TRUE(reader.Open(test_zip_file_)); | 652 ASSERT_TRUE(reader.Open(test_zip_file_)); |
612 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); | 653 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); |
613 ASSERT_FALSE(reader.ExtractCurrentEntry(&mock_writer)); | 654 ASSERT_FALSE(reader.ExtractCurrentEntry(&mock_writer, -1)); |
614 } | 655 } |
615 | 656 |
616 // Test that when WriterDelegate::WriteBytes returns false, no other methods on | 657 // Test that when WriterDelegate::WriteBytes returns false, no other methods on |
617 // the delegate are called and the extraction fails. | 658 // the delegate are called and the extraction fails. |
618 TEST_F(ZipReaderTest, ExtractCurrentEntryWriteBytesFailure) { | 659 TEST_F(ZipReaderTest, ExtractCurrentEntryWriteBytesFailure) { |
619 testing::StrictMock<MockWriterDelegate> mock_writer; | 660 testing::StrictMock<MockWriterDelegate> mock_writer; |
620 | 661 |
621 EXPECT_CALL(mock_writer, PrepareOutput()) | 662 EXPECT_CALL(mock_writer, PrepareOutput()) |
622 .WillOnce(Return(true)); | 663 .WillOnce(Return(true)); |
623 EXPECT_CALL(mock_writer, WriteBytes(_, _)) | 664 EXPECT_CALL(mock_writer, WriteBytes(_, _)) |
624 .WillOnce(Return(false)); | 665 .WillOnce(Return(false)); |
625 | 666 |
626 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); | 667 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); |
627 ZipReader reader; | 668 ZipReader reader; |
628 | 669 |
629 ASSERT_TRUE(reader.Open(test_zip_file_)); | 670 ASSERT_TRUE(reader.Open(test_zip_file_)); |
630 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); | 671 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); |
631 ASSERT_FALSE(reader.ExtractCurrentEntry(&mock_writer)); | 672 ASSERT_FALSE(reader.ExtractCurrentEntry(&mock_writer, -1)); |
632 } | 673 } |
633 | 674 |
634 // Test that extraction succeeds when the writer delegate reports all is well. | 675 // Test that extraction succeeds when the writer delegate reports all is well. |
635 TEST_F(ZipReaderTest, ExtractCurrentEntrySuccess) { | 676 TEST_F(ZipReaderTest, ExtractCurrentEntrySuccess) { |
636 testing::StrictMock<MockWriterDelegate> mock_writer; | 677 testing::StrictMock<MockWriterDelegate> mock_writer; |
637 | 678 |
638 EXPECT_CALL(mock_writer, PrepareOutput()) | 679 EXPECT_CALL(mock_writer, PrepareOutput()) |
639 .WillOnce(Return(true)); | 680 .WillOnce(Return(true)); |
640 EXPECT_CALL(mock_writer, WriteBytes(_, _)) | 681 EXPECT_CALL(mock_writer, WriteBytes(_, _)) |
641 .WillRepeatedly(Return(true)); | 682 .WillRepeatedly(Return(true)); |
642 | 683 |
643 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); | 684 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); |
644 ZipReader reader; | 685 ZipReader reader; |
645 | 686 |
646 ASSERT_TRUE(reader.Open(test_zip_file_)); | 687 ASSERT_TRUE(reader.Open(test_zip_file_)); |
647 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); | 688 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); |
648 ASSERT_TRUE(reader.ExtractCurrentEntry(&mock_writer)); | 689 ASSERT_TRUE(reader.ExtractCurrentEntry(&mock_writer, -1)); |
| 690 } |
| 691 |
| 692 // Test that when WriterDelegate::PrepareMock returns false, no other methods on |
| 693 // the delegate are called and the partial extraction fails. |
| 694 TEST_F(ZipReaderTest, ExtractFromBeginningOfCurrentEntryPrepareFailure) { |
| 695 testing::StrictMock<MockWriterDelegate> mock_writer; |
| 696 |
| 697 EXPECT_CALL(mock_writer, PrepareOutput()).WillOnce(Return(false)); |
| 698 |
| 699 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); |
| 700 ZipReader reader; |
| 701 |
| 702 ASSERT_TRUE(reader.Open(test_zip_file_)); |
| 703 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); |
| 704 ASSERT_FALSE(reader.ExtractCurrentEntry(&mock_writer, 1)); |
| 705 } |
| 706 |
| 707 // Test that when WriterDelegate::WriteBytes returns false, no other methods on |
| 708 // the delegate are called and the partial extraction fails. |
| 709 TEST_F(ZipReaderTest, ExtractFromBeginningOfCurrentEntryWriteBytesFailure) { |
| 710 testing::StrictMock<MockWriterDelegate> mock_writer; |
| 711 |
| 712 EXPECT_CALL(mock_writer, PrepareOutput()).WillOnce(Return(true)); |
| 713 EXPECT_CALL(mock_writer, WriteBytes(_, _)).WillOnce(Return(false)); |
| 714 |
| 715 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); |
| 716 ZipReader reader; |
| 717 |
| 718 ASSERT_TRUE(reader.Open(test_zip_file_)); |
| 719 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); |
| 720 ASSERT_FALSE(reader.ExtractCurrentEntry(&mock_writer, 1)); |
| 721 } |
| 722 |
| 723 // Test that partial extraction succeeds when the writer delegate reports all is |
| 724 // well. |
| 725 TEST_F(ZipReaderTest, ExtractFromBeginningOfCurrentEntrySuccess) { |
| 726 testing::StrictMock<MockWriterDelegate> mock_writer; |
| 727 |
| 728 EXPECT_CALL(mock_writer, PrepareOutput()).WillOnce(Return(true)); |
| 729 EXPECT_CALL(mock_writer, WriteBytes(_, _)).WillRepeatedly(Return(true)); |
| 730 |
| 731 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); |
| 732 ZipReader reader; |
| 733 |
| 734 ASSERT_TRUE(reader.Open(test_zip_file_)); |
| 735 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); |
| 736 ASSERT_TRUE(reader.ExtractCurrentEntry(&mock_writer, 1)); |
649 } | 737 } |
650 | 738 |
651 class FileWriterDelegateTest : public ::testing::Test { | 739 class FileWriterDelegateTest : public ::testing::Test { |
652 protected: | 740 protected: |
653 void SetUp() override { | 741 void SetUp() override { |
654 ASSERT_TRUE(base::CreateTemporaryFile(&temp_file_path_)); | 742 ASSERT_TRUE(base::CreateTemporaryFile(&temp_file_path_)); |
655 file_.Initialize(temp_file_path_, (base::File::FLAG_CREATE_ALWAYS | | 743 file_.Initialize(temp_file_path_, (base::File::FLAG_CREATE_ALWAYS | |
656 base::File::FLAG_READ | | 744 base::File::FLAG_READ | |
657 base::File::FLAG_WRITE | | 745 base::File::FLAG_WRITE | |
658 base::File::FLAG_TEMPORARY | | 746 base::File::FLAG_TEMPORARY | |
(...skipping 25 matching lines...) Expand all Loading... |
684 ASSERT_TRUE(writer.PrepareOutput()); | 772 ASSERT_TRUE(writer.PrepareOutput()); |
685 ASSERT_TRUE(writer.WriteBytes(kSomeData, kSomeDataLen)); | 773 ASSERT_TRUE(writer.WriteBytes(kSomeData, kSomeDataLen)); |
686 } | 774 } |
687 ASSERT_EQ(kSomeDataLen, file_.GetLength()); | 775 ASSERT_EQ(kSomeDataLen, file_.GetLength()); |
688 char buf[kSomeDataLen] = {}; | 776 char buf[kSomeDataLen] = {}; |
689 ASSERT_EQ(kSomeDataLen, file_.Read(0LL, buf, kSomeDataLen)); | 777 ASSERT_EQ(kSomeDataLen, file_.Read(0LL, buf, kSomeDataLen)); |
690 ASSERT_EQ(std::string(kSomeData), std::string(buf, kSomeDataLen)); | 778 ASSERT_EQ(std::string(kSomeData), std::string(buf, kSomeDataLen)); |
691 } | 779 } |
692 | 780 |
693 } // namespace zip | 781 } // namespace zip |
OLD | NEW |