blob: de8fafd30a21fdac5155a619cea51668455e6713 [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>
Tao Baoef0eb3b2016-11-14 21:29:52 -080040#include <ziparchive/zip_archive.h>
Tianjie Xu56ebe622017-03-16 00:48:21 -070041#include <ziparchive/zip_writer.h>
Tao Bao0c7839a2016-10-10 15:48:37 -070042
Tao Baoef0eb3b2016-11-14 21:29:52 -080043#include "common/test_constants.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070044#include "edify/expr.h"
Tao Bao1fc5bf32017-10-06 07:43:41 -070045#include "otautil/error_code.h"
Tao Bao641fa972018-04-25 18:59:40 -070046#include "otautil/paths.h"
Tao Bao09e468f2017-09-29 14:39:33 -070047#include "otautil/print_sha1.h"
Tao Bao17054c02018-05-03 22:41:23 -070048#include "otautil/sysutil.h"
Tianjie Xu56ebe622017-03-16 00:48:21 -070049#include "updater/blockimg.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070050#include "updater/install.h"
Tao Baoef0eb3b2016-11-14 21:29:52 -080051#include "updater/updater.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070052
Tao Baobc4a6d52018-05-23 00:12:21 -070053using PackageEntries = std::unordered_map<std::string, std::string>;
54
55static constexpr size_t kTransferListHeaderLines = 4;
56
57struct selabel_handle* sehandle = nullptr;
Tao Bao0c7839a2016-10-10 15:48:37 -070058
Tao Baoef0eb3b2016-11-14 21:29:52 -080059static void expect(const char* expected, const char* expr_str, CauseCode cause_code,
60 UpdaterInfo* info = nullptr) {
Tianjie Xuc4447322017-03-06 14:44:59 -080061 std::unique_ptr<Expr> e;
Tao Baoef0eb3b2016-11-14 21:29:52 -080062 int error_count = 0;
63 ASSERT_EQ(0, parse_string(expr_str, &e, &error_count));
64 ASSERT_EQ(0, error_count);
Tao Bao0c7839a2016-10-10 15:48:37 -070065
Tao Baoef0eb3b2016-11-14 21:29:52 -080066 State state(expr_str, info);
Tao Bao0c7839a2016-10-10 15:48:37 -070067
Tao Baoef0eb3b2016-11-14 21:29:52 -080068 std::string result;
69 bool status = Evaluate(&state, e, &result);
Tao Bao0c7839a2016-10-10 15:48:37 -070070
Tao Baoef0eb3b2016-11-14 21:29:52 -080071 if (expected == nullptr) {
72 ASSERT_FALSE(status);
73 } else {
74 ASSERT_TRUE(status);
75 ASSERT_STREQ(expected, result.c_str());
76 }
Tao Bao0c7839a2016-10-10 15:48:37 -070077
Tao Baoef0eb3b2016-11-14 21:29:52 -080078 // Error code is set in updater/updater.cpp only, by parsing State.errmsg.
79 ASSERT_EQ(kNoError, state.error_code);
Tao Bao361342c2016-02-08 11:15:50 -080080
Tao Baoef0eb3b2016-11-14 21:29:52 -080081 // Cause code should always be available.
82 ASSERT_EQ(cause_code, state.cause_code);
Tao Bao0c7839a2016-10-10 15:48:37 -070083}
84
Tao Baobc4a6d52018-05-23 00:12:21 -070085static void BuildUpdatePackage(const PackageEntries& entries, int fd) {
Tianjie Xu5450c842017-10-18 13:15:21 -070086 FILE* zip_file_ptr = fdopen(fd, "wb");
87 ZipWriter zip_writer(zip_file_ptr);
88
89 for (const auto& entry : entries) {
Tao Baobc4a6d52018-05-23 00:12:21 -070090 // All the entries are written as STORED.
Tianjie Xu5450c842017-10-18 13:15:21 -070091 ASSERT_EQ(0, zip_writer.StartEntry(entry.first.c_str(), 0));
92 if (!entry.second.empty()) {
93 ASSERT_EQ(0, zip_writer.WriteBytes(entry.second.data(), entry.second.size()));
94 }
95 ASSERT_EQ(0, zip_writer.FinishEntry());
96 }
97
98 ASSERT_EQ(0, zip_writer.Finish());
99 ASSERT_EQ(0, fclose(zip_file_ptr));
100}
101
Tao Baobc4a6d52018-05-23 00:12:21 -0700102static void RunBlockImageUpdate(bool is_verify, const PackageEntries& entries,
103 const std::string& image_file, const std::string& result) {
104 CHECK(entries.find("transfer_list") != entries.end());
105
106 // Build the update package.
107 TemporaryFile zip_file;
108 BuildUpdatePackage(entries, zip_file.release());
109
110 MemMapping map;
111 ASSERT_TRUE(map.MapFile(zip_file.path));
112 ZipArchiveHandle handle;
113 ASSERT_EQ(0, OpenArchiveFromMemory(map.addr, map.length, zip_file.path, &handle));
114
115 // Set up the handler, command_pipe, patch offset & length.
116 UpdaterInfo updater_info;
117 updater_info.package_zip = handle;
118 TemporaryFile temp_pipe;
119 updater_info.cmd_pipe = fdopen(temp_pipe.release(), "wbe");
120 updater_info.package_zip_addr = map.addr;
121 updater_info.package_zip_len = map.length;
122
123 std::string new_data = entries.find("new_data.br") != entries.end() ? "new_data.br" : "new_data";
124 std::string script = is_verify ? "block_image_verify" : "block_image_update";
125 script += R"((")" + image_file + R"(", package_extract_file("transfer_list"), ")" + new_data +
126 R"(", "patch_data"))";
127 expect(result.c_str(), script.c_str(), kNoCause, &updater_info);
128
129 ASSERT_EQ(0, fclose(updater_info.cmd_pipe));
130 CloseArchive(handle);
131}
132
Tianjie Xu56ebe622017-03-16 00:48:21 -0700133static std::string get_sha1(const std::string& content) {
134 uint8_t digest[SHA_DIGEST_LENGTH];
135 SHA1(reinterpret_cast<const uint8_t*>(content.c_str()), content.size(), digest);
136 return print_sha1(digest);
137}
138
Tao Bao0c7839a2016-10-10 15:48:37 -0700139class UpdaterTest : public ::testing::Test {
Tianjie Xu56ebe622017-03-16 00:48:21 -0700140 protected:
Tao Baobc4a6d52018-05-23 00:12:21 -0700141 void SetUp() override {
Tianjie Xu56ebe622017-03-16 00:48:21 -0700142 RegisterBuiltins();
143 RegisterInstallFunctions();
144 RegisterBlockImageFunctions();
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800145
Tao Bao7064aa22018-05-24 21:43:50 -0700146 // Each test is run in a separate process (isolated mode). Shared temporary files won't cause
147 // conflicts.
Tao Bao641fa972018-04-25 18:59:40 -0700148 Paths::Get().set_cache_temp_source(temp_saved_source_.path);
Tao Bao7064aa22018-05-24 21:43:50 -0700149 Paths::Get().set_last_command_file(temp_last_command_.path);
Tao Bao641fa972018-04-25 18:59:40 -0700150 Paths::Get().set_stash_directory_base(temp_stash_base_.path);
Tao Baobc4a6d52018-05-23 00:12:21 -0700151
Tao Bao7064aa22018-05-24 21:43:50 -0700152 last_command_file_ = temp_last_command_.path;
Tao Baobc4a6d52018-05-23 00:12:21 -0700153 image_file_ = image_temp_file_.path;
154 }
155
156 void TearDown() override {
Tao Bao7064aa22018-05-24 21:43:50 -0700157 // Clean up the last_command_file if any.
158 ASSERT_TRUE(android::base::RemoveFileIfExists(last_command_file_));
159
Tao Baobc4a6d52018-05-23 00:12:21 -0700160 // Clear partition updated marker if any.
161 std::string updated_marker{ temp_stash_base_.path };
162 updated_marker += "/" + get_sha1(image_temp_file_.path) + ".UPDATED";
163 ASSERT_TRUE(android::base::RemoveFileIfExists(updated_marker));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700164 }
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800165
166 TemporaryFile temp_saved_source_;
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800167 TemporaryDir temp_stash_base_;
Tao Bao7064aa22018-05-24 21:43:50 -0700168 std::string last_command_file_;
Tao Baobc4a6d52018-05-23 00:12:21 -0700169 std::string image_file_;
170
171 private:
Tao Bao7064aa22018-05-24 21:43:50 -0700172 TemporaryFile temp_last_command_;
Tao Baobc4a6d52018-05-23 00:12:21 -0700173 TemporaryFile image_temp_file_;
Tao Bao0c7839a2016-10-10 15:48:37 -0700174};
175
176TEST_F(UpdaterTest, getprop) {
177 expect(android::base::GetProperty("ro.product.device", "").c_str(),
178 "getprop(\"ro.product.device\")",
Tao Bao361342c2016-02-08 11:15:50 -0800179 kNoCause);
Tao Bao0c7839a2016-10-10 15:48:37 -0700180
181 expect(android::base::GetProperty("ro.build.fingerprint", "").c_str(),
182 "getprop(\"ro.build.fingerprint\")",
Tao Bao361342c2016-02-08 11:15:50 -0800183 kNoCause);
Tao Bao0c7839a2016-10-10 15:48:37 -0700184
185 // getprop() accepts only one parameter.
Tao Bao361342c2016-02-08 11:15:50 -0800186 expect(nullptr, "getprop()", kArgsParsingFailure);
187 expect(nullptr, "getprop(\"arg1\", \"arg2\")", kArgsParsingFailure);
188}
189
190TEST_F(UpdaterTest, sha1_check) {
191 // sha1_check(data) returns the SHA-1 of the data.
192 expect("81fe8bfe87576c3ecb22426f8e57847382917acf", "sha1_check(\"abcd\")", kNoCause);
193 expect("da39a3ee5e6b4b0d3255bfef95601890afd80709", "sha1_check(\"\")", kNoCause);
194
195 // sha1_check(data, sha1_hex, [sha1_hex, ...]) returns the matched SHA-1.
196 expect("81fe8bfe87576c3ecb22426f8e57847382917acf",
197 "sha1_check(\"abcd\", \"81fe8bfe87576c3ecb22426f8e57847382917acf\")",
198 kNoCause);
199
200 expect("81fe8bfe87576c3ecb22426f8e57847382917acf",
201 "sha1_check(\"abcd\", \"wrong_sha1\", \"81fe8bfe87576c3ecb22426f8e57847382917acf\")",
202 kNoCause);
203
204 // Or "" if there's no match.
205 expect("",
206 "sha1_check(\"abcd\", \"wrong_sha1\")",
207 kNoCause);
208
209 expect("",
210 "sha1_check(\"abcd\", \"wrong_sha1\", \"wrong_sha2\")",
211 kNoCause);
212
213 // sha1_check() expects at least one argument.
214 expect(nullptr, "sha1_check()", kArgsParsingFailure);
Tao Bao0c7839a2016-10-10 15:48:37 -0700215}
Tao Bao51d516e2016-11-03 14:49:01 -0700216
Tao Baodb56eb02017-03-23 06:34:20 -0700217TEST_F(UpdaterTest, apply_patch_check) {
218 // Zero-argument is not valid.
219 expect(nullptr, "apply_patch_check()", kArgsParsingFailure);
220
221 // File not found.
222 expect("", "apply_patch_check(\"/doesntexist\")", kNoCause);
223
224 std::string src_file = from_testdata_base("old.file");
225 std::string src_content;
226 ASSERT_TRUE(android::base::ReadFileToString(src_file, &src_content));
227 size_t src_size = src_content.size();
228 std::string src_hash = get_sha1(src_content);
229
230 // One-argument with EMMC:file:size:sha1 should pass the check.
231 std::string filename = android::base::Join(
232 std::vector<std::string>{ "EMMC", src_file, std::to_string(src_size), src_hash }, ":");
233 std::string cmd = "apply_patch_check(\"" + filename + "\")";
234 expect("t", cmd.c_str(), kNoCause);
235
236 // EMMC:file:(size-1):sha1:(size+1):sha1 should fail the check.
237 std::string filename_bad = android::base::Join(
238 std::vector<std::string>{ "EMMC", src_file, std::to_string(src_size - 1), src_hash,
239 std::to_string(src_size + 1), src_hash },
240 ":");
241 cmd = "apply_patch_check(\"" + filename_bad + "\")";
242 expect("", cmd.c_str(), kNoCause);
243
244 // EMMC:file:(size-1):sha1:size:sha1:(size+1):sha1 should pass the check.
245 filename_bad =
246 android::base::Join(std::vector<std::string>{ "EMMC", src_file, std::to_string(src_size - 1),
247 src_hash, std::to_string(src_size), src_hash,
248 std::to_string(src_size + 1), src_hash },
249 ":");
250 cmd = "apply_patch_check(\"" + filename_bad + "\")";
251 expect("t", cmd.c_str(), kNoCause);
252
253 // Multiple arguments.
254 cmd = "apply_patch_check(\"" + filename + "\", \"wrong_sha1\", \"wrong_sha2\")";
255 expect("", cmd.c_str(), kNoCause);
256
257 cmd = "apply_patch_check(\"" + filename + "\", \"wrong_sha1\", \"" + src_hash +
258 "\", \"wrong_sha2\")";
259 expect("t", cmd.c_str(), kNoCause);
260
261 cmd = "apply_patch_check(\"" + filename_bad + "\", \"wrong_sha1\", \"" + src_hash +
262 "\", \"wrong_sha2\")";
263 expect("t", cmd.c_str(), kNoCause);
264}
265
Tao Bao51d516e2016-11-03 14:49:01 -0700266TEST_F(UpdaterTest, file_getprop) {
267 // file_getprop() expects two arguments.
268 expect(nullptr, "file_getprop()", kArgsParsingFailure);
269 expect(nullptr, "file_getprop(\"arg1\")", kArgsParsingFailure);
270 expect(nullptr, "file_getprop(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
271
272 // File doesn't exist.
273 expect(nullptr, "file_getprop(\"/doesntexist\", \"key1\")", kFileGetPropFailure);
274
275 // Reject too large files (current limit = 65536).
276 TemporaryFile temp_file1;
277 std::string buffer(65540, '\0');
278 ASSERT_TRUE(android::base::WriteStringToFile(buffer, temp_file1.path));
279
280 // Read some keys.
281 TemporaryFile temp_file2;
282 std::string content("ro.product.name=tardis\n"
283 "# comment\n\n\n"
284 "ro.product.model\n"
285 "ro.product.board = magic \n");
286 ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file2.path));
287
288 std::string script1("file_getprop(\"" + std::string(temp_file2.path) +
289 "\", \"ro.product.name\")");
290 expect("tardis", script1.c_str(), kNoCause);
291
292 std::string script2("file_getprop(\"" + std::string(temp_file2.path) +
293 "\", \"ro.product.board\")");
294 expect("magic", script2.c_str(), kNoCause);
295
296 // No match.
297 std::string script3("file_getprop(\"" + std::string(temp_file2.path) +
298 "\", \"ro.product.wrong\")");
299 expect("", script3.c_str(), kNoCause);
300
301 std::string script4("file_getprop(\"" + std::string(temp_file2.path) +
302 "\", \"ro.product.name=\")");
303 expect("", script4.c_str(), kNoCause);
304
305 std::string script5("file_getprop(\"" + std::string(temp_file2.path) +
306 "\", \"ro.product.nam\")");
307 expect("", script5.c_str(), kNoCause);
308
309 std::string script6("file_getprop(\"" + std::string(temp_file2.path) +
310 "\", \"ro.product.model\")");
311 expect("", script6.c_str(), kNoCause);
312}
Tao Bao0831d0b2016-11-03 23:25:04 -0700313
Tao Baoef0eb3b2016-11-14 21:29:52 -0800314// TODO: Test extracting to block device.
315TEST_F(UpdaterTest, package_extract_file) {
316 // package_extract_file expects 1 or 2 arguments.
317 expect(nullptr, "package_extract_file()", kArgsParsingFailure);
318 expect(nullptr, "package_extract_file(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
319
320 std::string zip_path = from_testdata_base("ziptest_valid.zip");
321 ZipArchiveHandle handle;
322 ASSERT_EQ(0, OpenArchive(zip_path.c_str(), &handle));
323
324 // Need to set up the ziphandle.
325 UpdaterInfo updater_info;
326 updater_info.package_zip = handle;
327
328 // Two-argument version.
329 TemporaryFile temp_file1;
330 std::string script("package_extract_file(\"a.txt\", \"" + std::string(temp_file1.path) + "\")");
331 expect("t", script.c_str(), kNoCause, &updater_info);
332
333 // Verify the extracted entry.
334 std::string data;
335 ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data));
336 ASSERT_EQ(kATxtContents, data);
337
338 // Now extract another entry to the same location, which should overwrite.
339 script = "package_extract_file(\"b.txt\", \"" + std::string(temp_file1.path) + "\")";
340 expect("t", script.c_str(), kNoCause, &updater_info);
341
342 ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data));
343 ASSERT_EQ(kBTxtContents, data);
344
345 // Missing zip entry. The two-argument version doesn't abort.
346 script = "package_extract_file(\"doesntexist\", \"" + std::string(temp_file1.path) + "\")";
347 expect("", script.c_str(), kNoCause, &updater_info);
348
349 // Extract to /dev/full should fail.
350 script = "package_extract_file(\"a.txt\", \"/dev/full\")";
351 expect("", script.c_str(), kNoCause, &updater_info);
352
353 // One-argument version.
354 script = "sha1_check(package_extract_file(\"a.txt\"))";
355 expect(kATxtSha1Sum.c_str(), script.c_str(), kNoCause, &updater_info);
356
357 script = "sha1_check(package_extract_file(\"b.txt\"))";
358 expect(kBTxtSha1Sum.c_str(), script.c_str(), kNoCause, &updater_info);
359
360 // Missing entry. The one-argument version aborts the evaluation.
361 script = "package_extract_file(\"doesntexist\")";
362 expect(nullptr, script.c_str(), kPackageExtractFileFailure, &updater_info);
363
364 CloseArchive(handle);
365}
Tao Baod0f30882016-11-03 23:52:01 -0700366
367TEST_F(UpdaterTest, write_value) {
368 // write_value() expects two arguments.
369 expect(nullptr, "write_value()", kArgsParsingFailure);
370 expect(nullptr, "write_value(\"arg1\")", kArgsParsingFailure);
371 expect(nullptr, "write_value(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
372
373 // filename cannot be empty.
374 expect(nullptr, "write_value(\"value\", \"\")", kArgsParsingFailure);
375
376 // Write some value to file.
377 TemporaryFile temp_file;
378 std::string value = "magicvalue";
379 std::string script("write_value(\"" + value + "\", \"" + std::string(temp_file.path) + "\")");
380 expect("t", script.c_str(), kNoCause);
381
382 // Verify the content.
383 std::string content;
384 ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content));
385 ASSERT_EQ(value, content);
386
387 // Allow writing empty string.
388 script = "write_value(\"\", \"" + std::string(temp_file.path) + "\")";
389 expect("t", script.c_str(), kNoCause);
390
391 // Verify the content.
392 ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content));
393 ASSERT_EQ("", content);
394
395 // It should fail gracefully when write fails.
396 script = "write_value(\"value\", \"/proc/0/file1\")";
397 expect("", script.c_str(), kNoCause);
398}
Tao Baobedf5fc2016-11-18 12:01:26 -0800399
400TEST_F(UpdaterTest, get_stage) {
401 // get_stage() expects one argument.
402 expect(nullptr, "get_stage()", kArgsParsingFailure);
403 expect(nullptr, "get_stage(\"arg1\", \"arg2\")", kArgsParsingFailure);
404 expect(nullptr, "get_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
405
406 // Set up a local file as BCB.
407 TemporaryFile tf;
408 std::string temp_file(tf.path);
409 bootloader_message boot;
410 strlcpy(boot.stage, "2/3", sizeof(boot.stage));
411 std::string err;
412 ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err));
413
414 // Can read the stage value.
415 std::string script("get_stage(\"" + temp_file + "\")");
416 expect("2/3", script.c_str(), kNoCause);
417
418 // Bad BCB path.
419 script = "get_stage(\"doesntexist\")";
420 expect("", script.c_str(), kNoCause);
421}
422
423TEST_F(UpdaterTest, set_stage) {
424 // set_stage() expects two arguments.
425 expect(nullptr, "set_stage()", kArgsParsingFailure);
426 expect(nullptr, "set_stage(\"arg1\")", kArgsParsingFailure);
427 expect(nullptr, "set_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
428
429 // Set up a local file as BCB.
430 TemporaryFile tf;
431 std::string temp_file(tf.path);
432 bootloader_message boot;
433 strlcpy(boot.command, "command", sizeof(boot.command));
434 strlcpy(boot.stage, "2/3", sizeof(boot.stage));
435 std::string err;
436 ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err));
437
438 // Write with set_stage().
439 std::string script("set_stage(\"" + temp_file + "\", \"1/3\")");
440 expect(tf.path, script.c_str(), kNoCause);
441
442 // Verify.
443 bootloader_message boot_verify;
444 ASSERT_TRUE(read_bootloader_message_from(&boot_verify, temp_file, &err));
445
446 // Stage should be updated, with command part untouched.
447 ASSERT_STREQ("1/3", boot_verify.stage);
448 ASSERT_STREQ(boot.command, boot_verify.command);
449
450 // Bad BCB path.
451 script = "set_stage(\"doesntexist\", \"1/3\")";
452 expect("", script.c_str(), kNoCause);
453
454 script = "set_stage(\"/dev/full\", \"1/3\")";
455 expect("", script.c_str(), kNoCause);
456}
Tao Bao9aa7ab52017-01-05 17:27:19 -0800457
458TEST_F(UpdaterTest, set_progress) {
459 // set_progress() expects one argument.
460 expect(nullptr, "set_progress()", kArgsParsingFailure);
461 expect(nullptr, "set_progress(\"arg1\", \"arg2\")", kArgsParsingFailure);
462
463 // Invalid progress argument.
464 expect(nullptr, "set_progress(\"arg1\")", kArgsParsingFailure);
465 expect(nullptr, "set_progress(\"3x+5\")", kArgsParsingFailure);
466 expect(nullptr, "set_progress(\".3.5\")", kArgsParsingFailure);
467
468 TemporaryFile tf;
469 UpdaterInfo updater_info;
Tianjie Xu79327ac2017-09-08 17:09:10 -0700470 updater_info.cmd_pipe = fdopen(tf.release(), "w");
Tao Bao9aa7ab52017-01-05 17:27:19 -0800471 expect(".52", "set_progress(\".52\")", kNoCause, &updater_info);
472 fflush(updater_info.cmd_pipe);
473
474 std::string cmd;
475 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd));
476 ASSERT_EQ(android::base::StringPrintf("set_progress %f\n", .52), cmd);
477 // recovery-updater protocol expects 2 tokens ("set_progress <frac>").
478 ASSERT_EQ(2U, android::base::Split(cmd, " ").size());
Tianjie Xu79327ac2017-09-08 17:09:10 -0700479 ASSERT_EQ(0, fclose(updater_info.cmd_pipe));
Tao Bao9aa7ab52017-01-05 17:27:19 -0800480}
481
482TEST_F(UpdaterTest, show_progress) {
483 // show_progress() expects two arguments.
484 expect(nullptr, "show_progress()", kArgsParsingFailure);
485 expect(nullptr, "show_progress(\"arg1\")", kArgsParsingFailure);
486 expect(nullptr, "show_progress(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
487
488 // Invalid progress arguments.
489 expect(nullptr, "show_progress(\"arg1\", \"arg2\")", kArgsParsingFailure);
490 expect(nullptr, "show_progress(\"3x+5\", \"10\")", kArgsParsingFailure);
491 expect(nullptr, "show_progress(\".3\", \"5a\")", kArgsParsingFailure);
492
493 TemporaryFile tf;
494 UpdaterInfo updater_info;
Tianjie Xu79327ac2017-09-08 17:09:10 -0700495 updater_info.cmd_pipe = fdopen(tf.release(), "w");
Tao Bao9aa7ab52017-01-05 17:27:19 -0800496 expect(".52", "show_progress(\".52\", \"10\")", kNoCause, &updater_info);
497 fflush(updater_info.cmd_pipe);
498
499 std::string cmd;
500 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd));
501 ASSERT_EQ(android::base::StringPrintf("progress %f %d\n", .52, 10), cmd);
502 // recovery-updater protocol expects 3 tokens ("progress <frac> <secs>").
503 ASSERT_EQ(3U, android::base::Split(cmd, " ").size());
Tianjie Xu79327ac2017-09-08 17:09:10 -0700504 ASSERT_EQ(0, fclose(updater_info.cmd_pipe));
Tao Bao9aa7ab52017-01-05 17:27:19 -0800505}
Tianjie Xu56ebe622017-03-16 00:48:21 -0700506
Tianjie Xu5450c842017-10-18 13:15:21 -0700507TEST_F(UpdaterTest, block_image_update_patch_data) {
Tianjie Xu56ebe622017-03-16 00:48:21 -0700508 std::string src_content = std::string(4096, 'a') + std::string(4096, 'c');
509 std::string tgt_content = std::string(4096, 'b') + std::string(4096, 'd');
Tianjie Xu5450c842017-10-18 13:15:21 -0700510
511 // Generate the patch data.
Tianjie Xu56ebe622017-03-16 00:48:21 -0700512 TemporaryFile patch_file;
Tao Baobc4a6d52018-05-23 00:12:21 -0700513 ASSERT_EQ(0,
514 bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(src_content.data()), src_content.size(),
515 reinterpret_cast<const uint8_t*>(tgt_content.data()), tgt_content.size(),
516 patch_file.path, nullptr));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700517 std::string patch_content;
518 ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch_content));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700519
Tianjie Xu5450c842017-10-18 13:15:21 -0700520 // Create the transfer list that contains a bsdiff.
Tianjie Xu56ebe622017-03-16 00:48:21 -0700521 std::string src_hash = get_sha1(src_content);
522 std::string tgt_hash = get_sha1(tgt_content);
Tao Baobc4a6d52018-05-23 00:12:21 -0700523 std::vector<std::string> transfer_list{
524 // clang-format off
Tianjie Xu56ebe622017-03-16 00:48:21 -0700525 "4",
526 "2",
527 "0",
528 "2",
529 "stash " + src_hash + " 2,0,2",
530 android::base::StringPrintf("bsdiff 0 %zu %s %s 2,0,2 2 - %s:2,0,2", patch_content.size(),
531 src_hash.c_str(), tgt_hash.c_str(), src_hash.c_str()),
532 "free " + src_hash,
Tao Baobc4a6d52018-05-23 00:12:21 -0700533 // clang-format on
Tianjie Xu56ebe622017-03-16 00:48:21 -0700534 };
Tianjie Xu56ebe622017-03-16 00:48:21 -0700535
Tao Baobc4a6d52018-05-23 00:12:21 -0700536 PackageEntries entries{
Tianjie Xu5450c842017-10-18 13:15:21 -0700537 { "new_data", "" },
538 { "patch_data", patch_content },
539 { "transfer_list", android::base::Join(transfer_list, '\n') },
Tianjie Xu56ebe622017-03-16 00:48:21 -0700540 };
Tianjie Xu5450c842017-10-18 13:15:21 -0700541
Tao Baobc4a6d52018-05-23 00:12:21 -0700542 ASSERT_TRUE(android::base::WriteStringToFile(src_content, image_file_));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700543
Tao Baobc4a6d52018-05-23 00:12:21 -0700544 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu56ebe622017-03-16 00:48:21 -0700545
Tianjie Xu56ebe622017-03-16 00:48:21 -0700546 // The update_file should be patched correctly.
547 std::string updated_content;
Tao Baobc4a6d52018-05-23 00:12:21 -0700548 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_content));
549 ASSERT_EQ(tgt_content, updated_content);
Tianjie Xu5450c842017-10-18 13:15:21 -0700550}
551
552TEST_F(UpdaterTest, block_image_update_fail) {
553 std::string src_content(4096 * 2, 'e');
554 std::string src_hash = get_sha1(src_content);
555 // Stash and free some blocks, then fail the update intentionally.
Tao Baobc4a6d52018-05-23 00:12:21 -0700556 std::vector<std::string> transfer_list{
557 // clang-format off
558 "4",
559 "2",
560 "0",
561 "2",
562 "stash " + src_hash + " 2,0,2",
563 "free " + src_hash,
564 "fail",
565 // clang-format on
Tianjie Xu5450c842017-10-18 13:15:21 -0700566 };
567
568 // Add a new data of 10 bytes to test the deadlock.
Tao Baobc4a6d52018-05-23 00:12:21 -0700569 PackageEntries entries{
Tianjie Xu5450c842017-10-18 13:15:21 -0700570 { "new_data", std::string(10, 0) },
571 { "patch_data", "" },
572 { "transfer_list", android::base::Join(transfer_list, '\n') },
573 };
574
Tao Baobc4a6d52018-05-23 00:12:21 -0700575 ASSERT_TRUE(android::base::WriteStringToFile(src_content, image_file_));
Tianjie Xu5450c842017-10-18 13:15:21 -0700576
Tao Baobc4a6d52018-05-23 00:12:21 -0700577 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu5450c842017-10-18 13:15:21 -0700578
Tianjie Xu56ebe622017-03-16 00:48:21 -0700579 // Updater generates the stash name based on the input file name.
Tao Baobc4a6d52018-05-23 00:12:21 -0700580 std::string name_digest = get_sha1(image_file_);
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800581 std::string stash_base = std::string(temp_stash_base_.path) + "/" + name_digest;
Tianjie Xu56ebe622017-03-16 00:48:21 -0700582 ASSERT_EQ(0, access(stash_base.c_str(), F_OK));
Tao Baobc4a6d52018-05-23 00:12:21 -0700583 // Expect the stashed blocks to be freed.
Tianjie Xu5450c842017-10-18 13:15:21 -0700584 ASSERT_EQ(-1, access((stash_base + src_hash).c_str(), F_OK));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700585 ASSERT_EQ(0, rmdir(stash_base.c_str()));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700586}
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700587
Tianjie Xu5450c842017-10-18 13:15:21 -0700588TEST_F(UpdaterTest, new_data_over_write) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700589 std::vector<std::string> transfer_list{
590 // clang-format off
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700591 "4",
592 "1",
593 "0",
594 "0",
595 "new 2,0,1",
Tao Baobc4a6d52018-05-23 00:12:21 -0700596 // clang-format on
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700597 };
Tianjie Xu5450c842017-10-18 13:15:21 -0700598
Tao Baobc4a6d52018-05-23 00:12:21 -0700599 // Write 4096 + 100 bytes of new data.
600 PackageEntries entries{
601 { "new_data", std::string(4196, 0) },
Tianjie Xu5450c842017-10-18 13:15:21 -0700602 { "patch_data", "" },
603 { "transfer_list", android::base::Join(transfer_list, '\n') },
604 };
605
Tao Baobc4a6d52018-05-23 00:12:21 -0700606 RunBlockImageUpdate(false, entries, image_file_, "t");
607}
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700608
Tao Baobc4a6d52018-05-23 00:12:21 -0700609TEST_F(UpdaterTest, new_data_short_write) {
610 std::vector<std::string> transfer_list{
611 // clang-format off
612 "4",
613 "1",
614 "0",
615 "0",
616 "new 2,0,1",
617 // clang-format on
618 };
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700619
Tao Baobc4a6d52018-05-23 00:12:21 -0700620 PackageEntries entries{
621 { "patch_data", "" },
622 { "transfer_list", android::base::Join(transfer_list, '\n') },
623 };
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700624
625 // Updater should report the failure gracefully rather than stuck in deadlock.
Tao Baobc4a6d52018-05-23 00:12:21 -0700626 entries["new_data"] = "";
627 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700628
Tao Baobc4a6d52018-05-23 00:12:21 -0700629 entries["new_data"] = std::string(10, 'a');
630 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700631
632 // Expect to write 1 block of new data successfully.
Tao Baobc4a6d52018-05-23 00:12:21 -0700633 entries["new_data"] = std::string(4096, 'a');
634 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu107a34f2017-06-29 17:04:21 -0700635}
636
637TEST_F(UpdaterTest, brotli_new_data) {
Tianjie Xu107a34f2017-06-29 17:04:21 -0700638 auto generator = []() { return rand() % 128; };
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700639 // Generate 100 blocks of random data.
Tianjie Xu107a34f2017-06-29 17:04:21 -0700640 std::string brotli_new_data;
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700641 brotli_new_data.reserve(4096 * 100);
642 generate_n(back_inserter(brotli_new_data), 4096 * 100, generator);
Tianjie Xu107a34f2017-06-29 17:04:21 -0700643
644 size_t encoded_size = BrotliEncoderMaxCompressedSize(brotli_new_data.size());
Tianjie Xu5450c842017-10-18 13:15:21 -0700645 std::string encoded_data(encoded_size, 0);
Tianjie Xu107a34f2017-06-29 17:04:21 -0700646 ASSERT_TRUE(BrotliEncoderCompress(
647 BROTLI_DEFAULT_QUALITY, BROTLI_DEFAULT_WINDOW, BROTLI_DEFAULT_MODE, brotli_new_data.size(),
Tianjie Xu5450c842017-10-18 13:15:21 -0700648 reinterpret_cast<const uint8_t*>(brotli_new_data.data()), &encoded_size,
649 reinterpret_cast<uint8_t*>(const_cast<char*>(encoded_data.data()))));
650 encoded_data.resize(encoded_size);
Tianjie Xu107a34f2017-06-29 17:04:21 -0700651
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700652 // Write a few small chunks of new data, then a large chunk, and finally a few small chunks.
653 // This helps us to catch potential short writes.
Tianjie Xu107a34f2017-06-29 17:04:21 -0700654 std::vector<std::string> transfer_list = {
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700655 "4",
656 "100",
657 "0",
658 "0",
659 "new 2,0,1",
660 "new 2,1,2",
661 "new 4,2,50,50,97",
662 "new 2,97,98",
663 "new 2,98,99",
664 "new 2,99,100",
Tianjie Xu107a34f2017-06-29 17:04:21 -0700665 };
Tianjie Xu5450c842017-10-18 13:15:21 -0700666
Tao Baobc4a6d52018-05-23 00:12:21 -0700667 PackageEntries entries{
668 { "new_data.br", std::move(encoded_data) },
Tianjie Xu5450c842017-10-18 13:15:21 -0700669 { "patch_data", "" },
670 { "transfer_list", android::base::Join(transfer_list, '\n') },
671 };
672
Tao Baobc4a6d52018-05-23 00:12:21 -0700673 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu107a34f2017-06-29 17:04:21 -0700674
675 std::string updated_content;
Tao Baobc4a6d52018-05-23 00:12:21 -0700676 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_content));
Tianjie Xu107a34f2017-06-29 17:04:21 -0700677 ASSERT_EQ(brotli_new_data, updated_content);
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700678}
Tianjie Xu284752e2017-12-05 11:04:17 -0800679
680TEST_F(UpdaterTest, last_command_update) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700681 std::string block1(4096, '1');
682 std::string block2(4096, '2');
683 std::string block3(4096, '3');
Tianjie Xu284752e2017-12-05 11:04:17 -0800684 std::string block1_hash = get_sha1(block1);
685 std::string block2_hash = get_sha1(block2);
686 std::string block3_hash = get_sha1(block3);
687
688 // Compose the transfer list to fail the first update.
Tao Baobc4a6d52018-05-23 00:12:21 -0700689 std::vector<std::string> transfer_list_fail{
690 // clang-format off
Tianjie Xu284752e2017-12-05 11:04:17 -0800691 "4",
692 "2",
693 "0",
694 "2",
695 "stash " + block1_hash + " 2,0,1",
696 "move " + block1_hash + " 2,1,2 1 2,0,1",
697 "stash " + block3_hash + " 2,2,3",
698 "fail",
Tao Baobc4a6d52018-05-23 00:12:21 -0700699 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -0800700 };
701
702 // Mimic a resumed update with the same transfer commands.
Tao Baobc4a6d52018-05-23 00:12:21 -0700703 std::vector<std::string> transfer_list_continue{
704 // clang-format off
Tianjie Xu284752e2017-12-05 11:04:17 -0800705 "4",
706 "2",
707 "0",
708 "2",
709 "stash " + block1_hash + " 2,0,1",
710 "move " + block1_hash + " 2,1,2 1 2,0,1",
711 "stash " + block3_hash + " 2,2,3",
712 "move " + block1_hash + " 2,2,3 1 2,0,1",
Tao Baobc4a6d52018-05-23 00:12:21 -0700713 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -0800714 };
715
Tao Baobc4a6d52018-05-23 00:12:21 -0700716 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_));
717
718 PackageEntries entries{
Tianjie Xu284752e2017-12-05 11:04:17 -0800719 { "new_data", "" },
720 { "patch_data", "" },
Tao Baobc4a6d52018-05-23 00:12:21 -0700721 { "transfer_list", android::base::Join(transfer_list_fail, '\n') },
Tianjie Xu284752e2017-12-05 11:04:17 -0800722 };
723
Tao Baobc4a6d52018-05-23 00:12:21 -0700724 // "2\nstash " + block3_hash + " 2,2,3"
725 std::string last_command_content = "2\n" + transfer_list_fail[kTransferListHeaderLines + 2];
Tianjie Xu284752e2017-12-05 11:04:17 -0800726
Tao Baobc4a6d52018-05-23 00:12:21 -0700727 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu284752e2017-12-05 11:04:17 -0800728
729 // Expect last_command to contain the last stash command.
Tao Baobc4a6d52018-05-23 00:12:21 -0700730 std::string last_command_actual;
Tao Bao7064aa22018-05-24 21:43:50 -0700731 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
Tao Baobc4a6d52018-05-23 00:12:21 -0700732 EXPECT_EQ(last_command_content, last_command_actual);
733
Tianjie Xu284752e2017-12-05 11:04:17 -0800734 std::string updated_contents;
Tao Baobc4a6d52018-05-23 00:12:21 -0700735 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_contents));
Tianjie Xu284752e2017-12-05 11:04:17 -0800736 ASSERT_EQ(block1 + block1 + block3, updated_contents);
737
Tao Baobc4a6d52018-05-23 00:12:21 -0700738 // "Resume" the update. Expect the first 'move' to be skipped but the second 'move' to be
739 // executed. Note that we intentionally reset the image file.
740 entries["transfer_list"] = android::base::Join(transfer_list_continue, '\n');
741 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_));
742 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu284752e2017-12-05 11:04:17 -0800743
Tao Baobc4a6d52018-05-23 00:12:21 -0700744 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_contents));
745 ASSERT_EQ(block1 + block2 + block1, updated_contents);
Tianjie Xu284752e2017-12-05 11:04:17 -0800746}
747
748TEST_F(UpdaterTest, last_command_update_unresumable) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700749 std::string block1(4096, '1');
750 std::string block2(4096, '2');
Tianjie Xu284752e2017-12-05 11:04:17 -0800751 std::string block1_hash = get_sha1(block1);
752 std::string block2_hash = get_sha1(block2);
753
754 // Construct an unresumable update with source blocks mismatch.
Tao Baobc4a6d52018-05-23 00:12:21 -0700755 std::vector<std::string> transfer_list_unresumable{
756 // clang-format off
757 "4",
758 "2",
759 "0",
760 "2",
761 "stash " + block1_hash + " 2,0,1",
762 "move " + block2_hash + " 2,1,2 1 2,0,1",
763 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -0800764 };
765
Tao Baobc4a6d52018-05-23 00:12:21 -0700766 PackageEntries entries{
Tianjie Xu284752e2017-12-05 11:04:17 -0800767 { "new_data", "" },
768 { "patch_data", "" },
Tao Baobc4a6d52018-05-23 00:12:21 -0700769 { "transfer_list", android::base::Join(transfer_list_unresumable, '\n') },
Tianjie Xu284752e2017-12-05 11:04:17 -0800770 };
771
Tao Baobc4a6d52018-05-23 00:12:21 -0700772 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block1, image_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -0800773
Tao Baobc4a6d52018-05-23 00:12:21 -0700774 std::string last_command_content = "0\n" + transfer_list_unresumable[kTransferListHeaderLines];
Tao Bao7064aa22018-05-24 21:43:50 -0700775 ASSERT_TRUE(android::base::WriteStringToFile(last_command_content, last_command_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -0800776
Tao Baobc4a6d52018-05-23 00:12:21 -0700777 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu284752e2017-12-05 11:04:17 -0800778
Tao Baobc4a6d52018-05-23 00:12:21 -0700779 // The last_command_file will be deleted if the update encounters an unresumable failure later.
Tao Bao7064aa22018-05-24 21:43:50 -0700780 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
Tianjie Xu284752e2017-12-05 11:04:17 -0800781}
782
783TEST_F(UpdaterTest, last_command_verify) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700784 std::string block1(4096, '1');
785 std::string block2(4096, '2');
786 std::string block3(4096, '3');
Tianjie Xu284752e2017-12-05 11:04:17 -0800787 std::string block1_hash = get_sha1(block1);
788 std::string block2_hash = get_sha1(block2);
789 std::string block3_hash = get_sha1(block3);
790
Tao Baobc4a6d52018-05-23 00:12:21 -0700791 std::vector<std::string> transfer_list_verify{
792 // clang-format off
Tianjie Xu284752e2017-12-05 11:04:17 -0800793 "4",
794 "2",
795 "0",
796 "2",
797 "stash " + block1_hash + " 2,0,1",
798 "move " + block1_hash + " 2,0,1 1 2,0,1",
799 "move " + block1_hash + " 2,1,2 1 2,0,1",
800 "stash " + block3_hash + " 2,2,3",
Tao Baobc4a6d52018-05-23 00:12:21 -0700801 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -0800802 };
803
Tao Baobc4a6d52018-05-23 00:12:21 -0700804 PackageEntries entries{
Tianjie Xu284752e2017-12-05 11:04:17 -0800805 { "new_data", "" },
806 { "patch_data", "" },
Tao Baobc4a6d52018-05-23 00:12:21 -0700807 { "transfer_list", android::base::Join(transfer_list_verify, '\n') },
Tianjie Xu284752e2017-12-05 11:04:17 -0800808 };
809
Tao Baobc4a6d52018-05-23 00:12:21 -0700810 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block1 + block3, image_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -0800811
Tao Baobc4a6d52018-05-23 00:12:21 -0700812 // Last command: "move " + block1_hash + " 2,1,2 1 2,0,1"
813 std::string last_command_content = "2\n" + transfer_list_verify[kTransferListHeaderLines + 2];
Tianjie Xu284752e2017-12-05 11:04:17 -0800814
Tao Baobc4a6d52018-05-23 00:12:21 -0700815 // First run: expect the verification to succeed and the last_command_file is intact.
Tao Bao7064aa22018-05-24 21:43:50 -0700816 ASSERT_TRUE(android::base::WriteStringToFile(last_command_content, last_command_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -0800817
Tao Baobc4a6d52018-05-23 00:12:21 -0700818 RunBlockImageUpdate(true, entries, image_file_, "t");
Tianjie Xu284752e2017-12-05 11:04:17 -0800819
Tao Baobc4a6d52018-05-23 00:12:21 -0700820 std::string last_command_actual;
Tao Bao7064aa22018-05-24 21:43:50 -0700821 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
Tao Baobc4a6d52018-05-23 00:12:21 -0700822 EXPECT_EQ(last_command_content, last_command_actual);
Tianjie Xu284752e2017-12-05 11:04:17 -0800823
Tao Baobc4a6d52018-05-23 00:12:21 -0700824 // Second run with a mismatching block image: expect the verification to succeed but
825 // last_command_file to be deleted; because the target blocks in the last command don't have the
826 // expected contents for the second move command.
827 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_));
828 RunBlockImageUpdate(true, entries, image_file_, "t");
Tao Bao7064aa22018-05-24 21:43:50 -0700829 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
Tianjie Xu284752e2017-12-05 11:04:17 -0800830}