blob: 5652ddf46a6c13196fc97601523db88760b86ec8 [file] [log] [blame]
Tao Bao0c7839a2016-10-10 15:48:37 -07001/*
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 Bao9aa7ab52017-01-05 17:27:19 -080017#include <stdio.h>
Tao Bao89929022016-11-08 20:51:31 -080018#include <sys/stat.h>
19#include <sys/types.h>
20#include <unistd.h>
21
Tianjie Xuc4447322017-03-06 14:44:59 -080022#include <memory>
Tao Bao0c7839a2016-10-10 15:48:37 -070023#include <string>
Tianjie Xu56ebe622017-03-16 00:48:21 -070024#include <vector>
Tao Bao0c7839a2016-10-10 15:48:37 -070025
Tao Bao51d516e2016-11-03 14:49:01 -070026#include <android-base/file.h>
Tao Bao0c7839a2016-10-10 15:48:37 -070027#include <android-base/properties.h>
Tao Bao9aa7ab52017-01-05 17:27:19 -080028#include <android-base/stringprintf.h>
29#include <android-base/strings.h>
Tao Bao51d516e2016-11-03 14:49:01 -070030#include <android-base/test_utils.h>
Tao Baobedf5fc2016-11-18 12:01:26 -080031#include <bootloader_message/bootloader_message.h>
Tianjie Xu56ebe622017-03-16 00:48:21 -070032#include <bsdiff.h>
Tao Bao0c7839a2016-10-10 15:48:37 -070033#include <gtest/gtest.h>
Tao Baoef0eb3b2016-11-14 21:29:52 -080034#include <ziparchive/zip_archive.h>
Tianjie Xu56ebe622017-03-16 00:48:21 -070035#include <ziparchive/zip_writer.h>
Tao Bao0c7839a2016-10-10 15:48:37 -070036
Tao Baoef0eb3b2016-11-14 21:29:52 -080037#include "common/test_constants.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070038#include "edify/expr.h"
39#include "error_code.h"
Tianjie Xu56ebe622017-03-16 00:48:21 -070040#include "otautil/SysUtil.h"
41#include "print_sha1.h"
42#include "updater/blockimg.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070043#include "updater/install.h"
Tao Baoef0eb3b2016-11-14 21:29:52 -080044#include "updater/updater.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070045
46struct selabel_handle *sehandle = nullptr;
47
Tao Baoef0eb3b2016-11-14 21:29:52 -080048static void expect(const char* expected, const char* expr_str, CauseCode cause_code,
49 UpdaterInfo* info = nullptr) {
Tianjie Xuc4447322017-03-06 14:44:59 -080050 std::unique_ptr<Expr> e;
Tao Baoef0eb3b2016-11-14 21:29:52 -080051 int error_count = 0;
52 ASSERT_EQ(0, parse_string(expr_str, &e, &error_count));
53 ASSERT_EQ(0, error_count);
Tao Bao0c7839a2016-10-10 15:48:37 -070054
Tao Baoef0eb3b2016-11-14 21:29:52 -080055 State state(expr_str, info);
Tao Bao0c7839a2016-10-10 15:48:37 -070056
Tao Baoef0eb3b2016-11-14 21:29:52 -080057 std::string result;
58 bool status = Evaluate(&state, e, &result);
Tao Bao0c7839a2016-10-10 15:48:37 -070059
Tao Baoef0eb3b2016-11-14 21:29:52 -080060 if (expected == nullptr) {
61 ASSERT_FALSE(status);
62 } else {
63 ASSERT_TRUE(status);
64 ASSERT_STREQ(expected, result.c_str());
65 }
Tao Bao0c7839a2016-10-10 15:48:37 -070066
Tao Baoef0eb3b2016-11-14 21:29:52 -080067 // Error code is set in updater/updater.cpp only, by parsing State.errmsg.
68 ASSERT_EQ(kNoError, state.error_code);
Tao Bao361342c2016-02-08 11:15:50 -080069
Tao Baoef0eb3b2016-11-14 21:29:52 -080070 // Cause code should always be available.
71 ASSERT_EQ(cause_code, state.cause_code);
Tao Bao0c7839a2016-10-10 15:48:37 -070072}
73
Tianjie Xu56ebe622017-03-16 00:48:21 -070074static 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 Bao0c7839a2016-10-10 15:48:37 -070080class UpdaterTest : public ::testing::Test {
Tianjie Xu56ebe622017-03-16 00:48:21 -070081 protected:
82 virtual void SetUp() override {
83 RegisterBuiltins();
84 RegisterInstallFunctions();
85 RegisterBlockImageFunctions();
86 }
Tao Bao0c7839a2016-10-10 15:48:37 -070087};
88
89TEST_F(UpdaterTest, getprop) {
90 expect(android::base::GetProperty("ro.product.device", "").c_str(),
91 "getprop(\"ro.product.device\")",
Tao Bao361342c2016-02-08 11:15:50 -080092 kNoCause);
Tao Bao0c7839a2016-10-10 15:48:37 -070093
94 expect(android::base::GetProperty("ro.build.fingerprint", "").c_str(),
95 "getprop(\"ro.build.fingerprint\")",
Tao Bao361342c2016-02-08 11:15:50 -080096 kNoCause);
Tao Bao0c7839a2016-10-10 15:48:37 -070097
98 // getprop() accepts only one parameter.
Tao Bao361342c2016-02-08 11:15:50 -080099 expect(nullptr, "getprop()", kArgsParsingFailure);
100 expect(nullptr, "getprop(\"arg1\", \"arg2\")", kArgsParsingFailure);
101}
102
103TEST_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 Bao0c7839a2016-10-10 15:48:37 -0700128}
Tao Bao51d516e2016-11-03 14:49:01 -0700129
Tao Baodb56eb02017-03-23 06:34:20 -0700130TEST_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 Bao51d516e2016-11-03 14:49:01 -0700179TEST_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 Bao0831d0b2016-11-03 23:25:04 -0700226
Tao Bao1036d362016-11-17 22:49:56 -0800227TEST_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 Baoef0eb3b2016-11-14 21:29:52 -0800323// TODO: Test extracting to block device.
324TEST_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 Baod0f30882016-11-03 23:52:01 -0700375
376TEST_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 Baobedf5fc2016-11-18 12:01:26 -0800408
409TEST_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
432TEST_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 Bao9aa7ab52017-01-05 17:27:19 -0800466
467TEST_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
490TEST_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 Xu56ebe622017-03-16 00:48:21 -0700513
514TEST_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;
573 ASSERT_EQ(0, sysMapFile(zip_file.path, &map));
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}