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