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