zip_file_creator_browsertest.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/chromeos/file_manager/zip_file_creator.h"
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <vector>
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/callback.h"
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/file_util.h"
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/files/file_path.h"
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/files/scoped_temp_dir.h"
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/rand_util.h"
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/run_loop.h"
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/test/base/in_process_browser_test.h"
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "content/public/test/test_utils.h"
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "third_party/zlib/google/zip_reader.h"
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace file_manager {
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace {
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class TestObserver : public ZipFileCreator::Observer {
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public:
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  explicit TestObserver(const base::Closure& quit)
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      : success_(false), quit_(quit) {}
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void OnZipDone(bool success) OVERRIDE {
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    success_ = success;
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    quit_.Run();
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const bool success() const { return success_; }
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) private:
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool success_;
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const base::Closure quit_;
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class ZipFileCreatorTest : public InProcessBrowserTest {
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) protected:
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void SetUpOnMainThread() OVERRIDE {
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ASSERT_TRUE(dir_.CreateUniqueTempDir());
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ASSERT_TRUE(base::CreateDirectory(zip_base_dir()));
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::FilePath zip_archive_path() const {
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return dir_.path().AppendASCII("test.zip");
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::FilePath zip_base_dir() const {
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return dir_.path().AppendASCII("files");
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) protected:
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::ScopedTempDir dir_;
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(ZipFileCreatorTest, FailZipForAbsentFile) {
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop run_loop;
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  TestObserver observer(content::GetQuitTaskForRunLoop(&run_loop));
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::vector<base::FilePath> paths;
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  paths.push_back(base::FilePath(FILE_PATH_LITERAL("not.exist")));
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  scoped_refptr<ZipFileCreator> zipper(new ZipFileCreator(
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      &observer, zip_base_dir(), paths, zip_archive_path()));
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  zipper->Start();
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  content::RunThisRunLoop(&run_loop);
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_FALSE(observer.success());
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(ZipFileCreatorTest, SomeFilesZip) {
775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Prepare files.
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const base::FilePath kDir1(FILE_PATH_LITERAL("foo"));
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const base::FilePath kFile1(kDir1.AppendASCII("bar"));
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const base::FilePath kFile2(FILE_PATH_LITERAL("random"));
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const int kRandomDataSize = 100000;
825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const std::string kRandomData = base::RandBytesAsString(kRandomDataSize);
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::CreateDirectory(zip_base_dir().Append(kDir1));
845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  file_util::WriteFile(zip_base_dir().Append(kFile1), "123", 3);
855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  file_util::WriteFile(zip_base_dir().Append(kFile2),
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                       kRandomData.c_str(), kRandomData.size());
875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::RunLoop run_loop;
895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  TestObserver observer(content::GetQuitTaskForRunLoop(&run_loop));
905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::vector<base::FilePath> paths;
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  paths.push_back(kDir1);
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  paths.push_back(kFile1);
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  paths.push_back(kFile2);
955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  scoped_refptr<ZipFileCreator> zipper(new ZipFileCreator(
965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      &observer, zip_base_dir(), paths, zip_archive_path()));
975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  zipper->Start();
985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  content::RunThisRunLoop(&run_loop);
1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(observer.success());
1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Check the archive content.
1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  zip::ZipReader reader;
1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_TRUE(reader.Open(zip_archive_path()));
1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(3, reader.num_entries());
1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  while (reader.HasMore()) {
1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ASSERT_TRUE(reader.OpenCurrentEntryInZip());
1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const zip::ZipReader::EntryInfo* entry = reader.current_entry_info();
1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // ZipReader returns directory path with trailing slash.
1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (entry->file_path() == kDir1.AsEndingWithSeparator()) {
1115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      EXPECT_TRUE(entry->is_directory());
1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    } else if (entry->file_path() == kFile1) {
1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      EXPECT_FALSE(entry->is_directory());
1145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      EXPECT_EQ(3, entry->original_size());
1155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    } else if (entry->file_path() == kFile2) {
1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      EXPECT_FALSE(entry->is_directory());
1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      EXPECT_EQ(kRandomDataSize, entry->original_size());
1185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const base::FilePath out = dir_.path().AppendASCII("archived_content");
1205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      EXPECT_TRUE(reader.ExtractCurrentEntryToFilePath(out));
1215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      EXPECT_TRUE(base::ContentsEqual(zip_base_dir().Append(kFile2), out));
1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    } else {
1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ADD_FAILURE();
1245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
1255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ASSERT_TRUE(reader.AdvanceToNextEntry());
1265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
1275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace file_manager
130