blob: 5d3b2d996140e52c8179a4230468cf72d01da10a [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 Bao0c7839a2016-10-10 15:48:37 -070030#include <android-base/properties.h>
Tao Bao9aa7ab52017-01-05 17:27:19 -080031#include <android-base/stringprintf.h>
32#include <android-base/strings.h>
Tao Bao51d516e2016-11-03 14:49:01 -070033#include <android-base/test_utils.h>
Tao Baobedf5fc2016-11-18 12:01:26 -080034#include <bootloader_message/bootloader_message.h>
Tianjie Xu107a34f2017-06-29 17:04:21 -070035#include <brotli/encode.h>
Alex Deymofa188262017-10-10 17:56:17 +020036#include <bsdiff/bsdiff.h>
Tao Bao0c7839a2016-10-10 15:48:37 -070037#include <gtest/gtest.h>
Tao Baoef0eb3b2016-11-14 21:29:52 -080038#include <ziparchive/zip_archive.h>
Tianjie Xu56ebe622017-03-16 00:48:21 -070039#include <ziparchive/zip_writer.h>
Tao Bao0c7839a2016-10-10 15:48:37 -070040
Tao Baoef0eb3b2016-11-14 21:29:52 -080041#include "common/test_constants.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070042#include "edify/expr.h"
Tianjie Xu56ebe622017-03-16 00:48:21 -070043#include "otautil/SysUtil.h"
Tao Bao1fc5bf32017-10-06 07:43:41 -070044#include "otautil/error_code.h"
Tao Bao641fa972018-04-25 18:59:40 -070045#include "otautil/paths.h"
Tao Bao09e468f2017-09-29 14:39:33 -070046#include "otautil/print_sha1.h"
Tianjie Xu56ebe622017-03-16 00:48:21 -070047#include "updater/blockimg.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070048#include "updater/install.h"
Tao Baoef0eb3b2016-11-14 21:29:52 -080049#include "updater/updater.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070050
51struct selabel_handle *sehandle = nullptr;
52
Tao Baoef0eb3b2016-11-14 21:29:52 -080053static void expect(const char* expected, const char* expr_str, CauseCode cause_code,
54 UpdaterInfo* info = nullptr) {
Tianjie Xuc4447322017-03-06 14:44:59 -080055 std::unique_ptr<Expr> e;
Tao Baoef0eb3b2016-11-14 21:29:52 -080056 int error_count = 0;
57 ASSERT_EQ(0, parse_string(expr_str, &e, &error_count));
58 ASSERT_EQ(0, error_count);
Tao Bao0c7839a2016-10-10 15:48:37 -070059
Tao Baoef0eb3b2016-11-14 21:29:52 -080060 State state(expr_str, info);
Tao Bao0c7839a2016-10-10 15:48:37 -070061
Tao Baoef0eb3b2016-11-14 21:29:52 -080062 std::string result;
63 bool status = Evaluate(&state, e, &result);
Tao Bao0c7839a2016-10-10 15:48:37 -070064
Tao Baoef0eb3b2016-11-14 21:29:52 -080065 if (expected == nullptr) {
66 ASSERT_FALSE(status);
67 } else {
68 ASSERT_TRUE(status);
69 ASSERT_STREQ(expected, result.c_str());
70 }
Tao Bao0c7839a2016-10-10 15:48:37 -070071
Tao Baoef0eb3b2016-11-14 21:29:52 -080072 // Error code is set in updater/updater.cpp only, by parsing State.errmsg.
73 ASSERT_EQ(kNoError, state.error_code);
Tao Bao361342c2016-02-08 11:15:50 -080074
Tao Baoef0eb3b2016-11-14 21:29:52 -080075 // Cause code should always be available.
76 ASSERT_EQ(cause_code, state.cause_code);
Tao Bao0c7839a2016-10-10 15:48:37 -070077}
78
Tianjie Xu5450c842017-10-18 13:15:21 -070079static void BuildUpdatePackage(const std::unordered_map<std::string, std::string>& entries,
80 int fd) {
81 FILE* zip_file_ptr = fdopen(fd, "wb");
82 ZipWriter zip_writer(zip_file_ptr);
83
84 for (const auto& entry : entries) {
85 ASSERT_EQ(0, zip_writer.StartEntry(entry.first.c_str(), 0));
86 if (!entry.second.empty()) {
87 ASSERT_EQ(0, zip_writer.WriteBytes(entry.second.data(), entry.second.size()));
88 }
89 ASSERT_EQ(0, zip_writer.FinishEntry());
90 }
91
92 ASSERT_EQ(0, zip_writer.Finish());
93 ASSERT_EQ(0, fclose(zip_file_ptr));
94}
95
Tianjie Xu56ebe622017-03-16 00:48:21 -070096static std::string get_sha1(const std::string& content) {
97 uint8_t digest[SHA_DIGEST_LENGTH];
98 SHA1(reinterpret_cast<const uint8_t*>(content.c_str()), content.size(), digest);
99 return print_sha1(digest);
100}
101
Tao Bao0c7839a2016-10-10 15:48:37 -0700102class UpdaterTest : public ::testing::Test {
Tianjie Xu56ebe622017-03-16 00:48:21 -0700103 protected:
104 virtual void SetUp() override {
105 RegisterBuiltins();
106 RegisterInstallFunctions();
107 RegisterBlockImageFunctions();
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800108
Tao Bao641fa972018-04-25 18:59:40 -0700109 Paths::Get().set_cache_temp_source(temp_saved_source_.path);
110 Paths::Get().set_last_command_file(temp_last_command_.path);
111 Paths::Get().set_stash_directory_base(temp_stash_base_.path);
Tianjie Xu56ebe622017-03-16 00:48:21 -0700112 }
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800113
114 TemporaryFile temp_saved_source_;
115 TemporaryFile temp_last_command_;
116 TemporaryDir temp_stash_base_;
Tao Bao0c7839a2016-10-10 15:48:37 -0700117};
118
119TEST_F(UpdaterTest, getprop) {
120 expect(android::base::GetProperty("ro.product.device", "").c_str(),
121 "getprop(\"ro.product.device\")",
Tao Bao361342c2016-02-08 11:15:50 -0800122 kNoCause);
Tao Bao0c7839a2016-10-10 15:48:37 -0700123
124 expect(android::base::GetProperty("ro.build.fingerprint", "").c_str(),
125 "getprop(\"ro.build.fingerprint\")",
Tao Bao361342c2016-02-08 11:15:50 -0800126 kNoCause);
Tao Bao0c7839a2016-10-10 15:48:37 -0700127
128 // getprop() accepts only one parameter.
Tao Bao361342c2016-02-08 11:15:50 -0800129 expect(nullptr, "getprop()", kArgsParsingFailure);
130 expect(nullptr, "getprop(\"arg1\", \"arg2\")", kArgsParsingFailure);
131}
132
133TEST_F(UpdaterTest, sha1_check) {
134 // sha1_check(data) returns the SHA-1 of the data.
135 expect("81fe8bfe87576c3ecb22426f8e57847382917acf", "sha1_check(\"abcd\")", kNoCause);
136 expect("da39a3ee5e6b4b0d3255bfef95601890afd80709", "sha1_check(\"\")", kNoCause);
137
138 // sha1_check(data, sha1_hex, [sha1_hex, ...]) returns the matched SHA-1.
139 expect("81fe8bfe87576c3ecb22426f8e57847382917acf",
140 "sha1_check(\"abcd\", \"81fe8bfe87576c3ecb22426f8e57847382917acf\")",
141 kNoCause);
142
143 expect("81fe8bfe87576c3ecb22426f8e57847382917acf",
144 "sha1_check(\"abcd\", \"wrong_sha1\", \"81fe8bfe87576c3ecb22426f8e57847382917acf\")",
145 kNoCause);
146
147 // Or "" if there's no match.
148 expect("",
149 "sha1_check(\"abcd\", \"wrong_sha1\")",
150 kNoCause);
151
152 expect("",
153 "sha1_check(\"abcd\", \"wrong_sha1\", \"wrong_sha2\")",
154 kNoCause);
155
156 // sha1_check() expects at least one argument.
157 expect(nullptr, "sha1_check()", kArgsParsingFailure);
Tao Bao0c7839a2016-10-10 15:48:37 -0700158}
Tao Bao51d516e2016-11-03 14:49:01 -0700159
Tao Baodb56eb02017-03-23 06:34:20 -0700160TEST_F(UpdaterTest, apply_patch_check) {
161 // Zero-argument is not valid.
162 expect(nullptr, "apply_patch_check()", kArgsParsingFailure);
163
164 // File not found.
165 expect("", "apply_patch_check(\"/doesntexist\")", kNoCause);
166
167 std::string src_file = from_testdata_base("old.file");
168 std::string src_content;
169 ASSERT_TRUE(android::base::ReadFileToString(src_file, &src_content));
170 size_t src_size = src_content.size();
171 std::string src_hash = get_sha1(src_content);
172
173 // One-argument with EMMC:file:size:sha1 should pass the check.
174 std::string filename = android::base::Join(
175 std::vector<std::string>{ "EMMC", src_file, std::to_string(src_size), src_hash }, ":");
176 std::string cmd = "apply_patch_check(\"" + filename + "\")";
177 expect("t", cmd.c_str(), kNoCause);
178
179 // EMMC:file:(size-1):sha1:(size+1):sha1 should fail the check.
180 std::string filename_bad = android::base::Join(
181 std::vector<std::string>{ "EMMC", src_file, std::to_string(src_size - 1), src_hash,
182 std::to_string(src_size + 1), src_hash },
183 ":");
184 cmd = "apply_patch_check(\"" + filename_bad + "\")";
185 expect("", cmd.c_str(), kNoCause);
186
187 // EMMC:file:(size-1):sha1:size:sha1:(size+1):sha1 should pass the check.
188 filename_bad =
189 android::base::Join(std::vector<std::string>{ "EMMC", src_file, std::to_string(src_size - 1),
190 src_hash, std::to_string(src_size), src_hash,
191 std::to_string(src_size + 1), src_hash },
192 ":");
193 cmd = "apply_patch_check(\"" + filename_bad + "\")";
194 expect("t", cmd.c_str(), kNoCause);
195
196 // Multiple arguments.
197 cmd = "apply_patch_check(\"" + filename + "\", \"wrong_sha1\", \"wrong_sha2\")";
198 expect("", cmd.c_str(), kNoCause);
199
200 cmd = "apply_patch_check(\"" + filename + "\", \"wrong_sha1\", \"" + src_hash +
201 "\", \"wrong_sha2\")";
202 expect("t", cmd.c_str(), kNoCause);
203
204 cmd = "apply_patch_check(\"" + filename_bad + "\", \"wrong_sha1\", \"" + src_hash +
205 "\", \"wrong_sha2\")";
206 expect("t", cmd.c_str(), kNoCause);
207}
208
Tao Bao51d516e2016-11-03 14:49:01 -0700209TEST_F(UpdaterTest, file_getprop) {
210 // file_getprop() expects two arguments.
211 expect(nullptr, "file_getprop()", kArgsParsingFailure);
212 expect(nullptr, "file_getprop(\"arg1\")", kArgsParsingFailure);
213 expect(nullptr, "file_getprop(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
214
215 // File doesn't exist.
216 expect(nullptr, "file_getprop(\"/doesntexist\", \"key1\")", kFileGetPropFailure);
217
218 // Reject too large files (current limit = 65536).
219 TemporaryFile temp_file1;
220 std::string buffer(65540, '\0');
221 ASSERT_TRUE(android::base::WriteStringToFile(buffer, temp_file1.path));
222
223 // Read some keys.
224 TemporaryFile temp_file2;
225 std::string content("ro.product.name=tardis\n"
226 "# comment\n\n\n"
227 "ro.product.model\n"
228 "ro.product.board = magic \n");
229 ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file2.path));
230
231 std::string script1("file_getprop(\"" + std::string(temp_file2.path) +
232 "\", \"ro.product.name\")");
233 expect("tardis", script1.c_str(), kNoCause);
234
235 std::string script2("file_getprop(\"" + std::string(temp_file2.path) +
236 "\", \"ro.product.board\")");
237 expect("magic", script2.c_str(), kNoCause);
238
239 // No match.
240 std::string script3("file_getprop(\"" + std::string(temp_file2.path) +
241 "\", \"ro.product.wrong\")");
242 expect("", script3.c_str(), kNoCause);
243
244 std::string script4("file_getprop(\"" + std::string(temp_file2.path) +
245 "\", \"ro.product.name=\")");
246 expect("", script4.c_str(), kNoCause);
247
248 std::string script5("file_getprop(\"" + std::string(temp_file2.path) +
249 "\", \"ro.product.nam\")");
250 expect("", script5.c_str(), kNoCause);
251
252 std::string script6("file_getprop(\"" + std::string(temp_file2.path) +
253 "\", \"ro.product.model\")");
254 expect("", script6.c_str(), kNoCause);
255}
Tao Bao0831d0b2016-11-03 23:25:04 -0700256
Tao Baoef0eb3b2016-11-14 21:29:52 -0800257// TODO: Test extracting to block device.
258TEST_F(UpdaterTest, package_extract_file) {
259 // package_extract_file expects 1 or 2 arguments.
260 expect(nullptr, "package_extract_file()", kArgsParsingFailure);
261 expect(nullptr, "package_extract_file(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
262
263 std::string zip_path = from_testdata_base("ziptest_valid.zip");
264 ZipArchiveHandle handle;
265 ASSERT_EQ(0, OpenArchive(zip_path.c_str(), &handle));
266
267 // Need to set up the ziphandle.
268 UpdaterInfo updater_info;
269 updater_info.package_zip = handle;
270
271 // Two-argument version.
272 TemporaryFile temp_file1;
273 std::string script("package_extract_file(\"a.txt\", \"" + std::string(temp_file1.path) + "\")");
274 expect("t", script.c_str(), kNoCause, &updater_info);
275
276 // Verify the extracted entry.
277 std::string data;
278 ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data));
279 ASSERT_EQ(kATxtContents, data);
280
281 // Now extract another entry to the same location, which should overwrite.
282 script = "package_extract_file(\"b.txt\", \"" + std::string(temp_file1.path) + "\")";
283 expect("t", script.c_str(), kNoCause, &updater_info);
284
285 ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data));
286 ASSERT_EQ(kBTxtContents, data);
287
288 // Missing zip entry. The two-argument version doesn't abort.
289 script = "package_extract_file(\"doesntexist\", \"" + std::string(temp_file1.path) + "\")";
290 expect("", script.c_str(), kNoCause, &updater_info);
291
292 // Extract to /dev/full should fail.
293 script = "package_extract_file(\"a.txt\", \"/dev/full\")";
294 expect("", script.c_str(), kNoCause, &updater_info);
295
296 // One-argument version.
297 script = "sha1_check(package_extract_file(\"a.txt\"))";
298 expect(kATxtSha1Sum.c_str(), script.c_str(), kNoCause, &updater_info);
299
300 script = "sha1_check(package_extract_file(\"b.txt\"))";
301 expect(kBTxtSha1Sum.c_str(), script.c_str(), kNoCause, &updater_info);
302
303 // Missing entry. The one-argument version aborts the evaluation.
304 script = "package_extract_file(\"doesntexist\")";
305 expect(nullptr, script.c_str(), kPackageExtractFileFailure, &updater_info);
306
307 CloseArchive(handle);
308}
Tao Baod0f30882016-11-03 23:52:01 -0700309
310TEST_F(UpdaterTest, write_value) {
311 // write_value() expects two arguments.
312 expect(nullptr, "write_value()", kArgsParsingFailure);
313 expect(nullptr, "write_value(\"arg1\")", kArgsParsingFailure);
314 expect(nullptr, "write_value(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
315
316 // filename cannot be empty.
317 expect(nullptr, "write_value(\"value\", \"\")", kArgsParsingFailure);
318
319 // Write some value to file.
320 TemporaryFile temp_file;
321 std::string value = "magicvalue";
322 std::string script("write_value(\"" + value + "\", \"" + std::string(temp_file.path) + "\")");
323 expect("t", script.c_str(), kNoCause);
324
325 // Verify the content.
326 std::string content;
327 ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content));
328 ASSERT_EQ(value, content);
329
330 // Allow writing empty string.
331 script = "write_value(\"\", \"" + std::string(temp_file.path) + "\")";
332 expect("t", script.c_str(), kNoCause);
333
334 // Verify the content.
335 ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content));
336 ASSERT_EQ("", content);
337
338 // It should fail gracefully when write fails.
339 script = "write_value(\"value\", \"/proc/0/file1\")";
340 expect("", script.c_str(), kNoCause);
341}
Tao Baobedf5fc2016-11-18 12:01:26 -0800342
343TEST_F(UpdaterTest, get_stage) {
344 // get_stage() expects one argument.
345 expect(nullptr, "get_stage()", kArgsParsingFailure);
346 expect(nullptr, "get_stage(\"arg1\", \"arg2\")", kArgsParsingFailure);
347 expect(nullptr, "get_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
348
349 // Set up a local file as BCB.
350 TemporaryFile tf;
351 std::string temp_file(tf.path);
352 bootloader_message boot;
353 strlcpy(boot.stage, "2/3", sizeof(boot.stage));
354 std::string err;
355 ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err));
356
357 // Can read the stage value.
358 std::string script("get_stage(\"" + temp_file + "\")");
359 expect("2/3", script.c_str(), kNoCause);
360
361 // Bad BCB path.
362 script = "get_stage(\"doesntexist\")";
363 expect("", script.c_str(), kNoCause);
364}
365
366TEST_F(UpdaterTest, set_stage) {
367 // set_stage() expects two arguments.
368 expect(nullptr, "set_stage()", kArgsParsingFailure);
369 expect(nullptr, "set_stage(\"arg1\")", kArgsParsingFailure);
370 expect(nullptr, "set_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
371
372 // Set up a local file as BCB.
373 TemporaryFile tf;
374 std::string temp_file(tf.path);
375 bootloader_message boot;
376 strlcpy(boot.command, "command", sizeof(boot.command));
377 strlcpy(boot.stage, "2/3", sizeof(boot.stage));
378 std::string err;
379 ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err));
380
381 // Write with set_stage().
382 std::string script("set_stage(\"" + temp_file + "\", \"1/3\")");
383 expect(tf.path, script.c_str(), kNoCause);
384
385 // Verify.
386 bootloader_message boot_verify;
387 ASSERT_TRUE(read_bootloader_message_from(&boot_verify, temp_file, &err));
388
389 // Stage should be updated, with command part untouched.
390 ASSERT_STREQ("1/3", boot_verify.stage);
391 ASSERT_STREQ(boot.command, boot_verify.command);
392
393 // Bad BCB path.
394 script = "set_stage(\"doesntexist\", \"1/3\")";
395 expect("", script.c_str(), kNoCause);
396
397 script = "set_stage(\"/dev/full\", \"1/3\")";
398 expect("", script.c_str(), kNoCause);
399}
Tao Bao9aa7ab52017-01-05 17:27:19 -0800400
401TEST_F(UpdaterTest, set_progress) {
402 // set_progress() expects one argument.
403 expect(nullptr, "set_progress()", kArgsParsingFailure);
404 expect(nullptr, "set_progress(\"arg1\", \"arg2\")", kArgsParsingFailure);
405
406 // Invalid progress argument.
407 expect(nullptr, "set_progress(\"arg1\")", kArgsParsingFailure);
408 expect(nullptr, "set_progress(\"3x+5\")", kArgsParsingFailure);
409 expect(nullptr, "set_progress(\".3.5\")", kArgsParsingFailure);
410
411 TemporaryFile tf;
412 UpdaterInfo updater_info;
Tianjie Xu79327ac2017-09-08 17:09:10 -0700413 updater_info.cmd_pipe = fdopen(tf.release(), "w");
Tao Bao9aa7ab52017-01-05 17:27:19 -0800414 expect(".52", "set_progress(\".52\")", kNoCause, &updater_info);
415 fflush(updater_info.cmd_pipe);
416
417 std::string cmd;
418 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd));
419 ASSERT_EQ(android::base::StringPrintf("set_progress %f\n", .52), cmd);
420 // recovery-updater protocol expects 2 tokens ("set_progress <frac>").
421 ASSERT_EQ(2U, android::base::Split(cmd, " ").size());
Tianjie Xu79327ac2017-09-08 17:09:10 -0700422 ASSERT_EQ(0, fclose(updater_info.cmd_pipe));
Tao Bao9aa7ab52017-01-05 17:27:19 -0800423}
424
425TEST_F(UpdaterTest, show_progress) {
426 // show_progress() expects two arguments.
427 expect(nullptr, "show_progress()", kArgsParsingFailure);
428 expect(nullptr, "show_progress(\"arg1\")", kArgsParsingFailure);
429 expect(nullptr, "show_progress(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
430
431 // Invalid progress arguments.
432 expect(nullptr, "show_progress(\"arg1\", \"arg2\")", kArgsParsingFailure);
433 expect(nullptr, "show_progress(\"3x+5\", \"10\")", kArgsParsingFailure);
434 expect(nullptr, "show_progress(\".3\", \"5a\")", kArgsParsingFailure);
435
436 TemporaryFile tf;
437 UpdaterInfo updater_info;
Tianjie Xu79327ac2017-09-08 17:09:10 -0700438 updater_info.cmd_pipe = fdopen(tf.release(), "w");
Tao Bao9aa7ab52017-01-05 17:27:19 -0800439 expect(".52", "show_progress(\".52\", \"10\")", kNoCause, &updater_info);
440 fflush(updater_info.cmd_pipe);
441
442 std::string cmd;
443 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd));
444 ASSERT_EQ(android::base::StringPrintf("progress %f %d\n", .52, 10), cmd);
445 // recovery-updater protocol expects 3 tokens ("progress <frac> <secs>").
446 ASSERT_EQ(3U, android::base::Split(cmd, " ").size());
Tianjie Xu79327ac2017-09-08 17:09:10 -0700447 ASSERT_EQ(0, fclose(updater_info.cmd_pipe));
Tao Bao9aa7ab52017-01-05 17:27:19 -0800448}
Tianjie Xu56ebe622017-03-16 00:48:21 -0700449
Tianjie Xu5450c842017-10-18 13:15:21 -0700450TEST_F(UpdaterTest, block_image_update_patch_data) {
Tianjie Xu56ebe622017-03-16 00:48:21 -0700451 std::string src_content = std::string(4096, 'a') + std::string(4096, 'c');
452 std::string tgt_content = std::string(4096, 'b') + std::string(4096, 'd');
Tianjie Xu5450c842017-10-18 13:15:21 -0700453
454 // Generate the patch data.
Tianjie Xu56ebe622017-03-16 00:48:21 -0700455 TemporaryFile patch_file;
456 ASSERT_EQ(0, bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(src_content.data()),
457 src_content.size(), reinterpret_cast<const uint8_t*>(tgt_content.data()),
458 tgt_content.size(), patch_file.path, nullptr));
459 std::string patch_content;
460 ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch_content));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700461
Tianjie Xu5450c842017-10-18 13:15:21 -0700462 // Create the transfer list that contains a bsdiff.
Tianjie Xu56ebe622017-03-16 00:48:21 -0700463 std::string src_hash = get_sha1(src_content);
464 std::string tgt_hash = get_sha1(tgt_content);
465 std::vector<std::string> transfer_list = {
466 "4",
467 "2",
468 "0",
469 "2",
470 "stash " + src_hash + " 2,0,2",
471 android::base::StringPrintf("bsdiff 0 %zu %s %s 2,0,2 2 - %s:2,0,2", patch_content.size(),
472 src_hash.c_str(), tgt_hash.c_str(), src_hash.c_str()),
473 "free " + src_hash,
474 };
Tianjie Xu56ebe622017-03-16 00:48:21 -0700475
Tianjie Xu5450c842017-10-18 13:15:21 -0700476 std::unordered_map<std::string, std::string> entries = {
477 { "new_data", "" },
478 { "patch_data", patch_content },
479 { "transfer_list", android::base::Join(transfer_list, '\n') },
Tianjie Xu56ebe622017-03-16 00:48:21 -0700480 };
Tianjie Xu5450c842017-10-18 13:15:21 -0700481
482 // Build the update package.
483 TemporaryFile zip_file;
484 BuildUpdatePackage(entries, zip_file.release());
Tianjie Xu56ebe622017-03-16 00:48:21 -0700485
486 MemMapping map;
Tao Baob656a152017-04-18 23:54:29 -0700487 ASSERT_TRUE(map.MapFile(zip_file.path));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700488 ZipArchiveHandle handle;
489 ASSERT_EQ(0, OpenArchiveFromMemory(map.addr, map.length, zip_file.path, &handle));
490
491 // Set up the handler, command_pipe, patch offset & length.
492 UpdaterInfo updater_info;
493 updater_info.package_zip = handle;
494 TemporaryFile temp_pipe;
Tianjie Xu79327ac2017-09-08 17:09:10 -0700495 updater_info.cmd_pipe = fdopen(temp_pipe.release(), "wbe");
Tianjie Xu56ebe622017-03-16 00:48:21 -0700496 updater_info.package_zip_addr = map.addr;
497 updater_info.package_zip_len = map.length;
498
Tianjie Xu5450c842017-10-18 13:15:21 -0700499 // Execute the commands in the transfer list.
Tianjie Xu56ebe622017-03-16 00:48:21 -0700500 TemporaryFile update_file;
501 ASSERT_TRUE(android::base::WriteStringToFile(src_content, update_file.path));
502 std::string script = "block_image_update(\"" + std::string(update_file.path) +
503 R"(", package_extract_file("transfer_list"), "new_data", "patch_data"))";
504 expect("t", script.c_str(), kNoCause, &updater_info);
505 // The update_file should be patched correctly.
506 std::string updated_content;
507 ASSERT_TRUE(android::base::ReadFileToString(update_file.path, &updated_content));
508 ASSERT_EQ(tgt_hash, get_sha1(updated_content));
509
Tianjie Xu5450c842017-10-18 13:15:21 -0700510 ASSERT_EQ(0, fclose(updater_info.cmd_pipe));
511 CloseArchive(handle);
512}
513
514TEST_F(UpdaterTest, block_image_update_fail) {
515 std::string src_content(4096 * 2, 'e');
516 std::string src_hash = get_sha1(src_content);
517 // Stash and free some blocks, then fail the update intentionally.
518 std::vector<std::string> transfer_list = {
519 "4", "2", "0", "2", "stash " + src_hash + " 2,0,2", "free " + src_hash, "fail",
520 };
521
522 // Add a new data of 10 bytes to test the deadlock.
523 std::unordered_map<std::string, std::string> entries = {
524 { "new_data", std::string(10, 0) },
525 { "patch_data", "" },
526 { "transfer_list", android::base::Join(transfer_list, '\n') },
527 };
528
529 // Build the update package.
530 TemporaryFile zip_file;
531 BuildUpdatePackage(entries, zip_file.release());
532
533 MemMapping map;
534 ASSERT_TRUE(map.MapFile(zip_file.path));
535 ZipArchiveHandle handle;
536 ASSERT_EQ(0, OpenArchiveFromMemory(map.addr, map.length, zip_file.path, &handle));
537
538 // Set up the handler, command_pipe, patch offset & length.
539 UpdaterInfo updater_info;
540 updater_info.package_zip = handle;
541 TemporaryFile temp_pipe;
542 updater_info.cmd_pipe = fdopen(temp_pipe.release(), "wbe");
543 updater_info.package_zip_addr = map.addr;
544 updater_info.package_zip_len = map.length;
545
546 TemporaryFile update_file;
547 ASSERT_TRUE(android::base::WriteStringToFile(src_content, update_file.path));
548 // Expect the stashed blocks to be freed.
549 std::string script = "block_image_update(\"" + std::string(update_file.path) +
550 R"(", package_extract_file("transfer_list"), "new_data", "patch_data"))";
Tianjie Xu56ebe622017-03-16 00:48:21 -0700551 expect("", script.c_str(), kNoCause, &updater_info);
552 // Updater generates the stash name based on the input file name.
553 std::string name_digest = get_sha1(update_file.path);
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800554 std::string stash_base = std::string(temp_stash_base_.path) + "/" + name_digest;
Tianjie Xu56ebe622017-03-16 00:48:21 -0700555 ASSERT_EQ(0, access(stash_base.c_str(), F_OK));
Tianjie Xu5450c842017-10-18 13:15:21 -0700556 ASSERT_EQ(-1, access((stash_base + src_hash).c_str(), F_OK));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700557 ASSERT_EQ(0, rmdir(stash_base.c_str()));
558
559 ASSERT_EQ(0, fclose(updater_info.cmd_pipe));
560 CloseArchive(handle);
561}
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700562
Tianjie Xu5450c842017-10-18 13:15:21 -0700563TEST_F(UpdaterTest, new_data_over_write) {
564 std::vector<std::string> transfer_list = {
565 "4", "1", "0", "0", "new 2,0,1",
566 };
567
568 // Write 4096 + 100 bytes of new data.
569 std::unordered_map<std::string, std::string> entries = {
570 { "new_data", std::string(4196, 0) },
571 { "patch_data", "" },
572 { "transfer_list", android::base::Join(transfer_list, '\n') },
573 };
574
575 // Build the update package.
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700576 TemporaryFile zip_file;
Tianjie Xu5450c842017-10-18 13:15:21 -0700577 BuildUpdatePackage(entries, zip_file.release());
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700578
Tianjie Xu5450c842017-10-18 13:15:21 -0700579 MemMapping map;
580 ASSERT_TRUE(map.MapFile(zip_file.path));
581 ZipArchiveHandle handle;
582 ASSERT_EQ(0, OpenArchiveFromMemory(map.addr, map.length, zip_file.path, &handle));
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700583
Tianjie Xu5450c842017-10-18 13:15:21 -0700584 // Set up the handler, command_pipe, patch offset & length.
585 UpdaterInfo updater_info;
586 updater_info.package_zip = handle;
587 TemporaryFile temp_pipe;
588 updater_info.cmd_pipe = fdopen(temp_pipe.release(), "wbe");
589 updater_info.package_zip_addr = map.addr;
590 updater_info.package_zip_len = map.length;
591
592 TemporaryFile update_file;
593 std::string script = "block_image_update(\"" + std::string(update_file.path) +
594 R"(", package_extract_file("transfer_list"), "new_data", "patch_data"))";
595 expect("t", script.c_str(), kNoCause, &updater_info);
596
597 ASSERT_EQ(0, fclose(updater_info.cmd_pipe));
598 CloseArchive(handle);
599}
600
601TEST_F(UpdaterTest, new_data_short_write) {
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700602 std::vector<std::string> transfer_list = {
603 "4",
604 "1",
605 "0",
606 "0",
607 "new 2,0,1",
608 };
Tianjie Xu5450c842017-10-18 13:15:21 -0700609
610 std::unordered_map<std::string, std::string> entries = {
611 { "empty_new_data", "" },
612 { "short_new_data", std::string(10, 'a') },
613 { "exact_new_data", std::string(4096, 'a') },
614 { "patch_data", "" },
615 { "transfer_list", android::base::Join(transfer_list, '\n') },
616 };
617
618 TemporaryFile zip_file;
619 BuildUpdatePackage(entries, zip_file.release());
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700620
621 MemMapping map;
Tao Baob656a152017-04-18 23:54:29 -0700622 ASSERT_TRUE(map.MapFile(zip_file.path));
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700623 ZipArchiveHandle handle;
624 ASSERT_EQ(0, OpenArchiveFromMemory(map.addr, map.length, zip_file.path, &handle));
625
626 // Set up the handler, command_pipe, patch offset & length.
627 UpdaterInfo updater_info;
628 updater_info.package_zip = handle;
629 TemporaryFile temp_pipe;
Tianjie Xu79327ac2017-09-08 17:09:10 -0700630 updater_info.cmd_pipe = fdopen(temp_pipe.release(), "wbe");
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700631 updater_info.package_zip_addr = map.addr;
632 updater_info.package_zip_len = map.length;
633
634 // Updater should report the failure gracefully rather than stuck in deadlock.
635 TemporaryFile update_file;
636 std::string script_empty_data = "block_image_update(\"" + std::string(update_file.path) +
637 R"(", package_extract_file("transfer_list"), "empty_new_data", "patch_data"))";
638 expect("", script_empty_data.c_str(), kNoCause, &updater_info);
639
640 std::string script_short_data = "block_image_update(\"" + std::string(update_file.path) +
641 R"(", package_extract_file("transfer_list"), "short_new_data", "patch_data"))";
642 expect("", script_short_data.c_str(), kNoCause, &updater_info);
643
644 // Expect to write 1 block of new data successfully.
645 std::string script_exact_data = "block_image_update(\"" + std::string(update_file.path) +
646 R"(", package_extract_file("transfer_list"), "exact_new_data", "patch_data"))";
647 expect("t", script_exact_data.c_str(), kNoCause, &updater_info);
Tianjie Xu79327ac2017-09-08 17:09:10 -0700648
649 ASSERT_EQ(0, fclose(updater_info.cmd_pipe));
Tianjie Xu107a34f2017-06-29 17:04:21 -0700650 CloseArchive(handle);
651}
652
653TEST_F(UpdaterTest, brotli_new_data) {
Tianjie Xu107a34f2017-06-29 17:04:21 -0700654 auto generator = []() { return rand() % 128; };
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700655 // Generate 100 blocks of random data.
Tianjie Xu107a34f2017-06-29 17:04:21 -0700656 std::string brotli_new_data;
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700657 brotli_new_data.reserve(4096 * 100);
658 generate_n(back_inserter(brotli_new_data), 4096 * 100, generator);
Tianjie Xu107a34f2017-06-29 17:04:21 -0700659
660 size_t encoded_size = BrotliEncoderMaxCompressedSize(brotli_new_data.size());
Tianjie Xu5450c842017-10-18 13:15:21 -0700661 std::string encoded_data(encoded_size, 0);
Tianjie Xu107a34f2017-06-29 17:04:21 -0700662 ASSERT_TRUE(BrotliEncoderCompress(
663 BROTLI_DEFAULT_QUALITY, BROTLI_DEFAULT_WINDOW, BROTLI_DEFAULT_MODE, brotli_new_data.size(),
Tianjie Xu5450c842017-10-18 13:15:21 -0700664 reinterpret_cast<const uint8_t*>(brotli_new_data.data()), &encoded_size,
665 reinterpret_cast<uint8_t*>(const_cast<char*>(encoded_data.data()))));
666 encoded_data.resize(encoded_size);
Tianjie Xu107a34f2017-06-29 17:04:21 -0700667
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700668 // Write a few small chunks of new data, then a large chunk, and finally a few small chunks.
669 // This helps us to catch potential short writes.
Tianjie Xu107a34f2017-06-29 17:04:21 -0700670 std::vector<std::string> transfer_list = {
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700671 "4",
672 "100",
673 "0",
674 "0",
675 "new 2,0,1",
676 "new 2,1,2",
677 "new 4,2,50,50,97",
678 "new 2,97,98",
679 "new 2,98,99",
680 "new 2,99,100",
Tianjie Xu107a34f2017-06-29 17:04:21 -0700681 };
Tianjie Xu5450c842017-10-18 13:15:21 -0700682
683 std::unordered_map<std::string, std::string> entries = {
684 { "new.dat.br", std::move(encoded_data) },
685 { "patch_data", "" },
686 { "transfer_list", android::base::Join(transfer_list, '\n') },
687 };
688
689 TemporaryFile zip_file;
690 BuildUpdatePackage(entries, zip_file.release());
Tianjie Xu107a34f2017-06-29 17:04:21 -0700691
692 MemMapping map;
693 ASSERT_TRUE(map.MapFile(zip_file.path));
694 ZipArchiveHandle handle;
695 ASSERT_EQ(0, OpenArchiveFromMemory(map.addr, map.length, zip_file.path, &handle));
696
697 // Set up the handler, command_pipe, patch offset & length.
698 UpdaterInfo updater_info;
699 updater_info.package_zip = handle;
700 TemporaryFile temp_pipe;
Tianjie Xu79327ac2017-09-08 17:09:10 -0700701 updater_info.cmd_pipe = fdopen(temp_pipe.release(), "wb");
Tianjie Xu107a34f2017-06-29 17:04:21 -0700702 updater_info.package_zip_addr = map.addr;
703 updater_info.package_zip_len = map.length;
704
705 // Check if we can decompress the new data correctly.
706 TemporaryFile update_file;
707 std::string script_new_data =
708 "block_image_update(\"" + std::string(update_file.path) +
709 R"(", package_extract_file("transfer_list"), "new.dat.br", "patch_data"))";
710 expect("t", script_new_data.c_str(), kNoCause, &updater_info);
711
712 std::string updated_content;
713 ASSERT_TRUE(android::base::ReadFileToString(update_file.path, &updated_content));
714 ASSERT_EQ(brotli_new_data, updated_content);
Tianjie Xu79327ac2017-09-08 17:09:10 -0700715
716 ASSERT_EQ(0, fclose(updater_info.cmd_pipe));
Tianjie Xu107a34f2017-06-29 17:04:21 -0700717 CloseArchive(handle);
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700718}
Tianjie Xu284752e2017-12-05 11:04:17 -0800719
720TEST_F(UpdaterTest, last_command_update) {
Tao Bao641fa972018-04-25 18:59:40 -0700721 std::string last_command_file = Paths::Get().last_command_file();
Tianjie Xu284752e2017-12-05 11:04:17 -0800722
723 std::string block1 = std::string(4096, '1');
724 std::string block2 = std::string(4096, '2');
725 std::string block3 = std::string(4096, '3');
726 std::string block1_hash = get_sha1(block1);
727 std::string block2_hash = get_sha1(block2);
728 std::string block3_hash = get_sha1(block3);
729
730 // Compose the transfer list to fail the first update.
731 std::vector<std::string> transfer_list_fail = {
732 "4",
733 "2",
734 "0",
735 "2",
736 "stash " + block1_hash + " 2,0,1",
737 "move " + block1_hash + " 2,1,2 1 2,0,1",
738 "stash " + block3_hash + " 2,2,3",
739 "fail",
740 };
741
742 // Mimic a resumed update with the same transfer commands.
743 std::vector<std::string> transfer_list_continue = {
744 "4",
745 "2",
746 "0",
747 "2",
748 "stash " + block1_hash + " 2,0,1",
749 "move " + block1_hash + " 2,1,2 1 2,0,1",
750 "stash " + block3_hash + " 2,2,3",
751 "move " + block1_hash + " 2,2,3 1 2,0,1",
752 };
753
754 std::unordered_map<std::string, std::string> entries = {
755 { "new_data", "" },
756 { "patch_data", "" },
757 { "transfer_list_fail", android::base::Join(transfer_list_fail, '\n') },
758 { "transfer_list_continue", android::base::Join(transfer_list_continue, '\n') },
759 };
760
761 // Build the update package.
762 TemporaryFile zip_file;
763 BuildUpdatePackage(entries, zip_file.release());
764
765 MemMapping map;
766 ASSERT_TRUE(map.MapFile(zip_file.path));
767 ZipArchiveHandle handle;
768 ASSERT_EQ(0, OpenArchiveFromMemory(map.addr, map.length, zip_file.path, &handle));
769
770 // Set up the handler, command_pipe, patch offset & length.
771 UpdaterInfo updater_info;
772 updater_info.package_zip = handle;
773 TemporaryFile temp_pipe;
774 updater_info.cmd_pipe = fdopen(temp_pipe.release(), "wbe");
775 updater_info.package_zip_addr = map.addr;
776 updater_info.package_zip_len = map.length;
777
778 std::string src_content = block1 + block2 + block3;
779 TemporaryFile update_file;
780 ASSERT_TRUE(android::base::WriteStringToFile(src_content, update_file.path));
781 std::string script =
782 "block_image_update(\"" + std::string(update_file.path) +
783 R"(", package_extract_file("transfer_list_fail"), "new_data", "patch_data"))";
784 expect("", script.c_str(), kNoCause, &updater_info);
785
786 // Expect last_command to contain the last stash command.
787 std::string last_command_content;
788 ASSERT_TRUE(android::base::ReadFileToString(last_command_file.c_str(), &last_command_content));
789 EXPECT_EQ("2\nstash " + block3_hash + " 2,2,3", last_command_content);
790 std::string updated_contents;
791 ASSERT_TRUE(android::base::ReadFileToString(update_file.path, &updated_contents));
792 ASSERT_EQ(block1 + block1 + block3, updated_contents);
793
794 // Resume the update, expect the first 'move' to be skipped but the second 'move' to be executed.
795 ASSERT_TRUE(android::base::WriteStringToFile(src_content, update_file.path));
796 std::string script_second_update =
797 "block_image_update(\"" + std::string(update_file.path) +
798 R"(", package_extract_file("transfer_list_continue"), "new_data", "patch_data"))";
799 expect("t", script_second_update.c_str(), kNoCause, &updater_info);
800 ASSERT_TRUE(android::base::ReadFileToString(update_file.path, &updated_contents));
801 ASSERT_EQ(block1 + block2 + block1, updated_contents);
802
803 ASSERT_EQ(0, fclose(updater_info.cmd_pipe));
804 CloseArchive(handle);
805}
806
807TEST_F(UpdaterTest, last_command_update_unresumable) {
Tao Bao641fa972018-04-25 18:59:40 -0700808 std::string last_command_file = Paths::Get().last_command_file();
Tianjie Xu284752e2017-12-05 11:04:17 -0800809
810 std::string block1 = std::string(4096, '1');
811 std::string block2 = std::string(4096, '2');
812 std::string block1_hash = get_sha1(block1);
813 std::string block2_hash = get_sha1(block2);
814
815 // Construct an unresumable update with source blocks mismatch.
816 std::vector<std::string> transfer_list_unresumable = {
817 "4", "2", "0", "2", "stash " + block1_hash + " 2,0,1", "move " + block2_hash + " 2,1,2 1 2,0,1",
818 };
819
820 std::unordered_map<std::string, std::string> entries = {
821 { "new_data", "" },
822 { "patch_data", "" },
823 { "transfer_list_unresumable", android::base::Join(transfer_list_unresumable, '\n') },
824 };
825
826 // Build the update package.
827 TemporaryFile zip_file;
828 BuildUpdatePackage(entries, zip_file.release());
829
830 MemMapping map;
831 ASSERT_TRUE(map.MapFile(zip_file.path));
832 ZipArchiveHandle handle;
833 ASSERT_EQ(0, OpenArchiveFromMemory(map.addr, map.length, zip_file.path, &handle));
834
835 // Set up the handler, command_pipe, patch offset & length.
836 UpdaterInfo updater_info;
837 updater_info.package_zip = handle;
838 TemporaryFile temp_pipe;
839 updater_info.cmd_pipe = fdopen(temp_pipe.release(), "wbe");
840 updater_info.package_zip_addr = map.addr;
841 updater_info.package_zip_len = map.length;
842
843 // Set up the last_command_file
844 ASSERT_TRUE(
845 android::base::WriteStringToFile("0\nstash " + block1_hash + " 2,0,1", last_command_file));
846
847 // The last_command_file will be deleted if the update encounters an unresumable failure
848 // later.
849 std::string src_content = block1 + block1;
850 TemporaryFile update_file;
851 ASSERT_TRUE(android::base::WriteStringToFile(src_content, update_file.path));
852 std::string script =
853 "block_image_update(\"" + std::string(update_file.path) +
854 R"(", package_extract_file("transfer_list_unresumable"), "new_data", "patch_data"))";
855 expect("", script.c_str(), kNoCause, &updater_info);
856 ASSERT_EQ(-1, access(last_command_file.c_str(), R_OK));
857
858 ASSERT_EQ(0, fclose(updater_info.cmd_pipe));
859 CloseArchive(handle);
860}
861
862TEST_F(UpdaterTest, last_command_verify) {
Tao Bao641fa972018-04-25 18:59:40 -0700863 std::string last_command_file = Paths::Get().last_command_file();
Tianjie Xu284752e2017-12-05 11:04:17 -0800864
865 std::string block1 = std::string(4096, '1');
866 std::string block2 = std::string(4096, '2');
867 std::string block3 = std::string(4096, '3');
868 std::string block1_hash = get_sha1(block1);
869 std::string block2_hash = get_sha1(block2);
870 std::string block3_hash = get_sha1(block3);
871
872 std::vector<std::string> transfer_list_verify = {
873 "4",
874 "2",
875 "0",
876 "2",
877 "stash " + block1_hash + " 2,0,1",
878 "move " + block1_hash + " 2,0,1 1 2,0,1",
879 "move " + block1_hash + " 2,1,2 1 2,0,1",
880 "stash " + block3_hash + " 2,2,3",
881 };
882
883 std::unordered_map<std::string, std::string> entries = {
884 { "new_data", "" },
885 { "patch_data", "" },
886 { "transfer_list_verify", android::base::Join(transfer_list_verify, '\n') },
887 };
888
889 // Build the update package.
890 TemporaryFile zip_file;
891 BuildUpdatePackage(entries, zip_file.release());
892
893 MemMapping map;
894 ASSERT_TRUE(map.MapFile(zip_file.path));
895 ZipArchiveHandle handle;
896 ASSERT_EQ(0, OpenArchiveFromMemory(map.addr, map.length, zip_file.path, &handle));
897
898 // Set up the handler, command_pipe, patch offset & length.
899 UpdaterInfo updater_info;
900 updater_info.package_zip = handle;
901 TemporaryFile temp_pipe;
902 updater_info.cmd_pipe = fdopen(temp_pipe.release(), "wbe");
903 updater_info.package_zip_addr = map.addr;
904 updater_info.package_zip_len = map.length;
905
906 std::string src_content = block1 + block1 + block3;
907 TemporaryFile update_file;
908 ASSERT_TRUE(android::base::WriteStringToFile(src_content, update_file.path));
909
910 ASSERT_TRUE(
911 android::base::WriteStringToFile("2\nstash " + block3_hash + " 2,2,3", last_command_file));
912
913 // Expect the verification to succeed and the last_command_file is intact.
914 std::string script_verify =
915 "block_image_verify(\"" + std::string(update_file.path) +
916 R"(", package_extract_file("transfer_list_verify"), "new_data","patch_data"))";
917 expect("t", script_verify.c_str(), kNoCause, &updater_info);
918
919 std::string last_command_content;
920 ASSERT_TRUE(android::base::ReadFileToString(last_command_file.c_str(), &last_command_content));
921 EXPECT_EQ("2\nstash " + block3_hash + " 2,2,3", last_command_content);
922
923 // Expect the verification to succeed but last_command_file to be deleted; because the target
924 // blocks don't have the expected contents for the second move command.
925 src_content = block1 + block2 + block3;
926 ASSERT_TRUE(android::base::WriteStringToFile(src_content, update_file.path));
927 expect("t", script_verify.c_str(), kNoCause, &updater_info);
928 ASSERT_EQ(-1, access(last_command_file.c_str(), R_OK));
929
930 ASSERT_EQ(0, fclose(updater_info.cmd_pipe));
931 CloseArchive(handle);
932}