blob: bd6534b3b6e5188e3734587d3399870350581a13 [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
19#include <android-base/properties.h>
20#include <gtest/gtest.h>
21
22#include "edify/expr.h"
23#include "error_code.h"
24#include "updater/install.h"
25
26struct selabel_handle *sehandle = nullptr;
27
Tao Bao361342c2016-02-08 11:15:50 -080028static void expect(const char* expected, const char* expr_str, CauseCode cause_code) {
Tao Bao0c7839a2016-10-10 15:48:37 -070029 Expr* e;
30 int error_count;
31 EXPECT_EQ(parse_string(expr_str, &e, &error_count), 0);
32
33 State state(expr_str, nullptr);
34
35 char* result = Evaluate(&state, e);
36
37 if (expected == nullptr) {
38 EXPECT_EQ(nullptr, result);
39 } else {
40 EXPECT_STREQ(expected, result);
41 }
42
Tao Bao361342c2016-02-08 11:15:50 -080043 // Error code is set in updater/updater.cpp only, by parsing State.errmsg.
44 EXPECT_EQ(kNoError, state.error_code);
45
46 // Cause code should always be available.
Tao Bao0c7839a2016-10-10 15:48:37 -070047 EXPECT_EQ(cause_code, state.cause_code);
48
49 free(result);
50}
51
52class UpdaterTest : public ::testing::Test {
53 protected:
54 virtual void SetUp() {
55 RegisterBuiltins();
56 RegisterInstallFunctions();
57 FinishRegistration();
58 }
59};
60
61TEST_F(UpdaterTest, getprop) {
62 expect(android::base::GetProperty("ro.product.device", "").c_str(),
63 "getprop(\"ro.product.device\")",
Tao Bao361342c2016-02-08 11:15:50 -080064 kNoCause);
Tao Bao0c7839a2016-10-10 15:48:37 -070065
66 expect(android::base::GetProperty("ro.build.fingerprint", "").c_str(),
67 "getprop(\"ro.build.fingerprint\")",
Tao Bao361342c2016-02-08 11:15:50 -080068 kNoCause);
Tao Bao0c7839a2016-10-10 15:48:37 -070069
70 // getprop() accepts only one parameter.
Tao Bao361342c2016-02-08 11:15:50 -080071 expect(nullptr, "getprop()", kArgsParsingFailure);
72 expect(nullptr, "getprop(\"arg1\", \"arg2\")", kArgsParsingFailure);
73}
74
75TEST_F(UpdaterTest, sha1_check) {
76 // sha1_check(data) returns the SHA-1 of the data.
77 expect("81fe8bfe87576c3ecb22426f8e57847382917acf", "sha1_check(\"abcd\")", kNoCause);
78 expect("da39a3ee5e6b4b0d3255bfef95601890afd80709", "sha1_check(\"\")", kNoCause);
79
80 // sha1_check(data, sha1_hex, [sha1_hex, ...]) returns the matched SHA-1.
81 expect("81fe8bfe87576c3ecb22426f8e57847382917acf",
82 "sha1_check(\"abcd\", \"81fe8bfe87576c3ecb22426f8e57847382917acf\")",
83 kNoCause);
84
85 expect("81fe8bfe87576c3ecb22426f8e57847382917acf",
86 "sha1_check(\"abcd\", \"wrong_sha1\", \"81fe8bfe87576c3ecb22426f8e57847382917acf\")",
87 kNoCause);
88
89 // Or "" if there's no match.
90 expect("",
91 "sha1_check(\"abcd\", \"wrong_sha1\")",
92 kNoCause);
93
94 expect("",
95 "sha1_check(\"abcd\", \"wrong_sha1\", \"wrong_sha2\")",
96 kNoCause);
97
98 // sha1_check() expects at least one argument.
99 expect(nullptr, "sha1_check()", kArgsParsingFailure);
Tao Bao0c7839a2016-10-10 15:48:37 -0700100}