blob: 973c19de340b63817470407c876fbdd14b0fb3ff [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>
27
28#include "edify/expr.h"
29#include "error_code.h"
30#include "updater/install.h"
31
32struct selabel_handle *sehandle = nullptr;
33
Tao Bao361342c2016-02-08 11:15:50 -080034static void expect(const char* expected, const char* expr_str, CauseCode cause_code) {
Tao Bao0c7839a2016-10-10 15:48:37 -070035 Expr* e;
36 int error_count;
37 EXPECT_EQ(parse_string(expr_str, &e, &error_count), 0);
38
39 State state(expr_str, nullptr);
40
Tianjie Xuaced5d92016-10-12 10:55:04 -070041 std::string result;
42 bool status = Evaluate(&state, e, &result);
Tao Bao0c7839a2016-10-10 15:48:37 -070043
44 if (expected == nullptr) {
Tianjie Xuaced5d92016-10-12 10:55:04 -070045 EXPECT_FALSE(status);
Tao Bao0c7839a2016-10-10 15:48:37 -070046 } else {
Tianjie Xuaced5d92016-10-12 10:55:04 -070047 EXPECT_STREQ(expected, result.c_str());
Tao Bao0c7839a2016-10-10 15:48:37 -070048 }
49
Tao Bao361342c2016-02-08 11:15:50 -080050 // Error code is set in updater/updater.cpp only, by parsing State.errmsg.
51 EXPECT_EQ(kNoError, state.error_code);
52
53 // Cause code should always be available.
Tao Bao0c7839a2016-10-10 15:48:37 -070054 EXPECT_EQ(cause_code, state.cause_code);
55
Tao Bao0c7839a2016-10-10 15:48:37 -070056}
57
58class UpdaterTest : public ::testing::Test {
59 protected:
60 virtual void SetUp() {
61 RegisterBuiltins();
62 RegisterInstallFunctions();
Tao Bao0c7839a2016-10-10 15:48:37 -070063 }
64};
65
66TEST_F(UpdaterTest, getprop) {
67 expect(android::base::GetProperty("ro.product.device", "").c_str(),
68 "getprop(\"ro.product.device\")",
Tao Bao361342c2016-02-08 11:15:50 -080069 kNoCause);
Tao Bao0c7839a2016-10-10 15:48:37 -070070
71 expect(android::base::GetProperty("ro.build.fingerprint", "").c_str(),
72 "getprop(\"ro.build.fingerprint\")",
Tao Bao361342c2016-02-08 11:15:50 -080073 kNoCause);
Tao Bao0c7839a2016-10-10 15:48:37 -070074
75 // getprop() accepts only one parameter.
Tao Bao361342c2016-02-08 11:15:50 -080076 expect(nullptr, "getprop()", kArgsParsingFailure);
77 expect(nullptr, "getprop(\"arg1\", \"arg2\")", kArgsParsingFailure);
78}
79
80TEST_F(UpdaterTest, sha1_check) {
81 // sha1_check(data) returns the SHA-1 of the data.
82 expect("81fe8bfe87576c3ecb22426f8e57847382917acf", "sha1_check(\"abcd\")", kNoCause);
83 expect("da39a3ee5e6b4b0d3255bfef95601890afd80709", "sha1_check(\"\")", kNoCause);
84
85 // sha1_check(data, sha1_hex, [sha1_hex, ...]) returns the matched SHA-1.
86 expect("81fe8bfe87576c3ecb22426f8e57847382917acf",
87 "sha1_check(\"abcd\", \"81fe8bfe87576c3ecb22426f8e57847382917acf\")",
88 kNoCause);
89
90 expect("81fe8bfe87576c3ecb22426f8e57847382917acf",
91 "sha1_check(\"abcd\", \"wrong_sha1\", \"81fe8bfe87576c3ecb22426f8e57847382917acf\")",
92 kNoCause);
93
94 // Or "" if there's no match.
95 expect("",
96 "sha1_check(\"abcd\", \"wrong_sha1\")",
97 kNoCause);
98
99 expect("",
100 "sha1_check(\"abcd\", \"wrong_sha1\", \"wrong_sha2\")",
101 kNoCause);
102
103 // sha1_check() expects at least one argument.
104 expect(nullptr, "sha1_check()", kArgsParsingFailure);
Tao Bao0c7839a2016-10-10 15:48:37 -0700105}
Tao Bao51d516e2016-11-03 14:49:01 -0700106
107TEST_F(UpdaterTest, file_getprop) {
108 // file_getprop() expects two arguments.
109 expect(nullptr, "file_getprop()", kArgsParsingFailure);
110 expect(nullptr, "file_getprop(\"arg1\")", kArgsParsingFailure);
111 expect(nullptr, "file_getprop(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
112
113 // File doesn't exist.
114 expect(nullptr, "file_getprop(\"/doesntexist\", \"key1\")", kFileGetPropFailure);
115
116 // Reject too large files (current limit = 65536).
117 TemporaryFile temp_file1;
118 std::string buffer(65540, '\0');
119 ASSERT_TRUE(android::base::WriteStringToFile(buffer, temp_file1.path));
120
121 // Read some keys.
122 TemporaryFile temp_file2;
123 std::string content("ro.product.name=tardis\n"
124 "# comment\n\n\n"
125 "ro.product.model\n"
126 "ro.product.board = magic \n");
127 ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file2.path));
128
129 std::string script1("file_getprop(\"" + std::string(temp_file2.path) +
130 "\", \"ro.product.name\")");
131 expect("tardis", script1.c_str(), kNoCause);
132
133 std::string script2("file_getprop(\"" + std::string(temp_file2.path) +
134 "\", \"ro.product.board\")");
135 expect("magic", script2.c_str(), kNoCause);
136
137 // No match.
138 std::string script3("file_getprop(\"" + std::string(temp_file2.path) +
139 "\", \"ro.product.wrong\")");
140 expect("", script3.c_str(), kNoCause);
141
142 std::string script4("file_getprop(\"" + std::string(temp_file2.path) +
143 "\", \"ro.product.name=\")");
144 expect("", script4.c_str(), kNoCause);
145
146 std::string script5("file_getprop(\"" + std::string(temp_file2.path) +
147 "\", \"ro.product.nam\")");
148 expect("", script5.c_str(), kNoCause);
149
150 std::string script6("file_getprop(\"" + std::string(temp_file2.path) +
151 "\", \"ro.product.model\")");
152 expect("", script6.c_str(), kNoCause);
153}
Tao Bao0831d0b2016-11-03 23:25:04 -0700154
155TEST_F(UpdaterTest, delete) {
156 // Delete none.
157 expect("0", "delete()", kNoCause);
158 expect("0", "delete(\"/doesntexist\")", kNoCause);
159 expect("0", "delete(\"/doesntexist1\", \"/doesntexist2\")", kNoCause);
160 expect("0", "delete(\"/doesntexist1\", \"/doesntexist2\", \"/doesntexist3\")", kNoCause);
161
162 // Delete one file.
163 TemporaryFile temp_file1;
164 ASSERT_TRUE(android::base::WriteStringToFile("abc", temp_file1.path));
165 std::string script1("delete(\"" + std::string(temp_file1.path) + "\")");
166 expect("1", script1.c_str(), kNoCause);
167
168 // Delete two files.
169 TemporaryFile temp_file2;
170 ASSERT_TRUE(android::base::WriteStringToFile("abc", temp_file2.path));
171 TemporaryFile temp_file3;
172 ASSERT_TRUE(android::base::WriteStringToFile("abc", temp_file3.path));
173 std::string script2("delete(\"" + std::string(temp_file2.path) + "\", \"" +
174 std::string(temp_file3.path) + "\")");
175 expect("2", script2.c_str(), kNoCause);
176
177 // Delete already deleted files.
178 expect("0", script2.c_str(), kNoCause);
179
180 // Delete one out of three.
181 TemporaryFile temp_file4;
182 ASSERT_TRUE(android::base::WriteStringToFile("abc", temp_file4.path));
183 std::string script3("delete(\"/doesntexist1\", \"" + std::string(temp_file4.path) +
184 "\", \"/doesntexist2\")");
185 expect("1", script3.c_str(), kNoCause);
186}
Tao Baoa659d792016-11-03 23:25:04 -0700187
188TEST_F(UpdaterTest, rename) {
189 // rename() expects two arguments.
190 expect(nullptr, "rename()", kArgsParsingFailure);
191 expect(nullptr, "rename(\"arg1\")", kArgsParsingFailure);
192 expect(nullptr, "rename(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
193
194 // src_name or dst_name cannot be empty.
195 expect(nullptr, "rename(\"\", \"arg2\")", kArgsParsingFailure);
196 expect(nullptr, "rename(\"arg1\", \"\")", kArgsParsingFailure);
197
198 // File doesn't exist (both of src and dst).
199 expect(nullptr, "rename(\"/doesntexist\", \"/doesntexisteither\")" , kFileRenameFailure);
200
201 // Can't create parent directory.
202 TemporaryFile temp_file1;
203 ASSERT_TRUE(android::base::WriteStringToFile("abc", temp_file1.path));
204 std::string script1("rename(\"" + std::string(temp_file1.path) + "\", \"/proc/0/file1\")");
205 expect(nullptr, script1.c_str(), kFileRenameFailure);
206
207 // Rename.
208 TemporaryFile temp_file2;
209 std::string script2("rename(\"" + std::string(temp_file1.path) + "\", \"" +
210 std::string(temp_file2.path) + "\")");
211 expect(temp_file2.path, script2.c_str(), kNoCause);
212
213 // Already renamed.
214 expect(temp_file2.path, script2.c_str(), kNoCause);
Tianjie Xud75003d2016-11-04 11:31:29 -0700215
216 // Parents create successfully.
217 TemporaryFile temp_file3;
218 TemporaryDir td;
Tao Bao89929022016-11-08 20:51:31 -0800219 std::string temp_dir(td.path);
220 std::string dst_file = temp_dir + "/aaa/bbb/a.txt";
221 std::string script3("rename(\"" + std::string(temp_file3.path) + "\", \"" + dst_file + "\")");
222 expect(dst_file.c_str(), script3.c_str(), kNoCause);
223
224 // Clean up the temp files under td.
225 ASSERT_EQ(0, unlink(dst_file.c_str()));
226 ASSERT_EQ(0, rmdir((temp_dir + "/aaa/bbb").c_str()));
227 ASSERT_EQ(0, rmdir((temp_dir + "/aaa").c_str()));
Tianjie Xud75003d2016-11-04 11:31:29 -0700228}
229
230TEST_F(UpdaterTest, symlink) {
231 // symlink expects 1+ argument.
232 expect(nullptr, "symlink()", kArgsParsingFailure);
233
234 // symlink should fail if src is an empty string.
235 TemporaryFile temp_file1;
236 std::string script1("symlink(\"" + std::string(temp_file1.path) + "\", \"\")");
237 expect(nullptr, script1.c_str(), kSymlinkFailure);
238
Tao Bao89929022016-11-08 20:51:31 -0800239 std::string script2("symlink(\"" + std::string(temp_file1.path) + "\", \"src1\", \"\")");
Tianjie Xud75003d2016-11-04 11:31:29 -0700240 expect(nullptr, script2.c_str(), kSymlinkFailure);
Tao Bao89929022016-11-08 20:51:31 -0800241
242 // symlink failed to remove old src.
243 std::string script3("symlink(\"" + std::string(temp_file1.path) + "\", \"/proc\")");
244 expect(nullptr, script3.c_str(), kSymlinkFailure);
245
246 // symlink can create symlinks.
247 TemporaryFile temp_file;
248 std::string content = "magicvalue";
249 ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file.path));
250
251 TemporaryDir td;
252 std::string src1 = std::string(td.path) + "/symlink1";
253 std::string src2 = std::string(td.path) + "/symlink2";
254 std::string script4("symlink(\"" + std::string(temp_file.path) + "\", \"" +
255 src1 + "\", \"" + src2 + "\")");
256 expect("t", script4.c_str(), kNoCause);
257
258 // Verify the created symlinks.
259 struct stat sb;
260 ASSERT_TRUE(lstat(src1.c_str(), &sb) == 0 && S_ISLNK(sb.st_mode));
261 ASSERT_TRUE(lstat(src2.c_str(), &sb) == 0 && S_ISLNK(sb.st_mode));
262
263 // Clean up the leftovers.
264 ASSERT_EQ(0, unlink(src1.c_str()));
265 ASSERT_EQ(0, unlink(src2.c_str()));
Tao Baoa659d792016-11-03 23:25:04 -0700266}