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