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

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

Issue 92873003: Adds asynchronous unzip functions to ZipReader (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Removes unnecessary imports. Created 7 years 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
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 <set> 7 #include <set>
8 #include <string> 8 #include <string>
9 9
10 #include "base/bind.h"
10 #include "base/file_util.h" 11 #include "base/file_util.h"
11 #include "base/files/scoped_temp_dir.h" 12 #include "base/files/scoped_temp_dir.h"
12 #include "base/logging.h" 13 #include "base/logging.h"
13 #include "base/md5.h" 14 #include "base/md5.h"
14 #include "base/path_service.h" 15 #include "base/path_service.h"
15 #include "base/platform_file.h" 16 #include "base/platform_file.h"
17 #include "base/run_loop.h"
16 #include "base/strings/utf_string_conversions.h" 18 #include "base/strings/utf_string_conversions.h"
17 #include "base/time/time.h" 19 #include "base/time/time.h"
18 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
19 #include "testing/platform_test.h" 21 #include "testing/platform_test.h"
20 #include "third_party/zlib/google/zip_internal.h" 22 #include "third_party/zlib/google/zip_internal.h"
21 23
22 namespace { 24 namespace {
23 25
26 const static std::string kQuuxExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6";
27
24 // Wrap PlatformFiles in a class so that we don't leak them in tests. 28 // Wrap PlatformFiles in a class so that we don't leak them in tests.
25 class PlatformFileWrapper { 29 class PlatformFileWrapper {
26 public: 30 public:
27 typedef enum { 31 typedef enum {
28 READ_ONLY, 32 READ_ONLY,
29 READ_WRITE 33 READ_WRITE
30 } AccessMode; 34 } AccessMode;
31 35
32 PlatformFileWrapper(const base::FilePath& file, AccessMode mode) 36 PlatformFileWrapper(const base::FilePath& file, AccessMode mode)
33 : file_(base::kInvalidPlatformFileValue) { 37 : file_(base::kInvalidPlatformFileValue) {
(...skipping 20 matching lines...) Expand all
54 ~PlatformFileWrapper() { 58 ~PlatformFileWrapper() {
55 base::ClosePlatformFile(file_); 59 base::ClosePlatformFile(file_);
56 } 60 }
57 61
58 base::PlatformFile platform_file() { return file_; } 62 base::PlatformFile platform_file() { return file_; }
59 63
60 private: 64 private:
61 base::PlatformFile file_; 65 base::PlatformFile file_;
62 }; 66 };
63 67
68 class MockUnzipListener : public base::SupportsWeakPtr<MockUnzipListener> {
satorux1 2013/12/11 06:45:11 Please add a class comment.
Drew Haven 2013/12/11 18:20:53 Done.
69 public:
70 MockUnzipListener()
71 : success_calls(0),
72 failure_calls(0),
73 progress_calls(0),
74 current_progress(0) {
75 }
76
77 void OnUnzipSuccess() {
satorux1 2013/12/11 06:45:11 function comment is missing. maybe something like
Drew Haven 2013/12/11 18:20:53 Done.
78 success_calls++;
79 }
80
81 void OnUnzipFailure() {
82 failure_calls++;
83 }
84
85 void OnUnzipProgress(int64 progress) {
86 progress_calls++;
87 EXPECT_GT(progress, current_progress);
satorux1 2013/12/11 06:45:11 This may be a matter of taste, but I think EXPECT*
Drew Haven 2013/12/11 18:20:53 True. At least a DCHECK gives you a nice stack tr
88 current_progress = progress;
89 }
90
91 int success_calls;
satorux1 2013/12/11 06:45:11 success_calls_
Drew Haven 2013/12/11 18:20:53 Done.
92 int failure_calls;
93 int progress_calls;
94
95 int64 current_progress;
satorux1 2013/12/11 06:45:11 member variables should be private. Please add get
Drew Haven 2013/12/11 18:20:53 Done. I'm really confused why this is preferred o
satorux1 2013/12/12 08:13:01 Non-GMock code is a bit more verbose but anyone ca
96 };
97
64 } // namespace 98 } // namespace
65 99
66 namespace zip { 100 namespace zip {
67 101
68 // Make the test a PlatformTest to setup autorelease pools properly on Mac. 102 // Make the test a PlatformTest to setup autorelease pools properly on Mac.
69 class ZipReaderTest : public PlatformTest { 103 class ZipReaderTest : public PlatformTest {
70 protected: 104 protected:
71 virtual void SetUp() { 105 virtual void SetUp() {
72 PlatformTest::SetUp(); 106 PlatformTest::SetUp();
73 107
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 if (!success) 140 if (!success)
107 return false; 141 return false;
108 *path = path->AppendASCII("third_party"); 142 *path = path->AppendASCII("third_party");
109 *path = path->AppendASCII("zlib"); 143 *path = path->AppendASCII("zlib");
110 *path = path->AppendASCII("google"); 144 *path = path->AppendASCII("google");
111 *path = path->AppendASCII("test"); 145 *path = path->AppendASCII("test");
112 *path = path->AppendASCII("data"); 146 *path = path->AppendASCII("data");
113 return true; 147 return true;
114 } 148 }
115 149
150 void CompareFileAndMD5(const base::FilePath& path,
151 const std::string expected_md5) {
152 // Read the output file and compute the MD5.
153 std::string output;
154 ASSERT_TRUE(base::ReadFileToString(path, &output));
155 const std::string md5 = base::MD5String(output);
156 EXPECT_EQ(expected_md5, md5);
satorux1 2013/12/11 06:45:11 Instead of calling ASSERT and EXPECT here, please
Drew Haven 2013/12/11 18:20:53 Done.
157 }
158
116 // The path to temporary directory used to contain the test operations. 159 // The path to temporary directory used to contain the test operations.
117 base::FilePath test_dir_; 160 base::FilePath test_dir_;
118 // The path to the test data directory where test.zip etc. are located. 161 // The path to the test data directory where test.zip etc. are located.
119 base::FilePath test_data_dir_; 162 base::FilePath test_data_dir_;
120 // The path to test.zip in the test data directory. 163 // The path to test.zip in the test data directory.
121 base::FilePath test_zip_file_; 164 base::FilePath test_zip_file_;
122 // The path to evil.zip in the test data directory. 165 // The path to evil.zip in the test data directory.
123 base::FilePath evil_zip_file_; 166 base::FilePath evil_zip_file_;
124 // The path to evil_via_invalid_utf8.zip in the test data directory. 167 // The path to evil_via_invalid_utf8.zip in the test data directory.
125 base::FilePath evil_via_invalid_utf8_zip_file_; 168 base::FilePath evil_via_invalid_utf8_zip_file_;
126 // The path to evil_via_absolute_file_name.zip in the test data directory. 169 // The path to evil_via_absolute_file_name.zip in the test data directory.
127 base::FilePath evil_via_absolute_file_name_zip_file_; 170 base::FilePath evil_via_absolute_file_name_zip_file_;
128 std::set<base::FilePath> test_zip_contents_; 171 std::set<base::FilePath> test_zip_contents_;
129 172
130 base::ScopedTempDir temp_dir_; 173 base::ScopedTempDir temp_dir_;
174
175 base::MessageLoop message_loop_;
131 }; 176 };
132 177
133 TEST_F(ZipReaderTest, Open_ValidZipFile) { 178 TEST_F(ZipReaderTest, Open_ValidZipFile) {
134 ZipReader reader; 179 ZipReader reader;
135 ASSERT_TRUE(reader.Open(test_zip_file_)); 180 ASSERT_TRUE(reader.Open(test_zip_file_));
136 } 181 }
137 182
138 TEST_F(ZipReaderTest, Open_ValidZipPlatformFile) { 183 TEST_F(ZipReaderTest, Open_ValidZipPlatformFile) {
139 ZipReader reader; 184 ZipReader reader;
140 PlatformFileWrapper zip_fd_wrapper(test_zip_file_, 185 PlatformFileWrapper zip_fd_wrapper(test_zip_file_,
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 ASSERT_TRUE(reader.Open(test_zip_file_)); 258 ASSERT_TRUE(reader.Open(test_zip_file_));
214 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); 259 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt"));
215 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); 260 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path));
216 ASSERT_TRUE(reader.ExtractCurrentEntryToFilePath( 261 ASSERT_TRUE(reader.ExtractCurrentEntryToFilePath(
217 test_dir_.AppendASCII("quux.txt"))); 262 test_dir_.AppendASCII("quux.txt")));
218 // Read the output file ans compute the MD5. 263 // Read the output file ans compute the MD5.
219 std::string output; 264 std::string output;
220 ASSERT_TRUE(base::ReadFileToString(test_dir_.AppendASCII("quux.txt"), 265 ASSERT_TRUE(base::ReadFileToString(test_dir_.AppendASCII("quux.txt"),
221 &output)); 266 &output));
222 const std::string md5 = base::MD5String(output); 267 const std::string md5 = base::MD5String(output);
223 const std::string kExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6"; 268 EXPECT_EQ(kQuuxExpectedMD5, md5);
224 EXPECT_EQ(kExpectedMD5, md5);
225 // quux.txt should be larger than kZipBufSize so that we can exercise 269 // quux.txt should be larger than kZipBufSize so that we can exercise
226 // the loop in ExtractCurrentEntry(). 270 // the loop in ExtractCurrentEntry().
227 EXPECT_LT(static_cast<size_t>(internal::kZipBufSize), output.size()); 271 EXPECT_LT(static_cast<size_t>(internal::kZipBufSize), output.size());
228 } 272 }
229 273
230 TEST_F(ZipReaderTest, PlatformFileExtractCurrentEntryToFilePath_RegularFile) { 274 TEST_F(ZipReaderTest, PlatformFileExtractCurrentEntryToFilePath_RegularFile) {
231 ZipReader reader; 275 ZipReader reader;
232 PlatformFileWrapper zip_fd_wrapper(test_zip_file_, 276 PlatformFileWrapper zip_fd_wrapper(test_zip_file_,
233 PlatformFileWrapper::READ_ONLY); 277 PlatformFileWrapper::READ_ONLY);
234 ASSERT_TRUE(reader.OpenFromPlatformFile(zip_fd_wrapper.platform_file())); 278 ASSERT_TRUE(reader.OpenFromPlatformFile(zip_fd_wrapper.platform_file()));
235 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); 279 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt"));
236 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); 280 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path));
237 ASSERT_TRUE(reader.ExtractCurrentEntryToFilePath( 281 ASSERT_TRUE(reader.ExtractCurrentEntryToFilePath(
238 test_dir_.AppendASCII("quux.txt"))); 282 test_dir_.AppendASCII("quux.txt")));
239 // Read the output file and compute the MD5. 283 // Read the output file and compute the MD5.
240 std::string output; 284 std::string output;
241 ASSERT_TRUE(base::ReadFileToString(test_dir_.AppendASCII("quux.txt"), 285 ASSERT_TRUE(base::ReadFileToString(test_dir_.AppendASCII("quux.txt"),
242 &output)); 286 &output));
243 const std::string md5 = base::MD5String(output); 287 const std::string md5 = base::MD5String(output);
244 const std::string kExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6"; 288 EXPECT_EQ(kQuuxExpectedMD5, md5);
245 EXPECT_EQ(kExpectedMD5, md5);
246 // quux.txt should be larger than kZipBufSize so that we can exercise 289 // quux.txt should be larger than kZipBufSize so that we can exercise
247 // the loop in ExtractCurrentEntry(). 290 // the loop in ExtractCurrentEntry().
248 EXPECT_LT(static_cast<size_t>(internal::kZipBufSize), output.size()); 291 EXPECT_LT(static_cast<size_t>(internal::kZipBufSize), output.size());
249 } 292 }
250 293
251 #if defined(OS_POSIX) 294 #if defined(OS_POSIX)
252 TEST_F(ZipReaderTest, PlatformFileExtractCurrentEntryToFd_RegularFile) { 295 TEST_F(ZipReaderTest, PlatformFileExtractCurrentEntryToFd_RegularFile) {
253 ZipReader reader; 296 ZipReader reader;
254 PlatformFileWrapper zip_fd_wrapper(test_zip_file_, 297 PlatformFileWrapper zip_fd_wrapper(test_zip_file_,
255 PlatformFileWrapper::READ_ONLY); 298 PlatformFileWrapper::READ_ONLY);
256 ASSERT_TRUE(reader.OpenFromPlatformFile(zip_fd_wrapper.platform_file())); 299 ASSERT_TRUE(reader.OpenFromPlatformFile(zip_fd_wrapper.platform_file()));
257 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); 300 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt"));
258 base::FilePath out_path = test_dir_.AppendASCII("quux.txt"); 301 base::FilePath out_path = test_dir_.AppendASCII("quux.txt");
259 PlatformFileWrapper out_fd_w(out_path, PlatformFileWrapper::READ_WRITE); 302 PlatformFileWrapper out_fd_w(out_path, PlatformFileWrapper::READ_WRITE);
260 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); 303 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path));
261 ASSERT_TRUE(reader.ExtractCurrentEntryToFd(out_fd_w.platform_file())); 304 ASSERT_TRUE(reader.ExtractCurrentEntryToFd(out_fd_w.platform_file()));
262 // Read the output file and compute the MD5. 305 // Read the output file and compute the MD5.
263 std::string output; 306 std::string output;
264 ASSERT_TRUE(base::ReadFileToString(test_dir_.AppendASCII("quux.txt"), 307 ASSERT_TRUE(base::ReadFileToString(test_dir_.AppendASCII("quux.txt"),
265 &output)); 308 &output));
266 const std::string md5 = base::MD5String(output); 309 const std::string md5 = base::MD5String(output);
267 const std::string kExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6"; 310 EXPECT_EQ(kQuuxExpectedMD5, md5);
268 EXPECT_EQ(kExpectedMD5, md5);
269 // quux.txt should be larger than kZipBufSize so that we can exercise 311 // quux.txt should be larger than kZipBufSize so that we can exercise
270 // the loop in ExtractCurrentEntry(). 312 // the loop in ExtractCurrentEntry().
271 EXPECT_LT(static_cast<size_t>(internal::kZipBufSize), output.size()); 313 EXPECT_LT(static_cast<size_t>(internal::kZipBufSize), output.size());
272 } 314 }
273 #endif 315 #endif
274 316
275 TEST_F(ZipReaderTest, ExtractCurrentEntryToFilePath_Directory) { 317 TEST_F(ZipReaderTest, ExtractCurrentEntryToFilePath_Directory) {
276 ZipReader reader; 318 ZipReader reader;
277 ASSERT_TRUE(reader.Open(test_zip_file_)); 319 ASSERT_TRUE(reader.Open(test_zip_file_));
278 base::FilePath target_path(FILE_PATH_LITERAL("foo/")); 320 base::FilePath target_path(FILE_PATH_LITERAL("foo/"));
(...skipping 10 matching lines...) Expand all
289 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); 331 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt"));
290 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); 332 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path));
291 ASSERT_TRUE(reader.ExtractCurrentEntryIntoDirectory(test_dir_)); 333 ASSERT_TRUE(reader.ExtractCurrentEntryIntoDirectory(test_dir_));
292 // Sub directories should be created. 334 // Sub directories should be created.
293 ASSERT_TRUE(base::DirectoryExists(test_dir_.AppendASCII("foo/bar"))); 335 ASSERT_TRUE(base::DirectoryExists(test_dir_.AppendASCII("foo/bar")));
294 // And the file should be created. 336 // And the file should be created.
295 std::string output; 337 std::string output;
296 ASSERT_TRUE(base::ReadFileToString( 338 ASSERT_TRUE(base::ReadFileToString(
297 test_dir_.AppendASCII("foo/bar/quux.txt"), &output)); 339 test_dir_.AppendASCII("foo/bar/quux.txt"), &output));
298 const std::string md5 = base::MD5String(output); 340 const std::string md5 = base::MD5String(output);
299 const std::string kExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6"; 341 EXPECT_EQ(kQuuxExpectedMD5, md5);
300 EXPECT_EQ(kExpectedMD5, md5);
301 } 342 }
302 343
303 TEST_F(ZipReaderTest, current_entry_info_RegularFile) { 344 TEST_F(ZipReaderTest, current_entry_info_RegularFile) {
304 ZipReader reader; 345 ZipReader reader;
305 ASSERT_TRUE(reader.Open(test_zip_file_)); 346 ASSERT_TRUE(reader.Open(test_zip_file_));
306 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); 347 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt"));
307 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); 348 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path));
308 ZipReader::EntryInfo* current_entry_info = reader.current_entry_info(); 349 ZipReader::EntryInfo* current_entry_info = reader.current_entry_info();
309 350
310 EXPECT_EQ(target_path, current_entry_info->file_path()); 351 EXPECT_EQ(target_path, current_entry_info->file_path());
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
421 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); 462 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path));
422 ASSERT_TRUE(reader.ExtractCurrentEntryToFilePath( 463 ASSERT_TRUE(reader.ExtractCurrentEntryToFilePath(
423 test_dir_.AppendASCII("test.txt"))); 464 test_dir_.AppendASCII("test.txt")));
424 465
425 std::string actual; 466 std::string actual;
426 ASSERT_TRUE(base::ReadFileToString( 467 ASSERT_TRUE(base::ReadFileToString(
427 test_dir_.AppendASCII("test.txt"), &actual)); 468 test_dir_.AppendASCII("test.txt"), &actual));
428 EXPECT_EQ(std::string("This is a test.\n"), actual); 469 EXPECT_EQ(std::string("This is a test.\n"), actual);
429 } 470 }
430 471
472 // Verifies that the asynchronous extraction to a file works.
473 TEST_F(ZipReaderTest, ExtractToFileAsync_RegularFile) {
474 MockUnzipListener listener;
475
476 ZipReader reader;
477 base::FilePath target_file = test_dir_.AppendASCII("quux.txt");
478 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt"));
479 ASSERT_TRUE(reader.Open(test_zip_file_));
480 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path));
481 reader.ExtractCurrentEntryToFilePathAsync(
482 target_file,
483 base::Bind(&MockUnzipListener::OnUnzipSuccess,
484 listener.AsWeakPtr()),
485 base::Bind(&MockUnzipListener::OnUnzipFailure,
486 listener.AsWeakPtr()),
487 base::Bind(&MockUnzipListener::OnUnzipProgress,
488 listener.AsWeakPtr()));
489
490 EXPECT_EQ(0, listener.success_calls);
491 EXPECT_EQ(0, listener.failure_calls);
492 EXPECT_EQ(0, listener.progress_calls);
493
494 base::RunLoop().RunUntilIdle();
495
496 EXPECT_EQ(1, listener.success_calls);
497 EXPECT_EQ(0, listener.failure_calls);
498 EXPECT_GE(listener.progress_calls, 1);
499
500 CompareFileAndMD5(target_file, kQuuxExpectedMD5);
501
502 int64 file_size;
satorux1 2013/12/11 06:45:11 = 0; to make it initialized. doesn't matter here,
Drew Haven 2013/12/11 18:20:53 Done.
503 ASSERT_TRUE(base::GetFileSize(target_file, &file_size));
504
505 EXPECT_EQ(listener.current_progress, file_size);
506 }
507
508 // Verifies that the asynchronous extraction to a file works.
509 TEST_F(ZipReaderTest, ExtractToFileAsync_Directory) {
510 MockUnzipListener listener;
511
512 ZipReader reader;
513 base::FilePath target_path(FILE_PATH_LITERAL("foo/"));
514 ASSERT_TRUE(reader.Open(test_zip_file_));
515 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path));
516 reader.ExtractCurrentEntryToFilePathAsync(
517 target_path,
518 base::Bind(&MockUnzipListener::OnUnzipSuccess,
519 listener.AsWeakPtr()),
520 base::Bind(&MockUnzipListener::OnUnzipFailure,
521 listener.AsWeakPtr()),
522 base::Bind(&MockUnzipListener::OnUnzipProgress,
523 listener.AsWeakPtr()));
524
525 EXPECT_EQ(1, listener.success_calls);
526 EXPECT_EQ(0, listener.failure_calls);
527 EXPECT_GE(0, listener.progress_calls);
528
529 ASSERT_TRUE(base::DirectoryExists(target_path));
530 }
531
431 } // namespace zip 532 } // namespace zip
OLDNEW
« third_party/zlib/google/zip_reader.cc ('K') | « 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