Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 1 | /* |
| 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 Bao | 8992902 | 2016-11-08 20:51:31 -0800 | [diff] [blame] | 17 | #include <sys/stat.h> |
| 18 | #include <sys/types.h> |
| 19 | #include <unistd.h> |
| 20 | |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 21 | #include <string> |
| 22 | |
Tao Bao | 51d516e | 2016-11-03 14:49:01 -0700 | [diff] [blame] | 23 | #include <android-base/file.h> |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 24 | #include <android-base/properties.h> |
Tao Bao | 51d516e | 2016-11-03 14:49:01 -0700 | [diff] [blame] | 25 | #include <android-base/test_utils.h> |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 26 | #include <gtest/gtest.h> |
Tao Bao | 2274e57 | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 27 | #include <ziparchive/zip_archive.h> |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 28 | |
Tao Bao | 2274e57 | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 29 | #include "common/test_constants.h" |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 30 | #include "edify/expr.h" |
| 31 | #include "error_code.h" |
| 32 | #include "updater/install.h" |
Tao Bao | 2274e57 | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 33 | #include "updater/updater.h" |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 34 | |
| 35 | struct selabel_handle *sehandle = nullptr; |
| 36 | |
Tao Bao | 2274e57 | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 37 | static 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 Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 43 | |
Tao Bao | 2274e57 | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 44 | State state(expr_str, info); |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 45 | |
Tao Bao | 2274e57 | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 46 | std::string result; |
| 47 | bool status = Evaluate(&state, e, &result); |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 48 | |
Tao Bao | 2274e57 | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 49 | if (expected == nullptr) { |
| 50 | ASSERT_FALSE(status); |
| 51 | } else { |
| 52 | ASSERT_TRUE(status); |
| 53 | ASSERT_STREQ(expected, result.c_str()); |
| 54 | } |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 55 | |
Tao Bao | 2274e57 | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 56 | // Error code is set in updater/updater.cpp only, by parsing State.errmsg. |
| 57 | ASSERT_EQ(kNoError, state.error_code); |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 58 | |
Tao Bao | 2274e57 | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 59 | // Cause code should always be available. |
| 60 | ASSERT_EQ(cause_code, state.cause_code); |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | class UpdaterTest : public ::testing::Test { |
| 64 | protected: |
| 65 | virtual void SetUp() { |
| 66 | RegisterBuiltins(); |
| 67 | RegisterInstallFunctions(); |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 68 | } |
| 69 | }; |
| 70 | |
| 71 | TEST_F(UpdaterTest, getprop) { |
| 72 | expect(android::base::GetProperty("ro.product.device", "").c_str(), |
| 73 | "getprop(\"ro.product.device\")", |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 74 | kNoCause); |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 75 | |
| 76 | expect(android::base::GetProperty("ro.build.fingerprint", "").c_str(), |
| 77 | "getprop(\"ro.build.fingerprint\")", |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 78 | kNoCause); |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 79 | |
| 80 | // getprop() accepts only one parameter. |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 81 | expect(nullptr, "getprop()", kArgsParsingFailure); |
| 82 | expect(nullptr, "getprop(\"arg1\", \"arg2\")", kArgsParsingFailure); |
| 83 | } |
| 84 | |
| 85 | TEST_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 Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 110 | } |
Tao Bao | 51d516e | 2016-11-03 14:49:01 -0700 | [diff] [blame] | 111 | |
| 112 | TEST_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 Bao | 0831d0b | 2016-11-03 23:25:04 -0700 | [diff] [blame] | 159 | |
| 160 | TEST_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 Bao | a659d79 | 2016-11-03 23:25:04 -0700 | [diff] [blame] | 192 | |
| 193 | TEST_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 Xu | d75003d | 2016-11-04 11:31:29 -0700 | [diff] [blame] | 220 | |
| 221 | // Parents create successfully. |
| 222 | TemporaryFile temp_file3; |
| 223 | TemporaryDir td; |
Tao Bao | 8992902 | 2016-11-08 20:51:31 -0800 | [diff] [blame] | 224 | 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 Xu | d75003d | 2016-11-04 11:31:29 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | TEST_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 Bao | 8992902 | 2016-11-08 20:51:31 -0800 | [diff] [blame] | 244 | std::string script2("symlink(\"" + std::string(temp_file1.path) + "\", \"src1\", \"\")"); |
Tianjie Xu | d75003d | 2016-11-04 11:31:29 -0700 | [diff] [blame] | 245 | expect(nullptr, script2.c_str(), kSymlinkFailure); |
Tao Bao | 8992902 | 2016-11-08 20:51:31 -0800 | [diff] [blame] | 246 | |
| 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 Bao | a659d79 | 2016-11-03 23:25:04 -0700 | [diff] [blame] | 271 | } |
Tao Bao | 2274e57 | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 272 | |
| 273 | // TODO: Test extracting to block device. |
| 274 | TEST_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 | } |