Chromium Code Reviews| 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 "chrome/common/zip_reader.h" | 5 #include "chrome/common/zip_reader.h" |
| 6 | 6 |
| 7 #if defined(OS_POSIX) | |
| 8 #include <fcntl.h> | |
| 9 #include <sys/stat.h> | |
| 10 #include <sys/types.h> | |
| 11 #endif | |
| 12 | |
| 7 #include <set> | 13 #include <set> |
| 8 #include <string> | 14 #include <string> |
| 9 | 15 |
| 10 #include "base/file_util.h" | 16 #include "base/file_util.h" |
| 11 #include "base/md5.h" | 17 #include "base/md5.h" |
| 12 #include "base/path_service.h" | 18 #include "base/path_service.h" |
| 13 #include "base/scoped_temp_dir.h" | 19 #include "base/scoped_temp_dir.h" |
| 14 #include "base/time.h" | 20 #include "base/time.h" |
| 15 #include "base/utf_string_conversions.h" | 21 #include "base/utf_string_conversions.h" |
| 16 #include "chrome/common/chrome_paths.h" | 22 #include "chrome/common/chrome_paths.h" |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 45 test_zip_contents_.insert(FilePath(FILE_PATH_LITERAL("foo/bar/quux.txt"))); | 51 test_zip_contents_.insert(FilePath(FILE_PATH_LITERAL("foo/bar/quux.txt"))); |
| 46 test_zip_contents_.insert(FilePath(FILE_PATH_LITERAL("foo/bar.txt"))); | 52 test_zip_contents_.insert(FilePath(FILE_PATH_LITERAL("foo/bar.txt"))); |
| 47 test_zip_contents_.insert(FilePath(FILE_PATH_LITERAL("foo.txt"))); | 53 test_zip_contents_.insert(FilePath(FILE_PATH_LITERAL("foo.txt"))); |
| 48 test_zip_contents_.insert(FilePath(FILE_PATH_LITERAL("foo/bar/.hidden"))); | 54 test_zip_contents_.insert(FilePath(FILE_PATH_LITERAL("foo/bar/.hidden"))); |
| 49 } | 55 } |
| 50 | 56 |
| 51 virtual void TearDown() { | 57 virtual void TearDown() { |
| 52 PlatformTest::TearDown(); | 58 PlatformTest::TearDown(); |
| 53 } | 59 } |
| 54 | 60 |
| 61 #if defined(OS_POSIX) | |
| 62 int OpenFileRdOnly(FilePath& file) { | |
|
satorux1
2011/12/12 04:54:14
Please write a brief function comment.
http://www
Jorge Lucangeli Obes
2011/12/12 23:34:35
Done.
| |
| 63 return open(file.value().c_str(), O_RDONLY); | |
| 64 } | |
|
satorux1
2011/12/12 04:54:14
We usually have a blank line between function defi
Jorge Lucangeli Obes
2011/12/12 23:34:35
Done.
| |
| 65 int OpenFileRdWr(FilePath& file) { | |
| 66 return open(file.value().c_str(), | |
| 67 O_RDWR | O_CREAT, | |
| 68 S_IRUSR | S_IWUSR); | |
| 69 } | |
| 70 #endif | |
| 71 | |
| 55 // The path to temporary directory used to contain the test operations. | 72 // The path to temporary directory used to contain the test operations. |
| 56 FilePath test_dir_; | 73 FilePath test_dir_; |
| 57 // The path to the test data directory where test.zip etc. are located. | 74 // The path to the test data directory where test.zip etc. are located. |
| 58 FilePath test_data_dir_; | 75 FilePath test_data_dir_; |
| 59 // The path to test.zip in the test data directory. | 76 // The path to test.zip in the test data directory. |
| 60 FilePath test_zip_file_; | 77 FilePath test_zip_file_; |
| 61 // The path to evil.zip in the test data directory. | 78 // The path to evil.zip in the test data directory. |
| 62 FilePath evil_zip_file_; | 79 FilePath evil_zip_file_; |
| 63 // The path to evil_via_invalid_utf8.zip in the test data directory. | 80 // The path to evil_via_invalid_utf8.zip in the test data directory. |
| 64 FilePath evil_via_invalid_utf8_zip_file_; | 81 FilePath evil_via_invalid_utf8_zip_file_; |
| 65 // The path to evil_via_absolute_file_name.zip in the test data directory. | 82 // The path to evil_via_absolute_file_name.zip in the test data directory. |
| 66 FilePath evil_via_absolute_file_name_zip_file_; | 83 FilePath evil_via_absolute_file_name_zip_file_; |
| 67 std::set<FilePath> test_zip_contents_; | 84 std::set<FilePath> test_zip_contents_; |
| 68 | 85 |
| 69 ScopedTempDir temp_dir_; | 86 ScopedTempDir temp_dir_; |
| 70 }; | 87 }; |
| 71 | 88 |
| 72 TEST_F(ZipReaderTest, Open_ValidZipFile) { | 89 TEST_F(ZipReaderTest, Open_ValidZipFile) { |
| 73 ZipReader reader; | 90 ZipReader reader; |
| 74 ASSERT_TRUE(reader.Open(test_zip_file_)); | 91 ASSERT_TRUE(reader.Open(test_zip_file_)); |
| 75 } | 92 } |
| 76 | 93 |
| 94 #if defined(OS_POSIX) | |
| 95 TEST_F(ZipReaderTest, Open_ValidZipFd) { | |
| 96 ZipReader reader; | |
| 97 int zip_fd = OpenFileRdOnly(test_zip_file_); | |
| 98 ASSERT_TRUE(reader.OpenFd(zip_fd)); | |
| 99 } | |
| 100 #endif | |
| 101 | |
| 77 TEST_F(ZipReaderTest, Open_NonExistentFile) { | 102 TEST_F(ZipReaderTest, Open_NonExistentFile) { |
| 78 ZipReader reader; | 103 ZipReader reader; |
| 79 ASSERT_FALSE(reader.Open(test_data_dir_.AppendASCII("nonexistent.zip"))); | 104 ASSERT_FALSE(reader.Open(test_data_dir_.AppendASCII("nonexistent.zip"))); |
| 80 } | 105 } |
| 81 | 106 |
| 82 TEST_F(ZipReaderTest, Open_ExistentButNonZipFile) { | 107 TEST_F(ZipReaderTest, Open_ExistentButNonZipFile) { |
| 83 ZipReader reader; | 108 ZipReader reader; |
| 84 ASSERT_FALSE(reader.Open(test_data_dir_.AppendASCII("create_test_zip.sh"))); | 109 ASSERT_FALSE(reader.Open(test_data_dir_.AppendASCII("create_test_zip.sh"))); |
| 85 } | 110 } |
| 86 | 111 |
| 87 // Iterate through the contents in the test zip file, and compare that the | 112 // Iterate through the contents in the test zip file, and compare that the |
| 88 // contents collected from the zip reader matches the expected contents. | 113 // contents collected from the zip reader matches the expected contents. |
| 89 TEST_F(ZipReaderTest, Iteration) { | 114 TEST_F(ZipReaderTest, Iteration) { |
| 90 std::set<FilePath> actual_contents; | 115 std::set<FilePath> actual_contents; |
| 91 ZipReader reader; | 116 ZipReader reader; |
| 92 ASSERT_TRUE(reader.Open(test_zip_file_)); | 117 ASSERT_TRUE(reader.Open(test_zip_file_)); |
| 93 while (reader.HasMore()) { | 118 while (reader.HasMore()) { |
| 94 ASSERT_TRUE(reader.OpenCurrentEntryInZip()); | 119 ASSERT_TRUE(reader.OpenCurrentEntryInZip()); |
| 95 actual_contents.insert(reader.current_entry_info()->file_path()); | 120 actual_contents.insert(reader.current_entry_info()->file_path()); |
| 96 ASSERT_TRUE(reader.AdvanceToNextEntry()); | 121 ASSERT_TRUE(reader.AdvanceToNextEntry()); |
| 97 } | 122 } |
| 98 EXPECT_FALSE(reader.AdvanceToNextEntry()); // Shouldn't go further. | 123 EXPECT_FALSE(reader.AdvanceToNextEntry()); // Shouldn't go further. |
| 99 EXPECT_EQ(test_zip_contents_.size(), | 124 EXPECT_EQ(test_zip_contents_.size(), |
| 100 static_cast<size_t>(reader.num_entries())); | 125 static_cast<size_t>(reader.num_entries())); |
| 101 EXPECT_EQ(test_zip_contents_.size(), actual_contents.size()); | 126 EXPECT_EQ(test_zip_contents_.size(), actual_contents.size()); |
| 102 EXPECT_EQ(test_zip_contents_, actual_contents); | 127 EXPECT_EQ(test_zip_contents_, actual_contents); |
| 103 } | 128 } |
| 104 | 129 |
| 130 #if defined(OS_POSIX) | |
| 131 // Open the test zip file from a file descriptor, iterate through its contents, | |
| 132 // and compare that they match the expected contents. | |
| 133 TEST_F(ZipReaderTest, FdIteration) { | |
| 134 std::set<FilePath> actual_contents; | |
| 135 ZipReader reader; | |
| 136 int zip_fd = OpenFileRdOnly(test_zip_file_); | |
| 137 ASSERT_TRUE(reader.OpenFd(zip_fd)); | |
| 138 while (reader.HasMore()) { | |
| 139 ASSERT_TRUE(reader.OpenCurrentEntryInZip()); | |
| 140 actual_contents.insert(reader.current_entry_info()->file_path()); | |
| 141 ASSERT_TRUE(reader.AdvanceToNextEntry()); | |
| 142 } | |
| 143 EXPECT_FALSE(reader.AdvanceToNextEntry()); // Shouldn't go further. | |
| 144 EXPECT_EQ(test_zip_contents_.size(), | |
| 145 static_cast<size_t>(reader.num_entries())); | |
| 146 EXPECT_EQ(test_zip_contents_.size(), actual_contents.size()); | |
| 147 EXPECT_EQ(test_zip_contents_, actual_contents); | |
| 148 } | |
| 149 #endif | |
| 105 | 150 |
| 106 TEST_F(ZipReaderTest, LocateAndOpenEntry_ValidFile) { | 151 TEST_F(ZipReaderTest, LocateAndOpenEntry_ValidFile) { |
| 107 std::set<FilePath> actual_contents; | 152 std::set<FilePath> actual_contents; |
| 108 ZipReader reader; | 153 ZipReader reader; |
| 109 ASSERT_TRUE(reader.Open(test_zip_file_)); | 154 ASSERT_TRUE(reader.Open(test_zip_file_)); |
| 110 FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); | 155 FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); |
| 111 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); | 156 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); |
| 112 EXPECT_EQ(target_path, reader.current_entry_info()->file_path()); | 157 EXPECT_EQ(target_path, reader.current_entry_info()->file_path()); |
| 113 } | 158 } |
| 114 | 159 |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 133 ASSERT_TRUE(file_util::ReadFileToString(test_dir_.AppendASCII("quux.txt"), | 178 ASSERT_TRUE(file_util::ReadFileToString(test_dir_.AppendASCII("quux.txt"), |
| 134 &output)); | 179 &output)); |
| 135 const std::string md5 = base::MD5String(output); | 180 const std::string md5 = base::MD5String(output); |
| 136 const std::string kExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6"; | 181 const std::string kExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6"; |
| 137 EXPECT_EQ(kExpectedMD5, md5); | 182 EXPECT_EQ(kExpectedMD5, md5); |
| 138 // quux.txt should be larger than kZipBufSize so that we can exercise | 183 // quux.txt should be larger than kZipBufSize so that we can exercise |
| 139 // the loop in ExtractCurrentEntry(). | 184 // the loop in ExtractCurrentEntry(). |
| 140 EXPECT_LT(static_cast<size_t>(internal::kZipBufSize), output.size()); | 185 EXPECT_LT(static_cast<size_t>(internal::kZipBufSize), output.size()); |
| 141 } | 186 } |
| 142 | 187 |
| 188 #if defined(OS_POSIX) | |
| 189 TEST_F(ZipReaderTest, FdExtractCurrentEntryToFilePath_RegularFile) { | |
| 190 ZipReader reader; | |
| 191 int zip_fd = OpenFileRdOnly(test_zip_file_); | |
| 192 ASSERT_TRUE(reader.OpenFd(zip_fd)); | |
| 193 FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); | |
| 194 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); | |
| 195 ASSERT_TRUE(reader.ExtractCurrentEntryToFilePath( | |
| 196 test_dir_.AppendASCII("quux.txt"))); | |
| 197 // Read the output file and compute the MD5. | |
| 198 std::string output; | |
| 199 ASSERT_TRUE(file_util::ReadFileToString(test_dir_.AppendASCII("quux.txt"), | |
| 200 &output)); | |
| 201 const std::string md5 = base::MD5String(output); | |
| 202 const std::string kExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6"; | |
| 203 EXPECT_EQ(kExpectedMD5, md5); | |
| 204 // quux.txt should be larger than kZipBufSize so that we can exercise | |
| 205 // the loop in ExtractCurrentEntry(). | |
| 206 EXPECT_LT(static_cast<size_t>(internal::kZipBufSize), output.size()); | |
| 207 } | |
| 208 | |
| 209 TEST_F(ZipReaderTest, FdExtractCurrentEntryToFd_RegularFile) { | |
| 210 ZipReader reader; | |
| 211 int zip_fd = OpenFileRdOnly(test_zip_file_); | |
| 212 ASSERT_TRUE(reader.OpenFd(zip_fd)); | |
| 213 FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); | |
| 214 FilePath out_path = test_dir_.AppendASCII("quux.txt"); | |
| 215 int out_fd = OpenFileRdWr(out_path); | |
| 216 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); | |
| 217 ASSERT_TRUE(reader.ExtractCurrentEntryToFd(out_fd)); | |
| 218 // Read the output file and compute the MD5. | |
| 219 std::string output; | |
| 220 ASSERT_TRUE(file_util::ReadFileToString(test_dir_.AppendASCII("quux.txt"), | |
| 221 &output)); | |
| 222 const std::string md5 = base::MD5String(output); | |
| 223 const std::string kExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6"; | |
| 224 EXPECT_EQ(kExpectedMD5, md5); | |
| 225 // quux.txt should be larger than kZipBufSize so that we can exercise | |
| 226 // the loop in ExtractCurrentEntry(). | |
| 227 EXPECT_LT(static_cast<size_t>(internal::kZipBufSize), output.size()); | |
| 228 } | |
| 229 #endif | |
| 230 | |
| 143 TEST_F(ZipReaderTest, ExtractCurrentEntryToFilePath_Directory) { | 231 TEST_F(ZipReaderTest, ExtractCurrentEntryToFilePath_Directory) { |
| 144 ZipReader reader; | 232 ZipReader reader; |
| 145 ASSERT_TRUE(reader.Open(test_zip_file_)); | 233 ASSERT_TRUE(reader.Open(test_zip_file_)); |
| 146 FilePath target_path(FILE_PATH_LITERAL("foo/")); | 234 FilePath target_path(FILE_PATH_LITERAL("foo/")); |
| 147 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); | 235 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); |
| 148 ASSERT_TRUE(reader.ExtractCurrentEntryToFilePath( | 236 ASSERT_TRUE(reader.ExtractCurrentEntryToFilePath( |
| 149 test_dir_.AppendASCII("foo"))); | 237 test_dir_.AppendASCII("foo"))); |
| 150 // The directory should be created. | 238 // The directory should be created. |
| 151 ASSERT_TRUE(file_util::DirectoryExists(test_dir_.AppendASCII("foo"))); | 239 ASSERT_TRUE(file_util::DirectoryExists(test_dir_.AppendASCII("foo"))); |
| 152 } | 240 } |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 256 EXPECT_EQ(15, exploded.hour); | 344 EXPECT_EQ(15, exploded.hour); |
| 257 EXPECT_EQ(49, exploded.minute); | 345 EXPECT_EQ(49, exploded.minute); |
| 258 EXPECT_EQ(52, exploded.second); | 346 EXPECT_EQ(52, exploded.second); |
| 259 EXPECT_EQ(0, exploded.millisecond); | 347 EXPECT_EQ(0, exploded.millisecond); |
| 260 | 348 |
| 261 EXPECT_FALSE(current_entry_info->is_unsafe()); | 349 EXPECT_FALSE(current_entry_info->is_unsafe()); |
| 262 EXPECT_TRUE(current_entry_info->is_directory()); | 350 EXPECT_TRUE(current_entry_info->is_directory()); |
| 263 } | 351 } |
| 264 | 352 |
| 265 } // namespace zip | 353 } // namespace zip |
| OLD | NEW |