| OLD | NEW |
| (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/file_path.h" | |
| 10 #include "base/files/scoped_temp_dir.h" | |
| 11 #include "base/path_service.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" | |
| 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 bool GetTestDataDirectory(base::FilePath* path) { | |
| 53 bool success = PathService::Get(base::DIR_SOURCE_ROOT, path); | |
| 54 EXPECT_TRUE(success); | |
| 55 if (!success) | |
| 56 return false; | |
| 57 *path = path->AppendASCII("components"); | |
| 58 *path = path->AppendASCII("test"); | |
| 59 *path = path->AppendASCII("data"); | |
| 60 return true; | |
| 61 } | |
| 62 | |
| 63 void TestUnzipFile(const base::FilePath::StringType& filename, | |
| 64 bool expect_hidden_files) { | |
| 65 base::FilePath test_dir; | |
| 66 ASSERT_TRUE(GetTestDataDirectory(&test_dir)); | |
| 67 test_dir = test_dir.AppendASCII("zip"); | |
| 68 TestUnzipFile(test_dir.Append(filename), expect_hidden_files); | |
| 69 } | |
| 70 | |
| 71 void TestUnzipFile(const base::FilePath& path, bool expect_hidden_files) { | |
| 72 ASSERT_TRUE(file_util::PathExists(path)) << "no file " << path.value(); | |
| 73 ASSERT_TRUE(zip::Unzip(path, test_dir_)); | |
| 74 | |
| 75 file_util::FileEnumerator files(test_dir_, true, | |
| 76 file_util::FileEnumerator::FILES | | |
| 77 file_util::FileEnumerator::DIRECTORIES); | |
| 78 base::FilePath next_path = files.Next(); | |
| 79 size_t count = 0; | |
| 80 while (!next_path.value().empty()) { | |
| 81 if (next_path.value().find(FILE_PATH_LITERAL(".svn")) == | |
| 82 base::FilePath::StringType::npos) { | |
| 83 EXPECT_EQ(zip_contents_.count(next_path), 1U) << | |
| 84 "Couldn't find " << next_path.value(); | |
| 85 count++; | |
| 86 } | |
| 87 next_path = files.Next(); | |
| 88 } | |
| 89 | |
| 90 size_t expected_count = 0; | |
| 91 for (std::set<base::FilePath>::iterator iter = zip_contents_.begin(); | |
| 92 iter != zip_contents_.end(); ++iter) { | |
| 93 if (expect_hidden_files || iter->BaseName().value()[0] != '.') | |
| 94 ++expected_count; | |
| 95 } | |
| 96 | |
| 97 EXPECT_EQ(expected_count, count); | |
| 98 } | |
| 99 | |
| 100 // The path to temporary directory used to contain the test operations. | |
| 101 base::FilePath test_dir_; | |
| 102 | |
| 103 base::ScopedTempDir temp_dir_; | |
| 104 | |
| 105 // Hard-coded contents of a known zip file. | |
| 106 std::set<base::FilePath> zip_contents_; | |
| 107 | |
| 108 // Hard-coded list of relative paths for a zip file created with ZipFiles. | |
| 109 std::vector<base::FilePath> zip_file_list_; | |
| 110 }; | |
| 111 | |
| 112 TEST_F(ZipTest, Unzip) { | |
| 113 TestUnzipFile(FILE_PATH_LITERAL("test.zip"), true); | |
| 114 } | |
| 115 | |
| 116 TEST_F(ZipTest, UnzipUncompressed) { | |
| 117 TestUnzipFile(FILE_PATH_LITERAL("test_nocompress.zip"), true); | |
| 118 } | |
| 119 | |
| 120 TEST_F(ZipTest, UnzipEvil) { | |
| 121 base::FilePath path; | |
| 122 ASSERT_TRUE(GetTestDataDirectory(&path)); | |
| 123 path = path.AppendASCII("zip").AppendASCII("evil.zip"); | |
| 124 // 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 // failure. | |
| 127 base::FilePath output_dir = test_dir_.AppendASCII("out"); | |
| 128 ASSERT_FALSE(zip::Unzip(path, output_dir)); | |
| 129 base::FilePath evil_file = output_dir; | |
| 130 evil_file = evil_file.AppendASCII( | |
| 131 "../levilevilevilevilevilevilevilevilevilevilevilevil"); | |
| 132 ASSERT_FALSE(file_util::PathExists(evil_file)); | |
| 133 } | |
| 134 | |
| 135 TEST_F(ZipTest, UnzipEvil2) { | |
| 136 base::FilePath path; | |
| 137 ASSERT_TRUE(GetTestDataDirectory(&path)); | |
| 138 // The zip file contains an evil file with invalid UTF-8 in its file | |
| 139 // name. | |
| 140 path = path.AppendASCII("zip").AppendASCII("evil_via_invalid_utf8.zip"); | |
| 141 // See the comment at UnzipEvil() for why we do this. | |
| 142 base::FilePath output_dir = test_dir_.AppendASCII("out"); | |
| 143 // This should fail as it contains an evil file. | |
| 144 ASSERT_FALSE(zip::Unzip(path, output_dir)); | |
| 145 base::FilePath evil_file = output_dir; | |
| 146 evil_file = evil_file.AppendASCII("../evil.txt"); | |
| 147 ASSERT_FALSE(file_util::PathExists(evil_file)); | |
| 148 } | |
| 149 | |
| 150 TEST_F(ZipTest, Zip) { | |
| 151 base::FilePath src_dir; | |
| 152 ASSERT_TRUE(GetTestDataDirectory(&src_dir)); | |
| 153 src_dir = src_dir.AppendASCII("zip").AppendASCII("test"); | |
| 154 | |
| 155 base::ScopedTempDir temp_dir; | |
| 156 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
| 157 base::FilePath zip_file = temp_dir.path().AppendASCII("out.zip"); | |
| 158 | |
| 159 EXPECT_TRUE(zip::Zip(src_dir, zip_file, true)); | |
| 160 TestUnzipFile(zip_file, true); | |
| 161 } | |
| 162 | |
| 163 TEST_F(ZipTest, ZipIgnoreHidden) { | |
| 164 base::FilePath src_dir; | |
| 165 ASSERT_TRUE(GetTestDataDirectory(&src_dir)); | |
| 166 src_dir = src_dir.AppendASCII("zip").AppendASCII("test"); | |
| 167 | |
| 168 base::ScopedTempDir temp_dir; | |
| 169 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
| 170 base::FilePath zip_file = temp_dir.path().AppendASCII("out.zip"); | |
| 171 | |
| 172 EXPECT_TRUE(zip::Zip(src_dir, zip_file, false)); | |
| 173 TestUnzipFile(zip_file, false); | |
| 174 } | |
| 175 | |
| 176 #if defined(OS_POSIX) | |
| 177 TEST_F(ZipTest, ZipFiles) { | |
| 178 base::FilePath src_dir; | |
| 179 ASSERT_TRUE(GetTestDataDirectory(&src_dir)); | |
| 180 src_dir = src_dir.AppendASCII("zip").AppendASCII("test"); | |
| 181 | |
| 182 base::ScopedTempDir temp_dir; | |
| 183 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
| 184 base::FilePath zip_file = temp_dir.path().AppendASCII("out.zip"); | |
| 185 | |
| 186 const int flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE; | |
| 187 const base::PlatformFile zip_fd = | |
| 188 base::CreatePlatformFile(zip_file, flags, NULL, NULL); | |
| 189 ASSERT_LE(0, zip_fd); | |
| 190 EXPECT_TRUE(zip::ZipFiles(src_dir, zip_file_list_, zip_fd)); | |
| 191 base::ClosePlatformFile(zip_fd); | |
| 192 | |
| 193 zip::ZipReader reader; | |
| 194 EXPECT_TRUE(reader.Open(zip_file)); | |
| 195 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 EXPECT_TRUE(reader.LocateAndOpenEntry(zip_file_list_[i])); | |
| 198 // Check the path in the entry just in case. | |
| 199 const zip::ZipReader::EntryInfo* entry_info = reader.current_entry_info(); | |
| 200 EXPECT_EQ(entry_info->file_path(), zip_file_list_[i]); | |
| 201 } | |
| 202 } | |
| 203 #endif // defined(OS_POSIX) | |
| 204 | |
| 205 } // namespace | |
| 206 | |
| OLD | NEW |