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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/api/image_writer_private/operation.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/run_loop.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/extensions/api/image_writer_private/error_constants.h"
#include "chrome/browser/extensions/api/image_writer_private/operation_manager.h"
#include "chrome/browser/extensions/api/image_writer_private/test_utils.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/zlib/google/zip.h"
namespace extensions {
namespace image_writer {
namespace {
using testing::_;
using testing::AnyNumber;
using testing::AtLeast;
using testing::Gt;
using testing::Lt;
#if !BUILDFLAG(IS_CHROMEOS)
void SetUpUtilityClientProgressOnVerifyWrite(
const std::vector<int>& progress_list,
bool will_succeed,
FakeImageWriterClient* client) {
client->SimulateProgressOnVerifyWrite(progress_list, will_succeed);
}
#endif // !BUILDFLAG(IS_CHROMEOS)
} // namespace
// This class gives us a generic Operation with the ability to set or inspect
// the current path to the image file.
class OperationForTest : public Operation {
public:
OperationForTest(base::WeakPtr<OperationManager> manager_,
const ExtensionId& extension_id,
const std::string& device_path,
const base::FilePath& download_path)
: Operation(manager_, extension_id, device_path, download_path) {}
void StartImpl() override {}
// Expose internal stages for testing.
// Also wraps Operation's methods to run on correct sequence.
void Extract(base::OnceClosure continuation) {
PostTask(
base::BindOnce(&Operation::Extract, this, std::move(continuation)));
}
void Write(base::OnceClosure continuation) {
PostTask(base::BindOnce(&Operation::Write, this, std::move(continuation)));
}
void VerifyWrite(base::OnceClosure continuation) {
PostTask(
base::BindOnce(&Operation::VerifyWrite, this, std::move(continuation)));
}
void Start() { PostTask(base::BindOnce(&Operation::Start, this)); }
void Cancel() { PostTask(base::BindOnce(&Operation::Cancel, this)); }
// Helpers to set-up state for intermediate stages.
void SetImagePath(const base::FilePath image_path) {
image_path_ = image_path;
}
base::FilePath GetImagePath() { return image_path_; }
private:
~OperationForTest() override = default;
};
class ImageWriterOperationTest : public ImageWriterUnitTestBase {
protected:
ImageWriterOperationTest()
: profile_(new TestingProfile), manager_(profile_.get()) {}
void SetUp() override {
ImageWriterUnitTestBase::SetUp();
// Create the zip file.
base::FilePath image_dir = test_utils_.GetTempDir().AppendASCII("zip");
ASSERT_TRUE(base::CreateDirectory(image_dir));
ASSERT_TRUE(base::CreateTemporaryFileInDir(image_dir, &image_path_));
test_utils_.FillFile(image_path_, kImagePattern, kTestFileSize);
zip_file_ = test_utils_.GetTempDir().AppendASCII("test_image.zip");
ASSERT_TRUE(zip::Zip(image_dir, zip_file_, true));
// Operation setup.
operation_ =
new OperationForTest(manager_.AsWeakPtr(),
kDummyExtensionId,
test_utils_.GetDevicePath().AsUTF8Unsafe(),
base::FilePath(FILE_PATH_LITERAL("/var/tmp")));
operation_->SetImagePath(test_utils_.GetImagePath());
}
void TearDown() override {
// Ensure all callbacks have been destroyed and cleanup occurs.
// Cancel() will ensure we Shutdown() FakeImageWriterClient.
operation_->Cancel();
task_environment_.RunUntilIdle();
ImageWriterUnitTestBase::TearDown();
}
base::FilePath image_path_;
base::FilePath zip_file_;
std::unique_ptr<TestingProfile> profile_;
MockOperationManager manager_;
scoped_refptr<OperationForTest> operation_;
};
// Unizpping a non-zip should do nothing.
TEST_F(ImageWriterOperationTest, ExtractNonZipFile) {
EXPECT_CALL(manager_, OnProgress(kDummyExtensionId, _, _)).Times(0);
EXPECT_CALL(manager_, OnError(kDummyExtensionId, _, _, _)).Times(0);
EXPECT_CALL(manager_, OnProgress(kDummyExtensionId, _, _)).Times(0);
EXPECT_CALL(manager_, OnComplete(kDummyExtensionId)).Times(0);
operation_->Start();
base::RunLoop run_loop;
operation_->Extract(run_loop.QuitClosure());
run_loop.Run();
}
TEST_F(ImageWriterOperationTest, ExtractZipFile) {
EXPECT_CALL(manager_, OnError(kDummyExtensionId, _, _, _)).Times(0);
EXPECT_CALL(manager_,
OnProgress(kDummyExtensionId, image_writer_api::Stage::kUnzip, _))
.Times(AtLeast(1));
EXPECT_CALL(manager_,
OnProgress(kDummyExtensionId, image_writer_api::Stage::kUnzip, 0))
.Times(AtLeast(1));
EXPECT_CALL(manager_, OnProgress(kDummyExtensionId,
image_writer_api::Stage::kUnzip, 100))
.Times(AtLeast(1));
operation_->SetImagePath(zip_file_);
operation_->Start();
base::RunLoop run_loop;
operation_->Extract(run_loop.QuitClosure());
run_loop.Run();
EXPECT_TRUE(base::ContentsEqual(image_path_, operation_->GetImagePath()));
}
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
TEST_F(ImageWriterOperationTest, WriteImageToDevice) {
#if !BUILDFLAG(IS_CHROMEOS)
auto set_up_utility_client_progress =
[](const std::vector<int>& progress_list, bool will_succeed,
FakeImageWriterClient* client) {
client->SimulateProgressOnWrite(progress_list, will_succeed);
};
// Sets up client for simulating Operation::Progress() on Operation::Write.
std::vector<int> progress_list{0, kTestFileSize / 2, kTestFileSize};
test_utils_.RunOnUtilityClientCreation(base::BindOnce(
set_up_utility_client_progress, progress_list, true /* will_succeed */));
#endif
EXPECT_CALL(manager_, OnError(kDummyExtensionId, _, _, _)).Times(0);
EXPECT_CALL(manager_,
OnProgress(kDummyExtensionId, image_writer_api::Stage::kWrite, _))
.Times(AtLeast(1));
EXPECT_CALL(manager_,
OnProgress(kDummyExtensionId, image_writer_api::Stage::kWrite, 0))
.Times(AtLeast(1));
EXPECT_CALL(manager_, OnProgress(kDummyExtensionId,
image_writer_api::Stage::kWrite, 100))
.Times(AtLeast(1));
operation_->Start();
base::RunLoop run_loop;
operation_->Write(run_loop.QuitClosure());
run_loop.Run();
}
#endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
#if !BUILDFLAG(IS_CHROMEOS)
// Chrome OS doesn't support verification in the ImageBurner, so these two tests
// are skipped.
TEST_F(ImageWriterOperationTest, VerifyFileSuccess) {
// Sets up client for simulating Operation::Progress() on
// Operation::VerifyWrite.
std::vector<int> progress_list{0, kTestFileSize / 2, kTestFileSize};
test_utils_.RunOnUtilityClientCreation(
base::BindOnce(&SetUpUtilityClientProgressOnVerifyWrite, progress_list,
true /* will_succeed */));
EXPECT_CALL(manager_, OnError(kDummyExtensionId, _, _, _)).Times(0);
EXPECT_CALL(manager_, OnProgress(kDummyExtensionId,
image_writer_api::Stage::kVerifyWrite, _))
.Times(AtLeast(1));
EXPECT_CALL(manager_, OnProgress(kDummyExtensionId,
image_writer_api::Stage::kVerifyWrite, 0))
.Times(AtLeast(1));
EXPECT_CALL(manager_, OnProgress(kDummyExtensionId,
image_writer_api::Stage::kVerifyWrite, 100))
.Times(AtLeast(1));
test_utils_.FillFile(
test_utils_.GetDevicePath(), kImagePattern, kTestFileSize);
operation_->Start();
base::RunLoop run_loop;
operation_->VerifyWrite(run_loop.QuitClosure());
run_loop.Run();
}
TEST_F(ImageWriterOperationTest, VerifyFileFailure) {
// Sets up client for simulating Operation::Progress() on
// Operation::VerifyWrite. Also simulates failure.
std::vector<int> progress_list{0, kTestFileSize / 2};
test_utils_.RunOnUtilityClientCreation(
base::BindOnce(&SetUpUtilityClientProgressOnVerifyWrite, progress_list,
false /* will_succeed */));
EXPECT_CALL(manager_, OnProgress(kDummyExtensionId,
image_writer_api::Stage::kVerifyWrite, _))
.Times(AnyNumber());
EXPECT_CALL(manager_, OnProgress(kDummyExtensionId,
image_writer_api::Stage::kVerifyWrite, 100))
.Times(0);
EXPECT_CALL(manager_, OnComplete(kDummyExtensionId)).Times(0);
EXPECT_CALL(manager_, OnError(kDummyExtensionId,
image_writer_api::Stage::kVerifyWrite, _, _))
.Times(1);
test_utils_.FillFile(
test_utils_.GetDevicePath(), kDevicePattern, kTestFileSize);
operation_->Start();
operation_->VerifyWrite(base::DoNothing());
content::RunAllTasksUntilIdle();
}
#endif // !BUILDFLAG(IS_CHROMEOS)
// Tests that on creation the operation_ has the expected state.
TEST_F(ImageWriterOperationTest, Creation) {
EXPECT_EQ(0, operation_->GetProgress());
EXPECT_EQ(image_writer_api::Stage::kUnknown, operation_->GetStage());
}
} // namespace image_writer
} // namespace extensions
|