blob: c611c22912bee22b42c55cde1a0a356b748bd6a5 [file] [log] [blame]
Tao Bao0c7839a2016-10-10 15:48:37 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Tao Bao9aa7ab52017-01-05 17:27:19 -080017#include <stdio.h>
Tianjie Xu107a34f2017-06-29 17:04:21 -070018#include <stdlib.h>
Tao Bao89929022016-11-08 20:51:31 -080019#include <sys/stat.h>
20#include <sys/types.h>
21#include <unistd.h>
22
Tianjie Xu107a34f2017-06-29 17:04:21 -070023#include <algorithm>
Tianjie Xuc4447322017-03-06 14:44:59 -080024#include <memory>
Tao Bao0c7839a2016-10-10 15:48:37 -070025#include <string>
Tianjie Xu5450c842017-10-18 13:15:21 -070026#include <unordered_map>
Tianjie Xu56ebe622017-03-16 00:48:21 -070027#include <vector>
Tao Bao0c7839a2016-10-10 15:48:37 -070028
Tao Bao51d516e2016-11-03 14:49:01 -070029#include <android-base/file.h>
Tao Baobc4a6d52018-05-23 00:12:21 -070030#include <android-base/logging.h>
31#include <android-base/parseint.h>
Tao Bao0c7839a2016-10-10 15:48:37 -070032#include <android-base/properties.h>
Tao Bao9aa7ab52017-01-05 17:27:19 -080033#include <android-base/stringprintf.h>
34#include <android-base/strings.h>
Tao Bao51d516e2016-11-03 14:49:01 -070035#include <android-base/test_utils.h>
Tao Baobedf5fc2016-11-18 12:01:26 -080036#include <bootloader_message/bootloader_message.h>
Tianjie Xu107a34f2017-06-29 17:04:21 -070037#include <brotli/encode.h>
Alex Deymofa188262017-10-10 17:56:17 +020038#include <bsdiff/bsdiff.h>
Tao Bao0c7839a2016-10-10 15:48:37 -070039#include <gtest/gtest.h>
Tianjie Xu69ffa152018-08-01 16:40:00 -070040#include <verity/hash_tree_builder.h>
Tao Baoef0eb3b2016-11-14 21:29:52 -080041#include <ziparchive/zip_archive.h>
Tianjie Xu56ebe622017-03-16 00:48:21 -070042#include <ziparchive/zip_writer.h>
Tao Bao0c7839a2016-10-10 15:48:37 -070043
Tao Bao5609bc82018-06-20 00:30:48 -070044#include "applypatch/applypatch.h"
Tao Baoef0eb3b2016-11-14 21:29:52 -080045#include "common/test_constants.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070046#include "edify/expr.h"
Tao Bao1fc5bf32017-10-06 07:43:41 -070047#include "otautil/error_code.h"
Tao Bao641fa972018-04-25 18:59:40 -070048#include "otautil/paths.h"
Tao Bao09e468f2017-09-29 14:39:33 -070049#include "otautil/print_sha1.h"
Tao Bao17054c02018-05-03 22:41:23 -070050#include "otautil/sysutil.h"
Tao Bao91a649a2018-05-21 16:05:56 -070051#include "private/commands.h"
Tianjie Xu56ebe622017-03-16 00:48:21 -070052#include "updater/blockimg.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070053#include "updater/install.h"
Tao Baoef0eb3b2016-11-14 21:29:52 -080054#include "updater/updater.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070055
Tao Baobafd6c72018-07-09 15:08:50 -070056using namespace std::string_literals;
57
Tao Baobc4a6d52018-05-23 00:12:21 -070058using PackageEntries = std::unordered_map<std::string, std::string>;
59
Tao Baobc4a6d52018-05-23 00:12:21 -070060struct selabel_handle* sehandle = nullptr;
Tao Bao0c7839a2016-10-10 15:48:37 -070061
Tao Baod8d514f2018-07-09 13:32:28 -070062static void expect(const char* expected, const std::string& expr_str, CauseCode cause_code,
Tao Baoef0eb3b2016-11-14 21:29:52 -080063 UpdaterInfo* info = nullptr) {
Tianjie Xuc4447322017-03-06 14:44:59 -080064 std::unique_ptr<Expr> e;
Tao Baoef0eb3b2016-11-14 21:29:52 -080065 int error_count = 0;
Tao Baod8d514f2018-07-09 13:32:28 -070066 ASSERT_EQ(0, ParseString(expr_str, &e, &error_count));
Tao Baoef0eb3b2016-11-14 21:29:52 -080067 ASSERT_EQ(0, error_count);
Tao Bao0c7839a2016-10-10 15:48:37 -070068
Tao Baoef0eb3b2016-11-14 21:29:52 -080069 State state(expr_str, info);
Tao Bao0c7839a2016-10-10 15:48:37 -070070
Tao Baoef0eb3b2016-11-14 21:29:52 -080071 std::string result;
72 bool status = Evaluate(&state, e, &result);
Tao Bao0c7839a2016-10-10 15:48:37 -070073
Tao Baoef0eb3b2016-11-14 21:29:52 -080074 if (expected == nullptr) {
75 ASSERT_FALSE(status);
76 } else {
Tao Bao0b58e9a2018-07-06 15:01:05 -070077 ASSERT_TRUE(status) << "Evaluate() finished with error message: " << state.errmsg;
Tao Baoef0eb3b2016-11-14 21:29:52 -080078 ASSERT_STREQ(expected, result.c_str());
79 }
Tao Bao0c7839a2016-10-10 15:48:37 -070080
Tao Baoef0eb3b2016-11-14 21:29:52 -080081 // Error code is set in updater/updater.cpp only, by parsing State.errmsg.
82 ASSERT_EQ(kNoError, state.error_code);
Tao Bao361342c2016-02-08 11:15:50 -080083
Tao Baoef0eb3b2016-11-14 21:29:52 -080084 // Cause code should always be available.
85 ASSERT_EQ(cause_code, state.cause_code);
Tao Bao0c7839a2016-10-10 15:48:37 -070086}
87
Tao Baobc4a6d52018-05-23 00:12:21 -070088static void BuildUpdatePackage(const PackageEntries& entries, int fd) {
Tianjie Xu5450c842017-10-18 13:15:21 -070089 FILE* zip_file_ptr = fdopen(fd, "wb");
90 ZipWriter zip_writer(zip_file_ptr);
91
92 for (const auto& entry : entries) {
Tao Baobc4a6d52018-05-23 00:12:21 -070093 // All the entries are written as STORED.
Tianjie Xu5450c842017-10-18 13:15:21 -070094 ASSERT_EQ(0, zip_writer.StartEntry(entry.first.c_str(), 0));
95 if (!entry.second.empty()) {
96 ASSERT_EQ(0, zip_writer.WriteBytes(entry.second.data(), entry.second.size()));
97 }
98 ASSERT_EQ(0, zip_writer.FinishEntry());
99 }
100
101 ASSERT_EQ(0, zip_writer.Finish());
102 ASSERT_EQ(0, fclose(zip_file_ptr));
103}
104
Tao Baobc4a6d52018-05-23 00:12:21 -0700105static void RunBlockImageUpdate(bool is_verify, const PackageEntries& entries,
Tao Baoffede3e2018-06-07 09:56:19 -0700106 const std::string& image_file, const std::string& result,
107 CauseCode cause_code = kNoCause) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700108 CHECK(entries.find("transfer_list") != entries.end());
109
110 // Build the update package.
111 TemporaryFile zip_file;
112 BuildUpdatePackage(entries, zip_file.release());
113
114 MemMapping map;
115 ASSERT_TRUE(map.MapFile(zip_file.path));
116 ZipArchiveHandle handle;
117 ASSERT_EQ(0, OpenArchiveFromMemory(map.addr, map.length, zip_file.path, &handle));
118
119 // Set up the handler, command_pipe, patch offset & length.
120 UpdaterInfo updater_info;
121 updater_info.package_zip = handle;
122 TemporaryFile temp_pipe;
123 updater_info.cmd_pipe = fdopen(temp_pipe.release(), "wbe");
124 updater_info.package_zip_addr = map.addr;
125 updater_info.package_zip_len = map.length;
126
127 std::string new_data = entries.find("new_data.br") != entries.end() ? "new_data.br" : "new_data";
128 std::string script = is_verify ? "block_image_verify" : "block_image_update";
129 script += R"((")" + image_file + R"(", package_extract_file("transfer_list"), ")" + new_data +
130 R"(", "patch_data"))";
Tao Baod8d514f2018-07-09 13:32:28 -0700131 expect(result.c_str(), script, cause_code, &updater_info);
Tao Baobc4a6d52018-05-23 00:12:21 -0700132
133 ASSERT_EQ(0, fclose(updater_info.cmd_pipe));
134 CloseArchive(handle);
135}
136
Tianjie Xu56ebe622017-03-16 00:48:21 -0700137static std::string get_sha1(const std::string& content) {
138 uint8_t digest[SHA_DIGEST_LENGTH];
139 SHA1(reinterpret_cast<const uint8_t*>(content.c_str()), content.size(), digest);
140 return print_sha1(digest);
141}
142
Tao Bao0b58e9a2018-07-06 15:01:05 -0700143static Value* BlobToString(const char* name, State* state,
144 const std::vector<std::unique_ptr<Expr>>& argv) {
145 if (argv.size() != 1) {
146 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
147 }
148
149 std::vector<std::unique_ptr<Value>> args;
150 if (!ReadValueArgs(state, argv, &args)) {
151 return nullptr;
152 }
153
Tao Bao511d7592018-06-19 15:56:49 -0700154 if (args[0]->type != Value::Type::BLOB) {
Tao Bao0b58e9a2018-07-06 15:01:05 -0700155 return ErrorAbort(state, kArgsParsingFailure, "%s() expects a BLOB argument", name);
156 }
157
Tao Bao511d7592018-06-19 15:56:49 -0700158 args[0]->type = Value::Type::STRING;
Tao Bao0b58e9a2018-07-06 15:01:05 -0700159 return args[0].release();
160}
161
Tao Bao0c7839a2016-10-10 15:48:37 -0700162class UpdaterTest : public ::testing::Test {
Tianjie Xu56ebe622017-03-16 00:48:21 -0700163 protected:
Tao Baobc4a6d52018-05-23 00:12:21 -0700164 void SetUp() override {
Tianjie Xu56ebe622017-03-16 00:48:21 -0700165 RegisterBuiltins();
166 RegisterInstallFunctions();
167 RegisterBlockImageFunctions();
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800168
Tao Bao0b58e9a2018-07-06 15:01:05 -0700169 RegisterFunction("blob_to_string", BlobToString);
170
Tao Bao7064aa22018-05-24 21:43:50 -0700171 // Each test is run in a separate process (isolated mode). Shared temporary files won't cause
172 // conflicts.
Tao Bao641fa972018-04-25 18:59:40 -0700173 Paths::Get().set_cache_temp_source(temp_saved_source_.path);
Tao Bao7064aa22018-05-24 21:43:50 -0700174 Paths::Get().set_last_command_file(temp_last_command_.path);
Tao Bao641fa972018-04-25 18:59:40 -0700175 Paths::Get().set_stash_directory_base(temp_stash_base_.path);
Tao Baobc4a6d52018-05-23 00:12:21 -0700176
Tao Bao91a649a2018-05-21 16:05:56 -0700177 // Enable a special command "abort" to simulate interruption.
178 Command::abort_allowed_ = true;
179
Tao Bao7064aa22018-05-24 21:43:50 -0700180 last_command_file_ = temp_last_command_.path;
Tao Baobc4a6d52018-05-23 00:12:21 -0700181 image_file_ = image_temp_file_.path;
182 }
183
184 void TearDown() override {
Tao Bao7064aa22018-05-24 21:43:50 -0700185 // Clean up the last_command_file if any.
186 ASSERT_TRUE(android::base::RemoveFileIfExists(last_command_file_));
187
Tao Baobc4a6d52018-05-23 00:12:21 -0700188 // Clear partition updated marker if any.
189 std::string updated_marker{ temp_stash_base_.path };
190 updated_marker += "/" + get_sha1(image_temp_file_.path) + ".UPDATED";
191 ASSERT_TRUE(android::base::RemoveFileIfExists(updated_marker));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700192 }
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800193
194 TemporaryFile temp_saved_source_;
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800195 TemporaryDir temp_stash_base_;
Tao Bao7064aa22018-05-24 21:43:50 -0700196 std::string last_command_file_;
Tao Baobc4a6d52018-05-23 00:12:21 -0700197 std::string image_file_;
198
199 private:
Tao Bao7064aa22018-05-24 21:43:50 -0700200 TemporaryFile temp_last_command_;
Tao Baobc4a6d52018-05-23 00:12:21 -0700201 TemporaryFile image_temp_file_;
Tao Bao0c7839a2016-10-10 15:48:37 -0700202};
203
204TEST_F(UpdaterTest, getprop) {
205 expect(android::base::GetProperty("ro.product.device", "").c_str(),
206 "getprop(\"ro.product.device\")",
Tao Bao361342c2016-02-08 11:15:50 -0800207 kNoCause);
Tao Bao0c7839a2016-10-10 15:48:37 -0700208
209 expect(android::base::GetProperty("ro.build.fingerprint", "").c_str(),
210 "getprop(\"ro.build.fingerprint\")",
Tao Bao361342c2016-02-08 11:15:50 -0800211 kNoCause);
Tao Bao0c7839a2016-10-10 15:48:37 -0700212
213 // getprop() accepts only one parameter.
Tao Bao361342c2016-02-08 11:15:50 -0800214 expect(nullptr, "getprop()", kArgsParsingFailure);
215 expect(nullptr, "getprop(\"arg1\", \"arg2\")", kArgsParsingFailure);
216}
217
Tao Bao5609bc82018-06-20 00:30:48 -0700218TEST_F(UpdaterTest, patch_partition_check) {
219 // Zero argument is not valid.
220 expect(nullptr, "patch_partition_check()", kArgsParsingFailure);
Tao Baodb56eb02017-03-23 06:34:20 -0700221
Tao Bao5609bc82018-06-20 00:30:48 -0700222 std::string source_file = from_testdata_base("boot.img");
223 std::string source_content;
224 ASSERT_TRUE(android::base::ReadFileToString(source_file, &source_content));
225 size_t source_size = source_content.size();
226 std::string source_hash = get_sha1(source_content);
227 Partition source(source_file, source_size, source_hash);
Tao Baodb56eb02017-03-23 06:34:20 -0700228
Tao Bao5609bc82018-06-20 00:30:48 -0700229 std::string target_file = from_testdata_base("recovery.img");
230 std::string target_content;
231 ASSERT_TRUE(android::base::ReadFileToString(target_file, &target_content));
232 size_t target_size = target_content.size();
233 std::string target_hash = get_sha1(target_content);
234 Partition target(target_file, target_size, target_hash);
Tao Baodb56eb02017-03-23 06:34:20 -0700235
Tao Bao5609bc82018-06-20 00:30:48 -0700236 // One argument is not valid.
237 expect(nullptr, "patch_partition_check(\"" + source.ToString() + "\")", kArgsParsingFailure);
238 expect(nullptr, "patch_partition_check(\"" + target.ToString() + "\")", kArgsParsingFailure);
239
240 // Both of the source and target have the desired checksum.
241 std::string cmd =
242 "patch_partition_check(\"" + source.ToString() + "\", \"" + target.ToString() + "\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700243 expect("t", cmd, kNoCause);
Tao Baodb56eb02017-03-23 06:34:20 -0700244
Tao Bao5609bc82018-06-20 00:30:48 -0700245 // Only source partition has the desired checksum.
246 Partition bad_target(target_file, target_size - 1, target_hash);
247 cmd = "patch_partition_check(\"" + source.ToString() + "\", \"" + bad_target.ToString() + "\")";
248 expect("t", cmd, kNoCause);
249
250 // Only target partition has the desired checksum.
251 Partition bad_source(source_file, source_size + 1, source_hash);
252 cmd = "patch_partition_check(\"" + bad_source.ToString() + "\", \"" + target.ToString() + "\")";
253 expect("t", cmd, kNoCause);
254
255 // Neither of the source or target has the desired checksum.
256 cmd =
257 "patch_partition_check(\"" + bad_source.ToString() + "\", \"" + bad_target.ToString() + "\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700258 expect("", cmd, kNoCause);
Tao Baodb56eb02017-03-23 06:34:20 -0700259}
260
Tao Bao51d516e2016-11-03 14:49:01 -0700261TEST_F(UpdaterTest, file_getprop) {
262 // file_getprop() expects two arguments.
263 expect(nullptr, "file_getprop()", kArgsParsingFailure);
264 expect(nullptr, "file_getprop(\"arg1\")", kArgsParsingFailure);
265 expect(nullptr, "file_getprop(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
266
267 // File doesn't exist.
Tianjie Xu22f11202018-08-27 10:50:31 -0700268 expect(nullptr, "file_getprop(\"/doesntexist\", \"key1\")", kFreadFailure);
Tao Bao51d516e2016-11-03 14:49:01 -0700269
270 // Reject too large files (current limit = 65536).
271 TemporaryFile temp_file1;
272 std::string buffer(65540, '\0');
273 ASSERT_TRUE(android::base::WriteStringToFile(buffer, temp_file1.path));
274
275 // Read some keys.
276 TemporaryFile temp_file2;
277 std::string content("ro.product.name=tardis\n"
278 "# comment\n\n\n"
279 "ro.product.model\n"
280 "ro.product.board = magic \n");
281 ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file2.path));
282
283 std::string script1("file_getprop(\"" + std::string(temp_file2.path) +
284 "\", \"ro.product.name\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700285 expect("tardis", script1, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700286
287 std::string script2("file_getprop(\"" + std::string(temp_file2.path) +
288 "\", \"ro.product.board\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700289 expect("magic", script2, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700290
291 // No match.
292 std::string script3("file_getprop(\"" + std::string(temp_file2.path) +
293 "\", \"ro.product.wrong\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700294 expect("", script3, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700295
296 std::string script4("file_getprop(\"" + std::string(temp_file2.path) +
297 "\", \"ro.product.name=\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700298 expect("", script4, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700299
300 std::string script5("file_getprop(\"" + std::string(temp_file2.path) +
301 "\", \"ro.product.nam\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700302 expect("", script5, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700303
304 std::string script6("file_getprop(\"" + std::string(temp_file2.path) +
305 "\", \"ro.product.model\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700306 expect("", script6, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700307}
Tao Bao0831d0b2016-11-03 23:25:04 -0700308
Tao Baoef0eb3b2016-11-14 21:29:52 -0800309// TODO: Test extracting to block device.
310TEST_F(UpdaterTest, package_extract_file) {
311 // package_extract_file expects 1 or 2 arguments.
312 expect(nullptr, "package_extract_file()", kArgsParsingFailure);
313 expect(nullptr, "package_extract_file(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
314
315 std::string zip_path = from_testdata_base("ziptest_valid.zip");
316 ZipArchiveHandle handle;
317 ASSERT_EQ(0, OpenArchive(zip_path.c_str(), &handle));
318
319 // Need to set up the ziphandle.
320 UpdaterInfo updater_info;
321 updater_info.package_zip = handle;
322
323 // Two-argument version.
324 TemporaryFile temp_file1;
325 std::string script("package_extract_file(\"a.txt\", \"" + std::string(temp_file1.path) + "\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700326 expect("t", script, kNoCause, &updater_info);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800327
328 // Verify the extracted entry.
329 std::string data;
330 ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data));
331 ASSERT_EQ(kATxtContents, data);
332
333 // Now extract another entry to the same location, which should overwrite.
334 script = "package_extract_file(\"b.txt\", \"" + std::string(temp_file1.path) + "\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700335 expect("t", script, kNoCause, &updater_info);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800336
337 ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data));
338 ASSERT_EQ(kBTxtContents, data);
339
340 // Missing zip entry. The two-argument version doesn't abort.
341 script = "package_extract_file(\"doesntexist\", \"" + std::string(temp_file1.path) + "\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700342 expect("", script, kNoCause, &updater_info);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800343
344 // Extract to /dev/full should fail.
345 script = "package_extract_file(\"a.txt\", \"/dev/full\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700346 expect("", script, kNoCause, &updater_info);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800347
Tao Bao0b58e9a2018-07-06 15:01:05 -0700348 // One-argument version. package_extract_file() gives a VAL_BLOB, which needs to be converted to
349 // VAL_STRING for equality test.
350 script = "blob_to_string(package_extract_file(\"a.txt\")) == \"" + kATxtContents + "\"";
Tao Baod8d514f2018-07-09 13:32:28 -0700351 expect("t", script, kNoCause, &updater_info);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800352
Tao Bao0b58e9a2018-07-06 15:01:05 -0700353 script = "blob_to_string(package_extract_file(\"b.txt\")) == \"" + kBTxtContents + "\"";
Tao Baod8d514f2018-07-09 13:32:28 -0700354 expect("t", script, kNoCause, &updater_info);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800355
356 // Missing entry. The one-argument version aborts the evaluation.
357 script = "package_extract_file(\"doesntexist\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700358 expect(nullptr, script, kPackageExtractFileFailure, &updater_info);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800359
360 CloseArchive(handle);
361}
Tao Baod0f30882016-11-03 23:52:01 -0700362
Tao Baobafd6c72018-07-09 15:08:50 -0700363TEST_F(UpdaterTest, read_file) {
364 // read_file() expects one argument.
365 expect(nullptr, "read_file()", kArgsParsingFailure);
366 expect(nullptr, "read_file(\"arg1\", \"arg2\")", kArgsParsingFailure);
367
368 // Write some value to file and read back.
369 TemporaryFile temp_file;
370 std::string script("write_value(\"foo\", \""s + temp_file.path + "\");");
371 expect("t", script, kNoCause);
372
373 script = "read_file(\""s + temp_file.path + "\") == \"foo\"";
374 expect("t", script, kNoCause);
375
376 script = "read_file(\""s + temp_file.path + "\") == \"bar\"";
377 expect("", script, kNoCause);
378
379 // It should fail gracefully when read fails.
380 script = "read_file(\"/doesntexist\")";
381 expect("", script, kNoCause);
382}
383
Tianjie Xu69ffa152018-08-01 16:40:00 -0700384TEST_F(UpdaterTest, compute_hash_tree_smoke) {
385 std::string data;
386 for (unsigned char i = 0; i < 128; i++) {
387 data += std::string(4096, i);
388 }
389 // Appends an additional block for verity data.
390 data += std::string(4096, 0);
391 ASSERT_EQ(129 * 4096, data.size());
392 ASSERT_TRUE(android::base::WriteStringToFile(data, image_file_));
393
394 std::string salt = "aee087a5be3b982978c923f566a94613496b417f2af592639bc80d141e34dfe7";
395 std::string expected_root_hash =
396 "7e0a8d8747f54384014ab996f5b2dc4eb7ff00c630eede7134c9e3f05c0dd8ca";
397 // hash_tree_ranges, source_ranges, hash_algorithm, salt_hex, root_hash
398 std::vector<std::string> tokens{ "compute_hash_tree", "2,128,129", "2,0,128", "sha256", salt,
399 expected_root_hash };
400 std::string hash_tree_command = android::base::Join(tokens, " ");
401
402 std::vector<std::string> transfer_list{
403 "4", "2", "0", "2", hash_tree_command,
404 };
405
406 PackageEntries entries{
407 { "new_data", "" },
408 { "patch_data", "" },
409 { "transfer_list", android::base::Join(transfer_list, "\n") },
410 };
411
412 RunBlockImageUpdate(false, entries, image_file_, "t");
413
414 std::string updated;
415 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated));
416 ASSERT_EQ(129 * 4096, updated.size());
417 ASSERT_EQ(data.substr(0, 128 * 4096), updated.substr(0, 128 * 4096));
418
419 // Computes the SHA256 of the salt + hash_tree_data and expects the result to match with the
420 // root_hash.
421 std::vector<unsigned char> salt_bytes;
422 ASSERT_TRUE(HashTreeBuilder::ParseBytesArrayFromString(salt, &salt_bytes));
423 std::vector<unsigned char> hash_tree = std::move(salt_bytes);
424 hash_tree.insert(hash_tree.end(), updated.begin() + 128 * 4096, updated.end());
425
426 std::vector<unsigned char> digest(SHA256_DIGEST_LENGTH);
427 SHA256(hash_tree.data(), hash_tree.size(), digest.data());
428 ASSERT_EQ(expected_root_hash, HashTreeBuilder::BytesArrayToString(digest));
429}
430
431TEST_F(UpdaterTest, compute_hash_tree_root_mismatch) {
432 std::string data;
433 for (size_t i = 0; i < 128; i++) {
434 data += std::string(4096, i);
435 }
436 // Appends an additional block for verity data.
437 data += std::string(4096, 0);
438 ASSERT_EQ(129 * 4096, data.size());
439 // Corrupts one bit
440 data[4096] = 'A';
441 ASSERT_TRUE(android::base::WriteStringToFile(data, image_file_));
442
443 std::string salt = "aee087a5be3b982978c923f566a94613496b417f2af592639bc80d141e34dfe7";
444 std::string expected_root_hash =
445 "7e0a8d8747f54384014ab996f5b2dc4eb7ff00c630eede7134c9e3f05c0dd8ca";
446 // hash_tree_ranges, source_ranges, hash_algorithm, salt_hex, root_hash
447 std::vector<std::string> tokens{ "compute_hash_tree", "2,128,129", "2,0,128", "sha256", salt,
448 expected_root_hash };
449 std::string hash_tree_command = android::base::Join(tokens, " ");
450
451 std::vector<std::string> transfer_list{
452 "4", "2", "0", "2", hash_tree_command,
453 };
454
455 PackageEntries entries{
456 { "new_data", "" },
457 { "patch_data", "" },
458 { "transfer_list", android::base::Join(transfer_list, "\n") },
459 };
460
461 RunBlockImageUpdate(false, entries, image_file_, "", kHashTreeComputationFailure);
462}
463
Tao Baod0f30882016-11-03 23:52:01 -0700464TEST_F(UpdaterTest, write_value) {
465 // write_value() expects two arguments.
466 expect(nullptr, "write_value()", kArgsParsingFailure);
467 expect(nullptr, "write_value(\"arg1\")", kArgsParsingFailure);
468 expect(nullptr, "write_value(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
469
470 // filename cannot be empty.
471 expect(nullptr, "write_value(\"value\", \"\")", kArgsParsingFailure);
472
473 // Write some value to file.
474 TemporaryFile temp_file;
475 std::string value = "magicvalue";
476 std::string script("write_value(\"" + value + "\", \"" + std::string(temp_file.path) + "\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700477 expect("t", script, kNoCause);
Tao Baod0f30882016-11-03 23:52:01 -0700478
479 // Verify the content.
480 std::string content;
481 ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content));
482 ASSERT_EQ(value, content);
483
484 // Allow writing empty string.
485 script = "write_value(\"\", \"" + std::string(temp_file.path) + "\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700486 expect("t", script, kNoCause);
Tao Baod0f30882016-11-03 23:52:01 -0700487
488 // Verify the content.
489 ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content));
490 ASSERT_EQ("", content);
491
492 // It should fail gracefully when write fails.
493 script = "write_value(\"value\", \"/proc/0/file1\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700494 expect("", script, kNoCause);
Tao Baod0f30882016-11-03 23:52:01 -0700495}
Tao Baobedf5fc2016-11-18 12:01:26 -0800496
497TEST_F(UpdaterTest, get_stage) {
498 // get_stage() expects one argument.
499 expect(nullptr, "get_stage()", kArgsParsingFailure);
500 expect(nullptr, "get_stage(\"arg1\", \"arg2\")", kArgsParsingFailure);
501 expect(nullptr, "get_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
502
503 // Set up a local file as BCB.
504 TemporaryFile tf;
505 std::string temp_file(tf.path);
506 bootloader_message boot;
507 strlcpy(boot.stage, "2/3", sizeof(boot.stage));
508 std::string err;
509 ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err));
510
511 // Can read the stage value.
512 std::string script("get_stage(\"" + temp_file + "\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700513 expect("2/3", script, kNoCause);
Tao Baobedf5fc2016-11-18 12:01:26 -0800514
515 // Bad BCB path.
516 script = "get_stage(\"doesntexist\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700517 expect("", script, kNoCause);
Tao Baobedf5fc2016-11-18 12:01:26 -0800518}
519
520TEST_F(UpdaterTest, set_stage) {
521 // set_stage() expects two arguments.
522 expect(nullptr, "set_stage()", kArgsParsingFailure);
523 expect(nullptr, "set_stage(\"arg1\")", kArgsParsingFailure);
524 expect(nullptr, "set_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
525
526 // Set up a local file as BCB.
527 TemporaryFile tf;
528 std::string temp_file(tf.path);
529 bootloader_message boot;
530 strlcpy(boot.command, "command", sizeof(boot.command));
531 strlcpy(boot.stage, "2/3", sizeof(boot.stage));
532 std::string err;
533 ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err));
534
535 // Write with set_stage().
536 std::string script("set_stage(\"" + temp_file + "\", \"1/3\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700537 expect(tf.path, script, kNoCause);
Tao Baobedf5fc2016-11-18 12:01:26 -0800538
539 // Verify.
540 bootloader_message boot_verify;
541 ASSERT_TRUE(read_bootloader_message_from(&boot_verify, temp_file, &err));
542
543 // Stage should be updated, with command part untouched.
544 ASSERT_STREQ("1/3", boot_verify.stage);
545 ASSERT_STREQ(boot.command, boot_verify.command);
546
547 // Bad BCB path.
548 script = "set_stage(\"doesntexist\", \"1/3\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700549 expect("", script, kNoCause);
Tao Baobedf5fc2016-11-18 12:01:26 -0800550
551 script = "set_stage(\"/dev/full\", \"1/3\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700552 expect("", script, kNoCause);
Tao Baobedf5fc2016-11-18 12:01:26 -0800553}
Tao Bao9aa7ab52017-01-05 17:27:19 -0800554
555TEST_F(UpdaterTest, set_progress) {
556 // set_progress() expects one argument.
557 expect(nullptr, "set_progress()", kArgsParsingFailure);
558 expect(nullptr, "set_progress(\"arg1\", \"arg2\")", kArgsParsingFailure);
559
560 // Invalid progress argument.
561 expect(nullptr, "set_progress(\"arg1\")", kArgsParsingFailure);
562 expect(nullptr, "set_progress(\"3x+5\")", kArgsParsingFailure);
563 expect(nullptr, "set_progress(\".3.5\")", kArgsParsingFailure);
564
565 TemporaryFile tf;
566 UpdaterInfo updater_info;
Tianjie Xu79327ac2017-09-08 17:09:10 -0700567 updater_info.cmd_pipe = fdopen(tf.release(), "w");
Tao Bao9aa7ab52017-01-05 17:27:19 -0800568 expect(".52", "set_progress(\".52\")", kNoCause, &updater_info);
569 fflush(updater_info.cmd_pipe);
570
571 std::string cmd;
572 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd));
573 ASSERT_EQ(android::base::StringPrintf("set_progress %f\n", .52), cmd);
574 // recovery-updater protocol expects 2 tokens ("set_progress <frac>").
575 ASSERT_EQ(2U, android::base::Split(cmd, " ").size());
Tianjie Xu79327ac2017-09-08 17:09:10 -0700576 ASSERT_EQ(0, fclose(updater_info.cmd_pipe));
Tao Bao9aa7ab52017-01-05 17:27:19 -0800577}
578
579TEST_F(UpdaterTest, show_progress) {
580 // show_progress() expects two arguments.
581 expect(nullptr, "show_progress()", kArgsParsingFailure);
582 expect(nullptr, "show_progress(\"arg1\")", kArgsParsingFailure);
583 expect(nullptr, "show_progress(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
584
585 // Invalid progress arguments.
586 expect(nullptr, "show_progress(\"arg1\", \"arg2\")", kArgsParsingFailure);
587 expect(nullptr, "show_progress(\"3x+5\", \"10\")", kArgsParsingFailure);
588 expect(nullptr, "show_progress(\".3\", \"5a\")", kArgsParsingFailure);
589
590 TemporaryFile tf;
591 UpdaterInfo updater_info;
Tianjie Xu79327ac2017-09-08 17:09:10 -0700592 updater_info.cmd_pipe = fdopen(tf.release(), "w");
Tao Bao9aa7ab52017-01-05 17:27:19 -0800593 expect(".52", "show_progress(\".52\", \"10\")", kNoCause, &updater_info);
594 fflush(updater_info.cmd_pipe);
595
596 std::string cmd;
597 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd));
598 ASSERT_EQ(android::base::StringPrintf("progress %f %d\n", .52, 10), cmd);
599 // recovery-updater protocol expects 3 tokens ("progress <frac> <secs>").
600 ASSERT_EQ(3U, android::base::Split(cmd, " ").size());
Tianjie Xu79327ac2017-09-08 17:09:10 -0700601 ASSERT_EQ(0, fclose(updater_info.cmd_pipe));
Tao Bao9aa7ab52017-01-05 17:27:19 -0800602}
Tianjie Xu56ebe622017-03-16 00:48:21 -0700603
Tao Baoffede3e2018-06-07 09:56:19 -0700604TEST_F(UpdaterTest, block_image_update_parsing_error) {
605 std::vector<std::string> transfer_list{
606 // clang-format off
607 "4",
608 "2",
609 "0",
610 // clang-format on
611 };
612
613 PackageEntries entries{
614 { "new_data", "" },
615 { "patch_data", "" },
616 { "transfer_list", android::base::Join(transfer_list, '\n') },
617 };
618
619 RunBlockImageUpdate(false, entries, image_file_, "", kArgsParsingFailure);
620}
621
Tianjie Xu5450c842017-10-18 13:15:21 -0700622TEST_F(UpdaterTest, block_image_update_patch_data) {
Tianjie Xu56ebe622017-03-16 00:48:21 -0700623 std::string src_content = std::string(4096, 'a') + std::string(4096, 'c');
624 std::string tgt_content = std::string(4096, 'b') + std::string(4096, 'd');
Tianjie Xu5450c842017-10-18 13:15:21 -0700625
626 // Generate the patch data.
Tianjie Xu56ebe622017-03-16 00:48:21 -0700627 TemporaryFile patch_file;
Tao Baobc4a6d52018-05-23 00:12:21 -0700628 ASSERT_EQ(0,
629 bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(src_content.data()), src_content.size(),
630 reinterpret_cast<const uint8_t*>(tgt_content.data()), tgt_content.size(),
631 patch_file.path, nullptr));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700632 std::string patch_content;
633 ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch_content));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700634
Tianjie Xu5450c842017-10-18 13:15:21 -0700635 // Create the transfer list that contains a bsdiff.
Tianjie Xu56ebe622017-03-16 00:48:21 -0700636 std::string src_hash = get_sha1(src_content);
637 std::string tgt_hash = get_sha1(tgt_content);
Tao Baobc4a6d52018-05-23 00:12:21 -0700638 std::vector<std::string> transfer_list{
639 // clang-format off
Tianjie Xu56ebe622017-03-16 00:48:21 -0700640 "4",
641 "2",
642 "0",
643 "2",
644 "stash " + src_hash + " 2,0,2",
645 android::base::StringPrintf("bsdiff 0 %zu %s %s 2,0,2 2 - %s:2,0,2", patch_content.size(),
646 src_hash.c_str(), tgt_hash.c_str(), src_hash.c_str()),
647 "free " + src_hash,
Tao Baobc4a6d52018-05-23 00:12:21 -0700648 // clang-format on
Tianjie Xu56ebe622017-03-16 00:48:21 -0700649 };
Tianjie Xu56ebe622017-03-16 00:48:21 -0700650
Tao Baobc4a6d52018-05-23 00:12:21 -0700651 PackageEntries entries{
Tianjie Xu5450c842017-10-18 13:15:21 -0700652 { "new_data", "" },
653 { "patch_data", patch_content },
654 { "transfer_list", android::base::Join(transfer_list, '\n') },
Tianjie Xu56ebe622017-03-16 00:48:21 -0700655 };
Tianjie Xu5450c842017-10-18 13:15:21 -0700656
Tao Baobc4a6d52018-05-23 00:12:21 -0700657 ASSERT_TRUE(android::base::WriteStringToFile(src_content, image_file_));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700658
Tao Baobc4a6d52018-05-23 00:12:21 -0700659 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu56ebe622017-03-16 00:48:21 -0700660
Tianjie Xu56ebe622017-03-16 00:48:21 -0700661 // The update_file should be patched correctly.
662 std::string updated_content;
Tao Baobc4a6d52018-05-23 00:12:21 -0700663 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_content));
664 ASSERT_EQ(tgt_content, updated_content);
Tianjie Xu5450c842017-10-18 13:15:21 -0700665}
666
Tao Baoa2cff952018-11-02 15:44:07 -0700667TEST_F(UpdaterTest, block_image_update_patch_underrun) {
668 std::string src_content = std::string(4096, 'a') + std::string(4096, 'c');
669 std::string tgt_content = std::string(4096, 'b') + std::string(4096, 'd');
670
671 // Generate the patch data. We intentionally provide one-byte short target to trigger the underrun
672 // path.
673 TemporaryFile patch_file;
674 ASSERT_EQ(0,
675 bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(src_content.data()), src_content.size(),
676 reinterpret_cast<const uint8_t*>(tgt_content.data()),
677 tgt_content.size() - 1, patch_file.path, nullptr));
678 std::string patch_content;
679 ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch_content));
680
681 // Create the transfer list that contains a bsdiff.
682 std::string src_hash = get_sha1(src_content);
683 std::string tgt_hash = get_sha1(tgt_content);
684 std::vector<std::string> transfer_list{
685 // clang-format off
686 "4",
687 "2",
688 "0",
689 "2",
690 "stash " + src_hash + " 2,0,2",
691 android::base::StringPrintf("bsdiff 0 %zu %s %s 2,0,2 2 - %s:2,0,2", patch_content.size(),
692 src_hash.c_str(), tgt_hash.c_str(), src_hash.c_str()),
693 "free " + src_hash,
694 // clang-format on
695 };
696
697 PackageEntries entries{
698 { "new_data", "" },
699 { "patch_data", patch_content },
700 { "transfer_list", android::base::Join(transfer_list, '\n') },
701 };
702
703 ASSERT_TRUE(android::base::WriteStringToFile(src_content, image_file_));
704
705 // The update should fail due to underrun.
706 RunBlockImageUpdate(false, entries, image_file_, "", kPatchApplicationFailure);
707}
708
Tianjie Xu5450c842017-10-18 13:15:21 -0700709TEST_F(UpdaterTest, block_image_update_fail) {
710 std::string src_content(4096 * 2, 'e');
711 std::string src_hash = get_sha1(src_content);
712 // Stash and free some blocks, then fail the update intentionally.
Tao Baobc4a6d52018-05-23 00:12:21 -0700713 std::vector<std::string> transfer_list{
714 // clang-format off
715 "4",
716 "2",
717 "0",
718 "2",
719 "stash " + src_hash + " 2,0,2",
720 "free " + src_hash,
Tao Bao91a649a2018-05-21 16:05:56 -0700721 "abort",
Tao Baobc4a6d52018-05-23 00:12:21 -0700722 // clang-format on
Tianjie Xu5450c842017-10-18 13:15:21 -0700723 };
724
725 // Add a new data of 10 bytes to test the deadlock.
Tao Baobc4a6d52018-05-23 00:12:21 -0700726 PackageEntries entries{
Tianjie Xu5450c842017-10-18 13:15:21 -0700727 { "new_data", std::string(10, 0) },
728 { "patch_data", "" },
729 { "transfer_list", android::base::Join(transfer_list, '\n') },
730 };
731
Tao Baobc4a6d52018-05-23 00:12:21 -0700732 ASSERT_TRUE(android::base::WriteStringToFile(src_content, image_file_));
Tianjie Xu5450c842017-10-18 13:15:21 -0700733
Tao Baobc4a6d52018-05-23 00:12:21 -0700734 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu5450c842017-10-18 13:15:21 -0700735
Tianjie Xu56ebe622017-03-16 00:48:21 -0700736 // Updater generates the stash name based on the input file name.
Tao Baobc4a6d52018-05-23 00:12:21 -0700737 std::string name_digest = get_sha1(image_file_);
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800738 std::string stash_base = std::string(temp_stash_base_.path) + "/" + name_digest;
Tianjie Xu56ebe622017-03-16 00:48:21 -0700739 ASSERT_EQ(0, access(stash_base.c_str(), F_OK));
Tao Baobc4a6d52018-05-23 00:12:21 -0700740 // Expect the stashed blocks to be freed.
Tianjie Xu5450c842017-10-18 13:15:21 -0700741 ASSERT_EQ(-1, access((stash_base + src_hash).c_str(), F_OK));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700742 ASSERT_EQ(0, rmdir(stash_base.c_str()));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700743}
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700744
Tianjie Xu5450c842017-10-18 13:15:21 -0700745TEST_F(UpdaterTest, new_data_over_write) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700746 std::vector<std::string> transfer_list{
747 // clang-format off
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700748 "4",
749 "1",
750 "0",
751 "0",
752 "new 2,0,1",
Tao Baobc4a6d52018-05-23 00:12:21 -0700753 // clang-format on
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700754 };
Tianjie Xu5450c842017-10-18 13:15:21 -0700755
Tao Baobc4a6d52018-05-23 00:12:21 -0700756 // Write 4096 + 100 bytes of new data.
757 PackageEntries entries{
758 { "new_data", std::string(4196, 0) },
Tianjie Xu5450c842017-10-18 13:15:21 -0700759 { "patch_data", "" },
760 { "transfer_list", android::base::Join(transfer_list, '\n') },
761 };
762
Tao Baobc4a6d52018-05-23 00:12:21 -0700763 RunBlockImageUpdate(false, entries, image_file_, "t");
764}
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700765
Tao Baobc4a6d52018-05-23 00:12:21 -0700766TEST_F(UpdaterTest, new_data_short_write) {
767 std::vector<std::string> transfer_list{
768 // clang-format off
769 "4",
770 "1",
771 "0",
772 "0",
773 "new 2,0,1",
774 // clang-format on
775 };
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700776
Tao Baobc4a6d52018-05-23 00:12:21 -0700777 PackageEntries entries{
778 { "patch_data", "" },
779 { "transfer_list", android::base::Join(transfer_list, '\n') },
780 };
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700781
782 // Updater should report the failure gracefully rather than stuck in deadlock.
Tao Baobc4a6d52018-05-23 00:12:21 -0700783 entries["new_data"] = "";
784 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700785
Tao Baobc4a6d52018-05-23 00:12:21 -0700786 entries["new_data"] = std::string(10, 'a');
787 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700788
789 // Expect to write 1 block of new data successfully.
Tao Baobc4a6d52018-05-23 00:12:21 -0700790 entries["new_data"] = std::string(4096, 'a');
791 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu107a34f2017-06-29 17:04:21 -0700792}
793
794TEST_F(UpdaterTest, brotli_new_data) {
Tianjie Xu107a34f2017-06-29 17:04:21 -0700795 auto generator = []() { return rand() % 128; };
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700796 // Generate 100 blocks of random data.
Tianjie Xu107a34f2017-06-29 17:04:21 -0700797 std::string brotli_new_data;
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700798 brotli_new_data.reserve(4096 * 100);
799 generate_n(back_inserter(brotli_new_data), 4096 * 100, generator);
Tianjie Xu107a34f2017-06-29 17:04:21 -0700800
801 size_t encoded_size = BrotliEncoderMaxCompressedSize(brotli_new_data.size());
Tianjie Xu5450c842017-10-18 13:15:21 -0700802 std::string encoded_data(encoded_size, 0);
Tianjie Xu107a34f2017-06-29 17:04:21 -0700803 ASSERT_TRUE(BrotliEncoderCompress(
804 BROTLI_DEFAULT_QUALITY, BROTLI_DEFAULT_WINDOW, BROTLI_DEFAULT_MODE, brotli_new_data.size(),
Tianjie Xu5450c842017-10-18 13:15:21 -0700805 reinterpret_cast<const uint8_t*>(brotli_new_data.data()), &encoded_size,
806 reinterpret_cast<uint8_t*>(const_cast<char*>(encoded_data.data()))));
807 encoded_data.resize(encoded_size);
Tianjie Xu107a34f2017-06-29 17:04:21 -0700808
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700809 // Write a few small chunks of new data, then a large chunk, and finally a few small chunks.
810 // This helps us to catch potential short writes.
Tianjie Xu107a34f2017-06-29 17:04:21 -0700811 std::vector<std::string> transfer_list = {
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700812 "4",
813 "100",
814 "0",
815 "0",
816 "new 2,0,1",
817 "new 2,1,2",
818 "new 4,2,50,50,97",
819 "new 2,97,98",
820 "new 2,98,99",
821 "new 2,99,100",
Tianjie Xu107a34f2017-06-29 17:04:21 -0700822 };
Tianjie Xu5450c842017-10-18 13:15:21 -0700823
Tao Baobc4a6d52018-05-23 00:12:21 -0700824 PackageEntries entries{
825 { "new_data.br", std::move(encoded_data) },
Tianjie Xu5450c842017-10-18 13:15:21 -0700826 { "patch_data", "" },
827 { "transfer_list", android::base::Join(transfer_list, '\n') },
828 };
829
Tao Baobc4a6d52018-05-23 00:12:21 -0700830 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu107a34f2017-06-29 17:04:21 -0700831
832 std::string updated_content;
Tao Baobc4a6d52018-05-23 00:12:21 -0700833 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_content));
Tianjie Xu107a34f2017-06-29 17:04:21 -0700834 ASSERT_EQ(brotli_new_data, updated_content);
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700835}
Tianjie Xu284752e2017-12-05 11:04:17 -0800836
837TEST_F(UpdaterTest, last_command_update) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700838 std::string block1(4096, '1');
839 std::string block2(4096, '2');
840 std::string block3(4096, '3');
Tianjie Xu284752e2017-12-05 11:04:17 -0800841 std::string block1_hash = get_sha1(block1);
842 std::string block2_hash = get_sha1(block2);
843 std::string block3_hash = get_sha1(block3);
844
845 // Compose the transfer list to fail the first update.
Tao Baobc4a6d52018-05-23 00:12:21 -0700846 std::vector<std::string> transfer_list_fail{
847 // clang-format off
Tianjie Xu284752e2017-12-05 11:04:17 -0800848 "4",
849 "2",
850 "0",
851 "2",
852 "stash " + block1_hash + " 2,0,1",
853 "move " + block1_hash + " 2,1,2 1 2,0,1",
854 "stash " + block3_hash + " 2,2,3",
Tao Bao91a649a2018-05-21 16:05:56 -0700855 "abort",
Tao Baobc4a6d52018-05-23 00:12:21 -0700856 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -0800857 };
858
859 // Mimic a resumed update with the same transfer commands.
Tao Baobc4a6d52018-05-23 00:12:21 -0700860 std::vector<std::string> transfer_list_continue{
861 // clang-format off
Tianjie Xu284752e2017-12-05 11:04:17 -0800862 "4",
863 "2",
864 "0",
865 "2",
866 "stash " + block1_hash + " 2,0,1",
867 "move " + block1_hash + " 2,1,2 1 2,0,1",
868 "stash " + block3_hash + " 2,2,3",
869 "move " + block1_hash + " 2,2,3 1 2,0,1",
Tao Baobc4a6d52018-05-23 00:12:21 -0700870 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -0800871 };
872
Tao Baobc4a6d52018-05-23 00:12:21 -0700873 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_));
874
875 PackageEntries entries{
Tianjie Xu284752e2017-12-05 11:04:17 -0800876 { "new_data", "" },
877 { "patch_data", "" },
Tao Baobc4a6d52018-05-23 00:12:21 -0700878 { "transfer_list", android::base::Join(transfer_list_fail, '\n') },
Tianjie Xu284752e2017-12-05 11:04:17 -0800879 };
880
Tao Baobc4a6d52018-05-23 00:12:21 -0700881 // "2\nstash " + block3_hash + " 2,2,3"
Tao Baof8811bb2018-06-18 10:03:52 -0700882 std::string last_command_content =
883 "2\n" + transfer_list_fail[TransferList::kTransferListHeaderLines + 2];
Tianjie Xu284752e2017-12-05 11:04:17 -0800884
Tao Baobc4a6d52018-05-23 00:12:21 -0700885 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu284752e2017-12-05 11:04:17 -0800886
887 // Expect last_command to contain the last stash command.
Tao Baobc4a6d52018-05-23 00:12:21 -0700888 std::string last_command_actual;
Tao Bao7064aa22018-05-24 21:43:50 -0700889 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
Tao Baobc4a6d52018-05-23 00:12:21 -0700890 EXPECT_EQ(last_command_content, last_command_actual);
891
Tianjie Xu284752e2017-12-05 11:04:17 -0800892 std::string updated_contents;
Tao Baobc4a6d52018-05-23 00:12:21 -0700893 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_contents));
Tianjie Xu284752e2017-12-05 11:04:17 -0800894 ASSERT_EQ(block1 + block1 + block3, updated_contents);
895
Tao Baobc4a6d52018-05-23 00:12:21 -0700896 // "Resume" the update. Expect the first 'move' to be skipped but the second 'move' to be
897 // executed. Note that we intentionally reset the image file.
898 entries["transfer_list"] = android::base::Join(transfer_list_continue, '\n');
899 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_));
900 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu284752e2017-12-05 11:04:17 -0800901
Tao Baobc4a6d52018-05-23 00:12:21 -0700902 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_contents));
903 ASSERT_EQ(block1 + block2 + block1, updated_contents);
Tianjie Xu284752e2017-12-05 11:04:17 -0800904}
905
906TEST_F(UpdaterTest, last_command_update_unresumable) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700907 std::string block1(4096, '1');
908 std::string block2(4096, '2');
Tianjie Xu284752e2017-12-05 11:04:17 -0800909 std::string block1_hash = get_sha1(block1);
910 std::string block2_hash = get_sha1(block2);
911
912 // Construct an unresumable update with source blocks mismatch.
Tao Baobc4a6d52018-05-23 00:12:21 -0700913 std::vector<std::string> transfer_list_unresumable{
914 // clang-format off
915 "4",
916 "2",
917 "0",
918 "2",
919 "stash " + block1_hash + " 2,0,1",
920 "move " + block2_hash + " 2,1,2 1 2,0,1",
921 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -0800922 };
923
Tao Baobc4a6d52018-05-23 00:12:21 -0700924 PackageEntries entries{
Tianjie Xu284752e2017-12-05 11:04:17 -0800925 { "new_data", "" },
926 { "patch_data", "" },
Tao Baobc4a6d52018-05-23 00:12:21 -0700927 { "transfer_list", android::base::Join(transfer_list_unresumable, '\n') },
Tianjie Xu284752e2017-12-05 11:04:17 -0800928 };
929
Tao Baobc4a6d52018-05-23 00:12:21 -0700930 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block1, image_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -0800931
Tao Baof8811bb2018-06-18 10:03:52 -0700932 std::string last_command_content =
933 "0\n" + transfer_list_unresumable[TransferList::kTransferListHeaderLines];
Tao Bao7064aa22018-05-24 21:43:50 -0700934 ASSERT_TRUE(android::base::WriteStringToFile(last_command_content, last_command_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -0800935
Tao Baobc4a6d52018-05-23 00:12:21 -0700936 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu284752e2017-12-05 11:04:17 -0800937
Tao Baobc4a6d52018-05-23 00:12:21 -0700938 // The last_command_file will be deleted if the update encounters an unresumable failure later.
Tao Bao7064aa22018-05-24 21:43:50 -0700939 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
Tianjie Xu284752e2017-12-05 11:04:17 -0800940}
941
942TEST_F(UpdaterTest, last_command_verify) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700943 std::string block1(4096, '1');
944 std::string block2(4096, '2');
945 std::string block3(4096, '3');
Tianjie Xu284752e2017-12-05 11:04:17 -0800946 std::string block1_hash = get_sha1(block1);
947 std::string block2_hash = get_sha1(block2);
948 std::string block3_hash = get_sha1(block3);
949
Tao Baobc4a6d52018-05-23 00:12:21 -0700950 std::vector<std::string> transfer_list_verify{
951 // clang-format off
Tianjie Xu284752e2017-12-05 11:04:17 -0800952 "4",
953 "2",
954 "0",
955 "2",
956 "stash " + block1_hash + " 2,0,1",
957 "move " + block1_hash + " 2,0,1 1 2,0,1",
958 "move " + block1_hash + " 2,1,2 1 2,0,1",
959 "stash " + block3_hash + " 2,2,3",
Tao Baobc4a6d52018-05-23 00:12:21 -0700960 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -0800961 };
962
Tao Baobc4a6d52018-05-23 00:12:21 -0700963 PackageEntries entries{
Tianjie Xu284752e2017-12-05 11:04:17 -0800964 { "new_data", "" },
965 { "patch_data", "" },
Tao Baobc4a6d52018-05-23 00:12:21 -0700966 { "transfer_list", android::base::Join(transfer_list_verify, '\n') },
Tianjie Xu284752e2017-12-05 11:04:17 -0800967 };
968
Tao Baobc4a6d52018-05-23 00:12:21 -0700969 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block1 + block3, image_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -0800970
Tao Baobc4a6d52018-05-23 00:12:21 -0700971 // Last command: "move " + block1_hash + " 2,1,2 1 2,0,1"
Tao Baof8811bb2018-06-18 10:03:52 -0700972 std::string last_command_content =
973 "2\n" + transfer_list_verify[TransferList::kTransferListHeaderLines + 2];
Tianjie Xu284752e2017-12-05 11:04:17 -0800974
Tao Baobc4a6d52018-05-23 00:12:21 -0700975 // First run: expect the verification to succeed and the last_command_file is intact.
Tao Bao7064aa22018-05-24 21:43:50 -0700976 ASSERT_TRUE(android::base::WriteStringToFile(last_command_content, last_command_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -0800977
Tao Baobc4a6d52018-05-23 00:12:21 -0700978 RunBlockImageUpdate(true, entries, image_file_, "t");
Tianjie Xu284752e2017-12-05 11:04:17 -0800979
Tao Baobc4a6d52018-05-23 00:12:21 -0700980 std::string last_command_actual;
Tao Bao7064aa22018-05-24 21:43:50 -0700981 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
Tao Baobc4a6d52018-05-23 00:12:21 -0700982 EXPECT_EQ(last_command_content, last_command_actual);
Tianjie Xu284752e2017-12-05 11:04:17 -0800983
Tao Baobc4a6d52018-05-23 00:12:21 -0700984 // Second run with a mismatching block image: expect the verification to succeed but
985 // last_command_file to be deleted; because the target blocks in the last command don't have the
986 // expected contents for the second move command.
987 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_));
988 RunBlockImageUpdate(true, entries, image_file_, "t");
Tao Bao7064aa22018-05-24 21:43:50 -0700989 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
Tianjie Xu284752e2017-12-05 11:04:17 -0800990}
Tao Baoc0299ed2018-05-24 00:16:35 -0700991
992class ResumableUpdaterTest : public testing::TestWithParam<size_t> {
993 protected:
994 void SetUp() override {
995 RegisterBuiltins();
996 RegisterInstallFunctions();
997 RegisterBlockImageFunctions();
998
999 Paths::Get().set_cache_temp_source(temp_saved_source_.path);
1000 Paths::Get().set_last_command_file(temp_last_command_.path);
1001 Paths::Get().set_stash_directory_base(temp_stash_base_.path);
1002
Tao Bao91a649a2018-05-21 16:05:56 -07001003 // Enable a special command "abort" to simulate interruption.
1004 Command::abort_allowed_ = true;
1005
Tao Baoc0299ed2018-05-24 00:16:35 -07001006 index_ = GetParam();
1007 image_file_ = image_temp_file_.path;
1008 last_command_file_ = temp_last_command_.path;
1009 }
1010
1011 void TearDown() override {
1012 // Clean up the last_command_file if any.
1013 ASSERT_TRUE(android::base::RemoveFileIfExists(last_command_file_));
1014
1015 // Clear partition updated marker if any.
1016 std::string updated_marker{ temp_stash_base_.path };
1017 updated_marker += "/" + get_sha1(image_temp_file_.path) + ".UPDATED";
1018 ASSERT_TRUE(android::base::RemoveFileIfExists(updated_marker));
1019 }
1020
1021 TemporaryFile temp_saved_source_;
1022 TemporaryDir temp_stash_base_;
1023 std::string last_command_file_;
1024 std::string image_file_;
1025 size_t index_;
1026
1027 private:
1028 TemporaryFile temp_last_command_;
1029 TemporaryFile image_temp_file_;
1030};
1031
1032static std::string g_source_image;
1033static std::string g_target_image;
1034static PackageEntries g_entries;
1035
1036static std::vector<std::string> GenerateTransferList() {
1037 std::string a(4096, 'a');
1038 std::string b(4096, 'b');
1039 std::string c(4096, 'c');
1040 std::string d(4096, 'd');
1041 std::string e(4096, 'e');
1042 std::string f(4096, 'f');
1043 std::string g(4096, 'g');
1044 std::string h(4096, 'h');
1045 std::string i(4096, 'i');
1046 std::string zero(4096, '\0');
1047
1048 std::string a_hash = get_sha1(a);
1049 std::string b_hash = get_sha1(b);
1050 std::string c_hash = get_sha1(c);
1051 std::string e_hash = get_sha1(e);
1052
1053 auto loc = [](const std::string& range_text) {
1054 std::vector<std::string> pieces = android::base::Split(range_text, "-");
1055 size_t left;
1056 size_t right;
1057 if (pieces.size() == 1) {
1058 CHECK(android::base::ParseUint(pieces[0], &left));
1059 right = left + 1;
1060 } else {
1061 CHECK_EQ(2u, pieces.size());
1062 CHECK(android::base::ParseUint(pieces[0], &left));
1063 CHECK(android::base::ParseUint(pieces[1], &right));
1064 right++;
1065 }
1066 return android::base::StringPrintf("2,%zu,%zu", left, right);
1067 };
1068
1069 // patch 1: "b d c" -> "g"
1070 TemporaryFile patch_file_bdc_g;
1071 std::string bdc = b + d + c;
1072 std::string bdc_hash = get_sha1(bdc);
1073 std::string g_hash = get_sha1(g);
1074 CHECK_EQ(0, bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(bdc.data()), bdc.size(),
1075 reinterpret_cast<const uint8_t*>(g.data()), g.size(),
1076 patch_file_bdc_g.path, nullptr));
1077 std::string patch_bdc_g;
1078 CHECK(android::base::ReadFileToString(patch_file_bdc_g.path, &patch_bdc_g));
1079
1080 // patch 2: "a b c d" -> "d c b"
1081 TemporaryFile patch_file_abcd_dcb;
1082 std::string abcd = a + b + c + d;
1083 std::string abcd_hash = get_sha1(abcd);
1084 std::string dcb = d + c + b;
1085 std::string dcb_hash = get_sha1(dcb);
1086 CHECK_EQ(0, bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(abcd.data()), abcd.size(),
1087 reinterpret_cast<const uint8_t*>(dcb.data()), dcb.size(),
1088 patch_file_abcd_dcb.path, nullptr));
1089 std::string patch_abcd_dcb;
1090 CHECK(android::base::ReadFileToString(patch_file_abcd_dcb.path, &patch_abcd_dcb));
1091
1092 std::vector<std::string> transfer_list{
1093 "4",
1094 "10", // total blocks written
1095 "2", // maximum stash entries
1096 "2", // maximum number of stashed blocks
1097
1098 // a b c d e a b c d e
1099 "stash " + b_hash + " " + loc("1"),
1100 // a b c d e a b c d e [b(1)]
1101 "stash " + c_hash + " " + loc("2"),
1102 // a b c d e a b c d e [b(1)][c(2)]
1103 "new " + loc("1-2"),
1104 // a i h d e a b c d e [b(1)][c(2)]
1105 "zero " + loc("0"),
1106 // 0 i h d e a b c d e [b(1)][c(2)]
1107
1108 // bsdiff "b d c" (from stash, 3, stash) to get g(3)
1109 android::base::StringPrintf(
1110 "bsdiff 0 %zu %s %s %s 3 %s %s %s:%s %s:%s",
1111 patch_bdc_g.size(), // patch start (0), patch length
1112 bdc_hash.c_str(), // source hash
1113 g_hash.c_str(), // target hash
1114 loc("3").c_str(), // target range
1115 loc("3").c_str(), loc("1").c_str(), // load "d" from block 3, into buffer at offset 1
1116 b_hash.c_str(), loc("0").c_str(), // load "b" from stash, into buffer at offset 0
1117 c_hash.c_str(), loc("2").c_str()), // load "c" from stash, into buffer at offset 2
1118
1119 // 0 i h g e a b c d e [b(1)][c(2)]
1120 "free " + b_hash,
1121 // 0 i h g e a b c d e [c(2)]
1122 "free " + a_hash,
1123 // 0 i h g e a b c d e
1124 "stash " + a_hash + " " + loc("5"),
1125 // 0 i h g e a b c d e [a(5)]
1126 "move " + e_hash + " " + loc("5") + " 1 " + loc("4"),
1127 // 0 i h g e e b c d e [a(5)]
1128
1129 // bsdiff "a b c d" (from stash, 6-8) to "d c b" (6-8)
1130 android::base::StringPrintf( //
1131 "bsdiff %zu %zu %s %s %s 4 %s %s %s:%s",
1132 patch_bdc_g.size(), // patch start
1133 patch_bdc_g.size() + patch_abcd_dcb.size(), // patch length
1134 abcd_hash.c_str(), // source hash
1135 dcb_hash.c_str(), // target hash
1136 loc("6-8").c_str(), // target range
1137 loc("6-8").c_str(), // load "b c d" from blocks 6-8
1138 loc("1-3").c_str(), // into buffer at offset 1-3
1139 a_hash.c_str(), // load "a" from stash
1140 loc("0").c_str()), // into buffer at offset 0
1141
1142 // 0 i h g e e d c b e [a(5)]
1143 "new " + loc("4"),
1144 // 0 i h g f e d c b e [a(5)]
1145 "move " + a_hash + " " + loc("9") + " 1 - " + a_hash + ":" + loc("0"),
1146 // 0 i h g f e d c b a [a(5)]
1147 "free " + a_hash,
1148 // 0 i h g f e d c b a
1149 };
1150
1151 std::string new_data = i + h + f;
1152 std::string patch_data = patch_bdc_g + patch_abcd_dcb;
1153
1154 g_entries = {
1155 { "new_data", new_data },
1156 { "patch_data", patch_data },
1157 };
1158 g_source_image = a + b + c + d + e + a + b + c + d + e;
1159 g_target_image = zero + i + h + g + f + e + d + c + b + a;
1160
1161 return transfer_list;
1162}
1163
1164static const std::vector<std::string> g_transfer_list = GenerateTransferList();
1165
1166INSTANTIATE_TEST_CASE_P(InterruptAfterEachCommand, ResumableUpdaterTest,
1167 ::testing::Range(static_cast<size_t>(0),
Tao Baof8811bb2018-06-18 10:03:52 -07001168 g_transfer_list.size() -
1169 TransferList::kTransferListHeaderLines));
Tao Baoc0299ed2018-05-24 00:16:35 -07001170
1171TEST_P(ResumableUpdaterTest, InterruptVerifyResume) {
1172 ASSERT_TRUE(android::base::WriteStringToFile(g_source_image, image_file_));
1173
1174 LOG(INFO) << "Interrupting at line " << index_ << " ("
Tao Baof8811bb2018-06-18 10:03:52 -07001175 << g_transfer_list[TransferList::kTransferListHeaderLines + index_] << ")";
Tao Baoc0299ed2018-05-24 00:16:35 -07001176
1177 std::vector<std::string> transfer_list_copy{ g_transfer_list };
Tao Baof8811bb2018-06-18 10:03:52 -07001178 transfer_list_copy[TransferList::kTransferListHeaderLines + index_] = "abort";
Tao Baoc0299ed2018-05-24 00:16:35 -07001179
1180 g_entries["transfer_list"] = android::base::Join(transfer_list_copy, '\n');
1181
1182 // Run update that's expected to fail.
1183 RunBlockImageUpdate(false, g_entries, image_file_, "");
1184
1185 std::string last_command_expected;
1186
1187 // Assert the last_command_file.
1188 if (index_ == 0) {
1189 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
1190 } else {
Tao Baof8811bb2018-06-18 10:03:52 -07001191 last_command_expected = std::to_string(index_ - 1) + "\n" +
1192 g_transfer_list[TransferList::kTransferListHeaderLines + index_ - 1];
Tao Baoc0299ed2018-05-24 00:16:35 -07001193 std::string last_command_actual;
1194 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
1195 ASSERT_EQ(last_command_expected, last_command_actual);
1196 }
1197
1198 g_entries["transfer_list"] = android::base::Join(g_transfer_list, '\n');
1199
1200 // Resume the interrupted update, by doing verification first.
1201 RunBlockImageUpdate(true, g_entries, image_file_, "t");
1202
1203 // last_command_file should remain intact.
1204 if (index_ == 0) {
1205 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
1206 } else {
1207 std::string last_command_actual;
1208 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
1209 ASSERT_EQ(last_command_expected, last_command_actual);
1210 }
1211
1212 // Resume the update.
1213 RunBlockImageUpdate(false, g_entries, image_file_, "t");
1214
1215 // last_command_file should be gone after successful update.
1216 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
1217
1218 std::string updated_image_actual;
1219 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_image_actual));
1220 ASSERT_EQ(g_target_image, updated_image_actual);
1221}