blob: 0b6b96f7c4b4c7f977aa795bbab631d00b9d3f16 [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 {
Tao Bao0b58e9a2018-07-06 15:01:05 -070075 ASSERT_TRUE(status) << "Evaluate() finished with error message: " << state.errmsg;
Tao Baoef0eb3b2016-11-14 21:29:52 -080076 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 Bao0b58e9a2018-07-06 15:01:05 -0700141static Value* BlobToString(const char* name, State* state,
142 const std::vector<std::unique_ptr<Expr>>& argv) {
143 if (argv.size() != 1) {
144 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
145 }
146
147 std::vector<std::unique_ptr<Value>> args;
148 if (!ReadValueArgs(state, argv, &args)) {
149 return nullptr;
150 }
151
152 if (args[0]->type != VAL_BLOB) {
153 return ErrorAbort(state, kArgsParsingFailure, "%s() expects a BLOB argument", name);
154 }
155
156 args[0]->type = VAL_STRING;
157 return args[0].release();
158}
159
Tao Bao0c7839a2016-10-10 15:48:37 -0700160class UpdaterTest : public ::testing::Test {
Tianjie Xu56ebe622017-03-16 00:48:21 -0700161 protected:
Tao Baobc4a6d52018-05-23 00:12:21 -0700162 void SetUp() override {
Tianjie Xu56ebe622017-03-16 00:48:21 -0700163 RegisterBuiltins();
164 RegisterInstallFunctions();
165 RegisterBlockImageFunctions();
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800166
Tao Bao0b58e9a2018-07-06 15:01:05 -0700167 RegisterFunction("blob_to_string", BlobToString);
168
Tao Bao7064aa22018-05-24 21:43:50 -0700169 // Each test is run in a separate process (isolated mode). Shared temporary files won't cause
170 // conflicts.
Tao Bao641fa972018-04-25 18:59:40 -0700171 Paths::Get().set_cache_temp_source(temp_saved_source_.path);
Tao Bao7064aa22018-05-24 21:43:50 -0700172 Paths::Get().set_last_command_file(temp_last_command_.path);
Tao Bao641fa972018-04-25 18:59:40 -0700173 Paths::Get().set_stash_directory_base(temp_stash_base_.path);
Tao Baobc4a6d52018-05-23 00:12:21 -0700174
Tao Bao91a649a2018-05-21 16:05:56 -0700175 // Enable a special command "abort" to simulate interruption.
176 Command::abort_allowed_ = true;
177
Tao Bao7064aa22018-05-24 21:43:50 -0700178 last_command_file_ = temp_last_command_.path;
Tao Baobc4a6d52018-05-23 00:12:21 -0700179 image_file_ = image_temp_file_.path;
180 }
181
182 void TearDown() override {
Tao Bao7064aa22018-05-24 21:43:50 -0700183 // Clean up the last_command_file if any.
184 ASSERT_TRUE(android::base::RemoveFileIfExists(last_command_file_));
185
Tao Baobc4a6d52018-05-23 00:12:21 -0700186 // Clear partition updated marker if any.
187 std::string updated_marker{ temp_stash_base_.path };
188 updated_marker += "/" + get_sha1(image_temp_file_.path) + ".UPDATED";
189 ASSERT_TRUE(android::base::RemoveFileIfExists(updated_marker));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700190 }
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800191
192 TemporaryFile temp_saved_source_;
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800193 TemporaryDir temp_stash_base_;
Tao Bao7064aa22018-05-24 21:43:50 -0700194 std::string last_command_file_;
Tao Baobc4a6d52018-05-23 00:12:21 -0700195 std::string image_file_;
196
197 private:
Tao Bao7064aa22018-05-24 21:43:50 -0700198 TemporaryFile temp_last_command_;
Tao Baobc4a6d52018-05-23 00:12:21 -0700199 TemporaryFile image_temp_file_;
Tao Bao0c7839a2016-10-10 15:48:37 -0700200};
201
202TEST_F(UpdaterTest, getprop) {
203 expect(android::base::GetProperty("ro.product.device", "").c_str(),
204 "getprop(\"ro.product.device\")",
Tao Bao361342c2016-02-08 11:15:50 -0800205 kNoCause);
Tao Bao0c7839a2016-10-10 15:48:37 -0700206
207 expect(android::base::GetProperty("ro.build.fingerprint", "").c_str(),
208 "getprop(\"ro.build.fingerprint\")",
Tao Bao361342c2016-02-08 11:15:50 -0800209 kNoCause);
Tao Bao0c7839a2016-10-10 15:48:37 -0700210
211 // getprop() accepts only one parameter.
Tao Bao361342c2016-02-08 11:15:50 -0800212 expect(nullptr, "getprop()", kArgsParsingFailure);
213 expect(nullptr, "getprop(\"arg1\", \"arg2\")", kArgsParsingFailure);
214}
215
Tao Baodb56eb02017-03-23 06:34:20 -0700216TEST_F(UpdaterTest, apply_patch_check) {
217 // Zero-argument is not valid.
218 expect(nullptr, "apply_patch_check()", kArgsParsingFailure);
219
220 // File not found.
221 expect("", "apply_patch_check(\"/doesntexist\")", kNoCause);
222
223 std::string src_file = from_testdata_base("old.file");
224 std::string src_content;
225 ASSERT_TRUE(android::base::ReadFileToString(src_file, &src_content));
226 size_t src_size = src_content.size();
227 std::string src_hash = get_sha1(src_content);
228
229 // One-argument with EMMC:file:size:sha1 should pass the check.
230 std::string filename = android::base::Join(
231 std::vector<std::string>{ "EMMC", src_file, std::to_string(src_size), src_hash }, ":");
232 std::string cmd = "apply_patch_check(\"" + filename + "\")";
233 expect("t", cmd.c_str(), kNoCause);
234
235 // EMMC:file:(size-1):sha1:(size+1):sha1 should fail the check.
236 std::string filename_bad = android::base::Join(
237 std::vector<std::string>{ "EMMC", src_file, std::to_string(src_size - 1), src_hash,
238 std::to_string(src_size + 1), src_hash },
239 ":");
240 cmd = "apply_patch_check(\"" + filename_bad + "\")";
241 expect("", cmd.c_str(), kNoCause);
242
243 // EMMC:file:(size-1):sha1:size:sha1:(size+1):sha1 should pass the check.
244 filename_bad =
245 android::base::Join(std::vector<std::string>{ "EMMC", src_file, std::to_string(src_size - 1),
246 src_hash, std::to_string(src_size), src_hash,
247 std::to_string(src_size + 1), src_hash },
248 ":");
249 cmd = "apply_patch_check(\"" + filename_bad + "\")";
250 expect("t", cmd.c_str(), kNoCause);
251
252 // Multiple arguments.
253 cmd = "apply_patch_check(\"" + filename + "\", \"wrong_sha1\", \"wrong_sha2\")";
254 expect("", cmd.c_str(), kNoCause);
255
256 cmd = "apply_patch_check(\"" + filename + "\", \"wrong_sha1\", \"" + src_hash +
257 "\", \"wrong_sha2\")";
258 expect("t", cmd.c_str(), kNoCause);
259
260 cmd = "apply_patch_check(\"" + filename_bad + "\", \"wrong_sha1\", \"" + src_hash +
261 "\", \"wrong_sha2\")";
262 expect("t", cmd.c_str(), kNoCause);
263}
264
Tao Bao51d516e2016-11-03 14:49:01 -0700265TEST_F(UpdaterTest, file_getprop) {
266 // file_getprop() expects two arguments.
267 expect(nullptr, "file_getprop()", kArgsParsingFailure);
268 expect(nullptr, "file_getprop(\"arg1\")", kArgsParsingFailure);
269 expect(nullptr, "file_getprop(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
270
271 // File doesn't exist.
272 expect(nullptr, "file_getprop(\"/doesntexist\", \"key1\")", kFileGetPropFailure);
273
274 // Reject too large files (current limit = 65536).
275 TemporaryFile temp_file1;
276 std::string buffer(65540, '\0');
277 ASSERT_TRUE(android::base::WriteStringToFile(buffer, temp_file1.path));
278
279 // Read some keys.
280 TemporaryFile temp_file2;
281 std::string content("ro.product.name=tardis\n"
282 "# comment\n\n\n"
283 "ro.product.model\n"
284 "ro.product.board = magic \n");
285 ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file2.path));
286
287 std::string script1("file_getprop(\"" + std::string(temp_file2.path) +
288 "\", \"ro.product.name\")");
289 expect("tardis", script1.c_str(), kNoCause);
290
291 std::string script2("file_getprop(\"" + std::string(temp_file2.path) +
292 "\", \"ro.product.board\")");
293 expect("magic", script2.c_str(), kNoCause);
294
295 // No match.
296 std::string script3("file_getprop(\"" + std::string(temp_file2.path) +
297 "\", \"ro.product.wrong\")");
298 expect("", script3.c_str(), kNoCause);
299
300 std::string script4("file_getprop(\"" + std::string(temp_file2.path) +
301 "\", \"ro.product.name=\")");
302 expect("", script4.c_str(), kNoCause);
303
304 std::string script5("file_getprop(\"" + std::string(temp_file2.path) +
305 "\", \"ro.product.nam\")");
306 expect("", script5.c_str(), kNoCause);
307
308 std::string script6("file_getprop(\"" + std::string(temp_file2.path) +
309 "\", \"ro.product.model\")");
310 expect("", script6.c_str(), kNoCause);
311}
Tao Bao0831d0b2016-11-03 23:25:04 -0700312
Tao Baoef0eb3b2016-11-14 21:29:52 -0800313// TODO: Test extracting to block device.
314TEST_F(UpdaterTest, package_extract_file) {
315 // package_extract_file expects 1 or 2 arguments.
316 expect(nullptr, "package_extract_file()", kArgsParsingFailure);
317 expect(nullptr, "package_extract_file(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
318
319 std::string zip_path = from_testdata_base("ziptest_valid.zip");
320 ZipArchiveHandle handle;
321 ASSERT_EQ(0, OpenArchive(zip_path.c_str(), &handle));
322
323 // Need to set up the ziphandle.
324 UpdaterInfo updater_info;
325 updater_info.package_zip = handle;
326
327 // Two-argument version.
328 TemporaryFile temp_file1;
329 std::string script("package_extract_file(\"a.txt\", \"" + std::string(temp_file1.path) + "\")");
330 expect("t", script.c_str(), kNoCause, &updater_info);
331
332 // Verify the extracted entry.
333 std::string data;
334 ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data));
335 ASSERT_EQ(kATxtContents, data);
336
337 // Now extract another entry to the same location, which should overwrite.
338 script = "package_extract_file(\"b.txt\", \"" + std::string(temp_file1.path) + "\")";
339 expect("t", script.c_str(), kNoCause, &updater_info);
340
341 ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data));
342 ASSERT_EQ(kBTxtContents, data);
343
344 // Missing zip entry. The two-argument version doesn't abort.
345 script = "package_extract_file(\"doesntexist\", \"" + std::string(temp_file1.path) + "\")";
346 expect("", script.c_str(), kNoCause, &updater_info);
347
348 // Extract to /dev/full should fail.
349 script = "package_extract_file(\"a.txt\", \"/dev/full\")";
350 expect("", script.c_str(), kNoCause, &updater_info);
351
Tao Bao0b58e9a2018-07-06 15:01:05 -0700352 // One-argument version. package_extract_file() gives a VAL_BLOB, which needs to be converted to
353 // VAL_STRING for equality test.
354 script = "blob_to_string(package_extract_file(\"a.txt\")) == \"" + kATxtContents + "\"";
355 expect("t", script.c_str(), kNoCause, &updater_info);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800356
Tao Bao0b58e9a2018-07-06 15:01:05 -0700357 script = "blob_to_string(package_extract_file(\"b.txt\")) == \"" + kBTxtContents + "\"";
358 expect("t", script.c_str(), kNoCause, &updater_info);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800359
360 // Missing entry. The one-argument version aborts the evaluation.
361 script = "package_extract_file(\"doesntexist\")";
362 expect(nullptr, script.c_str(), kPackageExtractFileFailure, &updater_info);
363
364 CloseArchive(handle);
365}
Tao Baod0f30882016-11-03 23:52:01 -0700366
367TEST_F(UpdaterTest, write_value) {
368 // write_value() expects two arguments.
369 expect(nullptr, "write_value()", kArgsParsingFailure);
370 expect(nullptr, "write_value(\"arg1\")", kArgsParsingFailure);
371 expect(nullptr, "write_value(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
372
373 // filename cannot be empty.
374 expect(nullptr, "write_value(\"value\", \"\")", kArgsParsingFailure);
375
376 // Write some value to file.
377 TemporaryFile temp_file;
378 std::string value = "magicvalue";
379 std::string script("write_value(\"" + value + "\", \"" + std::string(temp_file.path) + "\")");
380 expect("t", script.c_str(), kNoCause);
381
382 // Verify the content.
383 std::string content;
384 ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content));
385 ASSERT_EQ(value, content);
386
387 // Allow writing empty string.
388 script = "write_value(\"\", \"" + std::string(temp_file.path) + "\")";
389 expect("t", script.c_str(), kNoCause);
390
391 // Verify the content.
392 ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content));
393 ASSERT_EQ("", content);
394
395 // It should fail gracefully when write fails.
396 script = "write_value(\"value\", \"/proc/0/file1\")";
397 expect("", script.c_str(), kNoCause);
398}
Tao Baobedf5fc2016-11-18 12:01:26 -0800399
400TEST_F(UpdaterTest, get_stage) {
401 // get_stage() expects one argument.
402 expect(nullptr, "get_stage()", kArgsParsingFailure);
403 expect(nullptr, "get_stage(\"arg1\", \"arg2\")", kArgsParsingFailure);
404 expect(nullptr, "get_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
405
406 // Set up a local file as BCB.
407 TemporaryFile tf;
408 std::string temp_file(tf.path);
409 bootloader_message boot;
410 strlcpy(boot.stage, "2/3", sizeof(boot.stage));
411 std::string err;
412 ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err));
413
414 // Can read the stage value.
415 std::string script("get_stage(\"" + temp_file + "\")");
416 expect("2/3", script.c_str(), kNoCause);
417
418 // Bad BCB path.
419 script = "get_stage(\"doesntexist\")";
420 expect("", script.c_str(), kNoCause);
421}
422
423TEST_F(UpdaterTest, set_stage) {
424 // set_stage() expects two arguments.
425 expect(nullptr, "set_stage()", kArgsParsingFailure);
426 expect(nullptr, "set_stage(\"arg1\")", kArgsParsingFailure);
427 expect(nullptr, "set_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
428
429 // Set up a local file as BCB.
430 TemporaryFile tf;
431 std::string temp_file(tf.path);
432 bootloader_message boot;
433 strlcpy(boot.command, "command", sizeof(boot.command));
434 strlcpy(boot.stage, "2/3", sizeof(boot.stage));
435 std::string err;
436 ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err));
437
438 // Write with set_stage().
439 std::string script("set_stage(\"" + temp_file + "\", \"1/3\")");
440 expect(tf.path, script.c_str(), kNoCause);
441
442 // Verify.
443 bootloader_message boot_verify;
444 ASSERT_TRUE(read_bootloader_message_from(&boot_verify, temp_file, &err));
445
446 // Stage should be updated, with command part untouched.
447 ASSERT_STREQ("1/3", boot_verify.stage);
448 ASSERT_STREQ(boot.command, boot_verify.command);
449
450 // Bad BCB path.
451 script = "set_stage(\"doesntexist\", \"1/3\")";
452 expect("", script.c_str(), kNoCause);
453
454 script = "set_stage(\"/dev/full\", \"1/3\")";
455 expect("", script.c_str(), kNoCause);
456}
Tao Bao9aa7ab52017-01-05 17:27:19 -0800457
458TEST_F(UpdaterTest, set_progress) {
459 // set_progress() expects one argument.
460 expect(nullptr, "set_progress()", kArgsParsingFailure);
461 expect(nullptr, "set_progress(\"arg1\", \"arg2\")", kArgsParsingFailure);
462
463 // Invalid progress argument.
464 expect(nullptr, "set_progress(\"arg1\")", kArgsParsingFailure);
465 expect(nullptr, "set_progress(\"3x+5\")", kArgsParsingFailure);
466 expect(nullptr, "set_progress(\".3.5\")", kArgsParsingFailure);
467
468 TemporaryFile tf;
469 UpdaterInfo updater_info;
Tianjie Xu79327ac2017-09-08 17:09:10 -0700470 updater_info.cmd_pipe = fdopen(tf.release(), "w");
Tao Bao9aa7ab52017-01-05 17:27:19 -0800471 expect(".52", "set_progress(\".52\")", kNoCause, &updater_info);
472 fflush(updater_info.cmd_pipe);
473
474 std::string cmd;
475 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd));
476 ASSERT_EQ(android::base::StringPrintf("set_progress %f\n", .52), cmd);
477 // recovery-updater protocol expects 2 tokens ("set_progress <frac>").
478 ASSERT_EQ(2U, android::base::Split(cmd, " ").size());
Tianjie Xu79327ac2017-09-08 17:09:10 -0700479 ASSERT_EQ(0, fclose(updater_info.cmd_pipe));
Tao Bao9aa7ab52017-01-05 17:27:19 -0800480}
481
482TEST_F(UpdaterTest, show_progress) {
483 // show_progress() expects two arguments.
484 expect(nullptr, "show_progress()", kArgsParsingFailure);
485 expect(nullptr, "show_progress(\"arg1\")", kArgsParsingFailure);
486 expect(nullptr, "show_progress(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
487
488 // Invalid progress arguments.
489 expect(nullptr, "show_progress(\"arg1\", \"arg2\")", kArgsParsingFailure);
490 expect(nullptr, "show_progress(\"3x+5\", \"10\")", kArgsParsingFailure);
491 expect(nullptr, "show_progress(\".3\", \"5a\")", kArgsParsingFailure);
492
493 TemporaryFile tf;
494 UpdaterInfo updater_info;
Tianjie Xu79327ac2017-09-08 17:09:10 -0700495 updater_info.cmd_pipe = fdopen(tf.release(), "w");
Tao Bao9aa7ab52017-01-05 17:27:19 -0800496 expect(".52", "show_progress(\".52\", \"10\")", kNoCause, &updater_info);
497 fflush(updater_info.cmd_pipe);
498
499 std::string cmd;
500 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd));
501 ASSERT_EQ(android::base::StringPrintf("progress %f %d\n", .52, 10), cmd);
502 // recovery-updater protocol expects 3 tokens ("progress <frac> <secs>").
503 ASSERT_EQ(3U, android::base::Split(cmd, " ").size());
Tianjie Xu79327ac2017-09-08 17:09:10 -0700504 ASSERT_EQ(0, fclose(updater_info.cmd_pipe));
Tao Bao9aa7ab52017-01-05 17:27:19 -0800505}
Tianjie Xu56ebe622017-03-16 00:48:21 -0700506
Tao Baoffede3e2018-06-07 09:56:19 -0700507TEST_F(UpdaterTest, block_image_update_parsing_error) {
508 std::vector<std::string> transfer_list{
509 // clang-format off
510 "4",
511 "2",
512 "0",
513 // clang-format on
514 };
515
516 PackageEntries entries{
517 { "new_data", "" },
518 { "patch_data", "" },
519 { "transfer_list", android::base::Join(transfer_list, '\n') },
520 };
521
522 RunBlockImageUpdate(false, entries, image_file_, "", kArgsParsingFailure);
523}
524
Tianjie Xu5450c842017-10-18 13:15:21 -0700525TEST_F(UpdaterTest, block_image_update_patch_data) {
Tianjie Xu56ebe622017-03-16 00:48:21 -0700526 std::string src_content = std::string(4096, 'a') + std::string(4096, 'c');
527 std::string tgt_content = std::string(4096, 'b') + std::string(4096, 'd');
Tianjie Xu5450c842017-10-18 13:15:21 -0700528
529 // Generate the patch data.
Tianjie Xu56ebe622017-03-16 00:48:21 -0700530 TemporaryFile patch_file;
Tao Baobc4a6d52018-05-23 00:12:21 -0700531 ASSERT_EQ(0,
532 bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(src_content.data()), src_content.size(),
533 reinterpret_cast<const uint8_t*>(tgt_content.data()), tgt_content.size(),
534 patch_file.path, nullptr));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700535 std::string patch_content;
536 ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch_content));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700537
Tianjie Xu5450c842017-10-18 13:15:21 -0700538 // Create the transfer list that contains a bsdiff.
Tianjie Xu56ebe622017-03-16 00:48:21 -0700539 std::string src_hash = get_sha1(src_content);
540 std::string tgt_hash = get_sha1(tgt_content);
Tao Baobc4a6d52018-05-23 00:12:21 -0700541 std::vector<std::string> transfer_list{
542 // clang-format off
Tianjie Xu56ebe622017-03-16 00:48:21 -0700543 "4",
544 "2",
545 "0",
546 "2",
547 "stash " + src_hash + " 2,0,2",
548 android::base::StringPrintf("bsdiff 0 %zu %s %s 2,0,2 2 - %s:2,0,2", patch_content.size(),
549 src_hash.c_str(), tgt_hash.c_str(), src_hash.c_str()),
550 "free " + src_hash,
Tao Baobc4a6d52018-05-23 00:12:21 -0700551 // clang-format on
Tianjie Xu56ebe622017-03-16 00:48:21 -0700552 };
Tianjie Xu56ebe622017-03-16 00:48:21 -0700553
Tao Baobc4a6d52018-05-23 00:12:21 -0700554 PackageEntries entries{
Tianjie Xu5450c842017-10-18 13:15:21 -0700555 { "new_data", "" },
556 { "patch_data", patch_content },
557 { "transfer_list", android::base::Join(transfer_list, '\n') },
Tianjie Xu56ebe622017-03-16 00:48:21 -0700558 };
Tianjie Xu5450c842017-10-18 13:15:21 -0700559
Tao Baobc4a6d52018-05-23 00:12:21 -0700560 ASSERT_TRUE(android::base::WriteStringToFile(src_content, image_file_));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700561
Tao Baobc4a6d52018-05-23 00:12:21 -0700562 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu56ebe622017-03-16 00:48:21 -0700563
Tianjie Xu56ebe622017-03-16 00:48:21 -0700564 // The update_file should be patched correctly.
565 std::string updated_content;
Tao Baobc4a6d52018-05-23 00:12:21 -0700566 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_content));
567 ASSERT_EQ(tgt_content, updated_content);
Tianjie Xu5450c842017-10-18 13:15:21 -0700568}
569
570TEST_F(UpdaterTest, block_image_update_fail) {
571 std::string src_content(4096 * 2, 'e');
572 std::string src_hash = get_sha1(src_content);
573 // Stash and free some blocks, then fail the update intentionally.
Tao Baobc4a6d52018-05-23 00:12:21 -0700574 std::vector<std::string> transfer_list{
575 // clang-format off
576 "4",
577 "2",
578 "0",
579 "2",
580 "stash " + src_hash + " 2,0,2",
581 "free " + src_hash,
Tao Bao91a649a2018-05-21 16:05:56 -0700582 "abort",
Tao Baobc4a6d52018-05-23 00:12:21 -0700583 // clang-format on
Tianjie Xu5450c842017-10-18 13:15:21 -0700584 };
585
586 // Add a new data of 10 bytes to test the deadlock.
Tao Baobc4a6d52018-05-23 00:12:21 -0700587 PackageEntries entries{
Tianjie Xu5450c842017-10-18 13:15:21 -0700588 { "new_data", std::string(10, 0) },
589 { "patch_data", "" },
590 { "transfer_list", android::base::Join(transfer_list, '\n') },
591 };
592
Tao Baobc4a6d52018-05-23 00:12:21 -0700593 ASSERT_TRUE(android::base::WriteStringToFile(src_content, image_file_));
Tianjie Xu5450c842017-10-18 13:15:21 -0700594
Tao Baobc4a6d52018-05-23 00:12:21 -0700595 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu5450c842017-10-18 13:15:21 -0700596
Tianjie Xu56ebe622017-03-16 00:48:21 -0700597 // Updater generates the stash name based on the input file name.
Tao Baobc4a6d52018-05-23 00:12:21 -0700598 std::string name_digest = get_sha1(image_file_);
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800599 std::string stash_base = std::string(temp_stash_base_.path) + "/" + name_digest;
Tianjie Xu56ebe622017-03-16 00:48:21 -0700600 ASSERT_EQ(0, access(stash_base.c_str(), F_OK));
Tao Baobc4a6d52018-05-23 00:12:21 -0700601 // Expect the stashed blocks to be freed.
Tianjie Xu5450c842017-10-18 13:15:21 -0700602 ASSERT_EQ(-1, access((stash_base + src_hash).c_str(), F_OK));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700603 ASSERT_EQ(0, rmdir(stash_base.c_str()));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700604}
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700605
Tianjie Xu5450c842017-10-18 13:15:21 -0700606TEST_F(UpdaterTest, new_data_over_write) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700607 std::vector<std::string> transfer_list{
608 // clang-format off
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700609 "4",
610 "1",
611 "0",
612 "0",
613 "new 2,0,1",
Tao Baobc4a6d52018-05-23 00:12:21 -0700614 // clang-format on
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700615 };
Tianjie Xu5450c842017-10-18 13:15:21 -0700616
Tao Baobc4a6d52018-05-23 00:12:21 -0700617 // Write 4096 + 100 bytes of new data.
618 PackageEntries entries{
619 { "new_data", std::string(4196, 0) },
Tianjie Xu5450c842017-10-18 13:15:21 -0700620 { "patch_data", "" },
621 { "transfer_list", android::base::Join(transfer_list, '\n') },
622 };
623
Tao Baobc4a6d52018-05-23 00:12:21 -0700624 RunBlockImageUpdate(false, entries, image_file_, "t");
625}
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700626
Tao Baobc4a6d52018-05-23 00:12:21 -0700627TEST_F(UpdaterTest, new_data_short_write) {
628 std::vector<std::string> transfer_list{
629 // clang-format off
630 "4",
631 "1",
632 "0",
633 "0",
634 "new 2,0,1",
635 // clang-format on
636 };
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700637
Tao Baobc4a6d52018-05-23 00:12:21 -0700638 PackageEntries entries{
639 { "patch_data", "" },
640 { "transfer_list", android::base::Join(transfer_list, '\n') },
641 };
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700642
643 // Updater should report the failure gracefully rather than stuck in deadlock.
Tao Baobc4a6d52018-05-23 00:12:21 -0700644 entries["new_data"] = "";
645 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700646
Tao Baobc4a6d52018-05-23 00:12:21 -0700647 entries["new_data"] = std::string(10, 'a');
648 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700649
650 // Expect to write 1 block of new data successfully.
Tao Baobc4a6d52018-05-23 00:12:21 -0700651 entries["new_data"] = std::string(4096, 'a');
652 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu107a34f2017-06-29 17:04:21 -0700653}
654
655TEST_F(UpdaterTest, brotli_new_data) {
Tianjie Xu107a34f2017-06-29 17:04:21 -0700656 auto generator = []() { return rand() % 128; };
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700657 // Generate 100 blocks of random data.
Tianjie Xu107a34f2017-06-29 17:04:21 -0700658 std::string brotli_new_data;
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700659 brotli_new_data.reserve(4096 * 100);
660 generate_n(back_inserter(brotli_new_data), 4096 * 100, generator);
Tianjie Xu107a34f2017-06-29 17:04:21 -0700661
662 size_t encoded_size = BrotliEncoderMaxCompressedSize(brotli_new_data.size());
Tianjie Xu5450c842017-10-18 13:15:21 -0700663 std::string encoded_data(encoded_size, 0);
Tianjie Xu107a34f2017-06-29 17:04:21 -0700664 ASSERT_TRUE(BrotliEncoderCompress(
665 BROTLI_DEFAULT_QUALITY, BROTLI_DEFAULT_WINDOW, BROTLI_DEFAULT_MODE, brotli_new_data.size(),
Tianjie Xu5450c842017-10-18 13:15:21 -0700666 reinterpret_cast<const uint8_t*>(brotli_new_data.data()), &encoded_size,
667 reinterpret_cast<uint8_t*>(const_cast<char*>(encoded_data.data()))));
668 encoded_data.resize(encoded_size);
Tianjie Xu107a34f2017-06-29 17:04:21 -0700669
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700670 // Write a few small chunks of new data, then a large chunk, and finally a few small chunks.
671 // This helps us to catch potential short writes.
Tianjie Xu107a34f2017-06-29 17:04:21 -0700672 std::vector<std::string> transfer_list = {
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700673 "4",
674 "100",
675 "0",
676 "0",
677 "new 2,0,1",
678 "new 2,1,2",
679 "new 4,2,50,50,97",
680 "new 2,97,98",
681 "new 2,98,99",
682 "new 2,99,100",
Tianjie Xu107a34f2017-06-29 17:04:21 -0700683 };
Tianjie Xu5450c842017-10-18 13:15:21 -0700684
Tao Baobc4a6d52018-05-23 00:12:21 -0700685 PackageEntries entries{
686 { "new_data.br", std::move(encoded_data) },
Tianjie Xu5450c842017-10-18 13:15:21 -0700687 { "patch_data", "" },
688 { "transfer_list", android::base::Join(transfer_list, '\n') },
689 };
690
Tao Baobc4a6d52018-05-23 00:12:21 -0700691 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu107a34f2017-06-29 17:04:21 -0700692
693 std::string updated_content;
Tao Baobc4a6d52018-05-23 00:12:21 -0700694 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_content));
Tianjie Xu107a34f2017-06-29 17:04:21 -0700695 ASSERT_EQ(brotli_new_data, updated_content);
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700696}
Tianjie Xu284752e2017-12-05 11:04:17 -0800697
698TEST_F(UpdaterTest, last_command_update) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700699 std::string block1(4096, '1');
700 std::string block2(4096, '2');
701 std::string block3(4096, '3');
Tianjie Xu284752e2017-12-05 11:04:17 -0800702 std::string block1_hash = get_sha1(block1);
703 std::string block2_hash = get_sha1(block2);
704 std::string block3_hash = get_sha1(block3);
705
706 // Compose the transfer list to fail the first update.
Tao Baobc4a6d52018-05-23 00:12:21 -0700707 std::vector<std::string> transfer_list_fail{
708 // clang-format off
Tianjie Xu284752e2017-12-05 11:04:17 -0800709 "4",
710 "2",
711 "0",
712 "2",
713 "stash " + block1_hash + " 2,0,1",
714 "move " + block1_hash + " 2,1,2 1 2,0,1",
715 "stash " + block3_hash + " 2,2,3",
Tao Bao91a649a2018-05-21 16:05:56 -0700716 "abort",
Tao Baobc4a6d52018-05-23 00:12:21 -0700717 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -0800718 };
719
720 // Mimic a resumed update with the same transfer commands.
Tao Baobc4a6d52018-05-23 00:12:21 -0700721 std::vector<std::string> transfer_list_continue{
722 // clang-format off
Tianjie Xu284752e2017-12-05 11:04:17 -0800723 "4",
724 "2",
725 "0",
726 "2",
727 "stash " + block1_hash + " 2,0,1",
728 "move " + block1_hash + " 2,1,2 1 2,0,1",
729 "stash " + block3_hash + " 2,2,3",
730 "move " + block1_hash + " 2,2,3 1 2,0,1",
Tao Baobc4a6d52018-05-23 00:12:21 -0700731 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -0800732 };
733
Tao Baobc4a6d52018-05-23 00:12:21 -0700734 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_));
735
736 PackageEntries entries{
Tianjie Xu284752e2017-12-05 11:04:17 -0800737 { "new_data", "" },
738 { "patch_data", "" },
Tao Baobc4a6d52018-05-23 00:12:21 -0700739 { "transfer_list", android::base::Join(transfer_list_fail, '\n') },
Tianjie Xu284752e2017-12-05 11:04:17 -0800740 };
741
Tao Baobc4a6d52018-05-23 00:12:21 -0700742 // "2\nstash " + block3_hash + " 2,2,3"
743 std::string last_command_content = "2\n" + transfer_list_fail[kTransferListHeaderLines + 2];
Tianjie Xu284752e2017-12-05 11:04:17 -0800744
Tao Baobc4a6d52018-05-23 00:12:21 -0700745 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu284752e2017-12-05 11:04:17 -0800746
747 // Expect last_command to contain the last stash command.
Tao Baobc4a6d52018-05-23 00:12:21 -0700748 std::string last_command_actual;
Tao Bao7064aa22018-05-24 21:43:50 -0700749 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
Tao Baobc4a6d52018-05-23 00:12:21 -0700750 EXPECT_EQ(last_command_content, last_command_actual);
751
Tianjie Xu284752e2017-12-05 11:04:17 -0800752 std::string updated_contents;
Tao Baobc4a6d52018-05-23 00:12:21 -0700753 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_contents));
Tianjie Xu284752e2017-12-05 11:04:17 -0800754 ASSERT_EQ(block1 + block1 + block3, updated_contents);
755
Tao Baobc4a6d52018-05-23 00:12:21 -0700756 // "Resume" the update. Expect the first 'move' to be skipped but the second 'move' to be
757 // executed. Note that we intentionally reset the image file.
758 entries["transfer_list"] = android::base::Join(transfer_list_continue, '\n');
759 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_));
760 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu284752e2017-12-05 11:04:17 -0800761
Tao Baobc4a6d52018-05-23 00:12:21 -0700762 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_contents));
763 ASSERT_EQ(block1 + block2 + block1, updated_contents);
Tianjie Xu284752e2017-12-05 11:04:17 -0800764}
765
766TEST_F(UpdaterTest, last_command_update_unresumable) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700767 std::string block1(4096, '1');
768 std::string block2(4096, '2');
Tianjie Xu284752e2017-12-05 11:04:17 -0800769 std::string block1_hash = get_sha1(block1);
770 std::string block2_hash = get_sha1(block2);
771
772 // Construct an unresumable update with source blocks mismatch.
Tao Baobc4a6d52018-05-23 00:12:21 -0700773 std::vector<std::string> transfer_list_unresumable{
774 // clang-format off
775 "4",
776 "2",
777 "0",
778 "2",
779 "stash " + block1_hash + " 2,0,1",
780 "move " + block2_hash + " 2,1,2 1 2,0,1",
781 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -0800782 };
783
Tao Baobc4a6d52018-05-23 00:12:21 -0700784 PackageEntries entries{
Tianjie Xu284752e2017-12-05 11:04:17 -0800785 { "new_data", "" },
786 { "patch_data", "" },
Tao Baobc4a6d52018-05-23 00:12:21 -0700787 { "transfer_list", android::base::Join(transfer_list_unresumable, '\n') },
Tianjie Xu284752e2017-12-05 11:04:17 -0800788 };
789
Tao Baobc4a6d52018-05-23 00:12:21 -0700790 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block1, image_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -0800791
Tao Baobc4a6d52018-05-23 00:12:21 -0700792 std::string last_command_content = "0\n" + transfer_list_unresumable[kTransferListHeaderLines];
Tao Bao7064aa22018-05-24 21:43:50 -0700793 ASSERT_TRUE(android::base::WriteStringToFile(last_command_content, last_command_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -0800794
Tao Baobc4a6d52018-05-23 00:12:21 -0700795 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu284752e2017-12-05 11:04:17 -0800796
Tao Baobc4a6d52018-05-23 00:12:21 -0700797 // The last_command_file will be deleted if the update encounters an unresumable failure later.
Tao Bao7064aa22018-05-24 21:43:50 -0700798 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
Tianjie Xu284752e2017-12-05 11:04:17 -0800799}
800
801TEST_F(UpdaterTest, last_command_verify) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700802 std::string block1(4096, '1');
803 std::string block2(4096, '2');
804 std::string block3(4096, '3');
Tianjie Xu284752e2017-12-05 11:04:17 -0800805 std::string block1_hash = get_sha1(block1);
806 std::string block2_hash = get_sha1(block2);
807 std::string block3_hash = get_sha1(block3);
808
Tao Baobc4a6d52018-05-23 00:12:21 -0700809 std::vector<std::string> transfer_list_verify{
810 // clang-format off
Tianjie Xu284752e2017-12-05 11:04:17 -0800811 "4",
812 "2",
813 "0",
814 "2",
815 "stash " + block1_hash + " 2,0,1",
816 "move " + block1_hash + " 2,0,1 1 2,0,1",
817 "move " + block1_hash + " 2,1,2 1 2,0,1",
818 "stash " + block3_hash + " 2,2,3",
Tao Baobc4a6d52018-05-23 00:12:21 -0700819 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -0800820 };
821
Tao Baobc4a6d52018-05-23 00:12:21 -0700822 PackageEntries entries{
Tianjie Xu284752e2017-12-05 11:04:17 -0800823 { "new_data", "" },
824 { "patch_data", "" },
Tao Baobc4a6d52018-05-23 00:12:21 -0700825 { "transfer_list", android::base::Join(transfer_list_verify, '\n') },
Tianjie Xu284752e2017-12-05 11:04:17 -0800826 };
827
Tao Baobc4a6d52018-05-23 00:12:21 -0700828 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block1 + block3, image_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -0800829
Tao Baobc4a6d52018-05-23 00:12:21 -0700830 // Last command: "move " + block1_hash + " 2,1,2 1 2,0,1"
831 std::string last_command_content = "2\n" + transfer_list_verify[kTransferListHeaderLines + 2];
Tianjie Xu284752e2017-12-05 11:04:17 -0800832
Tao Baobc4a6d52018-05-23 00:12:21 -0700833 // First run: expect the verification to succeed and the last_command_file is intact.
Tao Bao7064aa22018-05-24 21:43:50 -0700834 ASSERT_TRUE(android::base::WriteStringToFile(last_command_content, last_command_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -0800835
Tao Baobc4a6d52018-05-23 00:12:21 -0700836 RunBlockImageUpdate(true, entries, image_file_, "t");
Tianjie Xu284752e2017-12-05 11:04:17 -0800837
Tao Baobc4a6d52018-05-23 00:12:21 -0700838 std::string last_command_actual;
Tao Bao7064aa22018-05-24 21:43:50 -0700839 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
Tao Baobc4a6d52018-05-23 00:12:21 -0700840 EXPECT_EQ(last_command_content, last_command_actual);
Tianjie Xu284752e2017-12-05 11:04:17 -0800841
Tao Baobc4a6d52018-05-23 00:12:21 -0700842 // Second run with a mismatching block image: expect the verification to succeed but
843 // last_command_file to be deleted; because the target blocks in the last command don't have the
844 // expected contents for the second move command.
845 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_));
846 RunBlockImageUpdate(true, entries, image_file_, "t");
Tao Bao7064aa22018-05-24 21:43:50 -0700847 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
Tianjie Xu284752e2017-12-05 11:04:17 -0800848}
Tao Baoc0299ed2018-05-24 00:16:35 -0700849
850class ResumableUpdaterTest : public testing::TestWithParam<size_t> {
851 protected:
852 void SetUp() override {
853 RegisterBuiltins();
854 RegisterInstallFunctions();
855 RegisterBlockImageFunctions();
856
857 Paths::Get().set_cache_temp_source(temp_saved_source_.path);
858 Paths::Get().set_last_command_file(temp_last_command_.path);
859 Paths::Get().set_stash_directory_base(temp_stash_base_.path);
860
Tao Bao91a649a2018-05-21 16:05:56 -0700861 // Enable a special command "abort" to simulate interruption.
862 Command::abort_allowed_ = true;
863
Tao Baoc0299ed2018-05-24 00:16:35 -0700864 index_ = GetParam();
865 image_file_ = image_temp_file_.path;
866 last_command_file_ = temp_last_command_.path;
867 }
868
869 void TearDown() override {
870 // Clean up the last_command_file if any.
871 ASSERT_TRUE(android::base::RemoveFileIfExists(last_command_file_));
872
873 // Clear partition updated marker if any.
874 std::string updated_marker{ temp_stash_base_.path };
875 updated_marker += "/" + get_sha1(image_temp_file_.path) + ".UPDATED";
876 ASSERT_TRUE(android::base::RemoveFileIfExists(updated_marker));
877 }
878
879 TemporaryFile temp_saved_source_;
880 TemporaryDir temp_stash_base_;
881 std::string last_command_file_;
882 std::string image_file_;
883 size_t index_;
884
885 private:
886 TemporaryFile temp_last_command_;
887 TemporaryFile image_temp_file_;
888};
889
890static std::string g_source_image;
891static std::string g_target_image;
892static PackageEntries g_entries;
893
894static std::vector<std::string> GenerateTransferList() {
895 std::string a(4096, 'a');
896 std::string b(4096, 'b');
897 std::string c(4096, 'c');
898 std::string d(4096, 'd');
899 std::string e(4096, 'e');
900 std::string f(4096, 'f');
901 std::string g(4096, 'g');
902 std::string h(4096, 'h');
903 std::string i(4096, 'i');
904 std::string zero(4096, '\0');
905
906 std::string a_hash = get_sha1(a);
907 std::string b_hash = get_sha1(b);
908 std::string c_hash = get_sha1(c);
909 std::string e_hash = get_sha1(e);
910
911 auto loc = [](const std::string& range_text) {
912 std::vector<std::string> pieces = android::base::Split(range_text, "-");
913 size_t left;
914 size_t right;
915 if (pieces.size() == 1) {
916 CHECK(android::base::ParseUint(pieces[0], &left));
917 right = left + 1;
918 } else {
919 CHECK_EQ(2u, pieces.size());
920 CHECK(android::base::ParseUint(pieces[0], &left));
921 CHECK(android::base::ParseUint(pieces[1], &right));
922 right++;
923 }
924 return android::base::StringPrintf("2,%zu,%zu", left, right);
925 };
926
927 // patch 1: "b d c" -> "g"
928 TemporaryFile patch_file_bdc_g;
929 std::string bdc = b + d + c;
930 std::string bdc_hash = get_sha1(bdc);
931 std::string g_hash = get_sha1(g);
932 CHECK_EQ(0, bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(bdc.data()), bdc.size(),
933 reinterpret_cast<const uint8_t*>(g.data()), g.size(),
934 patch_file_bdc_g.path, nullptr));
935 std::string patch_bdc_g;
936 CHECK(android::base::ReadFileToString(patch_file_bdc_g.path, &patch_bdc_g));
937
938 // patch 2: "a b c d" -> "d c b"
939 TemporaryFile patch_file_abcd_dcb;
940 std::string abcd = a + b + c + d;
941 std::string abcd_hash = get_sha1(abcd);
942 std::string dcb = d + c + b;
943 std::string dcb_hash = get_sha1(dcb);
944 CHECK_EQ(0, bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(abcd.data()), abcd.size(),
945 reinterpret_cast<const uint8_t*>(dcb.data()), dcb.size(),
946 patch_file_abcd_dcb.path, nullptr));
947 std::string patch_abcd_dcb;
948 CHECK(android::base::ReadFileToString(patch_file_abcd_dcb.path, &patch_abcd_dcb));
949
950 std::vector<std::string> transfer_list{
951 "4",
952 "10", // total blocks written
953 "2", // maximum stash entries
954 "2", // maximum number of stashed blocks
955
956 // a b c d e a b c d e
957 "stash " + b_hash + " " + loc("1"),
958 // a b c d e a b c d e [b(1)]
959 "stash " + c_hash + " " + loc("2"),
960 // a b c d e a b c d e [b(1)][c(2)]
961 "new " + loc("1-2"),
962 // a i h d e a b c d e [b(1)][c(2)]
963 "zero " + loc("0"),
964 // 0 i h d e a b c d e [b(1)][c(2)]
965
966 // bsdiff "b d c" (from stash, 3, stash) to get g(3)
967 android::base::StringPrintf(
968 "bsdiff 0 %zu %s %s %s 3 %s %s %s:%s %s:%s",
969 patch_bdc_g.size(), // patch start (0), patch length
970 bdc_hash.c_str(), // source hash
971 g_hash.c_str(), // target hash
972 loc("3").c_str(), // target range
973 loc("3").c_str(), loc("1").c_str(), // load "d" from block 3, into buffer at offset 1
974 b_hash.c_str(), loc("0").c_str(), // load "b" from stash, into buffer at offset 0
975 c_hash.c_str(), loc("2").c_str()), // load "c" from stash, into buffer at offset 2
976
977 // 0 i h g e a b c d e [b(1)][c(2)]
978 "free " + b_hash,
979 // 0 i h g e a b c d e [c(2)]
980 "free " + a_hash,
981 // 0 i h g e a b c d e
982 "stash " + a_hash + " " + loc("5"),
983 // 0 i h g e a b c d e [a(5)]
984 "move " + e_hash + " " + loc("5") + " 1 " + loc("4"),
985 // 0 i h g e e b c d e [a(5)]
986
987 // bsdiff "a b c d" (from stash, 6-8) to "d c b" (6-8)
988 android::base::StringPrintf( //
989 "bsdiff %zu %zu %s %s %s 4 %s %s %s:%s",
990 patch_bdc_g.size(), // patch start
991 patch_bdc_g.size() + patch_abcd_dcb.size(), // patch length
992 abcd_hash.c_str(), // source hash
993 dcb_hash.c_str(), // target hash
994 loc("6-8").c_str(), // target range
995 loc("6-8").c_str(), // load "b c d" from blocks 6-8
996 loc("1-3").c_str(), // into buffer at offset 1-3
997 a_hash.c_str(), // load "a" from stash
998 loc("0").c_str()), // into buffer at offset 0
999
1000 // 0 i h g e e d c b e [a(5)]
1001 "new " + loc("4"),
1002 // 0 i h g f e d c b e [a(5)]
1003 "move " + a_hash + " " + loc("9") + " 1 - " + a_hash + ":" + loc("0"),
1004 // 0 i h g f e d c b a [a(5)]
1005 "free " + a_hash,
1006 // 0 i h g f e d c b a
1007 };
1008
1009 std::string new_data = i + h + f;
1010 std::string patch_data = patch_bdc_g + patch_abcd_dcb;
1011
1012 g_entries = {
1013 { "new_data", new_data },
1014 { "patch_data", patch_data },
1015 };
1016 g_source_image = a + b + c + d + e + a + b + c + d + e;
1017 g_target_image = zero + i + h + g + f + e + d + c + b + a;
1018
1019 return transfer_list;
1020}
1021
1022static const std::vector<std::string> g_transfer_list = GenerateTransferList();
1023
1024INSTANTIATE_TEST_CASE_P(InterruptAfterEachCommand, ResumableUpdaterTest,
1025 ::testing::Range(static_cast<size_t>(0),
1026 g_transfer_list.size() - kTransferListHeaderLines));
1027
1028TEST_P(ResumableUpdaterTest, InterruptVerifyResume) {
1029 ASSERT_TRUE(android::base::WriteStringToFile(g_source_image, image_file_));
1030
1031 LOG(INFO) << "Interrupting at line " << index_ << " ("
1032 << g_transfer_list[kTransferListHeaderLines + index_] << ")";
1033
1034 std::vector<std::string> transfer_list_copy{ g_transfer_list };
Tao Bao91a649a2018-05-21 16:05:56 -07001035 transfer_list_copy[kTransferListHeaderLines + index_] = "abort";
Tao Baoc0299ed2018-05-24 00:16:35 -07001036
1037 g_entries["transfer_list"] = android::base::Join(transfer_list_copy, '\n');
1038
1039 // Run update that's expected to fail.
1040 RunBlockImageUpdate(false, g_entries, image_file_, "");
1041
1042 std::string last_command_expected;
1043
1044 // Assert the last_command_file.
1045 if (index_ == 0) {
1046 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
1047 } else {
1048 last_command_expected =
1049 std::to_string(index_ - 1) + "\n" + g_transfer_list[kTransferListHeaderLines + index_ - 1];
1050 std::string last_command_actual;
1051 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
1052 ASSERT_EQ(last_command_expected, last_command_actual);
1053 }
1054
1055 g_entries["transfer_list"] = android::base::Join(g_transfer_list, '\n');
1056
1057 // Resume the interrupted update, by doing verification first.
1058 RunBlockImageUpdate(true, g_entries, image_file_, "t");
1059
1060 // last_command_file should remain intact.
1061 if (index_ == 0) {
1062 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
1063 } else {
1064 std::string last_command_actual;
1065 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
1066 ASSERT_EQ(last_command_expected, last_command_actual);
1067 }
1068
1069 // Resume the update.
1070 RunBlockImageUpdate(false, g_entries, image_file_, "t");
1071
1072 // last_command_file should be gone after successful update.
1073 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
1074
1075 std::string updated_image_actual;
1076 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_image_actual));
1077 ASSERT_EQ(g_target_image, updated_image_actual);
1078}