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