blob: e8abb535063578aa373ac9c6e35d276dbbf6b1ff [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"
Tao Bao91a649a2018-05-21 16:05:56 -070049#include "private/commands.h"
Tianjie Xu56ebe622017-03-16 00:48:21 -070050#include "updater/blockimg.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070051#include "updater/install.h"
Tao Baoef0eb3b2016-11-14 21:29:52 -080052#include "updater/updater.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070053
Tao Baobc4a6d52018-05-23 00:12:21 -070054using PackageEntries = std::unordered_map<std::string, std::string>;
55
56static constexpr size_t kTransferListHeaderLines = 4;
57
58struct selabel_handle* sehandle = nullptr;
Tao Bao0c7839a2016-10-10 15:48:37 -070059
Tao Baoef0eb3b2016-11-14 21:29:52 -080060static void expect(const char* expected, const char* expr_str, CauseCode cause_code,
61 UpdaterInfo* info = nullptr) {
Tianjie Xuc4447322017-03-06 14:44:59 -080062 std::unique_ptr<Expr> e;
Tao Baoef0eb3b2016-11-14 21:29:52 -080063 int error_count = 0;
64 ASSERT_EQ(0, parse_string(expr_str, &e, &error_count));
65 ASSERT_EQ(0, error_count);
Tao Bao0c7839a2016-10-10 15:48:37 -070066
Tao Baoef0eb3b2016-11-14 21:29:52 -080067 State state(expr_str, info);
Tao Bao0c7839a2016-10-10 15:48:37 -070068
Tao Baoef0eb3b2016-11-14 21:29:52 -080069 std::string result;
70 bool status = Evaluate(&state, e, &result);
Tao Bao0c7839a2016-10-10 15:48:37 -070071
Tao Baoef0eb3b2016-11-14 21:29:52 -080072 if (expected == nullptr) {
73 ASSERT_FALSE(status);
74 } else {
75 ASSERT_TRUE(status);
76 ASSERT_STREQ(expected, result.c_str());
77 }
Tao Bao0c7839a2016-10-10 15:48:37 -070078
Tao Baoef0eb3b2016-11-14 21:29:52 -080079 // Error code is set in updater/updater.cpp only, by parsing State.errmsg.
80 ASSERT_EQ(kNoError, state.error_code);
Tao Bao361342c2016-02-08 11:15:50 -080081
Tao Baoef0eb3b2016-11-14 21:29:52 -080082 // Cause code should always be available.
83 ASSERT_EQ(cause_code, state.cause_code);
Tao Bao0c7839a2016-10-10 15:48:37 -070084}
85
Tao Baobc4a6d52018-05-23 00:12:21 -070086static void BuildUpdatePackage(const PackageEntries& entries, int fd) {
Tianjie Xu5450c842017-10-18 13:15:21 -070087 FILE* zip_file_ptr = fdopen(fd, "wb");
88 ZipWriter zip_writer(zip_file_ptr);
89
90 for (const auto& entry : entries) {
Tao Baobc4a6d52018-05-23 00:12:21 -070091 // All the entries are written as STORED.
Tianjie Xu5450c842017-10-18 13:15:21 -070092 ASSERT_EQ(0, zip_writer.StartEntry(entry.first.c_str(), 0));
93 if (!entry.second.empty()) {
94 ASSERT_EQ(0, zip_writer.WriteBytes(entry.second.data(), entry.second.size()));
95 }
96 ASSERT_EQ(0, zip_writer.FinishEntry());
97 }
98
99 ASSERT_EQ(0, zip_writer.Finish());
100 ASSERT_EQ(0, fclose(zip_file_ptr));
101}
102
Tao Baobc4a6d52018-05-23 00:12:21 -0700103static void RunBlockImageUpdate(bool is_verify, const PackageEntries& entries,
Tao Baoffede3e2018-06-07 09:56:19 -0700104 const std::string& image_file, const std::string& result,
105 CauseCode cause_code = kNoCause) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700106 CHECK(entries.find("transfer_list") != entries.end());
107
108 // Build the update package.
109 TemporaryFile zip_file;
110 BuildUpdatePackage(entries, zip_file.release());
111
112 MemMapping map;
113 ASSERT_TRUE(map.MapFile(zip_file.path));
114 ZipArchiveHandle handle;
115 ASSERT_EQ(0, OpenArchiveFromMemory(map.addr, map.length, zip_file.path, &handle));
116
117 // Set up the handler, command_pipe, patch offset & length.
118 UpdaterInfo updater_info;
119 updater_info.package_zip = handle;
120 TemporaryFile temp_pipe;
121 updater_info.cmd_pipe = fdopen(temp_pipe.release(), "wbe");
122 updater_info.package_zip_addr = map.addr;
123 updater_info.package_zip_len = map.length;
124
125 std::string new_data = entries.find("new_data.br") != entries.end() ? "new_data.br" : "new_data";
126 std::string script = is_verify ? "block_image_verify" : "block_image_update";
127 script += R"((")" + image_file + R"(", package_extract_file("transfer_list"), ")" + new_data +
128 R"(", "patch_data"))";
Tao Baoffede3e2018-06-07 09:56:19 -0700129 expect(result.c_str(), script.c_str(), cause_code, &updater_info);
Tao Baobc4a6d52018-05-23 00:12:21 -0700130
131 ASSERT_EQ(0, fclose(updater_info.cmd_pipe));
132 CloseArchive(handle);
133}
134
Tianjie Xu56ebe622017-03-16 00:48:21 -0700135static std::string get_sha1(const std::string& content) {
136 uint8_t digest[SHA_DIGEST_LENGTH];
137 SHA1(reinterpret_cast<const uint8_t*>(content.c_str()), content.size(), digest);
138 return print_sha1(digest);
139}
140
Tao Bao0c7839a2016-10-10 15:48:37 -0700141class UpdaterTest : public ::testing::Test {
Tianjie Xu56ebe622017-03-16 00:48:21 -0700142 protected:
Tao Baobc4a6d52018-05-23 00:12:21 -0700143 void SetUp() override {
Tianjie Xu56ebe622017-03-16 00:48:21 -0700144 RegisterBuiltins();
145 RegisterInstallFunctions();
146 RegisterBlockImageFunctions();
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800147
Tao Bao7064aa22018-05-24 21:43:50 -0700148 // Each test is run in a separate process (isolated mode). Shared temporary files won't cause
149 // conflicts.
Tao Bao641fa972018-04-25 18:59:40 -0700150 Paths::Get().set_cache_temp_source(temp_saved_source_.path);
Tao Bao7064aa22018-05-24 21:43:50 -0700151 Paths::Get().set_last_command_file(temp_last_command_.path);
Tao Bao641fa972018-04-25 18:59:40 -0700152 Paths::Get().set_stash_directory_base(temp_stash_base_.path);
Tao Baobc4a6d52018-05-23 00:12:21 -0700153
Tao Bao91a649a2018-05-21 16:05:56 -0700154 // Enable a special command "abort" to simulate interruption.
155 Command::abort_allowed_ = true;
156
Tao Bao7064aa22018-05-24 21:43:50 -0700157 last_command_file_ = temp_last_command_.path;
Tao Baobc4a6d52018-05-23 00:12:21 -0700158 image_file_ = image_temp_file_.path;
159 }
160
161 void TearDown() override {
Tao Bao7064aa22018-05-24 21:43:50 -0700162 // Clean up the last_command_file if any.
163 ASSERT_TRUE(android::base::RemoveFileIfExists(last_command_file_));
164
Tao Baobc4a6d52018-05-23 00:12:21 -0700165 // Clear partition updated marker if any.
166 std::string updated_marker{ temp_stash_base_.path };
167 updated_marker += "/" + get_sha1(image_temp_file_.path) + ".UPDATED";
168 ASSERT_TRUE(android::base::RemoveFileIfExists(updated_marker));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700169 }
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800170
171 TemporaryFile temp_saved_source_;
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800172 TemporaryDir temp_stash_base_;
Tao Bao7064aa22018-05-24 21:43:50 -0700173 std::string last_command_file_;
Tao Baobc4a6d52018-05-23 00:12:21 -0700174 std::string image_file_;
175
176 private:
Tao Bao7064aa22018-05-24 21:43:50 -0700177 TemporaryFile temp_last_command_;
Tao Baobc4a6d52018-05-23 00:12:21 -0700178 TemporaryFile image_temp_file_;
Tao Bao0c7839a2016-10-10 15:48:37 -0700179};
180
181TEST_F(UpdaterTest, getprop) {
182 expect(android::base::GetProperty("ro.product.device", "").c_str(),
183 "getprop(\"ro.product.device\")",
Tao Bao361342c2016-02-08 11:15:50 -0800184 kNoCause);
Tao Bao0c7839a2016-10-10 15:48:37 -0700185
186 expect(android::base::GetProperty("ro.build.fingerprint", "").c_str(),
187 "getprop(\"ro.build.fingerprint\")",
Tao Bao361342c2016-02-08 11:15:50 -0800188 kNoCause);
Tao Bao0c7839a2016-10-10 15:48:37 -0700189
190 // getprop() accepts only one parameter.
Tao Bao361342c2016-02-08 11:15:50 -0800191 expect(nullptr, "getprop()", kArgsParsingFailure);
192 expect(nullptr, "getprop(\"arg1\", \"arg2\")", kArgsParsingFailure);
193}
194
195TEST_F(UpdaterTest, sha1_check) {
196 // sha1_check(data) returns the SHA-1 of the data.
197 expect("81fe8bfe87576c3ecb22426f8e57847382917acf", "sha1_check(\"abcd\")", kNoCause);
198 expect("da39a3ee5e6b4b0d3255bfef95601890afd80709", "sha1_check(\"\")", kNoCause);
199
200 // sha1_check(data, sha1_hex, [sha1_hex, ...]) returns the matched SHA-1.
201 expect("81fe8bfe87576c3ecb22426f8e57847382917acf",
202 "sha1_check(\"abcd\", \"81fe8bfe87576c3ecb22426f8e57847382917acf\")",
203 kNoCause);
204
205 expect("81fe8bfe87576c3ecb22426f8e57847382917acf",
206 "sha1_check(\"abcd\", \"wrong_sha1\", \"81fe8bfe87576c3ecb22426f8e57847382917acf\")",
207 kNoCause);
208
209 // Or "" if there's no match.
210 expect("",
211 "sha1_check(\"abcd\", \"wrong_sha1\")",
212 kNoCause);
213
214 expect("",
215 "sha1_check(\"abcd\", \"wrong_sha1\", \"wrong_sha2\")",
216 kNoCause);
217
218 // sha1_check() expects at least one argument.
219 expect(nullptr, "sha1_check()", kArgsParsingFailure);
Tao Bao0c7839a2016-10-10 15:48:37 -0700220}
Tao Bao51d516e2016-11-03 14:49:01 -0700221
Tao Baodb56eb02017-03-23 06:34:20 -0700222TEST_F(UpdaterTest, apply_patch_check) {
223 // Zero-argument is not valid.
224 expect(nullptr, "apply_patch_check()", kArgsParsingFailure);
225
226 // File not found.
227 expect("", "apply_patch_check(\"/doesntexist\")", kNoCause);
228
229 std::string src_file = from_testdata_base("old.file");
230 std::string src_content;
231 ASSERT_TRUE(android::base::ReadFileToString(src_file, &src_content));
232 size_t src_size = src_content.size();
233 std::string src_hash = get_sha1(src_content);
234
235 // One-argument with EMMC:file:size:sha1 should pass the check.
236 std::string filename = android::base::Join(
237 std::vector<std::string>{ "EMMC", src_file, std::to_string(src_size), src_hash }, ":");
238 std::string cmd = "apply_patch_check(\"" + filename + "\")";
239 expect("t", cmd.c_str(), kNoCause);
240
241 // EMMC:file:(size-1):sha1:(size+1):sha1 should fail the check.
242 std::string filename_bad = android::base::Join(
243 std::vector<std::string>{ "EMMC", src_file, std::to_string(src_size - 1), src_hash,
244 std::to_string(src_size + 1), src_hash },
245 ":");
246 cmd = "apply_patch_check(\"" + filename_bad + "\")";
247 expect("", cmd.c_str(), kNoCause);
248
249 // EMMC:file:(size-1):sha1:size:sha1:(size+1):sha1 should pass the check.
250 filename_bad =
251 android::base::Join(std::vector<std::string>{ "EMMC", src_file, std::to_string(src_size - 1),
252 src_hash, std::to_string(src_size), src_hash,
253 std::to_string(src_size + 1), src_hash },
254 ":");
255 cmd = "apply_patch_check(\"" + filename_bad + "\")";
256 expect("t", cmd.c_str(), kNoCause);
257
258 // Multiple arguments.
259 cmd = "apply_patch_check(\"" + filename + "\", \"wrong_sha1\", \"wrong_sha2\")";
260 expect("", cmd.c_str(), kNoCause);
261
262 cmd = "apply_patch_check(\"" + filename + "\", \"wrong_sha1\", \"" + src_hash +
263 "\", \"wrong_sha2\")";
264 expect("t", cmd.c_str(), kNoCause);
265
266 cmd = "apply_patch_check(\"" + filename_bad + "\", \"wrong_sha1\", \"" + src_hash +
267 "\", \"wrong_sha2\")";
268 expect("t", cmd.c_str(), kNoCause);
269}
270
Tao Bao51d516e2016-11-03 14:49:01 -0700271TEST_F(UpdaterTest, file_getprop) {
272 // file_getprop() expects two arguments.
273 expect(nullptr, "file_getprop()", kArgsParsingFailure);
274 expect(nullptr, "file_getprop(\"arg1\")", kArgsParsingFailure);
275 expect(nullptr, "file_getprop(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
276
277 // File doesn't exist.
278 expect(nullptr, "file_getprop(\"/doesntexist\", \"key1\")", kFileGetPropFailure);
279
280 // Reject too large files (current limit = 65536).
281 TemporaryFile temp_file1;
282 std::string buffer(65540, '\0');
283 ASSERT_TRUE(android::base::WriteStringToFile(buffer, temp_file1.path));
284
285 // Read some keys.
286 TemporaryFile temp_file2;
287 std::string content("ro.product.name=tardis\n"
288 "# comment\n\n\n"
289 "ro.product.model\n"
290 "ro.product.board = magic \n");
291 ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file2.path));
292
293 std::string script1("file_getprop(\"" + std::string(temp_file2.path) +
294 "\", \"ro.product.name\")");
295 expect("tardis", script1.c_str(), kNoCause);
296
297 std::string script2("file_getprop(\"" + std::string(temp_file2.path) +
298 "\", \"ro.product.board\")");
299 expect("magic", script2.c_str(), kNoCause);
300
301 // No match.
302 std::string script3("file_getprop(\"" + std::string(temp_file2.path) +
303 "\", \"ro.product.wrong\")");
304 expect("", script3.c_str(), kNoCause);
305
306 std::string script4("file_getprop(\"" + std::string(temp_file2.path) +
307 "\", \"ro.product.name=\")");
308 expect("", script4.c_str(), kNoCause);
309
310 std::string script5("file_getprop(\"" + std::string(temp_file2.path) +
311 "\", \"ro.product.nam\")");
312 expect("", script5.c_str(), kNoCause);
313
314 std::string script6("file_getprop(\"" + std::string(temp_file2.path) +
315 "\", \"ro.product.model\")");
316 expect("", script6.c_str(), kNoCause);
317}
Tao Bao0831d0b2016-11-03 23:25:04 -0700318
Tao Baoef0eb3b2016-11-14 21:29:52 -0800319// TODO: Test extracting to block device.
320TEST_F(UpdaterTest, package_extract_file) {
321 // package_extract_file expects 1 or 2 arguments.
322 expect(nullptr, "package_extract_file()", kArgsParsingFailure);
323 expect(nullptr, "package_extract_file(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
324
325 std::string zip_path = from_testdata_base("ziptest_valid.zip");
326 ZipArchiveHandle handle;
327 ASSERT_EQ(0, OpenArchive(zip_path.c_str(), &handle));
328
329 // Need to set up the ziphandle.
330 UpdaterInfo updater_info;
331 updater_info.package_zip = handle;
332
333 // Two-argument version.
334 TemporaryFile temp_file1;
335 std::string script("package_extract_file(\"a.txt\", \"" + std::string(temp_file1.path) + "\")");
336 expect("t", script.c_str(), kNoCause, &updater_info);
337
338 // Verify the extracted entry.
339 std::string data;
340 ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data));
341 ASSERT_EQ(kATxtContents, data);
342
343 // Now extract another entry to the same location, which should overwrite.
344 script = "package_extract_file(\"b.txt\", \"" + std::string(temp_file1.path) + "\")";
345 expect("t", script.c_str(), kNoCause, &updater_info);
346
347 ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data));
348 ASSERT_EQ(kBTxtContents, data);
349
350 // Missing zip entry. The two-argument version doesn't abort.
351 script = "package_extract_file(\"doesntexist\", \"" + std::string(temp_file1.path) + "\")";
352 expect("", script.c_str(), kNoCause, &updater_info);
353
354 // Extract to /dev/full should fail.
355 script = "package_extract_file(\"a.txt\", \"/dev/full\")";
356 expect("", script.c_str(), kNoCause, &updater_info);
357
358 // One-argument version.
359 script = "sha1_check(package_extract_file(\"a.txt\"))";
360 expect(kATxtSha1Sum.c_str(), script.c_str(), kNoCause, &updater_info);
361
362 script = "sha1_check(package_extract_file(\"b.txt\"))";
363 expect(kBTxtSha1Sum.c_str(), script.c_str(), kNoCause, &updater_info);
364
365 // Missing entry. The one-argument version aborts the evaluation.
366 script = "package_extract_file(\"doesntexist\")";
367 expect(nullptr, script.c_str(), kPackageExtractFileFailure, &updater_info);
368
369 CloseArchive(handle);
370}
Tao Baod0f30882016-11-03 23:52:01 -0700371
372TEST_F(UpdaterTest, write_value) {
373 // write_value() expects two arguments.
374 expect(nullptr, "write_value()", kArgsParsingFailure);
375 expect(nullptr, "write_value(\"arg1\")", kArgsParsingFailure);
376 expect(nullptr, "write_value(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
377
378 // filename cannot be empty.
379 expect(nullptr, "write_value(\"value\", \"\")", kArgsParsingFailure);
380
381 // Write some value to file.
382 TemporaryFile temp_file;
383 std::string value = "magicvalue";
384 std::string script("write_value(\"" + value + "\", \"" + std::string(temp_file.path) + "\")");
385 expect("t", script.c_str(), kNoCause);
386
387 // Verify the content.
388 std::string content;
389 ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content));
390 ASSERT_EQ(value, content);
391
392 // Allow writing empty string.
393 script = "write_value(\"\", \"" + std::string(temp_file.path) + "\")";
394 expect("t", script.c_str(), kNoCause);
395
396 // Verify the content.
397 ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content));
398 ASSERT_EQ("", content);
399
400 // It should fail gracefully when write fails.
401 script = "write_value(\"value\", \"/proc/0/file1\")";
402 expect("", script.c_str(), kNoCause);
403}
Tao Baobedf5fc2016-11-18 12:01:26 -0800404
405TEST_F(UpdaterTest, get_stage) {
406 // get_stage() expects one argument.
407 expect(nullptr, "get_stage()", kArgsParsingFailure);
408 expect(nullptr, "get_stage(\"arg1\", \"arg2\")", kArgsParsingFailure);
409 expect(nullptr, "get_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
410
411 // Set up a local file as BCB.
412 TemporaryFile tf;
413 std::string temp_file(tf.path);
414 bootloader_message boot;
415 strlcpy(boot.stage, "2/3", sizeof(boot.stage));
416 std::string err;
417 ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err));
418
419 // Can read the stage value.
420 std::string script("get_stage(\"" + temp_file + "\")");
421 expect("2/3", script.c_str(), kNoCause);
422
423 // Bad BCB path.
424 script = "get_stage(\"doesntexist\")";
425 expect("", script.c_str(), kNoCause);
426}
427
428TEST_F(UpdaterTest, set_stage) {
429 // set_stage() expects two arguments.
430 expect(nullptr, "set_stage()", kArgsParsingFailure);
431 expect(nullptr, "set_stage(\"arg1\")", kArgsParsingFailure);
432 expect(nullptr, "set_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
433
434 // Set up a local file as BCB.
435 TemporaryFile tf;
436 std::string temp_file(tf.path);
437 bootloader_message boot;
438 strlcpy(boot.command, "command", sizeof(boot.command));
439 strlcpy(boot.stage, "2/3", sizeof(boot.stage));
440 std::string err;
441 ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err));
442
443 // Write with set_stage().
444 std::string script("set_stage(\"" + temp_file + "\", \"1/3\")");
445 expect(tf.path, script.c_str(), kNoCause);
446
447 // Verify.
448 bootloader_message boot_verify;
449 ASSERT_TRUE(read_bootloader_message_from(&boot_verify, temp_file, &err));
450
451 // Stage should be updated, with command part untouched.
452 ASSERT_STREQ("1/3", boot_verify.stage);
453 ASSERT_STREQ(boot.command, boot_verify.command);
454
455 // Bad BCB path.
456 script = "set_stage(\"doesntexist\", \"1/3\")";
457 expect("", script.c_str(), kNoCause);
458
459 script = "set_stage(\"/dev/full\", \"1/3\")";
460 expect("", script.c_str(), kNoCause);
461}
Tao Bao9aa7ab52017-01-05 17:27:19 -0800462
463TEST_F(UpdaterTest, set_progress) {
464 // set_progress() expects one argument.
465 expect(nullptr, "set_progress()", kArgsParsingFailure);
466 expect(nullptr, "set_progress(\"arg1\", \"arg2\")", kArgsParsingFailure);
467
468 // Invalid progress argument.
469 expect(nullptr, "set_progress(\"arg1\")", kArgsParsingFailure);
470 expect(nullptr, "set_progress(\"3x+5\")", kArgsParsingFailure);
471 expect(nullptr, "set_progress(\".3.5\")", kArgsParsingFailure);
472
473 TemporaryFile tf;
474 UpdaterInfo updater_info;
Tianjie Xu79327ac2017-09-08 17:09:10 -0700475 updater_info.cmd_pipe = fdopen(tf.release(), "w");
Tao Bao9aa7ab52017-01-05 17:27:19 -0800476 expect(".52", "set_progress(\".52\")", kNoCause, &updater_info);
477 fflush(updater_info.cmd_pipe);
478
479 std::string cmd;
480 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd));
481 ASSERT_EQ(android::base::StringPrintf("set_progress %f\n", .52), cmd);
482 // recovery-updater protocol expects 2 tokens ("set_progress <frac>").
483 ASSERT_EQ(2U, android::base::Split(cmd, " ").size());
Tianjie Xu79327ac2017-09-08 17:09:10 -0700484 ASSERT_EQ(0, fclose(updater_info.cmd_pipe));
Tao Bao9aa7ab52017-01-05 17:27:19 -0800485}
486
487TEST_F(UpdaterTest, show_progress) {
488 // show_progress() expects two arguments.
489 expect(nullptr, "show_progress()", kArgsParsingFailure);
490 expect(nullptr, "show_progress(\"arg1\")", kArgsParsingFailure);
491 expect(nullptr, "show_progress(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
492
493 // Invalid progress arguments.
494 expect(nullptr, "show_progress(\"arg1\", \"arg2\")", kArgsParsingFailure);
495 expect(nullptr, "show_progress(\"3x+5\", \"10\")", kArgsParsingFailure);
496 expect(nullptr, "show_progress(\".3\", \"5a\")", kArgsParsingFailure);
497
498 TemporaryFile tf;
499 UpdaterInfo updater_info;
Tianjie Xu79327ac2017-09-08 17:09:10 -0700500 updater_info.cmd_pipe = fdopen(tf.release(), "w");
Tao Bao9aa7ab52017-01-05 17:27:19 -0800501 expect(".52", "show_progress(\".52\", \"10\")", kNoCause, &updater_info);
502 fflush(updater_info.cmd_pipe);
503
504 std::string cmd;
505 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd));
506 ASSERT_EQ(android::base::StringPrintf("progress %f %d\n", .52, 10), cmd);
507 // recovery-updater protocol expects 3 tokens ("progress <frac> <secs>").
508 ASSERT_EQ(3U, android::base::Split(cmd, " ").size());
Tianjie Xu79327ac2017-09-08 17:09:10 -0700509 ASSERT_EQ(0, fclose(updater_info.cmd_pipe));
Tao Bao9aa7ab52017-01-05 17:27:19 -0800510}
Tianjie Xu56ebe622017-03-16 00:48:21 -0700511
Tao Baoffede3e2018-06-07 09:56:19 -0700512TEST_F(UpdaterTest, block_image_update_parsing_error) {
513 std::vector<std::string> transfer_list{
514 // clang-format off
515 "4",
516 "2",
517 "0",
518 // clang-format on
519 };
520
521 PackageEntries entries{
522 { "new_data", "" },
523 { "patch_data", "" },
524 { "transfer_list", android::base::Join(transfer_list, '\n') },
525 };
526
527 RunBlockImageUpdate(false, entries, image_file_, "", kArgsParsingFailure);
528}
529
Tianjie Xu5450c842017-10-18 13:15:21 -0700530TEST_F(UpdaterTest, block_image_update_patch_data) {
Tianjie Xu56ebe622017-03-16 00:48:21 -0700531 std::string src_content = std::string(4096, 'a') + std::string(4096, 'c');
532 std::string tgt_content = std::string(4096, 'b') + std::string(4096, 'd');
Tianjie Xu5450c842017-10-18 13:15:21 -0700533
534 // Generate the patch data.
Tianjie Xu56ebe622017-03-16 00:48:21 -0700535 TemporaryFile patch_file;
Tao Baobc4a6d52018-05-23 00:12:21 -0700536 ASSERT_EQ(0,
537 bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(src_content.data()), src_content.size(),
538 reinterpret_cast<const uint8_t*>(tgt_content.data()), tgt_content.size(),
539 patch_file.path, nullptr));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700540 std::string patch_content;
541 ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch_content));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700542
Tianjie Xu5450c842017-10-18 13:15:21 -0700543 // Create the transfer list that contains a bsdiff.
Tianjie Xu56ebe622017-03-16 00:48:21 -0700544 std::string src_hash = get_sha1(src_content);
545 std::string tgt_hash = get_sha1(tgt_content);
Tao Baobc4a6d52018-05-23 00:12:21 -0700546 std::vector<std::string> transfer_list{
547 // clang-format off
Tianjie Xu56ebe622017-03-16 00:48:21 -0700548 "4",
549 "2",
550 "0",
551 "2",
552 "stash " + src_hash + " 2,0,2",
553 android::base::StringPrintf("bsdiff 0 %zu %s %s 2,0,2 2 - %s:2,0,2", patch_content.size(),
554 src_hash.c_str(), tgt_hash.c_str(), src_hash.c_str()),
555 "free " + src_hash,
Tao Baobc4a6d52018-05-23 00:12:21 -0700556 // clang-format on
Tianjie Xu56ebe622017-03-16 00:48:21 -0700557 };
Tianjie Xu56ebe622017-03-16 00:48:21 -0700558
Tao Baobc4a6d52018-05-23 00:12:21 -0700559 PackageEntries entries{
Tianjie Xu5450c842017-10-18 13:15:21 -0700560 { "new_data", "" },
561 { "patch_data", patch_content },
562 { "transfer_list", android::base::Join(transfer_list, '\n') },
Tianjie Xu56ebe622017-03-16 00:48:21 -0700563 };
Tianjie Xu5450c842017-10-18 13:15:21 -0700564
Tao Baobc4a6d52018-05-23 00:12:21 -0700565 ASSERT_TRUE(android::base::WriteStringToFile(src_content, image_file_));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700566
Tao Baobc4a6d52018-05-23 00:12:21 -0700567 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu56ebe622017-03-16 00:48:21 -0700568
Tianjie Xu56ebe622017-03-16 00:48:21 -0700569 // The update_file should be patched correctly.
570 std::string updated_content;
Tao Baobc4a6d52018-05-23 00:12:21 -0700571 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_content));
572 ASSERT_EQ(tgt_content, updated_content);
Tianjie Xu5450c842017-10-18 13:15:21 -0700573}
574
575TEST_F(UpdaterTest, block_image_update_fail) {
576 std::string src_content(4096 * 2, 'e');
577 std::string src_hash = get_sha1(src_content);
578 // Stash and free some blocks, then fail the update intentionally.
Tao Baobc4a6d52018-05-23 00:12:21 -0700579 std::vector<std::string> transfer_list{
580 // clang-format off
581 "4",
582 "2",
583 "0",
584 "2",
585 "stash " + src_hash + " 2,0,2",
586 "free " + src_hash,
Tao Bao91a649a2018-05-21 16:05:56 -0700587 "abort",
Tao Baobc4a6d52018-05-23 00:12:21 -0700588 // clang-format on
Tianjie Xu5450c842017-10-18 13:15:21 -0700589 };
590
591 // Add a new data of 10 bytes to test the deadlock.
Tao Baobc4a6d52018-05-23 00:12:21 -0700592 PackageEntries entries{
Tianjie Xu5450c842017-10-18 13:15:21 -0700593 { "new_data", std::string(10, 0) },
594 { "patch_data", "" },
595 { "transfer_list", android::base::Join(transfer_list, '\n') },
596 };
597
Tao Baobc4a6d52018-05-23 00:12:21 -0700598 ASSERT_TRUE(android::base::WriteStringToFile(src_content, image_file_));
Tianjie Xu5450c842017-10-18 13:15:21 -0700599
Tao Baobc4a6d52018-05-23 00:12:21 -0700600 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu5450c842017-10-18 13:15:21 -0700601
Tianjie Xu56ebe622017-03-16 00:48:21 -0700602 // Updater generates the stash name based on the input file name.
Tao Baobc4a6d52018-05-23 00:12:21 -0700603 std::string name_digest = get_sha1(image_file_);
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800604 std::string stash_base = std::string(temp_stash_base_.path) + "/" + name_digest;
Tianjie Xu56ebe622017-03-16 00:48:21 -0700605 ASSERT_EQ(0, access(stash_base.c_str(), F_OK));
Tao Baobc4a6d52018-05-23 00:12:21 -0700606 // Expect the stashed blocks to be freed.
Tianjie Xu5450c842017-10-18 13:15:21 -0700607 ASSERT_EQ(-1, access((stash_base + src_hash).c_str(), F_OK));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700608 ASSERT_EQ(0, rmdir(stash_base.c_str()));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700609}
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700610
Tianjie Xu5450c842017-10-18 13:15:21 -0700611TEST_F(UpdaterTest, new_data_over_write) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700612 std::vector<std::string> transfer_list{
613 // clang-format off
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700614 "4",
615 "1",
616 "0",
617 "0",
618 "new 2,0,1",
Tao Baobc4a6d52018-05-23 00:12:21 -0700619 // clang-format on
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700620 };
Tianjie Xu5450c842017-10-18 13:15:21 -0700621
Tao Baobc4a6d52018-05-23 00:12:21 -0700622 // Write 4096 + 100 bytes of new data.
623 PackageEntries entries{
624 { "new_data", std::string(4196, 0) },
Tianjie Xu5450c842017-10-18 13:15:21 -0700625 { "patch_data", "" },
626 { "transfer_list", android::base::Join(transfer_list, '\n') },
627 };
628
Tao Baobc4a6d52018-05-23 00:12:21 -0700629 RunBlockImageUpdate(false, entries, image_file_, "t");
630}
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700631
Tao Baobc4a6d52018-05-23 00:12:21 -0700632TEST_F(UpdaterTest, new_data_short_write) {
633 std::vector<std::string> transfer_list{
634 // clang-format off
635 "4",
636 "1",
637 "0",
638 "0",
639 "new 2,0,1",
640 // clang-format on
641 };
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700642
Tao Baobc4a6d52018-05-23 00:12:21 -0700643 PackageEntries entries{
644 { "patch_data", "" },
645 { "transfer_list", android::base::Join(transfer_list, '\n') },
646 };
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700647
648 // Updater should report the failure gracefully rather than stuck in deadlock.
Tao Baobc4a6d52018-05-23 00:12:21 -0700649 entries["new_data"] = "";
650 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700651
Tao Baobc4a6d52018-05-23 00:12:21 -0700652 entries["new_data"] = std::string(10, 'a');
653 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700654
655 // Expect to write 1 block of new data successfully.
Tao Baobc4a6d52018-05-23 00:12:21 -0700656 entries["new_data"] = std::string(4096, 'a');
657 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu107a34f2017-06-29 17:04:21 -0700658}
659
660TEST_F(UpdaterTest, brotli_new_data) {
Tianjie Xu107a34f2017-06-29 17:04:21 -0700661 auto generator = []() { return rand() % 128; };
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700662 // Generate 100 blocks of random data.
Tianjie Xu107a34f2017-06-29 17:04:21 -0700663 std::string brotli_new_data;
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700664 brotli_new_data.reserve(4096 * 100);
665 generate_n(back_inserter(brotli_new_data), 4096 * 100, generator);
Tianjie Xu107a34f2017-06-29 17:04:21 -0700666
667 size_t encoded_size = BrotliEncoderMaxCompressedSize(brotli_new_data.size());
Tianjie Xu5450c842017-10-18 13:15:21 -0700668 std::string encoded_data(encoded_size, 0);
Tianjie Xu107a34f2017-06-29 17:04:21 -0700669 ASSERT_TRUE(BrotliEncoderCompress(
670 BROTLI_DEFAULT_QUALITY, BROTLI_DEFAULT_WINDOW, BROTLI_DEFAULT_MODE, brotli_new_data.size(),
Tianjie Xu5450c842017-10-18 13:15:21 -0700671 reinterpret_cast<const uint8_t*>(brotli_new_data.data()), &encoded_size,
672 reinterpret_cast<uint8_t*>(const_cast<char*>(encoded_data.data()))));
673 encoded_data.resize(encoded_size);
Tianjie Xu107a34f2017-06-29 17:04:21 -0700674
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700675 // Write a few small chunks of new data, then a large chunk, and finally a few small chunks.
676 // This helps us to catch potential short writes.
Tianjie Xu107a34f2017-06-29 17:04:21 -0700677 std::vector<std::string> transfer_list = {
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700678 "4",
679 "100",
680 "0",
681 "0",
682 "new 2,0,1",
683 "new 2,1,2",
684 "new 4,2,50,50,97",
685 "new 2,97,98",
686 "new 2,98,99",
687 "new 2,99,100",
Tianjie Xu107a34f2017-06-29 17:04:21 -0700688 };
Tianjie Xu5450c842017-10-18 13:15:21 -0700689
Tao Baobc4a6d52018-05-23 00:12:21 -0700690 PackageEntries entries{
691 { "new_data.br", std::move(encoded_data) },
Tianjie Xu5450c842017-10-18 13:15:21 -0700692 { "patch_data", "" },
693 { "transfer_list", android::base::Join(transfer_list, '\n') },
694 };
695
Tao Baobc4a6d52018-05-23 00:12:21 -0700696 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu107a34f2017-06-29 17:04:21 -0700697
698 std::string updated_content;
Tao Baobc4a6d52018-05-23 00:12:21 -0700699 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_content));
Tianjie Xu107a34f2017-06-29 17:04:21 -0700700 ASSERT_EQ(brotli_new_data, updated_content);
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700701}
Tianjie Xu284752e2017-12-05 11:04:17 -0800702
703TEST_F(UpdaterTest, last_command_update) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700704 std::string block1(4096, '1');
705 std::string block2(4096, '2');
706 std::string block3(4096, '3');
Tianjie Xu284752e2017-12-05 11:04:17 -0800707 std::string block1_hash = get_sha1(block1);
708 std::string block2_hash = get_sha1(block2);
709 std::string block3_hash = get_sha1(block3);
710
711 // Compose the transfer list to fail the first update.
Tao Baobc4a6d52018-05-23 00:12:21 -0700712 std::vector<std::string> transfer_list_fail{
713 // clang-format off
Tianjie Xu284752e2017-12-05 11:04:17 -0800714 "4",
715 "2",
716 "0",
717 "2",
718 "stash " + block1_hash + " 2,0,1",
719 "move " + block1_hash + " 2,1,2 1 2,0,1",
720 "stash " + block3_hash + " 2,2,3",
Tao Bao91a649a2018-05-21 16:05:56 -0700721 "abort",
Tao Baobc4a6d52018-05-23 00:12:21 -0700722 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -0800723 };
724
725 // Mimic a resumed update with the same transfer commands.
Tao Baobc4a6d52018-05-23 00:12:21 -0700726 std::vector<std::string> transfer_list_continue{
727 // clang-format off
Tianjie Xu284752e2017-12-05 11:04:17 -0800728 "4",
729 "2",
730 "0",
731 "2",
732 "stash " + block1_hash + " 2,0,1",
733 "move " + block1_hash + " 2,1,2 1 2,0,1",
734 "stash " + block3_hash + " 2,2,3",
735 "move " + block1_hash + " 2,2,3 1 2,0,1",
Tao Baobc4a6d52018-05-23 00:12:21 -0700736 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -0800737 };
738
Tao Baobc4a6d52018-05-23 00:12:21 -0700739 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_));
740
741 PackageEntries entries{
Tianjie Xu284752e2017-12-05 11:04:17 -0800742 { "new_data", "" },
743 { "patch_data", "" },
Tao Baobc4a6d52018-05-23 00:12:21 -0700744 { "transfer_list", android::base::Join(transfer_list_fail, '\n') },
Tianjie Xu284752e2017-12-05 11:04:17 -0800745 };
746
Tao Baobc4a6d52018-05-23 00:12:21 -0700747 // "2\nstash " + block3_hash + " 2,2,3"
748 std::string last_command_content = "2\n" + transfer_list_fail[kTransferListHeaderLines + 2];
Tianjie Xu284752e2017-12-05 11:04:17 -0800749
Tao Baobc4a6d52018-05-23 00:12:21 -0700750 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu284752e2017-12-05 11:04:17 -0800751
752 // Expect last_command to contain the last stash command.
Tao Baobc4a6d52018-05-23 00:12:21 -0700753 std::string last_command_actual;
Tao Bao7064aa22018-05-24 21:43:50 -0700754 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
Tao Baobc4a6d52018-05-23 00:12:21 -0700755 EXPECT_EQ(last_command_content, last_command_actual);
756
Tianjie Xu284752e2017-12-05 11:04:17 -0800757 std::string updated_contents;
Tao Baobc4a6d52018-05-23 00:12:21 -0700758 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_contents));
Tianjie Xu284752e2017-12-05 11:04:17 -0800759 ASSERT_EQ(block1 + block1 + block3, updated_contents);
760
Tao Baobc4a6d52018-05-23 00:12:21 -0700761 // "Resume" the update. Expect the first 'move' to be skipped but the second 'move' to be
762 // executed. Note that we intentionally reset the image file.
763 entries["transfer_list"] = android::base::Join(transfer_list_continue, '\n');
764 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_));
765 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu284752e2017-12-05 11:04:17 -0800766
Tao Baobc4a6d52018-05-23 00:12:21 -0700767 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_contents));
768 ASSERT_EQ(block1 + block2 + block1, updated_contents);
Tianjie Xu284752e2017-12-05 11:04:17 -0800769}
770
771TEST_F(UpdaterTest, last_command_update_unresumable) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700772 std::string block1(4096, '1');
773 std::string block2(4096, '2');
Tianjie Xu284752e2017-12-05 11:04:17 -0800774 std::string block1_hash = get_sha1(block1);
775 std::string block2_hash = get_sha1(block2);
776
777 // Construct an unresumable update with source blocks mismatch.
Tao Baobc4a6d52018-05-23 00:12:21 -0700778 std::vector<std::string> transfer_list_unresumable{
779 // clang-format off
780 "4",
781 "2",
782 "0",
783 "2",
784 "stash " + block1_hash + " 2,0,1",
785 "move " + block2_hash + " 2,1,2 1 2,0,1",
786 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -0800787 };
788
Tao Baobc4a6d52018-05-23 00:12:21 -0700789 PackageEntries entries{
Tianjie Xu284752e2017-12-05 11:04:17 -0800790 { "new_data", "" },
791 { "patch_data", "" },
Tao Baobc4a6d52018-05-23 00:12:21 -0700792 { "transfer_list", android::base::Join(transfer_list_unresumable, '\n') },
Tianjie Xu284752e2017-12-05 11:04:17 -0800793 };
794
Tao Baobc4a6d52018-05-23 00:12:21 -0700795 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block1, image_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -0800796
Tao Baobc4a6d52018-05-23 00:12:21 -0700797 std::string last_command_content = "0\n" + transfer_list_unresumable[kTransferListHeaderLines];
Tao Bao7064aa22018-05-24 21:43:50 -0700798 ASSERT_TRUE(android::base::WriteStringToFile(last_command_content, last_command_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -0800799
Tao Baobc4a6d52018-05-23 00:12:21 -0700800 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu284752e2017-12-05 11:04:17 -0800801
Tao Baobc4a6d52018-05-23 00:12:21 -0700802 // The last_command_file will be deleted if the update encounters an unresumable failure later.
Tao Bao7064aa22018-05-24 21:43:50 -0700803 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
Tianjie Xu284752e2017-12-05 11:04:17 -0800804}
805
806TEST_F(UpdaterTest, last_command_verify) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700807 std::string block1(4096, '1');
808 std::string block2(4096, '2');
809 std::string block3(4096, '3');
Tianjie Xu284752e2017-12-05 11:04:17 -0800810 std::string block1_hash = get_sha1(block1);
811 std::string block2_hash = get_sha1(block2);
812 std::string block3_hash = get_sha1(block3);
813
Tao Baobc4a6d52018-05-23 00:12:21 -0700814 std::vector<std::string> transfer_list_verify{
815 // clang-format off
Tianjie Xu284752e2017-12-05 11:04:17 -0800816 "4",
817 "2",
818 "0",
819 "2",
820 "stash " + block1_hash + " 2,0,1",
821 "move " + block1_hash + " 2,0,1 1 2,0,1",
822 "move " + block1_hash + " 2,1,2 1 2,0,1",
823 "stash " + block3_hash + " 2,2,3",
Tao Baobc4a6d52018-05-23 00:12:21 -0700824 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -0800825 };
826
Tao Baobc4a6d52018-05-23 00:12:21 -0700827 PackageEntries entries{
Tianjie Xu284752e2017-12-05 11:04:17 -0800828 { "new_data", "" },
829 { "patch_data", "" },
Tao Baobc4a6d52018-05-23 00:12:21 -0700830 { "transfer_list", android::base::Join(transfer_list_verify, '\n') },
Tianjie Xu284752e2017-12-05 11:04:17 -0800831 };
832
Tao Baobc4a6d52018-05-23 00:12:21 -0700833 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block1 + block3, image_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -0800834
Tao Baobc4a6d52018-05-23 00:12:21 -0700835 // Last command: "move " + block1_hash + " 2,1,2 1 2,0,1"
836 std::string last_command_content = "2\n" + transfer_list_verify[kTransferListHeaderLines + 2];
Tianjie Xu284752e2017-12-05 11:04:17 -0800837
Tao Baobc4a6d52018-05-23 00:12:21 -0700838 // First run: expect the verification to succeed and the last_command_file is intact.
Tao Bao7064aa22018-05-24 21:43:50 -0700839 ASSERT_TRUE(android::base::WriteStringToFile(last_command_content, last_command_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -0800840
Tao Baobc4a6d52018-05-23 00:12:21 -0700841 RunBlockImageUpdate(true, entries, image_file_, "t");
Tianjie Xu284752e2017-12-05 11:04:17 -0800842
Tao Baobc4a6d52018-05-23 00:12:21 -0700843 std::string last_command_actual;
Tao Bao7064aa22018-05-24 21:43:50 -0700844 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
Tao Baobc4a6d52018-05-23 00:12:21 -0700845 EXPECT_EQ(last_command_content, last_command_actual);
Tianjie Xu284752e2017-12-05 11:04:17 -0800846
Tao Baobc4a6d52018-05-23 00:12:21 -0700847 // Second run with a mismatching block image: expect the verification to succeed but
848 // last_command_file to be deleted; because the target blocks in the last command don't have the
849 // expected contents for the second move command.
850 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_));
851 RunBlockImageUpdate(true, entries, image_file_, "t");
Tao Bao7064aa22018-05-24 21:43:50 -0700852 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
Tianjie Xu284752e2017-12-05 11:04:17 -0800853}
Tao Baoc0299ed2018-05-24 00:16:35 -0700854
855class ResumableUpdaterTest : public testing::TestWithParam<size_t> {
856 protected:
857 void SetUp() override {
858 RegisterBuiltins();
859 RegisterInstallFunctions();
860 RegisterBlockImageFunctions();
861
862 Paths::Get().set_cache_temp_source(temp_saved_source_.path);
863 Paths::Get().set_last_command_file(temp_last_command_.path);
864 Paths::Get().set_stash_directory_base(temp_stash_base_.path);
865
Tao Bao91a649a2018-05-21 16:05:56 -0700866 // Enable a special command "abort" to simulate interruption.
867 Command::abort_allowed_ = true;
868
Tao Baoc0299ed2018-05-24 00:16:35 -0700869 index_ = GetParam();
870 image_file_ = image_temp_file_.path;
871 last_command_file_ = temp_last_command_.path;
872 }
873
874 void TearDown() override {
875 // Clean up the last_command_file if any.
876 ASSERT_TRUE(android::base::RemoveFileIfExists(last_command_file_));
877
878 // Clear partition updated marker if any.
879 std::string updated_marker{ temp_stash_base_.path };
880 updated_marker += "/" + get_sha1(image_temp_file_.path) + ".UPDATED";
881 ASSERT_TRUE(android::base::RemoveFileIfExists(updated_marker));
882 }
883
884 TemporaryFile temp_saved_source_;
885 TemporaryDir temp_stash_base_;
886 std::string last_command_file_;
887 std::string image_file_;
888 size_t index_;
889
890 private:
891 TemporaryFile temp_last_command_;
892 TemporaryFile image_temp_file_;
893};
894
895static std::string g_source_image;
896static std::string g_target_image;
897static PackageEntries g_entries;
898
899static std::vector<std::string> GenerateTransferList() {
900 std::string a(4096, 'a');
901 std::string b(4096, 'b');
902 std::string c(4096, 'c');
903 std::string d(4096, 'd');
904 std::string e(4096, 'e');
905 std::string f(4096, 'f');
906 std::string g(4096, 'g');
907 std::string h(4096, 'h');
908 std::string i(4096, 'i');
909 std::string zero(4096, '\0');
910
911 std::string a_hash = get_sha1(a);
912 std::string b_hash = get_sha1(b);
913 std::string c_hash = get_sha1(c);
914 std::string e_hash = get_sha1(e);
915
916 auto loc = [](const std::string& range_text) {
917 std::vector<std::string> pieces = android::base::Split(range_text, "-");
918 size_t left;
919 size_t right;
920 if (pieces.size() == 1) {
921 CHECK(android::base::ParseUint(pieces[0], &left));
922 right = left + 1;
923 } else {
924 CHECK_EQ(2u, pieces.size());
925 CHECK(android::base::ParseUint(pieces[0], &left));
926 CHECK(android::base::ParseUint(pieces[1], &right));
927 right++;
928 }
929 return android::base::StringPrintf("2,%zu,%zu", left, right);
930 };
931
932 // patch 1: "b d c" -> "g"
933 TemporaryFile patch_file_bdc_g;
934 std::string bdc = b + d + c;
935 std::string bdc_hash = get_sha1(bdc);
936 std::string g_hash = get_sha1(g);
937 CHECK_EQ(0, bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(bdc.data()), bdc.size(),
938 reinterpret_cast<const uint8_t*>(g.data()), g.size(),
939 patch_file_bdc_g.path, nullptr));
940 std::string patch_bdc_g;
941 CHECK(android::base::ReadFileToString(patch_file_bdc_g.path, &patch_bdc_g));
942
943 // patch 2: "a b c d" -> "d c b"
944 TemporaryFile patch_file_abcd_dcb;
945 std::string abcd = a + b + c + d;
946 std::string abcd_hash = get_sha1(abcd);
947 std::string dcb = d + c + b;
948 std::string dcb_hash = get_sha1(dcb);
949 CHECK_EQ(0, bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(abcd.data()), abcd.size(),
950 reinterpret_cast<const uint8_t*>(dcb.data()), dcb.size(),
951 patch_file_abcd_dcb.path, nullptr));
952 std::string patch_abcd_dcb;
953 CHECK(android::base::ReadFileToString(patch_file_abcd_dcb.path, &patch_abcd_dcb));
954
955 std::vector<std::string> transfer_list{
956 "4",
957 "10", // total blocks written
958 "2", // maximum stash entries
959 "2", // maximum number of stashed blocks
960
961 // a b c d e a b c d e
962 "stash " + b_hash + " " + loc("1"),
963 // a b c d e a b c d e [b(1)]
964 "stash " + c_hash + " " + loc("2"),
965 // a b c d e a b c d e [b(1)][c(2)]
966 "new " + loc("1-2"),
967 // a i h d e a b c d e [b(1)][c(2)]
968 "zero " + loc("0"),
969 // 0 i h d e a b c d e [b(1)][c(2)]
970
971 // bsdiff "b d c" (from stash, 3, stash) to get g(3)
972 android::base::StringPrintf(
973 "bsdiff 0 %zu %s %s %s 3 %s %s %s:%s %s:%s",
974 patch_bdc_g.size(), // patch start (0), patch length
975 bdc_hash.c_str(), // source hash
976 g_hash.c_str(), // target hash
977 loc("3").c_str(), // target range
978 loc("3").c_str(), loc("1").c_str(), // load "d" from block 3, into buffer at offset 1
979 b_hash.c_str(), loc("0").c_str(), // load "b" from stash, into buffer at offset 0
980 c_hash.c_str(), loc("2").c_str()), // load "c" from stash, into buffer at offset 2
981
982 // 0 i h g e a b c d e [b(1)][c(2)]
983 "free " + b_hash,
984 // 0 i h g e a b c d e [c(2)]
985 "free " + a_hash,
986 // 0 i h g e a b c d e
987 "stash " + a_hash + " " + loc("5"),
988 // 0 i h g e a b c d e [a(5)]
989 "move " + e_hash + " " + loc("5") + " 1 " + loc("4"),
990 // 0 i h g e e b c d e [a(5)]
991
992 // bsdiff "a b c d" (from stash, 6-8) to "d c b" (6-8)
993 android::base::StringPrintf( //
994 "bsdiff %zu %zu %s %s %s 4 %s %s %s:%s",
995 patch_bdc_g.size(), // patch start
996 patch_bdc_g.size() + patch_abcd_dcb.size(), // patch length
997 abcd_hash.c_str(), // source hash
998 dcb_hash.c_str(), // target hash
999 loc("6-8").c_str(), // target range
1000 loc("6-8").c_str(), // load "b c d" from blocks 6-8
1001 loc("1-3").c_str(), // into buffer at offset 1-3
1002 a_hash.c_str(), // load "a" from stash
1003 loc("0").c_str()), // into buffer at offset 0
1004
1005 // 0 i h g e e d c b e [a(5)]
1006 "new " + loc("4"),
1007 // 0 i h g f e d c b e [a(5)]
1008 "move " + a_hash + " " + loc("9") + " 1 - " + a_hash + ":" + loc("0"),
1009 // 0 i h g f e d c b a [a(5)]
1010 "free " + a_hash,
1011 // 0 i h g f e d c b a
1012 };
1013
1014 std::string new_data = i + h + f;
1015 std::string patch_data = patch_bdc_g + patch_abcd_dcb;
1016
1017 g_entries = {
1018 { "new_data", new_data },
1019 { "patch_data", patch_data },
1020 };
1021 g_source_image = a + b + c + d + e + a + b + c + d + e;
1022 g_target_image = zero + i + h + g + f + e + d + c + b + a;
1023
1024 return transfer_list;
1025}
1026
1027static const std::vector<std::string> g_transfer_list = GenerateTransferList();
1028
1029INSTANTIATE_TEST_CASE_P(InterruptAfterEachCommand, ResumableUpdaterTest,
1030 ::testing::Range(static_cast<size_t>(0),
1031 g_transfer_list.size() - kTransferListHeaderLines));
1032
1033TEST_P(ResumableUpdaterTest, InterruptVerifyResume) {
1034 ASSERT_TRUE(android::base::WriteStringToFile(g_source_image, image_file_));
1035
1036 LOG(INFO) << "Interrupting at line " << index_ << " ("
1037 << g_transfer_list[kTransferListHeaderLines + index_] << ")";
1038
1039 std::vector<std::string> transfer_list_copy{ g_transfer_list };
Tao Bao91a649a2018-05-21 16:05:56 -07001040 transfer_list_copy[kTransferListHeaderLines + index_] = "abort";
Tao Baoc0299ed2018-05-24 00:16:35 -07001041
1042 g_entries["transfer_list"] = android::base::Join(transfer_list_copy, '\n');
1043
1044 // Run update that's expected to fail.
1045 RunBlockImageUpdate(false, g_entries, image_file_, "");
1046
1047 std::string last_command_expected;
1048
1049 // Assert the last_command_file.
1050 if (index_ == 0) {
1051 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
1052 } else {
1053 last_command_expected =
1054 std::to_string(index_ - 1) + "\n" + g_transfer_list[kTransferListHeaderLines + index_ - 1];
1055 std::string last_command_actual;
1056 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
1057 ASSERT_EQ(last_command_expected, last_command_actual);
1058 }
1059
1060 g_entries["transfer_list"] = android::base::Join(g_transfer_list, '\n');
1061
1062 // Resume the interrupted update, by doing verification first.
1063 RunBlockImageUpdate(true, g_entries, image_file_, "t");
1064
1065 // last_command_file should remain intact.
1066 if (index_ == 0) {
1067 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
1068 } else {
1069 std::string last_command_actual;
1070 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
1071 ASSERT_EQ(last_command_expected, last_command_actual);
1072 }
1073
1074 // Resume the update.
1075 RunBlockImageUpdate(false, g_entries, image_file_, "t");
1076
1077 // last_command_file should be gone after successful update.
1078 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
1079
1080 std::string updated_image_actual;
1081 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_image_actual));
1082 ASSERT_EQ(g_target_image, updated_image_actual);
1083}