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