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 | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 26 | #include <vector> |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 27 | |
Tao Bao | 51d516e | 2016-11-03 14:49:01 -0700 | [diff] [blame] | 28 | #include <android-base/file.h> |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 29 | #include <android-base/properties.h> |
Tao Bao | 9aa7ab5 | 2017-01-05 17:27:19 -0800 | [diff] [blame] | 30 | #include <android-base/stringprintf.h> |
| 31 | #include <android-base/strings.h> |
Tao Bao | 51d516e | 2016-11-03 14:49:01 -0700 | [diff] [blame] | 32 | #include <android-base/test_utils.h> |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 33 | #include <bootloader_message/bootloader_message.h> |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 34 | #include <brotli/encode.h> |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 35 | #include <bsdiff.h> |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 36 | #include <gtest/gtest.h> |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 37 | #include <ziparchive/zip_archive.h> |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 38 | #include <ziparchive/zip_writer.h> |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 39 | |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 40 | #include "common/test_constants.h" |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 41 | #include "edify/expr.h" |
| 42 | #include "error_code.h" |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 43 | #include "otautil/SysUtil.h" |
| 44 | #include "print_sha1.h" |
| 45 | #include "updater/blockimg.h" |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 46 | #include "updater/install.h" |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 47 | #include "updater/updater.h" |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 48 | |
| 49 | struct selabel_handle *sehandle = nullptr; |
| 50 | |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 51 | static void expect(const char* expected, const char* expr_str, CauseCode cause_code, |
| 52 | UpdaterInfo* info = nullptr) { |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 53 | std::unique_ptr<Expr> e; |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 54 | int error_count = 0; |
| 55 | ASSERT_EQ(0, parse_string(expr_str, &e, &error_count)); |
| 56 | ASSERT_EQ(0, error_count); |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 57 | |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 58 | State state(expr_str, info); |
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 | std::string result; |
| 61 | bool status = Evaluate(&state, e, &result); |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 62 | |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 63 | if (expected == nullptr) { |
| 64 | ASSERT_FALSE(status); |
| 65 | } else { |
| 66 | ASSERT_TRUE(status); |
| 67 | ASSERT_STREQ(expected, result.c_str()); |
| 68 | } |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 69 | |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 70 | // Error code is set in updater/updater.cpp only, by parsing State.errmsg. |
| 71 | ASSERT_EQ(kNoError, state.error_code); |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 72 | |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 73 | // Cause code should always be available. |
| 74 | ASSERT_EQ(cause_code, state.cause_code); |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 77 | static std::string get_sha1(const std::string& content) { |
| 78 | uint8_t digest[SHA_DIGEST_LENGTH]; |
| 79 | SHA1(reinterpret_cast<const uint8_t*>(content.c_str()), content.size(), digest); |
| 80 | return print_sha1(digest); |
| 81 | } |
| 82 | |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 83 | class UpdaterTest : public ::testing::Test { |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 84 | protected: |
| 85 | virtual void SetUp() override { |
| 86 | RegisterBuiltins(); |
| 87 | RegisterInstallFunctions(); |
| 88 | RegisterBlockImageFunctions(); |
| 89 | } |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | TEST_F(UpdaterTest, getprop) { |
| 93 | expect(android::base::GetProperty("ro.product.device", "").c_str(), |
| 94 | "getprop(\"ro.product.device\")", |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 95 | kNoCause); |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 96 | |
| 97 | expect(android::base::GetProperty("ro.build.fingerprint", "").c_str(), |
| 98 | "getprop(\"ro.build.fingerprint\")", |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 99 | kNoCause); |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 100 | |
| 101 | // getprop() accepts only one parameter. |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 102 | expect(nullptr, "getprop()", kArgsParsingFailure); |
| 103 | expect(nullptr, "getprop(\"arg1\", \"arg2\")", kArgsParsingFailure); |
| 104 | } |
| 105 | |
| 106 | TEST_F(UpdaterTest, sha1_check) { |
| 107 | // sha1_check(data) returns the SHA-1 of the data. |
| 108 | expect("81fe8bfe87576c3ecb22426f8e57847382917acf", "sha1_check(\"abcd\")", kNoCause); |
| 109 | expect("da39a3ee5e6b4b0d3255bfef95601890afd80709", "sha1_check(\"\")", kNoCause); |
| 110 | |
| 111 | // sha1_check(data, sha1_hex, [sha1_hex, ...]) returns the matched SHA-1. |
| 112 | expect("81fe8bfe87576c3ecb22426f8e57847382917acf", |
| 113 | "sha1_check(\"abcd\", \"81fe8bfe87576c3ecb22426f8e57847382917acf\")", |
| 114 | kNoCause); |
| 115 | |
| 116 | expect("81fe8bfe87576c3ecb22426f8e57847382917acf", |
| 117 | "sha1_check(\"abcd\", \"wrong_sha1\", \"81fe8bfe87576c3ecb22426f8e57847382917acf\")", |
| 118 | kNoCause); |
| 119 | |
| 120 | // Or "" if there's no match. |
| 121 | expect("", |
| 122 | "sha1_check(\"abcd\", \"wrong_sha1\")", |
| 123 | kNoCause); |
| 124 | |
| 125 | expect("", |
| 126 | "sha1_check(\"abcd\", \"wrong_sha1\", \"wrong_sha2\")", |
| 127 | kNoCause); |
| 128 | |
| 129 | // sha1_check() expects at least one argument. |
| 130 | expect(nullptr, "sha1_check()", kArgsParsingFailure); |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 131 | } |
Tao Bao | 51d516e | 2016-11-03 14:49:01 -0700 | [diff] [blame] | 132 | |
Tao Bao | db56eb0 | 2017-03-23 06:34:20 -0700 | [diff] [blame] | 133 | TEST_F(UpdaterTest, apply_patch_check) { |
| 134 | // Zero-argument is not valid. |
| 135 | expect(nullptr, "apply_patch_check()", kArgsParsingFailure); |
| 136 | |
| 137 | // File not found. |
| 138 | expect("", "apply_patch_check(\"/doesntexist\")", kNoCause); |
| 139 | |
| 140 | std::string src_file = from_testdata_base("old.file"); |
| 141 | std::string src_content; |
| 142 | ASSERT_TRUE(android::base::ReadFileToString(src_file, &src_content)); |
| 143 | size_t src_size = src_content.size(); |
| 144 | std::string src_hash = get_sha1(src_content); |
| 145 | |
| 146 | // One-argument with EMMC:file:size:sha1 should pass the check. |
| 147 | std::string filename = android::base::Join( |
| 148 | std::vector<std::string>{ "EMMC", src_file, std::to_string(src_size), src_hash }, ":"); |
| 149 | std::string cmd = "apply_patch_check(\"" + filename + "\")"; |
| 150 | expect("t", cmd.c_str(), kNoCause); |
| 151 | |
| 152 | // EMMC:file:(size-1):sha1:(size+1):sha1 should fail the check. |
| 153 | std::string filename_bad = android::base::Join( |
| 154 | std::vector<std::string>{ "EMMC", src_file, std::to_string(src_size - 1), src_hash, |
| 155 | std::to_string(src_size + 1), src_hash }, |
| 156 | ":"); |
| 157 | cmd = "apply_patch_check(\"" + filename_bad + "\")"; |
| 158 | expect("", cmd.c_str(), kNoCause); |
| 159 | |
| 160 | // EMMC:file:(size-1):sha1:size:sha1:(size+1):sha1 should pass the check. |
| 161 | filename_bad = |
| 162 | android::base::Join(std::vector<std::string>{ "EMMC", src_file, std::to_string(src_size - 1), |
| 163 | src_hash, std::to_string(src_size), src_hash, |
| 164 | std::to_string(src_size + 1), src_hash }, |
| 165 | ":"); |
| 166 | cmd = "apply_patch_check(\"" + filename_bad + "\")"; |
| 167 | expect("t", cmd.c_str(), kNoCause); |
| 168 | |
| 169 | // Multiple arguments. |
| 170 | cmd = "apply_patch_check(\"" + filename + "\", \"wrong_sha1\", \"wrong_sha2\")"; |
| 171 | expect("", cmd.c_str(), kNoCause); |
| 172 | |
| 173 | cmd = "apply_patch_check(\"" + filename + "\", \"wrong_sha1\", \"" + src_hash + |
| 174 | "\", \"wrong_sha2\")"; |
| 175 | expect("t", cmd.c_str(), kNoCause); |
| 176 | |
| 177 | cmd = "apply_patch_check(\"" + filename_bad + "\", \"wrong_sha1\", \"" + src_hash + |
| 178 | "\", \"wrong_sha2\")"; |
| 179 | expect("t", cmd.c_str(), kNoCause); |
| 180 | } |
| 181 | |
Tao Bao | 51d516e | 2016-11-03 14:49:01 -0700 | [diff] [blame] | 182 | TEST_F(UpdaterTest, file_getprop) { |
| 183 | // file_getprop() expects two arguments. |
| 184 | expect(nullptr, "file_getprop()", kArgsParsingFailure); |
| 185 | expect(nullptr, "file_getprop(\"arg1\")", kArgsParsingFailure); |
| 186 | expect(nullptr, "file_getprop(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure); |
| 187 | |
| 188 | // File doesn't exist. |
| 189 | expect(nullptr, "file_getprop(\"/doesntexist\", \"key1\")", kFileGetPropFailure); |
| 190 | |
| 191 | // Reject too large files (current limit = 65536). |
| 192 | TemporaryFile temp_file1; |
| 193 | std::string buffer(65540, '\0'); |
| 194 | ASSERT_TRUE(android::base::WriteStringToFile(buffer, temp_file1.path)); |
| 195 | |
| 196 | // Read some keys. |
| 197 | TemporaryFile temp_file2; |
| 198 | std::string content("ro.product.name=tardis\n" |
| 199 | "# comment\n\n\n" |
| 200 | "ro.product.model\n" |
| 201 | "ro.product.board = magic \n"); |
| 202 | ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file2.path)); |
| 203 | |
| 204 | std::string script1("file_getprop(\"" + std::string(temp_file2.path) + |
| 205 | "\", \"ro.product.name\")"); |
| 206 | expect("tardis", script1.c_str(), kNoCause); |
| 207 | |
| 208 | std::string script2("file_getprop(\"" + std::string(temp_file2.path) + |
| 209 | "\", \"ro.product.board\")"); |
| 210 | expect("magic", script2.c_str(), kNoCause); |
| 211 | |
| 212 | // No match. |
| 213 | std::string script3("file_getprop(\"" + std::string(temp_file2.path) + |
| 214 | "\", \"ro.product.wrong\")"); |
| 215 | expect("", script3.c_str(), kNoCause); |
| 216 | |
| 217 | std::string script4("file_getprop(\"" + std::string(temp_file2.path) + |
| 218 | "\", \"ro.product.name=\")"); |
| 219 | expect("", script4.c_str(), kNoCause); |
| 220 | |
| 221 | std::string script5("file_getprop(\"" + std::string(temp_file2.path) + |
| 222 | "\", \"ro.product.nam\")"); |
| 223 | expect("", script5.c_str(), kNoCause); |
| 224 | |
| 225 | std::string script6("file_getprop(\"" + std::string(temp_file2.path) + |
| 226 | "\", \"ro.product.model\")"); |
| 227 | expect("", script6.c_str(), kNoCause); |
| 228 | } |
Tao Bao | 0831d0b | 2016-11-03 23:25:04 -0700 | [diff] [blame] | 229 | |
Tom Marshall | bf4f24f | 2017-08-23 18:14:00 +0000 | [diff] [blame^] | 230 | TEST_F(UpdaterTest, delete) { |
| 231 | // Delete none. |
| 232 | expect("0", "delete()", kNoCause); |
| 233 | expect("0", "delete(\"/doesntexist\")", kNoCause); |
| 234 | expect("0", "delete(\"/doesntexist1\", \"/doesntexist2\")", kNoCause); |
| 235 | expect("0", "delete(\"/doesntexist1\", \"/doesntexist2\", \"/doesntexist3\")", kNoCause); |
| 236 | |
| 237 | // Delete one file. |
| 238 | TemporaryFile temp_file1; |
| 239 | ASSERT_TRUE(android::base::WriteStringToFile("abc", temp_file1.path)); |
| 240 | std::string script1("delete(\"" + std::string(temp_file1.path) + "\")"); |
| 241 | expect("1", script1.c_str(), kNoCause); |
| 242 | |
| 243 | // Delete two files. |
| 244 | TemporaryFile temp_file2; |
| 245 | ASSERT_TRUE(android::base::WriteStringToFile("abc", temp_file2.path)); |
| 246 | TemporaryFile temp_file3; |
| 247 | ASSERT_TRUE(android::base::WriteStringToFile("abc", temp_file3.path)); |
| 248 | std::string script2("delete(\"" + std::string(temp_file2.path) + "\", \"" + |
| 249 | std::string(temp_file3.path) + "\")"); |
| 250 | expect("2", script2.c_str(), kNoCause); |
| 251 | |
| 252 | // Delete already deleted files. |
| 253 | expect("0", script2.c_str(), kNoCause); |
| 254 | |
| 255 | // Delete one out of three. |
| 256 | TemporaryFile temp_file4; |
| 257 | ASSERT_TRUE(android::base::WriteStringToFile("abc", temp_file4.path)); |
| 258 | std::string script3("delete(\"/doesntexist1\", \"" + std::string(temp_file4.path) + |
| 259 | "\", \"/doesntexist2\")"); |
| 260 | expect("1", script3.c_str(), kNoCause); |
| 261 | } |
| 262 | |
| 263 | TEST_F(UpdaterTest, rename) { |
| 264 | // rename() expects two arguments. |
| 265 | expect(nullptr, "rename()", kArgsParsingFailure); |
| 266 | expect(nullptr, "rename(\"arg1\")", kArgsParsingFailure); |
| 267 | expect(nullptr, "rename(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure); |
| 268 | |
| 269 | // src_name or dst_name cannot be empty. |
| 270 | expect(nullptr, "rename(\"\", \"arg2\")", kArgsParsingFailure); |
| 271 | expect(nullptr, "rename(\"arg1\", \"\")", kArgsParsingFailure); |
| 272 | |
| 273 | // File doesn't exist (both of src and dst). |
| 274 | expect(nullptr, "rename(\"/doesntexist\", \"/doesntexisteither\")" , kFileRenameFailure); |
| 275 | |
| 276 | // Can't create parent directory. |
| 277 | TemporaryFile temp_file1; |
| 278 | ASSERT_TRUE(android::base::WriteStringToFile("abc", temp_file1.path)); |
| 279 | std::string script1("rename(\"" + std::string(temp_file1.path) + "\", \"/proc/0/file1\")"); |
| 280 | expect(nullptr, script1.c_str(), kFileRenameFailure); |
| 281 | |
| 282 | // Rename. |
| 283 | TemporaryFile temp_file2; |
| 284 | std::string script2("rename(\"" + std::string(temp_file1.path) + "\", \"" + |
| 285 | std::string(temp_file2.path) + "\")"); |
| 286 | expect(temp_file2.path, script2.c_str(), kNoCause); |
| 287 | |
| 288 | // Already renamed. |
| 289 | expect(temp_file2.path, script2.c_str(), kNoCause); |
| 290 | |
| 291 | // Parents create successfully. |
| 292 | TemporaryFile temp_file3; |
| 293 | TemporaryDir td; |
| 294 | std::string temp_dir(td.path); |
| 295 | std::string dst_file = temp_dir + "/aaa/bbb/a.txt"; |
| 296 | std::string script3("rename(\"" + std::string(temp_file3.path) + "\", \"" + dst_file + "\")"); |
| 297 | expect(dst_file.c_str(), script3.c_str(), kNoCause); |
| 298 | |
| 299 | // Clean up the temp files under td. |
| 300 | ASSERT_EQ(0, unlink(dst_file.c_str())); |
| 301 | ASSERT_EQ(0, rmdir((temp_dir + "/aaa/bbb").c_str())); |
| 302 | ASSERT_EQ(0, rmdir((temp_dir + "/aaa").c_str())); |
| 303 | } |
| 304 | |
| 305 | TEST_F(UpdaterTest, symlink) { |
| 306 | // symlink expects 1+ argument. |
| 307 | expect(nullptr, "symlink()", kArgsParsingFailure); |
| 308 | |
| 309 | // symlink should fail if src is an empty string. |
| 310 | TemporaryFile temp_file1; |
| 311 | std::string script1("symlink(\"" + std::string(temp_file1.path) + "\", \"\")"); |
| 312 | expect(nullptr, script1.c_str(), kSymlinkFailure); |
| 313 | |
| 314 | std::string script2("symlink(\"" + std::string(temp_file1.path) + "\", \"src1\", \"\")"); |
| 315 | expect(nullptr, script2.c_str(), kSymlinkFailure); |
| 316 | |
| 317 | // symlink failed to remove old src. |
| 318 | std::string script3("symlink(\"" + std::string(temp_file1.path) + "\", \"/proc\")"); |
| 319 | expect(nullptr, script3.c_str(), kSymlinkFailure); |
| 320 | |
| 321 | // symlink can create symlinks. |
| 322 | TemporaryFile temp_file; |
| 323 | std::string content = "magicvalue"; |
| 324 | ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file.path)); |
| 325 | |
| 326 | TemporaryDir td; |
| 327 | std::string src1 = std::string(td.path) + "/symlink1"; |
| 328 | std::string src2 = std::string(td.path) + "/symlink2"; |
| 329 | std::string script4("symlink(\"" + std::string(temp_file.path) + "\", \"" + |
| 330 | src1 + "\", \"" + src2 + "\")"); |
| 331 | expect("t", script4.c_str(), kNoCause); |
| 332 | |
| 333 | // Verify the created symlinks. |
| 334 | struct stat sb; |
| 335 | ASSERT_TRUE(lstat(src1.c_str(), &sb) == 0 && S_ISLNK(sb.st_mode)); |
| 336 | ASSERT_TRUE(lstat(src2.c_str(), &sb) == 0 && S_ISLNK(sb.st_mode)); |
| 337 | |
| 338 | // Clean up the leftovers. |
| 339 | ASSERT_EQ(0, unlink(src1.c_str())); |
| 340 | ASSERT_EQ(0, unlink(src2.c_str())); |
| 341 | } |
| 342 | |
Tom Marshall | 2c1a579 | 2017-12-14 22:37:17 +0100 | [diff] [blame] | 343 | TEST_F(UpdaterTest, package_extract_dir) { |
| 344 | // package_extract_dir expects 2 arguments. |
| 345 | expect(nullptr, "package_extract_dir()", kArgsParsingFailure); |
| 346 | expect(nullptr, "package_extract_dir(\"arg1\")", kArgsParsingFailure); |
| 347 | expect(nullptr, "package_extract_dir(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure); |
| 348 | |
| 349 | std::string zip_path = from_testdata_base("ziptest_valid.zip"); |
| 350 | ZipArchiveHandle handle; |
| 351 | ASSERT_EQ(0, OpenArchive(zip_path.c_str(), &handle)); |
| 352 | |
| 353 | // Need to set up the ziphandle. |
| 354 | UpdaterInfo updater_info; |
| 355 | updater_info.package_zip = handle; |
| 356 | |
| 357 | // Extract "b/c.txt" and "b/d.txt" with package_extract_dir("b", "<dir>"). |
| 358 | TemporaryDir td; |
| 359 | std::string temp_dir(td.path); |
| 360 | std::string script("package_extract_dir(\"b\", \"" + temp_dir + "\")"); |
| 361 | expect("t", script.c_str(), kNoCause, &updater_info); |
| 362 | |
| 363 | // Verify. |
| 364 | std::string data; |
| 365 | std::string file_c = temp_dir + "/c.txt"; |
| 366 | ASSERT_TRUE(android::base::ReadFileToString(file_c, &data)); |
| 367 | ASSERT_EQ(kCTxtContents, data); |
| 368 | |
| 369 | std::string file_d = temp_dir + "/d.txt"; |
| 370 | ASSERT_TRUE(android::base::ReadFileToString(file_d, &data)); |
| 371 | ASSERT_EQ(kDTxtContents, data); |
| 372 | |
| 373 | // Modify the contents in order to retry. It's expected to be overwritten. |
| 374 | ASSERT_TRUE(android::base::WriteStringToFile("random", file_c)); |
| 375 | ASSERT_TRUE(android::base::WriteStringToFile("random", file_d)); |
| 376 | |
| 377 | // Extract again and verify. |
| 378 | expect("t", script.c_str(), kNoCause, &updater_info); |
| 379 | |
| 380 | ASSERT_TRUE(android::base::ReadFileToString(file_c, &data)); |
| 381 | ASSERT_EQ(kCTxtContents, data); |
| 382 | ASSERT_TRUE(android::base::ReadFileToString(file_d, &data)); |
| 383 | ASSERT_EQ(kDTxtContents, data); |
| 384 | |
| 385 | // Clean up the temp files under td. |
| 386 | ASSERT_EQ(0, unlink(file_c.c_str())); |
| 387 | ASSERT_EQ(0, unlink(file_d.c_str())); |
| 388 | |
| 389 | // Extracting "b/" (with slash) should give the same result. |
| 390 | script = "package_extract_dir(\"b/\", \"" + temp_dir + "\")"; |
| 391 | expect("t", script.c_str(), kNoCause, &updater_info); |
| 392 | |
| 393 | ASSERT_TRUE(android::base::ReadFileToString(file_c, &data)); |
| 394 | ASSERT_EQ(kCTxtContents, data); |
| 395 | ASSERT_TRUE(android::base::ReadFileToString(file_d, &data)); |
| 396 | ASSERT_EQ(kDTxtContents, data); |
| 397 | |
| 398 | ASSERT_EQ(0, unlink(file_c.c_str())); |
| 399 | ASSERT_EQ(0, unlink(file_d.c_str())); |
| 400 | |
| 401 | // Extracting "" is allowed. The entries will carry the path name. |
| 402 | script = "package_extract_dir(\"\", \"" + temp_dir + "\")"; |
| 403 | expect("t", script.c_str(), kNoCause, &updater_info); |
| 404 | |
| 405 | std::string file_a = temp_dir + "/a.txt"; |
| 406 | ASSERT_TRUE(android::base::ReadFileToString(file_a, &data)); |
| 407 | ASSERT_EQ(kATxtContents, data); |
| 408 | std::string file_b = temp_dir + "/b.txt"; |
| 409 | ASSERT_TRUE(android::base::ReadFileToString(file_b, &data)); |
| 410 | ASSERT_EQ(kBTxtContents, data); |
| 411 | std::string file_b_c = temp_dir + "/b/c.txt"; |
| 412 | ASSERT_TRUE(android::base::ReadFileToString(file_b_c, &data)); |
| 413 | ASSERT_EQ(kCTxtContents, data); |
| 414 | std::string file_b_d = temp_dir + "/b/d.txt"; |
| 415 | ASSERT_TRUE(android::base::ReadFileToString(file_b_d, &data)); |
| 416 | ASSERT_EQ(kDTxtContents, data); |
| 417 | |
| 418 | ASSERT_EQ(0, unlink(file_a.c_str())); |
| 419 | ASSERT_EQ(0, unlink(file_b.c_str())); |
| 420 | ASSERT_EQ(0, unlink(file_b_c.c_str())); |
| 421 | ASSERT_EQ(0, unlink(file_b_d.c_str())); |
| 422 | ASSERT_EQ(0, rmdir((temp_dir + "/b").c_str())); |
| 423 | |
| 424 | // Extracting non-existent entry should still give "t". |
| 425 | script = "package_extract_dir(\"doesntexist\", \"" + temp_dir + "\")"; |
| 426 | expect("t", script.c_str(), kNoCause, &updater_info); |
| 427 | |
| 428 | // Only relative zip_path is allowed. |
| 429 | script = "package_extract_dir(\"/b\", \"" + temp_dir + "\")"; |
| 430 | expect("", script.c_str(), kNoCause, &updater_info); |
| 431 | |
| 432 | // Only absolute dest_path is allowed. |
| 433 | script = "package_extract_dir(\"b\", \"path\")"; |
| 434 | expect("", script.c_str(), kNoCause, &updater_info); |
| 435 | |
| 436 | CloseArchive(handle); |
| 437 | } |
| 438 | |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 439 | // TODO: Test extracting to block device. |
| 440 | TEST_F(UpdaterTest, package_extract_file) { |
| 441 | // package_extract_file expects 1 or 2 arguments. |
| 442 | expect(nullptr, "package_extract_file()", kArgsParsingFailure); |
| 443 | expect(nullptr, "package_extract_file(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure); |
| 444 | |
| 445 | std::string zip_path = from_testdata_base("ziptest_valid.zip"); |
| 446 | ZipArchiveHandle handle; |
| 447 | ASSERT_EQ(0, OpenArchive(zip_path.c_str(), &handle)); |
| 448 | |
| 449 | // Need to set up the ziphandle. |
| 450 | UpdaterInfo updater_info; |
| 451 | updater_info.package_zip = handle; |
| 452 | |
| 453 | // Two-argument version. |
| 454 | TemporaryFile temp_file1; |
| 455 | std::string script("package_extract_file(\"a.txt\", \"" + std::string(temp_file1.path) + "\")"); |
| 456 | expect("t", script.c_str(), kNoCause, &updater_info); |
| 457 | |
| 458 | // Verify the extracted entry. |
| 459 | std::string data; |
| 460 | ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data)); |
| 461 | ASSERT_EQ(kATxtContents, data); |
| 462 | |
| 463 | // Now extract another entry to the same location, which should overwrite. |
| 464 | script = "package_extract_file(\"b.txt\", \"" + std::string(temp_file1.path) + "\")"; |
| 465 | expect("t", script.c_str(), kNoCause, &updater_info); |
| 466 | |
| 467 | ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data)); |
| 468 | ASSERT_EQ(kBTxtContents, data); |
| 469 | |
| 470 | // Missing zip entry. The two-argument version doesn't abort. |
| 471 | script = "package_extract_file(\"doesntexist\", \"" + std::string(temp_file1.path) + "\")"; |
| 472 | expect("", script.c_str(), kNoCause, &updater_info); |
| 473 | |
| 474 | // Extract to /dev/full should fail. |
| 475 | script = "package_extract_file(\"a.txt\", \"/dev/full\")"; |
| 476 | expect("", script.c_str(), kNoCause, &updater_info); |
| 477 | |
| 478 | // One-argument version. |
| 479 | script = "sha1_check(package_extract_file(\"a.txt\"))"; |
| 480 | expect(kATxtSha1Sum.c_str(), script.c_str(), kNoCause, &updater_info); |
| 481 | |
| 482 | script = "sha1_check(package_extract_file(\"b.txt\"))"; |
| 483 | expect(kBTxtSha1Sum.c_str(), script.c_str(), kNoCause, &updater_info); |
| 484 | |
| 485 | // Missing entry. The one-argument version aborts the evaluation. |
| 486 | script = "package_extract_file(\"doesntexist\")"; |
| 487 | expect(nullptr, script.c_str(), kPackageExtractFileFailure, &updater_info); |
| 488 | |
| 489 | CloseArchive(handle); |
| 490 | } |
Tao Bao | d0f3088 | 2016-11-03 23:52:01 -0700 | [diff] [blame] | 491 | |
| 492 | TEST_F(UpdaterTest, write_value) { |
| 493 | // write_value() expects two arguments. |
| 494 | expect(nullptr, "write_value()", kArgsParsingFailure); |
| 495 | expect(nullptr, "write_value(\"arg1\")", kArgsParsingFailure); |
| 496 | expect(nullptr, "write_value(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure); |
| 497 | |
| 498 | // filename cannot be empty. |
| 499 | expect(nullptr, "write_value(\"value\", \"\")", kArgsParsingFailure); |
| 500 | |
| 501 | // Write some value to file. |
| 502 | TemporaryFile temp_file; |
| 503 | std::string value = "magicvalue"; |
| 504 | std::string script("write_value(\"" + value + "\", \"" + std::string(temp_file.path) + "\")"); |
| 505 | expect("t", script.c_str(), kNoCause); |
| 506 | |
| 507 | // Verify the content. |
| 508 | std::string content; |
| 509 | ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content)); |
| 510 | ASSERT_EQ(value, content); |
| 511 | |
| 512 | // Allow writing empty string. |
| 513 | script = "write_value(\"\", \"" + std::string(temp_file.path) + "\")"; |
| 514 | expect("t", script.c_str(), kNoCause); |
| 515 | |
| 516 | // Verify the content. |
| 517 | ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content)); |
| 518 | ASSERT_EQ("", content); |
| 519 | |
| 520 | // It should fail gracefully when write fails. |
| 521 | script = "write_value(\"value\", \"/proc/0/file1\")"; |
| 522 | expect("", script.c_str(), kNoCause); |
| 523 | } |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 524 | |
| 525 | TEST_F(UpdaterTest, get_stage) { |
| 526 | // get_stage() expects one argument. |
| 527 | expect(nullptr, "get_stage()", kArgsParsingFailure); |
| 528 | expect(nullptr, "get_stage(\"arg1\", \"arg2\")", kArgsParsingFailure); |
| 529 | expect(nullptr, "get_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure); |
| 530 | |
| 531 | // Set up a local file as BCB. |
| 532 | TemporaryFile tf; |
| 533 | std::string temp_file(tf.path); |
| 534 | bootloader_message boot; |
| 535 | strlcpy(boot.stage, "2/3", sizeof(boot.stage)); |
| 536 | std::string err; |
| 537 | ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err)); |
| 538 | |
| 539 | // Can read the stage value. |
| 540 | std::string script("get_stage(\"" + temp_file + "\")"); |
| 541 | expect("2/3", script.c_str(), kNoCause); |
| 542 | |
| 543 | // Bad BCB path. |
| 544 | script = "get_stage(\"doesntexist\")"; |
| 545 | expect("", script.c_str(), kNoCause); |
| 546 | } |
| 547 | |
| 548 | TEST_F(UpdaterTest, set_stage) { |
| 549 | // set_stage() expects two arguments. |
| 550 | expect(nullptr, "set_stage()", kArgsParsingFailure); |
| 551 | expect(nullptr, "set_stage(\"arg1\")", kArgsParsingFailure); |
| 552 | expect(nullptr, "set_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure); |
| 553 | |
| 554 | // Set up a local file as BCB. |
| 555 | TemporaryFile tf; |
| 556 | std::string temp_file(tf.path); |
| 557 | bootloader_message boot; |
| 558 | strlcpy(boot.command, "command", sizeof(boot.command)); |
| 559 | strlcpy(boot.stage, "2/3", sizeof(boot.stage)); |
| 560 | std::string err; |
| 561 | ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err)); |
| 562 | |
| 563 | // Write with set_stage(). |
| 564 | std::string script("set_stage(\"" + temp_file + "\", \"1/3\")"); |
| 565 | expect(tf.path, script.c_str(), kNoCause); |
| 566 | |
| 567 | // Verify. |
| 568 | bootloader_message boot_verify; |
| 569 | ASSERT_TRUE(read_bootloader_message_from(&boot_verify, temp_file, &err)); |
| 570 | |
| 571 | // Stage should be updated, with command part untouched. |
| 572 | ASSERT_STREQ("1/3", boot_verify.stage); |
| 573 | ASSERT_STREQ(boot.command, boot_verify.command); |
| 574 | |
| 575 | // Bad BCB path. |
| 576 | script = "set_stage(\"doesntexist\", \"1/3\")"; |
| 577 | expect("", script.c_str(), kNoCause); |
| 578 | |
| 579 | script = "set_stage(\"/dev/full\", \"1/3\")"; |
| 580 | expect("", script.c_str(), kNoCause); |
| 581 | } |
Tao Bao | 9aa7ab5 | 2017-01-05 17:27:19 -0800 | [diff] [blame] | 582 | |
| 583 | TEST_F(UpdaterTest, set_progress) { |
| 584 | // set_progress() expects one argument. |
| 585 | expect(nullptr, "set_progress()", kArgsParsingFailure); |
| 586 | expect(nullptr, "set_progress(\"arg1\", \"arg2\")", kArgsParsingFailure); |
| 587 | |
| 588 | // Invalid progress argument. |
| 589 | expect(nullptr, "set_progress(\"arg1\")", kArgsParsingFailure); |
| 590 | expect(nullptr, "set_progress(\"3x+5\")", kArgsParsingFailure); |
| 591 | expect(nullptr, "set_progress(\".3.5\")", kArgsParsingFailure); |
| 592 | |
| 593 | TemporaryFile tf; |
| 594 | UpdaterInfo updater_info; |
| 595 | updater_info.cmd_pipe = fdopen(tf.fd, "w"); |
| 596 | expect(".52", "set_progress(\".52\")", kNoCause, &updater_info); |
| 597 | fflush(updater_info.cmd_pipe); |
| 598 | |
| 599 | std::string cmd; |
| 600 | ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd)); |
| 601 | ASSERT_EQ(android::base::StringPrintf("set_progress %f\n", .52), cmd); |
| 602 | // recovery-updater protocol expects 2 tokens ("set_progress <frac>"). |
| 603 | ASSERT_EQ(2U, android::base::Split(cmd, " ").size()); |
| 604 | } |
| 605 | |
| 606 | TEST_F(UpdaterTest, show_progress) { |
| 607 | // show_progress() expects two arguments. |
| 608 | expect(nullptr, "show_progress()", kArgsParsingFailure); |
| 609 | expect(nullptr, "show_progress(\"arg1\")", kArgsParsingFailure); |
| 610 | expect(nullptr, "show_progress(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure); |
| 611 | |
| 612 | // Invalid progress arguments. |
| 613 | expect(nullptr, "show_progress(\"arg1\", \"arg2\")", kArgsParsingFailure); |
| 614 | expect(nullptr, "show_progress(\"3x+5\", \"10\")", kArgsParsingFailure); |
| 615 | expect(nullptr, "show_progress(\".3\", \"5a\")", kArgsParsingFailure); |
| 616 | |
| 617 | TemporaryFile tf; |
| 618 | UpdaterInfo updater_info; |
| 619 | updater_info.cmd_pipe = fdopen(tf.fd, "w"); |
| 620 | expect(".52", "show_progress(\".52\", \"10\")", kNoCause, &updater_info); |
| 621 | fflush(updater_info.cmd_pipe); |
| 622 | |
| 623 | std::string cmd; |
| 624 | ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd)); |
| 625 | ASSERT_EQ(android::base::StringPrintf("progress %f %d\n", .52, 10), cmd); |
| 626 | // recovery-updater protocol expects 3 tokens ("progress <frac> <secs>"). |
| 627 | ASSERT_EQ(3U, android::base::Split(cmd, " ").size()); |
| 628 | } |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 629 | |
| 630 | TEST_F(UpdaterTest, block_image_update) { |
| 631 | // Create a zip file with new_data and patch_data. |
| 632 | TemporaryFile zip_file; |
| 633 | FILE* zip_file_ptr = fdopen(zip_file.fd, "wb"); |
| 634 | ZipWriter zip_writer(zip_file_ptr); |
| 635 | |
| 636 | // Add a dummy new data. |
| 637 | ASSERT_EQ(0, zip_writer.StartEntry("new_data", 0)); |
| 638 | ASSERT_EQ(0, zip_writer.FinishEntry()); |
| 639 | |
| 640 | // Generate and add the patch data. |
| 641 | std::string src_content = std::string(4096, 'a') + std::string(4096, 'c'); |
| 642 | std::string tgt_content = std::string(4096, 'b') + std::string(4096, 'd'); |
| 643 | TemporaryFile patch_file; |
| 644 | ASSERT_EQ(0, bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(src_content.data()), |
| 645 | src_content.size(), reinterpret_cast<const uint8_t*>(tgt_content.data()), |
| 646 | tgt_content.size(), patch_file.path, nullptr)); |
| 647 | std::string patch_content; |
| 648 | ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch_content)); |
| 649 | ASSERT_EQ(0, zip_writer.StartEntry("patch_data", 0)); |
| 650 | ASSERT_EQ(0, zip_writer.WriteBytes(patch_content.data(), patch_content.size())); |
| 651 | ASSERT_EQ(0, zip_writer.FinishEntry()); |
| 652 | |
| 653 | // Add two transfer lists. The first one contains a bsdiff; and we expect the update to succeed. |
| 654 | std::string src_hash = get_sha1(src_content); |
| 655 | std::string tgt_hash = get_sha1(tgt_content); |
| 656 | std::vector<std::string> transfer_list = { |
| 657 | "4", |
| 658 | "2", |
| 659 | "0", |
| 660 | "2", |
| 661 | "stash " + src_hash + " 2,0,2", |
| 662 | android::base::StringPrintf("bsdiff 0 %zu %s %s 2,0,2 2 - %s:2,0,2", patch_content.size(), |
| 663 | src_hash.c_str(), tgt_hash.c_str(), src_hash.c_str()), |
| 664 | "free " + src_hash, |
| 665 | }; |
| 666 | ASSERT_EQ(0, zip_writer.StartEntry("transfer_list", 0)); |
| 667 | std::string commands = android::base::Join(transfer_list, '\n'); |
| 668 | ASSERT_EQ(0, zip_writer.WriteBytes(commands.data(), commands.size())); |
| 669 | ASSERT_EQ(0, zip_writer.FinishEntry()); |
| 670 | |
| 671 | // Stash and free some blocks, then fail the 2nd update intentionally. |
| 672 | std::vector<std::string> fail_transfer_list = { |
| 673 | "4", |
| 674 | "2", |
| 675 | "0", |
| 676 | "2", |
| 677 | "stash " + tgt_hash + " 2,0,2", |
| 678 | "free " + tgt_hash, |
| 679 | "fail", |
| 680 | }; |
| 681 | ASSERT_EQ(0, zip_writer.StartEntry("fail_transfer_list", 0)); |
| 682 | std::string fail_commands = android::base::Join(fail_transfer_list, '\n'); |
| 683 | ASSERT_EQ(0, zip_writer.WriteBytes(fail_commands.data(), fail_commands.size())); |
| 684 | ASSERT_EQ(0, zip_writer.FinishEntry()); |
| 685 | ASSERT_EQ(0, zip_writer.Finish()); |
| 686 | ASSERT_EQ(0, fclose(zip_file_ptr)); |
| 687 | |
| 688 | MemMapping map; |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 689 | ASSERT_TRUE(map.MapFile(zip_file.path)); |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 690 | ZipArchiveHandle handle; |
| 691 | ASSERT_EQ(0, OpenArchiveFromMemory(map.addr, map.length, zip_file.path, &handle)); |
| 692 | |
| 693 | // Set up the handler, command_pipe, patch offset & length. |
| 694 | UpdaterInfo updater_info; |
| 695 | updater_info.package_zip = handle; |
| 696 | TemporaryFile temp_pipe; |
Tianjie Xu | de6735e | 2017-07-10 15:13:33 -0700 | [diff] [blame] | 697 | updater_info.cmd_pipe = fopen(temp_pipe.path, "wbe"); |
Tianjie Xu | 56ebe62 | 2017-03-16 00:48:21 -0700 | [diff] [blame] | 698 | updater_info.package_zip_addr = map.addr; |
| 699 | updater_info.package_zip_len = map.length; |
| 700 | |
| 701 | // Execute the commands in the 1st transfer list. |
| 702 | TemporaryFile update_file; |
| 703 | ASSERT_TRUE(android::base::WriteStringToFile(src_content, update_file.path)); |
| 704 | std::string script = "block_image_update(\"" + std::string(update_file.path) + |
| 705 | R"(", package_extract_file("transfer_list"), "new_data", "patch_data"))"; |
| 706 | expect("t", script.c_str(), kNoCause, &updater_info); |
| 707 | // The update_file should be patched correctly. |
| 708 | std::string updated_content; |
| 709 | ASSERT_TRUE(android::base::ReadFileToString(update_file.path, &updated_content)); |
| 710 | ASSERT_EQ(tgt_hash, get_sha1(updated_content)); |
| 711 | |
| 712 | // Expect the 2nd update to fail, but expect the stashed blocks to be freed. |
| 713 | script = "block_image_update(\"" + std::string(update_file.path) + |
| 714 | R"(", package_extract_file("fail_transfer_list"), "new_data", "patch_data"))"; |
| 715 | expect("", script.c_str(), kNoCause, &updater_info); |
| 716 | // Updater generates the stash name based on the input file name. |
| 717 | std::string name_digest = get_sha1(update_file.path); |
| 718 | std::string stash_base = "/cache/recovery/" + name_digest; |
| 719 | ASSERT_EQ(0, access(stash_base.c_str(), F_OK)); |
| 720 | ASSERT_EQ(-1, access((stash_base + tgt_hash).c_str(), F_OK)); |
| 721 | ASSERT_EQ(0, rmdir(stash_base.c_str())); |
| 722 | |
| 723 | ASSERT_EQ(0, fclose(updater_info.cmd_pipe)); |
| 724 | CloseArchive(handle); |
| 725 | } |
Tianjie Xu | 3a8d98d | 2017-04-03 20:01:17 -0700 | [diff] [blame] | 726 | |
| 727 | TEST_F(UpdaterTest, new_data_short_write) { |
| 728 | // Create a zip file with new_data. |
| 729 | TemporaryFile zip_file; |
| 730 | FILE* zip_file_ptr = fdopen(zip_file.fd, "wb"); |
| 731 | ZipWriter zip_writer(zip_file_ptr); |
| 732 | |
| 733 | // Add the empty new data. |
| 734 | ASSERT_EQ(0, zip_writer.StartEntry("empty_new_data", 0)); |
| 735 | ASSERT_EQ(0, zip_writer.FinishEntry()); |
| 736 | // Add the short written new data. |
| 737 | ASSERT_EQ(0, zip_writer.StartEntry("short_new_data", 0)); |
| 738 | std::string new_data_short = std::string(10, 'a'); |
| 739 | ASSERT_EQ(0, zip_writer.WriteBytes(new_data_short.data(), new_data_short.size())); |
| 740 | ASSERT_EQ(0, zip_writer.FinishEntry()); |
| 741 | // Add the data of exactly one block. |
| 742 | ASSERT_EQ(0, zip_writer.StartEntry("exact_new_data", 0)); |
| 743 | std::string new_data_exact = std::string(4096, 'a'); |
| 744 | ASSERT_EQ(0, zip_writer.WriteBytes(new_data_exact.data(), new_data_exact.size())); |
| 745 | ASSERT_EQ(0, zip_writer.FinishEntry()); |
| 746 | // Add a dummy patch data. |
| 747 | ASSERT_EQ(0, zip_writer.StartEntry("patch_data", 0)); |
| 748 | ASSERT_EQ(0, zip_writer.FinishEntry()); |
| 749 | |
| 750 | std::vector<std::string> transfer_list = { |
| 751 | "4", |
| 752 | "1", |
| 753 | "0", |
| 754 | "0", |
| 755 | "new 2,0,1", |
| 756 | }; |
| 757 | ASSERT_EQ(0, zip_writer.StartEntry("transfer_list", 0)); |
| 758 | std::string commands = android::base::Join(transfer_list, '\n'); |
| 759 | ASSERT_EQ(0, zip_writer.WriteBytes(commands.data(), commands.size())); |
| 760 | ASSERT_EQ(0, zip_writer.FinishEntry()); |
| 761 | ASSERT_EQ(0, zip_writer.Finish()); |
| 762 | ASSERT_EQ(0, fclose(zip_file_ptr)); |
| 763 | |
| 764 | MemMapping map; |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 765 | ASSERT_TRUE(map.MapFile(zip_file.path)); |
Tianjie Xu | 3a8d98d | 2017-04-03 20:01:17 -0700 | [diff] [blame] | 766 | ZipArchiveHandle handle; |
| 767 | ASSERT_EQ(0, OpenArchiveFromMemory(map.addr, map.length, zip_file.path, &handle)); |
| 768 | |
| 769 | // Set up the handler, command_pipe, patch offset & length. |
| 770 | UpdaterInfo updater_info; |
| 771 | updater_info.package_zip = handle; |
| 772 | TemporaryFile temp_pipe; |
Tianjie Xu | de6735e | 2017-07-10 15:13:33 -0700 | [diff] [blame] | 773 | updater_info.cmd_pipe = fopen(temp_pipe.path, "wbe"); |
Tianjie Xu | 3a8d98d | 2017-04-03 20:01:17 -0700 | [diff] [blame] | 774 | updater_info.package_zip_addr = map.addr; |
| 775 | updater_info.package_zip_len = map.length; |
| 776 | |
| 777 | // Updater should report the failure gracefully rather than stuck in deadlock. |
| 778 | TemporaryFile update_file; |
| 779 | std::string script_empty_data = "block_image_update(\"" + std::string(update_file.path) + |
| 780 | R"(", package_extract_file("transfer_list"), "empty_new_data", "patch_data"))"; |
| 781 | expect("", script_empty_data.c_str(), kNoCause, &updater_info); |
| 782 | |
| 783 | std::string script_short_data = "block_image_update(\"" + std::string(update_file.path) + |
| 784 | R"(", package_extract_file("transfer_list"), "short_new_data", "patch_data"))"; |
| 785 | expect("", script_short_data.c_str(), kNoCause, &updater_info); |
| 786 | |
| 787 | // Expect to write 1 block of new data successfully. |
| 788 | std::string script_exact_data = "block_image_update(\"" + std::string(update_file.path) + |
| 789 | R"(", package_extract_file("transfer_list"), "exact_new_data", "patch_data"))"; |
| 790 | expect("t", script_exact_data.c_str(), kNoCause, &updater_info); |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 791 | CloseArchive(handle); |
| 792 | } |
| 793 | |
| 794 | TEST_F(UpdaterTest, brotli_new_data) { |
| 795 | // Create a zip file with new_data. |
| 796 | TemporaryFile zip_file; |
| 797 | FILE* zip_file_ptr = fdopen(zip_file.fd, "wb"); |
| 798 | ZipWriter zip_writer(zip_file_ptr); |
| 799 | |
| 800 | // Add a brotli compressed new data entry. |
| 801 | ASSERT_EQ(0, zip_writer.StartEntry("new.dat.br", 0)); |
| 802 | |
| 803 | auto generator = []() { return rand() % 128; }; |
Tianjie Xu | d9759e8 | 2017-07-21 21:06:52 +0000 | [diff] [blame] | 804 | // Generate 100 blocks of random data. |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 805 | std::string brotli_new_data; |
Tianjie Xu | d9759e8 | 2017-07-21 21:06:52 +0000 | [diff] [blame] | 806 | brotli_new_data.reserve(4096 * 100); |
| 807 | generate_n(back_inserter(brotli_new_data), 4096 * 100, generator); |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 808 | |
| 809 | size_t encoded_size = BrotliEncoderMaxCompressedSize(brotli_new_data.size()); |
| 810 | std::vector<uint8_t> encoded_data(encoded_size); |
| 811 | ASSERT_TRUE(BrotliEncoderCompress( |
| 812 | BROTLI_DEFAULT_QUALITY, BROTLI_DEFAULT_WINDOW, BROTLI_DEFAULT_MODE, brotli_new_data.size(), |
| 813 | reinterpret_cast<const uint8_t*>(brotli_new_data.data()), &encoded_size, encoded_data.data())); |
| 814 | |
| 815 | ASSERT_EQ(0, zip_writer.WriteBytes(encoded_data.data(), encoded_size)); |
| 816 | ASSERT_EQ(0, zip_writer.FinishEntry()); |
| 817 | // Add a dummy patch data. |
| 818 | ASSERT_EQ(0, zip_writer.StartEntry("patch_data", 0)); |
| 819 | ASSERT_EQ(0, zip_writer.FinishEntry()); |
| 820 | |
Tianjie Xu | d9759e8 | 2017-07-21 21:06:52 +0000 | [diff] [blame] | 821 | // Write a few small chunks of new data, then a large chunk, and finally a few small chunks. |
| 822 | // This helps us to catch potential short writes. |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 823 | std::vector<std::string> transfer_list = { |
Tianjie Xu | d9759e8 | 2017-07-21 21:06:52 +0000 | [diff] [blame] | 824 | "4", |
| 825 | "100", |
| 826 | "0", |
| 827 | "0", |
| 828 | "new 2,0,1", |
| 829 | "new 2,1,2", |
| 830 | "new 4,2,50,50,97", |
| 831 | "new 2,97,98", |
| 832 | "new 2,98,99", |
| 833 | "new 2,99,100", |
Tianjie Xu | 107a34f | 2017-06-29 17:04:21 -0700 | [diff] [blame] | 834 | }; |
| 835 | ASSERT_EQ(0, zip_writer.StartEntry("transfer_list", 0)); |
| 836 | std::string commands = android::base::Join(transfer_list, '\n'); |
| 837 | ASSERT_EQ(0, zip_writer.WriteBytes(commands.data(), commands.size())); |
| 838 | ASSERT_EQ(0, zip_writer.FinishEntry()); |
| 839 | ASSERT_EQ(0, zip_writer.Finish()); |
| 840 | ASSERT_EQ(0, fclose(zip_file_ptr)); |
| 841 | |
| 842 | MemMapping map; |
| 843 | ASSERT_TRUE(map.MapFile(zip_file.path)); |
| 844 | ZipArchiveHandle handle; |
| 845 | ASSERT_EQ(0, OpenArchiveFromMemory(map.addr, map.length, zip_file.path, &handle)); |
| 846 | |
| 847 | // Set up the handler, command_pipe, patch offset & length. |
| 848 | UpdaterInfo updater_info; |
| 849 | updater_info.package_zip = handle; |
| 850 | TemporaryFile temp_pipe; |
| 851 | updater_info.cmd_pipe = fopen(temp_pipe.path, "wb"); |
| 852 | updater_info.package_zip_addr = map.addr; |
| 853 | updater_info.package_zip_len = map.length; |
| 854 | |
| 855 | // Check if we can decompress the new data correctly. |
| 856 | TemporaryFile update_file; |
| 857 | std::string script_new_data = |
| 858 | "block_image_update(\"" + std::string(update_file.path) + |
| 859 | R"(", package_extract_file("transfer_list"), "new.dat.br", "patch_data"))"; |
| 860 | expect("t", script_new_data.c_str(), kNoCause, &updater_info); |
| 861 | |
| 862 | std::string updated_content; |
| 863 | ASSERT_TRUE(android::base::ReadFileToString(update_file.path, &updated_content)); |
| 864 | ASSERT_EQ(brotli_new_data, updated_content); |
| 865 | CloseArchive(handle); |
Tianjie Xu | 3a8d98d | 2017-04-03 20:01:17 -0700 | [diff] [blame] | 866 | } |