zip_file_creator_browsertest.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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/callback.h" 10#include "base/file_util.h" 11#include "base/files/file_path.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 20namespace file_manager { 21 22namespace { 23 24class TestObserver : public ZipFileCreator::Observer { 25 public: 26 explicit TestObserver(const base::Closure& quit) 27 : success_(false), quit_(quit) {} 28 29 virtual void OnZipDone(bool success) OVERRIDE { 30 success_ = success; 31 quit_.Run(); 32 } 33 34 const bool success() const { return success_; } 35 36 private: 37 bool success_; 38 const base::Closure quit_; 39}; 40 41class ZipFileCreatorTest : public InProcessBrowserTest { 42 protected: 43 virtual void SetUpOnMainThread() OVERRIDE { 44 ASSERT_TRUE(dir_.CreateUniqueTempDir()); 45 ASSERT_TRUE(base::CreateDirectory(zip_base_dir())); 46 } 47 48 base::FilePath zip_archive_path() const { 49 return dir_.path().AppendASCII("test.zip"); 50 } 51 52 base::FilePath zip_base_dir() const { 53 return dir_.path().AppendASCII("files"); 54 } 55 56 protected: 57 base::ScopedTempDir dir_; 58}; 59 60} // namespace 61 62IN_PROC_BROWSER_TEST_F(ZipFileCreatorTest, FailZipForAbsentFile) { 63 base::RunLoop run_loop; 64 TestObserver observer(content::GetQuitTaskForRunLoop(&run_loop)); 65 66 std::vector<base::FilePath> paths; 67 paths.push_back(base::FilePath(FILE_PATH_LITERAL("not.exist"))); 68 scoped_refptr<ZipFileCreator> zipper(new ZipFileCreator( 69 &observer, zip_base_dir(), paths, zip_archive_path())); 70 zipper->Start(); 71 72 content::RunThisRunLoop(&run_loop); 73 EXPECT_FALSE(observer.success()); 74} 75 76IN_PROC_BROWSER_TEST_F(ZipFileCreatorTest, SomeFilesZip) { 77 // Prepare files. 78 const base::FilePath kDir1(FILE_PATH_LITERAL("foo")); 79 const base::FilePath kFile1(kDir1.AppendASCII("bar")); 80 const base::FilePath kFile2(FILE_PATH_LITERAL("random")); 81 const int kRandomDataSize = 100000; 82 const std::string kRandomData = base::RandBytesAsString(kRandomDataSize); 83 base::CreateDirectory(zip_base_dir().Append(kDir1)); 84 file_util::WriteFile(zip_base_dir().Append(kFile1), "123", 3); 85 file_util::WriteFile(zip_base_dir().Append(kFile2), 86 kRandomData.c_str(), kRandomData.size()); 87 88 base::RunLoop run_loop; 89 TestObserver observer(content::GetQuitTaskForRunLoop(&run_loop)); 90 91 std::vector<base::FilePath> paths; 92 paths.push_back(kDir1); 93 paths.push_back(kFile1); 94 paths.push_back(kFile2); 95 scoped_refptr<ZipFileCreator> zipper(new ZipFileCreator( 96 &observer, zip_base_dir(), paths, zip_archive_path())); 97 zipper->Start(); 98 99 content::RunThisRunLoop(&run_loop); 100 EXPECT_TRUE(observer.success()); 101 102 // Check the archive content. 103 zip::ZipReader reader; 104 ASSERT_TRUE(reader.Open(zip_archive_path())); 105 EXPECT_EQ(3, reader.num_entries()); 106 while (reader.HasMore()) { 107 ASSERT_TRUE(reader.OpenCurrentEntryInZip()); 108 const zip::ZipReader::EntryInfo* entry = reader.current_entry_info(); 109 // ZipReader returns directory path with trailing slash. 110 if (entry->file_path() == kDir1.AsEndingWithSeparator()) { 111 EXPECT_TRUE(entry->is_directory()); 112 } else if (entry->file_path() == kFile1) { 113 EXPECT_FALSE(entry->is_directory()); 114 EXPECT_EQ(3, entry->original_size()); 115 } else if (entry->file_path() == kFile2) { 116 EXPECT_FALSE(entry->is_directory()); 117 EXPECT_EQ(kRandomDataSize, entry->original_size()); 118 119 const base::FilePath out = dir_.path().AppendASCII("archived_content"); 120 EXPECT_TRUE(reader.ExtractCurrentEntryToFilePath(out)); 121 EXPECT_TRUE(base::ContentsEqual(zip_base_dir().Append(kFile2), out)); 122 } else { 123 ADD_FAILURE(); 124 } 125 ASSERT_TRUE(reader.AdvanceToNextEntry()); 126 } 127} 128 129} // namespace file_manager 130