| 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 558 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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, &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, &contents)); |
| 579 EXPECT_EQ(i, contents.size()); | 579 EXPECT_EQ(base::StringPiece("0123456", i).as_string(), contents); |
| 580 EXPECT_EQ(0, memcmp(contents.c_str(), "0123456", i)); | |
| 581 } | 580 } |
| 582 | 581 |
| 583 // More than necessary byte read limit: must pass. | 582 // More than necessary byte read limit: must pass. |
| 584 EXPECT_TRUE(reader.ExtractCurrentEntryToString(16, &contents)); | 583 EXPECT_TRUE(reader.ExtractCurrentEntryToString(16, &contents)); |
| 585 EXPECT_EQ(i, contents.size()); | 584 EXPECT_EQ(base::StringPiece("0123456", i).as_string(), contents); |
| 586 EXPECT_EQ(0, memcmp(contents.c_str(), "0123456", i)); | |
| 587 } | 585 } |
| 588 reader.Close(); | 586 reader.Close(); |
| 589 } | 587 } |
| 590 | 588 |
| 589 TEST_F(ZipReaderTest, ExtractPartOfCurrentEntry) { |
| 590 // test_mismatch_size.zip contains files with names from 0.txt to 7.txt with |
| 591 // sizes from 0 to 7 bytes respectively, being the contents of each file a |
| 592 // substring of "0123456" starting at '0'. |
| 593 base::FilePath test_zip_file = |
| 594 test_data_dir_.AppendASCII("test_mismatch_size.zip"); |
| 595 |
| 596 ZipReader reader; |
| 597 std::string contents; |
| 598 ASSERT_TRUE(reader.Open(test_zip_file)); |
| 599 |
| 600 base::FilePath file_name0 = base::FilePath::FromUTF8Unsafe("0.txt"); |
| 601 ASSERT_TRUE(reader.LocateAndOpenEntry(file_name0)); |
| 602 EXPECT_TRUE(reader.ExtractCurrentEntryToString(0, &contents)); |
| 603 EXPECT_EQ("", contents); |
| 604 EXPECT_TRUE(reader.ExtractCurrentEntryToString(1, &contents)); |
| 605 EXPECT_EQ("", contents); |
| 606 |
| 607 base::FilePath file_name1 = base::FilePath::FromUTF8Unsafe("1.txt"); |
| 608 ASSERT_TRUE(reader.LocateAndOpenEntry(file_name1)); |
| 609 EXPECT_TRUE(reader.ExtractCurrentEntryToString(0, &contents)); |
| 610 EXPECT_EQ("", contents); |
| 611 EXPECT_TRUE(reader.ExtractCurrentEntryToString(1, &contents)); |
| 612 EXPECT_EQ("0", contents); |
| 613 EXPECT_TRUE(reader.ExtractCurrentEntryToString(2, &contents)); |
| 614 EXPECT_EQ("0", contents); |
| 615 |
| 616 base::FilePath file_name4 = base::FilePath::FromUTF8Unsafe("4.txt"); |
| 617 ASSERT_TRUE(reader.LocateAndOpenEntry(file_name4)); |
| 618 EXPECT_TRUE(reader.ExtractCurrentEntryToString(0, &contents)); |
| 619 EXPECT_EQ("", contents); |
| 620 EXPECT_FALSE(reader.ExtractCurrentEntryToString(2, &contents)); |
| 621 EXPECT_EQ("01", contents); |
| 622 EXPECT_TRUE(reader.ExtractCurrentEntryToString(4, &contents)); |
| 623 EXPECT_EQ("0123", contents); |
| 624 // Checks that entire file is extracted and function returns true when |
| 625 // |max_read_bytes| is larger than file size. |
| 626 EXPECT_TRUE(reader.ExtractCurrentEntryToString(5, &contents)); |
| 627 EXPECT_EQ("0123", contents); |
| 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( |
| 655 &mock_writer, std::numeric_limits<uint64_t>::max())); |
| 614 } | 656 } |
| 615 | 657 |
| 616 // Test that when WriterDelegate::WriteBytes returns false, no other methods on | 658 // Test that when WriterDelegate::WriteBytes returns false, no other methods on |
| 617 // the delegate are called and the extraction fails. | 659 // the delegate are called and the extraction fails. |
| 618 TEST_F(ZipReaderTest, ExtractCurrentEntryWriteBytesFailure) { | 660 TEST_F(ZipReaderTest, ExtractCurrentEntryWriteBytesFailure) { |
| 619 testing::StrictMock<MockWriterDelegate> mock_writer; | 661 testing::StrictMock<MockWriterDelegate> mock_writer; |
| 620 | 662 |
| 621 EXPECT_CALL(mock_writer, PrepareOutput()) | 663 EXPECT_CALL(mock_writer, PrepareOutput()) |
| 622 .WillOnce(Return(true)); | 664 .WillOnce(Return(true)); |
| 623 EXPECT_CALL(mock_writer, WriteBytes(_, _)) | 665 EXPECT_CALL(mock_writer, WriteBytes(_, _)) |
| 624 .WillOnce(Return(false)); | 666 .WillOnce(Return(false)); |
| 625 | 667 |
| 626 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); | 668 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); |
| 627 ZipReader reader; | 669 ZipReader reader; |
| 628 | 670 |
| 629 ASSERT_TRUE(reader.Open(test_zip_file_)); | 671 ASSERT_TRUE(reader.Open(test_zip_file_)); |
| 630 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); | 672 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); |
| 631 ASSERT_FALSE(reader.ExtractCurrentEntry(&mock_writer)); | 673 ASSERT_FALSE(reader.ExtractCurrentEntry( |
| 674 &mock_writer, std::numeric_limits<uint64_t>::max())); |
| 632 } | 675 } |
| 633 | 676 |
| 634 // Test that extraction succeeds when the writer delegate reports all is well. | 677 // Test that extraction succeeds when the writer delegate reports all is well. |
| 635 TEST_F(ZipReaderTest, ExtractCurrentEntrySuccess) { | 678 TEST_F(ZipReaderTest, ExtractCurrentEntrySuccess) { |
| 636 testing::StrictMock<MockWriterDelegate> mock_writer; | 679 testing::StrictMock<MockWriterDelegate> mock_writer; |
| 637 | 680 |
| 638 EXPECT_CALL(mock_writer, PrepareOutput()) | 681 EXPECT_CALL(mock_writer, PrepareOutput()) |
| 639 .WillOnce(Return(true)); | 682 .WillOnce(Return(true)); |
| 640 EXPECT_CALL(mock_writer, WriteBytes(_, _)) | 683 EXPECT_CALL(mock_writer, WriteBytes(_, _)) |
| 641 .WillRepeatedly(Return(true)); | 684 .WillRepeatedly(Return(true)); |
| 642 | 685 |
| 643 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); | 686 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); |
| 644 ZipReader reader; | 687 ZipReader reader; |
| 645 | 688 |
| 646 ASSERT_TRUE(reader.Open(test_zip_file_)); | 689 ASSERT_TRUE(reader.Open(test_zip_file_)); |
| 647 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); | 690 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); |
| 648 ASSERT_TRUE(reader.ExtractCurrentEntry(&mock_writer)); | 691 ASSERT_TRUE(reader.ExtractCurrentEntry(&mock_writer, |
| 692 std::numeric_limits<uint64_t>::max())); |
| 649 } | 693 } |
| 650 | 694 |
| 651 class FileWriterDelegateTest : public ::testing::Test { | 695 class FileWriterDelegateTest : public ::testing::Test { |
| 652 protected: | 696 protected: |
| 653 void SetUp() override { | 697 void SetUp() override { |
| 654 ASSERT_TRUE(base::CreateTemporaryFile(&temp_file_path_)); | 698 ASSERT_TRUE(base::CreateTemporaryFile(&temp_file_path_)); |
| 655 file_.Initialize(temp_file_path_, (base::File::FLAG_CREATE_ALWAYS | | 699 file_.Initialize(temp_file_path_, (base::File::FLAG_CREATE_ALWAYS | |
| 656 base::File::FLAG_READ | | 700 base::File::FLAG_READ | |
| 657 base::File::FLAG_WRITE | | 701 base::File::FLAG_WRITE | |
| 658 base::File::FLAG_TEMPORARY | | 702 base::File::FLAG_TEMPORARY | |
| (...skipping 25 matching lines...) Expand all Loading... |
| 684 ASSERT_TRUE(writer.PrepareOutput()); | 728 ASSERT_TRUE(writer.PrepareOutput()); |
| 685 ASSERT_TRUE(writer.WriteBytes(kSomeData, kSomeDataLen)); | 729 ASSERT_TRUE(writer.WriteBytes(kSomeData, kSomeDataLen)); |
| 686 } | 730 } |
| 687 ASSERT_EQ(kSomeDataLen, file_.GetLength()); | 731 ASSERT_EQ(kSomeDataLen, file_.GetLength()); |
| 688 char buf[kSomeDataLen] = {}; | 732 char buf[kSomeDataLen] = {}; |
| 689 ASSERT_EQ(kSomeDataLen, file_.Read(0LL, buf, kSomeDataLen)); | 733 ASSERT_EQ(kSomeDataLen, file_.Read(0LL, buf, kSomeDataLen)); |
| 690 ASSERT_EQ(std::string(kSomeData), std::string(buf, kSomeDataLen)); | 734 ASSERT_EQ(std::string(kSomeData), std::string(buf, kSomeDataLen)); |
| 691 } | 735 } |
| 692 | 736 |
| 693 } // namespace zip | 737 } // namespace zip |
| OLD | NEW |