Mocking get_uint_var()

Changing implementation to have mock fastboot driver return a the
sparse_limit rather than modifying the variable inside of flashing plan

Test: fastboot_test
Change-Id: I850ccd5bd09b6a8479ccc8cf7bf1d227abb87e3a
This commit is contained in:
Daniel Zheng 2023-07-10 16:45:54 -07:00
parent b95f100640
commit a0d2401d23
4 changed files with 22 additions and 14 deletions

View file

@ -76,6 +76,7 @@
#include "constants.h"
#include "diagnose_usb.h"
#include "fastboot_driver.h"
#include "fastboot_driver_interface.h"
#include "fs.h"
#include "storage.h"
#include "super_flash_helper.h"
@ -996,7 +997,7 @@ static std::vector<SparsePtr> load_sparse_files(int fd, int64_t max_size) {
return resparse_file(s.get(), max_size);
}
static uint64_t get_uint_var(const char* var_name) {
static uint64_t get_uint_var(const char* var_name, fastboot::IFastBootDriver* fb) {
std::string value_str;
if (fb->GetVar(var_name, &value_str) != fastboot::SUCCESS || value_str.empty()) {
verbose("target didn't report %s", var_name);
@ -1021,7 +1022,7 @@ int64_t get_sparse_limit(int64_t size, const FlashingPlan* fp) {
// Unlimited, so see what the target device's limit is.
// TODO: shouldn't we apply this limit even if you've used -S?
if (target_sparse_limit == -1) {
target_sparse_limit = static_cast<int64_t>(get_uint_var("max-download-size"));
target_sparse_limit = static_cast<int64_t>(get_uint_var("max-download-size", fp->fb));
}
if (target_sparse_limit > 0) {
limit = target_sparse_limit;
@ -1420,8 +1421,9 @@ bool is_retrofit_device() {
// Fetch a partition from the device to a given fd. This is a wrapper over FetchToFd to fetch
// the full image.
static uint64_t fetch_partition(const std::string& partition, borrowed_fd fd) {
uint64_t fetch_size = get_uint_var(FB_VAR_MAX_FETCH_SIZE);
static uint64_t fetch_partition(const std::string& partition, borrowed_fd fd,
fastboot::IFastBootDriver* fb) {
uint64_t fetch_size = get_uint_var(FB_VAR_MAX_FETCH_SIZE, fb);
if (fetch_size == 0) {
die("Unable to get %s. Device does not support fetch command.", FB_VAR_MAX_FETCH_SIZE);
}
@ -1443,17 +1445,18 @@ static uint64_t fetch_partition(const std::string& partition, borrowed_fd fd) {
}
static void do_fetch(const std::string& partition, const std::string& slot_override,
const std::string& outfile) {
const std::string& outfile, fastboot::IFastBootDriver* fb) {
unique_fd fd(TEMP_FAILURE_RETRY(
open(outfile.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_BINARY, 0644)));
auto fetch = std::bind(fetch_partition, _1, borrowed_fd(fd));
auto fetch = std::bind(fetch_partition, _1, borrowed_fd(fd), fb);
do_for_partitions(partition, slot_override, fetch, false /* force slot */);
}
// Return immediately if not flashing a vendor boot image. If flashing a vendor boot image,
// repack vendor_boot image with an updated ramdisk. After execution, buf is set
// to the new image to flash, and return value is the real partition name to flash.
static std::string repack_ramdisk(const char* pname, struct fastboot_buffer* buf) {
static std::string repack_ramdisk(const char* pname, struct fastboot_buffer* buf,
fastboot::IFastBootDriver* fb) {
std::string_view pname_sv{pname};
if (!android::base::StartsWith(pname_sv, "vendor_boot:") &&
@ -1471,7 +1474,7 @@ static std::string repack_ramdisk(const char* pname, struct fastboot_buffer* buf
std::string ramdisk(pname_sv.substr(pname_sv.find(':') + 1));
unique_fd vendor_boot(make_temporary_fd("vendor boot repack"));
uint64_t vendor_boot_size = fetch_partition(partition, vendor_boot);
uint64_t vendor_boot_size = fetch_partition(partition, vendor_boot, fb);
auto repack_res = replace_vendor_ramdisk(vendor_boot, vendor_boot_size, ramdisk, buf->fd,
static_cast<uint64_t>(buf->sz));
if (!repack_res.ok()) {
@ -1508,7 +1511,7 @@ void do_flash(const char* pname, const char* fname, const bool apply_vbmeta,
if (is_logical(pname)) {
fb->ResizePartition(pname, std::to_string(buf.image_size));
}
std::string flash_pname = repack_ramdisk(pname, &buf);
std::string flash_pname = repack_ramdisk(pname, &buf, fp->fb);
flash_buf(flash_pname, &buf, apply_vbmeta);
}
@ -2576,7 +2579,7 @@ int FastBootTool::Main(int argc, char* argv[]) {
} else if (command == FB_CMD_FETCH) {
std::string partition = next_arg(&args);
std::string outfile = next_arg(&args);
do_fetch(partition, fp->slot_override, outfile);
do_fetch(partition, fp->slot_override, outfile, fp->fb);
} else {
syntax_error("unknown command %s", command.c_str());
}

View file

@ -105,7 +105,7 @@ class FastBootDriver : public IFastBootDriver {
std::vector<std::string>* info = nullptr);
RetCode FetchToFd(const std::string& partition, android::base::borrowed_fd fd,
int64_t offset = -1, int64_t size = -1, std::string* response = nullptr,
std::vector<std::string>* info = nullptr);
std::vector<std::string>* info = nullptr) override;
/* HIGHER LEVEL COMMANDS -- Composed of the commands above */
RetCode FlashPartition(const std::string& partition, const std::vector<char>& data);

View file

@ -45,6 +45,10 @@ class IFastBootDriver {
std::vector<std::string>* info = nullptr) = 0;
RetCode virtual GetVar(const std::string& key, std::string* val,
std::vector<std::string>* info = nullptr) = 0;
RetCode virtual FetchToFd(const std::string& partition, android::base::borrowed_fd fd,
int64_t offset = -1, int64_t size = -1,
std::string* response = nullptr,
std::vector<std::string>* info = nullptr) = 0;
RetCode virtual Download(const std::string& name, android::base::borrowed_fd fd, size_t size,
std::string* response = nullptr,
std::vector<std::string>* info = nullptr) = 0;

View file

@ -28,15 +28,16 @@ class MockFastbootDriver : public IFastBootDriver {
MOCK_METHOD(RetCode, Reboot, (std::string*, std::vector<std::string>*), (override));
MOCK_METHOD(RetCode, RebootTo, (std::string, std::string*, std::vector<std::string>*),
(override));
MOCK_METHOD(RetCode, GetVar, (const std::string&, std::string*, std::vector<std::string>*),
(override));
MOCK_METHOD(RetCode, FetchToFd,
(const std::string&, android::base::borrowed_fd, int64_t offset, int64_t size,
std::string*, std::vector<std::string>*),
(override));
MOCK_METHOD(RetCode, Download,
(const std::string&, android::base::borrowed_fd, size_t, std::string*,
std::vector<std::string>*),
(override));
MOCK_METHOD(RetCode, RawCommand,
(const std::string&, const std::string&, std::string*, std::vector<std::string>*,
int*),