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