blob: d9582155e7ca715f1a4a070a348c3ebc8bf22cb4 [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 Bao641fa972018-04-25 18:59:40 -0700146 Paths::Get().set_cache_temp_source(temp_saved_source_.path);
Tao Bao641fa972018-04-25 18:59:40 -0700147 Paths::Get().set_stash_directory_base(temp_stash_base_.path);
Tao Baobc4a6d52018-05-23 00:12:21 -0700148
149 image_file_ = image_temp_file_.path;
150 }
151
152 void TearDown() override {
153 // Clear partition updated marker if any.
154 std::string updated_marker{ temp_stash_base_.path };
155 updated_marker += "/" + get_sha1(image_temp_file_.path) + ".UPDATED";
156 ASSERT_TRUE(android::base::RemoveFileIfExists(updated_marker));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700157 }
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800158
159 TemporaryFile temp_saved_source_;
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800160 TemporaryDir temp_stash_base_;
Tao Baobc4a6d52018-05-23 00:12:21 -0700161 std::string image_file_;
162
163 private:
164 TemporaryFile image_temp_file_;
Tao Bao0c7839a2016-10-10 15:48:37 -0700165};
166
167TEST_F(UpdaterTest, getprop) {
168 expect(android::base::GetProperty("ro.product.device", "").c_str(),
169 "getprop(\"ro.product.device\")",
Tao Bao361342c2016-02-08 11:15:50 -0800170 kNoCause);
Tao Bao0c7839a2016-10-10 15:48:37 -0700171
172 expect(android::base::GetProperty("ro.build.fingerprint", "").c_str(),
173 "getprop(\"ro.build.fingerprint\")",
Tao Bao361342c2016-02-08 11:15:50 -0800174 kNoCause);
Tao Bao0c7839a2016-10-10 15:48:37 -0700175
176 // getprop() accepts only one parameter.
Tao Bao361342c2016-02-08 11:15:50 -0800177 expect(nullptr, "getprop()", kArgsParsingFailure);
178 expect(nullptr, "getprop(\"arg1\", \"arg2\")", kArgsParsingFailure);
179}
180
181TEST_F(UpdaterTest, sha1_check) {
182 // sha1_check(data) returns the SHA-1 of the data.
183 expect("81fe8bfe87576c3ecb22426f8e57847382917acf", "sha1_check(\"abcd\")", kNoCause);
184 expect("da39a3ee5e6b4b0d3255bfef95601890afd80709", "sha1_check(\"\")", kNoCause);
185
186 // sha1_check(data, sha1_hex, [sha1_hex, ...]) returns the matched SHA-1.
187 expect("81fe8bfe87576c3ecb22426f8e57847382917acf",
188 "sha1_check(\"abcd\", \"81fe8bfe87576c3ecb22426f8e57847382917acf\")",
189 kNoCause);
190
191 expect("81fe8bfe87576c3ecb22426f8e57847382917acf",
192 "sha1_check(\"abcd\", \"wrong_sha1\", \"81fe8bfe87576c3ecb22426f8e57847382917acf\")",
193 kNoCause);
194
195 // Or "" if there's no match.
196 expect("",
197 "sha1_check(\"abcd\", \"wrong_sha1\")",
198 kNoCause);
199
200 expect("",
201 "sha1_check(\"abcd\", \"wrong_sha1\", \"wrong_sha2\")",
202 kNoCause);
203
204 // sha1_check() expects at least one argument.
205 expect(nullptr, "sha1_check()", kArgsParsingFailure);
Tao Bao0c7839a2016-10-10 15:48:37 -0700206}
Tao Bao51d516e2016-11-03 14:49:01 -0700207
Tao Baodb56eb02017-03-23 06:34:20 -0700208TEST_F(UpdaterTest, apply_patch_check) {
209 // Zero-argument is not valid.
210 expect(nullptr, "apply_patch_check()", kArgsParsingFailure);
211
212 // File not found.
213 expect("", "apply_patch_check(\"/doesntexist\")", kNoCause);
214
215 std::string src_file = from_testdata_base("old.file");
216 std::string src_content;
217 ASSERT_TRUE(android::base::ReadFileToString(src_file, &src_content));
218 size_t src_size = src_content.size();
219 std::string src_hash = get_sha1(src_content);
220
221 // One-argument with EMMC:file:size:sha1 should pass the check.
222 std::string filename = android::base::Join(
223 std::vector<std::string>{ "EMMC", src_file, std::to_string(src_size), src_hash }, ":");
224 std::string cmd = "apply_patch_check(\"" + filename + "\")";
225 expect("t", cmd.c_str(), kNoCause);
226
227 // EMMC:file:(size-1):sha1:(size+1):sha1 should fail the check.
228 std::string filename_bad = android::base::Join(
229 std::vector<std::string>{ "EMMC", src_file, std::to_string(src_size - 1), src_hash,
230 std::to_string(src_size + 1), src_hash },
231 ":");
232 cmd = "apply_patch_check(\"" + filename_bad + "\")";
233 expect("", cmd.c_str(), kNoCause);
234
235 // EMMC:file:(size-1):sha1:size:sha1:(size+1):sha1 should pass the check.
236 filename_bad =
237 android::base::Join(std::vector<std::string>{ "EMMC", src_file, std::to_string(src_size - 1),
238 src_hash, std::to_string(src_size), src_hash,
239 std::to_string(src_size + 1), src_hash },
240 ":");
241 cmd = "apply_patch_check(\"" + filename_bad + "\")";
242 expect("t", cmd.c_str(), kNoCause);
243
244 // Multiple arguments.
245 cmd = "apply_patch_check(\"" + filename + "\", \"wrong_sha1\", \"wrong_sha2\")";
246 expect("", cmd.c_str(), kNoCause);
247
248 cmd = "apply_patch_check(\"" + filename + "\", \"wrong_sha1\", \"" + src_hash +
249 "\", \"wrong_sha2\")";
250 expect("t", cmd.c_str(), kNoCause);
251
252 cmd = "apply_patch_check(\"" + filename_bad + "\", \"wrong_sha1\", \"" + src_hash +
253 "\", \"wrong_sha2\")";
254 expect("t", cmd.c_str(), kNoCause);
255}
256
Tao Bao51d516e2016-11-03 14:49:01 -0700257TEST_F(UpdaterTest, file_getprop) {
258 // file_getprop() expects two arguments.
259 expect(nullptr, "file_getprop()", kArgsParsingFailure);
260 expect(nullptr, "file_getprop(\"arg1\")", kArgsParsingFailure);
261 expect(nullptr, "file_getprop(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
262
263 // File doesn't exist.
264 expect(nullptr, "file_getprop(\"/doesntexist\", \"key1\")", kFileGetPropFailure);
265
266 // Reject too large files (current limit = 65536).
267 TemporaryFile temp_file1;
268 std::string buffer(65540, '\0');
269 ASSERT_TRUE(android::base::WriteStringToFile(buffer, temp_file1.path));
270
271 // Read some keys.
272 TemporaryFile temp_file2;
273 std::string content("ro.product.name=tardis\n"
274 "# comment\n\n\n"
275 "ro.product.model\n"
276 "ro.product.board = magic \n");
277 ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file2.path));
278
279 std::string script1("file_getprop(\"" + std::string(temp_file2.path) +
280 "\", \"ro.product.name\")");
281 expect("tardis", script1.c_str(), kNoCause);
282
283 std::string script2("file_getprop(\"" + std::string(temp_file2.path) +
284 "\", \"ro.product.board\")");
285 expect("magic", script2.c_str(), kNoCause);
286
287 // No match.
288 std::string script3("file_getprop(\"" + std::string(temp_file2.path) +
289 "\", \"ro.product.wrong\")");
290 expect("", script3.c_str(), kNoCause);
291
292 std::string script4("file_getprop(\"" + std::string(temp_file2.path) +
293 "\", \"ro.product.name=\")");
294 expect("", script4.c_str(), kNoCause);
295
296 std::string script5("file_getprop(\"" + std::string(temp_file2.path) +
297 "\", \"ro.product.nam\")");
298 expect("", script5.c_str(), kNoCause);
299
300 std::string script6("file_getprop(\"" + std::string(temp_file2.path) +
301 "\", \"ro.product.model\")");
302 expect("", script6.c_str(), kNoCause);
303}
Tao Bao0831d0b2016-11-03 23:25:04 -0700304
Tao Baoef0eb3b2016-11-14 21:29:52 -0800305// TODO: Test extracting to block device.
306TEST_F(UpdaterTest, package_extract_file) {
307 // package_extract_file expects 1 or 2 arguments.
308 expect(nullptr, "package_extract_file()", kArgsParsingFailure);
309 expect(nullptr, "package_extract_file(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
310
311 std::string zip_path = from_testdata_base("ziptest_valid.zip");
312 ZipArchiveHandle handle;
313 ASSERT_EQ(0, OpenArchive(zip_path.c_str(), &handle));
314
315 // Need to set up the ziphandle.
316 UpdaterInfo updater_info;
317 updater_info.package_zip = handle;
318
319 // Two-argument version.
320 TemporaryFile temp_file1;
321 std::string script("package_extract_file(\"a.txt\", \"" + std::string(temp_file1.path) + "\")");
322 expect("t", script.c_str(), kNoCause, &updater_info);
323
324 // Verify the extracted entry.
325 std::string data;
326 ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data));
327 ASSERT_EQ(kATxtContents, data);
328
329 // Now extract another entry to the same location, which should overwrite.
330 script = "package_extract_file(\"b.txt\", \"" + std::string(temp_file1.path) + "\")";
331 expect("t", script.c_str(), kNoCause, &updater_info);
332
333 ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data));
334 ASSERT_EQ(kBTxtContents, data);
335
336 // Missing zip entry. The two-argument version doesn't abort.
337 script = "package_extract_file(\"doesntexist\", \"" + std::string(temp_file1.path) + "\")";
338 expect("", script.c_str(), kNoCause, &updater_info);
339
340 // Extract to /dev/full should fail.
341 script = "package_extract_file(\"a.txt\", \"/dev/full\")";
342 expect("", script.c_str(), kNoCause, &updater_info);
343
344 // One-argument version.
345 script = "sha1_check(package_extract_file(\"a.txt\"))";
346 expect(kATxtSha1Sum.c_str(), script.c_str(), kNoCause, &updater_info);
347
348 script = "sha1_check(package_extract_file(\"b.txt\"))";
349 expect(kBTxtSha1Sum.c_str(), script.c_str(), kNoCause, &updater_info);
350
351 // Missing entry. The one-argument version aborts the evaluation.
352 script = "package_extract_file(\"doesntexist\")";
353 expect(nullptr, script.c_str(), kPackageExtractFileFailure, &updater_info);
354
355 CloseArchive(handle);
356}
Tao Baod0f30882016-11-03 23:52:01 -0700357
358TEST_F(UpdaterTest, write_value) {
359 // write_value() expects two arguments.
360 expect(nullptr, "write_value()", kArgsParsingFailure);
361 expect(nullptr, "write_value(\"arg1\")", kArgsParsingFailure);
362 expect(nullptr, "write_value(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
363
364 // filename cannot be empty.
365 expect(nullptr, "write_value(\"value\", \"\")", kArgsParsingFailure);
366
367 // Write some value to file.
368 TemporaryFile temp_file;
369 std::string value = "magicvalue";
370 std::string script("write_value(\"" + value + "\", \"" + std::string(temp_file.path) + "\")");
371 expect("t", script.c_str(), kNoCause);
372
373 // Verify the content.
374 std::string content;
375 ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content));
376 ASSERT_EQ(value, content);
377
378 // Allow writing empty string.
379 script = "write_value(\"\", \"" + std::string(temp_file.path) + "\")";
380 expect("t", script.c_str(), kNoCause);
381
382 // Verify the content.
383 ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content));
384 ASSERT_EQ("", content);
385
386 // It should fail gracefully when write fails.
387 script = "write_value(\"value\", \"/proc/0/file1\")";
388 expect("", script.c_str(), kNoCause);
389}
Tao Baobedf5fc2016-11-18 12:01:26 -0800390
391TEST_F(UpdaterTest, get_stage) {
392 // get_stage() expects one argument.
393 expect(nullptr, "get_stage()", kArgsParsingFailure);
394 expect(nullptr, "get_stage(\"arg1\", \"arg2\")", kArgsParsingFailure);
395 expect(nullptr, "get_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
396
397 // Set up a local file as BCB.
398 TemporaryFile tf;
399 std::string temp_file(tf.path);
400 bootloader_message boot;
401 strlcpy(boot.stage, "2/3", sizeof(boot.stage));
402 std::string err;
403 ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err));
404
405 // Can read the stage value.
406 std::string script("get_stage(\"" + temp_file + "\")");
407 expect("2/3", script.c_str(), kNoCause);
408
409 // Bad BCB path.
410 script = "get_stage(\"doesntexist\")";
411 expect("", script.c_str(), kNoCause);
412}
413
414TEST_F(UpdaterTest, set_stage) {
415 // set_stage() expects two arguments.
416 expect(nullptr, "set_stage()", kArgsParsingFailure);
417 expect(nullptr, "set_stage(\"arg1\")", kArgsParsingFailure);
418 expect(nullptr, "set_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
419
420 // Set up a local file as BCB.
421 TemporaryFile tf;
422 std::string temp_file(tf.path);
423 bootloader_message boot;
424 strlcpy(boot.command, "command", sizeof(boot.command));
425 strlcpy(boot.stage, "2/3", sizeof(boot.stage));
426 std::string err;
427 ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err));
428
429 // Write with set_stage().
430 std::string script("set_stage(\"" + temp_file + "\", \"1/3\")");
431 expect(tf.path, script.c_str(), kNoCause);
432
433 // Verify.
434 bootloader_message boot_verify;
435 ASSERT_TRUE(read_bootloader_message_from(&boot_verify, temp_file, &err));
436
437 // Stage should be updated, with command part untouched.
438 ASSERT_STREQ("1/3", boot_verify.stage);
439 ASSERT_STREQ(boot.command, boot_verify.command);
440
441 // Bad BCB path.
442 script = "set_stage(\"doesntexist\", \"1/3\")";
443 expect("", script.c_str(), kNoCause);
444
445 script = "set_stage(\"/dev/full\", \"1/3\")";
446 expect("", script.c_str(), kNoCause);
447}
Tao Bao9aa7ab52017-01-05 17:27:19 -0800448
449TEST_F(UpdaterTest, set_progress) {
450 // set_progress() expects one argument.
451 expect(nullptr, "set_progress()", kArgsParsingFailure);
452 expect(nullptr, "set_progress(\"arg1\", \"arg2\")", kArgsParsingFailure);
453
454 // Invalid progress argument.
455 expect(nullptr, "set_progress(\"arg1\")", kArgsParsingFailure);
456 expect(nullptr, "set_progress(\"3x+5\")", kArgsParsingFailure);
457 expect(nullptr, "set_progress(\".3.5\")", kArgsParsingFailure);
458
459 TemporaryFile tf;
460 UpdaterInfo updater_info;
Tianjie Xu79327ac2017-09-08 17:09:10 -0700461 updater_info.cmd_pipe = fdopen(tf.release(), "w");
Tao Bao9aa7ab52017-01-05 17:27:19 -0800462 expect(".52", "set_progress(\".52\")", kNoCause, &updater_info);
463 fflush(updater_info.cmd_pipe);
464
465 std::string cmd;
466 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd));
467 ASSERT_EQ(android::base::StringPrintf("set_progress %f\n", .52), cmd);
468 // recovery-updater protocol expects 2 tokens ("set_progress <frac>").
469 ASSERT_EQ(2U, android::base::Split(cmd, " ").size());
Tianjie Xu79327ac2017-09-08 17:09:10 -0700470 ASSERT_EQ(0, fclose(updater_info.cmd_pipe));
Tao Bao9aa7ab52017-01-05 17:27:19 -0800471}
472
473TEST_F(UpdaterTest, show_progress) {
474 // show_progress() expects two arguments.
475 expect(nullptr, "show_progress()", kArgsParsingFailure);
476 expect(nullptr, "show_progress(\"arg1\")", kArgsParsingFailure);
477 expect(nullptr, "show_progress(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
478
479 // Invalid progress arguments.
480 expect(nullptr, "show_progress(\"arg1\", \"arg2\")", kArgsParsingFailure);
481 expect(nullptr, "show_progress(\"3x+5\", \"10\")", kArgsParsingFailure);
482 expect(nullptr, "show_progress(\".3\", \"5a\")", kArgsParsingFailure);
483
484 TemporaryFile tf;
485 UpdaterInfo updater_info;
Tianjie Xu79327ac2017-09-08 17:09:10 -0700486 updater_info.cmd_pipe = fdopen(tf.release(), "w");
Tao Bao9aa7ab52017-01-05 17:27:19 -0800487 expect(".52", "show_progress(\".52\", \"10\")", kNoCause, &updater_info);
488 fflush(updater_info.cmd_pipe);
489
490 std::string cmd;
491 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd));
492 ASSERT_EQ(android::base::StringPrintf("progress %f %d\n", .52, 10), cmd);
493 // recovery-updater protocol expects 3 tokens ("progress <frac> <secs>").
494 ASSERT_EQ(3U, android::base::Split(cmd, " ").size());
Tianjie Xu79327ac2017-09-08 17:09:10 -0700495 ASSERT_EQ(0, fclose(updater_info.cmd_pipe));
Tao Bao9aa7ab52017-01-05 17:27:19 -0800496}
Tianjie Xu56ebe622017-03-16 00:48:21 -0700497
Tianjie Xu5450c842017-10-18 13:15:21 -0700498TEST_F(UpdaterTest, block_image_update_patch_data) {
Tianjie Xu56ebe622017-03-16 00:48:21 -0700499 std::string src_content = std::string(4096, 'a') + std::string(4096, 'c');
500 std::string tgt_content = std::string(4096, 'b') + std::string(4096, 'd');
Tianjie Xu5450c842017-10-18 13:15:21 -0700501
502 // Generate the patch data.
Tianjie Xu56ebe622017-03-16 00:48:21 -0700503 TemporaryFile patch_file;
Tao Baobc4a6d52018-05-23 00:12:21 -0700504 ASSERT_EQ(0,
505 bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(src_content.data()), src_content.size(),
506 reinterpret_cast<const uint8_t*>(tgt_content.data()), tgt_content.size(),
507 patch_file.path, nullptr));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700508 std::string patch_content;
509 ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch_content));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700510
Tianjie Xu5450c842017-10-18 13:15:21 -0700511 // Create the transfer list that contains a bsdiff.
Tianjie Xu56ebe622017-03-16 00:48:21 -0700512 std::string src_hash = get_sha1(src_content);
513 std::string tgt_hash = get_sha1(tgt_content);
Tao Baobc4a6d52018-05-23 00:12:21 -0700514 std::vector<std::string> transfer_list{
515 // clang-format off
Tianjie Xu56ebe622017-03-16 00:48:21 -0700516 "4",
517 "2",
518 "0",
519 "2",
520 "stash " + src_hash + " 2,0,2",
521 android::base::StringPrintf("bsdiff 0 %zu %s %s 2,0,2 2 - %s:2,0,2", patch_content.size(),
522 src_hash.c_str(), tgt_hash.c_str(), src_hash.c_str()),
523 "free " + src_hash,
Tao Baobc4a6d52018-05-23 00:12:21 -0700524 // clang-format on
Tianjie Xu56ebe622017-03-16 00:48:21 -0700525 };
Tianjie Xu56ebe622017-03-16 00:48:21 -0700526
Tao Baobc4a6d52018-05-23 00:12:21 -0700527 PackageEntries entries{
Tianjie Xu5450c842017-10-18 13:15:21 -0700528 { "new_data", "" },
529 { "patch_data", patch_content },
530 { "transfer_list", android::base::Join(transfer_list, '\n') },
Tianjie Xu56ebe622017-03-16 00:48:21 -0700531 };
Tianjie Xu5450c842017-10-18 13:15:21 -0700532
Tao Baobc4a6d52018-05-23 00:12:21 -0700533 ASSERT_TRUE(android::base::WriteStringToFile(src_content, image_file_));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700534
Tao Baobc4a6d52018-05-23 00:12:21 -0700535 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu56ebe622017-03-16 00:48:21 -0700536
Tianjie Xu56ebe622017-03-16 00:48:21 -0700537 // The update_file should be patched correctly.
538 std::string updated_content;
Tao Baobc4a6d52018-05-23 00:12:21 -0700539 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_content));
540 ASSERT_EQ(tgt_content, updated_content);
Tianjie Xu5450c842017-10-18 13:15:21 -0700541}
542
543TEST_F(UpdaterTest, block_image_update_fail) {
544 std::string src_content(4096 * 2, 'e');
545 std::string src_hash = get_sha1(src_content);
546 // Stash and free some blocks, then fail the update intentionally.
Tao Baobc4a6d52018-05-23 00:12:21 -0700547 std::vector<std::string> transfer_list{
548 // clang-format off
549 "4",
550 "2",
551 "0",
552 "2",
553 "stash " + src_hash + " 2,0,2",
554 "free " + src_hash,
555 "fail",
556 // clang-format on
Tianjie Xu5450c842017-10-18 13:15:21 -0700557 };
558
559 // Add a new data of 10 bytes to test the deadlock.
Tao Baobc4a6d52018-05-23 00:12:21 -0700560 PackageEntries entries{
Tianjie Xu5450c842017-10-18 13:15:21 -0700561 { "new_data", std::string(10, 0) },
562 { "patch_data", "" },
563 { "transfer_list", android::base::Join(transfer_list, '\n') },
564 };
565
Tao Baobc4a6d52018-05-23 00:12:21 -0700566 ASSERT_TRUE(android::base::WriteStringToFile(src_content, image_file_));
Tianjie Xu5450c842017-10-18 13:15:21 -0700567
Tao Baobc4a6d52018-05-23 00:12:21 -0700568 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu5450c842017-10-18 13:15:21 -0700569
Tianjie Xu56ebe622017-03-16 00:48:21 -0700570 // Updater generates the stash name based on the input file name.
Tao Baobc4a6d52018-05-23 00:12:21 -0700571 std::string name_digest = get_sha1(image_file_);
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800572 std::string stash_base = std::string(temp_stash_base_.path) + "/" + name_digest;
Tianjie Xu56ebe622017-03-16 00:48:21 -0700573 ASSERT_EQ(0, access(stash_base.c_str(), F_OK));
Tao Baobc4a6d52018-05-23 00:12:21 -0700574 // Expect the stashed blocks to be freed.
Tianjie Xu5450c842017-10-18 13:15:21 -0700575 ASSERT_EQ(-1, access((stash_base + src_hash).c_str(), F_OK));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700576 ASSERT_EQ(0, rmdir(stash_base.c_str()));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700577}
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700578
Tianjie Xu5450c842017-10-18 13:15:21 -0700579TEST_F(UpdaterTest, new_data_over_write) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700580 std::vector<std::string> transfer_list{
581 // clang-format off
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700582 "4",
583 "1",
584 "0",
585 "0",
586 "new 2,0,1",
Tao Baobc4a6d52018-05-23 00:12:21 -0700587 // clang-format on
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700588 };
Tianjie Xu5450c842017-10-18 13:15:21 -0700589
Tao Baobc4a6d52018-05-23 00:12:21 -0700590 // Write 4096 + 100 bytes of new data.
591 PackageEntries entries{
592 { "new_data", std::string(4196, 0) },
Tianjie Xu5450c842017-10-18 13:15:21 -0700593 { "patch_data", "" },
594 { "transfer_list", android::base::Join(transfer_list, '\n') },
595 };
596
Tao Baobc4a6d52018-05-23 00:12:21 -0700597 RunBlockImageUpdate(false, entries, image_file_, "t");
598}
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700599
Tao Baobc4a6d52018-05-23 00:12:21 -0700600TEST_F(UpdaterTest, new_data_short_write) {
601 std::vector<std::string> transfer_list{
602 // clang-format off
603 "4",
604 "1",
605 "0",
606 "0",
607 "new 2,0,1",
608 // clang-format on
609 };
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700610
Tao Baobc4a6d52018-05-23 00:12:21 -0700611 PackageEntries entries{
612 { "patch_data", "" },
613 { "transfer_list", android::base::Join(transfer_list, '\n') },
614 };
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700615
616 // Updater should report the failure gracefully rather than stuck in deadlock.
Tao Baobc4a6d52018-05-23 00:12:21 -0700617 entries["new_data"] = "";
618 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700619
Tao Baobc4a6d52018-05-23 00:12:21 -0700620 entries["new_data"] = std::string(10, 'a');
621 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700622
623 // Expect to write 1 block of new data successfully.
Tao Baobc4a6d52018-05-23 00:12:21 -0700624 entries["new_data"] = std::string(4096, 'a');
625 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu107a34f2017-06-29 17:04:21 -0700626}
627
628TEST_F(UpdaterTest, brotli_new_data) {
Tianjie Xu107a34f2017-06-29 17:04:21 -0700629 auto generator = []() { return rand() % 128; };
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700630 // Generate 100 blocks of random data.
Tianjie Xu107a34f2017-06-29 17:04:21 -0700631 std::string brotli_new_data;
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700632 brotli_new_data.reserve(4096 * 100);
633 generate_n(back_inserter(brotli_new_data), 4096 * 100, generator);
Tianjie Xu107a34f2017-06-29 17:04:21 -0700634
635 size_t encoded_size = BrotliEncoderMaxCompressedSize(brotli_new_data.size());
Tianjie Xu5450c842017-10-18 13:15:21 -0700636 std::string encoded_data(encoded_size, 0);
Tianjie Xu107a34f2017-06-29 17:04:21 -0700637 ASSERT_TRUE(BrotliEncoderCompress(
638 BROTLI_DEFAULT_QUALITY, BROTLI_DEFAULT_WINDOW, BROTLI_DEFAULT_MODE, brotli_new_data.size(),
Tianjie Xu5450c842017-10-18 13:15:21 -0700639 reinterpret_cast<const uint8_t*>(brotli_new_data.data()), &encoded_size,
640 reinterpret_cast<uint8_t*>(const_cast<char*>(encoded_data.data()))));
641 encoded_data.resize(encoded_size);
Tianjie Xu107a34f2017-06-29 17:04:21 -0700642
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700643 // Write a few small chunks of new data, then a large chunk, and finally a few small chunks.
644 // This helps us to catch potential short writes.
Tianjie Xu107a34f2017-06-29 17:04:21 -0700645 std::vector<std::string> transfer_list = {
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700646 "4",
647 "100",
648 "0",
649 "0",
650 "new 2,0,1",
651 "new 2,1,2",
652 "new 4,2,50,50,97",
653 "new 2,97,98",
654 "new 2,98,99",
655 "new 2,99,100",
Tianjie Xu107a34f2017-06-29 17:04:21 -0700656 };
Tianjie Xu5450c842017-10-18 13:15:21 -0700657
Tao Baobc4a6d52018-05-23 00:12:21 -0700658 PackageEntries entries{
659 { "new_data.br", std::move(encoded_data) },
Tianjie Xu5450c842017-10-18 13:15:21 -0700660 { "patch_data", "" },
661 { "transfer_list", android::base::Join(transfer_list, '\n') },
662 };
663
Tao Baobc4a6d52018-05-23 00:12:21 -0700664 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu107a34f2017-06-29 17:04:21 -0700665
666 std::string updated_content;
Tao Baobc4a6d52018-05-23 00:12:21 -0700667 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_content));
Tianjie Xu107a34f2017-06-29 17:04:21 -0700668 ASSERT_EQ(brotli_new_data, updated_content);
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700669}
Tianjie Xu284752e2017-12-05 11:04:17 -0800670
671TEST_F(UpdaterTest, last_command_update) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700672 std::string block1(4096, '1');
673 std::string block2(4096, '2');
674 std::string block3(4096, '3');
Tianjie Xu284752e2017-12-05 11:04:17 -0800675 std::string block1_hash = get_sha1(block1);
676 std::string block2_hash = get_sha1(block2);
677 std::string block3_hash = get_sha1(block3);
678
679 // Compose the transfer list to fail the first update.
Tao Baobc4a6d52018-05-23 00:12:21 -0700680 std::vector<std::string> transfer_list_fail{
681 // clang-format off
Tianjie Xu284752e2017-12-05 11:04:17 -0800682 "4",
683 "2",
684 "0",
685 "2",
686 "stash " + block1_hash + " 2,0,1",
687 "move " + block1_hash + " 2,1,2 1 2,0,1",
688 "stash " + block3_hash + " 2,2,3",
689 "fail",
Tao Baobc4a6d52018-05-23 00:12:21 -0700690 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -0800691 };
692
693 // Mimic a resumed update with the same transfer commands.
Tao Baobc4a6d52018-05-23 00:12:21 -0700694 std::vector<std::string> transfer_list_continue{
695 // clang-format off
Tianjie Xu284752e2017-12-05 11:04:17 -0800696 "4",
697 "2",
698 "0",
699 "2",
700 "stash " + block1_hash + " 2,0,1",
701 "move " + block1_hash + " 2,1,2 1 2,0,1",
702 "stash " + block3_hash + " 2,2,3",
703 "move " + block1_hash + " 2,2,3 1 2,0,1",
Tao Baobc4a6d52018-05-23 00:12:21 -0700704 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -0800705 };
706
Tao Baobc4a6d52018-05-23 00:12:21 -0700707 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_));
708
709 PackageEntries entries{
Tianjie Xu284752e2017-12-05 11:04:17 -0800710 { "new_data", "" },
711 { "patch_data", "" },
Tao Baobc4a6d52018-05-23 00:12:21 -0700712 { "transfer_list", android::base::Join(transfer_list_fail, '\n') },
Tianjie Xu284752e2017-12-05 11:04:17 -0800713 };
714
Tao Baobc4a6d52018-05-23 00:12:21 -0700715 // "2\nstash " + block3_hash + " 2,2,3"
716 std::string last_command_content = "2\n" + transfer_list_fail[kTransferListHeaderLines + 2];
717 TemporaryFile last_command_file;
718 Paths::Get().set_last_command_file(last_command_file.path);
Tianjie Xu284752e2017-12-05 11:04:17 -0800719
Tao Baobc4a6d52018-05-23 00:12:21 -0700720 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu284752e2017-12-05 11:04:17 -0800721
722 // Expect last_command to contain the last stash command.
Tao Baobc4a6d52018-05-23 00:12:21 -0700723 std::string last_command_actual;
724 ASSERT_TRUE(android::base::ReadFileToString(last_command_file.path, &last_command_actual));
725 EXPECT_EQ(last_command_content, last_command_actual);
726
Tianjie Xu284752e2017-12-05 11:04:17 -0800727 std::string updated_contents;
Tao Baobc4a6d52018-05-23 00:12:21 -0700728 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_contents));
Tianjie Xu284752e2017-12-05 11:04:17 -0800729 ASSERT_EQ(block1 + block1 + block3, updated_contents);
730
Tao Baobc4a6d52018-05-23 00:12:21 -0700731 // "Resume" the update. Expect the first 'move' to be skipped but the second 'move' to be
732 // executed. Note that we intentionally reset the image file.
733 entries["transfer_list"] = android::base::Join(transfer_list_continue, '\n');
734 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_));
735 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu284752e2017-12-05 11:04:17 -0800736
Tao Baobc4a6d52018-05-23 00:12:21 -0700737 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_contents));
738 ASSERT_EQ(block1 + block2 + block1, updated_contents);
Tianjie Xu284752e2017-12-05 11:04:17 -0800739}
740
741TEST_F(UpdaterTest, last_command_update_unresumable) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700742 std::string block1(4096, '1');
743 std::string block2(4096, '2');
Tianjie Xu284752e2017-12-05 11:04:17 -0800744 std::string block1_hash = get_sha1(block1);
745 std::string block2_hash = get_sha1(block2);
746
747 // Construct an unresumable update with source blocks mismatch.
Tao Baobc4a6d52018-05-23 00:12:21 -0700748 std::vector<std::string> transfer_list_unresumable{
749 // clang-format off
750 "4",
751 "2",
752 "0",
753 "2",
754 "stash " + block1_hash + " 2,0,1",
755 "move " + block2_hash + " 2,1,2 1 2,0,1",
756 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -0800757 };
758
Tao Baobc4a6d52018-05-23 00:12:21 -0700759 PackageEntries entries{
Tianjie Xu284752e2017-12-05 11:04:17 -0800760 { "new_data", "" },
761 { "patch_data", "" },
Tao Baobc4a6d52018-05-23 00:12:21 -0700762 { "transfer_list", android::base::Join(transfer_list_unresumable, '\n') },
Tianjie Xu284752e2017-12-05 11:04:17 -0800763 };
764
Tao Baobc4a6d52018-05-23 00:12:21 -0700765 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block1, image_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -0800766
Tao Baobc4a6d52018-05-23 00:12:21 -0700767 // Set up the last_command_file.
768 TemporaryFile last_command_file;
769 Paths::Get().set_last_command_file(last_command_file.path);
770 std::string last_command_content = "0\n" + transfer_list_unresumable[kTransferListHeaderLines];
771 ASSERT_TRUE(android::base::WriteStringToFile(last_command_content, last_command_file.path));
Tianjie Xu284752e2017-12-05 11:04:17 -0800772
Tao Baobc4a6d52018-05-23 00:12:21 -0700773 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu284752e2017-12-05 11:04:17 -0800774
Tao Baobc4a6d52018-05-23 00:12:21 -0700775 // The last_command_file will be deleted if the update encounters an unresumable failure later.
776 ASSERT_EQ(-1, access(last_command_file.path, R_OK));
Tianjie Xu284752e2017-12-05 11:04:17 -0800777}
778
779TEST_F(UpdaterTest, last_command_verify) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700780 std::string block1(4096, '1');
781 std::string block2(4096, '2');
782 std::string block3(4096, '3');
Tianjie Xu284752e2017-12-05 11:04:17 -0800783 std::string block1_hash = get_sha1(block1);
784 std::string block2_hash = get_sha1(block2);
785 std::string block3_hash = get_sha1(block3);
786
Tao Baobc4a6d52018-05-23 00:12:21 -0700787 std::vector<std::string> transfer_list_verify{
788 // clang-format off
Tianjie Xu284752e2017-12-05 11:04:17 -0800789 "4",
790 "2",
791 "0",
792 "2",
793 "stash " + block1_hash + " 2,0,1",
794 "move " + block1_hash + " 2,0,1 1 2,0,1",
795 "move " + block1_hash + " 2,1,2 1 2,0,1",
796 "stash " + block3_hash + " 2,2,3",
Tao Baobc4a6d52018-05-23 00:12:21 -0700797 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -0800798 };
799
Tao Baobc4a6d52018-05-23 00:12:21 -0700800 PackageEntries entries{
Tianjie Xu284752e2017-12-05 11:04:17 -0800801 { "new_data", "" },
802 { "patch_data", "" },
Tao Baobc4a6d52018-05-23 00:12:21 -0700803 { "transfer_list", android::base::Join(transfer_list_verify, '\n') },
Tianjie Xu284752e2017-12-05 11:04:17 -0800804 };
805
Tao Baobc4a6d52018-05-23 00:12:21 -0700806 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block1 + block3, image_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -0800807
Tao Baobc4a6d52018-05-23 00:12:21 -0700808 TemporaryFile last_command_file;
809 Paths::Get().set_last_command_file(last_command_file.path);
810 // Last command: "move " + block1_hash + " 2,1,2 1 2,0,1"
811 std::string last_command_content = "2\n" + transfer_list_verify[kTransferListHeaderLines + 2];
Tianjie Xu284752e2017-12-05 11:04:17 -0800812
Tao Baobc4a6d52018-05-23 00:12:21 -0700813 // First run: expect the verification to succeed and the last_command_file is intact.
814 ASSERT_TRUE(android::base::WriteStringToFile(last_command_content, last_command_file.path));
Tianjie Xu284752e2017-12-05 11:04:17 -0800815
Tao Baobc4a6d52018-05-23 00:12:21 -0700816 RunBlockImageUpdate(true, entries, image_file_, "t");
Tianjie Xu284752e2017-12-05 11:04:17 -0800817
Tao Baobc4a6d52018-05-23 00:12:21 -0700818 std::string last_command_actual;
819 ASSERT_TRUE(android::base::ReadFileToString(last_command_file.path, &last_command_actual));
820 EXPECT_EQ(last_command_content, last_command_actual);
Tianjie Xu284752e2017-12-05 11:04:17 -0800821
Tao Baobc4a6d52018-05-23 00:12:21 -0700822 // Second run with a mismatching block image: expect the verification to succeed but
823 // last_command_file to be deleted; because the target blocks in the last command don't have the
824 // expected contents for the second move command.
825 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_));
826 RunBlockImageUpdate(true, entries, image_file_, "t");
827 ASSERT_EQ(-1, access(last_command_file.path, R_OK));
Tianjie Xu284752e2017-12-05 11:04:17 -0800828}