• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "face_auth_hdi.h"
19 
20 #define LOG_LABEL OHOS::UserIam::Common::LABEL_FACE_AUTH_IMPL
21 
22 using namespace std;
23 using namespace testing::ext;
24 using namespace OHOS::UserIam::Common;
25 using namespace OHOS::HDI::FaceAuth::V1_0;
26 
27 static ExecutorImpl g_executorImpl;
28 static OHOS::Parcel parcel;
29 
SetUpTestCase()30 void UserIamFaceAuthTest::SetUpTestCase()
31 {
32 }
33 
TearDownTestCase()34 void UserIamFaceAuthTest::TearDownTestCase()
35 {
36 }
37 
SetUp()38 void UserIamFaceAuthTest::SetUp()
39 {
40 }
41 
TearDown()42 void UserIamFaceAuthTest::TearDown()
43 {
44 }
45 
46 class DummyIExecutorCallback : public IExecutorCallback {
47 public:
DummyIExecutorCallback(int32_t result,int32_t acquire)48     DummyIExecutorCallback(int32_t result, int32_t acquire) : result_(result), acquire_(acquire)
49     {
50     }
51 
OnResult(int32_t result,const std::vector<uint8_t> & extraInfo)52     int32_t OnResult(int32_t result, const std::vector<uint8_t> &extraInfo) override
53     {
54         cout << "result is " << result << " extraInfo len is " << extraInfo.size() << endl;
55         return result_;
56     }
57 
OnTip(int32_t acquire,const std::vector<uint8_t> & extraInfo)58     int32_t OnTip(int32_t acquire, const std::vector<uint8_t> &extraInfo) override
59     {
60         cout << "result is " << acquire << " extraInfo len is " << extraInfo.size() << endl;
61         return acquire_;
62     }
63 
64 private:
65     int32_t result_;
66     int32_t acquire_;
67 };
68 
FillTestExecutorInfo(Parcel & parcel,ExecutorInfo & executorInfo)69 static void FillTestExecutorInfo(Parcel &parcel, ExecutorInfo &executorInfo)
70 {
71     executorInfo.sensorId = parcel.ReadUint16();
72     executorInfo.executorType = parcel.ReadUint32();
73     executorInfo.executorRole = static_cast<ExecutorRole>(parcel.ReadInt32());
74     executorInfo.authType = static_cast<AuthType>(parcel.ReadInt32());
75     executorInfo.esl = static_cast<ExecutorSecureLevel>(parcel.ReadInt32());
76     FillTestUint8Vector(parcel, executorInfo.publicKey);
77     FillTestUint8Vector(parcel, executorInfo.extraInfo);
78     cout << "success" << endl;
79 }
80 
FillTestTemplateInfo(Parcel & parcel,TemplateInfo & templateInfo)81 static void FillTestTemplateInfo(Parcel &parcel, TemplateInfo &templateInfo)
82 {
83     templateInfo.executorType = parcel.ReadUint32();
84     templateInfo.lockoutDuration = parcel.ReadInt32();
85     templateInfo.remainAttempts = parcel.ReadInt32();
86     FillTestUint8Vector(parcel, templateInfo.extraInfo);
87     cout << "success" << endl;
88 }
89 
FillTestIExecutorCallback(Parcel & parcel,sptr<IExecutorCallback> & callbackObj)90 static void FillTestIExecutorCallback(Parcel &parcel, sptr<IExecutorCallback> &callbackObj)
91 {
92     bool isNull = parcel.ReadBool();
93     if (isNull) {
94         callbackObj = nullptr;
95     } else {
96         callbackObj = new (std::nothrow) DummyIExecutorCallback(parcel.ReadInt32(), parcel.ReadInt32());
97         if (callbackObj == nullptr) {
98             cout << "callbackObj construct fail" << endl;
99         }
100     }
101     cout << "success" << endl;
102 }
103 
104 /**
105  * @tc.number: Security_IAM_Face_HDI_FUNC_0101
106  * @tc.name: Test SendCommand
107  * @tc.size: MediumTest
108  * @tc.type: Function
109  * @tc.level: Level1
110  */
111 HWTEST_F(UserIamFaceAuthTest, Security_IAM_Face_HDI_FUNC_0101, Function | MediumTest | Level1)
112 {
113     cout << "start test SendCommand" << endl;
114     uint8_t commandId = parcel.ReadUint8();
115     std::vector<uint8_t> extraInfo;
116     FillTestUint8Vector(parcel, extraInfo);
117     sptr<IExecutorCallback> callbackObj;
118     FillTestIExecutorCallback(parcel, callbackObj);
119     int32_t ret = g_executorImpl.SendCommand(commandId, extraInfo, callbackObj);
120     cout << "ret is " << ret << endl;
121     EXPECT_EQ(ret, 0);
122 }
123 
124 /**
125  * @tc.number: Security_IAM_Face_HDI_FUNC_0102
126  * @tc.name: Test Cancel
127  * @tc.size: MediumTest
128  * @tc.type: Function
129  * @tc.level: Level1
130  */
131 HWTEST_F(UserIamFaceAuthTest, Security_IAM_Face_HDI_FUNC_0102, Function | MediumTest | Level1)
132 {
133     cout << "start test Cancel" << endl;
134     uint64_t scheduleId = parcel.ReadUint64();
135     int32_t ret = g_executorImpl.Cancel(scheduleId);
136     cout << "ret is " << ret << endl;
137     EXPECT_EQ(ret, 0);
138 }
139 
140 /**
141  * @tc.number: Security_IAM_Face_HDI_FUNC_0103
142  * @tc.name: Test Delete
143  * @tc.size: MediumTest
144  * @tc.type: Function
145  * @tc.level: Level1
146  */
147 HWTEST_F(UserIamFaceAuthTest, Security_IAM_Face_HDI_FUNC_0103, Function | MediumTest | Level1)
148 {
149     cout << "start test Delete" << endl;
150     std::vector<uint64_t> templateIdList;
151     FillTestUint64Vector(parcel, templateIdList);
152     int32_t ret = g_executorImpl.Delete(templateIdList);
153     cout << "ret is " << ret << endl;
154     EXPECT_EQ(ret, 0);
155 }
156 
157 /**
158  * @tc.number: Security_IAM_Face_HDI_FUNC_0104
159  * @tc.name: Test GetExecutorInfo
160  * @tc.size: MediumTest
161  * @tc.type: Function
162  * @tc.level: Level1
163  */
164 HWTEST_F(UserIamFaceAuthTest, Security_IAM_Face_HDI_FUNC_0104, Function | MediumTest | Level1)
165 {
166     cout << "start test GetExecutorInfo" << endl;
167     ExecutorInfo executorInfo;
168     FillTestExecutorInfo(parcel, executorInfo);
169     int32_t ret = g_executorImpl.GetExecutorInfo(executorInfo);
170     cout << "ret is " << ret << endl;
171     EXPECT_EQ(ret, 0);
172 }
173 
174 /**
175  * @tc.number: Security_IAM_Face_HDI_FUNC_0105
176  * @tc.name: Test GetTemplateInfo
177  * @tc.size: MediumTest
178  * @tc.type: Function
179  * @tc.level: Level1
180  */
181 HWTEST_F(UserIamFaceAuthTest, Security_IAM_Face_HDI_FUNC_0105, Function | MediumTest | Level1)
182 {
183     cout << "start test GetTemplateInfo" << endl;
184     uint64_t templateId = parcel.ReadUint64();
185     TemplateInfo templateInfo;
186     FillTestTemplateInfo(parcel, templateInfo);
187     int32_t ret = g_executorImpl.GetTemplateInfo(templateId, templateInfo);
188     cout << "ret is " << ret << endl;
189     EXPECT_EQ(ret, 0);
190 }
191 
192 /**
193  * @tc.number: Security_IAM_Face_HDI_FUNC_0106
194  * @tc.name: Test OnRegisterFinish
195  * @tc.size: MediumTest
196  * @tc.type: Function
197  * @tc.level: Level1
198  */
199 HWTEST_F(UserIamFaceAuthTest, Security_IAM_Face_HDI_FUNC_0106, Function | MediumTest | Level1)
200 {
201     cout << "start test OnRegisterFinish" << endl;
202     std::vector<uint64_t> templateIdList;
203     FillTestUint64Vector(parcel, templateIdList);
204     std::vector<uint8_t> frameworkPublicKey;
205     FillTestUint8Vector(parcel, frameworkPublicKey);
206     std::vector<uint8_t> extraInfo;
207     FillTestUint8Vector(parcel, extraInfo);
208     int32_t ret = g_executorImpl.OnRegisterFinish(templateIdList, frameworkPublicKey, extraInfo);
209     cout << "ret is " << ret << endl;
210     EXPECT_EQ(ret, 0);
211 }
212 
213 /**
214  * @tc.number: Security_IAM_Face_HDI_FUNC_0107
215  * @tc.name: Test Enroll
216  * @tc.size: MediumTest
217  * @tc.type: Function
218  * @tc.level: Level1
219  */
220 HWTEST_F(UserIamFaceAuthTest, Security_IAM_Face_HDI_FUNC_0107, Function | MediumTest | Level1)
221 {
222     cout << "start test Enroll" << endl;
223     uint64_t scheduleId = parcel.ReadUint64();
224     std::vector<uint8_t> extraInfo;
225     FillTestUint8Vector(parcel, extraInfo);
226     sptr<IExecutorCallback> callbackObj;
227     FillTestIExecutorCallback(parcel, callbackObj);
228     int32_t ret = g_executorImpl.Enroll(scheduleId, extraInfo, callbackObj);
229     cout << "ret is " << ret << endl;
230     EXPECT_EQ(ret, 0);
231 }
232 
233 /**
234  * @tc.number: Security_IAM_Face_HDI_FUNC_0108
235  * @tc.name: Test Authenticate
236  * @tc.size: MediumTest
237  * @tc.type: Function
238  * @tc.level: Level1
239  */
240 HWTEST_F(UserIamFaceAuthTest, Security_IAM_Face_HDI_FUNC_0108, Function | MediumTest | Level1)
241 {
242     cout << "start test Authenticate" << endl;
243     uint64_t scheduleId = parcel.ReadUint64();
244     std::vector<uint64_t> templateIdList;
245     FillTestUint64Vector(parcel, templateIdList);
246     std::vector<uint8_t> extraInfo;
247     FillTestUint8Vector(parcel, extraInfo);
248     sptr<IExecutorCallback> callbackObj;
249     FillTestIExecutorCallback(parcel, callbackObj);
250     int32_t ret = g_executorImpl.Authenticate(scheduleId, templateIdList, extraInfo, callbackObj);
251     cout << "ret is " << ret << endl;
252     EXPECT_EQ(ret, 0);
253 }
254 
255 /**
256  * @tc.number: Security_IAM_Face_HDI_FUNC_0109
257  * @tc.name: Test Identify
258  * @tc.size: MediumTest
259  * @tc.type: Function
260  * @tc.level: Level1
261  */
262 HWTEST_F(UserIamFaceAuthTest, Security_IAM_Face_HDI_FUNC_0109, Function | MediumTest | Level1)
263 {
264     cout << "start test Identify" << endl;
265     uint64_t scheduleId = parcel.ReadUint64();
266     std::vector<uint8_t> extraInfo;
267     FillTestUint8Vector(parcel, extraInfo);
268     sptr<IExecutorCallback> callbackObj;
269     FillTestIExecutorCallback(parcel, callbackObj);
270     int32_t ret = g_executorImpl.Identify(scheduleId, extraInfo, callbackObj);
271     cout << "ret is " << ret << endl;
272     EXPECT_EQ(ret, 0);
273 }
274 
275 /**
276  * @tc.number: Security_IAM_Face_HDI_FUNC_0110
277  * @tc.name: Test GetExecutorList
278  * @tc.size: MediumTest
279  * @tc.type: Function
280  * @tc.level: Level1
281  */
282 HWTEST_F(UserIamFaceAuthTest, Security_IAM_Face_HDI_FUNC_0110, Function | MediumTest | Level1)
283 {
284     cout << "start test GetExecutorList" << endl;
285     FaceAuthInterfaceService faceauth_Interface;
286     std::vector<sptr<IExecutor>> executorList;
287     int32_t ret = faceauth_Interface.GetExecutorList(executorList);
288     cout << "ret is " << ret << endl;
289     EXPECT_EQ(ret, 0);
290 }
291 
292 /**
293  * @tc.number: Security_IAM_Face_HDI_FUNC_0111
294  * @tc.name: Test SetBufferProducer
295  * @tc.size: MediumTest
296  * @tc.type: Function
297  * @tc.level: Level1
298  */
299 HWTEST_F(UserIamFaceAuthTest, Security_IAM_Face_HDI_FUNC_0111, Function | MediumTest | Level1)
300 {
301     cout << "start test SetBufferProducer" << endl;
302     sptr<IBufferProducer> bufferProducer = nullptr;
303     if (parcel.ReadBool()) {
304         auto surface = Surface::CreateSurfaceAsConsumer();
305         if (surface == nullptr) {
306             cout << "CreateSurfaceAsConsumer fail" << endl;
307         }
308         else
309         {
310             bufferProducer = surface->GetProducer();
311         }
312     }
313     sptr<BufferProducerSequenceable> producerSequenceable =
314         new (std::nothrow) BufferProducerSequenceable(bufferProducer);
315     int32_t ret = g_executorImpl.SetBufferProducer(producerSequenceable);
316     cout << "ret is " << ret << endl;
317     EXPECT_EQ(ret, 0);
318 }
319 
320