| OLD | NEW |
| 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 Loading... |
| 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 the accuracy that the zip file |
| 133 // supports. |
| 134 base::Time::Exploded now_parts; |
| 135 base::Time::Now().LocalExplode(&now_parts); |
| 136 now_parts.second = now_parts.second & ~1; |
| 137 now_parts.millisecond = 0; |
| 138 base::Time now_time = base::Time::FromLocalExploded(now_parts); |
| 139 |
| 140 EXPECT_EQ(1, file_util::WriteFile(src_file, "1", 1)); |
| 141 EXPECT_TRUE(base::TouchFile(src_file, base::Time::Now(), test_mtime)); |
| 142 |
| 143 EXPECT_TRUE(zip::Zip(src_dir, zip_file, true)); |
| 144 ASSERT_TRUE(zip::Unzip(zip_file, out_dir)); |
| 145 |
| 146 base::PlatformFileInfo file_info; |
| 147 EXPECT_TRUE(base::GetFileInfo(out_file, &file_info)); |
| 148 EXPECT_EQ(file_info.size, 1); |
| 149 |
| 150 if (valid_year == VALID_YEAR) { |
| 151 EXPECT_EQ(file_info.last_modified, test_mtime); |
| 152 } else { |
| 153 // Invalid date means the modification time will default to 'now'. |
| 154 EXPECT_GE(file_info.last_modified, now_time); |
| 155 } |
| 156 } |
| 157 |
| 101 // The path to temporary directory used to contain the test operations. | 158 // The path to temporary directory used to contain the test operations. |
| 102 base::FilePath test_dir_; | 159 base::FilePath test_dir_; |
| 103 | 160 |
| 104 base::ScopedTempDir temp_dir_; | 161 base::ScopedTempDir temp_dir_; |
| 105 | 162 |
| 106 // Hard-coded contents of a known zip file. | 163 // Hard-coded contents of a known zip file. |
| 107 std::set<base::FilePath> zip_contents_; | 164 std::set<base::FilePath> zip_contents_; |
| 108 | 165 |
| 109 // Hard-coded list of relative paths for a zip file created with ZipFiles. | 166 // Hard-coded list of relative paths for a zip file created with ZipFiles. |
| 110 std::vector<base::FilePath> zip_file_list_; | 167 std::vector<base::FilePath> zip_file_list_; |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 base::FilePath src_dir_russian = | 242 base::FilePath src_dir_russian = |
| 186 temp_dir.path().Append(base::FilePath::FromUTF8Unsafe( | 243 temp_dir.path().Append(base::FilePath::FromUTF8Unsafe( |
| 187 "\xD0\xA2\xD0\xB5\xD1\x81\xD1\x82")); | 244 "\xD0\xA2\xD0\xB5\xD1\x81\xD1\x82")); |
| 188 base::CopyDirectory(src_dir, src_dir_russian, true); | 245 base::CopyDirectory(src_dir, src_dir_russian, true); |
| 189 base::FilePath zip_file = temp_dir.path().AppendASCII("out_russian.zip"); | 246 base::FilePath zip_file = temp_dir.path().AppendASCII("out_russian.zip"); |
| 190 | 247 |
| 191 EXPECT_TRUE(zip::Zip(src_dir_russian, zip_file, true)); | 248 EXPECT_TRUE(zip::Zip(src_dir_russian, zip_file, true)); |
| 192 TestUnzipFile(zip_file, true); | 249 TestUnzipFile(zip_file, true); |
| 193 } | 250 } |
| 194 | 251 |
| 252 TEST_F(ZipTest, ZipTimeStamp) { |
| 253 // The dates tested are arbitrary, with some constraints. The zip format can |
| 254 // only store years from 1980 to 2107 and an even number of seconds, due to it |
| 255 // using the ms dos date format. |
| 256 |
| 257 // Valid arbitrary date. |
| 258 TestTimeStamp("23 Oct 1997 23:22:20", VALID_YEAR); |
| 259 |
| 260 // Date before 1980, zip format limitation, must default to unix epoch. |
| 261 TestTimeStamp("29 Dec 1979 21:00:10", INVALID_YEAR); |
| 262 |
| 263 // Despite the minizip headers telling the maximum year should be 2044, it |
| 264 // can actually go up to 2107. Beyond that, the dos date format cannot store |
| 265 // the year (2107-1980=127). To test that limit, the input file needs to be |
| 266 // touched, but the code that modifies the file access and modification times |
| 267 // relies on time_t which is defined as long, therefore being in many |
| 268 // platforms just a 4-byte integer, like 32-bit Mac OSX or linux. As such, it |
| 269 // suffers from the year-2038 bug. Therefore 2038 is the highest we can test |
| 270 // in all platforms reliably. |
| 271 TestTimeStamp("02 Jan 2038 23:59:58", VALID_YEAR); |
| 272 } |
| 273 |
| 195 #if defined(OS_POSIX) | 274 #if defined(OS_POSIX) |
| 196 TEST_F(ZipTest, ZipFiles) { | 275 TEST_F(ZipTest, ZipFiles) { |
| 197 base::FilePath src_dir; | 276 base::FilePath src_dir; |
| 198 ASSERT_TRUE(GetTestDataDirectory(&src_dir)); | 277 ASSERT_TRUE(GetTestDataDirectory(&src_dir)); |
| 199 src_dir = src_dir.AppendASCII("test"); | 278 src_dir = src_dir.AppendASCII("test"); |
| 200 | 279 |
| 201 base::ScopedTempDir temp_dir; | 280 base::ScopedTempDir temp_dir; |
| 202 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 281 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 203 base::FilePath zip_file = temp_dir.path().AppendASCII("out.zip"); | 282 base::FilePath zip_file = temp_dir.path().AppendASCII("out.zip"); |
| 204 | 283 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 216 EXPECT_TRUE(reader.LocateAndOpenEntry(zip_file_list_[i])); | 295 EXPECT_TRUE(reader.LocateAndOpenEntry(zip_file_list_[i])); |
| 217 // Check the path in the entry just in case. | 296 // Check the path in the entry just in case. |
| 218 const zip::ZipReader::EntryInfo* entry_info = reader.current_entry_info(); | 297 const zip::ZipReader::EntryInfo* entry_info = reader.current_entry_info(); |
| 219 EXPECT_EQ(entry_info->file_path(), zip_file_list_[i]); | 298 EXPECT_EQ(entry_info->file_path(), zip_file_list_[i]); |
| 220 } | 299 } |
| 221 } | 300 } |
| 222 #endif // defined(OS_POSIX) | 301 #endif // defined(OS_POSIX) |
| 223 | 302 |
| 224 } // namespace | 303 } // namespace |
| 225 | 304 |
| OLD | NEW |