Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 1 | /* |
| 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 Bao | 9aa7ab5 | 2017-01-05 17:27:19 -0800 | [diff] [blame] | 17 | #include <stdio.h> |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 18 | #include <stdlib.h> |
Tao Bao | 8992902 | 2016-11-08 20:51:31 -0800 | [diff] [blame] | 19 | #include <sys/stat.h> |
| 20 | #include <sys/types.h> |
| 21 | #include <unistd.h> |
| 22 | |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 23 | #include <algorithm> |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 24 | #include <memory> |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 25 | #include <string> |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 26 | #include <unordered_map> |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 27 | #include <vector> |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 28 | |
Tao Bao | 51d516e | 2016-11-03 14:49:01 -0700 | [diff] [blame] | 29 | #include <android-base/file.h> |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 30 | #include <android-base/logging.h> |
| 31 | #include <android-base/parseint.h> |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 32 | #include <android-base/properties.h> |
Tao Bao | 9aa7ab5 | 2017-01-05 17:27:19 -0800 | [diff] [blame] | 33 | #include <android-base/stringprintf.h> |
| 34 | #include <android-base/strings.h> |
Tao Bao | 51d516e | 2016-11-03 14:49:01 -0700 | [diff] [blame] | 35 | #include <android-base/test_utils.h> |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 36 | #include <bootloader_message/bootloader_message.h> |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 37 | #include <brotli/encode.h> |
Alex Deymo | fa18826 | 2017-10-10 17:56:17 +0200 | [diff] [blame] | 38 | #include <bsdiff/bsdiff.h> |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 39 | #include <gtest/gtest.h> |
Tianjie Xu | 69ffa15 | 2018-08-01 16:40:00 -0700 | [diff] [blame] | 40 | #include <verity/hash_tree_builder.h> |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 41 | #include <ziparchive/zip_archive.h> |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 42 | #include <ziparchive/zip_writer.h> |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 43 | |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 44 | #include "common/test_constants.h" |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 45 | #include "edify/expr.h" |
Tao Bao | 1fc5bf3 | 2017-10-06 07:43:41 -0700 | [diff] [blame] | 46 | #include "otautil/error_code.h" |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 47 | #include "otautil/paths.h" |
Tao Bao | 09e468f | 2017-09-29 14:39:33 -0700 | [diff] [blame] | 48 | #include "otautil/print_sha1.h" |
Tao Bao | 17054c0 | 2018-05-03 22:41:23 -0700 | [diff] [blame] | 49 | #include "otautil/sysutil.h" |
Tao Bao | 91a649a | 2018-05-21 16:05:56 -0700 | [diff] [blame] | 50 | #include "private/commands.h" |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 51 | #include "updater/blockimg.h" |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 52 | #include "updater/install.h" |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 53 | #include "updater/updater.h" |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 54 | |
Tao Bao | bafd6c7 | 2018-07-09 15:08:50 -0700 | [diff] [blame] | 55 | using namespace std::string_literals; |
| 56 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 57 | using PackageEntries = std::unordered_map<std::string, std::string>; |
| 58 | |
| 59 | static constexpr size_t kTransferListHeaderLines = 4; |
| 60 | |
| 61 | struct selabel_handle* sehandle = nullptr; |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 62 | |
Tao Bao | d8d514f | 2018-07-09 13:32:28 -0700 | [diff] [blame] | 63 | static void expect(const char* expected, const std::string& expr_str, CauseCode cause_code, |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 64 | UpdaterInfo* info = nullptr) { |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 65 | std::unique_ptr<Expr> e; |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 66 | int error_count = 0; |
Tao Bao | d8d514f | 2018-07-09 13:32:28 -0700 | [diff] [blame] | 67 | ASSERT_EQ(0, ParseString(expr_str, &e, &error_count)); |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 68 | ASSERT_EQ(0, error_count); |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 69 | |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 70 | State state(expr_str, info); |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 71 | |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 72 | std::string result; |
| 73 | bool status = Evaluate(&state, e, &result); |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 74 | |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 75 | if (expected == nullptr) { |
| 76 | ASSERT_FALSE(status); |
| 77 | } else { |
Tao Bao | 0b58e9a | 2018-07-06 15:01:05 -0700 | [diff] [blame] | 78 | ASSERT_TRUE(status) << "Evaluate() finished with error message: " << state.errmsg; |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 79 | ASSERT_STREQ(expected, result.c_str()); |
| 80 | } |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 81 | |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 82 | // Error code is set in updater/updater.cpp only, by parsing State.errmsg. |
| 83 | ASSERT_EQ(kNoError, state.error_code); |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 84 | |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 85 | // Cause code should always be available. |
| 86 | ASSERT_EQ(cause_code, state.cause_code); |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 89 | static void BuildUpdatePackage(const PackageEntries& entries, int fd) { |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 90 | FILE* zip_file_ptr = fdopen(fd, "wb"); |
| 91 | ZipWriter zip_writer(zip_file_ptr); |
| 92 | |
| 93 | for (const auto& entry : entries) { |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 94 | // All the entries are written as STORED. |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 95 | ASSERT_EQ(0, zip_writer.StartEntry(entry.first.c_str(), 0)); |
| 96 | if (!entry.second.empty()) { |
| 97 | ASSERT_EQ(0, zip_writer.WriteBytes(entry.second.data(), entry.second.size())); |
| 98 | } |
| 99 | ASSERT_EQ(0, zip_writer.FinishEntry()); |
| 100 | } |
| 101 | |
| 102 | ASSERT_EQ(0, zip_writer.Finish()); |
| 103 | ASSERT_EQ(0, fclose(zip_file_ptr)); |
| 104 | } |
| 105 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 106 | static void RunBlockImageUpdate(bool is_verify, const PackageEntries& entries, |
Tao Bao | ffede3e | 2018-06-07 09:56:19 -0700 | [diff] [blame] | 107 | const std::string& image_file, const std::string& result, |
| 108 | CauseCode cause_code = kNoCause) { |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 109 | CHECK(entries.find("transfer_list") != entries.end()); |
| 110 | |
| 111 | // Build the update package. |
| 112 | TemporaryFile zip_file; |
| 113 | BuildUpdatePackage(entries, zip_file.release()); |
| 114 | |
| 115 | MemMapping map; |
| 116 | ASSERT_TRUE(map.MapFile(zip_file.path)); |
| 117 | ZipArchiveHandle handle; |
| 118 | ASSERT_EQ(0, OpenArchiveFromMemory(map.addr, map.length, zip_file.path, &handle)); |
| 119 | |
| 120 | // Set up the handler, command_pipe, patch offset & length. |
| 121 | UpdaterInfo updater_info; |
| 122 | updater_info.package_zip = handle; |
| 123 | TemporaryFile temp_pipe; |
| 124 | updater_info.cmd_pipe = fdopen(temp_pipe.release(), "wbe"); |
| 125 | updater_info.package_zip_addr = map.addr; |
| 126 | updater_info.package_zip_len = map.length; |
| 127 | |
| 128 | std::string new_data = entries.find("new_data.br") != entries.end() ? "new_data.br" : "new_data"; |
| 129 | std::string script = is_verify ? "block_image_verify" : "block_image_update"; |
| 130 | script += R"((")" + image_file + R"(", package_extract_file("transfer_list"), ")" + new_data + |
| 131 | R"(", "patch_data"))"; |
Tao Bao | d8d514f | 2018-07-09 13:32:28 -0700 | [diff] [blame] | 132 | expect(result.c_str(), script, cause_code, &updater_info); |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 133 | |
| 134 | ASSERT_EQ(0, fclose(updater_info.cmd_pipe)); |
| 135 | CloseArchive(handle); |
| 136 | } |
| 137 | |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 138 | static std::string get_sha1(const std::string& content) { |
| 139 | uint8_t digest[SHA_DIGEST_LENGTH]; |
| 140 | SHA1(reinterpret_cast<const uint8_t*>(content.c_str()), content.size(), digest); |
| 141 | return print_sha1(digest); |
| 142 | } |
| 143 | |
Tao Bao | 0b58e9a | 2018-07-06 15:01:05 -0700 | [diff] [blame] | 144 | static Value* BlobToString(const char* name, State* state, |
| 145 | const std::vector<std::unique_ptr<Expr>>& argv) { |
| 146 | if (argv.size() != 1) { |
| 147 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size()); |
| 148 | } |
| 149 | |
| 150 | std::vector<std::unique_ptr<Value>> args; |
| 151 | if (!ReadValueArgs(state, argv, &args)) { |
| 152 | return nullptr; |
| 153 | } |
| 154 | |
Tao Bao | 511d759 | 2018-06-19 15:56:49 -0700 | [diff] [blame] | 155 | if (args[0]->type != Value::Type::BLOB) { |
Tao Bao | 0b58e9a | 2018-07-06 15:01:05 -0700 | [diff] [blame] | 156 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects a BLOB argument", name); |
| 157 | } |
| 158 | |
Tao Bao | 511d759 | 2018-06-19 15:56:49 -0700 | [diff] [blame] | 159 | args[0]->type = Value::Type::STRING; |
Tao Bao | 0b58e9a | 2018-07-06 15:01:05 -0700 | [diff] [blame] | 160 | return args[0].release(); |
| 161 | } |
| 162 | |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 163 | class UpdaterTest : public ::testing::Test { |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 164 | protected: |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 165 | void SetUp() override { |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 166 | RegisterBuiltins(); |
| 167 | RegisterInstallFunctions(); |
| 168 | RegisterBlockImageFunctions(); |
Tianjie Xu | 3bbb20f | 2018-02-27 15:56:11 -0800 | [diff] [blame] | 169 | |
Tao Bao | 0b58e9a | 2018-07-06 15:01:05 -0700 | [diff] [blame] | 170 | RegisterFunction("blob_to_string", BlobToString); |
| 171 | |
Tao Bao | 7064aa2 | 2018-05-24 21:43:50 -0700 | [diff] [blame] | 172 | // Each test is run in a separate process (isolated mode). Shared temporary files won't cause |
| 173 | // conflicts. |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 174 | Paths::Get().set_cache_temp_source(temp_saved_source_.path); |
Tao Bao | 7064aa2 | 2018-05-24 21:43:50 -0700 | [diff] [blame] | 175 | Paths::Get().set_last_command_file(temp_last_command_.path); |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 176 | Paths::Get().set_stash_directory_base(temp_stash_base_.path); |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 177 | |
Tao Bao | 91a649a | 2018-05-21 16:05:56 -0700 | [diff] [blame] | 178 | // Enable a special command "abort" to simulate interruption. |
| 179 | Command::abort_allowed_ = true; |
| 180 | |
Tao Bao | 7064aa2 | 2018-05-24 21:43:50 -0700 | [diff] [blame] | 181 | last_command_file_ = temp_last_command_.path; |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 182 | image_file_ = image_temp_file_.path; |
| 183 | } |
| 184 | |
| 185 | void TearDown() override { |
Tao Bao | 7064aa2 | 2018-05-24 21:43:50 -0700 | [diff] [blame] | 186 | // Clean up the last_command_file if any. |
| 187 | ASSERT_TRUE(android::base::RemoveFileIfExists(last_command_file_)); |
| 188 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 189 | // Clear partition updated marker if any. |
| 190 | std::string updated_marker{ temp_stash_base_.path }; |
| 191 | updated_marker += "/" + get_sha1(image_temp_file_.path) + ".UPDATED"; |
| 192 | ASSERT_TRUE(android::base::RemoveFileIfExists(updated_marker)); |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 193 | } |
Tianjie Xu | 3bbb20f | 2018-02-27 15:56:11 -0800 | [diff] [blame] | 194 | |
| 195 | TemporaryFile temp_saved_source_; |
Tianjie Xu | 3bbb20f | 2018-02-27 15:56:11 -0800 | [diff] [blame] | 196 | TemporaryDir temp_stash_base_; |
Tao Bao | 7064aa2 | 2018-05-24 21:43:50 -0700 | [diff] [blame] | 197 | std::string last_command_file_; |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 198 | std::string image_file_; |
| 199 | |
| 200 | private: |
Tao Bao | 7064aa2 | 2018-05-24 21:43:50 -0700 | [diff] [blame] | 201 | TemporaryFile temp_last_command_; |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 202 | TemporaryFile image_temp_file_; |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 203 | }; |
| 204 | |
| 205 | TEST_F(UpdaterTest, getprop) { |
| 206 | expect(android::base::GetProperty("ro.product.device", "").c_str(), |
| 207 | "getprop(\"ro.product.device\")", |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 208 | kNoCause); |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 209 | |
| 210 | expect(android::base::GetProperty("ro.build.fingerprint", "").c_str(), |
| 211 | "getprop(\"ro.build.fingerprint\")", |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 212 | kNoCause); |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 213 | |
| 214 | // getprop() accepts only one parameter. |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 215 | expect(nullptr, "getprop()", kArgsParsingFailure); |
| 216 | expect(nullptr, "getprop(\"arg1\", \"arg2\")", kArgsParsingFailure); |
| 217 | } |
| 218 | |
Tao Bao | db56eb0 | 2017-03-23 06:34:20 -0700 | [diff] [blame] | 219 | TEST_F(UpdaterTest, apply_patch_check) { |
| 220 | // Zero-argument is not valid. |
| 221 | expect(nullptr, "apply_patch_check()", kArgsParsingFailure); |
| 222 | |
| 223 | // File not found. |
| 224 | expect("", "apply_patch_check(\"/doesntexist\")", kNoCause); |
| 225 | |
| 226 | std::string src_file = from_testdata_base("old.file"); |
| 227 | std::string src_content; |
| 228 | ASSERT_TRUE(android::base::ReadFileToString(src_file, &src_content)); |
| 229 | size_t src_size = src_content.size(); |
| 230 | std::string src_hash = get_sha1(src_content); |
| 231 | |
| 232 | // One-argument with EMMC:file:size:sha1 should pass the check. |
| 233 | std::string filename = android::base::Join( |
| 234 | std::vector<std::string>{ "EMMC", src_file, std::to_string(src_size), src_hash }, ":"); |
| 235 | std::string cmd = "apply_patch_check(\"" + filename + "\")"; |
Tao Bao | d8d514f | 2018-07-09 13:32:28 -0700 | [diff] [blame] | 236 | expect("t", cmd, kNoCause); |
Tao Bao | db56eb0 | 2017-03-23 06:34:20 -0700 | [diff] [blame] | 237 | |
| 238 | // EMMC:file:(size-1):sha1:(size+1):sha1 should fail the check. |
| 239 | std::string filename_bad = android::base::Join( |
| 240 | std::vector<std::string>{ "EMMC", src_file, std::to_string(src_size - 1), src_hash, |
| 241 | std::to_string(src_size + 1), src_hash }, |
| 242 | ":"); |
| 243 | cmd = "apply_patch_check(\"" + filename_bad + "\")"; |
Tao Bao | d8d514f | 2018-07-09 13:32:28 -0700 | [diff] [blame] | 244 | expect("", cmd, kNoCause); |
Tao Bao | db56eb0 | 2017-03-23 06:34:20 -0700 | [diff] [blame] | 245 | |
| 246 | // EMMC:file:(size-1):sha1:size:sha1:(size+1):sha1 should pass the check. |
| 247 | filename_bad = |
| 248 | android::base::Join(std::vector<std::string>{ "EMMC", src_file, std::to_string(src_size - 1), |
| 249 | src_hash, std::to_string(src_size), src_hash, |
| 250 | std::to_string(src_size + 1), src_hash }, |
| 251 | ":"); |
| 252 | cmd = "apply_patch_check(\"" + filename_bad + "\")"; |
Tao Bao | d8d514f | 2018-07-09 13:32:28 -0700 | [diff] [blame] | 253 | expect("t", cmd, kNoCause); |
Tao Bao | db56eb0 | 2017-03-23 06:34:20 -0700 | [diff] [blame] | 254 | |
| 255 | // Multiple arguments. |
Tao Bao | 7c1d426 | 2018-07-06 23:18:14 -0700 | [diff] [blame] | 256 | // As long as it successfully loads the partition specified in filename, it won't check against |
| 257 | // any given SHAs. |
Tao Bao | db56eb0 | 2017-03-23 06:34:20 -0700 | [diff] [blame] | 258 | cmd = "apply_patch_check(\"" + filename + "\", \"wrong_sha1\", \"wrong_sha2\")"; |
Tao Bao | d8d514f | 2018-07-09 13:32:28 -0700 | [diff] [blame] | 259 | expect("t", cmd, kNoCause); |
Tao Bao | db56eb0 | 2017-03-23 06:34:20 -0700 | [diff] [blame] | 260 | |
| 261 | cmd = "apply_patch_check(\"" + filename + "\", \"wrong_sha1\", \"" + src_hash + |
| 262 | "\", \"wrong_sha2\")"; |
Tao Bao | d8d514f | 2018-07-09 13:32:28 -0700 | [diff] [blame] | 263 | expect("t", cmd, kNoCause); |
Tao Bao | db56eb0 | 2017-03-23 06:34:20 -0700 | [diff] [blame] | 264 | |
| 265 | cmd = "apply_patch_check(\"" + filename_bad + "\", \"wrong_sha1\", \"" + src_hash + |
| 266 | "\", \"wrong_sha2\")"; |
Tao Bao | d8d514f | 2018-07-09 13:32:28 -0700 | [diff] [blame] | 267 | expect("t", cmd, kNoCause); |
Tao Bao | db56eb0 | 2017-03-23 06:34:20 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Tao Bao | 51d516e | 2016-11-03 14:49:01 -0700 | [diff] [blame] | 270 | TEST_F(UpdaterTest, file_getprop) { |
| 271 | // file_getprop() expects two arguments. |
| 272 | expect(nullptr, "file_getprop()", kArgsParsingFailure); |
| 273 | expect(nullptr, "file_getprop(\"arg1\")", kArgsParsingFailure); |
| 274 | expect(nullptr, "file_getprop(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure); |
| 275 | |
| 276 | // File doesn't exist. |
| 277 | expect(nullptr, "file_getprop(\"/doesntexist\", \"key1\")", kFileGetPropFailure); |
| 278 | |
| 279 | // Reject too large files (current limit = 65536). |
| 280 | TemporaryFile temp_file1; |
| 281 | std::string buffer(65540, '\0'); |
| 282 | ASSERT_TRUE(android::base::WriteStringToFile(buffer, temp_file1.path)); |
| 283 | |
| 284 | // Read some keys. |
| 285 | TemporaryFile temp_file2; |
| 286 | std::string content("ro.product.name=tardis\n" |
| 287 | "# comment\n\n\n" |
| 288 | "ro.product.model\n" |
| 289 | "ro.product.board = magic \n"); |
| 290 | ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file2.path)); |
| 291 | |
| 292 | std::string script1("file_getprop(\"" + std::string(temp_file2.path) + |
| 293 | "\", \"ro.product.name\")"); |
Tao Bao | d8d514f | 2018-07-09 13:32:28 -0700 | [diff] [blame] | 294 | expect("tardis", script1, kNoCause); |
Tao Bao | 51d516e | 2016-11-03 14:49:01 -0700 | [diff] [blame] | 295 | |
| 296 | std::string script2("file_getprop(\"" + std::string(temp_file2.path) + |
| 297 | "\", \"ro.product.board\")"); |
Tao Bao | d8d514f | 2018-07-09 13:32:28 -0700 | [diff] [blame] | 298 | expect("magic", script2, kNoCause); |
Tao Bao | 51d516e | 2016-11-03 14:49:01 -0700 | [diff] [blame] | 299 | |
| 300 | // No match. |
| 301 | std::string script3("file_getprop(\"" + std::string(temp_file2.path) + |
| 302 | "\", \"ro.product.wrong\")"); |
Tao Bao | d8d514f | 2018-07-09 13:32:28 -0700 | [diff] [blame] | 303 | expect("", script3, kNoCause); |
Tao Bao | 51d516e | 2016-11-03 14:49:01 -0700 | [diff] [blame] | 304 | |
| 305 | std::string script4("file_getprop(\"" + std::string(temp_file2.path) + |
| 306 | "\", \"ro.product.name=\")"); |
Tao Bao | d8d514f | 2018-07-09 13:32:28 -0700 | [diff] [blame] | 307 | expect("", script4, kNoCause); |
Tao Bao | 51d516e | 2016-11-03 14:49:01 -0700 | [diff] [blame] | 308 | |
| 309 | std::string script5("file_getprop(\"" + std::string(temp_file2.path) + |
| 310 | "\", \"ro.product.nam\")"); |
Tao Bao | d8d514f | 2018-07-09 13:32:28 -0700 | [diff] [blame] | 311 | expect("", script5, kNoCause); |
Tao Bao | 51d516e | 2016-11-03 14:49:01 -0700 | [diff] [blame] | 312 | |
| 313 | std::string script6("file_getprop(\"" + std::string(temp_file2.path) + |
| 314 | "\", \"ro.product.model\")"); |
Tao Bao | d8d514f | 2018-07-09 13:32:28 -0700 | [diff] [blame] | 315 | expect("", script6, kNoCause); |
Tao Bao | 51d516e | 2016-11-03 14:49:01 -0700 | [diff] [blame] | 316 | } |
Tao Bao | 0831d0b | 2016-11-03 23:25:04 -0700 | [diff] [blame] | 317 | |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 318 | // TODO: Test extracting to block device. |
| 319 | TEST_F(UpdaterTest, package_extract_file) { |
| 320 | // package_extract_file expects 1 or 2 arguments. |
| 321 | expect(nullptr, "package_extract_file()", kArgsParsingFailure); |
| 322 | expect(nullptr, "package_extract_file(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure); |
| 323 | |
| 324 | std::string zip_path = from_testdata_base("ziptest_valid.zip"); |
| 325 | ZipArchiveHandle handle; |
| 326 | ASSERT_EQ(0, OpenArchive(zip_path.c_str(), &handle)); |
| 327 | |
| 328 | // Need to set up the ziphandle. |
| 329 | UpdaterInfo updater_info; |
| 330 | updater_info.package_zip = handle; |
| 331 | |
| 332 | // Two-argument version. |
| 333 | TemporaryFile temp_file1; |
| 334 | std::string script("package_extract_file(\"a.txt\", \"" + std::string(temp_file1.path) + "\")"); |
Tao Bao | d8d514f | 2018-07-09 13:32:28 -0700 | [diff] [blame] | 335 | expect("t", script, kNoCause, &updater_info); |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 336 | |
| 337 | // Verify the extracted entry. |
| 338 | std::string data; |
| 339 | ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data)); |
| 340 | ASSERT_EQ(kATxtContents, data); |
| 341 | |
| 342 | // Now extract another entry to the same location, which should overwrite. |
| 343 | script = "package_extract_file(\"b.txt\", \"" + std::string(temp_file1.path) + "\")"; |
Tao Bao | d8d514f | 2018-07-09 13:32:28 -0700 | [diff] [blame] | 344 | expect("t", script, kNoCause, &updater_info); |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 345 | |
| 346 | ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data)); |
| 347 | ASSERT_EQ(kBTxtContents, data); |
| 348 | |
| 349 | // Missing zip entry. The two-argument version doesn't abort. |
| 350 | script = "package_extract_file(\"doesntexist\", \"" + std::string(temp_file1.path) + "\")"; |
Tao Bao | d8d514f | 2018-07-09 13:32:28 -0700 | [diff] [blame] | 351 | expect("", script, kNoCause, &updater_info); |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 352 | |
| 353 | // Extract to /dev/full should fail. |
| 354 | script = "package_extract_file(\"a.txt\", \"/dev/full\")"; |
Tao Bao | d8d514f | 2018-07-09 13:32:28 -0700 | [diff] [blame] | 355 | expect("", script, kNoCause, &updater_info); |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 356 | |
Tao Bao | 0b58e9a | 2018-07-06 15:01:05 -0700 | [diff] [blame] | 357 | // One-argument version. package_extract_file() gives a VAL_BLOB, which needs to be converted to |
| 358 | // VAL_STRING for equality test. |
| 359 | script = "blob_to_string(package_extract_file(\"a.txt\")) == \"" + kATxtContents + "\""; |
Tao Bao | d8d514f | 2018-07-09 13:32:28 -0700 | [diff] [blame] | 360 | expect("t", script, kNoCause, &updater_info); |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 361 | |
Tao Bao | 0b58e9a | 2018-07-06 15:01:05 -0700 | [diff] [blame] | 362 | script = "blob_to_string(package_extract_file(\"b.txt\")) == \"" + kBTxtContents + "\""; |
Tao Bao | d8d514f | 2018-07-09 13:32:28 -0700 | [diff] [blame] | 363 | expect("t", script, kNoCause, &updater_info); |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 364 | |
| 365 | // Missing entry. The one-argument version aborts the evaluation. |
| 366 | script = "package_extract_file(\"doesntexist\")"; |
Tao Bao | d8d514f | 2018-07-09 13:32:28 -0700 | [diff] [blame] | 367 | expect(nullptr, script, kPackageExtractFileFailure, &updater_info); |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 368 | |
| 369 | CloseArchive(handle); |
| 370 | } |
Tao Bao | d0f3088 | 2016-11-03 23:52:01 -0700 | [diff] [blame] | 371 | |
Tao Bao | bafd6c7 | 2018-07-09 15:08:50 -0700 | [diff] [blame] | 372 | TEST_F(UpdaterTest, read_file) { |
| 373 | // read_file() expects one argument. |
| 374 | expect(nullptr, "read_file()", kArgsParsingFailure); |
| 375 | expect(nullptr, "read_file(\"arg1\", \"arg2\")", kArgsParsingFailure); |
| 376 | |
| 377 | // Write some value to file and read back. |
| 378 | TemporaryFile temp_file; |
| 379 | std::string script("write_value(\"foo\", \""s + temp_file.path + "\");"); |
| 380 | expect("t", script, kNoCause); |
| 381 | |
| 382 | script = "read_file(\""s + temp_file.path + "\") == \"foo\""; |
| 383 | expect("t", script, kNoCause); |
| 384 | |
| 385 | script = "read_file(\""s + temp_file.path + "\") == \"bar\""; |
| 386 | expect("", script, kNoCause); |
| 387 | |
| 388 | // It should fail gracefully when read fails. |
| 389 | script = "read_file(\"/doesntexist\")"; |
| 390 | expect("", script, kNoCause); |
| 391 | } |
| 392 | |
Tianjie Xu | 69ffa15 | 2018-08-01 16:40:00 -0700 | [diff] [blame] | 393 | TEST_F(UpdaterTest, compute_hash_tree_smoke) { |
| 394 | std::string data; |
| 395 | for (unsigned char i = 0; i < 128; i++) { |
| 396 | data += std::string(4096, i); |
| 397 | } |
| 398 | // Appends an additional block for verity data. |
| 399 | data += std::string(4096, 0); |
| 400 | ASSERT_EQ(129 * 4096, data.size()); |
| 401 | ASSERT_TRUE(android::base::WriteStringToFile(data, image_file_)); |
| 402 | |
| 403 | std::string salt = "aee087a5be3b982978c923f566a94613496b417f2af592639bc80d141e34dfe7"; |
| 404 | std::string expected_root_hash = |
| 405 | "7e0a8d8747f54384014ab996f5b2dc4eb7ff00c630eede7134c9e3f05c0dd8ca"; |
| 406 | // hash_tree_ranges, source_ranges, hash_algorithm, salt_hex, root_hash |
| 407 | std::vector<std::string> tokens{ "compute_hash_tree", "2,128,129", "2,0,128", "sha256", salt, |
| 408 | expected_root_hash }; |
| 409 | std::string hash_tree_command = android::base::Join(tokens, " "); |
| 410 | |
| 411 | std::vector<std::string> transfer_list{ |
| 412 | "4", "2", "0", "2", hash_tree_command, |
| 413 | }; |
| 414 | |
| 415 | PackageEntries entries{ |
| 416 | { "new_data", "" }, |
| 417 | { "patch_data", "" }, |
| 418 | { "transfer_list", android::base::Join(transfer_list, "\n") }, |
| 419 | }; |
| 420 | |
| 421 | RunBlockImageUpdate(false, entries, image_file_, "t"); |
| 422 | |
| 423 | std::string updated; |
| 424 | ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated)); |
| 425 | ASSERT_EQ(129 * 4096, updated.size()); |
| 426 | ASSERT_EQ(data.substr(0, 128 * 4096), updated.substr(0, 128 * 4096)); |
| 427 | |
| 428 | // Computes the SHA256 of the salt + hash_tree_data and expects the result to match with the |
| 429 | // root_hash. |
| 430 | std::vector<unsigned char> salt_bytes; |
| 431 | ASSERT_TRUE(HashTreeBuilder::ParseBytesArrayFromString(salt, &salt_bytes)); |
| 432 | std::vector<unsigned char> hash_tree = std::move(salt_bytes); |
| 433 | hash_tree.insert(hash_tree.end(), updated.begin() + 128 * 4096, updated.end()); |
| 434 | |
| 435 | std::vector<unsigned char> digest(SHA256_DIGEST_LENGTH); |
| 436 | SHA256(hash_tree.data(), hash_tree.size(), digest.data()); |
| 437 | ASSERT_EQ(expected_root_hash, HashTreeBuilder::BytesArrayToString(digest)); |
| 438 | } |
| 439 | |
| 440 | TEST_F(UpdaterTest, compute_hash_tree_root_mismatch) { |
| 441 | std::string data; |
| 442 | for (size_t i = 0; i < 128; i++) { |
| 443 | data += std::string(4096, i); |
| 444 | } |
| 445 | // Appends an additional block for verity data. |
| 446 | data += std::string(4096, 0); |
| 447 | ASSERT_EQ(129 * 4096, data.size()); |
| 448 | // Corrupts one bit |
| 449 | data[4096] = 'A'; |
| 450 | ASSERT_TRUE(android::base::WriteStringToFile(data, image_file_)); |
| 451 | |
| 452 | std::string salt = "aee087a5be3b982978c923f566a94613496b417f2af592639bc80d141e34dfe7"; |
| 453 | std::string expected_root_hash = |
| 454 | "7e0a8d8747f54384014ab996f5b2dc4eb7ff00c630eede7134c9e3f05c0dd8ca"; |
| 455 | // hash_tree_ranges, source_ranges, hash_algorithm, salt_hex, root_hash |
| 456 | std::vector<std::string> tokens{ "compute_hash_tree", "2,128,129", "2,0,128", "sha256", salt, |
| 457 | expected_root_hash }; |
| 458 | std::string hash_tree_command = android::base::Join(tokens, " "); |
| 459 | |
| 460 | std::vector<std::string> transfer_list{ |
| 461 | "4", "2", "0", "2", hash_tree_command, |
| 462 | }; |
| 463 | |
| 464 | PackageEntries entries{ |
| 465 | { "new_data", "" }, |
| 466 | { "patch_data", "" }, |
| 467 | { "transfer_list", android::base::Join(transfer_list, "\n") }, |
| 468 | }; |
| 469 | |
| 470 | RunBlockImageUpdate(false, entries, image_file_, "", kHashTreeComputationFailure); |
| 471 | } |
| 472 | |
Tao Bao | d0f3088 | 2016-11-03 23:52:01 -0700 | [diff] [blame] | 473 | TEST_F(UpdaterTest, write_value) { |
| 474 | // write_value() expects two arguments. |
| 475 | expect(nullptr, "write_value()", kArgsParsingFailure); |
| 476 | expect(nullptr, "write_value(\"arg1\")", kArgsParsingFailure); |
| 477 | expect(nullptr, "write_value(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure); |
| 478 | |
| 479 | // filename cannot be empty. |
| 480 | expect(nullptr, "write_value(\"value\", \"\")", kArgsParsingFailure); |
| 481 | |
| 482 | // Write some value to file. |
| 483 | TemporaryFile temp_file; |
| 484 | std::string value = "magicvalue"; |
| 485 | std::string script("write_value(\"" + value + "\", \"" + std::string(temp_file.path) + "\")"); |
Tao Bao | d8d514f | 2018-07-09 13:32:28 -0700 | [diff] [blame] | 486 | expect("t", script, kNoCause); |
Tao Bao | d0f3088 | 2016-11-03 23:52:01 -0700 | [diff] [blame] | 487 | |
| 488 | // Verify the content. |
| 489 | std::string content; |
| 490 | ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content)); |
| 491 | ASSERT_EQ(value, content); |
| 492 | |
| 493 | // Allow writing empty string. |
| 494 | script = "write_value(\"\", \"" + std::string(temp_file.path) + "\")"; |
Tao Bao | d8d514f | 2018-07-09 13:32:28 -0700 | [diff] [blame] | 495 | expect("t", script, kNoCause); |
Tao Bao | d0f3088 | 2016-11-03 23:52:01 -0700 | [diff] [blame] | 496 | |
| 497 | // Verify the content. |
| 498 | ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content)); |
| 499 | ASSERT_EQ("", content); |
| 500 | |
| 501 | // It should fail gracefully when write fails. |
| 502 | script = "write_value(\"value\", \"/proc/0/file1\")"; |
Tao Bao | d8d514f | 2018-07-09 13:32:28 -0700 | [diff] [blame] | 503 | expect("", script, kNoCause); |
Tao Bao | d0f3088 | 2016-11-03 23:52:01 -0700 | [diff] [blame] | 504 | } |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 505 | |
| 506 | TEST_F(UpdaterTest, get_stage) { |
| 507 | // get_stage() expects one argument. |
| 508 | expect(nullptr, "get_stage()", kArgsParsingFailure); |
| 509 | expect(nullptr, "get_stage(\"arg1\", \"arg2\")", kArgsParsingFailure); |
| 510 | expect(nullptr, "get_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure); |
| 511 | |
| 512 | // Set up a local file as BCB. |
| 513 | TemporaryFile tf; |
| 514 | std::string temp_file(tf.path); |
| 515 | bootloader_message boot; |
| 516 | strlcpy(boot.stage, "2/3", sizeof(boot.stage)); |
| 517 | std::string err; |
| 518 | ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err)); |
| 519 | |
| 520 | // Can read the stage value. |
| 521 | std::string script("get_stage(\"" + temp_file + "\")"); |
Tao Bao | d8d514f | 2018-07-09 13:32:28 -0700 | [diff] [blame] | 522 | expect("2/3", script, kNoCause); |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 523 | |
| 524 | // Bad BCB path. |
| 525 | script = "get_stage(\"doesntexist\")"; |
Tao Bao | d8d514f | 2018-07-09 13:32:28 -0700 | [diff] [blame] | 526 | expect("", script, kNoCause); |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | TEST_F(UpdaterTest, set_stage) { |
| 530 | // set_stage() expects two arguments. |
| 531 | expect(nullptr, "set_stage()", kArgsParsingFailure); |
| 532 | expect(nullptr, "set_stage(\"arg1\")", kArgsParsingFailure); |
| 533 | expect(nullptr, "set_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure); |
| 534 | |
| 535 | // Set up a local file as BCB. |
| 536 | TemporaryFile tf; |
| 537 | std::string temp_file(tf.path); |
| 538 | bootloader_message boot; |
| 539 | strlcpy(boot.command, "command", sizeof(boot.command)); |
| 540 | strlcpy(boot.stage, "2/3", sizeof(boot.stage)); |
| 541 | std::string err; |
| 542 | ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err)); |
| 543 | |
| 544 | // Write with set_stage(). |
| 545 | std::string script("set_stage(\"" + temp_file + "\", \"1/3\")"); |
Tao Bao | d8d514f | 2018-07-09 13:32:28 -0700 | [diff] [blame] | 546 | expect(tf.path, script, kNoCause); |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 547 | |
| 548 | // Verify. |
| 549 | bootloader_message boot_verify; |
| 550 | ASSERT_TRUE(read_bootloader_message_from(&boot_verify, temp_file, &err)); |
| 551 | |
| 552 | // Stage should be updated, with command part untouched. |
| 553 | ASSERT_STREQ("1/3", boot_verify.stage); |
| 554 | ASSERT_STREQ(boot.command, boot_verify.command); |
| 555 | |
| 556 | // Bad BCB path. |
| 557 | script = "set_stage(\"doesntexist\", \"1/3\")"; |
Tao Bao | d8d514f | 2018-07-09 13:32:28 -0700 | [diff] [blame] | 558 | expect("", script, kNoCause); |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 559 | |
| 560 | script = "set_stage(\"/dev/full\", \"1/3\")"; |
Tao Bao | d8d514f | 2018-07-09 13:32:28 -0700 | [diff] [blame] | 561 | expect("", script, kNoCause); |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 562 | } |
Tao Bao | 9aa7ab5 | 2017-01-05 17:27:19 -0800 | [diff] [blame] | 563 | |
| 564 | TEST_F(UpdaterTest, set_progress) { |
| 565 | // set_progress() expects one argument. |
| 566 | expect(nullptr, "set_progress()", kArgsParsingFailure); |
| 567 | expect(nullptr, "set_progress(\"arg1\", \"arg2\")", kArgsParsingFailure); |
| 568 | |
| 569 | // Invalid progress argument. |
| 570 | expect(nullptr, "set_progress(\"arg1\")", kArgsParsingFailure); |
| 571 | expect(nullptr, "set_progress(\"3x+5\")", kArgsParsingFailure); |
| 572 | expect(nullptr, "set_progress(\".3.5\")", kArgsParsingFailure); |
| 573 | |
| 574 | TemporaryFile tf; |
| 575 | UpdaterInfo updater_info; |
Tianjie Xu | 79327ac | 2017-09-08 17:09:10 -0700 | [diff] [blame] | 576 | updater_info.cmd_pipe = fdopen(tf.release(), "w"); |
Tao Bao | 9aa7ab5 | 2017-01-05 17:27:19 -0800 | [diff] [blame] | 577 | expect(".52", "set_progress(\".52\")", kNoCause, &updater_info); |
| 578 | fflush(updater_info.cmd_pipe); |
| 579 | |
| 580 | std::string cmd; |
| 581 | ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd)); |
| 582 | ASSERT_EQ(android::base::StringPrintf("set_progress %f\n", .52), cmd); |
| 583 | // recovery-updater protocol expects 2 tokens ("set_progress <frac>"). |
| 584 | ASSERT_EQ(2U, android::base::Split(cmd, " ").size()); |
Tianjie Xu | 79327ac | 2017-09-08 17:09:10 -0700 | [diff] [blame] | 585 | ASSERT_EQ(0, fclose(updater_info.cmd_pipe)); |
Tao Bao | 9aa7ab5 | 2017-01-05 17:27:19 -0800 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | TEST_F(UpdaterTest, show_progress) { |
| 589 | // show_progress() expects two arguments. |
| 590 | expect(nullptr, "show_progress()", kArgsParsingFailure); |
| 591 | expect(nullptr, "show_progress(\"arg1\")", kArgsParsingFailure); |
| 592 | expect(nullptr, "show_progress(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure); |
| 593 | |
| 594 | // Invalid progress arguments. |
| 595 | expect(nullptr, "show_progress(\"arg1\", \"arg2\")", kArgsParsingFailure); |
| 596 | expect(nullptr, "show_progress(\"3x+5\", \"10\")", kArgsParsingFailure); |
| 597 | expect(nullptr, "show_progress(\".3\", \"5a\")", kArgsParsingFailure); |
| 598 | |
| 599 | TemporaryFile tf; |
| 600 | UpdaterInfo updater_info; |
Tianjie Xu | 79327ac | 2017-09-08 17:09:10 -0700 | [diff] [blame] | 601 | updater_info.cmd_pipe = fdopen(tf.release(), "w"); |
Tao Bao | 9aa7ab5 | 2017-01-05 17:27:19 -0800 | [diff] [blame] | 602 | expect(".52", "show_progress(\".52\", \"10\")", kNoCause, &updater_info); |
| 603 | fflush(updater_info.cmd_pipe); |
| 604 | |
| 605 | std::string cmd; |
| 606 | ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd)); |
| 607 | ASSERT_EQ(android::base::StringPrintf("progress %f %d\n", .52, 10), cmd); |
| 608 | // recovery-updater protocol expects 3 tokens ("progress <frac> <secs>"). |
| 609 | ASSERT_EQ(3U, android::base::Split(cmd, " ").size()); |
Tianjie Xu | 79327ac | 2017-09-08 17:09:10 -0700 | [diff] [blame] | 610 | ASSERT_EQ(0, fclose(updater_info.cmd_pipe)); |
Tao Bao | 9aa7ab5 | 2017-01-05 17:27:19 -0800 | [diff] [blame] | 611 | } |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 612 | |
Tao Bao | ffede3e | 2018-06-07 09:56:19 -0700 | [diff] [blame] | 613 | TEST_F(UpdaterTest, block_image_update_parsing_error) { |
| 614 | std::vector<std::string> transfer_list{ |
| 615 | // clang-format off |
| 616 | "4", |
| 617 | "2", |
| 618 | "0", |
| 619 | // clang-format on |
| 620 | }; |
| 621 | |
| 622 | PackageEntries entries{ |
| 623 | { "new_data", "" }, |
| 624 | { "patch_data", "" }, |
| 625 | { "transfer_list", android::base::Join(transfer_list, '\n') }, |
| 626 | }; |
| 627 | |
| 628 | RunBlockImageUpdate(false, entries, image_file_, "", kArgsParsingFailure); |
| 629 | } |
| 630 | |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 631 | TEST_F(UpdaterTest, block_image_update_patch_data) { |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 632 | std::string src_content = std::string(4096, 'a') + std::string(4096, 'c'); |
| 633 | std::string tgt_content = std::string(4096, 'b') + std::string(4096, 'd'); |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 634 | |
| 635 | // Generate the patch data. |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 636 | TemporaryFile patch_file; |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 637 | ASSERT_EQ(0, |
| 638 | bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(src_content.data()), src_content.size(), |
| 639 | reinterpret_cast<const uint8_t*>(tgt_content.data()), tgt_content.size(), |
| 640 | patch_file.path, nullptr)); |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 641 | std::string patch_content; |
| 642 | ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch_content)); |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 643 | |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 644 | // Create the transfer list that contains a bsdiff. |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 645 | std::string src_hash = get_sha1(src_content); |
| 646 | std::string tgt_hash = get_sha1(tgt_content); |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 647 | std::vector<std::string> transfer_list{ |
| 648 | // clang-format off |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 649 | "4", |
| 650 | "2", |
| 651 | "0", |
| 652 | "2", |
| 653 | "stash " + src_hash + " 2,0,2", |
| 654 | android::base::StringPrintf("bsdiff 0 %zu %s %s 2,0,2 2 - %s:2,0,2", patch_content.size(), |
| 655 | src_hash.c_str(), tgt_hash.c_str(), src_hash.c_str()), |
| 656 | "free " + src_hash, |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 657 | // clang-format on |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 658 | }; |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 659 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 660 | PackageEntries entries{ |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 661 | { "new_data", "" }, |
| 662 | { "patch_data", patch_content }, |
| 663 | { "transfer_list", android::base::Join(transfer_list, '\n') }, |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 664 | }; |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 665 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 666 | ASSERT_TRUE(android::base::WriteStringToFile(src_content, image_file_)); |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 667 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 668 | RunBlockImageUpdate(false, entries, image_file_, "t"); |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 669 | |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 670 | // The update_file should be patched correctly. |
| 671 | std::string updated_content; |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 672 | ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_content)); |
| 673 | ASSERT_EQ(tgt_content, updated_content); |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | TEST_F(UpdaterTest, block_image_update_fail) { |
| 677 | std::string src_content(4096 * 2, 'e'); |
| 678 | std::string src_hash = get_sha1(src_content); |
| 679 | // Stash and free some blocks, then fail the update intentionally. |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 680 | std::vector<std::string> transfer_list{ |
| 681 | // clang-format off |
| 682 | "4", |
| 683 | "2", |
| 684 | "0", |
| 685 | "2", |
| 686 | "stash " + src_hash + " 2,0,2", |
| 687 | "free " + src_hash, |
Tao Bao | 91a649a | 2018-05-21 16:05:56 -0700 | [diff] [blame] | 688 | "abort", |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 689 | // clang-format on |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 690 | }; |
| 691 | |
| 692 | // Add a new data of 10 bytes to test the deadlock. |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 693 | PackageEntries entries{ |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 694 | { "new_data", std::string(10, 0) }, |
| 695 | { "patch_data", "" }, |
| 696 | { "transfer_list", android::base::Join(transfer_list, '\n') }, |
| 697 | }; |
| 698 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 699 | ASSERT_TRUE(android::base::WriteStringToFile(src_content, image_file_)); |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 700 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 701 | RunBlockImageUpdate(false, entries, image_file_, ""); |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 702 | |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 703 | // Updater generates the stash name based on the input file name. |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 704 | std::string name_digest = get_sha1(image_file_); |
Tianjie Xu | 3bbb20f | 2018-02-27 15:56:11 -0800 | [diff] [blame] | 705 | std::string stash_base = std::string(temp_stash_base_.path) + "/" + name_digest; |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 706 | ASSERT_EQ(0, access(stash_base.c_str(), F_OK)); |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 707 | // Expect the stashed blocks to be freed. |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 708 | ASSERT_EQ(-1, access((stash_base + src_hash).c_str(), F_OK)); |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 709 | ASSERT_EQ(0, rmdir(stash_base.c_str())); |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 710 | } |
Tianjie Xu | 3a8d98d | 2017-04-03 20:01:17 -0700 | [diff] [blame] | 711 | |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 712 | TEST_F(UpdaterTest, new_data_over_write) { |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 713 | std::vector<std::string> transfer_list{ |
| 714 | // clang-format off |
Tianjie Xu | 3a8d98d | 2017-04-03 20:01:17 -0700 | [diff] [blame] | 715 | "4", |
| 716 | "1", |
| 717 | "0", |
| 718 | "0", |
| 719 | "new 2,0,1", |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 720 | // clang-format on |
Tianjie Xu | 3a8d98d | 2017-04-03 20:01:17 -0700 | [diff] [blame] | 721 | }; |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 722 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 723 | // Write 4096 + 100 bytes of new data. |
| 724 | PackageEntries entries{ |
| 725 | { "new_data", std::string(4196, 0) }, |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 726 | { "patch_data", "" }, |
| 727 | { "transfer_list", android::base::Join(transfer_list, '\n') }, |
| 728 | }; |
| 729 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 730 | RunBlockImageUpdate(false, entries, image_file_, "t"); |
| 731 | } |
Tianjie Xu | 3a8d98d | 2017-04-03 20:01:17 -0700 | [diff] [blame] | 732 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 733 | TEST_F(UpdaterTest, new_data_short_write) { |
| 734 | std::vector<std::string> transfer_list{ |
| 735 | // clang-format off |
| 736 | "4", |
| 737 | "1", |
| 738 | "0", |
| 739 | "0", |
| 740 | "new 2,0,1", |
| 741 | // clang-format on |
| 742 | }; |
Tianjie Xu | 3a8d98d | 2017-04-03 20:01:17 -0700 | [diff] [blame] | 743 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 744 | PackageEntries entries{ |
| 745 | { "patch_data", "" }, |
| 746 | { "transfer_list", android::base::Join(transfer_list, '\n') }, |
| 747 | }; |
Tianjie Xu | 3a8d98d | 2017-04-03 20:01:17 -0700 | [diff] [blame] | 748 | |
| 749 | // Updater should report the failure gracefully rather than stuck in deadlock. |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 750 | entries["new_data"] = ""; |
| 751 | RunBlockImageUpdate(false, entries, image_file_, ""); |
Tianjie Xu | 3a8d98d | 2017-04-03 20:01:17 -0700 | [diff] [blame] | 752 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 753 | entries["new_data"] = std::string(10, 'a'); |
| 754 | RunBlockImageUpdate(false, entries, image_file_, ""); |
Tianjie Xu | 3a8d98d | 2017-04-03 20:01:17 -0700 | [diff] [blame] | 755 | |
| 756 | // Expect to write 1 block of new data successfully. |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 757 | entries["new_data"] = std::string(4096, 'a'); |
| 758 | RunBlockImageUpdate(false, entries, image_file_, "t"); |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | TEST_F(UpdaterTest, brotli_new_data) { |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 762 | auto generator = []() { return rand() % 128; }; |
Tianjie Xu | 6ed175d | 2017-07-18 11:29:40 -0700 | [diff] [blame] | 763 | // Generate 100 blocks of random data. |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 764 | std::string brotli_new_data; |
Tianjie Xu | 6ed175d | 2017-07-18 11:29:40 -0700 | [diff] [blame] | 765 | brotli_new_data.reserve(4096 * 100); |
| 766 | generate_n(back_inserter(brotli_new_data), 4096 * 100, generator); |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 767 | |
| 768 | size_t encoded_size = BrotliEncoderMaxCompressedSize(brotli_new_data.size()); |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 769 | std::string encoded_data(encoded_size, 0); |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 770 | ASSERT_TRUE(BrotliEncoderCompress( |
| 771 | BROTLI_DEFAULT_QUALITY, BROTLI_DEFAULT_WINDOW, BROTLI_DEFAULT_MODE, brotli_new_data.size(), |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 772 | reinterpret_cast<const uint8_t*>(brotli_new_data.data()), &encoded_size, |
| 773 | reinterpret_cast<uint8_t*>(const_cast<char*>(encoded_data.data())))); |
| 774 | encoded_data.resize(encoded_size); |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 775 | |
Tianjie Xu | 6ed175d | 2017-07-18 11:29:40 -0700 | [diff] [blame] | 776 | // Write a few small chunks of new data, then a large chunk, and finally a few small chunks. |
| 777 | // This helps us to catch potential short writes. |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 778 | std::vector<std::string> transfer_list = { |
Tianjie Xu | 6ed175d | 2017-07-18 11:29:40 -0700 | [diff] [blame] | 779 | "4", |
| 780 | "100", |
| 781 | "0", |
| 782 | "0", |
| 783 | "new 2,0,1", |
| 784 | "new 2,1,2", |
| 785 | "new 4,2,50,50,97", |
| 786 | "new 2,97,98", |
| 787 | "new 2,98,99", |
| 788 | "new 2,99,100", |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 789 | }; |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 790 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 791 | PackageEntries entries{ |
| 792 | { "new_data.br", std::move(encoded_data) }, |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 793 | { "patch_data", "" }, |
| 794 | { "transfer_list", android::base::Join(transfer_list, '\n') }, |
| 795 | }; |
| 796 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 797 | RunBlockImageUpdate(false, entries, image_file_, "t"); |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 798 | |
| 799 | std::string updated_content; |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 800 | ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_content)); |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 801 | ASSERT_EQ(brotli_new_data, updated_content); |
Tianjie Xu | 3a8d98d | 2017-04-03 20:01:17 -0700 | [diff] [blame] | 802 | } |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 803 | |
| 804 | TEST_F(UpdaterTest, last_command_update) { |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 805 | std::string block1(4096, '1'); |
| 806 | std::string block2(4096, '2'); |
| 807 | std::string block3(4096, '3'); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 808 | std::string block1_hash = get_sha1(block1); |
| 809 | std::string block2_hash = get_sha1(block2); |
| 810 | std::string block3_hash = get_sha1(block3); |
| 811 | |
| 812 | // Compose the transfer list to fail the first update. |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 813 | std::vector<std::string> transfer_list_fail{ |
| 814 | // clang-format off |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 815 | "4", |
| 816 | "2", |
| 817 | "0", |
| 818 | "2", |
| 819 | "stash " + block1_hash + " 2,0,1", |
| 820 | "move " + block1_hash + " 2,1,2 1 2,0,1", |
| 821 | "stash " + block3_hash + " 2,2,3", |
Tao Bao | 91a649a | 2018-05-21 16:05:56 -0700 | [diff] [blame] | 822 | "abort", |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 823 | // clang-format on |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 824 | }; |
| 825 | |
| 826 | // Mimic a resumed update with the same transfer commands. |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 827 | std::vector<std::string> transfer_list_continue{ |
| 828 | // clang-format off |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 829 | "4", |
| 830 | "2", |
| 831 | "0", |
| 832 | "2", |
| 833 | "stash " + block1_hash + " 2,0,1", |
| 834 | "move " + block1_hash + " 2,1,2 1 2,0,1", |
| 835 | "stash " + block3_hash + " 2,2,3", |
| 836 | "move " + block1_hash + " 2,2,3 1 2,0,1", |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 837 | // clang-format on |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 838 | }; |
| 839 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 840 | ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_)); |
| 841 | |
| 842 | PackageEntries entries{ |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 843 | { "new_data", "" }, |
| 844 | { "patch_data", "" }, |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 845 | { "transfer_list", android::base::Join(transfer_list_fail, '\n') }, |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 846 | }; |
| 847 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 848 | // "2\nstash " + block3_hash + " 2,2,3" |
| 849 | std::string last_command_content = "2\n" + transfer_list_fail[kTransferListHeaderLines + 2]; |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 850 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 851 | RunBlockImageUpdate(false, entries, image_file_, ""); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 852 | |
| 853 | // Expect last_command to contain the last stash command. |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 854 | std::string last_command_actual; |
Tao Bao | 7064aa2 | 2018-05-24 21:43:50 -0700 | [diff] [blame] | 855 | ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual)); |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 856 | EXPECT_EQ(last_command_content, last_command_actual); |
| 857 | |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 858 | std::string updated_contents; |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 859 | ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_contents)); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 860 | ASSERT_EQ(block1 + block1 + block3, updated_contents); |
| 861 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 862 | // "Resume" the update. Expect the first 'move' to be skipped but the second 'move' to be |
| 863 | // executed. Note that we intentionally reset the image file. |
| 864 | entries["transfer_list"] = android::base::Join(transfer_list_continue, '\n'); |
| 865 | ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_)); |
| 866 | RunBlockImageUpdate(false, entries, image_file_, "t"); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 867 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 868 | ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_contents)); |
| 869 | ASSERT_EQ(block1 + block2 + block1, updated_contents); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | TEST_F(UpdaterTest, last_command_update_unresumable) { |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 873 | std::string block1(4096, '1'); |
| 874 | std::string block2(4096, '2'); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 875 | std::string block1_hash = get_sha1(block1); |
| 876 | std::string block2_hash = get_sha1(block2); |
| 877 | |
| 878 | // Construct an unresumable update with source blocks mismatch. |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 879 | std::vector<std::string> transfer_list_unresumable{ |
| 880 | // clang-format off |
| 881 | "4", |
| 882 | "2", |
| 883 | "0", |
| 884 | "2", |
| 885 | "stash " + block1_hash + " 2,0,1", |
| 886 | "move " + block2_hash + " 2,1,2 1 2,0,1", |
| 887 | // clang-format on |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 888 | }; |
| 889 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 890 | PackageEntries entries{ |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 891 | { "new_data", "" }, |
| 892 | { "patch_data", "" }, |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 893 | { "transfer_list", android::base::Join(transfer_list_unresumable, '\n') }, |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 894 | }; |
| 895 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 896 | ASSERT_TRUE(android::base::WriteStringToFile(block1 + block1, image_file_)); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 897 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 898 | std::string last_command_content = "0\n" + transfer_list_unresumable[kTransferListHeaderLines]; |
Tao Bao | 7064aa2 | 2018-05-24 21:43:50 -0700 | [diff] [blame] | 899 | ASSERT_TRUE(android::base::WriteStringToFile(last_command_content, last_command_file_)); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 900 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 901 | RunBlockImageUpdate(false, entries, image_file_, ""); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 902 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 903 | // The last_command_file will be deleted if the update encounters an unresumable failure later. |
Tao Bao | 7064aa2 | 2018-05-24 21:43:50 -0700 | [diff] [blame] | 904 | ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK)); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | TEST_F(UpdaterTest, last_command_verify) { |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 908 | std::string block1(4096, '1'); |
| 909 | std::string block2(4096, '2'); |
| 910 | std::string block3(4096, '3'); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 911 | std::string block1_hash = get_sha1(block1); |
| 912 | std::string block2_hash = get_sha1(block2); |
| 913 | std::string block3_hash = get_sha1(block3); |
| 914 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 915 | std::vector<std::string> transfer_list_verify{ |
| 916 | // clang-format off |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 917 | "4", |
| 918 | "2", |
| 919 | "0", |
| 920 | "2", |
| 921 | "stash " + block1_hash + " 2,0,1", |
| 922 | "move " + block1_hash + " 2,0,1 1 2,0,1", |
| 923 | "move " + block1_hash + " 2,1,2 1 2,0,1", |
| 924 | "stash " + block3_hash + " 2,2,3", |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 925 | // clang-format on |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 926 | }; |
| 927 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 928 | PackageEntries entries{ |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 929 | { "new_data", "" }, |
| 930 | { "patch_data", "" }, |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 931 | { "transfer_list", android::base::Join(transfer_list_verify, '\n') }, |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 932 | }; |
| 933 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 934 | ASSERT_TRUE(android::base::WriteStringToFile(block1 + block1 + block3, image_file_)); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 935 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 936 | // Last command: "move " + block1_hash + " 2,1,2 1 2,0,1" |
| 937 | std::string last_command_content = "2\n" + transfer_list_verify[kTransferListHeaderLines + 2]; |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 938 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 939 | // First run: expect the verification to succeed and the last_command_file is intact. |
Tao Bao | 7064aa2 | 2018-05-24 21:43:50 -0700 | [diff] [blame] | 940 | ASSERT_TRUE(android::base::WriteStringToFile(last_command_content, last_command_file_)); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 941 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 942 | RunBlockImageUpdate(true, entries, image_file_, "t"); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 943 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 944 | std::string last_command_actual; |
Tao Bao | 7064aa2 | 2018-05-24 21:43:50 -0700 | [diff] [blame] | 945 | ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual)); |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 946 | EXPECT_EQ(last_command_content, last_command_actual); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 947 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 948 | // Second run with a mismatching block image: expect the verification to succeed but |
| 949 | // last_command_file to be deleted; because the target blocks in the last command don't have the |
| 950 | // expected contents for the second move command. |
| 951 | ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_)); |
| 952 | RunBlockImageUpdate(true, entries, image_file_, "t"); |
Tao Bao | 7064aa2 | 2018-05-24 21:43:50 -0700 | [diff] [blame] | 953 | ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK)); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 954 | } |
Tao Bao | c0299ed | 2018-05-24 00:16:35 -0700 | [diff] [blame] | 955 | |
| 956 | class ResumableUpdaterTest : public testing::TestWithParam<size_t> { |
| 957 | protected: |
| 958 | void SetUp() override { |
| 959 | RegisterBuiltins(); |
| 960 | RegisterInstallFunctions(); |
| 961 | RegisterBlockImageFunctions(); |
| 962 | |
| 963 | Paths::Get().set_cache_temp_source(temp_saved_source_.path); |
| 964 | Paths::Get().set_last_command_file(temp_last_command_.path); |
| 965 | Paths::Get().set_stash_directory_base(temp_stash_base_.path); |
| 966 | |
Tao Bao | 91a649a | 2018-05-21 16:05:56 -0700 | [diff] [blame] | 967 | // Enable a special command "abort" to simulate interruption. |
| 968 | Command::abort_allowed_ = true; |
| 969 | |
Tao Bao | c0299ed | 2018-05-24 00:16:35 -0700 | [diff] [blame] | 970 | index_ = GetParam(); |
| 971 | image_file_ = image_temp_file_.path; |
| 972 | last_command_file_ = temp_last_command_.path; |
| 973 | } |
| 974 | |
| 975 | void TearDown() override { |
| 976 | // Clean up the last_command_file if any. |
| 977 | ASSERT_TRUE(android::base::RemoveFileIfExists(last_command_file_)); |
| 978 | |
| 979 | // Clear partition updated marker if any. |
| 980 | std::string updated_marker{ temp_stash_base_.path }; |
| 981 | updated_marker += "/" + get_sha1(image_temp_file_.path) + ".UPDATED"; |
| 982 | ASSERT_TRUE(android::base::RemoveFileIfExists(updated_marker)); |
| 983 | } |
| 984 | |
| 985 | TemporaryFile temp_saved_source_; |
| 986 | TemporaryDir temp_stash_base_; |
| 987 | std::string last_command_file_; |
| 988 | std::string image_file_; |
| 989 | size_t index_; |
| 990 | |
| 991 | private: |
| 992 | TemporaryFile temp_last_command_; |
| 993 | TemporaryFile image_temp_file_; |
| 994 | }; |
| 995 | |
| 996 | static std::string g_source_image; |
| 997 | static std::string g_target_image; |
| 998 | static PackageEntries g_entries; |
| 999 | |
| 1000 | static std::vector<std::string> GenerateTransferList() { |
| 1001 | std::string a(4096, 'a'); |
| 1002 | std::string b(4096, 'b'); |
| 1003 | std::string c(4096, 'c'); |
| 1004 | std::string d(4096, 'd'); |
| 1005 | std::string e(4096, 'e'); |
| 1006 | std::string f(4096, 'f'); |
| 1007 | std::string g(4096, 'g'); |
| 1008 | std::string h(4096, 'h'); |
| 1009 | std::string i(4096, 'i'); |
| 1010 | std::string zero(4096, '\0'); |
| 1011 | |
| 1012 | std::string a_hash = get_sha1(a); |
| 1013 | std::string b_hash = get_sha1(b); |
| 1014 | std::string c_hash = get_sha1(c); |
| 1015 | std::string e_hash = get_sha1(e); |
| 1016 | |
| 1017 | auto loc = [](const std::string& range_text) { |
| 1018 | std::vector<std::string> pieces = android::base::Split(range_text, "-"); |
| 1019 | size_t left; |
| 1020 | size_t right; |
| 1021 | if (pieces.size() == 1) { |
| 1022 | CHECK(android::base::ParseUint(pieces[0], &left)); |
| 1023 | right = left + 1; |
| 1024 | } else { |
| 1025 | CHECK_EQ(2u, pieces.size()); |
| 1026 | CHECK(android::base::ParseUint(pieces[0], &left)); |
| 1027 | CHECK(android::base::ParseUint(pieces[1], &right)); |
| 1028 | right++; |
| 1029 | } |
| 1030 | return android::base::StringPrintf("2,%zu,%zu", left, right); |
| 1031 | }; |
| 1032 | |
| 1033 | // patch 1: "b d c" -> "g" |
| 1034 | TemporaryFile patch_file_bdc_g; |
| 1035 | std::string bdc = b + d + c; |
| 1036 | std::string bdc_hash = get_sha1(bdc); |
| 1037 | std::string g_hash = get_sha1(g); |
| 1038 | CHECK_EQ(0, bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(bdc.data()), bdc.size(), |
| 1039 | reinterpret_cast<const uint8_t*>(g.data()), g.size(), |
| 1040 | patch_file_bdc_g.path, nullptr)); |
| 1041 | std::string patch_bdc_g; |
| 1042 | CHECK(android::base::ReadFileToString(patch_file_bdc_g.path, &patch_bdc_g)); |
| 1043 | |
| 1044 | // patch 2: "a b c d" -> "d c b" |
| 1045 | TemporaryFile patch_file_abcd_dcb; |
| 1046 | std::string abcd = a + b + c + d; |
| 1047 | std::string abcd_hash = get_sha1(abcd); |
| 1048 | std::string dcb = d + c + b; |
| 1049 | std::string dcb_hash = get_sha1(dcb); |
| 1050 | CHECK_EQ(0, bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(abcd.data()), abcd.size(), |
| 1051 | reinterpret_cast<const uint8_t*>(dcb.data()), dcb.size(), |
| 1052 | patch_file_abcd_dcb.path, nullptr)); |
| 1053 | std::string patch_abcd_dcb; |
| 1054 | CHECK(android::base::ReadFileToString(patch_file_abcd_dcb.path, &patch_abcd_dcb)); |
| 1055 | |
| 1056 | std::vector<std::string> transfer_list{ |
| 1057 | "4", |
| 1058 | "10", // total blocks written |
| 1059 | "2", // maximum stash entries |
| 1060 | "2", // maximum number of stashed blocks |
| 1061 | |
| 1062 | // a b c d e a b c d e |
| 1063 | "stash " + b_hash + " " + loc("1"), |
| 1064 | // a b c d e a b c d e [b(1)] |
| 1065 | "stash " + c_hash + " " + loc("2"), |
| 1066 | // a b c d e a b c d e [b(1)][c(2)] |
| 1067 | "new " + loc("1-2"), |
| 1068 | // a i h d e a b c d e [b(1)][c(2)] |
| 1069 | "zero " + loc("0"), |
| 1070 | // 0 i h d e a b c d e [b(1)][c(2)] |
| 1071 | |
| 1072 | // bsdiff "b d c" (from stash, 3, stash) to get g(3) |
| 1073 | android::base::StringPrintf( |
| 1074 | "bsdiff 0 %zu %s %s %s 3 %s %s %s:%s %s:%s", |
| 1075 | patch_bdc_g.size(), // patch start (0), patch length |
| 1076 | bdc_hash.c_str(), // source hash |
| 1077 | g_hash.c_str(), // target hash |
| 1078 | loc("3").c_str(), // target range |
| 1079 | loc("3").c_str(), loc("1").c_str(), // load "d" from block 3, into buffer at offset 1 |
| 1080 | b_hash.c_str(), loc("0").c_str(), // load "b" from stash, into buffer at offset 0 |
| 1081 | c_hash.c_str(), loc("2").c_str()), // load "c" from stash, into buffer at offset 2 |
| 1082 | |
| 1083 | // 0 i h g e a b c d e [b(1)][c(2)] |
| 1084 | "free " + b_hash, |
| 1085 | // 0 i h g e a b c d e [c(2)] |
| 1086 | "free " + a_hash, |
| 1087 | // 0 i h g e a b c d e |
| 1088 | "stash " + a_hash + " " + loc("5"), |
| 1089 | // 0 i h g e a b c d e [a(5)] |
| 1090 | "move " + e_hash + " " + loc("5") + " 1 " + loc("4"), |
| 1091 | // 0 i h g e e b c d e [a(5)] |
| 1092 | |
| 1093 | // bsdiff "a b c d" (from stash, 6-8) to "d c b" (6-8) |
| 1094 | android::base::StringPrintf( // |
| 1095 | "bsdiff %zu %zu %s %s %s 4 %s %s %s:%s", |
| 1096 | patch_bdc_g.size(), // patch start |
| 1097 | patch_bdc_g.size() + patch_abcd_dcb.size(), // patch length |
| 1098 | abcd_hash.c_str(), // source hash |
| 1099 | dcb_hash.c_str(), // target hash |
| 1100 | loc("6-8").c_str(), // target range |
| 1101 | loc("6-8").c_str(), // load "b c d" from blocks 6-8 |
| 1102 | loc("1-3").c_str(), // into buffer at offset 1-3 |
| 1103 | a_hash.c_str(), // load "a" from stash |
| 1104 | loc("0").c_str()), // into buffer at offset 0 |
| 1105 | |
| 1106 | // 0 i h g e e d c b e [a(5)] |
| 1107 | "new " + loc("4"), |
| 1108 | // 0 i h g f e d c b e [a(5)] |
| 1109 | "move " + a_hash + " " + loc("9") + " 1 - " + a_hash + ":" + loc("0"), |
| 1110 | // 0 i h g f e d c b a [a(5)] |
| 1111 | "free " + a_hash, |
| 1112 | // 0 i h g f e d c b a |
| 1113 | }; |
| 1114 | |
| 1115 | std::string new_data = i + h + f; |
| 1116 | std::string patch_data = patch_bdc_g + patch_abcd_dcb; |
| 1117 | |
| 1118 | g_entries = { |
| 1119 | { "new_data", new_data }, |
| 1120 | { "patch_data", patch_data }, |
| 1121 | }; |
| 1122 | g_source_image = a + b + c + d + e + a + b + c + d + e; |
| 1123 | g_target_image = zero + i + h + g + f + e + d + c + b + a; |
| 1124 | |
| 1125 | return transfer_list; |
| 1126 | } |
| 1127 | |
| 1128 | static const std::vector<std::string> g_transfer_list = GenerateTransferList(); |
| 1129 | |
| 1130 | INSTANTIATE_TEST_CASE_P(InterruptAfterEachCommand, ResumableUpdaterTest, |
| 1131 | ::testing::Range(static_cast<size_t>(0), |
| 1132 | g_transfer_list.size() - kTransferListHeaderLines)); |
| 1133 | |
| 1134 | TEST_P(ResumableUpdaterTest, InterruptVerifyResume) { |
| 1135 | ASSERT_TRUE(android::base::WriteStringToFile(g_source_image, image_file_)); |
| 1136 | |
| 1137 | LOG(INFO) << "Interrupting at line " << index_ << " (" |
| 1138 | << g_transfer_list[kTransferListHeaderLines + index_] << ")"; |
| 1139 | |
| 1140 | std::vector<std::string> transfer_list_copy{ g_transfer_list }; |
Tao Bao | 91a649a | 2018-05-21 16:05:56 -0700 | [diff] [blame] | 1141 | transfer_list_copy[kTransferListHeaderLines + index_] = "abort"; |
Tao Bao | c0299ed | 2018-05-24 00:16:35 -0700 | [diff] [blame] | 1142 | |
| 1143 | g_entries["transfer_list"] = android::base::Join(transfer_list_copy, '\n'); |
| 1144 | |
| 1145 | // Run update that's expected to fail. |
| 1146 | RunBlockImageUpdate(false, g_entries, image_file_, ""); |
| 1147 | |
| 1148 | std::string last_command_expected; |
| 1149 | |
| 1150 | // Assert the last_command_file. |
| 1151 | if (index_ == 0) { |
| 1152 | ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK)); |
| 1153 | } else { |
| 1154 | last_command_expected = |
| 1155 | std::to_string(index_ - 1) + "\n" + g_transfer_list[kTransferListHeaderLines + index_ - 1]; |
| 1156 | std::string last_command_actual; |
| 1157 | ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual)); |
| 1158 | ASSERT_EQ(last_command_expected, last_command_actual); |
| 1159 | } |
| 1160 | |
| 1161 | g_entries["transfer_list"] = android::base::Join(g_transfer_list, '\n'); |
| 1162 | |
| 1163 | // Resume the interrupted update, by doing verification first. |
| 1164 | RunBlockImageUpdate(true, g_entries, image_file_, "t"); |
| 1165 | |
| 1166 | // last_command_file should remain intact. |
| 1167 | if (index_ == 0) { |
| 1168 | ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK)); |
| 1169 | } else { |
| 1170 | std::string last_command_actual; |
| 1171 | ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual)); |
| 1172 | ASSERT_EQ(last_command_expected, last_command_actual); |
| 1173 | } |
| 1174 | |
| 1175 | // Resume the update. |
| 1176 | RunBlockImageUpdate(false, g_entries, image_file_, "t"); |
| 1177 | |
| 1178 | // last_command_file should be gone after successful update. |
| 1179 | ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK)); |
| 1180 | |
| 1181 | std::string updated_image_actual; |
| 1182 | ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_image_actual)); |
| 1183 | ASSERT_EQ(g_target_image, updated_image_actual); |
| 1184 | } |