ChengYou Ho | 109522c | 2020-12-01 09:46:41 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2020 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 | #include <aidl/Gtest.h> |
| 17 | #include <aidl/Vintf.h> |
| 18 | |
| 19 | #include <aidl/android/hardware/oemlock/IOemLock.h> |
| 20 | #include <android/binder_manager.h> |
| 21 | #include <android/binder_process.h> |
| 22 | |
| 23 | using ::aidl::android::hardware::oemlock::IOemLock; |
| 24 | using ::aidl::android::hardware::oemlock::OemLockSecureStatus; |
| 25 | |
| 26 | using ndk::SpAIBinder; |
| 27 | |
| 28 | struct OemLockAidlTest : public ::testing::TestWithParam<std::string> { |
| 29 | virtual void SetUp() override { |
| 30 | oemlock = IOemLock::fromBinder( |
| 31 | SpAIBinder(AServiceManager_waitForService(GetParam().c_str()))); |
| 32 | ASSERT_NE(oemlock, nullptr); |
| 33 | } |
| 34 | |
| 35 | virtual void TearDown() override {} |
| 36 | |
| 37 | std::shared_ptr<IOemLock> oemlock; |
| 38 | }; |
| 39 | |
| 40 | /* |
| 41 | * Check the name can be retrieved |
| 42 | */ |
| 43 | TEST_P(OemLockAidlTest, GetName) { |
| 44 | std::string name; |
| 45 | |
| 46 | const auto ret = oemlock->getName(&name); |
| 47 | |
| 48 | ASSERT_TRUE(ret.isOk()); |
| 49 | // Any value acceptable |
| 50 | }; |
| 51 | |
| 52 | /* |
| 53 | * Check the unlock allowed by device state can be queried |
| 54 | */ |
| 55 | TEST_P(OemLockAidlTest, QueryUnlockAllowedByDevice) { |
| 56 | bool allowed; |
| 57 | |
| 58 | const auto ret = oemlock->isOemUnlockAllowedByDevice(&allowed); |
| 59 | |
| 60 | ASSERT_TRUE(ret.isOk()); |
| 61 | // Any value acceptable |
| 62 | } |
| 63 | |
| 64 | /* |
| 65 | * Check unlock allowed by device state can be toggled |
| 66 | */ |
| 67 | TEST_P(OemLockAidlTest, AllowedByDeviceCanBeToggled) { |
| 68 | bool allowed; |
| 69 | |
| 70 | // Get the original state so it can be restored |
| 71 | const auto get_ret = oemlock->isOemUnlockAllowedByDevice(&allowed); |
| 72 | ASSERT_TRUE(get_ret.isOk()); |
| 73 | const bool originallyAllowed = allowed; |
| 74 | |
| 75 | // Toggle the state |
| 76 | const auto set_ret = oemlock->setOemUnlockAllowedByDevice(!originallyAllowed); |
| 77 | ASSERT_TRUE(set_ret.isOk()); |
| 78 | |
| 79 | const auto check_set_ret = oemlock->isOemUnlockAllowedByDevice(&allowed); |
| 80 | ASSERT_TRUE(check_set_ret.isOk()); |
| 81 | ASSERT_EQ(allowed, !originallyAllowed); |
| 82 | |
| 83 | // Restore the state |
| 84 | const auto restore_ret = oemlock->setOemUnlockAllowedByDevice(originallyAllowed); |
| 85 | ASSERT_TRUE(restore_ret.isOk()); |
| 86 | |
| 87 | const auto check_restore_ret = oemlock->isOemUnlockAllowedByDevice(&allowed); |
| 88 | ASSERT_TRUE(check_restore_ret.isOk()); |
| 89 | ASSERT_EQ(allowed, originallyAllowed); |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | * Check the unlock allowed by device state can be queried |
| 94 | */ |
| 95 | TEST_P(OemLockAidlTest, QueryUnlockAllowedByCarrier) { |
| 96 | bool allowed; |
| 97 | |
| 98 | const auto ret = oemlock->isOemUnlockAllowedByCarrier(&allowed); |
| 99 | |
| 100 | ASSERT_TRUE(ret.isOk()); |
| 101 | // Any value acceptable |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * Attempt to check unlock allowed by carrier can be toggled |
| 106 | * |
| 107 | * The implementation may involve a signature which cannot be tested here. That |
| 108 | * is a valid implementation so the test will pass. If there is no signature |
| 109 | * required, the test will toggle the value. |
| 110 | */ |
| 111 | TEST_P(OemLockAidlTest, CarrierUnlock) { |
| 112 | const std::vector<uint8_t> noSignature = {}; |
| 113 | bool allowed; |
| 114 | OemLockSecureStatus secure_status; |
| 115 | |
| 116 | // Get the original state so it can be restored |
| 117 | const auto get_ret = oemlock->isOemUnlockAllowedByCarrier(&allowed); |
| 118 | ASSERT_TRUE(get_ret.isOk()); |
| 119 | const bool originallyAllowed = allowed; |
| 120 | |
| 121 | if (originallyAllowed) { |
| 122 | // Only applied to locked devices |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | // Toggle the state |
| 127 | const auto set_ret = oemlock->setOemUnlockAllowedByCarrier(!originallyAllowed, noSignature, &secure_status); |
| 128 | ASSERT_TRUE(set_ret.isOk()); |
| 129 | ASSERT_NE(secure_status, OemLockSecureStatus::FAILED); |
| 130 | const auto set_status = secure_status; |
| 131 | |
| 132 | const auto check_set_ret = oemlock->isOemUnlockAllowedByCarrier(&allowed); |
| 133 | ASSERT_TRUE(check_set_ret.isOk()); |
| 134 | |
| 135 | if (set_status == OemLockSecureStatus::INVALID_SIGNATURE) { |
| 136 | // Signature is required so we cannot toggle the value in the test, but this is allowed |
| 137 | ASSERT_EQ(allowed, originallyAllowed); |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | ASSERT_EQ(set_status, OemLockSecureStatus::OK); |
| 142 | ASSERT_EQ(allowed, !originallyAllowed); |
| 143 | |
| 144 | // Restore the state |
| 145 | const auto restore_ret = oemlock->setOemUnlockAllowedByCarrier(originallyAllowed, noSignature, &secure_status); |
| 146 | ASSERT_TRUE(restore_ret.isOk()); |
| 147 | ASSERT_EQ(secure_status, OemLockSecureStatus::OK); |
| 148 | |
| 149 | const auto check_restore_ret = oemlock->isOemUnlockAllowedByCarrier(&allowed); |
| 150 | ASSERT_TRUE(check_restore_ret.isOk()); |
| 151 | ASSERT_EQ(allowed, originallyAllowed); |
| 152 | } |
| 153 | |
| 154 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(OemLockAidlTest); |
| 155 | INSTANTIATE_TEST_SUITE_P( |
| 156 | PerInstance, OemLockAidlTest, |
| 157 | testing::ValuesIn(android::getAidlHalInstanceNames(IOemLock::descriptor)), |
| 158 | android::PrintInstanceNameToString); |
| 159 | |
| 160 | int main(int argc, char** argv) { |
| 161 | ::testing::InitGoogleTest(&argc, argv); |
| 162 | ABinderProcess_setThreadPoolMaxThreadCount(1); |
| 163 | ABinderProcess_startThreadPool(); |
| 164 | return RUN_ALL_TESTS(); |
| 165 | } |