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 | 7064aa2 | 2018-05-24 21:43:50 -0700 | [diff] [blame] | 146 | // Each test is run in a separate process (isolated mode). Shared temporary files won't cause |
| 147 | // conflicts. |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 148 | Paths::Get().set_cache_temp_source(temp_saved_source_.path); |
Tao Bao | 7064aa2 | 2018-05-24 21:43:50 -0700 | [diff] [blame] | 149 | Paths::Get().set_last_command_file(temp_last_command_.path); |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 150 | Paths::Get().set_stash_directory_base(temp_stash_base_.path); |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 151 | |
Tao Bao | 7064aa2 | 2018-05-24 21:43:50 -0700 | [diff] [blame] | 152 | last_command_file_ = temp_last_command_.path; |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 153 | image_file_ = image_temp_file_.path; |
| 154 | } |
| 155 | |
| 156 | void TearDown() override { |
Tao Bao | 7064aa2 | 2018-05-24 21:43:50 -0700 | [diff] [blame] | 157 | // Clean up the last_command_file if any. |
| 158 | ASSERT_TRUE(android::base::RemoveFileIfExists(last_command_file_)); |
| 159 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 160 | // Clear partition updated marker if any. |
| 161 | std::string updated_marker{ temp_stash_base_.path }; |
| 162 | updated_marker += "/" + get_sha1(image_temp_file_.path) + ".UPDATED"; |
| 163 | ASSERT_TRUE(android::base::RemoveFileIfExists(updated_marker)); |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 164 | } |
Tianjie Xu | 3bbb20f | 2018-02-27 15:56:11 -0800 | [diff] [blame] | 165 | |
| 166 | TemporaryFile temp_saved_source_; |
Tianjie Xu | 3bbb20f | 2018-02-27 15:56:11 -0800 | [diff] [blame] | 167 | TemporaryDir temp_stash_base_; |
Tao Bao | 7064aa2 | 2018-05-24 21:43:50 -0700 | [diff] [blame] | 168 | std::string last_command_file_; |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 169 | std::string image_file_; |
| 170 | |
| 171 | private: |
Tao Bao | 7064aa2 | 2018-05-24 21:43:50 -0700 | [diff] [blame] | 172 | TemporaryFile temp_last_command_; |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 173 | TemporaryFile image_temp_file_; |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 174 | }; |
| 175 | |
| 176 | TEST_F(UpdaterTest, getprop) { |
| 177 | expect(android::base::GetProperty("ro.product.device", "").c_str(), |
| 178 | "getprop(\"ro.product.device\")", |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 179 | kNoCause); |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 180 | |
| 181 | expect(android::base::GetProperty("ro.build.fingerprint", "").c_str(), |
| 182 | "getprop(\"ro.build.fingerprint\")", |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 183 | kNoCause); |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 184 | |
| 185 | // getprop() accepts only one parameter. |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 186 | expect(nullptr, "getprop()", kArgsParsingFailure); |
| 187 | expect(nullptr, "getprop(\"arg1\", \"arg2\")", kArgsParsingFailure); |
| 188 | } |
| 189 | |
| 190 | TEST_F(UpdaterTest, sha1_check) { |
| 191 | // sha1_check(data) returns the SHA-1 of the data. |
| 192 | expect("81fe8bfe87576c3ecb22426f8e57847382917acf", "sha1_check(\"abcd\")", kNoCause); |
| 193 | expect("da39a3ee5e6b4b0d3255bfef95601890afd80709", "sha1_check(\"\")", kNoCause); |
| 194 | |
| 195 | // sha1_check(data, sha1_hex, [sha1_hex, ...]) returns the matched SHA-1. |
| 196 | expect("81fe8bfe87576c3ecb22426f8e57847382917acf", |
| 197 | "sha1_check(\"abcd\", \"81fe8bfe87576c3ecb22426f8e57847382917acf\")", |
| 198 | kNoCause); |
| 199 | |
| 200 | expect("81fe8bfe87576c3ecb22426f8e57847382917acf", |
| 201 | "sha1_check(\"abcd\", \"wrong_sha1\", \"81fe8bfe87576c3ecb22426f8e57847382917acf\")", |
| 202 | kNoCause); |
| 203 | |
| 204 | // Or "" if there's no match. |
| 205 | expect("", |
| 206 | "sha1_check(\"abcd\", \"wrong_sha1\")", |
| 207 | kNoCause); |
| 208 | |
| 209 | expect("", |
| 210 | "sha1_check(\"abcd\", \"wrong_sha1\", \"wrong_sha2\")", |
| 211 | kNoCause); |
| 212 | |
| 213 | // sha1_check() expects at least one argument. |
| 214 | expect(nullptr, "sha1_check()", kArgsParsingFailure); |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 215 | } |
Tao Bao | 51d516e | 2016-11-03 14:49:01 -0700 | [diff] [blame] | 216 | |
Tao Bao | db56eb0 | 2017-03-23 06:34:20 -0700 | [diff] [blame] | 217 | TEST_F(UpdaterTest, apply_patch_check) { |
| 218 | // Zero-argument is not valid. |
| 219 | expect(nullptr, "apply_patch_check()", kArgsParsingFailure); |
| 220 | |
| 221 | // File not found. |
| 222 | expect("", "apply_patch_check(\"/doesntexist\")", kNoCause); |
| 223 | |
| 224 | std::string src_file = from_testdata_base("old.file"); |
| 225 | std::string src_content; |
| 226 | ASSERT_TRUE(android::base::ReadFileToString(src_file, &src_content)); |
| 227 | size_t src_size = src_content.size(); |
| 228 | std::string src_hash = get_sha1(src_content); |
| 229 | |
| 230 | // One-argument with EMMC:file:size:sha1 should pass the check. |
| 231 | std::string filename = android::base::Join( |
| 232 | std::vector<std::string>{ "EMMC", src_file, std::to_string(src_size), src_hash }, ":"); |
| 233 | std::string cmd = "apply_patch_check(\"" + filename + "\")"; |
| 234 | expect("t", cmd.c_str(), kNoCause); |
| 235 | |
| 236 | // EMMC:file:(size-1):sha1:(size+1):sha1 should fail the check. |
| 237 | std::string filename_bad = android::base::Join( |
| 238 | std::vector<std::string>{ "EMMC", src_file, std::to_string(src_size - 1), src_hash, |
| 239 | std::to_string(src_size + 1), src_hash }, |
| 240 | ":"); |
| 241 | cmd = "apply_patch_check(\"" + filename_bad + "\")"; |
| 242 | expect("", cmd.c_str(), kNoCause); |
| 243 | |
| 244 | // EMMC:file:(size-1):sha1:size:sha1:(size+1):sha1 should pass the check. |
| 245 | filename_bad = |
| 246 | android::base::Join(std::vector<std::string>{ "EMMC", src_file, std::to_string(src_size - 1), |
| 247 | src_hash, std::to_string(src_size), src_hash, |
| 248 | std::to_string(src_size + 1), src_hash }, |
| 249 | ":"); |
| 250 | cmd = "apply_patch_check(\"" + filename_bad + "\")"; |
| 251 | expect("t", cmd.c_str(), kNoCause); |
| 252 | |
| 253 | // Multiple arguments. |
| 254 | cmd = "apply_patch_check(\"" + filename + "\", \"wrong_sha1\", \"wrong_sha2\")"; |
| 255 | expect("", cmd.c_str(), kNoCause); |
| 256 | |
| 257 | cmd = "apply_patch_check(\"" + filename + "\", \"wrong_sha1\", \"" + src_hash + |
| 258 | "\", \"wrong_sha2\")"; |
| 259 | expect("t", cmd.c_str(), kNoCause); |
| 260 | |
| 261 | cmd = "apply_patch_check(\"" + filename_bad + "\", \"wrong_sha1\", \"" + src_hash + |
| 262 | "\", \"wrong_sha2\")"; |
| 263 | expect("t", cmd.c_str(), kNoCause); |
| 264 | } |
| 265 | |
Tao Bao | 51d516e | 2016-11-03 14:49:01 -0700 | [diff] [blame] | 266 | TEST_F(UpdaterTest, file_getprop) { |
| 267 | // file_getprop() expects two arguments. |
| 268 | expect(nullptr, "file_getprop()", kArgsParsingFailure); |
| 269 | expect(nullptr, "file_getprop(\"arg1\")", kArgsParsingFailure); |
| 270 | expect(nullptr, "file_getprop(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure); |
| 271 | |
| 272 | // File doesn't exist. |
| 273 | expect(nullptr, "file_getprop(\"/doesntexist\", \"key1\")", kFileGetPropFailure); |
| 274 | |
| 275 | // Reject too large files (current limit = 65536). |
| 276 | TemporaryFile temp_file1; |
| 277 | std::string buffer(65540, '\0'); |
| 278 | ASSERT_TRUE(android::base::WriteStringToFile(buffer, temp_file1.path)); |
| 279 | |
| 280 | // Read some keys. |
| 281 | TemporaryFile temp_file2; |
| 282 | std::string content("ro.product.name=tardis\n" |
| 283 | "# comment\n\n\n" |
| 284 | "ro.product.model\n" |
| 285 | "ro.product.board = magic \n"); |
| 286 | ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file2.path)); |
| 287 | |
| 288 | std::string script1("file_getprop(\"" + std::string(temp_file2.path) + |
| 289 | "\", \"ro.product.name\")"); |
| 290 | expect("tardis", script1.c_str(), kNoCause); |
| 291 | |
| 292 | std::string script2("file_getprop(\"" + std::string(temp_file2.path) + |
| 293 | "\", \"ro.product.board\")"); |
| 294 | expect("magic", script2.c_str(), kNoCause); |
| 295 | |
| 296 | // No match. |
| 297 | std::string script3("file_getprop(\"" + std::string(temp_file2.path) + |
| 298 | "\", \"ro.product.wrong\")"); |
| 299 | expect("", script3.c_str(), kNoCause); |
| 300 | |
| 301 | std::string script4("file_getprop(\"" + std::string(temp_file2.path) + |
| 302 | "\", \"ro.product.name=\")"); |
| 303 | expect("", script4.c_str(), kNoCause); |
| 304 | |
| 305 | std::string script5("file_getprop(\"" + std::string(temp_file2.path) + |
| 306 | "\", \"ro.product.nam\")"); |
| 307 | expect("", script5.c_str(), kNoCause); |
| 308 | |
| 309 | std::string script6("file_getprop(\"" + std::string(temp_file2.path) + |
| 310 | "\", \"ro.product.model\")"); |
| 311 | expect("", script6.c_str(), kNoCause); |
| 312 | } |
Tao Bao | 0831d0b | 2016-11-03 23:25:04 -0700 | [diff] [blame] | 313 | |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 314 | // TODO: Test extracting to block device. |
| 315 | TEST_F(UpdaterTest, package_extract_file) { |
| 316 | // package_extract_file expects 1 or 2 arguments. |
| 317 | expect(nullptr, "package_extract_file()", kArgsParsingFailure); |
| 318 | expect(nullptr, "package_extract_file(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure); |
| 319 | |
| 320 | std::string zip_path = from_testdata_base("ziptest_valid.zip"); |
| 321 | ZipArchiveHandle handle; |
| 322 | ASSERT_EQ(0, OpenArchive(zip_path.c_str(), &handle)); |
| 323 | |
| 324 | // Need to set up the ziphandle. |
| 325 | UpdaterInfo updater_info; |
| 326 | updater_info.package_zip = handle; |
| 327 | |
| 328 | // Two-argument version. |
| 329 | TemporaryFile temp_file1; |
| 330 | std::string script("package_extract_file(\"a.txt\", \"" + std::string(temp_file1.path) + "\")"); |
| 331 | expect("t", script.c_str(), kNoCause, &updater_info); |
| 332 | |
| 333 | // Verify the extracted entry. |
| 334 | std::string data; |
| 335 | ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data)); |
| 336 | ASSERT_EQ(kATxtContents, data); |
| 337 | |
| 338 | // Now extract another entry to the same location, which should overwrite. |
| 339 | script = "package_extract_file(\"b.txt\", \"" + std::string(temp_file1.path) + "\")"; |
| 340 | expect("t", script.c_str(), kNoCause, &updater_info); |
| 341 | |
| 342 | ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data)); |
| 343 | ASSERT_EQ(kBTxtContents, data); |
| 344 | |
| 345 | // Missing zip entry. The two-argument version doesn't abort. |
| 346 | script = "package_extract_file(\"doesntexist\", \"" + std::string(temp_file1.path) + "\")"; |
| 347 | expect("", script.c_str(), kNoCause, &updater_info); |
| 348 | |
| 349 | // Extract to /dev/full should fail. |
| 350 | script = "package_extract_file(\"a.txt\", \"/dev/full\")"; |
| 351 | expect("", script.c_str(), kNoCause, &updater_info); |
| 352 | |
| 353 | // One-argument version. |
| 354 | script = "sha1_check(package_extract_file(\"a.txt\"))"; |
| 355 | expect(kATxtSha1Sum.c_str(), script.c_str(), kNoCause, &updater_info); |
| 356 | |
| 357 | script = "sha1_check(package_extract_file(\"b.txt\"))"; |
| 358 | expect(kBTxtSha1Sum.c_str(), script.c_str(), kNoCause, &updater_info); |
| 359 | |
| 360 | // Missing entry. The one-argument version aborts the evaluation. |
| 361 | script = "package_extract_file(\"doesntexist\")"; |
| 362 | expect(nullptr, script.c_str(), kPackageExtractFileFailure, &updater_info); |
| 363 | |
| 364 | CloseArchive(handle); |
| 365 | } |
Tao Bao | d0f3088 | 2016-11-03 23:52:01 -0700 | [diff] [blame] | 366 | |
| 367 | TEST_F(UpdaterTest, write_value) { |
| 368 | // write_value() expects two arguments. |
| 369 | expect(nullptr, "write_value()", kArgsParsingFailure); |
| 370 | expect(nullptr, "write_value(\"arg1\")", kArgsParsingFailure); |
| 371 | expect(nullptr, "write_value(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure); |
| 372 | |
| 373 | // filename cannot be empty. |
| 374 | expect(nullptr, "write_value(\"value\", \"\")", kArgsParsingFailure); |
| 375 | |
| 376 | // Write some value to file. |
| 377 | TemporaryFile temp_file; |
| 378 | std::string value = "magicvalue"; |
| 379 | std::string script("write_value(\"" + value + "\", \"" + std::string(temp_file.path) + "\")"); |
| 380 | expect("t", script.c_str(), kNoCause); |
| 381 | |
| 382 | // Verify the content. |
| 383 | std::string content; |
| 384 | ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content)); |
| 385 | ASSERT_EQ(value, content); |
| 386 | |
| 387 | // Allow writing empty string. |
| 388 | script = "write_value(\"\", \"" + std::string(temp_file.path) + "\")"; |
| 389 | expect("t", script.c_str(), kNoCause); |
| 390 | |
| 391 | // Verify the content. |
| 392 | ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content)); |
| 393 | ASSERT_EQ("", content); |
| 394 | |
| 395 | // It should fail gracefully when write fails. |
| 396 | script = "write_value(\"value\", \"/proc/0/file1\")"; |
| 397 | expect("", script.c_str(), kNoCause); |
| 398 | } |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 399 | |
| 400 | TEST_F(UpdaterTest, get_stage) { |
| 401 | // get_stage() expects one argument. |
| 402 | expect(nullptr, "get_stage()", kArgsParsingFailure); |
| 403 | expect(nullptr, "get_stage(\"arg1\", \"arg2\")", kArgsParsingFailure); |
| 404 | expect(nullptr, "get_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure); |
| 405 | |
| 406 | // Set up a local file as BCB. |
| 407 | TemporaryFile tf; |
| 408 | std::string temp_file(tf.path); |
| 409 | bootloader_message boot; |
| 410 | strlcpy(boot.stage, "2/3", sizeof(boot.stage)); |
| 411 | std::string err; |
| 412 | ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err)); |
| 413 | |
| 414 | // Can read the stage value. |
| 415 | std::string script("get_stage(\"" + temp_file + "\")"); |
| 416 | expect("2/3", script.c_str(), kNoCause); |
| 417 | |
| 418 | // Bad BCB path. |
| 419 | script = "get_stage(\"doesntexist\")"; |
| 420 | expect("", script.c_str(), kNoCause); |
| 421 | } |
| 422 | |
| 423 | TEST_F(UpdaterTest, set_stage) { |
| 424 | // set_stage() expects two arguments. |
| 425 | expect(nullptr, "set_stage()", kArgsParsingFailure); |
| 426 | expect(nullptr, "set_stage(\"arg1\")", kArgsParsingFailure); |
| 427 | expect(nullptr, "set_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure); |
| 428 | |
| 429 | // Set up a local file as BCB. |
| 430 | TemporaryFile tf; |
| 431 | std::string temp_file(tf.path); |
| 432 | bootloader_message boot; |
| 433 | strlcpy(boot.command, "command", sizeof(boot.command)); |
| 434 | strlcpy(boot.stage, "2/3", sizeof(boot.stage)); |
| 435 | std::string err; |
| 436 | ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err)); |
| 437 | |
| 438 | // Write with set_stage(). |
| 439 | std::string script("set_stage(\"" + temp_file + "\", \"1/3\")"); |
| 440 | expect(tf.path, script.c_str(), kNoCause); |
| 441 | |
| 442 | // Verify. |
| 443 | bootloader_message boot_verify; |
| 444 | ASSERT_TRUE(read_bootloader_message_from(&boot_verify, temp_file, &err)); |
| 445 | |
| 446 | // Stage should be updated, with command part untouched. |
| 447 | ASSERT_STREQ("1/3", boot_verify.stage); |
| 448 | ASSERT_STREQ(boot.command, boot_verify.command); |
| 449 | |
| 450 | // Bad BCB path. |
| 451 | script = "set_stage(\"doesntexist\", \"1/3\")"; |
| 452 | expect("", script.c_str(), kNoCause); |
| 453 | |
| 454 | script = "set_stage(\"/dev/full\", \"1/3\")"; |
| 455 | expect("", script.c_str(), kNoCause); |
| 456 | } |
Tao Bao | 9aa7ab5 | 2017-01-05 17:27:19 -0800 | [diff] [blame] | 457 | |
| 458 | TEST_F(UpdaterTest, set_progress) { |
| 459 | // set_progress() expects one argument. |
| 460 | expect(nullptr, "set_progress()", kArgsParsingFailure); |
| 461 | expect(nullptr, "set_progress(\"arg1\", \"arg2\")", kArgsParsingFailure); |
| 462 | |
| 463 | // Invalid progress argument. |
| 464 | expect(nullptr, "set_progress(\"arg1\")", kArgsParsingFailure); |
| 465 | expect(nullptr, "set_progress(\"3x+5\")", kArgsParsingFailure); |
| 466 | expect(nullptr, "set_progress(\".3.5\")", kArgsParsingFailure); |
| 467 | |
| 468 | TemporaryFile tf; |
| 469 | UpdaterInfo updater_info; |
Tianjie Xu | 79327ac | 2017-09-08 17:09:10 -0700 | [diff] [blame] | 470 | updater_info.cmd_pipe = fdopen(tf.release(), "w"); |
Tao Bao | 9aa7ab5 | 2017-01-05 17:27:19 -0800 | [diff] [blame] | 471 | expect(".52", "set_progress(\".52\")", kNoCause, &updater_info); |
| 472 | fflush(updater_info.cmd_pipe); |
| 473 | |
| 474 | std::string cmd; |
| 475 | ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd)); |
| 476 | ASSERT_EQ(android::base::StringPrintf("set_progress %f\n", .52), cmd); |
| 477 | // recovery-updater protocol expects 2 tokens ("set_progress <frac>"). |
| 478 | ASSERT_EQ(2U, android::base::Split(cmd, " ").size()); |
Tianjie Xu | 79327ac | 2017-09-08 17:09:10 -0700 | [diff] [blame] | 479 | ASSERT_EQ(0, fclose(updater_info.cmd_pipe)); |
Tao Bao | 9aa7ab5 | 2017-01-05 17:27:19 -0800 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | TEST_F(UpdaterTest, show_progress) { |
| 483 | // show_progress() expects two arguments. |
| 484 | expect(nullptr, "show_progress()", kArgsParsingFailure); |
| 485 | expect(nullptr, "show_progress(\"arg1\")", kArgsParsingFailure); |
| 486 | expect(nullptr, "show_progress(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure); |
| 487 | |
| 488 | // Invalid progress arguments. |
| 489 | expect(nullptr, "show_progress(\"arg1\", \"arg2\")", kArgsParsingFailure); |
| 490 | expect(nullptr, "show_progress(\"3x+5\", \"10\")", kArgsParsingFailure); |
| 491 | expect(nullptr, "show_progress(\".3\", \"5a\")", kArgsParsingFailure); |
| 492 | |
| 493 | TemporaryFile tf; |
| 494 | UpdaterInfo updater_info; |
Tianjie Xu | 79327ac | 2017-09-08 17:09:10 -0700 | [diff] [blame] | 495 | updater_info.cmd_pipe = fdopen(tf.release(), "w"); |
Tao Bao | 9aa7ab5 | 2017-01-05 17:27:19 -0800 | [diff] [blame] | 496 | expect(".52", "show_progress(\".52\", \"10\")", kNoCause, &updater_info); |
| 497 | fflush(updater_info.cmd_pipe); |
| 498 | |
| 499 | std::string cmd; |
| 500 | ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd)); |
| 501 | ASSERT_EQ(android::base::StringPrintf("progress %f %d\n", .52, 10), cmd); |
| 502 | // recovery-updater protocol expects 3 tokens ("progress <frac> <secs>"). |
| 503 | ASSERT_EQ(3U, android::base::Split(cmd, " ").size()); |
Tianjie Xu | 79327ac | 2017-09-08 17:09:10 -0700 | [diff] [blame] | 504 | ASSERT_EQ(0, fclose(updater_info.cmd_pipe)); |
Tao Bao | 9aa7ab5 | 2017-01-05 17:27:19 -0800 | [diff] [blame] | 505 | } |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 506 | |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 507 | TEST_F(UpdaterTest, block_image_update_patch_data) { |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 508 | std::string src_content = std::string(4096, 'a') + std::string(4096, 'c'); |
| 509 | std::string tgt_content = std::string(4096, 'b') + std::string(4096, 'd'); |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 510 | |
| 511 | // Generate the patch data. |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 512 | TemporaryFile patch_file; |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 513 | ASSERT_EQ(0, |
| 514 | bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(src_content.data()), src_content.size(), |
| 515 | reinterpret_cast<const uint8_t*>(tgt_content.data()), tgt_content.size(), |
| 516 | patch_file.path, nullptr)); |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 517 | std::string patch_content; |
| 518 | ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch_content)); |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 519 | |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 520 | // Create the transfer list that contains a bsdiff. |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 521 | std::string src_hash = get_sha1(src_content); |
| 522 | std::string tgt_hash = get_sha1(tgt_content); |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 523 | std::vector<std::string> transfer_list{ |
| 524 | // clang-format off |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 525 | "4", |
| 526 | "2", |
| 527 | "0", |
| 528 | "2", |
| 529 | "stash " + src_hash + " 2,0,2", |
| 530 | android::base::StringPrintf("bsdiff 0 %zu %s %s 2,0,2 2 - %s:2,0,2", patch_content.size(), |
| 531 | src_hash.c_str(), tgt_hash.c_str(), src_hash.c_str()), |
| 532 | "free " + src_hash, |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 533 | // clang-format on |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 534 | }; |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 535 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 536 | PackageEntries entries{ |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 537 | { "new_data", "" }, |
| 538 | { "patch_data", patch_content }, |
| 539 | { "transfer_list", android::base::Join(transfer_list, '\n') }, |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 540 | }; |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 541 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 542 | ASSERT_TRUE(android::base::WriteStringToFile(src_content, image_file_)); |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 543 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 544 | RunBlockImageUpdate(false, entries, image_file_, "t"); |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 545 | |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 546 | // The update_file should be patched correctly. |
| 547 | std::string updated_content; |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 548 | ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_content)); |
| 549 | ASSERT_EQ(tgt_content, updated_content); |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | TEST_F(UpdaterTest, block_image_update_fail) { |
| 553 | std::string src_content(4096 * 2, 'e'); |
| 554 | std::string src_hash = get_sha1(src_content); |
| 555 | // Stash and free some blocks, then fail the update intentionally. |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 556 | std::vector<std::string> transfer_list{ |
| 557 | // clang-format off |
| 558 | "4", |
| 559 | "2", |
| 560 | "0", |
| 561 | "2", |
| 562 | "stash " + src_hash + " 2,0,2", |
| 563 | "free " + src_hash, |
| 564 | "fail", |
| 565 | // clang-format on |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 566 | }; |
| 567 | |
| 568 | // Add a new data of 10 bytes to test the deadlock. |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 569 | PackageEntries entries{ |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 570 | { "new_data", std::string(10, 0) }, |
| 571 | { "patch_data", "" }, |
| 572 | { "transfer_list", android::base::Join(transfer_list, '\n') }, |
| 573 | }; |
| 574 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 575 | ASSERT_TRUE(android::base::WriteStringToFile(src_content, image_file_)); |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 576 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 577 | RunBlockImageUpdate(false, entries, image_file_, ""); |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 578 | |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 579 | // Updater generates the stash name based on the input file name. |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 580 | std::string name_digest = get_sha1(image_file_); |
Tianjie Xu | 3bbb20f | 2018-02-27 15:56:11 -0800 | [diff] [blame] | 581 | std::string stash_base = std::string(temp_stash_base_.path) + "/" + name_digest; |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 582 | ASSERT_EQ(0, access(stash_base.c_str(), F_OK)); |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 583 | // Expect the stashed blocks to be freed. |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 584 | ASSERT_EQ(-1, access((stash_base + src_hash).c_str(), F_OK)); |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 585 | ASSERT_EQ(0, rmdir(stash_base.c_str())); |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 586 | } |
Tianjie Xu | 3a8d98d | 2017-04-03 20:01:17 -0700 | [diff] [blame] | 587 | |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 588 | TEST_F(UpdaterTest, new_data_over_write) { |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 589 | std::vector<std::string> transfer_list{ |
| 590 | // clang-format off |
Tianjie Xu | 3a8d98d | 2017-04-03 20:01:17 -0700 | [diff] [blame] | 591 | "4", |
| 592 | "1", |
| 593 | "0", |
| 594 | "0", |
| 595 | "new 2,0,1", |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 596 | // clang-format on |
Tianjie Xu | 3a8d98d | 2017-04-03 20:01:17 -0700 | [diff] [blame] | 597 | }; |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 598 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 599 | // Write 4096 + 100 bytes of new data. |
| 600 | PackageEntries entries{ |
| 601 | { "new_data", std::string(4196, 0) }, |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 602 | { "patch_data", "" }, |
| 603 | { "transfer_list", android::base::Join(transfer_list, '\n') }, |
| 604 | }; |
| 605 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 606 | RunBlockImageUpdate(false, entries, image_file_, "t"); |
| 607 | } |
Tianjie Xu | 3a8d98d | 2017-04-03 20:01:17 -0700 | [diff] [blame] | 608 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 609 | TEST_F(UpdaterTest, new_data_short_write) { |
| 610 | std::vector<std::string> transfer_list{ |
| 611 | // clang-format off |
| 612 | "4", |
| 613 | "1", |
| 614 | "0", |
| 615 | "0", |
| 616 | "new 2,0,1", |
| 617 | // clang-format on |
| 618 | }; |
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 | PackageEntries entries{ |
| 621 | { "patch_data", "" }, |
| 622 | { "transfer_list", android::base::Join(transfer_list, '\n') }, |
| 623 | }; |
Tianjie Xu | 3a8d98d | 2017-04-03 20:01:17 -0700 | [diff] [blame] | 624 | |
| 625 | // Updater should report the failure gracefully rather than stuck in deadlock. |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 626 | entries["new_data"] = ""; |
| 627 | RunBlockImageUpdate(false, entries, image_file_, ""); |
Tianjie Xu | 3a8d98d | 2017-04-03 20:01:17 -0700 | [diff] [blame] | 628 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 629 | entries["new_data"] = std::string(10, 'a'); |
| 630 | RunBlockImageUpdate(false, entries, image_file_, ""); |
Tianjie Xu | 3a8d98d | 2017-04-03 20:01:17 -0700 | [diff] [blame] | 631 | |
| 632 | // Expect to write 1 block of new data successfully. |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 633 | entries["new_data"] = std::string(4096, 'a'); |
| 634 | RunBlockImageUpdate(false, entries, image_file_, "t"); |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | TEST_F(UpdaterTest, brotli_new_data) { |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 638 | auto generator = []() { return rand() % 128; }; |
Tianjie Xu | 6ed175d | 2017-07-18 11:29:40 -0700 | [diff] [blame] | 639 | // Generate 100 blocks of random data. |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 640 | std::string brotli_new_data; |
Tianjie Xu | 6ed175d | 2017-07-18 11:29:40 -0700 | [diff] [blame] | 641 | brotli_new_data.reserve(4096 * 100); |
| 642 | generate_n(back_inserter(brotli_new_data), 4096 * 100, generator); |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 643 | |
| 644 | size_t encoded_size = BrotliEncoderMaxCompressedSize(brotli_new_data.size()); |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 645 | std::string encoded_data(encoded_size, 0); |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 646 | ASSERT_TRUE(BrotliEncoderCompress( |
| 647 | 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] | 648 | reinterpret_cast<const uint8_t*>(brotli_new_data.data()), &encoded_size, |
| 649 | reinterpret_cast<uint8_t*>(const_cast<char*>(encoded_data.data())))); |
| 650 | encoded_data.resize(encoded_size); |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 651 | |
Tianjie Xu | 6ed175d | 2017-07-18 11:29:40 -0700 | [diff] [blame] | 652 | // Write a few small chunks of new data, then a large chunk, and finally a few small chunks. |
| 653 | // This helps us to catch potential short writes. |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 654 | std::vector<std::string> transfer_list = { |
Tianjie Xu | 6ed175d | 2017-07-18 11:29:40 -0700 | [diff] [blame] | 655 | "4", |
| 656 | "100", |
| 657 | "0", |
| 658 | "0", |
| 659 | "new 2,0,1", |
| 660 | "new 2,1,2", |
| 661 | "new 4,2,50,50,97", |
| 662 | "new 2,97,98", |
| 663 | "new 2,98,99", |
| 664 | "new 2,99,100", |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 665 | }; |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 666 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 667 | PackageEntries entries{ |
| 668 | { "new_data.br", std::move(encoded_data) }, |
Tianjie Xu | 5450c84 | 2017-10-18 13:15:21 -0700 | [diff] [blame] | 669 | { "patch_data", "" }, |
| 670 | { "transfer_list", android::base::Join(transfer_list, '\n') }, |
| 671 | }; |
| 672 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 673 | RunBlockImageUpdate(false, entries, image_file_, "t"); |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 674 | |
| 675 | std::string updated_content; |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 676 | ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_content)); |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 677 | ASSERT_EQ(brotli_new_data, updated_content); |
Tianjie Xu | 3a8d98d | 2017-04-03 20:01:17 -0700 | [diff] [blame] | 678 | } |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 679 | |
| 680 | TEST_F(UpdaterTest, last_command_update) { |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 681 | std::string block1(4096, '1'); |
| 682 | std::string block2(4096, '2'); |
| 683 | std::string block3(4096, '3'); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 684 | std::string block1_hash = get_sha1(block1); |
| 685 | std::string block2_hash = get_sha1(block2); |
| 686 | std::string block3_hash = get_sha1(block3); |
| 687 | |
| 688 | // Compose the transfer list to fail the first update. |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 689 | std::vector<std::string> transfer_list_fail{ |
| 690 | // clang-format off |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 691 | "4", |
| 692 | "2", |
| 693 | "0", |
| 694 | "2", |
| 695 | "stash " + block1_hash + " 2,0,1", |
| 696 | "move " + block1_hash + " 2,1,2 1 2,0,1", |
| 697 | "stash " + block3_hash + " 2,2,3", |
| 698 | "fail", |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 699 | // clang-format on |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 700 | }; |
| 701 | |
| 702 | // Mimic a resumed update with the same transfer commands. |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 703 | std::vector<std::string> transfer_list_continue{ |
| 704 | // clang-format off |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 705 | "4", |
| 706 | "2", |
| 707 | "0", |
| 708 | "2", |
| 709 | "stash " + block1_hash + " 2,0,1", |
| 710 | "move " + block1_hash + " 2,1,2 1 2,0,1", |
| 711 | "stash " + block3_hash + " 2,2,3", |
| 712 | "move " + block1_hash + " 2,2,3 1 2,0,1", |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 713 | // clang-format on |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 714 | }; |
| 715 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 716 | ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_)); |
| 717 | |
| 718 | PackageEntries entries{ |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 719 | { "new_data", "" }, |
| 720 | { "patch_data", "" }, |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 721 | { "transfer_list", android::base::Join(transfer_list_fail, '\n') }, |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 722 | }; |
| 723 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 724 | // "2\nstash " + block3_hash + " 2,2,3" |
| 725 | std::string last_command_content = "2\n" + transfer_list_fail[kTransferListHeaderLines + 2]; |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 726 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 727 | RunBlockImageUpdate(false, entries, image_file_, ""); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 728 | |
| 729 | // Expect last_command to contain the last stash command. |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 730 | std::string last_command_actual; |
Tao Bao | 7064aa2 | 2018-05-24 21:43:50 -0700 | [diff] [blame] | 731 | ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual)); |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 732 | EXPECT_EQ(last_command_content, last_command_actual); |
| 733 | |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 734 | std::string updated_contents; |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 735 | ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_contents)); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 736 | ASSERT_EQ(block1 + block1 + block3, updated_contents); |
| 737 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 738 | // "Resume" the update. Expect the first 'move' to be skipped but the second 'move' to be |
| 739 | // executed. Note that we intentionally reset the image file. |
| 740 | entries["transfer_list"] = android::base::Join(transfer_list_continue, '\n'); |
| 741 | ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_)); |
| 742 | RunBlockImageUpdate(false, entries, image_file_, "t"); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 743 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 744 | ASSERT_TRUE(android::base::ReadFileToString(image_file_, &updated_contents)); |
| 745 | ASSERT_EQ(block1 + block2 + block1, updated_contents); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | TEST_F(UpdaterTest, last_command_update_unresumable) { |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 749 | std::string block1(4096, '1'); |
| 750 | std::string block2(4096, '2'); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 751 | std::string block1_hash = get_sha1(block1); |
| 752 | std::string block2_hash = get_sha1(block2); |
| 753 | |
| 754 | // Construct an unresumable update with source blocks mismatch. |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 755 | std::vector<std::string> transfer_list_unresumable{ |
| 756 | // clang-format off |
| 757 | "4", |
| 758 | "2", |
| 759 | "0", |
| 760 | "2", |
| 761 | "stash " + block1_hash + " 2,0,1", |
| 762 | "move " + block2_hash + " 2,1,2 1 2,0,1", |
| 763 | // clang-format on |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 764 | }; |
| 765 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 766 | PackageEntries entries{ |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 767 | { "new_data", "" }, |
| 768 | { "patch_data", "" }, |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 769 | { "transfer_list", android::base::Join(transfer_list_unresumable, '\n') }, |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 770 | }; |
| 771 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 772 | ASSERT_TRUE(android::base::WriteStringToFile(block1 + block1, image_file_)); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 773 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 774 | std::string last_command_content = "0\n" + transfer_list_unresumable[kTransferListHeaderLines]; |
Tao Bao | 7064aa2 | 2018-05-24 21:43:50 -0700 | [diff] [blame] | 775 | ASSERT_TRUE(android::base::WriteStringToFile(last_command_content, last_command_file_)); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 776 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 777 | RunBlockImageUpdate(false, entries, image_file_, ""); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 778 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 779 | // 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] | 780 | ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK)); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | TEST_F(UpdaterTest, last_command_verify) { |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 784 | std::string block1(4096, '1'); |
| 785 | std::string block2(4096, '2'); |
| 786 | std::string block3(4096, '3'); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 787 | std::string block1_hash = get_sha1(block1); |
| 788 | std::string block2_hash = get_sha1(block2); |
| 789 | std::string block3_hash = get_sha1(block3); |
| 790 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 791 | std::vector<std::string> transfer_list_verify{ |
| 792 | // clang-format off |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 793 | "4", |
| 794 | "2", |
| 795 | "0", |
| 796 | "2", |
| 797 | "stash " + block1_hash + " 2,0,1", |
| 798 | "move " + block1_hash + " 2,0,1 1 2,0,1", |
| 799 | "move " + block1_hash + " 2,1,2 1 2,0,1", |
| 800 | "stash " + block3_hash + " 2,2,3", |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 801 | // clang-format on |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 802 | }; |
| 803 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 804 | PackageEntries entries{ |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 805 | { "new_data", "" }, |
| 806 | { "patch_data", "" }, |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 807 | { "transfer_list", android::base::Join(transfer_list_verify, '\n') }, |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 808 | }; |
| 809 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 810 | ASSERT_TRUE(android::base::WriteStringToFile(block1 + block1 + block3, image_file_)); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 811 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 812 | // Last command: "move " + block1_hash + " 2,1,2 1 2,0,1" |
| 813 | std::string last_command_content = "2\n" + transfer_list_verify[kTransferListHeaderLines + 2]; |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 814 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 815 | // 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] | 816 | ASSERT_TRUE(android::base::WriteStringToFile(last_command_content, last_command_file_)); |
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 | RunBlockImageUpdate(true, entries, image_file_, "t"); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 819 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 820 | std::string last_command_actual; |
Tao Bao | 7064aa2 | 2018-05-24 21:43:50 -0700 | [diff] [blame] | 821 | ASSERT_TRUE(android::base::ReadFileToString(last_command_file_, &last_command_actual)); |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 822 | EXPECT_EQ(last_command_content, last_command_actual); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 823 | |
Tao Bao | bc4a6d5 | 2018-05-23 00:12:21 -0700 | [diff] [blame] | 824 | // Second run with a mismatching block image: expect the verification to succeed but |
| 825 | // last_command_file to be deleted; because the target blocks in the last command don't have the |
| 826 | // expected contents for the second move command. |
| 827 | ASSERT_TRUE(android::base::WriteStringToFile(block1 + block2 + block3, image_file_)); |
| 828 | RunBlockImageUpdate(true, entries, image_file_, "t"); |
Tao Bao | 7064aa2 | 2018-05-24 21:43:50 -0700 | [diff] [blame] | 829 | ASSERT_EQ(-1, access(last_command_file_.c_str(), R_OK)); |
Tianjie Xu | 284752e | 2017-12-05 11:04:17 -0800 | [diff] [blame] | 830 | } |