| 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 <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdint.h> | 6 #include <stdint.h> |
| 7 | 7 |
| 8 #include <set> | 8 #include <set> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 protected: | 30 protected: |
| 31 enum ValidYearType { | 31 enum ValidYearType { |
| 32 VALID_YEAR, | 32 VALID_YEAR, |
| 33 INVALID_YEAR | 33 INVALID_YEAR |
| 34 }; | 34 }; |
| 35 | 35 |
| 36 virtual void SetUp() { | 36 virtual void SetUp() { |
| 37 PlatformTest::SetUp(); | 37 PlatformTest::SetUp(); |
| 38 | 38 |
| 39 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 39 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 40 test_dir_ = temp_dir_.path(); | 40 test_dir_ = temp_dir_.GetPath(); |
| 41 | 41 |
| 42 base::FilePath zip_path(test_dir_); | 42 base::FilePath zip_path(test_dir_); |
| 43 zip_contents_.insert(zip_path.AppendASCII("foo.txt")); | 43 zip_contents_.insert(zip_path.AppendASCII("foo.txt")); |
| 44 zip_path = zip_path.AppendASCII("foo"); | 44 zip_path = zip_path.AppendASCII("foo"); |
| 45 zip_contents_.insert(zip_path); | 45 zip_contents_.insert(zip_path); |
| 46 zip_contents_.insert(zip_path.AppendASCII("bar.txt")); | 46 zip_contents_.insert(zip_path.AppendASCII("bar.txt")); |
| 47 zip_path = zip_path.AppendASCII("bar"); | 47 zip_path = zip_path.AppendASCII("bar"); |
| 48 zip_contents_.insert(zip_path); | 48 zip_contents_.insert(zip_path); |
| 49 zip_contents_.insert(zip_path.AppendASCII("baz.txt")); | 49 zip_contents_.insert(zip_path.AppendASCII("baz.txt")); |
| 50 zip_contents_.insert(zip_path.AppendASCII("quux.txt")); | 50 zip_contents_.insert(zip_path.AppendASCII("quux.txt")); |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 // 2) Zips test.txt and extracts it back into a different location. | 115 // 2) Zips test.txt and extracts it back into a different location. |
| 116 // 3) Confirms that test.txt in the output directory has the specified | 116 // 3) Confirms that test.txt in the output directory has the specified |
| 117 // last modification timestamp if it is valid (|valid_year| is true). | 117 // last modification timestamp if it is valid (|valid_year| is true). |
| 118 // If the timestamp is not supported by the zip format, the last | 118 // If the timestamp is not supported by the zip format, the last |
| 119 // modification defaults to the current time. | 119 // modification defaults to the current time. |
| 120 void TestTimeStamp(const char* date_time, ValidYearType valid_year) { | 120 void TestTimeStamp(const char* date_time, ValidYearType valid_year) { |
| 121 SCOPED_TRACE(std::string("TestTimeStamp(") + date_time + ")"); | 121 SCOPED_TRACE(std::string("TestTimeStamp(") + date_time + ")"); |
| 122 base::ScopedTempDir temp_dir; | 122 base::ScopedTempDir temp_dir; |
| 123 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 123 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 124 | 124 |
| 125 base::FilePath zip_file = temp_dir.path().AppendASCII("out.zip"); | 125 base::FilePath zip_file = temp_dir.GetPath().AppendASCII("out.zip"); |
| 126 base::FilePath src_dir = temp_dir.path().AppendASCII("input"); | 126 base::FilePath src_dir = temp_dir.GetPath().AppendASCII("input"); |
| 127 base::FilePath out_dir = temp_dir.path().AppendASCII("output"); | 127 base::FilePath out_dir = temp_dir.GetPath().AppendASCII("output"); |
| 128 | 128 |
| 129 base::FilePath src_file = src_dir.AppendASCII("test.txt"); | 129 base::FilePath src_file = src_dir.AppendASCII("test.txt"); |
| 130 base::FilePath out_file = out_dir.AppendASCII("test.txt"); | 130 base::FilePath out_file = out_dir.AppendASCII("test.txt"); |
| 131 | 131 |
| 132 EXPECT_TRUE(base::CreateDirectory(src_dir)); | 132 EXPECT_TRUE(base::CreateDirectory(src_dir)); |
| 133 EXPECT_TRUE(base::CreateDirectory(out_dir)); | 133 EXPECT_TRUE(base::CreateDirectory(out_dir)); |
| 134 | 134 |
| 135 base::Time test_mtime; | 135 base::Time test_mtime; |
| 136 ASSERT_TRUE(base::Time::FromString(date_time, &test_mtime)); | 136 ASSERT_TRUE(base::Time::FromString(date_time, &test_mtime)); |
| 137 | 137 |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 ASSERT_FALSE(base::PathExists(evil_file)); | 213 ASSERT_FALSE(base::PathExists(evil_file)); |
| 214 } | 214 } |
| 215 | 215 |
| 216 TEST_F(ZipTest, Zip) { | 216 TEST_F(ZipTest, Zip) { |
| 217 base::FilePath src_dir; | 217 base::FilePath src_dir; |
| 218 ASSERT_TRUE(GetTestDataDirectory(&src_dir)); | 218 ASSERT_TRUE(GetTestDataDirectory(&src_dir)); |
| 219 src_dir = src_dir.AppendASCII("test"); | 219 src_dir = src_dir.AppendASCII("test"); |
| 220 | 220 |
| 221 base::ScopedTempDir temp_dir; | 221 base::ScopedTempDir temp_dir; |
| 222 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 222 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 223 base::FilePath zip_file = temp_dir.path().AppendASCII("out.zip"); | 223 base::FilePath zip_file = temp_dir.GetPath().AppendASCII("out.zip"); |
| 224 | 224 |
| 225 EXPECT_TRUE(zip::Zip(src_dir, zip_file, true)); | 225 EXPECT_TRUE(zip::Zip(src_dir, zip_file, true)); |
| 226 TestUnzipFile(zip_file, true); | 226 TestUnzipFile(zip_file, true); |
| 227 } | 227 } |
| 228 | 228 |
| 229 TEST_F(ZipTest, ZipIgnoreHidden) { | 229 TEST_F(ZipTest, ZipIgnoreHidden) { |
| 230 base::FilePath src_dir; | 230 base::FilePath src_dir; |
| 231 ASSERT_TRUE(GetTestDataDirectory(&src_dir)); | 231 ASSERT_TRUE(GetTestDataDirectory(&src_dir)); |
| 232 src_dir = src_dir.AppendASCII("test"); | 232 src_dir = src_dir.AppendASCII("test"); |
| 233 | 233 |
| 234 base::ScopedTempDir temp_dir; | 234 base::ScopedTempDir temp_dir; |
| 235 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 235 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 236 base::FilePath zip_file = temp_dir.path().AppendASCII("out.zip"); | 236 base::FilePath zip_file = temp_dir.GetPath().AppendASCII("out.zip"); |
| 237 | 237 |
| 238 EXPECT_TRUE(zip::Zip(src_dir, zip_file, false)); | 238 EXPECT_TRUE(zip::Zip(src_dir, zip_file, false)); |
| 239 TestUnzipFile(zip_file, false); | 239 TestUnzipFile(zip_file, false); |
| 240 } | 240 } |
| 241 | 241 |
| 242 TEST_F(ZipTest, ZipNonASCIIDir) { | 242 TEST_F(ZipTest, ZipNonASCIIDir) { |
| 243 base::FilePath src_dir; | 243 base::FilePath src_dir; |
| 244 ASSERT_TRUE(GetTestDataDirectory(&src_dir)); | 244 ASSERT_TRUE(GetTestDataDirectory(&src_dir)); |
| 245 src_dir = src_dir.AppendASCII("test"); | 245 src_dir = src_dir.AppendASCII("test"); |
| 246 | 246 |
| 247 base::ScopedTempDir temp_dir; | 247 base::ScopedTempDir temp_dir; |
| 248 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 248 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 249 // Append 'Тест' (in cyrillic). | 249 // Append 'Тест' (in cyrillic). |
| 250 base::FilePath src_dir_russian = | 250 base::FilePath src_dir_russian = temp_dir.GetPath().Append( |
| 251 temp_dir.path().Append(base::FilePath::FromUTF8Unsafe( | 251 base::FilePath::FromUTF8Unsafe("\xD0\xA2\xD0\xB5\xD1\x81\xD1\x82")); |
| 252 "\xD0\xA2\xD0\xB5\xD1\x81\xD1\x82")); | |
| 253 base::CopyDirectory(src_dir, src_dir_russian, true); | 252 base::CopyDirectory(src_dir, src_dir_russian, true); |
| 254 base::FilePath zip_file = temp_dir.path().AppendASCII("out_russian.zip"); | 253 base::FilePath zip_file = temp_dir.GetPath().AppendASCII("out_russian.zip"); |
| 255 | 254 |
| 256 EXPECT_TRUE(zip::Zip(src_dir_russian, zip_file, true)); | 255 EXPECT_TRUE(zip::Zip(src_dir_russian, zip_file, true)); |
| 257 TestUnzipFile(zip_file, true); | 256 TestUnzipFile(zip_file, true); |
| 258 } | 257 } |
| 259 | 258 |
| 260 TEST_F(ZipTest, ZipTimeStamp) { | 259 TEST_F(ZipTest, ZipTimeStamp) { |
| 261 // The dates tested are arbitrary, with some constraints. The zip format can | 260 // The dates tested are arbitrary, with some constraints. The zip format can |
| 262 // only store years from 1980 to 2107 and an even number of seconds, due to it | 261 // only store years from 1980 to 2107 and an even number of seconds, due to it |
| 263 // using the ms dos date format. | 262 // using the ms dos date format. |
| 264 | 263 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 280 } | 279 } |
| 281 | 280 |
| 282 #if defined(OS_POSIX) | 281 #if defined(OS_POSIX) |
| 283 TEST_F(ZipTest, ZipFiles) { | 282 TEST_F(ZipTest, ZipFiles) { |
| 284 base::FilePath src_dir; | 283 base::FilePath src_dir; |
| 285 ASSERT_TRUE(GetTestDataDirectory(&src_dir)); | 284 ASSERT_TRUE(GetTestDataDirectory(&src_dir)); |
| 286 src_dir = src_dir.AppendASCII("test"); | 285 src_dir = src_dir.AppendASCII("test"); |
| 287 | 286 |
| 288 base::ScopedTempDir temp_dir; | 287 base::ScopedTempDir temp_dir; |
| 289 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 288 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 290 base::FilePath zip_name = temp_dir.path().AppendASCII("out.zip"); | 289 base::FilePath zip_name = temp_dir.GetPath().AppendASCII("out.zip"); |
| 291 | 290 |
| 292 base::File zip_file(zip_name, | 291 base::File zip_file(zip_name, |
| 293 base::File::FLAG_CREATE | base::File::FLAG_WRITE); | 292 base::File::FLAG_CREATE | base::File::FLAG_WRITE); |
| 294 ASSERT_TRUE(zip_file.IsValid()); | 293 ASSERT_TRUE(zip_file.IsValid()); |
| 295 EXPECT_TRUE(zip::ZipFiles(src_dir, zip_file_list_, | 294 EXPECT_TRUE(zip::ZipFiles(src_dir, zip_file_list_, |
| 296 zip_file.GetPlatformFile())); | 295 zip_file.GetPlatformFile())); |
| 297 zip_file.Close(); | 296 zip_file.Close(); |
| 298 | 297 |
| 299 zip::ZipReader reader; | 298 zip::ZipReader reader; |
| 300 EXPECT_TRUE(reader.Open(zip_name)); | 299 EXPECT_TRUE(reader.Open(zip_name)); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 314 | 313 |
| 315 // test_mismatch_size.zip contains files with names from 0.txt to 7.txt with | 314 // test_mismatch_size.zip contains files with names from 0.txt to 7.txt with |
| 316 // sizes from 0 to 7 bytes respectively, but the metadata in the zip file says | 315 // sizes from 0 to 7 bytes respectively, but the metadata in the zip file says |
| 317 // the uncompressed size is 3 bytes. The ZipReader and minizip code needs to | 316 // the uncompressed size is 3 bytes. The ZipReader and minizip code needs to |
| 318 // be clever enough to get all the data out. | 317 // be clever enough to get all the data out. |
| 319 base::FilePath test_zip_file = | 318 base::FilePath test_zip_file = |
| 320 test_data_folder.AppendASCII("test_mismatch_size.zip"); | 319 test_data_folder.AppendASCII("test_mismatch_size.zip"); |
| 321 | 320 |
| 322 base::ScopedTempDir scoped_temp_dir; | 321 base::ScopedTempDir scoped_temp_dir; |
| 323 ASSERT_TRUE(scoped_temp_dir.CreateUniqueTempDir()); | 322 ASSERT_TRUE(scoped_temp_dir.CreateUniqueTempDir()); |
| 324 const base::FilePath& temp_dir = scoped_temp_dir.path(); | 323 const base::FilePath& temp_dir = scoped_temp_dir.GetPath(); |
| 325 | 324 |
| 326 ASSERT_TRUE(zip::Unzip(test_zip_file, temp_dir)); | 325 ASSERT_TRUE(zip::Unzip(test_zip_file, temp_dir)); |
| 327 EXPECT_TRUE(base::DirectoryExists(temp_dir.AppendASCII("d"))); | 326 EXPECT_TRUE(base::DirectoryExists(temp_dir.AppendASCII("d"))); |
| 328 | 327 |
| 329 for (int i = 0; i < 8; i++) { | 328 for (int i = 0; i < 8; i++) { |
| 330 SCOPED_TRACE(base::StringPrintf("Processing %d.txt", i)); | 329 SCOPED_TRACE(base::StringPrintf("Processing %d.txt", i)); |
| 331 base::FilePath file_path = temp_dir.AppendASCII( | 330 base::FilePath file_path = temp_dir.AppendASCII( |
| 332 base::StringPrintf("%d.txt", i)); | 331 base::StringPrintf("%d.txt", i)); |
| 333 int64_t file_size = -1; | 332 int64_t file_size = -1; |
| 334 EXPECT_TRUE(base::GetFileSize(file_path, &file_size)); | 333 EXPECT_TRUE(base::GetFileSize(file_path, &file_size)); |
| 335 EXPECT_EQ(static_cast<int64_t>(i), file_size); | 334 EXPECT_EQ(static_cast<int64_t>(i), file_size); |
| 336 } | 335 } |
| 337 } | 336 } |
| 338 | 337 |
| 339 } // namespace | 338 } // namespace |
| OLD | NEW |