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

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

Powered by Google App Engine
This is Rietveld 408576698