blob: d34c0e54c47e0708256e3b98ae490bdc748bb8ff [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 Bao89929022016-11-08 20:51:31 -080017#include <sys/stat.h>
18#include <sys/types.h>
19#include <unistd.h>
20
Tao Bao0c7839a2016-10-10 15:48:37 -070021#include <string>
22
Tao Bao51d516e2016-11-03 14:49:01 -070023#include <android-base/file.h>
Tao Bao0c7839a2016-10-10 15:48:37 -070024#include <android-base/properties.h>
Tao Bao51d516e2016-11-03 14:49:01 -070025#include <android-base/test_utils.h>
Tao Bao0c7839a2016-10-10 15:48:37 -070026#include <gtest/gtest.h>
Tao Baoef0eb3b2016-11-14 21:29:52 -080027#include <ziparchive/zip_archive.h>
Tao Bao0c7839a2016-10-10 15:48:37 -070028
Tao Baoef0eb3b2016-11-14 21:29:52 -080029#include "common/test_constants.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070030#include "edify/expr.h"
31#include "error_code.h"
32#include "updater/install.h"
Tao Baoef0eb3b2016-11-14 21:29:52 -080033#include "updater/updater.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070034
35struct selabel_handle *sehandle = nullptr;
36
Tao Baoef0eb3b2016-11-14 21:29:52 -080037static void expect(const char* expected, const char* expr_str, CauseCode cause_code,
38 UpdaterInfo* info = nullptr) {
39 Expr* e;
40 int error_count = 0;
41 ASSERT_EQ(0, parse_string(expr_str, &e, &error_count));
42 ASSERT_EQ(0, error_count);
Tao Bao0c7839a2016-10-10 15:48:37 -070043
Tao Baoef0eb3b2016-11-14 21:29:52 -080044 State state(expr_str, info);
Tao Bao0c7839a2016-10-10 15:48:37 -070045
Tao Baoef0eb3b2016-11-14 21:29:52 -080046 std::string result;
47 bool status = Evaluate(&state, e, &result);
Tao Bao0c7839a2016-10-10 15:48:37 -070048
Tao Baoef0eb3b2016-11-14 21:29:52 -080049 if (expected == nullptr) {
50 ASSERT_FALSE(status);
51 } else {
52 ASSERT_TRUE(status);
53 ASSERT_STREQ(expected, result.c_str());
54 }
Tao Bao0c7839a2016-10-10 15:48:37 -070055
Tao Baoef0eb3b2016-11-14 21:29:52 -080056 // Error code is set in updater/updater.cpp only, by parsing State.errmsg.
57 ASSERT_EQ(kNoError, state.error_code);
Tao Bao361342c2016-02-08 11:15:50 -080058
Tao Baoef0eb3b2016-11-14 21:29:52 -080059 // Cause code should always be available.
60 ASSERT_EQ(cause_code, state.cause_code);
Tao Bao0c7839a2016-10-10 15:48:37 -070061}
62
63class UpdaterTest : public ::testing::Test {
64 protected:
65 virtual void SetUp() {
66 RegisterBuiltins();
67 RegisterInstallFunctions();
Tao Bao0c7839a2016-10-10 15:48:37 -070068 }
69};
70
71TEST_F(UpdaterTest, getprop) {
72 expect(android::base::GetProperty("ro.product.device", "").c_str(),
73 "getprop(\"ro.product.device\")",
Tao Bao361342c2016-02-08 11:15:50 -080074 kNoCause);
Tao Bao0c7839a2016-10-10 15:48:37 -070075
76 expect(android::base::GetProperty("ro.build.fingerprint", "").c_str(),
77 "getprop(\"ro.build.fingerprint\")",
Tao Bao361342c2016-02-08 11:15:50 -080078 kNoCause);
Tao Bao0c7839a2016-10-10 15:48:37 -070079
80 // getprop() accepts only one parameter.
Tao Bao361342c2016-02-08 11:15:50 -080081 expect(nullptr, "getprop()", kArgsParsingFailure);
82 expect(nullptr, "getprop(\"arg1\", \"arg2\")", kArgsParsingFailure);
83}
84
85TEST_F(UpdaterTest, sha1_check) {
86 // sha1_check(data) returns the SHA-1 of the data.
87 expect("81fe8bfe87576c3ecb22426f8e57847382917acf", "sha1_check(\"abcd\")", kNoCause);
88 expect("da39a3ee5e6b4b0d3255bfef95601890afd80709", "sha1_check(\"\")", kNoCause);
89
90 // sha1_check(data, sha1_hex, [sha1_hex, ...]) returns the matched SHA-1.
91 expect("81fe8bfe87576c3ecb22426f8e57847382917acf",
92 "sha1_check(\"abcd\", \"81fe8bfe87576c3ecb22426f8e57847382917acf\")",
93 kNoCause);
94
95 expect("81fe8bfe87576c3ecb22426f8e57847382917acf",
96 "sha1_check(\"abcd\", \"wrong_sha1\", \"81fe8bfe87576c3ecb22426f8e57847382917acf\")",
97 kNoCause);
98
99 // Or "" if there's no match.
100 expect("",
101 "sha1_check(\"abcd\", \"wrong_sha1\")",
102 kNoCause);
103
104 expect("",
105 "sha1_check(\"abcd\", \"wrong_sha1\", \"wrong_sha2\")",
106 kNoCause);
107
108 // sha1_check() expects at least one argument.
109 expect(nullptr, "sha1_check()", kArgsParsingFailure);
Tao Bao0c7839a2016-10-10 15:48:37 -0700110}
Tao Bao51d516e2016-11-03 14:49:01 -0700111
112TEST_F(UpdaterTest, file_getprop) {
113 // file_getprop() expects two arguments.
114 expect(nullptr, "file_getprop()", kArgsParsingFailure);
115 expect(nullptr, "file_getprop(\"arg1\")", kArgsParsingFailure);
116 expect(nullptr, "file_getprop(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
117
118 // File doesn't exist.
119 expect(nullptr, "file_getprop(\"/doesntexist\", \"key1\")", kFileGetPropFailure);
120
121 // Reject too large files (current limit = 65536).
122 TemporaryFile temp_file1;
123 std::string buffer(65540, '\0');
124 ASSERT_TRUE(android::base::WriteStringToFile(buffer, temp_file1.path));
125
126 // Read some keys.
127 TemporaryFile temp_file2;
128 std::string content("ro.product.name=tardis\n"
129 "# comment\n\n\n"
130 "ro.product.model\n"
131 "ro.product.board = magic \n");
132 ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file2.path));
133
134 std::string script1("file_getprop(\"" + std::string(temp_file2.path) +
135 "\", \"ro.product.name\")");
136 expect("tardis", script1.c_str(), kNoCause);
137
138 std::string script2("file_getprop(\"" + std::string(temp_file2.path) +
139 "\", \"ro.product.board\")");
140 expect("magic", script2.c_str(), kNoCause);
141
142 // No match.
143 std::string script3("file_getprop(\"" + std::string(temp_file2.path) +
144 "\", \"ro.product.wrong\")");
145 expect("", script3.c_str(), kNoCause);
146
147 std::string script4("file_getprop(\"" + std::string(temp_file2.path) +
148 "\", \"ro.product.name=\")");
149 expect("", script4.c_str(), kNoCause);
150
151 std::string script5("file_getprop(\"" + std::string(temp_file2.path) +
152 "\", \"ro.product.nam\")");
153 expect("", script5.c_str(), kNoCause);
154
155 std::string script6("file_getprop(\"" + std::string(temp_file2.path) +
156 "\", \"ro.product.model\")");
157 expect("", script6.c_str(), kNoCause);
158}
Tao Bao0831d0b2016-11-03 23:25:04 -0700159
160TEST_F(UpdaterTest, delete) {
161 // Delete none.
162 expect("0", "delete()", kNoCause);
163 expect("0", "delete(\"/doesntexist\")", kNoCause);
164 expect("0", "delete(\"/doesntexist1\", \"/doesntexist2\")", kNoCause);
165 expect("0", "delete(\"/doesntexist1\", \"/doesntexist2\", \"/doesntexist3\")", kNoCause);
166
167 // Delete one file.
168 TemporaryFile temp_file1;
169 ASSERT_TRUE(android::base::WriteStringToFile("abc", temp_file1.path));
170 std::string script1("delete(\"" + std::string(temp_file1.path) + "\")");
171 expect("1", script1.c_str(), kNoCause);
172
173 // Delete two files.
174 TemporaryFile temp_file2;
175 ASSERT_TRUE(android::base::WriteStringToFile("abc", temp_file2.path));
176 TemporaryFile temp_file3;
177 ASSERT_TRUE(android::base::WriteStringToFile("abc", temp_file3.path));
178 std::string script2("delete(\"" + std::string(temp_file2.path) + "\", \"" +
179 std::string(temp_file3.path) + "\")");
180 expect("2", script2.c_str(), kNoCause);
181
182 // Delete already deleted files.
183 expect("0", script2.c_str(), kNoCause);
184
185 // Delete one out of three.
186 TemporaryFile temp_file4;
187 ASSERT_TRUE(android::base::WriteStringToFile("abc", temp_file4.path));
188 std::string script3("delete(\"/doesntexist1\", \"" + std::string(temp_file4.path) +
189 "\", \"/doesntexist2\")");
190 expect("1", script3.c_str(), kNoCause);
191}
Tao Baoa659d792016-11-03 23:25:04 -0700192
193TEST_F(UpdaterTest, rename) {
194 // rename() expects two arguments.
195 expect(nullptr, "rename()", kArgsParsingFailure);
196 expect(nullptr, "rename(\"arg1\")", kArgsParsingFailure);
197 expect(nullptr, "rename(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
198
199 // src_name or dst_name cannot be empty.
200 expect(nullptr, "rename(\"\", \"arg2\")", kArgsParsingFailure);
201 expect(nullptr, "rename(\"arg1\", \"\")", kArgsParsingFailure);
202
203 // File doesn't exist (both of src and dst).
204 expect(nullptr, "rename(\"/doesntexist\", \"/doesntexisteither\")" , kFileRenameFailure);
205
206 // Can't create parent directory.
207 TemporaryFile temp_file1;
208 ASSERT_TRUE(android::base::WriteStringToFile("abc", temp_file1.path));
209 std::string script1("rename(\"" + std::string(temp_file1.path) + "\", \"/proc/0/file1\")");
210 expect(nullptr, script1.c_str(), kFileRenameFailure);
211
212 // Rename.
213 TemporaryFile temp_file2;
214 std::string script2("rename(\"" + std::string(temp_file1.path) + "\", \"" +
215 std::string(temp_file2.path) + "\")");
216 expect(temp_file2.path, script2.c_str(), kNoCause);
217
218 // Already renamed.
219 expect(temp_file2.path, script2.c_str(), kNoCause);
Tianjie Xud75003d2016-11-04 11:31:29 -0700220
221 // Parents create successfully.
222 TemporaryFile temp_file3;
223 TemporaryDir td;
Tao Bao89929022016-11-08 20:51:31 -0800224 std::string temp_dir(td.path);
225 std::string dst_file = temp_dir + "/aaa/bbb/a.txt";
226 std::string script3("rename(\"" + std::string(temp_file3.path) + "\", \"" + dst_file + "\")");
227 expect(dst_file.c_str(), script3.c_str(), kNoCause);
228
229 // Clean up the temp files under td.
230 ASSERT_EQ(0, unlink(dst_file.c_str()));
231 ASSERT_EQ(0, rmdir((temp_dir + "/aaa/bbb").c_str()));
232 ASSERT_EQ(0, rmdir((temp_dir + "/aaa").c_str()));
Tianjie Xud75003d2016-11-04 11:31:29 -0700233}
234
235TEST_F(UpdaterTest, symlink) {
236 // symlink expects 1+ argument.
237 expect(nullptr, "symlink()", kArgsParsingFailure);
238
239 // symlink should fail if src is an empty string.
240 TemporaryFile temp_file1;
241 std::string script1("symlink(\"" + std::string(temp_file1.path) + "\", \"\")");
242 expect(nullptr, script1.c_str(), kSymlinkFailure);
243
Tao Bao89929022016-11-08 20:51:31 -0800244 std::string script2("symlink(\"" + std::string(temp_file1.path) + "\", \"src1\", \"\")");
Tianjie Xud75003d2016-11-04 11:31:29 -0700245 expect(nullptr, script2.c_str(), kSymlinkFailure);
Tao Bao89929022016-11-08 20:51:31 -0800246
247 // symlink failed to remove old src.
248 std::string script3("symlink(\"" + std::string(temp_file1.path) + "\", \"/proc\")");
249 expect(nullptr, script3.c_str(), kSymlinkFailure);
250
251 // symlink can create symlinks.
252 TemporaryFile temp_file;
253 std::string content = "magicvalue";
254 ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file.path));
255
256 TemporaryDir td;
257 std::string src1 = std::string(td.path) + "/symlink1";
258 std::string src2 = std::string(td.path) + "/symlink2";
259 std::string script4("symlink(\"" + std::string(temp_file.path) + "\", \"" +
260 src1 + "\", \"" + src2 + "\")");
261 expect("t", script4.c_str(), kNoCause);
262
263 // Verify the created symlinks.
264 struct stat sb;
265 ASSERT_TRUE(lstat(src1.c_str(), &sb) == 0 && S_ISLNK(sb.st_mode));
266 ASSERT_TRUE(lstat(src2.c_str(), &sb) == 0 && S_ISLNK(sb.st_mode));
267
268 // Clean up the leftovers.
269 ASSERT_EQ(0, unlink(src1.c_str()));
270 ASSERT_EQ(0, unlink(src2.c_str()));
Tao Baoa659d792016-11-03 23:25:04 -0700271}
Tao Baoef0eb3b2016-11-14 21:29:52 -0800272
273// TODO: Test extracting to block device.
274TEST_F(UpdaterTest, package_extract_file) {
275 // package_extract_file expects 1 or 2 arguments.
276 expect(nullptr, "package_extract_file()", kArgsParsingFailure);
277 expect(nullptr, "package_extract_file(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
278
279 std::string zip_path = from_testdata_base("ziptest_valid.zip");
280 ZipArchiveHandle handle;
281 ASSERT_EQ(0, OpenArchive(zip_path.c_str(), &handle));
282
283 // Need to set up the ziphandle.
284 UpdaterInfo updater_info;
285 updater_info.package_zip = handle;
286
287 // Two-argument version.
288 TemporaryFile temp_file1;
289 std::string script("package_extract_file(\"a.txt\", \"" + std::string(temp_file1.path) + "\")");
290 expect("t", script.c_str(), kNoCause, &updater_info);
291
292 // Verify the extracted entry.
293 std::string data;
294 ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data));
295 ASSERT_EQ(kATxtContents, data);
296
297 // Now extract another entry to the same location, which should overwrite.
298 script = "package_extract_file(\"b.txt\", \"" + std::string(temp_file1.path) + "\")";
299 expect("t", script.c_str(), kNoCause, &updater_info);
300
301 ASSERT_TRUE(android::base::ReadFileToString(temp_file1.path, &data));
302 ASSERT_EQ(kBTxtContents, data);
303
304 // Missing zip entry. The two-argument version doesn't abort.
305 script = "package_extract_file(\"doesntexist\", \"" + std::string(temp_file1.path) + "\")";
306 expect("", script.c_str(), kNoCause, &updater_info);
307
308 // Extract to /dev/full should fail.
309 script = "package_extract_file(\"a.txt\", \"/dev/full\")";
310 expect("", script.c_str(), kNoCause, &updater_info);
311
312 // One-argument version.
313 script = "sha1_check(package_extract_file(\"a.txt\"))";
314 expect(kATxtSha1Sum.c_str(), script.c_str(), kNoCause, &updater_info);
315
316 script = "sha1_check(package_extract_file(\"b.txt\"))";
317 expect(kBTxtSha1Sum.c_str(), script.c_str(), kNoCause, &updater_info);
318
319 // Missing entry. The one-argument version aborts the evaluation.
320 script = "package_extract_file(\"doesntexist\")";
321 expect(nullptr, script.c_str(), kPackageExtractFileFailure, &updater_info);
322
323 CloseArchive(handle);
324}
Tao Baod0f30882016-11-03 23:52:01 -0700325
326TEST_F(UpdaterTest, write_value) {
327 // write_value() expects two arguments.
328 expect(nullptr, "write_value()", kArgsParsingFailure);
329 expect(nullptr, "write_value(\"arg1\")", kArgsParsingFailure);
330 expect(nullptr, "write_value(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
331
332 // filename cannot be empty.
333 expect(nullptr, "write_value(\"value\", \"\")", kArgsParsingFailure);
334
335 // Write some value to file.
336 TemporaryFile temp_file;
337 std::string value = "magicvalue";
338 std::string script("write_value(\"" + value + "\", \"" + std::string(temp_file.path) + "\")");
339 expect("t", script.c_str(), kNoCause);
340
341 // Verify the content.
342 std::string content;
343 ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content));
344 ASSERT_EQ(value, content);
345
346 // Allow writing empty string.
347 script = "write_value(\"\", \"" + std::string(temp_file.path) + "\")";
348 expect("t", script.c_str(), kNoCause);
349
350 // Verify the content.
351 ASSERT_TRUE(android::base::ReadFileToString(temp_file.path, &content));
352 ASSERT_EQ("", content);
353
354 // It should fail gracefully when write fails.
355 script = "write_value(\"value\", \"/proc/0/file1\")";
356 expect("", script.c_str(), kNoCause);
357}