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