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

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

Issue 8508003: zip: Add ZipReader and rework Unzip() using the new class. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 1 month 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
« chrome/common/zip_reader.cc ('K') | « chrome/common/zip_reader.cc ('k') | no next file » | 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/md5.h"
12 #include "base/path_service.h"
13 #include "base/scoped_temp_dir.h"
14 #include "base/time.h"
15 #include "base/utf_string_conversions.h"
16 #include "chrome/common/chrome_paths.h"
17 #include "chrome/common/zip_internal.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "testing/platform_test.h"
20
21 namespace zip {
22
23 // Make the test a PlatformTest to setup autorelease pools properly on Mac.
24 class ZipReaderTest : public PlatformTest {
25 protected:
26 virtual void SetUp() {
27 PlatformTest::SetUp();
28
29 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
30 test_dir_ = temp_dir_.path();
31
32 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_));
33 test_data_dir_ = test_data_dir_.AppendASCII("zip");
34
35 test_zip_file_ = test_data_dir_.AppendASCII("test.zip");
36 evil_zip_file_ = test_data_dir_.AppendASCII("evil.zip");
37 evil_via_invalid_utf8_zip_file_ = test_data_dir_.AppendASCII(
38 "evil_via_invalid_utf8.zip");
39
40 test_zip_contents_.insert(FilePath(FILE_PATH_LITERAL("foo/")));
41 test_zip_contents_.insert(FilePath(FILE_PATH_LITERAL("foo/bar/")));
42 test_zip_contents_.insert(FilePath(FILE_PATH_LITERAL("foo/bar/baz.txt")));
43 test_zip_contents_.insert(FilePath(FILE_PATH_LITERAL("foo/bar/quux.txt")));
44 test_zip_contents_.insert(FilePath(FILE_PATH_LITERAL("foo/bar.txt")));
45 test_zip_contents_.insert(FilePath(FILE_PATH_LITERAL("foo.txt")));
46 test_zip_contents_.insert(FilePath(FILE_PATH_LITERAL("foo/bar/.hidden")));
47 }
48
49 virtual void TearDown() {
50 PlatformTest::TearDown();
51 }
52
53 // The path to temporary directory used to contain the test operations.
54 FilePath test_dir_;
55 // The path to the test data directory where test.zip etc. are located.
56 FilePath test_data_dir_;
57 // The path to test.zip in the test data directory.
58 FilePath test_zip_file_;
59 // The path to evil.zip in the test data directory.
60 FilePath evil_zip_file_;
61 // The path to evil_via_invalid_utf8.zip in the test data directory.
62 FilePath evil_via_invalid_utf8_zip_file_;
63 std::set<FilePath> test_zip_contents_;
64
65 ScopedTempDir temp_dir_;
66 };
67
68 TEST_F(ZipReaderTest, Open_ValidZipFile) {
69 ZipReader reader;
70 ASSERT_TRUE(reader.Open(test_zip_file_));
71 }
72
73 TEST_F(ZipReaderTest, Open_NonExistentFile) {
74 ZipReader reader;
75 ASSERT_FALSE(reader.Open(test_data_dir_.AppendASCII("nonexistent.zip")));
76 }
77
78 TEST_F(ZipReaderTest, Open_ExistentButNonZipFile) {
79 ZipReader reader;
80 ASSERT_FALSE(reader.Open(test_data_dir_.AppendASCII("create_test_zip.sh")));
81 }
82
83 // Iterate through the contents in the test zip file, and compare that the
84 // contents collected from the zip reader matches the expected contents.
85 TEST_F(ZipReaderTest, Iteration) {
86 std::set<FilePath> actual_contents;
87 ZipReader reader;
88 ASSERT_TRUE(reader.Open(test_zip_file_));
89 while (reader.HasMore()) {
90 ASSERT_TRUE(reader.OpenCurrentFileInZip());
91 actual_contents.insert(reader.current_file_info()->file_path());
92 ASSERT_TRUE(reader.AdvanceToNextFile());
93 }
94 EXPECT_FALSE(reader.AdvanceToNextFile()); // Shouldn't go further.
95 EXPECT_EQ(test_zip_contents_.size(),
96 static_cast<size_t>(reader.num_files()));
97 EXPECT_EQ(test_zip_contents_.size(), actual_contents.size());
98 EXPECT_EQ(test_zip_contents_, actual_contents);
99 }
100
101
102 TEST_F(ZipReaderTest, LocateAndOpenFile_ValidFile) {
103 std::set<FilePath> actual_contents;
104 ZipReader reader;
105 ASSERT_TRUE(reader.Open(test_zip_file_));
106 FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt"));
107 ASSERT_TRUE(reader.LocateAndOpenFile(target_path));
108 EXPECT_EQ(target_path, reader.current_file_info()->file_path());
109 }
110
111 TEST_F(ZipReaderTest, LocateAndOpenFile_NonExistentFile) {
112 std::set<FilePath> actual_contents;
113 ZipReader reader;
114 ASSERT_TRUE(reader.Open(test_zip_file_));
115 FilePath target_path(FILE_PATH_LITERAL("nonexistent.txt"));
116 ASSERT_FALSE(reader.LocateAndOpenFile(target_path));
117 EXPECT_EQ(NULL, reader.current_file_info());
118 }
119
120 TEST_F(ZipReaderTest, ExtractCurrentFileToFile_RegularFile) {
121 ZipReader reader;
122 ASSERT_TRUE(reader.Open(test_zip_file_));
123 FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt"));
124 ASSERT_TRUE(reader.LocateAndOpenFile(target_path));
125 ASSERT_TRUE(reader.ExtractCurrentFileToFile(
126 test_dir_.AppendASCII("quux.txt")));
127 // Read the output file ans compute the MD5.
128 std::string output;
129 ASSERT_TRUE(file_util::ReadFileToString(test_dir_.AppendASCII("quux.txt"),
130 &output));
131 const std::string md5 = base::MD5String(output);
132 const std::string kExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6";
133 EXPECT_EQ(kExpectedMD5, md5);
134 // quux.txt should be larger than kZipBufSize so that we can exercise
135 // the loop in ExtractCurrentFile().
136 EXPECT_LT(static_cast<size_t>(internal::kZipBufSize), output.size());
137 }
138
139 TEST_F(ZipReaderTest, ExtractCurrentFileToFile_Directory) {
140 ZipReader reader;
141 ASSERT_TRUE(reader.Open(test_zip_file_));
142 FilePath target_path(FILE_PATH_LITERAL("foo/"));
143 ASSERT_TRUE(reader.LocateAndOpenFile(target_path));
144 ASSERT_TRUE(reader.ExtractCurrentFileToFile(test_dir_.AppendASCII("foo")));
145 // The directory should be created.
146 ASSERT_TRUE(file_util::DirectoryExists(test_dir_.AppendASCII("foo")));
147 }
148
149 TEST_F(ZipReaderTest, ExtractCurrentFileToDirectory_RegularFile) {
150 ZipReader reader;
151 ASSERT_TRUE(reader.Open(test_zip_file_));
152 FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt"));
153 ASSERT_TRUE(reader.LocateAndOpenFile(target_path));
154 ASSERT_TRUE(reader.ExtractCurrentFileToDirectory(test_dir_));
155 // Sub directories should be created.
156 ASSERT_TRUE(file_util::DirectoryExists(test_dir_.AppendASCII("foo/bar")));
157 // And the file should be created.
158 std::string output;
159 ASSERT_TRUE(file_util::ReadFileToString(
160 test_dir_.AppendASCII("foo/bar/quux.txt"), &output));
161 const std::string md5 = base::MD5String(output);
162 const std::string kExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6";
163 EXPECT_EQ(kExpectedMD5, md5);
164 }
165
166 TEST_F(ZipReaderTest, current_file_info_RegularFile) {
167 ZipReader reader;
168 ASSERT_TRUE(reader.Open(test_zip_file_));
169 FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt"));
170 ASSERT_TRUE(reader.LocateAndOpenFile(target_path));
171 ZipReader::FileInfo* current_file_info = reader.current_file_info();
172
173 EXPECT_EQ(target_path, current_file_info->file_path());
174 EXPECT_EQ(13527, current_file_info->original_size());
175
176 // The expected time stamp: 2009-05-29 06:22:20
177 base::Time::Exploded exploded = {}; // Zero-clear.
178 current_file_info->last_modified().LocalExplode(&exploded);
179 EXPECT_EQ(2009, exploded.year);
180 EXPECT_EQ(5, exploded.month);
181 EXPECT_EQ(29, exploded.day_of_month);
182 EXPECT_EQ(6, exploded.hour);
183 EXPECT_EQ(22, exploded.minute);
184 EXPECT_EQ(20, exploded.second);
185 EXPECT_EQ(0, exploded.millisecond);
186
187 EXPECT_FALSE(current_file_info->is_unsafe());
188 EXPECT_FALSE(current_file_info->is_directory());
189 }
190
191 TEST_F(ZipReaderTest, current_file_info_DotDotFile) {
192 ZipReader reader;
193 ASSERT_TRUE(reader.Open(evil_zip_file_));
194 FilePath target_path(FILE_PATH_LITERAL(
195 "../levilevilevilevilevilevilevilevilevilevilevilevil"));
196 ASSERT_TRUE(reader.LocateAndOpenFile(target_path));
197 ZipReader::FileInfo* current_file_info = reader.current_file_info();
198 EXPECT_EQ(target_path, current_file_info->file_path());
199
200 // This file is unsafe because of ".." in the file name.
201 EXPECT_TRUE(current_file_info->is_unsafe());
202 EXPECT_FALSE(current_file_info->is_directory());
203 }
204
205 TEST_F(ZipReaderTest, current_file_info_InvalidUTF8File) {
206 ZipReader reader;
207 ASSERT_TRUE(reader.Open(evil_via_invalid_utf8_zip_file_));
208 FilePath target_path(FILE_PATH_LITERAL(".\x80.\\evil.txt"));
209 ASSERT_TRUE(reader.LocateAndOpenFile(target_path));
210 ZipReader::FileInfo* current_file_info = reader.current_file_info();
211 EXPECT_EQ(target_path, current_file_info->file_path());
212
213 // This file is unsafe because of invalid UTF-8 in the file name.
214 EXPECT_TRUE(current_file_info->is_unsafe());
215 EXPECT_FALSE(current_file_info->is_directory());
216 }
217
218 TEST_F(ZipReaderTest, current_file_info_Directory) {
219 ZipReader reader;
220 ASSERT_TRUE(reader.Open(test_zip_file_));
221 FilePath target_path(FILE_PATH_LITERAL("foo/bar/"));
222 ASSERT_TRUE(reader.LocateAndOpenFile(target_path));
223 ZipReader::FileInfo* current_file_info = reader.current_file_info();
224
225 EXPECT_EQ(FilePath(FILE_PATH_LITERAL("foo/bar/")),
226 current_file_info->file_path());
227 // The directory size should be zero.
228 EXPECT_EQ(0, current_file_info->original_size());
229
230 // The expected time stamp: 2009-05-31 15:49:52
231 base::Time::Exploded exploded = {}; // Zero-clear.
232 current_file_info->last_modified().LocalExplode(&exploded);
233 EXPECT_EQ(2009, exploded.year);
234 EXPECT_EQ(5, exploded.month);
235 EXPECT_EQ(31, exploded.day_of_month);
236 EXPECT_EQ(15, exploded.hour);
237 EXPECT_EQ(49, exploded.minute);
238 EXPECT_EQ(52, exploded.second);
239 EXPECT_EQ(0, exploded.millisecond);
240
241 EXPECT_FALSE(current_file_info->is_unsafe());
242 EXPECT_TRUE(current_file_info->is_directory());
243 }
244
245 } // namespace zip
OLDNEW
« chrome/common/zip_reader.cc ('K') | « chrome/common/zip_reader.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698