xref: /aosp_15_r20/hardware/interfaces/authsecret/aidl/vts/VtsHalAuthSecretTargetTest.cpp (revision 4d7e907c777eeecc4c5bd7cf640a754fac206ff7)
1*4d7e907cSAndroid Build Coastguard Worker /*
2*4d7e907cSAndroid Build Coastguard Worker  * Copyright (C) 2020 The Android Open Source Project
3*4d7e907cSAndroid Build Coastguard Worker  *
4*4d7e907cSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*4d7e907cSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*4d7e907cSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*4d7e907cSAndroid Build Coastguard Worker  *
8*4d7e907cSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*4d7e907cSAndroid Build Coastguard Worker  *
10*4d7e907cSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*4d7e907cSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*4d7e907cSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*4d7e907cSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*4d7e907cSAndroid Build Coastguard Worker  * limitations under the License.
15*4d7e907cSAndroid Build Coastguard Worker  */
16*4d7e907cSAndroid Build Coastguard Worker #include <aidl/Gtest.h>
17*4d7e907cSAndroid Build Coastguard Worker #include <aidl/Vintf.h>
18*4d7e907cSAndroid Build Coastguard Worker 
19*4d7e907cSAndroid Build Coastguard Worker #include <aidl/android/hardware/authsecret/IAuthSecret.h>
20*4d7e907cSAndroid Build Coastguard Worker #include <android/binder_manager.h>
21*4d7e907cSAndroid Build Coastguard Worker #include <android/binder_process.h>
22*4d7e907cSAndroid Build Coastguard Worker 
23*4d7e907cSAndroid Build Coastguard Worker using ::aidl::android::hardware::authsecret::IAuthSecret;
24*4d7e907cSAndroid Build Coastguard Worker 
25*4d7e907cSAndroid Build Coastguard Worker using ::ndk::SpAIBinder;
26*4d7e907cSAndroid Build Coastguard Worker 
27*4d7e907cSAndroid Build Coastguard Worker /**
28*4d7e907cSAndroid Build Coastguard Worker  * There is no expected behaviour that can be tested so these tests check the
29*4d7e907cSAndroid Build Coastguard Worker  * HAL doesn't crash with different execution orders.
30*4d7e907cSAndroid Build Coastguard Worker  */
31*4d7e907cSAndroid Build Coastguard Worker class AuthSecretAidlTest : public testing::TestWithParam<std::string> {
32*4d7e907cSAndroid Build Coastguard Worker   public:
SetUp()33*4d7e907cSAndroid Build Coastguard Worker     virtual void SetUp() override {
34*4d7e907cSAndroid Build Coastguard Worker         authsecret = IAuthSecret::fromBinder(
35*4d7e907cSAndroid Build Coastguard Worker             SpAIBinder(AServiceManager_waitForService(GetParam().c_str())));
36*4d7e907cSAndroid Build Coastguard Worker         ASSERT_NE(authsecret, nullptr);
37*4d7e907cSAndroid Build Coastguard Worker 
38*4d7e907cSAndroid Build Coastguard Worker         // Notify LSS to generate PIN code '1234' and corresponding secret.
39*4d7e907cSAndroid Build Coastguard Worker         (void)system("cmd lock_settings set-pin 1234");
40*4d7e907cSAndroid Build Coastguard Worker 
41*4d7e907cSAndroid Build Coastguard Worker         // All tests must enroll the correct secret first as this cannot be changed
42*4d7e907cSAndroid Build Coastguard Worker         // without a factory reset and the order of tests could change.
43*4d7e907cSAndroid Build Coastguard Worker         authsecret->setPrimaryUserCredential(CORRECT_SECRET);
44*4d7e907cSAndroid Build Coastguard Worker     }
45*4d7e907cSAndroid Build Coastguard Worker 
TearDownTestSuite()46*4d7e907cSAndroid Build Coastguard Worker     static void TearDownTestSuite() {
47*4d7e907cSAndroid Build Coastguard Worker         // clean up PIN code after testing
48*4d7e907cSAndroid Build Coastguard Worker         (void)system("cmd lock_settings clear --old 1234");
49*4d7e907cSAndroid Build Coastguard Worker     }
50*4d7e907cSAndroid Build Coastguard Worker 
51*4d7e907cSAndroid Build Coastguard Worker     std::shared_ptr<IAuthSecret> authsecret;
52*4d7e907cSAndroid Build Coastguard Worker     std::vector<uint8_t> CORRECT_SECRET{61, 93, 124, 240, 5, 0, 7, 201, 9, 129, 11, 12, 0, 14, 0, 16};
53*4d7e907cSAndroid Build Coastguard Worker     std::vector<uint8_t> WRONG_SECRET{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
54*4d7e907cSAndroid Build Coastguard Worker };
55*4d7e907cSAndroid Build Coastguard Worker 
56*4d7e907cSAndroid Build Coastguard Worker /* Provision the primary user with a secret. */
TEST_P(AuthSecretAidlTest,provisionPrimaryUserCredential)57*4d7e907cSAndroid Build Coastguard Worker TEST_P(AuthSecretAidlTest, provisionPrimaryUserCredential) {
58*4d7e907cSAndroid Build Coastguard Worker     // Secret provisioned by SetUp()
59*4d7e907cSAndroid Build Coastguard Worker }
60*4d7e907cSAndroid Build Coastguard Worker 
61*4d7e907cSAndroid Build Coastguard Worker /* Provision the primary user with a secret and pass the secret again. */
TEST_P(AuthSecretAidlTest,provisionPrimaryUserCredentialAndPassAgain)62*4d7e907cSAndroid Build Coastguard Worker TEST_P(AuthSecretAidlTest, provisionPrimaryUserCredentialAndPassAgain) {
63*4d7e907cSAndroid Build Coastguard Worker     // Secret provisioned by SetUp()
64*4d7e907cSAndroid Build Coastguard Worker     authsecret->setPrimaryUserCredential(CORRECT_SECRET);
65*4d7e907cSAndroid Build Coastguard Worker }
66*4d7e907cSAndroid Build Coastguard Worker 
67*4d7e907cSAndroid Build Coastguard Worker /* Provision the primary user with a secret and pass the secret again repeatedly. */
TEST_P(AuthSecretAidlTest,provisionPrimaryUserCredentialAndPassAgainMultipleTimes)68*4d7e907cSAndroid Build Coastguard Worker TEST_P(AuthSecretAidlTest, provisionPrimaryUserCredentialAndPassAgainMultipleTimes) {
69*4d7e907cSAndroid Build Coastguard Worker     // Secret provisioned by SetUp()
70*4d7e907cSAndroid Build Coastguard Worker     constexpr int N = 5;
71*4d7e907cSAndroid Build Coastguard Worker     for (int i = 0; i < N; ++i) {
72*4d7e907cSAndroid Build Coastguard Worker         authsecret->setPrimaryUserCredential(CORRECT_SECRET);
73*4d7e907cSAndroid Build Coastguard Worker     }
74*4d7e907cSAndroid Build Coastguard Worker }
75*4d7e907cSAndroid Build Coastguard Worker 
76*4d7e907cSAndroid Build Coastguard Worker /* Provision the primary user with a secret and then pass the wrong secret. This
77*4d7e907cSAndroid Build Coastguard Worker  * should never happen and is an framework bug if it does. As the secret is
78*4d7e907cSAndroid Build Coastguard Worker  * wrong, the HAL implementation may not be able to function correctly but it
79*4d7e907cSAndroid Build Coastguard Worker  * should fail gracefully. */
TEST_P(AuthSecretAidlTest,provisionPrimaryUserCredentialAndWrongSecret)80*4d7e907cSAndroid Build Coastguard Worker TEST_P(AuthSecretAidlTest, provisionPrimaryUserCredentialAndWrongSecret) {
81*4d7e907cSAndroid Build Coastguard Worker     // Secret provisioned by SetUp()
82*4d7e907cSAndroid Build Coastguard Worker     authsecret->setPrimaryUserCredential(WRONG_SECRET);
83*4d7e907cSAndroid Build Coastguard Worker }
84*4d7e907cSAndroid Build Coastguard Worker 
85*4d7e907cSAndroid Build Coastguard Worker GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AuthSecretAidlTest);
86*4d7e907cSAndroid Build Coastguard Worker INSTANTIATE_TEST_SUITE_P(
87*4d7e907cSAndroid Build Coastguard Worker         PerInstance, AuthSecretAidlTest,
88*4d7e907cSAndroid Build Coastguard Worker         testing::ValuesIn(android::getAidlHalInstanceNames(IAuthSecret::descriptor)),
89*4d7e907cSAndroid Build Coastguard Worker         android::PrintInstanceNameToString);
90*4d7e907cSAndroid Build Coastguard Worker 
main(int argc,char ** argv)91*4d7e907cSAndroid Build Coastguard Worker int main(int argc, char** argv) {
92*4d7e907cSAndroid Build Coastguard Worker     ::testing::InitGoogleTest(&argc, argv);
93*4d7e907cSAndroid Build Coastguard Worker     ABinderProcess_setThreadPoolMaxThreadCount(1);
94*4d7e907cSAndroid Build Coastguard Worker     ABinderProcess_startThreadPool();
95*4d7e907cSAndroid Build Coastguard Worker     return RUN_ALL_TESTS();
96*4d7e907cSAndroid Build Coastguard Worker }
97