blob: 32fec3808563c3651ec393375b28889704b39ac7 [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 Bao51d516e2016-11-03 14:49:01 -070036#include <android-base/test_utils.h>
Tao Baobedf5fc2016-11-18 12:01:26 -080037#include <bootloader_message/bootloader_message.h>
Tianjie Xu107a34f2017-06-29 17:04:21 -070038#include <brotli/encode.h>
Alex Deymofa188262017-10-10 17:56:17 +020039#include <bsdiff/bsdiff.h>
Tao Bao0c7839a2016-10-10 15:48:37 -070040#include <gtest/gtest.h>
Tianjie Xu69ffa152018-08-01 16:40:00 -070041#include <verity/hash_tree_builder.h>
Tao Baoef0eb3b2016-11-14 21:29:52 -080042#include <ziparchive/zip_archive.h>
Tianjie Xu56ebe622017-03-16 00:48:21 -070043#include <ziparchive/zip_writer.h>
Tao Bao0c7839a2016-10-10 15:48:37 -070044
Tao Bao5609bc82018-06-20 00:30:48 -070045#include "applypatch/applypatch.h"
Tao Baoef0eb3b2016-11-14 21:29:52 -080046#include "common/test_constants.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070047#include "edify/expr.h"
Tao Bao1fc5bf32017-10-06 07:43:41 -070048#include "otautil/error_code.h"
Tao Bao641fa972018-04-25 18:59:40 -070049#include "otautil/paths.h"
Tao Bao09e468f2017-09-29 14:39:33 -070050#include "otautil/print_sha1.h"
Tao Bao17054c02018-05-03 22:41:23 -070051#include "otautil/sysutil.h"
Tao Bao91a649a2018-05-21 16:05:56 -070052#include "private/commands.h"
Tianjie Xu56ebe622017-03-16 00:48:21 -070053#include "updater/blockimg.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070054#include "updater/install.h"
Tao Baoef0eb3b2016-11-14 21:29:52 -080055#include "updater/updater.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070056
Tao Baobafd6c72018-07-09 15:08:50 -070057using namespace std::string_literals;
58
Tao Baobc4a6d52018-05-23 00:12:21 -070059using PackageEntries = std::unordered_map<std::string, std::string>;
60
Tao Baobc4a6d52018-05-23 00:12:21 -070061struct 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
Tao Bao3cb3c522018-11-02 13:36:58 -0700138static std::string GetSha1(std::string_view content) {
Tianjie Xu56ebe622017-03-16 00:48:21 -0700139 uint8_t digest[SHA_DIGEST_LENGTH];
Tao Bao3cb3c522018-11-02 13:36:58 -0700140 SHA1(reinterpret_cast<const uint8_t*>(content.data()), content.size(), digest);
Tianjie Xu56ebe622017-03-16 00:48:21 -0700141 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 };
Tao Bao3cb3c522018-11-02 13:36:58 -0700191 updated_marker += "/" + GetSha1(image_temp_file_.path) + ".UPDATED";
Tao Baobc4a6d52018-05-23 00:12:21 -0700192 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 Bao5609bc82018-06-20 00:30:48 -0700219TEST_F(UpdaterTest, patch_partition_check) {
220 // Zero argument is not valid.
221 expect(nullptr, "patch_partition_check()", kArgsParsingFailure);
Tao Baodb56eb02017-03-23 06:34:20 -0700222
Tao Bao5609bc82018-06-20 00:30:48 -0700223 std::string source_file = from_testdata_base("boot.img");
224 std::string source_content;
225 ASSERT_TRUE(android::base::ReadFileToString(source_file, &source_content));
226 size_t source_size = source_content.size();
Tao Bao3cb3c522018-11-02 13:36:58 -0700227 std::string source_hash = GetSha1(source_content);
Tao Bao5609bc82018-06-20 00:30:48 -0700228 Partition source(source_file, source_size, source_hash);
Tao Baodb56eb02017-03-23 06:34:20 -0700229
Tao Bao5609bc82018-06-20 00:30:48 -0700230 std::string target_file = from_testdata_base("recovery.img");
231 std::string target_content;
232 ASSERT_TRUE(android::base::ReadFileToString(target_file, &target_content));
233 size_t target_size = target_content.size();
Tao Bao3cb3c522018-11-02 13:36:58 -0700234 std::string target_hash = GetSha1(target_content);
Tao Bao5609bc82018-06-20 00:30:48 -0700235 Partition target(target_file, target_size, target_hash);
Tao Baodb56eb02017-03-23 06:34:20 -0700236
Tao Bao5609bc82018-06-20 00:30:48 -0700237 // One argument is not valid.
238 expect(nullptr, "patch_partition_check(\"" + source.ToString() + "\")", kArgsParsingFailure);
239 expect(nullptr, "patch_partition_check(\"" + target.ToString() + "\")", kArgsParsingFailure);
240
241 // Both of the source and target have the desired checksum.
242 std::string cmd =
243 "patch_partition_check(\"" + source.ToString() + "\", \"" + target.ToString() + "\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700244 expect("t", cmd, kNoCause);
Tao Baodb56eb02017-03-23 06:34:20 -0700245
Tao Bao5609bc82018-06-20 00:30:48 -0700246 // Only source partition has the desired checksum.
247 Partition bad_target(target_file, target_size - 1, target_hash);
248 cmd = "patch_partition_check(\"" + source.ToString() + "\", \"" + bad_target.ToString() + "\")";
249 expect("t", cmd, kNoCause);
250
251 // Only target partition has the desired checksum.
252 Partition bad_source(source_file, source_size + 1, source_hash);
253 cmd = "patch_partition_check(\"" + bad_source.ToString() + "\", \"" + target.ToString() + "\")";
254 expect("t", cmd, kNoCause);
255
256 // Neither of the source or target has the desired checksum.
257 cmd =
258 "patch_partition_check(\"" + bad_source.ToString() + "\", \"" + bad_target.ToString() + "\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700259 expect("", cmd, kNoCause);
Tao Baodb56eb02017-03-23 06:34:20 -0700260}
261
Tao Bao51d516e2016-11-03 14:49:01 -0700262TEST_F(UpdaterTest, file_getprop) {
263 // file_getprop() expects two arguments.
264 expect(nullptr, "file_getprop()", kArgsParsingFailure);
265 expect(nullptr, "file_getprop(\"arg1\")", kArgsParsingFailure);
266 expect(nullptr, "file_getprop(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
267
268 // File doesn't exist.
Tianjie Xu22f11202018-08-27 10:50:31 -0700269 expect(nullptr, "file_getprop(\"/doesntexist\", \"key1\")", kFreadFailure);
Tao Bao51d516e2016-11-03 14:49:01 -0700270
271 // Reject too large files (current limit = 65536).
272 TemporaryFile temp_file1;
273 std::string buffer(65540, '\0');
274 ASSERT_TRUE(android::base::WriteStringToFile(buffer, temp_file1.path));
275
276 // Read some keys.
277 TemporaryFile temp_file2;
278 std::string content("ro.product.name=tardis\n"
279 "# comment\n\n\n"
280 "ro.product.model\n"
281 "ro.product.board = magic \n");
282 ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file2.path));
283
284 std::string script1("file_getprop(\"" + std::string(temp_file2.path) +
285 "\", \"ro.product.name\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700286 expect("tardis", script1, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700287
288 std::string script2("file_getprop(\"" + std::string(temp_file2.path) +
289 "\", \"ro.product.board\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700290 expect("magic", script2, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700291
292 // No match.
293 std::string script3("file_getprop(\"" + std::string(temp_file2.path) +
294 "\", \"ro.product.wrong\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700295 expect("", script3, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700296
297 std::string script4("file_getprop(\"" + std::string(temp_file2.path) +
298 "\", \"ro.product.name=\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700299 expect("", script4, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700300
301 std::string script5("file_getprop(\"" + std::string(temp_file2.path) +
302 "\", \"ro.product.nam\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700303 expect("", script5, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700304
305 std::string script6("file_getprop(\"" + std::string(temp_file2.path) +
306 "\", \"ro.product.model\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700307 expect("", script6, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700308}
Tao Bao0831d0b2016-11-03 23:25:04 -0700309
Tao Baoef0eb3b2016-11-14 21:29:52 -0800310// TODO: Test extracting to block device.
311TEST_F(UpdaterTest, package_extract_file) {
312 // package_extract_file expects 1 or 2 arguments.
313 expect(nullptr, "package_extract_file()", kArgsParsingFailure);
314 expect(nullptr, "package_extract_file(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
315
316 std::string zip_path = from_testdata_base("ziptest_valid.zip");
317 ZipArchiveHandle handle;
318 ASSERT_EQ(0, OpenArchive(zip_path.c_str(), &handle));
319
320 // Need to set up the ziphandle.
321 UpdaterInfo updater_info;
322 updater_info.package_zip = handle;
323
324 // Two-argument version.
325 TemporaryFile temp_file1;
326 std::string script("package_extract_file(\"a.txt\", \"" + std::string(temp_file1.path) + "\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700327 expect("t", script, kNoCause, &updater_info);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800328
329 // Verify the extracted entry.
330 std::string data;
331 ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data));
332 ASSERT_EQ(kATxtContents, data);
333
334 // Now extract another entry to the same location, which should overwrite.
335 script = "package_extract_file(\"b.txt\", \"" + std::string(temp_file1.path) + "\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700336 expect("t", script, kNoCause, &updater_info);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800337
338 ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data));
339 ASSERT_EQ(kBTxtContents, data);
340
341 // Missing zip entry. The two-argument version doesn't abort.
342 script = "package_extract_file(\"doesntexist\", \"" + std::string(temp_file1.path) + "\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700343 expect("", script, kNoCause, &updater_info);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800344
345 // Extract to /dev/full should fail.
346 script = "package_extract_file(\"a.txt\", \"/dev/full\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700347 expect("", script, kNoCause, &updater_info);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800348
Tao Bao0b58e9a2018-07-06 15:01:05 -0700349 // One-argument version. package_extract_file() gives a VAL_BLOB, which needs to be converted to
350 // VAL_STRING for equality test.
351 script = "blob_to_string(package_extract_file(\"a.txt\")) == \"" + kATxtContents + "\"";
Tao Baod8d514f2018-07-09 13:32:28 -0700352 expect("t", script, kNoCause, &updater_info);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800353
Tao Bao0b58e9a2018-07-06 15:01:05 -0700354 script = "blob_to_string(package_extract_file(\"b.txt\")) == \"" + kBTxtContents + "\"";
Tao Baod8d514f2018-07-09 13:32:28 -0700355 expect("t", script, kNoCause, &updater_info);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800356
357 // Missing entry. The one-argument version aborts the evaluation.
358 script = "package_extract_file(\"doesntexist\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700359 expect(nullptr, script, kPackageExtractFileFailure, &updater_info);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800360
361 CloseArchive(handle);
362}
Tao Baod0f30882016-11-03 23:52:01 -0700363
Tao Baobafd6c72018-07-09 15:08:50 -0700364TEST_F(UpdaterTest, read_file) {
365 // read_file() expects one argument.
366 expect(nullptr, "read_file()", kArgsParsingFailure);
367 expect(nullptr, "read_file(\"arg1\", \"arg2\")", kArgsParsingFailure);
368
369 // Write some value to file and read back.
370 TemporaryFile temp_file;
371 std::string script("write_value(\"foo\", \""s + temp_file.path + "\");");
372 expect("t", script, kNoCause);
373
374 script = "read_file(\""s + temp_file.path + "\") == \"foo\"";
375 expect("t", script, kNoCause);
376
377 script = "read_file(\""s + temp_file.path + "\") == \"bar\"";
378 expect("", script, kNoCause);
379
380 // It should fail gracefully when read fails.
381 script = "read_file(\"/doesntexist\")";
382 expect("", script, kNoCause);
383}
384
Tianjie Xu69ffa152018-08-01 16:40:00 -0700385TEST_F(UpdaterTest, compute_hash_tree_smoke) {
386 std::string data;
387 for (unsigned char i = 0; i < 128; i++) {
388 data += std::string(4096, i);
389 }
390 // Appends an additional block for verity data.
391 data += std::string(4096, 0);
392 ASSERT_EQ(129 * 4096, data.size());
393 ASSERT_TRUE(android::base::WriteStringToFile(data, image_file_));
394
395 std::string salt = "aee087a5be3b982978c923f566a94613496b417f2af592639bc80d141e34dfe7";
396 std::string expected_root_hash =
397 "7e0a8d8747f54384014ab996f5b2dc4eb7ff00c630eede7134c9e3f05c0dd8ca";
398 // hash_tree_ranges, source_ranges, hash_algorithm, salt_hex, root_hash
399 std::vector<std::string> tokens{ "compute_hash_tree", "2,128,129", "2,0,128", "sha256", salt,
400 expected_root_hash };
401 std::string hash_tree_command = android::base::Join(tokens, " ");
402
403 std::vector<std::string> transfer_list{
404 "4", "2", "0", "2", hash_tree_command,
405 };
406
407 PackageEntries entries{
408 { "new_data", "" },
409 { "patch_data", "" },
410 { "transfer_list", android::base::Join(transfer_list, "\n") },
411 };
412
413 RunBlockImageUpdate(false, entries, image_file_, "t");
414
415 std::string updated;
416 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated));
417 ASSERT_EQ(129 * 4096, updated.size());
418 ASSERT_EQ(data.substr(0, 128 * 4096), updated.substr(0, 128 * 4096));
419
420 // Computes the SHA256 of the salt + hash_tree_data and expects the result to match with the
421 // root_hash.
422 std::vector<unsigned char> salt_bytes;
423 ASSERT_TRUE(HashTreeBuilder::ParseBytesArrayFromString(salt, &salt_bytes));
424 std::vector<unsigned char> hash_tree = std::move(salt_bytes);
425 hash_tree.insert(hash_tree.end(), updated.begin() + 128 * 4096, updated.end());
426
427 std::vector<unsigned char> digest(SHA256_DIGEST_LENGTH);
428 SHA256(hash_tree.data(), hash_tree.size(), digest.data());
429 ASSERT_EQ(expected_root_hash, HashTreeBuilder::BytesArrayToString(digest));
430}
431
432TEST_F(UpdaterTest, compute_hash_tree_root_mismatch) {
433 std::string data;
434 for (size_t i = 0; i < 128; i++) {
435 data += std::string(4096, i);
436 }
437 // Appends an additional block for verity data.
438 data += std::string(4096, 0);
439 ASSERT_EQ(129 * 4096, data.size());
440 // Corrupts one bit
441 data[4096] = 'A';
442 ASSERT_TRUE(android::base::WriteStringToFile(data, image_file_));
443
444 std::string salt = "aee087a5be3b982978c923f566a94613496b417f2af592639bc80d141e34dfe7";
445 std::string expected_root_hash =
446 "7e0a8d8747f54384014ab996f5b2dc4eb7ff00c630eede7134c9e3f05c0dd8ca";
447 // hash_tree_ranges, source_ranges, hash_algorithm, salt_hex, root_hash
448 std::vector<std::string> tokens{ "compute_hash_tree", "2,128,129", "2,0,128", "sha256", salt,
449 expected_root_hash };
450 std::string hash_tree_command = android::base::Join(tokens, " ");
451
452 std::vector<std::string> transfer_list{
453 "4", "2", "0", "2", hash_tree_command,
454 };
455
456 PackageEntries entries{
457 { "new_data", "" },
458 { "patch_data", "" },
459 { "transfer_list", android::base::Join(transfer_list, "\n") },
460 };
461
462 RunBlockImageUpdate(false, entries, image_file_, "", kHashTreeComputationFailure);
463}
464
Tao Baod0f30882016-11-03 23:52:01 -0700465TEST_F(UpdaterTest, write_value) {
466 // write_value() expects two arguments.
467 expect(nullptr, "write_value()", kArgsParsingFailure);
468 expect(nullptr, "write_value(\"arg1\")", kArgsParsingFailure);
469 expect(nullptr, "write_value(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
470
471 // filename cannot be empty.
472 expect(nullptr, "write_value(\"value\", \"\")", kArgsParsingFailure);
473
474 // Write some value to file.
475 TemporaryFile temp_file;
476 std::string value = "magicvalue";
477 std::string script("write_value(\"" + value + "\", \"" + std::string(temp_file.path) + "\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700478 expect("t", script, kNoCause);
Tao Baod0f30882016-11-03 23:52:01 -0700479
480 // Verify the content.
481 std::string content;
482 ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content));
483 ASSERT_EQ(value, content);
484
485 // Allow writing empty string.
486 script = "write_value(\"\", \"" + std::string(temp_file.path) + "\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700487 expect("t", script, kNoCause);
Tao Baod0f30882016-11-03 23:52:01 -0700488
489 // Verify the content.
490 ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content));
491 ASSERT_EQ("", content);
492
493 // It should fail gracefully when write fails.
494 script = "write_value(\"value\", \"/proc/0/file1\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700495 expect("", script, kNoCause);
Tao Baod0f30882016-11-03 23:52:01 -0700496}
Tao Baobedf5fc2016-11-18 12:01:26 -0800497
498TEST_F(UpdaterTest, get_stage) {
499 // get_stage() expects one argument.
500 expect(nullptr, "get_stage()", kArgsParsingFailure);
501 expect(nullptr, "get_stage(\"arg1\", \"arg2\")", kArgsParsingFailure);
502 expect(nullptr, "get_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
503
504 // Set up a local file as BCB.
505 TemporaryFile tf;
506 std::string temp_file(tf.path);
507 bootloader_message boot;
508 strlcpy(boot.stage, "2/3", sizeof(boot.stage));
509 std::string err;
510 ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err));
511
512 // Can read the stage value.
513 std::string script("get_stage(\"" + temp_file + "\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700514 expect("2/3", script, kNoCause);
Tao Baobedf5fc2016-11-18 12:01:26 -0800515
516 // Bad BCB path.
517 script = "get_stage(\"doesntexist\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700518 expect("", script, kNoCause);
Tao Baobedf5fc2016-11-18 12:01:26 -0800519}
520
521TEST_F(UpdaterTest, set_stage) {
522 // set_stage() expects two arguments.
523 expect(nullptr, "set_stage()", kArgsParsingFailure);
524 expect(nullptr, "set_stage(\"arg1\")", kArgsParsingFailure);
525 expect(nullptr, "set_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
526
527 // Set up a local file as BCB.
528 TemporaryFile tf;
529 std::string temp_file(tf.path);
530 bootloader_message boot;
531 strlcpy(boot.command, "command", sizeof(boot.command));
532 strlcpy(boot.stage, "2/3", sizeof(boot.stage));
533 std::string err;
534 ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err));
535
536 // Write with set_stage().
537 std::string script("set_stage(\"" + temp_file + "\", \"1/3\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700538 expect(tf.path, script, kNoCause);
Tao Baobedf5fc2016-11-18 12:01:26 -0800539
540 // Verify.
541 bootloader_message boot_verify;
542 ASSERT_TRUE(read_bootloader_message_from(&boot_verify, temp_file, &err));
543
544 // Stage should be updated, with command part untouched.
545 ASSERT_STREQ("1/3", boot_verify.stage);
546 ASSERT_STREQ(boot.command, boot_verify.command);
547
548 // Bad BCB path.
549 script = "set_stage(\"doesntexist\", \"1/3\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700550 expect("", script, kNoCause);
Tao Baobedf5fc2016-11-18 12:01:26 -0800551
552 script = "set_stage(\"/dev/full\", \"1/3\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700553 expect("", script, kNoCause);
Tao Baobedf5fc2016-11-18 12:01:26 -0800554}
Tao Bao9aa7ab52017-01-05 17:27:19 -0800555
556TEST_F(UpdaterTest, set_progress) {
557 // set_progress() expects one argument.
558 expect(nullptr, "set_progress()", kArgsParsingFailure);
559 expect(nullptr, "set_progress(\"arg1\", \"arg2\")", kArgsParsingFailure);
560
561 // Invalid progress argument.
562 expect(nullptr, "set_progress(\"arg1\")", kArgsParsingFailure);
563 expect(nullptr, "set_progress(\"3x+5\")", kArgsParsingFailure);
564 expect(nullptr, "set_progress(\".3.5\")", kArgsParsingFailure);
565
566 TemporaryFile tf;
567 UpdaterInfo updater_info;
Tianjie Xu79327ac2017-09-08 17:09:10 -0700568 updater_info.cmd_pipe = fdopen(tf.release(), "w");
Tao Bao9aa7ab52017-01-05 17:27:19 -0800569 expect(".52", "set_progress(\".52\")", kNoCause, &updater_info);
570 fflush(updater_info.cmd_pipe);
571
572 std::string cmd;
573 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd));
574 ASSERT_EQ(android::base::StringPrintf("set_progress %f\n", .52), cmd);
575 // recovery-updater protocol expects 2 tokens ("set_progress <frac>").
576 ASSERT_EQ(2U, android::base::Split(cmd, " ").size());
Tianjie Xu79327ac2017-09-08 17:09:10 -0700577 ASSERT_EQ(0, fclose(updater_info.cmd_pipe));
Tao Bao9aa7ab52017-01-05 17:27:19 -0800578}
579
580TEST_F(UpdaterTest, show_progress) {
581 // show_progress() expects two arguments.
582 expect(nullptr, "show_progress()", kArgsParsingFailure);
583 expect(nullptr, "show_progress(\"arg1\")", kArgsParsingFailure);
584 expect(nullptr, "show_progress(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
585
586 // Invalid progress arguments.
587 expect(nullptr, "show_progress(\"arg1\", \"arg2\")", kArgsParsingFailure);
588 expect(nullptr, "show_progress(\"3x+5\", \"10\")", kArgsParsingFailure);
589 expect(nullptr, "show_progress(\".3\", \"5a\")", kArgsParsingFailure);
590
591 TemporaryFile tf;
592 UpdaterInfo updater_info;
Tianjie Xu79327ac2017-09-08 17:09:10 -0700593 updater_info.cmd_pipe = fdopen(tf.release(), "w");
Tao Bao9aa7ab52017-01-05 17:27:19 -0800594 expect(".52", "show_progress(\".52\", \"10\")", kNoCause, &updater_info);
595 fflush(updater_info.cmd_pipe);
596
597 std::string cmd;
598 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd));
599 ASSERT_EQ(android::base::StringPrintf("progress %f %d\n", .52, 10), cmd);
600 // recovery-updater protocol expects 3 tokens ("progress <frac> <secs>").
601 ASSERT_EQ(3U, android::base::Split(cmd, " ").size());
Tianjie Xu79327ac2017-09-08 17:09:10 -0700602 ASSERT_EQ(0, fclose(updater_info.cmd_pipe));
Tao Bao9aa7ab52017-01-05 17:27:19 -0800603}
Tianjie Xu56ebe622017-03-16 00:48:21 -0700604
Tao Baoffede3e2018-06-07 09:56:19 -0700605TEST_F(UpdaterTest, block_image_update_parsing_error) {
606 std::vector<std::string> transfer_list{
607 // clang-format off
608 "4",
609 "2",
610 "0",
611 // clang-format on
612 };
613
614 PackageEntries entries{
615 { "new_data", "" },
616 { "patch_data", "" },
617 { "transfer_list", android::base::Join(transfer_list, '\n') },
618 };
619
620 RunBlockImageUpdate(false, entries, image_file_, "", kArgsParsingFailure);
621}
622
Tao Bao3cb3c522018-11-02 13:36:58 -0700623// Generates the bsdiff of the given source and target images, and writes the result entries.
624// target_blocks specifies the block count to be written into the `bsdiff` command, which may be
625// different from the given target size in order to trigger overrun / underrun paths.
626static void GetEntriesForBsdiff(std::string_view source, std::string_view target,
627 size_t target_blocks, PackageEntries* entries) {
Tianjie Xu5450c842017-10-18 13:15:21 -0700628 // Generate the patch data.
Tianjie Xu56ebe622017-03-16 00:48:21 -0700629 TemporaryFile patch_file;
Tao Bao3cb3c522018-11-02 13:36:58 -0700630 ASSERT_EQ(0, bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(source.data()), source.size(),
631 reinterpret_cast<const uint8_t*>(target.data()), target.size(),
632 patch_file.path, nullptr));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700633 std::string patch_content;
634 ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch_content));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700635
Tianjie Xu5450c842017-10-18 13:15:21 -0700636 // Create the transfer list that contains a bsdiff.
Tao Bao3cb3c522018-11-02 13:36:58 -0700637 std::string src_hash = GetSha1(source);
638 std::string tgt_hash = GetSha1(target);
639 size_t source_blocks = source.size() / 4096;
Tao Baobc4a6d52018-05-23 00:12:21 -0700640 std::vector<std::string> transfer_list{
641 // clang-format off
Tianjie Xu56ebe622017-03-16 00:48:21 -0700642 "4",
Tao Bao3cb3c522018-11-02 13:36:58 -0700643 std::to_string(target_blocks),
Tianjie Xu56ebe622017-03-16 00:48:21 -0700644 "0",
Tao Bao3cb3c522018-11-02 13:36:58 -0700645 "0",
646 // bsdiff patch_offset patch_length source_hash target_hash target_range source_block_count
647 // source_range
648 android::base::StringPrintf("bsdiff 0 %zu %s %s 2,0,%zu %zu 2,0,%zu", patch_content.size(),
649 src_hash.c_str(), tgt_hash.c_str(), target_blocks, source_blocks,
650 source_blocks),
Tao Baobc4a6d52018-05-23 00:12:21 -0700651 // clang-format on
Tianjie Xu56ebe622017-03-16 00:48:21 -0700652 };
Tianjie Xu56ebe622017-03-16 00:48:21 -0700653
Tao Bao3cb3c522018-11-02 13:36:58 -0700654 *entries = {
Tianjie Xu5450c842017-10-18 13:15:21 -0700655 { "new_data", "" },
656 { "patch_data", patch_content },
657 { "transfer_list", android::base::Join(transfer_list, '\n') },
Tianjie Xu56ebe622017-03-16 00:48:21 -0700658 };
Tao Bao3cb3c522018-11-02 13:36:58 -0700659}
Tianjie Xu5450c842017-10-18 13:15:21 -0700660
Tao Bao3cb3c522018-11-02 13:36:58 -0700661TEST_F(UpdaterTest, block_image_update_patch_data) {
662 // Both source and target images have 10 blocks.
663 std::string source =
664 std::string(4096, 'a') + std::string(4096, 'c') + std::string(4096 * 3, '\0');
665 std::string target =
666 std::string(4096, 'b') + std::string(4096, 'd') + std::string(4096 * 3, '\0');
667 ASSERT_TRUE(android::base::WriteStringToFile(source, image_file_));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700668
Tao Bao3cb3c522018-11-02 13:36:58 -0700669 PackageEntries entries;
670 GetEntriesForBsdiff(std::string_view(source).substr(0, 4096 * 2),
671 std::string_view(target).substr(0, 4096 * 2), 2, &entries);
Tao Baobc4a6d52018-05-23 00:12:21 -0700672 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu56ebe622017-03-16 00:48:21 -0700673
Tianjie Xu56ebe622017-03-16 00:48:21 -0700674 // The update_file should be patched correctly.
Tao Bao3cb3c522018-11-02 13:36:58 -0700675 std::string updated;
676 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated));
677 ASSERT_EQ(target, updated);
678}
679
680TEST_F(UpdaterTest, block_image_update_patch_overrun) {
681 // Both source and target images have 10 blocks.
682 std::string source =
683 std::string(4096, 'a') + std::string(4096, 'c') + std::string(4096 * 3, '\0');
684 std::string target =
685 std::string(4096, 'b') + std::string(4096, 'd') + std::string(4096 * 3, '\0');
686 ASSERT_TRUE(android::base::WriteStringToFile(source, image_file_));
687
688 // Provide one less block to trigger the overrun path.
689 PackageEntries entries;
690 GetEntriesForBsdiff(std::string_view(source).substr(0, 4096 * 2),
691 std::string_view(target).substr(0, 4096 * 2), 1, &entries);
692
693 // The update should fail due to overrun.
694 RunBlockImageUpdate(false, entries, image_file_, "", kPatchApplicationFailure);
Tianjie Xu5450c842017-10-18 13:15:21 -0700695}
696
Tao Baoa2cff952018-11-02 15:44:07 -0700697TEST_F(UpdaterTest, block_image_update_patch_underrun) {
Tao Bao3cb3c522018-11-02 13:36:58 -0700698 // 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_));
Tao Baoa2cff952018-11-02 15:44:07 -0700704
Tao Bao3cb3c522018-11-02 13:36:58 -0700705 // Provide one more 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), 3, &entries);
Tao Baoa2cff952018-11-02 15:44:07 -0700709
710 // The update should fail due to underrun.
711 RunBlockImageUpdate(false, entries, image_file_, "", kPatchApplicationFailure);
712}
713
Tianjie Xu5450c842017-10-18 13:15:21 -0700714TEST_F(UpdaterTest, block_image_update_fail) {
715 std::string src_content(4096 * 2, 'e');
Tao Bao3cb3c522018-11-02 13:36:58 -0700716 std::string src_hash = GetSha1(src_content);
Tianjie Xu5450c842017-10-18 13:15:21 -0700717 // Stash and free some blocks, then fail the update intentionally.
Tao Baobc4a6d52018-05-23 00:12:21 -0700718 std::vector<std::string> transfer_list{
719 // clang-format off
720 "4",
721 "2",
722 "0",
723 "2",
724 "stash " + src_hash + " 2,0,2",
725 "free " + src_hash,
Tao Bao91a649a2018-05-21 16:05:56 -0700726 "abort",
Tao Baobc4a6d52018-05-23 00:12:21 -0700727 // clang-format on
Tianjie Xu5450c842017-10-18 13:15:21 -0700728 };
729
730 // Add a new data of 10 bytes to test the deadlock.
Tao Baobc4a6d52018-05-23 00:12:21 -0700731 PackageEntries entries{
Tianjie Xu5450c842017-10-18 13:15:21 -0700732 { "new_data", std::string(10, 0) },
733 { "patch_data", "" },
734 { "transfer_list", android::base::Join(transfer_list, '\n') },
735 };
736
Tao Baobc4a6d52018-05-23 00:12:21 -0700737 ASSERT_TRUE(android::base::WriteStringToFile(src_content, image_file_));
Tianjie Xu5450c842017-10-18 13:15:21 -0700738
Tao Baobc4a6d52018-05-23 00:12:21 -0700739 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu5450c842017-10-18 13:15:21 -0700740
Tianjie Xu56ebe622017-03-16 00:48:21 -0700741 // Updater generates the stash name based on the input file name.
Tao Bao3cb3c522018-11-02 13:36:58 -0700742 std::string name_digest = GetSha1(image_file_);
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800743 std::string stash_base = std::string(temp_stash_base_.path) + "/" + name_digest;
Tianjie Xu56ebe622017-03-16 00:48:21 -0700744 ASSERT_EQ(0, access(stash_base.c_str(), F_OK));
Tao Baobc4a6d52018-05-23 00:12:21 -0700745 // Expect the stashed blocks to be freed.
Tianjie Xu5450c842017-10-18 13:15:21 -0700746 ASSERT_EQ(-1, access((stash_base + src_hash).c_str(), F_OK));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700747 ASSERT_EQ(0, rmdir(stash_base.c_str()));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700748}
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700749
Tianjie Xu5450c842017-10-18 13:15:21 -0700750TEST_F(UpdaterTest, new_data_over_write) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700751 std::vector<std::string> transfer_list{
752 // clang-format off
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700753 "4",
754 "1",
755 "0",
756 "0",
757 "new 2,0,1",
Tao Baobc4a6d52018-05-23 00:12:21 -0700758 // clang-format on
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700759 };
Tianjie Xu5450c842017-10-18 13:15:21 -0700760
Tao Baobc4a6d52018-05-23 00:12:21 -0700761 // Write 4096 + 100 bytes of new data.
762 PackageEntries entries{
763 { "new_data", std::string(4196, 0) },
Tianjie Xu5450c842017-10-18 13:15:21 -0700764 { "patch_data", "" },
765 { "transfer_list", android::base::Join(transfer_list, '\n') },
766 };
767
Tao Baobc4a6d52018-05-23 00:12:21 -0700768 RunBlockImageUpdate(false, entries, image_file_, "t");
769}
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700770
Tao Baobc4a6d52018-05-23 00:12:21 -0700771TEST_F(UpdaterTest, new_data_short_write) {
772 std::vector<std::string> transfer_list{
773 // clang-format off
774 "4",
775 "1",
776 "0",
777 "0",
778 "new 2,0,1",
779 // clang-format on
780 };
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700781
Tao Baobc4a6d52018-05-23 00:12:21 -0700782 PackageEntries entries{
783 { "patch_data", "" },
784 { "transfer_list", android::base::Join(transfer_list, '\n') },
785 };
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700786
787 // Updater should report the failure gracefully rather than stuck in deadlock.
Tao Baobc4a6d52018-05-23 00:12:21 -0700788 entries["new_data"] = "";
789 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700790
Tao Baobc4a6d52018-05-23 00:12:21 -0700791 entries["new_data"] = std::string(10, 'a');
792 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700793
794 // Expect to write 1 block of new data successfully.
Tao Baobc4a6d52018-05-23 00:12:21 -0700795 entries["new_data"] = std::string(4096, 'a');
796 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu107a34f2017-06-29 17:04:21 -0700797}
798
799TEST_F(UpdaterTest, brotli_new_data) {
Tianjie Xu107a34f2017-06-29 17:04:21 -0700800 auto generator = []() { return rand() % 128; };
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700801 // Generate 100 blocks of random data.
Tianjie Xu107a34f2017-06-29 17:04:21 -0700802 std::string brotli_new_data;
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700803 brotli_new_data.reserve(4096 * 100);
804 generate_n(back_inserter(brotli_new_data), 4096 * 100, generator);
Tianjie Xu107a34f2017-06-29 17:04:21 -0700805
806 size_t encoded_size = BrotliEncoderMaxCompressedSize(brotli_new_data.size());
Tianjie Xu5450c842017-10-18 13:15:21 -0700807 std::string encoded_data(encoded_size, 0);
Tianjie Xu107a34f2017-06-29 17:04:21 -0700808 ASSERT_TRUE(BrotliEncoderCompress(
809 BROTLI_DEFAULT_QUALITY, BROTLI_DEFAULT_WINDOW, BROTLI_DEFAULT_MODE, brotli_new_data.size(),
Tianjie Xu5450c842017-10-18 13:15:21 -0700810 reinterpret_cast<const uint8_t*>(brotli_new_data.data()), &encoded_size,
811 reinterpret_cast<uint8_t*>(const_cast<char*>(encoded_data.data()))));
812 encoded_data.resize(encoded_size);
Tianjie Xu107a34f2017-06-29 17:04:21 -0700813
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700814 // Write a few small chunks of new data, then a large chunk, and finally a few small chunks.
815 // This helps us to catch potential short writes.
Tianjie Xu107a34f2017-06-29 17:04:21 -0700816 std::vector<std::string> transfer_list = {
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700817 "4",
818 "100",
819 "0",
820 "0",
821 "new 2,0,1",
822 "new 2,1,2",
823 "new 4,2,50,50,97",
824 "new 2,97,98",
825 "new 2,98,99",
826 "new 2,99,100",
Tianjie Xu107a34f2017-06-29 17:04:21 -0700827 };
Tianjie Xu5450c842017-10-18 13:15:21 -0700828
Tao Baobc4a6d52018-05-23 00:12:21 -0700829 PackageEntries entries{
830 { "new_data.br", std::move(encoded_data) },
Tianjie Xu5450c842017-10-18 13:15:21 -0700831 { "patch_data", "" },
832 { "transfer_list", android::base::Join(transfer_list, '\n') },
833 };
834
Tao Baobc4a6d52018-05-23 00:12:21 -0700835 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu107a34f2017-06-29 17:04:21 -0700836
837 std::string updated_content;
Tao Baobc4a6d52018-05-23 00:12:21 -0700838 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_content));
Tianjie Xu107a34f2017-06-29 17:04:21 -0700839 ASSERT_EQ(brotli_new_data, updated_content);
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700840}
Tianjie Xu284752e2017-12-05 11:04:17 -0800841
842TEST_F(UpdaterTest, last_command_update) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700843 std::string block1(4096, '1');
844 std::string block2(4096, '2');
845 std::string block3(4096, '3');
Tao Bao3cb3c522018-11-02 13:36:58 -0700846 std::string block1_hash = GetSha1(block1);
847 std::string block2_hash = GetSha1(block2);
848 std::string block3_hash = GetSha1(block3);
Tianjie Xu284752e2017-12-05 11:04:17 -0800849
850 // Compose the transfer list to fail the first update.
Tao Baobc4a6d52018-05-23 00:12:21 -0700851 std::vector<std::string> transfer_list_fail{
852 // clang-format off
Tianjie Xu284752e2017-12-05 11:04:17 -0800853 "4",
854 "2",
855 "0",
856 "2",
857 "stash " + block1_hash + " 2,0,1",
858 "move " + block1_hash + " 2,1,2 1 2,0,1",
859 "stash " + block3_hash + " 2,2,3",
Tao Bao91a649a2018-05-21 16:05:56 -0700860 "abort",
Tao Baobc4a6d52018-05-23 00:12:21 -0700861 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -0800862 };
863
864 // Mimic a resumed update with the same transfer commands.
Tao Baobc4a6d52018-05-23 00:12:21 -0700865 std::vector<std::string> transfer_list_continue{
866 // clang-format off
Tianjie Xu284752e2017-12-05 11:04:17 -0800867 "4",
868 "2",
869 "0",
870 "2",
871 "stash " + block1_hash + " 2,0,1",
872 "move " + block1_hash + " 2,1,2 1 2,0,1",
873 "stash " + block3_hash + " 2,2,3",
874 "move " + block1_hash + " 2,2,3 1 2,0,1",
Tao Baobc4a6d52018-05-23 00:12:21 -0700875 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -0800876 };
877
Tao Baobc4a6d52018-05-23 00:12:21 -0700878 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_));
879
880 PackageEntries entries{
Tianjie Xu284752e2017-12-05 11:04:17 -0800881 { "new_data", "" },
882 { "patch_data", "" },
Tao Baobc4a6d52018-05-23 00:12:21 -0700883 { "transfer_list", android::base::Join(transfer_list_fail, '\n') },
Tianjie Xu284752e2017-12-05 11:04:17 -0800884 };
885
Tao Baobc4a6d52018-05-23 00:12:21 -0700886 // "2\nstash " + block3_hash + " 2,2,3"
Tao Baof8811bb2018-06-18 10:03:52 -0700887 std::string last_command_content =
888 "2\n" + transfer_list_fail[TransferList::kTransferListHeaderLines + 2];
Tianjie Xu284752e2017-12-05 11:04:17 -0800889
Tao Baobc4a6d52018-05-23 00:12:21 -0700890 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu284752e2017-12-05 11:04:17 -0800891
892 // Expect last_command to contain the last stash command.
Tao Baobc4a6d52018-05-23 00:12:21 -0700893 std::string last_command_actual;
Tao Bao7064aa22018-05-24 21:43:50 -0700894 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
Tao Baobc4a6d52018-05-23 00:12:21 -0700895 EXPECT_EQ(last_command_content, last_command_actual);
896
Tianjie Xu284752e2017-12-05 11:04:17 -0800897 std::string updated_contents;
Tao Baobc4a6d52018-05-23 00:12:21 -0700898 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_contents));
Tianjie Xu284752e2017-12-05 11:04:17 -0800899 ASSERT_EQ(block1 + block1 + block3, updated_contents);
900
Tao Baobc4a6d52018-05-23 00:12:21 -0700901 // "Resume" the update. Expect the first 'move' to be skipped but the second 'move' to be
902 // executed. Note that we intentionally reset the image file.
903 entries["transfer_list"] = android::base::Join(transfer_list_continue, '\n');
904 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_));
905 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu284752e2017-12-05 11:04:17 -0800906
Tao Baobc4a6d52018-05-23 00:12:21 -0700907 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_contents));
908 ASSERT_EQ(block1 + block2 + block1, updated_contents);
Tianjie Xu284752e2017-12-05 11:04:17 -0800909}
910
911TEST_F(UpdaterTest, last_command_update_unresumable) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700912 std::string block1(4096, '1');
913 std::string block2(4096, '2');
Tao Bao3cb3c522018-11-02 13:36:58 -0700914 std::string block1_hash = GetSha1(block1);
915 std::string block2_hash = GetSha1(block2);
Tianjie Xu284752e2017-12-05 11:04:17 -0800916
917 // Construct an unresumable update with source blocks mismatch.
Tao Baobc4a6d52018-05-23 00:12:21 -0700918 std::vector<std::string> transfer_list_unresumable{
919 // clang-format off
920 "4",
921 "2",
922 "0",
923 "2",
924 "stash " + block1_hash + " 2,0,1",
925 "move " + block2_hash + " 2,1,2 1 2,0,1",
926 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -0800927 };
928
Tao Baobc4a6d52018-05-23 00:12:21 -0700929 PackageEntries entries{
Tianjie Xu284752e2017-12-05 11:04:17 -0800930 { "new_data", "" },
931 { "patch_data", "" },
Tao Baobc4a6d52018-05-23 00:12:21 -0700932 { "transfer_list", android::base::Join(transfer_list_unresumable, '\n') },
Tianjie Xu284752e2017-12-05 11:04:17 -0800933 };
934
Tao Baobc4a6d52018-05-23 00:12:21 -0700935 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block1, image_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -0800936
Tao Baof8811bb2018-06-18 10:03:52 -0700937 std::string last_command_content =
938 "0\n" + transfer_list_unresumable[TransferList::kTransferListHeaderLines];
Tao Bao7064aa22018-05-24 21:43:50 -0700939 ASSERT_TRUE(android::base::WriteStringToFile(last_command_content, last_command_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -0800940
Tao Baobc4a6d52018-05-23 00:12:21 -0700941 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu284752e2017-12-05 11:04:17 -0800942
Tao Baobc4a6d52018-05-23 00:12:21 -0700943 // The last_command_file will be deleted if the update encounters an unresumable failure later.
Tao Bao7064aa22018-05-24 21:43:50 -0700944 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
Tianjie Xu284752e2017-12-05 11:04:17 -0800945}
946
947TEST_F(UpdaterTest, last_command_verify) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700948 std::string block1(4096, '1');
949 std::string block2(4096, '2');
950 std::string block3(4096, '3');
Tao Bao3cb3c522018-11-02 13:36:58 -0700951 std::string block1_hash = GetSha1(block1);
952 std::string block2_hash = GetSha1(block2);
953 std::string block3_hash = GetSha1(block3);
Tianjie Xu284752e2017-12-05 11:04:17 -0800954
Tao Baobc4a6d52018-05-23 00:12:21 -0700955 std::vector<std::string> transfer_list_verify{
956 // clang-format off
Tianjie Xu284752e2017-12-05 11:04:17 -0800957 "4",
958 "2",
959 "0",
960 "2",
961 "stash " + block1_hash + " 2,0,1",
962 "move " + block1_hash + " 2,0,1 1 2,0,1",
963 "move " + block1_hash + " 2,1,2 1 2,0,1",
964 "stash " + block3_hash + " 2,2,3",
Tao Baobc4a6d52018-05-23 00:12:21 -0700965 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -0800966 };
967
Tao Baobc4a6d52018-05-23 00:12:21 -0700968 PackageEntries entries{
Tianjie Xu284752e2017-12-05 11:04:17 -0800969 { "new_data", "" },
970 { "patch_data", "" },
Tao Baobc4a6d52018-05-23 00:12:21 -0700971 { "transfer_list", android::base::Join(transfer_list_verify, '\n') },
Tianjie Xu284752e2017-12-05 11:04:17 -0800972 };
973
Tao Baobc4a6d52018-05-23 00:12:21 -0700974 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block1 + block3, image_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -0800975
Tao Baobc4a6d52018-05-23 00:12:21 -0700976 // Last command: "move " + block1_hash + " 2,1,2 1 2,0,1"
Tao Baof8811bb2018-06-18 10:03:52 -0700977 std::string last_command_content =
978 "2\n" + transfer_list_verify[TransferList::kTransferListHeaderLines + 2];
Tianjie Xu284752e2017-12-05 11:04:17 -0800979
Tao Baobc4a6d52018-05-23 00:12:21 -0700980 // First run: expect the verification to succeed and the last_command_file is intact.
Tao Bao7064aa22018-05-24 21:43:50 -0700981 ASSERT_TRUE(android::base::WriteStringToFile(last_command_content, last_command_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -0800982
Tao Baobc4a6d52018-05-23 00:12:21 -0700983 RunBlockImageUpdate(true, entries, image_file_, "t");
Tianjie Xu284752e2017-12-05 11:04:17 -0800984
Tao Baobc4a6d52018-05-23 00:12:21 -0700985 std::string last_command_actual;
Tao Bao7064aa22018-05-24 21:43:50 -0700986 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
Tao Baobc4a6d52018-05-23 00:12:21 -0700987 EXPECT_EQ(last_command_content, last_command_actual);
Tianjie Xu284752e2017-12-05 11:04:17 -0800988
Tao Baobc4a6d52018-05-23 00:12:21 -0700989 // Second run with a mismatching block image: expect the verification to succeed but
990 // last_command_file to be deleted; because the target blocks in the last command don't have the
991 // expected contents for the second move command.
992 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_));
993 RunBlockImageUpdate(true, entries, image_file_, "t");
Tao Bao7064aa22018-05-24 21:43:50 -0700994 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
Tianjie Xu284752e2017-12-05 11:04:17 -0800995}
Tao Baoc0299ed2018-05-24 00:16:35 -0700996
997class ResumableUpdaterTest : public testing::TestWithParam<size_t> {
998 protected:
999 void SetUp() override {
1000 RegisterBuiltins();
1001 RegisterInstallFunctions();
1002 RegisterBlockImageFunctions();
1003
1004 Paths::Get().set_cache_temp_source(temp_saved_source_.path);
1005 Paths::Get().set_last_command_file(temp_last_command_.path);
1006 Paths::Get().set_stash_directory_base(temp_stash_base_.path);
1007
Tao Bao91a649a2018-05-21 16:05:56 -07001008 // Enable a special command "abort" to simulate interruption.
1009 Command::abort_allowed_ = true;
1010
Tao Baoc0299ed2018-05-24 00:16:35 -07001011 index_ = GetParam();
1012 image_file_ = image_temp_file_.path;
1013 last_command_file_ = temp_last_command_.path;
1014 }
1015
1016 void TearDown() override {
1017 // Clean up the last_command_file if any.
1018 ASSERT_TRUE(android::base::RemoveFileIfExists(last_command_file_));
1019
1020 // Clear partition updated marker if any.
1021 std::string updated_marker{ temp_stash_base_.path };
Tao Bao3cb3c522018-11-02 13:36:58 -07001022 updated_marker += "/" + GetSha1(image_temp_file_.path) + ".UPDATED";
Tao Baoc0299ed2018-05-24 00:16:35 -07001023 ASSERT_TRUE(android::base::RemoveFileIfExists(updated_marker));
1024 }
1025
1026 TemporaryFile temp_saved_source_;
1027 TemporaryDir temp_stash_base_;
1028 std::string last_command_file_;
1029 std::string image_file_;
1030 size_t index_;
1031
1032 private:
1033 TemporaryFile temp_last_command_;
1034 TemporaryFile image_temp_file_;
1035};
1036
1037static std::string g_source_image;
1038static std::string g_target_image;
1039static PackageEntries g_entries;
1040
1041static std::vector<std::string> GenerateTransferList() {
1042 std::string a(4096, 'a');
1043 std::string b(4096, 'b');
1044 std::string c(4096, 'c');
1045 std::string d(4096, 'd');
1046 std::string e(4096, 'e');
1047 std::string f(4096, 'f');
1048 std::string g(4096, 'g');
1049 std::string h(4096, 'h');
1050 std::string i(4096, 'i');
1051 std::string zero(4096, '\0');
1052
Tao Bao3cb3c522018-11-02 13:36:58 -07001053 std::string a_hash = GetSha1(a);
1054 std::string b_hash = GetSha1(b);
1055 std::string c_hash = GetSha1(c);
1056 std::string e_hash = GetSha1(e);
Tao Baoc0299ed2018-05-24 00:16:35 -07001057
1058 auto loc = [](const std::string& range_text) {
1059 std::vector<std::string> pieces = android::base::Split(range_text, "-");
1060 size_t left;
1061 size_t right;
1062 if (pieces.size() == 1) {
1063 CHECK(android::base::ParseUint(pieces[0], &left));
1064 right = left + 1;
1065 } else {
1066 CHECK_EQ(2u, pieces.size());
1067 CHECK(android::base::ParseUint(pieces[0], &left));
1068 CHECK(android::base::ParseUint(pieces[1], &right));
1069 right++;
1070 }
1071 return android::base::StringPrintf("2,%zu,%zu", left, right);
1072 };
1073
1074 // patch 1: "b d c" -> "g"
1075 TemporaryFile patch_file_bdc_g;
1076 std::string bdc = b + d + c;
Tao Bao3cb3c522018-11-02 13:36:58 -07001077 std::string bdc_hash = GetSha1(bdc);
1078 std::string g_hash = GetSha1(g);
Tao Baoc0299ed2018-05-24 00:16:35 -07001079 CHECK_EQ(0, bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(bdc.data()), bdc.size(),
1080 reinterpret_cast<const uint8_t*>(g.data()), g.size(),
1081 patch_file_bdc_g.path, nullptr));
1082 std::string patch_bdc_g;
1083 CHECK(android::base::ReadFileToString(patch_file_bdc_g.path, &patch_bdc_g));
1084
1085 // patch 2: "a b c d" -> "d c b"
1086 TemporaryFile patch_file_abcd_dcb;
1087 std::string abcd = a + b + c + d;
Tao Bao3cb3c522018-11-02 13:36:58 -07001088 std::string abcd_hash = GetSha1(abcd);
Tao Baoc0299ed2018-05-24 00:16:35 -07001089 std::string dcb = d + c + b;
Tao Bao3cb3c522018-11-02 13:36:58 -07001090 std::string dcb_hash = GetSha1(dcb);
Tao Baoc0299ed2018-05-24 00:16:35 -07001091 CHECK_EQ(0, bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(abcd.data()), abcd.size(),
1092 reinterpret_cast<const uint8_t*>(dcb.data()), dcb.size(),
1093 patch_file_abcd_dcb.path, nullptr));
1094 std::string patch_abcd_dcb;
1095 CHECK(android::base::ReadFileToString(patch_file_abcd_dcb.path, &patch_abcd_dcb));
1096
1097 std::vector<std::string> transfer_list{
1098 "4",
1099 "10", // total blocks written
1100 "2", // maximum stash entries
1101 "2", // maximum number of stashed blocks
1102
1103 // a b c d e a b c d e
1104 "stash " + b_hash + " " + loc("1"),
1105 // a b c d e a b c d e [b(1)]
1106 "stash " + c_hash + " " + loc("2"),
1107 // a b c d e a b c d e [b(1)][c(2)]
1108 "new " + loc("1-2"),
1109 // a i h d e a b c d e [b(1)][c(2)]
1110 "zero " + loc("0"),
1111 // 0 i h d e a b c d e [b(1)][c(2)]
1112
1113 // bsdiff "b d c" (from stash, 3, stash) to get g(3)
1114 android::base::StringPrintf(
1115 "bsdiff 0 %zu %s %s %s 3 %s %s %s:%s %s:%s",
1116 patch_bdc_g.size(), // patch start (0), patch length
1117 bdc_hash.c_str(), // source hash
1118 g_hash.c_str(), // target hash
1119 loc("3").c_str(), // target range
1120 loc("3").c_str(), loc("1").c_str(), // load "d" from block 3, into buffer at offset 1
1121 b_hash.c_str(), loc("0").c_str(), // load "b" from stash, into buffer at offset 0
1122 c_hash.c_str(), loc("2").c_str()), // load "c" from stash, into buffer at offset 2
1123
1124 // 0 i h g e a b c d e [b(1)][c(2)]
1125 "free " + b_hash,
1126 // 0 i h g e a b c d e [c(2)]
1127 "free " + a_hash,
1128 // 0 i h g e a b c d e
1129 "stash " + a_hash + " " + loc("5"),
1130 // 0 i h g e a b c d e [a(5)]
1131 "move " + e_hash + " " + loc("5") + " 1 " + loc("4"),
1132 // 0 i h g e e b c d e [a(5)]
1133
1134 // bsdiff "a b c d" (from stash, 6-8) to "d c b" (6-8)
1135 android::base::StringPrintf( //
1136 "bsdiff %zu %zu %s %s %s 4 %s %s %s:%s",
1137 patch_bdc_g.size(), // patch start
1138 patch_bdc_g.size() + patch_abcd_dcb.size(), // patch length
1139 abcd_hash.c_str(), // source hash
1140 dcb_hash.c_str(), // target hash
1141 loc("6-8").c_str(), // target range
1142 loc("6-8").c_str(), // load "b c d" from blocks 6-8
1143 loc("1-3").c_str(), // into buffer at offset 1-3
1144 a_hash.c_str(), // load "a" from stash
1145 loc("0").c_str()), // into buffer at offset 0
1146
1147 // 0 i h g e e d c b e [a(5)]
1148 "new " + loc("4"),
1149 // 0 i h g f e d c b e [a(5)]
1150 "move " + a_hash + " " + loc("9") + " 1 - " + a_hash + ":" + loc("0"),
1151 // 0 i h g f e d c b a [a(5)]
1152 "free " + a_hash,
1153 // 0 i h g f e d c b a
1154 };
1155
1156 std::string new_data = i + h + f;
1157 std::string patch_data = patch_bdc_g + patch_abcd_dcb;
1158
1159 g_entries = {
1160 { "new_data", new_data },
1161 { "patch_data", patch_data },
1162 };
1163 g_source_image = a + b + c + d + e + a + b + c + d + e;
1164 g_target_image = zero + i + h + g + f + e + d + c + b + a;
1165
1166 return transfer_list;
1167}
1168
1169static const std::vector<std::string> g_transfer_list = GenerateTransferList();
1170
1171INSTANTIATE_TEST_CASE_P(InterruptAfterEachCommand, ResumableUpdaterTest,
1172 ::testing::Range(static_cast<size_t>(0),
Tao Baof8811bb2018-06-18 10:03:52 -07001173 g_transfer_list.size() -
1174 TransferList::kTransferListHeaderLines));
Tao Baoc0299ed2018-05-24 00:16:35 -07001175
1176TEST_P(ResumableUpdaterTest, InterruptVerifyResume) {
1177 ASSERT_TRUE(android::base::WriteStringToFile(g_source_image, image_file_));
1178
1179 LOG(INFO) << "Interrupting at line " << index_ << " ("
Tao Baof8811bb2018-06-18 10:03:52 -07001180 << g_transfer_list[TransferList::kTransferListHeaderLines + index_] << ")";
Tao Baoc0299ed2018-05-24 00:16:35 -07001181
1182 std::vector<std::string> transfer_list_copy{ g_transfer_list };
Tao Baof8811bb2018-06-18 10:03:52 -07001183 transfer_list_copy[TransferList::kTransferListHeaderLines + index_] = "abort";
Tao Baoc0299ed2018-05-24 00:16:35 -07001184
1185 g_entries["transfer_list"] = android::base::Join(transfer_list_copy, '\n');
1186
1187 // Run update that's expected to fail.
1188 RunBlockImageUpdate(false, g_entries, image_file_, "");
1189
1190 std::string last_command_expected;
1191
1192 // Assert the last_command_file.
1193 if (index_ == 0) {
1194 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
1195 } else {
Tao Baof8811bb2018-06-18 10:03:52 -07001196 last_command_expected = std::to_string(index_ - 1) + "\n" +
1197 g_transfer_list[TransferList::kTransferListHeaderLines + index_ - 1];
Tao Baoc0299ed2018-05-24 00:16:35 -07001198 std::string last_command_actual;
1199 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
1200 ASSERT_EQ(last_command_expected, last_command_actual);
1201 }
1202
1203 g_entries["transfer_list"] = android::base::Join(g_transfer_list, '\n');
1204
1205 // Resume the interrupted update, by doing verification first.
1206 RunBlockImageUpdate(true, g_entries, image_file_, "t");
1207
1208 // last_command_file should remain intact.
1209 if (index_ == 0) {
1210 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
1211 } else {
1212 std::string last_command_actual;
1213 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
1214 ASSERT_EQ(last_command_expected, last_command_actual);
1215 }
1216
1217 // Resume the update.
1218 RunBlockImageUpdate(false, g_entries, image_file_, "t");
1219
1220 // last_command_file should be gone after successful update.
1221 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
1222
1223 std::string updated_image_actual;
1224 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_image_actual));
1225 ASSERT_EQ(g_target_image, updated_image_actual);
1226}