1 /*
2  * Copyright (c) 2021-2023 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 "fingerprint_auth_hdi_test.h"
17 
18 #include <hdf_base.h>
19 
20 #include "iam_hat_test.h"
21 
22 #define LOG_LABEL OHOS::UserIam::Common::LABEL_FINGERPRINT_AUTH_IMPL
23 
24 using namespace std;
25 using namespace testing::ext;
26 using namespace OHOS;
27 using namespace OHOS::UserIam::Common;
28 using namespace OHOS::HDI::FingerprintAuth;
29 
30 static AllInOneExecutorImpl g_executorImpl;
31 static OHOS::Parcel parcel;
32 
SetUpTestCase()33 void UserIamFingerprintAuthTest::SetUpTestCase()
34 {
35 }
36 
TearDownTestCase()37 void UserIamFingerprintAuthTest::TearDownTestCase()
38 {
39 }
40 
SetUp()41 void UserIamFingerprintAuthTest::SetUp()
42 {
43 }
44 
TearDown()45 void UserIamFingerprintAuthTest::TearDown()
46 {
47 }
48 
49 class DummyIExecutorCallback : public IExecutorCallback {
50 public:
DummyIExecutorCallback(int32_t result, int32_t tip, int32_t message)51     DummyIExecutorCallback(int32_t result, int32_t tip, int32_t message) : result_(result), tip_(tip), message_(message)
52     {
53     }
54 
55     int32_t OnResult(int32_t result, const std::vector<uint8_t> &extraInfo) override
56     {
57         cout << "result is " << result << " extraInfo len is " << extraInfo.size() << endl;
58         return result_;
59     }
60 
61     int32_t OnTip(int32_t tip, const std::vector<uint8_t> &extraInfo) override
62     {
63         cout << "tip is " << tip << " extraInfo len is " << extraInfo.size() << endl;
64         return tip_;
65     }
66 
67     int32_t OnMessage(int32_t destRole, const std::vector<uint8_t> &msg) override
68     {
69         cout << "destRole is " << destRole << " msg len is " << msg.size() << endl;
70         return message_;
71     }
72 
73 private:
74     int32_t result_;
75     int32_t tip_;
76     int32_t message_;
77 };
78 
79 class DummyISaCommandCallback : public ISaCommandCallback {
80 public:
DummyISaCommandCallback(int32_t result)81     explicit DummyISaCommandCallback(int32_t result) : result_(result)
82     {
83     }
84 
85     int32_t OnSaCommands(const std::vector<SaCommand> &commands) override
86     {
87         return result_;
88     }
89 
90 private:
91     int32_t result_;
92 };
93 
FillTestExecutorInfo(Parcel &parcel, ExecutorInfo &executorInfo)94 static void FillTestExecutorInfo(Parcel &parcel, ExecutorInfo &executorInfo)
95 {
96     executorInfo.sensorId = parcel.ReadUint16();
97     executorInfo.executorMatcher = parcel.ReadUint32();
98     executorInfo.executorRole = static_cast<ExecutorRole>(parcel.ReadInt32());
99     executorInfo.authType = static_cast<AuthType>(parcel.ReadInt32());
100     executorInfo.esl = static_cast<ExecutorSecureLevel>(parcel.ReadInt32());
101     executorInfo.maxTemplateAcl = parcel.ReadInt32();
102     FillTestUint8Vector(parcel, executorInfo.publicKey);
103     FillTestUint8Vector(parcel, executorInfo.extraInfo);
104     cout << "success" << endl;
105 }
106 
FillTestIExecutorCallback(Parcel &parcel, sptr<IExecutorCallback> &callbackObj)107 static void FillTestIExecutorCallback(Parcel &parcel, sptr<IExecutorCallback> &callbackObj)
108 {
109     bool isNull = parcel.ReadBool();
110     if (isNull) {
111         callbackObj = nullptr;
112     } else {
113         callbackObj =
114             new (std::nothrow) DummyIExecutorCallback(parcel.ReadInt32(), parcel.ReadInt32(), parcel.ReadInt32());
115         if (callbackObj == nullptr) {
116             cout << "callbackObj construct fail" << endl;
117         }
118     }
119     cout << "success" << endl;
120 }
121 
FillTestGetPropertyTypeVector(Parcel &parcel, std::vector<GetPropertyType> &types)122 void FillTestGetPropertyTypeVector(Parcel &parcel, std::vector<GetPropertyType> &types)
123 {
124     std::vector<uint32_t> propertyTypeUint32;
125     FillTestUint32Vector(parcel, propertyTypeUint32);
126     for (const auto &val : propertyTypeUint32) {
127         types.push_back(static_cast<GetPropertyType>(val));
128     }
129 
130     cout << "success" << endl;
131 }
132 
FillTestProperty(Parcel &parcel, Property &property)133 void FillTestProperty(Parcel &parcel, Property &property)
134 {
135     property.authSubType = parcel.ReadUint64();
136     property.lockoutDuration = parcel.ReadInt32();
137     property.remainAttempts = parcel.ReadInt32();
138     FillTestString(parcel, property.enrollmentProgress);
139     FillTestString(parcel, property.sensorInfo);
140 
141     cout << "success" << endl;
142 }
143 
FillTestISaCommandCallback(Parcel &parcel, sptr<ISaCommandCallback> &callbackObj)144 void FillTestISaCommandCallback(Parcel &parcel, sptr<ISaCommandCallback> &callbackObj)
145 {
146     bool isNull = parcel.ReadBool();
147     if (isNull) {
148         callbackObj = nullptr;
149     } else {
150         callbackObj = new (std::nothrow) DummyISaCommandCallback(parcel.ReadInt32());
151         if (callbackObj == nullptr) {
152             cout << "callbackObj construct fail" << endl;
153         }
154     }
155     cout << "success" << endl;
156 }
157 
158 /**
159  * @tc.number: Security_IAM_Fingerprint_HDI_FUNC_0101
160  * @tc.name: Test SendCommand
161  * @tc.size: MediumTest
162  * @tc.type: Function
163  * @tc.level: Level1
164  */
HWTEST_F(UserIamFingerprintAuthTest, Security_IAM_Fingerprint_HDI_FUNC_0101, Function | MediumTest | Level1)165 HWTEST_F(UserIamFingerprintAuthTest, Security_IAM_Fingerprint_HDI_FUNC_0101, Function | MediumTest | Level1)
166 {
167     cout << "start test SendCommand" << endl;
168     uint8_t commandId = parcel.ReadUint8();
169     cout << "commandId is " << commandId << endl;
170     std::vector<uint8_t> extraInfo;
171     FillTestUint8Vector(parcel, extraInfo);
172     sptr<IExecutorCallback> callbackObj;
173     FillTestIExecutorCallback(parcel, callbackObj);
174     int32_t ret = g_executorImpl.SendCommand(commandId, extraInfo, callbackObj);
175     cout << "ret is " << ret << endl;
176     EXPECT_EQ(ret, 0);
177 }
178 
179 /**
180  * @tc.number: Security_IAM_Fingerprint_HDI_FUNC_0102
181  * @tc.name: Test Cancel
182  * @tc.size: MediumTest
183  * @tc.type: Function
184  * @tc.level: Level1
185  */
HWTEST_F(UserIamFingerprintAuthTest, Security_IAM_Fingerprint_HDI_FUNC_0102, Function | MediumTest | Level1)186 HWTEST_F(UserIamFingerprintAuthTest, Security_IAM_Fingerprint_HDI_FUNC_0102, Function | MediumTest | Level1)
187 {
188     cout << "start test Cancel" << endl;
189     uint64_t scheduleId = parcel.ReadUint64();
190     int32_t ret = g_executorImpl.Cancel(scheduleId);
191     cout << "ret is " << ret << endl;
192     EXPECT_EQ(ret, 0);
193 }
194 
195 /**
196  * @tc.number: Security_IAM_Fingerprint_HDI_FUNC_0103
197  * @tc.name: Test Delete
198  * @tc.size: MediumTest
199  * @tc.type: Function
200  * @tc.level: Level1
201  */
HWTEST_F(UserIamFingerprintAuthTest, Security_IAM_Fingerprint_HDI_FUNC_0103, Function | MediumTest | Level1)202 HWTEST_F(UserIamFingerprintAuthTest, Security_IAM_Fingerprint_HDI_FUNC_0103, Function | MediumTest | Level1)
203 {
204     cout << "start test Delete" << endl;
205     std::vector<uint64_t> templateIdList;
206     FillTestUint64Vector(parcel, templateIdList);
207     int32_t ret = g_executorImpl.Delete(templateIdList);
208     cout << "ret is " << ret << endl;
209     EXPECT_EQ(ret, 0);
210 }
211 
212 /**
213  * @tc.number: Security_IAM_Fingerprint_HDI_FUNC_0104
214  * @tc.name: Test GetExecutorInfo
215  * @tc.size: MediumTest
216  * @tc.type: Function
217  * @tc.level: Level1
218  */
HWTEST_F(UserIamFingerprintAuthTest, Security_IAM_Fingerprint_HDI_FUNC_0104, Function | MediumTest | Level1)219 HWTEST_F(UserIamFingerprintAuthTest, Security_IAM_Fingerprint_HDI_FUNC_0104, Function | MediumTest | Level1)
220 {
221     cout << "start test GetExecutorInfo" << endl;
222     ExecutorInfo executorInfo;
223     FillTestExecutorInfo(parcel, executorInfo);
224     int32_t ret = g_executorImpl.GetExecutorInfo(executorInfo);
225     cout << "ret is " << ret << endl;
226     EXPECT_EQ(ret, 0);
227 }
228 
229 /**
230  * @tc.number: Security_IAM_Fingerprint_HDI_FUNC_0106
231  * @tc.name: Test OnRegisterFinish
232  * @tc.size: MediumTest
233  * @tc.type: Function
234  * @tc.level: Level1
235  */
HWTEST_F(UserIamFingerprintAuthTest, Security_IAM_Fingerprint_HDI_FUNC_0106, Function | MediumTest | Level1)236 HWTEST_F(UserIamFingerprintAuthTest, Security_IAM_Fingerprint_HDI_FUNC_0106, Function | MediumTest | Level1)
237 {
238     cout << "start test OnRegisterFinish" << endl;
239     std::vector<uint64_t> templateIdList;
240     FillTestUint64Vector(parcel, templateIdList);
241     std::vector<uint8_t> frameworkPublicKey;
242     FillTestUint8Vector(parcel, frameworkPublicKey);
243     std::vector<uint8_t> extraInfo;
244     FillTestUint8Vector(parcel, extraInfo);
245     int32_t ret = g_executorImpl.OnRegisterFinish(templateIdList, frameworkPublicKey, extraInfo);
246     cout << "ret is " << ret << endl;
247     EXPECT_EQ(ret, 0);
248 }
249 
250 /**
251  * @tc.number: Security_IAM_Fingerprint_HDI_FUNC_0107
252  * @tc.name: Test Enroll
253  * @tc.size: MediumTest
254  * @tc.type: Function
255  * @tc.level: Level1
256  */
HWTEST_F(UserIamFingerprintAuthTest, Security_IAM_Fingerprint_HDI_FUNC_0107, Function | MediumTest | Level1)257 HWTEST_F(UserIamFingerprintAuthTest, Security_IAM_Fingerprint_HDI_FUNC_0107, Function | MediumTest | Level1)
258 {
259     cout << "start test Enroll" << endl;
260     uint64_t scheduleId = parcel.ReadUint64();
261     std::vector<uint8_t> extraInfo;
262     FillTestUint8Vector(parcel, extraInfo);
263     sptr<IExecutorCallback> callbackObj;
264     FillTestIExecutorCallback(parcel, callbackObj);
265     int32_t ret = g_executorImpl.Enroll(scheduleId, extraInfo, callbackObj);
266     cout << "ret is " << ret << endl;
267     EXPECT_EQ(ret, 0);
268 }
269 
270 /**
271  * @tc.number: Security_IAM_Fingerprint_HDI_FUNC_0109
272  * @tc.name: Test Identify
273  * @tc.size: MediumTest
274  * @tc.type: Function
275  * @tc.level: Level1
276  */
HWTEST_F(UserIamFingerprintAuthTest, Security_IAM_Fingerprint_HDI_FUNC_0109, Function | MediumTest | Level1)277 HWTEST_F(UserIamFingerprintAuthTest, Security_IAM_Fingerprint_HDI_FUNC_0109, Function | MediumTest | Level1)
278 {
279     cout << "start test Identify" << endl;
280     uint64_t scheduleId = parcel.ReadUint64();
281     std::vector<uint8_t> extraInfo;
282     FillTestUint8Vector(parcel, extraInfo);
283     sptr<IExecutorCallback> callbackObj;
284     FillTestIExecutorCallback(parcel, callbackObj);
285     int32_t ret = g_executorImpl.Identify(scheduleId, extraInfo, callbackObj);
286     cout << "ret is " << ret << endl;
287     EXPECT_EQ(ret, 0);
288 }
289 
290 /**
291  * @tc.number: Security_IAM_Fingerprint_HDI_FUNC_0110
292  * @tc.name: Test GetExecutorList
293  * @tc.size: MediumTest
294  * @tc.type: Function
295  * @tc.level: Level1
296  */
HWTEST_F(UserIamFingerprintAuthTest, Security_IAM_Fingerprint_HDI_FUNC_0110, Function | MediumTest | Level1)297 HWTEST_F(UserIamFingerprintAuthTest, Security_IAM_Fingerprint_HDI_FUNC_0110, Function | MediumTest | Level1)
298 {
299     cout << "start test GetExecutorList" << endl;
300     FingerprintAuthInterfaceService fingerprint_Interface;
301     std::vector<sptr<IAllInOneExecutor>> executorList;
302     int32_t ret = fingerprint_Interface.GetExecutorList(executorList);
303     cout << "ret is " << ret << endl;
304     EXPECT_EQ(ret, 0);
305 }
306 
307 /**
308  * @tc.number: Security_IAM_Fingerprint_HDI_NEW_FUNC_0101
309  * @tc.name: Test GetProperty
310  * @tc.size: MediumTest
311  * @tc.type: Function
312  * @tc.level: Level1
313  */
HWTEST_F(UserIamFingerprintAuthTest, Security_IAM_Fingerprint_HDI_NEW_FUNC_0101, Function | MediumTest | Level1)314 HWTEST_F(UserIamFingerprintAuthTest, Security_IAM_Fingerprint_HDI_NEW_FUNC_0101, Function | MediumTest | Level1)
315 {
316     cout << "start test GetProperty" << endl;
317     std::vector<uint64_t> templateIdList;
318     FillTestUint64Vector(parcel, templateIdList);
319     std::vector<int32_t> propertyTypes;
320     FillTestInt32Vector(parcel, propertyTypes);
321     Property property;
322     FillTestProperty(parcel, property);
323 
324     int32_t ret = g_executorImpl.GetProperty(templateIdList, propertyTypes, property);
325     cout << "ret is " << ret << endl;
326     EXPECT_EQ(ret, 0);
327 }
328 
329 /**
330  * @tc.number: Security_IAM_Fingerprint_HDI_NEW_FUNC_0102
331  * @tc.name: Test SetCachedTemplates
332  * @tc.size: MediumTest
333  * @tc.type: Function
334  * @tc.level: Level1
335  */
HWTEST_F(UserIamFingerprintAuthTest, Security_IAM_Fingerprint_HDI_NEW_FUNC_0102, Function | MediumTest | Level1)336 HWTEST_F(UserIamFingerprintAuthTest, Security_IAM_Fingerprint_HDI_NEW_FUNC_0102, Function | MediumTest | Level1)
337 {
338     cout << "start test SetCachedTemplates" << endl;
339     std::vector<uint64_t> templateIdList;
340     FillTestUint64Vector(parcel, templateIdList);
341 
342     int32_t ret = g_executorImpl.SetCachedTemplates(templateIdList);
343 
344     cout << "ret is " << ret << endl;
345     EXPECT_EQ(ret, 0);
346 }
347 
348 /**
349  * @tc.number: Security_IAM_Fingerprint_HDI_NEW_FUNC_0103
350  * @tc.name: Test RegisterSaCommandCallback
351  * @tc.size: MediumTest
352  * @tc.type: Function
353  * @tc.level: Level1
354  */
HWTEST_F(UserIamFingerprintAuthTest, Security_IAM_Fingerprint_HDI_NEW_FUNC_0103, Function | MediumTest | Level1)355 HWTEST_F(UserIamFingerprintAuthTest, Security_IAM_Fingerprint_HDI_NEW_FUNC_0103, Function | MediumTest | Level1)
356 {
357     cout << "start test RegisterSaCommandCallback" << endl;
358     sptr<ISaCommandCallback> callbackObj = nullptr;
359     FillTestISaCommandCallback(parcel, callbackObj);
360 
361     int32_t ret = g_executorImpl.RegisterSaCommandCallback(callbackObj);
362 
363     cout << "ret is " << ret << endl;
364     EXPECT_EQ(ret, 0);
365 }
366 
367 /**
368  * @tc.number: Security_IAM_Fingerprint_HDI_NEW_FUNC_0104
369  * @tc.name: Test GetExecutorList
370  * @tc.size: MediumTest
371  * @tc.type: Function
372  * @tc.level: Level1
373  */
HWTEST_F(UserIamFingerprintAuthTest, Security_IAM_Fingerprint_HDI_NEW_FUNC_0104, Function | MediumTest | Level1)374 HWTEST_F(UserIamFingerprintAuthTest, Security_IAM_Fingerprint_HDI_NEW_FUNC_0104, Function | MediumTest | Level1)
375 {
376     cout << "start test GetExecutorList" << endl;
377     std::vector<sptr<IAllInOneExecutor>> executorList;
378     FingerprintAuthInterfaceService fingerprint_Interface;
379 
380     int32_t ret = fingerprint_Interface.GetExecutorList(executorList);
381 
382     cout << "ret is " << ret << endl;
383     EXPECT_EQ(ret, 0);
384 }
385 
386 /**
387  * @tc.number: Security_IAM_Fingerprint_HDI_NEW_FUNC_0105
388  * @tc.name: Test GetExecutorList
389  * @tc.size: MediumTest
390  * @tc.type: Function
391  * @tc.level: Level1
392  */
HWTEST_F(UserIamFingerprintAuthTest, Security_IAM_Fingerprint_HDI_NEW_FUNC_0105, Function | MediumTest | Level1)393 HWTEST_F(UserIamFingerprintAuthTest, Security_IAM_Fingerprint_HDI_NEW_FUNC_0105, Function | MediumTest | Level1)
394 {
395     cout << "start test GetExecutorList" << endl;
396     uint64_t scheduleId = parcel.ReadUint64();
397     std::vector<uint64_t> templateIdList;
398     FillTestUint64Vector(parcel, templateIdList);
399     std::vector<uint8_t> extraInfo;
400     FillTestUint8Vector(parcel, extraInfo);
401     sptr<IExecutorCallback> callbackObj;
402     FillTestIExecutorCallback(parcel, callbackObj);
403     int32_t ret = g_executorImpl.Authenticate(scheduleId, templateIdList, true, extraInfo, callbackObj);
404 
405     cout << "ret is " << ret << endl;
406     EXPECT_EQ(ret, 0);
407 }
408 
409 /**
410  * @tc.number: Security_IAM_Fingerprint_HDI_NEW_FUNC_0106
411  * @tc.name: Test Send Message
412  * @tc.size: MediumTest
413  * @tc.type: Function
414  * @tc.level: Level1
415  */
HWTEST_F(UserIamFingerprintAuthTest, Security_IAM_Fingerprint_HDI_NEW_FUNC_0106, Function | MediumTest | Level1)416 HWTEST_F(UserIamFingerprintAuthTest, Security_IAM_Fingerprint_HDI_NEW_FUNC_0106, Function | MediumTest | Level1)
417 {
418     cout << "start test RegisterSaCommandCallback" << endl;
419     sptr<ISaCommandCallback> callbackObj = nullptr;
420     FillTestISaCommandCallback(parcel, callbackObj);
421 
422     uint64_t scheduleId = parcel.ReadUint64();
423     int32_t srcRole = parcel.ReadInt32();
424     std::vector<uint8_t> msg;
425     FillTestUint8Vector(parcel, msg);
426 
427     int32_t ret = g_executorImpl.SendMessage(scheduleId, srcRole, msg);
428 
429     cout << "ret is " << ret << endl;
430     EXPECT_EQ(ret, 0);
431 }