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 15 matching lines...) Expand all Loading... |
26 | 26 |
27 FilePath zip_path(test_dir_); | 27 FilePath zip_path(test_dir_); |
28 zip_contents_.insert(zip_path.AppendASCII("foo.txt")); | 28 zip_contents_.insert(zip_path.AppendASCII("foo.txt")); |
29 zip_path = zip_path.AppendASCII("foo"); | 29 zip_path = zip_path.AppendASCII("foo"); |
30 zip_contents_.insert(zip_path); | 30 zip_contents_.insert(zip_path); |
31 zip_contents_.insert(zip_path.AppendASCII("bar.txt")); | 31 zip_contents_.insert(zip_path.AppendASCII("bar.txt")); |
32 zip_path = zip_path.AppendASCII("bar"); | 32 zip_path = zip_path.AppendASCII("bar"); |
33 zip_contents_.insert(zip_path); | 33 zip_contents_.insert(zip_path); |
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 } | 37 } |
37 | 38 |
38 virtual void TearDown() { | 39 virtual void TearDown() { |
39 PlatformTest::TearDown(); | 40 PlatformTest::TearDown(); |
40 } | 41 } |
41 | 42 |
42 void TestUnzipFile(const FilePath::StringType& filename, bool need_success) { | 43 void TestUnzipFile(const FilePath::StringType& filename, |
| 44 bool expect_hidden_files, bool need_success) { |
43 FilePath test_dir; | 45 FilePath test_dir; |
44 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir)); | 46 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir)); |
45 test_dir = test_dir.AppendASCII("zip"); | 47 test_dir = test_dir.AppendASCII("zip"); |
46 TestUnzipFile(test_dir.Append(filename), need_success); | 48 TestUnzipFile(test_dir.Append(filename), expect_hidden_files, |
| 49 need_success); |
47 } | 50 } |
48 | 51 |
49 void TestUnzipFile(const FilePath& path, bool need_success) { | 52 void TestUnzipFile(const FilePath& path, bool expect_hidden_files, |
| 53 bool need_success) { |
50 ASSERT_TRUE(file_util::PathExists(path)) << "no file " << path.value(); | 54 ASSERT_TRUE(file_util::PathExists(path)) << "no file " << path.value(); |
51 if (need_success) { | 55 if (need_success) { |
52 ASSERT_TRUE(Unzip(path, test_dir_)); | 56 ASSERT_TRUE(Unzip(path, test_dir_)); |
53 } else { | 57 } else { |
54 ASSERT_FALSE(Unzip(path, test_dir_)); | 58 ASSERT_FALSE(Unzip(path, test_dir_)); |
55 return; | 59 return; |
56 } | 60 } |
57 | 61 |
58 file_util::FileEnumerator files(test_dir_, true, | 62 file_util::FileEnumerator files(test_dir_, true, |
59 static_cast<file_util::FileEnumerator::FILE_TYPE>( | 63 static_cast<file_util::FileEnumerator::FILE_TYPE>( |
60 file_util::FileEnumerator::FILES | | 64 file_util::FileEnumerator::FILES | |
61 file_util::FileEnumerator::DIRECTORIES)); | 65 file_util::FileEnumerator::DIRECTORIES)); |
62 FilePath next_path = files.Next(); | 66 FilePath next_path = files.Next(); |
63 size_t count = 0; | 67 size_t count = 0; |
64 while (!next_path.value().empty()) { | 68 while (!next_path.value().empty()) { |
65 if (next_path.value().find(FILE_PATH_LITERAL(".svn")) == | 69 if (next_path.value().find(FILE_PATH_LITERAL(".svn")) == |
66 FilePath::StringType::npos) { | 70 FilePath::StringType::npos) { |
67 EXPECT_EQ(zip_contents_.count(next_path), 1U) << | 71 EXPECT_EQ(zip_contents_.count(next_path), 1U) << |
68 "Couldn't find " << next_path.value(); | 72 "Couldn't find " << next_path.value(); |
69 count++; | 73 count++; |
70 } | 74 } |
71 next_path = files.Next(); | 75 next_path = files.Next(); |
72 } | 76 } |
73 EXPECT_EQ(count, zip_contents_.size()); | 77 |
| 78 int expected_count = 0; |
| 79 for (std::set<FilePath>::iterator iter = zip_contents_.begin(); |
| 80 iter != zip_contents_.end(); ++iter) { |
| 81 if (expect_hidden_files || iter->BaseName().ToWStringHack()[0] != L'.') |
| 82 ++expected_count; |
| 83 } |
| 84 |
| 85 EXPECT_EQ(expected_count, count); |
74 } | 86 } |
75 | 87 |
76 // the path to temporary directory used to contain the test operations | 88 // the path to temporary directory used to contain the test operations |
77 FilePath test_dir_; | 89 FilePath test_dir_; |
78 | 90 |
79 ScopedTempDir temp_dir_; | 91 ScopedTempDir temp_dir_; |
80 | 92 |
81 // hard-coded contents of a known zip file | 93 // hard-coded contents of a known zip file |
82 std::set<FilePath> zip_contents_; | 94 std::set<FilePath> zip_contents_; |
83 }; | 95 }; |
84 | 96 |
85 TEST_F(ZipTest, Unzip) { | 97 TEST_F(ZipTest, Unzip) { |
86 TestUnzipFile(FILE_PATH_LITERAL("test.zip"), true); | 98 TestUnzipFile(FILE_PATH_LITERAL("test.zip"), true, true); |
87 } | 99 } |
88 | 100 |
89 TEST_F(ZipTest, UnzipUncompressed) { | 101 TEST_F(ZipTest, UnzipUncompressed) { |
90 TestUnzipFile(FILE_PATH_LITERAL("test_nocompress.zip"), true); | 102 TestUnzipFile(FILE_PATH_LITERAL("test_nocompress.zip"), true, true); |
91 } | 103 } |
92 | 104 |
93 TEST_F(ZipTest, UnzipEvil) { | 105 TEST_F(ZipTest, UnzipEvil) { |
94 TestUnzipFile(FILE_PATH_LITERAL("evil.zip"), false); | 106 TestUnzipFile(FILE_PATH_LITERAL("evil.zip"), true, false); |
95 FilePath evil_file = test_dir_; | 107 FilePath evil_file = test_dir_; |
96 evil_file = evil_file.AppendASCII( | 108 evil_file = evil_file.AppendASCII( |
97 "../levilevilevilevilevilevilevilevilevilevilevilevil"); | 109 "../levilevilevilevilevilevilevilevilevilevilevilevil"); |
98 ASSERT_FALSE(file_util::PathExists(evil_file)); | 110 ASSERT_FALSE(file_util::PathExists(evil_file)); |
99 } | 111 } |
100 | 112 |
101 TEST_F(ZipTest, Zip) { | 113 TEST_F(ZipTest, Zip) { |
102 FilePath src_dir; | 114 FilePath src_dir; |
103 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &src_dir)); | 115 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &src_dir)); |
104 src_dir = src_dir.AppendASCII("zip").AppendASCII("test"); | 116 src_dir = src_dir.AppendASCII("zip").AppendASCII("test"); |
105 | 117 |
106 ScopedTempDir temp_dir; | 118 ScopedTempDir temp_dir; |
107 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 119 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
108 FilePath zip_file = temp_dir.path().AppendASCII("out.zip"); | 120 FilePath zip_file = temp_dir.path().AppendASCII("out.zip"); |
109 | 121 |
110 EXPECT_TRUE(Zip(src_dir, zip_file)); | 122 EXPECT_TRUE(Zip(src_dir, zip_file, true)); |
| 123 TestUnzipFile(zip_file, true, true); |
| 124 } |
111 | 125 |
112 TestUnzipFile(zip_file, true); | 126 TEST_F(ZipTest, ZipIgnoreHidden) { |
| 127 FilePath src_dir; |
| 128 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &src_dir)); |
| 129 src_dir = src_dir.AppendASCII("zip").AppendASCII("test"); |
| 130 |
| 131 ScopedTempDir temp_dir; |
| 132 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 133 FilePath zip_file = temp_dir.path().AppendASCII("out.zip"); |
| 134 |
| 135 EXPECT_TRUE(Zip(src_dir, zip_file, false)); |
| 136 TestUnzipFile(zip_file, false, true); |
113 } | 137 } |
114 | 138 |
115 } // namespace | 139 } // namespace |
OLD | NEW |