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"
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
{
24 void TestCallback(bool* out_success
, const base::Closure
& quit
, bool success
) {
25 *out_success
= success
;
29 class ZipFileCreatorTest
: public InProcessBrowserTest
{
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");
45 base::ScopedTempDir dir_
;
50 IN_PROC_BROWSER_TEST_F(ZipFileCreatorTest
, FailZipForAbsentFile
) {
51 base::RunLoop run_loop
;
54 std::vector
<base::FilePath
> paths
;
55 paths
.push_back(base::FilePath(FILE_PATH_LITERAL("not.exist")));
58 &TestCallback
, &success
, content::GetQuitTaskForRunLoop(&run_loop
)),
61 zip_archive_path()))->Start();
63 content::RunThisRunLoop(&run_loop
);
64 EXPECT_FALSE(success
);
67 IN_PROC_BROWSER_TEST_F(ZipFileCreatorTest
, SomeFilesZip
) {
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());
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
);
88 &TestCallback
, &success
, content::GetQuitTaskForRunLoop(&run_loop
)),
91 zip_archive_path()))->Start();
93 content::RunThisRunLoop(&run_loop
);
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
));
119 ASSERT_TRUE(reader
.AdvanceToNextEntry());
123 } // namespace file_manager