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