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 | |
| 28 | static void expect(const char* expected, const char* expr_str, |
| 29 | ErrorCode error_code, CauseCode cause_code) { |
| 30 | Expr* e; |
| 31 | int error_count; |
| 32 | EXPECT_EQ(parse_string(expr_str, &e, &error_count), 0); |
| 33 | |
| 34 | State state(expr_str, nullptr); |
| 35 | |
| 36 | char* result = Evaluate(&state, e); |
| 37 | |
| 38 | if (expected == nullptr) { |
| 39 | EXPECT_EQ(nullptr, result); |
| 40 | } else { |
| 41 | EXPECT_STREQ(expected, result); |
| 42 | } |
| 43 | |
| 44 | EXPECT_EQ(error_code, state.error_code); |
| 45 | EXPECT_EQ(cause_code, state.cause_code); |
| 46 | |
| 47 | free(result); |
| 48 | } |
| 49 | |
| 50 | class UpdaterTest : public ::testing::Test { |
| 51 | protected: |
| 52 | virtual void SetUp() { |
| 53 | RegisterBuiltins(); |
| 54 | RegisterInstallFunctions(); |
| 55 | FinishRegistration(); |
| 56 | } |
| 57 | }; |
| 58 | |
| 59 | TEST_F(UpdaterTest, getprop) { |
| 60 | expect(android::base::GetProperty("ro.product.device", "").c_str(), |
| 61 | "getprop(\"ro.product.device\")", |
| 62 | kNoError, kNoCause); |
| 63 | |
| 64 | expect(android::base::GetProperty("ro.build.fingerprint", "").c_str(), |
| 65 | "getprop(\"ro.build.fingerprint\")", |
| 66 | kNoError, kNoCause); |
| 67 | |
| 68 | // getprop() accepts only one parameter. |
| 69 | expect(nullptr, "getprop()", kNoError, kArgsParsingFailure); |
| 70 | expect(nullptr, "getprop(\"arg1\", \"arg2\")", kNoError, kArgsParsingFailure); |
| 71 | } |