summaryrefslogtreecommitdiff
path: root/biometrics/face/aidl/vts/VtsHalBiometricsFaceTargetTest.cpp
blob: 08ab5d694b4b406fc9ab4a50f5195fd772e6b023 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <aidl/Gtest.h>
#include <aidl/Vintf.h>
#include <aidl/android/hardware/biometrics/face/BnFace.h>
#include <aidl/android/hardware/biometrics/face/BnSessionCallback.h>

#include <android/binder_manager.h>
#include <android/binder_process.h>

#include <chrono>
#include <future>

namespace aidl::android::hardware::biometrics::face {
namespace {

using namespace std::literals::chrono_literals;

using aidl::android::hardware::common::NativeHandle;

constexpr int kSensorId = 0;
constexpr int kUserId = 0;

class SessionCallback : public BnSessionCallback {
  public:
    ndk::ScopedAStatus onChallengeGenerated(int64_t challenge) override {
        auto lock = std::lock_guard{mMutex};
        mOnChallengeGeneratedInvoked = true;
        mGeneratedChallenge = challenge;
        mCv.notify_one();
        return ndk::ScopedAStatus::ok();
    }

    ndk::ScopedAStatus onChallengeRevoked(int64_t challenge) override {
        auto lock = std::lock_guard{mMutex};
        mOnChallengeRevokedInvoked = true;
        mRevokedChallenge = challenge;
        mCv.notify_one();
        return ndk::ScopedAStatus::ok();
    }

    ndk::ScopedAStatus onAuthenticationFrame(const AuthenticationFrame& /*frame*/) override {
        return ndk::ScopedAStatus::ok();
    }

    ndk::ScopedAStatus onEnrollmentFrame(const EnrollmentFrame& /*frame*/) override {
        return ndk::ScopedAStatus::ok();
    }

    ndk::ScopedAStatus onError(Error error, int32_t vendorCode) override {
        auto lock = std::lock_guard<std::mutex>{mMutex};
        mError = error;
        mVendorCode = vendorCode;
        mOnErrorInvoked = true;
        mCv.notify_one();
        return ndk::ScopedAStatus::ok();
    }

    ndk::ScopedAStatus onEnrollmentProgress(int32_t /*enrollmentId*/,
                                            int32_t /*remaining*/) override {
        return ndk::ScopedAStatus::ok();
    }

    ndk::ScopedAStatus onAuthenticationSucceeded(
            int32_t /*enrollmentId*/, const keymaster::HardwareAuthToken& /*hat*/) override {
        return ndk::ScopedAStatus::ok();
    }

    ndk::ScopedAStatus onAuthenticationFailed() override { return ndk::ScopedAStatus::ok(); }

    ndk::ScopedAStatus onLockoutTimed(int64_t /*durationMillis*/) override {
        return ndk::ScopedAStatus::ok();
    }

    ndk::ScopedAStatus onLockoutPermanent() override { return ndk::ScopedAStatus::ok(); }

    ndk::ScopedAStatus onLockoutCleared() override { return ndk::ScopedAStatus::ok(); }

    ndk::ScopedAStatus onInteractionDetected() override { return ndk::ScopedAStatus::ok(); }

    ndk::ScopedAStatus onEnrollmentsEnumerated(
            const std::vector<int32_t>& /*enrollmentIds*/) override {
        auto lock = std::lock_guard{mMutex};
        mOnEnrollmentsEnumeratedInvoked = true;
        mCv.notify_one();
        return ndk::ScopedAStatus::ok();
    }

    ndk::ScopedAStatus onEnrollmentsRemoved(
            const std::vector<int32_t>& /*enrollmentIds*/) override {
        auto lock = std::lock_guard{mMutex};
        mOnEnrollmentsRemovedInvoked = true;
        mCv.notify_one();
        return ndk::ScopedAStatus::ok();
    }

    ndk::ScopedAStatus onFeaturesRetrieved(const std::vector<Feature>& /*features*/) override {
        auto lock = std::lock_guard{mMutex};
        mOnFeaturesRetrievedInvoked = true;
        mCv.notify_one();
        return ndk::ScopedAStatus::ok();
    }

    ndk::ScopedAStatus onFeatureSet(Feature /*feature*/) override {
        return ndk::ScopedAStatus::ok();
    }

    ndk::ScopedAStatus onAuthenticatorIdRetrieved(int64_t /*authenticatorId*/) override {
        auto lock = std::lock_guard{mMutex};
        mOnAuthenticatorIdRetrievedInvoked = true;
        mCv.notify_one();
        return ndk::ScopedAStatus::ok();
    }

