| 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 570 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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, &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, ExtractFromBeginningOfCurrentEntry) { |
| 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( |
| 605 reader.ExtractFromBeginningOfCurrentEntryToString(0, false, &contents)); |
| 606 EXPECT_EQ(0, memcmp(contents.c_str(), "", 0)); |
| 607 EXPECT_FALSE( |
| 608 reader.ExtractFromBeginningOfCurrentEntryToString(1, false, &contents)); |
| 609 EXPECT_EQ(0, memcmp(contents.c_str(), "", 0)); |
| 610 |
| 611 base::FilePath file_name1 = base::FilePath::FromUTF8Unsafe("1.txt"); |
| 612 ASSERT_TRUE(reader.LocateAndOpenEntry(file_name1)); |
| 613 EXPECT_TRUE( |
| 614 reader.ExtractFromBeginningOfCurrentEntryToString(0, false, &contents)); |
| 615 EXPECT_EQ(0, memcmp(contents.c_str(), "", 0)); |
| 616 EXPECT_TRUE( |
| 617 reader.ExtractFromBeginningOfCurrentEntryToString(1, false, &contents)); |
| 618 EXPECT_EQ(0, memcmp(contents.c_str(), "0", 1)); |
| 619 EXPECT_FALSE( |
| 620 reader.ExtractFromBeginningOfCurrentEntryToString(2, false, &contents)); |
| 621 EXPECT_EQ(0, memcmp(contents.c_str(), "", 0)); |
| 622 |
| 623 base::FilePath file_name4 = base::FilePath::FromUTF8Unsafe("4.txt"); |
| 624 ASSERT_TRUE(reader.LocateAndOpenEntry(file_name4)); |
| 625 EXPECT_TRUE( |
| 626 reader.ExtractFromBeginningOfCurrentEntryToString(0, false, &contents)); |
| 627 EXPECT_EQ(0, memcmp(contents.c_str(), "", 0)); |
| 628 EXPECT_TRUE( |
| 629 reader.ExtractFromBeginningOfCurrentEntryToString(2, false, &contents)); |
| 630 EXPECT_EQ(0, memcmp(contents.c_str(), "01", 2)); |
| 631 EXPECT_TRUE( |
| 632 reader.ExtractFromBeginningOfCurrentEntryToString(4, false, &contents)); |
| 633 EXPECT_EQ(0, memcmp(contents.c_str(), "0123", 4)); |
| 634 EXPECT_FALSE( |
| 635 reader.ExtractFromBeginningOfCurrentEntryToString(5, false, &contents)); |
| 636 EXPECT_EQ(0, memcmp(contents.c_str(), "", 0)); |
| 637 |
| 638 reader.Close(); |
| 639 } |
| 640 |
| 591 // This test exposes http://crbug.com/430959, at least on OS X | 641 // This test exposes http://crbug.com/430959, at least on OS X |
| 592 TEST_F(ZipReaderTest, DISABLED_LeakDetectionTest) { | 642 TEST_F(ZipReaderTest, DISABLED_LeakDetectionTest) { |
| 593 for (int i = 0; i < 100000; ++i) { | 643 for (int i = 0; i < 100000; ++i) { |
| 594 FileWrapper zip_fd_wrapper(test_zip_file_, FileWrapper::READ_ONLY); | 644 FileWrapper zip_fd_wrapper(test_zip_file_, FileWrapper::READ_ONLY); |
| 595 ZipReader reader; | 645 ZipReader reader; |
| 596 ASSERT_TRUE(reader.OpenFromPlatformFile(zip_fd_wrapper.platform_file())); | 646 ASSERT_TRUE(reader.OpenFromPlatformFile(zip_fd_wrapper.platform_file())); |
| 597 } | 647 } |
| 598 } | 648 } |
| 599 | 649 |
| 600 // Test that when WriterDelegate::PrepareMock returns false, no other methods on | 650 // Test that when WriterDelegate::PrepareMock returns false, no other methods on |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 641 .WillRepeatedly(Return(true)); | 691 .WillRepeatedly(Return(true)); |
| 642 | 692 |
| 643 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); | 693 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); |
| 644 ZipReader reader; | 694 ZipReader reader; |
| 645 | 695 |
| 646 ASSERT_TRUE(reader.Open(test_zip_file_)); | 696 ASSERT_TRUE(reader.Open(test_zip_file_)); |
| 647 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); | 697 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); |
| 648 ASSERT_TRUE(reader.ExtractCurrentEntry(&mock_writer)); | 698 ASSERT_TRUE(reader.ExtractCurrentEntry(&mock_writer)); |
| 649 } | 699 } |
| 650 | 700 |
| 701 // Test that when WriterDelegate::PrepareMock returns false, no other methods on |
| 702 // the delegate are called and the partial extraction fails. |
| 703 TEST_F(ZipReaderTest, ExtractFromBeginningOfCurrentEntryPrepareFailure) { |
| 704 testing::StrictMock<MockWriterDelegate> mock_writer; |
| 705 |
| 706 EXPECT_CALL(mock_writer, PrepareOutput()).WillOnce(Return(false)); |
| 707 |
| 708 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); |
| 709 ZipReader reader; |
| 710 |
| 711 ASSERT_TRUE(reader.Open(test_zip_file_)); |
| 712 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); |
| 713 ASSERT_FALSE(reader.ExtractFromBeginningOfCurrentEntry(&mock_writer, 1)); |
| 714 } |
| 715 |
| 716 // Test that when WriterDelegate::WriteBytes returns false, no other methods on |
| 717 // the delegate are called and the partial extraction fails. |
| 718 TEST_F(ZipReaderTest, ExtractFromBeginningOfCurrentEntryWriteBytesFailure) { |
| 719 testing::StrictMock<MockWriterDelegate> mock_writer; |
| 720 |
| 721 EXPECT_CALL(mock_writer, PrepareOutput()).WillOnce(Return(true)); |
| 722 EXPECT_CALL(mock_writer, WriteBytes(_, _)).WillOnce(Return(false)); |
| 723 |
| 724 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); |
| 725 ZipReader reader; |
| 726 |
| 727 ASSERT_TRUE(reader.Open(test_zip_file_)); |
| 728 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); |
| 729 ASSERT_FALSE(reader.ExtractFromBeginningOfCurrentEntry(&mock_writer, 1)); |
| 730 } |
| 731 |
| 732 // Test that partial extraction succeeds when the writer delegate reports all is |
| 733 // well. |
| 734 TEST_F(ZipReaderTest, ExtractFromBeginningOfCurrentEntrySuccess) { |
| 735 testing::StrictMock<MockWriterDelegate> mock_writer; |
| 736 |
| 737 EXPECT_CALL(mock_writer, PrepareOutput()).WillOnce(Return(true)); |
| 738 EXPECT_CALL(mock_writer, WriteBytes(_, _)).WillRepeatedly(Return(true)); |
| 739 |
| 740 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); |
| 741 ZipReader reader; |
| 742 |
| 743 ASSERT_TRUE(reader.Open(test_zip_file_)); |
| 744 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); |
| 745 ASSERT_TRUE(reader.ExtractFromBeginningOfCurrentEntry(&mock_writer, 1)); |
| 746 } |
| 747 |
| 651 class FileWriterDelegateTest : public ::testing::Test { | 748 class FileWriterDelegateTest : public ::testing::Test { |
| 652 protected: | 749 protected: |
| 653 void SetUp() override { | 750 void SetUp() override { |
| 654 ASSERT_TRUE(base::CreateTemporaryFile(&temp_file_path_)); | 751 ASSERT_TRUE(base::CreateTemporaryFile(&temp_file_path_)); |
| 655 file_.Initialize(temp_file_path_, (base::File::FLAG_CREATE_ALWAYS | | 752 file_.Initialize(temp_file_path_, (base::File::FLAG_CREATE_ALWAYS | |
| 656 base::File::FLAG_READ | | 753 base::File::FLAG_READ | |
| 657 base::File::FLAG_WRITE | | 754 base::File::FLAG_WRITE | |
| 658 base::File::FLAG_TEMPORARY | | 755 base::File::FLAG_TEMPORARY | |
| 659 base::File::FLAG_DELETE_ON_CLOSE)); | 756 base::File::FLAG_DELETE_ON_CLOSE)); |
| 660 ASSERT_TRUE(file_.IsValid()); | 757 ASSERT_TRUE(file_.IsValid()); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 684 ASSERT_TRUE(writer.PrepareOutput()); | 781 ASSERT_TRUE(writer.PrepareOutput()); |
| 685 ASSERT_TRUE(writer.WriteBytes(kSomeData, kSomeDataLen)); | 782 ASSERT_TRUE(writer.WriteBytes(kSomeData, kSomeDataLen)); |
| 686 } | 783 } |
| 687 ASSERT_EQ(kSomeDataLen, file_.GetLength()); | 784 ASSERT_EQ(kSomeDataLen, file_.GetLength()); |
| 688 char buf[kSomeDataLen] = {}; | 785 char buf[kSomeDataLen] = {}; |
| 689 ASSERT_EQ(kSomeDataLen, file_.Read(0LL, buf, kSomeDataLen)); | 786 ASSERT_EQ(kSomeDataLen, file_.Read(0LL, buf, kSomeDataLen)); |
| 690 ASSERT_EQ(std::string(kSomeData), std::string(buf, kSomeDataLen)); | 787 ASSERT_EQ(std::string(kSomeData), std::string(buf, kSomeDataLen)); |
| 691 } | 788 } |
| 692 | 789 |
| 693 } // namespace zip | 790 } // namespace zip |
| OLD | NEW |