alecflett@chromium.org | d6d082e | 2013-05-03 23:02:57 +0000 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "third_party/zlib/google/zip_reader.h" |
| 6 | |
avi | aa96948 | 2015-12-27 13:36:49 -0800 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
| 9 | #include <string.h> |
| 10 | |
alecflett@chromium.org | d6d082e | 2013-05-03 23:02:57 +0000 | [diff] [blame] | 11 | #include <set> |
| 12 | #include <string> |
| 13 | |
haven@chromium.org | 84ed265 | 2014-01-17 00:36:28 +0000 | [diff] [blame] | 14 | #include "base/bind.h" |
rvargas@chromium.org | 0d73765 | 2014-02-27 05:58:13 +0000 | [diff] [blame] | 15 | #include "base/files/file.h" |
thestig | f698159 | 2014-09-22 12:06:21 -0700 | [diff] [blame] | 16 | #include "base/files/file_util.h" |
alecflett@chromium.org | d6d082e | 2013-05-03 23:02:57 +0000 | [diff] [blame] | 17 | #include "base/files/scoped_temp_dir.h" |
| 18 | #include "base/logging.h" |
avi | aa96948 | 2015-12-27 13:36:49 -0800 | [diff] [blame] | 19 | #include "base/macros.h" |
alecflett@chromium.org | d6d082e | 2013-05-03 23:02:57 +0000 | [diff] [blame] | 20 | #include "base/md5.h" |
| 21 | #include "base/path_service.h" |
haven@chromium.org | 84ed265 | 2014-01-17 00:36:28 +0000 | [diff] [blame] | 22 | #include "base/run_loop.h" |
joaoe@opera.com | 0002429 | 2014-06-20 18:12:13 +0000 | [diff] [blame] | 23 | #include "base/strings/stringprintf.h" |
avi@chromium.org | 5cb2477 | 2013-06-07 22:40:45 +0000 | [diff] [blame] | 24 | #include "base/strings/utf_string_conversions.h" |
fdoray | 4354a42 | 2017-04-06 08:54:30 -0700 | [diff] [blame] | 25 | #include "base/test/scoped_task_environment.h" |
avi@chromium.org | 47f1b55 | 2013-06-28 15:23:55 +0000 | [diff] [blame] | 26 | #include "base/time/time.h" |
grt | ebc765a | 2015-03-18 14:22:34 -0700 | [diff] [blame] | 27 | #include "testing/gmock/include/gmock/gmock.h" |
alecflett@chromium.org | d6d082e | 2013-05-03 23:02:57 +0000 | [diff] [blame] | 28 | #include "testing/gtest/include/gtest/gtest.h" |
| 29 | #include "testing/platform_test.h" |
| 30 | #include "third_party/zlib/google/zip_internal.h" |
| 31 | |
grt | ebc765a | 2015-03-18 14:22:34 -0700 | [diff] [blame] | 32 | using ::testing::Return; |
| 33 | using ::testing::_; |
| 34 | |
alecflett@chromium.org | d6d082e | 2013-05-03 23:02:57 +0000 | [diff] [blame] | 35 | namespace { |
| 36 | |
haven@chromium.org | 84ed265 | 2014-01-17 00:36:28 +0000 | [diff] [blame] | 37 | const static std::string kQuuxExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6"; |
| 38 | |
rvargas@chromium.org | 0d73765 | 2014-02-27 05:58:13 +0000 | [diff] [blame] | 39 | class FileWrapper { |
alecflett@chromium.org | d6d082e | 2013-05-03 23:02:57 +0000 | [diff] [blame] | 40 | public: |
| 41 | typedef enum { |
| 42 | READ_ONLY, |
| 43 | READ_WRITE |
| 44 | } AccessMode; |
| 45 | |
rvargas@chromium.org | 0d73765 | 2014-02-27 05:58:13 +0000 | [diff] [blame] | 46 | FileWrapper(const base::FilePath& path, AccessMode mode) { |
| 47 | int flags = base::File::FLAG_READ; |
| 48 | if (mode == READ_ONLY) |
| 49 | flags |= base::File::FLAG_OPEN; |
| 50 | else |
| 51 | flags |= base::File::FLAG_WRITE | base::File::FLAG_CREATE_ALWAYS; |
| 52 | |
| 53 | file_.Initialize(path, flags); |
alecflett@chromium.org | d6d082e | 2013-05-03 23:02:57 +0000 | [diff] [blame] | 54 | } |
| 55 | |
rvargas@chromium.org | 0d73765 | 2014-02-27 05:58:13 +0000 | [diff] [blame] | 56 | ~FileWrapper() {} |
alecflett@chromium.org | d6d082e | 2013-05-03 23:02:57 +0000 | [diff] [blame] | 57 | |
rvargas@chromium.org | 0d73765 | 2014-02-27 05:58:13 +0000 | [diff] [blame] | 58 | base::PlatformFile platform_file() { return file_.GetPlatformFile(); } |
alecflett@chromium.org | d6d082e | 2013-05-03 23:02:57 +0000 | [diff] [blame] | 59 | |
grt | ebc765a | 2015-03-18 14:22:34 -0700 | [diff] [blame] | 60 | base::File* file() { return &file_; } |
| 61 | |
alecflett@chromium.org | d6d082e | 2013-05-03 23:02:57 +0000 | [diff] [blame] | 62 | private: |
rvargas@chromium.org | 0d73765 | 2014-02-27 05:58:13 +0000 | [diff] [blame] | 63 | base::File file_; |
alecflett@chromium.org | d6d082e | 2013-05-03 23:02:57 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
haven@chromium.org | 84ed265 | 2014-01-17 00:36:28 +0000 | [diff] [blame] | 66 | // A mock that provides methods that can be used as callbacks in asynchronous |
| 67 | // unzip functions. Tracks the number of calls and number of bytes reported. |
| 68 | // Assumes that progress callbacks will be executed in-order. |
| 69 | class MockUnzipListener : public base::SupportsWeakPtr<MockUnzipListener> { |
| 70 | public: |
rvargas@chromium.org | 0d73765 | 2014-02-27 05:58:13 +0000 | [diff] [blame] | 71 | MockUnzipListener() |
haven@chromium.org | 84ed265 | 2014-01-17 00:36:28 +0000 | [diff] [blame] | 72 | : success_calls_(0), |
| 73 | failure_calls_(0), |
| 74 | progress_calls_(0), |
| 75 | current_progress_(0) { |
| 76 | } |
| 77 | |
| 78 | // Success callback for async functions. |
| 79 | void OnUnzipSuccess() { |
| 80 | success_calls_++; |
| 81 | } |
| 82 | |
| 83 | // Failure callback for async functions. |
| 84 | void OnUnzipFailure() { |
| 85 | failure_calls_++; |
| 86 | } |
| 87 | |
| 88 | // Progress callback for async functions. |
avi | aa96948 | 2015-12-27 13:36:49 -0800 | [diff] [blame] | 89 | void OnUnzipProgress(int64_t progress) { |
haven@chromium.org | 84ed265 | 2014-01-17 00:36:28 +0000 | [diff] [blame] | 90 | DCHECK(progress > current_progress_); |
| 91 | progress_calls_++; |
| 92 | current_progress_ = progress; |
| 93 | } |
| 94 | |
| 95 | int success_calls() { return success_calls_; } |
| 96 | int failure_calls() { return failure_calls_; } |
| 97 | int progress_calls() { return progress_calls_; } |
| 98 | int current_progress() { return current_progress_; } |
| 99 | |
| 100 | private: |
| 101 | int success_calls_; |
| 102 | int failure_calls_; |
| 103 | int progress_calls_; |
| 104 | |
avi | aa96948 | 2015-12-27 13:36:49 -0800 | [diff] [blame] | 105 | int64_t current_progress_; |
haven@chromium.org | 84ed265 | 2014-01-17 00:36:28 +0000 | [diff] [blame] | 106 | }; |
| 107 | |
grt | ebc765a | 2015-03-18 14:22:34 -0700 | [diff] [blame] | 108 | class MockWriterDelegate : public zip::WriterDelegate { |
| 109 | public: |
| 110 | MOCK_METHOD0(PrepareOutput, bool()); |
| 111 | MOCK_METHOD2(WriteBytes, bool(const char*, int)); |
Joshua Pawlicki | e31b503 | 2018-02-06 20:24:51 +0000 | [diff] [blame] | 112 | MOCK_METHOD1(SetTimeModified, void(const base::Time&)); |
grt | ebc765a | 2015-03-18 14:22:34 -0700 | [diff] [blame] | 113 | }; |
| 114 | |
Joshua Pawlicki | e31b503 | 2018-02-06 20:24:51 +0000 | [diff] [blame] | 115 | bool ExtractCurrentEntryToFilePath(zip::ZipReader* reader, |
| 116 | base::FilePath path) { |
| 117 | zip::FilePathWriterDelegate writer(path); |
| 118 | return reader->ExtractCurrentEntry(&writer, |
| 119 | std::numeric_limits<uint64_t>::max()); |
| 120 | } |
| 121 | |
| 122 | bool LocateAndOpenEntry(zip::ZipReader* reader, |
| 123 | const base::FilePath& path_in_zip) { |
| 124 | // The underlying library can do O(1) access, but ZipReader does not expose |
| 125 | // that. O(N) access is acceptable for these tests. |
| 126 | while (reader->HasMore()) { |
| 127 | if (!reader->OpenCurrentEntryInZip()) |
| 128 | return false; |
| 129 | if (reader->current_entry_info()->file_path() == path_in_zip) |
| 130 | return true; |
| 131 | reader->AdvanceToNextEntry(); |
| 132 | } |
| 133 | return false; |
| 134 | } |
| 135 | |
alecflett@chromium.org | d6d082e | 2013-05-03 23:02:57 +0000 | [diff] [blame] | 136 | } // namespace |
| 137 | |
| 138 | namespace zip { |
| 139 | |
| 140 | // Make the test a PlatformTest to setup autorelease pools properly on Mac. |
| 141 | class ZipReaderTest : public PlatformTest { |
| 142 | protected: |
| 143 | virtual void SetUp() { |
| 144 | PlatformTest::SetUp(); |
| 145 | |
| 146 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
vabr | 40925c7 | 2016-09-28 01:44:50 -0700 | [diff] [blame] | 147 | test_dir_ = temp_dir_.GetPath(); |
alecflett@chromium.org | d6d082e | 2013-05-03 23:02:57 +0000 | [diff] [blame] | 148 | |
| 149 | ASSERT_TRUE(GetTestDataDirectory(&test_data_dir_)); |
| 150 | |
| 151 | test_zip_file_ = test_data_dir_.AppendASCII("test.zip"); |
| 152 | evil_zip_file_ = test_data_dir_.AppendASCII("evil.zip"); |
| 153 | evil_via_invalid_utf8_zip_file_ = test_data_dir_.AppendASCII( |
| 154 | "evil_via_invalid_utf8.zip"); |
| 155 | evil_via_absolute_file_name_zip_file_ = test_data_dir_.AppendASCII( |
| 156 | "evil_via_absolute_file_name.zip"); |
| 157 | |
| 158 | test_zip_contents_.insert(base::FilePath(FILE_PATH_LITERAL("foo/"))); |
| 159 | test_zip_contents_.insert(base::FilePath(FILE_PATH_LITERAL("foo/bar/"))); |
| 160 | test_zip_contents_.insert( |
| 161 | base::FilePath(FILE_PATH_LITERAL("foo/bar/baz.txt"))); |
| 162 | test_zip_contents_.insert( |
| 163 | base::FilePath(FILE_PATH_LITERAL("foo/bar/quux.txt"))); |
| 164 | test_zip_contents_.insert( |
| 165 | base::FilePath(FILE_PATH_LITERAL("foo/bar.txt"))); |
| 166 | test_zip_contents_.insert(base::FilePath(FILE_PATH_LITERAL("foo.txt"))); |
| 167 | test_zip_contents_.insert( |
| 168 | base::FilePath(FILE_PATH_LITERAL("foo/bar/.hidden"))); |
| 169 | } |
| 170 | |
| 171 | virtual void TearDown() { |
| 172 | PlatformTest::TearDown(); |
| 173 | } |
| 174 | |
| 175 | bool GetTestDataDirectory(base::FilePath* path) { |
Avi Drissman | da0819d | 2018-05-07 18:55:12 +0000 | [diff] [blame^] | 176 | bool success = base::PathService::Get(base::DIR_SOURCE_ROOT, path); |
alecflett@chromium.org | d6d082e | 2013-05-03 23:02:57 +0000 | [diff] [blame] | 177 | EXPECT_TRUE(success); |
| 178 | if (!success) |
| 179 | return false; |
| 180 | *path = path->AppendASCII("third_party"); |
| 181 | *path = path->AppendASCII("zlib"); |
| 182 | *path = path->AppendASCII("google"); |
| 183 | *path = path->AppendASCII("test"); |
| 184 | *path = path->AppendASCII("data"); |
| 185 | return true; |
| 186 | } |
| 187 | |
haven@chromium.org | 84ed265 | 2014-01-17 00:36:28 +0000 | [diff] [blame] | 188 | bool CompareFileAndMD5(const base::FilePath& path, |
| 189 | const std::string expected_md5) { |
| 190 | // Read the output file and compute the MD5. |
| 191 | std::string output; |
| 192 | if (!base::ReadFileToString(path, &output)) |
| 193 | return false; |
| 194 | const std::string md5 = base::MD5String(output); |
| 195 | return expected_md5 == md5; |
| 196 | } |
| 197 | |
alecflett@chromium.org | d6d082e | 2013-05-03 23:02:57 +0000 | [diff] [blame] | 198 | // The path to temporary directory used to contain the test operations. |
| 199 | base::FilePath test_dir_; |
| 200 | // The path to the test data directory where test.zip etc. are located. |
| 201 | base::FilePath test_data_dir_; |
| 202 | // The path to test.zip in the test data directory. |
| 203 | base::FilePath test_zip_file_; |
| 204 | // The path to evil.zip in the test data directory. |
| 205 | base::FilePath evil_zip_file_; |
| 206 | // The path to evil_via_invalid_utf8.zip in the test data directory. |
| 207 | base::FilePath evil_via_invalid_utf8_zip_file_; |
| 208 | // The path to evil_via_absolute_file_name.zip in the test data directory. |
| 209 | base::FilePath evil_via_absolute_file_name_zip_file_; |
| 210 | std::set<base::FilePath> test_zip_contents_; |
| 211 | |
| 212 | base::ScopedTempDir temp_dir_; |
haven@chromium.org | 84ed265 | 2014-01-17 00:36:28 +0000 | [diff] [blame] | 213 | |
fdoray | 4354a42 | 2017-04-06 08:54:30 -0700 | [diff] [blame] | 214 | base::test::ScopedTaskEnvironment scoped_task_environment_; |
alecflett@chromium.org | d6d082e | 2013-05-03 23:02:57 +0000 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | TEST_F(ZipReaderTest, Open_ValidZipFile) { |
| 218 | ZipReader reader; |
| 219 | ASSERT_TRUE(reader.Open(test_zip_file_)); |
| 220 | } |
| 221 | |
| 222 | TEST_F(ZipReaderTest, Open_ValidZipPlatformFile) { |
| 223 | ZipReader reader; |
rvargas@chromium.org | 0d73765 | 2014-02-27 05:58:13 +0000 | [diff] [blame] | 224 | FileWrapper zip_fd_wrapper(test_zip_file_, FileWrapper::READ_ONLY); |
alecflett@chromium.org | d6d082e | 2013-05-03 23:02:57 +0000 | [diff] [blame] | 225 | ASSERT_TRUE(reader.OpenFromPlatformFile(zip_fd_wrapper.platform_file())); |
| 226 | } |
| 227 | |
| 228 | TEST_F(ZipReaderTest, Open_NonExistentFile) { |
| 229 | ZipReader reader; |
| 230 | ASSERT_FALSE(reader.Open(test_data_dir_.AppendASCII("nonexistent.zip"))); |
| 231 | } |
| 232 | |
| 233 | TEST_F(ZipReaderTest, Open_ExistentButNonZipFile) { |
| 234 | ZipReader reader; |
| 235 | ASSERT_FALSE(reader.Open(test_data_dir_.AppendASCII("create_test_zip.sh"))); |
| 236 | } |
| 237 | |
| 238 | // Iterate through the contents in the test zip file, and compare that the |
| 239 | // contents collected from the zip reader matches the expected contents. |
| 240 | TEST_F(ZipReaderTest, Iteration) { |
| 241 | std::set<base::FilePath> actual_contents; |
| 242 | ZipReader reader; |
| 243 | ASSERT_TRUE(reader.Open(test_zip_file_)); |
| 244 | while (reader.HasMore()) { |
| 245 | ASSERT_TRUE(reader.OpenCurrentEntryInZip()); |
| 246 | actual_contents.insert(reader.current_entry_info()->file_path()); |
| 247 | ASSERT_TRUE(reader.AdvanceToNextEntry()); |
| 248 | } |
| 249 | EXPECT_FALSE(reader.AdvanceToNextEntry()); // Shouldn't go further. |
| 250 | EXPECT_EQ(test_zip_contents_.size(), |
| 251 | static_cast<size_t>(reader.num_entries())); |
| 252 | EXPECT_EQ(test_zip_contents_.size(), actual_contents.size()); |
| 253 | EXPECT_EQ(test_zip_contents_, actual_contents); |
| 254 | } |
| 255 | |
| 256 | // Open the test zip file from a file descriptor, iterate through its contents, |
| 257 | // and compare that they match the expected contents. |
| 258 | TEST_F(ZipReaderTest, PlatformFileIteration) { |
| 259 | std::set<base::FilePath> actual_contents; |
| 260 | ZipReader reader; |
rvargas@chromium.org | 0d73765 | 2014-02-27 05:58:13 +0000 | [diff] [blame] | 261 | FileWrapper zip_fd_wrapper(test_zip_file_, FileWrapper::READ_ONLY); |
alecflett@chromium.org | d6d082e | 2013-05-03 23:02:57 +0000 | [diff] [blame] | 262 | ASSERT_TRUE(reader.OpenFromPlatformFile(zip_fd_wrapper.platform_file())); |
| 263 | while (reader.HasMore()) { |
| 264 | ASSERT_TRUE(reader.OpenCurrentEntryInZip()); |
| 265 | actual_contents.insert(reader.current_entry_info()->file_path()); |
| 266 | ASSERT_TRUE(reader.AdvanceToNextEntry()); |
| 267 | } |
| 268 | EXPECT_FALSE(reader.AdvanceToNextEntry()); // Shouldn't go further. |
| 269 | EXPECT_EQ(test_zip_contents_.size(), |
| 270 | static_cast<size_t>(reader.num_entries())); |
| 271 | EXPECT_EQ(test_zip_contents_.size(), actual_contents.size()); |
| 272 | EXPECT_EQ(test_zip_contents_, actual_contents); |
| 273 | } |
| 274 | |
alecflett@chromium.org | d6d082e | 2013-05-03 23:02:57 +0000 | [diff] [blame] | 275 | TEST_F(ZipReaderTest, current_entry_info_RegularFile) { |
| 276 | ZipReader reader; |
| 277 | ASSERT_TRUE(reader.Open(test_zip_file_)); |
| 278 | base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); |
Joshua Pawlicki | e31b503 | 2018-02-06 20:24:51 +0000 | [diff] [blame] | 279 | ASSERT_TRUE(LocateAndOpenEntry(&reader, target_path)); |
alecflett@chromium.org | d6d082e | 2013-05-03 23:02:57 +0000 | [diff] [blame] | 280 | ZipReader::EntryInfo* current_entry_info = reader.current_entry_info(); |
| 281 | |
| 282 | EXPECT_EQ(target_path, current_entry_info->file_path()); |
| 283 | EXPECT_EQ(13527, current_entry_info->original_size()); |
| 284 | |
| 285 | // The expected time stamp: 2009-05-29 06:22:20 |
| 286 | base::Time::Exploded exploded = {}; // Zero-clear. |
| 287 | current_entry_info->last_modified().LocalExplode(&exploded); |
| 288 | EXPECT_EQ(2009, exploded.year); |
| 289 | EXPECT_EQ(5, exploded.month); |
| 290 | EXPECT_EQ(29, exploded.day_of_month); |
| 291 | EXPECT_EQ(6, exploded.hour); |
| 292 | EXPECT_EQ(22, exploded.minute); |
| 293 | EXPECT_EQ(20, exploded.second); |
| 294 | EXPECT_EQ(0, exploded.millisecond); |
| 295 | |
| 296 | EXPECT_FALSE(current_entry_info->is_unsafe()); |
| 297 | EXPECT_FALSE(current_entry_info->is_directory()); |
| 298 | } |
| 299 | |
| 300 | TEST_F(ZipReaderTest, current_entry_info_DotDotFile) { |
| 301 | ZipReader reader; |
| 302 | ASSERT_TRUE(reader.Open(evil_zip_file_)); |
| 303 | base::FilePath target_path(FILE_PATH_LITERAL( |
| 304 | "../levilevilevilevilevilevilevilevilevilevilevilevil")); |
Joshua Pawlicki | e31b503 | 2018-02-06 20:24:51 +0000 | [diff] [blame] | 305 | ASSERT_TRUE(LocateAndOpenEntry(&reader, target_path)); |
alecflett@chromium.org | d6d082e | 2013-05-03 23:02:57 +0000 | [diff] [blame] | 306 | ZipReader::EntryInfo* current_entry_info = reader.current_entry_info(); |
| 307 | EXPECT_EQ(target_path, current_entry_info->file_path()); |
| 308 | |
| 309 | // This file is unsafe because of ".." in the file name. |
| 310 | EXPECT_TRUE(current_entry_info->is_unsafe()); |
| 311 | EXPECT_FALSE(current_entry_info->is_directory()); |
| 312 | } |
| 313 | |
| 314 | TEST_F(ZipReaderTest, current_entry_info_InvalidUTF8File) { |
| 315 | ZipReader reader; |
| 316 | ASSERT_TRUE(reader.Open(evil_via_invalid_utf8_zip_file_)); |
| 317 | // The evil file is the 2nd file in the zip file. |
| 318 | // We cannot locate by the file name ".\x80.\\evil.txt", |
| 319 | // as FilePath may internally convert the string. |
| 320 | ASSERT_TRUE(reader.AdvanceToNextEntry()); |
| 321 | ASSERT_TRUE(reader.OpenCurrentEntryInZip()); |
| 322 | ZipReader::EntryInfo* current_entry_info = reader.current_entry_info(); |
| 323 | |
| 324 | // This file is unsafe because of invalid UTF-8 in the file name. |
| 325 | EXPECT_TRUE(current_entry_info->is_unsafe()); |
| 326 | EXPECT_FALSE(current_entry_info->is_directory()); |
| 327 | } |
| 328 | |
| 329 | TEST_F(ZipReaderTest, current_entry_info_AbsoluteFile) { |
| 330 | ZipReader reader; |
| 331 | ASSERT_TRUE(reader.Open(evil_via_absolute_file_name_zip_file_)); |
| 332 | base::FilePath target_path(FILE_PATH_LITERAL("/evil.txt")); |
Joshua Pawlicki | e31b503 | 2018-02-06 20:24:51 +0000 | [diff] [blame] | 333 | ASSERT_TRUE(LocateAndOpenEntry(&reader, target_path)); |
alecflett@chromium.org | d6d082e | 2013-05-03 23:02:57 +0000 | [diff] [blame] | 334 | ZipReader::EntryInfo* current_entry_info = reader.current_entry_info(); |
| 335 | EXPECT_EQ(target_path, current_entry_info->file_path()); |
| 336 | |
| 337 | // This file is unsafe because of the absolute file name. |
| 338 | EXPECT_TRUE(current_entry_info->is_unsafe()); |
| 339 | EXPECT_FALSE(current_entry_info->is_directory()); |
| 340 | } |
| 341 | |
| 342 | TEST_F(ZipReaderTest, current_entry_info_Directory) { |
| 343 | ZipReader reader; |
| 344 | ASSERT_TRUE(reader.Open(test_zip_file_)); |
| 345 | base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/")); |
Joshua Pawlicki | e31b503 | 2018-02-06 20:24:51 +0000 | [diff] [blame] | 346 | ASSERT_TRUE(LocateAndOpenEntry(&reader, target_path)); |
alecflett@chromium.org | d6d082e | 2013-05-03 23:02:57 +0000 | [diff] [blame] | 347 | ZipReader::EntryInfo* current_entry_info = reader.current_entry_info(); |
| 348 | |
| 349 | EXPECT_EQ(base::FilePath(FILE_PATH_LITERAL("foo/bar/")), |
| 350 | current_entry_info->file_path()); |
| 351 | // The directory size should be zero. |
| 352 | EXPECT_EQ(0, current_entry_info->original_size()); |
| 353 | |
| 354 | // The expected time stamp: 2009-05-31 15:49:52 |
| 355 | base::Time::Exploded exploded = {}; // Zero-clear. |
| 356 | current_entry_info->last_modified().LocalExplode(&exploded); |
| 357 | EXPECT_EQ(2009, exploded.year); |
| 358 | EXPECT_EQ(5, exploded.month); |
| 359 | EXPECT_EQ(31, exploded.day_of_month); |
| 360 | EXPECT_EQ(15, exploded.hour); |
| 361 | EXPECT_EQ(49, exploded.minute); |
| 362 | EXPECT_EQ(52, exploded.second); |
| 363 | EXPECT_EQ(0, exploded.millisecond); |
| 364 | |
| 365 | EXPECT_FALSE(current_entry_info->is_unsafe()); |
| 366 | EXPECT_TRUE(current_entry_info->is_directory()); |
| 367 | } |
| 368 | |
| 369 | // Verifies that the ZipReader class can extract a file from a zip archive |
| 370 | // stored in memory. This test opens a zip archive in a std::string object, |
| 371 | // extracts its content, and verifies the content is the same as the expected |
| 372 | // text. |
| 373 | TEST_F(ZipReaderTest, OpenFromString) { |
| 374 | // A zip archive consisting of one file "test.txt", which is a 16-byte text |
| 375 | // file that contains "This is a test.\n". |
| 376 | const char kTestData[] = |
| 377 | "\x50\x4b\x03\x04\x0a\x00\x00\x00\x00\x00\xa4\x66\x24\x41\x13\xe8" |
| 378 | "\xcb\x27\x10\x00\x00\x00\x10\x00\x00\x00\x08\x00\x1c\x00\x74\x65" |
| 379 | "\x73\x74\x2e\x74\x78\x74\x55\x54\x09\x00\x03\x34\x89\x45\x50\x34" |
| 380 | "\x89\x45\x50\x75\x78\x0b\x00\x01\x04\x8e\xf0\x00\x00\x04\x88\x13" |
| 381 | "\x00\x00\x54\x68\x69\x73\x20\x69\x73\x20\x61\x20\x74\x65\x73\x74" |
| 382 | "\x2e\x0a\x50\x4b\x01\x02\x1e\x03\x0a\x00\x00\x00\x00\x00\xa4\x66" |
| 383 | "\x24\x41\x13\xe8\xcb\x27\x10\x00\x00\x00\x10\x00\x00\x00\x08\x00" |
| 384 | "\x18\x00\x00\x00\x00\x00\x01\x00\x00\x00\xa4\x81\x00\x00\x00\x00" |
| 385 | "\x74\x65\x73\x74\x2e\x74\x78\x74\x55\x54\x05\x00\x03\x34\x89\x45" |
| 386 | "\x50\x75\x78\x0b\x00\x01\x04\x8e\xf0\x00\x00\x04\x88\x13\x00\x00" |
| 387 | "\x50\x4b\x05\x06\x00\x00\x00\x00\x01\x00\x01\x00\x4e\x00\x00\x00" |
| 388 | "\x52\x00\x00\x00\x00\x00"; |
| 389 | std::string data(kTestData, arraysize(kTestData)); |
| 390 | ZipReader reader; |
| 391 | ASSERT_TRUE(reader.OpenFromString(data)); |
| 392 | base::FilePath target_path(FILE_PATH_LITERAL("test.txt")); |
Joshua Pawlicki | e31b503 | 2018-02-06 20:24:51 +0000 | [diff] [blame] | 393 | ASSERT_TRUE(LocateAndOpenEntry(&reader, target_path)); |
| 394 | ASSERT_TRUE(ExtractCurrentEntryToFilePath(&reader, |
| 395 | test_dir_.AppendASCII("test.txt"))); |
alecflett@chromium.org | d6d082e | 2013-05-03 23:02:57 +0000 | [diff] [blame] | 396 | |
| 397 | std::string actual; |
brettw@chromium.org | 997408f | 2013-08-30 18:23:50 +0000 | [diff] [blame] | 398 | ASSERT_TRUE(base::ReadFileToString( |
alecflett@chromium.org | d6d082e | 2013-05-03 23:02:57 +0000 | [diff] [blame] | 399 | test_dir_.AppendASCII("test.txt"), &actual)); |
| 400 | EXPECT_EQ(std::string("This is a test.\n"), actual); |
| 401 | } |
| 402 | |
haven@chromium.org | 84ed265 | 2014-01-17 00:36:28 +0000 | [diff] [blame] | 403 | // Verifies that the asynchronous extraction to a file works. |
| 404 | TEST_F(ZipReaderTest, ExtractToFileAsync_RegularFile) { |
| 405 | MockUnzipListener listener; |
| 406 | |
| 407 | ZipReader reader; |
| 408 | base::FilePath target_file = test_dir_.AppendASCII("quux.txt"); |
| 409 | base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); |
| 410 | ASSERT_TRUE(reader.Open(test_zip_file_)); |
Joshua Pawlicki | e31b503 | 2018-02-06 20:24:51 +0000 | [diff] [blame] | 411 | ASSERT_TRUE(LocateAndOpenEntry(&reader, target_path)); |
haven@chromium.org | 84ed265 | 2014-01-17 00:36:28 +0000 | [diff] [blame] | 412 | reader.ExtractCurrentEntryToFilePathAsync( |
| 413 | target_file, |
| 414 | base::Bind(&MockUnzipListener::OnUnzipSuccess, |
| 415 | listener.AsWeakPtr()), |
| 416 | base::Bind(&MockUnzipListener::OnUnzipFailure, |
| 417 | listener.AsWeakPtr()), |
| 418 | base::Bind(&MockUnzipListener::OnUnzipProgress, |
| 419 | listener.AsWeakPtr())); |
| 420 | |
| 421 | EXPECT_EQ(0, listener.success_calls()); |
| 422 | EXPECT_EQ(0, listener.failure_calls()); |
| 423 | EXPECT_EQ(0, listener.progress_calls()); |
| 424 | |
| 425 | base::RunLoop().RunUntilIdle(); |
| 426 | |
| 427 | EXPECT_EQ(1, listener.success_calls()); |
| 428 | EXPECT_EQ(0, listener.failure_calls()); |
| 429 | EXPECT_LE(1, listener.progress_calls()); |
| 430 | |
| 431 | std::string output; |
| 432 | ASSERT_TRUE(base::ReadFileToString(test_dir_.AppendASCII("quux.txt"), |
| 433 | &output)); |
| 434 | const std::string md5 = base::MD5String(output); |
| 435 | EXPECT_EQ(kQuuxExpectedMD5, md5); |
| 436 | |
avi | aa96948 | 2015-12-27 13:36:49 -0800 | [diff] [blame] | 437 | int64_t file_size = 0; |
haven@chromium.org | 84ed265 | 2014-01-17 00:36:28 +0000 | [diff] [blame] | 438 | ASSERT_TRUE(base::GetFileSize(target_file, &file_size)); |
| 439 | |
| 440 | EXPECT_EQ(file_size, listener.current_progress()); |
| 441 | } |
| 442 | |
| 443 | // Verifies that the asynchronous extraction to a file works. |
| 444 | TEST_F(ZipReaderTest, ExtractToFileAsync_Directory) { |
| 445 | MockUnzipListener listener; |
| 446 | |
| 447 | ZipReader reader; |
| 448 | base::FilePath target_file = test_dir_.AppendASCII("foo"); |
| 449 | base::FilePath target_path(FILE_PATH_LITERAL("foo/")); |
| 450 | ASSERT_TRUE(reader.Open(test_zip_file_)); |
Joshua Pawlicki | e31b503 | 2018-02-06 20:24:51 +0000 | [diff] [blame] | 451 | ASSERT_TRUE(LocateAndOpenEntry(&reader, target_path)); |
haven@chromium.org | 84ed265 | 2014-01-17 00:36:28 +0000 | [diff] [blame] | 452 | reader.ExtractCurrentEntryToFilePathAsync( |
| 453 | target_file, |
| 454 | base::Bind(&MockUnzipListener::OnUnzipSuccess, |
| 455 | listener.AsWeakPtr()), |
| 456 | base::Bind(&MockUnzipListener::OnUnzipFailure, |
| 457 | listener.AsWeakPtr()), |
| 458 | base::Bind(&MockUnzipListener::OnUnzipProgress, |
| 459 | listener.AsWeakPtr())); |
| 460 | |
| 461 | EXPECT_EQ(0, listener.success_calls()); |
| 462 | EXPECT_EQ(0, listener.failure_calls()); |
| 463 | EXPECT_EQ(0, listener.progress_calls()); |
| 464 | |
| 465 | base::RunLoop().RunUntilIdle(); |
| 466 | |
| 467 | EXPECT_EQ(1, listener.success_calls()); |
| 468 | EXPECT_EQ(0, listener.failure_calls()); |
| 469 | EXPECT_GE(0, listener.progress_calls()); |
| 470 | |
| 471 | ASSERT_TRUE(base::DirectoryExists(target_file)); |
| 472 | } |
| 473 | |
joaoe@opera.com | 0002429 | 2014-06-20 18:12:13 +0000 | [diff] [blame] | 474 | TEST_F(ZipReaderTest, ExtractCurrentEntryToString) { |
| 475 | // test_mismatch_size.zip contains files with names from 0.txt to 7.txt with |
| 476 | // sizes from 0 to 7 bytes respectively, being the contents of each file a |
| 477 | // substring of "0123456" starting at '0'. |
| 478 | base::FilePath test_zip_file = |
| 479 | test_data_dir_.AppendASCII("test_mismatch_size.zip"); |
| 480 | |
| 481 | ZipReader reader; |
| 482 | std::string contents; |
| 483 | ASSERT_TRUE(reader.Open(test_zip_file)); |
| 484 | |
| 485 | for (size_t i = 0; i < 8; i++) { |
| 486 | SCOPED_TRACE(base::StringPrintf("Processing %d.txt", static_cast<int>(i))); |
| 487 | |
| 488 | base::FilePath file_name = base::FilePath::FromUTF8Unsafe( |
| 489 | base::StringPrintf("%d.txt", static_cast<int>(i))); |
Joshua Pawlicki | e31b503 | 2018-02-06 20:24:51 +0000 | [diff] [blame] | 490 | ASSERT_TRUE(LocateAndOpenEntry(&reader, file_name)); |
joaoe@opera.com | 0002429 | 2014-06-20 18:12:13 +0000 | [diff] [blame] | 491 | |
| 492 | if (i > 1) { |
| 493 | // Off by one byte read limit: must fail. |
| 494 | EXPECT_FALSE(reader.ExtractCurrentEntryToString(i - 1, &contents)); |
| 495 | } |
| 496 | |
| 497 | if (i > 0) { |
| 498 | // Exact byte read limit: must pass. |
| 499 | EXPECT_TRUE(reader.ExtractCurrentEntryToString(i, &contents)); |
mortonm | b4298b0 | 2017-08-04 07:57:41 -0700 | [diff] [blame] | 500 | EXPECT_EQ(base::StringPiece("0123456", i).as_string(), contents); |
joaoe@opera.com | 0002429 | 2014-06-20 18:12:13 +0000 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | // More than necessary byte read limit: must pass. |
| 504 | EXPECT_TRUE(reader.ExtractCurrentEntryToString(16, &contents)); |
mortonm | b4298b0 | 2017-08-04 07:57:41 -0700 | [diff] [blame] | 505 | EXPECT_EQ(base::StringPiece("0123456", i).as_string(), contents); |
joaoe@opera.com | 0002429 | 2014-06-20 18:12:13 +0000 | [diff] [blame] | 506 | } |
| 507 | reader.Close(); |
| 508 | } |
| 509 | |
mortonm | b4298b0 | 2017-08-04 07:57:41 -0700 | [diff] [blame] | 510 | TEST_F(ZipReaderTest, ExtractPartOfCurrentEntry) { |
| 511 | // test_mismatch_size.zip contains files with names from 0.txt to 7.txt with |
| 512 | // sizes from 0 to 7 bytes respectively, being the contents of each file a |
| 513 | // substring of "0123456" starting at '0'. |
| 514 | base::FilePath test_zip_file = |
| 515 | test_data_dir_.AppendASCII("test_mismatch_size.zip"); |
| 516 | |
| 517 | ZipReader reader; |
| 518 | std::string contents; |
| 519 | ASSERT_TRUE(reader.Open(test_zip_file)); |
| 520 | |
| 521 | base::FilePath file_name0 = base::FilePath::FromUTF8Unsafe("0.txt"); |
Joshua Pawlicki | e31b503 | 2018-02-06 20:24:51 +0000 | [diff] [blame] | 522 | ASSERT_TRUE(LocateAndOpenEntry(&reader, file_name0)); |
mortonm | b4298b0 | 2017-08-04 07:57:41 -0700 | [diff] [blame] | 523 | EXPECT_TRUE(reader.ExtractCurrentEntryToString(0, &contents)); |
| 524 | EXPECT_EQ("", contents); |
| 525 | EXPECT_TRUE(reader.ExtractCurrentEntryToString(1, &contents)); |
| 526 | EXPECT_EQ("", contents); |
| 527 | |
| 528 | base::FilePath file_name1 = base::FilePath::FromUTF8Unsafe("1.txt"); |
Joshua Pawlicki | e31b503 | 2018-02-06 20:24:51 +0000 | [diff] [blame] | 529 | ASSERT_TRUE(LocateAndOpenEntry(&reader, file_name1)); |
mortonm | b4298b0 | 2017-08-04 07:57:41 -0700 | [diff] [blame] | 530 | EXPECT_TRUE(reader.ExtractCurrentEntryToString(0, &contents)); |
| 531 | EXPECT_EQ("", contents); |
| 532 | EXPECT_TRUE(reader.ExtractCurrentEntryToString(1, &contents)); |
| 533 | EXPECT_EQ("0", contents); |
| 534 | EXPECT_TRUE(reader.ExtractCurrentEntryToString(2, &contents)); |
| 535 | EXPECT_EQ("0", contents); |
| 536 | |
| 537 | base::FilePath file_name4 = base::FilePath::FromUTF8Unsafe("4.txt"); |
Joshua Pawlicki | e31b503 | 2018-02-06 20:24:51 +0000 | [diff] [blame] | 538 | ASSERT_TRUE(LocateAndOpenEntry(&reader, file_name4)); |
mortonm | b4298b0 | 2017-08-04 07:57:41 -0700 | [diff] [blame] | 539 | EXPECT_TRUE(reader.ExtractCurrentEntryToString(0, &contents)); |
| 540 | EXPECT_EQ("", contents); |
| 541 | EXPECT_FALSE(reader.ExtractCurrentEntryToString(2, &contents)); |
| 542 | EXPECT_EQ("01", contents); |
| 543 | EXPECT_TRUE(reader.ExtractCurrentEntryToString(4, &contents)); |
| 544 | EXPECT_EQ("0123", contents); |
| 545 | // Checks that entire file is extracted and function returns true when |
| 546 | // |max_read_bytes| is larger than file size. |
| 547 | EXPECT_TRUE(reader.ExtractCurrentEntryToString(5, &contents)); |
| 548 | EXPECT_EQ("0123", contents); |
| 549 | |
| 550 | reader.Close(); |
| 551 | } |
| 552 | |
jeremysspiegel | a6bba37 | 2014-11-19 15:53:16 -0800 | [diff] [blame] | 553 | // This test exposes http://crbug.com/430959, at least on OS X |
| 554 | TEST_F(ZipReaderTest, DISABLED_LeakDetectionTest) { |
| 555 | for (int i = 0; i < 100000; ++i) { |
| 556 | FileWrapper zip_fd_wrapper(test_zip_file_, FileWrapper::READ_ONLY); |
| 557 | ZipReader reader; |
| 558 | ASSERT_TRUE(reader.OpenFromPlatformFile(zip_fd_wrapper.platform_file())); |
| 559 | } |
| 560 | } |
| 561 | |
grt | ebc765a | 2015-03-18 14:22:34 -0700 | [diff] [blame] | 562 | // Test that when WriterDelegate::PrepareMock returns false, no other methods on |
| 563 | // the delegate are called and the extraction fails. |
| 564 | TEST_F(ZipReaderTest, ExtractCurrentEntryPrepareFailure) { |
| 565 | testing::StrictMock<MockWriterDelegate> mock_writer; |
| 566 | |
| 567 | EXPECT_CALL(mock_writer, PrepareOutput()) |
| 568 | .WillOnce(Return(false)); |
| 569 | |
| 570 | base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); |
| 571 | ZipReader reader; |
| 572 | |
| 573 | ASSERT_TRUE(reader.Open(test_zip_file_)); |
Joshua Pawlicki | e31b503 | 2018-02-06 20:24:51 +0000 | [diff] [blame] | 574 | ASSERT_TRUE(LocateAndOpenEntry(&reader, target_path)); |
mortonm | b4298b0 | 2017-08-04 07:57:41 -0700 | [diff] [blame] | 575 | ASSERT_FALSE(reader.ExtractCurrentEntry( |
| 576 | &mock_writer, std::numeric_limits<uint64_t>::max())); |
grt | ebc765a | 2015-03-18 14:22:34 -0700 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | // Test that when WriterDelegate::WriteBytes returns false, no other methods on |
| 580 | // the delegate are called and the extraction fails. |
| 581 | TEST_F(ZipReaderTest, ExtractCurrentEntryWriteBytesFailure) { |
| 582 | testing::StrictMock<MockWriterDelegate> mock_writer; |
| 583 | |
| 584 | EXPECT_CALL(mock_writer, PrepareOutput()) |
| 585 | .WillOnce(Return(true)); |
| 586 | EXPECT_CALL(mock_writer, WriteBytes(_, _)) |
| 587 | .WillOnce(Return(false)); |
| 588 | |
| 589 | base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); |
| 590 | ZipReader reader; |
| 591 | |
| 592 | ASSERT_TRUE(reader.Open(test_zip_file_)); |
Joshua Pawlicki | e31b503 | 2018-02-06 20:24:51 +0000 | [diff] [blame] | 593 | ASSERT_TRUE(LocateAndOpenEntry(&reader, target_path)); |
mortonm | b4298b0 | 2017-08-04 07:57:41 -0700 | [diff] [blame] | 594 | ASSERT_FALSE(reader.ExtractCurrentEntry( |
| 595 | &mock_writer, std::numeric_limits<uint64_t>::max())); |
grt | ebc765a | 2015-03-18 14:22:34 -0700 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | // Test that extraction succeeds when the writer delegate reports all is well. |
| 599 | TEST_F(ZipReaderTest, ExtractCurrentEntrySuccess) { |
| 600 | testing::StrictMock<MockWriterDelegate> mock_writer; |
| 601 | |
| 602 | EXPECT_CALL(mock_writer, PrepareOutput()) |
| 603 | .WillOnce(Return(true)); |
| 604 | EXPECT_CALL(mock_writer, WriteBytes(_, _)) |
| 605 | .WillRepeatedly(Return(true)); |
Joshua Pawlicki | e31b503 | 2018-02-06 20:24:51 +0000 | [diff] [blame] | 606 | EXPECT_CALL(mock_writer, SetTimeModified(_)); |
grt | ebc765a | 2015-03-18 14:22:34 -0700 | [diff] [blame] | 607 | |
| 608 | base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); |
| 609 | ZipReader reader; |
| 610 | |
| 611 | ASSERT_TRUE(reader.Open(test_zip_file_)); |
Joshua Pawlicki | e31b503 | 2018-02-06 20:24:51 +0000 | [diff] [blame] | 612 | ASSERT_TRUE(LocateAndOpenEntry(&reader, target_path)); |
mortonm | b4298b0 | 2017-08-04 07:57:41 -0700 | [diff] [blame] | 613 | ASSERT_TRUE(reader.ExtractCurrentEntry(&mock_writer, |
| 614 | std::numeric_limits<uint64_t>::max())); |
grt | ebc765a | 2015-03-18 14:22:34 -0700 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | class FileWriterDelegateTest : public ::testing::Test { |
| 618 | protected: |
| 619 | void SetUp() override { |
| 620 | ASSERT_TRUE(base::CreateTemporaryFile(&temp_file_path_)); |
| 621 | file_.Initialize(temp_file_path_, (base::File::FLAG_CREATE_ALWAYS | |
| 622 | base::File::FLAG_READ | |
| 623 | base::File::FLAG_WRITE | |
| 624 | base::File::FLAG_TEMPORARY | |
| 625 | base::File::FLAG_DELETE_ON_CLOSE)); |
| 626 | ASSERT_TRUE(file_.IsValid()); |
| 627 | } |
| 628 | |
| 629 | // Writes data to the file, leaving the current position at the end of the |
| 630 | // write. |
| 631 | void PopulateFile() { |
| 632 | static const char kSomeData[] = "this sure is some data."; |
| 633 | static const size_t kSomeDataLen = sizeof(kSomeData) - 1; |
| 634 | ASSERT_NE(-1LL, file_.Write(0LL, kSomeData, kSomeDataLen)); |
| 635 | } |
| 636 | |
| 637 | base::FilePath temp_file_path_; |
| 638 | base::File file_; |
| 639 | }; |
| 640 | |
| 641 | TEST_F(FileWriterDelegateTest, WriteToStartAndTruncate) { |
| 642 | // Write stuff and advance. |
| 643 | PopulateFile(); |
| 644 | |
| 645 | // This should rewind, write, then truncate. |
| 646 | static const char kSomeData[] = "short"; |
| 647 | static const int kSomeDataLen = sizeof(kSomeData) - 1; |
| 648 | { |
| 649 | FileWriterDelegate writer(&file_); |
| 650 | ASSERT_TRUE(writer.PrepareOutput()); |
| 651 | ASSERT_TRUE(writer.WriteBytes(kSomeData, kSomeDataLen)); |
| 652 | } |
| 653 | ASSERT_EQ(kSomeDataLen, file_.GetLength()); |
| 654 | char buf[kSomeDataLen] = {}; |
| 655 | ASSERT_EQ(kSomeDataLen, file_.Read(0LL, buf, kSomeDataLen)); |
| 656 | ASSERT_EQ(std::string(kSomeData), std::string(buf, kSomeDataLen)); |
| 657 | } |
| 658 | |
alecflett@chromium.org | d6d082e | 2013-05-03 23:02:57 +0000 | [diff] [blame] | 659 | } // namespace zip |