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>
20 #include <VtsHalHidlTargetTestEnvBase.h>
21
22 using ::android::hardware::hidl_vec;
23 using ::android::hardware::authsecret::V1_0::IAuthSecret;
24 using ::android::sp;
25
26 // Test environment for Boot HIDL HAL.
27 class AuthSecretHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase {
28 public:
29 // get the test environment singleton
Instance()30 static AuthSecretHidlEnvironment* Instance() {
31 static AuthSecretHidlEnvironment* instance = new AuthSecretHidlEnvironment;
32 return instance;
33 }
34
registerTestServices()35 virtual void registerTestServices() override { registerTestService<IAuthSecret>(); }
36
37 private:
AuthSecretHidlEnvironment()38 AuthSecretHidlEnvironment() {}
39 };
40
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 {
SetUpAuthSecretHidlTest46 virtual void SetUp() override {
47 authsecret = ::testing::VtsHalHidlTargetTestBase::getService<IAuthSecret>(
48 AuthSecretHidlEnvironment::Instance()->getServiceName<IAuthSecret>());
49 ASSERT_NE(authsecret, nullptr);
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);
54 }
55
56 sp<IAuthSecret> authsecret;
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};
59 };
60
61 /* Provision the primary user with a secret. */
TEST_F(AuthSecretHidlTest,provisionPrimaryUserCredential)62 TEST_F(AuthSecretHidlTest, provisionPrimaryUserCredential) {
63 // Secret provisioned by SetUp()
64 }
65
66 /* Provision the primary user with a secret and pass the secret again. */
TEST_F(AuthSecretHidlTest,provisionPrimaryUserCredentialAndPassAgain)67 TEST_F(AuthSecretHidlTest, provisionPrimaryUserCredentialAndPassAgain) {
68 // Secret provisioned by SetUp()
69 authsecret->primaryUserCredential(CORRECT_SECRET);
70 }
71
72 /* Provision the primary user with a secret and pass the secret again repeatedly. */
TEST_F(AuthSecretHidlTest,provisionPrimaryUserCredentialAndPassAgainMultipleTimes)73 TEST_F(AuthSecretHidlTest, provisionPrimaryUserCredentialAndPassAgainMultipleTimes) {
74 // Secret provisioned by SetUp()
75 constexpr int N = 5;
76 for (int i = 0; i < N; ++i) {
77 authsecret->primaryUserCredential(CORRECT_SECRET);
78 }
79 }
80
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. */
TEST_F(AuthSecretHidlTest,provisionPrimaryUserCredentialAndWrongSecret)85 TEST_F(AuthSecretHidlTest, provisionPrimaryUserCredentialAndWrongSecret) {
86 // Secret provisioned by SetUp()
87 authsecret->primaryUserCredential(WRONG_SECRET);
88 }
89
main(int argc,char ** argv)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 }
98