1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string>
#include <utility>
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/run_loop.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/mock_callback.h"
#include "chrome/browser/extensions/api/image_writer_private/error_constants.h"
#include "chrome/browser/extensions/api/image_writer_private/extraction_properties.h"
#include "chrome/browser/extensions/api/image_writer_private/tar_extractor.h"
#include "chrome/browser/extensions/api/image_writer_private/test_utils.h"
#include "chrome/browser/extensions/api/image_writer_private/xz_extractor.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "content/public/test/browser_test.h"
namespace extensions {
namespace image_writer {
using ::testing::_;
using ::testing::NiceMock;
using ::testing::SaveArg;
using ::testing::StrictMock;
class ExtractorBrowserTest : public InProcessBrowserTest {
protected:
ExtractorBrowserTest() = default;
~ExtractorBrowserTest() override = default;
void SetUpOnMainThread() override {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
properties_.temp_dir_path = temp_dir_.GetPath();
properties_.open_callback = open_callback_.Get();
properties_.complete_callback = complete_callback_.Get();
properties_.failure_callback = failure_callback_.Get();
properties_.progress_callback = progress_callback_.Get();
}
protected:
StrictMock<base::MockCallback<ExtractionProperties::OpenCallback>>
open_callback_;
StrictMock<base::MockCallback<ExtractionProperties::CompleteCallback>>
complete_callback_;
StrictMock<base::MockCallback<ExtractionProperties::FailureCallback>>
failure_callback_;
NiceMock<base::MockCallback<ExtractionProperties::ProgressCallback>>
progress_callback_;
ExtractionProperties properties_;
base::ScopedTempDir temp_dir_;
};
IN_PROC_BROWSER_TEST_F(ExtractorBrowserTest, ExtractTar) {
base::ScopedAllowBlockingForTesting allow_blocking;
base::FilePath test_data_dir;
ASSERT_TRUE(GetTestDataDirectory(&test_data_dir));
properties_.image_path = test_data_dir.AppendASCII("test.tar");
base::FilePath out_path;
base::RunLoop run_loop;
EXPECT_CALL(open_callback_, Run(_)).WillOnce(SaveArg<0>(&out_path));
EXPECT_CALL(complete_callback_, Run())
.WillOnce(base::test::RunClosure(run_loop.QuitClosure()));
TarExtractor::Extract(std::move(properties_));
run_loop.Run();
std::string contents;
ASSERT_TRUE(base::ReadFileToString(out_path, &contents));
EXPECT_EQ("foo\n", contents);
}
IN_PROC_BROWSER_TEST_F(ExtractorBrowserTest, ExtractTarXz) {
base::ScopedAllowBlockingForTesting allow_blocking;
base::FilePath test_data_dir;
ASSERT_TRUE(GetTestDataDirectory(&test_data_dir));
properties_.image_path = test_data_dir.AppendASCII("test.tar.xz");
base::FilePath out_path;
base::RunLoop run_loop;
EXPECT_CALL(open_callback_, Run(_)).WillOnce(SaveArg<0>(&out_path));
EXPECT_CALL(complete_callback_, Run())
.WillOnce(base::test::RunClosure(run_loop.QuitClosure()));
XzExtractor::Extract(std::move(properties_));
run_loop.Run();
std::string contents;
ASSERT_TRUE(base::ReadFileToString(out_path, &contents));
EXPECT_EQ("foo\n", contents);
}
IN_PROC_BROWSER_TEST_F(ExtractorBrowserTest, ExtractNonExistentTarXz) {
base::ScopedAllowBlockingForTesting allow_blocking;
base::FilePath test_data_dir;
ASSERT_TRUE(GetTestDataDirectory(&test_data_dir));
properties_.image_path = test_data_dir.AppendASCII("non_existent.tar.xz");
base::RunLoop run_loop;
EXPECT_CALL(failure_callback_, Run(error::kUnzipGenericError))
.WillOnce(base::test::RunClosure(run_loop.QuitClosure()));
XzExtractor::Extract(std::move(properties_));
run_loop.Run();
}
IN_PROC_BROWSER_TEST_F(ExtractorBrowserTest, ZeroByteTarXzFile) {
base::ScopedAllowBlockingForTesting allow_blocking;
// Use a tar.xz containing an empty file.
base::FilePath test_data_dir;
ASSERT_TRUE(GetTestDataDirectory(&test_data_dir));
properties_.image_path = test_data_dir.AppendASCII("empty_file.tar.xz");
base::FilePath out_path;
base::RunLoop run_loop;
EXPECT_CALL(open_callback_, Run(_)).WillOnce(SaveArg<0>(&out_path));
EXPECT_CALL(complete_callback_, Run())
.WillOnce(base::test::RunClosure(run_loop.QuitClosure()));
// Calling progress callback with total bytes == 0 causes 0 division, so it
// should not be called.
EXPECT_CALL(progress_callback_, Run(0, _)).Times(0);
XzExtractor::Extract(std::move(properties_));
run_loop.Run();
std::string contents;
ASSERT_TRUE(base::ReadFileToString(out_path, &contents));
EXPECT_TRUE(contents.empty());
}
IN_PROC_BROWSER_TEST_F(ExtractorBrowserTest, ExtractBigTarXzFile) {
base::ScopedAllowBlockingForTesting allow_blocking;
base::FilePath test_data_dir;
ASSERT_TRUE(GetTestDataDirectory(&test_data_dir));
properties_.image_path = test_data_dir.AppendASCII("2MBzeros.tar.xz");
base::FilePath out_path;
base::RunLoop run_loop;
EXPECT_CALL(open_callback_, Run(_)).WillOnce(SaveArg<0>(&out_path));
EXPECT_CALL(complete_callback_, Run())
.WillOnce(base::test::RunClosure(run_loop.QuitClosure()));
XzExtractor::Extract(std::move(properties_));
run_loop.Run();
std::string contents;
ASSERT_TRUE(base::ReadFileToString(out_path, &contents));
EXPECT_EQ(contents, std::string(2097152, '\0'));
}
} // namespace image_writer
} // namespace extensions
|