From c9e869bd9e2c3f663af2cb8750d2a7003ff1620c Mon Sep 17 00:00:00 2001 From: Daniel Zheng Date: Thu, 30 Mar 2023 14:59:48 -0700 Subject: [PATCH] Adding fastboot_driver interface + mock Adding mock fastboot_driver to be used for testing Test: tested fastboot_test Bug: 194686221 Change-Id: I6948310d44b75c0f389cbfd194747649aaccb0b8 --- fastboot/fastboot.h | 1 + fastboot/fastboot_driver.h | 32 ++++++--------- fastboot/fastboot_driver_interface.h | 59 ++++++++++++++++++++++++++++ fastboot/fastboot_driver_mock.h | 51 ++++++++++++++++++++++++ 4 files changed, 124 insertions(+), 19 deletions(-) create mode 100644 fastboot/fastboot_driver_interface.h create mode 100644 fastboot/fastboot_driver_mock.h diff --git a/fastboot/fastboot.h b/fastboot/fastboot.h index b5033ffd4..2d79e42a0 100644 --- a/fastboot/fastboot.h +++ b/fastboot/fastboot.h @@ -29,6 +29,7 @@ #include #include "fastboot_driver.h" +#include "fastboot_driver_interface.h" #include "filesystem.h" #include "super_flash_helper.h" #include "util.h" diff --git a/fastboot/fastboot_driver.h b/fastboot/fastboot_driver.h index 64e3c2b86..3d6c7b017 100644 --- a/fastboot/fastboot_driver.h +++ b/fastboot/fastboot_driver.h @@ -40,21 +40,13 @@ #include #include "constants.h" +#include "fastboot_driver_interface.h" #include "transport.h" class Transport; namespace fastboot { -enum RetCode : int { - SUCCESS = 0, - BAD_ARG, - IO_ERROR, - BAD_DEV_RESP, - DEVICE_FAIL, - TIMEOUT, -}; - struct DriverCallbacks { std::function prolog = [](const std::string&) {}; std::function epilog = [](int) {}; @@ -62,7 +54,7 @@ struct DriverCallbacks { std::function text = [](const std::string&) {}; }; -class FastBootDriver { +class FastBootDriver : public IFastBootDriver { friend class FastBootTest; public: @@ -77,9 +69,10 @@ class FastBootDriver { RetCode Boot(std::string* response = nullptr, std::vector* info = nullptr); RetCode Continue(std::string* response = nullptr, std::vector* info = nullptr); RetCode CreatePartition(const std::string& partition, const std::string& size); - RetCode DeletePartition(const std::string& partition); + RetCode DeletePartition(const std::string& partition) override; RetCode Download(const std::string& name, android::base::borrowed_fd fd, size_t size, - std::string* response = nullptr, std::vector* info = nullptr); + std::string* response = nullptr, + std::vector* info = nullptr) override; RetCode Download(android::base::borrowed_fd fd, size_t size, std::string* response = nullptr, std::vector* info = nullptr); RetCode Download(const std::string& name, const std::vector& buf, @@ -92,16 +85,17 @@ class FastBootDriver { RetCode Download(sparse_file* s, bool use_crc = false, std::string* response = nullptr, std::vector* info = nullptr); RetCode Erase(const std::string& partition, std::string* response = nullptr, - std::vector* info = nullptr); + std::vector* info = nullptr) override; RetCode Flash(const std::string& partition, std::string* response = nullptr, std::vector* info = nullptr); RetCode GetVar(const std::string& key, std::string* val, - std::vector* info = nullptr); + std::vector* info = nullptr) override; RetCode GetVarAll(std::vector* response); - RetCode Reboot(std::string* response = nullptr, std::vector* info = nullptr); + RetCode Reboot(std::string* response = nullptr, + std::vector* info = nullptr) override; RetCode RebootTo(std::string target, std::string* response = nullptr, - std::vector* info = nullptr); - RetCode ResizePartition(const std::string& partition, const std::string& size); + std::vector* info = nullptr) override; + RetCode ResizePartition(const std::string& partition, const std::string& size) override; RetCode SetActive(const std::string& slot, std::string* response = nullptr, std::vector* info = nullptr); RetCode Upload(const std::string& outfile, std::string* response = nullptr, @@ -115,7 +109,7 @@ class FastBootDriver { /* HIGHER LEVEL COMMANDS -- Composed of the commands above */ RetCode FlashPartition(const std::string& partition, const std::vector& data); RetCode FlashPartition(const std::string& partition, android::base::borrowed_fd fd, - uint32_t sz); + uint32_t sz) override; RetCode FlashPartition(const std::string& partition, sparse_file* s, uint32_t sz, size_t current, size_t total); @@ -127,7 +121,7 @@ class FastBootDriver { void SetInfoCallback(std::function info); static const std::string RCString(RetCode rc); std::string Error(); - RetCode WaitForDisconnect(); + RetCode WaitForDisconnect() override; // Note: set_transport will return the previous transport. Transport* set_transport(Transport* transport); diff --git a/fastboot/fastboot_driver_interface.h b/fastboot/fastboot_driver_interface.h new file mode 100644 index 000000000..795938fb4 --- /dev/null +++ b/fastboot/fastboot_driver_interface.h @@ -0,0 +1,59 @@ +// +// Copyright (C) 2023 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +#pragma once + +#include + +#include "android-base/unique_fd.h" + +class Transport; + +namespace fastboot { + +enum RetCode : int { + SUCCESS = 0, + BAD_ARG, + IO_ERROR, + BAD_DEV_RESP, + DEVICE_FAIL, + TIMEOUT, +}; + +class IFastBootDriver { + public: + RetCode virtual FlashPartition(const std::string& partition, android::base::borrowed_fd fd, + uint32_t sz) = 0; + RetCode virtual DeletePartition(const std::string& partition) = 0; + RetCode virtual WaitForDisconnect() = 0; + RetCode virtual Reboot(std::string* response = nullptr, + std::vector* info = nullptr) = 0; + + RetCode virtual RebootTo(std::string target, std::string* response = nullptr, + std::vector* info = nullptr) = 0; + RetCode virtual GetVar(const std::string& key, std::string* val, + std::vector* info = nullptr) = 0; + RetCode virtual Download(const std::string& name, android::base::borrowed_fd fd, size_t size, + std::string* response = nullptr, + std::vector* info = nullptr) = 0; + RetCode virtual RawCommand(const std::string& cmd, const std::string& message, + std::string* response = nullptr, + std::vector* info = nullptr, int* dsize = nullptr) = 0; + RetCode virtual ResizePartition(const std::string& partition, const std::string& size) = 0; + RetCode virtual Erase(const std::string& partition, std::string* response = nullptr, + std::vector* info = nullptr) = 0; + virtual ~IFastBootDriver() = default; +}; +} // namespace fastboot \ No newline at end of file diff --git a/fastboot/fastboot_driver_mock.h b/fastboot/fastboot_driver_mock.h new file mode 100644 index 000000000..62a570849 --- /dev/null +++ b/fastboot/fastboot_driver_mock.h @@ -0,0 +1,51 @@ +// +// Copyright (C) 2023 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +#pragma once + +#include +#include "fastboot_driver_interface.h" + +namespace fastboot { + +class MockFastbootDriver : public IFastBootDriver { + public: + MOCK_METHOD(RetCode, FlashPartition, + (const std::string& partition, android::base::borrowed_fd fd, uint32_t sz), + (override)); + MOCK_METHOD(RetCode, DeletePartition, (const std::string&), (override)); + MOCK_METHOD(RetCode, Reboot, (std::string*, std::vector*), (override)); + MOCK_METHOD(RetCode, RebootTo, (std::string, std::string*, std::vector*), + (override)); + + MOCK_METHOD(RetCode, GetVar, (const std::string&, std::string*, std::vector*), + (override)); + + MOCK_METHOD(RetCode, Download, + (const std::string&, android::base::borrowed_fd, size_t, std::string*, + std::vector*), + (override)); + + MOCK_METHOD(RetCode, RawCommand, + (const std::string&, const std::string&, std::string*, std::vector*, + int*), + (override)); + MOCK_METHOD(RetCode, ResizePartition, (const std::string&, const std::string&), (override)); + MOCK_METHOD(RetCode, Erase, (const std::string&, std::string*, std::vector*), + (override)); + MOCK_METHOD(RetCode, WaitForDisconnect, (), (override)); +}; + +} // namespace fastboot \ No newline at end of file