Revert 248827 "android: Migrate old content readback to use asyn..."
[chromium-blink-merge.git] / third_party / zlib / google / zip_unittest.cc
blobafa92f137c14283b1fc912a1cb7237a0eeb47fa1
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.
5 #include <set>
6 #include <string>
7 #include <vector>
9 #include "base/file_util.h"
10 #include "base/files/file_enumerator.h"
11 #include "base/files/file_path.h"
12 #include "base/files/scoped_temp_dir.h"
13 #include "base/path_service.h"
14 #include "base/strings/string_util.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "testing/platform_test.h"
17 #include "third_party/zlib/google/zip.h"
18 #include "third_party/zlib/google/zip_reader.h"
20 namespace {
22 // Make the test a PlatformTest to setup autorelease pools properly on Mac.
23 class ZipTest : public PlatformTest {
24 protected:
25 enum ValidYearType {
26 VALID_YEAR,
27 INVALID_YEAR
30 virtual void SetUp() {
31 PlatformTest::SetUp();
33 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
34 test_dir_ = temp_dir_.path();
36 base::FilePath zip_path(test_dir_);
37 zip_contents_.insert(zip_path.AppendASCII("foo.txt"));
38 zip_path = zip_path.AppendASCII("foo");
39 zip_contents_.insert(zip_path);
40 zip_contents_.insert(zip_path.AppendASCII("bar.txt"));
41 zip_path = zip_path.AppendASCII("bar");
42 zip_contents_.insert(zip_path);
43 zip_contents_.insert(zip_path.AppendASCII("baz.txt"));
44 zip_contents_.insert(zip_path.AppendASCII("quux.txt"));
45 zip_contents_.insert(zip_path.AppendASCII(".hidden"));
47 // Include a subset of files in |zip_file_list_| to test ZipFiles().
48 zip_file_list_.push_back(base::FilePath(FILE_PATH_LITERAL("foo.txt")));
49 zip_file_list_.push_back(
50 base::FilePath(FILE_PATH_LITERAL("foo/bar/quux.txt")));
51 zip_file_list_.push_back(
52 base::FilePath(FILE_PATH_LITERAL("foo/bar/.hidden")));
55 virtual void TearDown() {
56 PlatformTest::TearDown();
59 bool GetTestDataDirectory(base::FilePath* path) {
60 bool success = PathService::Get(base::DIR_SOURCE_ROOT, path);
61 EXPECT_TRUE(success);
62 if (!success)
63 return false;
64 *path = path->AppendASCII("third_party");
65 *path = path->AppendASCII("zlib");
66 *path = path->AppendASCII("google");
67 *path = path->AppendASCII("test");
68 *path = path->AppendASCII("data");
69 return true;
72 void TestUnzipFile(const base::FilePath::StringType& filename,
73 bool expect_hidden_files) {
74 base::FilePath test_dir;
75 ASSERT_TRUE(GetTestDataDirectory(&test_dir));
76 TestUnzipFile(test_dir.Append(filename), expect_hidden_files);
79 void TestUnzipFile(const base::FilePath& path, bool expect_hidden_files) {
80 ASSERT_TRUE(base::PathExists(path)) << "no file " << path.value();
81 ASSERT_TRUE(zip::Unzip(path, test_dir_));
83 base::FileEnumerator files(test_dir_, true,
84 base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES);
85 base::FilePath next_path = files.Next();
86 size_t count = 0;
87 while (!next_path.value().empty()) {
88 if (next_path.value().find(FILE_PATH_LITERAL(".svn")) ==
89 base::FilePath::StringType::npos) {
90 EXPECT_EQ(zip_contents_.count(next_path), 1U) <<
91 "Couldn't find " << next_path.value();
92 count++;
94 next_path = files.Next();
97 size_t expected_count = 0;
98 for (std::set<base::FilePath>::iterator iter = zip_contents_.begin();
99 iter != zip_contents_.end(); ++iter) {
100 if (expect_hidden_files || iter->BaseName().value()[0] != '.')
101 ++expected_count;
104 EXPECT_EQ(expected_count, count);
107 // This function does the following:
108 // 1) Creates a test.txt file with the given last modification timestamp
109 // 2) Zips test.txt and extracts it back into a different location.
110 // 3) Confirms that test.txt in the output directory has the specified
111 // last modification timestamp if it is valid (|valid_year| is true).
112 // If the timestamp is not supported by the zip format, the last
113 // modification defaults to the current time.
114 void TestTimeStamp(const char* date_time, ValidYearType valid_year) {
115 SCOPED_TRACE(std::string("TestTimeStamp(") + date_time + ")");
116 base::ScopedTempDir temp_dir;
117 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
119 base::FilePath zip_file = temp_dir.path().AppendASCII("out.zip");
120 base::FilePath src_dir = temp_dir.path().AppendASCII("input");
121 base::FilePath out_dir = temp_dir.path().AppendASCII("output");
123 base::FilePath src_file = src_dir.AppendASCII("test.txt");
124 base::FilePath out_file = out_dir.AppendASCII("test.txt");
126 EXPECT_TRUE(base::CreateDirectory(src_dir));
127 EXPECT_TRUE(base::CreateDirectory(out_dir));
129 base::Time test_mtime;
130 ASSERT_TRUE(base::Time::FromString(date_time, &test_mtime));
132 // Adjusting the current timestamp to the resolution that the zip file
133 // supports, which is 2 seconds. Note that between this call to Time::Now()
134 // and zip::Zip() the clock can advance a bit, hence the use of EXPECT_GE.
135 base::Time::Exploded now_parts;
136 base::Time::Now().LocalExplode(&now_parts);
137 now_parts.second = now_parts.second & ~1;
138 now_parts.millisecond = 0;
139 base::Time now_time = base::Time::FromLocalExploded(now_parts);
141 EXPECT_EQ(1, file_util::WriteFile(src_file, "1", 1));
142 EXPECT_TRUE(base::TouchFile(src_file, base::Time::Now(), test_mtime));
144 EXPECT_TRUE(zip::Zip(src_dir, zip_file, true));
145 ASSERT_TRUE(zip::Unzip(zip_file, out_dir));
147 base::File::Info file_info;
148 EXPECT_TRUE(base::GetFileInfo(out_file, &file_info));
149 EXPECT_EQ(file_info.size, 1);
151 if (valid_year == VALID_YEAR) {
152 EXPECT_EQ(file_info.last_modified, test_mtime);
153 } else {
154 // Invalid date means the modification time will default to 'now'.
155 EXPECT_GE(file_info.last_modified, now_time);
159 // The path to temporary directory used to contain the test operations.
160 base::FilePath test_dir_;
162 base::ScopedTempDir temp_dir_;
164 // Hard-coded contents of a known zip file.
165 std::set<base::FilePath> zip_contents_;
167 // Hard-coded list of relative paths for a zip file created with ZipFiles.
168 std::vector<base::FilePath> zip_file_list_;
171 TEST_F(ZipTest, Unzip) {
172 TestUnzipFile(FILE_PATH_LITERAL("test.zip"), true);
175 TEST_F(ZipTest, UnzipUncompressed) {
176 TestUnzipFile(FILE_PATH_LITERAL("test_nocompress.zip"), true);
179 TEST_F(ZipTest, UnzipEvil) {
180 base::FilePath path;
181 ASSERT_TRUE(GetTestDataDirectory(&path));
182 path = path.AppendASCII("evil.zip");
183 // Unzip the zip file into a sub directory of test_dir_ so evil.zip
184 // won't create a persistent file outside test_dir_ in case of a
185 // failure.
186 base::FilePath output_dir = test_dir_.AppendASCII("out");
187 ASSERT_FALSE(zip::Unzip(path, output_dir));
188 base::FilePath evil_file = output_dir;
189 evil_file = evil_file.AppendASCII(
190 "../levilevilevilevilevilevilevilevilevilevilevilevil");
191 ASSERT_FALSE(base::PathExists(evil_file));
194 TEST_F(ZipTest, UnzipEvil2) {
195 base::FilePath path;
196 ASSERT_TRUE(GetTestDataDirectory(&path));
197 // The zip file contains an evil file with invalid UTF-8 in its file
198 // name.
199 path = path.AppendASCII("evil_via_invalid_utf8.zip");
200 // See the comment at UnzipEvil() for why we do this.
201 base::FilePath output_dir = test_dir_.AppendASCII("out");
202 // This should fail as it contains an evil file.
203 ASSERT_FALSE(zip::Unzip(path, output_dir));
204 base::FilePath evil_file = output_dir;
205 evil_file = evil_file.AppendASCII("../evil.txt");
206 ASSERT_FALSE(base::PathExists(evil_file));
209 TEST_F(ZipTest, Zip) {
210 base::FilePath src_dir;
211 ASSERT_TRUE(GetTestDataDirectory(&src_dir));
212 src_dir = src_dir.AppendASCII("test");
214 base::ScopedTempDir temp_dir;
215 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
216 base::FilePath zip_file = temp_dir.path().AppendASCII("out.zip");
218 EXPECT_TRUE(zip::Zip(src_dir, zip_file, true));
219 TestUnzipFile(zip_file, true);
222 TEST_F(ZipTest, ZipIgnoreHidden) {
223 base::FilePath src_dir;
224 ASSERT_TRUE(GetTestDataDirectory(&src_dir));
225 src_dir = src_dir.AppendASCII("test");
227 base::ScopedTempDir temp_dir;
228 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
229 base::FilePath zip_file = temp_dir.path().AppendASCII("out.zip");
231 EXPECT_TRUE(zip::Zip(src_dir, zip_file, false));
232 TestUnzipFile(zip_file, false);
235 TEST_F(ZipTest, ZipNonASCIIDir) {
236 base::FilePath src_dir;
237 ASSERT_TRUE(GetTestDataDirectory(&src_dir));
238 src_dir = src_dir.AppendASCII("test");
240 base::ScopedTempDir temp_dir;
241 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
242 // Append 'Тест' (in cyrillic).
243 base::FilePath src_dir_russian =
244 temp_dir.path().Append(base::FilePath::FromUTF8Unsafe(
245 "\xD0\xA2\xD0\xB5\xD1\x81\xD1\x82"));
246 base::CopyDirectory(src_dir, src_dir_russian, true);
247 base::FilePath zip_file = temp_dir.path().AppendASCII("out_russian.zip");
249 EXPECT_TRUE(zip::Zip(src_dir_russian, zip_file, true));
250 TestUnzipFile(zip_file, true);
253 TEST_F(ZipTest, ZipTimeStamp) {
254 // The dates tested are arbitrary, with some constraints. The zip format can
255 // only store years from 1980 to 2107 and an even number of seconds, due to it
256 // using the ms dos date format.
258 // Valid arbitrary date.
259 TestTimeStamp("23 Oct 1997 23:22:20", VALID_YEAR);
261 // Date before 1980, zip format limitation, must default to unix epoch.
262 TestTimeStamp("29 Dec 1979 21:00:10", INVALID_YEAR);
264 // Despite the minizip headers telling the maximum year should be 2044, it
265 // can actually go up to 2107. Beyond that, the dos date format cannot store
266 // the year (2107-1980=127). To test that limit, the input file needs to be
267 // touched, but the code that modifies the file access and modification times
268 // relies on time_t which is defined as long, therefore being in many
269 // platforms just a 4-byte integer, like 32-bit Mac OSX or linux. As such, it
270 // suffers from the year-2038 bug. Therefore 2038 is the highest we can test
271 // in all platforms reliably.
272 TestTimeStamp("02 Jan 2038 23:59:58", VALID_YEAR);
275 #if defined(OS_POSIX)
276 TEST_F(ZipTest, ZipFiles) {
277 base::FilePath src_dir;
278 ASSERT_TRUE(GetTestDataDirectory(&src_dir));
279 src_dir = src_dir.AppendASCII("test");
281 base::ScopedTempDir temp_dir;
282 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
283 base::FilePath zip_file = temp_dir.path().AppendASCII("out.zip");
285 const int flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
286 const base::PlatformFile zip_fd =
287 base::CreatePlatformFile(zip_file, flags, NULL, NULL);
288 ASSERT_LE(0, zip_fd);
289 EXPECT_TRUE(zip::ZipFiles(src_dir, zip_file_list_, zip_fd));
290 base::ClosePlatformFile(zip_fd);
292 zip::ZipReader reader;
293 EXPECT_TRUE(reader.Open(zip_file));
294 EXPECT_EQ(zip_file_list_.size(), static_cast<size_t>(reader.num_entries()));
295 for (size_t i = 0; i < zip_file_list_.size(); ++i) {
296 EXPECT_TRUE(reader.LocateAndOpenEntry(zip_file_list_[i]));
297 // Check the path in the entry just in case.
298 const zip::ZipReader::EntryInfo* entry_info = reader.current_entry_info();
299 EXPECT_EQ(entry_info->file_path(), zip_file_list_[i]);
302 #endif // defined(OS_POSIX)
304 } // namespace