blob: 9fcf17f13d52b351c88f09703de7b1852cdc0b5c [file] [log] [blame]
Tao Bao0c7839a2016-10-10 15:48:37 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Tao Bao9aa7ab52017-01-05 17:27:19 -080017#include <stdio.h>
Tianjie Xu107a34f2017-06-29 17:04:21 -070018#include <stdlib.h>
Tao Bao89929022016-11-08 20:51:31 -080019#include <sys/stat.h>
20#include <sys/types.h>
21#include <unistd.h>
22
Tianjie Xu107a34f2017-06-29 17:04:21 -070023#include <algorithm>
Tianjie Xuc4447322017-03-06 14:44:59 -080024#include <memory>
Tao Bao0c7839a2016-10-10 15:48:37 -070025#include <string>
Tianjie Xu5450c842017-10-18 13:15:21 -070026#include <unordered_map>
Tianjie Xu56ebe622017-03-16 00:48:21 -070027#include <vector>
Tao Bao0c7839a2016-10-10 15:48:37 -070028
Tao Bao51d516e2016-11-03 14:49:01 -070029#include <android-base/file.h>
Tao Baobc4a6d52018-05-23 00:12:21 -070030#include <android-base/logging.h>
31#include <android-base/parseint.h>
Tao Bao0c7839a2016-10-10 15:48:37 -070032#include <android-base/properties.h>
Tao Bao9aa7ab52017-01-05 17:27:19 -080033#include <android-base/stringprintf.h>
34#include <android-base/strings.h>
Tao Bao51d516e2016-11-03 14:49:01 -070035#include <android-base/test_utils.h>
Tao Baobedf5fc2016-11-18 12:01:26 -080036#include <bootloader_message/bootloader_message.h>
Tianjie Xu107a34f2017-06-29 17:04:21 -070037#include <brotli/encode.h>
Alex Deymofa188262017-10-10 17:56:17 +020038#include <bsdiff/bsdiff.h>
Tao Bao0c7839a2016-10-10 15:48:37 -070039#include <gtest/gtest.h>
Tao Baoef0eb3b2016-11-14 21:29:52 -080040#include <ziparchive/zip_archive.h>
Tianjie Xu56ebe622017-03-16 00:48:21 -070041#include <ziparchive/zip_writer.h>
Tao Bao0c7839a2016-10-10 15:48:37 -070042
Tao Baoef0eb3b2016-11-14 21:29:52 -080043#include "common/test_constants.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070044#include "edify/expr.h"
Tao Bao1fc5bf32017-10-06 07:43:41 -070045#include "otautil/error_code.h"
Tao Bao641fa972018-04-25 18:59:40 -070046#include "otautil/paths.h"
Tao Bao09e468f2017-09-29 14:39:33 -070047#include "otautil/print_sha1.h"
Tao Bao17054c02018-05-03 22:41:23 -070048#include "otautil/sysutil.h"
Tao Bao91a649a2018-05-21 16:05:56 -070049#include "private/commands.h"
Tianjie Xu56ebe622017-03-16 00:48:21 -070050#include "updater/blockimg.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070051#include "updater/install.h"
Tao Baoef0eb3b2016-11-14 21:29:52 -080052#include "updater/updater.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070053
Tao Baobafd6c72018-07-09 15:08:50 -070054using namespace std::string_literals;
55
Tao Baobc4a6d52018-05-23 00:12:21 -070056using PackageEntries = std::unordered_map<std::string, std::string>;
57
58static constexpr size_t kTransferListHeaderLines = 4;
59
60struct 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
Tianjie Xu56ebe622017-03-16 00:48:21 -0700137static std::string get_sha1(const std::string& content) {
138 uint8_t digest[SHA_DIGEST_LENGTH];
139 SHA1(reinterpret_cast<const uint8_t*>(content.c_str()), content.size(), digest);
140 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 };
190 updated_marker += "/" + get_sha1(image_temp_file_.path) + ".UPDATED";
191 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 Baodb56eb02017-03-23 06:34:20 -0700218TEST_F(UpdaterTest, apply_patch_check) {
219 // Zero-argument is not valid.
220 expect(nullptr, "apply_patch_check()", kArgsParsingFailure);
221
222 // File not found.
223 expect("", "apply_patch_check(\"/doesntexist\")", kNoCause);
224
225 std::string src_file = from_testdata_base("old.file");
226 std::string src_content;
227 ASSERT_TRUE(android::base::ReadFileToString(src_file, &src_content));
228 size_t src_size = src_content.size();
229 std::string src_hash = get_sha1(src_content);
230
231 // One-argument with EMMC:file:size:sha1 should pass the check.
232 std::string filename = android::base::Join(
233 std::vector<std::string>{ "EMMC", src_file, std::to_string(src_size), src_hash }, ":");
234 std::string cmd = "apply_patch_check(\"" + filename + "\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700235 expect("t", cmd, kNoCause);
Tao Baodb56eb02017-03-23 06:34:20 -0700236
237 // EMMC:file:(size-1):sha1:(size+1):sha1 should fail the check.
238 std::string filename_bad = android::base::Join(
239 std::vector<std::string>{ "EMMC", src_file, std::to_string(src_size - 1), src_hash,
240 std::to_string(src_size + 1), src_hash },
241 ":");
242 cmd = "apply_patch_check(\"" + filename_bad + "\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700243 expect("", cmd, kNoCause);
Tao Baodb56eb02017-03-23 06:34:20 -0700244
245 // EMMC:file:(size-1):sha1:size:sha1:(size+1):sha1 should pass the check.
246 filename_bad =
247 android::base::Join(std::vector<std::string>{ "EMMC", src_file, std::to_string(src_size - 1),
248 src_hash, std::to_string(src_size), src_hash,
249 std::to_string(src_size + 1), src_hash },
250 ":");
251 cmd = "apply_patch_check(\"" + filename_bad + "\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700252 expect("t", cmd, kNoCause);
Tao Baodb56eb02017-03-23 06:34:20 -0700253
254 // Multiple arguments.
Tao Bao7c1d4262018-07-06 23:18:14 -0700255 // As long as it successfully loads the partition specified in filename, it won't check against
256 // any given SHAs.
Tao Baodb56eb02017-03-23 06:34:20 -0700257 cmd = "apply_patch_check(\"" + filename + "\", \"wrong_sha1\", \"wrong_sha2\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700258 expect("t", cmd, kNoCause);
Tao Baodb56eb02017-03-23 06:34:20 -0700259
260 cmd = "apply_patch_check(\"" + filename + "\", \"wrong_sha1\", \"" + src_hash +
261 "\", \"wrong_sha2\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700262 expect("t", cmd, kNoCause);
Tao Baodb56eb02017-03-23 06:34:20 -0700263
264 cmd = "apply_patch_check(\"" + filename_bad + "\", \"wrong_sha1\", \"" + src_hash +
265 "\", \"wrong_sha2\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700266 expect("t", cmd, kNoCause);
Tao Baodb56eb02017-03-23 06:34:20 -0700267}
268
Tao Bao51d516e2016-11-03 14:49:01 -0700269TEST_F(UpdaterTest, file_getprop) {
270 // file_getprop() expects two arguments.
271 expect(nullptr, "file_getprop()", kArgsParsingFailure);
272 expect(nullptr, "file_getprop(\"arg1\")", kArgsParsingFailure);
273 expect(nullptr, "file_getprop(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
274
275 // File doesn't exist.
276 expect(nullptr, "file_getprop(\"/doesntexist\", \"key1\")", kFileGetPropFailure);
277
278 // Reject too large files (current limit = 65536).
279 TemporaryFile temp_file1;
280 std::string buffer(65540, '\0');
281 ASSERT_TRUE(android::base::WriteStringToFile(buffer, temp_file1.path));
282
283 // Read some keys.
284 TemporaryFile temp_file2;
285 std::string content("ro.product.name=tardis\n"
286 "# comment\n\n\n"
287 "ro.product.model\n"
288 "ro.product.board = magic \n");
289 ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file2.path));
290
291 std::string script1("file_getprop(\"" + std::string(temp_file2.path) +
292 "\", \"ro.product.name\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700293 expect("tardis", script1, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700294
295 std::string script2("file_getprop(\"" + std::string(temp_file2.path) +
296 "\", \"ro.product.board\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700297 expect("magic", script2, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700298
299 // No match.
300 std::string script3("file_getprop(\"" + std::string(temp_file2.path) +
301 "\", \"ro.product.wrong\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700302 expect("", script3, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700303
304 std::string script4("file_getprop(\"" + std::string(temp_file2.path) +
305 "\", \"ro.product.name=\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700306 expect("", script4, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700307
308 std::string script5("file_getprop(\"" + std::string(temp_file2.path) +
309 "\", \"ro.product.nam\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700310 expect("", script5, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700311
312 std::string script6("file_getprop(\"" + std::string(temp_file2.path) +
313 "\", \"ro.product.model\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700314 expect("", script6, kNoCause);
Tao Bao51d516e2016-11-03 14:49:01 -0700315}
Tao Bao0831d0b2016-11-03 23:25:04 -0700316
Tao Baoef0eb3b2016-11-14 21:29:52 -0800317// TODO: Test extracting to block device.
318TEST_F(UpdaterTest, package_extract_file) {
319 // package_extract_file expects 1 or 2 arguments.
320 expect(nullptr, "package_extract_file()", kArgsParsingFailure);
321 expect(nullptr, "package_extract_file(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
322
323 std::string zip_path = from_testdata_base("ziptest_valid.zip");
324 ZipArchiveHandle handle;
325 ASSERT_EQ(0, OpenArchive(zip_path.c_str(), &handle));
326
327 // Need to set up the ziphandle.
328 UpdaterInfo updater_info;
329 updater_info.package_zip = handle;
330
331 // Two-argument version.
332 TemporaryFile temp_file1;
333 std::string script("package_extract_file(\"a.txt\", \"" + std::string(temp_file1.path) + "\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700334 expect("t", script, kNoCause, &updater_info);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800335
336 // Verify the extracted entry.
337 std::string data;
338 ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data));
339 ASSERT_EQ(kATxtContents, data);
340
341 // Now extract another entry to the same location, which should overwrite.
342 script = "package_extract_file(\"b.txt\", \"" + std::string(temp_file1.path) + "\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700343 expect("t", script, kNoCause, &updater_info);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800344
345 ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data));
346 ASSERT_EQ(kBTxtContents, data);
347
348 // Missing zip entry. The two-argument version doesn't abort.
349 script = "package_extract_file(\"doesntexist\", \"" + std::string(temp_file1.path) + "\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700350 expect("", script, kNoCause, &updater_info);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800351
352 // Extract to /dev/full should fail.
353 script = "package_extract_file(\"a.txt\", \"/dev/full\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700354 expect("", script, kNoCause, &updater_info);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800355
Tao Bao0b58e9a2018-07-06 15:01:05 -0700356 // One-argument version. package_extract_file() gives a VAL_BLOB, which needs to be converted to
357 // VAL_STRING for equality test.
358 script = "blob_to_string(package_extract_file(\"a.txt\")) == \"" + kATxtContents + "\"";
Tao Baod8d514f2018-07-09 13:32:28 -0700359 expect("t", script, kNoCause, &updater_info);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800360
Tao Bao0b58e9a2018-07-06 15:01:05 -0700361 script = "blob_to_string(package_extract_file(\"b.txt\")) == \"" + kBTxtContents + "\"";
Tao Baod8d514f2018-07-09 13:32:28 -0700362 expect("t", script, kNoCause, &updater_info);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800363
364 // Missing entry. The one-argument version aborts the evaluation.
365 script = "package_extract_file(\"doesntexist\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700366 expect(nullptr, script, kPackageExtractFileFailure, &updater_info);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800367
368 CloseArchive(handle);
369}
Tao Baod0f30882016-11-03 23:52:01 -0700370
Tao Baobafd6c72018-07-09 15:08:50 -0700371TEST_F(UpdaterTest, read_file) {
372 // read_file() expects one argument.
373 expect(nullptr, "read_file()", kArgsParsingFailure);
374 expect(nullptr, "read_file(\"arg1\", \"arg2\")", kArgsParsingFailure);
375
376 // Write some value to file and read back.
377 TemporaryFile temp_file;
378 std::string script("write_value(\"foo\", \""s + temp_file.path + "\");");
379 expect("t", script, kNoCause);
380
381 script = "read_file(\""s + temp_file.path + "\") == \"foo\"";
382 expect("t", script, kNoCause);
383
384 script = "read_file(\""s + temp_file.path + "\") == \"bar\"";
385 expect("", script, kNoCause);
386
387 // It should fail gracefully when read fails.
388 script = "read_file(\"/doesntexist\")";
389 expect("", script, kNoCause);
390}
391
Tao Baod0f30882016-11-03 23:52:01 -0700392TEST_F(UpdaterTest, write_value) {
393 // write_value() expects two arguments.
394 expect(nullptr, "write_value()", kArgsParsingFailure);
395 expect(nullptr, "write_value(\"arg1\")", kArgsParsingFailure);
396 expect(nullptr, "write_value(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
397
398 // filename cannot be empty.
399 expect(nullptr, "write_value(\"value\", \"\")", kArgsParsingFailure);
400
401 // Write some value to file.
402 TemporaryFile temp_file;
403 std::string value = "magicvalue";
404 std::string script("write_value(\"" + value + "\", \"" + std::string(temp_file.path) + "\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700405 expect("t", script, kNoCause);
Tao Baod0f30882016-11-03 23:52:01 -0700406
407 // Verify the content.
408 std::string content;
409 ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content));
410 ASSERT_EQ(value, content);
411
412 // Allow writing empty string.
413 script = "write_value(\"\", \"" + std::string(temp_file.path) + "\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700414 expect("t", script, kNoCause);
Tao Baod0f30882016-11-03 23:52:01 -0700415
416 // Verify the content.
417 ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content));
418 ASSERT_EQ("", content);
419
420 // It should fail gracefully when write fails.
421 script = "write_value(\"value\", \"/proc/0/file1\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700422 expect("", script, kNoCause);
Tao Baod0f30882016-11-03 23:52:01 -0700423}
Tao Baobedf5fc2016-11-18 12:01:26 -0800424
425TEST_F(UpdaterTest, get_stage) {
426 // get_stage() expects one argument.
427 expect(nullptr, "get_stage()", kArgsParsingFailure);
428 expect(nullptr, "get_stage(\"arg1\", \"arg2\")", kArgsParsingFailure);
429 expect(nullptr, "get_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
430
431 // Set up a local file as BCB.
432 TemporaryFile tf;
433 std::string temp_file(tf.path);
434 bootloader_message boot;
435 strlcpy(boot.stage, "2/3", sizeof(boot.stage));
436 std::string err;
437 ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err));
438
439 // Can read the stage value.
440 std::string script("get_stage(\"" + temp_file + "\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700441 expect("2/3", script, kNoCause);
Tao Baobedf5fc2016-11-18 12:01:26 -0800442
443 // Bad BCB path.
444 script = "get_stage(\"doesntexist\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700445 expect("", script, kNoCause);
Tao Baobedf5fc2016-11-18 12:01:26 -0800446}
447
448TEST_F(UpdaterTest, set_stage) {
449 // set_stage() expects two arguments.
450 expect(nullptr, "set_stage()", kArgsParsingFailure);
451 expect(nullptr, "set_stage(\"arg1\")", kArgsParsingFailure);
452 expect(nullptr, "set_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
453
454 // Set up a local file as BCB.
455 TemporaryFile tf;
456 std::string temp_file(tf.path);
457 bootloader_message boot;
458 strlcpy(boot.command, "command", sizeof(boot.command));
459 strlcpy(boot.stage, "2/3", sizeof(boot.stage));
460 std::string err;
461 ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err));
462
463 // Write with set_stage().
464 std::string script("set_stage(\"" + temp_file + "\", \"1/3\")");
Tao Baod8d514f2018-07-09 13:32:28 -0700465 expect(tf.path, script, kNoCause);
Tao Baobedf5fc2016-11-18 12:01:26 -0800466
467 // Verify.
468 bootloader_message boot_verify;
469 ASSERT_TRUE(read_bootloader_message_from(&boot_verify, temp_file, &err));
470
471 // Stage should be updated, with command part untouched.
472 ASSERT_STREQ("1/3", boot_verify.stage);
473 ASSERT_STREQ(boot.command, boot_verify.command);
474
475 // Bad BCB path.
476 script = "set_stage(\"doesntexist\", \"1/3\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700477 expect("", script, kNoCause);
Tao Baobedf5fc2016-11-18 12:01:26 -0800478
479 script = "set_stage(\"/dev/full\", \"1/3\")";
Tao Baod8d514f2018-07-09 13:32:28 -0700480 expect("", script, kNoCause);
Tao Baobedf5fc2016-11-18 12:01:26 -0800481}
Tao Bao9aa7ab52017-01-05 17:27:19 -0800482
483TEST_F(UpdaterTest, set_progress) {
484 // set_progress() expects one argument.
485 expect(nullptr, "set_progress()", kArgsParsingFailure);
486 expect(nullptr, "set_progress(\"arg1\", \"arg2\")", kArgsParsingFailure);
487
488 // Invalid progress argument.
489 expect(nullptr, "set_progress(\"arg1\")", kArgsParsingFailure);
490 expect(nullptr, "set_progress(\"3x+5\")", kArgsParsingFailure);
491 expect(nullptr, "set_progress(\".3.5\")", kArgsParsingFailure);
492
493 TemporaryFile tf;
494 UpdaterInfo updater_info;
Tianjie Xu79327ac2017-09-08 17:09:10 -0700495 updater_info.cmd_pipe = fdopen(tf.release(), "w");
Tao Bao9aa7ab52017-01-05 17:27:19 -0800496 expect(".52", "set_progress(\".52\")", kNoCause, &updater_info);
497 fflush(updater_info.cmd_pipe);
498
499 std::string cmd;
500 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd));
501 ASSERT_EQ(android::base::StringPrintf("set_progress %f\n", .52), cmd);
502 // recovery-updater protocol expects 2 tokens ("set_progress <frac>").
503 ASSERT_EQ(2U, android::base::Split(cmd, " ").size());
Tianjie Xu79327ac2017-09-08 17:09:10 -0700504 ASSERT_EQ(0, fclose(updater_info.cmd_pipe));
Tao Bao9aa7ab52017-01-05 17:27:19 -0800505}
506
507TEST_F(UpdaterTest, show_progress) {
508 // show_progress() expects two arguments.
509 expect(nullptr, "show_progress()", kArgsParsingFailure);
510 expect(nullptr, "show_progress(\"arg1\")", kArgsParsingFailure);
511 expect(nullptr, "show_progress(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
512
513 // Invalid progress arguments.
514 expect(nullptr, "show_progress(\"arg1\", \"arg2\")", kArgsParsingFailure);
515 expect(nullptr, "show_progress(\"3x+5\", \"10\")", kArgsParsingFailure);
516 expect(nullptr, "show_progress(\".3\", \"5a\")", kArgsParsingFailure);
517
518 TemporaryFile tf;
519 UpdaterInfo updater_info;
Tianjie Xu79327ac2017-09-08 17:09:10 -0700520 updater_info.cmd_pipe = fdopen(tf.release(), "w");
Tao Bao9aa7ab52017-01-05 17:27:19 -0800521 expect(".52", "show_progress(\".52\", \"10\")", kNoCause, &updater_info);
522 fflush(updater_info.cmd_pipe);
523
524 std::string cmd;
525 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd));
526 ASSERT_EQ(android::base::StringPrintf("progress %f %d\n", .52, 10), cmd);
527 // recovery-updater protocol expects 3 tokens ("progress <frac> <secs>").
528 ASSERT_EQ(3U, android::base::Split(cmd, " ").size());
Tianjie Xu79327ac2017-09-08 17:09:10 -0700529 ASSERT_EQ(0, fclose(updater_info.cmd_pipe));
Tao Bao9aa7ab52017-01-05 17:27:19 -0800530}
Tianjie Xu56ebe622017-03-16 00:48:21 -0700531
Tao Baoffede3e2018-06-07 09:56:19 -0700532TEST_F(UpdaterTest, block_image_update_parsing_error) {
533 std::vector<std::string> transfer_list{
534 // clang-format off
535 "4",
536 "2",
537 "0",
538 // clang-format on
539 };
540
541 PackageEntries entries{
542 { "new_data", "" },
543 { "patch_data", "" },
544 { "transfer_list", android::base::Join(transfer_list, '\n') },
545 };
546
547 RunBlockImageUpdate(false, entries, image_file_, "", kArgsParsingFailure);
548}
549
Tianjie Xu5450c842017-10-18 13:15:21 -0700550TEST_F(UpdaterTest, block_image_update_patch_data) {
Tianjie Xu56ebe622017-03-16 00:48:21 -0700551 std::string src_content = std::string(4096, 'a') + std::string(4096, 'c');
552 std::string tgt_content = std::string(4096, 'b') + std::string(4096, 'd');
Tianjie Xu5450c842017-10-18 13:15:21 -0700553
554 // Generate the patch data.
Tianjie Xu56ebe622017-03-16 00:48:21 -0700555 TemporaryFile patch_file;
Tao Baobc4a6d52018-05-23 00:12:21 -0700556 ASSERT_EQ(0,
557 bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(src_content.data()), src_content.size(),
558 reinterpret_cast<const uint8_t*>(tgt_content.data()), tgt_content.size(),
559 patch_file.path, nullptr));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700560 std::string patch_content;
561 ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch_content));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700562
Tianjie Xu5450c842017-10-18 13:15:21 -0700563 // Create the transfer list that contains a bsdiff.
Tianjie Xu56ebe622017-03-16 00:48:21 -0700564 std::string src_hash = get_sha1(src_content);
565 std::string tgt_hash = get_sha1(tgt_content);
Tao Baobc4a6d52018-05-23 00:12:21 -0700566 std::vector<std::string> transfer_list{
567 // clang-format off
Tianjie Xu56ebe622017-03-16 00:48:21 -0700568 "4",
569 "2",
570 "0",
571 "2",
572 "stash " + src_hash + " 2,0,2",
573 android::base::StringPrintf("bsdiff 0 %zu %s %s 2,0,2 2 - %s:2,0,2", patch_content.size(),
574 src_hash.c_str(), tgt_hash.c_str(), src_hash.c_str()),
575 "free " + src_hash,
Tao Baobc4a6d52018-05-23 00:12:21 -0700576 // clang-format on
Tianjie Xu56ebe622017-03-16 00:48:21 -0700577 };
Tianjie Xu56ebe622017-03-16 00:48:21 -0700578
Tao Baobc4a6d52018-05-23 00:12:21 -0700579 PackageEntries entries{
Tianjie Xu5450c842017-10-18 13:15:21 -0700580 { "new_data", "" },
581 { "patch_data", patch_content },
582 { "transfer_list", android::base::Join(transfer_list, '\n') },
Tianjie Xu56ebe622017-03-16 00:48:21 -0700583 };
Tianjie Xu5450c842017-10-18 13:15:21 -0700584
Tao Baobc4a6d52018-05-23 00:12:21 -0700585 ASSERT_TRUE(android::base::WriteStringToFile(src_content, image_file_));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700586
Tao Baobc4a6d52018-05-23 00:12:21 -0700587 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu56ebe622017-03-16 00:48:21 -0700588
Tianjie Xu56ebe622017-03-16 00:48:21 -0700589 // The update_file should be patched correctly.
590 std::string updated_content;
Tao Baobc4a6d52018-05-23 00:12:21 -0700591 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_content));
592 ASSERT_EQ(tgt_content, updated_content);
Tianjie Xu5450c842017-10-18 13:15:21 -0700593}
594
595TEST_F(UpdaterTest, block_image_update_fail) {
596 std::string src_content(4096 * 2, 'e');
597 std::string src_hash = get_sha1(src_content);
598 // Stash and free some blocks, then fail the update intentionally.
Tao Baobc4a6d52018-05-23 00:12:21 -0700599 std::vector<std::string> transfer_list{
600 // clang-format off
601 "4",
602 "2",
603 "0",
604 "2",
605 "stash " + src_hash + " 2,0,2",
606 "free " + src_hash,
Tao Bao91a649a2018-05-21 16:05:56 -0700607 "abort",
Tao Baobc4a6d52018-05-23 00:12:21 -0700608 // clang-format on
Tianjie Xu5450c842017-10-18 13:15:21 -0700609 };
610
611 // Add a new data of 10 bytes to test the deadlock.
Tao Baobc4a6d52018-05-23 00:12:21 -0700612 PackageEntries entries{
Tianjie Xu5450c842017-10-18 13:15:21 -0700613 { "new_data", std::string(10, 0) },
614 { "patch_data", "" },
615 { "transfer_list", android::base::Join(transfer_list, '\n') },
616 };
617
Tao Baobc4a6d52018-05-23 00:12:21 -0700618 ASSERT_TRUE(android::base::WriteStringToFile(src_content, image_file_));
Tianjie Xu5450c842017-10-18 13:15:21 -0700619
Tao Baobc4a6d52018-05-23 00:12:21 -0700620 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu5450c842017-10-18 13:15:21 -0700621
Tianjie Xu56ebe622017-03-16 00:48:21 -0700622 // Updater generates the stash name based on the input file name.
Tao Baobc4a6d52018-05-23 00:12:21 -0700623 std::string name_digest = get_sha1(image_file_);
Tianjie Xu3bbb20f2018-02-27 15:56:11 -0800624 std::string stash_base = std::string(temp_stash_base_.path) + "/" + name_digest;
Tianjie Xu56ebe622017-03-16 00:48:21 -0700625 ASSERT_EQ(0, access(stash_base.c_str(), F_OK));
Tao Baobc4a6d52018-05-23 00:12:21 -0700626 // Expect the stashed blocks to be freed.
Tianjie Xu5450c842017-10-18 13:15:21 -0700627 ASSERT_EQ(-1, access((stash_base + src_hash).c_str(), F_OK));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700628 ASSERT_EQ(0, rmdir(stash_base.c_str()));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700629}
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700630
Tianjie Xu5450c842017-10-18 13:15:21 -0700631TEST_F(UpdaterTest, new_data_over_write) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700632 std::vector<std::string> transfer_list{
633 // clang-format off
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700634 "4",
635 "1",
636 "0",
637 "0",
638 "new 2,0,1",
Tao Baobc4a6d52018-05-23 00:12:21 -0700639 // clang-format on
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700640 };
Tianjie Xu5450c842017-10-18 13:15:21 -0700641
Tao Baobc4a6d52018-05-23 00:12:21 -0700642 // Write 4096 + 100 bytes of new data.
643 PackageEntries entries{
644 { "new_data", std::string(4196, 0) },
Tianjie Xu5450c842017-10-18 13:15:21 -0700645 { "patch_data", "" },
646 { "transfer_list", android::base::Join(transfer_list, '\n') },
647 };
648
Tao Baobc4a6d52018-05-23 00:12:21 -0700649 RunBlockImageUpdate(false, entries, image_file_, "t");
650}
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700651
Tao Baobc4a6d52018-05-23 00:12:21 -0700652TEST_F(UpdaterTest, new_data_short_write) {
653 std::vector<std::string> transfer_list{
654 // clang-format off
655 "4",
656 "1",
657 "0",
658 "0",
659 "new 2,0,1",
660 // clang-format on
661 };
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700662
Tao Baobc4a6d52018-05-23 00:12:21 -0700663 PackageEntries entries{
664 { "patch_data", "" },
665 { "transfer_list", android::base::Join(transfer_list, '\n') },
666 };
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700667
668 // Updater should report the failure gracefully rather than stuck in deadlock.
Tao Baobc4a6d52018-05-23 00:12:21 -0700669 entries["new_data"] = "";
670 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700671
Tao Baobc4a6d52018-05-23 00:12:21 -0700672 entries["new_data"] = std::string(10, 'a');
673 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700674
675 // Expect to write 1 block of new data successfully.
Tao Baobc4a6d52018-05-23 00:12:21 -0700676 entries["new_data"] = std::string(4096, 'a');
677 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu107a34f2017-06-29 17:04:21 -0700678}
679
680TEST_F(UpdaterTest, brotli_new_data) {
Tianjie Xu107a34f2017-06-29 17:04:21 -0700681 auto generator = []() { return rand() % 128; };
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700682 // Generate 100 blocks of random data.
Tianjie Xu107a34f2017-06-29 17:04:21 -0700683 std::string brotli_new_data;
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700684 brotli_new_data.reserve(4096 * 100);
685 generate_n(back_inserter(brotli_new_data), 4096 * 100, generator);
Tianjie Xu107a34f2017-06-29 17:04:21 -0700686
687 size_t encoded_size = BrotliEncoderMaxCompressedSize(brotli_new_data.size());
Tianjie Xu5450c842017-10-18 13:15:21 -0700688 std::string encoded_data(encoded_size, 0);
Tianjie Xu107a34f2017-06-29 17:04:21 -0700689 ASSERT_TRUE(BrotliEncoderCompress(
690 BROTLI_DEFAULT_QUALITY, BROTLI_DEFAULT_WINDOW, BROTLI_DEFAULT_MODE, brotli_new_data.size(),
Tianjie Xu5450c842017-10-18 13:15:21 -0700691 reinterpret_cast<const uint8_t*>(brotli_new_data.data()), &encoded_size,
692 reinterpret_cast<uint8_t*>(const_cast<char*>(encoded_data.data()))));
693 encoded_data.resize(encoded_size);
Tianjie Xu107a34f2017-06-29 17:04:21 -0700694
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700695 // Write a few small chunks of new data, then a large chunk, and finally a few small chunks.
696 // This helps us to catch potential short writes.
Tianjie Xu107a34f2017-06-29 17:04:21 -0700697 std::vector<std::string> transfer_list = {
Tianjie Xu6ed175d2017-07-18 11:29:40 -0700698 "4",
699 "100",
700 "0",
701 "0",
702 "new 2,0,1",
703 "new 2,1,2",
704 "new 4,2,50,50,97",
705 "new 2,97,98",
706 "new 2,98,99",
707 "new 2,99,100",
Tianjie Xu107a34f2017-06-29 17:04:21 -0700708 };
Tianjie Xu5450c842017-10-18 13:15:21 -0700709
Tao Baobc4a6d52018-05-23 00:12:21 -0700710 PackageEntries entries{
711 { "new_data.br", std::move(encoded_data) },
Tianjie Xu5450c842017-10-18 13:15:21 -0700712 { "patch_data", "" },
713 { "transfer_list", android::base::Join(transfer_list, '\n') },
714 };
715
Tao Baobc4a6d52018-05-23 00:12:21 -0700716 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu107a34f2017-06-29 17:04:21 -0700717
718 std::string updated_content;
Tao Baobc4a6d52018-05-23 00:12:21 -0700719 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_content));
Tianjie Xu107a34f2017-06-29 17:04:21 -0700720 ASSERT_EQ(brotli_new_data, updated_content);
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700721}
Tianjie Xu284752e2017-12-05 11:04:17 -0800722
723TEST_F(UpdaterTest, last_command_update) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700724 std::string block1(4096, '1');
725 std::string block2(4096, '2');
726 std::string block3(4096, '3');
Tianjie Xu284752e2017-12-05 11:04:17 -0800727 std::string block1_hash = get_sha1(block1);
728 std::string block2_hash = get_sha1(block2);
729 std::string block3_hash = get_sha1(block3);
730
731 // Compose the transfer list to fail the first update.
Tao Baobc4a6d52018-05-23 00:12:21 -0700732 std::vector<std::string> transfer_list_fail{
733 // clang-format off
Tianjie Xu284752e2017-12-05 11:04:17 -0800734 "4",
735 "2",
736 "0",
737 "2",
738 "stash " + block1_hash + " 2,0,1",
739 "move " + block1_hash + " 2,1,2 1 2,0,1",
740 "stash " + block3_hash + " 2,2,3",
Tao Bao91a649a2018-05-21 16:05:56 -0700741 "abort",
Tao Baobc4a6d52018-05-23 00:12:21 -0700742 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -0800743 };
744
745 // Mimic a resumed update with the same transfer commands.
Tao Baobc4a6d52018-05-23 00:12:21 -0700746 std::vector<std::string> transfer_list_continue{
747 // clang-format off
Tianjie Xu284752e2017-12-05 11:04:17 -0800748 "4",
749 "2",
750 "0",
751 "2",
752 "stash " + block1_hash + " 2,0,1",
753 "move " + block1_hash + " 2,1,2 1 2,0,1",
754 "stash " + block3_hash + " 2,2,3",
755 "move " + block1_hash + " 2,2,3 1 2,0,1",
Tao Baobc4a6d52018-05-23 00:12:21 -0700756 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -0800757 };
758
Tao Baobc4a6d52018-05-23 00:12:21 -0700759 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_));
760
761 PackageEntries entries{
Tianjie Xu284752e2017-12-05 11:04:17 -0800762 { "new_data", "" },
763 { "patch_data", "" },
Tao Baobc4a6d52018-05-23 00:12:21 -0700764 { "transfer_list", android::base::Join(transfer_list_fail, '\n') },
Tianjie Xu284752e2017-12-05 11:04:17 -0800765 };
766
Tao Baobc4a6d52018-05-23 00:12:21 -0700767 // "2\nstash " + block3_hash + " 2,2,3"
768 std::string last_command_content = "2\n" + transfer_list_fail[kTransferListHeaderLines + 2];
Tianjie Xu284752e2017-12-05 11:04:17 -0800769
Tao Baobc4a6d52018-05-23 00:12:21 -0700770 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu284752e2017-12-05 11:04:17 -0800771
772 // Expect last_command to contain the last stash command.
Tao Baobc4a6d52018-05-23 00:12:21 -0700773 std::string last_command_actual;
Tao Bao7064aa22018-05-24 21:43:50 -0700774 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
Tao Baobc4a6d52018-05-23 00:12:21 -0700775 EXPECT_EQ(last_command_content, last_command_actual);
776
Tianjie Xu284752e2017-12-05 11:04:17 -0800777 std::string updated_contents;
Tao Baobc4a6d52018-05-23 00:12:21 -0700778 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_contents));
Tianjie Xu284752e2017-12-05 11:04:17 -0800779 ASSERT_EQ(block1 + block1 + block3, updated_contents);
780
Tao Baobc4a6d52018-05-23 00:12:21 -0700781 // "Resume" the update. Expect the first 'move' to be skipped but the second 'move' to be
782 // executed. Note that we intentionally reset the image file.
783 entries["transfer_list"] = android::base::Join(transfer_list_continue, '\n');
784 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_));
785 RunBlockImageUpdate(false, entries, image_file_, "t");
Tianjie Xu284752e2017-12-05 11:04:17 -0800786
Tao Baobc4a6d52018-05-23 00:12:21 -0700787 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_contents));
788 ASSERT_EQ(block1 + block2 + block1, updated_contents);
Tianjie Xu284752e2017-12-05 11:04:17 -0800789}
790
791TEST_F(UpdaterTest, last_command_update_unresumable) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700792 std::string block1(4096, '1');
793 std::string block2(4096, '2');
Tianjie Xu284752e2017-12-05 11:04:17 -0800794 std::string block1_hash = get_sha1(block1);
795 std::string block2_hash = get_sha1(block2);
796
797 // Construct an unresumable update with source blocks mismatch.
Tao Baobc4a6d52018-05-23 00:12:21 -0700798 std::vector<std::string> transfer_list_unresumable{
799 // clang-format off
800 "4",
801 "2",
802 "0",
803 "2",
804 "stash " + block1_hash + " 2,0,1",
805 "move " + block2_hash + " 2,1,2 1 2,0,1",
806 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -0800807 };
808
Tao Baobc4a6d52018-05-23 00:12:21 -0700809 PackageEntries entries{
Tianjie Xu284752e2017-12-05 11:04:17 -0800810 { "new_data", "" },
811 { "patch_data", "" },
Tao Baobc4a6d52018-05-23 00:12:21 -0700812 { "transfer_list", android::base::Join(transfer_list_unresumable, '\n') },
Tianjie Xu284752e2017-12-05 11:04:17 -0800813 };
814
Tao Baobc4a6d52018-05-23 00:12:21 -0700815 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block1, image_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -0800816
Tao Baobc4a6d52018-05-23 00:12:21 -0700817 std::string last_command_content = "0\n" + transfer_list_unresumable[kTransferListHeaderLines];
Tao Bao7064aa22018-05-24 21:43:50 -0700818 ASSERT_TRUE(android::base::WriteStringToFile(last_command_content, last_command_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -0800819
Tao Baobc4a6d52018-05-23 00:12:21 -0700820 RunBlockImageUpdate(false, entries, image_file_, "");
Tianjie Xu284752e2017-12-05 11:04:17 -0800821
Tao Baobc4a6d52018-05-23 00:12:21 -0700822 // The last_command_file will be deleted if the update encounters an unresumable failure later.
Tao Bao7064aa22018-05-24 21:43:50 -0700823 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
Tianjie Xu284752e2017-12-05 11:04:17 -0800824}
825
826TEST_F(UpdaterTest, last_command_verify) {
Tao Baobc4a6d52018-05-23 00:12:21 -0700827 std::string block1(4096, '1');
828 std::string block2(4096, '2');
829 std::string block3(4096, '3');
Tianjie Xu284752e2017-12-05 11:04:17 -0800830 std::string block1_hash = get_sha1(block1);
831 std::string block2_hash = get_sha1(block2);
832 std::string block3_hash = get_sha1(block3);
833
Tao Baobc4a6d52018-05-23 00:12:21 -0700834 std::vector<std::string> transfer_list_verify{
835 // clang-format off
Tianjie Xu284752e2017-12-05 11:04:17 -0800836 "4",
837 "2",
838 "0",
839 "2",
840 "stash " + block1_hash + " 2,0,1",
841 "move " + block1_hash + " 2,0,1 1 2,0,1",
842 "move " + block1_hash + " 2,1,2 1 2,0,1",
843 "stash " + block3_hash + " 2,2,3",
Tao Baobc4a6d52018-05-23 00:12:21 -0700844 // clang-format on
Tianjie Xu284752e2017-12-05 11:04:17 -0800845 };
846
Tao Baobc4a6d52018-05-23 00:12:21 -0700847 PackageEntries entries{
Tianjie Xu284752e2017-12-05 11:04:17 -0800848 { "new_data", "" },
849 { "patch_data", "" },
Tao Baobc4a6d52018-05-23 00:12:21 -0700850 { "transfer_list", android::base::Join(transfer_list_verify, '\n') },
Tianjie Xu284752e2017-12-05 11:04:17 -0800851 };
852
Tao Baobc4a6d52018-05-23 00:12:21 -0700853 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block1 + block3, image_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -0800854
Tao Baobc4a6d52018-05-23 00:12:21 -0700855 // Last command: "move " + block1_hash + " 2,1,2 1 2,0,1"
856 std::string last_command_content = "2\n" + transfer_list_verify[kTransferListHeaderLines + 2];
Tianjie Xu284752e2017-12-05 11:04:17 -0800857
Tao Baobc4a6d52018-05-23 00:12:21 -0700858 // First run: expect the verification to succeed and the last_command_file is intact.
Tao Bao7064aa22018-05-24 21:43:50 -0700859 ASSERT_TRUE(android::base::WriteStringToFile(last_command_content, last_command_file_));
Tianjie Xu284752e2017-12-05 11:04:17 -0800860
Tao Baobc4a6d52018-05-23 00:12:21 -0700861 RunBlockImageUpdate(true, entries, image_file_, "t");
Tianjie Xu284752e2017-12-05 11:04:17 -0800862
Tao Baobc4a6d52018-05-23 00:12:21 -0700863 std::string last_command_actual;
Tao Bao7064aa22018-05-24 21:43:50 -0700864 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
Tao Baobc4a6d52018-05-23 00:12:21 -0700865 EXPECT_EQ(last_command_content, last_command_actual);
Tianjie Xu284752e2017-12-05 11:04:17 -0800866
Tao Baobc4a6d52018-05-23 00:12:21 -0700867 // Second run with a mismatching block image: expect the verification to succeed but
868 // last_command_file to be deleted; because the target blocks in the last command don't have the
869 // expected contents for the second move command.
870 ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_));
871 RunBlockImageUpdate(true, entries, image_file_, "t");
Tao Bao7064aa22018-05-24 21:43:50 -0700872 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
Tianjie Xu284752e2017-12-05 11:04:17 -0800873}
Tao Baoc0299ed2018-05-24 00:16:35 -0700874
875class ResumableUpdaterTest : public testing::TestWithParam<size_t> {
876 protected:
877 void SetUp() override {
878 RegisterBuiltins();
879 RegisterInstallFunctions();
880 RegisterBlockImageFunctions();
881
882 Paths::Get().set_cache_temp_source(temp_saved_source_.path);
883 Paths::Get().set_last_command_file(temp_last_command_.path);
884 Paths::Get().set_stash_directory_base(temp_stash_base_.path);
885
Tao Bao91a649a2018-05-21 16:05:56 -0700886 // Enable a special command "abort" to simulate interruption.
887 Command::abort_allowed_ = true;
888
Tao Baoc0299ed2018-05-24 00:16:35 -0700889 index_ = GetParam();
890 image_file_ = image_temp_file_.path;
891 last_command_file_ = temp_last_command_.path;
892 }
893
894 void TearDown() override {
895 // Clean up the last_command_file if any.
896 ASSERT_TRUE(android::base::RemoveFileIfExists(last_command_file_));
897
898 // Clear partition updated marker if any.
899 std::string updated_marker{ temp_stash_base_.path };
900 updated_marker += "/" + get_sha1(image_temp_file_.path) + ".UPDATED";
901 ASSERT_TRUE(android::base::RemoveFileIfExists(updated_marker));
902 }
903
904 TemporaryFile temp_saved_source_;
905 TemporaryDir temp_stash_base_;
906 std::string last_command_file_;
907 std::string image_file_;
908 size_t index_;
909
910 private:
911 TemporaryFile temp_last_command_;
912 TemporaryFile image_temp_file_;
913};
914
915static std::string g_source_image;
916static std::string g_target_image;
917static PackageEntries g_entries;
918
919static std::vector<std::string> GenerateTransferList() {
920 std::string a(4096, 'a');
921 std::string b(4096, 'b');
922 std::string c(4096, 'c');
923 std::string d(4096, 'd');
924 std::string e(4096, 'e');
925 std::string f(4096, 'f');
926 std::string g(4096, 'g');
927 std::string h(4096, 'h');
928 std::string i(4096, 'i');
929 std::string zero(4096, '\0');
930
931 std::string a_hash = get_sha1(a);
932 std::string b_hash = get_sha1(b);
933 std::string c_hash = get_sha1(c);
934 std::string e_hash = get_sha1(e);
935
936 auto loc = [](const std::string& range_text) {
937 std::vector<std::string> pieces = android::base::Split(range_text, "-");
938 size_t left;
939 size_t right;
940 if (pieces.size() == 1) {
941 CHECK(android::base::ParseUint(pieces[0], &left));
942 right = left + 1;
943 } else {
944 CHECK_EQ(2u, pieces.size());
945 CHECK(android::base::ParseUint(pieces[0], &left));
946 CHECK(android::base::ParseUint(pieces[1], &right));
947 right++;
948 }
949 return android::base::StringPrintf("2,%zu,%zu", left, right);
950 };
951
952 // patch 1: "b d c" -> "g"
953 TemporaryFile patch_file_bdc_g;
954 std::string bdc = b + d + c;
955 std::string bdc_hash = get_sha1(bdc);
956 std::string g_hash = get_sha1(g);
957 CHECK_EQ(0, bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(bdc.data()), bdc.size(),
958 reinterpret_cast<const uint8_t*>(g.data()), g.size(),
959 patch_file_bdc_g.path, nullptr));
960 std::string patch_bdc_g;
961 CHECK(android::base::ReadFileToString(patch_file_bdc_g.path, &patch_bdc_g));
962
963 // patch 2: "a b c d" -> "d c b"
964 TemporaryFile patch_file_abcd_dcb;
965 std::string abcd = a + b + c + d;
966 std::string abcd_hash = get_sha1(abcd);
967 std::string dcb = d + c + b;
968 std::string dcb_hash = get_sha1(dcb);
969 CHECK_EQ(0, bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(abcd.data()), abcd.size(),
970 reinterpret_cast<const uint8_t*>(dcb.data()), dcb.size(),
971 patch_file_abcd_dcb.path, nullptr));
972 std::string patch_abcd_dcb;
973 CHECK(android::base::ReadFileToString(patch_file_abcd_dcb.path, &patch_abcd_dcb));
974
975 std::vector<std::string> transfer_list{
976 "4",
977 "10", // total blocks written
978 "2", // maximum stash entries
979 "2", // maximum number of stashed blocks
980
981 // a b c d e a b c d e
982 "stash " + b_hash + " " + loc("1"),
983 // a b c d e a b c d e [b(1)]
984 "stash " + c_hash + " " + loc("2"),
985 // a b c d e a b c d e [b(1)][c(2)]
986 "new " + loc("1-2"),
987 // a i h d e a b c d e [b(1)][c(2)]
988 "zero " + loc("0"),
989 // 0 i h d e a b c d e [b(1)][c(2)]
990
991 // bsdiff "b d c" (from stash, 3, stash) to get g(3)
992 android::base::StringPrintf(
993 "bsdiff 0 %zu %s %s %s 3 %s %s %s:%s %s:%s",
994 patch_bdc_g.size(), // patch start (0), patch length
995 bdc_hash.c_str(), // source hash
996 g_hash.c_str(), // target hash
997 loc("3").c_str(), // target range
998 loc("3").c_str(), loc("1").c_str(), // load "d" from block 3, into buffer at offset 1
999 b_hash.c_str(), loc("0").c_str(), // load "b" from stash, into buffer at offset 0
1000 c_hash.c_str(), loc("2").c_str()), // load "c" from stash, into buffer at offset 2
1001
1002 // 0 i h g e a b c d e [b(1)][c(2)]
1003 "free " + b_hash,
1004 // 0 i h g e a b c d e [c(2)]
1005 "free " + a_hash,
1006 // 0 i h g e a b c d e
1007 "stash " + a_hash + " " + loc("5"),
1008 // 0 i h g e a b c d e [a(5)]
1009 "move " + e_hash + " " + loc("5") + " 1 " + loc("4"),
1010 // 0 i h g e e b c d e [a(5)]
1011
1012 // bsdiff "a b c d" (from stash, 6-8) to "d c b" (6-8)
1013 android::base::StringPrintf( //
1014 "bsdiff %zu %zu %s %s %s 4 %s %s %s:%s",
1015 patch_bdc_g.size(), // patch start
1016 patch_bdc_g.size() + patch_abcd_dcb.size(), // patch length
1017 abcd_hash.c_str(), // source hash
1018 dcb_hash.c_str(), // target hash
1019 loc("6-8").c_str(), // target range
1020 loc("6-8").c_str(), // load "b c d" from blocks 6-8
1021 loc("1-3").c_str(), // into buffer at offset 1-3
1022 a_hash.c_str(), // load "a" from stash
1023 loc("0").c_str()), // into buffer at offset 0
1024
1025 // 0 i h g e e d c b e [a(5)]
1026 "new " + loc("4"),
1027 // 0 i h g f e d c b e [a(5)]
1028 "move " + a_hash + " " + loc("9") + " 1 - " + a_hash + ":" + loc("0"),
1029 // 0 i h g f e d c b a [a(5)]
1030 "free " + a_hash,
1031 // 0 i h g f e d c b a
1032 };
1033
1034 std::string new_data = i + h + f;
1035 std::string patch_data = patch_bdc_g + patch_abcd_dcb;
1036
1037 g_entries = {
1038 { "new_data", new_data },
1039 { "patch_data", patch_data },
1040 };
1041 g_source_image = a + b + c + d + e + a + b + c + d + e;
1042 g_target_image = zero + i + h + g + f + e + d + c + b + a;
1043
1044 return transfer_list;
1045}
1046
1047static const std::vector<std::string> g_transfer_list = GenerateTransferList();
1048
1049INSTANTIATE_TEST_CASE_P(InterruptAfterEachCommand, ResumableUpdaterTest,
1050 ::testing::Range(static_cast<size_t>(0),
1051 g_transfer_list.size() - kTransferListHeaderLines));
1052
1053TEST_P(ResumableUpdaterTest, InterruptVerifyResume) {
1054 ASSERT_TRUE(android::base::WriteStringToFile(g_source_image, image_file_));
1055
1056 LOG(INFO) << "Interrupting at line " << index_ << " ("
1057 << g_transfer_list[kTransferListHeaderLines + index_] << ")";
1058
1059 std::vector<std::string> transfer_list_copy{ g_transfer_list };
Tao Bao91a649a2018-05-21 16:05:56 -07001060 transfer_list_copy[kTransferListHeaderLines + index_] = "abort";
Tao Baoc0299ed2018-05-24 00:16:35 -07001061
1062 g_entries["transfer_list"] = android::base::Join(transfer_list_copy, '\n');
1063
1064 // Run update that's expected to fail.
1065 RunBlockImageUpdate(false, g_entries, image_file_, "");
1066
1067 std::string last_command_expected;
1068
1069 // Assert the last_command_file.
1070 if (index_ == 0) {
1071 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
1072 } else {
1073 last_command_expected =
1074 std::to_string(index_ - 1) + "\n" + g_transfer_list[kTransferListHeaderLines + index_ - 1];
1075 std::string last_command_actual;
1076 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
1077 ASSERT_EQ(last_command_expected, last_command_actual);
1078 }
1079
1080 g_entries["transfer_list"] = android::base::Join(g_transfer_list, '\n');
1081
1082 // Resume the interrupted update, by doing verification first.
1083 RunBlockImageUpdate(true, g_entries, image_file_, "t");
1084
1085 // last_command_file should remain intact.
1086 if (index_ == 0) {
1087 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
1088 } else {
1089 std::string last_command_actual;
1090 ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual));
1091 ASSERT_EQ(last_command_expected, last_command_actual);
1092 }
1093
1094 // Resume the update.
1095 RunBlockImageUpdate(false, g_entries, image_file_, "t");
1096
1097 // last_command_file should be gone after successful update.
1098 ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK));
1099
1100 std::string updated_image_actual;
1101 ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_image_actual));
1102 ASSERT_EQ(g_target_image, updated_image_actual);
1103}