blob: 4a8d1e6ff98b6e58657587c2da74adf9d54dd4b2 [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>
Tao Bao3cb3c522018-11-02 13:36:58 -070026#include <string_view>
Tianjie Xu5450c842017-10-18 13:15:21 -070027#include <unordered_map>
Tianjie Xu56ebe622017-03-16 00:48:21 -070028#include <vector>
Tao Bao0c7839a2016-10-10 15:48:37 -070029
Tao Bao51d516e2016-11-03 14:49:01 -070030#include <android-base/file.h>
Tao Baobc4a6d52018-05-23 00:12:21 -070031#include <android-base/logging.h>
32#include <android-base/parseint.h>
Tao Bao0c7839a2016-10-10 15:48:37 -070033#include <android-base/properties.h>
Tao Bao9aa7ab52017-01-05 17:27:19 -080034#include <android-base/stringprintf.h>
35#include <android-base/strings.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 Bao5609bc82018-06-20 00:30:48 -070044#include "applypatch/applypatch.h"
Tao Baoef0eb3b2016-11-14 21:29:52 -080045#include "common/test_constants.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070046#include "edify/expr.h"
Tao Bao1fc5bf32017-10-06 07:43:41 -070047#include "otautil/error_code.h"
Tao Bao641fa972018-04-25 18:59:40 -070048#include "otautil/paths.h"
Tao Bao09e468f2017-09-29 14:39:33 -070049#include "otautil/print_sha1.h"
Tao Bao17054c02018-05-03 22:41:23 -070050#include "otautil/sysutil.h"
Tao Bao91a649a2018-05-21 16:05:56 -070051#include "private/commands.h"
Tianjie Xu56ebe622017-03-16 00:48:21 -070052#include "updater/blockimg.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070053#include "updater/install.h"
Tao Baoef0eb3b2016-11-14 21:29:52 -080054#include "updater/updater.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070055
Tao Baobafd6c72018-07-09 15:08:50 -070056using namespace std::string_literals;
57
Tao Baobc4a6d52018-05-23 00:12:21 -070058using PackageEntries = std::unordered_map<std::string, std::string>;
59
Tao Baod8d514f2018-07-09 13:32:28 -070060static void expect(const char* expected, const std::string& expr_str, CauseCode cause_code,
Tianjie Xu58d59122019-05-03 01:05:04 -070061 Updater* updater = 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
Tianjie Xu58d59122019-05-03 01:05:04 -070067 State state(expr_str, updater);
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 Bao3cb3c522018-11-02 13:36:58 -0700103static std::string GetSha1(std::string_view content) {
Tianjie Xu56ebe622017-03-16 00:48:21 -0700104 uint8_t digest[SHA_DIGEST_LENGTH];
Tao Bao3cb3c522018-11-02 13:36:58 -0700105 SHA1(reinterpret_cast<const uint8_t*>(content.data()), content.size(), digest);
Tianjie Xu56ebe622017-03-16 00:48:21 -0700106 return print_sha1(digest);
107}
108
Tao Bao0b58e9a2018-07-06 15:01:05 -0700109static Value* BlobToString(const char* name, State* state,
110 const std::vector<std::unique_ptr<Expr>>& argv) {
111 if (argv.size() != 1) {
112 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
113 }
114
115 std::vector<std::unique_ptr<Value>> args;
116 if (!ReadValueArgs(state, argv, &args)) {
117 return nullptr;
118 }
119
Tao Bao511d7592018-06-19 15:56:49 -0700120 if (args[0]->type != Value::Type::BLOB) {
Tao Bao0b58e9a2018-07-06 15:01:05 -0700121 return ErrorAbort(state, kArgsParsingFailure, "%s() expects a BLOB argument", name);
122 }
123
Tao Bao511d7592018-06-19 15:56:49 -0700124 args[0]->type = Value::Type::STRING;
Tao Bao0b58e9a2018-07-06 15:01:05 -0700125 return args[0].release();
126}
127
Tianjie Xu58d59122019-05-03 01:05:04 -0700128class UpdaterTestBase {
Tianjie Xu56ebe622017-03-16 00:48:21 -0700129 protected:
Tianjie Xu58d59122019-05-03 01:05:04 -0700130 void SetUp() {
Tianjie Xu56ebe622017-03-16 00:48:21 -0700131 RegisterBuiltins();
132 RegisterInstallFunctions();
133 RegisterBlockImageFunctions();
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800134
Tao Bao7064aa22018-05-24 21:43:50 -0700135 // Each test is run in a separate process (isolated mode). Shared temporary files won't cause
136 // conflicts.
Tao Bao641fa972018-04-25 18:59:40 -0700137 Paths::Get().set_cache_temp_source(temp_saved_source_.path);
Tao Bao7064aa22018-05-24 21:43:50 -0700138 Paths::Get().set_last_command_file(temp_last_command_.path);
Tao Bao641fa972018-04-25 18:59:40 -0700139 Paths::Get().set_stash_directory_base(temp_stash_base_.path);
Tao Baobc4a6d52018-05-23 00:12:21 -0700140
Tao Bao7064aa22018-05-24 21:43:50 -0700141 last_command_file_ = temp_last_command_.path;
Tao Baobc4a6d52018-05-23 00:12:21 -0700142 image_file_ = image_temp_file_.path;
143 }
144
Tianjie Xu58d59122019-05-03 01:05:04 -0700145 void TearDown() {
Tao Bao7064aa22018-05-24 21:43:50 -0700146 // Clean up the last_command_file if any.
147 ASSERT_TRUE(android::base::RemoveFileIfExists(last_command_file_));
148
Tao Baobc4a6d52018-05-23 00:12:21 -0700149 // Clear partition updated marker if any.
150 std::string updated_marker{ temp_stash_base_.path };
Tao Bao3cb3c522018-11-02 13:36:58 -0700151 updated_marker += "/" + GetSha1(image_temp_file_.path) + ".UPDATED";
Tao Baobc4a6d52018-05-23 00:12:21 -0700152 ASSERT_TRUE(android::base::RemoveFileIfExists(updated_marker));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700153 }
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800154
Tianjie Xu58d59122019-05-03 01:05:04 -0700155 void RunBlockImageUpdate(bool is_verify, PackageEntries entries, const std::string& image_file,
156 const std::string& result, CauseCode cause_code = kNoCause) {
157 CHECK(entries.find("transfer_list") != entries.end());
158 std::string new_data =
159 entries.find("new_data.br") != entries.end() ? "new_data.br" : "new_data";
160 std::string script = is_verify ? "block_image_verify" : "block_image_update";
161 script += R"((")" + image_file + R"(", package_extract_file("transfer_list"), ")" + new_data +
162 R"(", "patch_data"))";
163 entries.emplace(Updater::SCRIPT_NAME, script);
164
165 // Build the update package.
166 TemporaryFile zip_file;
167 BuildUpdatePackage(entries, zip_file.release());
168
169 // Set up the handler, command_pipe, patch offset & length.
170 TemporaryFile temp_pipe;
171 ASSERT_TRUE(updater_.Init(temp_pipe.release(), zip_file.path, false, nullptr));
172 ASSERT_TRUE(updater_.RunUpdate());
173 ASSERT_EQ(result, updater_.result());
174
175 // Parse the cause code written to the command pipe.
176 int received_cause_code = kNoCause;
177 std::string pipe_content;
178 ASSERT_TRUE(android::base::ReadFileToString(temp_pipe.path, &pipe_content));
179 auto lines = android::base::Split(pipe_content, "\n");
180 for (std::string_view line : lines) {
181 if (android::base::ConsumePrefix(&line, "log cause: ")) {
182 ASSERT_TRUE(android::base::ParseInt(line.data(), &received_cause_code));
183 }
184 }
185 ASSERT_EQ(cause_code, received_cause_code);
186 }
187
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800188 TemporaryFile temp_saved_source_;
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800189 TemporaryDir temp_stash_base_;
Tao Bao7064aa22018-05-24 21:43:50 -0700190 std::string last_command_file_;
Tao Baobc4a6d52018-05-23 00:12:21 -0700191 std::string image_file_;
192
Tianjie Xu58d59122019-05-03 01:05:04 -0700193 Updater updater_;
194
Tao Baobc4a6d52018-05-23 00:12:21 -0700195 private:
Tao Bao7064aa22018-05-24 21:43:50 -0700196 TemporaryFile temp_last_command_;
Tao Baobc4a6d52018-05-23 00:12:21 -0700197 TemporaryFile image_temp_file_;
Tao Bao0c7839a2016-10-10 15:48:37 -0700198};
199
Tianjie Xu58d59122019-05-03 01:05:04 -0700200class UpdaterTest : public UpdaterTestBase, public ::testing::Test {
201 protected:
202 void SetUp() override {
203 UpdaterTestBase::SetUp();
204
205 RegisterFunction("blob_to_string", BlobToString);
206 // Enable a special command "abort" to simulate interruption.
207 Command::abort_allowed_ = true;
208 }
209
210 void TearDown() override {
211 UpdaterTestBase::TearDown();
212 }
213
214 void SetUpdaterCmdPipe(int fd) {
215 FILE* cmd_pipe = fdopen(fd, "w");
216 ASSERT_NE(nullptr, cmd_pipe);
217 updater_.cmd_pipe_.reset(cmd_pipe);
218 }
219
220 void SetUpdaterOtaPackageHandle(ZipArchiveHandle handle) {
221 updater_.package_handle_ = handle;
222 }
223
224 void FlushUpdaterCommandPipe() const {
225 fflush(updater_.cmd_pipe_.get());
226 }
227};
228
Tao Bao0c7839a2016-10-10 15:48:37 -0700229TEST_F(UpdaterTest, getprop) {
230 expect(android::base::GetProperty("ro.product.device", "").c_str(),
231 "getprop(\"ro.product.device\")",
Tao Bao361342c2016-02-08 11:15:50 -0800232 kNoCause);
Tao Bao0c7839a2016-10-10 15:48:37 -0700233
234 expect(android::base::GetProperty("ro.build.fingerprint", "").c_str(),
235 "getprop(\"ro.build.fingerprint\")",
Tao Bao361342c2016-02-08 11:15:50 -0800236 kNoCause);
Tao Bao0c7839a2016-10-10 15:48:37 -0700237
238 // getprop() accepts only one parameter.
Tao Bao361342c2016-02-08 11:15:50 -0800239 expect(nullptr, "getprop()", kArgsParsingFailure);
240 expect(nullptr, "getprop(\"arg1\", \"arg2\")", kArgsParsingFailure);
241}
242
Tao Bao5609bc82018-06-20 00:30:48 -0700243TEST_F(UpdaterTest, patch_partition_check) {
244 // Zero argument is not valid.
245 expect(nullptr, "patch_partition_check()", kArgsParsingFailure);
Tao Baodb56eb02017-03-23 06:34:20 -0700246
Tao Bao5609bc82018-06-20 00:30:48 -0700247 std::string source_file = from_testdata_base("boot.img");
248 std::string source_content;
249 ASSERT_TRUE(android::base::ReadFileToString(source_file, &source_content));
250 size_t source_size = source_content.size();
Tao Bao3cb3c522018-11-02 13:36:58 -0700251 std::string source_hash = GetSha1(source_content);
Tao Bao5609bc82018-06-20 00:30:48 -0700252 Partition source(source_file, source_size, source_hash);
Tao Baodb56eb02017-03-23 06:34:20 -0700253
Tao Bao5609bc82018-06-20 00:30:48 -0700254 std::string target_file = from_testdata_base("recovery.img");
255 std::string target_content;
256 ASSERT_TRUE(android::base::ReadFileToString(target_file, &target_content));
257 size_t target_size = target_content.size();
Tao Bao3cb3c522018-11-02 13:36:58 -0700258 std::string target_hash = GetSha1(target_content);
Tao Bao5609bc82018-06-20 00:30:48 -0700259 Partition target(target_file, target_size, target_hash);
Tao Baodb56eb02017-03-23 06:34:20 -0700260
Tao Bao5609bc82018-06-20 00:30:48 -0700261 // One argument is not valid.
262 expect(nullptr, "patch_partition_check(\"" + source.ToString() + "\")", kArgsParsingFailure);
263 expect(nullptr, "patch_partition_check(\"" + target.ToString() + "\")", kArgsParsingFailure);
264
265 // Both of the source and target have the desired checksum.
266 std::string cmd =
267 "patch_partition_check(\"" + source.ToString() + "\", \"" + target.ToString() + "\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700268 expect("t", cmd, kNoCause);
Tao Baodb56eb02017-03-23 06:34:20 -0700269
Tao Bao5609bc82018-06-20 00:30:48 -0700270 // Only source partition has the desired checksum.
271 Partition bad_target(target_file, target_size - 1, target_hash);
272 cmd = "patch_partition_check(\"" + source.ToString() + "\", \"" + bad_target.ToString() + "\")";
273 expect("t", cmd, kNoCause);
274
275 // Only target partition has the desired checksum.
276 Partition bad_source(source_file, source_size + 1, source_hash);
277 cmd = "patch_partition_check(\"" + bad_source.ToString() + "\", \"" + target.ToString() + "\")";
278 expect("t", cmd, kNoCause);
279
280 // Neither of the source or target has the desired checksum.
281 cmd =
282 "patch_partition_check(\"" + bad_source.ToString() + "\", \"" + bad_target.ToString() + "\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700283 expect("", cmd, kNoCause);
Tao Baodb56eb02017-03-23 06:34:20 -0700284}
285
Tao Bao51d516e2016-11-03 14:49:01 -0700286TEST_F(UpdaterTest, file_getprop) {
287 // file_getprop() expects two arguments.
288 expect(nullptr, "file_getprop()", kArgsParsingFailure);
289 expect(nullptr, "file_getprop(\"arg1\")", kArgsParsingFailure);
290 expect(nullptr, "file_getprop(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
291
292 // File doesn't exist.
Tianjie Xu22f11202018-08-27 10:50:31 -0700293 expect(nullptr, "file_getprop(\"/doesntexist\", \"key1\")", kFreadFailure);
Tao Bao51d516e2016-11-03 14:49:01 -0700294
295 // Reject too large files (current limit = 65536).
296 TemporaryFile temp_file1;
297 std::string buffer(65540, '\0');
298 ASSERT_TRUE(android::base::WriteStringToFile(buffer, temp_file1.path));
299
300 // Read some keys.
301 TemporaryFile temp_file2;
302 std::string content("ro.product.name=tardis\n"
303 "# comment\n\n\n"
304 "ro.product.model\n"
305 "ro.product.board = magic \n");
306 ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file2.path));
307
308 std::string script1("file_getprop(\"" + std::string(temp_file2.path) +
309 "\", \"ro.product.name\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700310 expect("tardis", script1, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700311
312 std::string script2("file_getprop(\"" + std::string(temp_file2.path) +
313 "\", \"ro.product.board\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700314 expect("magic", script2, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700315
316 // No match.
317 std::string script3("file_getprop(\"" + std::string(temp_file2.path) +
318 "\", \"ro.product.wrong\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700319 expect("", script3, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700320
321 std::string script4("file_getprop(\"" + std::string(temp_file2.path) +
322 "\", \"ro.product.name=\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700323 expect("", script4, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700324
325 std::string script5("file_getprop(\"" + std::string(temp_file2.path) +
326 "\", \"ro.product.nam\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700327 expect("", script5, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700328
329 std::string script6("file_getprop(\"" + std::string(temp_file2.path) +
330 "\", \"ro.product.model\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700331 expect("", script6, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700332}
Tao Bao0831d0b2016-11-03 23:25:04 -0700333
Tao Baoef0eb3b2016-11-14 21:29:52 -0800334// TODO: Test extracting to block device.
335TEST_F(UpdaterTest, package_extract_file) {
336 // package_extract_file expects 1 or 2 arguments.
337 expect(nullptr, "package_extract_file()", kArgsParsingFailure);
338 expect(nullptr, "package_extract_file(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
339
340 std::string zip_path = from_testdata_base("ziptest_valid.zip");
341 ZipArchiveHandle handle;
342 ASSERT_EQ(0, OpenArchive(zip_path.c_str(), &handle));
343
344 // Need to set up the ziphandle.
Tianjie Xu58d59122019-05-03 01:05:04 -0700345 SetUpdaterOtaPackageHandle(handle);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800346
347 // Two-argument version.
348 TemporaryFile temp_file1;
349 std::string script("package_extract_file(\"a.txt\", \"" + std::string(temp_file1.path) + "\")");
Tianjie Xu58d59122019-05-03 01:05:04 -0700350 expect("t", script, kNoCause, &updater_);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800351
352 // Verify the extracted entry.
353 std::string data;
354 ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data));
355 ASSERT_EQ(kATxtContents, data);
356
357 // Now extract another entry to the same location, which should overwrite.
358 script = "package_extract_file(\"b.txt\", \"" + std::string(temp_file1.path) + "\")";
Tianjie Xu58d59122019-05-03 01:05:04 -0700359 expect("t", script, kNoCause, &updater_);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800360
361 ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data));
362 ASSERT_EQ(kBTxtContents, data);
363
364 // Missing zip entry. The two-argument version doesn't abort.
365 script = "package_extract_file(\"doesntexist\", \"" + std::string(temp_file1.path) + "\")";
Tianjie Xu58d59122019-05-03 01:05:04 -0700366 expect("", script, kNoCause, &updater_);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800367
368 // Extract to /dev/full should fail.
369 script = "package_extract_file(\"a.txt\", \"/dev/full\")";
Tianjie Xu58d59122019-05-03 01:05:04 -0700370 expect("", script, kNoCause, &updater_);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800371
Tao Bao0b58e9a2018-07-06 15:01:05 -0700372 // One-argument version. package_extract_file() gives a VAL_BLOB, which needs to be converted to
373 // VAL_STRING for equality test.
374 script = "blob_to_string(package_extract_file(\"a.txt\")) == \"" + kATxtContents + "\"";
Tianjie Xu58d59122019-05-03 01:05:04 -0700375 expect("t", script, kNoCause, &updater_);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800376
Tao Bao0b58e9a2018-07-06 15:01:05 -0700377 script = "blob_to_string(package_extract_file(\"b.txt\")) == \"" + kBTxtContents + "\"";
Tianjie Xu58d59122019-05-03 01:05:04 -0700378 expect("t", script, kNoCause, &updater_);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800379
380 // Missing entry. The one-argument version aborts the evaluation.
381 script = "package_extract_file(\"doesntexist\")";
Tianjie Xu58d59122019-05-03 01:05:04 -0700382 expect(nullptr, script, kPackageExtractFileFailure, &updater_);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800383}
Tao Baod0f30882016-11-03 23:52:01 -0700384
Tao Baobafd6c72018-07-09 15:08:50 -0700385TEST_F(UpdaterTest, read_file) {
386 // read_file() expects one argument.
387 expect(nullptr, "read_file()", kArgsParsingFailure);
388 expect(nullptr, "read_file(\"arg1\", \"arg2\")", kArgsParsingFailure);
389
390 // Write some value to file and read back.
391 TemporaryFile temp_file;
392 std::string script("write_value(\"foo\", \""s + temp_file.path + "\");");
393 expect("t", script, kNoCause);
394
395 script = "read_file(\""s + temp_file.path + "\") == \"foo\"";
396 expect("t", script, kNoCause);
397
398 script = "read_file(\""s + temp_file.path + "\") == \"bar\"";
399 expect("", script, kNoCause);
400
401 // It should fail gracefully when read fails.
402 script = "read_file(\"/doesntexist\")";
403 expect("", script, kNoCause);
404}
405
Tianjie Xu69ffa152018-08-01 16:40:00 -0700406TEST_F(UpdaterTest, compute_hash_tree_smoke) {
407 std::string data;
408 for (unsigned char i = 0; i < 128; i++) {
409 data += std::string(4096, i);
410 }
411 // Appends an additional block for verity data.
412 data += std::string(4096, 0);
413 ASSERT_EQ(129 * 4096, data.size());
414 ASSERT_TRUE(android::base::WriteStringToFile(data, image_file_));
415
416 std::string salt = "aee087a5be3b982978c923f566a94613496b417f2af592639bc80d141e34dfe7";
417 std::string expected_root_hash =
418 "7e0a8d8747f54384014ab996f5b2dc4eb7ff00c630eede7134c9e3f05c0dd8ca";
419 // hash_tree_ranges, source_ranges, hash_algorithm, salt_hex, root_hash
420 std::vector<std::string> tokens{ "compute_hash_tree", "2,128,129", "2,0,128", "sha256", salt,
421 expected_root_hash };
422 std::string hash_tree_command = android::base::Join(tokens, " ");
423
424 std::vector<std::string> transfer_list{
425 "4", "2", "0", "2", hash_tree_command,
426 };
427
428 PackageEntries entries{
429 { "new_data", "" },
430 { "patch_data", "" },
431 { "transfer_list", android::base::Join(transfer_list, "\n") },
432 };
433
434 RunBlockImageUpdate(false, entries, image_file_, "t");
435
436 std::string updated;
437 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated));
438 ASSERT_EQ(129 * 4096, updated.size());
439 ASSERT_EQ(data.substr(0, 128 * 4096), updated.substr(0, 128 * 4096));
440
441 // Computes the SHA256 of the salt + hash_tree_data and expects the result to match with the
442 // root_hash.
443 std::vector<unsigned char> salt_bytes;
444 ASSERT_TRUE(HashTreeBuilder::ParseBytesArrayFromString(salt, &salt_bytes));
445 std::vector<unsigned char> hash_tree = std::move(salt_bytes);
446 hash_tree.insert(hash_tree.end(), updated.begin() + 128 * 4096, updated.end());
447
448 std::vector<unsigned char> digest(SHA256_DIGEST_LENGTH);
449 SHA256(hash_tree.data(), hash_tree.size(), digest.data());
450 ASSERT_EQ(expected_root_hash, HashTreeBuilder::BytesArrayToString(digest));
451}
452
453TEST_F(UpdaterTest, compute_hash_tree_root_mismatch) {
454 std::string data;
455 for (size_t i = 0; i < 128; i++) {
456 data += std::string(4096, i);
457 }
458 // Appends an additional block for verity data.
459 data += std::string(4096, 0);
460 ASSERT_EQ(129 * 4096, data.size());
461 // Corrupts one bit
462 data[4096] = 'A';
463 ASSERT_TRUE(android::base::WriteStringToFile(data, image_file_));
464
465 std::string salt = "aee087a5be3b982978c923f566a94613496b417f2af592639bc80d141e34dfe7";
466 std::string expected_root_hash =
467 "7e0a8d8747f54384014ab996f5b2dc4eb7ff00c630eede7134c9e3f05c0dd8ca";
468 // hash_tree_ranges, source_ranges, hash_algorithm, salt_hex, root_hash
469 std::vector<std::string> tokens{ "compute_hash_tree", "2,128,129", "2,0,128", "sha256", salt,
470 expected_root_hash };
471 std::string hash_tree_command = android::base::Join(tokens, " ");
472
473 std::vector<std::string> transfer_list{
474 "4", "2", "0", "2", hash_tree_command,
475 };
476
477 PackageEntries entries{
478 { "new_data", "" },
479 { "patch_data", "" },
480 { "transfer_list", android::base::Join(transfer_list, "\n") },
481 };
482
483 RunBlockImageUpdate(false, entries, image_file_, "", kHashTreeComputationFailure);
484}
485
Tao Baod0f30882016-11-03 23:52:01 -0700486TEST_F(UpdaterTest, write_value) {
487 // write_value() expects two arguments.
488 expect(nullptr, "write_value()", kArgsParsingFailure);
489 expect(nullptr, "write_value(\"arg1\")", kArgsParsingFailure);
490 expect(nullptr, "write_value(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
491
492 // filename cannot be empty.
493 expect(nullptr, "write_value(\"value\", \"\")", kArgsParsingFailure);
494
495 // Write some value to file.
496 TemporaryFile temp_file;
497 std::string value = "magicvalue";
498 std::string script("write_value(\"" + value + "\", \"" + std::string(temp_file.path) + "\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700499 expect("t", script, kNoCause);
Tao Baod0f30882016-11-03 23:52:01 -0700500
501 // Verify the content.
502 std::string content;
503 ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content));
504 ASSERT_EQ(value, content);
505
506 // Allow writing empty string.
507 script = "write_value(\"\", \"" + std::string(temp_file.path) + "\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700508 expect("t", script, kNoCause);
Tao Baod0f30882016-11-03 23:52:01 -0700509
510 // Verify the content.
511 ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content));
512 ASSERT_EQ("", content);
513
514 // It should fail gracefully when write fails.
515 script = "write_value(\"value\", \"/proc/0/file1\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700516 expect("", script, kNoCause);
Tao Baod0f30882016-11-03 23:52:01 -0700517}
Tao Baobedf5fc2016-11-18 12:01:26 -0800518
519TEST_F(UpdaterTest, get_stage) {
520 // get_stage() expects one argument.
521 expect(nullptr, "get_stage()", kArgsParsingFailure);
522 expect(nullptr, "get_stage(\"arg1\", \"arg2\")", kArgsParsingFailure);
523 expect(nullptr, "get_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
524
525 // Set up a local file as BCB.
526 TemporaryFile tf;
527 std::string temp_file(tf.path);
528 bootloader_message boot;
529 strlcpy(boot.stage, "2/3", sizeof(boot.stage));
530 std::string err;
531 ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err));
532
533 // Can read the stage value.
534 std::string script("get_stage(\"" + temp_file + "\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700535 expect("2/3", script, kNoCause);
Tao Baobedf5fc2016-11-18 12:01:26 -0800536
537 // Bad BCB path.
538 script = "get_stage(\"doesntexist\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700539 expect("", script, kNoCause);
Tao Baobedf5fc2016-11-18 12:01:26 -0800540}
541
542TEST_F(UpdaterTest, set_stage) {
543 // set_stage() expects two arguments.
544 expect(nullptr, "set_stage()", kArgsParsingFailure);
545 expect(nullptr, "set_stage(\"arg1\")", kArgsParsingFailure);
546 expect(nullptr, "set_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
547
548 // Set up a local file as BCB.
549 TemporaryFile tf;
550 std::string temp_file(tf.path);
551 bootloader_message boot;
552 strlcpy(boot.command, "command", sizeof(boot.command));
553 strlcpy(boot.stage, "2/3", sizeof(boot.stage));
554 std::string err;
555 ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err));
556
557 // Write with set_stage().
558 std::string script("set_stage(\"" + temp_file + "\", \"1/3\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700559 expect(tf.path, script, kNoCause);
Tao Baobedf5fc2016-11-18 12:01:26 -0800560
561 // Verify.
562 bootloader_message boot_verify;
563 ASSERT_TRUE(read_bootloader_message_from(&boot_verify, temp_file, &err));
564
565 // Stage should be updated, with command part untouched.
566 ASSERT_STREQ("1/3", boot_verify.stage);
567 ASSERT_STREQ(boot.command, boot_verify.command);
568
569 // Bad BCB path.
570 script = "set_stage(\"doesntexist\", \"1/3\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700571 expect("", script, kNoCause);
Tao Baobedf5fc2016-11-18 12:01:26 -0800572
573 script = "set_stage(\"/dev/full\", \"1/3\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700574 expect("", script, kNoCause);
Tao Baobedf5fc2016-11-18 12:01:26 -0800575}
Tao Bao9aa7ab52017-01-05 17:27:19 -0800576
577TEST_F(UpdaterTest, set_progress) {
578 // set_progress() expects one argument.
579 expect(nullptr, "set_progress()", kArgsParsingFailure);
580 expect(nullptr, "set_progress(\"arg1\", \"arg2\")", kArgsParsingFailure);
581
582 // Invalid progress argument.
583 expect(nullptr, "set_progress(\"arg1\")", kArgsParsingFailure);
584 expect(nullptr, "set_progress(\"3x+5\")", kArgsParsingFailure);
585 expect(nullptr, "set_progress(\".3.5\")", kArgsParsingFailure);
586
587 TemporaryFile tf;
Tianjie Xu58d59122019-05-03 01:05:04 -0700588 SetUpdaterCmdPipe(tf.release());
589 expect(".52", "set_progress(\".52\")", kNoCause, &updater_);
590 FlushUpdaterCommandPipe();
Tao Bao9aa7ab52017-01-05 17:27:19 -0800591
592 std::string cmd;
593 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd));
594 ASSERT_EQ(android::base::StringPrintf("set_progress %f\n", .52), cmd);
595 // recovery-updater protocol expects 2 tokens ("set_progress <frac>").
596 ASSERT_EQ(2U, android::base::Split(cmd, " ").size());
597}
598
599TEST_F(UpdaterTest, show_progress) {
600 // show_progress() expects two arguments.
601 expect(nullptr, "show_progress()", kArgsParsingFailure);
602 expect(nullptr, "show_progress(\"arg1\")", kArgsParsingFailure);
603 expect(nullptr, "show_progress(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
604
605 // Invalid progress arguments.
606 expect(nullptr, "show_progress(\"arg1\", \"arg2\")", kArgsParsingFailure);
607 expect(nullptr, "show_progress(\"3x+5\", \"10\")", kArgsParsingFailure);
608 expect(nullptr, "show_progress(\".3\", \"5a\")", kArgsParsingFailure);
609
610 TemporaryFile tf;
Tianjie Xu58d59122019-05-03 01:05:04 -0700611 SetUpdaterCmdPipe(tf.release());
612 expect(".52", "show_progress(\".52\", \"10\")", kNoCause, &updater_);
613 FlushUpdaterCommandPipe();
Tao Bao9aa7ab52017-01-05 17:27:19 -0800614
615 std::string cmd;
616 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd));
617 ASSERT_EQ(android::base::StringPrintf("progress %f %d\n", .52, 10), cmd);
618 // recovery-updater protocol expects 3 tokens ("progress <frac> <secs>").
619 ASSERT_EQ(3U, android::base::Split(cmd, " ").size());
620}
Tianjie Xu56ebe622017-03-16 00:48:21 -0700621
Tao Baoffede3e2018-06-07 09:56:19 -0700622TEST_F(UpdaterTest, block_image_update_parsing_error) {
623 std::vector<std::string> transfer_list{
624 // clang-format off
625 "4",
626 "2",
627 "0",
628 // clang-format on
629 };
630
631 PackageEntries entries{
632 { "new_data", "" },
633 { "patch_data", "" },
634 { "transfer_list", android::base::Join(transfer_list, '\n') },
635 };
636
637 RunBlockImageUpdate(false, entries, image_file_, "", kArgsParsingFailure);
638}
639
Tao Bao3cb3c522018-11-02 13:36:58 -0700640// Generates the bsdiff of the given source and target images, and writes the result entries.
641// target_blocks specifies the block count to be written into the `bsdiff` command, which may be
642// different from the given target size in order to trigger overrun / underrun paths.
643static void GetEntriesForBsdiff(std::string_view source, std::string_view target,
644 size_t target_blocks, PackageEntries* entries) {
Tianjie Xu5450c842017-10-18 13:15:21 -0700645 // Generate the patch data.
Tianjie Xu56ebe622017-03-16 00:48:21 -0700646 TemporaryFile patch_file;
Tao Bao3cb3c522018-11-02 13:36:58 -0700647 ASSERT_EQ(0, bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(source.data()), source.size(),
648 reinterpret_cast<const uint8_t*>(target.data()), target.size(),
649 patch_file.path, nullptr));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700650 std::string patch_content;
651 ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch_content));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700652
Tianjie Xu5450c842017-10-18 13:15:21 -0700653 // Create the transfer list that contains a bsdiff.
Tao Bao3cb3c522018-11-02 13:36:58 -0700654 std::string src_hash = GetSha1(source);
655 std::string tgt_hash = GetSha1(target);
656 size_t source_blocks = source.size() / 4096;
Tao Baobc4a6d52018-05-23 00:12:21 -0700657 std::vector<std::string> transfer_list{
658 // clang-format off
Tianjie Xu56ebe622017-03-16 00:48:21 -0700659 "4",
Tao Bao3cb3c522018-11-02 13:36:58 -0700660 std::to_string(target_blocks),
Tianjie Xu56ebe622017-03-16 00:48:21 -0700661 "0",
Tao Bao3cb3c522018-11-02 13:36:58 -0700662 "0",
663 // bsdiff patch_offset patch_length source_hash target_hash target_range source_block_count
664 // source_range
665 android::base::StringPrintf("bsdiff 0 %zu %s %s 2,0,%zu %zu 2,0,%zu", patch_content.size(),
666 src_hash.c_str(), tgt_hash.c_str(), target_blocks, source_blocks,
667 source_blocks),
Tao Baobc4a6d52018-05-23 00:12:21 -0700668 // clang-format on
Tianjie Xu56ebe622017-03-16 00:48:21 -0700669 };
Tianjie Xu56ebe622017-03-16 00:48:21 -0700670
Tao Bao3cb3c522018-11-02 13:36:58 -0700671 *entries = {
Tianjie Xu5450c842017-10-18 13:15:21 -0700672 { "new_data", "" },
673 { "patch_data", patch_content },
674 { "transfer_list", android::base::Join(transfer_list, '\n') },
Tianjie Xu56ebe622017-03-16 00:48:21 -0700675 };
Tao Bao3cb3c522018-11-02 13:36:58 -0700676}
Tianjie Xu5450c842017-10-18 13:15:21 -0700677
Tao Bao3cb3c522018-11-02 13:36:58 -0700678TEST_F(UpdaterTest, block_image_update_patch_data) {
679 // Both source and target images have 10 blocks.
680 std::string source =
681 std::string(4096, 'a') + std::string(4096, 'c') + std::string(4096 * 3, '\0');
682 std::string target =
683 std::string(4096, 'b') + std::string(4096, 'd') + std::string(4096 * 3, '\0');
684 ASSERT_TRUE(android::base::WriteStringToFile(source, image_file_));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700685
Tao Bao3cb3c522018-11-02 13:36:58 -0700686 PackageEntries entries;
687 GetEntriesForBsdiff(std::string_view(source).substr(0, 4096 * 2),
688 std::string_view(target).substr(0, 4096 * 2), 2, &entries);
Tao Baobc4a6d52018-05-23 00:12:21 -0700689 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu56ebe622017-03-16 00:48:21 -0700690
Tianjie Xu56ebe622017-03-16 00:48:21 -0700691 // The update_file should be patched correctly.
Tao Bao3cb3c522018-11-02 13:36:58 -0700692 std::string updated;
693 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated));
694 ASSERT_EQ(target, updated);
695}
696
697TEST_F(UpdaterTest, block_image_update_patch_overrun) {
698 // Both source and target images have 10 blocks.
699 std::string source =
700 std::string(4096, 'a') + std::string(4096, 'c') + std::string(4096 * 3, '\0');
701 std::string target =
702 std::string(4096, 'b') + std::string(4096, 'd') + std::string(4096 * 3, '\0');
703 ASSERT_TRUE(android::base::WriteStringToFile(source, image_file_));
704
705 // Provide one less block to trigger the overrun path.
706 PackageEntries entries;
707 GetEntriesForBsdiff(std::string_view(source).substr(0, 4096 * 2),
708 std::string_view(target).substr(0, 4096 * 2), 1, &entries);
709
710 // The update should fail due to overrun.
711 RunBlockImageUpdate(false, entries, image_file_, "", kPatchApplicationFailure);
Tianjie Xu5450c842017-10-18 13:15:21 -0700712}
713
Tao Baoa2cff952018-11-02 15:44:07 -0700714TEST_F(UpdaterTest, block_image_update_patch_underrun) {
Tao Bao3cb3c522018-11-02 13:36:58 -0700715 // Both source and target images have 10 blocks.
716 std::string source =
717 std::string(4096, 'a') + std::string(4096, 'c') + std::string(4096 * 3, '\0');
718 std::string target =
719 std::string(4096, 'b') + std::string(4096, 'd') + std::string(4096 * 3, '\0');
720 ASSERT_TRUE(android::base::WriteStringToFile(source, image_file_));
Tao Baoa2cff952018-11-02 15:44:07 -0700721
Tao Bao3cb3c522018-11-02 13:36:58 -0700722 // Provide one more block to trigger the overrun path.
723 PackageEntries entries;
724 GetEntriesForBsdiff(std::string_view(source).substr(0, 4096 * 2),
725 std::string_view(target).substr(0, 4096 * 2), 3, &entries);
Tao Baoa2cff952018-11-02 15:44:07 -0700726
727 // The update should fail due to underrun.
728 RunBlockImageUpdate(false, entries, image_file_, "", kPatchApplicationFailure);
729}
730
Tianjie Xu5450c842017-10-18 13:15:21 -0700731TEST_F(UpdaterTest, block_image_update_fail) {
732 std::string src_content(4096 * 2, 'e');
Tao Bao3cb3c522018-11-02 13:36:58 -0700733 std::string src_hash = GetSha1(src_content);
Tianjie Xu5450c842017-10-18 13:15:21 -0700734 // Stash and free some blocks, then fail the update intentionally.
Tao Baobc4a6d52018-05-23 00:12:21 -0700735 std::vector<std::string> transfer_list{
736 // clang-format off
737 "4",
738 "2",
739 "0",
740 "2",
741 "stash " + src_hash + " 2,0,2",
742 "free " + src_hash,
Tao Bao91a649a2018-05-21 16:05:56 -0700743 "abort",
Tao Baobc4a6d52018-05-23 00:12:21 -0700744 // clang-format on
Tianjie Xu5450c842017-10-18 13:15:21 -0700745 };
746
747 // Add a new data of 10 bytes to test the deadlock.
Tao Baobc4a6d52018-05-23 00:12:21 -0700748 PackageEntries entries{
Tianjie Xu5450c842017-10-18 13:15:21 -0700749 { "new_data", std::string(10, 0) },
750 { "patch_data", "" },
751 { "transfer_list", android::base::Join(transfer_list, '\n') },
752 };
753
Tao Baobc4a6d52018-05-23 00:12:21 -0700754 ASSERT_TRUE(android::base::WriteStringToFile(src_content, image_file_));
Tianjie Xu5450c842017-10-18 13:15:21 -0700755
Tao Baobc4a6d52018-05-23 00:12:21 -0700756 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu5450c842017-10-18 13:15:21 -0700757
Tianjie Xu56ebe622017-03-16 00:48:21 -0700758 // Updater generates the stash name based on the input file name.
Tao Bao3cb3c522018-11-02 13:36:58 -0700759 std::string name_digest = GetSha1(image_file_);
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800760 std::string stash_base = std::string(temp_stash_base_.path) + "/" + name_digest;
Tianjie Xu56ebe622017-03-16 00:48:21 -0700761 ASSERT_EQ(0, access(stash_base.c_str(), F_OK));
Tao Baobc4a6d52018-05-23 00:12:21 -0700762 // Expect the stashed blocks to be freed.
Tianjie Xu5450c842017-10-18 13:15:21 -0700763 ASSERT_EQ(-1, access((stash_base + src_hash).c_str(), F_OK));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700764 ASSERT_EQ(0, rmdir(stash_base.c_str()));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700765}
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700766
Tianjie Xu5450c842017-10-18 13:15:21 -0700767TEST_F(UpdaterTest, new_data_over_write) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700768 std::vector<std::string> transfer_list{
769 // clang-format off
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700770 "4",
771 "1",
772 "0",
773 "0",
774 "new 2,0,1",
Tao Baobc4a6d52018-05-23 00:12:21 -0700775 // clang-format on
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700776 };
Tianjie Xu5450c842017-10-18 13:15:21 -0700777
Tao Baobc4a6d52018-05-23 00:12:21 -0700778 // Write 4096 + 100 bytes of new data.
779 PackageEntries entries{
780 { "new_data", std::string(4196, 0) },
Tianjie Xu5450c842017-10-18 13:15:21 -0700781 { "patch_data", "" },
782 { "transfer_list", android::base::Join(transfer_list, '\n') },
783 };
784
Tao Baobc4a6d52018-05-23 00:12:21 -0700785 RunBlockImageUpdate(false, entries, image_file_, "t");
786}
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700787
Tao Baobc4a6d52018-05-23 00:12:21 -0700788TEST_F(UpdaterTest, new_data_short_write) {
789 std::vector<std::string> transfer_list{
790 // clang-format off
791 "4",
792 "1",
793 "0",
794 "0",
795 "new 2,0,1",
796 // clang-format on
797 };
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700798
Tao Baobc4a6d52018-05-23 00:12:21 -0700799 PackageEntries entries{
800 { "patch_data", "" },
801 { "transfer_list", android::base::Join(transfer_list, '\n') },
802 };
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700803
804 // Updater should report the failure gracefully rather than stuck in deadlock.
Tao Baobc4a6d52018-05-23 00:12:21 -0700805 entries["new_data"] = "";
806 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700807
Tao Baobc4a6d52018-05-23 00:12:21 -0700808 entries["new_data"] = std::string(10, 'a');
809 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700810
811 // Expect to write 1 block of new data successfully.
Tao Baobc4a6d52018-05-23 00:12:21 -0700812 entries["new_data"] = std::string(4096, 'a');
813 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu107a34f2017-06-29 17:04:21 -0700814}
815
816TEST_F(UpdaterTest, brotli_new_data) {
Tianjie Xu107a34f2017-06-29 17:04:21 -0700817 auto generator = []() { return rand() % 128; };
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700818 // Generate 100 blocks of random data.
Tianjie Xu107a34f2017-06-29 17:04:21 -0700819 std::string brotli_new_data;
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700820 brotli_new_data.reserve(4096 * 100);
821 generate_n(back_inserter(brotli_new_data), 4096 * 100, generator);
Tianjie Xu107a34f2017-06-29 17:04:21 -0700822
823 size_t encoded_size = BrotliEncoderMaxCompressedSize(brotli_new_data.size());
Tianjie Xu5450c842017-10-18 13:15:21 -0700824 std::string encoded_data(encoded_size, 0);
Tianjie Xu107a34f2017-06-29 17:04:21 -0700825 ASSERT_TRUE(BrotliEncoderCompress(
826 BROTLI_DEFAULT_QUALITY, BROTLI_DEFAULT_WINDOW, BROTLI_DEFAULT_MODE, brotli_new_data.size(),
Tianjie Xu5450c842017-10-18 13:15:21 -0700827 reinterpret_cast<const uint8_t*>(brotli_new_data.data()), &encoded_size,
828 reinterpret_cast<uint8_t*>(const_cast<char*>(encoded_data.data()))));
829 encoded_data.resize(encoded_size);
Tianjie Xu107a34f2017-06-29 17:04:21 -0700830
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700831 // Write a few small chunks of new data, then a large chunk, and finally a few small chunks.
832 // This helps us to catch potential short writes.
Tianjie Xu107a34f2017-06-29 17:04:21 -0700833 std::vector<std::string> transfer_list = {
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700834 "4",
835 "100",
836 "0",
837 "0",
838 "new 2,0,1",
839 "new 2,1,2",
840 "new 4,2,50,50,97",
841 "new 2,97,98",
842 "new 2,98,99",
843 "new 2,99,100",
Tianjie Xu107a34f2017-06-29 17:04:21 -0700844 };
Tianjie Xu5450c842017-10-18 13:15:21 -0700845
Tao Baobc4a6d52018-05-23 00:12:21 -0700846 PackageEntries entries{
847 { "new_data.br", std::move(encoded_data) },
Tianjie Xu5450c842017-10-18 13:15:21 -0700848 { "patch_data", "" },
849 { "transfer_list", android::base::Join(transfer_list, '\n') },
850 };
851
Tao Baobc4a6d52018-05-23 00:12:21 -0700852 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu107a34f2017-06-29 17:04:21 -0700853
854 std::string updated_content;
Tao Baobc4a6d52018-05-23 00:12:21 -0700855 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_content));
Tianjie Xu107a34f2017-06-29 17:04:21 -0700856 ASSERT_EQ(brotli_new_data, updated_content);
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700857}
Tianjie Xu284752e2017-12-05 11:04:17 -0800858
859TEST_F(UpdaterTest, last_command_update) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700860 std::string block1(4096, '1');
861 std::string block2(4096, '2');
862 std::string block3(4096, '3');
Tao Bao3cb3c522018-11-02 13:36:58 -0700863 std::string block1_hash = GetSha1(block1);
864 std::string block2_hash = GetSha1(block2);
865 std::string block3_hash = GetSha1(block3);
Tianjie Xu284752e2017-12-05 11:04:17 -0800866
867 // Compose the transfer list to fail the first update.
Tao Baobc4a6d52018-05-23 00:12:21 -0700868 std::vector<std::string> transfer_list_fail{
869 // clang-format off
Tianjie Xu284752e2017-12-05 11:04:17 -0800870 "4",
871 "2",
872 "0",
873 "2",
874 "stash " + block1_hash + " 2,0,1",
875 "move " + block1_hash + " 2,1,2 1 2,0,1",
876 "stash " + block3_hash + " 2,2,3",
Tao Bao91a649a2018-05-21 16:05:56 -0700877 "abort",
Tao Baobc4a6d52018-05-23 00:12:21 -0700878 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -0800879 };
880
881 // Mimic a resumed update with the same transfer commands.
Tao Baobc4a6d52018-05-23 00:12:21 -0700882 std::vector<std::string> transfer_list_continue{
883 // clang-format off
Tianjie Xu284752e2017-12-05 11:04:17 -0800884 "4",
885 "2",
886 "0",
887 "2",
888 "stash " + block1_hash + " 2,0,1",
889 "move " + block1_hash + " 2,1,2 1 2,0,1",
890 "stash " + block3_hash + " 2,2,3",
891 "move " + block1_hash + " 2,2,3 1 2,0,1",
Tao Baobc4a6d52018-05-23 00:12:21 -0700892 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -0800893 };
894
Tao Baobc4a6d52018-05-23 00:12:21 -0700895 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_));
896
897 PackageEntries entries{
Tianjie Xu284752e2017-12-05 11:04:17 -0800898 { "new_data", "" },
899 { "patch_data", "" },
Tao Baobc4a6d52018-05-23 00:12:21 -0700900 { "transfer_list", android::base::Join(transfer_list_fail, '\n') },
Tianjie Xu284752e2017-12-05 11:04:17 -0800901 };
902
Tao Baobc4a6d52018-05-23 00:12:21 -0700903 // "2\nstash " + block3_hash + " 2,2,3"
Tao Baof8811bb2018-06-18 10:03:52 -0700904 std::string last_command_content =
905 "2\n" + transfer_list_fail[TransferList::kTransferListHeaderLines + 2];
Tianjie Xu284752e2017-12-05 11:04:17 -0800906
Tao Baobc4a6d52018-05-23 00:12:21 -0700907 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu284752e2017-12-05 11:04:17 -0800908
909 // Expect last_command to contain the last stash command.
Tao Baobc4a6d52018-05-23 00:12:21 -0700910 std::string last_command_actual;
Tao Bao7064aa22018-05-24 21:43:50 -0700911 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
Tao Baobc4a6d52018-05-23 00:12:21 -0700912 EXPECT_EQ(last_command_content, last_command_actual);
913
Tianjie Xu284752e2017-12-05 11:04:17 -0800914 std::string updated_contents;
Tao Baobc4a6d52018-05-23 00:12:21 -0700915 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_contents));
Tianjie Xu284752e2017-12-05 11:04:17 -0800916 ASSERT_EQ(block1 + block1 + block3, updated_contents);
917
Tao Baobc4a6d52018-05-23 00:12:21 -0700918 // "Resume" the update. Expect the first 'move' to be skipped but the second 'move' to be
919 // executed. Note that we intentionally reset the image file.
920 entries["transfer_list"] = android::base::Join(transfer_list_continue, '\n');
921 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_));
922 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu284752e2017-12-05 11:04:17 -0800923
Tao Baobc4a6d52018-05-23 00:12:21 -0700924 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_contents));
925 ASSERT_EQ(block1 + block2 + block1, updated_contents);
Tianjie Xu284752e2017-12-05 11:04:17 -0800926}
927
928TEST_F(UpdaterTest, last_command_update_unresumable) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700929 std::string block1(4096, '1');
930 std::string block2(4096, '2');
Tao Bao3cb3c522018-11-02 13:36:58 -0700931 std::string block1_hash = GetSha1(block1);
932 std::string block2_hash = GetSha1(block2);
Tianjie Xu284752e2017-12-05 11:04:17 -0800933
934 // Construct an unresumable update with source blocks mismatch.
Tao Baobc4a6d52018-05-23 00:12:21 -0700935 std::vector<std::string> transfer_list_unresumable{
936 // clang-format off
937 "4",
938 "2",
939 "0",
940 "2",
941 "stash " + block1_hash + " 2,0,1",
942 "move " + block2_hash + " 2,1,2 1 2,0,1",
943 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -0800944 };
945
Tao Baobc4a6d52018-05-23 00:12:21 -0700946 PackageEntries entries{
Tianjie Xu284752e2017-12-05 11:04:17 -0800947 { "new_data", "" },
948 { "patch_data", "" },
Tao Baobc4a6d52018-05-23 00:12:21 -0700949 { "transfer_list", android::base::Join(transfer_list_unresumable, '\n') },
Tianjie Xu284752e2017-12-05 11:04:17 -0800950 };
951
Tao Baobc4a6d52018-05-23 00:12:21 -0700952 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block1, image_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -0800953
Tao Baof8811bb2018-06-18 10:03:52 -0700954 std::string last_command_content =
955 "0\n" + transfer_list_unresumable[TransferList::kTransferListHeaderLines];
Tao Bao7064aa22018-05-24 21:43:50 -0700956 ASSERT_TRUE(android::base::WriteStringToFile(last_command_content, last_command_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -0800957
Tao Baobc4a6d52018-05-23 00:12:21 -0700958 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu284752e2017-12-05 11:04:17 -0800959
Tao Baobc4a6d52018-05-23 00:12:21 -0700960 // The last_command_file will be deleted if the update encounters an unresumable failure later.
Tao Bao7064aa22018-05-24 21:43:50 -0700961 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
Tianjie Xu284752e2017-12-05 11:04:17 -0800962}
963
964TEST_F(UpdaterTest, last_command_verify) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700965 std::string block1(4096, '1');
966 std::string block2(4096, '2');
967 std::string block3(4096, '3');
Tao Bao3cb3c522018-11-02 13:36:58 -0700968 std::string block1_hash = GetSha1(block1);
969 std::string block2_hash = GetSha1(block2);
970 std::string block3_hash = GetSha1(block3);
Tianjie Xu284752e2017-12-05 11:04:17 -0800971
Tao Baobc4a6d52018-05-23 00:12:21 -0700972 std::vector<std::string> transfer_list_verify{
973 // clang-format off
Tianjie Xu284752e2017-12-05 11:04:17 -0800974 "4",
975 "2",
976 "0",
977 "2",
978 "stash " + block1_hash + " 2,0,1",
979 "move " + block1_hash + " 2,0,1 1 2,0,1",
980 "move " + block1_hash + " 2,1,2 1 2,0,1",
981 "stash " + block3_hash + " 2,2,3",
Tao Baobc4a6d52018-05-23 00:12:21 -0700982 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -0800983 };
984
Tao Baobc4a6d52018-05-23 00:12:21 -0700985 PackageEntries entries{
Tianjie Xu284752e2017-12-05 11:04:17 -0800986 { "new_data", "" },
987 { "patch_data", "" },
Tao Baobc4a6d52018-05-23 00:12:21 -0700988 { "transfer_list", android::base::Join(transfer_list_verify, '\n') },
Tianjie Xu284752e2017-12-05 11:04:17 -0800989 };
990
Tao Baobc4a6d52018-05-23 00:12:21 -0700991 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block1 + block3, image_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -0800992
Tao Baobc4a6d52018-05-23 00:12:21 -0700993 // Last command: "move " + block1_hash + " 2,1,2 1 2,0,1"
Tao Baof8811bb2018-06-18 10:03:52 -0700994 std::string last_command_content =
995 "2\n" + transfer_list_verify[TransferList::kTransferListHeaderLines + 2];
Tianjie Xu284752e2017-12-05 11:04:17 -0800996
Tao Baobc4a6d52018-05-23 00:12:21 -0700997 // First run: expect the verification to succeed and the last_command_file is intact.
Tao Bao7064aa22018-05-24 21:43:50 -0700998 ASSERT_TRUE(android::base::WriteStringToFile(last_command_content, last_command_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -0800999
Tao Baobc4a6d52018-05-23 00:12:21 -07001000 RunBlockImageUpdate(true, entries, image_file_, "t");
Tianjie Xu284752e2017-12-05 11:04:17 -08001001
Tao Baobc4a6d52018-05-23 00:12:21 -07001002 std::string last_command_actual;
Tao Bao7064aa22018-05-24 21:43:50 -07001003 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
Tao Baobc4a6d52018-05-23 00:12:21 -07001004 EXPECT_EQ(last_command_content, last_command_actual);
Tianjie Xu284752e2017-12-05 11:04:17 -08001005
Tao Baobc4a6d52018-05-23 00:12:21 -07001006 // Second run with a mismatching block image: expect the verification to succeed but
1007 // last_command_file to be deleted; because the target blocks in the last command don't have the
1008 // expected contents for the second move command.
1009 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_));
1010 RunBlockImageUpdate(true, entries, image_file_, "t");
Tao Bao7064aa22018-05-24 21:43:50 -07001011 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
Tianjie Xu284752e2017-12-05 11:04:17 -08001012}
Tao Baoc0299ed2018-05-24 00:16:35 -07001013
Tianjie Xu58d59122019-05-03 01:05:04 -07001014class ResumableUpdaterTest : public UpdaterTestBase, public testing::TestWithParam<size_t> {
Tao Baoc0299ed2018-05-24 00:16:35 -07001015 protected:
1016 void SetUp() override {
Tianjie Xu58d59122019-05-03 01:05:04 -07001017 UpdaterTestBase::SetUp();
Tao Bao91a649a2018-05-21 16:05:56 -07001018 // Enable a special command "abort" to simulate interruption.
1019 Command::abort_allowed_ = true;
Tao Baoc0299ed2018-05-24 00:16:35 -07001020 index_ = GetParam();
Tao Baoc0299ed2018-05-24 00:16:35 -07001021 }
1022
1023 void TearDown() override {
Tianjie Xu58d59122019-05-03 01:05:04 -07001024 UpdaterTestBase::TearDown();
Tao Baoc0299ed2018-05-24 00:16:35 -07001025 }
1026
Tao Baoc0299ed2018-05-24 00:16:35 -07001027 size_t index_;
Tao Baoc0299ed2018-05-24 00:16:35 -07001028};
1029
1030static std::string g_source_image;
1031static std::string g_target_image;
1032static PackageEntries g_entries;
1033
1034static std::vector<std::string> GenerateTransferList() {
1035 std::string a(4096, 'a');
1036 std::string b(4096, 'b');
1037 std::string c(4096, 'c');
1038 std::string d(4096, 'd');
1039 std::string e(4096, 'e');
1040 std::string f(4096, 'f');
1041 std::string g(4096, 'g');
1042 std::string h(4096, 'h');
1043 std::string i(4096, 'i');
1044 std::string zero(4096, '\0');
1045
Tao Bao3cb3c522018-11-02 13:36:58 -07001046 std::string a_hash = GetSha1(a);
1047 std::string b_hash = GetSha1(b);
1048 std::string c_hash = GetSha1(c);
1049 std::string e_hash = GetSha1(e);
Tao Baoc0299ed2018-05-24 00:16:35 -07001050
1051 auto loc = [](const std::string& range_text) {
1052 std::vector<std::string> pieces = android::base::Split(range_text, "-");
1053 size_t left;
1054 size_t right;
1055 if (pieces.size() == 1) {
1056 CHECK(android::base::ParseUint(pieces[0], &left));
1057 right = left + 1;
1058 } else {
1059 CHECK_EQ(2u, pieces.size());
1060 CHECK(android::base::ParseUint(pieces[0], &left));
1061 CHECK(android::base::ParseUint(pieces[1], &right));
1062 right++;
1063 }
1064 return android::base::StringPrintf("2,%zu,%zu", left, right);
1065 };
1066
1067 // patch 1: "b d c" -> "g"
1068 TemporaryFile patch_file_bdc_g;
1069 std::string bdc = b + d + c;
Tao Bao3cb3c522018-11-02 13:36:58 -07001070 std::string bdc_hash = GetSha1(bdc);
1071 std::string g_hash = GetSha1(g);
Tao Baoc0299ed2018-05-24 00:16:35 -07001072 CHECK_EQ(0, bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(bdc.data()), bdc.size(),
1073 reinterpret_cast<const uint8_t*>(g.data()), g.size(),
1074 patch_file_bdc_g.path, nullptr));
1075 std::string patch_bdc_g;
1076 CHECK(android::base::ReadFileToString(patch_file_bdc_g.path, &patch_bdc_g));
1077
1078 // patch 2: "a b c d" -> "d c b"
1079 TemporaryFile patch_file_abcd_dcb;
1080 std::string abcd = a + b + c + d;
Tao Bao3cb3c522018-11-02 13:36:58 -07001081 std::string abcd_hash = GetSha1(abcd);
Tao Baoc0299ed2018-05-24 00:16:35 -07001082 std::string dcb = d + c + b;
Tao Bao3cb3c522018-11-02 13:36:58 -07001083 std::string dcb_hash = GetSha1(dcb);
Tao Baoc0299ed2018-05-24 00:16:35 -07001084 CHECK_EQ(0, bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(abcd.data()), abcd.size(),
1085 reinterpret_cast<const uint8_t*>(dcb.data()), dcb.size(),
1086 patch_file_abcd_dcb.path, nullptr));
1087 std::string patch_abcd_dcb;
1088 CHECK(android::base::ReadFileToString(patch_file_abcd_dcb.path, &patch_abcd_dcb));
1089
1090 std::vector<std::string> transfer_list{
1091 "4",
1092 "10", // total blocks written
1093 "2", // maximum stash entries
1094 "2", // maximum number of stashed blocks
1095
1096 // a b c d e a b c d e
1097 "stash " + b_hash + " " + loc("1"),
1098 // a b c d e a b c d e [b(1)]
1099 "stash " + c_hash + " " + loc("2"),
1100 // a b c d e a b c d e [b(1)][c(2)]
1101 "new " + loc("1-2"),
1102 // a i h d e a b c d e [b(1)][c(2)]
1103 "zero " + loc("0"),
1104 // 0 i h d e a b c d e [b(1)][c(2)]
1105
1106 // bsdiff "b d c" (from stash, 3, stash) to get g(3)
1107 android::base::StringPrintf(
1108 "bsdiff 0 %zu %s %s %s 3 %s %s %s:%s %s:%s",
1109 patch_bdc_g.size(), // patch start (0), patch length
1110 bdc_hash.c_str(), // source hash
1111 g_hash.c_str(), // target hash
1112 loc("3").c_str(), // target range
1113 loc("3").c_str(), loc("1").c_str(), // load "d" from block 3, into buffer at offset 1
1114 b_hash.c_str(), loc("0").c_str(), // load "b" from stash, into buffer at offset 0
1115 c_hash.c_str(), loc("2").c_str()), // load "c" from stash, into buffer at offset 2
1116
1117 // 0 i h g e a b c d e [b(1)][c(2)]
1118 "free " + b_hash,
1119 // 0 i h g e a b c d e [c(2)]
1120 "free " + a_hash,
1121 // 0 i h g e a b c d e
1122 "stash " + a_hash + " " + loc("5"),
1123 // 0 i h g e a b c d e [a(5)]
1124 "move " + e_hash + " " + loc("5") + " 1 " + loc("4"),
1125 // 0 i h g e e b c d e [a(5)]
1126
1127 // bsdiff "a b c d" (from stash, 6-8) to "d c b" (6-8)
1128 android::base::StringPrintf( //
1129 "bsdiff %zu %zu %s %s %s 4 %s %s %s:%s",
1130 patch_bdc_g.size(), // patch start
1131 patch_bdc_g.size() + patch_abcd_dcb.size(), // patch length
1132 abcd_hash.c_str(), // source hash
1133 dcb_hash.c_str(), // target hash
1134 loc("6-8").c_str(), // target range
1135 loc("6-8").c_str(), // load "b c d" from blocks 6-8
1136 loc("1-3").c_str(), // into buffer at offset 1-3
1137 a_hash.c_str(), // load "a" from stash
1138 loc("0").c_str()), // into buffer at offset 0
1139
1140 // 0 i h g e e d c b e [a(5)]
1141 "new " + loc("4"),
1142 // 0 i h g f e d c b e [a(5)]
1143 "move " + a_hash + " " + loc("9") + " 1 - " + a_hash + ":" + loc("0"),
1144 // 0 i h g f e d c b a [a(5)]
1145 "free " + a_hash,
1146 // 0 i h g f e d c b a
1147 };
1148
1149 std::string new_data = i + h + f;
1150 std::string patch_data = patch_bdc_g + patch_abcd_dcb;
1151
1152 g_entries = {
1153 { "new_data", new_data },
1154 { "patch_data", patch_data },
1155 };
1156 g_source_image = a + b + c + d + e + a + b + c + d + e;
1157 g_target_image = zero + i + h + g + f + e + d + c + b + a;
1158
1159 return transfer_list;
1160}
1161
1162static const std::vector<std::string> g_transfer_list = GenerateTransferList();
1163
1164INSTANTIATE_TEST_CASE_P(InterruptAfterEachCommand, ResumableUpdaterTest,
1165 ::testing::Range(static_cast<size_t>(0),
Tao Baof8811bb2018-06-18 10:03:52 -07001166 g_transfer_list.size() -
1167 TransferList::kTransferListHeaderLines));
Tao Baoc0299ed2018-05-24 00:16:35 -07001168
1169TEST_P(ResumableUpdaterTest, InterruptVerifyResume) {
1170 ASSERT_TRUE(android::base::WriteStringToFile(g_source_image, image_file_));
1171
1172 LOG(INFO) << "Interrupting at line " << index_ << " ("
Tao Baof8811bb2018-06-18 10:03:52 -07001173 << g_transfer_list[TransferList::kTransferListHeaderLines + index_] << ")";
Tao Baoc0299ed2018-05-24 00:16:35 -07001174
1175 std::vector<std::string> transfer_list_copy{ g_transfer_list };
Tao Baof8811bb2018-06-18 10:03:52 -07001176 transfer_list_copy[TransferList::kTransferListHeaderLines + index_] = "abort";
Tao Baoc0299ed2018-05-24 00:16:35 -07001177
1178 g_entries["transfer_list"] = android::base::Join(transfer_list_copy, '\n');
1179
1180 // Run update that's expected to fail.
1181 RunBlockImageUpdate(false, g_entries, image_file_, "");
1182
1183 std::string last_command_expected;
1184
1185 // Assert the last_command_file.
1186 if (index_ == 0) {
1187 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
1188 } else {
Tao Baof8811bb2018-06-18 10:03:52 -07001189 last_command_expected = std::to_string(index_ - 1) + "\n" +
1190 g_transfer_list[TransferList::kTransferListHeaderLines + index_ - 1];
Tao Baoc0299ed2018-05-24 00:16:35 -07001191 std::string last_command_actual;
1192 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
1193 ASSERT_EQ(last_command_expected, last_command_actual);
1194 }
1195
1196 g_entries["transfer_list"] = android::base::Join(g_transfer_list, '\n');
1197
1198 // Resume the interrupted update, by doing verification first.
1199 RunBlockImageUpdate(true, g_entries, image_file_, "t");
1200
1201 // last_command_file should remain intact.
1202 if (index_ == 0) {
1203 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
1204 } else {
1205 std::string last_command_actual;
1206 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
1207 ASSERT_EQ(last_command_expected, last_command_actual);
1208 }
1209
1210 // Resume the update.
1211 RunBlockImageUpdate(false, g_entries, image_file_, "t");
1212
1213 // last_command_file should be gone after successful update.
1214 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
1215
1216 std::string updated_image_actual;
1217 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_image_actual));
1218 ASSERT_EQ(g_target_image, updated_image_actual);
1219}