blob: 64a6b37ce019284f3fddae8014f8346e0e6132bb [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();
Tao Bao0c7839a2016-10-10 15:48:37 -070057 }
58};
59
60TEST_F(UpdaterTest, getprop) {
61 expect(android::base::GetProperty("ro.product.device", "").c_str(),
62 "getprop(\"ro.product.device\")",
Tao Bao361342c2016-02-08 11:15:50 -080063 kNoCause);
Tao Bao0c7839a2016-10-10 15:48:37 -070064
65 expect(android::base::GetProperty("ro.build.fingerprint", "").c_str(),
66 "getprop(\"ro.build.fingerprint\")",
Tao Bao361342c2016-02-08 11:15:50 -080067 kNoCause);
Tao Bao0c7839a2016-10-10 15:48:37 -070068
69 // getprop() accepts only one parameter.
Tao Bao361342c2016-02-08 11:15:50 -080070 expect(nullptr, "getprop()", kArgsParsingFailure);
71 expect(nullptr, "getprop(\"arg1\", \"arg2\")", kArgsParsingFailure);
72}
73
74TEST_F(UpdaterTest, sha1_check) {
75 // sha1_check(data) returns the SHA-1 of the data.
76 expect("81fe8bfe87576c3ecb22426f8e57847382917acf", "sha1_check(\"abcd\")", kNoCause);
77 expect("da39a3ee5e6b4b0d3255bfef95601890afd80709", "sha1_check(\"\")", kNoCause);
78
79 // sha1_check(data, sha1_hex, [sha1_hex, ...]) returns the matched SHA-1.
80 expect("81fe8bfe87576c3ecb22426f8e57847382917acf",
81 "sha1_check(\"abcd\", \"81fe8bfe87576c3ecb22426f8e57847382917acf\")",
82 kNoCause);
83
84 expect("81fe8bfe87576c3ecb22426f8e57847382917acf",
85 "sha1_check(\"abcd\", \"wrong_sha1\", \"81fe8bfe87576c3ecb22426f8e57847382917acf\")",
86 kNoCause);
87
88 // Or "" if there's no match.
89 expect("",
90 "sha1_check(\"abcd\", \"wrong_sha1\")",
91 kNoCause);
92
93 expect("",
94 "sha1_check(\"abcd\", \"wrong_sha1\", \"wrong_sha2\")",
95 kNoCause);
96
97 // sha1_check() expects at least one argument.
98 expect(nullptr, "sha1_check()", kArgsParsingFailure);
Tao Bao0c7839a2016-10-10 15:48:37 -070099}