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