| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/scoped_temp_dir.h" | 8 #include "base/scoped_temp_dir.h" |
| 9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 zip_contents_.insert(zip_path.AppendASCII("baz.txt")); | 34 zip_contents_.insert(zip_path.AppendASCII("baz.txt")); |
| 35 zip_contents_.insert(zip_path.AppendASCII("quux.txt")); | 35 zip_contents_.insert(zip_path.AppendASCII("quux.txt")); |
| 36 zip_contents_.insert(zip_path.AppendASCII(".hidden")); | 36 zip_contents_.insert(zip_path.AppendASCII(".hidden")); |
| 37 } | 37 } |
| 38 | 38 |
| 39 virtual void TearDown() { | 39 virtual void TearDown() { |
| 40 PlatformTest::TearDown(); | 40 PlatformTest::TearDown(); |
| 41 } | 41 } |
| 42 | 42 |
| 43 void TestUnzipFile(const FilePath::StringType& filename, | 43 void TestUnzipFile(const FilePath::StringType& filename, |
| 44 bool expect_hidden_files, bool need_success) { | 44 bool expect_hidden_files) { |
| 45 FilePath test_dir; | 45 FilePath test_dir; |
| 46 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir)); | 46 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir)); |
| 47 test_dir = test_dir.AppendASCII("zip"); | 47 test_dir = test_dir.AppendASCII("zip"); |
| 48 TestUnzipFile(test_dir.Append(filename), expect_hidden_files, | 48 TestUnzipFile(test_dir.Append(filename), expect_hidden_files); |
| 49 need_success); | |
| 50 } | 49 } |
| 51 | 50 |
| 52 void TestUnzipFile(const FilePath& path, bool expect_hidden_files, | 51 void TestUnzipFile(const FilePath& path, bool expect_hidden_files) { |
| 53 bool need_success) { | |
| 54 ASSERT_TRUE(file_util::PathExists(path)) << "no file " << path.value(); | 52 ASSERT_TRUE(file_util::PathExists(path)) << "no file " << path.value(); |
| 55 if (need_success) { | 53 ASSERT_TRUE(Unzip(path, test_dir_)); |
| 56 ASSERT_TRUE(Unzip(path, test_dir_)); | |
| 57 } else { | |
| 58 ASSERT_FALSE(Unzip(path, test_dir_)); | |
| 59 return; | |
| 60 } | |
| 61 | 54 |
| 62 file_util::FileEnumerator files(test_dir_, true, | 55 file_util::FileEnumerator files(test_dir_, true, |
| 63 static_cast<file_util::FileEnumerator::FILE_TYPE>( | 56 static_cast<file_util::FileEnumerator::FILE_TYPE>( |
| 64 file_util::FileEnumerator::FILES | | 57 file_util::FileEnumerator::FILES | |
| 65 file_util::FileEnumerator::DIRECTORIES)); | 58 file_util::FileEnumerator::DIRECTORIES)); |
| 66 FilePath next_path = files.Next(); | 59 FilePath next_path = files.Next(); |
| 67 size_t count = 0; | 60 size_t count = 0; |
| 68 while (!next_path.value().empty()) { | 61 while (!next_path.value().empty()) { |
| 69 if (next_path.value().find(FILE_PATH_LITERAL(".svn")) == | 62 if (next_path.value().find(FILE_PATH_LITERAL(".svn")) == |
| 70 FilePath::StringType::npos) { | 63 FilePath::StringType::npos) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 88 // the path to temporary directory used to contain the test operations | 81 // the path to temporary directory used to contain the test operations |
| 89 FilePath test_dir_; | 82 FilePath test_dir_; |
| 90 | 83 |
| 91 ScopedTempDir temp_dir_; | 84 ScopedTempDir temp_dir_; |
| 92 | 85 |
| 93 // hard-coded contents of a known zip file | 86 // hard-coded contents of a known zip file |
| 94 std::set<FilePath> zip_contents_; | 87 std::set<FilePath> zip_contents_; |
| 95 }; | 88 }; |
| 96 | 89 |
| 97 TEST_F(ZipTest, Unzip) { | 90 TEST_F(ZipTest, Unzip) { |
| 98 TestUnzipFile(FILE_PATH_LITERAL("test.zip"), true, true); | 91 TestUnzipFile(FILE_PATH_LITERAL("test.zip"), true); |
| 99 } | 92 } |
| 100 | 93 |
| 101 TEST_F(ZipTest, UnzipUncompressed) { | 94 TEST_F(ZipTest, UnzipUncompressed) { |
| 102 TestUnzipFile(FILE_PATH_LITERAL("test_nocompress.zip"), true, true); | 95 TestUnzipFile(FILE_PATH_LITERAL("test_nocompress.zip"), true); |
| 103 } | 96 } |
| 104 | 97 |
| 105 TEST_F(ZipTest, UnzipEvil) { | 98 TEST_F(ZipTest, UnzipEvil) { |
| 106 TestUnzipFile(FILE_PATH_LITERAL("evil.zip"), true, false); | 99 FilePath path; |
| 100 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &path)); |
| 101 path = path.AppendASCII("zip").AppendASCII("evil.zip"); |
| 102 ASSERT_FALSE(Unzip(path, test_dir_)); |
| 107 FilePath evil_file = test_dir_; | 103 FilePath evil_file = test_dir_; |
| 108 evil_file = evil_file.AppendASCII( | 104 evil_file = evil_file.AppendASCII( |
| 109 "../levilevilevilevilevilevilevilevilevilevilevilevil"); | 105 "../levilevilevilevilevilevilevilevilevilevilevilevil"); |
| 110 ASSERT_FALSE(file_util::PathExists(evil_file)); | 106 ASSERT_FALSE(file_util::PathExists(evil_file)); |
| 111 } | 107 } |
| 112 | 108 |
| 113 TEST_F(ZipTest, UnzipEvil2) { | 109 TEST_F(ZipTest, UnzipEvil2) { |
| 114 ScopedTempDir dest_dir; | 110 FilePath path; |
| 115 ASSERT_TRUE(dest_dir.CreateUniqueTempDir()); | 111 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &path)); |
| 116 | 112 path = path.AppendASCII("zip").AppendASCII("evil_via_invalid_utf8.zip"); |
| 117 FilePath test_dir; | 113 ASSERT_TRUE(Unzip(path, test_dir_)); |
| 118 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir)); | 114 FilePath evil_file = test_dir_; |
| 119 test_dir = test_dir.AppendASCII("zip"); | |
| 120 TestUnzipFile(FILE_PATH_LITERAL("evil_via_invalid_utf8.zip"), true, false); | |
| 121 | |
| 122 FilePath evil_file = dest_dir.path(); | |
| 123 evil_file = evil_file.AppendASCII("../evil.txt"); | 115 evil_file = evil_file.AppendASCII("../evil.txt"); |
| 124 ASSERT_FALSE(file_util::PathExists(evil_file)); | 116 ASSERT_FALSE(file_util::PathExists(evil_file)); |
| 125 } | 117 } |
| 126 | 118 |
| 127 TEST_F(ZipTest, Zip) { | 119 TEST_F(ZipTest, Zip) { |
| 128 FilePath src_dir; | 120 FilePath src_dir; |
| 129 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &src_dir)); | 121 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &src_dir)); |
| 130 src_dir = src_dir.AppendASCII("zip").AppendASCII("test"); | 122 src_dir = src_dir.AppendASCII("zip").AppendASCII("test"); |
| 131 | 123 |
| 132 ScopedTempDir temp_dir; | 124 ScopedTempDir temp_dir; |
| 133 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 125 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 134 FilePath zip_file = temp_dir.path().AppendASCII("out.zip"); | 126 FilePath zip_file = temp_dir.path().AppendASCII("out.zip"); |
| 135 | 127 |
| 136 EXPECT_TRUE(Zip(src_dir, zip_file, true)); | 128 EXPECT_TRUE(Zip(src_dir, zip_file, true)); |
| 137 TestUnzipFile(zip_file, true, true); | 129 TestUnzipFile(zip_file, true); |
| 138 } | 130 } |
| 139 | 131 |
| 140 TEST_F(ZipTest, ZipIgnoreHidden) { | 132 TEST_F(ZipTest, ZipIgnoreHidden) { |
| 141 FilePath src_dir; | 133 FilePath src_dir; |
| 142 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &src_dir)); | 134 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &src_dir)); |
| 143 src_dir = src_dir.AppendASCII("zip").AppendASCII("test"); | 135 src_dir = src_dir.AppendASCII("zip").AppendASCII("test"); |
| 144 | 136 |
| 145 ScopedTempDir temp_dir; | 137 ScopedTempDir temp_dir; |
| 146 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 138 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 147 FilePath zip_file = temp_dir.path().AppendASCII("out.zip"); | 139 FilePath zip_file = temp_dir.path().AppendASCII("out.zip"); |
| 148 | 140 |
| 149 EXPECT_TRUE(Zip(src_dir, zip_file, false)); | 141 EXPECT_TRUE(Zip(src_dir, zip_file, false)); |
| 150 TestUnzipFile(zip_file, false, true); | 142 TestUnzipFile(zip_file, false); |
| 151 } | 143 } |
| 152 | 144 |
| 153 } // namespace | 145 } // namespace |
| OLD | NEW |