| 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 "third_party/zlib/google/zip_reader.h" | 5 #include "third_party/zlib/google/zip_reader.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 12 #include "base/files/file.h" |
| 12 #include "base/files/scoped_temp_dir.h" | 13 #include "base/files/scoped_temp_dir.h" |
| 13 #include "base/logging.h" | 14 #include "base/logging.h" |
| 14 #include "base/md5.h" | 15 #include "base/md5.h" |
| 15 #include "base/path_service.h" | 16 #include "base/path_service.h" |
| 16 #include "base/platform_file.h" | |
| 17 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
| 18 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
| 19 #include "base/time/time.h" | 19 #include "base/time/time.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 #include "testing/platform_test.h" | 21 #include "testing/platform_test.h" |
| 22 #include "third_party/zlib/google/zip_internal.h" | 22 #include "third_party/zlib/google/zip_internal.h" |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 const static std::string kQuuxExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6"; | 26 const static std::string kQuuxExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6"; |
| 27 | 27 |
| 28 // Wrap PlatformFiles in a class so that we don't leak them in tests. | 28 class FileWrapper { |
| 29 class PlatformFileWrapper { | |
| 30 public: | 29 public: |
| 31 typedef enum { | 30 typedef enum { |
| 32 READ_ONLY, | 31 READ_ONLY, |
| 33 READ_WRITE | 32 READ_WRITE |
| 34 } AccessMode; | 33 } AccessMode; |
| 35 | 34 |
| 36 PlatformFileWrapper(const base::FilePath& file, AccessMode mode) | 35 FileWrapper(const base::FilePath& path, AccessMode mode) { |
| 37 : file_(base::kInvalidPlatformFileValue) { | 36 int flags = base::File::FLAG_READ; |
| 38 switch (mode) { | 37 if (mode == READ_ONLY) |
| 39 case READ_ONLY: | 38 flags |= base::File::FLAG_OPEN; |
| 40 file_ = base::CreatePlatformFile(file, | 39 else |
| 41 base::PLATFORM_FILE_OPEN | | 40 flags |= base::File::FLAG_WRITE | base::File::FLAG_CREATE_ALWAYS; |
| 42 base::PLATFORM_FILE_READ, | 41 |
| 43 NULL, NULL); | 42 file_.Initialize(path, flags); |
| 44 break; | |
| 45 case READ_WRITE: | |
| 46 file_ = base::CreatePlatformFile(file, | |
| 47 base::PLATFORM_FILE_CREATE_ALWAYS | | |
| 48 base::PLATFORM_FILE_READ | | |
| 49 base::PLATFORM_FILE_WRITE, | |
| 50 NULL, NULL); | |
| 51 break; | |
| 52 default: | |
| 53 NOTREACHED(); | |
| 54 } | |
| 55 return; | |
| 56 } | 43 } |
| 57 | 44 |
| 58 ~PlatformFileWrapper() { | 45 ~FileWrapper() {} |
| 59 base::ClosePlatformFile(file_); | |
| 60 } | |
| 61 | 46 |
| 62 base::PlatformFile platform_file() { return file_; } | 47 base::PlatformFile platform_file() { return file_.GetPlatformFile(); } |
| 63 | 48 |
| 64 private: | 49 private: |
| 65 base::PlatformFile file_; | 50 base::File file_; |
| 66 }; | 51 }; |
| 67 | 52 |
| 68 // A mock that provides methods that can be used as callbacks in asynchronous | 53 // A mock that provides methods that can be used as callbacks in asynchronous |
| 69 // unzip functions. Tracks the number of calls and number of bytes reported. | 54 // unzip functions. Tracks the number of calls and number of bytes reported. |
| 70 // Assumes that progress callbacks will be executed in-order. | 55 // Assumes that progress callbacks will be executed in-order. |
| 71 class MockUnzipListener : public base::SupportsWeakPtr<MockUnzipListener> { | 56 class MockUnzipListener : public base::SupportsWeakPtr<MockUnzipListener> { |
| 72 public: | 57 public: |
| 73 MockUnzipListener() | 58 MockUnzipListener() |
| 74 : success_calls_(0), | 59 : success_calls_(0), |
| 75 failure_calls_(0), | 60 failure_calls_(0), |
| 76 progress_calls_(0), | 61 progress_calls_(0), |
| 77 current_progress_(0) { | 62 current_progress_(0) { |
| 78 } | 63 } |
| 79 | 64 |
| 80 // Success callback for async functions. | 65 // Success callback for async functions. |
| 81 void OnUnzipSuccess() { | 66 void OnUnzipSuccess() { |
| 82 success_calls_++; | 67 success_calls_++; |
| 83 } | 68 } |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 base::MessageLoop message_loop_; | 173 base::MessageLoop message_loop_; |
| 189 }; | 174 }; |
| 190 | 175 |
| 191 TEST_F(ZipReaderTest, Open_ValidZipFile) { | 176 TEST_F(ZipReaderTest, Open_ValidZipFile) { |
| 192 ZipReader reader; | 177 ZipReader reader; |
| 193 ASSERT_TRUE(reader.Open(test_zip_file_)); | 178 ASSERT_TRUE(reader.Open(test_zip_file_)); |
| 194 } | 179 } |
| 195 | 180 |
| 196 TEST_F(ZipReaderTest, Open_ValidZipPlatformFile) { | 181 TEST_F(ZipReaderTest, Open_ValidZipPlatformFile) { |
| 197 ZipReader reader; | 182 ZipReader reader; |
| 198 PlatformFileWrapper zip_fd_wrapper(test_zip_file_, | 183 FileWrapper zip_fd_wrapper(test_zip_file_, FileWrapper::READ_ONLY); |
| 199 PlatformFileWrapper::READ_ONLY); | |
| 200 ASSERT_TRUE(reader.OpenFromPlatformFile(zip_fd_wrapper.platform_file())); | 184 ASSERT_TRUE(reader.OpenFromPlatformFile(zip_fd_wrapper.platform_file())); |
| 201 } | 185 } |
| 202 | 186 |
| 203 TEST_F(ZipReaderTest, Open_NonExistentFile) { | 187 TEST_F(ZipReaderTest, Open_NonExistentFile) { |
| 204 ZipReader reader; | 188 ZipReader reader; |
| 205 ASSERT_FALSE(reader.Open(test_data_dir_.AppendASCII("nonexistent.zip"))); | 189 ASSERT_FALSE(reader.Open(test_data_dir_.AppendASCII("nonexistent.zip"))); |
| 206 } | 190 } |
| 207 | 191 |
| 208 TEST_F(ZipReaderTest, Open_ExistentButNonZipFile) { | 192 TEST_F(ZipReaderTest, Open_ExistentButNonZipFile) { |
| 209 ZipReader reader; | 193 ZipReader reader; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 226 static_cast<size_t>(reader.num_entries())); | 210 static_cast<size_t>(reader.num_entries())); |
| 227 EXPECT_EQ(test_zip_contents_.size(), actual_contents.size()); | 211 EXPECT_EQ(test_zip_contents_.size(), actual_contents.size()); |
| 228 EXPECT_EQ(test_zip_contents_, actual_contents); | 212 EXPECT_EQ(test_zip_contents_, actual_contents); |
| 229 } | 213 } |
| 230 | 214 |
| 231 // Open the test zip file from a file descriptor, iterate through its contents, | 215 // Open the test zip file from a file descriptor, iterate through its contents, |
| 232 // and compare that they match the expected contents. | 216 // and compare that they match the expected contents. |
| 233 TEST_F(ZipReaderTest, PlatformFileIteration) { | 217 TEST_F(ZipReaderTest, PlatformFileIteration) { |
| 234 std::set<base::FilePath> actual_contents; | 218 std::set<base::FilePath> actual_contents; |
| 235 ZipReader reader; | 219 ZipReader reader; |
| 236 PlatformFileWrapper zip_fd_wrapper(test_zip_file_, | 220 FileWrapper zip_fd_wrapper(test_zip_file_, FileWrapper::READ_ONLY); |
| 237 PlatformFileWrapper::READ_ONLY); | |
| 238 ASSERT_TRUE(reader.OpenFromPlatformFile(zip_fd_wrapper.platform_file())); | 221 ASSERT_TRUE(reader.OpenFromPlatformFile(zip_fd_wrapper.platform_file())); |
| 239 while (reader.HasMore()) { | 222 while (reader.HasMore()) { |
| 240 ASSERT_TRUE(reader.OpenCurrentEntryInZip()); | 223 ASSERT_TRUE(reader.OpenCurrentEntryInZip()); |
| 241 actual_contents.insert(reader.current_entry_info()->file_path()); | 224 actual_contents.insert(reader.current_entry_info()->file_path()); |
| 242 ASSERT_TRUE(reader.AdvanceToNextEntry()); | 225 ASSERT_TRUE(reader.AdvanceToNextEntry()); |
| 243 } | 226 } |
| 244 EXPECT_FALSE(reader.AdvanceToNextEntry()); // Shouldn't go further. | 227 EXPECT_FALSE(reader.AdvanceToNextEntry()); // Shouldn't go further. |
| 245 EXPECT_EQ(test_zip_contents_.size(), | 228 EXPECT_EQ(test_zip_contents_.size(), |
| 246 static_cast<size_t>(reader.num_entries())); | 229 static_cast<size_t>(reader.num_entries())); |
| 247 EXPECT_EQ(test_zip_contents_.size(), actual_contents.size()); | 230 EXPECT_EQ(test_zip_contents_.size(), actual_contents.size()); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 &output)); | 262 &output)); |
| 280 const std::string md5 = base::MD5String(output); | 263 const std::string md5 = base::MD5String(output); |
| 281 EXPECT_EQ(kQuuxExpectedMD5, md5); | 264 EXPECT_EQ(kQuuxExpectedMD5, md5); |
| 282 // quux.txt should be larger than kZipBufSize so that we can exercise | 265 // quux.txt should be larger than kZipBufSize so that we can exercise |
| 283 // the loop in ExtractCurrentEntry(). | 266 // the loop in ExtractCurrentEntry(). |
| 284 EXPECT_LT(static_cast<size_t>(internal::kZipBufSize), output.size()); | 267 EXPECT_LT(static_cast<size_t>(internal::kZipBufSize), output.size()); |
| 285 } | 268 } |
| 286 | 269 |
| 287 TEST_F(ZipReaderTest, PlatformFileExtractCurrentEntryToFilePath_RegularFile) { | 270 TEST_F(ZipReaderTest, PlatformFileExtractCurrentEntryToFilePath_RegularFile) { |
| 288 ZipReader reader; | 271 ZipReader reader; |
| 289 PlatformFileWrapper zip_fd_wrapper(test_zip_file_, | 272 FileWrapper zip_fd_wrapper(test_zip_file_, FileWrapper::READ_ONLY); |
| 290 PlatformFileWrapper::READ_ONLY); | |
| 291 ASSERT_TRUE(reader.OpenFromPlatformFile(zip_fd_wrapper.platform_file())); | 273 ASSERT_TRUE(reader.OpenFromPlatformFile(zip_fd_wrapper.platform_file())); |
| 292 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); | 274 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); |
| 293 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); | 275 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); |
| 294 ASSERT_TRUE(reader.ExtractCurrentEntryToFilePath( | 276 ASSERT_TRUE(reader.ExtractCurrentEntryToFilePath( |
| 295 test_dir_.AppendASCII("quux.txt"))); | 277 test_dir_.AppendASCII("quux.txt"))); |
| 296 // Read the output file and compute the MD5. | 278 // Read the output file and compute the MD5. |
| 297 std::string output; | 279 std::string output; |
| 298 ASSERT_TRUE(base::ReadFileToString(test_dir_.AppendASCII("quux.txt"), | 280 ASSERT_TRUE(base::ReadFileToString(test_dir_.AppendASCII("quux.txt"), |
| 299 &output)); | 281 &output)); |
| 300 const std::string md5 = base::MD5String(output); | 282 const std::string md5 = base::MD5String(output); |
| 301 EXPECT_EQ(kQuuxExpectedMD5, md5); | 283 EXPECT_EQ(kQuuxExpectedMD5, md5); |
| 302 // quux.txt should be larger than kZipBufSize so that we can exercise | 284 // quux.txt should be larger than kZipBufSize so that we can exercise |
| 303 // the loop in ExtractCurrentEntry(). | 285 // the loop in ExtractCurrentEntry(). |
| 304 EXPECT_LT(static_cast<size_t>(internal::kZipBufSize), output.size()); | 286 EXPECT_LT(static_cast<size_t>(internal::kZipBufSize), output.size()); |
| 305 } | 287 } |
| 306 | 288 |
| 307 #if defined(OS_POSIX) | 289 #if defined(OS_POSIX) |
| 308 TEST_F(ZipReaderTest, PlatformFileExtractCurrentEntryToFd_RegularFile) { | 290 TEST_F(ZipReaderTest, PlatformFileExtractCurrentEntryToFd_RegularFile) { |
| 309 ZipReader reader; | 291 ZipReader reader; |
| 310 PlatformFileWrapper zip_fd_wrapper(test_zip_file_, | 292 FileWrapper zip_fd_wrapper(test_zip_file_, FileWrapper::READ_ONLY); |
| 311 PlatformFileWrapper::READ_ONLY); | |
| 312 ASSERT_TRUE(reader.OpenFromPlatformFile(zip_fd_wrapper.platform_file())); | 293 ASSERT_TRUE(reader.OpenFromPlatformFile(zip_fd_wrapper.platform_file())); |
| 313 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); | 294 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); |
| 314 base::FilePath out_path = test_dir_.AppendASCII("quux.txt"); | 295 base::FilePath out_path = test_dir_.AppendASCII("quux.txt"); |
| 315 PlatformFileWrapper out_fd_w(out_path, PlatformFileWrapper::READ_WRITE); | 296 FileWrapper out_fd_w(out_path, FileWrapper::READ_WRITE); |
| 316 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); | 297 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); |
| 317 ASSERT_TRUE(reader.ExtractCurrentEntryToFd(out_fd_w.platform_file())); | 298 ASSERT_TRUE(reader.ExtractCurrentEntryToFd(out_fd_w.platform_file())); |
| 318 // Read the output file and compute the MD5. | 299 // Read the output file and compute the MD5. |
| 319 std::string output; | 300 std::string output; |
| 320 ASSERT_TRUE(base::ReadFileToString(test_dir_.AppendASCII("quux.txt"), | 301 ASSERT_TRUE(base::ReadFileToString(test_dir_.AppendASCII("quux.txt"), |
| 321 &output)); | 302 &output)); |
| 322 const std::string md5 = base::MD5String(output); | 303 const std::string md5 = base::MD5String(output); |
| 323 EXPECT_EQ(kQuuxExpectedMD5, md5); | 304 EXPECT_EQ(kQuuxExpectedMD5, md5); |
| 324 // quux.txt should be larger than kZipBufSize so that we can exercise | 305 // quux.txt should be larger than kZipBufSize so that we can exercise |
| 325 // the loop in ExtractCurrentEntry(). | 306 // the loop in ExtractCurrentEntry(). |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 547 base::RunLoop().RunUntilIdle(); | 528 base::RunLoop().RunUntilIdle(); |
| 548 | 529 |
| 549 EXPECT_EQ(1, listener.success_calls()); | 530 EXPECT_EQ(1, listener.success_calls()); |
| 550 EXPECT_EQ(0, listener.failure_calls()); | 531 EXPECT_EQ(0, listener.failure_calls()); |
| 551 EXPECT_GE(0, listener.progress_calls()); | 532 EXPECT_GE(0, listener.progress_calls()); |
| 552 | 533 |
| 553 ASSERT_TRUE(base::DirectoryExists(target_file)); | 534 ASSERT_TRUE(base::DirectoryExists(target_file)); |
| 554 } | 535 } |
| 555 | 536 |
| 556 } // namespace zip | 537 } // namespace zip |
| OLD | NEW |