Chromium Code Reviews
[email protected] (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(445)

Side by Side Diff: chrome/common/zip_unittest.cc

Issue 118028: Implements a Zip() utility function. Refactor existing (Closed)
Patch Set: Attempt to get rietveld to recognize copies Created 11 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/zip.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("zip");
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
94 // Make the test a PlatformTest to setup autorelease pools properly on Mac.
95 class ZipTest : public PlatformTest {
96 protected:
97 virtual void SetUp() {
98 PlatformTest::SetUp();
99
100 // We create a temporary directory because the zip utility wants to create
101 // its own file, but we need to actually create a filesystem entry to
102 // create a temporary name.
103 ASSERT_TRUE(file_util::CreateNewTempDirectory(
104 FILE_PATH_LITERAL("zip_unittest_"), &out_dir_));
105 }
106
107 virtual void TearDown() {
108 PlatformTest::TearDown();
109 // Clean up test directory
110 ASSERT_TRUE(file_util::Delete(out_dir_, true));
111 ASSERT_FALSE(file_util::PathExists(out_dir_));
112 }
113
114 // Path to a directory that contains our test output.
115 FilePath out_dir_;
116 };
117
118 TEST_F(ZipTest, Zip) {
119 FilePath src_dir;
120 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &src_dir));
121 src_dir = src_dir.AppendASCII("zip").AppendASCII("test");
122
123 FilePath zip_file = out_dir_.AppendASCII("out.zip");
124 ASSERT_TRUE(Zip(src_dir, zip_file));
Erik does not do reviews 2009/05/29 23:33:27 we should do a zip then unzip verification test
125
126 int64 size = 0;
127 ASSERT_TRUE(file_util::GetFileSize(zip_file, &size));
128 ASSERT_EQ(4718, size);
129 }
130
131 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698