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