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 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
|
// 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.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/390223051): Remove C-library calls to fix the errors.
#pragma allow_unsafe_libc_calls
#endif
#include "chrome/browser/extensions/api/image_writer_private/test_utils.h"
#include <string.h>
#include <utility>
#include "base/containers/heap_array.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/path_service.h"
#include "base/task/single_thread_task_runner.h"
#include "base/task/thread_pool.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/extensions/api/image_writer_private/error_constants.h"
#include "chrome/common/chrome_paths.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "chromeos/ash/components/dbus/concierge/concierge_client.h"
#include "chromeos/ash/components/dbus/dbus_thread_manager.h" // nogncheck
#include "chromeos/ash/components/dbus/image_burner/fake_image_burner_client.h"
#include "chromeos/ash/components/dbus/image_burner/image_burner_client.h"
#include "chromeos/ash/components/disks/disk.h"
#endif
namespace extensions {
namespace image_writer {
#if BUILDFLAG(IS_CHROMEOS)
class ImageWriterFakeImageBurnerClient : public ash::FakeImageBurnerClient {
public:
ImageWriterFakeImageBurnerClient() = default;
~ImageWriterFakeImageBurnerClient() override = default;
void SetEventHandlers(
BurnFinishedHandler burn_finished_handler,
const BurnProgressUpdateHandler& burn_progress_update_handler) override {
burn_finished_handler_ = std::move(burn_finished_handler);
burn_progress_update_handler_ = burn_progress_update_handler;
}
void BurnImage(const std::string& from_path,
const std::string& to_path,
ErrorCallback error_callback) override {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(burn_progress_update_handler_, to_path, 0, 100));
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(burn_progress_update_handler_, to_path, 50, 100));
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(burn_progress_update_handler_, to_path, 100, 100));
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(std::move(burn_finished_handler_), to_path, true, ""));
}
private:
BurnFinishedHandler burn_finished_handler_;
BurnProgressUpdateHandler burn_progress_update_handler_;
};
#endif
MockOperationManager::MockOperationManager(content::BrowserContext* context)
: OperationManager(context) {}
MockOperationManager::~MockOperationManager() = default;
#if BUILDFLAG(IS_CHROMEOS)
UnmountingMockDiskMountManager::UnmountingMockDiskMountManager() = default;
UnmountingMockDiskMountManager::~UnmountingMockDiskMountManager() = default;
void UnmountingMockDiskMountManager::UnmountDeviceRecursively(
const std::string& device_path,
UnmountDeviceRecursivelyCallbackType callback) {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(std::move(callback), ash::MountError::kSuccess));
}
#endif
SimulateProgressInfo::SimulateProgressInfo(
const std::vector<int>& progress_list,
bool will_succeed)
: progress_list(progress_list), will_succeed(will_succeed) {}
SimulateProgressInfo::~SimulateProgressInfo() = default;
SimulateProgressInfo::SimulateProgressInfo(const SimulateProgressInfo&) =
default;
FakeImageWriterClient::FakeImageWriterClient()
: ImageWriterUtilityClient(base::ThreadPool::CreateSequencedTaskRunner(
{base::MayBlock(), base::TaskPriority::USER_VISIBLE,
base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN})) {}
FakeImageWriterClient::~FakeImageWriterClient() = default;
void FakeImageWriterClient::SimulateProgressAndCompletion(
const SimulateProgressInfo& info) {
for (int progress : info.progress_list)
Progress(progress);
if (info.will_succeed) {
Success();
} else {
Error(error::kVerificationFailed);
}
}
void FakeImageWriterClient::Write(ProgressCallback progress_callback,
SuccessCallback success_callback,
ErrorCallback error_callback,
const base::FilePath& source,
const base::FilePath& target) {
progress_callback_ = std::move(progress_callback);
success_callback_ = std::move(success_callback);
error_callback_ = std::move(error_callback);
if (simulate_on_write_) {
SimulateProgressAndCompletion(*simulate_on_write_);
simulate_on_write_.reset();
}
}
void FakeImageWriterClient::Verify(ProgressCallback progress_callback,
SuccessCallback success_callback,
ErrorCallback error_callback,
const base::FilePath& source,
const base::FilePath& target) {
progress_callback_ = std::move(progress_callback);
success_callback_ = std::move(success_callback);
error_callback_ = std::move(error_callback);
if (simulate_on_verify_) {
SimulateProgressAndCompletion(*simulate_on_verify_);
simulate_on_verify_.reset();
}
}
void FakeImageWriterClient::Cancel(CancelCallback cancel_callback) {
cancel_callback_ = std::move(cancel_callback);
}
void FakeImageWriterClient::Shutdown() {
// Clear handlers to not hold any reference to the caller.
success_callback_.Reset();
progress_callback_.Reset();
error_callback_.Reset();
cancel_callback_.Reset();
simulate_on_write_.reset();
simulate_on_verify_.reset();
}
void FakeImageWriterClient::SimulateProgressOnWrite(
const std::vector<int>& progress_list,
bool will_succeed) {
simulate_on_write_ = SimulateProgressInfo(progress_list, will_succeed);
}
void FakeImageWriterClient::SimulateProgressOnVerifyWrite(
const std::vector<int>& progress_list,
bool will_succeed) {
simulate_on_verify_ = SimulateProgressInfo(progress_list, will_succeed);
}
void FakeImageWriterClient::Progress(int64_t progress) {
if (!progress_callback_.is_null())
progress_callback_.Run(progress);
}
void FakeImageWriterClient::Success() {
if (!success_callback_.is_null())
std::move(success_callback_).Run();
}
void FakeImageWriterClient::Error(const std::string& message) {
if (!error_callback_.is_null())
std::move(error_callback_).Run(message);
}
void FakeImageWriterClient::Cancel() {
if (!cancel_callback_.is_null())
std::move(cancel_callback_).Run();
}
#if !BUILDFLAG(IS_CHROMEOS)
scoped_refptr<ImageWriterUtilityClient> CreateFakeImageWriterUtilityClient(
ImageWriterTestUtils* utils) {
auto* client = new FakeImageWriterClient();
utils->OnUtilityClientCreated(client);
return base::WrapRefCounted(client);
}
#endif // !BUILDFLAG(IS_CHROMEOS)
ImageWriterTestUtils::ImageWriterTestUtils()
#if !BUILDFLAG(IS_CHROMEOS)
: utility_client_factory_(
base::BindRepeating(&CreateFakeImageWriterUtilityClient, this))
#endif
{
}
ImageWriterTestUtils::~ImageWriterTestUtils() = default;
#if !BUILDFLAG(IS_CHROMEOS)
void ImageWriterTestUtils::OnUtilityClientCreated(
FakeImageWriterClient* client) {
DCHECK(!client_.get())
<< "Single FakeImageWriterClient instance per test case expected.";
client_ = client;
if (!client_creation_callback_.is_null())
std::move(client_creation_callback_).Run(client);
}
#endif
#if !BUILDFLAG(IS_CHROMEOS)
void ImageWriterTestUtils::RunOnUtilityClientCreation(
base::OnceCallback<void(FakeImageWriterClient*)> closure) {
client_creation_callback_ = std::move(closure);
}
#endif
void ImageWriterTestUtils::SetUp() {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
ASSERT_TRUE(
base::CreateTemporaryFileInDir(temp_dir_.GetPath(), &test_image_path_));
ASSERT_TRUE(
base::CreateTemporaryFileInDir(temp_dir_.GetPath(), &test_device_path_));
ASSERT_TRUE(FillFile(test_image_path_, kImagePattern, kTestFileSize));
ASSERT_TRUE(FillFile(test_device_path_, kDevicePattern, kTestFileSize));
#if BUILDFLAG(IS_CHROMEOS)
// Browser tests might have already initialized ConciergeClient.
if (!ash::ConciergeClient::Get()) {
ash::ConciergeClient::InitializeFake(
/*fake_cicerone_client=*/nullptr);
concierge_client_initialized_ = true;
}
image_burner_client_ = std::make_unique<ImageWriterFakeImageBurnerClient>();
ash::ImageBurnerClient::SetInstanceForTest(image_burner_client_.get());
auto* disk_manager = new UnmountingMockDiskMountManager();
ash::disks::DiskMountManager::InitializeForTesting(disk_manager);
// Adds a disk entry for test_device_path_ with the same device and file path.
disk_manager->CreateDiskEntryForMountDevice(
{test_device_path_.value(), "/dummy/mount", ash::MountType::kDevice},
"device_id", "device_label", "Vendor", "Product", ash::DeviceType::kUSB,
kTestFileSize, true, true, true, false, kTestFileSystemType);
disk_manager->SetupDefaultReplies();
#else
ImageWriterUtilityClient::SetFactoryForTesting(&utility_client_factory_);
#endif
}
void ImageWriterTestUtils::TearDown() {
#if BUILDFLAG(IS_CHROMEOS)
ash::ImageBurnerClient::SetInstanceForTest(nullptr);
image_burner_client_.reset();
if (concierge_client_initialized_) {
ash::ConciergeClient::Shutdown();
concierge_client_initialized_ = false;
}
ash::disks::DiskMountManager::Shutdown();
#else
ImageWriterUtilityClient::SetFactoryForTesting(nullptr);
#endif
}
const base::FilePath& ImageWriterTestUtils::GetTempDir() {
return temp_dir_.GetPath();
}
const base::FilePath& ImageWriterTestUtils::GetImagePath() {
return test_image_path_;
}
const base::FilePath& ImageWriterTestUtils::GetDevicePath() {
return test_device_path_;
}
bool ImageWriterTestUtils::ImageWrittenToDevice() {
auto image_buffer = base::HeapArray<char>::Uninit(kTestFileSize);
auto device_buffer = base::HeapArray<char>::Uninit(kTestFileSize);
std::optional<uint64_t> image_bytes_read =
ReadFile(test_image_path_, image_buffer);
if (!image_bytes_read) {
return false;
}
std::optional<uint64_t> device_bytes_read =
ReadFile(test_device_path_, device_buffer);
if (image_bytes_read != device_bytes_read) {
return false;
}
return memcmp(image_buffer.data(), device_buffer.data(), *image_bytes_read) ==
0;
}
bool ImageWriterTestUtils::FillFile(const base::FilePath& file,
uint8_t pattern,
size_t length) {
std::vector<uint8_t> buffer(length, pattern);
return base::WriteFile(file, buffer);
}
ImageWriterUnitTestBase::ImageWriterUnitTestBase()
: task_environment_(content::BrowserTaskEnvironment::IO_MAINLOOP) {}
ImageWriterUnitTestBase::~ImageWriterUnitTestBase() = default;
void ImageWriterUnitTestBase::SetUp() {
testing::Test::SetUp();
test_utils_.SetUp();
}
void ImageWriterUnitTestBase::TearDown() {
test_utils_.TearDown();
testing::Test::TearDown();
}
bool GetTestDataDirectory(base::FilePath* path) {
bool success = base::PathService::Get(chrome::DIR_TEST_DATA, path);
if (!success)
return false;
*path = path->AppendASCII("image_writer_private");
return true;
}
} // namespace image_writer
} // namespace extensions
|