OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include <set> | |
6 | |
7 #include "base/file_path.h" | |
8 #include "base/file_util.h" | |
9 #include "base/path_service.h" | |
10 #include "base/string_util.h" | |
11 #include "chrome/common/chrome_paths.h" | |
12 #include "chrome/common/unzip.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 #include "testing/platform_test.h" | |
15 | |
16 namespace { | |
17 | |
18 // Make the test a PlatformTest to setup autorelease pools properly on Mac. | |
19 class UnzipTest : public PlatformTest { | |
20 protected: | |
21 virtual void SetUp() { | |
22 PlatformTest::SetUp(); | |
23 | |
24 ASSERT_TRUE(file_util::CreateNewTempDirectory( | |
25 FILE_PATH_LITERAL("unzip_unittest_"), &test_dir_)); | |
26 | |
27 FilePath zip_path(test_dir_.AppendASCII("test")); | |
28 zip_contents_.insert(zip_path); | |
29 zip_contents_.insert(zip_path.AppendASCII("foo.txt")); | |
30 zip_path = zip_path.AppendASCII("foo"); | |
31 zip_contents_.insert(zip_path); | |
32 zip_contents_.insert(zip_path.AppendASCII("bar.txt")); | |
33 zip_path = zip_path.AppendASCII("bar"); | |
34 zip_contents_.insert(zip_path); | |
35 zip_contents_.insert(zip_path.AppendASCII("baz.txt")); | |
36 zip_contents_.insert(zip_path.AppendASCII("quux.txt")); | |
37 zip_path = zip_path.AppendASCII("baz"); | |
38 zip_contents_.insert(zip_path); | |
39 } | |
40 | |
41 virtual void TearDown() { | |
42 PlatformTest::TearDown(); | |
43 // Clean up test directory | |
44 ASSERT_TRUE(file_util::Delete(test_dir_, true)); | |
45 ASSERT_FALSE(file_util::PathExists(test_dir_)); | |
46 } | |
47 | |
48 void TestZipFile(const FilePath::StringType& filename) { | |
49 FilePath test_dir; | |
50 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir)); | |
51 test_dir = test_dir.AppendASCII("unzip"); | |
52 FilePath path = test_dir.Append(filename); | |
53 | |
54 ASSERT_TRUE(file_util::PathExists(path)) << "no file " << path.value(); | |
55 std::vector<FilePath> out_files; | |
56 ASSERT_TRUE(Unzip(path, test_dir_, &out_files)); | |
57 | |
58 file_util::FileEnumerator files(test_dir_, true, | |
59 file_util::FileEnumerator::FILES_AND_DIRECTORIES); | |
60 FilePath next_path = files.Next(); | |
61 size_t count = 0; | |
62 while (!next_path.value().empty()) { | |
63 EXPECT_EQ(zip_contents_.count(next_path), 1U) << | |
64 "Couldn't find " << next_path.value(); | |
65 count++; | |
66 next_path = files.Next(); | |
67 } | |
68 EXPECT_EQ(count, zip_contents_.size()); | |
69 EXPECT_EQ(count, out_files.size()); | |
70 std::vector<FilePath>::iterator iter; | |
71 for (iter = out_files.begin(); iter != out_files.end(); ++iter) { | |
72 EXPECT_EQ(zip_contents_.count(*iter), 1U) << | |
73 "Couldn't find " << (*iter).value(); | |
74 } | |
75 } | |
76 | |
77 // the path to temporary directory used to contain the test operations | |
78 FilePath test_dir_; | |
79 | |
80 // hard-coded contents of a known zip file | |
81 std::set<FilePath> zip_contents_; | |
82 }; | |
83 | |
84 | |
85 TEST_F(UnzipTest, Unzip) { | |
86 TestZipFile(FILE_PATH_LITERAL("test.zip")); | |
87 } | |
88 | |
89 TEST_F(UnzipTest, UnzipUncompressed) { | |
90 TestZipFile(FILE_PATH_LITERAL("test_nocompress.zip")); | |
91 } | |
92 | |
93 } // namespace | |
OLD | NEW |