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" |
17 #include "chrome/common/zip_internal.h" | 23 #include "chrome/common/zip_internal.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
19 #include "testing/platform_test.h" | 25 #include "testing/platform_test.h" |
20 | 26 |
27 namespace { | |
28 | |
29 #if defined(OS_POSIX) | |
30 // Wrap file descriptors in a class so that we don't leak them in tests. | |
31 class FdWrapper { | |
32 public: | |
33 enum { READ_ONLY, | |
34 READ_WRITE }; | |
satorux1
2011/12/15 00:40:45
Sorry to be nit-picky, but enum should be formatt
Jorge Lucangeli Obes
2011/12/15 01:06:19
Done.
| |
35 | |
36 FdWrapper(const FilePath& file, int mode) : fd_(-1) { | |
satorux1
2011/12/15 00:40:45
Instead of 'int', please use enum type, like ReadM
Jorge Lucangeli Obes
2011/12/15 01:06:19
Done.
| |
37 switch (mode) { | |
38 case READ_ONLY: | |
39 fd_ = open(file.value().c_str(), O_RDONLY); | |
40 break; | |
41 case READ_WRITE: | |
42 fd_ = open(file.value().c_str(), | |
43 O_RDWR | O_CREAT, | |
44 S_IRUSR | S_IWUSR); | |
45 break; | |
46 default: | |
47 NOTREACHED(); | |
48 } | |
49 return; | |
50 } | |
51 | |
52 ~FdWrapper() { | |
53 close(fd_); | |
54 } | |
55 | |
56 int fd() { return fd_; } | |
57 | |
58 private: | |
59 int fd_; | |
60 }; | |
61 #endif | |
62 | |
63 } // namespace | |
64 | |
21 namespace zip { | 65 namespace zip { |
22 | 66 |
23 // Make the test a PlatformTest to setup autorelease pools properly on Mac. | 67 // Make the test a PlatformTest to setup autorelease pools properly on Mac. |
24 class ZipReaderTest : public PlatformTest { | 68 class ZipReaderTest : public PlatformTest { |
25 protected: | 69 protected: |
26 virtual void SetUp() { | 70 virtual void SetUp() { |
27 PlatformTest::SetUp(); | 71 PlatformTest::SetUp(); |
28 | 72 |
29 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 73 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
30 test_dir_ = temp_dir_.path(); | 74 test_dir_ = temp_dir_.path(); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
67 std::set<FilePath> test_zip_contents_; | 111 std::set<FilePath> test_zip_contents_; |
68 | 112 |
69 ScopedTempDir temp_dir_; | 113 ScopedTempDir temp_dir_; |
70 }; | 114 }; |
71 | 115 |
72 TEST_F(ZipReaderTest, Open_ValidZipFile) { | 116 TEST_F(ZipReaderTest, Open_ValidZipFile) { |
73 ZipReader reader; | 117 ZipReader reader; |
74 ASSERT_TRUE(reader.Open(test_zip_file_)); | 118 ASSERT_TRUE(reader.Open(test_zip_file_)); |
75 } | 119 } |
76 | 120 |
121 #if defined(OS_POSIX) | |
122 TEST_F(ZipReaderTest, Open_ValidZipFd) { | |
123 ZipReader reader; | |
124 FdWrapper zip_fd_wrapper(test_zip_file_, FdWrapper::READ_ONLY); | |
125 ASSERT_TRUE(reader.OpenFromFd(zip_fd_wrapper.fd())); | |
126 } | |
127 #endif | |
128 | |
77 TEST_F(ZipReaderTest, Open_NonExistentFile) { | 129 TEST_F(ZipReaderTest, Open_NonExistentFile) { |
78 ZipReader reader; | 130 ZipReader reader; |
79 ASSERT_FALSE(reader.Open(test_data_dir_.AppendASCII("nonexistent.zip"))); | 131 ASSERT_FALSE(reader.Open(test_data_dir_.AppendASCII("nonexistent.zip"))); |
80 } | 132 } |
81 | 133 |
82 TEST_F(ZipReaderTest, Open_ExistentButNonZipFile) { | 134 TEST_F(ZipReaderTest, Open_ExistentButNonZipFile) { |
83 ZipReader reader; | 135 ZipReader reader; |
84 ASSERT_FALSE(reader.Open(test_data_dir_.AppendASCII("create_test_zip.sh"))); | 136 ASSERT_FALSE(reader.Open(test_data_dir_.AppendASCII("create_test_zip.sh"))); |
85 } | 137 } |
86 | 138 |
87 // Iterate through the contents in the test zip file, and compare that the | 139 // Iterate through the contents in the test zip file, and compare that the |
88 // contents collected from the zip reader matches the expected contents. | 140 // contents collected from the zip reader matches the expected contents. |
89 TEST_F(ZipReaderTest, Iteration) { | 141 TEST_F(ZipReaderTest, Iteration) { |
90 std::set<FilePath> actual_contents; | 142 std::set<FilePath> actual_contents; |
91 ZipReader reader; | 143 ZipReader reader; |
92 ASSERT_TRUE(reader.Open(test_zip_file_)); | 144 ASSERT_TRUE(reader.Open(test_zip_file_)); |
93 while (reader.HasMore()) { | 145 while (reader.HasMore()) { |
94 ASSERT_TRUE(reader.OpenCurrentEntryInZip()); | 146 ASSERT_TRUE(reader.OpenCurrentEntryInZip()); |
95 actual_contents.insert(reader.current_entry_info()->file_path()); | 147 actual_contents.insert(reader.current_entry_info()->file_path()); |
96 ASSERT_TRUE(reader.AdvanceToNextEntry()); | 148 ASSERT_TRUE(reader.AdvanceToNextEntry()); |
97 } | 149 } |
98 EXPECT_FALSE(reader.AdvanceToNextEntry()); // Shouldn't go further. | 150 EXPECT_FALSE(reader.AdvanceToNextEntry()); // Shouldn't go further. |
99 EXPECT_EQ(test_zip_contents_.size(), | 151 EXPECT_EQ(test_zip_contents_.size(), |
100 static_cast<size_t>(reader.num_entries())); | 152 static_cast<size_t>(reader.num_entries())); |
101 EXPECT_EQ(test_zip_contents_.size(), actual_contents.size()); | 153 EXPECT_EQ(test_zip_contents_.size(), actual_contents.size()); |
102 EXPECT_EQ(test_zip_contents_, actual_contents); | 154 EXPECT_EQ(test_zip_contents_, actual_contents); |
103 } | 155 } |
104 | 156 |
157 #if defined(OS_POSIX) | |
158 // Open the test zip file from a file descriptor, iterate through its contents, | |
159 // and compare that they match the expected contents. | |
160 TEST_F(ZipReaderTest, FdIteration) { | |
161 std::set<FilePath> actual_contents; | |
162 ZipReader reader; | |
163 FdWrapper zip_fd_wrapper(test_zip_file_, FdWrapper::READ_ONLY); | |
164 ASSERT_TRUE(reader.OpenFromFd(zip_fd_wrapper.fd())); | |
165 while (reader.HasMore()) { | |
166 ASSERT_TRUE(reader.OpenCurrentEntryInZip()); | |
167 actual_contents.insert(reader.current_entry_info()->file_path()); | |
168 ASSERT_TRUE(reader.AdvanceToNextEntry()); | |
169 } | |
170 EXPECT_FALSE(reader.AdvanceToNextEntry()); // Shouldn't go further. | |
171 EXPECT_EQ(test_zip_contents_.size(), | |
172 static_cast<size_t>(reader.num_entries())); | |
173 EXPECT_EQ(test_zip_contents_.size(), actual_contents.size()); | |
174 EXPECT_EQ(test_zip_contents_, actual_contents); | |
175 } | |
176 #endif | |
105 | 177 |
106 TEST_F(ZipReaderTest, LocateAndOpenEntry_ValidFile) { | 178 TEST_F(ZipReaderTest, LocateAndOpenEntry_ValidFile) { |
107 std::set<FilePath> actual_contents; | 179 std::set<FilePath> actual_contents; |
108 ZipReader reader; | 180 ZipReader reader; |
109 ASSERT_TRUE(reader.Open(test_zip_file_)); | 181 ASSERT_TRUE(reader.Open(test_zip_file_)); |
110 FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); | 182 FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); |
111 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); | 183 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); |
112 EXPECT_EQ(target_path, reader.current_entry_info()->file_path()); | 184 EXPECT_EQ(target_path, reader.current_entry_info()->file_path()); |
113 } | 185 } |
114 | 186 |
(...skipping 18 matching lines...) Expand all Loading... | |
133 ASSERT_TRUE(file_util::ReadFileToString(test_dir_.AppendASCII("quux.txt"), | 205 ASSERT_TRUE(file_util::ReadFileToString(test_dir_.AppendASCII("quux.txt"), |
134 &output)); | 206 &output)); |
135 const std::string md5 = base::MD5String(output); | 207 const std::string md5 = base::MD5String(output); |
136 const std::string kExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6"; | 208 const std::string kExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6"; |
137 EXPECT_EQ(kExpectedMD5, md5); | 209 EXPECT_EQ(kExpectedMD5, md5); |
138 // quux.txt should be larger than kZipBufSize so that we can exercise | 210 // quux.txt should be larger than kZipBufSize so that we can exercise |
139 // the loop in ExtractCurrentEntry(). | 211 // the loop in ExtractCurrentEntry(). |
140 EXPECT_LT(static_cast<size_t>(internal::kZipBufSize), output.size()); | 212 EXPECT_LT(static_cast<size_t>(internal::kZipBufSize), output.size()); |
141 } | 213 } |
142 | 214 |
215 #if defined(OS_POSIX) | |
216 TEST_F(ZipReaderTest, FdExtractCurrentEntryToFilePath_RegularFile) { | |
217 ZipReader reader; | |
218 FdWrapper zip_fd_wrapper(test_zip_file_, FdWrapper::READ_ONLY); | |
219 ASSERT_TRUE(reader.OpenFromFd(zip_fd_wrapper.fd())); | |
220 FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); | |
221 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); | |
222 ASSERT_TRUE(reader.ExtractCurrentEntryToFilePath( | |
223 test_dir_.AppendASCII("quux.txt"))); | |
224 // Read the output file and compute the MD5. | |
225 std::string output; | |
226 ASSERT_TRUE(file_util::ReadFileToString(test_dir_.AppendASCII("quux.txt"), | |
227 &output)); | |
228 const std::string md5 = base::MD5String(output); | |
229 const std::string kExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6"; | |
230 EXPECT_EQ(kExpectedMD5, md5); | |
231 // quux.txt should be larger than kZipBufSize so that we can exercise | |
232 // the loop in ExtractCurrentEntry(). | |
233 EXPECT_LT(static_cast<size_t>(internal::kZipBufSize), output.size()); | |
234 } | |
235 | |
236 TEST_F(ZipReaderTest, FdExtractCurrentEntryToFd_RegularFile) { | |
237 ZipReader reader; | |
238 FdWrapper zip_fd_wrapper(test_zip_file_, FdWrapper::READ_ONLY); | |
239 ASSERT_TRUE(reader.OpenFromFd(zip_fd_wrapper.fd())); | |
240 FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); | |
241 FilePath out_path = test_dir_.AppendASCII("quux.txt"); | |
242 FdWrapper out_fd_w(out_path, FdWrapper::READ_WRITE); | |
243 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); | |
244 ASSERT_TRUE(reader.ExtractCurrentEntryToFd(out_fd_w.fd())); | |
245 // Read the output file and compute the MD5. | |
246 std::string output; | |
247 ASSERT_TRUE(file_util::ReadFileToString(test_dir_.AppendASCII("quux.txt"), | |
248 &output)); | |
249 const std::string md5 = base::MD5String(output); | |
250 const std::string kExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6"; | |
251 EXPECT_EQ(kExpectedMD5, md5); | |
252 // quux.txt should be larger than kZipBufSize so that we can exercise | |
253 // the loop in ExtractCurrentEntry(). | |
254 EXPECT_LT(static_cast<size_t>(internal::kZipBufSize), output.size()); | |
255 } | |
256 #endif | |
257 | |
143 TEST_F(ZipReaderTest, ExtractCurrentEntryToFilePath_Directory) { | 258 TEST_F(ZipReaderTest, ExtractCurrentEntryToFilePath_Directory) { |
144 ZipReader reader; | 259 ZipReader reader; |
145 ASSERT_TRUE(reader.Open(test_zip_file_)); | 260 ASSERT_TRUE(reader.Open(test_zip_file_)); |
146 FilePath target_path(FILE_PATH_LITERAL("foo/")); | 261 FilePath target_path(FILE_PATH_LITERAL("foo/")); |
147 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); | 262 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); |
148 ASSERT_TRUE(reader.ExtractCurrentEntryToFilePath( | 263 ASSERT_TRUE(reader.ExtractCurrentEntryToFilePath( |
149 test_dir_.AppendASCII("foo"))); | 264 test_dir_.AppendASCII("foo"))); |
150 // The directory should be created. | 265 // The directory should be created. |
151 ASSERT_TRUE(file_util::DirectoryExists(test_dir_.AppendASCII("foo"))); | 266 ASSERT_TRUE(file_util::DirectoryExists(test_dir_.AppendASCII("foo"))); |
152 } | 267 } |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
256 EXPECT_EQ(15, exploded.hour); | 371 EXPECT_EQ(15, exploded.hour); |
257 EXPECT_EQ(49, exploded.minute); | 372 EXPECT_EQ(49, exploded.minute); |
258 EXPECT_EQ(52, exploded.second); | 373 EXPECT_EQ(52, exploded.second); |
259 EXPECT_EQ(0, exploded.millisecond); | 374 EXPECT_EQ(0, exploded.millisecond); |
260 | 375 |
261 EXPECT_FALSE(current_entry_info->is_unsafe()); | 376 EXPECT_FALSE(current_entry_info->is_unsafe()); |
262 EXPECT_TRUE(current_entry_info->is_directory()); | 377 EXPECT_TRUE(current_entry_info->is_directory()); |
263 } | 378 } |
264 | 379 |
265 } // namespace zip | 380 } // namespace zip |
OLD | NEW |