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

Side by Side Diff: chrome/common/zip_reader_unittest.cc

Issue 13257004: [components] Make zip a component so that src/chromeos can use it. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: DEPS, OWNERS Created 7 years, 8 months 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 | Annotate | Revision Log
« no previous file with comments | « chrome/common/zip_reader.cc ('k') | chrome/common/zip_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 "chrome/common/zip_reader.h"
6
7 #include <set>
8 #include <string>
9
10 #include "base/file_util.h"
11 #include "base/files/scoped_temp_dir.h"
12 #include "base/logging.h"
13 #include "base/md5.h"
14 #include "base/path_service.h"
15 #include "base/platform_file.h"
16 #include "base/time.h"
17 #include "base/utf_string_conversions.h"
18 #include "chrome/common/chrome_paths.h"
19 #include "chrome/common/zip_internal.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21 #include "testing/platform_test.h"
22
23 namespace {
24
25 // Wrap PlatformFiles in a class so that we don't leak them in tests.
26 class PlatformFileWrapper {
27 public:
28 typedef enum {
29 READ_ONLY,
30 READ_WRITE
31 } AccessMode;
32
33 PlatformFileWrapper(const base::FilePath& file, AccessMode mode)
34 : file_(base::kInvalidPlatformFileValue) {
35 switch (mode) {
36 case READ_ONLY:
37 file_ = base::CreatePlatformFile(file,
38 base::PLATFORM_FILE_OPEN |
39 base::PLATFORM_FILE_READ,
40 NULL, NULL);
41 break;
42 case READ_WRITE:
43 file_ = base::CreatePlatformFile(file,
44 base::PLATFORM_FILE_CREATE_ALWAYS |
45 base::PLATFORM_FILE_READ |
46 base::PLATFORM_FILE_WRITE,
47 NULL, NULL);
48 break;
49 default:
50 NOTREACHED();
51 }
52 return;
53 }
54
55 ~PlatformFileWrapper() {
56 base::ClosePlatformFile(file_);
57 }
58
59 base::PlatformFile platform_file() { return file_; }
60
61 private:
62 base::PlatformFile file_;
63 };
64
65 } // namespace
66
67 namespace zip {
68
69 // Make the test a PlatformTest to setup autorelease pools properly on Mac.
70 class ZipReaderTest : public PlatformTest {
71 protected:
72 virtual void SetUp() {
73 PlatformTest::SetUp();
74
75 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
76 test_dir_ = temp_dir_.path();
77
78 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_));
79 test_data_dir_ = test_data_dir_.AppendASCII("zip");
80
81 test_zip_file_ = test_data_dir_.AppendASCII("test.zip");
82 evil_zip_file_ = test_data_dir_.AppendASCII("evil.zip");
83 evil_via_invalid_utf8_zip_file_ = test_data_dir_.AppendASCII(
84 "evil_via_invalid_utf8.zip");
85 evil_via_absolute_file_name_zip_file_ = test_data_dir_.AppendASCII(
86 "evil_via_absolute_file_name.zip");
87
88 test_zip_contents_.insert(base::FilePath(FILE_PATH_LITERAL("foo/")));
89 test_zip_contents_.insert(base::FilePath(FILE_PATH_LITERAL("foo/bar/")));
90 test_zip_contents_.insert(
91 base::FilePath(FILE_PATH_LITERAL("foo/bar/baz.txt")));
92 test_zip_contents_.insert(
93 base::FilePath(FILE_PATH_LITERAL("foo/bar/quux.txt")));
94 test_zip_contents_.insert(
95 base::FilePath(FILE_PATH_LITERAL("foo/bar.txt")));
96 test_zip_contents_.insert(base::FilePath(FILE_PATH_LITERAL("foo.txt")));
97 test_zip_contents_.insert(
98 base::FilePath(FILE_PATH_LITERAL("foo/bar/.hidden")));
99 }
100
101 virtual void TearDown() {
102 PlatformTest::TearDown();
103 }
104
105 // The path to temporary directory used to contain the test operations.
106 base::FilePath test_dir_;
107 // The path to the test data directory where test.zip etc. are located.
108 base::FilePath test_data_dir_;
109 // The path to test.zip in the test data directory.
110 base::FilePath test_zip_file_;
111 // The path to evil.zip in the test data directory.
112 base::FilePath evil_zip_file_;
113 // The path to evil_via_invalid_utf8.zip in the test data directory.
114 base::FilePath evil_via_invalid_utf8_zip_file_;
115 // The path to evil_via_absolute_file_name.zip in the test data directory.
116 base::FilePath evil_via_absolute_file_name_zip_file_;
117 std::set<base::FilePath> test_zip_contents_;
118
119 base::ScopedTempDir temp_dir_;
120 };
121
122 TEST_F(ZipReaderTest, Open_ValidZipFile) {
123 ZipReader reader;
124 ASSERT_TRUE(reader.Open(test_zip_file_));
125 }
126
127 TEST_F(ZipReaderTest, Open_ValidZipPlatformFile) {
128 ZipReader reader;
129 PlatformFileWrapper zip_fd_wrapper(test_zip_file_,
130 PlatformFileWrapper::READ_ONLY);
131 ASSERT_TRUE(reader.OpenFromPlatformFile(zip_fd_wrapper.platform_file()));
132 }
133
134 TEST_F(ZipReaderTest, Open_NonExistentFile) {
135 ZipReader reader;
136 ASSERT_FALSE(reader.Open(test_data_dir_.AppendASCII("nonexistent.zip")));
137 }
138
139 TEST_F(ZipReaderTest, Open_ExistentButNonZipFile) {
140 ZipReader reader;
141 ASSERT_FALSE(reader.Open(test_data_dir_.AppendASCII("create_test_zip.sh")));
142 }
143
144 // Iterate through the contents in the test zip file, and compare that the
145 // contents collected from the zip reader matches the expected contents.
146 TEST_F(ZipReaderTest, Iteration) {
147 std::set<base::FilePath> actual_contents;
148 ZipReader reader;
149 ASSERT_TRUE(reader.Open(test_zip_file_));
150 while (reader.HasMore()) {
151 ASSERT_TRUE(reader.OpenCurrentEntryInZip());
152 actual_contents.insert(reader.current_entry_info()->file_path());
153 ASSERT_TRUE(reader.AdvanceToNextEntry());
154 }
155 EXPECT_FALSE(reader.AdvanceToNextEntry()); // Shouldn't go further.
156 EXPECT_EQ(test_zip_contents_.size(),
157 static_cast<size_t>(reader.num_entries()));
158 EXPECT_EQ(test_zip_contents_.size(), actual_contents.size());
159 EXPECT_EQ(test_zip_contents_, actual_contents);
160 }
161
162 // Open the test zip file from a file descriptor, iterate through its contents,
163 // and compare that they match the expected contents.
164 TEST_F(ZipReaderTest, PlatformFileIteration) {
165 std::set<base::FilePath> actual_contents;
166 ZipReader reader;
167 PlatformFileWrapper zip_fd_wrapper(test_zip_file_,
168 PlatformFileWrapper::READ_ONLY);
169 ASSERT_TRUE(reader.OpenFromPlatformFile(zip_fd_wrapper.platform_file()));
170 while (reader.HasMore()) {
171 ASSERT_TRUE(reader.OpenCurrentEntryInZip());
172 actual_contents.insert(reader.current_entry_info()->file_path());
173 ASSERT_TRUE(reader.AdvanceToNextEntry());
174 }
175 EXPECT_FALSE(reader.AdvanceToNextEntry()); // Shouldn't go further.
176 EXPECT_EQ(test_zip_contents_.size(),
177 static_cast<size_t>(reader.num_entries()));
178 EXPECT_EQ(test_zip_contents_.size(), actual_contents.size());
179 EXPECT_EQ(test_zip_contents_, actual_contents);
180 }
181
182 TEST_F(ZipReaderTest, LocateAndOpenEntry_ValidFile) {
183 std::set<base::FilePath> actual_contents;
184 ZipReader reader;
185 ASSERT_TRUE(reader.Open(test_zip_file_));
186 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt"));
187 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path));
188 EXPECT_EQ(target_path, reader.current_entry_info()->file_path());
189 }
190
191 TEST_F(ZipReaderTest, LocateAndOpenEntry_NonExistentFile) {
192 std::set<base::FilePath> actual_contents;
193 ZipReader reader;
194 ASSERT_TRUE(reader.Open(test_zip_file_));
195 base::FilePath target_path(FILE_PATH_LITERAL("nonexistent.txt"));
196 ASSERT_FALSE(reader.LocateAndOpenEntry(target_path));
197 EXPECT_EQ(NULL, reader.current_entry_info());
198 }
199
200 TEST_F(ZipReaderTest, ExtractCurrentEntryToFilePath_RegularFile) {
201 ZipReader reader;
202 ASSERT_TRUE(reader.Open(test_zip_file_));
203 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt"));
204 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path));
205 ASSERT_TRUE(reader.ExtractCurrentEntryToFilePath(
206 test_dir_.AppendASCII("quux.txt")));
207 // Read the output file ans compute the MD5.
208 std::string output;
209 ASSERT_TRUE(file_util::ReadFileToString(test_dir_.AppendASCII("quux.txt"),
210 &output));
211 const std::string md5 = base::MD5String(output);
212 const std::string kExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6";
213 EXPECT_EQ(kExpectedMD5, md5);
214 // quux.txt should be larger than kZipBufSize so that we can exercise
215 // the loop in ExtractCurrentEntry().
216 EXPECT_LT(static_cast<size_t>(internal::kZipBufSize), output.size());
217 }
218
219 TEST_F(ZipReaderTest, PlatformFileExtractCurrentEntryToFilePath_RegularFile) {
220 ZipReader reader;
221 PlatformFileWrapper zip_fd_wrapper(test_zip_file_,
222 PlatformFileWrapper::READ_ONLY);
223 ASSERT_TRUE(reader.OpenFromPlatformFile(zip_fd_wrapper.platform_file()));
224 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt"));
225 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path));
226 ASSERT_TRUE(reader.ExtractCurrentEntryToFilePath(
227 test_dir_.AppendASCII("quux.txt")));
228 // Read the output file and compute the MD5.
229 std::string output;
230 ASSERT_TRUE(file_util::ReadFileToString(test_dir_.AppendASCII("quux.txt"),
231 &output));
232 const std::string md5 = base::MD5String(output);
233 const std::string kExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6";
234 EXPECT_EQ(kExpectedMD5, md5);
235 // quux.txt should be larger than kZipBufSize so that we can exercise
236 // the loop in ExtractCurrentEntry().
237 EXPECT_LT(static_cast<size_t>(internal::kZipBufSize), output.size());
238 }
239
240 #if defined(OS_POSIX)
241 TEST_F(ZipReaderTest, PlatformFileExtractCurrentEntryToFd_RegularFile) {
242 ZipReader reader;
243 PlatformFileWrapper zip_fd_wrapper(test_zip_file_,
244 PlatformFileWrapper::READ_ONLY);
245 ASSERT_TRUE(reader.OpenFromPlatformFile(zip_fd_wrapper.platform_file()));
246 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt"));
247 base::FilePath out_path = test_dir_.AppendASCII("quux.txt");
248 PlatformFileWrapper out_fd_w(out_path, PlatformFileWrapper::READ_WRITE);
249 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path));
250 ASSERT_TRUE(reader.ExtractCurrentEntryToFd(out_fd_w.platform_file()));
251 // Read the output file and compute the MD5.
252 std::string output;
253 ASSERT_TRUE(file_util::ReadFileToString(test_dir_.AppendASCII("quux.txt"),
254 &output));
255 const std::string md5 = base::MD5String(output);
256 const std::string kExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6";
257 EXPECT_EQ(kExpectedMD5, md5);
258 // quux.txt should be larger than kZipBufSize so that we can exercise
259 // the loop in ExtractCurrentEntry().
260 EXPECT_LT(static_cast<size_t>(internal::kZipBufSize), output.size());
261 }
262 #endif
263
264 TEST_F(ZipReaderTest, ExtractCurrentEntryToFilePath_Directory) {
265 ZipReader reader;
266 ASSERT_TRUE(reader.Open(test_zip_file_));
267 base::FilePath target_path(FILE_PATH_LITERAL("foo/"));
268 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path));
269 ASSERT_TRUE(reader.ExtractCurrentEntryToFilePath(
270 test_dir_.AppendASCII("foo")));
271 // The directory should be created.
272 ASSERT_TRUE(file_util::DirectoryExists(test_dir_.AppendASCII("foo")));
273 }
274
275 TEST_F(ZipReaderTest, ExtractCurrentEntryIntoDirectory_RegularFile) {
276 ZipReader reader;
277 ASSERT_TRUE(reader.Open(test_zip_file_));
278 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt"));
279 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path));
280 ASSERT_TRUE(reader.ExtractCurrentEntryIntoDirectory(test_dir_));
281 // Sub directories should be created.
282 ASSERT_TRUE(file_util::DirectoryExists(test_dir_.AppendASCII("foo/bar")));
283 // And the file should be created.
284 std::string output;
285 ASSERT_TRUE(file_util::ReadFileToString(
286 test_dir_.AppendASCII("foo/bar/quux.txt"), &output));
287 const std::string md5 = base::MD5String(output);
288 const std::string kExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6";
289 EXPECT_EQ(kExpectedMD5, md5);
290 }
291
292 TEST_F(ZipReaderTest, current_entry_info_RegularFile) {
293 ZipReader reader;
294 ASSERT_TRUE(reader.Open(test_zip_file_));
295 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt"));
296 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path));
297 ZipReader::EntryInfo* current_entry_info = reader.current_entry_info();
298
299 EXPECT_EQ(target_path, current_entry_info->file_path());
300 EXPECT_EQ(13527, current_entry_info->original_size());
301
302 // The expected time stamp: 2009-05-29 06:22:20
303 base::Time::Exploded exploded = {}; // Zero-clear.
304 current_entry_info->last_modified().LocalExplode(&exploded);
305 EXPECT_EQ(2009, exploded.year);
306 EXPECT_EQ(5, exploded.month);
307 EXPECT_EQ(29, exploded.day_of_month);
308 EXPECT_EQ(6, exploded.hour);
309 EXPECT_EQ(22, exploded.minute);
310 EXPECT_EQ(20, exploded.second);
311 EXPECT_EQ(0, exploded.millisecond);
312
313 EXPECT_FALSE(current_entry_info->is_unsafe());
314 EXPECT_FALSE(current_entry_info->is_directory());
315 }
316
317 TEST_F(ZipReaderTest, current_entry_info_DotDotFile) {
318 ZipReader reader;
319 ASSERT_TRUE(reader.Open(evil_zip_file_));
320 base::FilePath target_path(FILE_PATH_LITERAL(
321 "../levilevilevilevilevilevilevilevilevilevilevilevil"));
322 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path));
323 ZipReader::EntryInfo* current_entry_info = reader.current_entry_info();
324 EXPECT_EQ(target_path, current_entry_info->file_path());
325
326 // This file is unsafe because of ".." in the file name.
327 EXPECT_TRUE(current_entry_info->is_unsafe());
328 EXPECT_FALSE(current_entry_info->is_directory());
329 }
330
331 TEST_F(ZipReaderTest, current_entry_info_InvalidUTF8File) {
332 ZipReader reader;
333 ASSERT_TRUE(reader.Open(evil_via_invalid_utf8_zip_file_));
334 // The evil file is the 2nd file in the zip file.
335 // We cannot locate by the file name ".\x80.\\evil.txt",
336 // as FilePath may internally convert the string.
337 ASSERT_TRUE(reader.AdvanceToNextEntry());
338 ASSERT_TRUE(reader.OpenCurrentEntryInZip());
339 ZipReader::EntryInfo* current_entry_info = reader.current_entry_info();
340
341 // This file is unsafe because of invalid UTF-8 in the file name.
342 EXPECT_TRUE(current_entry_info->is_unsafe());
343 EXPECT_FALSE(current_entry_info->is_directory());
344 }
345
346 TEST_F(ZipReaderTest, current_entry_info_AbsoluteFile) {
347 ZipReader reader;
348 ASSERT_TRUE(reader.Open(evil_via_absolute_file_name_zip_file_));
349 base::FilePath target_path(FILE_PATH_LITERAL("/evil.txt"));
350 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path));
351 ZipReader::EntryInfo* current_entry_info = reader.current_entry_info();
352 EXPECT_EQ(target_path, current_entry_info->file_path());
353
354 // This file is unsafe because of the absolute file name.
355 EXPECT_TRUE(current_entry_info->is_unsafe());
356 EXPECT_FALSE(current_entry_info->is_directory());
357 }
358
359 TEST_F(ZipReaderTest, current_entry_info_Directory) {
360 ZipReader reader;
361 ASSERT_TRUE(reader.Open(test_zip_file_));
362 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/"));
363 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path));
364 ZipReader::EntryInfo* current_entry_info = reader.current_entry_info();
365
366 EXPECT_EQ(base::FilePath(FILE_PATH_LITERAL("foo/bar/")),
367 current_entry_info->file_path());
368 // The directory size should be zero.
369 EXPECT_EQ(0, current_entry_info->original_size());
370
371 // The expected time stamp: 2009-05-31 15:49:52
372 base::Time::Exploded exploded = {}; // Zero-clear.
373 current_entry_info->last_modified().LocalExplode(&exploded);
374 EXPECT_EQ(2009, exploded.year);
375 EXPECT_EQ(5, exploded.month);
376 EXPECT_EQ(31, exploded.day_of_month);
377 EXPECT_EQ(15, exploded.hour);
378 EXPECT_EQ(49, exploded.minute);
379 EXPECT_EQ(52, exploded.second);
380 EXPECT_EQ(0, exploded.millisecond);
381
382 EXPECT_FALSE(current_entry_info->is_unsafe());
383 EXPECT_TRUE(current_entry_info->is_directory());
384 }
385
386 // Verifies that the ZipReader class can extract a file from a zip archive
387 // stored in memory. This test opens a zip archive in a std::string object,
388 // extracts its content, and verifies the content is the same as the expected
389 // text.
390 TEST_F(ZipReaderTest, OpenFromString) {
391 // A zip archive consisting of one file "test.txt", which is a 16-byte text
392 // file that contains "This is a test.\n".
393 const char kTestData[] =
394 "\x50\x4b\x03\x04\x0a\x00\x00\x00\x00\x00\xa4\x66\x24\x41\x13\xe8"
395 "\xcb\x27\x10\x00\x00\x00\x10\x00\x00\x00\x08\x00\x1c\x00\x74\x65"
396 "\x73\x74\x2e\x74\x78\x74\x55\x54\x09\x00\x03\x34\x89\x45\x50\x34"
397 "\x89\x45\x50\x75\x78\x0b\x00\x01\x04\x8e\xf0\x00\x00\x04\x88\x13"
398 "\x00\x00\x54\x68\x69\x73\x20\x69\x73\x20\x61\x20\x74\x65\x73\x74"
399 "\x2e\x0a\x50\x4b\x01\x02\x1e\x03\x0a\x00\x00\x00\x00\x00\xa4\x66"
400 "\x24\x41\x13\xe8\xcb\x27\x10\x00\x00\x00\x10\x00\x00\x00\x08\x00"
401 "\x18\x00\x00\x00\x00\x00\x01\x00\x00\x00\xa4\x81\x00\x00\x00\x00"
402 "\x74\x65\x73\x74\x2e\x74\x78\x74\x55\x54\x05\x00\x03\x34\x89\x45"
403 "\x50\x75\x78\x0b\x00\x01\x04\x8e\xf0\x00\x00\x04\x88\x13\x00\x00"
404 "\x50\x4b\x05\x06\x00\x00\x00\x00\x01\x00\x01\x00\x4e\x00\x00\x00"
405 "\x52\x00\x00\x00\x00\x00";
406 std::string data(kTestData, arraysize(kTestData));
407 ZipReader reader;
408 ASSERT_TRUE(reader.OpenFromString(data));
409 base::FilePath target_path(FILE_PATH_LITERAL("test.txt"));
410 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path));
411 ASSERT_TRUE(reader.ExtractCurrentEntryToFilePath(
412 test_dir_.AppendASCII("test.txt")));
413
414 std::string actual;
415 ASSERT_TRUE(file_util::ReadFileToString(
416 test_dir_.AppendASCII("test.txt"), &actual));
417 EXPECT_EQ(std::string("This is a test.\n"), actual);
418 }
419
420 } // namespace zip
OLDNEW
« no previous file with comments | « chrome/common/zip_reader.cc ('k') | chrome/common/zip_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698