blob: 81ea3b61bfacd8d0594ef6de1c9a98b23f3caeea [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 Baod8d514f2018-07-09 13:32:28 -070060static void expect(const char* expected, const std::string& expr_str, CauseCode cause_code,
Tao Baoef0eb3b2016-11-14 21:29:52 -080061 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;
Tao Baod8d514f2018-07-09 13:32:28 -070064 ASSERT_EQ(0, ParseString(expr_str, &e, &error_count));
Tao Baoef0eb3b2016-11-14 21:29:52 -080065 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 Baod8d514f2018-07-09 13:32:28 -0700129 expect(result.c_str(), script, 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 + "\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700233 expect("t", cmd, kNoCause);
Tao Baodb56eb02017-03-23 06:34:20 -0700234
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 + "\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700241 expect("", cmd, kNoCause);
Tao Baodb56eb02017-03-23 06:34:20 -0700242
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 + "\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700250 expect("t", cmd, kNoCause);
Tao Baodb56eb02017-03-23 06:34:20 -0700251
252 // Multiple arguments.
Tao Bao7c1d4262018-07-06 23:18:14 -0700253 // As long as it successfully loads the partition specified in filename, it won't check against
254 // any given SHAs.
Tao Baodb56eb02017-03-23 06:34:20 -0700255 cmd = "apply_patch_check(\"" + filename + "\", \"wrong_sha1\", \"wrong_sha2\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700256 expect("t", cmd, kNoCause);
Tao Baodb56eb02017-03-23 06:34:20 -0700257
258 cmd = "apply_patch_check(\"" + filename + "\", \"wrong_sha1\", \"" + src_hash +
259 "\", \"wrong_sha2\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700260 expect("t", cmd, kNoCause);
Tao Baodb56eb02017-03-23 06:34:20 -0700261
262 cmd = "apply_patch_check(\"" + filename_bad + "\", \"wrong_sha1\", \"" + src_hash +
263 "\", \"wrong_sha2\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700264 expect("t", cmd, kNoCause);
Tao Baodb56eb02017-03-23 06:34:20 -0700265}
266
Tao Bao51d516e2016-11-03 14:49:01 -0700267TEST_F(UpdaterTest, file_getprop) {
268 // file_getprop() expects two arguments.
269 expect(nullptr, "file_getprop()", kArgsParsingFailure);
270 expect(nullptr, "file_getprop(\"arg1\")", kArgsParsingFailure);
271 expect(nullptr, "file_getprop(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
272
273 // File doesn't exist.
274 expect(nullptr, "file_getprop(\"/doesntexist\", \"key1\")", kFileGetPropFailure);
275
276 // Reject too large files (current limit = 65536).
277 TemporaryFile temp_file1;
278 std::string buffer(65540, '\0');
279 ASSERT_TRUE(android::base::WriteStringToFile(buffer, temp_file1.path));
280
281 // Read some keys.
282 TemporaryFile temp_file2;
283 std::string content("ro.product.name=tardis\n"
284 "# comment\n\n\n"
285 "ro.product.model\n"
286 "ro.product.board = magic \n");
287 ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file2.path));
288
289 std::string script1("file_getprop(\"" + std::string(temp_file2.path) +
290 "\", \"ro.product.name\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700291 expect("tardis", script1, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700292
293 std::string script2("file_getprop(\"" + std::string(temp_file2.path) +
294 "\", \"ro.product.board\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700295 expect("magic", script2, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700296
297 // No match.
298 std::string script3("file_getprop(\"" + std::string(temp_file2.path) +
299 "\", \"ro.product.wrong\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700300 expect("", script3, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700301
302 std::string script4("file_getprop(\"" + std::string(temp_file2.path) +
303 "\", \"ro.product.name=\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700304 expect("", script4, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700305
306 std::string script5("file_getprop(\"" + std::string(temp_file2.path) +
307 "\", \"ro.product.nam\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700308 expect("", script5, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700309
310 std::string script6("file_getprop(\"" + std::string(temp_file2.path) +
311 "\", \"ro.product.model\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700312 expect("", script6, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700313}
Tao Bao0831d0b2016-11-03 23:25:04 -0700314
Tao Baoef0eb3b2016-11-14 21:29:52 -0800315// TODO: Test extracting to block device.
316TEST_F(UpdaterTest, package_extract_file) {
317 // package_extract_file expects 1 or 2 arguments.
318 expect(nullptr, "package_extract_file()", kArgsParsingFailure);
319 expect(nullptr, "package_extract_file(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
320
321 std::string zip_path = from_testdata_base("ziptest_valid.zip");
322 ZipArchiveHandle handle;
323 ASSERT_EQ(0, OpenArchive(zip_path.c_str(), &handle));
324
325 // Need to set up the ziphandle.
326 UpdaterInfo updater_info;
327 updater_info.package_zip = handle;
328
329 // Two-argument version.
330 TemporaryFile temp_file1;
331 std::string script("package_extract_file(\"a.txt\", \"" + std::string(temp_file1.path) + "\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700332 expect("t", script, kNoCause, &updater_info);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800333
334 // Verify the extracted entry.
335 std::string data;
336 ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data));
337 ASSERT_EQ(kATxtContents, data);
338
339 // Now extract another entry to the same location, which should overwrite.
340 script = "package_extract_file(\"b.txt\", \"" + std::string(temp_file1.path) + "\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700341 expect("t", script, kNoCause, &updater_info);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800342
343 ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data));
344 ASSERT_EQ(kBTxtContents, data);
345
346 // Missing zip entry. The two-argument version doesn't abort.
347 script = "package_extract_file(\"doesntexist\", \"" + std::string(temp_file1.path) + "\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700348 expect("", script, kNoCause, &updater_info);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800349
350 // Extract to /dev/full should fail.
351 script = "package_extract_file(\"a.txt\", \"/dev/full\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700352 expect("", script, kNoCause, &updater_info);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800353
Tao Bao0b58e9a2018-07-06 15:01:05 -0700354 // One-argument version. package_extract_file() gives a VAL_BLOB, which needs to be converted to
355 // VAL_STRING for equality test.
356 script = "blob_to_string(package_extract_file(\"a.txt\")) == \"" + kATxtContents + "\"";
Tao Baod8d514f2018-07-09 13:32:28 -0700357 expect("t", script, kNoCause, &updater_info);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800358
Tao Bao0b58e9a2018-07-06 15:01:05 -0700359 script = "blob_to_string(package_extract_file(\"b.txt\")) == \"" + kBTxtContents + "\"";
Tao Baod8d514f2018-07-09 13:32:28 -0700360 expect("t", script, kNoCause, &updater_info);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800361
362 // Missing entry. The one-argument version aborts the evaluation.
363 script = "package_extract_file(\"doesntexist\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700364 expect(nullptr, script, kPackageExtractFileFailure, &updater_info);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800365
366 CloseArchive(handle);
367}
Tao Baod0f30882016-11-03 23:52:01 -0700368
369TEST_F(UpdaterTest, write_value) {
370 // write_value() expects two arguments.
371 expect(nullptr, "write_value()", kArgsParsingFailure);
372 expect(nullptr, "write_value(\"arg1\")", kArgsParsingFailure);
373 expect(nullptr, "write_value(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
374
375 // filename cannot be empty.
376 expect(nullptr, "write_value(\"value\", \"\")", kArgsParsingFailure);
377
378 // Write some value to file.
379 TemporaryFile temp_file;
380 std::string value = "magicvalue";
381 std::string script("write_value(\"" + value + "\", \"" + std::string(temp_file.path) + "\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700382 expect("t", script, kNoCause);
Tao Baod0f30882016-11-03 23:52:01 -0700383
384 // Verify the content.
385 std::string content;
386 ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content));
387 ASSERT_EQ(value, content);
388
389 // Allow writing empty string.
390 script = "write_value(\"\", \"" + std::string(temp_file.path) + "\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700391 expect("t", script, kNoCause);
Tao Baod0f30882016-11-03 23:52:01 -0700392
393 // Verify the content.
394 ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content));
395 ASSERT_EQ("", content);
396
397 // It should fail gracefully when write fails.
398 script = "write_value(\"value\", \"/proc/0/file1\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700399 expect("", script, kNoCause);
Tao Baod0f30882016-11-03 23:52:01 -0700400}
Tao Baobedf5fc2016-11-18 12:01:26 -0800401
402TEST_F(UpdaterTest, get_stage) {
403 // get_stage() expects one argument.
404 expect(nullptr, "get_stage()", kArgsParsingFailure);
405 expect(nullptr, "get_stage(\"arg1\", \"arg2\")", kArgsParsingFailure);
406 expect(nullptr, "get_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
407
408 // Set up a local file as BCB.
409 TemporaryFile tf;
410 std::string temp_file(tf.path);
411 bootloader_message boot;
412 strlcpy(boot.stage, "2/3", sizeof(boot.stage));
413 std::string err;
414 ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err));
415
416 // Can read the stage value.
417 std::string script("get_stage(\"" + temp_file + "\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700418 expect("2/3", script, kNoCause);
Tao Baobedf5fc2016-11-18 12:01:26 -0800419
420 // Bad BCB path.
421 script = "get_stage(\"doesntexist\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700422 expect("", script, kNoCause);
Tao Baobedf5fc2016-11-18 12:01:26 -0800423}
424
425TEST_F(UpdaterTest, set_stage) {
426 // set_stage() expects two arguments.
427 expect(nullptr, "set_stage()", kArgsParsingFailure);
428 expect(nullptr, "set_stage(\"arg1\")", kArgsParsingFailure);
429 expect(nullptr, "set_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
430
431 // Set up a local file as BCB.
432 TemporaryFile tf;
433 std::string temp_file(tf.path);
434 bootloader_message boot;
435 strlcpy(boot.command, "command", sizeof(boot.command));
436 strlcpy(boot.stage, "2/3", sizeof(boot.stage));
437 std::string err;
438 ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err));
439
440 // Write with set_stage().
441 std::string script("set_stage(\"" + temp_file + "\", \"1/3\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700442 expect(tf.path, script, kNoCause);
Tao Baobedf5fc2016-11-18 12:01:26 -0800443
444 // Verify.
445 bootloader_message boot_verify;
446 ASSERT_TRUE(read_bootloader_message_from(&boot_verify, temp_file, &err));
447
448 // Stage should be updated, with command part untouched.
449 ASSERT_STREQ("1/3", boot_verify.stage);
450 ASSERT_STREQ(boot.command, boot_verify.command);
451
452 // Bad BCB path.
453 script = "set_stage(\"doesntexist\", \"1/3\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700454 expect("", script, kNoCause);
Tao Baobedf5fc2016-11-18 12:01:26 -0800455
456 script = "set_stage(\"/dev/full\", \"1/3\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700457 expect("", script, kNoCause);
Tao Baobedf5fc2016-11-18 12:01:26 -0800458}
Tao Bao9aa7ab52017-01-05 17:27:19 -0800459
460TEST_F(UpdaterTest, set_progress) {
461 // set_progress() expects one argument.
462 expect(nullptr, "set_progress()", kArgsParsingFailure);
463 expect(nullptr, "set_progress(\"arg1\", \"arg2\")", kArgsParsingFailure);
464
465 // Invalid progress argument.
466 expect(nullptr, "set_progress(\"arg1\")", kArgsParsingFailure);
467 expect(nullptr, "set_progress(\"3x+5\")", kArgsParsingFailure);
468 expect(nullptr, "set_progress(\".3.5\")", kArgsParsingFailure);
469
470 TemporaryFile tf;
471 UpdaterInfo updater_info;
Tianjie Xu79327ac2017-09-08 17:09:10 -0700472 updater_info.cmd_pipe = fdopen(tf.release(), "w");
Tao Bao9aa7ab52017-01-05 17:27:19 -0800473 expect(".52", "set_progress(\".52\")", kNoCause, &updater_info);
474 fflush(updater_info.cmd_pipe);
475
476 std::string cmd;
477 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd));
478 ASSERT_EQ(android::base::StringPrintf("set_progress %f\n", .52), cmd);
479 // recovery-updater protocol expects 2 tokens ("set_progress <frac>").
480 ASSERT_EQ(2U, android::base::Split(cmd, " ").size());
Tianjie Xu79327ac2017-09-08 17:09:10 -0700481 ASSERT_EQ(0, fclose(updater_info.cmd_pipe));
Tao Bao9aa7ab52017-01-05 17:27:19 -0800482}
483
484TEST_F(UpdaterTest, show_progress) {
485 // show_progress() expects two arguments.
486 expect(nullptr, "show_progress()", kArgsParsingFailure);
487 expect(nullptr, "show_progress(\"arg1\")", kArgsParsingFailure);
488 expect(nullptr, "show_progress(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
489
490 // Invalid progress arguments.
491 expect(nullptr, "show_progress(\"arg1\", \"arg2\")", kArgsParsingFailure);
492 expect(nullptr, "show_progress(\"3x+5\", \"10\")", kArgsParsingFailure);
493 expect(nullptr, "show_progress(\".3\", \"5a\")", kArgsParsingFailure);
494
495 TemporaryFile tf;
496 UpdaterInfo updater_info;
Tianjie Xu79327ac2017-09-08 17:09:10 -0700497 updater_info.cmd_pipe = fdopen(tf.release(), "w");
Tao Bao9aa7ab52017-01-05 17:27:19 -0800498 expect(".52", "show_progress(\".52\", \"10\")", kNoCause, &updater_info);
499 fflush(updater_info.cmd_pipe);
500
501 std::string cmd;
502 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd));
503 ASSERT_EQ(android::base::StringPrintf("progress %f %d\n", .52, 10), cmd);
504 // recovery-updater protocol expects 3 tokens ("progress <frac> <secs>").
505 ASSERT_EQ(3U, android::base::Split(cmd, " ").size());
Tianjie Xu79327ac2017-09-08 17:09:10 -0700506 ASSERT_EQ(0, fclose(updater_info.cmd_pipe));
Tao Bao9aa7ab52017-01-05 17:27:19 -0800507}
Tianjie Xu56ebe622017-03-16 00:48:21 -0700508
Tao Baoffede3e2018-06-07 09:56:19 -0700509TEST_F(UpdaterTest, block_image_update_parsing_error) {
510 std::vector<std::string> transfer_list{
511 // clang-format off
512 "4",
513 "2",
514 "0",
515 // clang-format on
516 };
517
518 PackageEntries entries{
519 { "new_data", "" },
520 { "patch_data", "" },
521 { "transfer_list", android::base::Join(transfer_list, '\n') },
522 };
523
524 RunBlockImageUpdate(false, entries, image_file_, "", kArgsParsingFailure);
525}
526
Tianjie Xu5450c842017-10-18 13:15:21 -0700527TEST_F(UpdaterTest, block_image_update_patch_data) {
Tianjie Xu56ebe622017-03-16 00:48:21 -0700528 std::string src_content = std::string(4096, 'a') + std::string(4096, 'c');
529 std::string tgt_content = std::string(4096, 'b') + std::string(4096, 'd');
Tianjie Xu5450c842017-10-18 13:15:21 -0700530
531 // Generate the patch data.
Tianjie Xu56ebe622017-03-16 00:48:21 -0700532 TemporaryFile patch_file;
Tao Baobc4a6d52018-05-23 00:12:21 -0700533 ASSERT_EQ(0,
534 bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(src_content.data()), src_content.size(),
535 reinterpret_cast<const uint8_t*>(tgt_content.data()), tgt_content.size(),
536 patch_file.path, nullptr));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700537 std::string patch_content;
538 ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch_content));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700539
Tianjie Xu5450c842017-10-18 13:15:21 -0700540 // Create the transfer list that contains a bsdiff.
Tianjie Xu56ebe622017-03-16 00:48:21 -0700541 std::string src_hash = get_sha1(src_content);
542 std::string tgt_hash = get_sha1(tgt_content);
Tao Baobc4a6d52018-05-23 00:12:21 -0700543 std::vector<std::string> transfer_list{
544 // clang-format off
Tianjie Xu56ebe622017-03-16 00:48:21 -0700545 "4",
546 "2",
547 "0",
548 "2",
549 "stash " + src_hash + " 2,0,2",
550 android::base::StringPrintf("bsdiff 0 %zu %s %s 2,0,2 2 - %s:2,0,2", patch_content.size(),
551 src_hash.c_str(), tgt_hash.c_str(), src_hash.c_str()),
552 "free " + src_hash,
Tao Baobc4a6d52018-05-23 00:12:21 -0700553 // clang-format on
Tianjie Xu56ebe622017-03-16 00:48:21 -0700554 };
Tianjie Xu56ebe622017-03-16 00:48:21 -0700555
Tao Baobc4a6d52018-05-23 00:12:21 -0700556 PackageEntries entries{
Tianjie Xu5450c842017-10-18 13:15:21 -0700557 { "new_data", "" },
558 { "patch_data", patch_content },
559 { "transfer_list", android::base::Join(transfer_list, '\n') },
Tianjie Xu56ebe622017-03-16 00:48:21 -0700560 };
Tianjie Xu5450c842017-10-18 13:15:21 -0700561
Tao Baobc4a6d52018-05-23 00:12:21 -0700562 ASSERT_TRUE(android::base::WriteStringToFile(src_content, image_file_));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700563
Tao Baobc4a6d52018-05-23 00:12:21 -0700564 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu56ebe622017-03-16 00:48:21 -0700565
Tianjie Xu56ebe622017-03-16 00:48:21 -0700566 // The update_file should be patched correctly.
567 std::string updated_content;
Tao Baobc4a6d52018-05-23 00:12:21 -0700568 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_content));
569 ASSERT_EQ(tgt_content, updated_content);
Tianjie Xu5450c842017-10-18 13:15:21 -0700570}
571
572TEST_F(UpdaterTest, block_image_update_fail) {
573 std::string src_content(4096 * 2, 'e');
574 std::string src_hash = get_sha1(src_content);
575 // Stash and free some blocks, then fail the update intentionally.
Tao Baobc4a6d52018-05-23 00:12:21 -0700576 std::vector<std::string> transfer_list{
577 // clang-format off
578 "4",
579 "2",
580 "0",
581 "2",
582 "stash " + src_hash + " 2,0,2",
583 "free " + src_hash,
Tao Bao91a649a2018-05-21 16:05:56 -0700584 "abort",
Tao Baobc4a6d52018-05-23 00:12:21 -0700585 // clang-format on
Tianjie Xu5450c842017-10-18 13:15:21 -0700586 };
587
588 // Add a new data of 10 bytes to test the deadlock.
Tao Baobc4a6d52018-05-23 00:12:21 -0700589 PackageEntries entries{
Tianjie Xu5450c842017-10-18 13:15:21 -0700590 { "new_data", std::string(10, 0) },
591 { "patch_data", "" },
592 { "transfer_list", android::base::Join(transfer_list, '\n') },
593 };
594
Tao Baobc4a6d52018-05-23 00:12:21 -0700595 ASSERT_TRUE(android::base::WriteStringToFile(src_content, image_file_));
Tianjie Xu5450c842017-10-18 13:15:21 -0700596
Tao Baobc4a6d52018-05-23 00:12:21 -0700597 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu5450c842017-10-18 13:15:21 -0700598
Tianjie Xu56ebe622017-03-16 00:48:21 -0700599 // Updater generates the stash name based on the input file name.
Tao Baobc4a6d52018-05-23 00:12:21 -0700600 std::string name_digest = get_sha1(image_file_);
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800601 std::string stash_base = std::string(temp_stash_base_.path) + "/" + name_digest;
Tianjie Xu56ebe622017-03-16 00:48:21 -0700602 ASSERT_EQ(0, access(stash_base.c_str(), F_OK));
Tao Baobc4a6d52018-05-23 00:12:21 -0700603 // Expect the stashed blocks to be freed.
Tianjie Xu5450c842017-10-18 13:15:21 -0700604 ASSERT_EQ(-1, access((stash_base + src_hash).c_str(), F_OK));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700605 ASSERT_EQ(0, rmdir(stash_base.c_str()));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700606}
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700607
Tianjie Xu5450c842017-10-18 13:15:21 -0700608TEST_F(UpdaterTest, new_data_over_write) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700609 std::vector<std::string> transfer_list{
610 // clang-format off
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700611 "4",
612 "1",
613 "0",
614 "0",
615 "new 2,0,1",
Tao Baobc4a6d52018-05-23 00:12:21 -0700616 // clang-format on
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700617 };
Tianjie Xu5450c842017-10-18 13:15:21 -0700618
Tao Baobc4a6d52018-05-23 00:12:21 -0700619 // Write 4096 + 100 bytes of new data.
620 PackageEntries entries{
621 { "new_data", std::string(4196, 0) },
Tianjie Xu5450c842017-10-18 13:15:21 -0700622 { "patch_data", "" },
623 { "transfer_list", android::base::Join(transfer_list, '\n') },
624 };
625
Tao Baobc4a6d52018-05-23 00:12:21 -0700626 RunBlockImageUpdate(false, entries, image_file_, "t");
627}
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700628
Tao Baobc4a6d52018-05-23 00:12:21 -0700629TEST_F(UpdaterTest, new_data_short_write) {
630 std::vector<std::string> transfer_list{
631 // clang-format off
632 "4",
633 "1",
634 "0",
635 "0",
636 "new 2,0,1",
637 // clang-format on
638 };
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700639
Tao Baobc4a6d52018-05-23 00:12:21 -0700640 PackageEntries entries{
641 { "patch_data", "" },
642 { "transfer_list", android::base::Join(transfer_list, '\n') },
643 };
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700644
645 // Updater should report the failure gracefully rather than stuck in deadlock.
Tao Baobc4a6d52018-05-23 00:12:21 -0700646 entries["new_data"] = "";
647 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700648
Tao Baobc4a6d52018-05-23 00:12:21 -0700649 entries["new_data"] = std::string(10, 'a');
650 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700651
652 // Expect to write 1 block of new data successfully.
Tao Baobc4a6d52018-05-23 00:12:21 -0700653 entries["new_data"] = std::string(4096, 'a');
654 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu107a34f2017-06-29 17:04:21 -0700655}
656
657TEST_F(UpdaterTest, brotli_new_data) {
Tianjie Xu107a34f2017-06-29 17:04:21 -0700658 auto generator = []() { return rand() % 128; };
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700659 // Generate 100 blocks of random data.
Tianjie Xu107a34f2017-06-29 17:04:21 -0700660 std::string brotli_new_data;
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700661 brotli_new_data.reserve(4096 * 100);
662 generate_n(back_inserter(brotli_new_data), 4096 * 100, generator);
Tianjie Xu107a34f2017-06-29 17:04:21 -0700663
664 size_t encoded_size = BrotliEncoderMaxCompressedSize(brotli_new_data.size());
Tianjie Xu5450c842017-10-18 13:15:21 -0700665 std::string encoded_data(encoded_size, 0);
Tianjie Xu107a34f2017-06-29 17:04:21 -0700666 ASSERT_TRUE(BrotliEncoderCompress(
667 BROTLI_DEFAULT_QUALITY, BROTLI_DEFAULT_WINDOW, BROTLI_DEFAULT_MODE, brotli_new_data.size(),
Tianjie Xu5450c842017-10-18 13:15:21 -0700668 reinterpret_cast<const uint8_t*>(brotli_new_data.data()), &encoded_size,
669 reinterpret_cast<uint8_t*>(const_cast<char*>(encoded_data.data()))));
670 encoded_data.resize(encoded_size);
Tianjie Xu107a34f2017-06-29 17:04:21 -0700671
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700672 // Write a few small chunks of new data, then a large chunk, and finally a few small chunks.
673 // This helps us to catch potential short writes.
Tianjie Xu107a34f2017-06-29 17:04:21 -0700674 std::vector<std::string> transfer_list = {
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700675 "4",
676 "100",
677 "0",
678 "0",
679 "new 2,0,1",
680 "new 2,1,2",
681 "new 4,2,50,50,97",
682 "new 2,97,98",
683 "new 2,98,99",
684 "new 2,99,100",
Tianjie Xu107a34f2017-06-29 17:04:21 -0700685 };
Tianjie Xu5450c842017-10-18 13:15:21 -0700686
Tao Baobc4a6d52018-05-23 00:12:21 -0700687 PackageEntries entries{
688 { "new_data.br", std::move(encoded_data) },
Tianjie Xu5450c842017-10-18 13:15:21 -0700689 { "patch_data", "" },
690 { "transfer_list", android::base::Join(transfer_list, '\n') },
691 };
692
Tao Baobc4a6d52018-05-23 00:12:21 -0700693 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu107a34f2017-06-29 17:04:21 -0700694
695 std::string updated_content;
Tao Baobc4a6d52018-05-23 00:12:21 -0700696 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_content));
Tianjie Xu107a34f2017-06-29 17:04:21 -0700697 ASSERT_EQ(brotli_new_data, updated_content);
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700698}
Tianjie Xu284752e2017-12-05 11:04:17 -0800699
700TEST_F(UpdaterTest, last_command_update) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700701 std::string block1(4096, '1');
702 std::string block2(4096, '2');
703 std::string block3(4096, '3');
Tianjie Xu284752e2017-12-05 11:04:17 -0800704 std::string block1_hash = get_sha1(block1);
705 std::string block2_hash = get_sha1(block2);
706 std::string block3_hash = get_sha1(block3);
707
708 // Compose the transfer list to fail the first update.
Tao Baobc4a6d52018-05-23 00:12:21 -0700709 std::vector<std::string> transfer_list_fail{
710 // clang-format off
Tianjie Xu284752e2017-12-05 11:04:17 -0800711 "4",
712 "2",
713 "0",
714 "2",
715 "stash " + block1_hash + " 2,0,1",
716 "move " + block1_hash + " 2,1,2 1 2,0,1",
717 "stash " + block3_hash + " 2,2,3",
Tao Bao91a649a2018-05-21 16:05:56 -0700718 "abort",
Tao Baobc4a6d52018-05-23 00:12:21 -0700719 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -0800720 };
721
722 // Mimic a resumed update with the same transfer commands.
Tao Baobc4a6d52018-05-23 00:12:21 -0700723 std::vector<std::string> transfer_list_continue{
724 // clang-format off
Tianjie Xu284752e2017-12-05 11:04:17 -0800725 "4",
726 "2",
727 "0",
728 "2",
729 "stash " + block1_hash + " 2,0,1",
730 "move " + block1_hash + " 2,1,2 1 2,0,1",
731 "stash " + block3_hash + " 2,2,3",
732 "move " + block1_hash + " 2,2,3 1 2,0,1",
Tao Baobc4a6d52018-05-23 00:12:21 -0700733 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -0800734 };
735
Tao Baobc4a6d52018-05-23 00:12:21 -0700736 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_));
737
738 PackageEntries entries{
Tianjie Xu284752e2017-12-05 11:04:17 -0800739 { "new_data", "" },
740 { "patch_data", "" },
Tao Baobc4a6d52018-05-23 00:12:21 -0700741 { "transfer_list", android::base::Join(transfer_list_fail, '\n') },
Tianjie Xu284752e2017-12-05 11:04:17 -0800742 };
743
Tao Baobc4a6d52018-05-23 00:12:21 -0700744 // "2\nstash " + block3_hash + " 2,2,3"
745 std::string last_command_content = "2\n" + transfer_list_fail[kTransferListHeaderLines + 2];
Tianjie Xu284752e2017-12-05 11:04:17 -0800746
Tao Baobc4a6d52018-05-23 00:12:21 -0700747 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu284752e2017-12-05 11:04:17 -0800748
749 // Expect last_command to contain the last stash command.
Tao Baobc4a6d52018-05-23 00:12:21 -0700750 std::string last_command_actual;
Tao Bao7064aa22018-05-24 21:43:50 -0700751 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
Tao Baobc4a6d52018-05-23 00:12:21 -0700752 EXPECT_EQ(last_command_content, last_command_actual);
753
Tianjie Xu284752e2017-12-05 11:04:17 -0800754 std::string updated_contents;
Tao Baobc4a6d52018-05-23 00:12:21 -0700755 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_contents));
Tianjie Xu284752e2017-12-05 11:04:17 -0800756 ASSERT_EQ(block1 + block1 + block3, updated_contents);
757
Tao Baobc4a6d52018-05-23 00:12:21 -0700758 // "Resume" the update. Expect the first 'move' to be skipped but the second 'move' to be
759 // executed. Note that we intentionally reset the image file.
760 entries["transfer_list"] = android::base::Join(transfer_list_continue, '\n');
761 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_));
762 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu284752e2017-12-05 11:04:17 -0800763
Tao Baobc4a6d52018-05-23 00:12:21 -0700764 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_contents));
765 ASSERT_EQ(block1 + block2 + block1, updated_contents);
Tianjie Xu284752e2017-12-05 11:04:17 -0800766}
767
768TEST_F(UpdaterTest, last_command_update_unresumable) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700769 std::string block1(4096, '1');
770 std::string block2(4096, '2');
Tianjie Xu284752e2017-12-05 11:04:17 -0800771 std::string block1_hash = get_sha1(block1);
772 std::string block2_hash = get_sha1(block2);
773
774 // Construct an unresumable update with source blocks mismatch.
Tao Baobc4a6d52018-05-23 00:12:21 -0700775 std::vector<std::string> transfer_list_unresumable{
776 // clang-format off
777 "4",
778 "2",
779 "0",
780 "2",
781 "stash " + block1_hash + " 2,0,1",
782 "move " + block2_hash + " 2,1,2 1 2,0,1",
783 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -0800784 };
785
Tao Baobc4a6d52018-05-23 00:12:21 -0700786 PackageEntries entries{
Tianjie Xu284752e2017-12-05 11:04:17 -0800787 { "new_data", "" },
788 { "patch_data", "" },
Tao Baobc4a6d52018-05-23 00:12:21 -0700789 { "transfer_list", android::base::Join(transfer_list_unresumable, '\n') },
Tianjie Xu284752e2017-12-05 11:04:17 -0800790 };
791
Tao Baobc4a6d52018-05-23 00:12:21 -0700792 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block1, image_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -0800793
Tao Baobc4a6d52018-05-23 00:12:21 -0700794 std::string last_command_content = "0\n" + transfer_list_unresumable[kTransferListHeaderLines];
Tao Bao7064aa22018-05-24 21:43:50 -0700795 ASSERT_TRUE(android::base::WriteStringToFile(last_command_content, last_command_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -0800796
Tao Baobc4a6d52018-05-23 00:12:21 -0700797 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu284752e2017-12-05 11:04:17 -0800798
Tao Baobc4a6d52018-05-23 00:12:21 -0700799 // The last_command_file will be deleted if the update encounters an unresumable failure later.
Tao Bao7064aa22018-05-24 21:43:50 -0700800 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
Tianjie Xu284752e2017-12-05 11:04:17 -0800801}
802
803TEST_F(UpdaterTest, last_command_verify) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700804 std::string block1(4096, '1');
805 std::string block2(4096, '2');
806 std::string block3(4096, '3');
Tianjie Xu284752e2017-12-05 11:04:17 -0800807 std::string block1_hash = get_sha1(block1);
808 std::string block2_hash = get_sha1(block2);
809 std::string block3_hash = get_sha1(block3);
810
Tao Baobc4a6d52018-05-23 00:12:21 -0700811 std::vector<std::string> transfer_list_verify{
812 // clang-format off
Tianjie Xu284752e2017-12-05 11:04:17 -0800813 "4",
814 "2",
815 "0",
816 "2",
817 "stash " + block1_hash + " 2,0,1",
818 "move " + block1_hash + " 2,0,1 1 2,0,1",
819 "move " + block1_hash + " 2,1,2 1 2,0,1",
820 "stash " + block3_hash + " 2,2,3",
Tao Baobc4a6d52018-05-23 00:12:21 -0700821 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -0800822 };
823
Tao Baobc4a6d52018-05-23 00:12:21 -0700824 PackageEntries entries{
Tianjie Xu284752e2017-12-05 11:04:17 -0800825 { "new_data", "" },
826 { "patch_data", "" },
Tao Baobc4a6d52018-05-23 00:12:21 -0700827 { "transfer_list", android::base::Join(transfer_list_verify, '\n') },
Tianjie Xu284752e2017-12-05 11:04:17 -0800828 };
829
Tao Baobc4a6d52018-05-23 00:12:21 -0700830 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block1 + block3, image_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -0800831
Tao Baobc4a6d52018-05-23 00:12:21 -0700832 // Last command: "move " + block1_hash + " 2,1,2 1 2,0,1"
833 std::string last_command_content = "2\n" + transfer_list_verify[kTransferListHeaderLines + 2];
Tianjie Xu284752e2017-12-05 11:04:17 -0800834
Tao Baobc4a6d52018-05-23 00:12:21 -0700835 // First run: expect the verification to succeed and the last_command_file is intact.
Tao Bao7064aa22018-05-24 21:43:50 -0700836 ASSERT_TRUE(android::base::WriteStringToFile(last_command_content, last_command_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -0800837
Tao Baobc4a6d52018-05-23 00:12:21 -0700838 RunBlockImageUpdate(true, entries, image_file_, "t");
Tianjie Xu284752e2017-12-05 11:04:17 -0800839
Tao Baobc4a6d52018-05-23 00:12:21 -0700840 std::string last_command_actual;
Tao Bao7064aa22018-05-24 21:43:50 -0700841 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
Tao Baobc4a6d52018-05-23 00:12:21 -0700842 EXPECT_EQ(last_command_content, last_command_actual);
Tianjie Xu284752e2017-12-05 11:04:17 -0800843
Tao Baobc4a6d52018-05-23 00:12:21 -0700844 // Second run with a mismatching block image: expect the verification to succeed but
845 // last_command_file to be deleted; because the target blocks in the last command don't have the
846 // expected contents for the second move command.
847 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_));
848 RunBlockImageUpdate(true, entries, image_file_, "t");
Tao Bao7064aa22018-05-24 21:43:50 -0700849 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
Tianjie Xu284752e2017-12-05 11:04:17 -0800850}
Tao Baoc0299ed2018-05-24 00:16:35 -0700851
852class ResumableUpdaterTest : public testing::TestWithParam<size_t> {
853 protected:
854 void SetUp() override {
855 RegisterBuiltins();
856 RegisterInstallFunctions();
857 RegisterBlockImageFunctions();
858
859 Paths::Get().set_cache_temp_source(temp_saved_source_.path);
860 Paths::Get().set_last_command_file(temp_last_command_.path);
861 Paths::Get().set_stash_directory_base(temp_stash_base_.path);
862
Tao Bao91a649a2018-05-21 16:05:56 -0700863 // Enable a special command "abort" to simulate interruption.
864 Command::abort_allowed_ = true;
865
Tao Baoc0299ed2018-05-24 00:16:35 -0700866 index_ = GetParam();
867 image_file_ = image_temp_file_.path;
868 last_command_file_ = temp_last_command_.path;
869 }
870
871 void TearDown() override {
872 // Clean up the last_command_file if any.
873 ASSERT_TRUE(android::base::RemoveFileIfExists(last_command_file_));
874
875 // Clear partition updated marker if any.
876 std::string updated_marker{ temp_stash_base_.path };
877 updated_marker += "/" + get_sha1(image_temp_file_.path) + ".UPDATED";
878 ASSERT_TRUE(android::base::RemoveFileIfExists(updated_marker));
879 }
880
881 TemporaryFile temp_saved_source_;
882 TemporaryDir temp_stash_base_;
883 std::string last_command_file_;
884 std::string image_file_;
885 size_t index_;
886
887 private:
888 TemporaryFile temp_last_command_;
889 TemporaryFile image_temp_file_;
890};
891
892static std::string g_source_image;
893static std::string g_target_image;
894static PackageEntries g_entries;
895
896static std::vector<std::string> GenerateTransferList() {
897 std::string a(4096, 'a');
898 std::string b(4096, 'b');
899 std::string c(4096, 'c');
900 std::string d(4096, 'd');
901 std::string e(4096, 'e');
902 std::string f(4096, 'f');
903 std::string g(4096, 'g');
904 std::string h(4096, 'h');
905 std::string i(4096, 'i');
906 std::string zero(4096, '\0');
907
908 std::string a_hash = get_sha1(a);
909 std::string b_hash = get_sha1(b);
910 std::string c_hash = get_sha1(c);
911 std::string e_hash = get_sha1(e);
912
913 auto loc = [](const std::string& range_text) {
914 std::vector<std::string> pieces = android::base::Split(range_text, "-");
915 size_t left;
916 size_t right;
917 if (pieces.size() == 1) {
918 CHECK(android::base::ParseUint(pieces[0], &left));
919 right = left + 1;
920 } else {
921 CHECK_EQ(2u, pieces.size());
922 CHECK(android::base::ParseUint(pieces[0], &left));
923 CHECK(android::base::ParseUint(pieces[1], &right));
924 right++;
925 }
926 return android::base::StringPrintf("2,%zu,%zu", left, right);
927 };
928
929 // patch 1: "b d c" -> "g"
930 TemporaryFile patch_file_bdc_g;
931 std::string bdc = b + d + c;
932 std::string bdc_hash = get_sha1(bdc);
933 std::string g_hash = get_sha1(g);
934 CHECK_EQ(0, bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(bdc.data()), bdc.size(),
935 reinterpret_cast<const uint8_t*>(g.data()), g.size(),
936 patch_file_bdc_g.path, nullptr));
937 std::string patch_bdc_g;
938 CHECK(android::base::ReadFileToString(patch_file_bdc_g.path, &patch_bdc_g));
939
940 // patch 2: "a b c d" -> "d c b"
941 TemporaryFile patch_file_abcd_dcb;
942 std::string abcd = a + b + c + d;
943 std::string abcd_hash = get_sha1(abcd);
944 std::string dcb = d + c + b;
945 std::string dcb_hash = get_sha1(dcb);
946 CHECK_EQ(0, bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(abcd.data()), abcd.size(),
947 reinterpret_cast<const uint8_t*>(dcb.data()), dcb.size(),
948 patch_file_abcd_dcb.path, nullptr));
949 std::string patch_abcd_dcb;
950 CHECK(android::base::ReadFileToString(patch_file_abcd_dcb.path, &patch_abcd_dcb));
951
952 std::vector<std::string> transfer_list{
953 "4",
954 "10", // total blocks written
955 "2", // maximum stash entries
956 "2", // maximum number of stashed blocks
957
958 // a b c d e a b c d e
959 "stash " + b_hash + " " + loc("1"),
960 // a b c d e a b c d e [b(1)]
961 "stash " + c_hash + " " + loc("2"),
962 // a b c d e a b c d e [b(1)][c(2)]
963 "new " + loc("1-2"),
964 // a i h d e a b c d e [b(1)][c(2)]
965 "zero " + loc("0"),
966 // 0 i h d e a b c d e [b(1)][c(2)]
967
968 // bsdiff "b d c" (from stash, 3, stash) to get g(3)
969 android::base::StringPrintf(
970 "bsdiff 0 %zu %s %s %s 3 %s %s %s:%s %s:%s",
971 patch_bdc_g.size(), // patch start (0), patch length
972 bdc_hash.c_str(), // source hash
973 g_hash.c_str(), // target hash
974 loc("3").c_str(), // target range
975 loc("3").c_str(), loc("1").c_str(), // load "d" from block 3, into buffer at offset 1
976 b_hash.c_str(), loc("0").c_str(), // load "b" from stash, into buffer at offset 0
977 c_hash.c_str(), loc("2").c_str()), // load "c" from stash, into buffer at offset 2
978
979 // 0 i h g e a b c d e [b(1)][c(2)]
980 "free " + b_hash,
981 // 0 i h g e a b c d e [c(2)]
982 "free " + a_hash,
983 // 0 i h g e a b c d e
984 "stash " + a_hash + " " + loc("5"),
985 // 0 i h g e a b c d e [a(5)]
986 "move " + e_hash + " " + loc("5") + " 1 " + loc("4"),
987 // 0 i h g e e b c d e [a(5)]
988
989 // bsdiff "a b c d" (from stash, 6-8) to "d c b" (6-8)
990 android::base::StringPrintf( //
991 "bsdiff %zu %zu %s %s %s 4 %s %s %s:%s",
992 patch_bdc_g.size(), // patch start
993 patch_bdc_g.size() + patch_abcd_dcb.size(), // patch length
994 abcd_hash.c_str(), // source hash
995 dcb_hash.c_str(), // target hash
996 loc("6-8").c_str(), // target range
997 loc("6-8").c_str(), // load "b c d" from blocks 6-8
998 loc("1-3").c_str(), // into buffer at offset 1-3
999 a_hash.c_str(), // load "a" from stash
1000 loc("0").c_str()), // into buffer at offset 0
1001
1002 // 0 i h g e e d c b e [a(5)]
1003 "new " + loc("4"),
1004 // 0 i h g f e d c b e [a(5)]
1005 "move " + a_hash + " " + loc("9") + " 1 - " + a_hash + ":" + loc("0"),
1006 // 0 i h g f e d c b a [a(5)]
1007 "free " + a_hash,
1008 // 0 i h g f e d c b a
1009 };
1010
1011 std::string new_data = i + h + f;
1012 std::string patch_data = patch_bdc_g + patch_abcd_dcb;
1013
1014 g_entries = {
1015 { "new_data", new_data },
1016 { "patch_data", patch_data },
1017 };
1018 g_source_image = a + b + c + d + e + a + b + c + d + e;
1019 g_target_image = zero + i + h + g + f + e + d + c + b + a;
1020
1021 return transfer_list;
1022}
1023
1024static const std::vector<std::string> g_transfer_list = GenerateTransferList();
1025
1026INSTANTIATE_TEST_CASE_P(InterruptAfterEachCommand, ResumableUpdaterTest,
1027 ::testing::Range(static_cast<size_t>(0),
1028 g_transfer_list.size() - kTransferListHeaderLines));
1029
1030TEST_P(ResumableUpdaterTest, InterruptVerifyResume) {
1031 ASSERT_TRUE(android::base::WriteStringToFile(g_source_image, image_file_));
1032
1033 LOG(INFO) << "Interrupting at line " << index_ << " ("
1034 << g_transfer_list[kTransferListHeaderLines + index_] << ")";
1035
1036 std::vector<std::string> transfer_list_copy{ g_transfer_list };
Tao Bao91a649a2018-05-21 16:05:56 -07001037 transfer_list_copy[kTransferListHeaderLines + index_] = "abort";
Tao Baoc0299ed2018-05-24 00:16:35 -07001038
1039 g_entries["transfer_list"] = android::base::Join(transfer_list_copy, '\n');
1040
1041 // Run update that's expected to fail.
1042 RunBlockImageUpdate(false, g_entries, image_file_, "");
1043
1044 std::string last_command_expected;
1045
1046 // Assert the last_command_file.
1047 if (index_ == 0) {
1048 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
1049 } else {
1050 last_command_expected =
1051 std::to_string(index_ - 1) + "\n" + g_transfer_list[kTransferListHeaderLines + index_ - 1];
1052 std::string last_command_actual;
1053 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
1054 ASSERT_EQ(last_command_expected, last_command_actual);
1055 }
1056
1057 g_entries["transfer_list"] = android::base::Join(g_transfer_list, '\n');
1058
1059 // Resume the interrupted update, by doing verification first.
1060 RunBlockImageUpdate(true, g_entries, image_file_, "t");
1061
1062 // last_command_file should remain intact.
1063 if (index_ == 0) {
1064 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
1065 } else {
1066 std::string last_command_actual;
1067 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
1068 ASSERT_EQ(last_command_expected, last_command_actual);
1069 }
1070
1071 // Resume the update.
1072 RunBlockImageUpdate(false, g_entries, image_file_, "t");
1073
1074 // last_command_file should be gone after successful update.
1075 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
1076
1077 std::string updated_image_actual;
1078 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_image_actual));
1079 ASSERT_EQ(g_target_image, updated_image_actual);
1080}