| 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/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/path_service.h" | 11 #include "base/path_service.h" |
| 11 #include "base/string_util.h" | 12 #include "base/string_util.h" |
| 12 #include "chrome/common/chrome_paths.h" | 13 #include "components/zip/zip.h" |
| 13 #include "chrome/common/zip.h" | 14 #include "components/zip/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. |
| 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(); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 42 zip_file_list_.push_back( | 42 zip_file_list_.push_back( |
| 43 base::FilePath(FILE_PATH_LITERAL("foo/bar/quux.txt"))); | 43 base::FilePath(FILE_PATH_LITERAL("foo/bar/quux.txt"))); |
| 44 zip_file_list_.push_back( | 44 zip_file_list_.push_back( |
| 45 base::FilePath(FILE_PATH_LITERAL("foo/bar/.hidden"))); | 45 base::FilePath(FILE_PATH_LITERAL("foo/bar/.hidden"))); |
| 46 } | 46 } |
| 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) { |
| 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 |
| 52 void TestUnzipFile(const base::FilePath::StringType& filename, | 63 void TestUnzipFile(const base::FilePath::StringType& filename, |
| 53 bool expect_hidden_files) { | 64 bool expect_hidden_files) { |
| 54 base::FilePath test_dir; | 65 base::FilePath test_dir; |
| 55 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir)); | 66 ASSERT_TRUE(GetTestDataDirectory(&test_dir)); |
| 56 test_dir = test_dir.AppendASCII("zip"); | 67 test_dir = test_dir.AppendASCII("zip"); |
| 57 TestUnzipFile(test_dir.Append(filename), expect_hidden_files); | 68 TestUnzipFile(test_dir.Append(filename), expect_hidden_files); |
| 58 } | 69 } |
| 59 | 70 |
| 60 void TestUnzipFile(const base::FilePath& path, bool expect_hidden_files) { | 71 void TestUnzipFile(const base::FilePath& path, bool expect_hidden_files) { |
| 61 ASSERT_TRUE(file_util::PathExists(path)) << "no file " << path.value(); | 72 ASSERT_TRUE(file_util::PathExists(path)) << "no file " << path.value(); |
| 62 ASSERT_TRUE(zip::Unzip(path, test_dir_)); | 73 ASSERT_TRUE(zip::Unzip(path, test_dir_)); |
| 63 | 74 |
| 64 file_util::FileEnumerator files(test_dir_, true, | 75 file_util::FileEnumerator files(test_dir_, true, |
| 65 file_util::FileEnumerator::FILES | | 76 file_util::FileEnumerator::FILES | |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 TEST_F(ZipTest, Unzip) { | 112 TEST_F(ZipTest, Unzip) { |
| 102 TestUnzipFile(FILE_PATH_LITERAL("test.zip"), true); | 113 TestUnzipFile(FILE_PATH_LITERAL("test.zip"), true); |
| 103 } | 114 } |
| 104 | 115 |
| 105 TEST_F(ZipTest, UnzipUncompressed) { | 116 TEST_F(ZipTest, UnzipUncompressed) { |
| 106 TestUnzipFile(FILE_PATH_LITERAL("test_nocompress.zip"), true); | 117 TestUnzipFile(FILE_PATH_LITERAL("test_nocompress.zip"), true); |
| 107 } | 118 } |
| 108 | 119 |
| 109 TEST_F(ZipTest, UnzipEvil) { | 120 TEST_F(ZipTest, UnzipEvil) { |
| 110 base::FilePath path; | 121 base::FilePath path; |
| 111 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &path)); | 122 ASSERT_TRUE(GetTestDataDirectory(&path)); |
| 112 path = path.AppendASCII("zip").AppendASCII("evil.zip"); | 123 path = path.AppendASCII("zip").AppendASCII("evil.zip"); |
| 113 // Unzip the zip file into a sub directory of test_dir_ so evil.zip | 124 // 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 | 125 // won't create a persistent file outside test_dir_ in case of a |
| 115 // failure. | 126 // failure. |
| 116 base::FilePath output_dir = test_dir_.AppendASCII("out"); | 127 base::FilePath output_dir = test_dir_.AppendASCII("out"); |
| 117 ASSERT_FALSE(zip::Unzip(path, output_dir)); | 128 ASSERT_FALSE(zip::Unzip(path, output_dir)); |
| 118 base::FilePath evil_file = output_dir; | 129 base::FilePath evil_file = output_dir; |
| 119 evil_file = evil_file.AppendASCII( | 130 evil_file = evil_file.AppendASCII( |
| 120 "../levilevilevilevilevilevilevilevilevilevilevilevil"); | 131 "../levilevilevilevilevilevilevilevilevilevilevilevil"); |
| 121 ASSERT_FALSE(file_util::PathExists(evil_file)); | 132 ASSERT_FALSE(file_util::PathExists(evil_file)); |
| 122 } | 133 } |
| 123 | 134 |
| 124 TEST_F(ZipTest, UnzipEvil2) { | 135 TEST_F(ZipTest, UnzipEvil2) { |
| 125 base::FilePath path; | 136 base::FilePath path; |
| 126 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &path)); | 137 ASSERT_TRUE(GetTestDataDirectory(&path)); |
| 127 // The zip file contains an evil file with invalid UTF-8 in its file | 138 // The zip file contains an evil file with invalid UTF-8 in its file |
| 128 // name. | 139 // name. |
| 129 path = path.AppendASCII("zip").AppendASCII("evil_via_invalid_utf8.zip"); | 140 path = path.AppendASCII("zip").AppendASCII("evil_via_invalid_utf8.zip"); |
| 130 // See the comment at UnzipEvil() for why we do this. | 141 // See the comment at UnzipEvil() for why we do this. |
| 131 base::FilePath output_dir = test_dir_.AppendASCII("out"); | 142 base::FilePath output_dir = test_dir_.AppendASCII("out"); |
| 132 // This should fail as it contains an evil file. | 143 // This should fail as it contains an evil file. |
| 133 ASSERT_FALSE(zip::Unzip(path, output_dir)); | 144 ASSERT_FALSE(zip::Unzip(path, output_dir)); |
| 134 base::FilePath evil_file = output_dir; | 145 base::FilePath evil_file = output_dir; |
| 135 evil_file = evil_file.AppendASCII("../evil.txt"); | 146 evil_file = evil_file.AppendASCII("../evil.txt"); |
| 136 ASSERT_FALSE(file_util::PathExists(evil_file)); | 147 ASSERT_FALSE(file_util::PathExists(evil_file)); |
| 137 } | 148 } |
| 138 | 149 |
| 139 TEST_F(ZipTest, Zip) { | 150 TEST_F(ZipTest, Zip) { |
| 140 base::FilePath src_dir; | 151 base::FilePath src_dir; |
| 141 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &src_dir)); | 152 ASSERT_TRUE(GetTestDataDirectory(&src_dir)); |
| 142 src_dir = src_dir.AppendASCII("zip").AppendASCII("test"); | 153 src_dir = src_dir.AppendASCII("zip").AppendASCII("test"); |
| 143 | 154 |
| 144 base::ScopedTempDir temp_dir; | 155 base::ScopedTempDir temp_dir; |
| 145 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 156 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 146 base::FilePath zip_file = temp_dir.path().AppendASCII("out.zip"); | 157 base::FilePath zip_file = temp_dir.path().AppendASCII("out.zip"); |
| 147 | 158 |
| 148 EXPECT_TRUE(zip::Zip(src_dir, zip_file, true)); | 159 EXPECT_TRUE(zip::Zip(src_dir, zip_file, true)); |
| 149 TestUnzipFile(zip_file, true); | 160 TestUnzipFile(zip_file, true); |
| 150 } | 161 } |
| 151 | 162 |
| 152 TEST_F(ZipTest, ZipIgnoreHidden) { | 163 TEST_F(ZipTest, ZipIgnoreHidden) { |
| 153 base::FilePath src_dir; | 164 base::FilePath src_dir; |
| 154 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &src_dir)); | 165 ASSERT_TRUE(GetTestDataDirectory(&src_dir)); |
| 155 src_dir = src_dir.AppendASCII("zip").AppendASCII("test"); | 166 src_dir = src_dir.AppendASCII("zip").AppendASCII("test"); |
| 156 | 167 |
| 157 base::ScopedTempDir temp_dir; | 168 base::ScopedTempDir temp_dir; |
| 158 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 169 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 159 base::FilePath zip_file = temp_dir.path().AppendASCII("out.zip"); | 170 base::FilePath zip_file = temp_dir.path().AppendASCII("out.zip"); |
| 160 | 171 |
| 161 EXPECT_TRUE(zip::Zip(src_dir, zip_file, false)); | 172 EXPECT_TRUE(zip::Zip(src_dir, zip_file, false)); |
| 162 TestUnzipFile(zip_file, false); | 173 TestUnzipFile(zip_file, false); |
| 163 } | 174 } |
| 164 | 175 |
| 165 #if defined(OS_POSIX) | 176 #if defined(OS_POSIX) |
| 166 TEST_F(ZipTest, ZipFiles) { | 177 TEST_F(ZipTest, ZipFiles) { |
| 167 base::FilePath src_dir; | 178 base::FilePath src_dir; |
| 168 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &src_dir)); | 179 ASSERT_TRUE(GetTestDataDirectory(&src_dir)); |
| 169 src_dir = src_dir.AppendASCII("zip").AppendASCII("test"); | 180 src_dir = src_dir.AppendASCII("zip").AppendASCII("test"); |
| 170 | 181 |
| 171 base::ScopedTempDir temp_dir; | 182 base::ScopedTempDir temp_dir; |
| 172 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 183 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 173 base::FilePath zip_file = temp_dir.path().AppendASCII("out.zip"); | 184 base::FilePath zip_file = temp_dir.path().AppendASCII("out.zip"); |
| 174 | 185 |
| 175 const int flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE; | 186 const int flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE; |
| 176 const base::PlatformFile zip_fd = | 187 const base::PlatformFile zip_fd = |
| 177 base::CreatePlatformFile(zip_file, flags, NULL, NULL); | 188 base::CreatePlatformFile(zip_file, flags, NULL, NULL); |
| 178 ASSERT_LE(0, zip_fd); | 189 ASSERT_LE(0, zip_fd); |
| 179 EXPECT_TRUE(zip::ZipFiles(src_dir, zip_file_list_, zip_fd)); | 190 EXPECT_TRUE(zip::ZipFiles(src_dir, zip_file_list_, zip_fd)); |
| 180 base::ClosePlatformFile(zip_fd); | 191 base::ClosePlatformFile(zip_fd); |
| 181 | 192 |
| 182 zip::ZipReader reader; | 193 zip::ZipReader reader; |
| 183 EXPECT_TRUE(reader.Open(zip_file)); | 194 EXPECT_TRUE(reader.Open(zip_file)); |
| 184 EXPECT_EQ(zip_file_list_.size(), static_cast<size_t>(reader.num_entries())); | 195 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) { | 196 for (size_t i = 0; i < zip_file_list_.size(); ++i) { |
| 186 EXPECT_TRUE(reader.LocateAndOpenEntry(zip_file_list_[i])); | 197 EXPECT_TRUE(reader.LocateAndOpenEntry(zip_file_list_[i])); |
| 187 // Check the path in the entry just in case. | 198 // Check the path in the entry just in case. |
| 188 const zip::ZipReader::EntryInfo* entry_info = reader.current_entry_info(); | 199 const zip::ZipReader::EntryInfo* entry_info = reader.current_entry_info(); |
| 189 EXPECT_EQ(entry_info->file_path(), zip_file_list_[i]); | 200 EXPECT_EQ(entry_info->file_path(), zip_file_list_[i]); |
| 190 } | 201 } |
| 191 } | 202 } |
| 192 #endif // defined(OS_POSIX) | 203 #endif // defined(OS_POSIX) |
| 193 | 204 |
| 194 } // namespace | 205 } // namespace |
| 195 | 206 |
| OLD | NEW |