blob: aa2321de85e7cee69c7ca9924014596f325e8550 [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>
Tianjie Xu107a34f2017-06-29 17:04:21 -070018#include <stdlib.h>
Tao Bao89929022016-11-08 20:51:31 -080019#include <sys/stat.h>
20#include <sys/types.h>
21#include <unistd.h>
22
Tianjie Xu107a34f2017-06-29 17:04:21 -070023#include <algorithm>
Tianjie Xuc4447322017-03-06 14:44:59 -080024#include <memory>
Tao Bao0c7839a2016-10-10 15:48:37 -070025#include <string>
Tianjie Xu56ebe622017-03-16 00:48:21 -070026#include <vector>
Tao Bao0c7839a2016-10-10 15:48:37 -070027
Tao Bao51d516e2016-11-03 14:49:01 -070028#include <android-base/file.h>
Tao Bao0c7839a2016-10-10 15:48:37 -070029#include <android-base/properties.h>
Tao Bao9aa7ab52017-01-05 17:27:19 -080030#include <android-base/stringprintf.h>
31#include <android-base/strings.h>
Tao Bao51d516e2016-11-03 14:49:01 -070032#include <android-base/test_utils.h>
Tao Baobedf5fc2016-11-18 12:01:26 -080033#include <bootloader_message/bootloader_message.h>
Tianjie Xu107a34f2017-06-29 17:04:21 -070034#include <brotli/encode.h>
Tianjie Xu56ebe622017-03-16 00:48:21 -070035#include <bsdiff.h>
Tao Bao0c7839a2016-10-10 15:48:37 -070036#include <gtest/gtest.h>
Tao Baoef0eb3b2016-11-14 21:29:52 -080037#include <ziparchive/zip_archive.h>
Tianjie Xu56ebe622017-03-16 00:48:21 -070038#include <ziparchive/zip_writer.h>
Tao Bao0c7839a2016-10-10 15:48:37 -070039
Tao Baoef0eb3b2016-11-14 21:29:52 -080040#include "common/test_constants.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070041#include "edify/expr.h"
42#include "error_code.h"
Tianjie Xu56ebe622017-03-16 00:48:21 -070043#include "otautil/SysUtil.h"
44#include "print_sha1.h"
45#include "updater/blockimg.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070046#include "updater/install.h"
Tao Baoef0eb3b2016-11-14 21:29:52 -080047#include "updater/updater.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070048
49struct selabel_handle *sehandle = nullptr;
50
Tao Baoef0eb3b2016-11-14 21:29:52 -080051static void expect(const char* expected, const char* expr_str, CauseCode cause_code,
52 UpdaterInfo* info = nullptr) {
Tianjie Xuc4447322017-03-06 14:44:59 -080053 std::unique_ptr<Expr> e;
Tao Baoef0eb3b2016-11-14 21:29:52 -080054 int error_count = 0;
55 ASSERT_EQ(0, parse_string(expr_str, &e, &error_count));
56 ASSERT_EQ(0, error_count);
Tao Bao0c7839a2016-10-10 15:48:37 -070057
Tao Baoef0eb3b2016-11-14 21:29:52 -080058 State state(expr_str, info);
Tao Bao0c7839a2016-10-10 15:48:37 -070059
Tao Baoef0eb3b2016-11-14 21:29:52 -080060 std::string result;
61 bool status = Evaluate(&state, e, &result);
Tao Bao0c7839a2016-10-10 15:48:37 -070062
Tao Baoef0eb3b2016-11-14 21:29:52 -080063 if (expected == nullptr) {
64 ASSERT_FALSE(status);
65 } else {
66 ASSERT_TRUE(status);
67 ASSERT_STREQ(expected, result.c_str());
68 }
Tao Bao0c7839a2016-10-10 15:48:37 -070069
Tao Baoef0eb3b2016-11-14 21:29:52 -080070 // Error code is set in updater/updater.cpp only, by parsing State.errmsg.
71 ASSERT_EQ(kNoError, state.error_code);
Tao Bao361342c2016-02-08 11:15:50 -080072
Tao Baoef0eb3b2016-11-14 21:29:52 -080073 // Cause code should always be available.
74 ASSERT_EQ(cause_code, state.cause_code);
Tao Bao0c7839a2016-10-10 15:48:37 -070075}
76
Tianjie Xu56ebe622017-03-16 00:48:21 -070077static std::string get_sha1(const std::string& content) {
78 uint8_t digest[SHA_DIGEST_LENGTH];
79 SHA1(reinterpret_cast<const uint8_t*>(content.c_str()), content.size(), digest);
80 return print_sha1(digest);
81}
82
Tao Bao0c7839a2016-10-10 15:48:37 -070083class UpdaterTest : public ::testing::Test {
Tianjie Xu56ebe622017-03-16 00:48:21 -070084 protected:
85 virtual void SetUp() override {
86 RegisterBuiltins();
87 RegisterInstallFunctions();
88 RegisterBlockImageFunctions();
89 }
Tao Bao0c7839a2016-10-10 15:48:37 -070090};
91
92TEST_F(UpdaterTest, getprop) {
93 expect(android::base::GetProperty("ro.product.device", "").c_str(),
94 "getprop(\"ro.product.device\")",
Tao Bao361342c2016-02-08 11:15:50 -080095 kNoCause);
Tao Bao0c7839a2016-10-10 15:48:37 -070096
97 expect(android::base::GetProperty("ro.build.fingerprint", "").c_str(),
98 "getprop(\"ro.build.fingerprint\")",
Tao Bao361342c2016-02-08 11:15:50 -080099 kNoCause);
Tao Bao0c7839a2016-10-10 15:48:37 -0700100
101 // getprop() accepts only one parameter.
Tao Bao361342c2016-02-08 11:15:50 -0800102 expect(nullptr, "getprop()", kArgsParsingFailure);
103 expect(nullptr, "getprop(\"arg1\", \"arg2\")", kArgsParsingFailure);
104}
105
106TEST_F(UpdaterTest, sha1_check) {
107 // sha1_check(data) returns the SHA-1 of the data.
108 expect("81fe8bfe87576c3ecb22426f8e57847382917acf", "sha1_check(\"abcd\")", kNoCause);
109 expect("da39a3ee5e6b4b0d3255bfef95601890afd80709", "sha1_check(\"\")", kNoCause);
110
111 // sha1_check(data, sha1_hex, [sha1_hex, ...]) returns the matched SHA-1.
112 expect("81fe8bfe87576c3ecb22426f8e57847382917acf",
113 "sha1_check(\"abcd\", \"81fe8bfe87576c3ecb22426f8e57847382917acf\")",
114 kNoCause);
115
116 expect("81fe8bfe87576c3ecb22426f8e57847382917acf",
117 "sha1_check(\"abcd\", \"wrong_sha1\", \"81fe8bfe87576c3ecb22426f8e57847382917acf\")",
118 kNoCause);
119
120 // Or "" if there's no match.
121 expect("",
122 "sha1_check(\"abcd\", \"wrong_sha1\")",
123 kNoCause);
124
125 expect("",
126 "sha1_check(\"abcd\", \"wrong_sha1\", \"wrong_sha2\")",
127 kNoCause);
128
129 // sha1_check() expects at least one argument.
130 expect(nullptr, "sha1_check()", kArgsParsingFailure);
Tao Bao0c7839a2016-10-10 15:48:37 -0700131}
Tao Bao51d516e2016-11-03 14:49:01 -0700132
Tao Baodb56eb02017-03-23 06:34:20 -0700133TEST_F(UpdaterTest, apply_patch_check) {
134 // Zero-argument is not valid.
135 expect(nullptr, "apply_patch_check()", kArgsParsingFailure);
136
137 // File not found.
138 expect("", "apply_patch_check(\"/doesntexist\")", kNoCause);
139
140 std::string src_file = from_testdata_base("old.file");
141 std::string src_content;
142 ASSERT_TRUE(android::base::ReadFileToString(src_file, &src_content));
143 size_t src_size = src_content.size();
144 std::string src_hash = get_sha1(src_content);
145
146 // One-argument with EMMC:file:size:sha1 should pass the check.
147 std::string filename = android::base::Join(
148 std::vector<std::string>{ "EMMC", src_file, std::to_string(src_size), src_hash }, ":");
149 std::string cmd = "apply_patch_check(\"" + filename + "\")";
150 expect("t", cmd.c_str(), kNoCause);
151
152 // EMMC:file:(size-1):sha1:(size+1):sha1 should fail the check.
153 std::string filename_bad = android::base::Join(
154 std::vector<std::string>{ "EMMC", src_file, std::to_string(src_size - 1), src_hash,
155 std::to_string(src_size + 1), src_hash },
156 ":");
157 cmd = "apply_patch_check(\"" + filename_bad + "\")";
158 expect("", cmd.c_str(), kNoCause);
159
160 // EMMC:file:(size-1):sha1:size:sha1:(size+1):sha1 should pass the check.
161 filename_bad =
162 android::base::Join(std::vector<std::string>{ "EMMC", src_file, std::to_string(src_size - 1),
163 src_hash, std::to_string(src_size), src_hash,
164 std::to_string(src_size + 1), src_hash },
165 ":");
166 cmd = "apply_patch_check(\"" + filename_bad + "\")";
167 expect("t", cmd.c_str(), kNoCause);
168
169 // Multiple arguments.
170 cmd = "apply_patch_check(\"" + filename + "\", \"wrong_sha1\", \"wrong_sha2\")";
171 expect("", cmd.c_str(), kNoCause);
172
173 cmd = "apply_patch_check(\"" + filename + "\", \"wrong_sha1\", \"" + src_hash +
174 "\", \"wrong_sha2\")";
175 expect("t", cmd.c_str(), kNoCause);
176
177 cmd = "apply_patch_check(\"" + filename_bad + "\", \"wrong_sha1\", \"" + src_hash +
178 "\", \"wrong_sha2\")";
179 expect("t", cmd.c_str(), kNoCause);
180}
181
Tao Bao51d516e2016-11-03 14:49:01 -0700182TEST_F(UpdaterTest, file_getprop) {
183 // file_getprop() expects two arguments.
184 expect(nullptr, "file_getprop()", kArgsParsingFailure);
185 expect(nullptr, "file_getprop(\"arg1\")", kArgsParsingFailure);
186 expect(nullptr, "file_getprop(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
187
188 // File doesn't exist.
189 expect(nullptr, "file_getprop(\"/doesntexist\", \"key1\")", kFileGetPropFailure);
190
191 // Reject too large files (current limit = 65536).
192 TemporaryFile temp_file1;
193 std::string buffer(65540, '\0');
194 ASSERT_TRUE(android::base::WriteStringToFile(buffer, temp_file1.path));
195
196 // Read some keys.
197 TemporaryFile temp_file2;
198 std::string content("ro.product.name=tardis\n"
199 "# comment\n\n\n"
200 "ro.product.model\n"
201 "ro.product.board = magic \n");
202 ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file2.path));
203
204 std::string script1("file_getprop(\"" + std::string(temp_file2.path) +
205 "\", \"ro.product.name\")");
206 expect("tardis", script1.c_str(), kNoCause);
207
208 std::string script2("file_getprop(\"" + std::string(temp_file2.path) +
209 "\", \"ro.product.board\")");
210 expect("magic", script2.c_str(), kNoCause);
211
212 // No match.
213 std::string script3("file_getprop(\"" + std::string(temp_file2.path) +
214 "\", \"ro.product.wrong\")");
215 expect("", script3.c_str(), kNoCause);
216
217 std::string script4("file_getprop(\"" + std::string(temp_file2.path) +
218 "\", \"ro.product.name=\")");
219 expect("", script4.c_str(), kNoCause);
220
221 std::string script5("file_getprop(\"" + std::string(temp_file2.path) +
222 "\", \"ro.product.nam\")");
223 expect("", script5.c_str(), kNoCause);
224
225 std::string script6("file_getprop(\"" + std::string(temp_file2.path) +
226 "\", \"ro.product.model\")");
227 expect("", script6.c_str(), kNoCause);
228}
Tao Bao0831d0b2016-11-03 23:25:04 -0700229
Tom Marshall2c1a5792017-12-14 22:37:17 +0100230TEST_F(UpdaterTest, package_extract_dir) {
231 // package_extract_dir expects 2 arguments.
232 expect(nullptr, "package_extract_dir()", kArgsParsingFailure);
233 expect(nullptr, "package_extract_dir(\"arg1\")", kArgsParsingFailure);
234 expect(nullptr, "package_extract_dir(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
235
236 std::string zip_path = from_testdata_base("ziptest_valid.zip");
237 ZipArchiveHandle handle;
238 ASSERT_EQ(0, OpenArchive(zip_path.c_str(), &handle));
239
240 // Need to set up the ziphandle.
241 UpdaterInfo updater_info;
242 updater_info.package_zip = handle;
243
244 // Extract "b/c.txt" and "b/d.txt" with package_extract_dir("b", "<dir>").
245 TemporaryDir td;
246 std::string temp_dir(td.path);
247 std::string script("package_extract_dir(\"b\", \"" + temp_dir + "\")");
248 expect("t", script.c_str(), kNoCause, &updater_info);
249
250 // Verify.
251 std::string data;
252 std::string file_c = temp_dir + "/c.txt";
253 ASSERT_TRUE(android::base::ReadFileToString(file_c, &data));
254 ASSERT_EQ(kCTxtContents, data);
255
256 std::string file_d = temp_dir + "/d.txt";
257 ASSERT_TRUE(android::base::ReadFileToString(file_d, &data));
258 ASSERT_EQ(kDTxtContents, data);
259
260 // Modify the contents in order to retry. It's expected to be overwritten.
261 ASSERT_TRUE(android::base::WriteStringToFile("random", file_c));
262 ASSERT_TRUE(android::base::WriteStringToFile("random", file_d));
263
264 // Extract again and verify.
265 expect("t", script.c_str(), kNoCause, &updater_info);
266
267 ASSERT_TRUE(android::base::ReadFileToString(file_c, &data));
268 ASSERT_EQ(kCTxtContents, data);
269 ASSERT_TRUE(android::base::ReadFileToString(file_d, &data));
270 ASSERT_EQ(kDTxtContents, data);
271
272 // Clean up the temp files under td.
273 ASSERT_EQ(0, unlink(file_c.c_str()));
274 ASSERT_EQ(0, unlink(file_d.c_str()));
275
276 // Extracting "b/" (with slash) should give the same result.
277 script = "package_extract_dir(\"b/\", \"" + temp_dir + "\")";
278 expect("t", script.c_str(), kNoCause, &updater_info);
279
280 ASSERT_TRUE(android::base::ReadFileToString(file_c, &data));
281 ASSERT_EQ(kCTxtContents, data);
282 ASSERT_TRUE(android::base::ReadFileToString(file_d, &data));
283 ASSERT_EQ(kDTxtContents, data);
284
285 ASSERT_EQ(0, unlink(file_c.c_str()));
286 ASSERT_EQ(0, unlink(file_d.c_str()));
287
288 // Extracting "" is allowed. The entries will carry the path name.
289 script = "package_extract_dir(\"\", \"" + temp_dir + "\")";
290 expect("t", script.c_str(), kNoCause, &updater_info);
291
292 std::string file_a = temp_dir + "/a.txt";
293 ASSERT_TRUE(android::base::ReadFileToString(file_a, &data));
294 ASSERT_EQ(kATxtContents, data);
295 std::string file_b = temp_dir + "/b.txt";
296 ASSERT_TRUE(android::base::ReadFileToString(file_b, &data));
297 ASSERT_EQ(kBTxtContents, data);
298 std::string file_b_c = temp_dir + "/b/c.txt";
299 ASSERT_TRUE(android::base::ReadFileToString(file_b_c, &data));
300 ASSERT_EQ(kCTxtContents, data);
301 std::string file_b_d = temp_dir + "/b/d.txt";
302 ASSERT_TRUE(android::base::ReadFileToString(file_b_d, &data));
303 ASSERT_EQ(kDTxtContents, data);
304
305 ASSERT_EQ(0, unlink(file_a.c_str()));
306 ASSERT_EQ(0, unlink(file_b.c_str()));
307 ASSERT_EQ(0, unlink(file_b_c.c_str()));
308 ASSERT_EQ(0, unlink(file_b_d.c_str()));
309 ASSERT_EQ(0, rmdir((temp_dir + "/b").c_str()));
310
311 // Extracting non-existent entry should still give "t".
312 script = "package_extract_dir(\"doesntexist\", \"" + temp_dir + "\")";
313 expect("t", script.c_str(), kNoCause, &updater_info);
314
315 // Only relative zip_path is allowed.
316 script = "package_extract_dir(\"/b\", \"" + temp_dir + "\")";
317 expect("", script.c_str(), kNoCause, &updater_info);
318
319 // Only absolute dest_path is allowed.
320 script = "package_extract_dir(\"b\", \"path\")";
321 expect("", script.c_str(), kNoCause, &updater_info);
322
323 CloseArchive(handle);
324}
325
Tao Baoef0eb3b2016-11-14 21:29:52 -0800326// TODO: Test extracting to block device.
327TEST_F(UpdaterTest, package_extract_file) {
328 // package_extract_file expects 1 or 2 arguments.
329 expect(nullptr, "package_extract_file()", kArgsParsingFailure);
330 expect(nullptr, "package_extract_file(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
331
332 std::string zip_path = from_testdata_base("ziptest_valid.zip");
333 ZipArchiveHandle handle;
334 ASSERT_EQ(0, OpenArchive(zip_path.c_str(), &handle));
335
336 // Need to set up the ziphandle.
337 UpdaterInfo updater_info;
338 updater_info.package_zip = handle;
339
340 // Two-argument version.
341 TemporaryFile temp_file1;
342 std::string script("package_extract_file(\"a.txt\", \"" + std::string(temp_file1.path) + "\")");
343 expect("t", script.c_str(), kNoCause, &updater_info);
344
345 // Verify the extracted entry.
346 std::string data;
347 ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data));
348 ASSERT_EQ(kATxtContents, data);
349
350 // Now extract another entry to the same location, which should overwrite.
351 script = "package_extract_file(\"b.txt\", \"" + std::string(temp_file1.path) + "\")";
352 expect("t", script.c_str(), kNoCause, &updater_info);
353
354 ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data));
355 ASSERT_EQ(kBTxtContents, data);
356
357 // Missing zip entry. The two-argument version doesn't abort.
358 script = "package_extract_file(\"doesntexist\", \"" + std::string(temp_file1.path) + "\")";
359 expect("", script.c_str(), kNoCause, &updater_info);
360
361 // Extract to /dev/full should fail.
362 script = "package_extract_file(\"a.txt\", \"/dev/full\")";
363 expect("", script.c_str(), kNoCause, &updater_info);
364
365 // One-argument version.
366 script = "sha1_check(package_extract_file(\"a.txt\"))";
367 expect(kATxtSha1Sum.c_str(), script.c_str(), kNoCause, &updater_info);
368
369 script = "sha1_check(package_extract_file(\"b.txt\"))";
370 expect(kBTxtSha1Sum.c_str(), script.c_str(), kNoCause, &updater_info);
371
372 // Missing entry. The one-argument version aborts the evaluation.
373 script = "package_extract_file(\"doesntexist\")";
374 expect(nullptr, script.c_str(), kPackageExtractFileFailure, &updater_info);
375
376 CloseArchive(handle);
377}
Tao Baod0f30882016-11-03 23:52:01 -0700378
379TEST_F(UpdaterTest, write_value) {
380 // write_value() expects two arguments.
381 expect(nullptr, "write_value()", kArgsParsingFailure);
382 expect(nullptr, "write_value(\"arg1\")", kArgsParsingFailure);
383 expect(nullptr, "write_value(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
384
385 // filename cannot be empty.
386 expect(nullptr, "write_value(\"value\", \"\")", kArgsParsingFailure);
387
388 // Write some value to file.
389 TemporaryFile temp_file;
390 std::string value = "magicvalue";
391 std::string script("write_value(\"" + value + "\", \"" + std::string(temp_file.path) + "\")");
392 expect("t", script.c_str(), kNoCause);
393
394 // Verify the content.
395 std::string content;
396 ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content));
397 ASSERT_EQ(value, content);
398
399 // Allow writing empty string.
400 script = "write_value(\"\", \"" + std::string(temp_file.path) + "\")";
401 expect("t", script.c_str(), kNoCause);
402
403 // Verify the content.
404 ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content));
405 ASSERT_EQ("", content);
406
407 // It should fail gracefully when write fails.
408 script = "write_value(\"value\", \"/proc/0/file1\")";
409 expect("", script.c_str(), kNoCause);
410}
Tao Baobedf5fc2016-11-18 12:01:26 -0800411
412TEST_F(UpdaterTest, get_stage) {
413 // get_stage() expects one argument.
414 expect(nullptr, "get_stage()", kArgsParsingFailure);
415 expect(nullptr, "get_stage(\"arg1\", \"arg2\")", kArgsParsingFailure);
416 expect(nullptr, "get_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
417
418 // Set up a local file as BCB.
419 TemporaryFile tf;
420 std::string temp_file(tf.path);
421 bootloader_message boot;
422 strlcpy(boot.stage, "2/3", sizeof(boot.stage));
423 std::string err;
424 ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err));
425
426 // Can read the stage value.
427 std::string script("get_stage(\"" + temp_file + "\")");
428 expect("2/3", script.c_str(), kNoCause);
429
430 // Bad BCB path.
431 script = "get_stage(\"doesntexist\")";
432 expect("", script.c_str(), kNoCause);
433}
434
435TEST_F(UpdaterTest, set_stage) {
436 // set_stage() expects two arguments.
437 expect(nullptr, "set_stage()", kArgsParsingFailure);
438 expect(nullptr, "set_stage(\"arg1\")", kArgsParsingFailure);
439 expect(nullptr, "set_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
440
441 // Set up a local file as BCB.
442 TemporaryFile tf;
443 std::string temp_file(tf.path);
444 bootloader_message boot;
445 strlcpy(boot.command, "command", sizeof(boot.command));
446 strlcpy(boot.stage, "2/3", sizeof(boot.stage));
447 std::string err;
448 ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err));
449
450 // Write with set_stage().
451 std::string script("set_stage(\"" + temp_file + "\", \"1/3\")");
452 expect(tf.path, script.c_str(), kNoCause);
453
454 // Verify.
455 bootloader_message boot_verify;
456 ASSERT_TRUE(read_bootloader_message_from(&boot_verify, temp_file, &err));
457
458 // Stage should be updated, with command part untouched.
459 ASSERT_STREQ("1/3", boot_verify.stage);
460 ASSERT_STREQ(boot.command, boot_verify.command);
461
462 // Bad BCB path.
463 script = "set_stage(\"doesntexist\", \"1/3\")";
464 expect("", script.c_str(), kNoCause);
465
466 script = "set_stage(\"/dev/full\", \"1/3\")";
467 expect("", script.c_str(), kNoCause);
468}
Tao Bao9aa7ab52017-01-05 17:27:19 -0800469
470TEST_F(UpdaterTest, set_progress) {
471 // set_progress() expects one argument.
472 expect(nullptr, "set_progress()", kArgsParsingFailure);
473 expect(nullptr, "set_progress(\"arg1\", \"arg2\")", kArgsParsingFailure);
474
475 // Invalid progress argument.
476 expect(nullptr, "set_progress(\"arg1\")", kArgsParsingFailure);
477 expect(nullptr, "set_progress(\"3x+5\")", kArgsParsingFailure);
478 expect(nullptr, "set_progress(\".3.5\")", kArgsParsingFailure);
479
480 TemporaryFile tf;
481 UpdaterInfo updater_info;
482 updater_info.cmd_pipe = fdopen(tf.fd, "w");
483 expect(".52", "set_progress(\".52\")", kNoCause, &updater_info);
484 fflush(updater_info.cmd_pipe);
485
486 std::string cmd;
487 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd));
488 ASSERT_EQ(android::base::StringPrintf("set_progress %f\n", .52), cmd);
489 // recovery-updater protocol expects 2 tokens ("set_progress <frac>").
490 ASSERT_EQ(2U, android::base::Split(cmd, " ").size());
491}
492
493TEST_F(UpdaterTest, show_progress) {
494 // show_progress() expects two arguments.
495 expect(nullptr, "show_progress()", kArgsParsingFailure);
496 expect(nullptr, "show_progress(\"arg1\")", kArgsParsingFailure);
497 expect(nullptr, "show_progress(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
498
499 // Invalid progress arguments.
500 expect(nullptr, "show_progress(\"arg1\", \"arg2\")", kArgsParsingFailure);
501 expect(nullptr, "show_progress(\"3x+5\", \"10\")", kArgsParsingFailure);
502 expect(nullptr, "show_progress(\".3\", \"5a\")", kArgsParsingFailure);
503
504 TemporaryFile tf;
505 UpdaterInfo updater_info;
506 updater_info.cmd_pipe = fdopen(tf.fd, "w");
507 expect(".52", "show_progress(\".52\", \"10\")", kNoCause, &updater_info);
508 fflush(updater_info.cmd_pipe);
509
510 std::string cmd;
511 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd));
512 ASSERT_EQ(android::base::StringPrintf("progress %f %d\n", .52, 10), cmd);
513 // recovery-updater protocol expects 3 tokens ("progress <frac> <secs>").
514 ASSERT_EQ(3U, android::base::Split(cmd, " ").size());
515}
Tianjie Xu56ebe622017-03-16 00:48:21 -0700516
517TEST_F(UpdaterTest, block_image_update) {
518 // Create a zip file with new_data and patch_data.
519 TemporaryFile zip_file;
520 FILE* zip_file_ptr = fdopen(zip_file.fd, "wb");
521 ZipWriter zip_writer(zip_file_ptr);
522
523 // Add a dummy new data.
524 ASSERT_EQ(0, zip_writer.StartEntry("new_data", 0));
525 ASSERT_EQ(0, zip_writer.FinishEntry());
526
527 // Generate and add the patch data.
528 std::string src_content = std::string(4096, 'a') + std::string(4096, 'c');
529 std::string tgt_content = std::string(4096, 'b') + std::string(4096, 'd');
530 TemporaryFile patch_file;
531 ASSERT_EQ(0, bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(src_content.data()),
532 src_content.size(), reinterpret_cast<const uint8_t*>(tgt_content.data()),
533 tgt_content.size(), patch_file.path, nullptr));
534 std::string patch_content;
535 ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch_content));
536 ASSERT_EQ(0, zip_writer.StartEntry("patch_data", 0));
537 ASSERT_EQ(0, zip_writer.WriteBytes(patch_content.data(), patch_content.size()));
538 ASSERT_EQ(0, zip_writer.FinishEntry());
539
540 // Add two transfer lists. The first one contains a bsdiff; and we expect the update to succeed.
541 std::string src_hash = get_sha1(src_content);
542 std::string tgt_hash = get_sha1(tgt_content);
543 std::vector<std::string> transfer_list = {
544 "4",
545 "2",
546 "0",
547 "2",
548 "stash " + src_hash + " 2,0,2",
549 android::base::StringPrintf("bsdiff 0 %zu %s %s 2,0,2 2 - %s:2,0,2", patch_content.size(),
550 src_hash.c_str(), tgt_hash.c_str(), src_hash.c_str()),
551 "free " + src_hash,
552 };
553 ASSERT_EQ(0, zip_writer.StartEntry("transfer_list", 0));
554 std::string commands = android::base::Join(transfer_list, '\n');
555 ASSERT_EQ(0, zip_writer.WriteBytes(commands.data(), commands.size()));
556 ASSERT_EQ(0, zip_writer.FinishEntry());
557
558 // Stash and free some blocks, then fail the 2nd update intentionally.
559 std::vector<std::string> fail_transfer_list = {
560 "4",
561 "2",
562 "0",
563 "2",
564 "stash " + tgt_hash + " 2,0,2",
565 "free " + tgt_hash,
566 "fail",
567 };
568 ASSERT_EQ(0, zip_writer.StartEntry("fail_transfer_list", 0));
569 std::string fail_commands = android::base::Join(fail_transfer_list, '\n');
570 ASSERT_EQ(0, zip_writer.WriteBytes(fail_commands.data(), fail_commands.size()));
571 ASSERT_EQ(0, zip_writer.FinishEntry());
572 ASSERT_EQ(0, zip_writer.Finish());
573 ASSERT_EQ(0, fclose(zip_file_ptr));
574
575 MemMapping map;
Tao Baob656a152017-04-18 23:54:29 -0700576 ASSERT_TRUE(map.MapFile(zip_file.path));
Tianjie Xu56ebe622017-03-16 00:48:21 -0700577 ZipArchiveHandle handle;
578 ASSERT_EQ(0, OpenArchiveFromMemory(map.addr, map.length, zip_file.path, &handle));
579
580 // Set up the handler, command_pipe, patch offset & length.
581 UpdaterInfo updater_info;
582 updater_info.package_zip = handle;
583 TemporaryFile temp_pipe;
Tianjie Xude6735e2017-07-10 15:13:33 -0700584 updater_info.cmd_pipe = fopen(temp_pipe.path, "wbe");
Tianjie Xu56ebe622017-03-16 00:48:21 -0700585 updater_info.package_zip_addr = map.addr;
586 updater_info.package_zip_len = map.length;
587
588 // Execute the commands in the 1st transfer list.
589 TemporaryFile update_file;
590 ASSERT_TRUE(android::base::WriteStringToFile(src_content, update_file.path));
591 std::string script = "block_image_update(\"" + std::string(update_file.path) +
592 R"(", package_extract_file("transfer_list"), "new_data", "patch_data"))";
593 expect("t", script.c_str(), kNoCause, &updater_info);
594 // The update_file should be patched correctly.
595 std::string updated_content;
596 ASSERT_TRUE(android::base::ReadFileToString(update_file.path, &updated_content));
597 ASSERT_EQ(tgt_hash, get_sha1(updated_content));
598
599 // Expect the 2nd update to fail, but expect the stashed blocks to be freed.
600 script = "block_image_update(\"" + std::string(update_file.path) +
601 R"(", package_extract_file("fail_transfer_list"), "new_data", "patch_data"))";
602 expect("", script.c_str(), kNoCause, &updater_info);
603 // Updater generates the stash name based on the input file name.
604 std::string name_digest = get_sha1(update_file.path);
605 std::string stash_base = "/cache/recovery/" + name_digest;
606 ASSERT_EQ(0, access(stash_base.c_str(), F_OK));
607 ASSERT_EQ(-1, access((stash_base + tgt_hash).c_str(), F_OK));
608 ASSERT_EQ(0, rmdir(stash_base.c_str()));
609
610 ASSERT_EQ(0, fclose(updater_info.cmd_pipe));
611 CloseArchive(handle);
612}
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700613
614TEST_F(UpdaterTest, new_data_short_write) {
615 // Create a zip file with new_data.
616 TemporaryFile zip_file;
617 FILE* zip_file_ptr = fdopen(zip_file.fd, "wb");
618 ZipWriter zip_writer(zip_file_ptr);
619
620 // Add the empty new data.
621 ASSERT_EQ(0, zip_writer.StartEntry("empty_new_data", 0));
622 ASSERT_EQ(0, zip_writer.FinishEntry());
623 // Add the short written new data.
624 ASSERT_EQ(0, zip_writer.StartEntry("short_new_data", 0));
625 std::string new_data_short = std::string(10, 'a');
626 ASSERT_EQ(0, zip_writer.WriteBytes(new_data_short.data(), new_data_short.size()));
627 ASSERT_EQ(0, zip_writer.FinishEntry());
628 // Add the data of exactly one block.
629 ASSERT_EQ(0, zip_writer.StartEntry("exact_new_data", 0));
630 std::string new_data_exact = std::string(4096, 'a');
631 ASSERT_EQ(0, zip_writer.WriteBytes(new_data_exact.data(), new_data_exact.size()));
632 ASSERT_EQ(0, zip_writer.FinishEntry());
633 // Add a dummy patch data.
634 ASSERT_EQ(0, zip_writer.StartEntry("patch_data", 0));
635 ASSERT_EQ(0, zip_writer.FinishEntry());
636
637 std::vector<std::string> transfer_list = {
638 "4",
639 "1",
640 "0",
641 "0",
642 "new 2,0,1",
643 };
644 ASSERT_EQ(0, zip_writer.StartEntry("transfer_list", 0));
645 std::string commands = android::base::Join(transfer_list, '\n');
646 ASSERT_EQ(0, zip_writer.WriteBytes(commands.data(), commands.size()));
647 ASSERT_EQ(0, zip_writer.FinishEntry());
648 ASSERT_EQ(0, zip_writer.Finish());
649 ASSERT_EQ(0, fclose(zip_file_ptr));
650
651 MemMapping map;
Tao Baob656a152017-04-18 23:54:29 -0700652 ASSERT_TRUE(map.MapFile(zip_file.path));
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700653 ZipArchiveHandle handle;
654 ASSERT_EQ(0, OpenArchiveFromMemory(map.addr, map.length, zip_file.path, &handle));
655
656 // Set up the handler, command_pipe, patch offset & length.
657 UpdaterInfo updater_info;
658 updater_info.package_zip = handle;
659 TemporaryFile temp_pipe;
Tianjie Xude6735e2017-07-10 15:13:33 -0700660 updater_info.cmd_pipe = fopen(temp_pipe.path, "wbe");
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700661 updater_info.package_zip_addr = map.addr;
662 updater_info.package_zip_len = map.length;
663
664 // Updater should report the failure gracefully rather than stuck in deadlock.
665 TemporaryFile update_file;
666 std::string script_empty_data = "block_image_update(\"" + std::string(update_file.path) +
667 R"(", package_extract_file("transfer_list"), "empty_new_data", "patch_data"))";
668 expect("", script_empty_data.c_str(), kNoCause, &updater_info);
669
670 std::string script_short_data = "block_image_update(\"" + std::string(update_file.path) +
671 R"(", package_extract_file("transfer_list"), "short_new_data", "patch_data"))";
672 expect("", script_short_data.c_str(), kNoCause, &updater_info);
673
674 // Expect to write 1 block of new data successfully.
675 std::string script_exact_data = "block_image_update(\"" + std::string(update_file.path) +
676 R"(", package_extract_file("transfer_list"), "exact_new_data", "patch_data"))";
677 expect("t", script_exact_data.c_str(), kNoCause, &updater_info);
Tianjie Xu107a34f2017-06-29 17:04:21 -0700678 CloseArchive(handle);
679}
680
681TEST_F(UpdaterTest, brotli_new_data) {
682 // Create a zip file with new_data.
683 TemporaryFile zip_file;
684 FILE* zip_file_ptr = fdopen(zip_file.fd, "wb");
685 ZipWriter zip_writer(zip_file_ptr);
686
687 // Add a brotli compressed new data entry.
688 ASSERT_EQ(0, zip_writer.StartEntry("new.dat.br", 0));
689
690 auto generator = []() { return rand() % 128; };
Tianjie Xud9759e82017-07-21 21:06:52 +0000691 // Generate 100 blocks of random data.
Tianjie Xu107a34f2017-06-29 17:04:21 -0700692 std::string brotli_new_data;
Tianjie Xud9759e82017-07-21 21:06:52 +0000693 brotli_new_data.reserve(4096 * 100);
694 generate_n(back_inserter(brotli_new_data), 4096 * 100, generator);
Tianjie Xu107a34f2017-06-29 17:04:21 -0700695
696 size_t encoded_size = BrotliEncoderMaxCompressedSize(brotli_new_data.size());
697 std::vector<uint8_t> encoded_data(encoded_size);
698 ASSERT_TRUE(BrotliEncoderCompress(
699 BROTLI_DEFAULT_QUALITY, BROTLI_DEFAULT_WINDOW, BROTLI_DEFAULT_MODE, brotli_new_data.size(),
700 reinterpret_cast<const uint8_t*>(brotli_new_data.data()), &encoded_size, encoded_data.data()));
701
702 ASSERT_EQ(0, zip_writer.WriteBytes(encoded_data.data(), encoded_size));
703 ASSERT_EQ(0, zip_writer.FinishEntry());
704 // Add a dummy patch data.
705 ASSERT_EQ(0, zip_writer.StartEntry("patch_data", 0));
706 ASSERT_EQ(0, zip_writer.FinishEntry());
707
Tianjie Xud9759e82017-07-21 21:06:52 +0000708 // Write a few small chunks of new data, then a large chunk, and finally a few small chunks.
709 // This helps us to catch potential short writes.
Tianjie Xu107a34f2017-06-29 17:04:21 -0700710 std::vector<std::string> transfer_list = {
Tianjie Xud9759e82017-07-21 21:06:52 +0000711 "4",
712 "100",
713 "0",
714 "0",
715 "new 2,0,1",
716 "new 2,1,2",
717 "new 4,2,50,50,97",
718 "new 2,97,98",
719 "new 2,98,99",
720 "new 2,99,100",
Tianjie Xu107a34f2017-06-29 17:04:21 -0700721 };
722 ASSERT_EQ(0, zip_writer.StartEntry("transfer_list", 0));
723 std::string commands = android::base::Join(transfer_list, '\n');
724 ASSERT_EQ(0, zip_writer.WriteBytes(commands.data(), commands.size()));
725 ASSERT_EQ(0, zip_writer.FinishEntry());
726 ASSERT_EQ(0, zip_writer.Finish());
727 ASSERT_EQ(0, fclose(zip_file_ptr));
728
729 MemMapping map;
730 ASSERT_TRUE(map.MapFile(zip_file.path));
731 ZipArchiveHandle handle;
732 ASSERT_EQ(0, OpenArchiveFromMemory(map.addr, map.length, zip_file.path, &handle));
733
734 // Set up the handler, command_pipe, patch offset & length.
735 UpdaterInfo updater_info;
736 updater_info.package_zip = handle;
737 TemporaryFile temp_pipe;
738 updater_info.cmd_pipe = fopen(temp_pipe.path, "wb");
739 updater_info.package_zip_addr = map.addr;
740 updater_info.package_zip_len = map.length;
741
742 // Check if we can decompress the new data correctly.
743 TemporaryFile update_file;
744 std::string script_new_data =
745 "block_image_update(\"" + std::string(update_file.path) +
746 R"(", package_extract_file("transfer_list"), "new.dat.br", "patch_data"))";
747 expect("t", script_new_data.c_str(), kNoCause, &updater_info);
748
749 std::string updated_content;
750 ASSERT_TRUE(android::base::ReadFileToString(update_file.path, &updated_content));
751 ASSERT_EQ(brotli_new_data, updated_content);
752 CloseArchive(handle);
Tianjie Xu3a8d98d2017-04-03 20:01:17 -0700753}