Andrew Scull | 7093431 | 2018-01-03 11:51:54 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 <android/hardware/authsecret/1.0/IAuthSecret.h> |
| 18 | |
nelsonli | ff4de28 | 2019-10-15 17:09:04 +0800 | [diff] [blame] | 19 | #include <gtest/gtest.h> |
| 20 | #include <hidl/GtestPrinter.h> |
| 21 | #include <hidl/ServiceManagement.h> |
Andrew Scull | 7093431 | 2018-01-03 11:51:54 +0000 | [diff] [blame] | 22 | |
| 23 | using ::android::hardware::hidl_vec; |
| 24 | using ::android::hardware::authsecret::V1_0::IAuthSecret; |
| 25 | using ::android::sp; |
| 26 | |
| 27 | /** |
| 28 | * There is no expected behaviour that can be tested so these tests check the |
| 29 | * HAL doesn't crash with different execution orders. |
| 30 | */ |
nelsonli | ff4de28 | 2019-10-15 17:09:04 +0800 | [diff] [blame] | 31 | class AuthSecretHidlTest : public testing::TestWithParam<std::string> { |
| 32 | public: |
Andrew Scull | 7093431 | 2018-01-03 11:51:54 +0000 | [diff] [blame] | 33 | virtual void SetUp() override { |
nelsonli | ff4de28 | 2019-10-15 17:09:04 +0800 | [diff] [blame] | 34 | authsecret = IAuthSecret::getService(GetParam()); |
Andrew Scull | 7093431 | 2018-01-03 11:51:54 +0000 | [diff] [blame] | 35 | ASSERT_NE(authsecret, nullptr); |
Andrew Scull | bd4e48c | 2018-01-19 19:17:56 +0000 | [diff] [blame] | 36 | |
| 37 | // All tests must enroll the correct secret first as this cannot be changed |
| 38 | // without a factory reset and the order of tests could change. |
| 39 | authsecret->primaryUserCredential(CORRECT_SECRET); |
Andrew Scull | 7093431 | 2018-01-03 11:51:54 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | sp<IAuthSecret> authsecret; |
Andrew Scull | bd4e48c | 2018-01-19 19:17:56 +0000 | [diff] [blame] | 43 | hidl_vec<uint8_t> CORRECT_SECRET{61, 93, 124, 240, 5, 0, 7, 201, 9, 129, 11, 12, 0, 14, 0, 16}; |
| 44 | hidl_vec<uint8_t> WRONG_SECRET{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; |
Andrew Scull | 7093431 | 2018-01-03 11:51:54 +0000 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | /* Provision the primary user with a secret. */ |
nelsonli | ff4de28 | 2019-10-15 17:09:04 +0800 | [diff] [blame] | 48 | TEST_P(AuthSecretHidlTest, provisionPrimaryUserCredential) { |
Andrew Scull | bd4e48c | 2018-01-19 19:17:56 +0000 | [diff] [blame] | 49 | // Secret provisioned by SetUp() |
Andrew Scull | 7093431 | 2018-01-03 11:51:54 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | /* Provision the primary user with a secret and pass the secret again. */ |
nelsonli | ff4de28 | 2019-10-15 17:09:04 +0800 | [diff] [blame] | 53 | TEST_P(AuthSecretHidlTest, provisionPrimaryUserCredentialAndPassAgain) { |
Andrew Scull | bd4e48c | 2018-01-19 19:17:56 +0000 | [diff] [blame] | 54 | // Secret provisioned by SetUp() |
| 55 | authsecret->primaryUserCredential(CORRECT_SECRET); |
Andrew Scull | 7093431 | 2018-01-03 11:51:54 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /* Provision the primary user with a secret and pass the secret again repeatedly. */ |
nelsonli | ff4de28 | 2019-10-15 17:09:04 +0800 | [diff] [blame] | 59 | TEST_P(AuthSecretHidlTest, provisionPrimaryUserCredentialAndPassAgainMultipleTimes) { |
Andrew Scull | bd4e48c | 2018-01-19 19:17:56 +0000 | [diff] [blame] | 60 | // Secret provisioned by SetUp() |
Andrew Scull | 7093431 | 2018-01-03 11:51:54 +0000 | [diff] [blame] | 61 | constexpr int N = 5; |
| 62 | for (int i = 0; i < N; ++i) { |
Andrew Scull | bd4e48c | 2018-01-19 19:17:56 +0000 | [diff] [blame] | 63 | authsecret->primaryUserCredential(CORRECT_SECRET); |
Andrew Scull | 7093431 | 2018-01-03 11:51:54 +0000 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
Andrew Scull | bd4e48c | 2018-01-19 19:17:56 +0000 | [diff] [blame] | 67 | /* Provision the primary user with a secret and then pass the wrong secret. This |
| 68 | * should never happen and is an framework bug if it does. As the secret is |
| 69 | * wrong, the HAL implementation may not be able to function correctly but it |
| 70 | * should fail gracefully. */ |
nelsonli | ff4de28 | 2019-10-15 17:09:04 +0800 | [diff] [blame] | 71 | TEST_P(AuthSecretHidlTest, provisionPrimaryUserCredentialAndWrongSecret) { |
Andrew Scull | bd4e48c | 2018-01-19 19:17:56 +0000 | [diff] [blame] | 72 | // Secret provisioned by SetUp() |
| 73 | authsecret->primaryUserCredential(WRONG_SECRET); |
Andrew Scull | 7093431 | 2018-01-03 11:51:54 +0000 | [diff] [blame] | 74 | } |
Zhuoyao Zhang | 3c3b000 | 2018-02-28 18:04:10 -0800 | [diff] [blame] | 75 | |
nelsonli | ff4de28 | 2019-10-15 17:09:04 +0800 | [diff] [blame] | 76 | INSTANTIATE_TEST_SUITE_P( |
| 77 | PerInstance, AuthSecretHidlTest, |
| 78 | testing::ValuesIn(android::hardware::getAllHalInstanceNames(IAuthSecret::descriptor)), |
| 79 | android::hardware::PrintInstanceNameToString); |