blob: dabe9f68b0f0d1f85447b531dc8c19ca575e1701 [file] [log] [blame]
alecflett@chromium.orgd6d082e2013-05-03 23:02:57 +00001// 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
aviaa969482015-12-27 13:36:49 -08007#include <stddef.h>
8#include <stdint.h>
9#include <string.h>
10
alecflett@chromium.orgd6d082e2013-05-03 23:02:57 +000011#include <set>
12#include <string>
13
haven@chromium.org84ed2652014-01-17 00:36:28 +000014#include "base/bind.h"
rvargas@chromium.org0d737652014-02-27 05:58:13 +000015#include "base/files/file.h"
thestigf6981592014-09-22 12:06:21 -070016#include "base/files/file_util.h"
alecflett@chromium.orgd6d082e2013-05-03 23:02:57 +000017#include "base/files/scoped_temp_dir.h"
18#include "base/logging.h"
aviaa969482015-12-27 13:36:49 -080019#include "base/macros.h"
alecflett@chromium.orgd6d082e2013-05-03 23:02:57 +000020#include "base/md5.h"
21#include "base/path_service.h"
haven@chromium.org84ed2652014-01-17 00:36:28 +000022#include "base/run_loop.h"
joaoe@opera.com00024292014-06-20 18:12:13 +000023#include "base/strings/stringprintf.h"
avi@chromium.org5cb24772013-06-07 22:40:45 +000024#include "base/strings/utf_string_conversions.h"
fdoray4354a422017-04-06 08:54:30 -070025#include "base/test/scoped_task_environment.h"
avi@chromium.org47f1b552013-06-28 15:23:55 +000026#include "base/time/time.h"
grtebc765a2015-03-18 14:22:34 -070027#include "testing/gmock/include/gmock/gmock.h"
alecflett@chromium.orgd6d082e2013-05-03 23:02:57 +000028#include "testing/gtest/include/gtest/gtest.h"
29#include "testing/platform_test.h"
30#include "third_party/zlib/google/zip_internal.h"
31
grtebc765a2015-03-18 14:22:34 -070032using ::testing::Return;
33using ::testing::_;
34
alecflett@chromium.orgd6d082e2013-05-03 23:02:57 +000035namespace {
36
haven@chromium.org84ed2652014-01-17 00:36:28 +000037const static std::string kQuuxExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6";
38
rvargas@chromium.org0d737652014-02-27 05:58:13 +000039class FileWrapper {
alecflett@chromium.orgd6d082e2013-05-03 23:02:57 +000040 public:
41 typedef enum {
42 READ_ONLY,
43 READ_WRITE
44 } AccessMode;
45
rvargas@chromium.org0d737652014-02-27 05:58:13 +000046 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.orgd6d082e2013-05-03 23:02:57 +000054 }
55
rvargas@chromium.org0d737652014-02-27 05:58:13 +000056 ~FileWrapper() {}
alecflett@chromium.orgd6d082e2013-05-03 23:02:57 +000057
rvargas@chromium.org0d737652014-02-27 05:58:13 +000058 base::PlatformFile platform_file() { return file_.GetPlatformFile(); }
alecflett@chromium.orgd6d082e2013-05-03 23:02:57 +000059
grtebc765a2015-03-18 14:22:34 -070060 base::File* file() { return &file_; }
61
alecflett@chromium.orgd6d082e2013-05-03 23:02:57 +000062 private:
rvargas@chromium.org0d737652014-02-27 05:58:13 +000063 base::File file_;
alecflett@chromium.orgd6d082e2013-05-03 23:02:57 +000064};
65
haven@chromium.org84ed2652014-01-17 00:36:28 +000066// 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.
69class MockUnzipListener : public base::SupportsWeakPtr<MockUnzipListener> {
70 public:
rvargas@chromium.org0d737652014-02-27 05:58:13 +000071 MockUnzipListener()
haven@chromium.org84ed2652014-01-17 00:36:28 +000072 : 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.
aviaa969482015-12-27 13:36:49 -080089 void OnUnzipProgress(int64_t progress) {
haven@chromium.org84ed2652014-01-17 00:36:28 +000090 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
aviaa969482015-12-27 13:36:49 -0800105 int64_t current_progress_;
haven@chromium.org84ed2652014-01-17 00:36:28 +0000106};
107
grtebc765a2015-03-18 14:22:34 -0700108class MockWriterDelegate : public zip::WriterDelegate {
109 public:
110 MOCK_METHOD0(PrepareOutput, bool());
111 MOCK_METHOD2(WriteBytes, bool(const char*, int));
Joshua Pawlickie31b5032018-02-06 20:24:51 +0000112 MOCK_METHOD1(SetTimeModified, void(const base::Time&));
grtebc765a2015-03-18 14:22:34 -0700113};
114
Joshua Pawlickie31b5032018-02-06 20:24:51 +0000115bool 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
122bool 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.orgd6d082e2013-05-03 23:02:57 +0000136} // namespace
137
138namespace zip {
139
140// Make the test a PlatformTest to setup autorelease pools properly on Mac.
141class ZipReaderTest : public PlatformTest {
142 protected:
143 virtual void SetUp() {
144 PlatformTest::SetUp();
145
146 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
vabr40925c72016-09-28 01:44:50 -0700147 test_dir_ = temp_dir_.GetPath();
alecflett@chromium.orgd6d082e2013-05-03 23:02:57 +0000148
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 Drissmanda0819d2018-05-07 18:55:12 +0000176 bool success = base::PathService::Get(base::DIR_SOURCE_ROOT, path);
alecflett@chromium.orgd6d082e2013-05-03 23:02:57 +0000177 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.org84ed2652014-01-17 00:36:28 +0000188 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.orgd6d082e2013-05-03 23:02:57 +0000198 // 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.org84ed2652014-01-17 00:36:28 +0000213
fdoray4354a422017-04-06 08:54:30 -0700214 base::test::ScopedTaskEnvironment scoped_task_environment_;
alecflett@chromium.orgd6d082e2013-05-03 23:02:57 +0000215};
216
217TEST_F(ZipReaderTest, Open_ValidZipFile) {
218 ZipReader reader;
219 ASSERT_TRUE(reader.Open(test_zip_file_));
220}
221
222TEST_F(ZipReaderTest, Open_ValidZipPlatformFile) {
223 ZipReader reader;
rvargas@chromium.org0d737652014-02-27 05:58:13 +0000224 FileWrapper zip_fd_wrapper(test_zip_file_, FileWrapper::READ_ONLY);
alecflett@chromium.orgd6d082e2013-05-03 23:02:57 +0000225 ASSERT_TRUE(reader.OpenFromPlatformFile(zip_fd_wrapper.platform_file()));
226}
227
228TEST_F(ZipReaderTest, Open_NonExistentFile) {
229 ZipReader reader;
230 ASSERT_FALSE(reader.Open(test_data_dir_.AppendASCII("nonexistent.zip")));
231}
232
233TEST_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.
240TEST_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.
258TEST_F(ZipReaderTest, PlatformFileIteration) {
259 std::set<base::FilePath> actual_contents;
260 ZipReader reader;
rvargas@chromium.org0d737652014-02-27 05:58:13 +0000261 FileWrapper zip_fd_wrapper(test_zip_file_, FileWrapper::READ_ONLY);
alecflett@chromium.orgd6d082e2013-05-03 23:02:57 +0000262 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.orgd6d082e2013-05-03 23:02:57 +0000275TEST_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 Pawlickie31b5032018-02-06 20:24:51 +0000279 ASSERT_TRUE(LocateAndOpenEntry(&reader, target_path));
alecflett@chromium.orgd6d082e2013-05-03 23:02:57 +0000280 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
300TEST_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 Pawlickie31b5032018-02-06 20:24:51 +0000305 ASSERT_TRUE(LocateAndOpenEntry(&reader, target_path));
alecflett@chromium.orgd6d082e2013-05-03 23:02:57 +0000306 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
314TEST_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
329TEST_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 Pawlickie31b5032018-02-06 20:24:51 +0000333 ASSERT_TRUE(LocateAndOpenEntry(&reader, target_path));
alecflett@chromium.orgd6d082e2013-05-03 23:02:57 +0000334 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
342TEST_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 Pawlickie31b5032018-02-06 20:24:51 +0000346 ASSERT_TRUE(LocateAndOpenEntry(&reader, target_path));
alecflett@chromium.orgd6d082e2013-05-03 23:02:57 +0000347 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.
373TEST_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 Pawlickie31b5032018-02-06 20:24:51 +0000393 ASSERT_TRUE(LocateAndOpenEntry(&reader, target_path));
394 ASSERT_TRUE(ExtractCurrentEntryToFilePath(&reader,
395 test_dir_.AppendASCII("test.txt")));
alecflett@chromium.orgd6d082e2013-05-03 23:02:57 +0000396
397 std::string actual;
brettw@chromium.org997408f2013-08-30 18:23:50 +0000398 ASSERT_TRUE(base::ReadFileToString(
alecflett@chromium.orgd6d082e2013-05-03 23:02:57 +0000399 test_dir_.AppendASCII("test.txt"), &actual));
400 EXPECT_EQ(std::string("This is a test.\n"), actual);
401}
402
haven@chromium.org84ed2652014-01-17 00:36:28 +0000403// Verifies that the asynchronous extraction to a file works.
404TEST_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 Pawlickie31b5032018-02-06 20:24:51 +0000411 ASSERT_TRUE(LocateAndOpenEntry(&reader, target_path));
haven@chromium.org84ed2652014-01-17 00:36:28 +0000412 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
aviaa969482015-12-27 13:36:49 -0800437 int64_t file_size = 0;
haven@chromium.org84ed2652014-01-17 00:36:28 +0000438 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.
444TEST_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 Pawlickie31b5032018-02-06 20:24:51 +0000451 ASSERT_TRUE(LocateAndOpenEntry(&reader, target_path));
haven@chromium.org84ed2652014-01-17 00:36:28 +0000452 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.com00024292014-06-20 18:12:13 +0000474TEST_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 Pawlickie31b5032018-02-06 20:24:51 +0000490 ASSERT_TRUE(LocateAndOpenEntry(&reader, file_name));
joaoe@opera.com00024292014-06-20 18:12:13 +0000491
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));
mortonmb4298b02017-08-04 07:57:41 -0700500 EXPECT_EQ(base::StringPiece("0123456", i).as_string(), contents);
joaoe@opera.com00024292014-06-20 18:12:13 +0000501 }
502
503 // More than necessary byte read limit: must pass.
504 EXPECT_TRUE(reader.ExtractCurrentEntryToString(16, &contents));
mortonmb4298b02017-08-04 07:57:41 -0700505 EXPECT_EQ(base::StringPiece("0123456", i).as_string(), contents);
joaoe@opera.com00024292014-06-20 18:12:13 +0000506 }
507 reader.Close();
508}
509
mortonmb4298b02017-08-04 07:57:41 -0700510TEST_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 Pawlickie31b5032018-02-06 20:24:51 +0000522 ASSERT_TRUE(LocateAndOpenEntry(&reader, file_name0));
mortonmb4298b02017-08-04 07:57:41 -0700523 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 Pawlickie31b5032018-02-06 20:24:51 +0000529 ASSERT_TRUE(LocateAndOpenEntry(&reader, file_name1));
mortonmb4298b02017-08-04 07:57:41 -0700530 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 Pawlickie31b5032018-02-06 20:24:51 +0000538 ASSERT_TRUE(LocateAndOpenEntry(&reader, file_name4));
mortonmb4298b02017-08-04 07:57:41 -0700539 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
jeremysspiegela6bba372014-11-19 15:53:16 -0800553// This test exposes http://crbug.com/430959, at least on OS X
554TEST_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
grtebc765a2015-03-18 14:22:34 -0700562// Test that when WriterDelegate::PrepareMock returns false, no other methods on
563// the delegate are called and the extraction fails.
564TEST_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 Pawlickie31b5032018-02-06 20:24:51 +0000574 ASSERT_TRUE(LocateAndOpenEntry(&reader, target_path));
mortonmb4298b02017-08-04 07:57:41 -0700575 ASSERT_FALSE(reader.ExtractCurrentEntry(
576 &mock_writer, std::numeric_limits<uint64_t>::max()));
grtebc765a2015-03-18 14:22:34 -0700577}
578
579// Test that when WriterDelegate::WriteBytes returns false, no other methods on
580// the delegate are called and the extraction fails.
581TEST_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 Pawlickie31b5032018-02-06 20:24:51 +0000593 ASSERT_TRUE(LocateAndOpenEntry(&reader, target_path));
mortonmb4298b02017-08-04 07:57:41 -0700594 ASSERT_FALSE(reader.ExtractCurrentEntry(
595 &mock_writer, std::numeric_limits<uint64_t>::max()));
grtebc765a2015-03-18 14:22:34 -0700596}
597
598// Test that extraction succeeds when the writer delegate reports all is well.
599TEST_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 Pawlickie31b5032018-02-06 20:24:51 +0000606 EXPECT_CALL(mock_writer, SetTimeModified(_));
grtebc765a2015-03-18 14:22:34 -0700607
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 Pawlickie31b5032018-02-06 20:24:51 +0000612 ASSERT_TRUE(LocateAndOpenEntry(&reader, target_path));
mortonmb4298b02017-08-04 07:57:41 -0700613 ASSERT_TRUE(reader.ExtractCurrentEntry(&mock_writer,
614 std::numeric_limits<uint64_t>::max()));
grtebc765a2015-03-18 14:22:34 -0700615}
616
617class 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
641TEST_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.orgd6d082e2013-05-03 23:02:57 +0000659} // namespace zip