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
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stdint.h>
#include <string.h>
#include <string>
#include <vector>
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/test/task_environment.h"
#include "chrome/utility/image_writer/error_message_strings.h"
#include "chrome/utility/image_writer/image_writer.h"
#include "chrome/utility/image_writer/image_writer_handler.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace image_writer {
using testing::_;
using testing::AnyNumber;
using testing::AtLeast;
using testing::Lt;
namespace {
const int64_t kTestFileSize = 1 << 15; // 32 kB
const uint8_t kTestPattern = 0x55;
class ImageWriterUtilityTest : public testing::Test {
protected:
void SetUp() override {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
ASSERT_TRUE(
base::CreateTemporaryFileInDir(temp_dir_.GetPath(), &image_path_));
ASSERT_TRUE(
base::CreateTemporaryFileInDir(temp_dir_.GetPath(), &device_path_));
}
void TearDown() override {}
void FillFile(const base::FilePath& path, uint8_t pattern) {
std::vector<uint8_t> buffer(kTestFileSize, pattern);
ASSERT_TRUE(base::WriteFile(path, buffer));
}
void FillDefault(const base::FilePath& path) { FillFile(path, kTestPattern); }
base::FilePath image_path_;
base::FilePath device_path_;
private:
base::test::SingleThreadTaskEnvironment task_environment_;
base::ScopedTempDir temp_dir_;
};
class MockHandler : public ImageWriterHandler {
public:
MOCK_METHOD1(SendProgress, void(int64_t));
MOCK_METHOD1(SendFailed, void(const std::string& message));
MOCK_METHOD0(SendSucceeded, void());
};
// This Mock has the additional feature that it will start verification when
// the write completes.
class VerifyingHandler : public MockHandler {
public:
VerifyingHandler() : image_writer_(nullptr), verified_(false) {}
void SendSucceeded() override {
MockHandler::SendSucceeded();
if (!verified_) {
image_writer_->Verify();
verified_ = true;
}
}
raw_ptr<ImageWriter> image_writer_;
private:
bool verified_;
};
} // namespace
TEST_F(ImageWriterUtilityTest, Getters) {
MockHandler mock_handler;
ImageWriter image_writer(&mock_handler, image_path_, device_path_);
EXPECT_EQ(image_path_, image_writer.GetImagePath());
EXPECT_EQ(device_path_, image_writer.GetDevicePath());
}
TEST_F(ImageWriterUtilityTest, WriteSuccessful) {
MockHandler mock_handler;
ImageWriter image_writer(&mock_handler, image_path_, device_path_);
EXPECT_CALL(mock_handler, SendProgress(_)).Times(AnyNumber());
EXPECT_CALL(mock_handler, SendProgress(kTestFileSize)).Times(1);
EXPECT_CALL(mock_handler, SendProgress(0)).Times(1);
EXPECT_CALL(mock_handler, SendSucceeded()).Times(1);
EXPECT_CALL(mock_handler, SendFailed(_)).Times(0);
FillDefault(image_path_);
image_writer.Write();
base::RunLoop().RunUntilIdle();
}
TEST_F(ImageWriterUtilityTest, WriteInvalidImageFile) {
MockHandler mock_handler;
ImageWriter image_writer(&mock_handler, image_path_, device_path_);
EXPECT_CALL(mock_handler, SendProgress(_)).Times(0);
EXPECT_CALL(mock_handler, SendSucceeded()).Times(0);
EXPECT_CALL(mock_handler, SendFailed(error::kOpenImage)).Times(1);
ASSERT_TRUE(base::DeleteFile(image_path_));
image_writer.Write();
base::RunLoop().RunUntilIdle();
}
TEST_F(ImageWriterUtilityTest, WriteInvalidDeviceFile) {
MockHandler mock_handler;
ImageWriter image_writer(&mock_handler, image_path_, device_path_);
EXPECT_CALL(mock_handler, SendProgress(_)).Times(0);
EXPECT_CALL(mock_handler, SendSucceeded()).Times(0);
EXPECT_CALL(mock_handler, SendFailed(error::kOpenDevice)).Times(1);
ASSERT_TRUE(base::DeleteFile(device_path_));
image_writer.Write();
base::RunLoop().RunUntilIdle();
}
TEST_F(ImageWriterUtilityTest, VerifySuccessful) {
MockHandler mock_handler;
ImageWriter image_writer(&mock_handler, image_path_, device_path_);
EXPECT_CALL(mock_handler, SendProgress(_)).Times(AnyNumber());
EXPECT_CALL(mock_handler, SendProgress(kTestFileSize)).Times(1);
EXPECT_CALL(mock_handler, SendProgress(0)).Times(1);
EXPECT_CALL(mock_handler, SendSucceeded()).Times(1);
EXPECT_CALL(mock_handler, SendFailed(_)).Times(0);
FillDefault(image_path_);
FillDefault(device_path_);
image_writer.Verify();
base::RunLoop().RunUntilIdle();
}
TEST_F(ImageWriterUtilityTest, VerifyInvalidImageFile) {
MockHandler mock_handler;
ImageWriter image_writer(&mock_handler, image_path_, device_path_);
EXPECT_CALL(mock_handler, SendProgress(_)).Times(0);
EXPECT_CALL(mock_handler, SendSucceeded()).Times(0);
EXPECT_CALL(mock_handler, SendFailed(error::kOpenImage)).Times(1);
ASSERT_TRUE(base::DeleteFile(image_path_));
image_writer.Verify();
base::RunLoop().RunUntilIdle();
}
TEST_F(ImageWriterUtilityTest, VerifyInvalidDeviceFile) {
MockHandler mock_handler;
ImageWriter image_writer(&mock_handler, image_path_, device_path_);
EXPECT_CALL(mock_handler, SendProgress(_)).Times(0);
EXPECT_CALL(mock_handler, SendSucceeded()).Times(0);
EXPECT_CALL(mock_handler, SendFailed(error::kOpenDevice)).Times(1);
ASSERT_TRUE(base::DeleteFile(device_path_));
image_writer.Verify();
base::RunLoop().RunUntilIdle();
}
TEST_F(ImageWriterUtilityTest, VerifyEmptyDevice) {
MockHandler mock_handler;
ImageWriter image_writer(&mock_handler, image_path_, device_path_);
EXPECT_CALL(mock_handler, SendProgress(_)).Times(AnyNumber());
EXPECT_CALL(mock_handler, SendSucceeded()).Times(0);
EXPECT_CALL(mock_handler, SendFailed(error::kReadDevice)).Times(1);
FillDefault(image_path_);
image_writer.Verify();
base::RunLoop().RunUntilIdle();
}
TEST_F(ImageWriterUtilityTest, VerifyFailed) {
MockHandler mock_handler;
ImageWriter image_writer(&mock_handler, image_path_, device_path_);
EXPECT_CALL(mock_handler, SendProgress(_)).Times(AnyNumber());
EXPECT_CALL(mock_handler, SendSucceeded()).Times(0);
EXPECT_CALL(mock_handler, SendFailed(error::kVerificationFailed)).Times(1);
FillDefault(image_path_);
FillFile(device_path_, ~kTestPattern);
image_writer.Verify();
base::RunLoop().RunUntilIdle();
}
TEST_F(ImageWriterUtilityTest, WriteWithVerifySuccessful) {
VerifyingHandler mock_handler;
ImageWriter image_writer(&mock_handler, image_path_, device_path_);
mock_handler.image_writer_ = &image_writer;
EXPECT_CALL(mock_handler, SendProgress(_)).Times(AnyNumber());
EXPECT_CALL(mock_handler, SendProgress(kTestFileSize)).Times(2);
EXPECT_CALL(mock_handler, SendProgress(0)).Times(2);
EXPECT_CALL(mock_handler, SendSucceeded()).Times(2);
EXPECT_CALL(mock_handler, SendFailed(_)).Times(0);
FillDefault(image_path_);
image_writer.Write();
base::RunLoop().RunUntilIdle();
}
} // namespace image_writer
|