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