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/scoped_temp_dir.h" |
9 #include "base/path_service.h" | 10 #include "base/path_service.h" |
10 #include "base/scoped_temp_dir.h" | |
11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
12 #include "chrome/common/chrome_paths.h" | 12 #include "chrome/common/chrome_paths.h" |
13 #include "chrome/common/zip.h" | 13 #include "chrome/common/zip.h" |
14 #include "chrome/common/zip_reader.h" | 14 #include "chrome/common/zip_reader.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 #include "testing/platform_test.h" | 16 #include "testing/platform_test.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. |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 if (expect_hidden_files || iter->BaseName().value()[0] != '.') | 80 if (expect_hidden_files || iter->BaseName().value()[0] != '.') |
81 ++expected_count; | 81 ++expected_count; |
82 } | 82 } |
83 | 83 |
84 EXPECT_EQ(expected_count, count); | 84 EXPECT_EQ(expected_count, count); |
85 } | 85 } |
86 | 86 |
87 // The path to temporary directory used to contain the test operations. | 87 // The path to temporary directory used to contain the test operations. |
88 FilePath test_dir_; | 88 FilePath test_dir_; |
89 | 89 |
90 ScopedTempDir temp_dir_; | 90 base::ScopedTempDir temp_dir_; |
91 | 91 |
92 // Hard-coded contents of a known zip file. | 92 // Hard-coded contents of a known zip file. |
93 std::set<FilePath> zip_contents_; | 93 std::set<FilePath> zip_contents_; |
94 | 94 |
95 // Hard-coded list of relative paths for a zip file created with ZipFiles. | 95 // Hard-coded list of relative paths for a zip file created with ZipFiles. |
96 std::vector<FilePath> zip_file_list_; | 96 std::vector<FilePath> zip_file_list_; |
97 }; | 97 }; |
98 | 98 |
99 TEST_F(ZipTest, Unzip) { | 99 TEST_F(ZipTest, Unzip) { |
100 TestUnzipFile(FILE_PATH_LITERAL("test.zip"), true); | 100 TestUnzipFile(FILE_PATH_LITERAL("test.zip"), true); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 FilePath evil_file = output_dir; | 132 FilePath evil_file = output_dir; |
133 evil_file = evil_file.AppendASCII("../evil.txt"); | 133 evil_file = evil_file.AppendASCII("../evil.txt"); |
134 ASSERT_FALSE(file_util::PathExists(evil_file)); | 134 ASSERT_FALSE(file_util::PathExists(evil_file)); |
135 } | 135 } |
136 | 136 |
137 TEST_F(ZipTest, Zip) { | 137 TEST_F(ZipTest, Zip) { |
138 FilePath src_dir; | 138 FilePath src_dir; |
139 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &src_dir)); | 139 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &src_dir)); |
140 src_dir = src_dir.AppendASCII("zip").AppendASCII("test"); | 140 src_dir = src_dir.AppendASCII("zip").AppendASCII("test"); |
141 | 141 |
142 ScopedTempDir temp_dir; | 142 base::ScopedTempDir temp_dir; |
143 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 143 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
144 FilePath zip_file = temp_dir.path().AppendASCII("out.zip"); | 144 FilePath zip_file = temp_dir.path().AppendASCII("out.zip"); |
145 | 145 |
146 EXPECT_TRUE(zip::Zip(src_dir, zip_file, true)); | 146 EXPECT_TRUE(zip::Zip(src_dir, zip_file, true)); |
147 TestUnzipFile(zip_file, true); | 147 TestUnzipFile(zip_file, true); |
148 } | 148 } |
149 | 149 |
150 TEST_F(ZipTest, ZipIgnoreHidden) { | 150 TEST_F(ZipTest, ZipIgnoreHidden) { |
151 FilePath src_dir; | 151 FilePath src_dir; |
152 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &src_dir)); | 152 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &src_dir)); |
153 src_dir = src_dir.AppendASCII("zip").AppendASCII("test"); | 153 src_dir = src_dir.AppendASCII("zip").AppendASCII("test"); |
154 | 154 |
155 ScopedTempDir temp_dir; | 155 base::ScopedTempDir temp_dir; |
156 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 156 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
157 FilePath zip_file = temp_dir.path().AppendASCII("out.zip"); | 157 FilePath zip_file = temp_dir.path().AppendASCII("out.zip"); |
158 | 158 |
159 EXPECT_TRUE(zip::Zip(src_dir, zip_file, false)); | 159 EXPECT_TRUE(zip::Zip(src_dir, zip_file, false)); |
160 TestUnzipFile(zip_file, false); | 160 TestUnzipFile(zip_file, false); |
161 } | 161 } |
162 | 162 |
163 TEST_F(ZipTest, ZipFiles) { | 163 TEST_F(ZipTest, ZipFiles) { |
164 FilePath src_dir; | 164 FilePath src_dir; |
165 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &src_dir)); | 165 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &src_dir)); |
166 src_dir = src_dir.AppendASCII("zip").AppendASCII("test"); | 166 src_dir = src_dir.AppendASCII("zip").AppendASCII("test"); |
167 | 167 |
168 ScopedTempDir temp_dir; | 168 base::ScopedTempDir temp_dir; |
169 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 169 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
170 FilePath zip_file = temp_dir.path().AppendASCII("out.zip"); | 170 FilePath zip_file = temp_dir.path().AppendASCII("out.zip"); |
171 | 171 |
172 EXPECT_TRUE(zip::ZipFiles(src_dir, zip_file_list_, zip_file)); | 172 EXPECT_TRUE(zip::ZipFiles(src_dir, zip_file_list_, zip_file)); |
173 | 173 |
174 zip::ZipReader reader; | 174 zip::ZipReader reader; |
175 EXPECT_TRUE(reader.Open(zip_file)); | 175 EXPECT_TRUE(reader.Open(zip_file)); |
176 EXPECT_EQ(zip_file_list_.size(), static_cast<size_t>(reader.num_entries())); | 176 EXPECT_EQ(zip_file_list_.size(), static_cast<size_t>(reader.num_entries())); |
177 for (size_t i = 0; i < zip_file_list_.size(); ++i) { | 177 for (size_t i = 0; i < zip_file_list_.size(); ++i) { |
178 EXPECT_TRUE(reader.LocateAndOpenEntry(zip_file_list_[i])); | 178 EXPECT_TRUE(reader.LocateAndOpenEntry(zip_file_list_[i])); |
179 // Check the path in the entry just in case. | 179 // Check the path in the entry just in case. |
180 const zip::ZipReader::EntryInfo* entry_info = reader.current_entry_info(); | 180 const zip::ZipReader::EntryInfo* entry_info = reader.current_entry_info(); |
181 EXPECT_EQ(entry_info->file_path(), zip_file_list_[i]); | 181 EXPECT_EQ(entry_info->file_path(), zip_file_list_[i]); |
182 } | 182 } |
183 } | 183 } |
184 | 184 |
185 } // namespace | 185 } // namespace |
186 | 186 |
OLD | NEW |