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 <set> | 7 #include <set> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
12 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/md5.h" | 14 #include "base/md5.h" |
15 #include "base/path_service.h" | 15 #include "base/path_service.h" |
16 #include "base/platform_file.h" | 16 #include "base/platform_file.h" |
17 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
| 18 #include "base/strings/stringprintf.h" |
18 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
19 #include "base/time/time.h" | 20 #include "base/time/time.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
21 #include "testing/platform_test.h" | 22 #include "testing/platform_test.h" |
22 #include "third_party/zlib/google/zip_internal.h" | 23 #include "third_party/zlib/google/zip_internal.h" |
23 | 24 |
24 namespace { | 25 namespace { |
25 | 26 |
26 const static std::string kQuuxExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6"; | 27 const static std::string kQuuxExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6"; |
27 | 28 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 | 64 |
64 private: | 65 private: |
65 base::PlatformFile file_; | 66 base::PlatformFile file_; |
66 }; | 67 }; |
67 | 68 |
68 // A mock that provides methods that can be used as callbacks in asynchronous | 69 // A mock that provides methods that can be used as callbacks in asynchronous |
69 // unzip functions. Tracks the number of calls and number of bytes reported. | 70 // unzip functions. Tracks the number of calls and number of bytes reported. |
70 // Assumes that progress callbacks will be executed in-order. | 71 // Assumes that progress callbacks will be executed in-order. |
71 class MockUnzipListener : public base::SupportsWeakPtr<MockUnzipListener> { | 72 class MockUnzipListener : public base::SupportsWeakPtr<MockUnzipListener> { |
72 public: | 73 public: |
73 MockUnzipListener() | 74 MockUnzipListener() |
74 : success_calls_(0), | 75 : success_calls_(0), |
75 failure_calls_(0), | 76 failure_calls_(0), |
76 progress_calls_(0), | 77 progress_calls_(0), |
77 current_progress_(0) { | 78 current_progress_(0) { |
78 } | 79 } |
79 | 80 |
80 // Success callback for async functions. | 81 // Success callback for async functions. |
81 void OnUnzipSuccess() { | 82 void OnUnzipSuccess() { |
82 success_calls_++; | 83 success_calls_++; |
83 } | 84 } |
(...skipping 462 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
546 | 547 |
547 base::RunLoop().RunUntilIdle(); | 548 base::RunLoop().RunUntilIdle(); |
548 | 549 |
549 EXPECT_EQ(1, listener.success_calls()); | 550 EXPECT_EQ(1, listener.success_calls()); |
550 EXPECT_EQ(0, listener.failure_calls()); | 551 EXPECT_EQ(0, listener.failure_calls()); |
551 EXPECT_GE(0, listener.progress_calls()); | 552 EXPECT_GE(0, listener.progress_calls()); |
552 | 553 |
553 ASSERT_TRUE(base::DirectoryExists(target_file)); | 554 ASSERT_TRUE(base::DirectoryExists(target_file)); |
554 } | 555 } |
555 | 556 |
| 557 TEST_F(ZipReaderTest, ExtractCurrentEntryToString) { |
| 558 // test_mismatch_size.zip contains files with names from 0.txt to 7.txt with |
| 559 // sizes from 0 to 7 bytes respectively, but the metadata in the zip file says |
| 560 // the uncompressed size is 3 bytes. The ZipReader code needs to be clever |
| 561 // enough to get all the data out. |
| 562 base::FilePath test_zip_file = |
| 563 test_data_dir_.AppendASCII("test_mismatch_size.zip"); |
| 564 |
| 565 ZipReader reader; |
| 566 std::string contents; |
| 567 ASSERT_TRUE(reader.Open(test_zip_file)); |
| 568 |
| 569 for (size_t k = 0; k < 8; k++) { |
| 570 SCOPED_TRACE(base::StringPrintf("<loop:%d>", static_cast<int>(k))); |
| 571 |
| 572 base::FilePath file_name = base::FilePath( |
| 573 base::StringPrintf(FILE_PATH_LITERAL("%d.txt"), static_cast<int>(k))); |
| 574 ASSERT_TRUE(reader.LocateAndOpenEntry(file_name)); |
| 575 EXPECT_EQ(3, reader.current_entry_info()->original_size()); |
| 576 |
| 577 if (k > 1) { |
| 578 // Off by one byte read limit: must fail. |
| 579 EXPECT_FALSE(reader.ExtractCurrentEntryToString(k - 1, &contents)); |
| 580 } |
| 581 |
| 582 if (k > 0) { |
| 583 // Exact byte read limit: must pass. |
| 584 EXPECT_TRUE(reader.ExtractCurrentEntryToString(k, &contents)); |
| 585 SCOPED_TRACE(contents); |
| 586 EXPECT_EQ(k, contents.size()); |
| 587 EXPECT_EQ(0, memcmp(contents.c_str(), "0123456", k)); |
| 588 } |
| 589 |
| 590 // More than necessary byte read limit: must pass. |
| 591 EXPECT_TRUE(reader.ExtractCurrentEntryToString(16, &contents)); |
| 592 SCOPED_TRACE(contents); |
| 593 EXPECT_EQ(k, contents.size()); |
| 594 EXPECT_EQ(0, memcmp(contents.c_str(), "0123456", k)); |
| 595 } |
| 596 reader.Close(); |
| 597 } |
| 598 |
556 } // namespace zip | 599 } // namespace zip |
OLD | NEW |