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