Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / chromeos / file_manager / zip_file_creator_browsertest.cc
blob976136cc199b14c9f89671eb363f0f314b8890fa
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.
5 #include "chrome/browser/chromeos/file_manager/zip_file_creator.h"
7 #include <vector>
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"
20 namespace file_manager {
22 namespace {
24 void TestCallback(bool* out_success, const base::Closure& quit, bool success) {
25 *out_success = success;
26 quit.Run();
29 class ZipFileCreatorTest : public InProcessBrowserTest {
30 protected:
31 void SetUpOnMainThread() override {
32 ASSERT_TRUE(dir_.CreateUniqueTempDir());
33 ASSERT_TRUE(base::CreateDirectory(zip_base_dir()));
36 base::FilePath zip_archive_path() const {
37 return dir_.path().AppendASCII("test.zip");
40 base::FilePath zip_base_dir() const {
41 return dir_.path().AppendASCII("files");
44 protected:
45 base::ScopedTempDir dir_;
48 } // namespace
50 IN_PROC_BROWSER_TEST_F(ZipFileCreatorTest, FailZipForAbsentFile) {
51 base::RunLoop run_loop;
52 bool success = true;
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();
63 content::RunThisRunLoop(&run_loop);
64 EXPECT_FALSE(success);
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());
79 bool success = false;
80 base::RunLoop run_loop;
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();
93 content::RunThisRunLoop(&run_loop);
94 EXPECT_TRUE(success);
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());
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();
119 ASSERT_TRUE(reader.AdvanceToNextEntry());
123 } // namespace file_manager