1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "gtest/gtest.h"
17 
18 #include "hdf_base.h"
19 #include "iam_logger.h"
20 #include "iam_ptr.h"
21 
22 #include "fingerprint_auth_all_in_one_executor_hdi.h"
23 #include "fingerprint_auth_defines.h"
24 #include "mock_iall_in_one_executor.h"
25 #include "mock_iexecute_callback.h"
26 
27 #define LOG_TAG "FINGERPRINT_AUTH_SA"
28 
29 using namespace testing;
30 using namespace testing::ext;
31 using namespace OHOS::UserIam::Common;
32 
33 namespace OHOS {
34 namespace UserIam {
35 namespace FingerprintAuth {
36 using IamResultCode = OHOS::UserIam::UserAuth::ResultCode;
37 using IamExecutorRole = OHOS::UserIam::UserAuth::ExecutorRole;
38 using IamExecutorInfo = OHOS::UserIam::UserAuth::ExecutorInfo;
39 using IamAuthType = OHOS::UserIam::UserAuth::AuthType;
40 using IamExecutorSecureLevel = OHOS::UserIam::UserAuth::ExecutorSecureLevel;
41 using IamPropertyMode = OHOS::UserIam::UserAuth::PropertyMode;
42 namespace {
43 static const std::map<HDF_STATUS, IamResultCode> RESULT_CODE_MAP = {
44     { HDF_SUCCESS, IamResultCode::SUCCESS },
45     { HDF_FAILURE, IamResultCode::GENERAL_ERROR },
46     { HDF_ERR_TIMEOUT, IamResultCode::TIMEOUT },
47     { HDF_ERR_QUEUE_FULL, IamResultCode::BUSY },
48     { HDF_ERR_DEVICE_BUSY, IamResultCode::BUSY },
49     { static_cast<HDF_STATUS>(HDF_ERR_DEVICE_BUSY - 1), IamResultCode::GENERAL_ERROR },
50     { static_cast<HDF_STATUS>(HDF_SUCCESS + 1), IamResultCode::GENERAL_ERROR },
51 };
52 }
53 
54 class FingerprintAllInOneExecutorHdiUnitTest : public testing::Test {
55 public:
56     static void SetUpTestCase();
57     static void TearDownTestCase();
58     void SetUp();
59     void TearDown();
60 };
61 
SetUpTestCase()62 void FingerprintAllInOneExecutorHdiUnitTest::SetUpTestCase()
63 {
64 }
65 
TearDownTestCase()66 void FingerprintAllInOneExecutorHdiUnitTest::TearDownTestCase()
67 {
68 }
69 
SetUp()70 void FingerprintAllInOneExecutorHdiUnitTest::SetUp()
71 {
72 }
73 
TearDown()74 void FingerprintAllInOneExecutorHdiUnitTest::TearDown()
75 {
76 }
77 
78 HWTEST_F(FingerprintAllInOneExecutorHdiUnitTest, FingerprintAllInOneExecutorHdi_GetExecutorInfo_001, TestSize.Level0)
79 {
80     auto executorProxy = new (std::nothrow) MockIAllInOneExecutor();
81     ASSERT_TRUE(executorProxy != nullptr);
__anon1c0fbb0f0202(ExecutorInfo &info) 82     EXPECT_CALL(*executorProxy, GetExecutorInfo(_)).Times(Exactly(1)).WillOnce([](ExecutorInfo &info) {
83         info = {
84             .executorRole = ExecutorRole::ALL_IN_ONE,
85             .authType = AuthType::FINGERPRINT,
86             .esl = ExecutorSecureLevel::ESL0,
87         };
88         return HDF_SUCCESS;
89     });
90     auto executorHdi = MakeShared<FingerprintAllInOneExecutorHdi>(executorProxy);
91     IamExecutorInfo info = {};
92     auto ret = executorHdi->GetExecutorInfo(info);
93     EXPECT_TRUE(info.executorRole == IamExecutorRole::ALL_IN_ONE);
94     EXPECT_TRUE(info.authType == IamAuthType::FINGERPRINT);
95     EXPECT_TRUE(info.esl == IamExecutorSecureLevel::ESL0);
96     EXPECT_TRUE(ret == IamResultCode::SUCCESS);
97 }
98 
99 HWTEST_F(FingerprintAllInOneExecutorHdiUnitTest, FingerprintAllInOneExecutorHdi_GetExecutorInfo_002, TestSize.Level0)
100 {
101     for (const auto &pair : RESULT_CODE_MAP) {
102         auto executorProxy = new (std::nothrow) MockIAllInOneExecutor();
103         ASSERT_TRUE(executorProxy != nullptr);
__anon1c0fbb0f0302(ExecutorInfo &info) 104         EXPECT_CALL(*executorProxy, GetExecutorInfo(_)).Times(Exactly(1)).WillOnce([&pair](ExecutorInfo &info) {
105             info = {
106                 .executorRole = ExecutorRole::ALL_IN_ONE,
107                 .authType = AuthType::FINGERPRINT,
108                 .esl = ExecutorSecureLevel::ESL0,
109             };
110             return static_cast<int32_t>(pair.first);
111         });
112         auto executorHdi = MakeShared<FingerprintAllInOneExecutorHdi>(executorProxy);
113         IamExecutorInfo info = {};
114         auto ret = executorHdi->GetExecutorInfo(info);
115         EXPECT_TRUE(ret == pair.second);
116     }
117 }
118 
119 HWTEST_F(FingerprintAllInOneExecutorHdiUnitTest, FingerprintAllInOneExecutorHdi_GetExecutorInfo_003, TestSize.Level0)
120 {
121     static const std::map<AuthType, pair<IamAuthType, IamResultCode>> data = {
122         { AuthType::FINGERPRINT, { IamAuthType::FINGERPRINT, IamResultCode::SUCCESS } },
123         { static_cast<AuthType>(AuthType::FINGERPRINT + 1),
124             { IamAuthType::FINGERPRINT, IamResultCode::GENERAL_ERROR } },
125         { static_cast<AuthType>(AuthType::FINGERPRINT - 1),
126             { IamAuthType::FINGERPRINT, IamResultCode::GENERAL_ERROR } },
127     };
128     for (const auto &pair : data) {
129         auto executorProxy = new (std::nothrow) MockIAllInOneExecutor();
130         ASSERT_TRUE(executorProxy != nullptr);
__anon1c0fbb0f0402(ExecutorInfo &info) 131         EXPECT_CALL(*executorProxy, GetExecutorInfo(_)).Times(Exactly(1)).WillOnce([&pair](ExecutorInfo &info) {
132             info = {
133                 .executorRole = ExecutorRole::ALL_IN_ONE,
134                 .authType = pair.first,
135                 .esl = ExecutorSecureLevel::ESL0,
136             };
137             return HDF_SUCCESS;
138         });
139         auto executorHdi = MakeShared<FingerprintAllInOneExecutorHdi>(executorProxy);
140         IamExecutorInfo info = {};
141         auto ret = executorHdi->GetExecutorInfo(info);
142         EXPECT_TRUE(ret == pair.second.second);
143         if (ret == IamResultCode::SUCCESS) {
144             EXPECT_TRUE(info.authType == pair.second.first);
145         }
146     }
147 }
148 
149 HWTEST_F(FingerprintAllInOneExecutorHdiUnitTest, FingerprintAllInOneExecutorHdi_GetExecutorInfo_004, TestSize.Level0)
150 {
151     static const std::map<ExecutorRole, pair<IamExecutorRole, IamResultCode>> data = {
152         { ExecutorRole::COLLECTOR, { IamExecutorRole::COLLECTOR, IamResultCode::SUCCESS } },
153         { ExecutorRole::VERIFIER, { IamExecutorRole::VERIFIER, IamResultCode::SUCCESS } },
154         { ExecutorRole::ALL_IN_ONE, { IamExecutorRole::ALL_IN_ONE, IamResultCode::SUCCESS } },
155         { static_cast<ExecutorRole>(ExecutorRole::COLLECTOR - 1),
156             { IamExecutorRole::ALL_IN_ONE, IamResultCode::GENERAL_ERROR } },
157         { static_cast<ExecutorRole>(ExecutorRole::ALL_IN_ONE + 1),
158             { IamExecutorRole::ALL_IN_ONE, IamResultCode::GENERAL_ERROR } },
159     };
160     for (const auto &pair : data) {
161         auto executorProxy = new (std::nothrow) MockIAllInOneExecutor();
162         ASSERT_TRUE(executorProxy != nullptr);
__anon1c0fbb0f0502(ExecutorInfo &info) 163         EXPECT_CALL(*executorProxy, GetExecutorInfo(_)).Times(Exactly(1)).WillOnce([&pair](ExecutorInfo &info) {
164             info = {
165                 .executorRole = pair.first,
166                 .authType = AuthType::FINGERPRINT,
167                 .esl = ExecutorSecureLevel::ESL0,
168             };
169             return HDF_SUCCESS;
170         });
171         auto executorHdi = MakeShared<FingerprintAllInOneExecutorHdi>(executorProxy);
172         IamExecutorInfo info = {};
173         auto ret = executorHdi->GetExecutorInfo(info);
174         EXPECT_TRUE(ret == pair.second.second);
175         if (ret == IamResultCode::SUCCESS) {
176             EXPECT_TRUE(info.executorRole == pair.second.first);
177         }
178     }
179 }
180 
181 HWTEST_F(FingerprintAllInOneExecutorHdiUnitTest, FingerprintAllInOneExecutorHdi_GetExecutorInfo_005, TestSize.Level0)
182 {
183     static const std::map<ExecutorSecureLevel, pair<IamExecutorSecureLevel, IamResultCode>> data = {
184         { ExecutorSecureLevel::ESL0, { IamExecutorSecureLevel::ESL0, IamResultCode::SUCCESS } },
185         { ExecutorSecureLevel::ESL1, { IamExecutorSecureLevel::ESL1, IamResultCode::SUCCESS } },
186         { ExecutorSecureLevel::ESL2, { IamExecutorSecureLevel::ESL2, IamResultCode::SUCCESS } },
187         { ExecutorSecureLevel::ESL3, { IamExecutorSecureLevel::ESL3, IamResultCode::SUCCESS } },
188         { static_cast<ExecutorSecureLevel>(ExecutorSecureLevel::ESL0 - 1),
189             { IamExecutorSecureLevel::ESL3, IamResultCode::GENERAL_ERROR } },
190         { static_cast<ExecutorSecureLevel>(ExecutorSecureLevel::ESL3 + 1),
191             { IamExecutorSecureLevel::ESL3, IamResultCode::GENERAL_ERROR } },
192     };
193     for (const auto &pair : data) {
194         auto executorProxy = new (std::nothrow) MockIAllInOneExecutor();
195         ASSERT_TRUE(executorProxy != nullptr);
__anon1c0fbb0f0602(ExecutorInfo &info) 196         EXPECT_CALL(*executorProxy, GetExecutorInfo(_)).Times(Exactly(1)).WillOnce([&pair](ExecutorInfo &info) {
197             info = {
198                 .executorRole = ExecutorRole::ALL_IN_ONE,
199                 .authType = AuthType::FINGERPRINT,
200                 .esl = pair.first,
201             };
202             return HDF_SUCCESS;
203         });
204         auto executorHdi = MakeShared<FingerprintAllInOneExecutorHdi>(executorProxy);
205         IamExecutorInfo info = {};
206         auto ret = executorHdi->GetExecutorInfo(info);
207         EXPECT_TRUE(ret == pair.second.second);
208         if (ret == IamResultCode::SUCCESS) {
209             EXPECT_TRUE(info.esl == pair.second.first);
210         }
211     }
212 }
213 
214 HWTEST_F(FingerprintAllInOneExecutorHdiUnitTest, FingerprintAllInOneExecutorHdi_GetExecutorInfo_006, TestSize.Level0)
215 {
216     auto executorHdi = MakeShared<FingerprintAllInOneExecutorHdi>(nullptr);
217     IamExecutorInfo info = {};
218     auto ret = executorHdi->GetExecutorInfo(info);
219     EXPECT_TRUE(ret == IamResultCode::GENERAL_ERROR);
220 }
221 
222 HWTEST_F(FingerprintAllInOneExecutorHdiUnitTest, FingerprintAllInOneExecutorHdi_OnRegisterFinish_001, TestSize.Level0)
223 {
224     for (const auto &pair : RESULT_CODE_MAP) {
225         sptr<MockIAllInOneExecutor> executorProxy(new (std::nothrow) MockIAllInOneExecutor());
226         ASSERT_TRUE(executorProxy != nullptr);
227         EXPECT_CALL(*executorProxy, RegisterSaCommandCallback(_))
__anon1c0fbb0f0702(const sptr<ISaCommandCallback> &callbackObj) 228             .WillRepeatedly([](const sptr<ISaCommandCallback> &callbackObj) { return HDF_SUCCESS; });
229 
230         EXPECT_CALL(*executorProxy, OnRegisterFinish(_, _, _))
231             .Times(Exactly(1))
232             .WillOnce(
233                 [&pair](const std::vector<uint64_t> &templateIdList, const std::vector<uint8_t> &frameworkPublicKey,
__anon1c0fbb0f0802(const std::vector<uint64_t> &templateIdList, const std::vector<uint8_t> &frameworkPublicKey, const std::vector<uint8_t> &extraInfo) 234                     const std::vector<uint8_t> &extraInfo) { return pair.first; });
235         if (pair.first == HDF_SUCCESS) {
236             EXPECT_CALL(*executorProxy, RegisterSaCommandCallback(_))
237                 .Times(Exactly(1))
__anon1c0fbb0f0902(const sptr<ISaCommandCallback> &callbackObj) 238                 .WillOnce([](const sptr<ISaCommandCallback> &callbackObj) { return HDF_SUCCESS; });
239         }
240         auto executorHdi = MakeShared<FingerprintAllInOneExecutorHdi>(executorProxy);
241         auto ret =
242             executorHdi->OnRegisterFinish(std::vector<uint64_t>(), std::vector<uint8_t>(), std::vector<uint8_t>());
243         EXPECT_TRUE(ret == pair.second);
244     }
245 }
246 
247 HWTEST_F(FingerprintAllInOneExecutorHdiUnitTest, FingerprintAllInOneExecutorHdi_OnRegisterFinish_002, TestSize.Level0)
248 {
249     auto executorHdi = MakeShared<FingerprintAllInOneExecutorHdi>(nullptr);
250     auto ret = executorHdi->OnRegisterFinish(std::vector<uint64_t>(), std::vector<uint8_t>(), std::vector<uint8_t>());
251     EXPECT_TRUE(ret == IamResultCode::GENERAL_ERROR);
252 }
253 
254 HWTEST_F(FingerprintAllInOneExecutorHdiUnitTest, FingerprintAllInOneExecutorHdi_OnRegisterFinish_003, TestSize.Level0)
255 {
256     for (const auto &pair : RESULT_CODE_MAP) {
257         sptr<MockIAllInOneExecutor> executorProxy(new (std::nothrow) MockIAllInOneExecutor());
258         ASSERT_TRUE(executorProxy != nullptr);
259         EXPECT_CALL(*executorProxy, OnRegisterFinish(_, _, _))
260             .Times(Exactly(1))
261             .WillOnce([](const std::vector<uint64_t> &templateIdList, const std::vector<uint8_t> &frameworkPublicKey,
__anon1c0fbb0f0a02(const std::vector<uint64_t> &templateIdList, const std::vector<uint8_t> &frameworkPublicKey, const std::vector<uint8_t> &extraInfo) 262                           const std::vector<uint8_t> &extraInfo) { return HDF_SUCCESS; });
263         EXPECT_CALL(*executorProxy, RegisterSaCommandCallback(_))
264             .Times(Exactly(1))
__anon1c0fbb0f0b02(const sptr<ISaCommandCallback> &callbackObj) 265             .WillOnce([&pair](const sptr<ISaCommandCallback> &callbackObj) { return pair.first; });
266         auto executorHdi = MakeShared<FingerprintAllInOneExecutorHdi>(executorProxy);
267         auto ret =
268             executorHdi->OnRegisterFinish(std::vector<uint64_t>(), std::vector<uint8_t>(), std::vector<uint8_t>());
269         EXPECT_TRUE(ret == pair.second);
270     }
271 }
272 
273 HWTEST_F(FingerprintAllInOneExecutorHdiUnitTest, FingerprintAllInOneExecutorHdi_Enroll_001, TestSize.Level0)
274 {
275     for (const auto &pair : RESULT_CODE_MAP) {
276         sptr<MockIAllInOneExecutor> executorProxy(new (std::nothrow) MockIAllInOneExecutor());
277         ASSERT_TRUE(executorProxy != nullptr);
278         EXPECT_CALL(*executorProxy, Enroll(_, _, _))
279             .Times(Exactly(1))
280             .WillOnce([&pair](uint64_t scheduleId, const std::vector<uint8_t> &extraInfo,
__anon1c0fbb0f0c02(uint64_t scheduleId, const std::vector<uint8_t> &extraInfo, const sptr<IExecutorCallback> &callbackObj) 281                           const sptr<IExecutorCallback> &callbackObj) { return pair.first; });
282         auto executorHdi = MakeShared<FingerprintAllInOneExecutorHdi>(executorProxy);
283         auto executeCallback = MakeShared<UserIam::UserAuth::MockIExecuteCallback>();
284         ASSERT_TRUE(executeCallback != nullptr);
285         auto ret = executorHdi->Enroll(0, UserAuth::EnrollParam { 0, std::vector<uint8_t>() }, executeCallback);
286         EXPECT_TRUE(ret == pair.second);
287     }
288 }
289 
290 HWTEST_F(FingerprintAllInOneExecutorHdiUnitTest, FingerprintAllInOneExecutorHdi_Enroll_002, TestSize.Level0)
291 {
292     sptr<MockIAllInOneExecutor> executorProxy(new (std::nothrow) MockIAllInOneExecutor());
293     ASSERT_TRUE(executorProxy != nullptr);
294     EXPECT_CALL(*executorProxy, Enroll(_, _, _)).Times(Exactly(0));
295     auto executorHdi = MakeShared<FingerprintAllInOneExecutorHdi>(executorProxy);
296     auto ret = executorHdi->Enroll(0, UserAuth::EnrollParam { 0, std::vector<uint8_t>() }, nullptr);
297     EXPECT_TRUE(ret == IamResultCode::GENERAL_ERROR);
298 }
299 
300 HWTEST_F(FingerprintAllInOneExecutorHdiUnitTest, FingerprintAllInOneExecutorHdi_Enroll_003, TestSize.Level0)
301 {
302     auto executorHdi = MakeShared<FingerprintAllInOneExecutorHdi>(nullptr);
303     auto executeCallback = MakeShared<UserIam::UserAuth::MockIExecuteCallback>();
304     ASSERT_TRUE(executeCallback != nullptr);
305     auto ret = executorHdi->Enroll(0, UserAuth::EnrollParam { 0, std::vector<uint8_t>() }, executeCallback);
306     EXPECT_TRUE(ret == IamResultCode::GENERAL_ERROR);
307 }
308 
309 HWTEST_F(FingerprintAllInOneExecutorHdiUnitTest, FingerprintAllInOneExecutorHdi_Authenticate_001, TestSize.Level0)
310 {
311     for (const auto &pair : RESULT_CODE_MAP) {
312         sptr<MockIAllInOneExecutor> executorProxy(new (std::nothrow) MockIAllInOneExecutor());
313         ASSERT_TRUE(executorProxy != nullptr);
314         EXPECT_CALL(*executorProxy, Authenticate(_, _, _, _, _))
315             .Times(Exactly(1))
316             .WillOnce([&pair](uint64_t scheduleId, const std::vector<uint64_t> &templateIdList, bool endAfterFirstFail,
317                           const std::vector<uint8_t> &extraInfo,
__anon1c0fbb0f0d02(uint64_t scheduleId, const std::vector<uint64_t> &templateIdList, bool endAfterFirstFail, const std::vector<uint8_t> &extraInfo, const sptr<IExecutorCallback> &callbackObj) 318                           const sptr<IExecutorCallback> &callbackObj) { return pair.first; });
319         auto executorHdi = MakeShared<FingerprintAllInOneExecutorHdi>(executorProxy);
320         auto executeCallback = MakeShared<UserIam::UserAuth::MockIExecuteCallback>();
321         ASSERT_TRUE(executeCallback != nullptr);
322         auto ret = executorHdi->Authenticate(0,
323             UserAuth::AuthenticateParam { 0, std::vector<uint64_t>(), std::vector<uint8_t>(), false }, executeCallback);
324         EXPECT_TRUE(ret == pair.second);
325     }
326 }
327 
328 HWTEST_F(FingerprintAllInOneExecutorHdiUnitTest, FingerprintAllInOneExecutorHdi_Authenticate_002, TestSize.Level0)
329 {
330     auto executorProxy = new (std::nothrow) MockIAllInOneExecutor();
331     ASSERT_TRUE(executorProxy != nullptr);
332     EXPECT_CALL(*executorProxy, Authenticate(_, _, _, _, _)).Times(Exactly(0));
333     auto executorHdi = MakeShared<FingerprintAllInOneExecutorHdi>(executorProxy);
334     auto ret = executorHdi->Authenticate(0,
335         UserAuth::AuthenticateParam { 0, std::vector<uint64_t>(), std::vector<uint8_t>(), false }, nullptr);
336     EXPECT_TRUE(ret == IamResultCode::GENERAL_ERROR);
337 }
338 
339 HWTEST_F(FingerprintAllInOneExecutorHdiUnitTest, FingerprintAllInOneExecutorHdi_Authenticate_003, TestSize.Level0)
340 {
341     auto executorHdi = MakeShared<FingerprintAllInOneExecutorHdi>(nullptr);
342     auto executeCallback = MakeShared<UserIam::UserAuth::MockIExecuteCallback>();
343     ASSERT_TRUE(executeCallback != nullptr);
344     auto ret = executorHdi->Authenticate(0,
345         UserAuth::AuthenticateParam { 0, std::vector<uint64_t>(), std::vector<uint8_t>(), false }, executeCallback);
346     EXPECT_TRUE(ret == IamResultCode::GENERAL_ERROR);
347 }
348 HWTEST_F(FingerprintAllInOneExecutorHdiUnitTest, FingerprintAllInOneExecutorHdi_Identify_001, TestSize.Level0)
349 {
350     for (const auto &pair : RESULT_CODE_MAP) {
351         sptr<MockIAllInOneExecutor> executorProxy(new (std::nothrow) MockIAllInOneExecutor());
352         ASSERT_TRUE(executorProxy != nullptr);
353         EXPECT_CALL(*executorProxy, Identify(_, _, _))
354             .Times(Exactly(1))
355             .WillOnce([&pair](uint64_t scheduleId, const std::vector<uint8_t> &extraInfo,
__anon1c0fbb0f0e02(uint64_t scheduleId, const std::vector<uint8_t> &extraInfo, const sptr<IExecutorCallback> &callbackObj) 356                           const sptr<IExecutorCallback> &callbackObj) { return pair.first; });
357         auto executorHdi = MakeShared<FingerprintAllInOneExecutorHdi>(executorProxy);
358         auto executeCallback = MakeShared<UserIam::UserAuth::MockIExecuteCallback>();
359         ASSERT_TRUE(executeCallback != nullptr);
360         auto ret = executorHdi->Identify(0, UserAuth::IdentifyParam { 0, std::vector<uint8_t>() }, executeCallback);
361         EXPECT_TRUE(ret == pair.second);
362     }
363 }
364 
365 HWTEST_F(FingerprintAllInOneExecutorHdiUnitTest, FingerprintAllInOneExecutorHdi_Identify_002, TestSize.Level0)
366 {
367     auto executorProxy = new (std::nothrow) MockIAllInOneExecutor();
368     ASSERT_TRUE(executorProxy != nullptr);
369     EXPECT_CALL(*executorProxy, Identify(_, _, _)).Times(Exactly(0));
370     auto executorHdi = MakeShared<FingerprintAllInOneExecutorHdi>(executorProxy);
371     auto ret = executorHdi->Identify(0, UserAuth::IdentifyParam { 0, std::vector<uint8_t>() }, nullptr);
372     EXPECT_TRUE(ret == IamResultCode::GENERAL_ERROR);
373 }
374 
375 HWTEST_F(FingerprintAllInOneExecutorHdiUnitTest, FingerprintAllInOneExecutorHdi_Identify_003, TestSize.Level0)
376 {
377     auto executorHdi = MakeShared<FingerprintAllInOneExecutorHdi>(nullptr);
378     auto executeCallback = MakeShared<UserIam::UserAuth::MockIExecuteCallback>();
379     ASSERT_TRUE(executeCallback != nullptr);
380     auto ret = executorHdi->Identify(0, UserAuth::IdentifyParam { 0, std::vector<uint8_t>() }, executeCallback);
381     EXPECT_TRUE(ret == IamResultCode::GENERAL_ERROR);
382 }
383 
384 HWTEST_F(FingerprintAllInOneExecutorHdiUnitTest, FingerprintAllInOneExecutorHdi_Delete_001, TestSize.Level0)
385 {
386     for (const auto &pair : RESULT_CODE_MAP) {
387         auto executorProxy = new (std::nothrow) MockIAllInOneExecutor();
388         ASSERT_TRUE(executorProxy != nullptr);
389         EXPECT_CALL(*executorProxy, Delete(_))
390             .Times(Exactly(1))
__anon1c0fbb0f0f02(const std::vector<uint64_t> &templateIdList) 391             .WillOnce([&pair](const std::vector<uint64_t> &templateIdList) { return pair.first; });
392         auto executorHdi = MakeShared<FingerprintAllInOneExecutorHdi>(executorProxy);
393         auto ret = executorHdi->Delete(std::vector<uint64_t>());
394         EXPECT_TRUE(ret == pair.second);
395     }
396 }
397 
398 HWTEST_F(FingerprintAllInOneExecutorHdiUnitTest, FingerprintAllInOneExecutorHdi_Delete_002, TestSize.Level0)
399 {
400     auto executorHdi = MakeShared<FingerprintAllInOneExecutorHdi>(nullptr);
401     auto ret = executorHdi->Delete(std::vector<uint64_t>());
402     EXPECT_TRUE(ret == IamResultCode::GENERAL_ERROR);
403 }
404 
405 HWTEST_F(FingerprintAllInOneExecutorHdiUnitTest, FingerprintAllInOneExecutorHdi_Cancel_001, TestSize.Level0)
406 {
407     for (const auto &pair : RESULT_CODE_MAP) {
408         auto executorProxy = new (std::nothrow) MockIAllInOneExecutor();
409         ASSERT_TRUE(executorProxy != nullptr);
__anon1c0fbb0f1002(uint64_t scheduleId) 410         EXPECT_CALL(*executorProxy, Cancel(_)).Times(Exactly(1)).WillOnce([&pair](uint64_t scheduleId) {
411             return pair.first;
412         });
413         auto executorHdi = MakeShared<FingerprintAllInOneExecutorHdi>(executorProxy);
414         auto ret = executorHdi->Cancel(0);
415         EXPECT_TRUE(ret == pair.second);
416     }
417 }
418 
419 HWTEST_F(FingerprintAllInOneExecutorHdiUnitTest, FingerprintAllInOneExecutorHdi_Cancel_002, TestSize.Level0)
420 {
421     auto executorHdi = MakeShared<FingerprintAllInOneExecutorHdi>(nullptr);
422     auto ret = executorHdi->Cancel(0);
423     EXPECT_TRUE(ret == IamResultCode::GENERAL_ERROR);
424 }
425 
426 HWTEST_F(FingerprintAllInOneExecutorHdiUnitTest, FingerprintAllInOneExecutorHdi_SendCommand_001, TestSize.Level0)
427 {
428     auto executorHdi = MakeShared<FingerprintAllInOneExecutorHdi>(nullptr);
429     auto executeCallback = MakeShared<UserIam::UserAuth::MockIExecuteCallback>();
430     ASSERT_TRUE(executeCallback != nullptr);
431     auto ret = executorHdi->SendCommand(IamPropertyMode::PROPERTY_MODE_FREEZE, std::vector<uint8_t>(), executeCallback);
432     EXPECT_TRUE(ret == IamResultCode::GENERAL_ERROR);
433 }
434 
435 HWTEST_F(FingerprintAllInOneExecutorHdiUnitTest, FingerprintAllInOneExecutorHdi_SendCommand_002, TestSize.Level0)
436 {
437     auto executorProxy = new (std::nothrow) MockIAllInOneExecutor();
438     ASSERT_TRUE(executorProxy != nullptr);
439     EXPECT_CALL(*executorProxy, SendCommand(_, _, _)).Times(Exactly(0));
440     auto executorHdi = MakeShared<FingerprintAllInOneExecutorHdi>(executorProxy);
441 
442     auto ret = executorHdi->SendCommand(IamPropertyMode::PROPERTY_MODE_FREEZE, std::vector<uint8_t>(), nullptr);
443     EXPECT_TRUE(ret == IamResultCode::GENERAL_ERROR);
444 }
445 
446 HWTEST_F(FingerprintAllInOneExecutorHdiUnitTest, FingerprintAllInOneExecutorHdi_SendCommand_003, TestSize.Level0)
447 {
448     static const std::map<IamPropertyMode, pair<DriverCommandId, IamResultCode>> data = {
449         { IamPropertyMode::PROPERTY_INIT_ALGORITHM, { DriverCommandId::INIT_ALGORITHM, IamResultCode::SUCCESS } },
450         { IamPropertyMode::PROPERTY_MODE_FREEZE, { DriverCommandId::LOCK_TEMPLATE, IamResultCode::SUCCESS } },
451         { IamPropertyMode::PROPERTY_MODE_UNFREEZE, { DriverCommandId::UNLOCK_TEMPLATE, IamResultCode::SUCCESS } },
452         { static_cast<IamPropertyMode>(IamPropertyMode::PROPERTY_MODE_FREEZE - 1),
453             { DriverCommandId::UNLOCK_TEMPLATE, IamResultCode::INVALID_PARAMETERS } },
454         { static_cast<IamPropertyMode>(IamPropertyMode::PROPERTY_MODE_UNFREEZE + 1),
455             { DriverCommandId::UNLOCK_TEMPLATE, IamResultCode::INVALID_PARAMETERS } },
456         { static_cast<IamPropertyMode>(DriverCommandId::VENDOR_COMMAND_BEGIN),
457             { DriverCommandId::UNLOCK_TEMPLATE, IamResultCode::INVALID_PARAMETERS } },
458         { static_cast<IamPropertyMode>(DriverCommandId::VENDOR_COMMAND_BEGIN + 1),
459             { static_cast<DriverCommandId>(DriverCommandId::VENDOR_COMMAND_BEGIN + 1), IamResultCode::SUCCESS } }
460     };
461     for (const auto &pair : data) {
462         sptr<MockIAllInOneExecutor> executorProxy(new (std::nothrow) MockIAllInOneExecutor());
463         ASSERT_TRUE(executorProxy != nullptr);
464         if (pair.second.second == IamResultCode::SUCCESS) {
465             EXPECT_CALL(*executorProxy, SendCommand(_, _, _))
466                 .Times(Exactly(1))
467                 .WillOnce([&pair](int32_t commandId, const std::vector<uint8_t> &extraInfo,
__anon1c0fbb0f1102(int32_t commandId, const std::vector<uint8_t> &extraInfo, const sptr<IExecutorCallback> &callbackObj) 468                               const sptr<IExecutorCallback> &callbackObj) {
469                     EXPECT_TRUE(commandId == pair.second.first);
470                     return HDF_SUCCESS;
471                 });
472         } else {
473             EXPECT_CALL(*executorProxy, SendCommand(_, _, _)).Times(Exactly(0));
474         }
475         auto executorHdi = MakeShared<FingerprintAllInOneExecutorHdi>(executorProxy);
476         auto executeCallback = MakeShared<UserIam::UserAuth::MockIExecuteCallback>();
477         ASSERT_TRUE(executeCallback != nullptr);
478         auto ret = executorHdi->SendCommand(pair.first, std::vector<uint8_t>(), executeCallback);
479         EXPECT_TRUE(ret == pair.second.second);
480     }
481 }
482 
483 HWTEST_F(FingerprintAllInOneExecutorHdiUnitTest, FingerprintAllInOneExecutorHdi_SendCommand_004, TestSize.Level0)
484 {
485     for (const auto &pair : RESULT_CODE_MAP) {
486         sptr<MockIAllInOneExecutor> executorProxy(new (std::nothrow) MockIAllInOneExecutor());
487         ASSERT_TRUE(executorProxy != nullptr);
488         EXPECT_CALL(*executorProxy, SendCommand(_, _, _))
489             .Times(Exactly(1))
490             .WillOnce([&pair](int32_t commandId, const std::vector<uint8_t> &extraInfo,
__anon1c0fbb0f1202(int32_t commandId, const std::vector<uint8_t> &extraInfo, const sptr<IExecutorCallback> &callbackObj) 491                           const sptr<IExecutorCallback> &callbackObj) { return pair.first; });
492         auto executorHdi = MakeShared<FingerprintAllInOneExecutorHdi>(executorProxy);
493         auto executeCallback = MakeShared<UserIam::UserAuth::MockIExecuteCallback>();
494         ASSERT_TRUE(executeCallback != nullptr);
495         auto ret =
496             executorHdi->SendCommand(IamPropertyMode::PROPERTY_MODE_FREEZE, std::vector<uint8_t>(), executeCallback);
497         EXPECT_TRUE(ret == pair.second);
498     }
499 }
500 
501 HWTEST_F(FingerprintAllInOneExecutorHdiUnitTest, FingerprintAllInOneExecutorHdi_SendMessage_001, TestSize.Level0)
502 {
503     for (const auto &pair : RESULT_CODE_MAP) {
504         sptr<MockIAllInOneExecutor> executorProxy(new (std::nothrow) MockIAllInOneExecutor());
505         ASSERT_TRUE(executorProxy != nullptr);
506         EXPECT_CALL(*executorProxy, SendMessage(_, _, _))
507             .Times(Exactly(1))
508             .WillOnce(Return(pair.first));
509         auto executorHdi = MakeShared<FingerprintAllInOneExecutorHdi>(executorProxy);
510         auto executeCallback = MakeShared<UserIam::UserAuth::MockIExecuteCallback>();
511         ASSERT_TRUE(executeCallback != nullptr);
512         std::vector<uint8_t> data;
513         auto ret =
514             executorHdi->SendMessage(1, 1, data);
515         EXPECT_TRUE(ret == pair.second);
516     }
517 }
518 
519 HWTEST_F(FingerprintAllInOneExecutorHdiUnitTest, FingerprintAllInOneExecutorHdi_GetProperty_001, TestSize.Level0)
520 {
521     auto executorHdi = MakeShared<FingerprintAllInOneExecutorHdi>(nullptr);
522     ASSERT_TRUE(executorHdi != nullptr);
523     std::vector<uint64_t> templateIdList;
524     std::vector<UserAuth::Attributes::AttributeKey> keys;
525     UserAuth::Property property = {};
526     auto ret = executorHdi->GetProperty(templateIdList, keys, property);
527     EXPECT_TRUE(ret == IamResultCode::GENERAL_ERROR);
528 }
529 
530 HWTEST_F(FingerprintAllInOneExecutorHdiUnitTest, FingerprintAllInOneExecutorHdi_GetProperty_002, TestSize.Level0)
531 {
532     auto executorProxy = new (std::nothrow) MockIAllInOneExecutor();
533     ASSERT_TRUE(executorProxy != nullptr);
534     auto executorHdi = MakeShared<FingerprintAllInOneExecutorHdi>(executorProxy);
535     ASSERT_TRUE(executorHdi != nullptr);
536     std::vector<uint64_t> templateIdList;
537     std::vector<UserAuth::Attributes::AttributeKey> keys = { UserAuth::Attributes::ATTR_SIGNATURE };
538     UserAuth::Property property = {};
539     auto ret = executorHdi->GetProperty(templateIdList, keys, property);
540     EXPECT_TRUE(ret == IamResultCode::GENERAL_ERROR);
541 }
542 
543 HWTEST_F(FingerprintAllInOneExecutorHdiUnitTest, FingerprintAllInOneExecutorHdi_GetProperty_003, TestSize.Level0)
544 {
545     for (const auto &pair : RESULT_CODE_MAP) {
546         auto executorProxy = new (std::nothrow) MockIAllInOneExecutor();
547         ASSERT_TRUE(executorProxy != nullptr);
548         EXPECT_CALL(*executorProxy, GetProperty(_, _, _))
549             .Times(Exactly(1))
550             .WillOnce([&pair](const std::vector<uint64_t> &templateIdList, const std::vector<int32_t> &propertyTypes,
__anon1c0fbb0f1302(const std::vector<uint64_t> &templateIdList, const std::vector<int32_t> &propertyTypes, Property &property) 551                           Property &property) { return pair.first; });
552         auto executorHdi = MakeShared<FingerprintAllInOneExecutorHdi>(executorProxy);
553         ASSERT_TRUE(executorHdi != nullptr);
554         std::vector<uint64_t> templateIdList;
555         std::vector<UserAuth::Attributes::AttributeKey> keys;
556         if (pair.first != HDF_SUCCESS) {
557             keys.push_back(UserAuth::Attributes::ATTR_PIN_SUB_TYPE);
558         }
559         UserAuth::Property property = {};
560         auto ret = executorHdi->GetProperty(templateIdList, keys, property);
561         EXPECT_TRUE(ret == pair.second);
562     }
563 }
564 
565 HWTEST_F(FingerprintAllInOneExecutorHdiUnitTest, FingerprintAllInOneExecutorHdi_SetCachedTemplates_001, TestSize.Level0)
566 {
567     auto executorHdi = MakeShared<FingerprintAllInOneExecutorHdi>(nullptr);
568     ASSERT_TRUE(executorHdi != nullptr);
569     std::vector<uint64_t> templateIdList;
570     auto ret = executorHdi->SetCachedTemplates(templateIdList);
571     EXPECT_TRUE(ret == IamResultCode::GENERAL_ERROR);
572 }
573 
574 HWTEST_F(FingerprintAllInOneExecutorHdiUnitTest, FingerprintAllInOneExecutorHdi_SetCachedTemplates_002, TestSize.Level0)
575 {
576     for (const auto &pair : RESULT_CODE_MAP) {
577         auto executorProxy = new (std::nothrow) MockIAllInOneExecutor();
578         ASSERT_TRUE(executorProxy != nullptr);
579         EXPECT_CALL(*executorProxy, SetCachedTemplates(_))
580             .Times(Exactly(1))
__anon1c0fbb0f1402(const std::vector<uint64_t> &templateIdList) 581             .WillOnce([&pair](const std::vector<uint64_t> &templateIdList) { return pair.first; });
582         auto executorHdi = MakeShared<FingerprintAllInOneExecutorHdi>(executorProxy);
583         ASSERT_TRUE(executorHdi != nullptr);
584         std::vector<uint64_t> templateIdList;
585         auto ret = executorHdi->SetCachedTemplates(templateIdList);
586         EXPECT_TRUE(ret == pair.second);
587     }
588 }
589 
590 HWTEST_F(FingerprintAllInOneExecutorHdiUnitTest, FingerprintAllInOneExecutorHdi_OnHdiDisconnect_001, TestSize.Level0)
591 {
592     auto executorHdi = MakeShared<FingerprintAllInOneExecutorHdi>(nullptr);
593     ASSERT_TRUE(executorHdi != nullptr);
594     executorHdi->OnHdiDisconnect();
595 }
596 
597 HWTEST_F(FingerprintAllInOneExecutorHdiUnitTest, FingerprintAllInOneExecutorHdi_SaCommandCallback_OnSaCommands_001,
598     TestSize.Level0)
599 {
600     sptr<MockIAllInOneExecutor> executorProxy(new (std::nothrow) MockIAllInOneExecutor());
601     ASSERT_TRUE(executorProxy != nullptr);
602     EXPECT_CALL(*executorProxy, OnRegisterFinish(_, _, _))
603         .Times(Exactly(1))
604         .WillOnce([](const std::vector<uint64_t> &templateIdList, const std::vector<uint8_t> &frameworkPublicKey,
__anon1c0fbb0f1502(const std::vector<uint64_t> &templateIdList, const std::vector<uint8_t> &frameworkPublicKey, const std::vector<uint8_t> &extraInfo) 605                       const std::vector<uint8_t> &extraInfo) { return HDF_SUCCESS; });
606     EXPECT_CALL(*executorProxy, RegisterSaCommandCallback(_))
607         .Times(Exactly(1))
__anon1c0fbb0f1602(const sptr<ISaCommandCallback> &callbackObj) 608         .WillOnce([](const sptr<ISaCommandCallback> &callbackObj) {
609             std::vector<SaCommand> commands;
610             callbackObj->OnSaCommands(commands);
611             return HDF_SUCCESS;
612         });
613     auto executorHdi = MakeShared<FingerprintAllInOneExecutorHdi>(executorProxy);
614     auto ret = executorHdi->OnRegisterFinish(std::vector<uint64_t>(), std::vector<uint8_t>(), std::vector<uint8_t>());
615     EXPECT_TRUE(ret == IamResultCode::SUCCESS);
616 }
617 } // namespace FingerprintAuth
618 } // namespace UserIam
619 } // namespace OHOS
620