blob: 0720e821feb92b2e0c414a40143f897dce05a8cd [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"
Tianjie Xu1536db82019-05-14 10:54:43 -070055#include "updater/updater_runtime.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 Baod8d514f2018-07-09 13:32:28 -070061static void expect(const char* expected, const std::string& expr_str, CauseCode cause_code,
Tianjie Xu1536db82019-05-14 10:54:43 -070062 Updater* updater) {
Tianjie Xuc4447322017-03-06 14:44:59 -080063 std::unique_ptr<Expr> e;
Tao Baoef0eb3b2016-11-14 21:29:52 -080064 int error_count = 0;
Tao Baod8d514f2018-07-09 13:32:28 -070065 ASSERT_EQ(0, ParseString(expr_str, &e, &error_count));
Tao Baoef0eb3b2016-11-14 21:29:52 -080066 ASSERT_EQ(0, error_count);
Tao Bao0c7839a2016-10-10 15:48:37 -070067
Tianjie Xu58d59122019-05-03 01:05:04 -070068 State state(expr_str, updater);
Tao Bao0c7839a2016-10-10 15:48:37 -070069
Tao Baoef0eb3b2016-11-14 21:29:52 -080070 std::string result;
71 bool status = Evaluate(&state, e, &result);
Tao Bao0c7839a2016-10-10 15:48:37 -070072
Tao Baoef0eb3b2016-11-14 21:29:52 -080073 if (expected == nullptr) {
74 ASSERT_FALSE(status);
75 } else {
Tao Bao0b58e9a2018-07-06 15:01:05 -070076 ASSERT_TRUE(status) << "Evaluate() finished with error message: " << state.errmsg;
Tao Baoef0eb3b2016-11-14 21:29:52 -080077 ASSERT_STREQ(expected, result.c_str());
78 }
Tao Bao0c7839a2016-10-10 15:48:37 -070079
Tao Baoef0eb3b2016-11-14 21:29:52 -080080 // Error code is set in updater/updater.cpp only, by parsing State.errmsg.
81 ASSERT_EQ(kNoError, state.error_code);
Tao Bao361342c2016-02-08 11:15:50 -080082
Tao Baoef0eb3b2016-11-14 21:29:52 -080083 // Cause code should always be available.
84 ASSERT_EQ(cause_code, state.cause_code);
Tao Bao0c7839a2016-10-10 15:48:37 -070085}
86
Tianjie Xu1536db82019-05-14 10:54:43 -070087static void expect(const char* expected, const std::string& expr_str, CauseCode cause_code) {
Tianjie Xu27556d02019-05-22 14:48:35 -070088 Updater updater(std::make_unique<UpdaterRuntime>(nullptr));
Tianjie Xu1536db82019-05-14 10:54:43 -070089 expect(expected, expr_str, cause_code, &updater);
90}
91
Tao Baobc4a6d52018-05-23 00:12:21 -070092static void BuildUpdatePackage(const PackageEntries& entries, int fd) {
Tianjie Xu5450c842017-10-18 13:15:21 -070093 FILE* zip_file_ptr = fdopen(fd, "wb");
94 ZipWriter zip_writer(zip_file_ptr);
95
96 for (const auto& entry : entries) {
Tao Baobc4a6d52018-05-23 00:12:21 -070097 // All the entries are written as STORED.
Tianjie Xu5450c842017-10-18 13:15:21 -070098 ASSERT_EQ(0, zip_writer.StartEntry(entry.first.c_str(), 0));
99 if (!entry.second.empty()) {
100 ASSERT_EQ(0, zip_writer.WriteBytes(entry.second.data(), entry.second.size()));
101 }
102 ASSERT_EQ(0, zip_writer.FinishEntry());
103 }
104
105 ASSERT_EQ(0, zip_writer.Finish());
106 ASSERT_EQ(0, fclose(zip_file_ptr));
107}
108
Tao Bao3cb3c522018-11-02 13:36:58 -0700109static std::string GetSha1(std::string_view content) {
Tianjie Xu56ebe622017-03-16 00:48:21 -0700110 uint8_t digest[SHA_DIGEST_LENGTH];
Tao Bao3cb3c522018-11-02 13:36:58 -0700111 SHA1(reinterpret_cast<const uint8_t*>(content.data()), content.size(), digest);
Tianjie Xu56ebe622017-03-16 00:48:21 -0700112 return print_sha1(digest);
113}
114
Tao Bao0b58e9a2018-07-06 15:01:05 -0700115static Value* BlobToString(const char* name, State* state,
116 const std::vector<std::unique_ptr<Expr>>& argv) {
117 if (argv.size() != 1) {
118 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
119 }
120
121 std::vector<std::unique_ptr<Value>> args;
122 if (!ReadValueArgs(state, argv, &args)) {
123 return nullptr;
124 }
125
Tao Bao511d7592018-06-19 15:56:49 -0700126 if (args[0]->type != Value::Type::BLOB) {
Tao Bao0b58e9a2018-07-06 15:01:05 -0700127 return ErrorAbort(state, kArgsParsingFailure, "%s() expects a BLOB argument", name);
128 }
129
Tao Bao511d7592018-06-19 15:56:49 -0700130 args[0]->type = Value::Type::STRING;
Tao Bao0b58e9a2018-07-06 15:01:05 -0700131 return args[0].release();
132}
133
Tianjie Xu58d59122019-05-03 01:05:04 -0700134class UpdaterTestBase {
Tianjie Xu56ebe622017-03-16 00:48:21 -0700135 protected:
Tianjie Xu27556d02019-05-22 14:48:35 -0700136 UpdaterTestBase() : updater_(std::make_unique<UpdaterRuntime>(nullptr)) {}
137
Tianjie Xu58d59122019-05-03 01:05:04 -0700138 void SetUp() {
Tianjie Xu56ebe622017-03-16 00:48:21 -0700139 RegisterBuiltins();
140 RegisterInstallFunctions();
141 RegisterBlockImageFunctions();
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800142
Tao Bao7064aa22018-05-24 21:43:50 -0700143 // Each test is run in a separate process (isolated mode). Shared temporary files won't cause
144 // conflicts.
Tao Bao641fa972018-04-25 18:59:40 -0700145 Paths::Get().set_cache_temp_source(temp_saved_source_.path);
Tao Bao7064aa22018-05-24 21:43:50 -0700146 Paths::Get().set_last_command_file(temp_last_command_.path);
Tao Bao641fa972018-04-25 18:59:40 -0700147 Paths::Get().set_stash_directory_base(temp_stash_base_.path);
Tao Baobc4a6d52018-05-23 00:12:21 -0700148
Tao Bao7064aa22018-05-24 21:43:50 -0700149 last_command_file_ = temp_last_command_.path;
Tao Baobc4a6d52018-05-23 00:12:21 -0700150 image_file_ = image_temp_file_.path;
151 }
152
Tianjie Xu58d59122019-05-03 01:05:04 -0700153 void TearDown() {
Tao Bao7064aa22018-05-24 21:43:50 -0700154 // Clean up the last_command_file if any.
155 ASSERT_TRUE(android::base::RemoveFileIfExists(last_command_file_));
156
Tao Baobc4a6d52018-05-23 00:12:21 -0700157 // Clear partition updated marker if any.
158 std::string updated_marker{ temp_stash_base_.path };
Tao Bao3cb3c522018-11-02 13:36:58 -0700159 updated_marker += "/" + GetSha1(image_temp_file_.path) + ".UPDATED";
Tao Baobc4a6d52018-05-23 00:12:21 -0700160 ASSERT_TRUE(android::base::RemoveFileIfExists(updated_marker));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700161 }
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800162
Tianjie Xu58d59122019-05-03 01:05:04 -0700163 void RunBlockImageUpdate(bool is_verify, PackageEntries entries, const std::string& image_file,
164 const std::string& result, CauseCode cause_code = kNoCause) {
165 CHECK(entries.find("transfer_list") != entries.end());
166 std::string new_data =
167 entries.find("new_data.br") != entries.end() ? "new_data.br" : "new_data";
168 std::string script = is_verify ? "block_image_verify" : "block_image_update";
169 script += R"((")" + image_file + R"(", package_extract_file("transfer_list"), ")" + new_data +
170 R"(", "patch_data"))";
171 entries.emplace(Updater::SCRIPT_NAME, script);
172
173 // Build the update package.
174 TemporaryFile zip_file;
175 BuildUpdatePackage(entries, zip_file.release());
176
177 // Set up the handler, command_pipe, patch offset & length.
178 TemporaryFile temp_pipe;
Tianjie Xu1536db82019-05-14 10:54:43 -0700179 ASSERT_TRUE(updater_.Init(temp_pipe.release(), zip_file.path, false));
Tianjie Xu58d59122019-05-03 01:05:04 -0700180 ASSERT_TRUE(updater_.RunUpdate());
Tianjie Xu1536db82019-05-14 10:54:43 -0700181 ASSERT_EQ(result, updater_.GetResult());
Tianjie Xu58d59122019-05-03 01:05:04 -0700182
183 // Parse the cause code written to the command pipe.
184 int received_cause_code = kNoCause;
185 std::string pipe_content;
186 ASSERT_TRUE(android::base::ReadFileToString(temp_pipe.path, &pipe_content));
187 auto lines = android::base::Split(pipe_content, "\n");
188 for (std::string_view line : lines) {
189 if (android::base::ConsumePrefix(&line, "log cause: ")) {
190 ASSERT_TRUE(android::base::ParseInt(line.data(), &received_cause_code));
191 }
192 }
193 ASSERT_EQ(cause_code, received_cause_code);
194 }
195
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800196 TemporaryFile temp_saved_source_;
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800197 TemporaryDir temp_stash_base_;
Tao Bao7064aa22018-05-24 21:43:50 -0700198 std::string last_command_file_;
Tao Baobc4a6d52018-05-23 00:12:21 -0700199 std::string image_file_;
200
Tianjie Xu58d59122019-05-03 01:05:04 -0700201 Updater updater_;
202
Tao Baobc4a6d52018-05-23 00:12:21 -0700203 private:
Tao Bao7064aa22018-05-24 21:43:50 -0700204 TemporaryFile temp_last_command_;
Tao Baobc4a6d52018-05-23 00:12:21 -0700205 TemporaryFile image_temp_file_;
Tao Bao0c7839a2016-10-10 15:48:37 -0700206};
207
Tianjie Xu58d59122019-05-03 01:05:04 -0700208class UpdaterTest : public UpdaterTestBase, public ::testing::Test {
209 protected:
210 void SetUp() override {
211 UpdaterTestBase::SetUp();
212
213 RegisterFunction("blob_to_string", BlobToString);
214 // Enable a special command "abort" to simulate interruption.
215 Command::abort_allowed_ = true;
216 }
217
218 void TearDown() override {
219 UpdaterTestBase::TearDown();
220 }
221
222 void SetUpdaterCmdPipe(int fd) {
223 FILE* cmd_pipe = fdopen(fd, "w");
224 ASSERT_NE(nullptr, cmd_pipe);
225 updater_.cmd_pipe_.reset(cmd_pipe);
226 }
227
228 void SetUpdaterOtaPackageHandle(ZipArchiveHandle handle) {
229 updater_.package_handle_ = handle;
230 }
231
232 void FlushUpdaterCommandPipe() const {
233 fflush(updater_.cmd_pipe_.get());
234 }
235};
236
Tao Bao0c7839a2016-10-10 15:48:37 -0700237TEST_F(UpdaterTest, getprop) {
238 expect(android::base::GetProperty("ro.product.device", "").c_str(),
239 "getprop(\"ro.product.device\")",
Tao Bao361342c2016-02-08 11:15:50 -0800240 kNoCause);
Tao Bao0c7839a2016-10-10 15:48:37 -0700241
242 expect(android::base::GetProperty("ro.build.fingerprint", "").c_str(),
243 "getprop(\"ro.build.fingerprint\")",
Tao Bao361342c2016-02-08 11:15:50 -0800244 kNoCause);
Tao Bao0c7839a2016-10-10 15:48:37 -0700245
246 // getprop() accepts only one parameter.
Tao Bao361342c2016-02-08 11:15:50 -0800247 expect(nullptr, "getprop()", kArgsParsingFailure);
248 expect(nullptr, "getprop(\"arg1\", \"arg2\")", kArgsParsingFailure);
249}
250
Tao Bao5609bc82018-06-20 00:30:48 -0700251TEST_F(UpdaterTest, patch_partition_check) {
252 // Zero argument is not valid.
253 expect(nullptr, "patch_partition_check()", kArgsParsingFailure);
Tao Baodb56eb02017-03-23 06:34:20 -0700254
Tao Bao5609bc82018-06-20 00:30:48 -0700255 std::string source_file = from_testdata_base("boot.img");
256 std::string source_content;
257 ASSERT_TRUE(android::base::ReadFileToString(source_file, &source_content));
258 size_t source_size = source_content.size();
Tao Bao3cb3c522018-11-02 13:36:58 -0700259 std::string source_hash = GetSha1(source_content);
Tao Bao5609bc82018-06-20 00:30:48 -0700260 Partition source(source_file, source_size, source_hash);
Tao Baodb56eb02017-03-23 06:34:20 -0700261
Tao Bao5609bc82018-06-20 00:30:48 -0700262 std::string target_file = from_testdata_base("recovery.img");
263 std::string target_content;
264 ASSERT_TRUE(android::base::ReadFileToString(target_file, &target_content));
265 size_t target_size = target_content.size();
Tao Bao3cb3c522018-11-02 13:36:58 -0700266 std::string target_hash = GetSha1(target_content);
Tao Bao5609bc82018-06-20 00:30:48 -0700267 Partition target(target_file, target_size, target_hash);
Tao Baodb56eb02017-03-23 06:34:20 -0700268
Tao Bao5609bc82018-06-20 00:30:48 -0700269 // One argument is not valid.
270 expect(nullptr, "patch_partition_check(\"" + source.ToString() + "\")", kArgsParsingFailure);
271 expect(nullptr, "patch_partition_check(\"" + target.ToString() + "\")", kArgsParsingFailure);
272
273 // Both of the source and target have the desired checksum.
274 std::string cmd =
275 "patch_partition_check(\"" + source.ToString() + "\", \"" + target.ToString() + "\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700276 expect("t", cmd, kNoCause);
Tao Baodb56eb02017-03-23 06:34:20 -0700277
Tao Bao5609bc82018-06-20 00:30:48 -0700278 // Only source partition has the desired checksum.
279 Partition bad_target(target_file, target_size - 1, target_hash);
280 cmd = "patch_partition_check(\"" + source.ToString() + "\", \"" + bad_target.ToString() + "\")";
281 expect("t", cmd, kNoCause);
282
283 // Only target partition has the desired checksum.
284 Partition bad_source(source_file, source_size + 1, source_hash);
285 cmd = "patch_partition_check(\"" + bad_source.ToString() + "\", \"" + target.ToString() + "\")";
286 expect("t", cmd, kNoCause);
287
288 // Neither of the source or target has the desired checksum.
289 cmd =
290 "patch_partition_check(\"" + bad_source.ToString() + "\", \"" + bad_target.ToString() + "\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700291 expect("", cmd, kNoCause);
Tao Baodb56eb02017-03-23 06:34:20 -0700292}
293
Tao Bao51d516e2016-11-03 14:49:01 -0700294TEST_F(UpdaterTest, file_getprop) {
295 // file_getprop() expects two arguments.
296 expect(nullptr, "file_getprop()", kArgsParsingFailure);
297 expect(nullptr, "file_getprop(\"arg1\")", kArgsParsingFailure);
298 expect(nullptr, "file_getprop(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
299
300 // File doesn't exist.
Tianjie Xu22f11202018-08-27 10:50:31 -0700301 expect(nullptr, "file_getprop(\"/doesntexist\", \"key1\")", kFreadFailure);
Tao Bao51d516e2016-11-03 14:49:01 -0700302
303 // Reject too large files (current limit = 65536).
304 TemporaryFile temp_file1;
305 std::string buffer(65540, '\0');
306 ASSERT_TRUE(android::base::WriteStringToFile(buffer, temp_file1.path));
307
308 // Read some keys.
309 TemporaryFile temp_file2;
310 std::string content("ro.product.name=tardis\n"
311 "# comment\n\n\n"
312 "ro.product.model\n"
313 "ro.product.board = magic \n");
314 ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file2.path));
315
316 std::string script1("file_getprop(\"" + std::string(temp_file2.path) +
317 "\", \"ro.product.name\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700318 expect("tardis", script1, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700319
320 std::string script2("file_getprop(\"" + std::string(temp_file2.path) +
321 "\", \"ro.product.board\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700322 expect("magic", script2, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700323
324 // No match.
325 std::string script3("file_getprop(\"" + std::string(temp_file2.path) +
326 "\", \"ro.product.wrong\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700327 expect("", script3, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700328
329 std::string script4("file_getprop(\"" + std::string(temp_file2.path) +
330 "\", \"ro.product.name=\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700331 expect("", script4, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700332
333 std::string script5("file_getprop(\"" + std::string(temp_file2.path) +
334 "\", \"ro.product.nam\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700335 expect("", script5, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700336
337 std::string script6("file_getprop(\"" + std::string(temp_file2.path) +
338 "\", \"ro.product.model\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700339 expect("", script6, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700340}
Tao Bao0831d0b2016-11-03 23:25:04 -0700341
Michael Bestasddc7b5a2019-09-19 21:42:12 +0300342TEST_F(UpdaterTest, delete) {
343 // Delete none.
344 expect("0", "delete()", kNoCause);
345 expect("0", "delete(\"/doesntexist\")", kNoCause);
346 expect("0", "delete(\"/doesntexist1\", \"/doesntexist2\")", kNoCause);
347 expect("0", "delete(\"/doesntexist1\", \"/doesntexist2\", \"/doesntexist3\")", kNoCause);
348
349 // Delete one file.
350 TemporaryFile temp_file1;
351 ASSERT_TRUE(android::base::WriteStringToFile("abc", temp_file1.path));
352 std::string script1("delete(\"" + std::string(temp_file1.path) + "\")");
353 expect("1", script1.c_str(), kNoCause);
354
355 // Delete two files.
356 TemporaryFile temp_file2;
357 ASSERT_TRUE(android::base::WriteStringToFile("abc", temp_file2.path));
358 TemporaryFile temp_file3;
359 ASSERT_TRUE(android::base::WriteStringToFile("abc", temp_file3.path));
360 std::string script2("delete(\"" + std::string(temp_file2.path) + "\", \"" +
361 std::string(temp_file3.path) + "\")");
362 expect("2", script2.c_str(), kNoCause);
363
364 // Delete already deleted files.
365 expect("0", script2.c_str(), kNoCause);
366
367 // Delete one out of three.
368 TemporaryFile temp_file4;
369 ASSERT_TRUE(android::base::WriteStringToFile("abc", temp_file4.path));
370 std::string script3("delete(\"/doesntexist1\", \"" + std::string(temp_file4.path) +
371 "\", \"/doesntexist2\")");
372 expect("1", script3.c_str(), kNoCause);
373}
374
375TEST_F(UpdaterTest, rename) {
376 // rename() expects two arguments.
377 expect(nullptr, "rename()", kArgsParsingFailure);
378 expect(nullptr, "rename(\"arg1\")", kArgsParsingFailure);
379 expect(nullptr, "rename(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
380
381 // src_name or dst_name cannot be empty.
382 expect(nullptr, "rename(\"\", \"arg2\")", kArgsParsingFailure);
383 expect(nullptr, "rename(\"arg1\", \"\")", kArgsParsingFailure);
384
385 // File doesn't exist (both of src and dst).
386 expect(nullptr, "rename(\"/doesntexist\", \"/doesntexisteither\")", kFileRenameFailure);
387
388 // Can't create parent directory.
389 TemporaryFile temp_file1;
390 ASSERT_TRUE(android::base::WriteStringToFile("abc", temp_file1.path));
391 std::string script1("rename(\"" + std::string(temp_file1.path) + "\", \"/proc/0/file1\")");
392 expect(nullptr, script1.c_str(), kFileRenameFailure);
393
394 // Rename.
395 TemporaryFile temp_file2;
396 std::string script2("rename(\"" + std::string(temp_file1.path) + "\", \"" +
397 std::string(temp_file2.path) + "\")");
398 expect(temp_file2.path, script2.c_str(), kNoCause);
399
400 // Already renamed.
401 expect(temp_file2.path, script2.c_str(), kNoCause);
402
403 // Parents create successfully.
404 TemporaryFile temp_file3;
405 TemporaryDir td;
406 std::string temp_dir(td.path);
407 std::string dst_file = temp_dir + "/aaa/bbb/a.txt";
408 std::string script3("rename(\"" + std::string(temp_file3.path) + "\", \"" + dst_file + "\")");
409 expect(dst_file.c_str(), script3.c_str(), kNoCause);
410
411 // Clean up the temp files under td.
412 ASSERT_EQ(0, unlink(dst_file.c_str()));
413 ASSERT_EQ(0, rmdir((temp_dir + "/aaa/bbb").c_str()));
414 ASSERT_EQ(0, rmdir((temp_dir + "/aaa").c_str()));
415}
416
417TEST_F(UpdaterTest, symlink) {
418 // symlink expects 1+ argument.
419 expect(nullptr, "symlink()", kArgsParsingFailure);
420
421 // symlink should fail if src is an empty string.
422 TemporaryFile temp_file1;
423 std::string script1("symlink(\"" + std::string(temp_file1.path) + "\", \"\")");
424 expect(nullptr, script1.c_str(), kSymlinkFailure);
425
426 std::string script2("symlink(\"" + std::string(temp_file1.path) + "\", \"src1\", \"\")");
427 expect(nullptr, script2.c_str(), kSymlinkFailure);
428
429 // symlink failed to remove old src.
430 std::string script3("symlink(\"" + std::string(temp_file1.path) + "\", \"/proc\")");
431 expect(nullptr, script3.c_str(), kSymlinkFailure);
432
433 // symlink can create symlinks.
434 TemporaryFile temp_file;
435 std::string content = "magicvalue";
436 ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file.path));
437
438 TemporaryDir td;
439 std::string src1 = std::string(td.path) + "/symlink1";
440 std::string src2 = std::string(td.path) + "/symlink2";
441 std::string script4("symlink(\"" + std::string(temp_file.path) + "\", \"" + src1 + "\", \"" +
442 src2 + "\")");
443 expect("t", script4.c_str(), kNoCause);
444
445 // Verify the created symlinks.
446 struct stat sb;
447 ASSERT_TRUE(lstat(src1.c_str(), &sb) == 0 && S_ISLNK(sb.st_mode));
448 ASSERT_TRUE(lstat(src2.c_str(), &sb) == 0 && S_ISLNK(sb.st_mode));
449
450 // Clean up the leftovers.
451 ASSERT_EQ(0, unlink(src1.c_str()));
452 ASSERT_EQ(0, unlink(src2.c_str()));
453}
454
455TEST_F(UpdaterTest, package_extract_dir) {
456 // package_extract_dir expects 2 arguments.
457 expect(nullptr, "package_extract_dir()", kArgsParsingFailure);
458 expect(nullptr, "package_extract_dir(\"arg1\")", kArgsParsingFailure);
459 expect(nullptr, "package_extract_dir(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
460
461 std::string zip_path = from_testdata_base("ziptest_valid.zip");
462 ZipArchiveHandle handle;
463 ASSERT_EQ(0, OpenArchive(zip_path.c_str(), &handle));
464
465 // Need to set up the ziphandle.
466 SetUpdaterOtaPackageHandle(handle);
467
468 // Extract "b/c.txt" and "b/d.txt" with package_extract_dir("b", "<dir>").
469 TemporaryDir td;
470 std::string temp_dir(td.path);
471 std::string script("package_extract_dir(\"b\", \"" + temp_dir + "\")");
472 expect("t", script.c_str(), kNoCause, &updater_);
473
474 // Verify.
475 std::string data;
476 std::string file_c = temp_dir + "/c.txt";
477 ASSERT_TRUE(android::base::ReadFileToString(file_c, &data));
478 ASSERT_EQ(kCTxtContents, data);
479
480 std::string file_d = temp_dir + "/d.txt";
481 ASSERT_TRUE(android::base::ReadFileToString(file_d, &data));
482 ASSERT_EQ(kDTxtContents, data);
483
484 // Modify the contents in order to retry. It's expected to be overwritten.
485 ASSERT_TRUE(android::base::WriteStringToFile("random", file_c));
486 ASSERT_TRUE(android::base::WriteStringToFile("random", file_d));
487
488 // Extract again and verify.
489 expect("t", script.c_str(), kNoCause, &updater_);
490
491 ASSERT_TRUE(android::base::ReadFileToString(file_c, &data));
492 ASSERT_EQ(kCTxtContents, data);
493 ASSERT_TRUE(android::base::ReadFileToString(file_d, &data));
494 ASSERT_EQ(kDTxtContents, data);
495
496 // Clean up the temp files under td.
497 ASSERT_EQ(0, unlink(file_c.c_str()));
498 ASSERT_EQ(0, unlink(file_d.c_str()));
499
500 // Extracting "b/" (with slash) should give the same result.
501 script = "package_extract_dir(\"b/\", \"" + temp_dir + "\")";
502 expect("t", script.c_str(), kNoCause, &updater_);
503
504 ASSERT_TRUE(android::base::ReadFileToString(file_c, &data));
505 ASSERT_EQ(kCTxtContents, data);
506 ASSERT_TRUE(android::base::ReadFileToString(file_d, &data));
507 ASSERT_EQ(kDTxtContents, data);
508
509 ASSERT_EQ(0, unlink(file_c.c_str()));
510 ASSERT_EQ(0, unlink(file_d.c_str()));
511
512 // Extracting "" is allowed. The entries will carry the path name.
513 script = "package_extract_dir(\"\", \"" + temp_dir + "\")";
514 expect("t", script.c_str(), kNoCause, &updater_);
515
516 std::string file_a = temp_dir + "/a.txt";
517 ASSERT_TRUE(android::base::ReadFileToString(file_a, &data));
518 ASSERT_EQ(kATxtContents, data);
519 std::string file_b = temp_dir + "/b.txt";
520 ASSERT_TRUE(android::base::ReadFileToString(file_b, &data));
521 ASSERT_EQ(kBTxtContents, data);
522 std::string file_b_c = temp_dir + "/b/c.txt";
523 ASSERT_TRUE(android::base::ReadFileToString(file_b_c, &data));
524 ASSERT_EQ(kCTxtContents, data);
525 std::string file_b_d = temp_dir + "/b/d.txt";
526 ASSERT_TRUE(android::base::ReadFileToString(file_b_d, &data));
527 ASSERT_EQ(kDTxtContents, data);
528
529 ASSERT_EQ(0, unlink(file_a.c_str()));
530 ASSERT_EQ(0, unlink(file_b.c_str()));
531 ASSERT_EQ(0, unlink(file_b_c.c_str()));
532 ASSERT_EQ(0, unlink(file_b_d.c_str()));
533 ASSERT_EQ(0, rmdir((temp_dir + "/b").c_str()));
534
535 // Extracting non-existent entry should still give "t".
536 script = "package_extract_dir(\"doesntexist\", \"" + temp_dir + "\")";
537 expect("t", script.c_str(), kNoCause, &updater_);
538
539 // Only relative zip_path is allowed.
540 script = "package_extract_dir(\"/b\", \"" + temp_dir + "\")";
541 expect("", script.c_str(), kNoCause, &updater_);
542
543 // Only absolute dest_path is allowed.
544 script = "package_extract_dir(\"b\", \"path\")";
545 expect("", script.c_str(), kNoCause, &updater_);
546}
547
Tao Baoef0eb3b2016-11-14 21:29:52 -0800548// TODO: Test extracting to block device.
549TEST_F(UpdaterTest, package_extract_file) {
550 // package_extract_file expects 1 or 2 arguments.
551 expect(nullptr, "package_extract_file()", kArgsParsingFailure);
552 expect(nullptr, "package_extract_file(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
553
554 std::string zip_path = from_testdata_base("ziptest_valid.zip");
555 ZipArchiveHandle handle;
556 ASSERT_EQ(0, OpenArchive(zip_path.c_str(), &handle));
557
558 // Need to set up the ziphandle.
Tianjie Xu58d59122019-05-03 01:05:04 -0700559 SetUpdaterOtaPackageHandle(handle);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800560
561 // Two-argument version.
562 TemporaryFile temp_file1;
563 std::string script("package_extract_file(\"a.txt\", \"" + std::string(temp_file1.path) + "\")");
Tianjie Xu58d59122019-05-03 01:05:04 -0700564 expect("t", script, kNoCause, &updater_);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800565
566 // Verify the extracted entry.
567 std::string data;
568 ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data));
569 ASSERT_EQ(kATxtContents, data);
570
571 // Now extract another entry to the same location, which should overwrite.
572 script = "package_extract_file(\"b.txt\", \"" + std::string(temp_file1.path) + "\")";
Tianjie Xu58d59122019-05-03 01:05:04 -0700573 expect("t", script, kNoCause, &updater_);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800574
575 ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data));
576 ASSERT_EQ(kBTxtContents, data);
577
578 // Missing zip entry. The two-argument version doesn't abort.
579 script = "package_extract_file(\"doesntexist\", \"" + std::string(temp_file1.path) + "\")";
Tianjie Xu58d59122019-05-03 01:05:04 -0700580 expect("", script, kNoCause, &updater_);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800581
582 // Extract to /dev/full should fail.
583 script = "package_extract_file(\"a.txt\", \"/dev/full\")";
Tianjie Xu58d59122019-05-03 01:05:04 -0700584 expect("", script, kNoCause, &updater_);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800585
Tao Bao0b58e9a2018-07-06 15:01:05 -0700586 // One-argument version. package_extract_file() gives a VAL_BLOB, which needs to be converted to
587 // VAL_STRING for equality test.
588 script = "blob_to_string(package_extract_file(\"a.txt\")) == \"" + kATxtContents + "\"";
Tianjie Xu58d59122019-05-03 01:05:04 -0700589 expect("t", script, kNoCause, &updater_);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800590
Tao Bao0b58e9a2018-07-06 15:01:05 -0700591 script = "blob_to_string(package_extract_file(\"b.txt\")) == \"" + kBTxtContents + "\"";
Tianjie Xu58d59122019-05-03 01:05:04 -0700592 expect("t", script, kNoCause, &updater_);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800593
594 // Missing entry. The one-argument version aborts the evaluation.
595 script = "package_extract_file(\"doesntexist\")";
Tianjie Xu58d59122019-05-03 01:05:04 -0700596 expect(nullptr, script, kPackageExtractFileFailure, &updater_);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800597}
Tao Baod0f30882016-11-03 23:52:01 -0700598
Tao Baobafd6c72018-07-09 15:08:50 -0700599TEST_F(UpdaterTest, read_file) {
600 // read_file() expects one argument.
601 expect(nullptr, "read_file()", kArgsParsingFailure);
602 expect(nullptr, "read_file(\"arg1\", \"arg2\")", kArgsParsingFailure);
603
604 // Write some value to file and read back.
605 TemporaryFile temp_file;
606 std::string script("write_value(\"foo\", \""s + temp_file.path + "\");");
607 expect("t", script, kNoCause);
608
609 script = "read_file(\""s + temp_file.path + "\") == \"foo\"";
610 expect("t", script, kNoCause);
611
612 script = "read_file(\""s + temp_file.path + "\") == \"bar\"";
613 expect("", script, kNoCause);
614
615 // It should fail gracefully when read fails.
616 script = "read_file(\"/doesntexist\")";
617 expect("", script, kNoCause);
618}
619
Tianjie Xu69ffa152018-08-01 16:40:00 -0700620TEST_F(UpdaterTest, compute_hash_tree_smoke) {
621 std::string data;
622 for (unsigned char i = 0; i < 128; i++) {
623 data += std::string(4096, i);
624 }
625 // Appends an additional block for verity data.
626 data += std::string(4096, 0);
627 ASSERT_EQ(129 * 4096, data.size());
628 ASSERT_TRUE(android::base::WriteStringToFile(data, image_file_));
629
630 std::string salt = "aee087a5be3b982978c923f566a94613496b417f2af592639bc80d141e34dfe7";
631 std::string expected_root_hash =
632 "7e0a8d8747f54384014ab996f5b2dc4eb7ff00c630eede7134c9e3f05c0dd8ca";
633 // hash_tree_ranges, source_ranges, hash_algorithm, salt_hex, root_hash
634 std::vector<std::string> tokens{ "compute_hash_tree", "2,128,129", "2,0,128", "sha256", salt,
635 expected_root_hash };
636 std::string hash_tree_command = android::base::Join(tokens, " ");
637
638 std::vector<std::string> transfer_list{
639 "4", "2", "0", "2", hash_tree_command,
640 };
641
642 PackageEntries entries{
643 { "new_data", "" },
644 { "patch_data", "" },
645 { "transfer_list", android::base::Join(transfer_list, "\n") },
646 };
647
648 RunBlockImageUpdate(false, entries, image_file_, "t");
649
650 std::string updated;
651 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated));
652 ASSERT_EQ(129 * 4096, updated.size());
653 ASSERT_EQ(data.substr(0, 128 * 4096), updated.substr(0, 128 * 4096));
654
655 // Computes the SHA256 of the salt + hash_tree_data and expects the result to match with the
656 // root_hash.
657 std::vector<unsigned char> salt_bytes;
658 ASSERT_TRUE(HashTreeBuilder::ParseBytesArrayFromString(salt, &salt_bytes));
659 std::vector<unsigned char> hash_tree = std::move(salt_bytes);
660 hash_tree.insert(hash_tree.end(), updated.begin() + 128 * 4096, updated.end());
661
662 std::vector<unsigned char> digest(SHA256_DIGEST_LENGTH);
663 SHA256(hash_tree.data(), hash_tree.size(), digest.data());
664 ASSERT_EQ(expected_root_hash, HashTreeBuilder::BytesArrayToString(digest));
665}
666
667TEST_F(UpdaterTest, compute_hash_tree_root_mismatch) {
668 std::string data;
669 for (size_t i = 0; i < 128; i++) {
670 data += std::string(4096, i);
671 }
672 // Appends an additional block for verity data.
673 data += std::string(4096, 0);
674 ASSERT_EQ(129 * 4096, data.size());
675 // Corrupts one bit
676 data[4096] = 'A';
677 ASSERT_TRUE(android::base::WriteStringToFile(data, image_file_));
678
679 std::string salt = "aee087a5be3b982978c923f566a94613496b417f2af592639bc80d141e34dfe7";
680 std::string expected_root_hash =
681 "7e0a8d8747f54384014ab996f5b2dc4eb7ff00c630eede7134c9e3f05c0dd8ca";
682 // hash_tree_ranges, source_ranges, hash_algorithm, salt_hex, root_hash
683 std::vector<std::string> tokens{ "compute_hash_tree", "2,128,129", "2,0,128", "sha256", salt,
684 expected_root_hash };
685 std::string hash_tree_command = android::base::Join(tokens, " ");
686
687 std::vector<std::string> transfer_list{
688 "4", "2", "0", "2", hash_tree_command,
689 };
690
691 PackageEntries entries{
692 { "new_data", "" },
693 { "patch_data", "" },
694 { "transfer_list", android::base::Join(transfer_list, "\n") },
695 };
696
697 RunBlockImageUpdate(false, entries, image_file_, "", kHashTreeComputationFailure);
698}
699
Tao Baod0f30882016-11-03 23:52:01 -0700700TEST_F(UpdaterTest, write_value) {
701 // write_value() expects two arguments.
702 expect(nullptr, "write_value()", kArgsParsingFailure);
703 expect(nullptr, "write_value(\"arg1\")", kArgsParsingFailure);
704 expect(nullptr, "write_value(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
705
706 // filename cannot be empty.
707 expect(nullptr, "write_value(\"value\", \"\")", kArgsParsingFailure);
708
709 // Write some value to file.
710 TemporaryFile temp_file;
711 std::string value = "magicvalue";
712 std::string script("write_value(\"" + value + "\", \"" + std::string(temp_file.path) + "\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700713 expect("t", script, kNoCause);
Tao Baod0f30882016-11-03 23:52:01 -0700714
715 // Verify the content.
716 std::string content;
717 ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content));
718 ASSERT_EQ(value, content);
719
720 // Allow writing empty string.
721 script = "write_value(\"\", \"" + std::string(temp_file.path) + "\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700722 expect("t", script, kNoCause);
Tao Baod0f30882016-11-03 23:52:01 -0700723
724 // Verify the content.
725 ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content));
726 ASSERT_EQ("", content);
727
728 // It should fail gracefully when write fails.
729 script = "write_value(\"value\", \"/proc/0/file1\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700730 expect("", script, kNoCause);
Tao Baod0f30882016-11-03 23:52:01 -0700731}
Tao Baobedf5fc2016-11-18 12:01:26 -0800732
733TEST_F(UpdaterTest, get_stage) {
734 // get_stage() expects one argument.
735 expect(nullptr, "get_stage()", kArgsParsingFailure);
736 expect(nullptr, "get_stage(\"arg1\", \"arg2\")", kArgsParsingFailure);
737 expect(nullptr, "get_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
738
739 // Set up a local file as BCB.
740 TemporaryFile tf;
741 std::string temp_file(tf.path);
742 bootloader_message boot;
743 strlcpy(boot.stage, "2/3", sizeof(boot.stage));
744 std::string err;
745 ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err));
746
747 // Can read the stage value.
748 std::string script("get_stage(\"" + temp_file + "\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700749 expect("2/3", script, kNoCause);
Tao Baobedf5fc2016-11-18 12:01:26 -0800750
751 // Bad BCB path.
752 script = "get_stage(\"doesntexist\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700753 expect("", script, kNoCause);
Tao Baobedf5fc2016-11-18 12:01:26 -0800754}
755
756TEST_F(UpdaterTest, set_stage) {
757 // set_stage() expects two arguments.
758 expect(nullptr, "set_stage()", kArgsParsingFailure);
759 expect(nullptr, "set_stage(\"arg1\")", kArgsParsingFailure);
760 expect(nullptr, "set_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
761
762 // Set up a local file as BCB.
763 TemporaryFile tf;
764 std::string temp_file(tf.path);
765 bootloader_message boot;
766 strlcpy(boot.command, "command", sizeof(boot.command));
767 strlcpy(boot.stage, "2/3", sizeof(boot.stage));
768 std::string err;
769 ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err));
770
771 // Write with set_stage().
772 std::string script("set_stage(\"" + temp_file + "\", \"1/3\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700773 expect(tf.path, script, kNoCause);
Tao Baobedf5fc2016-11-18 12:01:26 -0800774
775 // Verify.
776 bootloader_message boot_verify;
777 ASSERT_TRUE(read_bootloader_message_from(&boot_verify, temp_file, &err));
778
779 // Stage should be updated, with command part untouched.
780 ASSERT_STREQ("1/3", boot_verify.stage);
781 ASSERT_STREQ(boot.command, boot_verify.command);
782
783 // Bad BCB path.
784 script = "set_stage(\"doesntexist\", \"1/3\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700785 expect("", script, kNoCause);
Tao Baobedf5fc2016-11-18 12:01:26 -0800786
787 script = "set_stage(\"/dev/full\", \"1/3\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700788 expect("", script, kNoCause);
Tao Baobedf5fc2016-11-18 12:01:26 -0800789}
Tao Bao9aa7ab52017-01-05 17:27:19 -0800790
791TEST_F(UpdaterTest, set_progress) {
792 // set_progress() expects one argument.
793 expect(nullptr, "set_progress()", kArgsParsingFailure);
794 expect(nullptr, "set_progress(\"arg1\", \"arg2\")", kArgsParsingFailure);
795
796 // Invalid progress argument.
797 expect(nullptr, "set_progress(\"arg1\")", kArgsParsingFailure);
798 expect(nullptr, "set_progress(\"3x+5\")", kArgsParsingFailure);
799 expect(nullptr, "set_progress(\".3.5\")", kArgsParsingFailure);
800
801 TemporaryFile tf;
Tianjie Xu58d59122019-05-03 01:05:04 -0700802 SetUpdaterCmdPipe(tf.release());
803 expect(".52", "set_progress(\".52\")", kNoCause, &updater_);
804 FlushUpdaterCommandPipe();
Tao Bao9aa7ab52017-01-05 17:27:19 -0800805
806 std::string cmd;
807 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd));
808 ASSERT_EQ(android::base::StringPrintf("set_progress %f\n", .52), cmd);
809 // recovery-updater protocol expects 2 tokens ("set_progress <frac>").
810 ASSERT_EQ(2U, android::base::Split(cmd, " ").size());
811}
812
813TEST_F(UpdaterTest, show_progress) {
814 // show_progress() expects two arguments.
815 expect(nullptr, "show_progress()", kArgsParsingFailure);
816 expect(nullptr, "show_progress(\"arg1\")", kArgsParsingFailure);
817 expect(nullptr, "show_progress(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
818
819 // Invalid progress arguments.
820 expect(nullptr, "show_progress(\"arg1\", \"arg2\")", kArgsParsingFailure);
821 expect(nullptr, "show_progress(\"3x+5\", \"10\")", kArgsParsingFailure);
822 expect(nullptr, "show_progress(\".3\", \"5a\")", kArgsParsingFailure);
823
824 TemporaryFile tf;
Tianjie Xu58d59122019-05-03 01:05:04 -0700825 SetUpdaterCmdPipe(tf.release());
826 expect(".52", "show_progress(\".52\", \"10\")", kNoCause, &updater_);
827 FlushUpdaterCommandPipe();
Tao Bao9aa7ab52017-01-05 17:27:19 -0800828
829 std::string cmd;
830 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd));
831 ASSERT_EQ(android::base::StringPrintf("progress %f %d\n", .52, 10), cmd);
832 // recovery-updater protocol expects 3 tokens ("progress <frac> <secs>").
833 ASSERT_EQ(3U, android::base::Split(cmd, " ").size());
834}
Tianjie Xu56ebe622017-03-16 00:48:21 -0700835
Tao Baoffede3e2018-06-07 09:56:19 -0700836TEST_F(UpdaterTest, block_image_update_parsing_error) {
837 std::vector<std::string> transfer_list{
838 // clang-format off
839 "4",
840 "2",
841 "0",
842 // clang-format on
843 };
844
845 PackageEntries entries{
846 { "new_data", "" },
847 { "patch_data", "" },
848 { "transfer_list", android::base::Join(transfer_list, '\n') },
849 };
850
851 RunBlockImageUpdate(false, entries, image_file_, "", kArgsParsingFailure);
852}
853
Tao Bao3cb3c522018-11-02 13:36:58 -0700854// Generates the bsdiff of the given source and target images, and writes the result entries.
855// target_blocks specifies the block count to be written into the `bsdiff` command, which may be
856// different from the given target size in order to trigger overrun / underrun paths.
857static void GetEntriesForBsdiff(std::string_view source, std::string_view target,
858 size_t target_blocks, PackageEntries* entries) {
Tianjie Xu5450c842017-10-18 13:15:21 -0700859 // Generate the patch data.
Tianjie Xu56ebe622017-03-16 00:48:21 -0700860 TemporaryFile patch_file;
Tao Bao3cb3c522018-11-02 13:36:58 -0700861 ASSERT_EQ(0, bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(source.data()), source.size(),
862 reinterpret_cast<const uint8_t*>(target.data()), target.size(),
863 patch_file.path, nullptr));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700864 std::string patch_content;
865 ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch_content));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700866
Tianjie Xu5450c842017-10-18 13:15:21 -0700867 // Create the transfer list that contains a bsdiff.
Tao Bao3cb3c522018-11-02 13:36:58 -0700868 std::string src_hash = GetSha1(source);
869 std::string tgt_hash = GetSha1(target);
870 size_t source_blocks = source.size() / 4096;
Tao Baobc4a6d52018-05-23 00:12:21 -0700871 std::vector<std::string> transfer_list{
872 // clang-format off
Tianjie Xu56ebe622017-03-16 00:48:21 -0700873 "4",
Tao Bao3cb3c522018-11-02 13:36:58 -0700874 std::to_string(target_blocks),
Tianjie Xu56ebe622017-03-16 00:48:21 -0700875 "0",
Tao Bao3cb3c522018-11-02 13:36:58 -0700876 "0",
877 // bsdiff patch_offset patch_length source_hash target_hash target_range source_block_count
878 // source_range
879 android::base::StringPrintf("bsdiff 0 %zu %s %s 2,0,%zu %zu 2,0,%zu", patch_content.size(),
880 src_hash.c_str(), tgt_hash.c_str(), target_blocks, source_blocks,
881 source_blocks),
Tao Baobc4a6d52018-05-23 00:12:21 -0700882 // clang-format on
Tianjie Xu56ebe622017-03-16 00:48:21 -0700883 };
Tianjie Xu56ebe622017-03-16 00:48:21 -0700884
Tao Bao3cb3c522018-11-02 13:36:58 -0700885 *entries = {
Tianjie Xu5450c842017-10-18 13:15:21 -0700886 { "new_data", "" },
887 { "patch_data", patch_content },
888 { "transfer_list", android::base::Join(transfer_list, '\n') },
Tianjie Xu56ebe622017-03-16 00:48:21 -0700889 };
Tao Bao3cb3c522018-11-02 13:36:58 -0700890}
Tianjie Xu5450c842017-10-18 13:15:21 -0700891
Tao Bao3cb3c522018-11-02 13:36:58 -0700892TEST_F(UpdaterTest, block_image_update_patch_data) {
893 // Both source and target images have 10 blocks.
894 std::string source =
895 std::string(4096, 'a') + std::string(4096, 'c') + std::string(4096 * 3, '\0');
896 std::string target =
897 std::string(4096, 'b') + std::string(4096, 'd') + std::string(4096 * 3, '\0');
898 ASSERT_TRUE(android::base::WriteStringToFile(source, image_file_));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700899
Tao Bao3cb3c522018-11-02 13:36:58 -0700900 PackageEntries entries;
901 GetEntriesForBsdiff(std::string_view(source).substr(0, 4096 * 2),
902 std::string_view(target).substr(0, 4096 * 2), 2, &entries);
Tao Baobc4a6d52018-05-23 00:12:21 -0700903 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu56ebe622017-03-16 00:48:21 -0700904
Tianjie Xu56ebe622017-03-16 00:48:21 -0700905 // The update_file should be patched correctly.
Tao Bao3cb3c522018-11-02 13:36:58 -0700906 std::string updated;
907 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated));
908 ASSERT_EQ(target, updated);
909}
910
911TEST_F(UpdaterTest, block_image_update_patch_overrun) {
912 // Both source and target images have 10 blocks.
913 std::string source =
914 std::string(4096, 'a') + std::string(4096, 'c') + std::string(4096 * 3, '\0');
915 std::string target =
916 std::string(4096, 'b') + std::string(4096, 'd') + std::string(4096 * 3, '\0');
917 ASSERT_TRUE(android::base::WriteStringToFile(source, image_file_));
918
919 // Provide one less block to trigger the overrun path.
920 PackageEntries entries;
921 GetEntriesForBsdiff(std::string_view(source).substr(0, 4096 * 2),
922 std::string_view(target).substr(0, 4096 * 2), 1, &entries);
923
924 // The update should fail due to overrun.
925 RunBlockImageUpdate(false, entries, image_file_, "", kPatchApplicationFailure);
Tianjie Xu5450c842017-10-18 13:15:21 -0700926}
927
Tao Baoa2cff952018-11-02 15:44:07 -0700928TEST_F(UpdaterTest, block_image_update_patch_underrun) {
Tao Bao3cb3c522018-11-02 13:36:58 -0700929 // Both source and target images have 10 blocks.
930 std::string source =
931 std::string(4096, 'a') + std::string(4096, 'c') + std::string(4096 * 3, '\0');
932 std::string target =
933 std::string(4096, 'b') + std::string(4096, 'd') + std::string(4096 * 3, '\0');
934 ASSERT_TRUE(android::base::WriteStringToFile(source, image_file_));
Tao Baoa2cff952018-11-02 15:44:07 -0700935
Tao Bao3cb3c522018-11-02 13:36:58 -0700936 // Provide one more block to trigger the overrun path.
937 PackageEntries entries;
938 GetEntriesForBsdiff(std::string_view(source).substr(0, 4096 * 2),
939 std::string_view(target).substr(0, 4096 * 2), 3, &entries);
Tao Baoa2cff952018-11-02 15:44:07 -0700940
941 // The update should fail due to underrun.
942 RunBlockImageUpdate(false, entries, image_file_, "", kPatchApplicationFailure);
943}
944
Tianjie Xu5450c842017-10-18 13:15:21 -0700945TEST_F(UpdaterTest, block_image_update_fail) {
946 std::string src_content(4096 * 2, 'e');
Tao Bao3cb3c522018-11-02 13:36:58 -0700947 std::string src_hash = GetSha1(src_content);
Tianjie Xu5450c842017-10-18 13:15:21 -0700948 // Stash and free some blocks, then fail the update intentionally.
Tao Baobc4a6d52018-05-23 00:12:21 -0700949 std::vector<std::string> transfer_list{
950 // clang-format off
951 "4",
952 "2",
953 "0",
954 "2",
955 "stash " + src_hash + " 2,0,2",
956 "free " + src_hash,
Tao Bao91a649a2018-05-21 16:05:56 -0700957 "abort",
Tao Baobc4a6d52018-05-23 00:12:21 -0700958 // clang-format on
Tianjie Xu5450c842017-10-18 13:15:21 -0700959 };
960
961 // Add a new data of 10 bytes to test the deadlock.
Tao Baobc4a6d52018-05-23 00:12:21 -0700962 PackageEntries entries{
Tianjie Xu5450c842017-10-18 13:15:21 -0700963 { "new_data", std::string(10, 0) },
964 { "patch_data", "" },
965 { "transfer_list", android::base::Join(transfer_list, '\n') },
966 };
967
Tao Baobc4a6d52018-05-23 00:12:21 -0700968 ASSERT_TRUE(android::base::WriteStringToFile(src_content, image_file_));
Tianjie Xu5450c842017-10-18 13:15:21 -0700969
Tao Baobc4a6d52018-05-23 00:12:21 -0700970 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu5450c842017-10-18 13:15:21 -0700971
Tianjie Xu56ebe622017-03-16 00:48:21 -0700972 // Updater generates the stash name based on the input file name.
Tao Bao3cb3c522018-11-02 13:36:58 -0700973 std::string name_digest = GetSha1(image_file_);
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800974 std::string stash_base = std::string(temp_stash_base_.path) + "/" + name_digest;
Tianjie Xu56ebe622017-03-16 00:48:21 -0700975 ASSERT_EQ(0, access(stash_base.c_str(), F_OK));
Tao Baobc4a6d52018-05-23 00:12:21 -0700976 // Expect the stashed blocks to be freed.
Tianjie Xu5450c842017-10-18 13:15:21 -0700977 ASSERT_EQ(-1, access((stash_base + src_hash).c_str(), F_OK));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700978 ASSERT_EQ(0, rmdir(stash_base.c_str()));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700979}
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700980
Tianjie Xu5450c842017-10-18 13:15:21 -0700981TEST_F(UpdaterTest, new_data_over_write) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700982 std::vector<std::string> transfer_list{
983 // clang-format off
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700984 "4",
985 "1",
986 "0",
987 "0",
988 "new 2,0,1",
Tao Baobc4a6d52018-05-23 00:12:21 -0700989 // clang-format on
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700990 };
Tianjie Xu5450c842017-10-18 13:15:21 -0700991
Tao Baobc4a6d52018-05-23 00:12:21 -0700992 // Write 4096 + 100 bytes of new data.
993 PackageEntries entries{
994 { "new_data", std::string(4196, 0) },
Tianjie Xu5450c842017-10-18 13:15:21 -0700995 { "patch_data", "" },
996 { "transfer_list", android::base::Join(transfer_list, '\n') },
997 };
998
Tao Baobc4a6d52018-05-23 00:12:21 -0700999 RunBlockImageUpdate(false, entries, image_file_, "t");
1000}
Tianjie Xu3a8d98d2017-04-03 20:01:17 -07001001
Tao Baobc4a6d52018-05-23 00:12:21 -07001002TEST_F(UpdaterTest, new_data_short_write) {
1003 std::vector<std::string> transfer_list{
1004 // clang-format off
1005 "4",
1006 "1",
1007 "0",
1008 "0",
1009 "new 2,0,1",
1010 // clang-format on
1011 };
Tianjie Xu3a8d98d2017-04-03 20:01:17 -07001012
Tao Baobc4a6d52018-05-23 00:12:21 -07001013 PackageEntries entries{
1014 { "patch_data", "" },
1015 { "transfer_list", android::base::Join(transfer_list, '\n') },
1016 };
Tianjie Xu3a8d98d2017-04-03 20:01:17 -07001017
1018 // Updater should report the failure gracefully rather than stuck in deadlock.
Tao Baobc4a6d52018-05-23 00:12:21 -07001019 entries["new_data"] = "";
1020 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu3a8d98d2017-04-03 20:01:17 -07001021
Tao Baobc4a6d52018-05-23 00:12:21 -07001022 entries["new_data"] = std::string(10, 'a');
1023 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu3a8d98d2017-04-03 20:01:17 -07001024
1025 // Expect to write 1 block of new data successfully.
Tao Baobc4a6d52018-05-23 00:12:21 -07001026 entries["new_data"] = std::string(4096, 'a');
1027 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu107a34f2017-06-29 17:04:21 -07001028}
1029
1030TEST_F(UpdaterTest, brotli_new_data) {
Tianjie Xu107a34f2017-06-29 17:04:21 -07001031 auto generator = []() { return rand() % 128; };
Tianjie Xu6ed175d2017-07-18 11:29:40 -07001032 // Generate 100 blocks of random data.
Tianjie Xu107a34f2017-06-29 17:04:21 -07001033 std::string brotli_new_data;
Tianjie Xu6ed175d2017-07-18 11:29:40 -07001034 brotli_new_data.reserve(4096 * 100);
1035 generate_n(back_inserter(brotli_new_data), 4096 * 100, generator);
Tianjie Xu107a34f2017-06-29 17:04:21 -07001036
1037 size_t encoded_size = BrotliEncoderMaxCompressedSize(brotli_new_data.size());
Tianjie Xu5450c842017-10-18 13:15:21 -07001038 std::string encoded_data(encoded_size, 0);
Tianjie Xu107a34f2017-06-29 17:04:21 -07001039 ASSERT_TRUE(BrotliEncoderCompress(
1040 BROTLI_DEFAULT_QUALITY, BROTLI_DEFAULT_WINDOW, BROTLI_DEFAULT_MODE, brotli_new_data.size(),
Tianjie Xu5450c842017-10-18 13:15:21 -07001041 reinterpret_cast<const uint8_t*>(brotli_new_data.data()), &encoded_size,
1042 reinterpret_cast<uint8_t*>(const_cast<char*>(encoded_data.data()))));
1043 encoded_data.resize(encoded_size);
Tianjie Xu107a34f2017-06-29 17:04:21 -07001044
Tianjie Xu6ed175d2017-07-18 11:29:40 -07001045 // Write a few small chunks of new data, then a large chunk, and finally a few small chunks.
1046 // This helps us to catch potential short writes.
Tianjie Xu107a34f2017-06-29 17:04:21 -07001047 std::vector<std::string> transfer_list = {
Tianjie Xu6ed175d2017-07-18 11:29:40 -07001048 "4",
1049 "100",
1050 "0",
1051 "0",
1052 "new 2,0,1",
1053 "new 2,1,2",
1054 "new 4,2,50,50,97",
1055 "new 2,97,98",
1056 "new 2,98,99",
1057 "new 2,99,100",
Tianjie Xu107a34f2017-06-29 17:04:21 -07001058 };
Tianjie Xu5450c842017-10-18 13:15:21 -07001059
Tao Baobc4a6d52018-05-23 00:12:21 -07001060 PackageEntries entries{
1061 { "new_data.br", std::move(encoded_data) },
Tianjie Xu5450c842017-10-18 13:15:21 -07001062 { "patch_data", "" },
1063 { "transfer_list", android::base::Join(transfer_list, '\n') },
1064 };
1065
Tao Baobc4a6d52018-05-23 00:12:21 -07001066 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu107a34f2017-06-29 17:04:21 -07001067
1068 std::string updated_content;
Tao Baobc4a6d52018-05-23 00:12:21 -07001069 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_content));
Tianjie Xu107a34f2017-06-29 17:04:21 -07001070 ASSERT_EQ(brotli_new_data, updated_content);
Tianjie Xu3a8d98d2017-04-03 20:01:17 -07001071}
Tianjie Xu284752e2017-12-05 11:04:17 -08001072
1073TEST_F(UpdaterTest, last_command_update) {
Tao Baobc4a6d52018-05-23 00:12:21 -07001074 std::string block1(4096, '1');
1075 std::string block2(4096, '2');
1076 std::string block3(4096, '3');
Tao Bao3cb3c522018-11-02 13:36:58 -07001077 std::string block1_hash = GetSha1(block1);
1078 std::string block2_hash = GetSha1(block2);
1079 std::string block3_hash = GetSha1(block3);
Tianjie Xu284752e2017-12-05 11:04:17 -08001080
1081 // Compose the transfer list to fail the first update.
Tao Baobc4a6d52018-05-23 00:12:21 -07001082 std::vector<std::string> transfer_list_fail{
1083 // clang-format off
Tianjie Xu284752e2017-12-05 11:04:17 -08001084 "4",
1085 "2",
1086 "0",
1087 "2",
1088 "stash " + block1_hash + " 2,0,1",
1089 "move " + block1_hash + " 2,1,2 1 2,0,1",
1090 "stash " + block3_hash + " 2,2,3",
Tao Bao91a649a2018-05-21 16:05:56 -07001091 "abort",
Tao Baobc4a6d52018-05-23 00:12:21 -07001092 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -08001093 };
1094
1095 // Mimic a resumed update with the same transfer commands.
Tao Baobc4a6d52018-05-23 00:12:21 -07001096 std::vector<std::string> transfer_list_continue{
1097 // clang-format off
Tianjie Xu284752e2017-12-05 11:04:17 -08001098 "4",
1099 "2",
1100 "0",
1101 "2",
1102 "stash " + block1_hash + " 2,0,1",
1103 "move " + block1_hash + " 2,1,2 1 2,0,1",
1104 "stash " + block3_hash + " 2,2,3",
1105 "move " + block1_hash + " 2,2,3 1 2,0,1",
Tao Baobc4a6d52018-05-23 00:12:21 -07001106 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -08001107 };
1108
Tao Baobc4a6d52018-05-23 00:12:21 -07001109 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_));
1110
1111 PackageEntries entries{
Tianjie Xu284752e2017-12-05 11:04:17 -08001112 { "new_data", "" },
1113 { "patch_data", "" },
Tao Baobc4a6d52018-05-23 00:12:21 -07001114 { "transfer_list", android::base::Join(transfer_list_fail, '\n') },
Tianjie Xu284752e2017-12-05 11:04:17 -08001115 };
1116
Tao Baobc4a6d52018-05-23 00:12:21 -07001117 // "2\nstash " + block3_hash + " 2,2,3"
Tao Baof8811bb2018-06-18 10:03:52 -07001118 std::string last_command_content =
1119 "2\n" + transfer_list_fail[TransferList::kTransferListHeaderLines + 2];
Tianjie Xu284752e2017-12-05 11:04:17 -08001120
Tao Baobc4a6d52018-05-23 00:12:21 -07001121 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu284752e2017-12-05 11:04:17 -08001122
1123 // Expect last_command to contain the last stash command.
Tao Baobc4a6d52018-05-23 00:12:21 -07001124 std::string last_command_actual;
Tao Bao7064aa22018-05-24 21:43:50 -07001125 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
Tao Baobc4a6d52018-05-23 00:12:21 -07001126 EXPECT_EQ(last_command_content, last_command_actual);
1127
Tianjie Xu284752e2017-12-05 11:04:17 -08001128 std::string updated_contents;
Tao Baobc4a6d52018-05-23 00:12:21 -07001129 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_contents));
Tianjie Xu284752e2017-12-05 11:04:17 -08001130 ASSERT_EQ(block1 + block1 + block3, updated_contents);
1131
Tao Baobc4a6d52018-05-23 00:12:21 -07001132 // "Resume" the update. Expect the first 'move' to be skipped but the second 'move' to be
1133 // executed. Note that we intentionally reset the image file.
1134 entries["transfer_list"] = android::base::Join(transfer_list_continue, '\n');
1135 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_));
1136 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu284752e2017-12-05 11:04:17 -08001137
Tao Baobc4a6d52018-05-23 00:12:21 -07001138 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_contents));
1139 ASSERT_EQ(block1 + block2 + block1, updated_contents);
Tianjie Xu284752e2017-12-05 11:04:17 -08001140}
1141
1142TEST_F(UpdaterTest, last_command_update_unresumable) {
Tao Baobc4a6d52018-05-23 00:12:21 -07001143 std::string block1(4096, '1');
1144 std::string block2(4096, '2');
Tao Bao3cb3c522018-11-02 13:36:58 -07001145 std::string block1_hash = GetSha1(block1);
1146 std::string block2_hash = GetSha1(block2);
Tianjie Xu284752e2017-12-05 11:04:17 -08001147
1148 // Construct an unresumable update with source blocks mismatch.
Tao Baobc4a6d52018-05-23 00:12:21 -07001149 std::vector<std::string> transfer_list_unresumable{
1150 // clang-format off
1151 "4",
1152 "2",
1153 "0",
1154 "2",
1155 "stash " + block1_hash + " 2,0,1",
1156 "move " + block2_hash + " 2,1,2 1 2,0,1",
1157 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -08001158 };
1159
Tao Baobc4a6d52018-05-23 00:12:21 -07001160 PackageEntries entries{
Tianjie Xu284752e2017-12-05 11:04:17 -08001161 { "new_data", "" },
1162 { "patch_data", "" },
Tao Baobc4a6d52018-05-23 00:12:21 -07001163 { "transfer_list", android::base::Join(transfer_list_unresumable, '\n') },
Tianjie Xu284752e2017-12-05 11:04:17 -08001164 };
1165
Tao Baobc4a6d52018-05-23 00:12:21 -07001166 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block1, image_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -08001167
Tao Baof8811bb2018-06-18 10:03:52 -07001168 std::string last_command_content =
1169 "0\n" + transfer_list_unresumable[TransferList::kTransferListHeaderLines];
Tao Bao7064aa22018-05-24 21:43:50 -07001170 ASSERT_TRUE(android::base::WriteStringToFile(last_command_content, last_command_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -08001171
Tao Baobc4a6d52018-05-23 00:12:21 -07001172 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu284752e2017-12-05 11:04:17 -08001173
Tao Baobc4a6d52018-05-23 00:12:21 -07001174 // The last_command_file will be deleted if the update encounters an unresumable failure later.
Tao Bao7064aa22018-05-24 21:43:50 -07001175 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
Tianjie Xu284752e2017-12-05 11:04:17 -08001176}
1177
1178TEST_F(UpdaterTest, last_command_verify) {
Tao Baobc4a6d52018-05-23 00:12:21 -07001179 std::string block1(4096, '1');
1180 std::string block2(4096, '2');
1181 std::string block3(4096, '3');
Tao Bao3cb3c522018-11-02 13:36:58 -07001182 std::string block1_hash = GetSha1(block1);
1183 std::string block2_hash = GetSha1(block2);
1184 std::string block3_hash = GetSha1(block3);
Tianjie Xu284752e2017-12-05 11:04:17 -08001185
Tao Baobc4a6d52018-05-23 00:12:21 -07001186 std::vector<std::string> transfer_list_verify{
1187 // clang-format off
Tianjie Xu284752e2017-12-05 11:04:17 -08001188 "4",
1189 "2",
1190 "0",
1191 "2",
1192 "stash " + block1_hash + " 2,0,1",
1193 "move " + block1_hash + " 2,0,1 1 2,0,1",
1194 "move " + block1_hash + " 2,1,2 1 2,0,1",
1195 "stash " + block3_hash + " 2,2,3",
Tao Baobc4a6d52018-05-23 00:12:21 -07001196 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -08001197 };
1198
Tao Baobc4a6d52018-05-23 00:12:21 -07001199 PackageEntries entries{
Tianjie Xu284752e2017-12-05 11:04:17 -08001200 { "new_data", "" },
1201 { "patch_data", "" },
Tao Baobc4a6d52018-05-23 00:12:21 -07001202 { "transfer_list", android::base::Join(transfer_list_verify, '\n') },
Tianjie Xu284752e2017-12-05 11:04:17 -08001203 };
1204
Tao Baobc4a6d52018-05-23 00:12:21 -07001205 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block1 + block3, image_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -08001206
Tao Baobc4a6d52018-05-23 00:12:21 -07001207 // Last command: "move " + block1_hash + " 2,1,2 1 2,0,1"
Tao Baof8811bb2018-06-18 10:03:52 -07001208 std::string last_command_content =
1209 "2\n" + transfer_list_verify[TransferList::kTransferListHeaderLines + 2];
Tianjie Xu284752e2017-12-05 11:04:17 -08001210
Tao Baobc4a6d52018-05-23 00:12:21 -07001211 // First run: expect the verification to succeed and the last_command_file is intact.
Tao Bao7064aa22018-05-24 21:43:50 -07001212 ASSERT_TRUE(android::base::WriteStringToFile(last_command_content, last_command_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -08001213
Tao Baobc4a6d52018-05-23 00:12:21 -07001214 RunBlockImageUpdate(true, entries, image_file_, "t");
Tianjie Xu284752e2017-12-05 11:04:17 -08001215
Tao Baobc4a6d52018-05-23 00:12:21 -07001216 std::string last_command_actual;
Tao Bao7064aa22018-05-24 21:43:50 -07001217 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
Tao Baobc4a6d52018-05-23 00:12:21 -07001218 EXPECT_EQ(last_command_content, last_command_actual);
Tianjie Xu284752e2017-12-05 11:04:17 -08001219
Tao Baobc4a6d52018-05-23 00:12:21 -07001220 // Second run with a mismatching block image: expect the verification to succeed but
1221 // last_command_file to be deleted; because the target blocks in the last command don't have the
1222 // expected contents for the second move command.
1223 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_));
1224 RunBlockImageUpdate(true, entries, image_file_, "t");
Tao Bao7064aa22018-05-24 21:43:50 -07001225 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
Tianjie Xu284752e2017-12-05 11:04:17 -08001226}
Tao Baoc0299ed2018-05-24 00:16:35 -07001227
Tianjie Xu58d59122019-05-03 01:05:04 -07001228class ResumableUpdaterTest : public UpdaterTestBase, public testing::TestWithParam<size_t> {
Tao Baoc0299ed2018-05-24 00:16:35 -07001229 protected:
1230 void SetUp() override {
Tianjie Xu58d59122019-05-03 01:05:04 -07001231 UpdaterTestBase::SetUp();
Tao Bao91a649a2018-05-21 16:05:56 -07001232 // Enable a special command "abort" to simulate interruption.
1233 Command::abort_allowed_ = true;
Tao Baoc0299ed2018-05-24 00:16:35 -07001234 index_ = GetParam();
Tao Baoc0299ed2018-05-24 00:16:35 -07001235 }
1236
1237 void TearDown() override {
Tianjie Xu58d59122019-05-03 01:05:04 -07001238 UpdaterTestBase::TearDown();
Tao Baoc0299ed2018-05-24 00:16:35 -07001239 }
1240
Tao Baoc0299ed2018-05-24 00:16:35 -07001241 size_t index_;
Tao Baoc0299ed2018-05-24 00:16:35 -07001242};
1243
1244static std::string g_source_image;
1245static std::string g_target_image;
1246static PackageEntries g_entries;
1247
1248static std::vector<std::string> GenerateTransferList() {
1249 std::string a(4096, 'a');
1250 std::string b(4096, 'b');
1251 std::string c(4096, 'c');
1252 std::string d(4096, 'd');
1253 std::string e(4096, 'e');
1254 std::string f(4096, 'f');
1255 std::string g(4096, 'g');
1256 std::string h(4096, 'h');
1257 std::string i(4096, 'i');
1258 std::string zero(4096, '\0');
1259
Tao Bao3cb3c522018-11-02 13:36:58 -07001260 std::string a_hash = GetSha1(a);
1261 std::string b_hash = GetSha1(b);
1262 std::string c_hash = GetSha1(c);
1263 std::string e_hash = GetSha1(e);
Tao Baoc0299ed2018-05-24 00:16:35 -07001264
1265 auto loc = [](const std::string& range_text) {
1266 std::vector<std::string> pieces = android::base::Split(range_text, "-");
1267 size_t left;
1268 size_t right;
1269 if (pieces.size() == 1) {
1270 CHECK(android::base::ParseUint(pieces[0], &left));
1271 right = left + 1;
1272 } else {
1273 CHECK_EQ(2u, pieces.size());
1274 CHECK(android::base::ParseUint(pieces[0], &left));
1275 CHECK(android::base::ParseUint(pieces[1], &right));
1276 right++;
1277 }
1278 return android::base::StringPrintf("2,%zu,%zu", left, right);
1279 };
1280
1281 // patch 1: "b d c" -> "g"
1282 TemporaryFile patch_file_bdc_g;
1283 std::string bdc = b + d + c;
Tao Bao3cb3c522018-11-02 13:36:58 -07001284 std::string bdc_hash = GetSha1(bdc);
1285 std::string g_hash = GetSha1(g);
Tao Baoc0299ed2018-05-24 00:16:35 -07001286 CHECK_EQ(0, bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(bdc.data()), bdc.size(),
1287 reinterpret_cast<const uint8_t*>(g.data()), g.size(),
1288 patch_file_bdc_g.path, nullptr));
1289 std::string patch_bdc_g;
1290 CHECK(android::base::ReadFileToString(patch_file_bdc_g.path, &patch_bdc_g));
1291
1292 // patch 2: "a b c d" -> "d c b"
1293 TemporaryFile patch_file_abcd_dcb;
1294 std::string abcd = a + b + c + d;
Tao Bao3cb3c522018-11-02 13:36:58 -07001295 std::string abcd_hash = GetSha1(abcd);
Tao Baoc0299ed2018-05-24 00:16:35 -07001296 std::string dcb = d + c + b;
Tao Bao3cb3c522018-11-02 13:36:58 -07001297 std::string dcb_hash = GetSha1(dcb);
Tao Baoc0299ed2018-05-24 00:16:35 -07001298 CHECK_EQ(0, bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(abcd.data()), abcd.size(),
1299 reinterpret_cast<const uint8_t*>(dcb.data()), dcb.size(),
1300 patch_file_abcd_dcb.path, nullptr));
1301 std::string patch_abcd_dcb;
1302 CHECK(android::base::ReadFileToString(patch_file_abcd_dcb.path, &patch_abcd_dcb));
1303
1304 std::vector<std::string> transfer_list{
1305 "4",
1306 "10", // total blocks written
1307 "2", // maximum stash entries
1308 "2", // maximum number of stashed blocks
1309
1310 // a b c d e a b c d e
1311 "stash " + b_hash + " " + loc("1"),
1312 // a b c d e a b c d e [b(1)]
1313 "stash " + c_hash + " " + loc("2"),
1314 // a b c d e a b c d e [b(1)][c(2)]
1315 "new " + loc("1-2"),
1316 // a i h d e a b c d e [b(1)][c(2)]
1317 "zero " + loc("0"),
1318 // 0 i h d e a b c d e [b(1)][c(2)]
1319
1320 // bsdiff "b d c" (from stash, 3, stash) to get g(3)
1321 android::base::StringPrintf(
1322 "bsdiff 0 %zu %s %s %s 3 %s %s %s:%s %s:%s",
1323 patch_bdc_g.size(), // patch start (0), patch length
1324 bdc_hash.c_str(), // source hash
1325 g_hash.c_str(), // target hash
1326 loc("3").c_str(), // target range
1327 loc("3").c_str(), loc("1").c_str(), // load "d" from block 3, into buffer at offset 1
1328 b_hash.c_str(), loc("0").c_str(), // load "b" from stash, into buffer at offset 0
1329 c_hash.c_str(), loc("2").c_str()), // load "c" from stash, into buffer at offset 2
1330
1331 // 0 i h g e a b c d e [b(1)][c(2)]
1332 "free " + b_hash,
1333 // 0 i h g e a b c d e [c(2)]
1334 "free " + a_hash,
1335 // 0 i h g e a b c d e
1336 "stash " + a_hash + " " + loc("5"),
1337 // 0 i h g e a b c d e [a(5)]
1338 "move " + e_hash + " " + loc("5") + " 1 " + loc("4"),
1339 // 0 i h g e e b c d e [a(5)]
1340
1341 // bsdiff "a b c d" (from stash, 6-8) to "d c b" (6-8)
1342 android::base::StringPrintf( //
1343 "bsdiff %zu %zu %s %s %s 4 %s %s %s:%s",
1344 patch_bdc_g.size(), // patch start
1345 patch_bdc_g.size() + patch_abcd_dcb.size(), // patch length
1346 abcd_hash.c_str(), // source hash
1347 dcb_hash.c_str(), // target hash
1348 loc("6-8").c_str(), // target range
1349 loc("6-8").c_str(), // load "b c d" from blocks 6-8
1350 loc("1-3").c_str(), // into buffer at offset 1-3
1351 a_hash.c_str(), // load "a" from stash
1352 loc("0").c_str()), // into buffer at offset 0
1353
1354 // 0 i h g e e d c b e [a(5)]
1355 "new " + loc("4"),
1356 // 0 i h g f e d c b e [a(5)]
1357 "move " + a_hash + " " + loc("9") + " 1 - " + a_hash + ":" + loc("0"),
1358 // 0 i h g f e d c b a [a(5)]
1359 "free " + a_hash,
1360 // 0 i h g f e d c b a
1361 };
1362
1363 std::string new_data = i + h + f;
1364 std::string patch_data = patch_bdc_g + patch_abcd_dcb;
1365
1366 g_entries = {
1367 { "new_data", new_data },
1368 { "patch_data", patch_data },
1369 };
1370 g_source_image = a + b + c + d + e + a + b + c + d + e;
1371 g_target_image = zero + i + h + g + f + e + d + c + b + a;
1372
1373 return transfer_list;
1374}
1375
1376static const std::vector<std::string> g_transfer_list = GenerateTransferList();
1377
1378INSTANTIATE_TEST_CASE_P(InterruptAfterEachCommand, ResumableUpdaterTest,
1379 ::testing::Range(static_cast<size_t>(0),
Tao Baof8811bb2018-06-18 10:03:52 -07001380 g_transfer_list.size() -
1381 TransferList::kTransferListHeaderLines));
Tao Baoc0299ed2018-05-24 00:16:35 -07001382
1383TEST_P(ResumableUpdaterTest, InterruptVerifyResume) {
1384 ASSERT_TRUE(android::base::WriteStringToFile(g_source_image, image_file_));
1385
1386 LOG(INFO) << "Interrupting at line " << index_ << " ("
Tao Baof8811bb2018-06-18 10:03:52 -07001387 << g_transfer_list[TransferList::kTransferListHeaderLines + index_] << ")";
Tao Baoc0299ed2018-05-24 00:16:35 -07001388
1389 std::vector<std::string> transfer_list_copy{ g_transfer_list };
Tao Baof8811bb2018-06-18 10:03:52 -07001390 transfer_list_copy[TransferList::kTransferListHeaderLines + index_] = "abort";
Tao Baoc0299ed2018-05-24 00:16:35 -07001391
1392 g_entries["transfer_list"] = android::base::Join(transfer_list_copy, '\n');
1393
1394 // Run update that's expected to fail.
1395 RunBlockImageUpdate(false, g_entries, image_file_, "");
1396
1397 std::string last_command_expected;
1398
1399 // Assert the last_command_file.
1400 if (index_ == 0) {
1401 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
1402 } else {
Tao Baof8811bb2018-06-18 10:03:52 -07001403 last_command_expected = std::to_string(index_ - 1) + "\n" +
1404 g_transfer_list[TransferList::kTransferListHeaderLines + index_ - 1];
Tao Baoc0299ed2018-05-24 00:16:35 -07001405 std::string last_command_actual;
1406 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
1407 ASSERT_EQ(last_command_expected, last_command_actual);
1408 }
1409
1410 g_entries["transfer_list"] = android::base::Join(g_transfer_list, '\n');
1411
1412 // Resume the interrupted update, by doing verification first.
1413 RunBlockImageUpdate(true, g_entries, image_file_, "t");
1414
1415 // last_command_file should remain intact.
1416 if (index_ == 0) {
1417 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
1418 } else {
1419 std::string last_command_actual;
1420 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
1421 ASSERT_EQ(last_command_expected, last_command_actual);
1422 }
1423
1424 // Resume the update.
1425 RunBlockImageUpdate(false, g_entries, image_file_, "t");
1426
1427 // last_command_file should be gone after successful update.
1428 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
1429
1430 std::string updated_image_actual;
1431 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_image_actual));
1432 ASSERT_EQ(g_target_image, updated_image_actual);
1433}