blob: ef121a973178f36cc5783d30a2abb04a96a3396f [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
130TEST_F(UpdaterTest, file_getprop) {
131 // file_getprop() expects two arguments.
132 expect(nullptr, "file_getprop()", kArgsParsingFailure);
133 expect(nullptr, "file_getprop(\"arg1\")", kArgsParsingFailure);
134 expect(nullptr, "file_getprop(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
135
136 // File doesn't exist.
137 expect(nullptr, "file_getprop(\"/doesntexist\", \"key1\")", kFileGetPropFailure);
138
139 // Reject too large files (current limit = 65536).
140 TemporaryFile temp_file1;
141 std::string buffer(65540, '\0');
142 ASSERT_TRUE(android::base::WriteStringToFile(buffer, temp_file1.path));
143
144 // Read some keys.
145 TemporaryFile temp_file2;
146 std::string content("ro.product.name=tardis\n"
147 "# comment\n\n\n"
148 "ro.product.model\n"
149 "ro.product.board = magic \n");
150 ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file2.path));
151
152 std::string script1("file_getprop(\"" + std::string(temp_file2.path) +
153 "\", \"ro.product.name\")");
154 expect("tardis", script1.c_str(), kNoCause);
155
156 std::string script2("file_getprop(\"" + std::string(temp_file2.path) +
157 "\", \"ro.product.board\")");
158 expect("magic", script2.c_str(), kNoCause);
159
160 // No match.
161 std::string script3("file_getprop(\"" + std::string(temp_file2.path) +
162 "\", \"ro.product.wrong\")");
163 expect("", script3.c_str(), kNoCause);
164
165 std::string script4("file_getprop(\"" + std::string(temp_file2.path) +
166 "\", \"ro.product.name=\")");
167 expect("", script4.c_str(), kNoCause);
168
169 std::string script5("file_getprop(\"" + std::string(temp_file2.path) +
170 "\", \"ro.product.nam\")");
171 expect("", script5.c_str(), kNoCause);
172
173 std::string script6("file_getprop(\"" + std::string(temp_file2.path) +
174 "\", \"ro.product.model\")");
175 expect("", script6.c_str(), kNoCause);
176}
Tao Bao0831d0b2016-11-03 23:25:04 -0700177
Tao Bao1036d362016-11-17 22:49:56 -0800178TEST_F(UpdaterTest, package_extract_dir) {
179 // package_extract_dir expects 2 arguments.
180 expect(nullptr, "package_extract_dir()", kArgsParsingFailure);
181 expect(nullptr, "package_extract_dir(\"arg1\")", kArgsParsingFailure);
182 expect(nullptr, "package_extract_dir(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
183
184 std::string zip_path = from_testdata_base("ziptest_valid.zip");
185 ZipArchiveHandle handle;
186 ASSERT_EQ(0, OpenArchive(zip_path.c_str(), &handle));
187
188 // Need to set up the ziphandle.
189 UpdaterInfo updater_info;
190 updater_info.package_zip = handle;
191
192 // Extract "b/c.txt" and "b/d.txt" with package_extract_dir("b", "<dir>").
193 TemporaryDir td;
194 std::string temp_dir(td.path);
195 std::string script("package_extract_dir(\"b\", \"" + temp_dir + "\")");
196 expect("t", script.c_str(), kNoCause, &updater_info);
197
198 // Verify.
199 std::string data;
200 std::string file_c = temp_dir + "/c.txt";
201 ASSERT_TRUE(android::base::ReadFileToString(file_c, &data));
202 ASSERT_EQ(kCTxtContents, data);
203
204 std::string file_d = temp_dir + "/d.txt";
205 ASSERT_TRUE(android::base::ReadFileToString(file_d, &data));
206 ASSERT_EQ(kDTxtContents, data);
207
208 // Modify the contents in order to retry. It's expected to be overwritten.
209 ASSERT_TRUE(android::base::WriteStringToFile("random", file_c));
210 ASSERT_TRUE(android::base::WriteStringToFile("random", file_d));
211
212 // Extract again and verify.
213 expect("t", script.c_str(), kNoCause, &updater_info);
214
215 ASSERT_TRUE(android::base::ReadFileToString(file_c, &data));
216 ASSERT_EQ(kCTxtContents, data);
217 ASSERT_TRUE(android::base::ReadFileToString(file_d, &data));
218 ASSERT_EQ(kDTxtContents, data);
219
220 // Clean up the temp files under td.
221 ASSERT_EQ(0, unlink(file_c.c_str()));
222 ASSERT_EQ(0, unlink(file_d.c_str()));
223
224 // Extracting "b/" (with slash) should give the same result.
225 script = "package_extract_dir(\"b/\", \"" + temp_dir + "\")";
226 expect("t", script.c_str(), kNoCause, &updater_info);
227
228 ASSERT_TRUE(android::base::ReadFileToString(file_c, &data));
229 ASSERT_EQ(kCTxtContents, data);
230 ASSERT_TRUE(android::base::ReadFileToString(file_d, &data));
231 ASSERT_EQ(kDTxtContents, data);
232
233 ASSERT_EQ(0, unlink(file_c.c_str()));
234 ASSERT_EQ(0, unlink(file_d.c_str()));
235
236 // Extracting "" is allowed. The entries will carry the path name.
237 script = "package_extract_dir(\"\", \"" + temp_dir + "\")";
238 expect("t", script.c_str(), kNoCause, &updater_info);
239
240 std::string file_a = temp_dir + "/a.txt";
241 ASSERT_TRUE(android::base::ReadFileToString(file_a, &data));
242 ASSERT_EQ(kATxtContents, data);
243 std::string file_b = temp_dir + "/b.txt";
244 ASSERT_TRUE(android::base::ReadFileToString(file_b, &data));
245 ASSERT_EQ(kBTxtContents, data);
246 std::string file_b_c = temp_dir + "/b/c.txt";
247 ASSERT_TRUE(android::base::ReadFileToString(file_b_c, &data));
248 ASSERT_EQ(kCTxtContents, data);
249 std::string file_b_d = temp_dir + "/b/d.txt";
250 ASSERT_TRUE(android::base::ReadFileToString(file_b_d, &data));
251 ASSERT_EQ(kDTxtContents, data);
252
253 ASSERT_EQ(0, unlink(file_a.c_str()));
254 ASSERT_EQ(0, unlink(file_b.c_str()));
255 ASSERT_EQ(0, unlink(file_b_c.c_str()));
256 ASSERT_EQ(0, unlink(file_b_d.c_str()));
257 ASSERT_EQ(0, rmdir((temp_dir + "/b").c_str()));
258
259 // Extracting non-existent entry should still give "t".
260 script = "package_extract_dir(\"doesntexist\", \"" + temp_dir + "\")";
261 expect("t", script.c_str(), kNoCause, &updater_info);
262
263 // Only relative zip_path is allowed.
264 script = "package_extract_dir(\"/b\", \"" + temp_dir + "\")";
265 expect("", script.c_str(), kNoCause, &updater_info);
266
267 // Only absolute dest_path is allowed.
268 script = "package_extract_dir(\"b\", \"path\")";
269 expect("", script.c_str(), kNoCause, &updater_info);
270
271 CloseArchive(handle);
272}
273
Tao Baoef0eb3b2016-11-14 21:29:52 -0800274// TODO: Test extracting to block device.
275TEST_F(UpdaterTest, package_extract_file) {
276 // package_extract_file expects 1 or 2 arguments.
277 expect(nullptr, "package_extract_file()", kArgsParsingFailure);
278 expect(nullptr, "package_extract_file(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
279
280 std::string zip_path = from_testdata_base("ziptest_valid.zip");
281 ZipArchiveHandle handle;
282 ASSERT_EQ(0, OpenArchive(zip_path.c_str(), &handle));
283
284 // Need to set up the ziphandle.
285 UpdaterInfo updater_info;
286 updater_info.package_zip = handle;
287
288 // Two-argument version.
289 TemporaryFile temp_file1;
290 std::string script("package_extract_file(\"a.txt\", \"" + std::string(temp_file1.path) + "\")");
291 expect("t", script.c_str(), kNoCause, &updater_info);
292
293 // Verify the extracted entry.
294 std::string data;
295 ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data));
296 ASSERT_EQ(kATxtContents, data);
297
298 // Now extract another entry to the same location, which should overwrite.
299 script = "package_extract_file(\"b.txt\", \"" + std::string(temp_file1.path) + "\")";
300 expect("t", script.c_str(), kNoCause, &updater_info);
301
302 ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data));
303 ASSERT_EQ(kBTxtContents, data);
304
305 // Missing zip entry. The two-argument version doesn't abort.
306 script = "package_extract_file(\"doesntexist\", \"" + std::string(temp_file1.path) + "\")";
307 expect("", script.c_str(), kNoCause, &updater_info);
308
309 // Extract to /dev/full should fail.
310 script = "package_extract_file(\"a.txt\", \"/dev/full\")";
311 expect("", script.c_str(), kNoCause, &updater_info);
312
313 // One-argument version.
314 script = "sha1_check(package_extract_file(\"a.txt\"))";
315 expect(kATxtSha1Sum.c_str(), script.c_str(), kNoCause, &updater_info);
316
317 script = "sha1_check(package_extract_file(\"b.txt\"))";
318 expect(kBTxtSha1Sum.c_str(), script.c_str(), kNoCause, &updater_info);
319
320 // Missing entry. The one-argument version aborts the evaluation.
321 script = "package_extract_file(\"doesntexist\")";
322 expect(nullptr, script.c_str(), kPackageExtractFileFailure, &updater_info);
323
324 CloseArchive(handle);
325}
Tao Baod0f30882016-11-03 23:52:01 -0700326
327TEST_F(UpdaterTest, write_value) {
328 // write_value() expects two arguments.
329 expect(nullptr, "write_value()", kArgsParsingFailure);
330 expect(nullptr, "write_value(\"arg1\")", kArgsParsingFailure);
331 expect(nullptr, "write_value(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
332
333 // filename cannot be empty.
334 expect(nullptr, "write_value(\"value\", \"\")", kArgsParsingFailure);
335
336 // Write some value to file.
337 TemporaryFile temp_file;
338 std::string value = "magicvalue";
339 std::string script("write_value(\"" + value + "\", \"" + std::string(temp_file.path) + "\")");
340 expect("t", script.c_str(), kNoCause);
341
342 // Verify the content.
343 std::string content;
344 ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content));
345 ASSERT_EQ(value, content);
346
347 // Allow writing empty string.
348 script = "write_value(\"\", \"" + std::string(temp_file.path) + "\")";
349 expect("t", script.c_str(), kNoCause);
350
351 // Verify the content.
352 ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content));
353 ASSERT_EQ("", content);
354
355 // It should fail gracefully when write fails.
356 script = "write_value(\"value\", \"/proc/0/file1\")";
357 expect("", script.c_str(), kNoCause);
358}
Tao Baobedf5fc2016-11-18 12:01:26 -0800359
360TEST_F(UpdaterTest, get_stage) {
361 // get_stage() expects one argument.
362 expect(nullptr, "get_stage()", kArgsParsingFailure);
363 expect(nullptr, "get_stage(\"arg1\", \"arg2\")", kArgsParsingFailure);
364 expect(nullptr, "get_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
365
366 // Set up a local file as BCB.
367 TemporaryFile tf;
368 std::string temp_file(tf.path);
369 bootloader_message boot;
370 strlcpy(boot.stage, "2/3", sizeof(boot.stage));
371 std::string err;
372 ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err));
373
374 // Can read the stage value.
375 std::string script("get_stage(\"" + temp_file + "\")");
376 expect("2/3", script.c_str(), kNoCause);
377
378 // Bad BCB path.
379 script = "get_stage(\"doesntexist\")";
380 expect("", script.c_str(), kNoCause);
381}
382
383TEST_F(UpdaterTest, set_stage) {
384 // set_stage() expects two arguments.
385 expect(nullptr, "set_stage()", kArgsParsingFailure);
386 expect(nullptr, "set_stage(\"arg1\")", kArgsParsingFailure);
387 expect(nullptr, "set_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
388
389 // Set up a local file as BCB.
390 TemporaryFile tf;
391 std::string temp_file(tf.path);
392 bootloader_message boot;
393 strlcpy(boot.command, "command", sizeof(boot.command));
394 strlcpy(boot.stage, "2/3", sizeof(boot.stage));
395 std::string err;
396 ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err));
397
398 // Write with set_stage().
399 std::string script("set_stage(\"" + temp_file + "\", \"1/3\")");
400 expect(tf.path, script.c_str(), kNoCause);
401
402 // Verify.
403 bootloader_message boot_verify;
404 ASSERT_TRUE(read_bootloader_message_from(&boot_verify, temp_file, &err));
405
406 // Stage should be updated, with command part untouched.
407 ASSERT_STREQ("1/3", boot_verify.stage);
408 ASSERT_STREQ(boot.command, boot_verify.command);
409
410 // Bad BCB path.
411 script = "set_stage(\"doesntexist\", \"1/3\")";
412 expect("", script.c_str(), kNoCause);
413
414 script = "set_stage(\"/dev/full\", \"1/3\")";
415 expect("", script.c_str(), kNoCause);
416}
Tao Bao9aa7ab52017-01-05 17:27:19 -0800417
418TEST_F(UpdaterTest, set_progress) {
419 // set_progress() expects one argument.
420 expect(nullptr, "set_progress()", kArgsParsingFailure);
421 expect(nullptr, "set_progress(\"arg1\", \"arg2\")", kArgsParsingFailure);
422
423 // Invalid progress argument.
424 expect(nullptr, "set_progress(\"arg1\")", kArgsParsingFailure);
425 expect(nullptr, "set_progress(\"3x+5\")", kArgsParsingFailure);
426 expect(nullptr, "set_progress(\".3.5\")", kArgsParsingFailure);
427
428 TemporaryFile tf;
429 UpdaterInfo updater_info;
430 updater_info.cmd_pipe = fdopen(tf.fd, "w");
431 expect(".52", "set_progress(\".52\")", kNoCause, &updater_info);
432 fflush(updater_info.cmd_pipe);
433
434 std::string cmd;
435 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd));
436 ASSERT_EQ(android::base::StringPrintf("set_progress %f\n", .52), cmd);
437 // recovery-updater protocol expects 2 tokens ("set_progress <frac>").
438 ASSERT_EQ(2U, android::base::Split(cmd, " ").size());
439}
440
441TEST_F(UpdaterTest, show_progress) {
442 // show_progress() expects two arguments.
443 expect(nullptr, "show_progress()", kArgsParsingFailure);
444 expect(nullptr, "show_progress(\"arg1\")", kArgsParsingFailure);
445 expect(nullptr, "show_progress(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
446
447 // Invalid progress arguments.
448 expect(nullptr, "show_progress(\"arg1\", \"arg2\")", kArgsParsingFailure);
449 expect(nullptr, "show_progress(\"3x+5\", \"10\")", kArgsParsingFailure);
450 expect(nullptr, "show_progress(\".3\", \"5a\")", kArgsParsingFailure);
451
452 TemporaryFile tf;
453 UpdaterInfo updater_info;
454 updater_info.cmd_pipe = fdopen(tf.fd, "w");
455 expect(".52", "show_progress(\".52\", \"10\")", kNoCause, &updater_info);
456 fflush(updater_info.cmd_pipe);
457
458 std::string cmd;
459 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd));
460 ASSERT_EQ(android::base::StringPrintf("progress %f %d\n", .52, 10), cmd);
461 // recovery-updater protocol expects 3 tokens ("progress <frac> <secs>").
462 ASSERT_EQ(3U, android::base::Split(cmd, " ").size());
463}
Tianjie Xu56ebe622017-03-16 00:48:21 -0700464
465TEST_F(UpdaterTest, block_image_update) {
466 // Create a zip file with new_data and patch_data.
467 TemporaryFile zip_file;
468 FILE* zip_file_ptr = fdopen(zip_file.fd, "wb");
469 ZipWriter zip_writer(zip_file_ptr);
470
471 // Add a dummy new data.
472 ASSERT_EQ(0, zip_writer.StartEntry("new_data", 0));
473 ASSERT_EQ(0, zip_writer.FinishEntry());
474
475 // Generate and add the patch data.
476 std::string src_content = std::string(4096, 'a') + std::string(4096, 'c');
477 std::string tgt_content = std::string(4096, 'b') + std::string(4096, 'd');
478 TemporaryFile patch_file;
479 ASSERT_EQ(0, bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(src_content.data()),
480 src_content.size(), reinterpret_cast<const uint8_t*>(tgt_content.data()),
481 tgt_content.size(), patch_file.path, nullptr));
482 std::string patch_content;
483 ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch_content));
484 ASSERT_EQ(0, zip_writer.StartEntry("patch_data", 0));
485 ASSERT_EQ(0, zip_writer.WriteBytes(patch_content.data(), patch_content.size()));
486 ASSERT_EQ(0, zip_writer.FinishEntry());
487
488 // Add two transfer lists. The first one contains a bsdiff; and we expect the update to succeed.
489 std::string src_hash = get_sha1(src_content);
490 std::string tgt_hash = get_sha1(tgt_content);
491 std::vector<std::string> transfer_list = {
492 "4",
493 "2",
494 "0",
495 "2",
496 "stash " + src_hash + " 2,0,2",
497 android::base::StringPrintf("bsdiff 0 %zu %s %s 2,0,2 2 - %s:2,0,2", patch_content.size(),
498 src_hash.c_str(), tgt_hash.c_str(), src_hash.c_str()),
499 "free " + src_hash,
500 };
501 ASSERT_EQ(0, zip_writer.StartEntry("transfer_list", 0));
502 std::string commands = android::base::Join(transfer_list, '\n');
503 ASSERT_EQ(0, zip_writer.WriteBytes(commands.data(), commands.size()));
504 ASSERT_EQ(0, zip_writer.FinishEntry());
505
506 // Stash and free some blocks, then fail the 2nd update intentionally.
507 std::vector<std::string> fail_transfer_list = {
508 "4",
509 "2",
510 "0",
511 "2",
512 "stash " + tgt_hash + " 2,0,2",
513 "free " + tgt_hash,
514 "fail",
515 };
516 ASSERT_EQ(0, zip_writer.StartEntry("fail_transfer_list", 0));
517 std::string fail_commands = android::base::Join(fail_transfer_list, '\n');
518 ASSERT_EQ(0, zip_writer.WriteBytes(fail_commands.data(), fail_commands.size()));
519 ASSERT_EQ(0, zip_writer.FinishEntry());
520 ASSERT_EQ(0, zip_writer.Finish());
521 ASSERT_EQ(0, fclose(zip_file_ptr));
522
523 MemMapping map;
524 ASSERT_EQ(0, sysMapFile(zip_file.path, &map));
525 ZipArchiveHandle handle;
526 ASSERT_EQ(0, OpenArchiveFromMemory(map.addr, map.length, zip_file.path, &handle));
527
528 // Set up the handler, command_pipe, patch offset & length.
529 UpdaterInfo updater_info;
530 updater_info.package_zip = handle;
531 TemporaryFile temp_pipe;
532 updater_info.cmd_pipe = fopen(temp_pipe.path, "wb");
533 updater_info.package_zip_addr = map.addr;
534 updater_info.package_zip_len = map.length;
535
536 // Execute the commands in the 1st transfer list.
537 TemporaryFile update_file;
538 ASSERT_TRUE(android::base::WriteStringToFile(src_content, update_file.path));
539 std::string script = "block_image_update(\"" + std::string(update_file.path) +
540 R"(", package_extract_file("transfer_list"), "new_data", "patch_data"))";
541 expect("t", script.c_str(), kNoCause, &updater_info);
542 // The update_file should be patched correctly.
543 std::string updated_content;
544 ASSERT_TRUE(android::base::ReadFileToString(update_file.path, &updated_content));
545 ASSERT_EQ(tgt_hash, get_sha1(updated_content));
546
547 // Expect the 2nd update to fail, but expect the stashed blocks to be freed.
548 script = "block_image_update(\"" + std::string(update_file.path) +
549 R"(", package_extract_file("fail_transfer_list"), "new_data", "patch_data"))";
550 expect("", script.c_str(), kNoCause, &updater_info);
551 // Updater generates the stash name based on the input file name.
552 std::string name_digest = get_sha1(update_file.path);
553 std::string stash_base = "/cache/recovery/" + name_digest;
554 ASSERT_EQ(0, access(stash_base.c_str(), F_OK));
555 ASSERT_EQ(-1, access((stash_base + tgt_hash).c_str(), F_OK));
556 ASSERT_EQ(0, rmdir(stash_base.c_str()));
557
558 ASSERT_EQ(0, fclose(updater_info.cmd_pipe));
559 CloseArchive(handle);
560}