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 | |
| 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 | |
| 26 | struct selabel_handle *sehandle = nullptr; |
| 27 | |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 28 | static void expect(const char* expected, const char* expr_str, CauseCode cause_code) { |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 29 | 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 | |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 35 | std::string result; |
| 36 | bool status = Evaluate(&state, e, &result); |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 37 | |
| 38 | if (expected == nullptr) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 39 | EXPECT_FALSE(status); |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 40 | } else { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 41 | EXPECT_STREQ(expected, result.c_str()); |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 44 | // Error code is set in updater/updater.cpp only, by parsing State.errmsg. |
| 45 | EXPECT_EQ(kNoError, state.error_code); |
| 46 | |
| 47 | // Cause code should always be available. |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 48 | EXPECT_EQ(cause_code, state.cause_code); |
| 49 | |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | class UpdaterTest : public ::testing::Test { |
| 53 | protected: |
| 54 | virtual void SetUp() { |
| 55 | RegisterBuiltins(); |
| 56 | RegisterInstallFunctions(); |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 57 | } |
| 58 | }; |
| 59 | |
| 60 | TEST_F(UpdaterTest, getprop) { |
| 61 | expect(android::base::GetProperty("ro.product.device", "").c_str(), |
| 62 | "getprop(\"ro.product.device\")", |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 63 | kNoCause); |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 64 | |
| 65 | expect(android::base::GetProperty("ro.build.fingerprint", "").c_str(), |
| 66 | "getprop(\"ro.build.fingerprint\")", |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 67 | kNoCause); |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 68 | |
| 69 | // getprop() accepts only one parameter. |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 70 | expect(nullptr, "getprop()", kArgsParsingFailure); |
| 71 | expect(nullptr, "getprop(\"arg1\", \"arg2\")", kArgsParsingFailure); |
| 72 | } |
| 73 | |
| 74 | TEST_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 Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 99 | } |