| 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/file_util.h" | 10 #include "base/file_util.h" |
| (...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 | 210 |
| 211 TEST_F(ZipReaderTest, ExtractCurrentEntryToFilePath_RegularFile) { | 211 TEST_F(ZipReaderTest, ExtractCurrentEntryToFilePath_RegularFile) { |
| 212 ZipReader reader; | 212 ZipReader reader; |
| 213 ASSERT_TRUE(reader.Open(test_zip_file_)); | 213 ASSERT_TRUE(reader.Open(test_zip_file_)); |
| 214 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); | 214 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); |
| 215 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); | 215 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); |
| 216 ASSERT_TRUE(reader.ExtractCurrentEntryToFilePath( | 216 ASSERT_TRUE(reader.ExtractCurrentEntryToFilePath( |
| 217 test_dir_.AppendASCII("quux.txt"))); | 217 test_dir_.AppendASCII("quux.txt"))); |
| 218 // Read the output file ans compute the MD5. | 218 // Read the output file ans compute the MD5. |
| 219 std::string output; | 219 std::string output; |
| 220 ASSERT_TRUE(file_util::ReadFileToString(test_dir_.AppendASCII("quux.txt"), | 220 ASSERT_TRUE(base::ReadFileToString(test_dir_.AppendASCII("quux.txt"), |
| 221 &output)); | 221 &output)); |
| 222 const std::string md5 = base::MD5String(output); | 222 const std::string md5 = base::MD5String(output); |
| 223 const std::string kExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6"; | 223 const std::string kExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6"; |
| 224 EXPECT_EQ(kExpectedMD5, md5); | 224 EXPECT_EQ(kExpectedMD5, md5); |
| 225 // quux.txt should be larger than kZipBufSize so that we can exercise | 225 // quux.txt should be larger than kZipBufSize so that we can exercise |
| 226 // the loop in ExtractCurrentEntry(). | 226 // the loop in ExtractCurrentEntry(). |
| 227 EXPECT_LT(static_cast<size_t>(internal::kZipBufSize), output.size()); | 227 EXPECT_LT(static_cast<size_t>(internal::kZipBufSize), output.size()); |
| 228 } | 228 } |
| 229 | 229 |
| 230 TEST_F(ZipReaderTest, PlatformFileExtractCurrentEntryToFilePath_RegularFile) { | 230 TEST_F(ZipReaderTest, PlatformFileExtractCurrentEntryToFilePath_RegularFile) { |
| 231 ZipReader reader; | 231 ZipReader reader; |
| 232 PlatformFileWrapper zip_fd_wrapper(test_zip_file_, | 232 PlatformFileWrapper zip_fd_wrapper(test_zip_file_, |
| 233 PlatformFileWrapper::READ_ONLY); | 233 PlatformFileWrapper::READ_ONLY); |
| 234 ASSERT_TRUE(reader.OpenFromPlatformFile(zip_fd_wrapper.platform_file())); | 234 ASSERT_TRUE(reader.OpenFromPlatformFile(zip_fd_wrapper.platform_file())); |
| 235 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); | 235 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); |
| 236 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); | 236 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); |
| 237 ASSERT_TRUE(reader.ExtractCurrentEntryToFilePath( | 237 ASSERT_TRUE(reader.ExtractCurrentEntryToFilePath( |
| 238 test_dir_.AppendASCII("quux.txt"))); | 238 test_dir_.AppendASCII("quux.txt"))); |
| 239 // Read the output file and compute the MD5. | 239 // Read the output file and compute the MD5. |
| 240 std::string output; | 240 std::string output; |
| 241 ASSERT_TRUE(file_util::ReadFileToString(test_dir_.AppendASCII("quux.txt"), | 241 ASSERT_TRUE(base::ReadFileToString(test_dir_.AppendASCII("quux.txt"), |
| 242 &output)); | 242 &output)); |
| 243 const std::string md5 = base::MD5String(output); | 243 const std::string md5 = base::MD5String(output); |
| 244 const std::string kExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6"; | 244 const std::string kExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6"; |
| 245 EXPECT_EQ(kExpectedMD5, md5); | 245 EXPECT_EQ(kExpectedMD5, md5); |
| 246 // quux.txt should be larger than kZipBufSize so that we can exercise | 246 // quux.txt should be larger than kZipBufSize so that we can exercise |
| 247 // the loop in ExtractCurrentEntry(). | 247 // the loop in ExtractCurrentEntry(). |
| 248 EXPECT_LT(static_cast<size_t>(internal::kZipBufSize), output.size()); | 248 EXPECT_LT(static_cast<size_t>(internal::kZipBufSize), output.size()); |
| 249 } | 249 } |
| 250 | 250 |
| 251 #if defined(OS_POSIX) | 251 #if defined(OS_POSIX) |
| 252 TEST_F(ZipReaderTest, PlatformFileExtractCurrentEntryToFd_RegularFile) { | 252 TEST_F(ZipReaderTest, PlatformFileExtractCurrentEntryToFd_RegularFile) { |
| 253 ZipReader reader; | 253 ZipReader reader; |
| 254 PlatformFileWrapper zip_fd_wrapper(test_zip_file_, | 254 PlatformFileWrapper zip_fd_wrapper(test_zip_file_, |
| 255 PlatformFileWrapper::READ_ONLY); | 255 PlatformFileWrapper::READ_ONLY); |
| 256 ASSERT_TRUE(reader.OpenFromPlatformFile(zip_fd_wrapper.platform_file())); | 256 ASSERT_TRUE(reader.OpenFromPlatformFile(zip_fd_wrapper.platform_file())); |
| 257 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); | 257 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); |
| 258 base::FilePath out_path = test_dir_.AppendASCII("quux.txt"); | 258 base::FilePath out_path = test_dir_.AppendASCII("quux.txt"); |
| 259 PlatformFileWrapper out_fd_w(out_path, PlatformFileWrapper::READ_WRITE); | 259 PlatformFileWrapper out_fd_w(out_path, PlatformFileWrapper::READ_WRITE); |
| 260 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); | 260 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); |
| 261 ASSERT_TRUE(reader.ExtractCurrentEntryToFd(out_fd_w.platform_file())); | 261 ASSERT_TRUE(reader.ExtractCurrentEntryToFd(out_fd_w.platform_file())); |
| 262 // Read the output file and compute the MD5. | 262 // Read the output file and compute the MD5. |
| 263 std::string output; | 263 std::string output; |
| 264 ASSERT_TRUE(file_util::ReadFileToString(test_dir_.AppendASCII("quux.txt"), | 264 ASSERT_TRUE(base::ReadFileToString(test_dir_.AppendASCII("quux.txt"), |
| 265 &output)); | 265 &output)); |
| 266 const std::string md5 = base::MD5String(output); | 266 const std::string md5 = base::MD5String(output); |
| 267 const std::string kExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6"; | 267 const std::string kExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6"; |
| 268 EXPECT_EQ(kExpectedMD5, md5); | 268 EXPECT_EQ(kExpectedMD5, md5); |
| 269 // quux.txt should be larger than kZipBufSize so that we can exercise | 269 // quux.txt should be larger than kZipBufSize so that we can exercise |
| 270 // the loop in ExtractCurrentEntry(). | 270 // the loop in ExtractCurrentEntry(). |
| 271 EXPECT_LT(static_cast<size_t>(internal::kZipBufSize), output.size()); | 271 EXPECT_LT(static_cast<size_t>(internal::kZipBufSize), output.size()); |
| 272 } | 272 } |
| 273 #endif | 273 #endif |
| 274 | 274 |
| 275 TEST_F(ZipReaderTest, ExtractCurrentEntryToFilePath_Directory) { | 275 TEST_F(ZipReaderTest, ExtractCurrentEntryToFilePath_Directory) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 286 TEST_F(ZipReaderTest, ExtractCurrentEntryIntoDirectory_RegularFile) { | 286 TEST_F(ZipReaderTest, ExtractCurrentEntryIntoDirectory_RegularFile) { |
| 287 ZipReader reader; | 287 ZipReader reader; |
| 288 ASSERT_TRUE(reader.Open(test_zip_file_)); | 288 ASSERT_TRUE(reader.Open(test_zip_file_)); |
| 289 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); | 289 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); |
| 290 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); | 290 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); |
| 291 ASSERT_TRUE(reader.ExtractCurrentEntryIntoDirectory(test_dir_)); | 291 ASSERT_TRUE(reader.ExtractCurrentEntryIntoDirectory(test_dir_)); |
| 292 // Sub directories should be created. | 292 // Sub directories should be created. |
| 293 ASSERT_TRUE(base::DirectoryExists(test_dir_.AppendASCII("foo/bar"))); | 293 ASSERT_TRUE(base::DirectoryExists(test_dir_.AppendASCII("foo/bar"))); |
| 294 // And the file should be created. | 294 // And the file should be created. |
| 295 std::string output; | 295 std::string output; |
| 296 ASSERT_TRUE(file_util::ReadFileToString( | 296 ASSERT_TRUE(base::ReadFileToString( |
| 297 test_dir_.AppendASCII("foo/bar/quux.txt"), &output)); | 297 test_dir_.AppendASCII("foo/bar/quux.txt"), &output)); |
| 298 const std::string md5 = base::MD5String(output); | 298 const std::string md5 = base::MD5String(output); |
| 299 const std::string kExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6"; | 299 const std::string kExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6"; |
| 300 EXPECT_EQ(kExpectedMD5, md5); | 300 EXPECT_EQ(kExpectedMD5, md5); |
| 301 } | 301 } |
| 302 | 302 |
| 303 TEST_F(ZipReaderTest, current_entry_info_RegularFile) { | 303 TEST_F(ZipReaderTest, current_entry_info_RegularFile) { |
| 304 ZipReader reader; | 304 ZipReader reader; |
| 305 ASSERT_TRUE(reader.Open(test_zip_file_)); | 305 ASSERT_TRUE(reader.Open(test_zip_file_)); |
| 306 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); | 306 base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 416 "\x52\x00\x00\x00\x00\x00"; | 416 "\x52\x00\x00\x00\x00\x00"; |
| 417 std::string data(kTestData, arraysize(kTestData)); | 417 std::string data(kTestData, arraysize(kTestData)); |
| 418 ZipReader reader; | 418 ZipReader reader; |
| 419 ASSERT_TRUE(reader.OpenFromString(data)); | 419 ASSERT_TRUE(reader.OpenFromString(data)); |
| 420 base::FilePath target_path(FILE_PATH_LITERAL("test.txt")); | 420 base::FilePath target_path(FILE_PATH_LITERAL("test.txt")); |
| 421 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); | 421 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); |
| 422 ASSERT_TRUE(reader.ExtractCurrentEntryToFilePath( | 422 ASSERT_TRUE(reader.ExtractCurrentEntryToFilePath( |
| 423 test_dir_.AppendASCII("test.txt"))); | 423 test_dir_.AppendASCII("test.txt"))); |
| 424 | 424 |
| 425 std::string actual; | 425 std::string actual; |
| 426 ASSERT_TRUE(file_util::ReadFileToString( | 426 ASSERT_TRUE(base::ReadFileToString( |
| 427 test_dir_.AppendASCII("test.txt"), &actual)); | 427 test_dir_.AppendASCII("test.txt"), &actual)); |
| 428 EXPECT_EQ(std::string("This is a test.\n"), actual); | 428 EXPECT_EQ(std::string("This is a test.\n"), actual); |
| 429 } | 429 } |
| 430 | 430 |
| 431 } // namespace zip | 431 } // namespace zip |
| OLD | NEW |