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
|
// 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.
#ifndef CHROME_BROWSER_EXTENSIONS_API_IMAGE_WRITER_PRIVATE_TEST_UTILS_H_
#define CHROME_BROWSER_EXTENSIONS_API_IMAGE_WRITER_PRIVATE_TEST_UTILS_H_
#include <stdint.h>
#include <memory>
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/run_loop.h"
#include "base/test/task_environment.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/extensions/api/image_writer_private/image_writer_utility_client.h"
#include "chrome/browser/extensions/api/image_writer_private/operation_manager.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_utils.h"
#include "extensions/common/extension_id.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "chromeos/ash/components/disks/disk_mount_manager.h"
#include "chromeos/ash/components/disks/mock_disk_mount_manager.h"
#endif
namespace extensions {
namespace image_writer {
#if BUILDFLAG(IS_CHROMEOS)
class ImageWriterFakeImageBurnerClient;
#endif
inline constexpr char kDummyExtensionId[] = "DummyExtension";
// Default file size to use in tests. Currently 32kB.
inline constexpr size_t kTestFileSize = 32 * 1024;
// Pattern to use in the image file.
inline constexpr uint8_t kImagePattern = 0x55; // 01010101
// Pattern to use in the device file.
inline constexpr uint8_t kDevicePattern = 0xAA; // 10101010
// Disk file system type
inline constexpr char kTestFileSystemType[] = "vfat";
// A mock around the operation manager for tracking callbacks. Note that there
// are non-virtual methods on this class that should not be called in tests.
class MockOperationManager : public OperationManager {
public:
explicit MockOperationManager(content::BrowserContext* context);
~MockOperationManager() override;
MOCK_METHOD3(OnProgress, void(const ExtensionId& extension_id,
image_writer_api::Stage stage,
int progress));
// Callback for completion events.
MOCK_METHOD1(OnComplete, void(const ExtensionId& extension_id));
// Callback for error events.
MOCK_METHOD4(OnError, void(const ExtensionId& extension_id,
image_writer_api::Stage stage,
int progress,
const std::string& error_message));
};
#if BUILDFLAG(IS_CHROMEOS)
// A mock for the DiskMountManager that will successfully call the unmount
// callback.
class UnmountingMockDiskMountManager : public ash::disks::MockDiskMountManager {
public:
UnmountingMockDiskMountManager();
~UnmountingMockDiskMountManager() override;
void UnmountDeviceRecursively(
const std::string& device_path,
UnmountDeviceRecursivelyCallbackType callback) override;
private:
Disks disks_;
};
#endif
struct SimulateProgressInfo {
SimulateProgressInfo(const std::vector<int>& progress_list,
bool will_succeed);
~SimulateProgressInfo();
SimulateProgressInfo(const SimulateProgressInfo&);
std::vector<int> progress_list;
bool will_succeed;
};
class FakeImageWriterClient : public ImageWriterUtilityClient {
public:
FakeImageWriterClient();
void Write(ProgressCallback progress_callback,
SuccessCallback success_callback,
ErrorCallback error_callback,
const base::FilePath& source,
const base::FilePath& target) override;
void Verify(ProgressCallback progress_callback,
SuccessCallback success_callback,
ErrorCallback error_callback,
const base::FilePath& source,
const base::FilePath& target) override;
void Cancel(CancelCallback cancel_callback) override;
void Shutdown() override;
// Issues Operation::Progress() calls with items in `progress_list` on
// Operation Write(). Sends Operation::Success() iff `will_succeed` is true,
// otherwise issues an error.
void SimulateProgressOnWrite(const std::vector<int>& progress_list,
bool will_succeed);
// Same as SimulateProgressOnWrite, but applies to Operation::VerifyWrite().
void SimulateProgressOnVerifyWrite(const std::vector<int>& progress_list,
bool will_succeed);
// Triggers the progress callback.
void Progress(int64_t progress);
// Triggers the success callback.
void Success();
// Triggers the error callback.
void Error(const std::string& message);
// Triggers the cancel callback.
void Cancel();
protected:
~FakeImageWriterClient() override;
private:
void SimulateProgressAndCompletion(const SimulateProgressInfo& info);
ProgressCallback progress_callback_;
SuccessCallback success_callback_;
ErrorCallback error_callback_;
CancelCallback cancel_callback_;
std::optional<SimulateProgressInfo> simulate_on_write_;
std::optional<SimulateProgressInfo> simulate_on_verify_;
};
class ImageWriterTestUtils {
public:
ImageWriterTestUtils();
virtual ~ImageWriterTestUtils();
#if !BUILDFLAG(IS_CHROMEOS)
using UtilityClientCreationCallback =
base::OnceCallback<void(FakeImageWriterClient*)>;
void RunOnUtilityClientCreation(UtilityClientCreationCallback callback);
// Called when an instance of utility client is created.
void OnUtilityClientCreated(FakeImageWriterClient* client);
#endif
// Verifies that the data in image_path was written to the file at
// device_path. This is different from base::ContentsEqual because the device
// may be larger than the image.
bool ImageWrittenToDevice();
// Fills `file` with `length` bytes of `pattern`, overwriting any existing
// data.
bool FillFile(const base::FilePath& file, uint8_t pattern, size_t length);
// Set up the test utils, creating temporary folders and such.
virtual void SetUp();
virtual void TearDown();
const base::FilePath& GetTempDir();
const base::FilePath& GetImagePath();
const base::FilePath& GetDevicePath();
protected:
base::ScopedTempDir temp_dir_;
base::FilePath test_image_path_;
base::FilePath test_device_path_;
#if BUILDFLAG(IS_CHROMEOS)
std::unique_ptr<ImageWriterFakeImageBurnerClient> image_burner_client_;
bool concierge_client_initialized_ = false;
#else
scoped_refptr<FakeImageWriterClient> client_;
ImageWriterUtilityClient::ImageWriterUtilityClientFactory
utility_client_factory_;
base::OnceCallback<void(FakeImageWriterClient*)> client_creation_callback_;
#endif
};
// Base class for unit tests that manages creating image and device files.
class ImageWriterUnitTestBase : public testing::Test {
protected:
ImageWriterUnitTestBase();
~ImageWriterUnitTestBase() override;
void SetUp() override;
void TearDown() override;
ImageWriterTestUtils test_utils_;
content::BrowserTaskEnvironment task_environment_;
};
bool GetTestDataDirectory(base::FilePath* path);
} // namespace image_writer
} // namespace extensions
#endif // CHROME_BROWSER_EXTENSIONS_API_IMAGE_WRITER_PRIVATE_TEST_UTILS_H_
|