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

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: adressed review comments 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
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"
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 size_t expected_count = 0; 92 size_t expected_count = 0;
92 for (std::set<base::FilePath>::iterator iter = zip_contents_.begin(); 93 for (std::set<base::FilePath>::iterator iter = zip_contents_.begin();
93 iter != zip_contents_.end(); ++iter) { 94 iter != zip_contents_.end(); ++iter) {
94 if (expect_hidden_files || iter->BaseName().value()[0] != '.') 95 if (expect_hidden_files || iter->BaseName().value()[0] != '.')
95 ++expected_count; 96 ++expected_count;
96 } 97 }
97 98
98 EXPECT_EQ(expected_count, count); 99 EXPECT_EQ(expected_count, count);
99 } 100 }
100 101
102 // This function does the following:
103 // 1) Creates a test.txt file with the given last modification timestamp
104 // set to the valud or |date_time|.
satorux1 2013/12/13 04:52:50 valud? this line seems to be unnecessary.
satorux1 2013/12/17 02:32:25 Please mark comments you addressed with 'Done'. Th
João Eiras 2013/12/17 10:15:38 Done.
João Eiras 2013/12/17 10:15:38 Done.
105 // 2) Zips test.txt and extracts it back into a different location.
106 // 3) Confirms that test.txt in the output directory has the specified
107 // last modification timestamp if it is valid (|valid_year| is true).
108 // If the timestamp is not supported by the zip format, the last
109 // modification defaults to the current time.
satorux1 2013/12/13 04:52:50 I think we should have documentation like this in
João Eiras 2013/12/17 10:15:38 Done.
110 void TestTimeStamp(const char* date_time, bool valid_year) {
111 SCOPED_TRACE(std::string("TestTimeStamp(") + date_time + ")");
112 base::ScopedTempDir temp_dir;
113 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
114
115 base::FilePath zip_file = temp_dir.path().AppendASCII("out.zip");
116 base::FilePath src_dir = temp_dir.path().AppendASCII("input");
117 base::FilePath out_dir = temp_dir.path().AppendASCII("output");
118
119 base::FilePath src_file = src_dir.AppendASCII("test.txt");
120 base::FilePath out_file = out_dir.AppendASCII("test.txt");
121
122 EXPECT_TRUE(base::CreateDirectory(src_dir));
123 EXPECT_TRUE(base::CreateDirectory(out_dir));
124
125 base::Time test_mtime;
126 ASSERT_TRUE(base::Time::FromString(date_time, &test_mtime));
127 base::Time now_time = base::Time::Now() - base::TimeDelta::FromSeconds(1);
satorux1 2013/12/13 04:52:50 |now_time| actually points to a time in the past (
João Eiras 2013/12/13 17:01:57 The seconds in the file's timestamp are floored w
satorux1 2013/12/17 02:32:25 Didn't know that the timestamp resolution in zip f
João Eiras 2013/12/17 10:15:38 Done.
João Eiras 2013/12/17 10:15:38 Done.
João Eiras 2013/12/17 10:15:38 Done.
128
129 EXPECT_EQ(1, file_util::WriteFile(src_file, "1", 1));
130 EXPECT_TRUE(base::TouchFile(src_file, base::Time::Now(), test_mtime));
131
132 EXPECT_TRUE(zip::Zip(src_dir, zip_file, true));
133 ASSERT_TRUE(zip::Unzip(zip_file, out_dir));
134
135 base::PlatformFileInfo file_info;
136 EXPECT_TRUE(base::GetFileInfo(out_file, &file_info));
137 EXPECT_EQ(file_info.size, 1);
138
139 if (valid_year) {
140 EXPECT_EQ(file_info.last_modified, test_mtime);
141 } else {
142 // Invalid date means the modification time will default to 'now'.
143 EXPECT_GE(file_info.last_modified, now_time);
144 }
145 }
146
101 // The path to temporary directory used to contain the test operations. 147 // The path to temporary directory used to contain the test operations.
102 base::FilePath test_dir_; 148 base::FilePath test_dir_;
103 149
104 base::ScopedTempDir temp_dir_; 150 base::ScopedTempDir temp_dir_;
105 151
106 // Hard-coded contents of a known zip file. 152 // Hard-coded contents of a known zip file.
107 std::set<base::FilePath> zip_contents_; 153 std::set<base::FilePath> zip_contents_;
108 154
109 // Hard-coded list of relative paths for a zip file created with ZipFiles. 155 // Hard-coded list of relative paths for a zip file created with ZipFiles.
110 std::vector<base::FilePath> zip_file_list_; 156 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 = 231 base::FilePath src_dir_russian =
186 temp_dir.path().Append(base::FilePath::FromUTF8Unsafe( 232 temp_dir.path().Append(base::FilePath::FromUTF8Unsafe(
187 "\xD0\xA2\xD0\xB5\xD1\x81\xD1\x82")); 233 "\xD0\xA2\xD0\xB5\xD1\x81\xD1\x82"));
188 base::CopyDirectory(src_dir, src_dir_russian, true); 234 base::CopyDirectory(src_dir, src_dir_russian, true);
189 base::FilePath zip_file = temp_dir.path().AppendASCII("out_russian.zip"); 235 base::FilePath zip_file = temp_dir.path().AppendASCII("out_russian.zip");
190 236
191 EXPECT_TRUE(zip::Zip(src_dir_russian, zip_file, true)); 237 EXPECT_TRUE(zip::Zip(src_dir_russian, zip_file, true));
192 TestUnzipFile(zip_file, true); 238 TestUnzipFile(zip_file, true);
193 } 239 }
194 240
241 TEST_F(ZipTest, ZipTimeStamp) {
242 // The dates tested are arbitrary, with some constraints. The zip format can
243 // only store years from 1980 to 2107 and an even number of seconds, due to it
244 // using the ms dos date format.
245
246 // Valid arbitrary date.
247 TestTimeStamp("23 Oct 1997 23:22:20", true);
satorux1 2013/12/13 04:52:50 true/false looks cryptic. It'd be nice to define a
João Eiras 2013/12/17 10:15:38 Done.
248
249 // Date before 1980, zip format limitation, must default to unix epoch.
250 TestTimeStamp("29 Dec 1979 21:00:10", false);
251
252 // Despite the minizip headers telling the maximum year should be 2044, it
253 // can actually go up to 2107. Beyond that, the dos date format cannot store
254 // the year (2107-1980=127). To test that limit, the input file needs to be
255 // touched, but the code that modifies the file access and modification times
256 // relies on time_t which is defined as long, therefore being in many
257 // platforms just a 4-byte integer, like 32-bit Mac OSX or linux. As such, it
258 // suffers from the year-2038 bug. Therefore 2038 is the highest we can test
259 // in all platforms reliably.
260 TestTimeStamp("02 Jan 2038 23:59:58", true);
261 }
262
195 #if defined(OS_POSIX) 263 #if defined(OS_POSIX)
196 TEST_F(ZipTest, ZipFiles) { 264 TEST_F(ZipTest, ZipFiles) {
197 base::FilePath src_dir; 265 base::FilePath src_dir;
198 ASSERT_TRUE(GetTestDataDirectory(&src_dir)); 266 ASSERT_TRUE(GetTestDataDirectory(&src_dir));
199 src_dir = src_dir.AppendASCII("test"); 267 src_dir = src_dir.AppendASCII("test");
200 268
201 base::ScopedTempDir temp_dir; 269 base::ScopedTempDir temp_dir;
202 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 270 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
203 base::FilePath zip_file = temp_dir.path().AppendASCII("out.zip"); 271 base::FilePath zip_file = temp_dir.path().AppendASCII("out.zip");
204 272
(...skipping 11 matching lines...) Expand all
216 EXPECT_TRUE(reader.LocateAndOpenEntry(zip_file_list_[i])); 284 EXPECT_TRUE(reader.LocateAndOpenEntry(zip_file_list_[i]));
217 // Check the path in the entry just in case. 285 // Check the path in the entry just in case.
218 const zip::ZipReader::EntryInfo* entry_info = reader.current_entry_info(); 286 const zip::ZipReader::EntryInfo* entry_info = reader.current_entry_info();
219 EXPECT_EQ(entry_info->file_path(), zip_file_list_[i]); 287 EXPECT_EQ(entry_info->file_path(), zip_file_list_[i]);
220 } 288 }
221 } 289 }
222 #endif // defined(OS_POSIX) 290 #endif // defined(OS_POSIX)
223 291
224 } // namespace 292 } // namespace
225 293
OLDNEW
« third_party/zlib/google/zip.cc ('K') | « 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