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 | |
| 19 | #include <VtsHalHidlTargetTestBase.h> |
Zhuoyao Zhang | 3c3b000 | 2018-02-28 18:04:10 -0800 | [diff] [blame] | 20 | #include <VtsHalHidlTargetTestEnvBase.h> |
Andrew Scull | 7093431 | 2018-01-03 11:51:54 +0000 | [diff] [blame] | 21 | |
| 22 | using ::android::hardware::hidl_vec; |
| 23 | using ::android::hardware::authsecret::V1_0::IAuthSecret; |
| 24 | using ::android::sp; |
| 25 | |
Zhuoyao Zhang | 3c3b000 | 2018-02-28 18:04:10 -0800 | [diff] [blame] | 26 | // Test environment for Boot HIDL HAL. |
| 27 | class AuthSecretHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase { |
| 28 | public: |
| 29 | // get the test environment singleton |
| 30 | static AuthSecretHidlEnvironment* Instance() { |
| 31 | static AuthSecretHidlEnvironment* instance = new AuthSecretHidlEnvironment; |
| 32 | return instance; |
| 33 | } |
| 34 | |
| 35 | virtual void registerTestServices() override { registerTestService<IAuthSecret>(); } |
| 36 | |
| 37 | private: |
| 38 | AuthSecretHidlEnvironment() {} |
| 39 | }; |
| 40 | |
Andrew Scull | 7093431 | 2018-01-03 11:51:54 +0000 | [diff] [blame] | 41 | /** |
| 42 | * There is no expected behaviour that can be tested so these tests check the |
| 43 | * HAL doesn't crash with different execution orders. |
| 44 | */ |
| 45 | struct AuthSecretHidlTest : public ::testing::VtsHalHidlTargetTestBase { |
| 46 | virtual void SetUp() override { |
Zhuoyao Zhang | 3c3b000 | 2018-02-28 18:04:10 -0800 | [diff] [blame] | 47 | authsecret = ::testing::VtsHalHidlTargetTestBase::getService<IAuthSecret>( |
| 48 | AuthSecretHidlEnvironment::Instance()->getServiceName<IAuthSecret>()); |
Andrew Scull | 7093431 | 2018-01-03 11:51:54 +0000 | [diff] [blame] | 49 | ASSERT_NE(authsecret, nullptr); |
Andrew Scull | bd4e48c | 2018-01-19 19:17:56 +0000 | [diff] [blame] | 50 | |
| 51 | // All tests must enroll the correct secret first as this cannot be changed |
| 52 | // without a factory reset and the order of tests could change. |
| 53 | authsecret->primaryUserCredential(CORRECT_SECRET); |
Andrew Scull | 7093431 | 2018-01-03 11:51:54 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | sp<IAuthSecret> authsecret; |
Andrew Scull | bd4e48c | 2018-01-19 19:17:56 +0000 | [diff] [blame] | 57 | hidl_vec<uint8_t> CORRECT_SECRET{61, 93, 124, 240, 5, 0, 7, 201, 9, 129, 11, 12, 0, 14, 0, 16}; |
| 58 | 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] | 59 | }; |
| 60 | |
| 61 | /* Provision the primary user with a secret. */ |
| 62 | TEST_F(AuthSecretHidlTest, provisionPrimaryUserCredential) { |
Andrew Scull | bd4e48c | 2018-01-19 19:17:56 +0000 | [diff] [blame] | 63 | // Secret provisioned by SetUp() |
Andrew Scull | 7093431 | 2018-01-03 11:51:54 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /* Provision the primary user with a secret and pass the secret again. */ |
| 67 | TEST_F(AuthSecretHidlTest, provisionPrimaryUserCredentialAndPassAgain) { |
Andrew Scull | bd4e48c | 2018-01-19 19:17:56 +0000 | [diff] [blame] | 68 | // Secret provisioned by SetUp() |
| 69 | authsecret->primaryUserCredential(CORRECT_SECRET); |
Andrew Scull | 7093431 | 2018-01-03 11:51:54 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /* Provision the primary user with a secret and pass the secret again repeatedly. */ |
| 73 | TEST_F(AuthSecretHidlTest, provisionPrimaryUserCredentialAndPassAgainMultipleTimes) { |
Andrew Scull | bd4e48c | 2018-01-19 19:17:56 +0000 | [diff] [blame] | 74 | // Secret provisioned by SetUp() |
Andrew Scull | 7093431 | 2018-01-03 11:51:54 +0000 | [diff] [blame] | 75 | constexpr int N = 5; |
| 76 | for (int i = 0; i < N; ++i) { |
Andrew Scull | bd4e48c | 2018-01-19 19:17:56 +0000 | [diff] [blame] | 77 | authsecret->primaryUserCredential(CORRECT_SECRET); |
Andrew Scull | 7093431 | 2018-01-03 11:51:54 +0000 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
Andrew Scull | bd4e48c | 2018-01-19 19:17:56 +0000 | [diff] [blame] | 81 | /* Provision the primary user with a secret and then pass the wrong secret. This |
| 82 | * should never happen and is an framework bug if it does. As the secret is |
| 83 | * wrong, the HAL implementation may not be able to function correctly but it |
| 84 | * should fail gracefully. */ |
| 85 | TEST_F(AuthSecretHidlTest, provisionPrimaryUserCredentialAndWrongSecret) { |
| 86 | // Secret provisioned by SetUp() |
| 87 | authsecret->primaryUserCredential(WRONG_SECRET); |
Andrew Scull | 7093431 | 2018-01-03 11:51:54 +0000 | [diff] [blame] | 88 | } |
Zhuoyao Zhang | 3c3b000 | 2018-02-28 18:04:10 -0800 | [diff] [blame] | 89 | |
| 90 | int main(int argc, char** argv) { |
| 91 | ::testing::AddGlobalTestEnvironment(AuthSecretHidlEnvironment::Instance()); |
| 92 | ::testing::InitGoogleTest(&argc, argv); |
| 93 | AuthSecretHidlEnvironment::Instance()->init(&argc, argv); |
| 94 | int status = RUN_ALL_TESTS(); |
| 95 | ALOGI("Test result = %d", status); |
| 96 | return status; |
| 97 | } |