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