Home | History | Annotate | Download | only in file_manager
      1 // Copyright 2013 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 "chrome/browser/chromeos/file_manager/zip_file_creator.h"
      6 
      7 #include <vector>
      8 
      9 #include "base/bind.h"
     10 #include "base/files/file_path.h"
     11 #include "base/files/file_util.h"
     12 #include "base/files/scoped_temp_dir.h"
     13 #include "base/rand_util.h"
     14 #include "base/run_loop.h"
     15 #include "chrome/test/base/in_process_browser_test.h"
     16 #include "content/public/test/test_utils.h"
     17 #include "testing/gtest/include/gtest/gtest.h"
     18 #include "third_party/zlib/google/zip_reader.h"
     19 
     20 namespace file_manager {
     21 
     22 namespace {
     23 
     24 void TestCallback(bool* out_success, const base::Closure& quit, bool success) {
     25   *out_success = success;
     26   quit.Run();
     27 }
     28 
     29 class ZipFileCreatorTest : public InProcessBrowserTest {
     30  protected:
     31   virtual void SetUpOnMainThread() OVERRIDE {
     32     ASSERT_TRUE(dir_.CreateUniqueTempDir());
     33     ASSERT_TRUE(base::CreateDirectory(zip_base_dir()));
     34   }
     35 
     36   base::FilePath zip_archive_path() const {
     37     return dir_.path().AppendASCII("test.zip");
     38   }
     39 
     40   base::FilePath zip_base_dir() const {
     41     return dir_.path().AppendASCII("files");
     42   }
     43 
     44  protected:
     45   base::ScopedTempDir dir_;
     46 };
     47 
     48 }  // namespace
     49 
     50 IN_PROC_BROWSER_TEST_F(ZipFileCreatorTest, FailZipForAbsentFile) {
     51   base::RunLoop run_loop;
     52   bool success = true;
     53 
     54   std::vector<base::FilePath> paths;
     55   paths.push_back(base::FilePath(FILE_PATH_LITERAL("not.exist")));
     56   (new ZipFileCreator(
     57        base::Bind(
     58            &TestCallback, &success, content::GetQuitTaskForRunLoop(&run_loop)),
     59        zip_base_dir(),
     60        paths,
     61        zip_archive_path()))->Start();
     62 
     63   content::RunThisRunLoop(&run_loop);
     64   EXPECT_FALSE(success);
     65 }
     66 
     67 IN_PROC_BROWSER_TEST_F(ZipFileCreatorTest, SomeFilesZip) {
     68   // Prepare files.
     69   const base::FilePath kDir1(FILE_PATH_LITERAL("foo"));
     70   const base::FilePath kFile1(kDir1.AppendASCII("bar"));
     71   const base::FilePath kFile2(FILE_PATH_LITERAL("random"));
     72   const int kRandomDataSize = 100000;
     73   const std::string kRandomData = base::RandBytesAsString(kRandomDataSize);
     74   base::CreateDirectory(zip_base_dir().Append(kDir1));
     75   base::WriteFile(zip_base_dir().Append(kFile1), "123", 3);
     76   base::WriteFile(zip_base_dir().Append(kFile2),
     77                   kRandomData.c_str(), kRandomData.size());
     78 
     79   bool success = false;
     80   base::RunLoop run_loop;
     81 
     82   std::vector<base::FilePath> paths;
     83   paths.push_back(kDir1);
     84   paths.push_back(kFile1);
     85   paths.push_back(kFile2);
     86   (new ZipFileCreator(
     87        base::Bind(
     88            &TestCallback, &success, content::GetQuitTaskForRunLoop(&run_loop)),
     89        zip_base_dir(),
     90        paths,
     91        zip_archive_path()))->Start();
     92 
     93   content::RunThisRunLoop(&run_loop);
     94   EXPECT_TRUE(success);
     95 
     96   // Check the archive content.
     97   zip::ZipReader reader;
     98   ASSERT_TRUE(reader.Open(zip_archive_path()));
     99   EXPECT_EQ(3, reader.num_entries());
    100   while (reader.HasMore()) {
    101     ASSERT_TRUE(reader.OpenCurrentEntryInZip());
    102     const zip::ZipReader::EntryInfo* entry = reader.current_entry_info();
    103     // ZipReader returns directory path with trailing slash.
    104     if (entry->file_path() == kDir1.AsEndingWithSeparator()) {
    105       EXPECT_TRUE(entry->is_directory());
    106     } else if (entry->file_path() == kFile1) {
    107       EXPECT_FALSE(entry->is_directory());
    108       EXPECT_EQ(3, entry->original_size());
    109     } else if (entry->file_path() == kFile2) {
    110       EXPECT_FALSE(entry->is_directory());
    111       EXPECT_EQ(kRandomDataSize, entry->original_size());
    112 
    113       const base::FilePath out = dir_.path().AppendASCII("archived_content");
    114       EXPECT_TRUE(reader.ExtractCurrentEntryToFilePath(out));
    115       EXPECT_TRUE(base::ContentsEqual(zip_base_dir().Append(kFile2), out));
    116     } else {
    117       ADD_FAILURE();
    118     }
    119     ASSERT_TRUE(reader.AdvanceToNextEntry());
    120   }
    121 }
    122 
    123 }  // namespace file_manager
    124