blob: 484cea9067790af9126bd5bd3dba422cbfa086d2 [file] [log] [blame]
Tao Bao0c7839a2016-10-10 15:48:37 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Tao Bao9aa7ab52017-01-05 17:27:19 -080017#include <stdio.h>
Tianjie Xu107a34f2017-06-29 17:04:21 -070018#include <stdlib.h>
Tao Bao89929022016-11-08 20:51:31 -080019#include <sys/stat.h>
20#include <sys/types.h>
21#include <unistd.h>
22
Tianjie Xu107a34f2017-06-29 17:04:21 -070023#include <algorithm>
Tianjie Xuc4447322017-03-06 14:44:59 -080024#include <memory>
Tao Bao0c7839a2016-10-10 15:48:37 -070025#include <string>
Tao Bao3cb3c522018-11-02 13:36:58 -070026#include <string_view>
Tianjie Xu5450c842017-10-18 13:15:21 -070027#include <unordered_map>
Tianjie Xu56ebe622017-03-16 00:48:21 -070028#include <vector>
Tao Bao0c7839a2016-10-10 15:48:37 -070029
Tao Bao51d516e2016-11-03 14:49:01 -070030#include <android-base/file.h>
Tao Baobc4a6d52018-05-23 00:12:21 -070031#include <android-base/logging.h>
32#include <android-base/parseint.h>
Tao Bao0c7839a2016-10-10 15:48:37 -070033#include <android-base/properties.h>
Tao Bao9aa7ab52017-01-05 17:27:19 -080034#include <android-base/stringprintf.h>
35#include <android-base/strings.h>
Tao Baobedf5fc2016-11-18 12:01:26 -080036#include <bootloader_message/bootloader_message.h>
Tianjie Xu107a34f2017-06-29 17:04:21 -070037#include <brotli/encode.h>
Alex Deymofa188262017-10-10 17:56:17 +020038#include <bsdiff/bsdiff.h>
Tao Bao0c7839a2016-10-10 15:48:37 -070039#include <gtest/gtest.h>
Tianjie Xu69ffa152018-08-01 16:40:00 -070040#include <verity/hash_tree_builder.h>
Tao Baoef0eb3b2016-11-14 21:29:52 -080041#include <ziparchive/zip_archive.h>
Tianjie Xu56ebe622017-03-16 00:48:21 -070042#include <ziparchive/zip_writer.h>
Tao Bao0c7839a2016-10-10 15:48:37 -070043
Tao Bao5609bc82018-06-20 00:30:48 -070044#include "applypatch/applypatch.h"
Tao Baoef0eb3b2016-11-14 21:29:52 -080045#include "common/test_constants.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070046#include "edify/expr.h"
Tao Bao1fc5bf32017-10-06 07:43:41 -070047#include "otautil/error_code.h"
Tao Bao641fa972018-04-25 18:59:40 -070048#include "otautil/paths.h"
Tao Bao09e468f2017-09-29 14:39:33 -070049#include "otautil/print_sha1.h"
Tao Bao17054c02018-05-03 22:41:23 -070050#include "otautil/sysutil.h"
Tao Bao91a649a2018-05-21 16:05:56 -070051#include "private/commands.h"
Tianjie Xu56ebe622017-03-16 00:48:21 -070052#include "updater/blockimg.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070053#include "updater/install.h"
Tao Baoef0eb3b2016-11-14 21:29:52 -080054#include "updater/updater.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070055
Tao Baobafd6c72018-07-09 15:08:50 -070056using namespace std::string_literals;
Tao Bao0c7839a2016-10-10 15:48:37 -070057
Tao Baobc4a6d52018-05-23 00:12:21 -070058using PackageEntries = std::unordered_map<std::string, std::string>;
59
Tao Baobc4a6d52018-05-23 00:12:21 -070060struct selabel_handle* sehandle = nullptr;
Tao Bao0c7839a2016-10-10 15:48:37 -070061
Tao Baod8d514f2018-07-09 13:32:28 -070062static void expect(const char* expected, const std::string& expr_str, CauseCode cause_code,
Tao Baoef0eb3b2016-11-14 21:29:52 -080063 UpdaterInfo* info = nullptr) {
Tianjie Xuc4447322017-03-06 14:44:59 -080064 std::unique_ptr<Expr> e;
Tao Baoef0eb3b2016-11-14 21:29:52 -080065 int error_count = 0;
Tao Baod8d514f2018-07-09 13:32:28 -070066 ASSERT_EQ(0, ParseString(expr_str, &e, &error_count));
Tao Baoef0eb3b2016-11-14 21:29:52 -080067 ASSERT_EQ(0, error_count);
Tao Bao0c7839a2016-10-10 15:48:37 -070068
Tao Baoef0eb3b2016-11-14 21:29:52 -080069 State state(expr_str, info);
Tao Bao0c7839a2016-10-10 15:48:37 -070070
Tao Baoef0eb3b2016-11-14 21:29:52 -080071 std::string result;
72 bool status = Evaluate(&state, e, &result);
Tao Bao0c7839a2016-10-10 15:48:37 -070073
Tao Baoef0eb3b2016-11-14 21:29:52 -080074 if (expected == nullptr) {
75 ASSERT_FALSE(status);
76 } else {
Tao Bao0b58e9a2018-07-06 15:01:05 -070077 ASSERT_TRUE(status) << "Evaluate() finished with error message: " << state.errmsg;
Tao Baoef0eb3b2016-11-14 21:29:52 -080078 ASSERT_STREQ(expected, result.c_str());
79 }
Tao Bao0c7839a2016-10-10 15:48:37 -070080
Tao Baoef0eb3b2016-11-14 21:29:52 -080081 // Error code is set in updater/updater.cpp only, by parsing State.errmsg.
82 ASSERT_EQ(kNoError, state.error_code);
Tao Bao361342c2016-02-08 11:15:50 -080083
Tao Baoef0eb3b2016-11-14 21:29:52 -080084 // Cause code should always be available.
85 ASSERT_EQ(cause_code, state.cause_code);
Tao Bao0c7839a2016-10-10 15:48:37 -070086}
87
Tao Baobc4a6d52018-05-23 00:12:21 -070088static void BuildUpdatePackage(const PackageEntries& entries, int fd) {
Tianjie Xu5450c842017-10-18 13:15:21 -070089 FILE* zip_file_ptr = fdopen(fd, "wb");
90 ZipWriter zip_writer(zip_file_ptr);
91
92 for (const auto& entry : entries) {
Tao Baobc4a6d52018-05-23 00:12:21 -070093 // All the entries are written as STORED.
Tianjie Xu5450c842017-10-18 13:15:21 -070094 ASSERT_EQ(0, zip_writer.StartEntry(entry.first.c_str(), 0));
95 if (!entry.second.empty()) {
96 ASSERT_EQ(0, zip_writer.WriteBytes(entry.second.data(), entry.second.size()));
97 }
98 ASSERT_EQ(0, zip_writer.FinishEntry());
99 }
100
101 ASSERT_EQ(0, zip_writer.Finish());
102 ASSERT_EQ(0, fclose(zip_file_ptr));
103}
104
Tao Baobc4a6d52018-05-23 00:12:21 -0700105static void RunBlockImageUpdate(bool is_verify, const PackageEntries& entries,
Tao Baoffede3e2018-06-07 09:56:19 -0700106 const std::string& image_file, const std::string& result,
107 CauseCode cause_code = kNoCause) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700108 CHECK(entries.find("transfer_list") != entries.end());
109
110 // Build the update package.
111 TemporaryFile zip_file;
112 BuildUpdatePackage(entries, zip_file.release());
113
114 MemMapping map;
115 ASSERT_TRUE(map.MapFile(zip_file.path));
116 ZipArchiveHandle handle;
117 ASSERT_EQ(0, OpenArchiveFromMemory(map.addr, map.length, zip_file.path, &handle));
118
119 // Set up the handler, command_pipe, patch offset & length.
120 UpdaterInfo updater_info;
121 updater_info.package_zip = handle;
122 TemporaryFile temp_pipe;
123 updater_info.cmd_pipe = fdopen(temp_pipe.release(), "wbe");
124 updater_info.package_zip_addr = map.addr;
125 updater_info.package_zip_len = map.length;
126
127 std::string new_data = entries.find("new_data.br") != entries.end() ? "new_data.br" : "new_data";
128 std::string script = is_verify ? "block_image_verify" : "block_image_update";
129 script += R"((")" + image_file + R"(", package_extract_file("transfer_list"), ")" + new_data +
130 R"(", "patch_data"))";
Tao Baod8d514f2018-07-09 13:32:28 -0700131 expect(result.c_str(), script, cause_code, &updater_info);
Tao Baobc4a6d52018-05-23 00:12:21 -0700132
133 ASSERT_EQ(0, fclose(updater_info.cmd_pipe));
134 CloseArchive(handle);
135}
136
Tao Bao3cb3c522018-11-02 13:36:58 -0700137static std::string GetSha1(std::string_view content) {
Tianjie Xu56ebe622017-03-16 00:48:21 -0700138 uint8_t digest[SHA_DIGEST_LENGTH];
Tao Bao3cb3c522018-11-02 13:36:58 -0700139 SHA1(reinterpret_cast<const uint8_t*>(content.data()), content.size(), digest);
Tianjie Xu56ebe622017-03-16 00:48:21 -0700140 return print_sha1(digest);
141}
142
Tao Bao0b58e9a2018-07-06 15:01:05 -0700143static Value* BlobToString(const char* name, State* state,
144 const std::vector<std::unique_ptr<Expr>>& argv) {
145 if (argv.size() != 1) {
146 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
147 }
148
149 std::vector<std::unique_ptr<Value>> args;
150 if (!ReadValueArgs(state, argv, &args)) {
151 return nullptr;
152 }
153
Tao Bao511d7592018-06-19 15:56:49 -0700154 if (args[0]->type != Value::Type::BLOB) {
Tao Bao0b58e9a2018-07-06 15:01:05 -0700155 return ErrorAbort(state, kArgsParsingFailure, "%s() expects a BLOB argument", name);
156 }
157
Tao Bao511d7592018-06-19 15:56:49 -0700158 args[0]->type = Value::Type::STRING;
Tao Bao0b58e9a2018-07-06 15:01:05 -0700159 return args[0].release();
160}
161
Tao Bao0c7839a2016-10-10 15:48:37 -0700162class UpdaterTest : public ::testing::Test {
Tianjie Xu56ebe622017-03-16 00:48:21 -0700163 protected:
Tao Baobc4a6d52018-05-23 00:12:21 -0700164 void SetUp() override {
Tianjie Xu56ebe622017-03-16 00:48:21 -0700165 RegisterBuiltins();
166 RegisterInstallFunctions();
167 RegisterBlockImageFunctions();
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800168
Tao Bao0b58e9a2018-07-06 15:01:05 -0700169 RegisterFunction("blob_to_string", BlobToString);
170
Tao Bao7064aa22018-05-24 21:43:50 -0700171 // Each test is run in a separate process (isolated mode). Shared temporary files won't cause
172 // conflicts.
Tao Bao641fa972018-04-25 18:59:40 -0700173 Paths::Get().set_cache_temp_source(temp_saved_source_.path);
Tao Bao7064aa22018-05-24 21:43:50 -0700174 Paths::Get().set_last_command_file(temp_last_command_.path);
Tao Bao641fa972018-04-25 18:59:40 -0700175 Paths::Get().set_stash_directory_base(temp_stash_base_.path);
Tao Baobc4a6d52018-05-23 00:12:21 -0700176
Tao Bao91a649a2018-05-21 16:05:56 -0700177 // Enable a special command "abort" to simulate interruption.
178 Command::abort_allowed_ = true;
179
Tao Bao7064aa22018-05-24 21:43:50 -0700180 last_command_file_ = temp_last_command_.path;
Tao Baobc4a6d52018-05-23 00:12:21 -0700181 image_file_ = image_temp_file_.path;
182 }
183
184 void TearDown() override {
Tao Bao7064aa22018-05-24 21:43:50 -0700185 // Clean up the last_command_file if any.
186 ASSERT_TRUE(android::base::RemoveFileIfExists(last_command_file_));
187
Tao Baobc4a6d52018-05-23 00:12:21 -0700188 // Clear partition updated marker if any.
189 std::string updated_marker{ temp_stash_base_.path };
Tao Bao3cb3c522018-11-02 13:36:58 -0700190 updated_marker += "/" + GetSha1(image_temp_file_.path) + ".UPDATED";
Tao Baobc4a6d52018-05-23 00:12:21 -0700191 ASSERT_TRUE(android::base::RemoveFileIfExists(updated_marker));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700192 }
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800193
194 TemporaryFile temp_saved_source_;
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800195 TemporaryDir temp_stash_base_;
Tao Bao7064aa22018-05-24 21:43:50 -0700196 std::string last_command_file_;
Tao Baobc4a6d52018-05-23 00:12:21 -0700197 std::string image_file_;
198
199 private:
Tao Bao7064aa22018-05-24 21:43:50 -0700200 TemporaryFile temp_last_command_;
Tao Baobc4a6d52018-05-23 00:12:21 -0700201 TemporaryFile image_temp_file_;
Tao Bao0c7839a2016-10-10 15:48:37 -0700202};
203
204TEST_F(UpdaterTest, getprop) {
205 expect(android::base::GetProperty("ro.product.device", "").c_str(),
206 "getprop(\"ro.product.device\")",
Tao Bao361342c2016-02-08 11:15:50 -0800207 kNoCause);
Tao Bao0c7839a2016-10-10 15:48:37 -0700208
209 expect(android::base::GetProperty("ro.build.fingerprint", "").c_str(),
210 "getprop(\"ro.build.fingerprint\")",
Tao Bao361342c2016-02-08 11:15:50 -0800211 kNoCause);
Tao Bao0c7839a2016-10-10 15:48:37 -0700212
213 // getprop() accepts only one parameter.
Tao Bao361342c2016-02-08 11:15:50 -0800214 expect(nullptr, "getprop()", kArgsParsingFailure);
215 expect(nullptr, "getprop(\"arg1\", \"arg2\")", kArgsParsingFailure);
216}
217
Tao Bao5609bc82018-06-20 00:30:48 -0700218TEST_F(UpdaterTest, patch_partition_check) {
219 // Zero argument is not valid.
220 expect(nullptr, "patch_partition_check()", kArgsParsingFailure);
Tao Bao361342c2016-02-08 11:15:50 -0800221
Tao Bao5609bc82018-06-20 00:30:48 -0700222 std::string source_file = from_testdata_base("boot.img");
223 std::string source_content;
224 ASSERT_TRUE(android::base::ReadFileToString(source_file, &source_content));
225 size_t source_size = source_content.size();
Tao Bao3cb3c522018-11-02 13:36:58 -0700226 std::string source_hash = GetSha1(source_content);
Tao Bao5609bc82018-06-20 00:30:48 -0700227 Partition source(source_file, source_size, source_hash);
Tao Bao361342c2016-02-08 11:15:50 -0800228
Tao Bao5609bc82018-06-20 00:30:48 -0700229 std::string target_file = from_testdata_base("recovery.img");
230 std::string target_content;
231 ASSERT_TRUE(android::base::ReadFileToString(target_file, &target_content));
232 size_t target_size = target_content.size();
Tao Bao3cb3c522018-11-02 13:36:58 -0700233 std::string target_hash = GetSha1(target_content);
Tao Bao5609bc82018-06-20 00:30:48 -0700234 Partition target(target_file, target_size, target_hash);
Tao Bao361342c2016-02-08 11:15:50 -0800235
Tao Bao5609bc82018-06-20 00:30:48 -0700236 // One argument is not valid.
237 expect(nullptr, "patch_partition_check(\"" + source.ToString() + "\")", kArgsParsingFailure);
238 expect(nullptr, "patch_partition_check(\"" + target.ToString() + "\")", kArgsParsingFailure);
Tao Bao361342c2016-02-08 11:15:50 -0800239
Tao Bao5609bc82018-06-20 00:30:48 -0700240 // Both of the source and target have the desired checksum.
241 std::string cmd =
242 "patch_partition_check(\"" + source.ToString() + "\", \"" + target.ToString() + "\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700243 expect("t", cmd, kNoCause);
Tao Bao361342c2016-02-08 11:15:50 -0800244
Tao Bao5609bc82018-06-20 00:30:48 -0700245 // Only source partition has the desired checksum.
246 Partition bad_target(target_file, target_size - 1, target_hash);
247 cmd = "patch_partition_check(\"" + source.ToString() + "\", \"" + bad_target.ToString() + "\")";
248 expect("t", cmd, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700249
Tao Bao5609bc82018-06-20 00:30:48 -0700250 // Only target partition has the desired checksum.
251 Partition bad_source(source_file, source_size + 1, source_hash);
252 cmd = "patch_partition_check(\"" + bad_source.ToString() + "\", \"" + target.ToString() + "\")";
253 expect("t", cmd, kNoCause);
Tao Baodb56eb02017-03-23 06:34:20 -0700254
Tao Bao5609bc82018-06-20 00:30:48 -0700255 // Neither of the source or target has the desired checksum.
256 cmd =
257 "patch_partition_check(\"" + bad_source.ToString() + "\", \"" + bad_target.ToString() + "\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700258 expect("", cmd, kNoCause);
Tao Baodb56eb02017-03-23 06:34:20 -0700259}
260
Tao Bao51d516e2016-11-03 14:49:01 -0700261TEST_F(UpdaterTest, file_getprop) {
262 // file_getprop() expects two arguments.
263 expect(nullptr, "file_getprop()", kArgsParsingFailure);
264 expect(nullptr, "file_getprop(\"arg1\")", kArgsParsingFailure);
265 expect(nullptr, "file_getprop(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
266
267 // File doesn't exist.
Tianjie Xu22f11202018-08-27 10:50:31 -0700268 expect(nullptr, "file_getprop(\"/doesntexist\", \"key1\")", kFreadFailure);
Tao Bao51d516e2016-11-03 14:49:01 -0700269
270 // Reject too large files (current limit = 65536).
271 TemporaryFile temp_file1;
272 std::string buffer(65540, '\0');
273 ASSERT_TRUE(android::base::WriteStringToFile(buffer, temp_file1.path));
274
275 // Read some keys.
276 TemporaryFile temp_file2;
277 std::string content("ro.product.name=tardis\n"
278 "# comment\n\n\n"
279 "ro.product.model\n"
280 "ro.product.board = magic \n");
281 ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file2.path));
282
283 std::string script1("file_getprop(\"" + std::string(temp_file2.path) +
284 "\", \"ro.product.name\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700285 expect("tardis", script1, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700286
287 std::string script2("file_getprop(\"" + std::string(temp_file2.path) +
288 "\", \"ro.product.board\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700289 expect("magic", script2, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700290
291 // No match.
292 std::string script3("file_getprop(\"" + std::string(temp_file2.path) +
293 "\", \"ro.product.wrong\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700294 expect("", script3, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700295
296 std::string script4("file_getprop(\"" + std::string(temp_file2.path) +
297 "\", \"ro.product.name=\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700298 expect("", script4, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700299
300 std::string script5("file_getprop(\"" + std::string(temp_file2.path) +
301 "\", \"ro.product.nam\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700302 expect("", script5, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700303
304 std::string script6("file_getprop(\"" + std::string(temp_file2.path) +
305 "\", \"ro.product.model\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700306 expect("", script6, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700307}
Tao Bao0831d0b2016-11-03 23:25:04 -0700308
Tom Marshallbf4f24f2017-08-23 18:14:00 +0000309TEST_F(UpdaterTest, delete) {
310 // Delete none.
311 expect("0", "delete()", kNoCause);
312 expect("0", "delete(\"/doesntexist\")", kNoCause);
313 expect("0", "delete(\"/doesntexist1\", \"/doesntexist2\")", kNoCause);
314 expect("0", "delete(\"/doesntexist1\", \"/doesntexist2\", \"/doesntexist3\")", kNoCause);
315
316 // Delete one file.
317 TemporaryFile temp_file1;
318 ASSERT_TRUE(android::base::WriteStringToFile("abc", temp_file1.path));
319 std::string script1("delete(\"" + std::string(temp_file1.path) + "\")");
320 expect("1", script1.c_str(), kNoCause);
321
322 // Delete two files.
323 TemporaryFile temp_file2;
324 ASSERT_TRUE(android::base::WriteStringToFile("abc", temp_file2.path));
325 TemporaryFile temp_file3;
326 ASSERT_TRUE(android::base::WriteStringToFile("abc", temp_file3.path));
327 std::string script2("delete(\"" + std::string(temp_file2.path) + "\", \"" +
328 std::string(temp_file3.path) + "\")");
329 expect("2", script2.c_str(), kNoCause);
330
331 // Delete already deleted files.
332 expect("0", script2.c_str(), kNoCause);
333
334 // Delete one out of three.
335 TemporaryFile temp_file4;
336 ASSERT_TRUE(android::base::WriteStringToFile("abc", temp_file4.path));
337 std::string script3("delete(\"/doesntexist1\", \"" + std::string(temp_file4.path) +
338 "\", \"/doesntexist2\")");
339 expect("1", script3.c_str(), kNoCause);
340}
341
342TEST_F(UpdaterTest, rename) {
343 // rename() expects two arguments.
344 expect(nullptr, "rename()", kArgsParsingFailure);
345 expect(nullptr, "rename(\"arg1\")", kArgsParsingFailure);
346 expect(nullptr, "rename(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
347
348 // src_name or dst_name cannot be empty.
349 expect(nullptr, "rename(\"\", \"arg2\")", kArgsParsingFailure);
350 expect(nullptr, "rename(\"arg1\", \"\")", kArgsParsingFailure);
351
352 // File doesn't exist (both of src and dst).
353 expect(nullptr, "rename(\"/doesntexist\", \"/doesntexisteither\")" , kFileRenameFailure);
354
355 // Can't create parent directory.
356 TemporaryFile temp_file1;
357 ASSERT_TRUE(android::base::WriteStringToFile("abc", temp_file1.path));
358 std::string script1("rename(\"" + std::string(temp_file1.path) + "\", \"/proc/0/file1\")");
359 expect(nullptr, script1.c_str(), kFileRenameFailure);
360
361 // Rename.
362 TemporaryFile temp_file2;
363 std::string script2("rename(\"" + std::string(temp_file1.path) + "\", \"" +
364 std::string(temp_file2.path) + "\")");
365 expect(temp_file2.path, script2.c_str(), kNoCause);
366
367 // Already renamed.
368 expect(temp_file2.path, script2.c_str(), kNoCause);
369
370 // Parents create successfully.
371 TemporaryFile temp_file3;
372 TemporaryDir td;
373 std::string temp_dir(td.path);
374 std::string dst_file = temp_dir + "/aaa/bbb/a.txt";
375 std::string script3("rename(\"" + std::string(temp_file3.path) + "\", \"" + dst_file + "\")");
376 expect(dst_file.c_str(), script3.c_str(), kNoCause);
377
378 // Clean up the temp files under td.
379 ASSERT_EQ(0, unlink(dst_file.c_str()));
380 ASSERT_EQ(0, rmdir((temp_dir + "/aaa/bbb").c_str()));
381 ASSERT_EQ(0, rmdir((temp_dir + "/aaa").c_str()));
382}
383
384TEST_F(UpdaterTest, symlink) {
385 // symlink expects 1+ argument.
386 expect(nullptr, "symlink()", kArgsParsingFailure);
387
388 // symlink should fail if src is an empty string.
389 TemporaryFile temp_file1;
390 std::string script1("symlink(\"" + std::string(temp_file1.path) + "\", \"\")");
391 expect(nullptr, script1.c_str(), kSymlinkFailure);
392
393 std::string script2("symlink(\"" + std::string(temp_file1.path) + "\", \"src1\", \"\")");
394 expect(nullptr, script2.c_str(), kSymlinkFailure);
395
396 // symlink failed to remove old src.
397 std::string script3("symlink(\"" + std::string(temp_file1.path) + "\", \"/proc\")");
398 expect(nullptr, script3.c_str(), kSymlinkFailure);
399
400 // symlink can create symlinks.
401 TemporaryFile temp_file;
402 std::string content = "magicvalue";
403 ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file.path));
404
405 TemporaryDir td;
406 std::string src1 = std::string(td.path) + "/symlink1";
407 std::string src2 = std::string(td.path) + "/symlink2";
408 std::string script4("symlink(\"" + std::string(temp_file.path) + "\", \"" +
409 src1 + "\", \"" + src2 + "\")");
410 expect("t", script4.c_str(), kNoCause);
411
412 // Verify the created symlinks.
413 struct stat sb;
414 ASSERT_TRUE(lstat(src1.c_str(), &sb) == 0 && S_ISLNK(sb.st_mode));
415 ASSERT_TRUE(lstat(src2.c_str(), &sb) == 0 && S_ISLNK(sb.st_mode));
416
417 // Clean up the leftovers.
418 ASSERT_EQ(0, unlink(src1.c_str()));
419 ASSERT_EQ(0, unlink(src2.c_str()));
420}
421
Tom Marshall2c1a5792017-12-14 22:37:17 +0100422TEST_F(UpdaterTest, package_extract_dir) {
423 // package_extract_dir expects 2 arguments.
424 expect(nullptr, "package_extract_dir()", kArgsParsingFailure);
425 expect(nullptr, "package_extract_dir(\"arg1\")", kArgsParsingFailure);
426 expect(nullptr, "package_extract_dir(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
427
428 std::string zip_path = from_testdata_base("ziptest_valid.zip");
429 ZipArchiveHandle handle;
430 ASSERT_EQ(0, OpenArchive(zip_path.c_str(), &handle));
431
432 // Need to set up the ziphandle.
433 UpdaterInfo updater_info;
434 updater_info.package_zip = handle;
435
436 // Extract "b/c.txt" and "b/d.txt" with package_extract_dir("b", "<dir>").
437 TemporaryDir td;
438 std::string temp_dir(td.path);
439 std::string script("package_extract_dir(\"b\", \"" + temp_dir + "\")");
440 expect("t", script.c_str(), kNoCause, &updater_info);
441
442 // Verify.
443 std::string data;
444 std::string file_c = temp_dir + "/c.txt";
445 ASSERT_TRUE(android::base::ReadFileToString(file_c, &data));
446 ASSERT_EQ(kCTxtContents, data);
447
448 std::string file_d = temp_dir + "/d.txt";
449 ASSERT_TRUE(android::base::ReadFileToString(file_d, &data));
450 ASSERT_EQ(kDTxtContents, data);
451
452 // Modify the contents in order to retry. It's expected to be overwritten.
453 ASSERT_TRUE(android::base::WriteStringToFile("random", file_c));
454 ASSERT_TRUE(android::base::WriteStringToFile("random", file_d));
455
456 // Extract again and verify.
457 expect("t", script.c_str(), kNoCause, &updater_info);
458
459 ASSERT_TRUE(android::base::ReadFileToString(file_c, &data));
460 ASSERT_EQ(kCTxtContents, data);
461 ASSERT_TRUE(android::base::ReadFileToString(file_d, &data));
462 ASSERT_EQ(kDTxtContents, data);
463
464 // Clean up the temp files under td.
465 ASSERT_EQ(0, unlink(file_c.c_str()));
466 ASSERT_EQ(0, unlink(file_d.c_str()));
467
468 // Extracting "b/" (with slash) should give the same result.
469 script = "package_extract_dir(\"b/\", \"" + temp_dir + "\")";
470 expect("t", script.c_str(), kNoCause, &updater_info);
471
472 ASSERT_TRUE(android::base::ReadFileToString(file_c, &data));
473 ASSERT_EQ(kCTxtContents, data);
474 ASSERT_TRUE(android::base::ReadFileToString(file_d, &data));
475 ASSERT_EQ(kDTxtContents, data);
476
477 ASSERT_EQ(0, unlink(file_c.c_str()));
478 ASSERT_EQ(0, unlink(file_d.c_str()));
479
480 // Extracting "" is allowed. The entries will carry the path name.
481 script = "package_extract_dir(\"\", \"" + temp_dir + "\")";
482 expect("t", script.c_str(), kNoCause, &updater_info);
483
484 std::string file_a = temp_dir + "/a.txt";
485 ASSERT_TRUE(android::base::ReadFileToString(file_a, &data));
486 ASSERT_EQ(kATxtContents, data);
487 std::string file_b = temp_dir + "/b.txt";
488 ASSERT_TRUE(android::base::ReadFileToString(file_b, &data));
489 ASSERT_EQ(kBTxtContents, data);
490 std::string file_b_c = temp_dir + "/b/c.txt";
491 ASSERT_TRUE(android::base::ReadFileToString(file_b_c, &data));
492 ASSERT_EQ(kCTxtContents, data);
493 std::string file_b_d = temp_dir + "/b/d.txt";
494 ASSERT_TRUE(android::base::ReadFileToString(file_b_d, &data));
495 ASSERT_EQ(kDTxtContents, data);
496
497 ASSERT_EQ(0, unlink(file_a.c_str()));
498 ASSERT_EQ(0, unlink(file_b.c_str()));
499 ASSERT_EQ(0, unlink(file_b_c.c_str()));
500 ASSERT_EQ(0, unlink(file_b_d.c_str()));
501 ASSERT_EQ(0, rmdir((temp_dir + "/b").c_str()));
502
503 // Extracting non-existent entry should still give "t".
504 script = "package_extract_dir(\"doesntexist\", \"" + temp_dir + "\")";
505 expect("t", script.c_str(), kNoCause, &updater_info);
506
507 // Only relative zip_path is allowed.
508 script = "package_extract_dir(\"/b\", \"" + temp_dir + "\")";
509 expect("", script.c_str(), kNoCause, &updater_info);
510
511 // Only absolute dest_path is allowed.
512 script = "package_extract_dir(\"b\", \"path\")";
513 expect("", script.c_str(), kNoCause, &updater_info);
514
515 CloseArchive(handle);
516}
517
Tao Baoef0eb3b2016-11-14 21:29:52 -0800518// TODO: Test extracting to block device.
519TEST_F(UpdaterTest, package_extract_file) {
520 // package_extract_file expects 1 or 2 arguments.
521 expect(nullptr, "package_extract_file()", kArgsParsingFailure);
522 expect(nullptr, "package_extract_file(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
523
524 std::string zip_path = from_testdata_base("ziptest_valid.zip");
525 ZipArchiveHandle handle;
526 ASSERT_EQ(0, OpenArchive(zip_path.c_str(), &handle));
527
528 // Need to set up the ziphandle.
529 UpdaterInfo updater_info;
530 updater_info.package_zip = handle;
531
532 // Two-argument version.
533 TemporaryFile temp_file1;
534 std::string script("package_extract_file(\"a.txt\", \"" + std::string(temp_file1.path) + "\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700535 expect("t", script, kNoCause, &updater_info);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800536
537 // Verify the extracted entry.
538 std::string data;
539 ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data));
540 ASSERT_EQ(kATxtContents, data);
541
542 // Now extract another entry to the same location, which should overwrite.
543 script = "package_extract_file(\"b.txt\", \"" + std::string(temp_file1.path) + "\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700544 expect("t", script, kNoCause, &updater_info);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800545
546 ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data));
547 ASSERT_EQ(kBTxtContents, data);
548
549 // Missing zip entry. The two-argument version doesn't abort.
550 script = "package_extract_file(\"doesntexist\", \"" + std::string(temp_file1.path) + "\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700551 expect("", script, kNoCause, &updater_info);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800552
553 // Extract to /dev/full should fail.
554 script = "package_extract_file(\"a.txt\", \"/dev/full\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700555 expect("", script, kNoCause, &updater_info);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800556
Tao Bao0b58e9a2018-07-06 15:01:05 -0700557 // One-argument version. package_extract_file() gives a VAL_BLOB, which needs to be converted to
558 // VAL_STRING for equality test.
559 script = "blob_to_string(package_extract_file(\"a.txt\")) == \"" + kATxtContents + "\"";
Tao Baod8d514f2018-07-09 13:32:28 -0700560 expect("t", script, kNoCause, &updater_info);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800561
Tao Bao0b58e9a2018-07-06 15:01:05 -0700562 script = "blob_to_string(package_extract_file(\"b.txt\")) == \"" + kBTxtContents + "\"";
Tao Baod8d514f2018-07-09 13:32:28 -0700563 expect("t", script, kNoCause, &updater_info);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800564
565 // Missing entry. The one-argument version aborts the evaluation.
566 script = "package_extract_file(\"doesntexist\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700567 expect(nullptr, script, kPackageExtractFileFailure, &updater_info);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800568
569 CloseArchive(handle);
570}
Tao Baod0f30882016-11-03 23:52:01 -0700571
Tao Baobafd6c72018-07-09 15:08:50 -0700572TEST_F(UpdaterTest, read_file) {
573 // read_file() expects one argument.
574 expect(nullptr, "read_file()", kArgsParsingFailure);
575 expect(nullptr, "read_file(\"arg1\", \"arg2\")", kArgsParsingFailure);
576
577 // Write some value to file and read back.
578 TemporaryFile temp_file;
579 std::string script("write_value(\"foo\", \""s + temp_file.path + "\");");
580 expect("t", script, kNoCause);
581
582 script = "read_file(\""s + temp_file.path + "\") == \"foo\"";
583 expect("t", script, kNoCause);
584
585 script = "read_file(\""s + temp_file.path + "\") == \"bar\"";
586 expect("", script, kNoCause);
587
588 // It should fail gracefully when read fails.
589 script = "read_file(\"/doesntexist\")";
590 expect("", script, kNoCause);
591}
592
Tianjie Xu69ffa152018-08-01 16:40:00 -0700593TEST_F(UpdaterTest, compute_hash_tree_smoke) {
594 std::string data;
595 for (unsigned char i = 0; i < 128; i++) {
596 data += std::string(4096, i);
597 }
598 // Appends an additional block for verity data.
599 data += std::string(4096, 0);
600 ASSERT_EQ(129 * 4096, data.size());
601 ASSERT_TRUE(android::base::WriteStringToFile(data, image_file_));
602
603 std::string salt = "aee087a5be3b982978c923f566a94613496b417f2af592639bc80d141e34dfe7";
604 std::string expected_root_hash =
605 "7e0a8d8747f54384014ab996f5b2dc4eb7ff00c630eede7134c9e3f05c0dd8ca";
606 // hash_tree_ranges, source_ranges, hash_algorithm, salt_hex, root_hash
607 std::vector<std::string> tokens{ "compute_hash_tree", "2,128,129", "2,0,128", "sha256", salt,
608 expected_root_hash };
609 std::string hash_tree_command = android::base::Join(tokens, " ");
610
611 std::vector<std::string> transfer_list{
612 "4", "2", "0", "2", hash_tree_command,
613 };
614
615 PackageEntries entries{
616 { "new_data", "" },
617 { "patch_data", "" },
618 { "transfer_list", android::base::Join(transfer_list, "\n") },
619 };
620
621 RunBlockImageUpdate(false, entries, image_file_, "t");
622
623 std::string updated;
624 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated));
625 ASSERT_EQ(129 * 4096, updated.size());
626 ASSERT_EQ(data.substr(0, 128 * 4096), updated.substr(0, 128 * 4096));
627
628 // Computes the SHA256 of the salt + hash_tree_data and expects the result to match with the
629 // root_hash.
630 std::vector<unsigned char> salt_bytes;
631 ASSERT_TRUE(HashTreeBuilder::ParseBytesArrayFromString(salt, &salt_bytes));
632 std::vector<unsigned char> hash_tree = std::move(salt_bytes);
633 hash_tree.insert(hash_tree.end(), updated.begin() + 128 * 4096, updated.end());
634
635 std::vector<unsigned char> digest(SHA256_DIGEST_LENGTH);
636 SHA256(hash_tree.data(), hash_tree.size(), digest.data());
637 ASSERT_EQ(expected_root_hash, HashTreeBuilder::BytesArrayToString(digest));
638}
639
640TEST_F(UpdaterTest, compute_hash_tree_root_mismatch) {
641 std::string data;
642 for (size_t i = 0; i < 128; i++) {
643 data += std::string(4096, i);
644 }
645 // Appends an additional block for verity data.
646 data += std::string(4096, 0);
647 ASSERT_EQ(129 * 4096, data.size());
648 // Corrupts one bit
649 data[4096] = 'A';
650 ASSERT_TRUE(android::base::WriteStringToFile(data, image_file_));
651
652 std::string salt = "aee087a5be3b982978c923f566a94613496b417f2af592639bc80d141e34dfe7";
653 std::string expected_root_hash =
654 "7e0a8d8747f54384014ab996f5b2dc4eb7ff00c630eede7134c9e3f05c0dd8ca";
655 // hash_tree_ranges, source_ranges, hash_algorithm, salt_hex, root_hash
656 std::vector<std::string> tokens{ "compute_hash_tree", "2,128,129", "2,0,128", "sha256", salt,
657 expected_root_hash };
658 std::string hash_tree_command = android::base::Join(tokens, " ");
659
660 std::vector<std::string> transfer_list{
661 "4", "2", "0", "2", hash_tree_command,
662 };
663
664 PackageEntries entries{
665 { "new_data", "" },
666 { "patch_data", "" },
667 { "transfer_list", android::base::Join(transfer_list, "\n") },
668 };
669
670 RunBlockImageUpdate(false, entries, image_file_, "", kHashTreeComputationFailure);
671}
672
Tao Baod0f30882016-11-03 23:52:01 -0700673TEST_F(UpdaterTest, write_value) {
674 // write_value() expects two arguments.
675 expect(nullptr, "write_value()", kArgsParsingFailure);
676 expect(nullptr, "write_value(\"arg1\")", kArgsParsingFailure);
677 expect(nullptr, "write_value(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
678
679 // filename cannot be empty.
680 expect(nullptr, "write_value(\"value\", \"\")", kArgsParsingFailure);
681
682 // Write some value to file.
683 TemporaryFile temp_file;
684 std::string value = "magicvalue";
685 std::string script("write_value(\"" + value + "\", \"" + std::string(temp_file.path) + "\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700686 expect("t", script, kNoCause);
Tao Baod0f30882016-11-03 23:52:01 -0700687
688 // Verify the content.
689 std::string content;
690 ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content));
691 ASSERT_EQ(value, content);
692
693 // Allow writing empty string.
694 script = "write_value(\"\", \"" + std::string(temp_file.path) + "\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700695 expect("t", script, kNoCause);
Tao Baod0f30882016-11-03 23:52:01 -0700696
697 // Verify the content.
698 ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content));
699 ASSERT_EQ("", content);
700
701 // It should fail gracefully when write fails.
702 script = "write_value(\"value\", \"/proc/0/file1\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700703 expect("", script, kNoCause);
Tao Baod0f30882016-11-03 23:52:01 -0700704}
Tao Baobedf5fc2016-11-18 12:01:26 -0800705
706TEST_F(UpdaterTest, get_stage) {
707 // get_stage() expects one argument.
708 expect(nullptr, "get_stage()", kArgsParsingFailure);
709 expect(nullptr, "get_stage(\"arg1\", \"arg2\")", kArgsParsingFailure);
710 expect(nullptr, "get_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
711
712 // Set up a local file as BCB.
713 TemporaryFile tf;
714 std::string temp_file(tf.path);
715 bootloader_message boot;
716 strlcpy(boot.stage, "2/3", sizeof(boot.stage));
717 std::string err;
718 ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err));
719
720 // Can read the stage value.
721 std::string script("get_stage(\"" + temp_file + "\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700722 expect("2/3", script, kNoCause);
Tao Baobedf5fc2016-11-18 12:01:26 -0800723
724 // Bad BCB path.
725 script = "get_stage(\"doesntexist\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700726 expect("", script, kNoCause);
Tao Baobedf5fc2016-11-18 12:01:26 -0800727}
728
729TEST_F(UpdaterTest, set_stage) {
730 // set_stage() expects two arguments.
731 expect(nullptr, "set_stage()", kArgsParsingFailure);
732 expect(nullptr, "set_stage(\"arg1\")", kArgsParsingFailure);
733 expect(nullptr, "set_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
734
735 // Set up a local file as BCB.
736 TemporaryFile tf;
737 std::string temp_file(tf.path);
738 bootloader_message boot;
739 strlcpy(boot.command, "command", sizeof(boot.command));
740 strlcpy(boot.stage, "2/3", sizeof(boot.stage));
741 std::string err;
742 ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err));
743
744 // Write with set_stage().
745 std::string script("set_stage(\"" + temp_file + "\", \"1/3\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700746 expect(tf.path, script, kNoCause);
Tao Baobedf5fc2016-11-18 12:01:26 -0800747
748 // Verify.
749 bootloader_message boot_verify;
750 ASSERT_TRUE(read_bootloader_message_from(&boot_verify, temp_file, &err));
751
752 // Stage should be updated, with command part untouched.
753 ASSERT_STREQ("1/3", boot_verify.stage);
754 ASSERT_STREQ(boot.command, boot_verify.command);
755
756 // Bad BCB path.
757 script = "set_stage(\"doesntexist\", \"1/3\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700758 expect("", script, kNoCause);
Tao Baobedf5fc2016-11-18 12:01:26 -0800759
760 script = "set_stage(\"/dev/full\", \"1/3\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700761 expect("", script, kNoCause);
Tao Baobedf5fc2016-11-18 12:01:26 -0800762}
Tao Bao9aa7ab52017-01-05 17:27:19 -0800763
764TEST_F(UpdaterTest, set_progress) {
765 // set_progress() expects one argument.
766 expect(nullptr, "set_progress()", kArgsParsingFailure);
767 expect(nullptr, "set_progress(\"arg1\", \"arg2\")", kArgsParsingFailure);
768
769 // Invalid progress argument.
770 expect(nullptr, "set_progress(\"arg1\")", kArgsParsingFailure);
771 expect(nullptr, "set_progress(\"3x+5\")", kArgsParsingFailure);
772 expect(nullptr, "set_progress(\".3.5\")", kArgsParsingFailure);
773
774 TemporaryFile tf;
775 UpdaterInfo updater_info;
Tianjie Xu79327ac2017-09-08 17:09:10 -0700776 updater_info.cmd_pipe = fdopen(tf.release(), "w");
Tao Bao9aa7ab52017-01-05 17:27:19 -0800777 expect(".52", "set_progress(\".52\")", kNoCause, &updater_info);
778 fflush(updater_info.cmd_pipe);
779
780 std::string cmd;
781 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd));
782 ASSERT_EQ(android::base::StringPrintf("set_progress %f\n", .52), cmd);
783 // recovery-updater protocol expects 2 tokens ("set_progress <frac>").
784 ASSERT_EQ(2U, android::base::Split(cmd, " ").size());
Tianjie Xu79327ac2017-09-08 17:09:10 -0700785 ASSERT_EQ(0, fclose(updater_info.cmd_pipe));
Tao Bao9aa7ab52017-01-05 17:27:19 -0800786}
787
788TEST_F(UpdaterTest, show_progress) {
789 // show_progress() expects two arguments.
790 expect(nullptr, "show_progress()", kArgsParsingFailure);
791 expect(nullptr, "show_progress(\"arg1\")", kArgsParsingFailure);
792 expect(nullptr, "show_progress(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
793
794 // Invalid progress arguments.
795 expect(nullptr, "show_progress(\"arg1\", \"arg2\")", kArgsParsingFailure);
796 expect(nullptr, "show_progress(\"3x+5\", \"10\")", kArgsParsingFailure);
797 expect(nullptr, "show_progress(\".3\", \"5a\")", kArgsParsingFailure);
798
799 TemporaryFile tf;
800 UpdaterInfo updater_info;
Tianjie Xu79327ac2017-09-08 17:09:10 -0700801 updater_info.cmd_pipe = fdopen(tf.release(), "w");
Tao Bao9aa7ab52017-01-05 17:27:19 -0800802 expect(".52", "show_progress(\".52\", \"10\")", kNoCause, &updater_info);
803 fflush(updater_info.cmd_pipe);
804
805 std::string cmd;
806 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd));
807 ASSERT_EQ(android::base::StringPrintf("progress %f %d\n", .52, 10), cmd);
808 // recovery-updater protocol expects 3 tokens ("progress <frac> <secs>").
809 ASSERT_EQ(3U, android::base::Split(cmd, " ").size());
Tianjie Xu79327ac2017-09-08 17:09:10 -0700810 ASSERT_EQ(0, fclose(updater_info.cmd_pipe));
Tao Bao9aa7ab52017-01-05 17:27:19 -0800811}
Tianjie Xu56ebe622017-03-16 00:48:21 -0700812
Tao Baoffede3e2018-06-07 09:56:19 -0700813TEST_F(UpdaterTest, block_image_update_parsing_error) {
814 std::vector<std::string> transfer_list{
815 // clang-format off
816 "4",
817 "2",
818 "0",
819 // clang-format on
820 };
Tianjie Xu5450c842017-10-18 13:15:21 -0700821
Tao Baoffede3e2018-06-07 09:56:19 -0700822 PackageEntries entries{
823 { "new_data", "" },
824 { "patch_data", "" },
825 { "transfer_list", android::base::Join(transfer_list, '\n') },
826 };
827
828 RunBlockImageUpdate(false, entries, image_file_, "", kArgsParsingFailure);
829}
830
Tao Bao3cb3c522018-11-02 13:36:58 -0700831// Generates the bsdiff of the given source and target images, and writes the result entries.
832// target_blocks specifies the block count to be written into the `bsdiff` command, which may be
833// different from the given target size in order to trigger overrun / underrun paths.
834static void GetEntriesForBsdiff(std::string_view source, std::string_view target,
835 size_t target_blocks, PackageEntries* entries) {
Tianjie Xu5450c842017-10-18 13:15:21 -0700836 // Generate the patch data.
Tianjie Xu56ebe622017-03-16 00:48:21 -0700837 TemporaryFile patch_file;
Tao Bao3cb3c522018-11-02 13:36:58 -0700838 ASSERT_EQ(0, bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(source.data()), source.size(),
839 reinterpret_cast<const uint8_t*>(target.data()), target.size(),
840 patch_file.path, nullptr));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700841 std::string patch_content;
842 ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch_content));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700843
Tianjie Xu5450c842017-10-18 13:15:21 -0700844 // Create the transfer list that contains a bsdiff.
Tao Bao3cb3c522018-11-02 13:36:58 -0700845 std::string src_hash = GetSha1(source);
846 std::string tgt_hash = GetSha1(target);
847 size_t source_blocks = source.size() / 4096;
Tao Baobc4a6d52018-05-23 00:12:21 -0700848 std::vector<std::string> transfer_list{
849 // clang-format off
Tianjie Xu56ebe622017-03-16 00:48:21 -0700850 "4",
Tao Bao3cb3c522018-11-02 13:36:58 -0700851 std::to_string(target_blocks),
Tianjie Xu56ebe622017-03-16 00:48:21 -0700852 "0",
Tao Bao3cb3c522018-11-02 13:36:58 -0700853 "0",
854 // bsdiff patch_offset patch_length source_hash target_hash target_range source_block_count
855 // source_range
856 android::base::StringPrintf("bsdiff 0 %zu %s %s 2,0,%zu %zu 2,0,%zu", patch_content.size(),
857 src_hash.c_str(), tgt_hash.c_str(), target_blocks, source_blocks,
858 source_blocks),
Tao Baobc4a6d52018-05-23 00:12:21 -0700859 // clang-format on
Tianjie Xu56ebe622017-03-16 00:48:21 -0700860 };
Tianjie Xu56ebe622017-03-16 00:48:21 -0700861
Tao Bao3cb3c522018-11-02 13:36:58 -0700862 *entries = {
Tianjie Xu5450c842017-10-18 13:15:21 -0700863 { "new_data", "" },
864 { "patch_data", patch_content },
865 { "transfer_list", android::base::Join(transfer_list, '\n') },
Tianjie Xu56ebe622017-03-16 00:48:21 -0700866 };
Tao Bao3cb3c522018-11-02 13:36:58 -0700867}
Tianjie Xu5450c842017-10-18 13:15:21 -0700868
Tao Bao3cb3c522018-11-02 13:36:58 -0700869TEST_F(UpdaterTest, block_image_update_patch_data) {
870 // Both source and target images have 10 blocks.
871 std::string source =
872 std::string(4096, 'a') + std::string(4096, 'c') + std::string(4096 * 3, '\0');
873 std::string target =
874 std::string(4096, 'b') + std::string(4096, 'd') + std::string(4096 * 3, '\0');
875 ASSERT_TRUE(android::base::WriteStringToFile(source, image_file_));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700876
Tao Bao3cb3c522018-11-02 13:36:58 -0700877 PackageEntries entries;
878 GetEntriesForBsdiff(std::string_view(source).substr(0, 4096 * 2),
879 std::string_view(target).substr(0, 4096 * 2), 2, &entries);
Tao Baobc4a6d52018-05-23 00:12:21 -0700880 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu56ebe622017-03-16 00:48:21 -0700881
Tianjie Xu56ebe622017-03-16 00:48:21 -0700882 // The update_file should be patched correctly.
Tao Bao3cb3c522018-11-02 13:36:58 -0700883 std::string updated;
884 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated));
885 ASSERT_EQ(target, updated);
886}
887
888TEST_F(UpdaterTest, block_image_update_patch_overrun) {
889 // Both source and target images have 10 blocks.
890 std::string source =
891 std::string(4096, 'a') + std::string(4096, 'c') + std::string(4096 * 3, '\0');
892 std::string target =
893 std::string(4096, 'b') + std::string(4096, 'd') + std::string(4096 * 3, '\0');
894 ASSERT_TRUE(android::base::WriteStringToFile(source, image_file_));
895
896 // Provide one less block to trigger the overrun path.
897 PackageEntries entries;
898 GetEntriesForBsdiff(std::string_view(source).substr(0, 4096 * 2),
899 std::string_view(target).substr(0, 4096 * 2), 1, &entries);
900
901 // The update should fail due to overrun.
902 RunBlockImageUpdate(false, entries, image_file_, "", kPatchApplicationFailure);
Tianjie Xu5450c842017-10-18 13:15:21 -0700903}
904
Tao Baoa2cff952018-11-02 15:44:07 -0700905TEST_F(UpdaterTest, block_image_update_patch_underrun) {
Tao Bao3cb3c522018-11-02 13:36:58 -0700906 // Both source and target images have 10 blocks.
907 std::string source =
908 std::string(4096, 'a') + std::string(4096, 'c') + std::string(4096 * 3, '\0');
909 std::string target =
910 std::string(4096, 'b') + std::string(4096, 'd') + std::string(4096 * 3, '\0');
911 ASSERT_TRUE(android::base::WriteStringToFile(source, image_file_));
Tao Baoa2cff952018-11-02 15:44:07 -0700912
Tao Bao3cb3c522018-11-02 13:36:58 -0700913 // Provide one more block to trigger the overrun path.
914 PackageEntries entries;
915 GetEntriesForBsdiff(std::string_view(source).substr(0, 4096 * 2),
916 std::string_view(target).substr(0, 4096 * 2), 3, &entries);
Tao Baoa2cff952018-11-02 15:44:07 -0700917
918 // The update should fail due to underrun.
919 RunBlockImageUpdate(false, entries, image_file_, "", kPatchApplicationFailure);
920}
921
Tianjie Xu5450c842017-10-18 13:15:21 -0700922TEST_F(UpdaterTest, block_image_update_fail) {
923 std::string src_content(4096 * 2, 'e');
Tao Bao3cb3c522018-11-02 13:36:58 -0700924 std::string src_hash = GetSha1(src_content);
Tianjie Xu5450c842017-10-18 13:15:21 -0700925 // Stash and free some blocks, then fail the update intentionally.
Tao Baobc4a6d52018-05-23 00:12:21 -0700926 std::vector<std::string> transfer_list{
927 // clang-format off
Tianjie Xu56ebe622017-03-16 00:48:21 -0700928 "4",
929 "2",
930 "0",
931 "2",
932 "stash " + src_hash + " 2,0,2",
Tianjie Xu56ebe622017-03-16 00:48:21 -0700933 "free " + src_hash,
Tao Bao91a649a2018-05-21 16:05:56 -0700934 "abort",
Tao Baobc4a6d52018-05-23 00:12:21 -0700935 // clang-format on
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700936 };
Tianjie Xu5450c842017-10-18 13:15:21 -0700937
938 // Add a new data of 10 bytes to test the deadlock.
Tao Baobc4a6d52018-05-23 00:12:21 -0700939 PackageEntries entries{
Tianjie Xu5450c842017-10-18 13:15:21 -0700940 { "new_data", std::string(10, 0) },
941 { "patch_data", "" },
942 { "transfer_list", android::base::Join(transfer_list, '\n') },
943 };
944
Tao Baobc4a6d52018-05-23 00:12:21 -0700945 ASSERT_TRUE(android::base::WriteStringToFile(src_content, image_file_));
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700946
Tao Baobc4a6d52018-05-23 00:12:21 -0700947 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700948
Tianjie Xu56ebe622017-03-16 00:48:21 -0700949 // Updater generates the stash name based on the input file name.
Tao Bao3cb3c522018-11-02 13:36:58 -0700950 std::string name_digest = GetSha1(image_file_);
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800951 std::string stash_base = std::string(temp_stash_base_.path) + "/" + name_digest;
Tianjie Xu56ebe622017-03-16 00:48:21 -0700952 ASSERT_EQ(0, access(stash_base.c_str(), F_OK));
Tao Baobc4a6d52018-05-23 00:12:21 -0700953 // Expect the stashed blocks to be freed.
Tianjie Xu5450c842017-10-18 13:15:21 -0700954 ASSERT_EQ(-1, access((stash_base + src_hash).c_str(), F_OK));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700955 ASSERT_EQ(0, rmdir(stash_base.c_str()));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700956}
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700957
Tianjie Xu5450c842017-10-18 13:15:21 -0700958TEST_F(UpdaterTest, new_data_over_write) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700959 std::vector<std::string> transfer_list{
960 // clang-format off
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700961 "4",
962 "1",
963 "0",
964 "0",
965 "new 2,0,1",
Tao Baobc4a6d52018-05-23 00:12:21 -0700966 // clang-format on
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700967 };
Tianjie Xu5450c842017-10-18 13:15:21 -0700968
Tao Baobc4a6d52018-05-23 00:12:21 -0700969 // Write 4096 + 100 bytes of new data.
970 PackageEntries entries{
971 { "new_data", std::string(4196, 0) },
Tianjie Xu5450c842017-10-18 13:15:21 -0700972 { "patch_data", "" },
973 { "transfer_list", android::base::Join(transfer_list, '\n') },
974 };
975
Tao Baobc4a6d52018-05-23 00:12:21 -0700976 RunBlockImageUpdate(false, entries, image_file_, "t");
977}
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700978
Tao Baobc4a6d52018-05-23 00:12:21 -0700979TEST_F(UpdaterTest, new_data_short_write) {
980 std::vector<std::string> transfer_list{
981 // clang-format off
982 "4",
983 "1",
984 "0",
985 "0",
986 "new 2,0,1",
987 // clang-format on
988 };
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700989
Tao Baobc4a6d52018-05-23 00:12:21 -0700990 PackageEntries entries{
991 { "patch_data", "" },
992 { "transfer_list", android::base::Join(transfer_list, '\n') },
993 };
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700994
995 // Updater should report the failure gracefully rather than stuck in deadlock.
Tao Baobc4a6d52018-05-23 00:12:21 -0700996 entries["new_data"] = "";
997 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700998
Tao Baobc4a6d52018-05-23 00:12:21 -0700999 entries["new_data"] = std::string(10, 'a');
1000 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu3a8d98d2017-04-03 20:01:17 -07001001
1002 // Expect to write 1 block of new data successfully.
Tao Baobc4a6d52018-05-23 00:12:21 -07001003 entries["new_data"] = std::string(4096, 'a');
1004 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu107a34f2017-06-29 17:04:21 -07001005}
1006
1007TEST_F(UpdaterTest, brotli_new_data) {
Tianjie Xu107a34f2017-06-29 17:04:21 -07001008 auto generator = []() { return rand() % 128; };
Tianjie Xud9759e82017-07-21 21:06:52 +00001009 // Generate 100 blocks of random data.
Tianjie Xu107a34f2017-06-29 17:04:21 -07001010 std::string brotli_new_data;
Tianjie Xud9759e82017-07-21 21:06:52 +00001011 brotli_new_data.reserve(4096 * 100);
1012 generate_n(back_inserter(brotli_new_data), 4096 * 100, generator);
Tianjie Xu107a34f2017-06-29 17:04:21 -07001013
1014 size_t encoded_size = BrotliEncoderMaxCompressedSize(brotli_new_data.size());
Tianjie Xu5450c842017-10-18 13:15:21 -07001015 std::string encoded_data(encoded_size, 0);
Tianjie Xu107a34f2017-06-29 17:04:21 -07001016 ASSERT_TRUE(BrotliEncoderCompress(
1017 BROTLI_DEFAULT_QUALITY, BROTLI_DEFAULT_WINDOW, BROTLI_DEFAULT_MODE, brotli_new_data.size(),
Tianjie Xu5450c842017-10-18 13:15:21 -07001018 reinterpret_cast<const uint8_t*>(brotli_new_data.data()), &encoded_size,
1019 reinterpret_cast<uint8_t*>(const_cast<char*>(encoded_data.data()))));
1020 encoded_data.resize(encoded_size);
Tianjie Xu107a34f2017-06-29 17:04:21 -07001021
Tianjie Xud9759e82017-07-21 21:06:52 +00001022 // Write a few small chunks of new data, then a large chunk, and finally a few small chunks.
1023 // This helps us to catch potential short writes.
Tianjie Xu107a34f2017-06-29 17:04:21 -07001024 std::vector<std::string> transfer_list = {
Tianjie Xud9759e82017-07-21 21:06:52 +00001025 "4",
1026 "100",
1027 "0",
1028 "0",
1029 "new 2,0,1",
1030 "new 2,1,2",
1031 "new 4,2,50,50,97",
1032 "new 2,97,98",
1033 "new 2,98,99",
1034 "new 2,99,100",
Tianjie Xu107a34f2017-06-29 17:04:21 -07001035 };
Tianjie Xu5450c842017-10-18 13:15:21 -07001036
Tao Baobc4a6d52018-05-23 00:12:21 -07001037 PackageEntries entries{
1038 { "new_data.br", std::move(encoded_data) },
Tianjie Xu5450c842017-10-18 13:15:21 -07001039 { "patch_data", "" },
1040 { "transfer_list", android::base::Join(transfer_list, '\n') },
1041 };
1042
Tao Baobc4a6d52018-05-23 00:12:21 -07001043 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu107a34f2017-06-29 17:04:21 -07001044
1045 std::string updated_content;
Tao Baobc4a6d52018-05-23 00:12:21 -07001046 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_content));
Tianjie Xu107a34f2017-06-29 17:04:21 -07001047 ASSERT_EQ(brotli_new_data, updated_content);
Tianjie Xu3a8d98d2017-04-03 20:01:17 -07001048}
Tianjie Xu284752e2017-12-05 11:04:17 -08001049
1050TEST_F(UpdaterTest, last_command_update) {
Tao Baobc4a6d52018-05-23 00:12:21 -07001051 std::string block1(4096, '1');
1052 std::string block2(4096, '2');
1053 std::string block3(4096, '3');
Tao Bao3cb3c522018-11-02 13:36:58 -07001054 std::string block1_hash = GetSha1(block1);
1055 std::string block2_hash = GetSha1(block2);
1056 std::string block3_hash = GetSha1(block3);
Tianjie Xu284752e2017-12-05 11:04:17 -08001057
1058 // Compose the transfer list to fail the first update.
Tao Baobc4a6d52018-05-23 00:12:21 -07001059 std::vector<std::string> transfer_list_fail{
1060 // clang-format off
Tianjie Xu284752e2017-12-05 11:04:17 -08001061 "4",
1062 "2",
1063 "0",
1064 "2",
1065 "stash " + block1_hash + " 2,0,1",
1066 "move " + block1_hash + " 2,1,2 1 2,0,1",
1067 "stash " + block3_hash + " 2,2,3",
Tao Bao91a649a2018-05-21 16:05:56 -07001068 "abort",
Tao Baobc4a6d52018-05-23 00:12:21 -07001069 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -08001070 };
1071
1072 // Mimic a resumed update with the same transfer commands.
Tao Baobc4a6d52018-05-23 00:12:21 -07001073 std::vector<std::string> transfer_list_continue{
1074 // clang-format off
Tianjie Xu284752e2017-12-05 11:04:17 -08001075 "4",
1076 "2",
1077 "0",
1078 "2",
1079 "stash " + block1_hash + " 2,0,1",
1080 "move " + block1_hash + " 2,1,2 1 2,0,1",
1081 "stash " + block3_hash + " 2,2,3",
1082 "move " + block1_hash + " 2,2,3 1 2,0,1",
Tao Baobc4a6d52018-05-23 00:12:21 -07001083 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -08001084 };
1085
Tao Baobc4a6d52018-05-23 00:12:21 -07001086 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_));
1087
1088 PackageEntries entries{
Tianjie Xu284752e2017-12-05 11:04:17 -08001089 { "new_data", "" },
1090 { "patch_data", "" },
Tao Baobc4a6d52018-05-23 00:12:21 -07001091 { "transfer_list", android::base::Join(transfer_list_fail, '\n') },
Tianjie Xu284752e2017-12-05 11:04:17 -08001092 };
1093
Tao Baobc4a6d52018-05-23 00:12:21 -07001094 // "2\nstash " + block3_hash + " 2,2,3"
Tao Baof8811bb2018-06-18 10:03:52 -07001095 std::string last_command_content =
1096 "2\n" + transfer_list_fail[TransferList::kTransferListHeaderLines + 2];
Tianjie Xu284752e2017-12-05 11:04:17 -08001097
Tao Baobc4a6d52018-05-23 00:12:21 -07001098 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu284752e2017-12-05 11:04:17 -08001099
1100 // Expect last_command to contain the last stash command.
Tao Baobc4a6d52018-05-23 00:12:21 -07001101 std::string last_command_actual;
Tao Bao7064aa22018-05-24 21:43:50 -07001102 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
Tao Baobc4a6d52018-05-23 00:12:21 -07001103 EXPECT_EQ(last_command_content, last_command_actual);
1104
Tianjie Xu284752e2017-12-05 11:04:17 -08001105 std::string updated_contents;
Tao Baobc4a6d52018-05-23 00:12:21 -07001106 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_contents));
Tianjie Xu284752e2017-12-05 11:04:17 -08001107 ASSERT_EQ(block1 + block1 + block3, updated_contents);
1108
Tao Baobc4a6d52018-05-23 00:12:21 -07001109 // "Resume" the update. Expect the first 'move' to be skipped but the second 'move' to be
1110 // executed. Note that we intentionally reset the image file.
1111 entries["transfer_list"] = android::base::Join(transfer_list_continue, '\n');
1112 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_));
1113 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu284752e2017-12-05 11:04:17 -08001114
Tao Baobc4a6d52018-05-23 00:12:21 -07001115 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_contents));
1116 ASSERT_EQ(block1 + block2 + block1, updated_contents);
Tianjie Xu284752e2017-12-05 11:04:17 -08001117}
1118
1119TEST_F(UpdaterTest, last_command_update_unresumable) {
Tao Baobc4a6d52018-05-23 00:12:21 -07001120 std::string block1(4096, '1');
1121 std::string block2(4096, '2');
Tao Bao3cb3c522018-11-02 13:36:58 -07001122 std::string block1_hash = GetSha1(block1);
1123 std::string block2_hash = GetSha1(block2);
Tianjie Xu284752e2017-12-05 11:04:17 -08001124
1125 // Construct an unresumable update with source blocks mismatch.
Tao Baobc4a6d52018-05-23 00:12:21 -07001126 std::vector<std::string> transfer_list_unresumable{
1127 // clang-format off
1128 "4",
1129 "2",
1130 "0",
1131 "2",
1132 "stash " + block1_hash + " 2,0,1",
1133 "move " + block2_hash + " 2,1,2 1 2,0,1",
1134 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -08001135 };
1136
Tao Baobc4a6d52018-05-23 00:12:21 -07001137 PackageEntries entries{
Tianjie Xu284752e2017-12-05 11:04:17 -08001138 { "new_data", "" },
1139 { "patch_data", "" },
Tao Baobc4a6d52018-05-23 00:12:21 -07001140 { "transfer_list", android::base::Join(transfer_list_unresumable, '\n') },
Tianjie Xu284752e2017-12-05 11:04:17 -08001141 };
1142
Tao Baobc4a6d52018-05-23 00:12:21 -07001143 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block1, image_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -08001144
Tao Baof8811bb2018-06-18 10:03:52 -07001145 std::string last_command_content =
1146 "0\n" + transfer_list_unresumable[TransferList::kTransferListHeaderLines];
Tao Bao7064aa22018-05-24 21:43:50 -07001147 ASSERT_TRUE(android::base::WriteStringToFile(last_command_content, last_command_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -08001148
Tao Baobc4a6d52018-05-23 00:12:21 -07001149 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu284752e2017-12-05 11:04:17 -08001150
Tao Baobc4a6d52018-05-23 00:12:21 -07001151 // The last_command_file will be deleted if the update encounters an unresumable failure later.
Tao Bao7064aa22018-05-24 21:43:50 -07001152 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
Tianjie Xu284752e2017-12-05 11:04:17 -08001153}
1154
1155TEST_F(UpdaterTest, last_command_verify) {
Tao Baobc4a6d52018-05-23 00:12:21 -07001156 std::string block1(4096, '1');
1157 std::string block2(4096, '2');
1158 std::string block3(4096, '3');
Tao Bao3cb3c522018-11-02 13:36:58 -07001159 std::string block1_hash = GetSha1(block1);
1160 std::string block2_hash = GetSha1(block2);
1161 std::string block3_hash = GetSha1(block3);
Tianjie Xu284752e2017-12-05 11:04:17 -08001162
Tao Baobc4a6d52018-05-23 00:12:21 -07001163 std::vector<std::string> transfer_list_verify{
1164 // clang-format off
Tianjie Xu284752e2017-12-05 11:04:17 -08001165 "4",
1166 "2",
1167 "0",
1168 "2",
1169 "stash " + block1_hash + " 2,0,1",
1170 "move " + block1_hash + " 2,0,1 1 2,0,1",
1171 "move " + block1_hash + " 2,1,2 1 2,0,1",
1172 "stash " + block3_hash + " 2,2,3",
Tao Baobc4a6d52018-05-23 00:12:21 -07001173 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -08001174 };
1175
Tao Baobc4a6d52018-05-23 00:12:21 -07001176 PackageEntries entries{
Tianjie Xu284752e2017-12-05 11:04:17 -08001177 { "new_data", "" },
1178 { "patch_data", "" },
Tao Baobc4a6d52018-05-23 00:12:21 -07001179 { "transfer_list", android::base::Join(transfer_list_verify, '\n') },
Tianjie Xu284752e2017-12-05 11:04:17 -08001180 };
1181
Tao Baobc4a6d52018-05-23 00:12:21 -07001182 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block1 + block3, image_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -08001183
Tao Baobc4a6d52018-05-23 00:12:21 -07001184 // Last command: "move " + block1_hash + " 2,1,2 1 2,0,1"
Tao Baof8811bb2018-06-18 10:03:52 -07001185 std::string last_command_content =
1186 "2\n" + transfer_list_verify[TransferList::kTransferListHeaderLines + 2];
Tianjie Xu284752e2017-12-05 11:04:17 -08001187
Tao Baobc4a6d52018-05-23 00:12:21 -07001188 // First run: expect the verification to succeed and the last_command_file is intact.
Tao Bao7064aa22018-05-24 21:43:50 -07001189 ASSERT_TRUE(android::base::WriteStringToFile(last_command_content, last_command_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -08001190
Tao Baobc4a6d52018-05-23 00:12:21 -07001191 RunBlockImageUpdate(true, entries, image_file_, "t");
Tianjie Xu284752e2017-12-05 11:04:17 -08001192
Tao Baobc4a6d52018-05-23 00:12:21 -07001193 std::string last_command_actual;
Tao Bao7064aa22018-05-24 21:43:50 -07001194 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
Tao Baobc4a6d52018-05-23 00:12:21 -07001195 EXPECT_EQ(last_command_content, last_command_actual);
Tianjie Xu284752e2017-12-05 11:04:17 -08001196
Tao Baobc4a6d52018-05-23 00:12:21 -07001197 // Second run with a mismatching block image: expect the verification to succeed but
1198 // last_command_file to be deleted; because the target blocks in the last command don't have the
1199 // expected contents for the second move command.
1200 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_));
1201 RunBlockImageUpdate(true, entries, image_file_, "t");
Tao Bao7064aa22018-05-24 21:43:50 -07001202 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
Tianjie Xu284752e2017-12-05 11:04:17 -08001203}
Tao Bao0c7839a2016-10-10 15:48:37 -07001204
Tao Baoc0299ed2018-05-24 00:16:35 -07001205class ResumableUpdaterTest : public testing::TestWithParam<size_t> {
1206 protected:
1207 void SetUp() override {
1208 RegisterBuiltins();
1209 RegisterInstallFunctions();
1210 RegisterBlockImageFunctions();
Tao Bao0c7839a2016-10-10 15:48:37 -07001211
Tao Baoc0299ed2018-05-24 00:16:35 -07001212 Paths::Get().set_cache_temp_source(temp_saved_source_.path);
1213 Paths::Get().set_last_command_file(temp_last_command_.path);
1214 Paths::Get().set_stash_directory_base(temp_stash_base_.path);
Tao Bao0c7839a2016-10-10 15:48:37 -07001215
Tao Bao91a649a2018-05-21 16:05:56 -07001216 // Enable a special command "abort" to simulate interruption.
1217 Command::abort_allowed_ = true;
1218
Tao Baoc0299ed2018-05-24 00:16:35 -07001219 index_ = GetParam();
1220 image_file_ = image_temp_file_.path;
1221 last_command_file_ = temp_last_command_.path;
1222 }
1223
1224 void TearDown() override {
1225 // Clean up the last_command_file if any.
1226 ASSERT_TRUE(android::base::RemoveFileIfExists(last_command_file_));
1227
1228 // Clear partition updated marker if any.
1229 std::string updated_marker{ temp_stash_base_.path };
Tao Bao3cb3c522018-11-02 13:36:58 -07001230 updated_marker += "/" + GetSha1(image_temp_file_.path) + ".UPDATED";
Tao Baoc0299ed2018-05-24 00:16:35 -07001231 ASSERT_TRUE(android::base::RemoveFileIfExists(updated_marker));
1232 }
1233
1234 TemporaryFile temp_saved_source_;
1235 TemporaryDir temp_stash_base_;
1236 std::string last_command_file_;
1237 std::string image_file_;
1238 size_t index_;
1239
1240 private:
1241 TemporaryFile temp_last_command_;
1242 TemporaryFile image_temp_file_;
1243};
1244
1245static std::string g_source_image;
1246static std::string g_target_image;
1247static PackageEntries g_entries;
1248
1249static std::vector<std::string> GenerateTransferList() {
1250 std::string a(4096, 'a');
1251 std::string b(4096, 'b');
1252 std::string c(4096, 'c');
1253 std::string d(4096, 'd');
1254 std::string e(4096, 'e');
1255 std::string f(4096, 'f');
1256 std::string g(4096, 'g');
1257 std::string h(4096, 'h');
1258 std::string i(4096, 'i');
1259 std::string zero(4096, '\0');
1260
Tao Bao3cb3c522018-11-02 13:36:58 -07001261 std::string a_hash = GetSha1(a);
1262 std::string b_hash = GetSha1(b);
1263 std::string c_hash = GetSha1(c);
1264 std::string e_hash = GetSha1(e);
Tao Baoc0299ed2018-05-24 00:16:35 -07001265
1266 auto loc = [](const std::string& range_text) {
1267 std::vector<std::string> pieces = android::base::Split(range_text, "-");
1268 size_t left;
1269 size_t right;
1270 if (pieces.size() == 1) {
1271 CHECK(android::base::ParseUint(pieces[0], &left));
1272 right = left + 1;
1273 } else {
1274 CHECK_EQ(2u, pieces.size());
1275 CHECK(android::base::ParseUint(pieces[0], &left));
1276 CHECK(android::base::ParseUint(pieces[1], &right));
1277 right++;
1278 }
1279 return android::base::StringPrintf("2,%zu,%zu", left, right);
1280 };
1281
1282 // patch 1: "b d c" -> "g"
1283 TemporaryFile patch_file_bdc_g;
1284 std::string bdc = b + d + c;
Tao Bao3cb3c522018-11-02 13:36:58 -07001285 std::string bdc_hash = GetSha1(bdc);
1286 std::string g_hash = GetSha1(g);
Tao Baoc0299ed2018-05-24 00:16:35 -07001287 CHECK_EQ(0, bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(bdc.data()), bdc.size(),
1288 reinterpret_cast<const uint8_t*>(g.data()), g.size(),
1289 patch_file_bdc_g.path, nullptr));
1290 std::string patch_bdc_g;
1291 CHECK(android::base::ReadFileToString(patch_file_bdc_g.path, &patch_bdc_g));
1292
1293 // patch 2: "a b c d" -> "d c b"
1294 TemporaryFile patch_file_abcd_dcb;
1295 std::string abcd = a + b + c + d;
Tao Bao3cb3c522018-11-02 13:36:58 -07001296 std::string abcd_hash = GetSha1(abcd);
Tao Baoc0299ed2018-05-24 00:16:35 -07001297 std::string dcb = d + c + b;
Tao Bao3cb3c522018-11-02 13:36:58 -07001298 std::string dcb_hash = GetSha1(dcb);
Tao Baoc0299ed2018-05-24 00:16:35 -07001299 CHECK_EQ(0, bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(abcd.data()), abcd.size(),
1300 reinterpret_cast<const uint8_t*>(dcb.data()), dcb.size(),
1301 patch_file_abcd_dcb.path, nullptr));
1302 std::string patch_abcd_dcb;
1303 CHECK(android::base::ReadFileToString(patch_file_abcd_dcb.path, &patch_abcd_dcb));
1304
1305 std::vector<std::string> transfer_list{
1306 "4",
1307 "10", // total blocks written
1308 "2", // maximum stash entries
1309 "2", // maximum number of stashed blocks
1310
1311 // a b c d e a b c d e
1312 "stash " + b_hash + " " + loc("1"),
1313 // a b c d e a b c d e [b(1)]
1314 "stash " + c_hash + " " + loc("2"),
1315 // a b c d e a b c d e [b(1)][c(2)]
1316 "new " + loc("1-2"),
1317 // a i h d e a b c d e [b(1)][c(2)]
1318 "zero " + loc("0"),
1319 // 0 i h d e a b c d e [b(1)][c(2)]
1320
1321 // bsdiff "b d c" (from stash, 3, stash) to get g(3)
1322 android::base::StringPrintf(
1323 "bsdiff 0 %zu %s %s %s 3 %s %s %s:%s %s:%s",
1324 patch_bdc_g.size(), // patch start (0), patch length
1325 bdc_hash.c_str(), // source hash
1326 g_hash.c_str(), // target hash
1327 loc("3").c_str(), // target range
1328 loc("3").c_str(), loc("1").c_str(), // load "d" from block 3, into buffer at offset 1
1329 b_hash.c_str(), loc("0").c_str(), // load "b" from stash, into buffer at offset 0
1330 c_hash.c_str(), loc("2").c_str()), // load "c" from stash, into buffer at offset 2
1331
1332 // 0 i h g e a b c d e [b(1)][c(2)]
1333 "free " + b_hash,
1334 // 0 i h g e a b c d e [c(2)]
1335 "free " + a_hash,
1336 // 0 i h g e a b c d e
1337 "stash " + a_hash + " " + loc("5"),
1338 // 0 i h g e a b c d e [a(5)]
1339 "move " + e_hash + " " + loc("5") + " 1 " + loc("4"),
1340 // 0 i h g e e b c d e [a(5)]
1341
1342 // bsdiff "a b c d" (from stash, 6-8) to "d c b" (6-8)
1343 android::base::StringPrintf( //
1344 "bsdiff %zu %zu %s %s %s 4 %s %s %s:%s",
1345 patch_bdc_g.size(), // patch start
1346 patch_bdc_g.size() + patch_abcd_dcb.size(), // patch length
1347 abcd_hash.c_str(), // source hash
1348 dcb_hash.c_str(), // target hash
1349 loc("6-8").c_str(), // target range
1350 loc("6-8").c_str(), // load "b c d" from blocks 6-8
1351 loc("1-3").c_str(), // into buffer at offset 1-3
1352 a_hash.c_str(), // load "a" from stash
1353 loc("0").c_str()), // into buffer at offset 0
1354
1355 // 0 i h g e e d c b e [a(5)]
1356 "new " + loc("4"),
1357 // 0 i h g f e d c b e [a(5)]
1358 "move " + a_hash + " " + loc("9") + " 1 - " + a_hash + ":" + loc("0"),
1359 // 0 i h g f e d c b a [a(5)]
1360 "free " + a_hash,
1361 // 0 i h g f e d c b a
1362 };
1363
1364 std::string new_data = i + h + f;
1365 std::string patch_data = patch_bdc_g + patch_abcd_dcb;
1366
1367 g_entries = {
1368 { "new_data", new_data },
1369 { "patch_data", patch_data },
1370 };
1371 g_source_image = a + b + c + d + e + a + b + c + d + e;
1372 g_target_image = zero + i + h + g + f + e + d + c + b + a;
1373
1374 return transfer_list;
1375}
1376
1377static const std::vector<std::string> g_transfer_list = GenerateTransferList();
1378
1379INSTANTIATE_TEST_CASE_P(InterruptAfterEachCommand, ResumableUpdaterTest,
1380 ::testing::Range(static_cast<size_t>(0),
Tao Baof8811bb2018-06-18 10:03:52 -07001381 g_transfer_list.size() -
1382 TransferList::kTransferListHeaderLines));
Tao Baoc0299ed2018-05-24 00:16:35 -07001383
1384TEST_P(ResumableUpdaterTest, InterruptVerifyResume) {
1385 ASSERT_TRUE(android::base::WriteStringToFile(g_source_image, image_file_));
1386
1387 LOG(INFO) << "Interrupting at line " << index_ << " ("
Tao Baof8811bb2018-06-18 10:03:52 -07001388 << g_transfer_list[TransferList::kTransferListHeaderLines + index_] << ")";
Tao Baoc0299ed2018-05-24 00:16:35 -07001389
1390 std::vector<std::string> transfer_list_copy{ g_transfer_list };
Tao Baof8811bb2018-06-18 10:03:52 -07001391 transfer_list_copy[TransferList::kTransferListHeaderLines + index_] = "abort";
Tao Baoc0299ed2018-05-24 00:16:35 -07001392
1393 g_entries["transfer_list"] = android::base::Join(transfer_list_copy, '\n');
1394
1395 // Run update that's expected to fail.
1396 RunBlockImageUpdate(false, g_entries, image_file_, "");
1397
1398 std::string last_command_expected;
1399
1400 // Assert the last_command_file.
1401 if (index_ == 0) {
1402 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
1403 } else {
Tao Baof8811bb2018-06-18 10:03:52 -07001404 last_command_expected = std::to_string(index_ - 1) + "\n" +
1405 g_transfer_list[TransferList::kTransferListHeaderLines + index_ - 1];
Tao Baoc0299ed2018-05-24 00:16:35 -07001406 std::string last_command_actual;
1407 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
1408 ASSERT_EQ(last_command_expected, last_command_actual);
1409 }
1410
1411 g_entries["transfer_list"] = android::base::Join(g_transfer_list, '\n');
1412
1413 // Resume the interrupted update, by doing verification first.
1414 RunBlockImageUpdate(true, g_entries, image_file_, "t");
1415
1416 // last_command_file should remain intact.
1417 if (index_ == 0) {
1418 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
1419 } else {
1420 std::string last_command_actual;
1421 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
1422 ASSERT_EQ(last_command_expected, last_command_actual);
1423 }
1424
1425 // Resume the update.
1426 RunBlockImageUpdate(false, g_entries, image_file_, "t");
1427
1428 // last_command_file should be gone after successful update.
1429 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
1430
1431 std::string updated_image_actual;
1432 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_image_actual));
1433 ASSERT_EQ(g_target_image, updated_image_actual);
Tao Bao0c7839a2016-10-10 15:48:37 -07001434}