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

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

Issue 2023703002: Beginning work on GN build (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Really add //build. Add dart_bootstrap rule. Created 4 years, 6 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
« no previous file with comments | « third_party/zlib/google/zip_reader_unittest.cc ('k') | third_party/zlib/gzclose.c » ('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 <stddef.h>
6 #include <stdint.h>
7
8 #include <set>
9 #include <string>
10 #include <vector>
11
12 #include "base/files/file.h"
13 #include "base/files/file_enumerator.h"
14 #include "base/files/file_path.h"
15 #include "base/files/file_util.h"
16 #include "base/files/scoped_temp_dir.h"
17 #include "base/path_service.h"
18 #include "base/strings/string_util.h"
19 #include "base/strings/stringprintf.h"
20 #include "build/build_config.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "testing/platform_test.h"
23 #include "third_party/zlib/google/zip.h"
24 #include "third_party/zlib/google/zip_reader.h"
25
26 namespace {
27
28 // Make the test a PlatformTest to setup autorelease pools properly on Mac.
29 class ZipTest : public PlatformTest {
30 protected:
31 enum ValidYearType {
32 VALID_YEAR,
33 INVALID_YEAR
34 };
35
36 virtual void SetUp() {
37 PlatformTest::SetUp();
38
39 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
40 test_dir_ = temp_dir_.path();
41
42 base::FilePath zip_path(test_dir_);
43 zip_contents_.insert(zip_path.AppendASCII("foo.txt"));
44 zip_path = zip_path.AppendASCII("foo");
45 zip_contents_.insert(zip_path);
46 zip_contents_.insert(zip_path.AppendASCII("bar.txt"));
47 zip_path = zip_path.AppendASCII("bar");
48 zip_contents_.insert(zip_path);
49 zip_contents_.insert(zip_path.AppendASCII("baz.txt"));
50 zip_contents_.insert(zip_path.AppendASCII("quux.txt"));
51 zip_contents_.insert(zip_path.AppendASCII(".hidden"));
52
53 // Include a subset of files in |zip_file_list_| to test ZipFiles().
54 zip_file_list_.push_back(base::FilePath(FILE_PATH_LITERAL("foo.txt")));
55 zip_file_list_.push_back(
56 base::FilePath(FILE_PATH_LITERAL("foo/bar/quux.txt")));
57 zip_file_list_.push_back(
58 base::FilePath(FILE_PATH_LITERAL("foo/bar/.hidden")));
59 }
60
61 virtual void TearDown() {
62 PlatformTest::TearDown();
63 }
64
65 bool GetTestDataDirectory(base::FilePath* path) {
66 bool success = PathService::Get(base::DIR_SOURCE_ROOT, path);
67 EXPECT_TRUE(success);
68 if (!success)
69 return false;
70 *path = path->AppendASCII("third_party");
71 *path = path->AppendASCII("zlib");
72 *path = path->AppendASCII("google");
73 *path = path->AppendASCII("test");
74 *path = path->AppendASCII("data");
75 return true;
76 }
77
78 void TestUnzipFile(const base::FilePath::StringType& filename,
79 bool expect_hidden_files) {
80 base::FilePath test_dir;
81 ASSERT_TRUE(GetTestDataDirectory(&test_dir));
82 TestUnzipFile(test_dir.Append(filename), expect_hidden_files);
83 }
84
85 void TestUnzipFile(const base::FilePath& path, bool expect_hidden_files) {
86 ASSERT_TRUE(base::PathExists(path)) << "no file " << path.value();
87 ASSERT_TRUE(zip::Unzip(path, test_dir_));
88
89 base::FileEnumerator files(test_dir_, true,
90 base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES);
91 base::FilePath next_path = files.Next();
92 size_t count = 0;
93 while (!next_path.value().empty()) {
94 if (next_path.value().find(FILE_PATH_LITERAL(".svn")) ==
95 base::FilePath::StringType::npos) {
96 EXPECT_EQ(zip_contents_.count(next_path), 1U) <<
97 "Couldn't find " << next_path.value();
98 count++;
99 }
100 next_path = files.Next();
101 }
102
103 size_t expected_count = 0;
104 for (std::set<base::FilePath>::iterator iter = zip_contents_.begin();
105 iter != zip_contents_.end(); ++iter) {
106 if (expect_hidden_files || iter->BaseName().value()[0] != '.')
107 ++expected_count;
108 }
109
110 EXPECT_EQ(expected_count, count);
111 }
112
113 // This function does the following:
114 // 1) Creates a test.txt file with the given last modification timestamp
115 // 2) Zips test.txt and extracts it back into a different location.
116 // 3) Confirms that test.txt in the output directory has the specified
117 // last modification timestamp if it is valid (|valid_year| is true).
118 // If the timestamp is not supported by the zip format, the last
119 // modification defaults to the current time.
120 void TestTimeStamp(const char* date_time, ValidYearType valid_year) {
121 SCOPED_TRACE(std::string("TestTimeStamp(") + date_time + ")");
122 base::ScopedTempDir temp_dir;
123 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
124
125 base::FilePath zip_file = temp_dir.path().AppendASCII("out.zip");
126 base::FilePath src_dir = temp_dir.path().AppendASCII("input");
127 base::FilePath out_dir = temp_dir.path().AppendASCII("output");
128
129 base::FilePath src_file = src_dir.AppendASCII("test.txt");
130 base::FilePath out_file = out_dir.AppendASCII("test.txt");
131
132 EXPECT_TRUE(base::CreateDirectory(src_dir));
133 EXPECT_TRUE(base::CreateDirectory(out_dir));
134
135 base::Time test_mtime;
136 ASSERT_TRUE(base::Time::FromString(date_time, &test_mtime));
137
138 // Adjusting the current timestamp to the resolution that the zip file
139 // supports, which is 2 seconds. Note that between this call to Time::Now()
140 // and zip::Zip() the clock can advance a bit, hence the use of EXPECT_GE.
141 base::Time::Exploded now_parts;
142 base::Time::Now().LocalExplode(&now_parts);
143 now_parts.second = now_parts.second & ~1;
144 now_parts.millisecond = 0;
145 base::Time now_time = base::Time::FromLocalExploded(now_parts);
146
147 EXPECT_EQ(1, base::WriteFile(src_file, "1", 1));
148 EXPECT_TRUE(base::TouchFile(src_file, base::Time::Now(), test_mtime));
149
150 EXPECT_TRUE(zip::Zip(src_dir, zip_file, true));
151 ASSERT_TRUE(zip::Unzip(zip_file, out_dir));
152
153 base::File::Info file_info;
154 EXPECT_TRUE(base::GetFileInfo(out_file, &file_info));
155 EXPECT_EQ(file_info.size, 1);
156
157 if (valid_year == VALID_YEAR) {
158 EXPECT_EQ(file_info.last_modified, test_mtime);
159 } else {
160 // Invalid date means the modification time will default to 'now'.
161 EXPECT_GE(file_info.last_modified, now_time);
162 }
163 }
164
165 // The path to temporary directory used to contain the test operations.
166 base::FilePath test_dir_;
167
168 base::ScopedTempDir temp_dir_;
169
170 // Hard-coded contents of a known zip file.
171 std::set<base::FilePath> zip_contents_;
172
173 // Hard-coded list of relative paths for a zip file created with ZipFiles.
174 std::vector<base::FilePath> zip_file_list_;
175 };
176
177 TEST_F(ZipTest, Unzip) {
178 TestUnzipFile(FILE_PATH_LITERAL("test.zip"), true);
179 }
180
181 TEST_F(ZipTest, UnzipUncompressed) {
182 TestUnzipFile(FILE_PATH_LITERAL("test_nocompress.zip"), true);
183 }
184
185 TEST_F(ZipTest, UnzipEvil) {
186 base::FilePath path;
187 ASSERT_TRUE(GetTestDataDirectory(&path));
188 path = path.AppendASCII("evil.zip");
189 // Unzip the zip file into a sub directory of test_dir_ so evil.zip
190 // won't create a persistent file outside test_dir_ in case of a
191 // failure.
192 base::FilePath output_dir = test_dir_.AppendASCII("out");
193 ASSERT_FALSE(zip::Unzip(path, output_dir));
194 base::FilePath evil_file = output_dir;
195 evil_file = evil_file.AppendASCII(
196 "../levilevilevilevilevilevilevilevilevilevilevilevil");
197 ASSERT_FALSE(base::PathExists(evil_file));
198 }
199
200 TEST_F(ZipTest, UnzipEvil2) {
201 base::FilePath path;
202 ASSERT_TRUE(GetTestDataDirectory(&path));
203 // The zip file contains an evil file with invalid UTF-8 in its file
204 // name.
205 path = path.AppendASCII("evil_via_invalid_utf8.zip");
206 // See the comment at UnzipEvil() for why we do this.
207 base::FilePath output_dir = test_dir_.AppendASCII("out");
208 // This should fail as it contains an evil file.
209 ASSERT_FALSE(zip::Unzip(path, output_dir));
210 base::FilePath evil_file = output_dir;
211 evil_file = evil_file.AppendASCII("../evil.txt");
212 ASSERT_FALSE(base::PathExists(evil_file));
213 }
214
215 TEST_F(ZipTest, Zip) {
216 base::FilePath src_dir;
217 ASSERT_TRUE(GetTestDataDirectory(&src_dir));
218 src_dir = src_dir.AppendASCII("test");
219
220 base::ScopedTempDir temp_dir;
221 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
222 base::FilePath zip_file = temp_dir.path().AppendASCII("out.zip");
223
224 EXPECT_TRUE(zip::Zip(src_dir, zip_file, true));
225 TestUnzipFile(zip_file, true);
226 }
227
228 TEST_F(ZipTest, ZipIgnoreHidden) {
229 base::FilePath src_dir;
230 ASSERT_TRUE(GetTestDataDirectory(&src_dir));
231 src_dir = src_dir.AppendASCII("test");
232
233 base::ScopedTempDir temp_dir;
234 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
235 base::FilePath zip_file = temp_dir.path().AppendASCII("out.zip");
236
237 EXPECT_TRUE(zip::Zip(src_dir, zip_file, false));
238 TestUnzipFile(zip_file, false);
239 }
240
241 TEST_F(ZipTest, ZipNonASCIIDir) {
242 base::FilePath src_dir;
243 ASSERT_TRUE(GetTestDataDirectory(&src_dir));
244 src_dir = src_dir.AppendASCII("test");
245
246 base::ScopedTempDir temp_dir;
247 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
248 // Append 'Тест' (in cyrillic).
249 base::FilePath src_dir_russian =
250 temp_dir.path().Append(base::FilePath::FromUTF8Unsafe(
251 "\xD0\xA2\xD0\xB5\xD1\x81\xD1\x82"));
252 base::CopyDirectory(src_dir, src_dir_russian, true);
253 base::FilePath zip_file = temp_dir.path().AppendASCII("out_russian.zip");
254
255 EXPECT_TRUE(zip::Zip(src_dir_russian, zip_file, true));
256 TestUnzipFile(zip_file, true);
257 }
258
259 TEST_F(ZipTest, ZipTimeStamp) {
260 // The dates tested are arbitrary, with some constraints. The zip format can
261 // only store years from 1980 to 2107 and an even number of seconds, due to it
262 // using the ms dos date format.
263
264 // Valid arbitrary date.
265 TestTimeStamp("23 Oct 1997 23:22:20", VALID_YEAR);
266
267 // Date before 1980, zip format limitation, must default to unix epoch.
268 TestTimeStamp("29 Dec 1979 21:00:10", INVALID_YEAR);
269
270 // Despite the minizip headers telling the maximum year should be 2044, it
271 // can actually go up to 2107. Beyond that, the dos date format cannot store
272 // the year (2107-1980=127). To test that limit, the input file needs to be
273 // touched, but the code that modifies the file access and modification times
274 // relies on time_t which is defined as long, therefore being in many
275 // platforms just a 4-byte integer, like 32-bit Mac OSX or linux. As such, it
276 // suffers from the year-2038 bug. Therefore 2038 is the highest we can test
277 // in all platforms reliably.
278 TestTimeStamp("02 Jan 2038 23:59:58", VALID_YEAR);
279 }
280
281 #if defined(OS_POSIX)
282 TEST_F(ZipTest, ZipFiles) {
283 base::FilePath src_dir;
284 ASSERT_TRUE(GetTestDataDirectory(&src_dir));
285 src_dir = src_dir.AppendASCII("test");
286
287 base::ScopedTempDir temp_dir;
288 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
289 base::FilePath zip_name = temp_dir.path().AppendASCII("out.zip");
290
291 base::File zip_file(zip_name,
292 base::File::FLAG_CREATE | base::File::FLAG_WRITE);
293 ASSERT_TRUE(zip_file.IsValid());
294 EXPECT_TRUE(zip::ZipFiles(src_dir, zip_file_list_,
295 zip_file.GetPlatformFile()));
296 zip_file.Close();
297
298 zip::ZipReader reader;
299 EXPECT_TRUE(reader.Open(zip_name));
300 EXPECT_EQ(zip_file_list_.size(), static_cast<size_t>(reader.num_entries()));
301 for (size_t i = 0; i < zip_file_list_.size(); ++i) {
302 EXPECT_TRUE(reader.LocateAndOpenEntry(zip_file_list_[i]));
303 // Check the path in the entry just in case.
304 const zip::ZipReader::EntryInfo* entry_info = reader.current_entry_info();
305 EXPECT_EQ(entry_info->file_path(), zip_file_list_[i]);
306 }
307 }
308 #endif // defined(OS_POSIX)
309
310 TEST_F(ZipTest, UnzipFilesWithIncorrectSize) {
311 base::FilePath test_data_folder;
312 ASSERT_TRUE(GetTestDataDirectory(&test_data_folder));
313
314 // test_mismatch_size.zip contains files with names from 0.txt to 7.txt with
315 // sizes from 0 to 7 bytes respectively, but the metadata in the zip file says
316 // the uncompressed size is 3 bytes. The ZipReader and minizip code needs to
317 // be clever enough to get all the data out.
318 base::FilePath test_zip_file =
319 test_data_folder.AppendASCII("test_mismatch_size.zip");
320
321 base::ScopedTempDir scoped_temp_dir;
322 ASSERT_TRUE(scoped_temp_dir.CreateUniqueTempDir());
323 const base::FilePath& temp_dir = scoped_temp_dir.path();
324
325 ASSERT_TRUE(zip::Unzip(test_zip_file, temp_dir));
326 EXPECT_TRUE(base::DirectoryExists(temp_dir.AppendASCII("d")));
327
328 for (int i = 0; i < 8; i++) {
329 SCOPED_TRACE(base::StringPrintf("Processing %d.txt", i));
330 base::FilePath file_path = temp_dir.AppendASCII(
331 base::StringPrintf("%d.txt", i));
332 int64_t file_size = -1;
333 EXPECT_TRUE(base::GetFileSize(file_path, &file_size));
334 EXPECT_EQ(static_cast<int64_t>(i), file_size);
335 }
336 }
337
338 } // namespace
OLDNEW
« no previous file with comments | « third_party/zlib/google/zip_reader_unittest.cc ('k') | third_party/zlib/gzclose.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698