From bafd6c7afb134b78673b4d8ff680082cb3d5a805 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Mon, 9 Jul 2018 15:08:50 -0700 Subject: updater: Let read_file() return Value::Type::STRING. It used to return a Value blob to be consumed by sha1_check() (which has been deprecated). Currently there's no other generic updater function that works with BLOB Values. This CL changes read_file() to return a string Value to make it more useful (e.g. allowing equality check). Test: Run recovery_component_test and recovery_unit_test on marlin. Change-Id: Iba986ba649030112babefe898f26aa9ffe69eeb7 --- tests/component/updater_test.cpp | 23 +++++++++++++++++++++++ updater/install.cpp | 10 +++++----- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/tests/component/updater_test.cpp b/tests/component/updater_test.cpp index 91e5cc1aa..9fcf17f13 100644 --- a/tests/component/updater_test.cpp +++ b/tests/component/updater_test.cpp @@ -51,6 +51,8 @@ #include "updater/install.h" #include "updater/updater.h" +using namespace std::string_literals; + using PackageEntries = std::unordered_map; static constexpr size_t kTransferListHeaderLines = 4; @@ -366,6 +368,27 @@ TEST_F(UpdaterTest, package_extract_file) { CloseArchive(handle); } +TEST_F(UpdaterTest, read_file) { + // read_file() expects one argument. + expect(nullptr, "read_file()", kArgsParsingFailure); + expect(nullptr, "read_file(\"arg1\", \"arg2\")", kArgsParsingFailure); + + // Write some value to file and read back. + TemporaryFile temp_file; + std::string script("write_value(\"foo\", \""s + temp_file.path + "\");"); + expect("t", script, kNoCause); + + script = "read_file(\""s + temp_file.path + "\") == \"foo\""; + expect("t", script, kNoCause); + + script = "read_file(\""s + temp_file.path + "\") == \"bar\""; + expect("", script, kNoCause); + + // It should fail gracefully when read fails. + script = "read_file(\"/doesntexist\")"; + expect("", script, kNoCause); +} + TEST_F(UpdaterTest, write_value) { // write_value() expects two arguments. expect(nullptr, "write_value()", kArgsParsingFailure); diff --git a/updater/install.cpp b/updater/install.cpp index 02a6fe7c5..d0be955a7 100644 --- a/updater/install.cpp +++ b/updater/install.cpp @@ -742,7 +742,7 @@ Value* RunProgramFn(const char* name, State* state, const std::vector>& argv) { if (argv.size() != 1) { return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size()); @@ -754,13 +754,13 @@ Value* ReadFileFn(const char* name, State* state, const std::vector