blob: 0298a76458d831c42fc2758670cce13da7e639ca [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 Baoef0eb3b2016-11-14 21:29:52 -0800227// TODO: Test extracting to block device.
228TEST_F(UpdaterTest, package_extract_file) {
229 // package_extract_file expects 1 or 2 arguments.
230 expect(nullptr, "package_extract_file()", kArgsParsingFailure);
231 expect(nullptr, "package_extract_file(\"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 // Two-argument version.
242 TemporaryFile temp_file1;
243 std::string script("package_extract_file(\"a.txt\", \"" + std::string(temp_file1.path) + "\")");
244 expect("t", script.c_str(), kNoCause, &updater_info);
245
246 // Verify the extracted entry.
247 std::string data;
248 ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data));
249 ASSERT_EQ(kATxtContents, data);
250
251 // Now extract another entry to the same location, which should overwrite.
252 script = "package_extract_file(\"b.txt\", \"" + std::string(temp_file1.path) + "\")";
253 expect("t", script.c_str(), kNoCause, &updater_info);
254
255 ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data));
256 ASSERT_EQ(kBTxtContents, data);
257
258 // Missing zip entry. The two-argument version doesn't abort.
259 script = "package_extract_file(\"doesntexist\", \"" + std::string(temp_file1.path) + "\")";
260 expect("", script.c_str(), kNoCause, &updater_info);
261
262 // Extract to /dev/full should fail.
263 script = "package_extract_file(\"a.txt\", \"/dev/full\")";
264 expect("", script.c_str(), kNoCause, &updater_info);
265
266 // One-argument version.
267 script = "sha1_check(package_extract_file(\"a.txt\"))";
268 expect(kATxtSha1Sum.c_str(), script.c_str(), kNoCause, &updater_info);
269
270 script = "sha1_check(package_extract_file(\"b.txt\"))";
271 expect(kBTxtSha1Sum.c_str(), script.c_str(), kNoCause, &updater_info);
272
273 // Missing entry. The one-argument version aborts the evaluation.
274 script = "package_extract_file(\"doesntexist\")";
275 expect(nullptr, script.c_str(), kPackageExtractFileFailure, &updater_info);
276
277 CloseArchive(handle);
278}
Tao Baod0f30882016-11-03 23:52:01 -0700279
280TEST_F(UpdaterTest, write_value) {
281 // write_value() expects two arguments.
282 expect(nullptr, "write_value()", kArgsParsingFailure);
283 expect(nullptr, "write_value(\"arg1\")", kArgsParsingFailure);
284 expect(nullptr, "write_value(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
285
286 // filename cannot be empty.
287 expect(nullptr, "write_value(\"value\", \"\")", kArgsParsingFailure);
288
289 // Write some value to file.
290 TemporaryFile temp_file;
291 std::string value = "magicvalue";
292 std::string script("write_value(\"" + value + "\", \"" + std::string(temp_file.path) + "\")");
293 expect("t", script.c_str(), kNoCause);
294
295 // Verify the content.
296 std::string content;
297 ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content));
298 ASSERT_EQ(value, content);
299
300 // Allow writing empty string.
301 script = "write_value(\"\", \"" + std::string(temp_file.path) + "\")";
302 expect("t", script.c_str(), kNoCause);
303
304 // Verify the content.
305 ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content));
306 ASSERT_EQ("", content);
307
308 // It should fail gracefully when write fails.
309 script = "write_value(\"value\", \"/proc/0/file1\")";
310 expect("", script.c_str(), kNoCause);
311}
Tao Baobedf5fc2016-11-18 12:01:26 -0800312
313TEST_F(UpdaterTest, get_stage) {
314 // get_stage() expects one argument.
315 expect(nullptr, "get_stage()", kArgsParsingFailure);
316 expect(nullptr, "get_stage(\"arg1\", \"arg2\")", kArgsParsingFailure);
317 expect(nullptr, "get_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
318
319 // Set up a local file as BCB.
320 TemporaryFile tf;
321 std::string temp_file(tf.path);
322 bootloader_message boot;
323 strlcpy(boot.stage, "2/3", sizeof(boot.stage));
324 std::string err;
325 ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err));
326
327 // Can read the stage value.
328 std::string script("get_stage(\"" + temp_file + "\")");
329 expect("2/3", script.c_str(), kNoCause);
330
331 // Bad BCB path.
332 script = "get_stage(\"doesntexist\")";
333 expect("", script.c_str(), kNoCause);
334}
335
336TEST_F(UpdaterTest, set_stage) {
337 // set_stage() expects two arguments.
338 expect(nullptr, "set_stage()", kArgsParsingFailure);
339 expect(nullptr, "set_stage(\"arg1\")", kArgsParsingFailure);
340 expect(nullptr, "set_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
341
342 // Set up a local file as BCB.
343 TemporaryFile tf;
344 std::string temp_file(tf.path);
345 bootloader_message boot;
346 strlcpy(boot.command, "command", sizeof(boot.command));
347 strlcpy(boot.stage, "2/3", sizeof(boot.stage));
348 std::string err;
349 ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err));
350
351 // Write with set_stage().
352 std::string script("set_stage(\"" + temp_file + "\", \"1/3\")");
353 expect(tf.path, script.c_str(), kNoCause);
354
355 // Verify.
356 bootloader_message boot_verify;
357 ASSERT_TRUE(read_bootloader_message_from(&boot_verify, temp_file, &err));
358
359 // Stage should be updated, with command part untouched.
360 ASSERT_STREQ("1/3", boot_verify.stage);
361 ASSERT_STREQ(boot.command, boot_verify.command);
362
363 // Bad BCB path.
364 script = "set_stage(\"doesntexist\", \"1/3\")";
365 expect("", script.c_str(), kNoCause);
366
367 script = "set_stage(\"/dev/full\", \"1/3\")";
368 expect("", script.c_str(), kNoCause);
369}
Tao Bao9aa7ab52017-01-05 17:27:19 -0800370
371TEST_F(UpdaterTest, set_progress) {
372 // set_progress() expects one argument.
373 expect(nullptr, "set_progress()", kArgsParsingFailure);
374 expect(nullptr, "set_progress(\"arg1\", \"arg2\")", kArgsParsingFailure);
375
376 // Invalid progress argument.
377 expect(nullptr, "set_progress(\"arg1\")", kArgsParsingFailure);
378 expect(nullptr, "set_progress(\"3x+5\")", kArgsParsingFailure);
379 expect(nullptr, "set_progress(\".3.5\")", kArgsParsingFailure);
380
381 TemporaryFile tf;
382 UpdaterInfo updater_info;
383 updater_info.cmd_pipe = fdopen(tf.fd, "w");
384 expect(".52", "set_progress(\".52\")", kNoCause, &updater_info);
385 fflush(updater_info.cmd_pipe);
386
387 std::string cmd;
388 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd));
389 ASSERT_EQ(android::base::StringPrintf("set_progress %f\n", .52), cmd);
390 // recovery-updater protocol expects 2 tokens ("set_progress <frac>").
391 ASSERT_EQ(2U, android::base::Split(cmd, " ").size());
392}
393
394TEST_F(UpdaterTest, show_progress) {
395 // show_progress() expects two arguments.
396 expect(nullptr, "show_progress()", kArgsParsingFailure);
397 expect(nullptr, "show_progress(\"arg1\")", kArgsParsingFailure);
398 expect(nullptr, "show_progress(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
399
400 // Invalid progress arguments.
401 expect(nullptr, "show_progress(\"arg1\", \"arg2\")", kArgsParsingFailure);
402 expect(nullptr, "show_progress(\"3x+5\", \"10\")", kArgsParsingFailure);
403 expect(nullptr, "show_progress(\".3\", \"5a\")", kArgsParsingFailure);
404
405 TemporaryFile tf;
406 UpdaterInfo updater_info;
407 updater_info.cmd_pipe = fdopen(tf.fd, "w");
408 expect(".52", "show_progress(\".52\", \"10\")", kNoCause, &updater_info);
409 fflush(updater_info.cmd_pipe);
410
411 std::string cmd;
412 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd));
413 ASSERT_EQ(android::base::StringPrintf("progress %f %d\n", .52, 10), cmd);
414 // recovery-updater protocol expects 3 tokens ("progress <frac> <secs>").
415 ASSERT_EQ(3U, android::base::Split(cmd, " ").size());
416}
Tianjie Xu56ebe622017-03-16 00:48:21 -0700417
418TEST_F(UpdaterTest, block_image_update) {
419 // Create a zip file with new_data and patch_data.
420 TemporaryFile zip_file;
421 FILE* zip_file_ptr = fdopen(zip_file.fd, "wb");
422 ZipWriter zip_writer(zip_file_ptr);
423
424 // Add a dummy new data.
425 ASSERT_EQ(0, zip_writer.StartEntry("new_data", 0));
426 ASSERT_EQ(0, zip_writer.FinishEntry());
427
428 // Generate and add the patch data.
429 std::string src_content = std::string(4096, 'a') + std::string(4096, 'c');
430 std::string tgt_content = std::string(4096, 'b') + std::string(4096, 'd');
431 TemporaryFile patch_file;
432 ASSERT_EQ(0, bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(src_content.data()),
433 src_content.size(), reinterpret_cast<const uint8_t*>(tgt_content.data()),
434 tgt_content.size(), patch_file.path, nullptr));
435 std::string patch_content;
436 ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch_content));
437 ASSERT_EQ(0, zip_writer.StartEntry("patch_data", 0));
438 ASSERT_EQ(0, zip_writer.WriteBytes(patch_content.data(), patch_content.size()));
439 ASSERT_EQ(0, zip_writer.FinishEntry());
440
441 // Add two transfer lists. The first one contains a bsdiff; and we expect the update to succeed.
442 std::string src_hash = get_sha1(src_content);
443 std::string tgt_hash = get_sha1(tgt_content);
444 std::vector<std::string> transfer_list = {
445 "4",
446 "2",
447 "0",
448 "2",
449 "stash " + src_hash + " 2,0,2",
450 android::base::StringPrintf("bsdiff 0 %zu %s %s 2,0,2 2 - %s:2,0,2", patch_content.size(),
451 src_hash.c_str(), tgt_hash.c_str(), src_hash.c_str()),
452 "free " + src_hash,
453 };
454 ASSERT_EQ(0, zip_writer.StartEntry("transfer_list", 0));
455 std::string commands = android::base::Join(transfer_list, '\n');
456 ASSERT_EQ(0, zip_writer.WriteBytes(commands.data(), commands.size()));
457 ASSERT_EQ(0, zip_writer.FinishEntry());
458
459 // Stash and free some blocks, then fail the 2nd update intentionally.
460 std::vector<std::string> fail_transfer_list = {
461 "4",
462 "2",
463 "0",
464 "2",
465 "stash " + tgt_hash + " 2,0,2",
466 "free " + tgt_hash,
467 "fail",
468 };
469 ASSERT_EQ(0, zip_writer.StartEntry("fail_transfer_list", 0));
470 std::string fail_commands = android::base::Join(fail_transfer_list, '\n');
471 ASSERT_EQ(0, zip_writer.WriteBytes(fail_commands.data(), fail_commands.size()));
472 ASSERT_EQ(0, zip_writer.FinishEntry());
473 ASSERT_EQ(0, zip_writer.Finish());
474 ASSERT_EQ(0, fclose(zip_file_ptr));
475
476 MemMapping map;
Tao Baob656a152017-04-18 23:54:29 -0700477 ASSERT_TRUE(map.MapFile(zip_file.path));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700478 ZipArchiveHandle handle;
479 ASSERT_EQ(0, OpenArchiveFromMemory(map.addr, map.length, zip_file.path, &handle));
480
481 // Set up the handler, command_pipe, patch offset & length.
482 UpdaterInfo updater_info;
483 updater_info.package_zip = handle;
484 TemporaryFile temp_pipe;
485 updater_info.cmd_pipe = fopen(temp_pipe.path, "wb");
486 updater_info.package_zip_addr = map.addr;
487 updater_info.package_zip_len = map.length;
488
489 // Execute the commands in the 1st transfer list.
490 TemporaryFile update_file;
491 ASSERT_TRUE(android::base::WriteStringToFile(src_content, update_file.path));
492 std::string script = "block_image_update(\"" + std::string(update_file.path) +
493 R"(", package_extract_file("transfer_list"), "new_data", "patch_data"))";
494 expect("t", script.c_str(), kNoCause, &updater_info);
495 // The update_file should be patched correctly.
496 std::string updated_content;
497 ASSERT_TRUE(android::base::ReadFileToString(update_file.path, &updated_content));
498 ASSERT_EQ(tgt_hash, get_sha1(updated_content));
499
500 // Expect the 2nd update to fail, but expect the stashed blocks to be freed.
501 script = "block_image_update(\"" + std::string(update_file.path) +
502 R"(", package_extract_file("fail_transfer_list"), "new_data", "patch_data"))";
503 expect("", script.c_str(), kNoCause, &updater_info);
504 // Updater generates the stash name based on the input file name.
505 std::string name_digest = get_sha1(update_file.path);
506 std::string stash_base = "/cache/recovery/" + name_digest;
507 ASSERT_EQ(0, access(stash_base.c_str(), F_OK));
508 ASSERT_EQ(-1, access((stash_base + tgt_hash).c_str(), F_OK));
509 ASSERT_EQ(0, rmdir(stash_base.c_str()));
510
511 ASSERT_EQ(0, fclose(updater_info.cmd_pipe));
512 CloseArchive(handle);
513}
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700514
515TEST_F(UpdaterTest, new_data_short_write) {
516 // Create a zip file with new_data.
517 TemporaryFile zip_file;
518 FILE* zip_file_ptr = fdopen(zip_file.fd, "wb");
519 ZipWriter zip_writer(zip_file_ptr);
520
521 // Add the empty new data.
522 ASSERT_EQ(0, zip_writer.StartEntry("empty_new_data", 0));
523 ASSERT_EQ(0, zip_writer.FinishEntry());
524 // Add the short written new data.
525 ASSERT_EQ(0, zip_writer.StartEntry("short_new_data", 0));
526 std::string new_data_short = std::string(10, 'a');
527 ASSERT_EQ(0, zip_writer.WriteBytes(new_data_short.data(), new_data_short.size()));
528 ASSERT_EQ(0, zip_writer.FinishEntry());
529 // Add the data of exactly one block.
530 ASSERT_EQ(0, zip_writer.StartEntry("exact_new_data", 0));
531 std::string new_data_exact = std::string(4096, 'a');
532 ASSERT_EQ(0, zip_writer.WriteBytes(new_data_exact.data(), new_data_exact.size()));
533 ASSERT_EQ(0, zip_writer.FinishEntry());
534 // Add a dummy patch data.
535 ASSERT_EQ(0, zip_writer.StartEntry("patch_data", 0));
536 ASSERT_EQ(0, zip_writer.FinishEntry());
537
538 std::vector<std::string> transfer_list = {
539 "4",
540 "1",
541 "0",
542 "0",
543 "new 2,0,1",
544 };
545 ASSERT_EQ(0, zip_writer.StartEntry("transfer_list", 0));
546 std::string commands = android::base::Join(transfer_list, '\n');
547 ASSERT_EQ(0, zip_writer.WriteBytes(commands.data(), commands.size()));
548 ASSERT_EQ(0, zip_writer.FinishEntry());
549 ASSERT_EQ(0, zip_writer.Finish());
550 ASSERT_EQ(0, fclose(zip_file_ptr));
551
552 MemMapping map;
Tao Baob656a152017-04-18 23:54:29 -0700553 ASSERT_TRUE(map.MapFile(zip_file.path));
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700554 ZipArchiveHandle handle;
555 ASSERT_EQ(0, OpenArchiveFromMemory(map.addr, map.length, zip_file.path, &handle));
556
557 // Set up the handler, command_pipe, patch offset & length.
558 UpdaterInfo updater_info;
559 updater_info.package_zip = handle;
560 TemporaryFile temp_pipe;
561 updater_info.cmd_pipe = fopen(temp_pipe.path, "wb");
562 updater_info.package_zip_addr = map.addr;
563 updater_info.package_zip_len = map.length;
564
565 // Updater should report the failure gracefully rather than stuck in deadlock.
566 TemporaryFile update_file;
567 std::string script_empty_data = "block_image_update(\"" + std::string(update_file.path) +
568 R"(", package_extract_file("transfer_list"), "empty_new_data", "patch_data"))";
569 expect("", script_empty_data.c_str(), kNoCause, &updater_info);
570
571 std::string script_short_data = "block_image_update(\"" + std::string(update_file.path) +
572 R"(", package_extract_file("transfer_list"), "short_new_data", "patch_data"))";
573 expect("", script_short_data.c_str(), kNoCause, &updater_info);
574
575 // Expect to write 1 block of new data successfully.
576 std::string script_exact_data = "block_image_update(\"" + std::string(update_file.path) +
577 R"(", package_extract_file("transfer_list"), "exact_new_data", "patch_data"))";
578 expect("t", script_exact_data.c_str(), kNoCause, &updater_info);
579}