    ndk::ScopedAStatus onAuthenticatorIdInvalidated(int64_t /*newAuthenticatorId*/) override {
        auto lock = std::lock_guard{mMutex};
        mOnAuthenticatorIdInvalidatedInvoked = true;
        mCv.notify_one();
        return ndk::ScopedAStatus::ok();
    }

    ndk::ScopedAStatus onSessionClosed() override {
        auto lock = std::lock_guard<std::mutex>{mMutex};
        mOnSessionClosedInvoked = true;
        mCv.notify_one();
        return ndk::ScopedAStatus::ok();
    }

    std::mutex mMutex;
    std::condition_variable mCv;
    Error mError = Error::UNKNOWN;
    int32_t mVendorCode = 0;
    int64_t mGeneratedChallenge = 0;
    int64_t mRevokedChallenge = 0;
    bool mOnChallengeGeneratedInvoked = false;
    bool mOnChallengeRevokedInvoked = false;
    bool mOnErrorInvoked = false;
    bool mOnEnrollmentsEnumeratedInvoked = false;
    bool mOnEnrollmentsRemovedInvoked = false;
    bool mOnFeaturesRetrievedInvoked = false;
    bool mOnAuthenticatorIdRetrievedInvoked = false;
    bool mOnAuthenticatorIdInvalidatedInvoked = false;
    bool mOnSessionClosedInvoked = false;
};

class Face : public testing::TestWithParam<std::string> {
  protected:
    void SetUp() override {
        // Prepare the callback.
        mCb = ndk::SharedRefBase::make<SessionCallback>();

        int retries = 0;
        bool isOk = false;
        // If the first attempt to create a session fails, we try to create a session again. The
        // first attempt might fail if the framework already has an active session. The AIDL
        // contract doesn't allow to create a new session without closing the old one. However, we
        // can't close the framework's session from VTS. The expectation here is that the HAL will
        // crash after the first illegal attempt to create a session, then it will restart, and then
        // we'll be able to create a session.
        do {
            // Get an instance of the HAL.
            AIBinder* binder = AServiceManager_waitForService(GetParam().c_str());
            ASSERT_NE(binder, nullptr);
            mHal = IFace::fromBinder(ndk::SpAIBinder(binder));

            // Create a session.
            isOk = mHal->createSession(kSensorId, kUserId, mCb, &mSession).isOk();
            ++retries;
        } while (!isOk && retries < 2);

        ASSERT_TRUE(isOk);
    }

    void TearDown() override {
        // Close the mSession.
        ASSERT_TRUE(mSession->close().isOk());

        // Make sure the mSession is closed.
        auto lock = std::unique_lock<std::mutex>(mCb->mMutex);
        mCb->mCv.wait(lock, [this] { return mCb->mOnSessionClosedInvoked; });
    }

