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

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

Issue 13257004: [components] Make zip a component so that src/chromeos can use it. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: DEPS, OWNERS Created 7 years, 8 months 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 | Annotate | Revision Log
« no previous file with comments | « chrome/common/zip_reader_unittest.cc ('k') | chrome/test/data/zip/create_test_zip.sh » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include <set>
6 #include <vector>
7
8 #include "base/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/path_service.h"
11 #include "base/string_util.h"
12 #include "chrome/common/chrome_paths.h"
13 #include "chrome/common/zip.h"
14 #include "chrome/common/zip_reader.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "testing/platform_test.h"
17
18 namespace {
19
20 // Make the test a PlatformTest to setup autorelease pools properly on Mac.
21 class ZipTest : public PlatformTest {
22 protected:
23 virtual void SetUp() {
24 PlatformTest::SetUp();
25
26 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
27 test_dir_ = temp_dir_.path();
28
29 base::FilePath zip_path(test_dir_);
30 zip_contents_.insert(zip_path.AppendASCII("foo.txt"));
31 zip_path = zip_path.AppendASCII("foo");
32 zip_contents_.insert(zip_path);
33 zip_contents_.insert(zip_path.AppendASCII("bar.txt"));
34 zip_path = zip_path.AppendASCII("bar");
35 zip_contents_.insert(zip_path);
36 zip_contents_.insert(zip_path.AppendASCII("baz.txt"));
37 zip_contents_.insert(zip_path.AppendASCII("quux.txt"));
38 zip_contents_.insert(zip_path.AppendASCII(".hidden"));
39
40 // Include a subset of files in |zip_file_list_| to test ZipFiles().
41 zip_file_list_.push_back(base::FilePath(FILE_PATH_LITERAL("foo.txt")));
42 zip_file_list_.push_back(
43 base::FilePath(FILE_PATH_LITERAL("foo/bar/quux.txt")));
44 zip_file_list_.push_back(
45 base::FilePath(FILE_PATH_LITERAL("foo/bar/.hidden")));
46 }
47
48 virtual void TearDown() {
49 PlatformTest::TearDown();
50 }
51
52 void TestUnzipFile(const base::FilePath::StringType& filename,
53 bool expect_hidden_files) {
54 base::FilePath test_dir;
55 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir));
56 test_dir = test_dir.AppendASCII("zip");
57 TestUnzipFile(test_dir.Append(filename), expect_hidden_files);
58 }
59
60 void TestUnzipFile(const base::FilePath& path, bool expect_hidden_files) {
61 ASSERT_TRUE(file_util::PathExists(path)) << "no file " << path.value();
62 ASSERT_TRUE(zip::Unzip(path, test_dir_));
63
64 file_util::FileEnumerator files(test_dir_, true,
65 file_util::FileEnumerator::FILES |
66 file_util::FileEnumerator::DIRECTORIES);
67 base::FilePath next_path = files.Next();
68 size_t count = 0;
69 while (!next_path.value().empty()) {
70 if (next_path.value().find(FILE_PATH_LITERAL(".svn")) ==
71 base::FilePath::StringType::npos) {
72 EXPECT_EQ(zip_contents_.count(next_path), 1U) <<
73 "Couldn't find " << next_path.value();
74 count++;
75 }
76 next_path = files.Next();
77 }
78
79 size_t expected_count = 0;
80 for (std::set<base::FilePath>::iterator iter = zip_contents_.begin();
81 iter != zip_contents_.end(); ++iter) {
82 if (expect_hidden_files || iter->BaseName().value()[0] != '.')
83 ++expected_count;
84 }
85
86 EXPECT_EQ(expected_count, count);
87 }
88
89 // The path to temporary directory used to contain the test operations.
90 base::FilePath test_dir_;
91
92 base::ScopedTempDir temp_dir_;
93
94 // Hard-coded contents of a known zip file.
95 std::set<base::FilePath> zip_contents_;
96
97 // Hard-coded list of relative paths for a zip file created with ZipFiles.
98 std::vector<base::FilePath> zip_file_list_;
99 };
100
101 TEST_F(ZipTest, Unzip) {
102 TestUnzipFile(FILE_PATH_LITERAL("test.zip"), true);
103 }
104
105 TEST_F(ZipTest, UnzipUncompressed) {
106 TestUnzipFile(FILE_PATH_LITERAL("test_nocompress.zip"), true);
107 }
108
109 TEST_F(ZipTest, UnzipEvil) {
110 base::FilePath path;
111 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &path));
112 path = path.AppendASCII("zip").AppendASCII("evil.zip");
113 // Unzip the zip file into a sub directory of test_dir_ so evil.zip
114 // won't create a persistent file outside test_dir_ in case of a
115 // failure.
116 base::FilePath output_dir = test_dir_.AppendASCII("out");
117 ASSERT_FALSE(zip::Unzip(path, output_dir));
118 base::FilePath evil_file = output_dir;
119 evil_file = evil_file.AppendASCII(
120 "../levilevilevilevilevilevilevilevilevilevilevilevil");
121 ASSERT_FALSE(file_util::PathExists(evil_file));
122 }
123
124 TEST_F(ZipTest, UnzipEvil2) {
125 base::FilePath path;
126 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &path));
127 // The zip file contains an evil file with invalid UTF-8 in its file
128 // name.
129 path = path.AppendASCII("zip").AppendASCII("evil_via_invalid_utf8.zip");
130 // See the comment at UnzipEvil() for why we do this.
131 base::FilePath output_dir = test_dir_.AppendASCII("out");
132 // This should fail as it contains an evil file.
133 ASSERT_FALSE(zip::Unzip(path, output_dir));
134 base::FilePath evil_file = output_dir;
135 evil_file = evil_file.AppendASCII("../evil.txt");
136 ASSERT_FALSE(file_util::PathExists(evil_file));
137 }
138
139 TEST_F(ZipTest, Zip) {
140 base::FilePath src_dir;
141 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &src_dir));
142 src_dir = src_dir.AppendASCII("zip").AppendASCII("test");
143
144 base::ScopedTempDir temp_dir;
145 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
146 base::FilePath zip_file = temp_dir.path().AppendASCII("out.zip");
147
148 EXPECT_TRUE(zip::Zip(src_dir, zip_file, true));
149 TestUnzipFile(zip_file, true);
150 }
151
152 TEST_F(ZipTest, ZipIgnoreHidden) {
153 base::FilePath src_dir;
154 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &src_dir));
155 src_dir = src_dir.AppendASCII("zip").AppendASCII("test");
156
157 base::ScopedTempDir temp_dir;
158 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
159 base::FilePath zip_file = temp_dir.path().AppendASCII("out.zip");
160
161 EXPECT_TRUE(zip::Zip(src_dir, zip_file, false));
162 TestUnzipFile(zip_file, false);
163 }
164
165 #if defined(OS_POSIX)
166 TEST_F(ZipTest, ZipFiles) {
167 base::FilePath src_dir;
168 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &src_dir));
169 src_dir = src_dir.AppendASCII("zip").AppendASCII("test");
170
171 base::ScopedTempDir temp_dir;
172 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
173 base::FilePath zip_file = temp_dir.path().AppendASCII("out.zip");
174
175 const int flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
176 const base::PlatformFile zip_fd =
177 base::CreatePlatformFile(zip_file, flags, NULL, NULL);
178 ASSERT_LE(0, zip_fd);
179 EXPECT_TRUE(zip::ZipFiles(src_dir, zip_file_list_, zip_fd));
180 base::ClosePlatformFile(zip_fd);
181
182 zip::ZipReader reader;
183 EXPECT_TRUE(reader.Open(zip_file));
184 EXPECT_EQ(zip_file_list_.size(), static_cast<size_t>(reader.num_entries()));
185 for (size_t i = 0; i < zip_file_list_.size(); ++i) {
186 EXPECT_TRUE(reader.LocateAndOpenEntry(zip_file_list_[i]));
187 // Check the path in the entry just in case.
188 const zip::ZipReader::EntryInfo* entry_info = reader.current_entry_info();
189 EXPECT_EQ(entry_info->file_path(), zip_file_list_[i]);
190 }
191 }
192 #endif // defined(OS_POSIX)
193
194 } // namespace
195
OLDNEW
« no previous file with comments | « chrome/common/zip_reader_unittest.cc ('k') | chrome/test/data/zip/create_test_zip.sh » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698