Chromium Code Reviews| Index: chrome/common/zip_unittest.cc |
| diff --git a/chrome/common/zip_unittest.cc b/chrome/common/zip_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..cebfedc811a28946a2d31b48e9753bfaa54553f2 |
| --- /dev/null |
| +++ b/chrome/common/zip_unittest.cc |
| @@ -0,0 +1,131 @@ |
| +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <set> |
| + |
| +#include "base/file_path.h" |
| +#include "base/file_util.h" |
| +#include "base/path_service.h" |
| +#include "base/string_util.h" |
| +#include "chrome/common/chrome_paths.h" |
| +#include "chrome/common/zip.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "testing/platform_test.h" |
| + |
| +namespace { |
| + |
| +// Make the test a PlatformTest to setup autorelease pools properly on Mac. |
| +class UnzipTest : public PlatformTest { |
| + protected: |
| + virtual void SetUp() { |
| + PlatformTest::SetUp(); |
| + |
| + ASSERT_TRUE(file_util::CreateNewTempDirectory( |
| + FILE_PATH_LITERAL("unzip_unittest_"), &test_dir_)); |
| + |
| + FilePath zip_path(test_dir_.AppendASCII("test")); |
| + zip_contents_.insert(zip_path); |
| + zip_contents_.insert(zip_path.AppendASCII("foo.txt")); |
| + zip_path = zip_path.AppendASCII("foo"); |
| + zip_contents_.insert(zip_path); |
| + zip_contents_.insert(zip_path.AppendASCII("bar.txt")); |
| + zip_path = zip_path.AppendASCII("bar"); |
| + zip_contents_.insert(zip_path); |
| + zip_contents_.insert(zip_path.AppendASCII("baz.txt")); |
| + zip_contents_.insert(zip_path.AppendASCII("quux.txt")); |
| + zip_path = zip_path.AppendASCII("baz"); |
| + zip_contents_.insert(zip_path); |
| + } |
| + |
| + virtual void TearDown() { |
| + PlatformTest::TearDown(); |
| + // Clean up test directory |
| + ASSERT_TRUE(file_util::Delete(test_dir_, true)); |
| + ASSERT_FALSE(file_util::PathExists(test_dir_)); |
| + } |
| + |
| + void TestZipFile(const FilePath::StringType& filename) { |
| + FilePath test_dir; |
| + ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir)); |
| + test_dir = test_dir.AppendASCII("zip"); |
| + FilePath path = test_dir.Append(filename); |
| + |
| + ASSERT_TRUE(file_util::PathExists(path)) << "no file " << path.value(); |
| + std::vector<FilePath> out_files; |
| + ASSERT_TRUE(Unzip(path, test_dir_, &out_files)); |
| + |
| + file_util::FileEnumerator files(test_dir_, true, |
| + file_util::FileEnumerator::FILES_AND_DIRECTORIES); |
| + FilePath next_path = files.Next(); |
| + size_t count = 0; |
| + while (!next_path.value().empty()) { |
| + EXPECT_EQ(zip_contents_.count(next_path), 1U) << |
| + "Couldn't find " << next_path.value(); |
| + count++; |
| + next_path = files.Next(); |
| + } |
| + EXPECT_EQ(count, zip_contents_.size()); |
| + EXPECT_EQ(count, out_files.size()); |
| + std::vector<FilePath>::iterator iter; |
| + for (iter = out_files.begin(); iter != out_files.end(); ++iter) { |
| + EXPECT_EQ(zip_contents_.count(*iter), 1U) << |
| + "Couldn't find " << (*iter).value(); |
| + } |
| + } |
| + |
| + // the path to temporary directory used to contain the test operations |
| + FilePath test_dir_; |
| + |
| + // hard-coded contents of a known zip file |
| + std::set<FilePath> zip_contents_; |
| +}; |
| + |
| + |
| +TEST_F(UnzipTest, Unzip) { |
| + TestZipFile(FILE_PATH_LITERAL("test.zip")); |
| +} |
| + |
| +TEST_F(UnzipTest, UnzipUncompressed) { |
| + TestZipFile(FILE_PATH_LITERAL("test_nocompress.zip")); |
| +} |
| + |
| + |
| +// Make the test a PlatformTest to setup autorelease pools properly on Mac. |
| +class ZipTest : public PlatformTest { |
| + protected: |
| + virtual void SetUp() { |
| + PlatformTest::SetUp(); |
| + |
| + // We create a temporary directory because the zip utility wants to create |
| + // its own file, but we need to actually create a filesystem entry to |
| + // create a temporary name. |
| + ASSERT_TRUE(file_util::CreateNewTempDirectory( |
| + FILE_PATH_LITERAL("zip_unittest_"), &out_dir_)); |
| + } |
| + |
| + virtual void TearDown() { |
| + PlatformTest::TearDown(); |
| + // Clean up test directory |
| + ASSERT_TRUE(file_util::Delete(out_dir_, true)); |
| + ASSERT_FALSE(file_util::PathExists(out_dir_)); |
| + } |
| + |
| + // Path to a directory that contains our test output. |
| + FilePath out_dir_; |
| +}; |
| + |
| +TEST_F(ZipTest, Zip) { |
| + FilePath src_dir; |
| + ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &src_dir)); |
| + src_dir = src_dir.AppendASCII("zip").AppendASCII("test"); |
| + |
| + FilePath zip_file = out_dir_.AppendASCII("out.zip"); |
| + 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
|
| + |
| + int64 size = 0; |
| + ASSERT_TRUE(file_util::GetFileSize(zip_file, &size)); |
| + ASSERT_EQ(4718, size); |
| +} |
| + |
| +} // namespace |