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

Side by Side Diff: third_party/zlib/google/zip_reader_unittest.cc

Issue 2961373002: Improve Zip File Scanning on Mac (Closed)
Patch Set: avoiding multiple calls to unzReadCurrentFile Created 3 years, 4 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 | « third_party/zlib/google/zip_reader.cc ('k') | no next file » | 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) 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 559 matching lines...) Expand 10 before | Expand all | Expand 10 after
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(i, contents.size());
580 EXPECT_EQ(0, memcmp(contents.c_str(), "0123456", i)); 580 EXPECT_EQ(0, memcmp(contents.c_str(), "0123456", i));
satorux1 2017/08/01 08:01:09 EXPECT_EQ(base::StringPiece("0123456", i).as__stri
mortonm 2017/08/01 16:16:46 Done.
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));
satorux1 2017/08/01 08:01:09 ditto
mortonm 2017/08/01 16:16:46 Done.
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, &contents));
605 EXPECT_EQ(0, memcmp(contents.c_str(), "", 0));
satorux1 2017/08/01 08:01:09 EXPECT_EQ("", contents)?
mortonm 2017/08/01 16:16:46 Done.
606 EXPECT_TRUE(reader.ExtractCurrentEntryToString(1, &contents));
607 EXPECT_EQ(0, memcmp(contents.c_str(), "", 0));
satorux1 2017/08/01 08:01:09 EXPECT_EQ("", contents)?
mortonm 2017/08/01 16:16:46 Done.
608
609 base::FilePath file_name1 = base::FilePath::FromUTF8Unsafe("1.txt");
610 ASSERT_TRUE(reader.LocateAndOpenEntry(file_name1));
611 EXPECT_TRUE(reader.ExtractCurrentEntryToString(0, &contents));
612 EXPECT_EQ(0, memcmp(contents.c_str(), "", 0));
satorux1 2017/08/01 08:01:09 EXPECT_EQ("", contents)?
mortonm 2017/08/01 16:16:46 Done.
613 EXPECT_TRUE(reader.ExtractCurrentEntryToString(1, &contents));
614 EXPECT_EQ(0, memcmp(contents.c_str(), "0", 1));
satorux1 2017/08/01 08:01:09 EXPECT_EQ("0", contents)?
mortonm 2017/08/01 16:16:46 Done.
615 EXPECT_TRUE(reader.ExtractCurrentEntryToString(2, &contents));
616 EXPECT_EQ(0, memcmp(contents.c_str(), "0", 1));
satorux1 2017/08/01 08:01:09 EXPECT_EQ("0", contents)?
mortonm 2017/08/01 16:16:46 Done.
617
618 base::FilePath file_name4 = base::FilePath::FromUTF8Unsafe("4.txt");
619 ASSERT_TRUE(reader.LocateAndOpenEntry(file_name4));
620 EXPECT_TRUE(reader.ExtractCurrentEntryToString(0, &contents));
satorux1 2017/08/01 08:01:09 EXPECT_EQ("", contents)?
mortonm 2017/08/01 16:16:46 Done.
621 EXPECT_EQ(0, memcmp(contents.c_str(), "", 0));
622 EXPECT_FALSE(reader.ExtractCurrentEntryToString(2, &contents));
623 EXPECT_EQ(0, memcmp(contents.c_str(), "01", 2));
satorux1 2017/08/01 08:01:09 EXPECT_EQ("01", contents)?
mortonm 2017/08/01 16:16:46 Done.
624 EXPECT_TRUE(reader.ExtractCurrentEntryToString(4, &contents));
625 EXPECT_EQ(0, memcmp(contents.c_str(), "0123", 4));
satorux1 2017/08/01 08:01:09 EXPECT_EQ("0123", contents)?
mortonm 2017/08/01 16:16:46 Done.
626 EXPECT_TRUE(reader.ExtractCurrentEntryToString(5, &contents));
627 EXPECT_EQ(0, memcmp(contents.c_str(), "0123", 0));
satorux1 2017/08/01 08:01:09 0 here does not look correct. EXPECT_EQ("0123", c
mortonm 2017/08/01 16:16:46 The file is only 4 characters long. I am checking
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()));
693 }
694
695 // Test that when WriterDelegate::PrepareMock returns false, no other methods on
696 // the delegate are called and the partial extraction fails.
697 TEST_F(ZipReaderTest, ExtractFromBeginningOfCurrentEntryPrepareFailure) {
698 testing::StrictMock<MockWriterDelegate> mock_writer;
699
700 EXPECT_CALL(mock_writer, PrepareOutput()).WillOnce(Return(false));
701
702 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt"));
703 ZipReader reader;
704
705 ASSERT_TRUE(reader.Open(test_zip_file_));
706 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path));
707 ASSERT_FALSE(reader.ExtractCurrentEntry(
708 &mock_writer, std::numeric_limits<uint64_t>::max()));
709 }
710
711 // Test that when WriterDelegate::WriteBytes returns false, no other methods on
712 // the delegate are called and the partial extraction fails.
713 TEST_F(ZipReaderTest, ExtractFromBeginningOfCurrentEntryWriteBytesFailure) {
714 testing::StrictMock<MockWriterDelegate> mock_writer;
715
716 EXPECT_CALL(mock_writer, PrepareOutput()).WillOnce(Return(true));
717 EXPECT_CALL(mock_writer, WriteBytes(_, _)).WillOnce(Return(false));
718
719 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt"));
720 ZipReader reader;
721
722 ASSERT_TRUE(reader.Open(test_zip_file_));
723 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path));
724 ASSERT_FALSE(reader.ExtractCurrentEntry(
725 &mock_writer, std::numeric_limits<uint64_t>::max()));
726 }
727
728 // Test that partial extraction succeeds when the writer delegate reports all is
729 // well.
730 TEST_F(ZipReaderTest, ExtractFromBeginningOfCurrentEntrySuccess) {
731 testing::StrictMock<MockWriterDelegate> mock_writer;
732
733 EXPECT_CALL(mock_writer, PrepareOutput()).WillOnce(Return(true));
734 EXPECT_CALL(mock_writer, WriteBytes(_, _)).WillRepeatedly(Return(true));
735
736 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt"));
737 ZipReader reader;
738
739 ASSERT_TRUE(reader.Open(test_zip_file_));
740 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path));
741 ASSERT_TRUE(reader.ExtractCurrentEntry(&mock_writer,
742 std::numeric_limits<uint64_t>::max()));
649 } 743 }
650 744
651 class FileWriterDelegateTest : public ::testing::Test { 745 class FileWriterDelegateTest : public ::testing::Test {
652 protected: 746 protected:
653 void SetUp() override { 747 void SetUp() override {
654 ASSERT_TRUE(base::CreateTemporaryFile(&temp_file_path_)); 748 ASSERT_TRUE(base::CreateTemporaryFile(&temp_file_path_));
655 file_.Initialize(temp_file_path_, (base::File::FLAG_CREATE_ALWAYS | 749 file_.Initialize(temp_file_path_, (base::File::FLAG_CREATE_ALWAYS |
656 base::File::FLAG_READ | 750 base::File::FLAG_READ |
657 base::File::FLAG_WRITE | 751 base::File::FLAG_WRITE |
658 base::File::FLAG_TEMPORARY | 752 base::File::FLAG_TEMPORARY |
(...skipping 25 matching lines...) Expand all
684 ASSERT_TRUE(writer.PrepareOutput()); 778 ASSERT_TRUE(writer.PrepareOutput());
685 ASSERT_TRUE(writer.WriteBytes(kSomeData, kSomeDataLen)); 779 ASSERT_TRUE(writer.WriteBytes(kSomeData, kSomeDataLen));
686 } 780 }
687 ASSERT_EQ(kSomeDataLen, file_.GetLength()); 781 ASSERT_EQ(kSomeDataLen, file_.GetLength());
688 char buf[kSomeDataLen] = {}; 782 char buf[kSomeDataLen] = {};
689 ASSERT_EQ(kSomeDataLen, file_.Read(0LL, buf, kSomeDataLen)); 783 ASSERT_EQ(kSomeDataLen, file_.Read(0LL, buf, kSomeDataLen));
690 ASSERT_EQ(std::string(kSomeData), std::string(buf, kSomeDataLen)); 784 ASSERT_EQ(std::string(kSomeData), std::string(buf, kSomeDataLen));
691 } 785 }
692 786
693 } // namespace zip 787 } // namespace zip
OLDNEW
« no previous file with comments | « third_party/zlib/google/zip_reader.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698