blob: 248b469b092cfb251e3b052d28298e48f70dc3db [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>
Tianjie Xu69ffa152018-08-01 16:40:00 -070040#include <verity/hash_tree_builder.h>
Tao Baoef0eb3b2016-11-14 21:29:52 -080041#include <ziparchive/zip_archive.h>
Tianjie Xu56ebe622017-03-16 00:48:21 -070042#include <ziparchive/zip_writer.h>
Tao Bao0c7839a2016-10-10 15:48:37 -070043
Tao Baoef0eb3b2016-11-14 21:29:52 -080044#include "common/test_constants.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070045#include "edify/expr.h"
Tao Bao1fc5bf32017-10-06 07:43:41 -070046#include "otautil/error_code.h"
Tao Bao641fa972018-04-25 18:59:40 -070047#include "otautil/paths.h"
Tao Bao09e468f2017-09-29 14:39:33 -070048#include "otautil/print_sha1.h"
Tao Bao17054c02018-05-03 22:41:23 -070049#include "otautil/sysutil.h"
Tao Bao91a649a2018-05-21 16:05:56 -070050#include "private/commands.h"
Tianjie Xu56ebe622017-03-16 00:48:21 -070051#include "updater/blockimg.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070052#include "updater/install.h"
Tao Baoef0eb3b2016-11-14 21:29:52 -080053#include "updater/updater.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070054
Tao Baobafd6c72018-07-09 15:08:50 -070055using namespace std::string_literals;
56
Tao Baobc4a6d52018-05-23 00:12:21 -070057using PackageEntries = std::unordered_map<std::string, std::string>;
58
59static constexpr size_t kTransferListHeaderLines = 4;
60
61struct selabel_handle* sehandle = nullptr;
Tao Bao0c7839a2016-10-10 15:48:37 -070062
Tao Baod8d514f2018-07-09 13:32:28 -070063static void expect(const char* expected, const std::string& expr_str, CauseCode cause_code,
Tao Baoef0eb3b2016-11-14 21:29:52 -080064 UpdaterInfo* info = nullptr) {
Tianjie Xuc4447322017-03-06 14:44:59 -080065 std::unique_ptr<Expr> e;
Tao Baoef0eb3b2016-11-14 21:29:52 -080066 int error_count = 0;
Tao Baod8d514f2018-07-09 13:32:28 -070067 ASSERT_EQ(0, ParseString(expr_str, &e, &error_count));
Tao Baoef0eb3b2016-11-14 21:29:52 -080068 ASSERT_EQ(0, error_count);
Tao Bao0c7839a2016-10-10 15:48:37 -070069
Tao Baoef0eb3b2016-11-14 21:29:52 -080070 State state(expr_str, info);
Tao Bao0c7839a2016-10-10 15:48:37 -070071
Tao Baoef0eb3b2016-11-14 21:29:52 -080072 std::string result;
73 bool status = Evaluate(&state, e, &result);
Tao Bao0c7839a2016-10-10 15:48:37 -070074
Tao Baoef0eb3b2016-11-14 21:29:52 -080075 if (expected == nullptr) {
76 ASSERT_FALSE(status);
77 } else {
Tao Bao0b58e9a2018-07-06 15:01:05 -070078 ASSERT_TRUE(status) << "Evaluate() finished with error message: " << state.errmsg;
Tao Baoef0eb3b2016-11-14 21:29:52 -080079 ASSERT_STREQ(expected, result.c_str());
80 }
Tao Bao0c7839a2016-10-10 15:48:37 -070081
Tao Baoef0eb3b2016-11-14 21:29:52 -080082 // Error code is set in updater/updater.cpp only, by parsing State.errmsg.
83 ASSERT_EQ(kNoError, state.error_code);
Tao Bao361342c2016-02-08 11:15:50 -080084
Tao Baoef0eb3b2016-11-14 21:29:52 -080085 // Cause code should always be available.
86 ASSERT_EQ(cause_code, state.cause_code);
Tao Bao0c7839a2016-10-10 15:48:37 -070087}
88
Tao Baobc4a6d52018-05-23 00:12:21 -070089static void BuildUpdatePackage(const PackageEntries& entries, int fd) {
Tianjie Xu5450c842017-10-18 13:15:21 -070090 FILE* zip_file_ptr = fdopen(fd, "wb");
91 ZipWriter zip_writer(zip_file_ptr);
92
93 for (const auto& entry : entries) {
Tao Baobc4a6d52018-05-23 00:12:21 -070094 // All the entries are written as STORED.
Tianjie Xu5450c842017-10-18 13:15:21 -070095 ASSERT_EQ(0, zip_writer.StartEntry(entry.first.c_str(), 0));
96 if (!entry.second.empty()) {
97 ASSERT_EQ(0, zip_writer.WriteBytes(entry.second.data(), entry.second.size()));
98 }
99 ASSERT_EQ(0, zip_writer.FinishEntry());
100 }
101
102 ASSERT_EQ(0, zip_writer.Finish());
103 ASSERT_EQ(0, fclose(zip_file_ptr));
104}
105
Tao Baobc4a6d52018-05-23 00:12:21 -0700106static void RunBlockImageUpdate(bool is_verify, const PackageEntries& entries,
Tao Baoffede3e2018-06-07 09:56:19 -0700107 const std::string& image_file, const std::string& result,
108 CauseCode cause_code = kNoCause) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700109 CHECK(entries.find("transfer_list") != entries.end());
110
111 // Build the update package.
112 TemporaryFile zip_file;
113 BuildUpdatePackage(entries, zip_file.release());
114
115 MemMapping map;
116 ASSERT_TRUE(map.MapFile(zip_file.path));
117 ZipArchiveHandle handle;
118 ASSERT_EQ(0, OpenArchiveFromMemory(map.addr, map.length, zip_file.path, &handle));
119
120 // Set up the handler, command_pipe, patch offset & length.
121 UpdaterInfo updater_info;
122 updater_info.package_zip = handle;
123 TemporaryFile temp_pipe;
124 updater_info.cmd_pipe = fdopen(temp_pipe.release(), "wbe");
125 updater_info.package_zip_addr = map.addr;
126 updater_info.package_zip_len = map.length;
127
128 std::string new_data = entries.find("new_data.br") != entries.end() ? "new_data.br" : "new_data";
129 std::string script = is_verify ? "block_image_verify" : "block_image_update";
130 script += R"((")" + image_file + R"(", package_extract_file("transfer_list"), ")" + new_data +
131 R"(", "patch_data"))";
Tao Baod8d514f2018-07-09 13:32:28 -0700132 expect(result.c_str(), script, cause_code, &updater_info);
Tao Baobc4a6d52018-05-23 00:12:21 -0700133
134 ASSERT_EQ(0, fclose(updater_info.cmd_pipe));
135 CloseArchive(handle);
136}
137
Tianjie Xu56ebe622017-03-16 00:48:21 -0700138static std::string get_sha1(const std::string& content) {
139 uint8_t digest[SHA_DIGEST_LENGTH];
140 SHA1(reinterpret_cast<const uint8_t*>(content.c_str()), content.size(), digest);
141 return print_sha1(digest);
142}
143
Tao Bao0b58e9a2018-07-06 15:01:05 -0700144static Value* BlobToString(const char* name, State* state,
145 const std::vector<std::unique_ptr<Expr>>& argv) {
146 if (argv.size() != 1) {
147 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
148 }
149
150 std::vector<std::unique_ptr<Value>> args;
151 if (!ReadValueArgs(state, argv, &args)) {
152 return nullptr;
153 }
154
Tao Bao511d7592018-06-19 15:56:49 -0700155 if (args[0]->type != Value::Type::BLOB) {
Tao Bao0b58e9a2018-07-06 15:01:05 -0700156 return ErrorAbort(state, kArgsParsingFailure, "%s() expects a BLOB argument", name);
157 }
158
Tao Bao511d7592018-06-19 15:56:49 -0700159 args[0]->type = Value::Type::STRING;
Tao Bao0b58e9a2018-07-06 15:01:05 -0700160 return args[0].release();
161}
162
Tao Bao0c7839a2016-10-10 15:48:37 -0700163class UpdaterTest : public ::testing::Test {
Tianjie Xu56ebe622017-03-16 00:48:21 -0700164 protected:
Tao Baobc4a6d52018-05-23 00:12:21 -0700165 void SetUp() override {
Tianjie Xu56ebe622017-03-16 00:48:21 -0700166 RegisterBuiltins();
167 RegisterInstallFunctions();
168 RegisterBlockImageFunctions();
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800169
Tao Bao0b58e9a2018-07-06 15:01:05 -0700170 RegisterFunction("blob_to_string", BlobToString);
171
Tao Bao7064aa22018-05-24 21:43:50 -0700172 // Each test is run in a separate process (isolated mode). Shared temporary files won't cause
173 // conflicts.
Tao Bao641fa972018-04-25 18:59:40 -0700174 Paths::Get().set_cache_temp_source(temp_saved_source_.path);
Tao Bao7064aa22018-05-24 21:43:50 -0700175 Paths::Get().set_last_command_file(temp_last_command_.path);
Tao Bao641fa972018-04-25 18:59:40 -0700176 Paths::Get().set_stash_directory_base(temp_stash_base_.path);
Tao Baobc4a6d52018-05-23 00:12:21 -0700177
Tao Bao91a649a2018-05-21 16:05:56 -0700178 // Enable a special command "abort" to simulate interruption.
179 Command::abort_allowed_ = true;
180
Tao Bao7064aa22018-05-24 21:43:50 -0700181 last_command_file_ = temp_last_command_.path;
Tao Baobc4a6d52018-05-23 00:12:21 -0700182 image_file_ = image_temp_file_.path;
183 }
184
185 void TearDown() override {
Tao Bao7064aa22018-05-24 21:43:50 -0700186 // Clean up the last_command_file if any.
187 ASSERT_TRUE(android::base::RemoveFileIfExists(last_command_file_));
188
Tao Baobc4a6d52018-05-23 00:12:21 -0700189 // Clear partition updated marker if any.
190 std::string updated_marker{ temp_stash_base_.path };
191 updated_marker += "/" + get_sha1(image_temp_file_.path) + ".UPDATED";
192 ASSERT_TRUE(android::base::RemoveFileIfExists(updated_marker));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700193 }
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800194
195 TemporaryFile temp_saved_source_;
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800196 TemporaryDir temp_stash_base_;
Tao Bao7064aa22018-05-24 21:43:50 -0700197 std::string last_command_file_;
Tao Baobc4a6d52018-05-23 00:12:21 -0700198 std::string image_file_;
199
200 private:
Tao Bao7064aa22018-05-24 21:43:50 -0700201 TemporaryFile temp_last_command_;
Tao Baobc4a6d52018-05-23 00:12:21 -0700202 TemporaryFile image_temp_file_;
Tao Bao0c7839a2016-10-10 15:48:37 -0700203};
204
205TEST_F(UpdaterTest, getprop) {
206 expect(android::base::GetProperty("ro.product.device", "").c_str(),
207 "getprop(\"ro.product.device\")",
Tao Bao361342c2016-02-08 11:15:50 -0800208 kNoCause);
Tao Bao0c7839a2016-10-10 15:48:37 -0700209
210 expect(android::base::GetProperty("ro.build.fingerprint", "").c_str(),
211 "getprop(\"ro.build.fingerprint\")",
Tao Bao361342c2016-02-08 11:15:50 -0800212 kNoCause);
Tao Bao0c7839a2016-10-10 15:48:37 -0700213
214 // getprop() accepts only one parameter.
Tao Bao361342c2016-02-08 11:15:50 -0800215 expect(nullptr, "getprop()", kArgsParsingFailure);
216 expect(nullptr, "getprop(\"arg1\", \"arg2\")", kArgsParsingFailure);
217}
218
Tao Baodb56eb02017-03-23 06:34:20 -0700219TEST_F(UpdaterTest, apply_patch_check) {
220 // Zero-argument is not valid.
221 expect(nullptr, "apply_patch_check()", kArgsParsingFailure);
222
223 // File not found.
224 expect("", "apply_patch_check(\"/doesntexist\")", kNoCause);
225
226 std::string src_file = from_testdata_base("old.file");
227 std::string src_content;
228 ASSERT_TRUE(android::base::ReadFileToString(src_file, &src_content));
229 size_t src_size = src_content.size();
230 std::string src_hash = get_sha1(src_content);
231
232 // One-argument with EMMC:file:size:sha1 should pass the check.
233 std::string filename = android::base::Join(
234 std::vector<std::string>{ "EMMC", src_file, std::to_string(src_size), src_hash }, ":");
235 std::string cmd = "apply_patch_check(\"" + filename + "\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700236 expect("t", cmd, kNoCause);
Tao Baodb56eb02017-03-23 06:34:20 -0700237
238 // EMMC:file:(size-1):sha1:(size+1):sha1 should fail the check.
239 std::string filename_bad = android::base::Join(
240 std::vector<std::string>{ "EMMC", src_file, std::to_string(src_size - 1), src_hash,
241 std::to_string(src_size + 1), src_hash },
242 ":");
243 cmd = "apply_patch_check(\"" + filename_bad + "\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700244 expect("", cmd, kNoCause);
Tao Baodb56eb02017-03-23 06:34:20 -0700245
246 // EMMC:file:(size-1):sha1:size:sha1:(size+1):sha1 should pass the check.
247 filename_bad =
248 android::base::Join(std::vector<std::string>{ "EMMC", src_file, std::to_string(src_size - 1),
249 src_hash, std::to_string(src_size), src_hash,
250 std::to_string(src_size + 1), src_hash },
251 ":");
252 cmd = "apply_patch_check(\"" + filename_bad + "\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700253 expect("t", cmd, kNoCause);
Tao Baodb56eb02017-03-23 06:34:20 -0700254
255 // Multiple arguments.
Tao Bao7c1d4262018-07-06 23:18:14 -0700256 // As long as it successfully loads the partition specified in filename, it won't check against
257 // any given SHAs.
Tao Baodb56eb02017-03-23 06:34:20 -0700258 cmd = "apply_patch_check(\"" + filename + "\", \"wrong_sha1\", \"wrong_sha2\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700259 expect("t", cmd, kNoCause);
Tao Baodb56eb02017-03-23 06:34:20 -0700260
261 cmd = "apply_patch_check(\"" + filename + "\", \"wrong_sha1\", \"" + src_hash +
262 "\", \"wrong_sha2\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700263 expect("t", cmd, kNoCause);
Tao Baodb56eb02017-03-23 06:34:20 -0700264
265 cmd = "apply_patch_check(\"" + filename_bad + "\", \"wrong_sha1\", \"" + src_hash +
266 "\", \"wrong_sha2\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700267 expect("t", cmd, kNoCause);
Tao Baodb56eb02017-03-23 06:34:20 -0700268}
269
Tao Bao51d516e2016-11-03 14:49:01 -0700270TEST_F(UpdaterTest, file_getprop) {
271 // file_getprop() expects two arguments.
272 expect(nullptr, "file_getprop()", kArgsParsingFailure);
273 expect(nullptr, "file_getprop(\"arg1\")", kArgsParsingFailure);
274 expect(nullptr, "file_getprop(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
275
276 // File doesn't exist.
277 expect(nullptr, "file_getprop(\"/doesntexist\", \"key1\")", kFileGetPropFailure);
278
279 // Reject too large files (current limit = 65536).
280 TemporaryFile temp_file1;
281 std::string buffer(65540, '\0');
282 ASSERT_TRUE(android::base::WriteStringToFile(buffer, temp_file1.path));
283
284 // Read some keys.
285 TemporaryFile temp_file2;
286 std::string content("ro.product.name=tardis\n"
287 "# comment\n\n\n"
288 "ro.product.model\n"
289 "ro.product.board = magic \n");
290 ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file2.path));
291
292 std::string script1("file_getprop(\"" + std::string(temp_file2.path) +
293 "\", \"ro.product.name\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700294 expect("tardis", script1, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700295
296 std::string script2("file_getprop(\"" + std::string(temp_file2.path) +
297 "\", \"ro.product.board\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700298 expect("magic", script2, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700299
300 // No match.
301 std::string script3("file_getprop(\"" + std::string(temp_file2.path) +
302 "\", \"ro.product.wrong\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700303 expect("", script3, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700304
305 std::string script4("file_getprop(\"" + std::string(temp_file2.path) +
306 "\", \"ro.product.name=\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700307 expect("", script4, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700308
309 std::string script5("file_getprop(\"" + std::string(temp_file2.path) +
310 "\", \"ro.product.nam\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700311 expect("", script5, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700312
313 std::string script6("file_getprop(\"" + std::string(temp_file2.path) +
314 "\", \"ro.product.model\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700315 expect("", script6, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700316}
Tao Bao0831d0b2016-11-03 23:25:04 -0700317
Tao Baoef0eb3b2016-11-14 21:29:52 -0800318// TODO: Test extracting to block device.
319TEST_F(UpdaterTest, package_extract_file) {
320 // package_extract_file expects 1 or 2 arguments.
321 expect(nullptr, "package_extract_file()", kArgsParsingFailure);
322 expect(nullptr, "package_extract_file(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
323
324 std::string zip_path = from_testdata_base("ziptest_valid.zip");
325 ZipArchiveHandle handle;
326 ASSERT_EQ(0, OpenArchive(zip_path.c_str(), &handle));
327
328 // Need to set up the ziphandle.
329 UpdaterInfo updater_info;
330 updater_info.package_zip = handle;
331
332 // Two-argument version.
333 TemporaryFile temp_file1;
334 std::string script("package_extract_file(\"a.txt\", \"" + std::string(temp_file1.path) + "\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700335 expect("t", script, kNoCause, &updater_info);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800336
337 // Verify the extracted entry.
338 std::string data;
339 ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data));
340 ASSERT_EQ(kATxtContents, data);
341
342 // Now extract another entry to the same location, which should overwrite.
343 script = "package_extract_file(\"b.txt\", \"" + std::string(temp_file1.path) + "\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700344 expect("t", script, kNoCause, &updater_info);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800345
346 ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data));
347 ASSERT_EQ(kBTxtContents, data);
348
349 // Missing zip entry. The two-argument version doesn't abort.
350 script = "package_extract_file(\"doesntexist\", \"" + std::string(temp_file1.path) + "\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700351 expect("", script, kNoCause, &updater_info);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800352
353 // Extract to /dev/full should fail.
354 script = "package_extract_file(\"a.txt\", \"/dev/full\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700355 expect("", script, kNoCause, &updater_info);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800356
Tao Bao0b58e9a2018-07-06 15:01:05 -0700357 // One-argument version. package_extract_file() gives a VAL_BLOB, which needs to be converted to
358 // VAL_STRING for equality test.
359 script = "blob_to_string(package_extract_file(\"a.txt\")) == \"" + kATxtContents + "\"";
Tao Baod8d514f2018-07-09 13:32:28 -0700360 expect("t", script, kNoCause, &updater_info);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800361
Tao Bao0b58e9a2018-07-06 15:01:05 -0700362 script = "blob_to_string(package_extract_file(\"b.txt\")) == \"" + kBTxtContents + "\"";
Tao Baod8d514f2018-07-09 13:32:28 -0700363 expect("t", script, kNoCause, &updater_info);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800364
365 // Missing entry. The one-argument version aborts the evaluation.
366 script = "package_extract_file(\"doesntexist\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700367 expect(nullptr, script, kPackageExtractFileFailure, &updater_info);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800368
369 CloseArchive(handle);
370}
Tao Baod0f30882016-11-03 23:52:01 -0700371
Tao Baobafd6c72018-07-09 15:08:50 -0700372TEST_F(UpdaterTest, read_file) {
373 // read_file() expects one argument.
374 expect(nullptr, "read_file()", kArgsParsingFailure);
375 expect(nullptr, "read_file(\"arg1\", \"arg2\")", kArgsParsingFailure);
376
377 // Write some value to file and read back.
378 TemporaryFile temp_file;
379 std::string script("write_value(\"foo\", \""s + temp_file.path + "\");");
380 expect("t", script, kNoCause);
381
382 script = "read_file(\""s + temp_file.path + "\") == \"foo\"";
383 expect("t", script, kNoCause);
384
385 script = "read_file(\""s + temp_file.path + "\") == \"bar\"";
386 expect("", script, kNoCause);
387
388 // It should fail gracefully when read fails.
389 script = "read_file(\"/doesntexist\")";
390 expect("", script, kNoCause);
391}
392
Tianjie Xu69ffa152018-08-01 16:40:00 -0700393TEST_F(UpdaterTest, compute_hash_tree_smoke) {
394 std::string data;
395 for (unsigned char i = 0; i < 128; i++) {
396 data += std::string(4096, i);
397 }
398 // Appends an additional block for verity data.
399 data += std::string(4096, 0);
400 ASSERT_EQ(129 * 4096, data.size());
401 ASSERT_TRUE(android::base::WriteStringToFile(data, image_file_));
402
403 std::string salt = "aee087a5be3b982978c923f566a94613496b417f2af592639bc80d141e34dfe7";
404 std::string expected_root_hash =
405 "7e0a8d8747f54384014ab996f5b2dc4eb7ff00c630eede7134c9e3f05c0dd8ca";
406 // hash_tree_ranges, source_ranges, hash_algorithm, salt_hex, root_hash
407 std::vector<std::string> tokens{ "compute_hash_tree", "2,128,129", "2,0,128", "sha256", salt,
408 expected_root_hash };
409 std::string hash_tree_command = android::base::Join(tokens, " ");
410
411 std::vector<std::string> transfer_list{
412 "4", "2", "0", "2", hash_tree_command,
413 };
414
415 PackageEntries entries{
416 { "new_data", "" },
417 { "patch_data", "" },
418 { "transfer_list", android::base::Join(transfer_list, "\n") },
419 };
420
421 RunBlockImageUpdate(false, entries, image_file_, "t");
422
423 std::string updated;
424 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated));
425 ASSERT_EQ(129 * 4096, updated.size());
426 ASSERT_EQ(data.substr(0, 128 * 4096), updated.substr(0, 128 * 4096));
427
428 // Computes the SHA256 of the salt + hash_tree_data and expects the result to match with the
429 // root_hash.
430 std::vector<unsigned char> salt_bytes;
431 ASSERT_TRUE(HashTreeBuilder::ParseBytesArrayFromString(salt, &salt_bytes));
432 std::vector<unsigned char> hash_tree = std::move(salt_bytes);
433 hash_tree.insert(hash_tree.end(), updated.begin() + 128 * 4096, updated.end());
434
435 std::vector<unsigned char> digest(SHA256_DIGEST_LENGTH);
436 SHA256(hash_tree.data(), hash_tree.size(), digest.data());
437 ASSERT_EQ(expected_root_hash, HashTreeBuilder::BytesArrayToString(digest));
438}
439
440TEST_F(UpdaterTest, compute_hash_tree_root_mismatch) {
441 std::string data;
442 for (size_t i = 0; i < 128; i++) {
443 data += std::string(4096, i);
444 }
445 // Appends an additional block for verity data.
446 data += std::string(4096, 0);
447 ASSERT_EQ(129 * 4096, data.size());
448 // Corrupts one bit
449 data[4096] = 'A';
450 ASSERT_TRUE(android::base::WriteStringToFile(data, image_file_));
451
452 std::string salt = "aee087a5be3b982978c923f566a94613496b417f2af592639bc80d141e34dfe7";
453 std::string expected_root_hash =
454 "7e0a8d8747f54384014ab996f5b2dc4eb7ff00c630eede7134c9e3f05c0dd8ca";
455 // hash_tree_ranges, source_ranges, hash_algorithm, salt_hex, root_hash
456 std::vector<std::string> tokens{ "compute_hash_tree", "2,128,129", "2,0,128", "sha256", salt,
457 expected_root_hash };
458 std::string hash_tree_command = android::base::Join(tokens, " ");
459
460 std::vector<std::string> transfer_list{
461 "4", "2", "0", "2", hash_tree_command,
462 };
463
464 PackageEntries entries{
465 { "new_data", "" },
466 { "patch_data", "" },
467 { "transfer_list", android::base::Join(transfer_list, "\n") },
468 };
469
470 RunBlockImageUpdate(false, entries, image_file_, "", kHashTreeComputationFailure);
471}
472
Tao Baod0f30882016-11-03 23:52:01 -0700473TEST_F(UpdaterTest, write_value) {
474 // write_value() expects two arguments.
475 expect(nullptr, "write_value()", kArgsParsingFailure);
476 expect(nullptr, "write_value(\"arg1\")", kArgsParsingFailure);
477 expect(nullptr, "write_value(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
478
479 // filename cannot be empty.
480 expect(nullptr, "write_value(\"value\", \"\")", kArgsParsingFailure);
481
482 // Write some value to file.
483 TemporaryFile temp_file;
484 std::string value = "magicvalue";
485 std::string script("write_value(\"" + value + "\", \"" + std::string(temp_file.path) + "\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700486 expect("t", script, kNoCause);
Tao Baod0f30882016-11-03 23:52:01 -0700487
488 // Verify the content.
489 std::string content;
490 ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content));
491 ASSERT_EQ(value, content);
492
493 // Allow writing empty string.
494 script = "write_value(\"\", \"" + std::string(temp_file.path) + "\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700495 expect("t", script, kNoCause);
Tao Baod0f30882016-11-03 23:52:01 -0700496
497 // Verify the content.
498 ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content));
499 ASSERT_EQ("", content);
500
501 // It should fail gracefully when write fails.
502 script = "write_value(\"value\", \"/proc/0/file1\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700503 expect("", script, kNoCause);
Tao Baod0f30882016-11-03 23:52:01 -0700504}
Tao Baobedf5fc2016-11-18 12:01:26 -0800505
506TEST_F(UpdaterTest, get_stage) {
507 // get_stage() expects one argument.
508 expect(nullptr, "get_stage()", kArgsParsingFailure);
509 expect(nullptr, "get_stage(\"arg1\", \"arg2\")", kArgsParsingFailure);
510 expect(nullptr, "get_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
511
512 // Set up a local file as BCB.
513 TemporaryFile tf;
514 std::string temp_file(tf.path);
515 bootloader_message boot;
516 strlcpy(boot.stage, "2/3", sizeof(boot.stage));
517 std::string err;
518 ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err));
519
520 // Can read the stage value.
521 std::string script("get_stage(\"" + temp_file + "\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700522 expect("2/3", script, kNoCause);
Tao Baobedf5fc2016-11-18 12:01:26 -0800523
524 // Bad BCB path.
525 script = "get_stage(\"doesntexist\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700526 expect("", script, kNoCause);
Tao Baobedf5fc2016-11-18 12:01:26 -0800527}
528
529TEST_F(UpdaterTest, set_stage) {
530 // set_stage() expects two arguments.
531 expect(nullptr, "set_stage()", kArgsParsingFailure);
532 expect(nullptr, "set_stage(\"arg1\")", kArgsParsingFailure);
533 expect(nullptr, "set_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
534
535 // Set up a local file as BCB.
536 TemporaryFile tf;
537 std::string temp_file(tf.path);
538 bootloader_message boot;
539 strlcpy(boot.command, "command", sizeof(boot.command));
540 strlcpy(boot.stage, "2/3", sizeof(boot.stage));
541 std::string err;
542 ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err));
543
544 // Write with set_stage().
545 std::string script("set_stage(\"" + temp_file + "\", \"1/3\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700546 expect(tf.path, script, kNoCause);
Tao Baobedf5fc2016-11-18 12:01:26 -0800547
548 // Verify.
549 bootloader_message boot_verify;
550 ASSERT_TRUE(read_bootloader_message_from(&boot_verify, temp_file, &err));
551
552 // Stage should be updated, with command part untouched.
553 ASSERT_STREQ("1/3", boot_verify.stage);
554 ASSERT_STREQ(boot.command, boot_verify.command);
555
556 // Bad BCB path.
557 script = "set_stage(\"doesntexist\", \"1/3\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700558 expect("", script, kNoCause);
Tao Baobedf5fc2016-11-18 12:01:26 -0800559
560 script = "set_stage(\"/dev/full\", \"1/3\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700561 expect("", script, kNoCause);
Tao Baobedf5fc2016-11-18 12:01:26 -0800562}
Tao Bao9aa7ab52017-01-05 17:27:19 -0800563
564TEST_F(UpdaterTest, set_progress) {
565 // set_progress() expects one argument.
566 expect(nullptr, "set_progress()", kArgsParsingFailure);
567 expect(nullptr, "set_progress(\"arg1\", \"arg2\")", kArgsParsingFailure);
568
569 // Invalid progress argument.
570 expect(nullptr, "set_progress(\"arg1\")", kArgsParsingFailure);
571 expect(nullptr, "set_progress(\"3x+5\")", kArgsParsingFailure);
572 expect(nullptr, "set_progress(\".3.5\")", kArgsParsingFailure);
573
574 TemporaryFile tf;
575 UpdaterInfo updater_info;
Tianjie Xu79327ac2017-09-08 17:09:10 -0700576 updater_info.cmd_pipe = fdopen(tf.release(), "w");
Tao Bao9aa7ab52017-01-05 17:27:19 -0800577 expect(".52", "set_progress(\".52\")", kNoCause, &updater_info);
578 fflush(updater_info.cmd_pipe);
579
580 std::string cmd;
581 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd));
582 ASSERT_EQ(android::base::StringPrintf("set_progress %f\n", .52), cmd);
583 // recovery-updater protocol expects 2 tokens ("set_progress <frac>").
584 ASSERT_EQ(2U, android::base::Split(cmd, " ").size());
Tianjie Xu79327ac2017-09-08 17:09:10 -0700585 ASSERT_EQ(0, fclose(updater_info.cmd_pipe));
Tao Bao9aa7ab52017-01-05 17:27:19 -0800586}
587
588TEST_F(UpdaterTest, show_progress) {
589 // show_progress() expects two arguments.
590 expect(nullptr, "show_progress()", kArgsParsingFailure);
591 expect(nullptr, "show_progress(\"arg1\")", kArgsParsingFailure);
592 expect(nullptr, "show_progress(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
593
594 // Invalid progress arguments.
595 expect(nullptr, "show_progress(\"arg1\", \"arg2\")", kArgsParsingFailure);
596 expect(nullptr, "show_progress(\"3x+5\", \"10\")", kArgsParsingFailure);
597 expect(nullptr, "show_progress(\".3\", \"5a\")", kArgsParsingFailure);
598
599 TemporaryFile tf;
600 UpdaterInfo updater_info;
Tianjie Xu79327ac2017-09-08 17:09:10 -0700601 updater_info.cmd_pipe = fdopen(tf.release(), "w");
Tao Bao9aa7ab52017-01-05 17:27:19 -0800602 expect(".52", "show_progress(\".52\", \"10\")", kNoCause, &updater_info);
603 fflush(updater_info.cmd_pipe);
604
605 std::string cmd;
606 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd));
607 ASSERT_EQ(android::base::StringPrintf("progress %f %d\n", .52, 10), cmd);
608 // recovery-updater protocol expects 3 tokens ("progress <frac> <secs>").
609 ASSERT_EQ(3U, android::base::Split(cmd, " ").size());
Tianjie Xu79327ac2017-09-08 17:09:10 -0700610 ASSERT_EQ(0, fclose(updater_info.cmd_pipe));
Tao Bao9aa7ab52017-01-05 17:27:19 -0800611}
Tianjie Xu56ebe622017-03-16 00:48:21 -0700612
Tao Baoffede3e2018-06-07 09:56:19 -0700613TEST_F(UpdaterTest, block_image_update_parsing_error) {
614 std::vector<std::string> transfer_list{
615 // clang-format off
616 "4",
617 "2",
618 "0",
619 // clang-format on
620 };
621
622 PackageEntries entries{
623 { "new_data", "" },
624 { "patch_data", "" },
625 { "transfer_list", android::base::Join(transfer_list, '\n') },
626 };
627
628 RunBlockImageUpdate(false, entries, image_file_, "", kArgsParsingFailure);
629}
630
Tianjie Xu5450c842017-10-18 13:15:21 -0700631TEST_F(UpdaterTest, block_image_update_patch_data) {
Tianjie Xu56ebe622017-03-16 00:48:21 -0700632 std::string src_content = std::string(4096, 'a') + std::string(4096, 'c');
633 std::string tgt_content = std::string(4096, 'b') + std::string(4096, 'd');
Tianjie Xu5450c842017-10-18 13:15:21 -0700634
635 // Generate the patch data.
Tianjie Xu56ebe622017-03-16 00:48:21 -0700636 TemporaryFile patch_file;
Tao Baobc4a6d52018-05-23 00:12:21 -0700637 ASSERT_EQ(0,
638 bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(src_content.data()), src_content.size(),
639 reinterpret_cast<const uint8_t*>(tgt_content.data()), tgt_content.size(),
640 patch_file.path, nullptr));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700641 std::string patch_content;
642 ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch_content));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700643
Tianjie Xu5450c842017-10-18 13:15:21 -0700644 // Create the transfer list that contains a bsdiff.
Tianjie Xu56ebe622017-03-16 00:48:21 -0700645 std::string src_hash = get_sha1(src_content);
646 std::string tgt_hash = get_sha1(tgt_content);
Tao Baobc4a6d52018-05-23 00:12:21 -0700647 std::vector<std::string> transfer_list{
648 // clang-format off
Tianjie Xu56ebe622017-03-16 00:48:21 -0700649 "4",
650 "2",
651 "0",
652 "2",
653 "stash " + src_hash + " 2,0,2",
654 android::base::StringPrintf("bsdiff 0 %zu %s %s 2,0,2 2 - %s:2,0,2", patch_content.size(),
655 src_hash.c_str(), tgt_hash.c_str(), src_hash.c_str()),
656 "free " + src_hash,
Tao Baobc4a6d52018-05-23 00:12:21 -0700657 // clang-format on
Tianjie Xu56ebe622017-03-16 00:48:21 -0700658 };
Tianjie Xu56ebe622017-03-16 00:48:21 -0700659
Tao Baobc4a6d52018-05-23 00:12:21 -0700660 PackageEntries entries{
Tianjie Xu5450c842017-10-18 13:15:21 -0700661 { "new_data", "" },
662 { "patch_data", patch_content },
663 { "transfer_list", android::base::Join(transfer_list, '\n') },
Tianjie Xu56ebe622017-03-16 00:48:21 -0700664 };
Tianjie Xu5450c842017-10-18 13:15:21 -0700665
Tao Baobc4a6d52018-05-23 00:12:21 -0700666 ASSERT_TRUE(android::base::WriteStringToFile(src_content, image_file_));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700667
Tao Baobc4a6d52018-05-23 00:12:21 -0700668 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu56ebe622017-03-16 00:48:21 -0700669
Tianjie Xu56ebe622017-03-16 00:48:21 -0700670 // The update_file should be patched correctly.
671 std::string updated_content;
Tao Baobc4a6d52018-05-23 00:12:21 -0700672 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_content));
673 ASSERT_EQ(tgt_content, updated_content);
Tianjie Xu5450c842017-10-18 13:15:21 -0700674}
675
676TEST_F(UpdaterTest, block_image_update_fail) {
677 std::string src_content(4096 * 2, 'e');
678 std::string src_hash = get_sha1(src_content);
679 // Stash and free some blocks, then fail the update intentionally.
Tao Baobc4a6d52018-05-23 00:12:21 -0700680 std::vector<std::string> transfer_list{
681 // clang-format off
682 "4",
683 "2",
684 "0",
685 "2",
686 "stash " + src_hash + " 2,0,2",
687 "free " + src_hash,
Tao Bao91a649a2018-05-21 16:05:56 -0700688 "abort",
Tao Baobc4a6d52018-05-23 00:12:21 -0700689 // clang-format on
Tianjie Xu5450c842017-10-18 13:15:21 -0700690 };
691
692 // Add a new data of 10 bytes to test the deadlock.
Tao Baobc4a6d52018-05-23 00:12:21 -0700693 PackageEntries entries{
Tianjie Xu5450c842017-10-18 13:15:21 -0700694 { "new_data", std::string(10, 0) },
695 { "patch_data", "" },
696 { "transfer_list", android::base::Join(transfer_list, '\n') },
697 };
698
Tao Baobc4a6d52018-05-23 00:12:21 -0700699 ASSERT_TRUE(android::base::WriteStringToFile(src_content, image_file_));
Tianjie Xu5450c842017-10-18 13:15:21 -0700700
Tao Baobc4a6d52018-05-23 00:12:21 -0700701 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu5450c842017-10-18 13:15:21 -0700702
Tianjie Xu56ebe622017-03-16 00:48:21 -0700703 // Updater generates the stash name based on the input file name.
Tao Baobc4a6d52018-05-23 00:12:21 -0700704 std::string name_digest = get_sha1(image_file_);
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800705 std::string stash_base = std::string(temp_stash_base_.path) + "/" + name_digest;
Tianjie Xu56ebe622017-03-16 00:48:21 -0700706 ASSERT_EQ(0, access(stash_base.c_str(), F_OK));
Tao Baobc4a6d52018-05-23 00:12:21 -0700707 // Expect the stashed blocks to be freed.
Tianjie Xu5450c842017-10-18 13:15:21 -0700708 ASSERT_EQ(-1, access((stash_base + src_hash).c_str(), F_OK));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700709 ASSERT_EQ(0, rmdir(stash_base.c_str()));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700710}
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700711
Tianjie Xu5450c842017-10-18 13:15:21 -0700712TEST_F(UpdaterTest, new_data_over_write) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700713 std::vector<std::string> transfer_list{
714 // clang-format off
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700715 "4",
716 "1",
717 "0",
718 "0",
719 "new 2,0,1",
Tao Baobc4a6d52018-05-23 00:12:21 -0700720 // clang-format on
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700721 };
Tianjie Xu5450c842017-10-18 13:15:21 -0700722
Tao Baobc4a6d52018-05-23 00:12:21 -0700723 // Write 4096 + 100 bytes of new data.
724 PackageEntries entries{
725 { "new_data", std::string(4196, 0) },
Tianjie Xu5450c842017-10-18 13:15:21 -0700726 { "patch_data", "" },
727 { "transfer_list", android::base::Join(transfer_list, '\n') },
728 };
729
Tao Baobc4a6d52018-05-23 00:12:21 -0700730 RunBlockImageUpdate(false, entries, image_file_, "t");
731}
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700732
Tao Baobc4a6d52018-05-23 00:12:21 -0700733TEST_F(UpdaterTest, new_data_short_write) {
734 std::vector<std::string> transfer_list{
735 // clang-format off
736 "4",
737 "1",
738 "0",
739 "0",
740 "new 2,0,1",
741 // clang-format on
742 };
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700743
Tao Baobc4a6d52018-05-23 00:12:21 -0700744 PackageEntries entries{
745 { "patch_data", "" },
746 { "transfer_list", android::base::Join(transfer_list, '\n') },
747 };
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700748
749 // Updater should report the failure gracefully rather than stuck in deadlock.
Tao Baobc4a6d52018-05-23 00:12:21 -0700750 entries["new_data"] = "";
751 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700752
Tao Baobc4a6d52018-05-23 00:12:21 -0700753 entries["new_data"] = std::string(10, 'a');
754 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700755
756 // Expect to write 1 block of new data successfully.
Tao Baobc4a6d52018-05-23 00:12:21 -0700757 entries["new_data"] = std::string(4096, 'a');
758 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu107a34f2017-06-29 17:04:21 -0700759}
760
761TEST_F(UpdaterTest, brotli_new_data) {
Tianjie Xu107a34f2017-06-29 17:04:21 -0700762 auto generator = []() { return rand() % 128; };
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700763 // Generate 100 blocks of random data.
Tianjie Xu107a34f2017-06-29 17:04:21 -0700764 std::string brotli_new_data;
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700765 brotli_new_data.reserve(4096 * 100);
766 generate_n(back_inserter(brotli_new_data), 4096 * 100, generator);
Tianjie Xu107a34f2017-06-29 17:04:21 -0700767
768 size_t encoded_size = BrotliEncoderMaxCompressedSize(brotli_new_data.size());
Tianjie Xu5450c842017-10-18 13:15:21 -0700769 std::string encoded_data(encoded_size, 0);
Tianjie Xu107a34f2017-06-29 17:04:21 -0700770 ASSERT_TRUE(BrotliEncoderCompress(
771 BROTLI_DEFAULT_QUALITY, BROTLI_DEFAULT_WINDOW, BROTLI_DEFAULT_MODE, brotli_new_data.size(),
Tianjie Xu5450c842017-10-18 13:15:21 -0700772 reinterpret_cast<const uint8_t*>(brotli_new_data.data()), &encoded_size,
773 reinterpret_cast<uint8_t*>(const_cast<char*>(encoded_data.data()))));
774 encoded_data.resize(encoded_size);
Tianjie Xu107a34f2017-06-29 17:04:21 -0700775
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700776 // Write a few small chunks of new data, then a large chunk, and finally a few small chunks.
777 // This helps us to catch potential short writes.
Tianjie Xu107a34f2017-06-29 17:04:21 -0700778 std::vector<std::string> transfer_list = {
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700779 "4",
780 "100",
781 "0",
782 "0",
783 "new 2,0,1",
784 "new 2,1,2",
785 "new 4,2,50,50,97",
786 "new 2,97,98",
787 "new 2,98,99",
788 "new 2,99,100",
Tianjie Xu107a34f2017-06-29 17:04:21 -0700789 };
Tianjie Xu5450c842017-10-18 13:15:21 -0700790
Tao Baobc4a6d52018-05-23 00:12:21 -0700791 PackageEntries entries{
792 { "new_data.br", std::move(encoded_data) },
Tianjie Xu5450c842017-10-18 13:15:21 -0700793 { "patch_data", "" },
794 { "transfer_list", android::base::Join(transfer_list, '\n') },
795 };
796
Tao Baobc4a6d52018-05-23 00:12:21 -0700797 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu107a34f2017-06-29 17:04:21 -0700798
799 std::string updated_content;
Tao Baobc4a6d52018-05-23 00:12:21 -0700800 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_content));
Tianjie Xu107a34f2017-06-29 17:04:21 -0700801 ASSERT_EQ(brotli_new_data, updated_content);
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700802}
Tianjie Xu284752e2017-12-05 11:04:17 -0800803
804TEST_F(UpdaterTest, last_command_update) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700805 std::string block1(4096, '1');
806 std::string block2(4096, '2');
807 std::string block3(4096, '3');
Tianjie Xu284752e2017-12-05 11:04:17 -0800808 std::string block1_hash = get_sha1(block1);
809 std::string block2_hash = get_sha1(block2);
810 std::string block3_hash = get_sha1(block3);
811
812 // Compose the transfer list to fail the first update.
Tao Baobc4a6d52018-05-23 00:12:21 -0700813 std::vector<std::string> transfer_list_fail{
814 // clang-format off
Tianjie Xu284752e2017-12-05 11:04:17 -0800815 "4",
816 "2",
817 "0",
818 "2",
819 "stash " + block1_hash + " 2,0,1",
820 "move " + block1_hash + " 2,1,2 1 2,0,1",
821 "stash " + block3_hash + " 2,2,3",
Tao Bao91a649a2018-05-21 16:05:56 -0700822 "abort",
Tao Baobc4a6d52018-05-23 00:12:21 -0700823 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -0800824 };
825
826 // Mimic a resumed update with the same transfer commands.
Tao Baobc4a6d52018-05-23 00:12:21 -0700827 std::vector<std::string> transfer_list_continue{
828 // clang-format off
Tianjie Xu284752e2017-12-05 11:04:17 -0800829 "4",
830 "2",
831 "0",
832 "2",
833 "stash " + block1_hash + " 2,0,1",
834 "move " + block1_hash + " 2,1,2 1 2,0,1",
835 "stash " + block3_hash + " 2,2,3",
836 "move " + block1_hash + " 2,2,3 1 2,0,1",
Tao Baobc4a6d52018-05-23 00:12:21 -0700837 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -0800838 };
839
Tao Baobc4a6d52018-05-23 00:12:21 -0700840 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_));
841
842 PackageEntries entries{
Tianjie Xu284752e2017-12-05 11:04:17 -0800843 { "new_data", "" },
844 { "patch_data", "" },
Tao Baobc4a6d52018-05-23 00:12:21 -0700845 { "transfer_list", android::base::Join(transfer_list_fail, '\n') },
Tianjie Xu284752e2017-12-05 11:04:17 -0800846 };
847
Tao Baobc4a6d52018-05-23 00:12:21 -0700848 // "2\nstash " + block3_hash + " 2,2,3"
849 std::string last_command_content = "2\n" + transfer_list_fail[kTransferListHeaderLines + 2];
Tianjie Xu284752e2017-12-05 11:04:17 -0800850
Tao Baobc4a6d52018-05-23 00:12:21 -0700851 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu284752e2017-12-05 11:04:17 -0800852
853 // Expect last_command to contain the last stash command.
Tao Baobc4a6d52018-05-23 00:12:21 -0700854 std::string last_command_actual;
Tao Bao7064aa22018-05-24 21:43:50 -0700855 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
Tao Baobc4a6d52018-05-23 00:12:21 -0700856 EXPECT_EQ(last_command_content, last_command_actual);
857
Tianjie Xu284752e2017-12-05 11:04:17 -0800858 std::string updated_contents;
Tao Baobc4a6d52018-05-23 00:12:21 -0700859 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_contents));
Tianjie Xu284752e2017-12-05 11:04:17 -0800860 ASSERT_EQ(block1 + block1 + block3, updated_contents);
861
Tao Baobc4a6d52018-05-23 00:12:21 -0700862 // "Resume" the update. Expect the first 'move' to be skipped but the second 'move' to be
863 // executed. Note that we intentionally reset the image file.
864 entries["transfer_list"] = android::base::Join(transfer_list_continue, '\n');
865 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_));
866 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu284752e2017-12-05 11:04:17 -0800867
Tao Baobc4a6d52018-05-23 00:12:21 -0700868 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_contents));
869 ASSERT_EQ(block1 + block2 + block1, updated_contents);
Tianjie Xu284752e2017-12-05 11:04:17 -0800870}
871
872TEST_F(UpdaterTest, last_command_update_unresumable) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700873 std::string block1(4096, '1');
874 std::string block2(4096, '2');
Tianjie Xu284752e2017-12-05 11:04:17 -0800875 std::string block1_hash = get_sha1(block1);
876 std::string block2_hash = get_sha1(block2);
877
878 // Construct an unresumable update with source blocks mismatch.
Tao Baobc4a6d52018-05-23 00:12:21 -0700879 std::vector<std::string> transfer_list_unresumable{
880 // clang-format off
881 "4",
882 "2",
883 "0",
884 "2",
885 "stash " + block1_hash + " 2,0,1",
886 "move " + block2_hash + " 2,1,2 1 2,0,1",
887 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -0800888 };
889
Tao Baobc4a6d52018-05-23 00:12:21 -0700890 PackageEntries entries{
Tianjie Xu284752e2017-12-05 11:04:17 -0800891 { "new_data", "" },
892 { "patch_data", "" },
Tao Baobc4a6d52018-05-23 00:12:21 -0700893 { "transfer_list", android::base::Join(transfer_list_unresumable, '\n') },
Tianjie Xu284752e2017-12-05 11:04:17 -0800894 };
895
Tao Baobc4a6d52018-05-23 00:12:21 -0700896 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block1, image_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -0800897
Tao Baobc4a6d52018-05-23 00:12:21 -0700898 std::string last_command_content = "0\n" + transfer_list_unresumable[kTransferListHeaderLines];
Tao Bao7064aa22018-05-24 21:43:50 -0700899 ASSERT_TRUE(android::base::WriteStringToFile(last_command_content, last_command_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -0800900
Tao Baobc4a6d52018-05-23 00:12:21 -0700901 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu284752e2017-12-05 11:04:17 -0800902
Tao Baobc4a6d52018-05-23 00:12:21 -0700903 // The last_command_file will be deleted if the update encounters an unresumable failure later.
Tao Bao7064aa22018-05-24 21:43:50 -0700904 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
Tianjie Xu284752e2017-12-05 11:04:17 -0800905}
906
907TEST_F(UpdaterTest, last_command_verify) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700908 std::string block1(4096, '1');
909 std::string block2(4096, '2');
910 std::string block3(4096, '3');
Tianjie Xu284752e2017-12-05 11:04:17 -0800911 std::string block1_hash = get_sha1(block1);
912 std::string block2_hash = get_sha1(block2);
913 std::string block3_hash = get_sha1(block3);
914
Tao Baobc4a6d52018-05-23 00:12:21 -0700915 std::vector<std::string> transfer_list_verify{
916 // clang-format off
Tianjie Xu284752e2017-12-05 11:04:17 -0800917 "4",
918 "2",
919 "0",
920 "2",
921 "stash " + block1_hash + " 2,0,1",
922 "move " + block1_hash + " 2,0,1 1 2,0,1",
923 "move " + block1_hash + " 2,1,2 1 2,0,1",
924 "stash " + block3_hash + " 2,2,3",
Tao Baobc4a6d52018-05-23 00:12:21 -0700925 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -0800926 };
927
Tao Baobc4a6d52018-05-23 00:12:21 -0700928 PackageEntries entries{
Tianjie Xu284752e2017-12-05 11:04:17 -0800929 { "new_data", "" },
930 { "patch_data", "" },
Tao Baobc4a6d52018-05-23 00:12:21 -0700931 { "transfer_list", android::base::Join(transfer_list_verify, '\n') },
Tianjie Xu284752e2017-12-05 11:04:17 -0800932 };
933
Tao Baobc4a6d52018-05-23 00:12:21 -0700934 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block1 + block3, image_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -0800935
Tao Baobc4a6d52018-05-23 00:12:21 -0700936 // Last command: "move " + block1_hash + " 2,1,2 1 2,0,1"
937 std::string last_command_content = "2\n" + transfer_list_verify[kTransferListHeaderLines + 2];
Tianjie Xu284752e2017-12-05 11:04:17 -0800938
Tao Baobc4a6d52018-05-23 00:12:21 -0700939 // First run: expect the verification to succeed and the last_command_file is intact.
Tao Bao7064aa22018-05-24 21:43:50 -0700940 ASSERT_TRUE(android::base::WriteStringToFile(last_command_content, last_command_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -0800941
Tao Baobc4a6d52018-05-23 00:12:21 -0700942 RunBlockImageUpdate(true, entries, image_file_, "t");
Tianjie Xu284752e2017-12-05 11:04:17 -0800943
Tao Baobc4a6d52018-05-23 00:12:21 -0700944 std::string last_command_actual;
Tao Bao7064aa22018-05-24 21:43:50 -0700945 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
Tao Baobc4a6d52018-05-23 00:12:21 -0700946 EXPECT_EQ(last_command_content, last_command_actual);
Tianjie Xu284752e2017-12-05 11:04:17 -0800947
Tao Baobc4a6d52018-05-23 00:12:21 -0700948 // Second run with a mismatching block image: expect the verification to succeed but
949 // last_command_file to be deleted; because the target blocks in the last command don't have the
950 // expected contents for the second move command.
951 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_));
952 RunBlockImageUpdate(true, entries, image_file_, "t");
Tao Bao7064aa22018-05-24 21:43:50 -0700953 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
Tianjie Xu284752e2017-12-05 11:04:17 -0800954}
Tao Baoc0299ed2018-05-24 00:16:35 -0700955
956class ResumableUpdaterTest : public testing::TestWithParam<size_t> {
957 protected:
958 void SetUp() override {
959 RegisterBuiltins();
960 RegisterInstallFunctions();
961 RegisterBlockImageFunctions();
962
963 Paths::Get().set_cache_temp_source(temp_saved_source_.path);
964 Paths::Get().set_last_command_file(temp_last_command_.path);
965 Paths::Get().set_stash_directory_base(temp_stash_base_.path);
966
Tao Bao91a649a2018-05-21 16:05:56 -0700967 // Enable a special command "abort" to simulate interruption.
968 Command::abort_allowed_ = true;
969
Tao Baoc0299ed2018-05-24 00:16:35 -0700970 index_ = GetParam();
971 image_file_ = image_temp_file_.path;
972 last_command_file_ = temp_last_command_.path;
973 }
974
975 void TearDown() override {
976 // Clean up the last_command_file if any.
977 ASSERT_TRUE(android::base::RemoveFileIfExists(last_command_file_));
978
979 // Clear partition updated marker if any.
980 std::string updated_marker{ temp_stash_base_.path };
981 updated_marker += "/" + get_sha1(image_temp_file_.path) + ".UPDATED";
982 ASSERT_TRUE(android::base::RemoveFileIfExists(updated_marker));
983 }
984
985 TemporaryFile temp_saved_source_;
986 TemporaryDir temp_stash_base_;
987 std::string last_command_file_;
988 std::string image_file_;
989 size_t index_;
990
991 private:
992 TemporaryFile temp_last_command_;
993 TemporaryFile image_temp_file_;
994};
995
996static std::string g_source_image;
997static std::string g_target_image;
998static PackageEntries g_entries;
999
1000static std::vector<std::string> GenerateTransferList() {
1001 std::string a(4096, 'a');
1002 std::string b(4096, 'b');
1003 std::string c(4096, 'c');
1004 std::string d(4096, 'd');
1005 std::string e(4096, 'e');
1006 std::string f(4096, 'f');
1007 std::string g(4096, 'g');
1008 std::string h(4096, 'h');
1009 std::string i(4096, 'i');
1010 std::string zero(4096, '\0');
1011
1012 std::string a_hash = get_sha1(a);
1013 std::string b_hash = get_sha1(b);
1014 std::string c_hash = get_sha1(c);
1015 std::string e_hash = get_sha1(e);
1016
1017 auto loc = [](const std::string& range_text) {
1018 std::vector<std::string> pieces = android::base::Split(range_text, "-");
1019 size_t left;
1020 size_t right;
1021 if (pieces.size() == 1) {
1022 CHECK(android::base::ParseUint(pieces[0], &left));
1023 right = left + 1;
1024 } else {
1025 CHECK_EQ(2u, pieces.size());
1026 CHECK(android::base::ParseUint(pieces[0], &left));
1027 CHECK(android::base::ParseUint(pieces[1], &right));
1028 right++;
1029 }
1030 return android::base::StringPrintf("2,%zu,%zu", left, right);
1031 };
1032
1033 // patch 1: "b d c" -> "g"
1034 TemporaryFile patch_file_bdc_g;
1035 std::string bdc = b + d + c;
1036 std::string bdc_hash = get_sha1(bdc);
1037 std::string g_hash = get_sha1(g);
1038 CHECK_EQ(0, bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(bdc.data()), bdc.size(),
1039 reinterpret_cast<const uint8_t*>(g.data()), g.size(),
1040 patch_file_bdc_g.path, nullptr));
1041 std::string patch_bdc_g;
1042 CHECK(android::base::ReadFileToString(patch_file_bdc_g.path, &patch_bdc_g));
1043
1044 // patch 2: "a b c d" -> "d c b"
1045 TemporaryFile patch_file_abcd_dcb;
1046 std::string abcd = a + b + c + d;
1047 std::string abcd_hash = get_sha1(abcd);
1048 std::string dcb = d + c + b;
1049 std::string dcb_hash = get_sha1(dcb);
1050 CHECK_EQ(0, bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(abcd.data()), abcd.size(),
1051 reinterpret_cast<const uint8_t*>(dcb.data()), dcb.size(),
1052 patch_file_abcd_dcb.path, nullptr));
1053 std::string patch_abcd_dcb;
1054 CHECK(android::base::ReadFileToString(patch_file_abcd_dcb.path, &patch_abcd_dcb));
1055
1056 std::vector<std::string> transfer_list{
1057 "4",
1058 "10", // total blocks written
1059 "2", // maximum stash entries
1060 "2", // maximum number of stashed blocks
1061
1062 // a b c d e a b c d e
1063 "stash " + b_hash + " " + loc("1"),
1064 // a b c d e a b c d e [b(1)]
1065 "stash " + c_hash + " " + loc("2"),
1066 // a b c d e a b c d e [b(1)][c(2)]
1067 "new " + loc("1-2"),
1068 // a i h d e a b c d e [b(1)][c(2)]
1069 "zero " + loc("0"),
1070 // 0 i h d e a b c d e [b(1)][c(2)]
1071
1072 // bsdiff "b d c" (from stash, 3, stash) to get g(3)
1073 android::base::StringPrintf(
1074 "bsdiff 0 %zu %s %s %s 3 %s %s %s:%s %s:%s",
1075 patch_bdc_g.size(), // patch start (0), patch length
1076 bdc_hash.c_str(), // source hash
1077 g_hash.c_str(), // target hash
1078 loc("3").c_str(), // target range
1079 loc("3").c_str(), loc("1").c_str(), // load "d" from block 3, into buffer at offset 1
1080 b_hash.c_str(), loc("0").c_str(), // load "b" from stash, into buffer at offset 0
1081 c_hash.c_str(), loc("2").c_str()), // load "c" from stash, into buffer at offset 2
1082
1083 // 0 i h g e a b c d e [b(1)][c(2)]
1084 "free " + b_hash,
1085 // 0 i h g e a b c d e [c(2)]
1086 "free " + a_hash,
1087 // 0 i h g e a b c d e
1088 "stash " + a_hash + " " + loc("5"),
1089 // 0 i h g e a b c d e [a(5)]
1090 "move " + e_hash + " " + loc("5") + " 1 " + loc("4"),
1091 // 0 i h g e e b c d e [a(5)]
1092
1093 // bsdiff "a b c d" (from stash, 6-8) to "d c b" (6-8)
1094 android::base::StringPrintf( //
1095 "bsdiff %zu %zu %s %s %s 4 %s %s %s:%s",
1096 patch_bdc_g.size(), // patch start
1097 patch_bdc_g.size() + patch_abcd_dcb.size(), // patch length
1098 abcd_hash.c_str(), // source hash
1099 dcb_hash.c_str(), // target hash
1100 loc("6-8").c_str(), // target range
1101 loc("6-8").c_str(), // load "b c d" from blocks 6-8
1102 loc("1-3").c_str(), // into buffer at offset 1-3
1103 a_hash.c_str(), // load "a" from stash
1104 loc("0").c_str()), // into buffer at offset 0
1105
1106 // 0 i h g e e d c b e [a(5)]
1107 "new " + loc("4"),
1108 // 0 i h g f e d c b e [a(5)]
1109 "move " + a_hash + " " + loc("9") + " 1 - " + a_hash + ":" + loc("0"),
1110 // 0 i h g f e d c b a [a(5)]
1111 "free " + a_hash,
1112 // 0 i h g f e d c b a
1113 };
1114
1115 std::string new_data = i + h + f;
1116 std::string patch_data = patch_bdc_g + patch_abcd_dcb;
1117
1118 g_entries = {
1119 { "new_data", new_data },
1120 { "patch_data", patch_data },
1121 };
1122 g_source_image = a + b + c + d + e + a + b + c + d + e;
1123 g_target_image = zero + i + h + g + f + e + d + c + b + a;
1124
1125 return transfer_list;
1126}
1127
1128static const std::vector<std::string> g_transfer_list = GenerateTransferList();
1129
1130INSTANTIATE_TEST_CASE_P(InterruptAfterEachCommand, ResumableUpdaterTest,
1131 ::testing::Range(static_cast<size_t>(0),
1132 g_transfer_list.size() - kTransferListHeaderLines));
1133
1134TEST_P(ResumableUpdaterTest, InterruptVerifyResume) {
1135 ASSERT_TRUE(android::base::WriteStringToFile(g_source_image, image_file_));
1136
1137 LOG(INFO) << "Interrupting at line " << index_ << " ("
1138 << g_transfer_list[kTransferListHeaderLines + index_] << ")";
1139
1140 std::vector<std::string> transfer_list_copy{ g_transfer_list };
Tao Bao91a649a2018-05-21 16:05:56 -07001141 transfer_list_copy[kTransferListHeaderLines + index_] = "abort";
Tao Baoc0299ed2018-05-24 00:16:35 -07001142
1143 g_entries["transfer_list"] = android::base::Join(transfer_list_copy, '\n');
1144
1145 // Run update that's expected to fail.
1146 RunBlockImageUpdate(false, g_entries, image_file_, "");
1147
1148 std::string last_command_expected;
1149
1150 // Assert the last_command_file.
1151 if (index_ == 0) {
1152 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
1153 } else {
1154 last_command_expected =
1155 std::to_string(index_ - 1) + "\n" + g_transfer_list[kTransferListHeaderLines + index_ - 1];
1156 std::string last_command_actual;
1157 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
1158 ASSERT_EQ(last_command_expected, last_command_actual);
1159 }
1160
1161 g_entries["transfer_list"] = android::base::Join(g_transfer_list, '\n');
1162
1163 // Resume the interrupted update, by doing verification first.
1164 RunBlockImageUpdate(true, g_entries, image_file_, "t");
1165
1166 // last_command_file should remain intact.
1167 if (index_ == 0) {
1168 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
1169 } else {
1170 std::string last_command_actual;
1171 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
1172 ASSERT_EQ(last_command_expected, last_command_actual);
1173 }
1174
1175 // Resume the update.
1176 RunBlockImageUpdate(false, g_entries, image_file_, "t");
1177
1178 // last_command_file should be gone after successful update.
1179 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
1180
1181 std::string updated_image_actual;
1182 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_image_actual));
1183 ASSERT_EQ(g_target_image, updated_image_actual);
1184}