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