blob: f922933cda9dafcbe30bae06b23e199209d4cfde [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
17#include <string>
18
Tao Bao51d516e2016-11-03 14:49:01 -070019#include <android-base/file.h>
Tao Bao0c7839a2016-10-10 15:48:37 -070020#include <android-base/properties.h>
Tao Bao51d516e2016-11-03 14:49:01 -070021#include <android-base/test_utils.h>
Tao Bao0c7839a2016-10-10 15:48:37 -070022#include <gtest/gtest.h>
23
24#include "edify/expr.h"
25#include "error_code.h"
26#include "updater/install.h"
27
28struct selabel_handle *sehandle = nullptr;
29
Tao Bao361342c2016-02-08 11:15:50 -080030static void expect(const char* expected, const char* expr_str, CauseCode cause_code) {
Tao Bao0c7839a2016-10-10 15:48:37 -070031 Expr* e;
32 int error_count;
33 EXPECT_EQ(parse_string(expr_str, &e, &error_count), 0);
34
35 State state(expr_str, nullptr);
36
Tianjie Xuaced5d92016-10-12 10:55:04 -070037 std::string result;
38 bool status = Evaluate(&state, e, &result);
Tao Bao0c7839a2016-10-10 15:48:37 -070039
40 if (expected == nullptr) {
Tianjie Xuaced5d92016-10-12 10:55:04 -070041 EXPECT_FALSE(status);
Tao Bao0c7839a2016-10-10 15:48:37 -070042 } else {
Tianjie Xuaced5d92016-10-12 10:55:04 -070043 EXPECT_STREQ(expected, result.c_str());
Tao Bao0c7839a2016-10-10 15:48:37 -070044 }
45
Tao Bao361342c2016-02-08 11:15:50 -080046 // Error code is set in updater/updater.cpp only, by parsing State.errmsg.
47 EXPECT_EQ(kNoError, state.error_code);
48
49 // Cause code should always be available.
Tao Bao0c7839a2016-10-10 15:48:37 -070050 EXPECT_EQ(cause_code, state.cause_code);
51
Tao Bao0c7839a2016-10-10 15:48:37 -070052}
53
54class UpdaterTest : public ::testing::Test {
55 protected:
56 virtual void SetUp() {
57 RegisterBuiltins();
58 RegisterInstallFunctions();
Tao Bao0c7839a2016-10-10 15:48:37 -070059 }
60};
61
62TEST_F(UpdaterTest, getprop) {
63 expect(android::base::GetProperty("ro.product.device", "").c_str(),
64 "getprop(\"ro.product.device\")",
Tao Bao361342c2016-02-08 11:15:50 -080065 kNoCause);
Tao Bao0c7839a2016-10-10 15:48:37 -070066
67 expect(android::base::GetProperty("ro.build.fingerprint", "").c_str(),
68 "getprop(\"ro.build.fingerprint\")",
Tao Bao361342c2016-02-08 11:15:50 -080069 kNoCause);
Tao Bao0c7839a2016-10-10 15:48:37 -070070
71 // getprop() accepts only one parameter.
Tao Bao361342c2016-02-08 11:15:50 -080072 expect(nullptr, "getprop()", kArgsParsingFailure);
73 expect(nullptr, "getprop(\"arg1\", \"arg2\")", kArgsParsingFailure);
74}
75
76TEST_F(UpdaterTest, sha1_check) {
77 // sha1_check(data) returns the SHA-1 of the data.
78 expect("81fe8bfe87576c3ecb22426f8e57847382917acf", "sha1_check(\"abcd\")", kNoCause);
79 expect("da39a3ee5e6b4b0d3255bfef95601890afd80709", "sha1_check(\"\")", kNoCause);
80
81 // sha1_check(data, sha1_hex, [sha1_hex, ...]) returns the matched SHA-1.
82 expect("81fe8bfe87576c3ecb22426f8e57847382917acf",
83 "sha1_check(\"abcd\", \"81fe8bfe87576c3ecb22426f8e57847382917acf\")",
84 kNoCause);
85
86 expect("81fe8bfe87576c3ecb22426f8e57847382917acf",
87 "sha1_check(\"abcd\", \"wrong_sha1\", \"81fe8bfe87576c3ecb22426f8e57847382917acf\")",
88 kNoCause);
89
90 // Or "" if there's no match.
91 expect("",
92 "sha1_check(\"abcd\", \"wrong_sha1\")",
93 kNoCause);
94
95 expect("",
96 "sha1_check(\"abcd\", \"wrong_sha1\", \"wrong_sha2\")",
97 kNoCause);
98
99 // sha1_check() expects at least one argument.
100 expect(nullptr, "sha1_check()", kArgsParsingFailure);
Tao Bao0c7839a2016-10-10 15:48:37 -0700101}
Tao Bao51d516e2016-11-03 14:49:01 -0700102
103TEST_F(UpdaterTest, file_getprop) {
104 // file_getprop() expects two arguments.
105 expect(nullptr, "file_getprop()", kArgsParsingFailure);
106 expect(nullptr, "file_getprop(\"arg1\")", kArgsParsingFailure);
107 expect(nullptr, "file_getprop(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
108
109 // File doesn't exist.
110 expect(nullptr, "file_getprop(\"/doesntexist\", \"key1\")", kFileGetPropFailure);
111
112 // Reject too large files (current limit = 65536).
113 TemporaryFile temp_file1;
114 std::string buffer(65540, '\0');
115 ASSERT_TRUE(android::base::WriteStringToFile(buffer, temp_file1.path));
116
117 // Read some keys.
118 TemporaryFile temp_file2;
119 std::string content("ro.product.name=tardis\n"
120 "# comment\n\n\n"
121 "ro.product.model\n"
122 "ro.product.board = magic \n");
123 ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file2.path));
124
125 std::string script1("file_getprop(\"" + std::string(temp_file2.path) +
126 "\", \"ro.product.name\")");
127 expect("tardis", script1.c_str(), kNoCause);
128
129 std::string script2("file_getprop(\"" + std::string(temp_file2.path) +
130 "\", \"ro.product.board\")");
131 expect("magic", script2.c_str(), kNoCause);
132
133 // No match.
134 std::string script3("file_getprop(\"" + std::string(temp_file2.path) +
135 "\", \"ro.product.wrong\")");
136 expect("", script3.c_str(), kNoCause);
137
138 std::string script4("file_getprop(\"" + std::string(temp_file2.path) +
139 "\", \"ro.product.name=\")");
140 expect("", script4.c_str(), kNoCause);
141
142 std::string script5("file_getprop(\"" + std::string(temp_file2.path) +
143 "\", \"ro.product.nam\")");
144 expect("", script5.c_str(), kNoCause);
145
146 std::string script6("file_getprop(\"" + std::string(temp_file2.path) +
147 "\", \"ro.product.model\")");
148 expect("", script6.c_str(), kNoCause);
149}
Tao Bao0831d0b2016-11-03 23:25:04 -0700150
151TEST_F(UpdaterTest, delete) {
152 // Delete none.
153 expect("0", "delete()", kNoCause);
154 expect("0", "delete(\"/doesntexist\")", kNoCause);
155 expect("0", "delete(\"/doesntexist1\", \"/doesntexist2\")", kNoCause);
156 expect("0", "delete(\"/doesntexist1\", \"/doesntexist2\", \"/doesntexist3\")", kNoCause);
157
158 // Delete one file.
159 TemporaryFile temp_file1;
160 ASSERT_TRUE(android::base::WriteStringToFile("abc", temp_file1.path));
161 std::string script1("delete(\"" + std::string(temp_file1.path) + "\")");
162 expect("1", script1.c_str(), kNoCause);
163
164 // Delete two files.
165 TemporaryFile temp_file2;
166 ASSERT_TRUE(android::base::WriteStringToFile("abc", temp_file2.path));
167 TemporaryFile temp_file3;
168 ASSERT_TRUE(android::base::WriteStringToFile("abc", temp_file3.path));
169 std::string script2("delete(\"" + std::string(temp_file2.path) + "\", \"" +
170 std::string(temp_file3.path) + "\")");
171 expect("2", script2.c_str(), kNoCause);
172
173 // Delete already deleted files.
174 expect("0", script2.c_str(), kNoCause);
175
176 // Delete one out of three.
177 TemporaryFile temp_file4;
178 ASSERT_TRUE(android::base::WriteStringToFile("abc", temp_file4.path));
179 std::string script3("delete(\"/doesntexist1\", \"" + std::string(temp_file4.path) +
180 "\", \"/doesntexist2\")");
181 expect("1", script3.c_str(), kNoCause);
182}
Tao Baoa659d792016-11-03 23:25:04 -0700183
184TEST_F(UpdaterTest, rename) {
185 // rename() expects two arguments.
186 expect(nullptr, "rename()", kArgsParsingFailure);
187 expect(nullptr, "rename(\"arg1\")", kArgsParsingFailure);
188 expect(nullptr, "rename(\"arg1\", \"arg2\", \"arg3\")", kArgsParsingFailure);
189
190 // src_name or dst_name cannot be empty.
191 expect(nullptr, "rename(\"\", \"arg2\")", kArgsParsingFailure);
192 expect(nullptr, "rename(\"arg1\", \"\")", kArgsParsingFailure);
193
194 // File doesn't exist (both of src and dst).
195 expect(nullptr, "rename(\"/doesntexist\", \"/doesntexisteither\")" , kFileRenameFailure);
196
197 // Can't create parent directory.
198 TemporaryFile temp_file1;
199 ASSERT_TRUE(android::base::WriteStringToFile("abc", temp_file1.path));
200 std::string script1("rename(\"" + std::string(temp_file1.path) + "\", \"/proc/0/file1\")");
201 expect(nullptr, script1.c_str(), kFileRenameFailure);
202
203 // Rename.
204 TemporaryFile temp_file2;
205 std::string script2("rename(\"" + std::string(temp_file1.path) + "\", \"" +
206 std::string(temp_file2.path) + "\")");
207 expect(temp_file2.path, script2.c_str(), kNoCause);
208
209 // Already renamed.
210 expect(temp_file2.path, script2.c_str(), kNoCause);
211}