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 
17 #define LOG_TAG "fingerprint_hidl_hal_test"
18 
19 #include <VtsHalHidlTargetCallbackBase.h>
20 #include <android-base/properties.h>
21 #include <android/hardware/biometrics/fingerprint/2.1/IBiometricsFingerprint.h>
22 #include <android/hardware/biometrics/fingerprint/2.2/IBiometricsFingerprintClientCallback.h>
23 #include <gtest/gtest.h>
24 #include <hidl/GtestPrinter.h>
25 #include <hidl/HidlSupport.h>
26 #include <hidl/ServiceManagement.h>
27 
28 #include <cinttypes>
29 
30 namespace {
31 
32 namespace hidl_interface = android::hardware::biometrics::fingerprint::V2_1;
33 namespace hidl_interface_2_2 = android::hardware::biometrics::fingerprint::V2_2;
34 
35 using hidl_interface::FingerprintError;
36 using hidl_interface::IBiometricsFingerprint;
37 using hidl_interface::RequestStatus;
38 
39 using android::sp;
40 using android::base::GetUintProperty;
41 using android::hardware::hidl_vec;
42 using android::hardware::Return;
43 using android::hardware::Void;
44 
45 constexpr uint32_t kTimeoutSec = 3;
46 constexpr auto kTimeout = std::chrono::seconds(kTimeoutSec);
47 constexpr uint32_t kGroupId = 99;
48 constexpr char kCallbackNameOnAcquired[] = "onAcquired";
49 
50 // Callback arguments that need to be captured for the tests.
51 struct FingerprintCallbackArgs {
52     // The info passed to the last onAcquired() callback.
53     hidl_interface_2_2::FingerprintAcquiredInfo info;
54 };
55 
56 // Test callback class for the BiometricsFingerprint HAL.
57 // The HAL will call these callback methods to notify about completed operations
58 // or encountered errors.
59 class FingerprintCallback : public ::testing::VtsHalHidlTargetCallbackBase<FingerprintCallbackArgs>,
60                             public hidl_interface_2_2::IBiometricsFingerprintClientCallback {
61   public:
onEnrollResult(uint64_t,uint32_t,uint32_t,uint32_t)62     Return<void> onEnrollResult(uint64_t, uint32_t, uint32_t, uint32_t) override { return Void(); }
63 
onAcquired(uint64_t,hidl_interface::FingerprintAcquiredInfo,int32_t)64     Return<void> onAcquired(uint64_t, hidl_interface::FingerprintAcquiredInfo, int32_t) override {
65         return Void();
66     }
67 
onAcquired_2_2(uint64_t,hidl_interface_2_2::FingerprintAcquiredInfo info,int32_t)68     Return<void> onAcquired_2_2(uint64_t, hidl_interface_2_2::FingerprintAcquiredInfo info,
69                                 int32_t) override {
70         FingerprintCallbackArgs args = {};
71         args.info = info;
72         NotifyFromCallback(kCallbackNameOnAcquired, args);
73         return Void();
74     }
75 
onAuthenticated(uint64_t,uint32_t,uint32_t,const hidl_vec<uint8_t> &)76     Return<void> onAuthenticated(uint64_t, uint32_t, uint32_t, const hidl_vec<uint8_t>&) override {
77         return Void();
78     }
79 
onError(uint64_t,FingerprintError,int32_t)80     Return<void> onError(uint64_t, FingerprintError, int32_t) override { return Void(); }
81 
onRemoved(uint64_t,uint32_t,uint32_t,uint32_t)82     Return<void> onRemoved(uint64_t, uint32_t, uint32_t, uint32_t) override { return Void(); }
83 
onEnumerate(uint64_t,uint32_t,uint32_t,uint32_t)84     Return<void> onEnumerate(uint64_t, uint32_t, uint32_t, uint32_t) override { return Void(); }
85 };
86 
87 class FingerprintHidlTest : public ::testing::TestWithParam<std::string> {
88   public:
SetUp()89     void SetUp() override {
90         mService = IBiometricsFingerprint::getService(GetParam());
91         ASSERT_NE(mService, nullptr);
92         mCallback = new FingerprintCallback();
93         mCallback->SetWaitTimeoutDefault(kTimeout);
94         Return<uint64_t> ret1 = mService->setNotify(mCallback);
95         ASSERT_NE(0UL, static_cast<uint64_t>(ret1));
96 
97         /*
98          * Devices shipped from now on will instead store
99          * fingerprint data under /data/vendor_de/<user-id>/fpdata.
100          * Support for /data/vendor_de and /data/vendor_ce has been added to vold.
101          */
102 
103         auto api_level = GetUintProperty<uint64_t>("ro.product.first_api_level", 0);
104         if (api_level == 0) {
105             api_level = GetUintProperty<uint64_t>("ro.build.version.sdk", 0);
106         }
107         ASSERT_NE(api_level, 0);
108 
109         // 27 is the API number for O-MR1
110         string tmpDir;
111         if (api_level <= 27) {
112             tmpDir = "/data/system/users/0/fpdata/";
113         } else {
114             tmpDir = "/data/vendor_de/0/fpdata/";
115         }
116 
117         Return<RequestStatus> res = mService->setActiveGroup(kGroupId, tmpDir);
118         ASSERT_EQ(RequestStatus::SYS_OK, static_cast<RequestStatus>(res));
119     }
120 
121     sp<IBiometricsFingerprint> mService;
122     sp<FingerprintCallback> mCallback;
123 };
124 
125 // The START message and onAcquired_2_2 method should exist and work together correctly.
126 // Note, this test doesn't use the HAL. It just makes sure that the newly added constant and
127 // callback compile. Unfortunately, there is no way to test the usage of the constant within the
128 // actual HAL.
TEST_P(FingerprintHidlTest,acquiredInfoStartTest)129 TEST_P(FingerprintHidlTest, acquiredInfoStartTest) {
130     mCallback->SetWaitTimeoutDefault(kTimeout);
131     mCallback->onAcquired_2_2(0 /* deviceId */, hidl_interface_2_2::FingerprintAcquiredInfo::START,
132                               0 /* vendorCode */);
133     auto res = mCallback->WaitForCallback(kCallbackNameOnAcquired);
134     ASSERT_EQ(hidl_interface_2_2::FingerprintAcquiredInfo::START, res.args->info);
135 }
136 
137 }  // anonymous namespace
138 
139 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(FingerprintHidlTest);
140 INSTANTIATE_TEST_SUITE_P(PerInstance, FingerprintHidlTest,
141                          testing::ValuesIn(android::hardware::getAllHalInstanceNames(
142                                  IBiometricsFingerprint::descriptor)),
143                          android::hardware::PrintInstanceNameToString);
144