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