summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTao Bao <tbao@google.com>2018-07-10 21:41:24 +0200
committerandroid-build-merger <android-build-merger@google.com>2018-07-10 21:41:24 +0200
commitee1246e2bfa966af67561f88043878975bc56b9a (patch)
tree38f70bda3279d9be673785cc6ddc9cedf3126155
parentMerge "edify: Rename parse_string to ParseString and let it take std::string." am: 6b18eb706e (diff)
parentMerge "updater: Let read_file() return Value::Type::STRING." (diff)
downloadandroid_bootable_recovery-ee1246e2bfa966af67561f88043878975bc56b9a.tar
android_bootable_recovery-ee1246e2bfa966af67561f88043878975bc56b9a.tar.gz
android_bootable_recovery-ee1246e2bfa966af67561f88043878975bc56b9a.tar.bz2
android_bootable_recovery-ee1246e2bfa966af67561f88043878975bc56b9a.tar.lz
android_bootable_recovery-ee1246e2bfa966af67561f88043878975bc56b9a.tar.xz
android_bootable_recovery-ee1246e2bfa966af67561f88043878975bc56b9a.tar.zst
android_bootable_recovery-ee1246e2bfa966af67561f88043878975bc56b9a.zip
-rw-r--r--tests/component/updater_test.cpp23
-rw-r--r--updater/install.cpp10
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<std::string, std::string>;
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 75e805224..ba7bd55b0 100644
--- a/updater/install.cpp
+++ b/updater/install.cpp
@@ -740,7 +740,7 @@ Value* RunProgramFn(const char* name, State* state, const std::vector<std::uniqu
}
// read_file(filename)
-// Reads a local file 'filename' and returns its contents as a Value string.
+// Reads a local file 'filename' and returns its contents as a string Value.
Value* ReadFileFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
if (argv.size() != 1) {
return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
@@ -752,13 +752,13 @@ Value* ReadFileFn(const char* name, State* state, const std::vector<std::unique_
}
const std::string& filename = args[0];
- FileContents fc;
- if (LoadFileContents(filename.c_str(), &fc) == 0) {
- return new Value(Value::Type::BLOB, std::string(fc.data.cbegin(), fc.data.cend()));
+ std::string contents;
+ if (android::base::ReadFileToString(filename, &contents)) {
+ return new Value(Value::Type::STRING, std::move(contents));
}
// Leave it to caller to handle the failure.
- LOG(ERROR) << name << ": Failed to read " << filename;
+ PLOG(ERROR) << name << ": Failed to read " << filename;
return StringValue("");
}