blob: fe4f45e1544076911c9d614b0a6f4ed568fe3a59 [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,
Tao Baoffede3e2018-06-07 09:56:19 -0700103 const std::string& image_file, const std::string& result,
104 CauseCode cause_code = kNoCause) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700105 CHECK(entries.find("transfer_list") != entries.end());
106
107 // Build the update package.
108 TemporaryFile zip_file;
109 BuildUpdatePackage(entries, zip_file.release());
110
111 MemMapping map;
112 ASSERT_TRUE(map.MapFile(zip_file.path));
113 ZipArchiveHandle handle;
114 ASSERT_EQ(0, OpenArchiveFromMemory(map.addr, map.length, zip_file.path, &handle));
115
116 // Set up the handler, command_pipe, patch offset & length.
117 UpdaterInfo updater_info;
118 updater_info.package_zip = handle;
119 TemporaryFile temp_pipe;
120 updater_info.cmd_pipe = fdopen(temp_pipe.release(), "wbe");
121 updater_info.package_zip_addr = map.addr;
122 updater_info.package_zip_len = map.length;
123
124 std::string new_data = entries.find("new_data.br") != entries.end() ? "new_data.br" : "new_data";
125 std::string script = is_verify ? "block_image_verify" : "block_image_update";
126 script += R"((")" + image_file + R"(", package_extract_file("transfer_list"), ")" + new_data +
127 R"(", "patch_data"))";
Tao Baoffede3e2018-06-07 09:56:19 -0700128 expect(result.c_str(), script.c_str(), cause_code, &updater_info);
Tao Baobc4a6d52018-05-23 00:12:21 -0700129
130 ASSERT_EQ(0, fclose(updater_info.cmd_pipe));
131 CloseArchive(handle);
132}
133
Tianjie Xu56ebe622017-03-16 00:48:21 -0700134static std::string get_sha1(const std::string& content) {
135 uint8_t digest[SHA_DIGEST_LENGTH];
136 SHA1(reinterpret_cast<const uint8_t*>(content.c_str()), content.size(), digest);
137 return print_sha1(digest);
138}
139
Tao Bao0c7839a2016-10-10 15:48:37 -0700140class UpdaterTest : public ::testing::Test {
Tianjie Xu56ebe622017-03-16 00:48:21 -0700141 protected:
Tao Baobc4a6d52018-05-23 00:12:21 -0700142 void SetUp() override {
Tianjie Xu56ebe622017-03-16 00:48:21 -0700143 RegisterBuiltins();
144 RegisterInstallFunctions();
145 RegisterBlockImageFunctions();
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800146
Tao Bao7064aa22018-05-24 21:43:50 -0700147 // Each test is run in a separate process (isolated mode). Shared temporary files won't cause
148 // conflicts.
Tao Bao641fa972018-04-25 18:59:40 -0700149 Paths::Get().set_cache_temp_source(temp_saved_source_.path);
Tao Bao7064aa22018-05-24 21:43:50 -0700150 Paths::Get().set_last_command_file(temp_last_command_.path);
Tao Bao641fa972018-04-25 18:59:40 -0700151 Paths::Get().set_stash_directory_base(temp_stash_base_.path);
Tao Baobc4a6d52018-05-23 00:12:21 -0700152
Tao Bao7064aa22018-05-24 21:43:50 -0700153 last_command_file_ = temp_last_command_.path;
Tao Baobc4a6d52018-05-23 00:12:21 -0700154 image_file_ = image_temp_file_.path;
155 }
156
157 void TearDown() override {
Tao Bao7064aa22018-05-24 21:43:50 -0700158 // Clean up the last_command_file if any.
159 ASSERT_TRUE(android::base::RemoveFileIfExists(last_command_file_));
160
Tao Baobc4a6d52018-05-23 00:12:21 -0700161 // Clear partition updated marker if any.
162 std::string updated_marker{ temp_stash_base_.path };
163 updated_marker += "/" + get_sha1(image_temp_file_.path) + ".UPDATED";
164 ASSERT_TRUE(android::base::RemoveFileIfExists(updated_marker));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700165 }
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800166
167 TemporaryFile temp_saved_source_;
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800168 TemporaryDir temp_stash_base_;
Tao Bao7064aa22018-05-24 21:43:50 -0700169 std::string last_command_file_;
Tao Baobc4a6d52018-05-23 00:12:21 -0700170 std::string image_file_;
171
172 private:
Tao Bao7064aa22018-05-24 21:43:50 -0700173 TemporaryFile temp_last_command_;
Tao Baobc4a6d52018-05-23 00:12:21 -0700174 TemporaryFile image_temp_file_;
Tao Bao0c7839a2016-10-10 15:48:37 -0700175};
176
177TEST_F(UpdaterTest, getprop) {
178 expect(android::base::GetProperty("ro.product.device", "").c_str(),
179 "getprop(\"ro.product.device\")",
Tao Bao361342c2016-02-08 11:15:50 -0800180 kNoCause);
Tao Bao0c7839a2016-10-10 15:48:37 -0700181
182 expect(android::base::GetProperty("ro.build.fingerprint", "").c_str(),
183 "getprop(\"ro.build.fingerprint\")",
Tao Bao361342c2016-02-08 11:15:50 -0800184 kNoCause);
Tao Bao0c7839a2016-10-10 15:48:37 -0700185
186 // getprop() accepts only one parameter.
Tao Bao361342c2016-02-08 11:15:50 -0800187 expect(nullptr, "getprop()", kArgsParsingFailure);
188 expect(nullptr, "getprop(\"arg1\", \"arg2\")", kArgsParsingFailure);
189}
190
191TEST_F(UpdaterTest, sha1_check) {
192 // sha1_check(data) returns the SHA-1 of the data.
193 expect("81fe8bfe87576c3ecb22426f8e57847382917acf", "sha1_check(\"abcd\")", kNoCause);
194 expect("da39a3ee5e6b4b0d3255bfef95601890afd80709", "sha1_check(\"\")", kNoCause);
195
196 // sha1_check(data, sha1_hex, [sha1_hex, ...]) returns the matched SHA-1.
197 expect("81fe8bfe87576c3ecb22426f8e57847382917acf",
198 "sha1_check(\"abcd\", \"81fe8bfe87576c3ecb22426f8e57847382917acf\")",
199 kNoCause);
200
201 expect("81fe8bfe87576c3ecb22426f8e57847382917acf",
202 "sha1_check(\"abcd\", \"wrong_sha1\", \"81fe8bfe87576c3ecb22426f8e57847382917acf\")",
203 kNoCause);
204
205 // Or "" if there's no match.
206 expect("",
207 "sha1_check(\"abcd\", \"wrong_sha1\")",
208 kNoCause);
209
210 expect("",
211 "sha1_check(\"abcd\", \"wrong_sha1\", \"wrong_sha2\")",
212 kNoCause);
213
214 // sha1_check() expects at least one argument.
215 expect(nullptr, "sha1_check()", kArgsParsingFailure);
Tao Bao0c7839a2016-10-10 15:48:37 -0700216}
Tao Bao51d516e2016-11-03 14:49:01 -0700217
Tao Baodb56eb02017-03-23 06:34:20 -0700218TEST_F(UpdaterTest, apply_patch_check) {
219 // Zero-argument is not valid.
220 expect(nullptr, "apply_patch_check()", kArgsParsingFailure);
221
222 // File not found.
223 expect("", "apply_patch_check(\"/doesntexist\")", kNoCause);
224
225 std::string src_file = from_testdata_base("old.file");
226 std::string src_content;
227 ASSERT_TRUE(android::base::ReadFileToString(src_file, &src_content));
228 size_t src_size = src_content.size();
229 std::string src_hash = get_sha1(src_content);
230
231 // One-argument with EMMC:file:size:sha1 should pass the check.
232 std::string filename = android::base::Join(
233 std::vector<std::string>{ "EMMC", src_file, std::to_string(src_size), src_hash }, ":");
234 std::string cmd = "apply_patch_check(\"" + filename + "\")";
235 expect("t", cmd.c_str(), kNoCause);
236
237 // EMMC:file:(size-1):sha1:(size+1):sha1 should fail the check.
238 std::string filename_bad = android::base::Join(
239 std::vector<std::string>{ "EMMC", src_file, std::to_string(src_size - 1), src_hash,
240 std::to_string(src_size + 1), src_hash },
241 ":");
242 cmd = "apply_patch_check(\"" + filename_bad + "\")";
243 expect("", cmd.c_str(), kNoCause);
244
245 // EMMC:file:(size-1):sha1:size:sha1:(size+1):sha1 should pass the check.
246 filename_bad =
247 android::base::Join(std::vector<std::string>{ "EMMC", src_file, std::to_string(src_size - 1),
248 src_hash, std::to_string(src_size), src_hash,
249 std::to_string(src_size + 1), src_hash },
250 ":");
251 cmd = "apply_patch_check(\"" + filename_bad + "\")";
252 expect("t", cmd.c_str(), kNoCause);
253
254 // Multiple arguments.
255 cmd = "apply_patch_check(\"" + filename + "\", \"wrong_sha1\", \"wrong_sha2\")";
256 expect("", cmd.c_str(), kNoCause);
257
258 cmd = "apply_patch_check(\"" + filename + "\", \"wrong_sha1\", \"" + src_hash +
259 "\", \"wrong_sha2\")";
260 expect("t", cmd.c_str(), kNoCause);
261
262 cmd = "apply_patch_check(\"" + filename_bad + "\", \"wrong_sha1\", \"" + src_hash +
263 "\", \"wrong_sha2\")";
264 expect("t", cmd.c_str(), kNoCause);
265}
266
Tao Bao51d516e2016-11-03 14:49:01 -0700267TEST_F(UpdaterTest, file_getprop) {
268 // file_getprop() expects two arguments.
269 expect(nullptr, "file_getprop()", kArgsParsingFailure);
270 expect(nullptr, "file_getprop(\"arg1\")", kArgsParsingFailure);
271 expect(nullptr, "file_getprop(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
272
273 // File doesn't exist.
274 expect(nullptr, "file_getprop(\"/doesntexist\", \"key1\")", kFileGetPropFailure);
275
276 // Reject too large files (current limit = 65536).
277 TemporaryFile temp_file1;
278 std::string buffer(65540, '\0');
279 ASSERT_TRUE(android::base::WriteStringToFile(buffer, temp_file1.path));
280
281 // Read some keys.
282 TemporaryFile temp_file2;
283 std::string content("ro.product.name=tardis\n"
284 "# comment\n\n\n"
285 "ro.product.model\n"
286 "ro.product.board = magic \n");
287 ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file2.path));
288
289 std::string script1("file_getprop(\"" + std::string(temp_file2.path) +
290 "\", \"ro.product.name\")");
291 expect("tardis", script1.c_str(), kNoCause);
292
293 std::string script2("file_getprop(\"" + std::string(temp_file2.path) +
294 "\", \"ro.product.board\")");
295 expect("magic", script2.c_str(), kNoCause);
296
297 // No match.
298 std::string script3("file_getprop(\"" + std::string(temp_file2.path) +
299 "\", \"ro.product.wrong\")");
300 expect("", script3.c_str(), kNoCause);
301
302 std::string script4("file_getprop(\"" + std::string(temp_file2.path) +
303 "\", \"ro.product.name=\")");
304 expect("", script4.c_str(), kNoCause);
305
306 std::string script5("file_getprop(\"" + std::string(temp_file2.path) +
307 "\", \"ro.product.nam\")");
308 expect("", script5.c_str(), kNoCause);
309
310 std::string script6("file_getprop(\"" + std::string(temp_file2.path) +
311 "\", \"ro.product.model\")");
312 expect("", script6.c_str(), kNoCause);
313}
Tao Bao0831d0b2016-11-03 23:25:04 -0700314
Tao Baoef0eb3b2016-11-14 21:29:52 -0800315// TODO: Test extracting to block device.
316TEST_F(UpdaterTest, package_extract_file) {
317 // package_extract_file expects 1 or 2 arguments.
318 expect(nullptr, "package_extract_file()", kArgsParsingFailure);
319 expect(nullptr, "package_extract_file(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
320
321 std::string zip_path = from_testdata_base("ziptest_valid.zip");
322 ZipArchiveHandle handle;
323 ASSERT_EQ(0, OpenArchive(zip_path.c_str(), &handle));
324
325 // Need to set up the ziphandle.
326 UpdaterInfo updater_info;
327 updater_info.package_zip = handle;
328
329 // Two-argument version.
330 TemporaryFile temp_file1;
331 std::string script("package_extract_file(\"a.txt\", \"" + std::string(temp_file1.path) + "\")");
332 expect("t", script.c_str(), kNoCause, &updater_info);
333
334 // Verify the extracted entry.
335 std::string data;
336 ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data));
337 ASSERT_EQ(kATxtContents, data);
338
339 // Now extract another entry to the same location, which should overwrite.
340 script = "package_extract_file(\"b.txt\", \"" + std::string(temp_file1.path) + "\")";
341 expect("t", script.c_str(), kNoCause, &updater_info);
342
343 ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data));
344 ASSERT_EQ(kBTxtContents, data);
345
346 // Missing zip entry. The two-argument version doesn't abort.
347 script = "package_extract_file(\"doesntexist\", \"" + std::string(temp_file1.path) + "\")";
348 expect("", script.c_str(), kNoCause, &updater_info);
349
350 // Extract to /dev/full should fail.
351 script = "package_extract_file(\"a.txt\", \"/dev/full\")";
352 expect("", script.c_str(), kNoCause, &updater_info);
353
354 // One-argument version.
355 script = "sha1_check(package_extract_file(\"a.txt\"))";
356 expect(kATxtSha1Sum.c_str(), script.c_str(), kNoCause, &updater_info);
357
358 script = "sha1_check(package_extract_file(\"b.txt\"))";
359 expect(kBTxtSha1Sum.c_str(), script.c_str(), kNoCause, &updater_info);
360
361 // Missing entry. The one-argument version aborts the evaluation.
362 script = "package_extract_file(\"doesntexist\")";
363 expect(nullptr, script.c_str(), kPackageExtractFileFailure, &updater_info);
364
365 CloseArchive(handle);
366}
Tao Baod0f30882016-11-03 23:52:01 -0700367
368TEST_F(UpdaterTest, write_value) {
369 // write_value() expects two arguments.
370 expect(nullptr, "write_value()", kArgsParsingFailure);
371 expect(nullptr, "write_value(\"arg1\")", kArgsParsingFailure);
372 expect(nullptr, "write_value(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
373
374 // filename cannot be empty.
375 expect(nullptr, "write_value(\"value\", \"\")", kArgsParsingFailure);
376
377 // Write some value to file.
378 TemporaryFile temp_file;
379 std::string value = "magicvalue";
380 std::string script("write_value(\"" + value + "\", \"" + std::string(temp_file.path) + "\")");
381 expect("t", script.c_str(), kNoCause);
382
383 // Verify the content.
384 std::string content;
385 ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content));
386 ASSERT_EQ(value, content);
387
388 // Allow writing empty string.
389 script = "write_value(\"\", \"" + std::string(temp_file.path) + "\")";
390 expect("t", script.c_str(), kNoCause);
391
392 // Verify the content.
393 ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content));
394 ASSERT_EQ("", content);
395
396 // It should fail gracefully when write fails.
397 script = "write_value(\"value\", \"/proc/0/file1\")";
398 expect("", script.c_str(), kNoCause);
399}
Tao Baobedf5fc2016-11-18 12:01:26 -0800400
401TEST_F(UpdaterTest, get_stage) {
402 // get_stage() expects one argument.
403 expect(nullptr, "get_stage()", kArgsParsingFailure);
404 expect(nullptr, "get_stage(\"arg1\", \"arg2\")", kArgsParsingFailure);
405 expect(nullptr, "get_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
406
407 // Set up a local file as BCB.
408 TemporaryFile tf;
409 std::string temp_file(tf.path);
410 bootloader_message boot;
411 strlcpy(boot.stage, "2/3", sizeof(boot.stage));
412 std::string err;
413 ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err));
414
415 // Can read the stage value.
416 std::string script("get_stage(\"" + temp_file + "\")");
417 expect("2/3", script.c_str(), kNoCause);
418
419 // Bad BCB path.
420 script = "get_stage(\"doesntexist\")";
421 expect("", script.c_str(), kNoCause);
422}
423
424TEST_F(UpdaterTest, set_stage) {
425 // set_stage() expects two arguments.
426 expect(nullptr, "set_stage()", kArgsParsingFailure);
427 expect(nullptr, "set_stage(\"arg1\")", kArgsParsingFailure);
428 expect(nullptr, "set_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
429
430 // Set up a local file as BCB.
431 TemporaryFile tf;
432 std::string temp_file(tf.path);
433 bootloader_message boot;
434 strlcpy(boot.command, "command", sizeof(boot.command));
435 strlcpy(boot.stage, "2/3", sizeof(boot.stage));
436 std::string err;
437 ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err));
438
439 // Write with set_stage().
440 std::string script("set_stage(\"" + temp_file + "\", \"1/3\")");
441 expect(tf.path, script.c_str(), kNoCause);
442
443 // Verify.
444 bootloader_message boot_verify;
445 ASSERT_TRUE(read_bootloader_message_from(&boot_verify, temp_file, &err));
446
447 // Stage should be updated, with command part untouched.
448 ASSERT_STREQ("1/3", boot_verify.stage);
449 ASSERT_STREQ(boot.command, boot_verify.command);
450
451 // Bad BCB path.
452 script = "set_stage(\"doesntexist\", \"1/3\")";
453 expect("", script.c_str(), kNoCause);
454
455 script = "set_stage(\"/dev/full\", \"1/3\")";
456 expect("", script.c_str(), kNoCause);
457}
Tao Bao9aa7ab52017-01-05 17:27:19 -0800458
459TEST_F(UpdaterTest, set_progress) {
460 // set_progress() expects one argument.
461 expect(nullptr, "set_progress()", kArgsParsingFailure);
462 expect(nullptr, "set_progress(\"arg1\", \"arg2\")", kArgsParsingFailure);
463
464 // Invalid progress argument.
465 expect(nullptr, "set_progress(\"arg1\")", kArgsParsingFailure);
466 expect(nullptr, "set_progress(\"3x+5\")", kArgsParsingFailure);
467 expect(nullptr, "set_progress(\".3.5\")", kArgsParsingFailure);
468
469 TemporaryFile tf;
470 UpdaterInfo updater_info;
Tianjie Xu79327ac2017-09-08 17:09:10 -0700471 updater_info.cmd_pipe = fdopen(tf.release(), "w");
Tao Bao9aa7ab52017-01-05 17:27:19 -0800472 expect(".52", "set_progress(\".52\")", kNoCause, &updater_info);
473 fflush(updater_info.cmd_pipe);
474
475 std::string cmd;
476 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd));
477 ASSERT_EQ(android::base::StringPrintf("set_progress %f\n", .52), cmd);
478 // recovery-updater protocol expects 2 tokens ("set_progress <frac>").
479 ASSERT_EQ(2U, android::base::Split(cmd, " ").size());
Tianjie Xu79327ac2017-09-08 17:09:10 -0700480 ASSERT_EQ(0, fclose(updater_info.cmd_pipe));
Tao Bao9aa7ab52017-01-05 17:27:19 -0800481}
482
483TEST_F(UpdaterTest, show_progress) {
484 // show_progress() expects two arguments.
485 expect(nullptr, "show_progress()", kArgsParsingFailure);
486 expect(nullptr, "show_progress(\"arg1\")", kArgsParsingFailure);
487 expect(nullptr, "show_progress(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
488
489 // Invalid progress arguments.
490 expect(nullptr, "show_progress(\"arg1\", \"arg2\")", kArgsParsingFailure);
491 expect(nullptr, "show_progress(\"3x+5\", \"10\")", kArgsParsingFailure);
492 expect(nullptr, "show_progress(\".3\", \"5a\")", kArgsParsingFailure);
493
494 TemporaryFile tf;
495 UpdaterInfo updater_info;
Tianjie Xu79327ac2017-09-08 17:09:10 -0700496 updater_info.cmd_pipe = fdopen(tf.release(), "w");
Tao Bao9aa7ab52017-01-05 17:27:19 -0800497 expect(".52", "show_progress(\".52\", \"10\")", kNoCause, &updater_info);
498 fflush(updater_info.cmd_pipe);
499
500 std::string cmd;
501 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd));
502 ASSERT_EQ(android::base::StringPrintf("progress %f %d\n", .52, 10), cmd);
503 // recovery-updater protocol expects 3 tokens ("progress <frac> <secs>").
504 ASSERT_EQ(3U, android::base::Split(cmd, " ").size());
Tianjie Xu79327ac2017-09-08 17:09:10 -0700505 ASSERT_EQ(0, fclose(updater_info.cmd_pipe));
Tao Bao9aa7ab52017-01-05 17:27:19 -0800506}
Tianjie Xu56ebe622017-03-16 00:48:21 -0700507
Tao Baoffede3e2018-06-07 09:56:19 -0700508TEST_F(UpdaterTest, block_image_update_parsing_error) {
509 std::vector<std::string> transfer_list{
510 // clang-format off
511 "4",
512 "2",
513 "0",
514 // clang-format on
515 };
516
517 PackageEntries entries{
518 { "new_data", "" },
519 { "patch_data", "" },
520 { "transfer_list", android::base::Join(transfer_list, '\n') },
521 };
522
523 RunBlockImageUpdate(false, entries, image_file_, "", kArgsParsingFailure);
524}
525
Tianjie Xu5450c842017-10-18 13:15:21 -0700526TEST_F(UpdaterTest, block_image_update_patch_data) {
Tianjie Xu56ebe622017-03-16 00:48:21 -0700527 std::string src_content = std::string(4096, 'a') + std::string(4096, 'c');
528 std::string tgt_content = std::string(4096, 'b') + std::string(4096, 'd');
Tianjie Xu5450c842017-10-18 13:15:21 -0700529
530 // Generate the patch data.
Tianjie Xu56ebe622017-03-16 00:48:21 -0700531 TemporaryFile patch_file;
Tao Baobc4a6d52018-05-23 00:12:21 -0700532 ASSERT_EQ(0,
533 bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(src_content.data()), src_content.size(),
534 reinterpret_cast<const uint8_t*>(tgt_content.data()), tgt_content.size(),
535 patch_file.path, nullptr));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700536 std::string patch_content;
537 ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch_content));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700538
Tianjie Xu5450c842017-10-18 13:15:21 -0700539 // Create the transfer list that contains a bsdiff.
Tianjie Xu56ebe622017-03-16 00:48:21 -0700540 std::string src_hash = get_sha1(src_content);
541 std::string tgt_hash = get_sha1(tgt_content);
Tao Baobc4a6d52018-05-23 00:12:21 -0700542 std::vector<std::string> transfer_list{
543 // clang-format off
Tianjie Xu56ebe622017-03-16 00:48:21 -0700544 "4",
545 "2",
546 "0",
547 "2",
548 "stash " + src_hash + " 2,0,2",
549 android::base::StringPrintf("bsdiff 0 %zu %s %s 2,0,2 2 - %s:2,0,2", patch_content.size(),
550 src_hash.c_str(), tgt_hash.c_str(), src_hash.c_str()),
551 "free " + src_hash,
Tao Baobc4a6d52018-05-23 00:12:21 -0700552 // clang-format on
Tianjie Xu56ebe622017-03-16 00:48:21 -0700553 };
Tianjie Xu56ebe622017-03-16 00:48:21 -0700554
Tao Baobc4a6d52018-05-23 00:12:21 -0700555 PackageEntries entries{
Tianjie Xu5450c842017-10-18 13:15:21 -0700556 { "new_data", "" },
557 { "patch_data", patch_content },
558 { "transfer_list", android::base::Join(transfer_list, '\n') },
Tianjie Xu56ebe622017-03-16 00:48:21 -0700559 };
Tianjie Xu5450c842017-10-18 13:15:21 -0700560
Tao Baobc4a6d52018-05-23 00:12:21 -0700561 ASSERT_TRUE(android::base::WriteStringToFile(src_content, image_file_));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700562
Tao Baobc4a6d52018-05-23 00:12:21 -0700563 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu56ebe622017-03-16 00:48:21 -0700564
Tianjie Xu56ebe622017-03-16 00:48:21 -0700565 // The update_file should be patched correctly.
566 std::string updated_content;
Tao Baobc4a6d52018-05-23 00:12:21 -0700567 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_content));
568 ASSERT_EQ(tgt_content, updated_content);
Tianjie Xu5450c842017-10-18 13:15:21 -0700569}
570
571TEST_F(UpdaterTest, block_image_update_fail) {
572 std::string src_content(4096 * 2, 'e');
573 std::string src_hash = get_sha1(src_content);
574 // Stash and free some blocks, then fail the update intentionally.
Tao Baobc4a6d52018-05-23 00:12:21 -0700575 std::vector<std::string> transfer_list{
576 // clang-format off
577 "4",
578 "2",
579 "0",
580 "2",
581 "stash " + src_hash + " 2,0,2",
582 "free " + src_hash,
583 "fail",
584 // clang-format on
Tianjie Xu5450c842017-10-18 13:15:21 -0700585 };
586
587 // Add a new data of 10 bytes to test the deadlock.
Tao Baobc4a6d52018-05-23 00:12:21 -0700588 PackageEntries entries{
Tianjie Xu5450c842017-10-18 13:15:21 -0700589 { "new_data", std::string(10, 0) },
590 { "patch_data", "" },
591 { "transfer_list", android::base::Join(transfer_list, '\n') },
592 };
593
Tao Baobc4a6d52018-05-23 00:12:21 -0700594 ASSERT_TRUE(android::base::WriteStringToFile(src_content, image_file_));
Tianjie Xu5450c842017-10-18 13:15:21 -0700595
Tao Baobc4a6d52018-05-23 00:12:21 -0700596 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu5450c842017-10-18 13:15:21 -0700597
Tianjie Xu56ebe622017-03-16 00:48:21 -0700598 // Updater generates the stash name based on the input file name.
Tao Baobc4a6d52018-05-23 00:12:21 -0700599 std::string name_digest = get_sha1(image_file_);
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800600 std::string stash_base = std::string(temp_stash_base_.path) + "/" + name_digest;
Tianjie Xu56ebe622017-03-16 00:48:21 -0700601 ASSERT_EQ(0, access(stash_base.c_str(), F_OK));
Tao Baobc4a6d52018-05-23 00:12:21 -0700602 // Expect the stashed blocks to be freed.
Tianjie Xu5450c842017-10-18 13:15:21 -0700603 ASSERT_EQ(-1, access((stash_base + src_hash).c_str(), F_OK));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700604 ASSERT_EQ(0, rmdir(stash_base.c_str()));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700605}
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700606
Tianjie Xu5450c842017-10-18 13:15:21 -0700607TEST_F(UpdaterTest, new_data_over_write) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700608 std::vector<std::string> transfer_list{
609 // clang-format off
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700610 "4",
611 "1",
612 "0",
613 "0",
614 "new 2,0,1",
Tao Baobc4a6d52018-05-23 00:12:21 -0700615 // clang-format on
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700616 };
Tianjie Xu5450c842017-10-18 13:15:21 -0700617
Tao Baobc4a6d52018-05-23 00:12:21 -0700618 // Write 4096 + 100 bytes of new data.
619 PackageEntries entries{
620 { "new_data", std::string(4196, 0) },
Tianjie Xu5450c842017-10-18 13:15:21 -0700621 { "patch_data", "" },
622 { "transfer_list", android::base::Join(transfer_list, '\n') },
623 };
624
Tao Baobc4a6d52018-05-23 00:12:21 -0700625 RunBlockImageUpdate(false, entries, image_file_, "t");
626}
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700627
Tao Baobc4a6d52018-05-23 00:12:21 -0700628TEST_F(UpdaterTest, new_data_short_write) {
629 std::vector<std::string> transfer_list{
630 // clang-format off
631 "4",
632 "1",
633 "0",
634 "0",
635 "new 2,0,1",
636 // clang-format on
637 };
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700638
Tao Baobc4a6d52018-05-23 00:12:21 -0700639 PackageEntries entries{
640 { "patch_data", "" },
641 { "transfer_list", android::base::Join(transfer_list, '\n') },
642 };
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700643
644 // Updater should report the failure gracefully rather than stuck in deadlock.
Tao Baobc4a6d52018-05-23 00:12:21 -0700645 entries["new_data"] = "";
646 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700647
Tao Baobc4a6d52018-05-23 00:12:21 -0700648 entries["new_data"] = std::string(10, 'a');
649 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700650
651 // Expect to write 1 block of new data successfully.
Tao Baobc4a6d52018-05-23 00:12:21 -0700652 entries["new_data"] = std::string(4096, 'a');
653 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu107a34f2017-06-29 17:04:21 -0700654}
655
656TEST_F(UpdaterTest, brotli_new_data) {
Tianjie Xu107a34f2017-06-29 17:04:21 -0700657 auto generator = []() { return rand() % 128; };
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700658 // Generate 100 blocks of random data.
Tianjie Xu107a34f2017-06-29 17:04:21 -0700659 std::string brotli_new_data;
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700660 brotli_new_data.reserve(4096 * 100);
661 generate_n(back_inserter(brotli_new_data), 4096 * 100, generator);
Tianjie Xu107a34f2017-06-29 17:04:21 -0700662
663 size_t encoded_size = BrotliEncoderMaxCompressedSize(brotli_new_data.size());
Tianjie Xu5450c842017-10-18 13:15:21 -0700664 std::string encoded_data(encoded_size, 0);
Tianjie Xu107a34f2017-06-29 17:04:21 -0700665 ASSERT_TRUE(BrotliEncoderCompress(
666 BROTLI_DEFAULT_QUALITY, BROTLI_DEFAULT_WINDOW, BROTLI_DEFAULT_MODE, brotli_new_data.size(),
Tianjie Xu5450c842017-10-18 13:15:21 -0700667 reinterpret_cast<const uint8_t*>(brotli_new_data.data()), &encoded_size,
668 reinterpret_cast<uint8_t*>(const_cast<char*>(encoded_data.data()))));
669 encoded_data.resize(encoded_size);
Tianjie Xu107a34f2017-06-29 17:04:21 -0700670
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700671 // Write a few small chunks of new data, then a large chunk, and finally a few small chunks.
672 // This helps us to catch potential short writes.
Tianjie Xu107a34f2017-06-29 17:04:21 -0700673 std::vector<std::string> transfer_list = {
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700674 "4",
675 "100",
676 "0",
677 "0",
678 "new 2,0,1",
679 "new 2,1,2",
680 "new 4,2,50,50,97",
681 "new 2,97,98",
682 "new 2,98,99",
683 "new 2,99,100",
Tianjie Xu107a34f2017-06-29 17:04:21 -0700684 };
Tianjie Xu5450c842017-10-18 13:15:21 -0700685
Tao Baobc4a6d52018-05-23 00:12:21 -0700686 PackageEntries entries{
687 { "new_data.br", std::move(encoded_data) },
Tianjie Xu5450c842017-10-18 13:15:21 -0700688 { "patch_data", "" },
689 { "transfer_list", android::base::Join(transfer_list, '\n') },
690 };
691
Tao Baobc4a6d52018-05-23 00:12:21 -0700692 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu107a34f2017-06-29 17:04:21 -0700693
694 std::string updated_content;
Tao Baobc4a6d52018-05-23 00:12:21 -0700695 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_content));
Tianjie Xu107a34f2017-06-29 17:04:21 -0700696 ASSERT_EQ(brotli_new_data, updated_content);
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700697}
Tianjie Xu284752e2017-12-05 11:04:17 -0800698
699TEST_F(UpdaterTest, last_command_update) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700700 std::string block1(4096, '1');
701 std::string block2(4096, '2');
702 std::string block3(4096, '3');
Tianjie Xu284752e2017-12-05 11:04:17 -0800703 std::string block1_hash = get_sha1(block1);
704 std::string block2_hash = get_sha1(block2);
705 std::string block3_hash = get_sha1(block3);
706
707 // Compose the transfer list to fail the first update.
Tao Baobc4a6d52018-05-23 00:12:21 -0700708 std::vector<std::string> transfer_list_fail{
709 // clang-format off
Tianjie Xu284752e2017-12-05 11:04:17 -0800710 "4",
711 "2",
712 "0",
713 "2",
714 "stash " + block1_hash + " 2,0,1",
715 "move " + block1_hash + " 2,1,2 1 2,0,1",
716 "stash " + block3_hash + " 2,2,3",
717 "fail",
Tao Baobc4a6d52018-05-23 00:12:21 -0700718 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -0800719 };
720
721 // Mimic a resumed update with the same transfer commands.
Tao Baobc4a6d52018-05-23 00:12:21 -0700722 std::vector<std::string> transfer_list_continue{
723 // clang-format off
Tianjie Xu284752e2017-12-05 11:04:17 -0800724 "4",
725 "2",
726 "0",
727 "2",
728 "stash " + block1_hash + " 2,0,1",
729 "move " + block1_hash + " 2,1,2 1 2,0,1",
730 "stash " + block3_hash + " 2,2,3",
731 "move " + block1_hash + " 2,2,3 1 2,0,1",
Tao Baobc4a6d52018-05-23 00:12:21 -0700732 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -0800733 };
734
Tao Baobc4a6d52018-05-23 00:12:21 -0700735 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_));
736
737 PackageEntries entries{
Tianjie Xu284752e2017-12-05 11:04:17 -0800738 { "new_data", "" },
739 { "patch_data", "" },
Tao Baobc4a6d52018-05-23 00:12:21 -0700740 { "transfer_list", android::base::Join(transfer_list_fail, '\n') },
Tianjie Xu284752e2017-12-05 11:04:17 -0800741 };
742
Tao Baobc4a6d52018-05-23 00:12:21 -0700743 // "2\nstash " + block3_hash + " 2,2,3"
744 std::string last_command_content = "2\n" + transfer_list_fail[kTransferListHeaderLines + 2];
Tianjie Xu284752e2017-12-05 11:04:17 -0800745
Tao Baobc4a6d52018-05-23 00:12:21 -0700746 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu284752e2017-12-05 11:04:17 -0800747
748 // Expect last_command to contain the last stash command.
Tao Baobc4a6d52018-05-23 00:12:21 -0700749 std::string last_command_actual;
Tao Bao7064aa22018-05-24 21:43:50 -0700750 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
Tao Baobc4a6d52018-05-23 00:12:21 -0700751 EXPECT_EQ(last_command_content, last_command_actual);
752
Tianjie Xu284752e2017-12-05 11:04:17 -0800753 std::string updated_contents;
Tao Baobc4a6d52018-05-23 00:12:21 -0700754 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_contents));
Tianjie Xu284752e2017-12-05 11:04:17 -0800755 ASSERT_EQ(block1 + block1 + block3, updated_contents);
756
Tao Baobc4a6d52018-05-23 00:12:21 -0700757 // "Resume" the update. Expect the first 'move' to be skipped but the second 'move' to be
758 // executed. Note that we intentionally reset the image file.
759 entries["transfer_list"] = android::base::Join(transfer_list_continue, '\n');
760 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_));
761 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu284752e2017-12-05 11:04:17 -0800762
Tao Baobc4a6d52018-05-23 00:12:21 -0700763 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_contents));
764 ASSERT_EQ(block1 + block2 + block1, updated_contents);
Tianjie Xu284752e2017-12-05 11:04:17 -0800765}
766
767TEST_F(UpdaterTest, last_command_update_unresumable) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700768 std::string block1(4096, '1');
769 std::string block2(4096, '2');
Tianjie Xu284752e2017-12-05 11:04:17 -0800770 std::string block1_hash = get_sha1(block1);
771 std::string block2_hash = get_sha1(block2);
772
773 // Construct an unresumable update with source blocks mismatch.
Tao Baobc4a6d52018-05-23 00:12:21 -0700774 std::vector<std::string> transfer_list_unresumable{
775 // clang-format off
776 "4",
777 "2",
778 "0",
779 "2",
780 "stash " + block1_hash + " 2,0,1",
781 "move " + block2_hash + " 2,1,2 1 2,0,1",
782 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -0800783 };
784
Tao Baobc4a6d52018-05-23 00:12:21 -0700785 PackageEntries entries{
Tianjie Xu284752e2017-12-05 11:04:17 -0800786 { "new_data", "" },
787 { "patch_data", "" },
Tao Baobc4a6d52018-05-23 00:12:21 -0700788 { "transfer_list", android::base::Join(transfer_list_unresumable, '\n') },
Tianjie Xu284752e2017-12-05 11:04:17 -0800789 };
790
Tao Baobc4a6d52018-05-23 00:12:21 -0700791 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block1, image_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -0800792
Tao Baobc4a6d52018-05-23 00:12:21 -0700793 std::string last_command_content = "0\n" + transfer_list_unresumable[kTransferListHeaderLines];
Tao Bao7064aa22018-05-24 21:43:50 -0700794 ASSERT_TRUE(android::base::WriteStringToFile(last_command_content, last_command_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -0800795
Tao Baobc4a6d52018-05-23 00:12:21 -0700796 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu284752e2017-12-05 11:04:17 -0800797
Tao Baobc4a6d52018-05-23 00:12:21 -0700798 // The last_command_file will be deleted if the update encounters an unresumable failure later.
Tao Bao7064aa22018-05-24 21:43:50 -0700799 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
Tianjie Xu284752e2017-12-05 11:04:17 -0800800}
801
802TEST_F(UpdaterTest, last_command_verify) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700803 std::string block1(4096, '1');
804 std::string block2(4096, '2');
805 std::string block3(4096, '3');
Tianjie Xu284752e2017-12-05 11:04:17 -0800806 std::string block1_hash = get_sha1(block1);
807 std::string block2_hash = get_sha1(block2);
808 std::string block3_hash = get_sha1(block3);
809
Tao Baobc4a6d52018-05-23 00:12:21 -0700810 std::vector<std::string> transfer_list_verify{
811 // clang-format off
Tianjie Xu284752e2017-12-05 11:04:17 -0800812 "4",
813 "2",
814 "0",
815 "2",
816 "stash " + block1_hash + " 2,0,1",
817 "move " + block1_hash + " 2,0,1 1 2,0,1",
818 "move " + block1_hash + " 2,1,2 1 2,0,1",
819 "stash " + block3_hash + " 2,2,3",
Tao Baobc4a6d52018-05-23 00:12:21 -0700820 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -0800821 };
822
Tao Baobc4a6d52018-05-23 00:12:21 -0700823 PackageEntries entries{
Tianjie Xu284752e2017-12-05 11:04:17 -0800824 { "new_data", "" },
825 { "patch_data", "" },
Tao Baobc4a6d52018-05-23 00:12:21 -0700826 { "transfer_list", android::base::Join(transfer_list_verify, '\n') },
Tianjie Xu284752e2017-12-05 11:04:17 -0800827 };
828
Tao Baobc4a6d52018-05-23 00:12:21 -0700829 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block1 + block3, image_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -0800830
Tao Baobc4a6d52018-05-23 00:12:21 -0700831 // Last command: "move " + block1_hash + " 2,1,2 1 2,0,1"
832 std::string last_command_content = "2\n" + transfer_list_verify[kTransferListHeaderLines + 2];
Tianjie Xu284752e2017-12-05 11:04:17 -0800833
Tao Baobc4a6d52018-05-23 00:12:21 -0700834 // First run: expect the verification to succeed and the last_command_file is intact.
Tao Bao7064aa22018-05-24 21:43:50 -0700835 ASSERT_TRUE(android::base::WriteStringToFile(last_command_content, last_command_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -0800836
Tao Baobc4a6d52018-05-23 00:12:21 -0700837 RunBlockImageUpdate(true, entries, image_file_, "t");
Tianjie Xu284752e2017-12-05 11:04:17 -0800838
Tao Baobc4a6d52018-05-23 00:12:21 -0700839 std::string last_command_actual;
Tao Bao7064aa22018-05-24 21:43:50 -0700840 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
Tao Baobc4a6d52018-05-23 00:12:21 -0700841 EXPECT_EQ(last_command_content, last_command_actual);
Tianjie Xu284752e2017-12-05 11:04:17 -0800842
Tao Baobc4a6d52018-05-23 00:12:21 -0700843 // Second run with a mismatching block image: expect the verification to succeed but
844 // last_command_file to be deleted; because the target blocks in the last command don't have the
845 // expected contents for the second move command.
846 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_));
847 RunBlockImageUpdate(true, entries, image_file_, "t");
Tao Bao7064aa22018-05-24 21:43:50 -0700848 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
Tianjie Xu284752e2017-12-05 11:04:17 -0800849}
Tao Baoc0299ed2018-05-24 00:16:35 -0700850
851class ResumableUpdaterTest : public testing::TestWithParam<size_t> {
852 protected:
853 void SetUp() override {
854 RegisterBuiltins();
855 RegisterInstallFunctions();
856 RegisterBlockImageFunctions();
857
858 Paths::Get().set_cache_temp_source(temp_saved_source_.path);
859 Paths::Get().set_last_command_file(temp_last_command_.path);
860 Paths::Get().set_stash_directory_base(temp_stash_base_.path);
861
862 index_ = GetParam();
863 image_file_ = image_temp_file_.path;
864 last_command_file_ = temp_last_command_.path;
865 }
866
867 void TearDown() override {
868 // Clean up the last_command_file if any.
869 ASSERT_TRUE(android::base::RemoveFileIfExists(last_command_file_));
870
871 // Clear partition updated marker if any.
872 std::string updated_marker{ temp_stash_base_.path };
873 updated_marker += "/" + get_sha1(image_temp_file_.path) + ".UPDATED";
874 ASSERT_TRUE(android::base::RemoveFileIfExists(updated_marker));
875 }
876
877 TemporaryFile temp_saved_source_;
878 TemporaryDir temp_stash_base_;
879 std::string last_command_file_;
880 std::string image_file_;
881 size_t index_;
882
883 private:
884 TemporaryFile temp_last_command_;
885 TemporaryFile image_temp_file_;
886};
887
888static std::string g_source_image;
889static std::string g_target_image;
890static PackageEntries g_entries;
891
892static std::vector<std::string> GenerateTransferList() {
893 std::string a(4096, 'a');
894 std::string b(4096, 'b');
895 std::string c(4096, 'c');
896 std::string d(4096, 'd');
897 std::string e(4096, 'e');
898 std::string f(4096, 'f');
899 std::string g(4096, 'g');
900 std::string h(4096, 'h');
901 std::string i(4096, 'i');
902 std::string zero(4096, '\0');
903
904 std::string a_hash = get_sha1(a);
905 std::string b_hash = get_sha1(b);
906 std::string c_hash = get_sha1(c);
907 std::string e_hash = get_sha1(e);
908
909 auto loc = [](const std::string& range_text) {
910 std::vector<std::string> pieces = android::base::Split(range_text, "-");
911 size_t left;
912 size_t right;
913 if (pieces.size() == 1) {
914 CHECK(android::base::ParseUint(pieces[0], &left));
915 right = left + 1;
916 } else {
917 CHECK_EQ(2u, pieces.size());
918 CHECK(android::base::ParseUint(pieces[0], &left));
919 CHECK(android::base::ParseUint(pieces[1], &right));
920 right++;
921 }
922 return android::base::StringPrintf("2,%zu,%zu", left, right);
923 };
924
925 // patch 1: "b d c" -> "g"
926 TemporaryFile patch_file_bdc_g;
927 std::string bdc = b + d + c;
928 std::string bdc_hash = get_sha1(bdc);
929 std::string g_hash = get_sha1(g);
930 CHECK_EQ(0, bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(bdc.data()), bdc.size(),
931 reinterpret_cast<const uint8_t*>(g.data()), g.size(),
932 patch_file_bdc_g.path, nullptr));
933 std::string patch_bdc_g;
934 CHECK(android::base::ReadFileToString(patch_file_bdc_g.path, &patch_bdc_g));
935
936 // patch 2: "a b c d" -> "d c b"
937 TemporaryFile patch_file_abcd_dcb;
938 std::string abcd = a + b + c + d;
939 std::string abcd_hash = get_sha1(abcd);
940 std::string dcb = d + c + b;
941 std::string dcb_hash = get_sha1(dcb);
942 CHECK_EQ(0, bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(abcd.data()), abcd.size(),
943 reinterpret_cast<const uint8_t*>(dcb.data()), dcb.size(),
944 patch_file_abcd_dcb.path, nullptr));
945 std::string patch_abcd_dcb;
946 CHECK(android::base::ReadFileToString(patch_file_abcd_dcb.path, &patch_abcd_dcb));
947
948 std::vector<std::string> transfer_list{
949 "4",
950 "10", // total blocks written
951 "2", // maximum stash entries
952 "2", // maximum number of stashed blocks
953
954 // a b c d e a b c d e
955 "stash " + b_hash + " " + loc("1"),
956 // a b c d e a b c d e [b(1)]
957 "stash " + c_hash + " " + loc("2"),
958 // a b c d e a b c d e [b(1)][c(2)]
959 "new " + loc("1-2"),
960 // a i h d e a b c d e [b(1)][c(2)]
961 "zero " + loc("0"),
962 // 0 i h d e a b c d e [b(1)][c(2)]
963
964 // bsdiff "b d c" (from stash, 3, stash) to get g(3)
965 android::base::StringPrintf(
966 "bsdiff 0 %zu %s %s %s 3 %s %s %s:%s %s:%s",
967 patch_bdc_g.size(), // patch start (0), patch length
968 bdc_hash.c_str(), // source hash
969 g_hash.c_str(), // target hash
970 loc("3").c_str(), // target range
971 loc("3").c_str(), loc("1").c_str(), // load "d" from block 3, into buffer at offset 1
972 b_hash.c_str(), loc("0").c_str(), // load "b" from stash, into buffer at offset 0
973 c_hash.c_str(), loc("2").c_str()), // load "c" from stash, into buffer at offset 2
974
975 // 0 i h g e a b c d e [b(1)][c(2)]
976 "free " + b_hash,
977 // 0 i h g e a b c d e [c(2)]
978 "free " + a_hash,
979 // 0 i h g e a b c d e
980 "stash " + a_hash + " " + loc("5"),
981 // 0 i h g e a b c d e [a(5)]
982 "move " + e_hash + " " + loc("5") + " 1 " + loc("4"),
983 // 0 i h g e e b c d e [a(5)]
984
985 // bsdiff "a b c d" (from stash, 6-8) to "d c b" (6-8)
986 android::base::StringPrintf( //
987 "bsdiff %zu %zu %s %s %s 4 %s %s %s:%s",
988 patch_bdc_g.size(), // patch start
989 patch_bdc_g.size() + patch_abcd_dcb.size(), // patch length
990 abcd_hash.c_str(), // source hash
991 dcb_hash.c_str(), // target hash
992 loc("6-8").c_str(), // target range
993 loc("6-8").c_str(), // load "b c d" from blocks 6-8
994 loc("1-3").c_str(), // into buffer at offset 1-3
995 a_hash.c_str(), // load "a" from stash
996 loc("0").c_str()), // into buffer at offset 0
997
998 // 0 i h g e e d c b e [a(5)]
999 "new " + loc("4"),
1000 // 0 i h g f e d c b e [a(5)]
1001 "move " + a_hash + " " + loc("9") + " 1 - " + a_hash + ":" + loc("0"),
1002 // 0 i h g f e d c b a [a(5)]
1003 "free " + a_hash,
1004 // 0 i h g f e d c b a
1005 };
1006
1007 std::string new_data = i + h + f;
1008 std::string patch_data = patch_bdc_g + patch_abcd_dcb;
1009
1010 g_entries = {
1011 { "new_data", new_data },
1012 { "patch_data", patch_data },
1013 };
1014 g_source_image = a + b + c + d + e + a + b + c + d + e;
1015 g_target_image = zero + i + h + g + f + e + d + c + b + a;
1016
1017 return transfer_list;
1018}
1019
1020static const std::vector<std::string> g_transfer_list = GenerateTransferList();
1021
1022INSTANTIATE_TEST_CASE_P(InterruptAfterEachCommand, ResumableUpdaterTest,
1023 ::testing::Range(static_cast<size_t>(0),
1024 g_transfer_list.size() - kTransferListHeaderLines));
1025
1026TEST_P(ResumableUpdaterTest, InterruptVerifyResume) {
1027 ASSERT_TRUE(android::base::WriteStringToFile(g_source_image, image_file_));
1028
1029 LOG(INFO) << "Interrupting at line " << index_ << " ("
1030 << g_transfer_list[kTransferListHeaderLines + index_] << ")";
1031
1032 std::vector<std::string> transfer_list_copy{ g_transfer_list };
1033 transfer_list_copy[kTransferListHeaderLines + index_] = "fail";
1034
1035 g_entries["transfer_list"] = android::base::Join(transfer_list_copy, '\n');
1036
1037 // Run update that's expected to fail.
1038 RunBlockImageUpdate(false, g_entries, image_file_, "");
1039
1040 std::string last_command_expected;
1041
1042 // Assert the last_command_file.
1043 if (index_ == 0) {
1044 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
1045 } else {
1046 last_command_expected =
1047 std::to_string(index_ - 1) + "\n" + g_transfer_list[kTransferListHeaderLines + index_ - 1];
1048 std::string last_command_actual;
1049 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
1050 ASSERT_EQ(last_command_expected, last_command_actual);
1051 }
1052
1053 g_entries["transfer_list"] = android::base::Join(g_transfer_list, '\n');
1054
1055 // Resume the interrupted update, by doing verification first.
1056 RunBlockImageUpdate(true, g_entries, image_file_, "t");
1057
1058 // last_command_file should remain intact.
1059 if (index_ == 0) {
1060 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
1061 } else {
1062 std::string last_command_actual;
1063 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
1064 ASSERT_EQ(last_command_expected, last_command_actual);
1065 }
1066
1067 // Resume the update.
1068 RunBlockImageUpdate(false, g_entries, image_file_, "t");
1069
1070 // last_command_file should be gone after successful update.
1071 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
1072
1073 std::string updated_image_actual;
1074 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_image_actual));
1075 ASSERT_EQ(g_target_image, updated_image_actual);
1076}