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 <set> | 5 #include <set> |
6 #include <vector> | 6 #include <vector> |
7 | 7 |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
11 #include "base/path_service.h" | 11 #include "base/path_service.h" |
12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
13 #include "components/zip/zip.h" | |
14 #include "components/zip/zip_reader.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
16 #include "testing/platform_test.h" | 14 #include "testing/platform_test.h" |
| 15 #include "third_party/zlib/google/zip.h" |
| 16 #include "third_party/zlib/google/zip_reader.h" |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
20 // Make the test a PlatformTest to setup autorelease pools properly on Mac. | 20 // Make the test a PlatformTest to setup autorelease pools properly on Mac. |
21 class ZipTest : public PlatformTest { | 21 class ZipTest : public PlatformTest { |
22 protected: | 22 protected: |
23 virtual void SetUp() { | 23 virtual void SetUp() { |
24 PlatformTest::SetUp(); | 24 PlatformTest::SetUp(); |
25 | 25 |
26 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 26 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
(...skipping 20 matching lines...) Expand all Loading... |
47 | 47 |
48 virtual void TearDown() { | 48 virtual void TearDown() { |
49 PlatformTest::TearDown(); | 49 PlatformTest::TearDown(); |
50 } | 50 } |
51 | 51 |
52 bool GetTestDataDirectory(base::FilePath* path) { | 52 bool GetTestDataDirectory(base::FilePath* path) { |
53 bool success = PathService::Get(base::DIR_SOURCE_ROOT, path); | 53 bool success = PathService::Get(base::DIR_SOURCE_ROOT, path); |
54 EXPECT_TRUE(success); | 54 EXPECT_TRUE(success); |
55 if (!success) | 55 if (!success) |
56 return false; | 56 return false; |
57 *path = path->AppendASCII("components"); | 57 *path = path->AppendASCII("third_party"); |
| 58 *path = path->AppendASCII("zlib"); |
| 59 *path = path->AppendASCII("google"); |
58 *path = path->AppendASCII("test"); | 60 *path = path->AppendASCII("test"); |
59 *path = path->AppendASCII("data"); | 61 *path = path->AppendASCII("data"); |
60 return true; | 62 return true; |
61 } | 63 } |
62 | 64 |
63 void TestUnzipFile(const base::FilePath::StringType& filename, | 65 void TestUnzipFile(const base::FilePath::StringType& filename, |
64 bool expect_hidden_files) { | 66 bool expect_hidden_files) { |
65 base::FilePath test_dir; | 67 base::FilePath test_dir; |
66 ASSERT_TRUE(GetTestDataDirectory(&test_dir)); | 68 ASSERT_TRUE(GetTestDataDirectory(&test_dir)); |
67 test_dir = test_dir.AppendASCII("zip"); | |
68 TestUnzipFile(test_dir.Append(filename), expect_hidden_files); | 69 TestUnzipFile(test_dir.Append(filename), expect_hidden_files); |
69 } | 70 } |
70 | 71 |
71 void TestUnzipFile(const base::FilePath& path, bool expect_hidden_files) { | 72 void TestUnzipFile(const base::FilePath& path, bool expect_hidden_files) { |
72 ASSERT_TRUE(file_util::PathExists(path)) << "no file " << path.value(); | 73 ASSERT_TRUE(file_util::PathExists(path)) << "no file " << path.value(); |
73 ASSERT_TRUE(zip::Unzip(path, test_dir_)); | 74 ASSERT_TRUE(zip::Unzip(path, test_dir_)); |
74 | 75 |
75 file_util::FileEnumerator files(test_dir_, true, | 76 file_util::FileEnumerator files(test_dir_, true, |
76 file_util::FileEnumerator::FILES | | 77 file_util::FileEnumerator::FILES | |
77 file_util::FileEnumerator::DIRECTORIES); | 78 file_util::FileEnumerator::DIRECTORIES); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 TestUnzipFile(FILE_PATH_LITERAL("test.zip"), true); | 114 TestUnzipFile(FILE_PATH_LITERAL("test.zip"), true); |
114 } | 115 } |
115 | 116 |
116 TEST_F(ZipTest, UnzipUncompressed) { | 117 TEST_F(ZipTest, UnzipUncompressed) { |
117 TestUnzipFile(FILE_PATH_LITERAL("test_nocompress.zip"), true); | 118 TestUnzipFile(FILE_PATH_LITERAL("test_nocompress.zip"), true); |
118 } | 119 } |
119 | 120 |
120 TEST_F(ZipTest, UnzipEvil) { | 121 TEST_F(ZipTest, UnzipEvil) { |
121 base::FilePath path; | 122 base::FilePath path; |
122 ASSERT_TRUE(GetTestDataDirectory(&path)); | 123 ASSERT_TRUE(GetTestDataDirectory(&path)); |
123 path = path.AppendASCII("zip").AppendASCII("evil.zip"); | 124 path = path.AppendASCII("evil.zip"); |
124 // Unzip the zip file into a sub directory of test_dir_ so evil.zip | 125 // Unzip the zip file into a sub directory of test_dir_ so evil.zip |
125 // won't create a persistent file outside test_dir_ in case of a | 126 // won't create a persistent file outside test_dir_ in case of a |
126 // failure. | 127 // failure. |
127 base::FilePath output_dir = test_dir_.AppendASCII("out"); | 128 base::FilePath output_dir = test_dir_.AppendASCII("out"); |
128 ASSERT_FALSE(zip::Unzip(path, output_dir)); | 129 ASSERT_FALSE(zip::Unzip(path, output_dir)); |
129 base::FilePath evil_file = output_dir; | 130 base::FilePath evil_file = output_dir; |
130 evil_file = evil_file.AppendASCII( | 131 evil_file = evil_file.AppendASCII( |
131 "../levilevilevilevilevilevilevilevilevilevilevilevil"); | 132 "../levilevilevilevilevilevilevilevilevilevilevilevil"); |
132 ASSERT_FALSE(file_util::PathExists(evil_file)); | 133 ASSERT_FALSE(file_util::PathExists(evil_file)); |
133 } | 134 } |
134 | 135 |
135 TEST_F(ZipTest, UnzipEvil2) { | 136 TEST_F(ZipTest, UnzipEvil2) { |
136 base::FilePath path; | 137 base::FilePath path; |
137 ASSERT_TRUE(GetTestDataDirectory(&path)); | 138 ASSERT_TRUE(GetTestDataDirectory(&path)); |
138 // The zip file contains an evil file with invalid UTF-8 in its file | 139 // The zip file contains an evil file with invalid UTF-8 in its file |
139 // name. | 140 // name. |
140 path = path.AppendASCII("zip").AppendASCII("evil_via_invalid_utf8.zip"); | 141 path = path.AppendASCII("evil_via_invalid_utf8.zip"); |
141 // See the comment at UnzipEvil() for why we do this. | 142 // See the comment at UnzipEvil() for why we do this. |
142 base::FilePath output_dir = test_dir_.AppendASCII("out"); | 143 base::FilePath output_dir = test_dir_.AppendASCII("out"); |
143 // This should fail as it contains an evil file. | 144 // This should fail as it contains an evil file. |
144 ASSERT_FALSE(zip::Unzip(path, output_dir)); | 145 ASSERT_FALSE(zip::Unzip(path, output_dir)); |
145 base::FilePath evil_file = output_dir; | 146 base::FilePath evil_file = output_dir; |
146 evil_file = evil_file.AppendASCII("../evil.txt"); | 147 evil_file = evil_file.AppendASCII("../evil.txt"); |
147 ASSERT_FALSE(file_util::PathExists(evil_file)); | 148 ASSERT_FALSE(file_util::PathExists(evil_file)); |
148 } | 149 } |
149 | 150 |
150 TEST_F(ZipTest, Zip) { | 151 TEST_F(ZipTest, Zip) { |
151 base::FilePath src_dir; | 152 base::FilePath src_dir; |
152 ASSERT_TRUE(GetTestDataDirectory(&src_dir)); | 153 ASSERT_TRUE(GetTestDataDirectory(&src_dir)); |
153 src_dir = src_dir.AppendASCII("zip").AppendASCII("test"); | 154 src_dir = src_dir.AppendASCII("test"); |
154 | 155 |
155 base::ScopedTempDir temp_dir; | 156 base::ScopedTempDir temp_dir; |
156 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 157 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
157 base::FilePath zip_file = temp_dir.path().AppendASCII("out.zip"); | 158 base::FilePath zip_file = temp_dir.path().AppendASCII("out.zip"); |
158 | 159 |
159 EXPECT_TRUE(zip::Zip(src_dir, zip_file, true)); | 160 EXPECT_TRUE(zip::Zip(src_dir, zip_file, true)); |
160 TestUnzipFile(zip_file, true); | 161 TestUnzipFile(zip_file, true); |
161 } | 162 } |
162 | 163 |
163 TEST_F(ZipTest, ZipIgnoreHidden) { | 164 TEST_F(ZipTest, ZipIgnoreHidden) { |
164 base::FilePath src_dir; | 165 base::FilePath src_dir; |
165 ASSERT_TRUE(GetTestDataDirectory(&src_dir)); | 166 ASSERT_TRUE(GetTestDataDirectory(&src_dir)); |
166 src_dir = src_dir.AppendASCII("zip").AppendASCII("test"); | 167 src_dir = src_dir.AppendASCII("test"); |
167 | 168 |
168 base::ScopedTempDir temp_dir; | 169 base::ScopedTempDir temp_dir; |
169 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 170 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
170 base::FilePath zip_file = temp_dir.path().AppendASCII("out.zip"); | 171 base::FilePath zip_file = temp_dir.path().AppendASCII("out.zip"); |
171 | 172 |
172 EXPECT_TRUE(zip::Zip(src_dir, zip_file, false)); | 173 EXPECT_TRUE(zip::Zip(src_dir, zip_file, false)); |
173 TestUnzipFile(zip_file, false); | 174 TestUnzipFile(zip_file, false); |
174 } | 175 } |
175 | 176 |
176 #if defined(OS_POSIX) | 177 #if defined(OS_POSIX) |
177 TEST_F(ZipTest, ZipFiles) { | 178 TEST_F(ZipTest, ZipFiles) { |
178 base::FilePath src_dir; | 179 base::FilePath src_dir; |
179 ASSERT_TRUE(GetTestDataDirectory(&src_dir)); | 180 ASSERT_TRUE(GetTestDataDirectory(&src_dir)); |
180 src_dir = src_dir.AppendASCII("zip").AppendASCII("test"); | 181 src_dir = src_dir.AppendASCII("test"); |
181 | 182 |
182 base::ScopedTempDir temp_dir; | 183 base::ScopedTempDir temp_dir; |
183 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 184 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
184 base::FilePath zip_file = temp_dir.path().AppendASCII("out.zip"); | 185 base::FilePath zip_file = temp_dir.path().AppendASCII("out.zip"); |
185 | 186 |
186 const int flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE; | 187 const int flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE; |
187 const base::PlatformFile zip_fd = | 188 const base::PlatformFile zip_fd = |
188 base::CreatePlatformFile(zip_file, flags, NULL, NULL); | 189 base::CreatePlatformFile(zip_file, flags, NULL, NULL); |
189 ASSERT_LE(0, zip_fd); | 190 ASSERT_LE(0, zip_fd); |
190 EXPECT_TRUE(zip::ZipFiles(src_dir, zip_file_list_, zip_fd)); | 191 EXPECT_TRUE(zip::ZipFiles(src_dir, zip_file_list_, zip_fd)); |
191 base::ClosePlatformFile(zip_fd); | 192 base::ClosePlatformFile(zip_fd); |
192 | 193 |
193 zip::ZipReader reader; | 194 zip::ZipReader reader; |
194 EXPECT_TRUE(reader.Open(zip_file)); | 195 EXPECT_TRUE(reader.Open(zip_file)); |
195 EXPECT_EQ(zip_file_list_.size(), static_cast<size_t>(reader.num_entries())); | 196 EXPECT_EQ(zip_file_list_.size(), static_cast<size_t>(reader.num_entries())); |
196 for (size_t i = 0; i < zip_file_list_.size(); ++i) { | 197 for (size_t i = 0; i < zip_file_list_.size(); ++i) { |
197 EXPECT_TRUE(reader.LocateAndOpenEntry(zip_file_list_[i])); | 198 EXPECT_TRUE(reader.LocateAndOpenEntry(zip_file_list_[i])); |
198 // Check the path in the entry just in case. | 199 // Check the path in the entry just in case. |
199 const zip::ZipReader::EntryInfo* entry_info = reader.current_entry_info(); | 200 const zip::ZipReader::EntryInfo* entry_info = reader.current_entry_info(); |
200 EXPECT_EQ(entry_info->file_path(), zip_file_list_[i]); | 201 EXPECT_EQ(entry_info->file_path(), zip_file_list_[i]); |
201 } | 202 } |
202 } | 203 } |
203 #endif // defined(OS_POSIX) | 204 #endif // defined(OS_POSIX) |
204 | 205 |
205 } // namespace | 206 } // namespace |
206 | 207 |
OLD | NEW |