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, ExtractPartOfCurrentEntryToString) { |
| 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.ExtractPartOfCurrentEntryToString(0, &contents)); |
| 605 EXPECT_EQ(0, memcmp(contents.c_str(), "", 0)); |
| 606 EXPECT_FALSE(reader.ExtractPartOfCurrentEntryToString(1, &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.ExtractPartOfCurrentEntryToString(0, &contents)); |
| 612 EXPECT_EQ(0, memcmp(contents.c_str(), "", 0)); |
| 613 EXPECT_TRUE(reader.ExtractPartOfCurrentEntryToString(1, &contents)); |
| 614 EXPECT_EQ(0, memcmp(contents.c_str(), "0", 1)); |
| 615 EXPECT_FALSE(reader.ExtractPartOfCurrentEntryToString(2, &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.ExtractPartOfCurrentEntryToString(0, &contents)); |
| 621 EXPECT_EQ(0, memcmp(contents.c_str(), "", 0)); |
| 622 EXPECT_TRUE(reader.ExtractPartOfCurrentEntryToString(2, &contents)); |
| 623 EXPECT_EQ(0, memcmp(contents.c_str(), "01", 2)); |
| 624 EXPECT_TRUE(reader.ExtractPartOfCurrentEntryToString(4, &contents)); |
| 625 EXPECT_EQ(0, memcmp(contents.c_str(), "0123", 4)); |
| 626 EXPECT_FALSE(reader.ExtractPartOfCurrentEntryToString(5, &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 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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)); |
649 } | 690 } |
650 | 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, ExtractPartOfCurrentEntryPrepareFailure) { |
| 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.ExtractPartOfCurrentEntry(&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, ExtractPartOfCurrentEntryWriteBytesFailure) { |
| 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.ExtractPartOfCurrentEntry(&mock_writer, 1)); |
| 721 } |
| 722 |
| 723 // Test that partial extraction succeeds when the writer delegate reports all is |
| 724 // well. |
| 725 TEST_F(ZipReaderTest, ExtractPartOfCurrentEntrySuccess) { |
| 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.ExtractPartOfCurrentEntry(&mock_writer, 1)); |
| 737 } |
| 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 | |
659 base::File::FLAG_DELETE_ON_CLOSE)); | 747 base::File::FLAG_DELETE_ON_CLOSE)); |
660 ASSERT_TRUE(file_.IsValid()); | 748 ASSERT_TRUE(file_.IsValid()); |
(...skipping 23 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 |