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