blob: caf8157f4aa0a31ee4e9e0bda7a200298a0f3dc4 [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
Tom Marshall8e2579f2017-08-23 18:14:00 +0000227TEST_F(UpdaterTest, delete) {
228 // Delete none.
229 expect("0", "delete()", kNoCause);
230 expect("0", "delete(\"/doesntexist\")", kNoCause);
231 expect("0", "delete(\"/doesntexist1\", \"/doesntexist2\")", kNoCause);
232 expect("0", "delete(\"/doesntexist1\", \"/doesntexist2\", \"/doesntexist3\")", kNoCause);
233
234 // Delete one file.
235 TemporaryFile temp_file1;
236 ASSERT_TRUE(android::base::WriteStringToFile("abc", temp_file1.path));
237 std::string script1("delete(\"" + std::string(temp_file1.path) + "\")");
238 expect("1", script1.c_str(), kNoCause);
239
240 // Delete two files.
241 TemporaryFile temp_file2;
242 ASSERT_TRUE(android::base::WriteStringToFile("abc", temp_file2.path));
243 TemporaryFile temp_file3;
244 ASSERT_TRUE(android::base::WriteStringToFile("abc", temp_file3.path));
245 std::string script2("delete(\"" + std::string(temp_file2.path) + "\", \"" +
246 std::string(temp_file3.path) + "\")");
247 expect("2", script2.c_str(), kNoCause);
248
249 // Delete already deleted files.
250 expect("0", script2.c_str(), kNoCause);
251
252 // Delete one out of three.
253 TemporaryFile temp_file4;
254 ASSERT_TRUE(android::base::WriteStringToFile("abc", temp_file4.path));
255 std::string script3("delete(\"/doesntexist1\", \"" + std::string(temp_file4.path) +
256 "\", \"/doesntexist2\")");
257 expect("1", script3.c_str(), kNoCause);
258}
259
260TEST_F(UpdaterTest, rename) {
261 // rename() expects two arguments.
262 expect(nullptr, "rename()", kArgsParsingFailure);
263 expect(nullptr, "rename(\"arg1\")", kArgsParsingFailure);
264 expect(nullptr, "rename(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
265
266 // src_name or dst_name cannot be empty.
267 expect(nullptr, "rename(\"\", \"arg2\")", kArgsParsingFailure);
268 expect(nullptr, "rename(\"arg1\", \"\")", kArgsParsingFailure);
269
270 // File doesn't exist (both of src and dst).
271 expect(nullptr, "rename(\"/doesntexist\", \"/doesntexisteither\")" , kFileRenameFailure);
272
273 // Can't create parent directory.
274 TemporaryFile temp_file1;
275 ASSERT_TRUE(android::base::WriteStringToFile("abc", temp_file1.path));
276 std::string script1("rename(\"" + std::string(temp_file1.path) + "\", \"/proc/0/file1\")");
277 expect(nullptr, script1.c_str(), kFileRenameFailure);
278
279 // Rename.
280 TemporaryFile temp_file2;
281 std::string script2("rename(\"" + std::string(temp_file1.path) + "\", \"" +
282 std::string(temp_file2.path) + "\")");
283 expect(temp_file2.path, script2.c_str(), kNoCause);
284
285 // Already renamed.
286 expect(temp_file2.path, script2.c_str(), kNoCause);
287
288 // Parents create successfully.
289 TemporaryFile temp_file3;
290 TemporaryDir td;
291 std::string temp_dir(td.path);
292 std::string dst_file = temp_dir + "/aaa/bbb/a.txt";
293 std::string script3("rename(\"" + std::string(temp_file3.path) + "\", \"" + dst_file + "\")");
294 expect(dst_file.c_str(), script3.c_str(), kNoCause);
295
296 // Clean up the temp files under td.
297 ASSERT_EQ(0, unlink(dst_file.c_str()));
298 ASSERT_EQ(0, rmdir((temp_dir + "/aaa/bbb").c_str()));
299 ASSERT_EQ(0, rmdir((temp_dir + "/aaa").c_str()));
300}
301
302TEST_F(UpdaterTest, symlink) {
303 // symlink expects 1+ argument.
304 expect(nullptr, "symlink()", kArgsParsingFailure);
305
306 // symlink should fail if src is an empty string.
307 TemporaryFile temp_file1;
308 std::string script1("symlink(\"" + std::string(temp_file1.path) + "\", \"\")");
309 expect(nullptr, script1.c_str(), kSymlinkFailure);
310
311 std::string script2("symlink(\"" + std::string(temp_file1.path) + "\", \"src1\", \"\")");
312 expect(nullptr, script2.c_str(), kSymlinkFailure);
313
314 // symlink failed to remove old src.
315 std::string script3("symlink(\"" + std::string(temp_file1.path) + "\", \"/proc\")");
316 expect(nullptr, script3.c_str(), kSymlinkFailure);
317
318 // symlink can create symlinks.
319 TemporaryFile temp_file;
320 std::string content = "magicvalue";
321 ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file.path));
322
323 TemporaryDir td;
324 std::string src1 = std::string(td.path) + "/symlink1";
325 std::string src2 = std::string(td.path) + "/symlink2";
326 std::string script4("symlink(\"" + std::string(temp_file.path) + "\", \"" +
327 src1 + "\", \"" + src2 + "\")");
328 expect("t", script4.c_str(), kNoCause);
329
330 // Verify the created symlinks.
331 struct stat sb;
332 ASSERT_TRUE(lstat(src1.c_str(), &sb) == 0 && S_ISLNK(sb.st_mode));
333 ASSERT_TRUE(lstat(src2.c_str(), &sb) == 0 && S_ISLNK(sb.st_mode));
334
335 // Clean up the leftovers.
336 ASSERT_EQ(0, unlink(src1.c_str()));
337 ASSERT_EQ(0, unlink(src2.c_str()));
338}
339
Tao Bao1036d362016-11-17 22:49:56 -0800340TEST_F(UpdaterTest, package_extract_dir) {
341 // package_extract_dir expects 2 arguments.
342 expect(nullptr, "package_extract_dir()", kArgsParsingFailure);
343 expect(nullptr, "package_extract_dir(\"arg1\")", kArgsParsingFailure);
344 expect(nullptr, "package_extract_dir(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
345
346 std::string zip_path = from_testdata_base("ziptest_valid.zip");
347 ZipArchiveHandle handle;
348 ASSERT_EQ(0, OpenArchive(zip_path.c_str(), &handle));
349
350 // Need to set up the ziphandle.
351 UpdaterInfo updater_info;
352 updater_info.package_zip = handle;
353
354 // Extract "b/c.txt" and "b/d.txt" with package_extract_dir("b", "<dir>").
355 TemporaryDir td;
356 std::string temp_dir(td.path);
357 std::string script("package_extract_dir(\"b\", \"" + temp_dir + "\")");
358 expect("t", script.c_str(), kNoCause, &updater_info);
359
360 // Verify.
361 std::string data;
362 std::string file_c = temp_dir + "/c.txt";
363 ASSERT_TRUE(android::base::ReadFileToString(file_c, &data));
364 ASSERT_EQ(kCTxtContents, data);
365
366 std::string file_d = temp_dir + "/d.txt";
367 ASSERT_TRUE(android::base::ReadFileToString(file_d, &data));
368 ASSERT_EQ(kDTxtContents, data);
369
370 // Modify the contents in order to retry. It's expected to be overwritten.
371 ASSERT_TRUE(android::base::WriteStringToFile("random", file_c));
372 ASSERT_TRUE(android::base::WriteStringToFile("random", file_d));
373
374 // Extract again and verify.
375 expect("t", script.c_str(), kNoCause, &updater_info);
376
377 ASSERT_TRUE(android::base::ReadFileToString(file_c, &data));
378 ASSERT_EQ(kCTxtContents, data);
379 ASSERT_TRUE(android::base::ReadFileToString(file_d, &data));
380 ASSERT_EQ(kDTxtContents, data);
381
382 // Clean up the temp files under td.
383 ASSERT_EQ(0, unlink(file_c.c_str()));
384 ASSERT_EQ(0, unlink(file_d.c_str()));
385
386 // Extracting "b/" (with slash) should give the same result.
387 script = "package_extract_dir(\"b/\", \"" + temp_dir + "\")";
388 expect("t", script.c_str(), kNoCause, &updater_info);
389
390 ASSERT_TRUE(android::base::ReadFileToString(file_c, &data));
391 ASSERT_EQ(kCTxtContents, data);
392 ASSERT_TRUE(android::base::ReadFileToString(file_d, &data));
393 ASSERT_EQ(kDTxtContents, data);
394
395 ASSERT_EQ(0, unlink(file_c.c_str()));
396 ASSERT_EQ(0, unlink(file_d.c_str()));
397
398 // Extracting "" is allowed. The entries will carry the path name.
399 script = "package_extract_dir(\"\", \"" + temp_dir + "\")";
400 expect("t", script.c_str(), kNoCause, &updater_info);
401
402 std::string file_a = temp_dir + "/a.txt";
403 ASSERT_TRUE(android::base::ReadFileToString(file_a, &data));
404 ASSERT_EQ(kATxtContents, data);
405 std::string file_b = temp_dir + "/b.txt";
406 ASSERT_TRUE(android::base::ReadFileToString(file_b, &data));
407 ASSERT_EQ(kBTxtContents, data);
408 std::string file_b_c = temp_dir + "/b/c.txt";
409 ASSERT_TRUE(android::base::ReadFileToString(file_b_c, &data));
410 ASSERT_EQ(kCTxtContents, data);
411 std::string file_b_d = temp_dir + "/b/d.txt";
412 ASSERT_TRUE(android::base::ReadFileToString(file_b_d, &data));
413 ASSERT_EQ(kDTxtContents, data);
414
415 ASSERT_EQ(0, unlink(file_a.c_str()));
416 ASSERT_EQ(0, unlink(file_b.c_str()));
417 ASSERT_EQ(0, unlink(file_b_c.c_str()));
418 ASSERT_EQ(0, unlink(file_b_d.c_str()));
419 ASSERT_EQ(0, rmdir((temp_dir + "/b").c_str()));
420
421 // Extracting non-existent entry should still give "t".
422 script = "package_extract_dir(\"doesntexist\", \"" + temp_dir + "\")";
423 expect("t", script.c_str(), kNoCause, &updater_info);
424
425 // Only relative zip_path is allowed.
426 script = "package_extract_dir(\"/b\", \"" + temp_dir + "\")";
427 expect("", script.c_str(), kNoCause, &updater_info);
428
429 // Only absolute dest_path is allowed.
430 script = "package_extract_dir(\"b\", \"path\")";
431 expect("", script.c_str(), kNoCause, &updater_info);
432
433 CloseArchive(handle);
434}
435
Tao Baoef0eb3b2016-11-14 21:29:52 -0800436// TODO: Test extracting to block device.
437TEST_F(UpdaterTest, package_extract_file) {
438 // package_extract_file expects 1 or 2 arguments.
439 expect(nullptr, "package_extract_file()", kArgsParsingFailure);
440 expect(nullptr, "package_extract_file(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
441
442 std::string zip_path = from_testdata_base("ziptest_valid.zip");
443 ZipArchiveHandle handle;
444 ASSERT_EQ(0, OpenArchive(zip_path.c_str(), &handle));
445
446 // Need to set up the ziphandle.
447 UpdaterInfo updater_info;
448 updater_info.package_zip = handle;
449
450 // Two-argument version.
451 TemporaryFile temp_file1;
452 std::string script("package_extract_file(\"a.txt\", \"" + std::string(temp_file1.path) + "\")");
453 expect("t", script.c_str(), kNoCause, &updater_info);
454
455 // Verify the extracted entry.
456 std::string data;
457 ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data));
458 ASSERT_EQ(kATxtContents, data);
459
460 // Now extract another entry to the same location, which should overwrite.
461 script = "package_extract_file(\"b.txt\", \"" + std::string(temp_file1.path) + "\")";
462 expect("t", script.c_str(), kNoCause, &updater_info);
463
464 ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data));
465 ASSERT_EQ(kBTxtContents, data);
466
467 // Missing zip entry. The two-argument version doesn't abort.
468 script = "package_extract_file(\"doesntexist\", \"" + std::string(temp_file1.path) + "\")";
469 expect("", script.c_str(), kNoCause, &updater_info);
470
471 // Extract to /dev/full should fail.
472 script = "package_extract_file(\"a.txt\", \"/dev/full\")";
473 expect("", script.c_str(), kNoCause, &updater_info);
474
475 // One-argument version.
476 script = "sha1_check(package_extract_file(\"a.txt\"))";
477 expect(kATxtSha1Sum.c_str(), script.c_str(), kNoCause, &updater_info);
478
479 script = "sha1_check(package_extract_file(\"b.txt\"))";
480 expect(kBTxtSha1Sum.c_str(), script.c_str(), kNoCause, &updater_info);
481
482 // Missing entry. The one-argument version aborts the evaluation.
483 script = "package_extract_file(\"doesntexist\")";
484 expect(nullptr, script.c_str(), kPackageExtractFileFailure, &updater_info);
485
486 CloseArchive(handle);
487}
Tao Baod0f30882016-11-03 23:52:01 -0700488
489TEST_F(UpdaterTest, write_value) {
490 // write_value() expects two arguments.
491 expect(nullptr, "write_value()", kArgsParsingFailure);
492 expect(nullptr, "write_value(\"arg1\")", kArgsParsingFailure);
493 expect(nullptr, "write_value(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
494
495 // filename cannot be empty.
496 expect(nullptr, "write_value(\"value\", \"\")", kArgsParsingFailure);
497
498 // Write some value to file.
499 TemporaryFile temp_file;
500 std::string value = "magicvalue";
501 std::string script("write_value(\"" + value + "\", \"" + std::string(temp_file.path) + "\")");
502 expect("t", script.c_str(), kNoCause);
503
504 // Verify the content.
505 std::string content;
506 ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content));
507 ASSERT_EQ(value, content);
508
509 // Allow writing empty string.
510 script = "write_value(\"\", \"" + std::string(temp_file.path) + "\")";
511 expect("t", script.c_str(), kNoCause);
512
513 // Verify the content.
514 ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content));
515 ASSERT_EQ("", content);
516
517 // It should fail gracefully when write fails.
518 script = "write_value(\"value\", \"/proc/0/file1\")";
519 expect("", script.c_str(), kNoCause);
520}
Tao Baobedf5fc2016-11-18 12:01:26 -0800521
522TEST_F(UpdaterTest, get_stage) {
523 // get_stage() expects one argument.
524 expect(nullptr, "get_stage()", kArgsParsingFailure);
525 expect(nullptr, "get_stage(\"arg1\", \"arg2\")", kArgsParsingFailure);
526 expect(nullptr, "get_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
527
528 // Set up a local file as BCB.
529 TemporaryFile tf;
530 std::string temp_file(tf.path);
531 bootloader_message boot;
532 strlcpy(boot.stage, "2/3", sizeof(boot.stage));
533 std::string err;
534 ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err));
535
536 // Can read the stage value.
537 std::string script("get_stage(\"" + temp_file + "\")");
538 expect("2/3", script.c_str(), kNoCause);
539
540 // Bad BCB path.
541 script = "get_stage(\"doesntexist\")";
542 expect("", script.c_str(), kNoCause);
543}
544
545TEST_F(UpdaterTest, set_stage) {
546 // set_stage() expects two arguments.
547 expect(nullptr, "set_stage()", kArgsParsingFailure);
548 expect(nullptr, "set_stage(\"arg1\")", kArgsParsingFailure);
549 expect(nullptr, "set_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
550
551 // Set up a local file as BCB.
552 TemporaryFile tf;
553 std::string temp_file(tf.path);
554 bootloader_message boot;
555 strlcpy(boot.command, "command", sizeof(boot.command));
556 strlcpy(boot.stage, "2/3", sizeof(boot.stage));
557 std::string err;
558 ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err));
559
560 // Write with set_stage().
561 std::string script("set_stage(\"" + temp_file + "\", \"1/3\")");
562 expect(tf.path, script.c_str(), kNoCause);
563
564 // Verify.
565 bootloader_message boot_verify;
566 ASSERT_TRUE(read_bootloader_message_from(&boot_verify, temp_file, &err));
567
568 // Stage should be updated, with command part untouched.
569 ASSERT_STREQ("1/3", boot_verify.stage);
570 ASSERT_STREQ(boot.command, boot_verify.command);
571
572 // Bad BCB path.
573 script = "set_stage(\"doesntexist\", \"1/3\")";
574 expect("", script.c_str(), kNoCause);
575
576 script = "set_stage(\"/dev/full\", \"1/3\")";
577 expect("", script.c_str(), kNoCause);
578}
Tao Bao9aa7ab52017-01-05 17:27:19 -0800579
580TEST_F(UpdaterTest, set_progress) {
581 // set_progress() expects one argument.
582 expect(nullptr, "set_progress()", kArgsParsingFailure);
583 expect(nullptr, "set_progress(\"arg1\", \"arg2\")", kArgsParsingFailure);
584
585 // Invalid progress argument.
586 expect(nullptr, "set_progress(\"arg1\")", kArgsParsingFailure);
587 expect(nullptr, "set_progress(\"3x+5\")", kArgsParsingFailure);
588 expect(nullptr, "set_progress(\".3.5\")", kArgsParsingFailure);
589
590 TemporaryFile tf;
591 UpdaterInfo updater_info;
592 updater_info.cmd_pipe = fdopen(tf.fd, "w");
593 expect(".52", "set_progress(\".52\")", kNoCause, &updater_info);
594 fflush(updater_info.cmd_pipe);
595
596 std::string cmd;
597 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd));
598 ASSERT_EQ(android::base::StringPrintf("set_progress %f\n", .52), cmd);
599 // recovery-updater protocol expects 2 tokens ("set_progress <frac>").
600 ASSERT_EQ(2U, android::base::Split(cmd, " ").size());
601}
602
603TEST_F(UpdaterTest, show_progress) {
604 // show_progress() expects two arguments.
605 expect(nullptr, "show_progress()", kArgsParsingFailure);
606 expect(nullptr, "show_progress(\"arg1\")", kArgsParsingFailure);
607 expect(nullptr, "show_progress(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
608
609 // Invalid progress arguments.
610 expect(nullptr, "show_progress(\"arg1\", \"arg2\")", kArgsParsingFailure);
611 expect(nullptr, "show_progress(\"3x+5\", \"10\")", kArgsParsingFailure);
612 expect(nullptr, "show_progress(\".3\", \"5a\")", kArgsParsingFailure);
613
614 TemporaryFile tf;
615 UpdaterInfo updater_info;
616 updater_info.cmd_pipe = fdopen(tf.fd, "w");
617 expect(".52", "show_progress(\".52\", \"10\")", kNoCause, &updater_info);
618 fflush(updater_info.cmd_pipe);
619
620 std::string cmd;
621 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd));
622 ASSERT_EQ(android::base::StringPrintf("progress %f %d\n", .52, 10), cmd);
623 // recovery-updater protocol expects 3 tokens ("progress <frac> <secs>").
624 ASSERT_EQ(3U, android::base::Split(cmd, " ").size());
625}
Tianjie Xu56ebe622017-03-16 00:48:21 -0700626
627TEST_F(UpdaterTest, block_image_update) {
628 // Create a zip file with new_data and patch_data.
629 TemporaryFile zip_file;
630 FILE* zip_file_ptr = fdopen(zip_file.fd, "wb");
631 ZipWriter zip_writer(zip_file_ptr);
632
633 // Add a dummy new data.
634 ASSERT_EQ(0, zip_writer.StartEntry("new_data", 0));
635 ASSERT_EQ(0, zip_writer.FinishEntry());
636
637 // Generate and add the patch data.
638 std::string src_content = std::string(4096, 'a') + std::string(4096, 'c');
639 std::string tgt_content = std::string(4096, 'b') + std::string(4096, 'd');
640 TemporaryFile patch_file;
641 ASSERT_EQ(0, bsdiff::bsdiff(reinterpret_cast<const uint8_t*>(src_content.data()),
642 src_content.size(), reinterpret_cast<const uint8_t*>(tgt_content.data()),
643 tgt_content.size(), patch_file.path, nullptr));
644 std::string patch_content;
645 ASSERT_TRUE(android::base::ReadFileToString(patch_file.path, &patch_content));
646 ASSERT_EQ(0, zip_writer.StartEntry("patch_data", 0));
647 ASSERT_EQ(0, zip_writer.WriteBytes(patch_content.data(), patch_content.size()));
648 ASSERT_EQ(0, zip_writer.FinishEntry());
649
650 // Add two transfer lists. The first one contains a bsdiff; and we expect the update to succeed.
651 std::string src_hash = get_sha1(src_content);
652 std::string tgt_hash = get_sha1(tgt_content);
653 std::vector<std::string> transfer_list = {
654 "4",
655 "2",
656 "0",
657 "2",
658 "stash " + src_hash + " 2,0,2",
659 android::base::StringPrintf("bsdiff 0 %zu %s %s 2,0,2 2 - %s:2,0,2", patch_content.size(),
660 src_hash.c_str(), tgt_hash.c_str(), src_hash.c_str()),
661 "free " + src_hash,
662 };
663 ASSERT_EQ(0, zip_writer.StartEntry("transfer_list", 0));
664 std::string commands = android::base::Join(transfer_list, '\n');
665 ASSERT_EQ(0, zip_writer.WriteBytes(commands.data(), commands.size()));
666 ASSERT_EQ(0, zip_writer.FinishEntry());
667
668 // Stash and free some blocks, then fail the 2nd update intentionally.
669 std::vector<std::string> fail_transfer_list = {
670 "4",
671 "2",
672 "0",
673 "2",
674 "stash " + tgt_hash + " 2,0,2",
675 "free " + tgt_hash,
676 "fail",
677 };
678 ASSERT_EQ(0, zip_writer.StartEntry("fail_transfer_list", 0));
679 std::string fail_commands = android::base::Join(fail_transfer_list, '\n');
680 ASSERT_EQ(0, zip_writer.WriteBytes(fail_commands.data(), fail_commands.size()));
681 ASSERT_EQ(0, zip_writer.FinishEntry());
682 ASSERT_EQ(0, zip_writer.Finish());
683 ASSERT_EQ(0, fclose(zip_file_ptr));
684
685 MemMapping map;
686 ASSERT_EQ(0, sysMapFile(zip_file.path, &map));
687 ZipArchiveHandle handle;
688 ASSERT_EQ(0, OpenArchiveFromMemory(map.addr, map.length, zip_file.path, &handle));
689
690 // Set up the handler, command_pipe, patch offset & length.
691 UpdaterInfo updater_info;
692 updater_info.package_zip = handle;
693 TemporaryFile temp_pipe;
694 updater_info.cmd_pipe = fopen(temp_pipe.path, "wb");
695 updater_info.package_zip_addr = map.addr;
696 updater_info.package_zip_len = map.length;
697
698 // Execute the commands in the 1st transfer list.
699 TemporaryFile update_file;
700 ASSERT_TRUE(android::base::WriteStringToFile(src_content, update_file.path));
701 std::string script = "block_image_update(\"" + std::string(update_file.path) +
702 R"(", package_extract_file("transfer_list"), "new_data", "patch_data"))";
703 expect("t", script.c_str(), kNoCause, &updater_info);
704 // The update_file should be patched correctly.
705 std::string updated_content;
706 ASSERT_TRUE(android::base::ReadFileToString(update_file.path, &updated_content));
707 ASSERT_EQ(tgt_hash, get_sha1(updated_content));
708
709 // Expect the 2nd update to fail, but expect the stashed blocks to be freed.
710 script = "block_image_update(\"" + std::string(update_file.path) +
711 R"(", package_extract_file("fail_transfer_list"), "new_data", "patch_data"))";
712 expect("", script.c_str(), kNoCause, &updater_info);
713 // Updater generates the stash name based on the input file name.
714 std::string name_digest = get_sha1(update_file.path);
715 std::string stash_base = "/cache/recovery/" + name_digest;
716 ASSERT_EQ(0, access(stash_base.c_str(), F_OK));
717 ASSERT_EQ(-1, access((stash_base + tgt_hash).c_str(), F_OK));
718 ASSERT_EQ(0, rmdir(stash_base.c_str()));
719
720 ASSERT_EQ(0, fclose(updater_info.cmd_pipe));
721 CloseArchive(handle);
722}