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 <gtest/gtest.h>
20 #include <hidl/GtestPrinter.h>
21 #include <hidl/ServiceManagement.h>
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  */
31 class AuthSecretHidlTest : public testing::TestWithParam<std::string> {
32   public:
SetUp()33     virtual void SetUp() override {
34         authsecret = IAuthSecret::getService(GetParam());
35         ASSERT_NE(authsecret, nullptr);
36 
37         // Notify LSS to generate PIN code '1234' and corresponding secret.
38         (void)system("cmd lock_settings set-pin 1234");
39 
40         // All tests must enroll the correct secret first as this cannot be changed
41         // without a factory reset and the order of tests could change.
42         authsecret->primaryUserCredential(CORRECT_SECRET);
43     }
44 
TearDownTestSuite()45     static void TearDownTestSuite() {
46         // clean up PIN code after testing
47         (void)system("cmd lock_settings clear --old 1234");
48     }
49 
50     sp<IAuthSecret> authsecret;
51     hidl_vec<uint8_t> CORRECT_SECRET{61, 93, 124, 240, 5, 0, 7, 201, 9, 129, 11, 12, 0, 14, 0, 16};
52     hidl_vec<uint8_t> WRONG_SECRET{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
53 };
54 
55 /* Provision the primary user with a secret. */
TEST_P(AuthSecretHidlTest,provisionPrimaryUserCredential)56 TEST_P(AuthSecretHidlTest, provisionPrimaryUserCredential) {
57     // Secret provisioned by SetUp()
58 }
59 
60 /* Provision the primary user with a secret and pass the secret again. */
TEST_P(AuthSecretHidlTest,provisionPrimaryUserCredentialAndPassAgain)61 TEST_P(AuthSecretHidlTest, provisionPrimaryUserCredentialAndPassAgain) {
62     // Secret provisioned by SetUp()
63     authsecret->primaryUserCredential(CORRECT_SECRET);
64 }
65 
66 /* Provision the primary user with a secret and pass the secret again repeatedly. */
TEST_P(AuthSecretHidlTest,provisionPrimaryUserCredentialAndPassAgainMultipleTimes)67 TEST_P(AuthSecretHidlTest, provisionPrimaryUserCredentialAndPassAgainMultipleTimes) {
68     // Secret provisioned by SetUp()
69     constexpr int N = 5;
70     for (int i = 0; i < N; ++i) {
71         authsecret->primaryUserCredential(CORRECT_SECRET);
72     }
73 }
74 
75 /* Provision the primary user with a secret and then pass the wrong secret. This
76  * should never happen and is an framework bug if it does. As the secret is
77  * wrong, the HAL implementation may not be able to function correctly but it
78  * should fail gracefully. */
TEST_P(AuthSecretHidlTest,provisionPrimaryUserCredentialAndWrongSecret)79 TEST_P(AuthSecretHidlTest, provisionPrimaryUserCredentialAndWrongSecret) {
80     // Secret provisioned by SetUp()
81     authsecret->primaryUserCredential(WRONG_SECRET);
82 }
83 
84 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AuthSecretHidlTest);
85 INSTANTIATE_TEST_SUITE_P(
86         PerInstance, AuthSecretHidlTest,
87         testing::ValuesIn(android::hardware::getAllHalInstanceNames(IAuthSecret::descriptor)),
88         android::hardware::PrintInstanceNameToString);
89