blob: fa5f0313480e32027670959462bd70154d09cd10 [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
164TEST_F(UpdaterTest, delete) {
165 // Delete none.
166 expect("0", "delete()", kNoCause);
167 expect("0", "delete(\"/doesntexist\")", kNoCause);
168 expect("0", "delete(\"/doesntexist1\", \"/doesntexist2\")", kNoCause);
169 expect("0", "delete(\"/doesntexist1\", \"/doesntexist2\", \"/doesntexist3\")", kNoCause);
170
171 // Delete one file.
172 TemporaryFile temp_file1;
173 ASSERT_TRUE(android::base::WriteStringToFile("abc", temp_file1.path));
174 std::string script1("delete(\"" + std::string(temp_file1.path) + "\")");
175 expect("1", script1.c_str(), kNoCause);
176
177 // Delete two files.
178 TemporaryFile temp_file2;
179 ASSERT_TRUE(android::base::WriteStringToFile("abc", temp_file2.path));
180 TemporaryFile temp_file3;
181 ASSERT_TRUE(android::base::WriteStringToFile("abc", temp_file3.path));
182 std::string script2("delete(\"" + std::string(temp_file2.path) + "\", \"" +
183 std::string(temp_file3.path) + "\")");
184 expect("2", script2.c_str(), kNoCause);
185
186 // Delete already deleted files.
187 expect("0", script2.c_str(), kNoCause);
188
189 // Delete one out of three.
190 TemporaryFile temp_file4;
191 ASSERT_TRUE(android::base::WriteStringToFile("abc", temp_file4.path));
192 std::string script3("delete(\"/doesntexist1\", \"" + std::string(temp_file4.path) +
193 "\", \"/doesntexist2\")");
194 expect("1", script3.c_str(), kNoCause);
195}
Tao Baoa659d792016-11-03 23:25:04 -0700196
197TEST_F(UpdaterTest, rename) {
198 // rename() expects two arguments.
199 expect(nullptr, "rename()", kArgsParsingFailure);
200 expect(nullptr, "rename(\"arg1\")", kArgsParsingFailure);
201 expect(nullptr, "rename(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
202
203 // src_name or dst_name cannot be empty.
204 expect(nullptr, "rename(\"\", \"arg2\")", kArgsParsingFailure);
205 expect(nullptr, "rename(\"arg1\", \"\")", kArgsParsingFailure);
206
207 // File doesn't exist (both of src and dst).
208 expect(nullptr, "rename(\"/doesntexist\", \"/doesntexisteither\")" , kFileRenameFailure);
209
210 // Can't create parent directory.
211 TemporaryFile temp_file1;
212 ASSERT_TRUE(android::base::WriteStringToFile("abc", temp_file1.path));
213 std::string script1("rename(\"" + std::string(temp_file1.path) + "\", \"/proc/0/file1\")");
214 expect(nullptr, script1.c_str(), kFileRenameFailure);
215
216 // Rename.
217 TemporaryFile temp_file2;
218 std::string script2("rename(\"" + std::string(temp_file1.path) + "\", \"" +
219 std::string(temp_file2.path) + "\")");
220 expect(temp_file2.path, script2.c_str(), kNoCause);
221
222 // Already renamed.
223 expect(temp_file2.path, script2.c_str(), kNoCause);
Tianjie Xud75003d2016-11-04 11:31:29 -0700224
225 // Parents create successfully.
226 TemporaryFile temp_file3;
227 TemporaryDir td;
Tao Bao89929022016-11-08 20:51:31 -0800228 std::string temp_dir(td.path);
229 std::string dst_file = temp_dir + "/aaa/bbb/a.txt";
230 std::string script3("rename(\"" + std::string(temp_file3.path) + "\", \"" + dst_file + "\")");
231 expect(dst_file.c_str(), script3.c_str(), kNoCause);
232
233 // Clean up the temp files under td.
234 ASSERT_EQ(0, unlink(dst_file.c_str()));
235 ASSERT_EQ(0, rmdir((temp_dir + "/aaa/bbb").c_str()));
236 ASSERT_EQ(0, rmdir((temp_dir + "/aaa").c_str()));
Tianjie Xud75003d2016-11-04 11:31:29 -0700237}
238
239TEST_F(UpdaterTest, symlink) {
240 // symlink expects 1+ argument.
241 expect(nullptr, "symlink()", kArgsParsingFailure);
242
243 // symlink should fail if src is an empty string.
244 TemporaryFile temp_file1;
245 std::string script1("symlink(\"" + std::string(temp_file1.path) + "\", \"\")");
246 expect(nullptr, script1.c_str(), kSymlinkFailure);
247
Tao Bao89929022016-11-08 20:51:31 -0800248 std::string script2("symlink(\"" + std::string(temp_file1.path) + "\", \"src1\", \"\")");
Tianjie Xud75003d2016-11-04 11:31:29 -0700249 expect(nullptr, script2.c_str(), kSymlinkFailure);
Tao Bao89929022016-11-08 20:51:31 -0800250
251 // symlink failed to remove old src.
252 std::string script3("symlink(\"" + std::string(temp_file1.path) + "\", \"/proc\")");
253 expect(nullptr, script3.c_str(), kSymlinkFailure);
254
255 // symlink can create symlinks.
256 TemporaryFile temp_file;
257 std::string content = "magicvalue";
258 ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file.path));
259
260 TemporaryDir td;
261 std::string src1 = std::string(td.path) + "/symlink1";
262 std::string src2 = std::string(td.path) + "/symlink2";
263 std::string script4("symlink(\"" + std::string(temp_file.path) + "\", \"" +
264 src1 + "\", \"" + src2 + "\")");
265 expect("t", script4.c_str(), kNoCause);
266
267 // Verify the created symlinks.
268 struct stat sb;
269 ASSERT_TRUE(lstat(src1.c_str(), &sb) == 0 && S_ISLNK(sb.st_mode));
270 ASSERT_TRUE(lstat(src2.c_str(), &sb) == 0 && S_ISLNK(sb.st_mode));
271
272 // Clean up the leftovers.
273 ASSERT_EQ(0, unlink(src1.c_str()));
274 ASSERT_EQ(0, unlink(src2.c_str()));
Tao Baoa659d792016-11-03 23:25:04 -0700275}
Tao Baoef0eb3b2016-11-14 21:29:52 -0800276
Tao Bao1036d362016-11-17 22:49:56 -0800277TEST_F(UpdaterTest, package_extract_dir) {
278 // package_extract_dir expects 2 arguments.
279 expect(nullptr, "package_extract_dir()", kArgsParsingFailure);
280 expect(nullptr, "package_extract_dir(\"arg1\")", kArgsParsingFailure);
281 expect(nullptr, "package_extract_dir(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
282
283 std::string zip_path = from_testdata_base("ziptest_valid.zip");
284 ZipArchiveHandle handle;
285 ASSERT_EQ(0, OpenArchive(zip_path.c_str(), &handle));
286
287 // Need to set up the ziphandle.
288 UpdaterInfo updater_info;
289 updater_info.package_zip = handle;
290
291 // Extract "b/c.txt" and "b/d.txt" with package_extract_dir("b", "<dir>").
292 TemporaryDir td;
293 std::string temp_dir(td.path);
294 std::string script("package_extract_dir(\"b\", \"" + temp_dir + "\")");
295 expect("t", script.c_str(), kNoCause, &updater_info);
296
297 // Verify.
298 std::string data;
299 std::string file_c = temp_dir + "/c.txt";
300 ASSERT_TRUE(android::base::ReadFileToString(file_c, &data));
301 ASSERT_EQ(kCTxtContents, data);
302
303 std::string file_d = temp_dir + "/d.txt";
304 ASSERT_TRUE(android::base::ReadFileToString(file_d, &data));
305 ASSERT_EQ(kDTxtContents, data);
306
307 // Modify the contents in order to retry. It's expected to be overwritten.
308 ASSERT_TRUE(android::base::WriteStringToFile("random", file_c));
309 ASSERT_TRUE(android::base::WriteStringToFile("random", file_d));
310
311 // Extract again and verify.
312 expect("t", script.c_str(), kNoCause, &updater_info);
313
314 ASSERT_TRUE(android::base::ReadFileToString(file_c, &data));
315 ASSERT_EQ(kCTxtContents, data);
316 ASSERT_TRUE(android::base::ReadFileToString(file_d, &data));
317 ASSERT_EQ(kDTxtContents, data);
318
319 // Clean up the temp files under td.
320 ASSERT_EQ(0, unlink(file_c.c_str()));
321 ASSERT_EQ(0, unlink(file_d.c_str()));
322
323 // Extracting "b/" (with slash) should give the same result.
324 script = "package_extract_dir(\"b/\", \"" + temp_dir + "\")";
325 expect("t", script.c_str(), kNoCause, &updater_info);
326
327 ASSERT_TRUE(android::base::ReadFileToString(file_c, &data));
328 ASSERT_EQ(kCTxtContents, data);
329 ASSERT_TRUE(android::base::ReadFileToString(file_d, &data));
330 ASSERT_EQ(kDTxtContents, data);
331
332 ASSERT_EQ(0, unlink(file_c.c_str()));
333 ASSERT_EQ(0, unlink(file_d.c_str()));
334
335 // Extracting "" is allowed. The entries will carry the path name.
336 script = "package_extract_dir(\"\", \"" + temp_dir + "\")";
337 expect("t", script.c_str(), kNoCause, &updater_info);
338
339 std::string file_a = temp_dir + "/a.txt";
340 ASSERT_TRUE(android::base::ReadFileToString(file_a, &data));
341 ASSERT_EQ(kATxtContents, data);
342 std::string file_b = temp_dir + "/b.txt";
343 ASSERT_TRUE(android::base::ReadFileToString(file_b, &data));
344 ASSERT_EQ(kBTxtContents, data);
345 std::string file_b_c = temp_dir + "/b/c.txt";
346 ASSERT_TRUE(android::base::ReadFileToString(file_b_c, &data));
347 ASSERT_EQ(kCTxtContents, data);
348 std::string file_b_d = temp_dir + "/b/d.txt";
349 ASSERT_TRUE(android::base::ReadFileToString(file_b_d, &data));
350 ASSERT_EQ(kDTxtContents, data);
351
352 ASSERT_EQ(0, unlink(file_a.c_str()));
353 ASSERT_EQ(0, unlink(file_b.c_str()));
354 ASSERT_EQ(0, unlink(file_b_c.c_str()));
355 ASSERT_EQ(0, unlink(file_b_d.c_str()));
356 ASSERT_EQ(0, rmdir((temp_dir + "/b").c_str()));
357
358 // Extracting non-existent entry should still give "t".
359 script = "package_extract_dir(\"doesntexist\", \"" + temp_dir + "\")";
360 expect("t", script.c_str(), kNoCause, &updater_info);
361
362 // Only relative zip_path is allowed.
363 script = "package_extract_dir(\"/b\", \"" + temp_dir + "\")";
364 expect("", script.c_str(), kNoCause, &updater_info);
365
366 // Only absolute dest_path is allowed.
367 script = "package_extract_dir(\"b\", \"path\")";
368 expect("", script.c_str(), kNoCause, &updater_info);
369
370 CloseArchive(handle);
371}
372
Tao Baoef0eb3b2016-11-14 21:29:52 -0800373// TODO: Test extracting to block device.
374TEST_F(UpdaterTest, package_extract_file) {
375 // package_extract_file expects 1 or 2 arguments.
376 expect(nullptr, "package_extract_file()", kArgsParsingFailure);
377 expect(nullptr, "package_extract_file(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
378
379 std::string zip_path = from_testdata_base("ziptest_valid.zip");
380 ZipArchiveHandle handle;
381 ASSERT_EQ(0, OpenArchive(zip_path.c_str(), &handle));
382
383 // Need to set up the ziphandle.
384 UpdaterInfo updater_info;
385 updater_info.package_zip = handle;
386
387 // Two-argument version.
388 TemporaryFile temp_file1;
389 std::string script("package_extract_file(\"a.txt\", \"" + std::string(temp_file1.path) + "\")");
390 expect("t", script.c_str(), kNoCause, &updater_info);
391
392 // Verify the extracted entry.
393 std::string data;
394 ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data));
395 ASSERT_EQ(kATxtContents, data);
396
397 // Now extract another entry to the same location, which should overwrite.
398 script = "package_extract_file(\"b.txt\", \"" + std::string(temp_file1.path) + "\")";
399 expect("t", script.c_str(), kNoCause, &updater_info);
400
401 ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data));
402 ASSERT_EQ(kBTxtContents, data);
403
404 // Missing zip entry. The two-argument version doesn't abort.
405 script = "package_extract_file(\"doesntexist\", \"" + std::string(temp_file1.path) + "\")";
406 expect("", script.c_str(), kNoCause, &updater_info);
407
408 // Extract to /dev/full should fail.
409 script = "package_extract_file(\"a.txt\", \"/dev/full\")";
410 expect("", script.c_str(), kNoCause, &updater_info);
411
412 // One-argument version.
413 script = "sha1_check(package_extract_file(\"a.txt\"))";
414 expect(kATxtSha1Sum.c_str(), script.c_str(), kNoCause, &updater_info);
415
416 script = "sha1_check(package_extract_file(\"b.txt\"))";
417 expect(kBTxtSha1Sum.c_str(), script.c_str(), kNoCause, &updater_info);
418
419 // Missing entry. The one-argument version aborts the evaluation.
420 script = "package_extract_file(\"doesntexist\")";
421 expect(nullptr, script.c_str(), kPackageExtractFileFailure, &updater_info);
422
423 CloseArchive(handle);
424}
Tao Baod0f30882016-11-03 23:52:01 -0700425
426TEST_F(UpdaterTest, write_value) {
427 // write_value() expects two arguments.
428 expect(nullptr, "write_value()", kArgsParsingFailure);
429 expect(nullptr, "write_value(\"arg1\")", kArgsParsingFailure);
430 expect(nullptr, "write_value(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
431
432 // filename cannot be empty.
433 expect(nullptr, "write_value(\"value\", \"\")", kArgsParsingFailure);
434
435 // Write some value to file.
436 TemporaryFile temp_file;
437 std::string value = "magicvalue";
438 std::string script("write_value(\"" + value + "\", \"" + std::string(temp_file.path) + "\")");
439 expect("t", script.c_str(), kNoCause);
440
441 // Verify the content.
442 std::string content;
443 ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content));
444 ASSERT_EQ(value, content);
445
446 // Allow writing empty string.
447 script = "write_value(\"\", \"" + std::string(temp_file.path) + "\")";
448 expect("t", script.c_str(), kNoCause);
449
450 // Verify the content.
451 ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content));
452 ASSERT_EQ("", content);
453
454 // It should fail gracefully when write fails.
455 script = "write_value(\"value\", \"/proc/0/file1\")";
456 expect("", script.c_str(), kNoCause);
457}
Tao Baobedf5fc2016-11-18 12:01:26 -0800458
459TEST_F(UpdaterTest, get_stage) {
460 // get_stage() expects one argument.
461 expect(nullptr, "get_stage()", kArgsParsingFailure);
462 expect(nullptr, "get_stage(\"arg1\", \"arg2\")", kArgsParsingFailure);
463 expect(nullptr, "get_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
464
465 // Set up a local file as BCB.
466 TemporaryFile tf;
467 std::string temp_file(tf.path);
468 bootloader_message boot;
469 strlcpy(boot.stage, "2/3", sizeof(boot.stage));
470 std::string err;
471 ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err));
472
473 // Can read the stage value.
474 std::string script("get_stage(\"" + temp_file + "\")");
475 expect("2/3", script.c_str(), kNoCause);
476
477 // Bad BCB path.
478 script = "get_stage(\"doesntexist\")";
479 expect("", script.c_str(), kNoCause);
480}
481
482TEST_F(UpdaterTest, set_stage) {
483 // set_stage() expects two arguments.
484 expect(nullptr, "set_stage()", kArgsParsingFailure);
485 expect(nullptr, "set_stage(\"arg1\")", kArgsParsingFailure);
486 expect(nullptr, "set_stage(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
487
488 // Set up a local file as BCB.
489 TemporaryFile tf;
490 std::string temp_file(tf.path);
491 bootloader_message boot;
492 strlcpy(boot.command, "command", sizeof(boot.command));
493 strlcpy(boot.stage, "2/3", sizeof(boot.stage));
494 std::string err;
495 ASSERT_TRUE(write_bootloader_message_to(boot, temp_file, &err));
496
497 // Write with set_stage().
498 std::string script("set_stage(\"" + temp_file + "\", \"1/3\")");
499 expect(tf.path, script.c_str(), kNoCause);
500
501 // Verify.
502 bootloader_message boot_verify;
503 ASSERT_TRUE(read_bootloader_message_from(&boot_verify, temp_file, &err));
504
505 // Stage should be updated, with command part untouched.
506 ASSERT_STREQ("1/3", boot_verify.stage);
507 ASSERT_STREQ(boot.command, boot_verify.command);
508
509 // Bad BCB path.
510 script = "set_stage(\"doesntexist\", \"1/3\")";
511 expect("", script.c_str(), kNoCause);
512
513 script = "set_stage(\"/dev/full\", \"1/3\")";
514 expect("", script.c_str(), kNoCause);
515}
Tao Bao9aa7ab52017-01-05 17:27:19 -0800516
517TEST_F(UpdaterTest, set_progress) {
518 // set_progress() expects one argument.
519 expect(nullptr, "set_progress()", kArgsParsingFailure);
520 expect(nullptr, "set_progress(\"arg1\", \"arg2\")", kArgsParsingFailure);
521
522 // Invalid progress argument.
523 expect(nullptr, "set_progress(\"arg1\")", kArgsParsingFailure);
524 expect(nullptr, "set_progress(\"3x+5\")", kArgsParsingFailure);
525 expect(nullptr, "set_progress(\".3.5\")", kArgsParsingFailure);
526
527 TemporaryFile tf;
528 UpdaterInfo updater_info;
529 updater_info.cmd_pipe = fdopen(tf.fd, "w");
530 expect(".52", "set_progress(\".52\")", kNoCause, &updater_info);
531 fflush(updater_info.cmd_pipe);
532
533 std::string cmd;
534 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd));
535 ASSERT_EQ(android::base::StringPrintf("set_progress %f\n", .52), cmd);
536 // recovery-updater protocol expects 2 tokens ("set_progress <frac>").
537 ASSERT_EQ(2U, android::base::Split(cmd, " ").size());
538}
539
540TEST_F(UpdaterTest, show_progress) {
541 // show_progress() expects two arguments.
542 expect(nullptr, "show_progress()", kArgsParsingFailure);
543 expect(nullptr, "show_progress(\"arg1\")", kArgsParsingFailure);
544 expect(nullptr, "show_progress(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
545
546 // Invalid progress arguments.
547 expect(nullptr, "show_progress(\"arg1\", \"arg2\")", kArgsParsingFailure);
548 expect(nullptr, "show_progress(\"3x+5\", \"10\")", kArgsParsingFailure);
549 expect(nullptr, "show_progress(\".3\", \"5a\")", kArgsParsingFailure);
550
551 TemporaryFile tf;
552 UpdaterInfo updater_info;
553 updater_info.cmd_pipe = fdopen(tf.fd, "w");
554 expect(".52", "show_progress(\".52\", \"10\")", kNoCause, &updater_info);
555 fflush(updater_info.cmd_pipe);
556
557 std::string cmd;
558 ASSERT_TRUE(android::base::ReadFileToString(tf.path, &cmd));
559 ASSERT_EQ(android::base::StringPrintf("progress %f %d\n", .52, 10), cmd);
560 // recovery-updater protocol expects 3 tokens ("progress <frac> <secs>").
561 ASSERT_EQ(3U, android::base::Split(cmd, " ").size());
562}