Home | History | Annotate | Download | only in google
      1 // Copyright (c) 2011 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 #include <vector>
      7 
      8 #include "base/file_util.h"
      9 #include "base/files/file_enumerator.h"
     10 #include "base/files/file_path.h"
     11 #include "base/files/scoped_temp_dir.h"
     12 #include "base/path_service.h"
     13 #include "base/strings/string_util.h"
     14 #include "testing/gtest/include/gtest/gtest.h"
     15 #include "testing/platform_test.h"
     16 #include "third_party/zlib/google/zip.h"
     17 #include "third_party/zlib/google/zip_reader.h"
     18 
     19 namespace {
     20 
     21 // Make the test a PlatformTest to setup autorelease pools properly on Mac.
     22 class ZipTest : public PlatformTest {
     23  protected:
     24   virtual void SetUp() {
     25     PlatformTest::SetUp();
     26 
     27     ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
     28     test_dir_ = temp_dir_.path();
     29 
     30     base::FilePath zip_path(test_dir_);
     31     zip_contents_.insert(zip_path.AppendASCII("foo.txt"));
     32     zip_path = zip_path.AppendASCII("foo");
     33     zip_contents_.insert(zip_path);
     34     zip_contents_.insert(zip_path.AppendASCII("bar.txt"));
     35     zip_path = zip_path.AppendASCII("bar");
     36     zip_contents_.insert(zip_path);
     37     zip_contents_.insert(zip_path.AppendASCII("baz.txt"));
     38     zip_contents_.insert(zip_path.AppendASCII("quux.txt"));
     39     zip_contents_.insert(zip_path.AppendASCII(".hidden"));
     40 
     41     // Include a subset of files in |zip_file_list_| to test ZipFiles().
     42     zip_file_list_.push_back(base::FilePath(FILE_PATH_LITERAL("foo.txt")));
     43     zip_file_list_.push_back(
     44         base::FilePath(FILE_PATH_LITERAL("foo/bar/quux.txt")));
     45     zip_file_list_.push_back(
     46         base::FilePath(FILE_PATH_LITERAL("foo/bar/.hidden")));
     47   }
     48 
     49   virtual void TearDown() {
     50     PlatformTest::TearDown();
     51   }
     52 
     53   bool GetTestDataDirectory(base::FilePath* path) {
     54     bool success = PathService::Get(base::DIR_SOURCE_ROOT, path);
     55     EXPECT_TRUE(success);
     56     if (!success)
     57       return false;
     58     *path = path->AppendASCII("third_party");
     59     *path = path->AppendASCII("zlib");
     60     *path = path->AppendASCII("google");
     61     *path = path->AppendASCII("test");
     62     *path = path->AppendASCII("data");
     63     return true;
     64   }
     65 
     66   void TestUnzipFile(const base::FilePath::StringType& filename,
     67                      bool expect_hidden_files) {
     68     base::FilePath test_dir;
     69     ASSERT_TRUE(GetTestDataDirectory(&test_dir));
     70     TestUnzipFile(test_dir.Append(filename), expect_hidden_files);
     71   }
     72 
     73   void TestUnzipFile(const base::FilePath& path, bool expect_hidden_files) {
     74     ASSERT_TRUE(base::PathExists(path)) << "no file " << path.value();
     75     ASSERT_TRUE(zip::Unzip(path, test_dir_));
     76 
     77     base::FileEnumerator files(test_dir_, true,
     78         base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES);
     79     base::FilePath next_path = files.Next();
     80     size_t count = 0;
     81     while (!next_path.value().empty()) {
     82       if (next_path.value().find(FILE_PATH_LITERAL(".svn")) ==
     83           base::FilePath::StringType::npos) {
     84         EXPECT_EQ(zip_contents_.count(next_path), 1U) <<
     85             "Couldn't find " << next_path.value();
     86         count++;
     87       }
     88       next_path = files.Next();
     89     }
     90 
     91     size_t expected_count = 0;
     92     for (std::set<base::FilePath>::iterator iter = zip_contents_.begin();
     93          iter != zip_contents_.end(); ++iter) {
     94       if (expect_hidden_files || iter->BaseName().value()[0] != '.')
     95         ++expected_count;
     96     }
     97 
     98     EXPECT_EQ(expected_count, count);
     99   }
    100 
    101   // The path to temporary directory used to contain the test operations.
    102   base::FilePath test_dir_;
    103 
    104   base::ScopedTempDir temp_dir_;
    105 
    106   // Hard-coded contents of a known zip file.
    107   std::set<base::FilePath> zip_contents_;
    108 
    109   // Hard-coded list of relative paths for a zip file created with ZipFiles.
    110   std::vector<base::FilePath> zip_file_list_;
    111 };
    112 
    113 TEST_F(ZipTest, Unzip) {
    114   TestUnzipFile(FILE_PATH_LITERAL("test.zip"), true);
    115 }
    116 
    117 TEST_F(ZipTest, UnzipUncompressed) {
    118   TestUnzipFile(FILE_PATH_LITERAL("test_nocompress.zip"), true);
    119 }
    120 
    121 TEST_F(ZipTest, UnzipEvil) {
    122   base::FilePath path;
    123   ASSERT_TRUE(GetTestDataDirectory(&path));
    124   path = path.AppendASCII("evil.zip");
    125   // Unzip the zip file into a sub directory of test_dir_ so evil.zip
    126   // won't create a persistent file outside test_dir_ in case of a
    127   // failure.
    128   base::FilePath output_dir = test_dir_.AppendASCII("out");
    129   ASSERT_FALSE(zip::Unzip(path, output_dir));
    130   base::FilePath evil_file = output_dir;
    131   evil_file = evil_file.AppendASCII(
    132       "../levilevilevilevilevilevilevilevilevilevilevilevil");
    133   ASSERT_FALSE(base::PathExists(evil_file));
    134 }
    135 
    136 TEST_F(ZipTest, UnzipEvil2) {
    137   base::FilePath path;
    138   ASSERT_TRUE(GetTestDataDirectory(&path));
    139   // The zip file contains an evil file with invalid UTF-8 in its file
    140   // name.
    141   path = path.AppendASCII("evil_via_invalid_utf8.zip");
    142   // See the comment at UnzipEvil() for why we do this.
    143   base::FilePath output_dir = test_dir_.AppendASCII("out");
    144   // This should fail as it contains an evil file.
    145   ASSERT_FALSE(zip::Unzip(path, output_dir));
    146   base::FilePath evil_file = output_dir;
    147   evil_file = evil_file.AppendASCII("../evil.txt");
    148   ASSERT_FALSE(base::PathExists(evil_file));
    149 }
    150 
    151 TEST_F(ZipTest, Zip) {
    152   base::FilePath src_dir;
    153   ASSERT_TRUE(GetTestDataDirectory(&src_dir));
    154   src_dir = src_dir.AppendASCII("test");
    155 
    156   base::ScopedTempDir temp_dir;
    157   ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
    158   base::FilePath zip_file = temp_dir.path().AppendASCII("out.zip");
    159 
    160   EXPECT_TRUE(zip::Zip(src_dir, zip_file, true));
    161   TestUnzipFile(zip_file, true);
    162 }
    163 
    164 TEST_F(ZipTest, ZipIgnoreHidden) {
    165   base::FilePath src_dir;
    166   ASSERT_TRUE(GetTestDataDirectory(&src_dir));
    167   src_dir = src_dir.AppendASCII("test");
    168 
    169   base::ScopedTempDir temp_dir;
    170   ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
    171   base::FilePath zip_file = temp_dir.path().AppendASCII("out.zip");
    172 
    173   EXPECT_TRUE(zip::Zip(src_dir, zip_file, false));
    174   TestUnzipFile(zip_file, false);
    175 }
    176 
    177 TEST_F(ZipTest, ZipNonASCIIDir) {
    178   base::FilePath src_dir;
    179   ASSERT_TRUE(GetTestDataDirectory(&src_dir));
    180   src_dir = src_dir.AppendASCII("test");
    181 
    182   base::ScopedTempDir temp_dir;
    183   ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
    184   // Append '' (in cyrillic).
    185   base::FilePath src_dir_russian =
    186       temp_dir.path().Append(base::FilePath::FromUTF8Unsafe(
    187           "\xD0\xA2\xD0\xB5\xD1\x81\xD1\x82"));
    188   base::CopyDirectory(src_dir, src_dir_russian, true);
    189   base::FilePath zip_file = temp_dir.path().AppendASCII("out_russian.zip");
    190 
    191   EXPECT_TRUE(zip::Zip(src_dir_russian, zip_file, true));
    192   TestUnzipFile(zip_file, true);
    193 }
    194 
    195 #if defined(OS_POSIX)
    196 TEST_F(ZipTest, ZipFiles) {
    197   base::FilePath src_dir;
    198   ASSERT_TRUE(GetTestDataDirectory(&src_dir));
    199   src_dir = src_dir.AppendASCII("test");
    200 
    201   base::ScopedTempDir temp_dir;
    202   ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
    203   base::FilePath zip_file = temp_dir.path().AppendASCII("out.zip");
    204 
    205   const int flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
    206   const base::PlatformFile zip_fd =
    207       base::CreatePlatformFile(zip_file, flags, NULL, NULL);
    208   ASSERT_LE(0, zip_fd);
    209   EXPECT_TRUE(zip::ZipFiles(src_dir, zip_file_list_, zip_fd));
    210   base::ClosePlatformFile(zip_fd);
    211 
    212   zip::ZipReader reader;
    213   EXPECT_TRUE(reader.Open(zip_file));
    214   EXPECT_EQ(zip_file_list_.size(), static_cast<size_t>(reader.num_entries()));
    215   for (size_t i = 0; i < zip_file_list_.size(); ++i) {
    216     EXPECT_TRUE(reader.LocateAndOpenEntry(zip_file_list_[i]));
    217     // Check the path in the entry just in case.
    218     const zip::ZipReader::EntryInfo* entry_info = reader.current_entry_info();
    219     EXPECT_EQ(entry_info->file_path(), zip_file_list_[i]);
    220   }
    221 }
    222 #endif  // defined(OS_POSIX)
    223 
    224 }  // namespace
    225 
    226