    std::shared_ptr<IFace> mHal;
    std::shared_ptr<SessionCallback> mCb;
    std::shared_ptr<ISession> mSession;
};

TEST_P(Face, GetSensorPropsWorksTest) {
    std::vector<SensorProps> sensorProps;

    // Call the method.
    ASSERT_TRUE(mHal->getSensorProps(&sensorProps).isOk());

    // Make sure the sensorProps aren't empty.
    ASSERT_FALSE(sensorProps.empty());
    ASSERT_FALSE(sensorProps[0].commonProps.componentInfo.empty());
}

TEST_P(Face, EnrollWithBadHatResultsInErrorTest) {
    // Call the method.
    auto hat = keymaster::HardwareAuthToken{};
    std::shared_ptr<common::ICancellationSignal> cancellationSignal;
    ASSERT_TRUE(
            mSession->enroll(hat, EnrollmentType::DEFAULT, {}, std::nullopt, &cancellationSignal)
                    .isOk());

    // Make sure an error is returned.
    auto lock = std::unique_lock{mCb->mMutex};
    mCb->mCv.wait(lock, [this] { return mCb->mOnErrorInvoked; });
    EXPECT_EQ(mCb->mError, Error::UNABLE_TO_PROCESS);
    EXPECT_EQ(mCb->mVendorCode, 0);
}

TEST_P(Face, GenerateChallengeProducesUniqueChallengesTest) {
    static constexpr int kIterations = 100;

    auto challenges = std::set<int>{};
    for (unsigned int i = 0; i < kIterations; ++i) {
        // Call the method.
        ASSERT_TRUE(mSession->generateChallenge().isOk());

        // Check that the generated challenge is unique and not 0.
        auto lock = std::unique_lock{mCb->mMutex};
        mCb->mCv.wait(lock, [this] { return mCb->mOnChallengeGeneratedInvoked; });
        ASSERT_NE(mCb->mGeneratedChallenge, 0);
        ASSERT_EQ(challenges.find(mCb->mGeneratedChallenge), challenges.end());

        challenges.insert(mCb->mGeneratedChallenge);
        mCb->mOnChallengeGeneratedInvoked = false;
    }
}

TEST_P(Face, RevokeChallengeWorksForNonexistentChallengeTest) {
    const int64_t nonexistentChallenge = 123;

    // Call the method.
    ASSERT_TRUE(mSession->revokeChallenge(nonexistentChallenge).isOk());

    // Check that the challenge is revoked and matches the requested challenge.
    auto lock = std::unique_lock{mCb->mMutex};
    mCb->mCv.wait(lock, [this] { return mCb->mOnChallengeRevokedInvoked; });
    ASSERT_EQ(mCb->mRevokedChallenge, nonexistentChallenge);
}

TEST_P(Face, RevokeChallengeWorksForExistentChallengeTest) {
    // Generate a challenge.
    ASSERT_TRUE(mSession->generateChallenge().isOk());

    // Wait for the result.
    auto lock = std::unique_lock{mCb->mMutex};
    mCb->mCv.wait(lock, [this] { return mCb->mOnChallengeGeneratedInvoked; });
    lock.unlock();

    // Revoke the challenge.
    ASSERT_TRUE(mSession->revokeChallenge(mCb->mGeneratedChallenge).isOk());

    // Check that the challenge is revoked and matches the requested challenge.
    lock.lock();
    mCb->mCv.wait(lock, [this] { return mCb->mOnChallengeRevokedInvoked; });
    ASSERT_EQ(mCb->mRevokedChallenge, mCb->mGeneratedChallenge);
}

TEST_P(Face, EnumerateEnrollmentsWorksTest) {
    // Call the method.
    ASSERT_TRUE(mSession->enumerateEnrollments().isOk());

    // Wait for the result.
    auto lock = std::unique_lock{mCb->mMutex};
    mCb->mCv.wait(lock, [this] { return mCb->mOnEnrollmentsEnumeratedInvoked; });
}

TEST_P(Face, RemoveEnrollmentsWorksTest) {
    // Call the method.
    ASSERT_TRUE(mSession->removeEnrollments({}).isOk());

    // Wait for the result.
    auto lock = std::unique_lock{mCb->mMutex};
    mCb->mCv.wait(lock, [this] { return mCb->mOnEnrollmentsRemovedInvoked; });
}

TEST_P(Face, GetFeaturesWithoutEnrollmentsResultsInUnableToProcess) {
    // Call the method.
    ASSERT_TRUE(mSession->getFeatures().isOk());

    // Wait for the result.
    auto lock = std::unique_lock{mCb->mMutex};
    mCb->mCv.wait(lock, [this] { return mCb->mOnErrorInvoked; });
    EXPECT_EQ(mCb->mError, Error::UNABLE_TO_PROCESS);
    EXPECT_EQ(mCb->mVendorCode, 0);
}

TEST_P(Face, GetAuthenticatorIdWorksTest) {
    // Call the method.
    ASSERT_TRUE(mSession->getAuthenticatorId().isOk());

    // Wait for the result.
    auto lock = std::unique_lock{mCb->mMutex};
    mCb->mCv.wait(lock, [this] { return mCb->mOnAuthenticatorIdRetrievedInvoked; });
}

TEST_P(Face, InvalidateAuthenticatorIdWorksTest) {
    // Call the method.
    ASSERT_TRUE(mSession->invalidateAuthenticatorId().isOk());

    // Wait for the result.
    auto lock = std::unique_lock{mCb->mMutex};
    mCb->mCv.wait(lock, [this] { return mCb->mOnAuthenticatorIdInvalidatedInvoked; });
}

GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(Face);
INSTANTIATE_TEST_SUITE_P(IFace, Face,
                         testing::ValuesIn(::android::getAidlHalInstanceNames(IFace::descriptor)),
                         ::android::PrintInstanceNameToString);

}  // namespace
}  // namespace aidl::android::hardware::biometrics::face

int main(int argc, char** argv) {
    ::testing::InitGoogleTest(&argc, argv);
    ABinderProcess_setThreadPoolMaxThreadCount(1);
    ABinderProcess_startThreadPool();
    return RUN_ALL_TESTS();
}