Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(42)

Side by Side Diff: chrome/common/zip_reader_unittest.cc

Issue 8873039: Add an API to unpack Zip files directly from and to file descriptors. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: More style fixes. Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/common/zip_reader.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 typedef enum {
34 READ_ONLY,
35 READ_WRITE
36 } AccessMode;
37
38 FdWrapper(const FilePath& file, AccessMode mode) : fd_(-1) {
39 switch (mode) {
40 case READ_ONLY:
41 fd_ = open(file.value().c_str(), O_RDONLY);
42 break;
43 case READ_WRITE:
44 fd_ = open(file.value().c_str(),
45 O_RDWR | O_CREAT,
46 S_IRUSR | S_IWUSR);
47 break;
48 default:
49 NOTREACHED();
50 }
51 return;
52 }
53
54 ~FdWrapper() {
55 close(fd_);
56 }
57
58 int fd() { return fd_; }
59
60 private:
61 int fd_;
62 };
63 #endif
64
65 } // namespace
66
21 namespace zip { 67 namespace zip {
22 68
23 // Make the test a PlatformTest to setup autorelease pools properly on Mac. 69 // Make the test a PlatformTest to setup autorelease pools properly on Mac.
24 class ZipReaderTest : public PlatformTest { 70 class ZipReaderTest : public PlatformTest {
25 protected: 71 protected:
26 virtual void SetUp() { 72 virtual void SetUp() {
27 PlatformTest::SetUp(); 73 PlatformTest::SetUp();
28 74
29 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 75 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
30 test_dir_ = temp_dir_.path(); 76 test_dir_ = temp_dir_.path();
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 std::set<FilePath> test_zip_contents_; 113 std::set<FilePath> test_zip_contents_;
68 114
69 ScopedTempDir temp_dir_; 115 ScopedTempDir temp_dir_;
70 }; 116 };
71 117
72 TEST_F(ZipReaderTest, Open_ValidZipFile) { 118 TEST_F(ZipReaderTest, Open_ValidZipFile) {
73 ZipReader reader; 119 ZipReader reader;
74 ASSERT_TRUE(reader.Open(test_zip_file_)); 120 ASSERT_TRUE(reader.Open(test_zip_file_));
75 } 121 }
76 122
123 #if defined(OS_POSIX)
124 TEST_F(ZipReaderTest, Open_ValidZipFd) {
125 ZipReader reader;
126 FdWrapper zip_fd_wrapper(test_zip_file_, FdWrapper::READ_ONLY);
127 ASSERT_TRUE(reader.OpenFromFd(zip_fd_wrapper.fd()));
128 }
129 #endif
130
77 TEST_F(ZipReaderTest, Open_NonExistentFile) { 131 TEST_F(ZipReaderTest, Open_NonExistentFile) {
78 ZipReader reader; 132 ZipReader reader;
79 ASSERT_FALSE(reader.Open(test_data_dir_.AppendASCII("nonexistent.zip"))); 133 ASSERT_FALSE(reader.Open(test_data_dir_.AppendASCII("nonexistent.zip")));
80 } 134 }
81 135
82 TEST_F(ZipReaderTest, Open_ExistentButNonZipFile) { 136 TEST_F(ZipReaderTest, Open_ExistentButNonZipFile) {
83 ZipReader reader; 137 ZipReader reader;
84 ASSERT_FALSE(reader.Open(test_data_dir_.AppendASCII("create_test_zip.sh"))); 138 ASSERT_FALSE(reader.Open(test_data_dir_.AppendASCII("create_test_zip.sh")));
85 } 139 }
86 140
87 // Iterate through the contents in the test zip file, and compare that the 141 // Iterate through the contents in the test zip file, and compare that the
88 // contents collected from the zip reader matches the expected contents. 142 // contents collected from the zip reader matches the expected contents.
89 TEST_F(ZipReaderTest, Iteration) { 143 TEST_F(ZipReaderTest, Iteration) {
90 std::set<FilePath> actual_contents; 144 std::set<FilePath> actual_contents;
91 ZipReader reader; 145 ZipReader reader;
92 ASSERT_TRUE(reader.Open(test_zip_file_)); 146 ASSERT_TRUE(reader.Open(test_zip_file_));
93 while (reader.HasMore()) { 147 while (reader.HasMore()) {
94 ASSERT_TRUE(reader.OpenCurrentEntryInZip()); 148 ASSERT_TRUE(reader.OpenCurrentEntryInZip());
95 actual_contents.insert(reader.current_entry_info()->file_path()); 149 actual_contents.insert(reader.current_entry_info()->file_path());
96 ASSERT_TRUE(reader.AdvanceToNextEntry()); 150 ASSERT_TRUE(reader.AdvanceToNextEntry());
97 } 151 }
98 EXPECT_FALSE(reader.AdvanceToNextEntry()); // Shouldn't go further. 152 EXPECT_FALSE(reader.AdvanceToNextEntry()); // Shouldn't go further.
99 EXPECT_EQ(test_zip_contents_.size(), 153 EXPECT_EQ(test_zip_contents_.size(),
100 static_cast<size_t>(reader.num_entries())); 154 static_cast<size_t>(reader.num_entries()));
101 EXPECT_EQ(test_zip_contents_.size(), actual_contents.size()); 155 EXPECT_EQ(test_zip_contents_.size(), actual_contents.size());
102 EXPECT_EQ(test_zip_contents_, actual_contents); 156 EXPECT_EQ(test_zip_contents_, actual_contents);
103 } 157 }
104 158
159 #if defined(OS_POSIX)
160 // Open the test zip file from a file descriptor, iterate through its contents,
161 // and compare that they match the expected contents.
162 TEST_F(ZipReaderTest, FdIteration) {
163 std::set<FilePath> actual_contents;
164 ZipReader reader;
165 FdWrapper zip_fd_wrapper(test_zip_file_, FdWrapper::READ_ONLY);
166 ASSERT_TRUE(reader.OpenFromFd(zip_fd_wrapper.fd()));
167 while (reader.HasMore()) {
168 ASSERT_TRUE(reader.OpenCurrentEntryInZip());
169 actual_contents.insert(reader.current_entry_info()->file_path());
170 ASSERT_TRUE(reader.AdvanceToNextEntry());
171 }
172 EXPECT_FALSE(reader.AdvanceToNextEntry()); // Shouldn't go further.
173 EXPECT_EQ(test_zip_contents_.size(),
174 static_cast<size_t>(reader.num_entries()));
175 EXPECT_EQ(test_zip_contents_.size(), actual_contents.size());
176 EXPECT_EQ(test_zip_contents_, actual_contents);
177 }
178 #endif
105 179
106 TEST_F(ZipReaderTest, LocateAndOpenEntry_ValidFile) { 180 TEST_F(ZipReaderTest, LocateAndOpenEntry_ValidFile) {
107 std::set<FilePath> actual_contents; 181 std::set<FilePath> actual_contents;
108 ZipReader reader; 182 ZipReader reader;
109 ASSERT_TRUE(reader.Open(test_zip_file_)); 183 ASSERT_TRUE(reader.Open(test_zip_file_));
110 FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); 184 FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt"));
111 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); 185 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path));
112 EXPECT_EQ(target_path, reader.current_entry_info()->file_path()); 186 EXPECT_EQ(target_path, reader.current_entry_info()->file_path());
113 } 187 }
114 188
(...skipping 18 matching lines...) Expand all
133 ASSERT_TRUE(file_util::ReadFileToString(test_dir_.AppendASCII("quux.txt"), 207 ASSERT_TRUE(file_util::ReadFileToString(test_dir_.AppendASCII("quux.txt"),
134 &output)); 208 &output));
135 const std::string md5 = base::MD5String(output); 209 const std::string md5 = base::MD5String(output);
136 const std::string kExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6"; 210 const std::string kExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6";
137 EXPECT_EQ(kExpectedMD5, md5); 211 EXPECT_EQ(kExpectedMD5, md5);
138 // quux.txt should be larger than kZipBufSize so that we can exercise 212 // quux.txt should be larger than kZipBufSize so that we can exercise
139 // the loop in ExtractCurrentEntry(). 213 // the loop in ExtractCurrentEntry().
140 EXPECT_LT(static_cast<size_t>(internal::kZipBufSize), output.size()); 214 EXPECT_LT(static_cast<size_t>(internal::kZipBufSize), output.size());
141 } 215 }
142 216
217 #if defined(OS_POSIX)
218 TEST_F(ZipReaderTest, FdExtractCurrentEntryToFilePath_RegularFile) {
219 ZipReader reader;
220 FdWrapper zip_fd_wrapper(test_zip_file_, FdWrapper::READ_ONLY);
221 ASSERT_TRUE(reader.OpenFromFd(zip_fd_wrapper.fd()));
222 FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt"));
223 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path));
224 ASSERT_TRUE(reader.ExtractCurrentEntryToFilePath(
225 test_dir_.AppendASCII("quux.txt")));
226 // Read the output file and compute the MD5.
227 std::string output;
228 ASSERT_TRUE(file_util::ReadFileToString(test_dir_.AppendASCII("quux.txt"),
229 &output));
230 const std::string md5 = base::MD5String(output);
231 const std::string kExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6";
232 EXPECT_EQ(kExpectedMD5, md5);
233 // quux.txt should be larger than kZipBufSize so that we can exercise
234 // the loop in ExtractCurrentEntry().
235 EXPECT_LT(static_cast<size_t>(internal::kZipBufSize), output.size());
236 }
237
238 TEST_F(ZipReaderTest, FdExtractCurrentEntryToFd_RegularFile) {
239 ZipReader reader;
240 FdWrapper zip_fd_wrapper(test_zip_file_, FdWrapper::READ_ONLY);
241 ASSERT_TRUE(reader.OpenFromFd(zip_fd_wrapper.fd()));
242 FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt"));
243 FilePath out_path = test_dir_.AppendASCII("quux.txt");
244 FdWrapper out_fd_w(out_path, FdWrapper::READ_WRITE);
245 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path));
246 ASSERT_TRUE(reader.ExtractCurrentEntryToFd(out_fd_w.fd()));
247 // Read the output file and compute the MD5.
248 std::string output;
249 ASSERT_TRUE(file_util::ReadFileToString(test_dir_.AppendASCII("quux.txt"),
250 &output));
251 const std::string md5 = base::MD5String(output);
252 const std::string kExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6";
253 EXPECT_EQ(kExpectedMD5, md5);
254 // quux.txt should be larger than kZipBufSize so that we can exercise
255 // the loop in ExtractCurrentEntry().
256 EXPECT_LT(static_cast<size_t>(internal::kZipBufSize), output.size());
257 }
258 #endif
259
143 TEST_F(ZipReaderTest, ExtractCurrentEntryToFilePath_Directory) { 260 TEST_F(ZipReaderTest, ExtractCurrentEntryToFilePath_Directory) {
144 ZipReader reader; 261 ZipReader reader;
145 ASSERT_TRUE(reader.Open(test_zip_file_)); 262 ASSERT_TRUE(reader.Open(test_zip_file_));
146 FilePath target_path(FILE_PATH_LITERAL("foo/")); 263 FilePath target_path(FILE_PATH_LITERAL("foo/"));
147 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); 264 ASSERT_TRUE(reader.LocateAndOpenEntry(target_path));
148 ASSERT_TRUE(reader.ExtractCurrentEntryToFilePath( 265 ASSERT_TRUE(reader.ExtractCurrentEntryToFilePath(
149 test_dir_.AppendASCII("foo"))); 266 test_dir_.AppendASCII("foo")));
150 // The directory should be created. 267 // The directory should be created.
151 ASSERT_TRUE(file_util::DirectoryExists(test_dir_.AppendASCII("foo"))); 268 ASSERT_TRUE(file_util::DirectoryExists(test_dir_.AppendASCII("foo")));
152 } 269 }
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 EXPECT_EQ(15, exploded.hour); 373 EXPECT_EQ(15, exploded.hour);
257 EXPECT_EQ(49, exploded.minute); 374 EXPECT_EQ(49, exploded.minute);
258 EXPECT_EQ(52, exploded.second); 375 EXPECT_EQ(52, exploded.second);
259 EXPECT_EQ(0, exploded.millisecond); 376 EXPECT_EQ(0, exploded.millisecond);
260 377
261 EXPECT_FALSE(current_entry_info->is_unsafe()); 378 EXPECT_FALSE(current_entry_info->is_unsafe());
262 EXPECT_TRUE(current_entry_info->is_directory()); 379 EXPECT_TRUE(current_entry_info->is_directory());
263 } 380 }
264 381
265 } // namespace zip 382 } // namespace zip
OLDNEW
« no previous file with comments | « chrome/common/zip_reader.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698