Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Tao Bao | 9aa7ab5 | 2017-01-05 17:27:19 -0800 | [diff] [blame] | 17 | #include <stdio.h> |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 18 | #include <stdlib.h> |
Tao Bao | 8992902 | 2016-11-08 20:51:31 -0800 | [diff] [blame] | 19 | #include <sys/stat.h> |
| 20 | #include <sys/types.h> |
| 21 | #include <unistd.h> |
| 22 | |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 23 | #include <algorithm> |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 24 | #include <memory> |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 25 | #include <string> |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 26 | #include <unordered_map> |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 27 | #include <vector> |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 28 | |
Tao Bao | 51d516e | 2016-11-03 14:49:01 -0700 | [diff] [blame] | 29 | #include <android-base/file.h> |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 30 | #include <android-base/logging.h> |
| 31 | #include <android-base/parseint.h> |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 32 | #include <android-base/properties.h> |
Tao Bao | 9aa7ab5 | 2017-01-05 17:27:19 -0800 | [diff] [blame] | 33 | #include <android-base/stringprintf.h> |
| 34 | #include <android-base/strings.h> |
Tao Bao | 51d516e | 2016-11-03 14:49:01 -0700 | [diff] [blame] | 35 | #include <android-base/test_utils.h> |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 36 | #include <bootloader_message/bootloader_message.h> |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 37 | #include <brotli/encode.h> |
Alex Deymo | fa18826 | 2017-10-10 17:56:17 +0200 | [diff] [blame] | 38 | #include <bsdiff/bsdiff.h> |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 39 | #include <gtest/gtest.h> |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 40 | #include <ziparchive/zip_archive.h> |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 41 | #include <ziparchive/zip_writer.h> |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 42 | |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 43 | #include "common/test_constants.h" |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 44 | #include "edify/expr.h" |
Tao Bao | 1fc5bf3 | 2017-10-06 07:43:41 -0700 | [diff] [blame] | 45 | #include "otautil/error_code.h" |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 46 | #include "otautil/paths.h" |
Tao Bao | 09e468f | 2017-09-29 14:39:33 -0700 | [diff] [blame] | 47 | #include "otautil/print_sha1.h" |
Tao Bao | 17054c0 | 2018-05-03 22:41:23 -0700 | [diff] [blame] | 48 | #include "otautil/sysutil.h" |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 49 | #include "updater/blockimg.h" |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 50 | #include "updater/install.h" |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 51 | #include "updater/updater.h" |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 52 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 53 | using PackageEntries = std::unordered_map<std::string, std::string>; |
| 54 | |
| 55 | static constexpr size_t kTransferListHeaderLines = 4; |
| 56 | |
| 57 | struct selabel_handle* sehandle = nullptr; |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 58 | |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 59 | static void expect(const char* expected, const char* expr_str, CauseCode cause_code, |
| 60 | UpdaterInfo* info = nullptr) { |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 61 | std::unique_ptr<Expr> e; |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 62 | int error_count = 0; |
| 63 | ASSERT_EQ(0, parse_string(expr_str, &e, &error_count)); |
| 64 | ASSERT_EQ(0, error_count); |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 65 | |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 66 | State state(expr_str, info); |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 67 | |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 68 | std::string result; |
| 69 | bool status = Evaluate(&state, e, &result); |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 70 | |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 71 | if (expected == nullptr) { |
| 72 | ASSERT_FALSE(status); |
| 73 | } else { |
| 74 | ASSERT_TRUE(status); |
| 75 | ASSERT_STREQ(expected, result.c_str()); |
| 76 | } |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 77 | |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 78 | // Error code is set in updater/updater.cpp only, by parsing State.errmsg. |
| 79 | ASSERT_EQ(kNoError, state.error_code); |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 80 | |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 81 | // Cause code should always be available. |
| 82 | ASSERT_EQ(cause_code, state.cause_code); |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 85 | static void BuildUpdatePackage(const PackageEntries& entries, int fd) { |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 86 | FILE* zip_file_ptr = fdopen(fd, "wb"); |
| 87 | ZipWriter zip_writer(zip_file_ptr); |
| 88 | |
| 89 | for (const auto& entry : entries) { |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 90 | // All the entries are written as STORED. |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 91 | ASSERT_EQ(0, zip_writer.StartEntry(entry.first.c_str(), 0)); |
| 92 | if (!entry.second.empty()) { |
| 93 | ASSERT_EQ(0, zip_writer.WriteBytes(entry.second.data(), entry.second.size())); |
| 94 | } |
| 95 | ASSERT_EQ(0, zip_writer.FinishEntry()); |
| 96 | } |
| 97 | |
| 98 | ASSERT_EQ(0, zip_writer.Finish()); |
| 99 | ASSERT_EQ(0, fclose(zip_file_ptr)); |
| 100 | } |
| 101 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 102 | static void RunBlockImageUpdate(bool is_verify, const PackageEntries& entries, |
| 103 | const std::string& image_file, const std::string& result) { |
| 104 | CHECK(entries.find("transfer_list") != entries.end()); |
| 105 | |
| 106 | // Build the update package. |
| 107 | TemporaryFile zip_file; |
| 108 | BuildUpdatePackage(entries, zip_file.release()); |
| 109 | |
| 110 | MemMapping map; |
| 111 | ASSERT_TRUE(map.MapFile(zip_file.path)); |
| 112 | ZipArchiveHandle handle; |
| 113 | ASSERT_EQ(0, OpenArchiveFromMemory(map.addr, map.length, zip_file.path, &handle)); |
| 114 | |
| 115 | // Set up the handler, command_pipe, patch offset & length. |
| 116 | UpdaterInfo updater_info; |
| 117 | updater_info.package_zip = handle; |
| 118 | TemporaryFile temp_pipe; |
| 119 | updater_info.cmd_pipe = fdopen(temp_pipe.release(), "wbe"); |
| 120 | updater_info.package_zip_addr = map.addr; |
| 121 | updater_info.package_zip_len = map.length; |
| 122 | |
| 123 | std::string new_data = entries.find("new_data.br") != entries.end() ? "new_data.br" : "new_data"; |
| 124 | std::string script = is_verify ? "block_image_verify" : "block_image_update"; |
| 125 | script += R"((")" + image_file + R"(", package_extract_file("transfer_list"), ")" + new_data + |
| 126 | R"(", "patch_data"))"; |
| 127 | expect(result.c_str(), script.c_str(), kNoCause, &updater_info); |
| 128 | |
| 129 | ASSERT_EQ(0, fclose(updater_info.cmd_pipe)); |
| 130 | CloseArchive(handle); |
| 131 | } |
| 132 | |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 133 | static std::string get_sha1(const std::string& content) { |
| 134 | uint8_t digest[SHA_DIGEST_LENGTH]; |
| 135 | SHA1(reinterpret_cast<const uint8_t*>(content.c_str()), content.size(), digest); |
| 136 | return print_sha1(digest); |
| 137 | } |
| 138 | |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 139 | class UpdaterTest : public ::testing::Test { |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 140 | protected: |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 141 | void SetUp() override { |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 142 | RegisterBuiltins(); |
| 143 | RegisterInstallFunctions(); |
| 144 | RegisterBlockImageFunctions(); |
Tianjie Xu | 3bbb20f | 2018-02-27 15:56:11 -0800 | [diff] [blame] | 145 | |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 146 | Paths::Get().set_cache_temp_source(temp_saved_source_.path); |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 147 | Paths::Get().set_stash_directory_base(temp_stash_base_.path); |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 148 | |
| 149 | image_file_ = image_temp_file_.path; |
| 150 | } |
| 151 | |
| 152 | void TearDown() override { |
| 153 | // Clear partition updated marker if any. |
| 154 | std::string updated_marker{ temp_stash_base_.path }; |
| 155 | updated_marker += "/" + get_sha1(image_temp_file_.path) + ".UPDATED"; |
| 156 | ASSERT_TRUE(android::base::RemoveFileIfExists(updated_marker)); |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 157 | } |
Tianjie Xu | 3bbb20f | 2018-02-27 15:56:11 -0800 | [diff] [blame] | 158 | |
| 159 | TemporaryFile temp_saved_source_; |
Tianjie Xu | 3bbb20f | 2018-02-27 15:56:11 -0800 | [diff] [blame] | 160 | TemporaryDir temp_stash_base_; |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 161 | std::string image_file_; |
| 162 | |
| 163 | private: |
| 164 | TemporaryFile image_temp_file_; |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 165 | }; |
| 166 | |
| 167 | TEST_F(UpdaterTest, getprop) { |
| 168 | expect(android::base::GetProperty("ro.product.device", "").c_str(), |
| 169 | "getprop(\"ro.product.device\")", |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 170 | kNoCause); |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 171 | |
| 172 | expect(android::base::GetProperty("ro.build.fingerprint", "").c_str(), |
| 173 | "getprop(\"ro.build.fingerprint\")", |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 174 | kNoCause); |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 175 | |
| 176 | // getprop() accepts only one parameter. |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 177 | expect(nullptr, "getprop()", kArgsParsingFailure); |
| 178 | expect(nullptr, "getprop(\"arg1\", \"arg2\")", kArgsParsingFailure); |
| 179 | } |
| 180 | |
| 181 | TEST_F(UpdaterTest, sha1_check) { |
| 182 | // sha1_check(data) returns the SHA-1 of the data. |
| 183 | expect("81fe8bfe87576c3ecb22426f8e57847382917acf", "sha1_check(\"abcd\")", kNoCause); |
| 184 | expect("da39a3ee5e6b4b0d3255bfef95601890afd80709", "sha1_check(\"\")", kNoCause); |
| 185 | |
| 186 | // sha1_check(data, sha1_hex, [sha1_hex, ...]) returns the matched SHA-1. |
| 187 | expect("81fe8bfe87576c3ecb22426f8e57847382917acf", |
| 188 | "sha1_check(\"abcd\", \"81fe8bfe87576c3ecb22426f8e57847382917acf\")", |
| 189 | kNoCause); |
| 190 | |
| 191 | expect("81fe8bfe87576c3ecb22426f8e57847382917acf", |
| 192 | "sha1_check(\"abcd\", \"wrong_sha1\", \"81fe8bfe87576c3ecb22426f8e57847382917acf\")", |
| 193 | kNoCause); |
| 194 | |
| 195 | // Or "" if there's no match. |
| 196 | expect("", |
| 197 | "sha1_check(\"abcd\", \"wrong_sha1\")", |
| 198 | kNoCause); |
| 199 | |
| 200 | expect("", |
| 201 | "sha1_check(\"abcd\", \"wrong_sha1\", \"wrong_sha2\")", |
| 202 | kNoCause); |
| 203 | |
| 204 | // sha1_check() expects at least one argument. |
| 205 | expect(nullptr, "sha1_check()", kArgsParsingFailure); |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 206 | } |
Tao Bao | 51d516e | 2016-11-03 14:49:01 -0700 | [diff] [blame] | 207 | |
Tao Bao | db56eb0 | 2017-03-23 06:34:20 -0700 | [diff] [blame] | 208 | TEST_F(UpdaterTest, apply_patch_check) { |
| 209 | // Zero-argument is not valid. |
| 210 | expect(nullptr, "apply_patch_check()", kArgsParsingFailure); |
| 211 | |
| 212 | // File not found. |
| 213 | expect("", "apply_patch_check(\"/doesntexist\")", kNoCause); |
| 214 | |
| 215 | std::string src_file = from_testdata_base("old.file"); |
| 216 | std::string src_content; |
| 217 | ASSERT_TRUE(android::base::ReadFileToString(src_file, &src_content)); |
| 218 | size_t src_size = src_content.size(); |
| 219 | std::string src_hash = get_sha1(src_content); |
| 220 | |
| 221 | // One-argument with EMMC:file:size:sha1 should pass the check. |
| 222 | std::string filename = android::base::Join( |
| 223 | std::vector<std::string>{ "EMMC", src_file, std::to_string(src_size), src_hash }, ":"); |
| 224 | std::string cmd = "apply_patch_check(\"" + filename + "\")"; |
| 225 | expect("t", cmd.c_str(), kNoCause); |
| 226 | |
| 227 | // EMMC:file:(size-1):sha1:(size+1):sha1 should fail the check. |
| 228 | std::string filename_bad = android::base::Join( |
| 229 | std::vector<std::string>{ "EMMC", src_file, std::to_string(src_size - 1), src_hash, |
| 230 | std::to_string(src_size + 1), src_hash }, |
| 231 | ":"); |
| 232 | cmd = "apply_patch_check(\"" + filename_bad + "\")"; |
| 233 | expect("", cmd.c_str(), kNoCause); |
| 234 | |
| 235 | // EMMC:file:(size-1):sha1:size:sha1:(size+1):sha1 should pass the check. |
| 236 | filename_bad = |
| 237 | android::base::Join(std::vector<std::string>{ "EMMC", src_file, std::to_string(src_size - 1), |
| 238 | src_hash, std::to_string(src_size), src_hash, |
| 239 | std::to_string(src_size + 1), src_hash }, |
| 240 | ":"); |
| 241 | cmd = "apply_patch_check(\"" + filename_bad + "\")"; |
| 242 | expect("t", cmd.c_str(), kNoCause); |
| 243 | |
| 244 | // Multiple arguments. |
| 245 | cmd = "apply_patch_check(\"" + filename + "\", \"wrong_sha1\", \"wrong_sha2\")"; |
| 246 | expect("", cmd.c_str(), kNoCause); |
| 247 | |
| 248 | cmd = "apply_patch_check(\"" + filename + "\", \"wrong_sha1\", \"" + src_hash + |
| 249 | "\", \"wrong_sha2\")"; |
| 250 | expect("t", cmd.c_str(), kNoCause); |
| 251 | |
| 252 | cmd = "apply_patch_check(\"" + filename_bad + "\", \"wrong_sha1\", \"" + src_hash + |
| 253 | "\", \"wrong_sha2\")"; |
| 254 | expect("t", cmd.c_str(), kNoCause); |
| 255 | } |
| 256 | |
Tao Bao | 51d516e | 2016-11-03 14:49:01 -0700 | [diff] [blame] | 257 | TEST_F(UpdaterTest, file_getprop) { |
| 258 | // file_getprop() expects two arguments. |
| 259 | expect(nullptr, "file_getprop()", kArgsParsingFailure); |
| 260 | expect(nullptr, "file_getprop(\"arg1\")", kArgsParsingFailure); |
| 261 | expect(nullptr, "file_getprop(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure); |
| 262 | |
| 263 | // File doesn't exist. |
| 264 | expect(nullptr, "file_getprop(\"/doesntexist\", \"key1\")", kFileGetPropFailure); |
| 265 | |
| 266 | // Reject too large files (current limit = 65536). |
| 267 | TemporaryFile temp_file1; |
| 268 | std::string buffer(65540, '\0'); |
| 269 | ASSERT_TRUE(android::base::WriteStringToFile(buffer, temp_file1.path)); |
| 270 | |
| 271 | // Read some keys. |
| 272 | TemporaryFile temp_file2; |
| 273 | std::string content("ro.product.name=tardis\n" |
| 274 | "# comment\n\n\n" |
| 275 | "ro.product.model\n" |
| 276 | "ro.product.board = magic \n"); |
| 277 | ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file2.path)); |
| 278 | |
| 279 | std::string script1("file_getprop(\"" + std::string(temp_file2.path) + |
| 280 | "\", \"ro.product.name\")"); |
| 281 | expect("tardis", script1.c_str(), kNoCause); |
| 282 | |
| 283 | std::string script2("file_getprop(\"" + std::string(temp_file2.path) + |
| 284 | "\", \"ro.product.board\")"); |
| 285 | expect("magic", script2.c_str(), kNoCause); |
| 286 | |
| 287 | // No match. |
| 288 | std::string script3("file_getprop(\"" + std::string(temp_file2.path) + |
| 289 | "\", \"ro.product.wrong\")"); |
| 290 | expect("", script3.c_str(), kNoCause); |
| 291 | |
| 292 | std::string script4("file_getprop(\"" + std::string(temp_file2.path) + |
| 293 | "\", \"ro.product.name=\")"); |
| 294 | expect("", script4.c_str(), kNoCause); |
| 295 | |
| 296 | std::string script5("file_getprop(\"" + std::string(temp_file2.path) + |
| 297 | "\", \"ro.product.nam\")"); |
| 298 | expect("", script5.c_str(), kNoCause); |
| 299 | |
| 300 | std::string script6("file_getprop(\"" + std::string(temp_file2.path) + |
| 301 | "\", \"ro.product.model\")"); |
| 302 | expect("", script6.c_str(), kNoCause); |
| 303 | } |
Tao Bao | 0831d0b | 2016-11-03 23:25:04 -0700 | [diff] [blame] | 304 | |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 305 | // TODO: Test extracting to block device. |
| 306 | TEST_F(UpdaterTest, package_extract_file) { |
| 307 | // package_extract_file expects 1 or 2 arguments. |
| 308 | expect(nullptr, "package_extract_file()", kArgsParsingFailure); |
| 309 | expect(nullptr, "package_extract_file(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure); |
| 310 | |
| 311 | std::string zip_path = from_testdata_base("ziptest_valid.zip"); |
| 312 | ZipArchiveHandle handle; |
| 313 | ASSERT_EQ(0, OpenArchive(zip_path.c_str(), &handle)); |
| 314 | |
| 315 | // Need to set up the ziphandle. |
| 316 | UpdaterInfo updater_info; |
| 317 | updater_info.package_zip = handle; |
| 318 | |
| 319 | // Two-argument version. |
| 320 | TemporaryFile temp_file1; |
| 321 | std::string script("package_extract_file(\"a.txt\", \"" + std::string(temp_file1.path) + "\")"); |
| 322 | expect("t", script.c_str(), kNoCause, &updater_info); |
| 323 | |
| 324 | // Verify the extracted entry. |
| 325 | std::string data; |
| 326 | ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data)); |
| 327 | ASSERT_EQ(kATxtContents, data); |
| 328 | |
| 329 | // Now extract another entry to the same location, which should overwrite. |
| 330 | script = "package_extract_file(\"b.txt\", \"" + std::string(temp_file1.path) + "\")"; |
| 331 | expect("t", script.c_str(), kNoCause, &updater_info); |
| 332 | |
| 333 | ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data)); |
| 334 | ASSERT_EQ(kBTxtContents, data); |
| 335 | |
| 336 | // Missing zip entry. The two-argument version doesn't abort. |
| 337 | script = "package_extract_file(\"doesntexist\", \"" + std::string(temp_file1.path) + "\")"; |
| 338 | expect("", script.c_str(), kNoCause, &updater_info); |
| 339 | |
| 340 | // Extract to /dev/full should fail. |
| 341 | script = "package_extract_file(\"a.txt\", \"/dev/full\")"; |
| 342 | expect("", script.c_str(), kNoCause, &updater_info); |
| 343 | |
| 344 | // One-argument version. |
| 345 | script = "sha1_check(package_extract_file(\"a.txt\"))"; |
| 346 | expect(kATxtSha1Sum.c_str(), script.c_str(), kNoCause, &updater_info); |
| 347 | |
| 348 | script = "sha1_check(package_extract_file(\"b.txt\"))"; |
| 349 | expect(kBTxtSha1Sum.c_str(), script.c_str(), kNoCause, &updater_info); |
| 350 | |
| 351 | // Missing entry. The one-argument version aborts the evaluation. |
| 352 | script = "package_extract_file(\"doesntexist\")"; |
| 353 | expect(nullptr, script.c_str(), kPackageExtractFileFailure, &updater_info); |
| 354 | |
| 355 | CloseArchive(handle); |
| 356 | } |
Tao Bao | d0f3088 | 2016-11-03 23:52:01 -0700 | [diff] [blame] | 357 | |
| 358 | TEST_F(UpdaterTest, write_value) { |
| 359 | // write_value() expects two arguments. |
| 360 | expect(nullptr, "write_value()", kArgsParsingFailure); |
| 361 | expect(nullptr, "write_value(\"arg1\")", kArgsParsingFailure); |
| 362 | expect(nullptr, "write_value(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure); |
| 363 | |
| 364 | // filename cannot be empty. |
| 365 | expect(nullptr, "write_value(\"value\", \"\")", kArgsParsingFailure); |
| 366 | |
| 367 | // Write some value to file. |
| 368 | TemporaryFile temp_file; |
| 369 | std::string value = "magicvalue"; |
| 370 | std::string script("write_value(\"" + value + "\", \"" + std::string(temp_file.path) + "\")"); |
| 371 | expect("t", script.c_str(), kNoCause); |
| 372 | |
| 373 | // Verify the content. |
| 374 | std::string content; |
| 375 | ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content)); |
| 376 | ASSERT_EQ(value, content); |
| 377 | |
| 378 | // Allow writing empty string. |
| 379 | script = "write_value(\"\", \"" + std::string(temp_file.path) + "\")"; |
| 380 | expect("t", script.c_str(), kNoCause); |
| 381 | |
| 382 | // Verify the content. |
| 383 | ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content)); |
| 384 | ASSERT_EQ("", content); |
| 385 | |
| 386 | // It should fail gracefully when write fails. |
| 387 | script = "write_value(\"value\", \"/proc/0/file1\")"; |
| 388 | expect("", script.c_str(), kNoCause); |
| 389 | } |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 390 | |
| 391 | TEST_F(UpdaterTest, get_stage) { |
| 392 | // get_stage() expects one argument. |
| 393 | expect(nullptr, "get_stage()", kArgsParsingFailure); |
| 394 | expect(nullptr, "get_stage(\"arg1\", \"arg2\")", kArgsParsingFailure); |
| 395 | expect(nullptr, "get_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure); |
| 396 | |
| 397 | // Set up a local file as BCB. |
| 398 | TemporaryFile tf; |
| 399 | std::string temp_file(tf.path); |
| 400 | bootloader_message boot; |
| 401 | strlcpy(boot.stage, "2/3", sizeof(boot.stage)); |
| 402 | std::string err; |
| 403 | ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err)); |
| 404 | |
| 405 | // Can read the stage value. |
| 406 | std::string script("get_stage(\"" + temp_file + "\")"); |
| 407 | expect("2/3", script.c_str(), kNoCause); |
| 408 | |
| 409 | // Bad BCB path. |
| 410 | script = "get_stage(\"doesntexist\")"; |
| 411 | expect("", script.c_str(), kNoCause); |
| 412 | } |
| 413 | |
| 414 | TEST_F(UpdaterTest, set_stage) { |
| 415 | // set_stage() expects two arguments. |
| 416 | expect(nullptr, "set_stage()", kArgsParsingFailure); |
| 417 | expect(nullptr, "set_stage(\"arg1\")", kArgsParsingFailure); |
| 418 | expect(nullptr, "set_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure); |
| 419 | |
| 420 | // Set up a local file as BCB. |
| 421 | TemporaryFile tf; |
| 422 | std::string temp_file(tf.path); |
| 423 | bootloader_message boot; |
| 424 | strlcpy(boot.command, "command", sizeof(boot.command)); |
| 425 | strlcpy(boot.stage, "2/3", sizeof(boot.stage)); |
| 426 | std::string err; |
| 427 | ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err)); |
| 428 | |
| 429 | // Write with set_stage(). |
| 430 | std::string script("set_stage(\"" + temp_file + "\", \"1/3\")"); |
| 431 | expect(tf.path, script.c_str(), kNoCause); |
| 432 | |
| 433 | // Verify. |
| 434 | bootloader_message boot_verify; |
| 435 | ASSERT_TRUE(read_bootloader_message_from(&boot_verify, temp_file, &err)); |
| 436 | |
| 437 | // Stage should be updated, with command part untouched. |
| 438 | ASSERT_STREQ("1/3", boot_verify.stage); |
| 439 | ASSERT_STREQ(boot.command, boot_verify.command); |
| 440 | |
| 441 | // Bad BCB path. |
| 442 | script = "set_stage(\"doesntexist\", \"1/3\")"; |
| 443 | expect("", script.c_str(), kNoCause); |
| 444 | |
| 445 | script = "set_stage(\"/dev/full\", \"1/3\")"; |
| 446 | expect("", script.c_str(), kNoCause); |
| 447 | } |
Tao Bao | 9aa7ab5 | 2017-01-05 17:27:19 -0800 | [diff] [blame] | 448 | |
| 449 | TEST_F(UpdaterTest, set_progress) { |
| 450 | // set_progress() expects one argument. |
| 451 | expect(nullptr, "set_progress()", kArgsParsingFailure); |
| 452 | expect(nullptr, "set_progress(\"arg1\", \"arg2\")", kArgsParsingFailure); |
| 453 | |
| 454 | // Invalid progress argument. |
| 455 | expect(nullptr, "set_progress(\"arg1\")", kArgsParsingFailure); |
| 456 | expect(nullptr, "set_progress(\"3x+5\")", kArgsParsingFailure); |
| 457 | expect(nullptr, "set_progress(\".3.5\")", kArgsParsingFailure); |
| 458 | |
| 459 | TemporaryFile tf; |
| 460 | UpdaterInfo updater_info; |
Tianjie Xu | 79327ac | 2017-09-08 17:09:10 -0700 | [diff] [blame] | 461 | updater_info.cmd_pipe = fdopen(tf.release(), "w"); |
Tao Bao | 9aa7ab5 | 2017-01-05 17:27:19 -0800 | [diff] [blame] | 462 | expect(".52", "set_progress(\".52\")", kNoCause, &updater_info); |
| 463 | fflush(updater_info.cmd_pipe); |
| 464 | |
| 465 | std::string cmd; |
| 466 | ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd)); |
| 467 | ASSERT_EQ(android::base::StringPrintf("set_progress %f\n", .52), cmd); |
| 468 | // recovery-updater protocol expects 2 tokens ("set_progress <frac>"). |
| 469 | ASSERT_EQ(2U, android::base::Split(cmd, " ").size()); |
Tianjie Xu | 79327ac | 2017-09-08 17:09:10 -0700 | [diff] [blame] | 470 | ASSERT_EQ(0, fclose(updater_info.cmd_pipe)); |
Tao Bao | 9aa7ab5 | 2017-01-05 17:27:19 -0800 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | TEST_F(UpdaterTest, show_progress) { |
| 474 | // show_progress() expects two arguments. |
| 475 | expect(nullptr, "show_progress()", kArgsParsingFailure); |
| 476 | expect(nullptr, "show_progress(\"arg1\")", kArgsParsingFailure); |
| 477 | expect(nullptr, "show_progress(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure); |
| 478 | |
| 479 | // Invalid progress arguments. |
| 480 | expect(nullptr, "show_progress(\"arg1\", \"arg2\")", kArgsParsingFailure); |
| 481 | expect(nullptr, "show_progress(\"3x+5\", \"10\")", kArgsParsingFailure); |
| 482 | expect(nullptr, "show_progress(\".3\", \"5a\")", kArgsParsingFailure); |
| 483 | |
| 484 | TemporaryFile tf; |
| 485 | UpdaterInfo updater_info; |
Tianjie Xu | 79327ac | 2017-09-08 17:09:10 -0700 | [diff] [blame] | 486 | updater_info.cmd_pipe = fdopen(tf.release(), "w"); |
Tao Bao | 9aa7ab5 | 2017-01-05 17:27:19 -0800 | [diff] [blame] | 487 | expect(".52", "show_progress(\".52\", \"10\")", kNoCause, &updater_info); |
| 488 | fflush(updater_info.cmd_pipe); |
| 489 | |
| 490 | std::string cmd; |
| 491 | ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd)); |
| 492 | ASSERT_EQ(android::base::StringPrintf("progress %f %d\n", .52, 10), cmd); |
| 493 | // recovery-updater protocol expects 3 tokens ("progress <frac> <secs>"). |
| 494 | ASSERT_EQ(3U, android::base::Split(cmd, " ").size()); |
Tianjie Xu | 79327ac | 2017-09-08 17:09:10 -0700 | [diff] [blame] | 495 | ASSERT_EQ(0, fclose(updater_info.cmd_pipe)); |
Tao Bao | 9aa7ab5 | 2017-01-05 17:27:19 -0800 | [diff] [blame] | 496 | } |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 497 | |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 498 | TEST_F(UpdaterTest, block_image_update_patch_data) { |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 499 | std::string src_content = std::string(4096, 'a') + std::string(4096, 'c'); |
| 500 | std::string tgt_content = std::string(4096, 'b') + std::string(4096, 'd'); |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 501 | |
| 502 | // Generate the patch data. |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 503 | TemporaryFile patch_file; |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 504 | ASSERT_EQ(0, |
| 505 | bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(src_content.data()), src_content.size(), |
| 506 | reinterpret_cast<const uint8_t*>(tgt_content.data()), tgt_content.size(), |
| 507 | patch_file.path, nullptr)); |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 508 | std::string patch_content; |
| 509 | ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch_content)); |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 510 | |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 511 | // Create the transfer list that contains a bsdiff. |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 512 | std::string src_hash = get_sha1(src_content); |
| 513 | std::string tgt_hash = get_sha1(tgt_content); |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 514 | std::vector<std::string> transfer_list{ |
| 515 | // clang-format off |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 516 | "4", |
| 517 | "2", |
| 518 | "0", |
| 519 | "2", |
| 520 | "stash " + src_hash + " 2,0,2", |
| 521 | android::base::StringPrintf("bsdiff 0 %zu %s %s 2,0,2 2 - %s:2,0,2", patch_content.size(), |
| 522 | src_hash.c_str(), tgt_hash.c_str(), src_hash.c_str()), |
| 523 | "free " + src_hash, |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 524 | // clang-format on |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 525 | }; |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 526 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 527 | PackageEntries entries{ |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 528 | { "new_data", "" }, |
| 529 | { "patch_data", patch_content }, |
| 530 | { "transfer_list", android::base::Join(transfer_list, '\n') }, |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 531 | }; |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 532 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 533 | ASSERT_TRUE(android::base::WriteStringToFile(src_content, image_file_)); |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 534 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 535 | RunBlockImageUpdate(false, entries, image_file_, "t"); |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 536 | |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 537 | // The update_file should be patched correctly. |
| 538 | std::string updated_content; |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 539 | ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_content)); |
| 540 | ASSERT_EQ(tgt_content, updated_content); |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | TEST_F(UpdaterTest, block_image_update_fail) { |
| 544 | std::string src_content(4096 * 2, 'e'); |
| 545 | std::string src_hash = get_sha1(src_content); |
| 546 | // Stash and free some blocks, then fail the update intentionally. |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 547 | std::vector<std::string> transfer_list{ |
| 548 | // clang-format off |
| 549 | "4", |
| 550 | "2", |
| 551 | "0", |
| 552 | "2", |
| 553 | "stash " + src_hash + " 2,0,2", |
| 554 | "free " + src_hash, |
| 555 | "fail", |
| 556 | // clang-format on |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 557 | }; |
| 558 | |
| 559 | // Add a new data of 10 bytes to test the deadlock. |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 560 | PackageEntries entries{ |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 561 | { "new_data", std::string(10, 0) }, |
| 562 | { "patch_data", "" }, |
| 563 | { "transfer_list", android::base::Join(transfer_list, '\n') }, |
| 564 | }; |
| 565 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 566 | ASSERT_TRUE(android::base::WriteStringToFile(src_content, image_file_)); |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 567 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 568 | RunBlockImageUpdate(false, entries, image_file_, ""); |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 569 | |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 570 | // Updater generates the stash name based on the input file name. |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 571 | std::string name_digest = get_sha1(image_file_); |
Tianjie Xu | 3bbb20f | 2018-02-27 15:56:11 -0800 | [diff] [blame] | 572 | std::string stash_base = std::string(temp_stash_base_.path) + "/" + name_digest; |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 573 | ASSERT_EQ(0, access(stash_base.c_str(), F_OK)); |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 574 | // Expect the stashed blocks to be freed. |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 575 | ASSERT_EQ(-1, access((stash_base + src_hash).c_str(), F_OK)); |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 576 | ASSERT_EQ(0, rmdir(stash_base.c_str())); |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 577 | } |
Tianjie Xu | 3a8d98d | 2017-04-03 20:01:17 -0700 | [diff] [blame] | 578 | |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 579 | TEST_F(UpdaterTest, new_data_over_write) { |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 580 | std::vector<std::string> transfer_list{ |
| 581 | // clang-format off |
Tianjie Xu | 3a8d98d | 2017-04-03 20:01:17 -0700 | [diff] [blame] | 582 | "4", |
| 583 | "1", |
| 584 | "0", |
| 585 | "0", |
| 586 | "new 2,0,1", |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 587 | // clang-format on |
Tianjie Xu | 3a8d98d | 2017-04-03 20:01:17 -0700 | [diff] [blame] | 588 | }; |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 589 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 590 | // Write 4096 + 100 bytes of new data. |
| 591 | PackageEntries entries{ |
| 592 | { "new_data", std::string(4196, 0) }, |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 593 | { "patch_data", "" }, |
| 594 | { "transfer_list", android::base::Join(transfer_list, '\n') }, |
| 595 | }; |
| 596 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 597 | RunBlockImageUpdate(false, entries, image_file_, "t"); |
| 598 | } |
Tianjie Xu | 3a8d98d | 2017-04-03 20:01:17 -0700 | [diff] [blame] | 599 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 600 | TEST_F(UpdaterTest, new_data_short_write) { |
| 601 | std::vector<std::string> transfer_list{ |
| 602 | // clang-format off |
| 603 | "4", |
| 604 | "1", |
| 605 | "0", |
| 606 | "0", |
| 607 | "new 2,0,1", |
| 608 | // clang-format on |
| 609 | }; |
Tianjie Xu | 3a8d98d | 2017-04-03 20:01:17 -0700 | [diff] [blame] | 610 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 611 | PackageEntries entries{ |
| 612 | { "patch_data", "" }, |
| 613 | { "transfer_list", android::base::Join(transfer_list, '\n') }, |
| 614 | }; |
Tianjie Xu | 3a8d98d | 2017-04-03 20:01:17 -0700 | [diff] [blame] | 615 | |
| 616 | // Updater should report the failure gracefully rather than stuck in deadlock. |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 617 | entries["new_data"] = ""; |
| 618 | RunBlockImageUpdate(false, entries, image_file_, ""); |
Tianjie Xu | 3a8d98d | 2017-04-03 20:01:17 -0700 | [diff] [blame] | 619 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 620 | entries["new_data"] = std::string(10, 'a'); |
| 621 | RunBlockImageUpdate(false, entries, image_file_, ""); |
Tianjie Xu | 3a8d98d | 2017-04-03 20:01:17 -0700 | [diff] [blame] | 622 | |
| 623 | // Expect to write 1 block of new data successfully. |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 624 | entries["new_data"] = std::string(4096, 'a'); |
| 625 | RunBlockImageUpdate(false, entries, image_file_, "t"); |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | TEST_F(UpdaterTest, brotli_new_data) { |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 629 | auto generator = []() { return rand() % 128; }; |
Tianjie Xu | 6ed175d | 2017-07-18 11:29:40 -0700 | [diff] [blame] | 630 | // Generate 100 blocks of random data. |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 631 | std::string brotli_new_data; |
Tianjie Xu | 6ed175d | 2017-07-18 11:29:40 -0700 | [diff] [blame] | 632 | brotli_new_data.reserve(4096 * 100); |
| 633 | generate_n(back_inserter(brotli_new_data), 4096 * 100, generator); |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 634 | |
| 635 | size_t encoded_size = BrotliEncoderMaxCompressedSize(brotli_new_data.size()); |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 636 | std::string encoded_data(encoded_size, 0); |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 637 | ASSERT_TRUE(BrotliEncoderCompress( |
| 638 | 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] | 639 | reinterpret_cast<const uint8_t*>(brotli_new_data.data()), &encoded_size, |
| 640 | reinterpret_cast<uint8_t*>(const_cast<char*>(encoded_data.data())))); |
| 641 | encoded_data.resize(encoded_size); |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 642 | |
Tianjie Xu | 6ed175d | 2017-07-18 11:29:40 -0700 | [diff] [blame] | 643 | // Write a few small chunks of new data, then a large chunk, and finally a few small chunks. |
| 644 | // This helps us to catch potential short writes. |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 645 | std::vector<std::string> transfer_list = { |
Tianjie Xu | 6ed175d | 2017-07-18 11:29:40 -0700 | [diff] [blame] | 646 | "4", |
| 647 | "100", |
| 648 | "0", |
| 649 | "0", |
| 650 | "new 2,0,1", |
| 651 | "new 2,1,2", |
| 652 | "new 4,2,50,50,97", |
| 653 | "new 2,97,98", |
| 654 | "new 2,98,99", |
| 655 | "new 2,99,100", |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 656 | }; |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 657 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 658 | PackageEntries entries{ |
| 659 | { "new_data.br", std::move(encoded_data) }, |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 660 | { "patch_data", "" }, |
| 661 | { "transfer_list", android::base::Join(transfer_list, '\n') }, |
| 662 | }; |
| 663 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 664 | RunBlockImageUpdate(false, entries, image_file_, "t"); |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 665 | |
| 666 | std::string updated_content; |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 667 | ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_content)); |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 668 | ASSERT_EQ(brotli_new_data, updated_content); |
Tianjie Xu | 3a8d98d | 2017-04-03 20:01:17 -0700 | [diff] [blame] | 669 | } |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 670 | |
| 671 | TEST_F(UpdaterTest, last_command_update) { |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 672 | std::string block1(4096, '1'); |
| 673 | std::string block2(4096, '2'); |
| 674 | std::string block3(4096, '3'); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 675 | std::string block1_hash = get_sha1(block1); |
| 676 | std::string block2_hash = get_sha1(block2); |
| 677 | std::string block3_hash = get_sha1(block3); |
| 678 | |
| 679 | // Compose the transfer list to fail the first update. |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 680 | std::vector<std::string> transfer_list_fail{ |
| 681 | // clang-format off |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 682 | "4", |
| 683 | "2", |
| 684 | "0", |
| 685 | "2", |
| 686 | "stash " + block1_hash + " 2,0,1", |
| 687 | "move " + block1_hash + " 2,1,2 1 2,0,1", |
| 688 | "stash " + block3_hash + " 2,2,3", |
| 689 | "fail", |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 690 | // clang-format on |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 691 | }; |
| 692 | |
| 693 | // Mimic a resumed update with the same transfer commands. |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 694 | std::vector<std::string> transfer_list_continue{ |
| 695 | // clang-format off |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 696 | "4", |
| 697 | "2", |
| 698 | "0", |
| 699 | "2", |
| 700 | "stash " + block1_hash + " 2,0,1", |
| 701 | "move " + block1_hash + " 2,1,2 1 2,0,1", |
| 702 | "stash " + block3_hash + " 2,2,3", |
| 703 | "move " + block1_hash + " 2,2,3 1 2,0,1", |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 704 | // clang-format on |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 705 | }; |
| 706 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 707 | ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_)); |
| 708 | |
| 709 | PackageEntries entries{ |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 710 | { "new_data", "" }, |
| 711 | { "patch_data", "" }, |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 712 | { "transfer_list", android::base::Join(transfer_list_fail, '\n') }, |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 713 | }; |
| 714 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 715 | // "2\nstash " + block3_hash + " 2,2,3" |
| 716 | std::string last_command_content = "2\n" + transfer_list_fail[kTransferListHeaderLines + 2]; |
| 717 | TemporaryFile last_command_file; |
| 718 | Paths::Get().set_last_command_file(last_command_file.path); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 719 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 720 | RunBlockImageUpdate(false, entries, image_file_, ""); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 721 | |
| 722 | // Expect last_command to contain the last stash command. |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 723 | std::string last_command_actual; |
| 724 | ASSERT_TRUE(android::base::ReadFileToString(last_command_file.path, &last_command_actual)); |
| 725 | EXPECT_EQ(last_command_content, last_command_actual); |
| 726 | |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 727 | std::string updated_contents; |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 728 | ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_contents)); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 729 | ASSERT_EQ(block1 + block1 + block3, updated_contents); |
| 730 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 731 | // "Resume" the update. Expect the first 'move' to be skipped but the second 'move' to be |
| 732 | // executed. Note that we intentionally reset the image file. |
| 733 | entries["transfer_list"] = android::base::Join(transfer_list_continue, '\n'); |
| 734 | ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_)); |
| 735 | RunBlockImageUpdate(false, entries, image_file_, "t"); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 736 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 737 | ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_contents)); |
| 738 | ASSERT_EQ(block1 + block2 + block1, updated_contents); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | TEST_F(UpdaterTest, last_command_update_unresumable) { |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 742 | std::string block1(4096, '1'); |
| 743 | std::string block2(4096, '2'); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 744 | std::string block1_hash = get_sha1(block1); |
| 745 | std::string block2_hash = get_sha1(block2); |
| 746 | |
| 747 | // Construct an unresumable update with source blocks mismatch. |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 748 | std::vector<std::string> transfer_list_unresumable{ |
| 749 | // clang-format off |
| 750 | "4", |
| 751 | "2", |
| 752 | "0", |
| 753 | "2", |
| 754 | "stash " + block1_hash + " 2,0,1", |
| 755 | "move " + block2_hash + " 2,1,2 1 2,0,1", |
| 756 | // clang-format on |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 757 | }; |
| 758 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 759 | PackageEntries entries{ |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 760 | { "new_data", "" }, |
| 761 | { "patch_data", "" }, |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 762 | { "transfer_list", android::base::Join(transfer_list_unresumable, '\n') }, |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 763 | }; |
| 764 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 765 | ASSERT_TRUE(android::base::WriteStringToFile(block1 + block1, image_file_)); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 766 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 767 | // Set up the last_command_file. |
| 768 | TemporaryFile last_command_file; |
| 769 | Paths::Get().set_last_command_file(last_command_file.path); |
| 770 | std::string last_command_content = "0\n" + transfer_list_unresumable[kTransferListHeaderLines]; |
| 771 | ASSERT_TRUE(android::base::WriteStringToFile(last_command_content, last_command_file.path)); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 772 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 773 | RunBlockImageUpdate(false, entries, image_file_, ""); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 774 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 775 | // The last_command_file will be deleted if the update encounters an unresumable failure later. |
| 776 | ASSERT_EQ(-1, access(last_command_file.path, R_OK)); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | TEST_F(UpdaterTest, last_command_verify) { |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 780 | std::string block1(4096, '1'); |
| 781 | std::string block2(4096, '2'); |
| 782 | std::string block3(4096, '3'); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 783 | std::string block1_hash = get_sha1(block1); |
| 784 | std::string block2_hash = get_sha1(block2); |
| 785 | std::string block3_hash = get_sha1(block3); |
| 786 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 787 | std::vector<std::string> transfer_list_verify{ |
| 788 | // clang-format off |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 789 | "4", |
| 790 | "2", |
| 791 | "0", |
| 792 | "2", |
| 793 | "stash " + block1_hash + " 2,0,1", |
| 794 | "move " + block1_hash + " 2,0,1 1 2,0,1", |
| 795 | "move " + block1_hash + " 2,1,2 1 2,0,1", |
| 796 | "stash " + block3_hash + " 2,2,3", |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 797 | // clang-format on |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 798 | }; |
| 799 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 800 | PackageEntries entries{ |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 801 | { "new_data", "" }, |
| 802 | { "patch_data", "" }, |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 803 | { "transfer_list", android::base::Join(transfer_list_verify, '\n') }, |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 804 | }; |
| 805 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 806 | ASSERT_TRUE(android::base::WriteStringToFile(block1 + block1 + block3, image_file_)); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 807 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 808 | TemporaryFile last_command_file; |
| 809 | Paths::Get().set_last_command_file(last_command_file.path); |
| 810 | // Last command: "move " + block1_hash + " 2,1,2 1 2,0,1" |
| 811 | std::string last_command_content = "2\n" + transfer_list_verify[kTransferListHeaderLines + 2]; |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 812 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 813 | // First run: expect the verification to succeed and the last_command_file is intact. |
| 814 | ASSERT_TRUE(android::base::WriteStringToFile(last_command_content, last_command_file.path)); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 815 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 816 | RunBlockImageUpdate(true, entries, image_file_, "t"); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 817 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 818 | std::string last_command_actual; |
| 819 | ASSERT_TRUE(android::base::ReadFileToString(last_command_file.path, &last_command_actual)); |
| 820 | EXPECT_EQ(last_command_content, last_command_actual); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 821 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 822 | // Second run with a mismatching block image: expect the verification to succeed but |
| 823 | // last_command_file to be deleted; because the target blocks in the last command don't have the |
| 824 | // expected contents for the second move command. |
| 825 | ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_)); |
| 826 | RunBlockImageUpdate(true, entries, image_file_, "t"); |
| 827 | ASSERT_EQ(-1, access(last_command_file.path, R_OK)); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 828 | } |