blob: 8c4bdbaa4b44562cec30b6f16e3eac49dc761a0a [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
Tao Bao0c7839a2016-10-10 15:48:37 -070022#include <string>
23
Tao Bao51d516e2016-11-03 14:49:01 -070024#include <android-base/file.h>
Tao Bao0c7839a2016-10-10 15:48:37 -070025#include <android-base/properties.h>
Tao Bao9aa7ab52017-01-05 17:27:19 -080026#include <android-base/stringprintf.h>
27#include <android-base/strings.h>
Tao Bao51d516e2016-11-03 14:49:01 -070028#include <android-base/test_utils.h>
Tao Baobedf5fc2016-11-18 12:01:26 -080029#include <bootloader_message/bootloader_message.h>
Tao Bao0c7839a2016-10-10 15:48:37 -070030#include <gtest/gtest.h>
Tao Baoef0eb3b2016-11-14 21:29:52 -080031#include <ziparchive/zip_archive.h>
Tao Bao0c7839a2016-10-10 15:48:37 -070032
Tao Baoef0eb3b2016-11-14 21:29:52 -080033#include "common/test_constants.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070034#include "edify/expr.h"
35#include "error_code.h"
36#include "updater/install.h"
Tao Baoef0eb3b2016-11-14 21:29:52 -080037#include "updater/updater.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070038
39struct selabel_handle *sehandle = nullptr;
40
Tao Baoef0eb3b2016-11-14 21:29:52 -080041static void expect(const char* expected, const char* expr_str, CauseCode cause_code,
42 UpdaterInfo* info = nullptr) {
43 Expr* e;
44 int error_count = 0;
45 ASSERT_EQ(0, parse_string(expr_str, &e, &error_count));
46 ASSERT_EQ(0, error_count);
Tao Bao0c7839a2016-10-10 15:48:37 -070047
Tao Baoef0eb3b2016-11-14 21:29:52 -080048 State state(expr_str, info);
Tao Bao0c7839a2016-10-10 15:48:37 -070049
Tao Baoef0eb3b2016-11-14 21:29:52 -080050 std::string result;
51 bool status = Evaluate(&state, e, &result);
Tao Bao0c7839a2016-10-10 15:48:37 -070052
Tao Baoef0eb3b2016-11-14 21:29:52 -080053 if (expected == nullptr) {
54 ASSERT_FALSE(status);
55 } else {
56 ASSERT_TRUE(status);
57 ASSERT_STREQ(expected, result.c_str());
58 }
Tao Bao0c7839a2016-10-10 15:48:37 -070059
Tao Baoef0eb3b2016-11-14 21:29:52 -080060 // Error code is set in updater/updater.cpp only, by parsing State.errmsg.
61 ASSERT_EQ(kNoError, state.error_code);
Tao Bao361342c2016-02-08 11:15:50 -080062
Tao Baoef0eb3b2016-11-14 21:29:52 -080063 // Cause code should always be available.
64 ASSERT_EQ(cause_code, state.cause_code);
Tao Bao0c7839a2016-10-10 15:48:37 -070065}
66
67class UpdaterTest : public ::testing::Test {
68 protected:
69 virtual void SetUp() {
70 RegisterBuiltins();
71 RegisterInstallFunctions();
Tao Bao0c7839a2016-10-10 15:48:37 -070072 }
73};
74
75TEST_F(UpdaterTest, getprop) {
76 expect(android::base::GetProperty("ro.product.device", "").c_str(),
77 "getprop(\"ro.product.device\")",
Tao Bao361342c2016-02-08 11:15:50 -080078 kNoCause);
Tao Bao0c7839a2016-10-10 15:48:37 -070079
80 expect(android::base::GetProperty("ro.build.fingerprint", "").c_str(),
81 "getprop(\"ro.build.fingerprint\")",
Tao Bao361342c2016-02-08 11:15:50 -080082 kNoCause);
Tao Bao0c7839a2016-10-10 15:48:37 -070083
84 // getprop() accepts only one parameter.
Tao Bao361342c2016-02-08 11:15:50 -080085 expect(nullptr, "getprop()", kArgsParsingFailure);
86 expect(nullptr, "getprop(\"arg1\", \"arg2\")", kArgsParsingFailure);
87}
88
89TEST_F(UpdaterTest, sha1_check) {
90 // sha1_check(data) returns the SHA-1 of the data.
91 expect("81fe8bfe87576c3ecb22426f8e57847382917acf", "sha1_check(\"abcd\")", kNoCause);
92 expect("da39a3ee5e6b4b0d3255bfef95601890afd80709", "sha1_check(\"\")", kNoCause);
93
94 // sha1_check(data, sha1_hex, [sha1_hex, ...]) returns the matched SHA-1.
95 expect("81fe8bfe87576c3ecb22426f8e57847382917acf",
96 "sha1_check(\"abcd\", \"81fe8bfe87576c3ecb22426f8e57847382917acf\")",
97 kNoCause);
98
99 expect("81fe8bfe87576c3ecb22426f8e57847382917acf",
100 "sha1_check(\"abcd\", \"wrong_sha1\", \"81fe8bfe87576c3ecb22426f8e57847382917acf\")",
101 kNoCause);
102
103 // Or "" if there's no match.
104 expect("",
105 "sha1_check(\"abcd\", \"wrong_sha1\")",
106 kNoCause);
107
108 expect("",
109 "sha1_check(\"abcd\", \"wrong_sha1\", \"wrong_sha2\")",
110 kNoCause);
111
112 // sha1_check() expects at least one argument.
113 expect(nullptr, "sha1_check()", kArgsParsingFailure);
Tao Bao0c7839a2016-10-10 15:48:37 -0700114}
Tao Bao51d516e2016-11-03 14:49:01 -0700115
116TEST_F(UpdaterTest, file_getprop) {
117 // file_getprop() expects two arguments.
118 expect(nullptr, "file_getprop()", kArgsParsingFailure);
119 expect(nullptr, "file_getprop(\"arg1\")", kArgsParsingFailure);
120 expect(nullptr, "file_getprop(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
121
122 // File doesn't exist.
123 expect(nullptr, "file_getprop(\"/doesntexist\", \"key1\")", kFileGetPropFailure);
124
125 // Reject too large files (current limit = 65536).
126 TemporaryFile temp_file1;
127 std::string buffer(65540, '\0');
128 ASSERT_TRUE(android::base::WriteStringToFile(buffer, temp_file1.path));
129
130 // Read some keys.
131 TemporaryFile temp_file2;
132 std::string content("ro.product.name=tardis\n"
133 "# comment\n\n\n"
134 "ro.product.model\n"
135 "ro.product.board = magic \n");
136 ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file2.path));
137
138 std::string script1("file_getprop(\"" + std::string(temp_file2.path) +
139 "\", \"ro.product.name\")");
140 expect("tardis", script1.c_str(), kNoCause);
141
142 std::string script2("file_getprop(\"" + std::string(temp_file2.path) +
143 "\", \"ro.product.board\")");
144 expect("magic", script2.c_str(), kNoCause);
145
146 // No match.
147 std::string script3("file_getprop(\"" + std::string(temp_file2.path) +
148 "\", \"ro.product.wrong\")");
149 expect("", script3.c_str(), kNoCause);
150
151 std::string script4("file_getprop(\"" + std::string(temp_file2.path) +
152 "\", \"ro.product.name=\")");
153 expect("", script4.c_str(), kNoCause);
154
155 std::string script5("file_getprop(\"" + std::string(temp_file2.path) +
156 "\", \"ro.product.nam\")");
157 expect("", script5.c_str(), kNoCause);
158
159 std::string script6("file_getprop(\"" + std::string(temp_file2.path) +
160 "\", \"ro.product.model\")");
161 expect("", script6.c_str(), kNoCause);
162}
Tao Bao0831d0b2016-11-03 23:25:04 -0700163
Tao Bao1036d362016-11-17 22:49:56 -0800164TEST_F(UpdaterTest, package_extract_dir) {
165 // package_extract_dir expects 2 arguments.
166 expect(nullptr, "package_extract_dir()", kArgsParsingFailure);
167 expect(nullptr, "package_extract_dir(\"arg1\")", kArgsParsingFailure);
168 expect(nullptr, "package_extract_dir(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
169
170 std::string zip_path = from_testdata_base("ziptest_valid.zip");
171 ZipArchiveHandle handle;
172 ASSERT_EQ(0, OpenArchive(zip_path.c_str(), &handle));
173
174 // Need to set up the ziphandle.
175 UpdaterInfo updater_info;
176 updater_info.package_zip = handle;
177
178 // Extract "b/c.txt" and "b/d.txt" with package_extract_dir("b", "<dir>").
179 TemporaryDir td;
180 std::string temp_dir(td.path);
181 std::string script("package_extract_dir(\"b\", \"" + temp_dir + "\")");
182 expect("t", script.c_str(), kNoCause, &updater_info);
183
184 // Verify.
185 std::string data;
186 std::string file_c = temp_dir + "/c.txt";
187 ASSERT_TRUE(android::base::ReadFileToString(file_c, &data));
188 ASSERT_EQ(kCTxtContents, data);
189
190 std::string file_d = temp_dir + "/d.txt";
191 ASSERT_TRUE(android::base::ReadFileToString(file_d, &data));
192 ASSERT_EQ(kDTxtContents, data);
193
194 // Modify the contents in order to retry. It's expected to be overwritten.
195 ASSERT_TRUE(android::base::WriteStringToFile("random", file_c));
196 ASSERT_TRUE(android::base::WriteStringToFile("random", file_d));
197
198 // Extract again and verify.
199 expect("t", script.c_str(), kNoCause, &updater_info);
200
201 ASSERT_TRUE(android::base::ReadFileToString(file_c, &data));
202 ASSERT_EQ(kCTxtContents, data);
203 ASSERT_TRUE(android::base::ReadFileToString(file_d, &data));
204 ASSERT_EQ(kDTxtContents, data);
205
206 // Clean up the temp files under td.
207 ASSERT_EQ(0, unlink(file_c.c_str()));
208 ASSERT_EQ(0, unlink(file_d.c_str()));
209
210 // Extracting "b/" (with slash) should give the same result.
211 script = "package_extract_dir(\"b/\", \"" + temp_dir + "\")";
212 expect("t", script.c_str(), kNoCause, &updater_info);
213
214 ASSERT_TRUE(android::base::ReadFileToString(file_c, &data));
215 ASSERT_EQ(kCTxtContents, data);
216 ASSERT_TRUE(android::base::ReadFileToString(file_d, &data));
217 ASSERT_EQ(kDTxtContents, data);
218
219 ASSERT_EQ(0, unlink(file_c.c_str()));
220 ASSERT_EQ(0, unlink(file_d.c_str()));
221
222 // Extracting "" is allowed. The entries will carry the path name.
223 script = "package_extract_dir(\"\", \"" + temp_dir + "\")";
224 expect("t", script.c_str(), kNoCause, &updater_info);
225
226 std::string file_a = temp_dir + "/a.txt";
227 ASSERT_TRUE(android::base::ReadFileToString(file_a, &data));
228 ASSERT_EQ(kATxtContents, data);
229 std::string file_b = temp_dir + "/b.txt";
230 ASSERT_TRUE(android::base::ReadFileToString(file_b, &data));
231 ASSERT_EQ(kBTxtContents, data);
232 std::string file_b_c = temp_dir + "/b/c.txt";
233 ASSERT_TRUE(android::base::ReadFileToString(file_b_c, &data));
234 ASSERT_EQ(kCTxtContents, data);
235 std::string file_b_d = temp_dir + "/b/d.txt";
236 ASSERT_TRUE(android::base::ReadFileToString(file_b_d, &data));
237 ASSERT_EQ(kDTxtContents, data);
238
239 ASSERT_EQ(0, unlink(file_a.c_str()));
240 ASSERT_EQ(0, unlink(file_b.c_str()));
241 ASSERT_EQ(0, unlink(file_b_c.c_str()));
242 ASSERT_EQ(0, unlink(file_b_d.c_str()));
243 ASSERT_EQ(0, rmdir((temp_dir + "/b").c_str()));
244
245 // Extracting non-existent entry should still give "t".
246 script = "package_extract_dir(\"doesntexist\", \"" + temp_dir + "\")";
247 expect("t", script.c_str(), kNoCause, &updater_info);
248
249 // Only relative zip_path is allowed.
250 script = "package_extract_dir(\"/b\", \"" + temp_dir + "\")";
251 expect("", script.c_str(), kNoCause, &updater_info);
252
253 // Only absolute dest_path is allowed.
254 script = "package_extract_dir(\"b\", \"path\")";
255 expect("", script.c_str(), kNoCause, &updater_info);
256
257 CloseArchive(handle);
258}
259
Tao Baoef0eb3b2016-11-14 21:29:52 -0800260// TODO: Test extracting to block device.
261TEST_F(UpdaterTest, package_extract_file) {
262 // package_extract_file expects 1 or 2 arguments.
263 expect(nullptr, "package_extract_file()", kArgsParsingFailure);
264 expect(nullptr, "package_extract_file(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
265
266 std::string zip_path = from_testdata_base("ziptest_valid.zip");
267 ZipArchiveHandle handle;
268 ASSERT_EQ(0, OpenArchive(zip_path.c_str(), &handle));
269
270 // Need to set up the ziphandle.
271 UpdaterInfo updater_info;
272 updater_info.package_zip = handle;
273
274 // Two-argument version.
275 TemporaryFile temp_file1;
276 std::string script("package_extract_file(\"a.txt\", \"" + std::string(temp_file1.path) + "\")");
277 expect("t", script.c_str(), kNoCause, &updater_info);
278
279 // Verify the extracted entry.
280 std::string data;
281 ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data));
282 ASSERT_EQ(kATxtContents, data);
283
284 // Now extract another entry to the same location, which should overwrite.
285 script = "package_extract_file(\"b.txt\", \"" + std::string(temp_file1.path) + "\")";
286 expect("t", script.c_str(), kNoCause, &updater_info);
287
288 ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data));
289 ASSERT_EQ(kBTxtContents, data);
290
291 // Missing zip entry. The two-argument version doesn't abort.
292 script = "package_extract_file(\"doesntexist\", \"" + std::string(temp_file1.path) + "\")";
293 expect("", script.c_str(), kNoCause, &updater_info);
294
295 // Extract to /dev/full should fail.
296 script = "package_extract_file(\"a.txt\", \"/dev/full\")";
297 expect("", script.c_str(), kNoCause, &updater_info);
298
299 // One-argument version.
300 script = "sha1_check(package_extract_file(\"a.txt\"))";
301 expect(kATxtSha1Sum.c_str(), script.c_str(), kNoCause, &updater_info);
302
303 script = "sha1_check(package_extract_file(\"b.txt\"))";
304 expect(kBTxtSha1Sum.c_str(), script.c_str(), kNoCause, &updater_info);
305
306 // Missing entry. The one-argument version aborts the evaluation.
307 script = "package_extract_file(\"doesntexist\")";
308 expect(nullptr, script.c_str(), kPackageExtractFileFailure, &updater_info);
309
310 CloseArchive(handle);
311}
Tao Baod0f30882016-11-03 23:52:01 -0700312
313TEST_F(UpdaterTest, write_value) {
314 // write_value() expects two arguments.
315 expect(nullptr, "write_value()", kArgsParsingFailure);
316 expect(nullptr, "write_value(\"arg1\")", kArgsParsingFailure);
317 expect(nullptr, "write_value(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
318
319 // filename cannot be empty.
320 expect(nullptr, "write_value(\"value\", \"\")", kArgsParsingFailure);
321
322 // Write some value to file.
323 TemporaryFile temp_file;
324 std::string value = "magicvalue";
325 std::string script("write_value(\"" + value + "\", \"" + std::string(temp_file.path) + "\")");
326 expect("t", script.c_str(), kNoCause);
327
328 // Verify the content.
329 std::string content;
330 ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content));
331 ASSERT_EQ(value, content);
332
333 // Allow writing empty string.
334 script = "write_value(\"\", \"" + std::string(temp_file.path) + "\")";
335 expect("t", script.c_str(), kNoCause);
336
337 // Verify the content.
338 ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content));
339 ASSERT_EQ("", content);
340
341 // It should fail gracefully when write fails.
342 script = "write_value(\"value\", \"/proc/0/file1\")";
343 expect("", script.c_str(), kNoCause);
344}
Tao Baobedf5fc2016-11-18 12:01:26 -0800345
346TEST_F(UpdaterTest, get_stage) {
347 // get_stage() expects one argument.
348 expect(nullptr, "get_stage()", kArgsParsingFailure);
349 expect(nullptr, "get_stage(\"arg1\", \"arg2\")", kArgsParsingFailure);
350 expect(nullptr, "get_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
351
352 // Set up a local file as BCB.
353 TemporaryFile tf;
354 std::string temp_file(tf.path);
355 bootloader_message boot;
356 strlcpy(boot.stage, "2/3", sizeof(boot.stage));
357 std::string err;
358 ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err));
359
360 // Can read the stage value.
361 std::string script("get_stage(\"" + temp_file + "\")");
362 expect("2/3", script.c_str(), kNoCause);
363
364 // Bad BCB path.
365 script = "get_stage(\"doesntexist\")";
366 expect("", script.c_str(), kNoCause);
367}
368
369TEST_F(UpdaterTest, set_stage) {
370 // set_stage() expects two arguments.
371 expect(nullptr, "set_stage()", kArgsParsingFailure);
372 expect(nullptr, "set_stage(\"arg1\")", kArgsParsingFailure);
373 expect(nullptr, "set_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
374
375 // Set up a local file as BCB.
376 TemporaryFile tf;
377 std::string temp_file(tf.path);
378 bootloader_message boot;
379 strlcpy(boot.command, "command", sizeof(boot.command));
380 strlcpy(boot.stage, "2/3", sizeof(boot.stage));
381 std::string err;
382 ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err));
383
384 // Write with set_stage().
385 std::string script("set_stage(\"" + temp_file + "\", \"1/3\")");
386 expect(tf.path, script.c_str(), kNoCause);
387
388 // Verify.
389 bootloader_message boot_verify;
390 ASSERT_TRUE(read_bootloader_message_from(&boot_verify, temp_file, &err));
391
392 // Stage should be updated, with command part untouched.
393 ASSERT_STREQ("1/3", boot_verify.stage);
394 ASSERT_STREQ(boot.command, boot_verify.command);
395
396 // Bad BCB path.
397 script = "set_stage(\"doesntexist\", \"1/3\")";
398 expect("", script.c_str(), kNoCause);
399
400 script = "set_stage(\"/dev/full\", \"1/3\")";
401 expect("", script.c_str(), kNoCause);
402}
Tao Bao9aa7ab52017-01-05 17:27:19 -0800403
404TEST_F(UpdaterTest, set_progress) {
405 // set_progress() expects one argument.
406 expect(nullptr, "set_progress()", kArgsParsingFailure);
407 expect(nullptr, "set_progress(\"arg1\", \"arg2\")", kArgsParsingFailure);
408
409 // Invalid progress argument.
410 expect(nullptr, "set_progress(\"arg1\")", kArgsParsingFailure);
411 expect(nullptr, "set_progress(\"3x+5\")", kArgsParsingFailure);
412 expect(nullptr, "set_progress(\".3.5\")", kArgsParsingFailure);
413
414 TemporaryFile tf;
415 UpdaterInfo updater_info;
416 updater_info.cmd_pipe = fdopen(tf.fd, "w");
417 expect(".52", "set_progress(\".52\")", kNoCause, &updater_info);
418 fflush(updater_info.cmd_pipe);
419
420 std::string cmd;
421 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd));
422 ASSERT_EQ(android::base::StringPrintf("set_progress %f\n", .52), cmd);
423 // recovery-updater protocol expects 2 tokens ("set_progress <frac>").
424 ASSERT_EQ(2U, android::base::Split(cmd, " ").size());
425}
426
427TEST_F(UpdaterTest, show_progress) {
428 // show_progress() expects two arguments.
429 expect(nullptr, "show_progress()", kArgsParsingFailure);
430 expect(nullptr, "show_progress(\"arg1\")", kArgsParsingFailure);
431 expect(nullptr, "show_progress(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
432
433 // Invalid progress arguments.
434 expect(nullptr, "show_progress(\"arg1\", \"arg2\")", kArgsParsingFailure);
435 expect(nullptr, "show_progress(\"3x+5\", \"10\")", kArgsParsingFailure);
436 expect(nullptr, "show_progress(\".3\", \"5a\")", kArgsParsingFailure);
437
438 TemporaryFile tf;
439 UpdaterInfo updater_info;
440 updater_info.cmd_pipe = fdopen(tf.fd, "w");
441 expect(".52", "show_progress(\".52\", \"10\")", kNoCause, &updater_info);
442 fflush(updater_info.cmd_pipe);
443
444 std::string cmd;
445 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd));
446 ASSERT_EQ(android::base::StringPrintf("progress %f %d\n", .52, 10), cmd);
447 // recovery-updater protocol expects 3 tokens ("progress <frac> <secs>").
448 ASSERT_EQ(3U, android::base::Split(cmd, " ").size());
449}