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

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

Issue 99333019: Preserve modification timestamp when zipping files. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: better comments, cleanup Created 7 years 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.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
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <set> 5 #include <set>
6 #include <string>
6 #include <vector> 7 #include <vector>
7 8
8 #include "base/file_util.h" 9 #include "base/file_util.h"
9 #include "base/files/file_enumerator.h" 10 #include "base/files/file_enumerator.h"
10 #include "base/files/file_path.h" 11 #include "base/files/file_path.h"
11 #include "base/files/scoped_temp_dir.h" 12 #include "base/files/scoped_temp_dir.h"
12 #include "base/path_service.h" 13 #include "base/path_service.h"
13 #include "base/strings/string_util.h" 14 #include "base/strings/string_util.h"
14 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
15 #include "testing/platform_test.h" 16 #include "testing/platform_test.h"
16 #include "third_party/zlib/google/zip.h" 17 #include "third_party/zlib/google/zip.h"
17 #include "third_party/zlib/google/zip_reader.h" 18 #include "third_party/zlib/google/zip_reader.h"
18 19
19 namespace { 20 namespace {
20 21
21 // Make the test a PlatformTest to setup autorelease pools properly on Mac. 22 // Make the test a PlatformTest to setup autorelease pools properly on Mac.
22 class ZipTest : public PlatformTest { 23 class ZipTest : public PlatformTest {
23 protected: 24 protected:
25 enum ValidYearType {
26 VALID_YEAR,
27 INVALID_YEAR
28 };
29
24 virtual void SetUp() { 30 virtual void SetUp() {
25 PlatformTest::SetUp(); 31 PlatformTest::SetUp();
26 32
27 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 33 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
28 test_dir_ = temp_dir_.path(); 34 test_dir_ = temp_dir_.path();
29 35
30 base::FilePath zip_path(test_dir_); 36 base::FilePath zip_path(test_dir_);
31 zip_contents_.insert(zip_path.AppendASCII("foo.txt")); 37 zip_contents_.insert(zip_path.AppendASCII("foo.txt"));
32 zip_path = zip_path.AppendASCII("foo"); 38 zip_path = zip_path.AppendASCII("foo");
33 zip_contents_.insert(zip_path); 39 zip_contents_.insert(zip_path);
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 size_t expected_count = 0; 97 size_t expected_count = 0;
92 for (std::set<base::FilePath>::iterator iter = zip_contents_.begin(); 98 for (std::set<base::FilePath>::iterator iter = zip_contents_.begin();
93 iter != zip_contents_.end(); ++iter) { 99 iter != zip_contents_.end(); ++iter) {
94 if (expect_hidden_files || iter->BaseName().value()[0] != '.') 100 if (expect_hidden_files || iter->BaseName().value()[0] != '.')
95 ++expected_count; 101 ++expected_count;
96 } 102 }
97 103
98 EXPECT_EQ(expected_count, count); 104 EXPECT_EQ(expected_count, count);
99 } 105 }
100 106
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());
118
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");
122
123 base::FilePath src_file = src_dir.AppendASCII("test.txt");
124 base::FilePath out_file = out_dir.AppendASCII("test.txt");
125
126 EXPECT_TRUE(base::CreateDirectory(src_dir));
127 EXPECT_TRUE(base::CreateDirectory(out_dir));
128
129 base::Time test_mtime;
130 ASSERT_TRUE(base::Time::FromString(date_time, &test_mtime));
131
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);
140
141 EXPECT_EQ(1, file_util::WriteFile(src_file, "1", 1));
142 EXPECT_TRUE(base::TouchFile(src_file, base::Time::Now(), test_mtime));
143
144 EXPECT_TRUE(zip::Zip(src_dir, zip_file, true));
145 ASSERT_TRUE(zip::Unzip(zip_file, out_dir));
146
147 base::PlatformFileInfo file_info;
148 EXPECT_TRUE(base::GetFileInfo(out_file, &file_info));
149 EXPECT_EQ(file_info.size, 1);
150
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);
156 }
157 }
158
101 // The path to temporary directory used to contain the test operations. 159 // The path to temporary directory used to contain the test operations.
102 base::FilePath test_dir_; 160 base::FilePath test_dir_;
103 161
104 base::ScopedTempDir temp_dir_; 162 base::ScopedTempDir temp_dir_;
105 163
106 // Hard-coded contents of a known zip file. 164 // Hard-coded contents of a known zip file.
107 std::set<base::FilePath> zip_contents_; 165 std::set<base::FilePath> zip_contents_;
108 166
109 // Hard-coded list of relative paths for a zip file created with ZipFiles. 167 // Hard-coded list of relative paths for a zip file created with ZipFiles.
110 std::vector<base::FilePath> zip_file_list_; 168 std::vector<base::FilePath> zip_file_list_;
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 base::FilePath src_dir_russian = 243 base::FilePath src_dir_russian =
186 temp_dir.path().Append(base::FilePath::FromUTF8Unsafe( 244 temp_dir.path().Append(base::FilePath::FromUTF8Unsafe(
187 "\xD0\xA2\xD0\xB5\xD1\x81\xD1\x82")); 245 "\xD0\xA2\xD0\xB5\xD1\x81\xD1\x82"));
188 base::CopyDirectory(src_dir, src_dir_russian, true); 246 base::CopyDirectory(src_dir, src_dir_russian, true);
189 base::FilePath zip_file = temp_dir.path().AppendASCII("out_russian.zip"); 247 base::FilePath zip_file = temp_dir.path().AppendASCII("out_russian.zip");
190 248
191 EXPECT_TRUE(zip::Zip(src_dir_russian, zip_file, true)); 249 EXPECT_TRUE(zip::Zip(src_dir_russian, zip_file, true));
192 TestUnzipFile(zip_file, true); 250 TestUnzipFile(zip_file, true);
193 } 251 }
194 252
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.
257
258 // Valid arbitrary date.
259 TestTimeStamp("23 Oct 1997 23:22:20", VALID_YEAR);
260
261 // Date before 1980, zip format limitation, must default to unix epoch.
262 TestTimeStamp("29 Dec 1979 21:00:10", INVALID_YEAR);
263
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);
273 }
274
195 #if defined(OS_POSIX) 275 #if defined(OS_POSIX)
196 TEST_F(ZipTest, ZipFiles) { 276 TEST_F(ZipTest, ZipFiles) {
197 base::FilePath src_dir; 277 base::FilePath src_dir;
198 ASSERT_TRUE(GetTestDataDirectory(&src_dir)); 278 ASSERT_TRUE(GetTestDataDirectory(&src_dir));
199 src_dir = src_dir.AppendASCII("test"); 279 src_dir = src_dir.AppendASCII("test");
200 280
201 base::ScopedTempDir temp_dir; 281 base::ScopedTempDir temp_dir;
202 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 282 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
203 base::FilePath zip_file = temp_dir.path().AppendASCII("out.zip"); 283 base::FilePath zip_file = temp_dir.path().AppendASCII("out.zip");
204 284
(...skipping 11 matching lines...) Expand all
216 EXPECT_TRUE(reader.LocateAndOpenEntry(zip_file_list_[i])); 296 EXPECT_TRUE(reader.LocateAndOpenEntry(zip_file_list_[i]));
217 // Check the path in the entry just in case. 297 // Check the path in the entry just in case.
218 const zip::ZipReader::EntryInfo* entry_info = reader.current_entry_info(); 298 const zip::ZipReader::EntryInfo* entry_info = reader.current_entry_info();
219 EXPECT_EQ(entry_info->file_path(), zip_file_list_[i]); 299 EXPECT_EQ(entry_info->file_path(), zip_file_list_[i]);
220 } 300 }
221 } 301 }
222 #endif // defined(OS_POSIX) 302 #endif // defined(OS_POSIX)
223 303
224 } // namespace 304 } // namespace
225 305
OLDNEW
« no previous file with comments | « third_party/zlib/google/zip_reader.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698