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