1 /*
2 * Copyright (c) 2021-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 "fingerprint_auth_hdi.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::V1_0;
26
27 static ExecutorImpl g_executorImpl;
28 static OHOS::Parcel parcel;
29
SetUpTestCase()30 void UserIamFingerprintAuthTest::SetUpTestCase()
31 {
32 }
33
TearDownTestCase()34 void UserIamFingerprintAuthTest::TearDownTestCase()
35 {
36 }
37
SetUp()38 void UserIamFingerprintAuthTest::SetUp()
39 {
40 }
41
TearDown()42 void UserIamFingerprintAuthTest::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_Fingerprint_HDI_FUNC_0101
106 * @tc.name: Test SendCommand
107 * @tc.size: MediumTest
108 * @tc.type: Function
109 * @tc.level: Level1
110 */
111 HWTEST_F(UserIamFingerprintAuthTest, Security_IAM_Fingerprint_HDI_FUNC_0101, Function | MediumTest | Level1)
112 {
113 cout << "start test SendCommand" << endl;
114 uint8_t commandId = parcel.ReadUint8();
115 cout << "commandId is " << commandId << endl;
116 std::vector<uint8_t> extraInfo;
117 FillTestUint8Vector(parcel, extraInfo);
118 sptr<IExecutorCallback> callbackObj;
119 FillTestIExecutorCallback(parcel, callbackObj);
120 int32_t ret = g_executorImpl.SendCommand(commandId, extraInfo, callbackObj);
121 cout << "ret is " << ret << endl;
122 EXPECT_EQ(ret, 0);
123 }
124
125 /**
126 * @tc.number: Security_IAM_Fingerprint_HDI_FUNC_0102
127 * @tc.name: Test Cancel
128 * @tc.size: MediumTest
129 * @tc.type: Function
130 * @tc.level: Level1
131 */
132 HWTEST_F(UserIamFingerprintAuthTest, Security_IAM_Fingerprint_HDI_FUNC_0102, Function | MediumTest | Level1)
133 {
134 cout << "start test Cancel" << endl;
135 uint64_t scheduleId = parcel.ReadUint64();
136 int32_t ret = g_executorImpl.Cancel(scheduleId);
137 cout << "ret is " << ret << endl;
138 EXPECT_EQ(ret, 0);
139 }
140
141 /**
142 * @tc.number: Security_IAM_Fingerprint_HDI_FUNC_0103
143 * @tc.name: Test Delete
144 * @tc.size: MediumTest
145 * @tc.type: Function
146 * @tc.level: Level1
147 */
148 HWTEST_F(UserIamFingerprintAuthTest, Security_IAM_Fingerprint_HDI_FUNC_0103, Function | MediumTest | Level1)
149 {
150 cout << "start test Delete" << endl;
151 std::vector<uint64_t> templateIdList;
152 FillTestUint64Vector(parcel, templateIdList);
153 int32_t ret = g_executorImpl.Delete(templateIdList);
154 cout << "ret is " << ret << endl;
155 EXPECT_EQ(ret, 0);
156 }
157
158 /**
159 * @tc.number: Security_IAM_Fingerprint_HDI_FUNC_0104
160 * @tc.name: Test GetExecutorInfo
161 * @tc.size: MediumTest
162 * @tc.type: Function
163 * @tc.level: Level1
164 */
165 HWTEST_F(UserIamFingerprintAuthTest, Security_IAM_Fingerprint_HDI_FUNC_0104, Function | MediumTest | Level1)
166 {
167 cout << "start test GetExecutorInfo" << endl;
168 ExecutorInfo executorInfo;
169 FillTestExecutorInfo(parcel, executorInfo);
170 int32_t ret = g_executorImpl.GetExecutorInfo(executorInfo);
171 cout << "ret is " << ret << endl;
172 EXPECT_EQ(ret, 0);
173 }
174
175 /**
176 * @tc.number: Security_IAM_Fingerprint_HDI_FUNC_0105
177 * @tc.name: Test GetTemplateInfo
178 * @tc.size: MediumTest
179 * @tc.type: Function
180 * @tc.level: Level1
181 */
182 HWTEST_F(UserIamFingerprintAuthTest, Security_IAM_Fingerprint_HDI_FUNC_0105, Function | MediumTest | Level1)
183 {
184 cout << "start test GetTemplateInfo" << endl;
185 uint64_t templateId = parcel.ReadUint64();
186 TemplateInfo templateInfo;
187 FillTestTemplateInfo(parcel, templateInfo);
188 int32_t ret = g_executorImpl.GetTemplateInfo(templateId, templateInfo);
189 cout << "ret is " << ret << endl;
190 EXPECT_EQ(ret, 0);
191 }
192
193 /**
194 * @tc.number: Security_IAM_Fingerprint_HDI_FUNC_0106
195 * @tc.name: Test OnRegisterFinish
196 * @tc.size: MediumTest
197 * @tc.type: Function
198 * @tc.level: Level1
199 */
200 HWTEST_F(UserIamFingerprintAuthTest, Security_IAM_Fingerprint_HDI_FUNC_0106, Function | MediumTest | Level1)
201 {
202 cout << "start test OnRegisterFinish" << endl;
203 std::vector<uint64_t> templateIdList;
204 FillTestUint64Vector(parcel, templateIdList);
205 std::vector<uint8_t> frameworkPublicKey;
206 FillTestUint8Vector(parcel, frameworkPublicKey);
207 std::vector<uint8_t> extraInfo;
208 FillTestUint8Vector(parcel, extraInfo);
209 int32_t ret = g_executorImpl.OnRegisterFinish(templateIdList, frameworkPublicKey, extraInfo);
210 cout << "ret is " << ret << endl;
211 EXPECT_EQ(ret, 0);
212 }
213
214 /**
215 * @tc.number: Security_IAM_Fingerprint_HDI_FUNC_0107
216 * @tc.name: Test Enroll
217 * @tc.size: MediumTest
218 * @tc.type: Function
219 * @tc.level: Level1
220 */
221 HWTEST_F(UserIamFingerprintAuthTest, Security_IAM_Fingerprint_HDI_FUNC_0107, Function | MediumTest | Level1)
222 {
223 cout << "start test Enroll" << endl;
224 uint64_t scheduleId = parcel.ReadUint64();
225 std::vector<uint8_t> extraInfo;
226 FillTestUint8Vector(parcel, extraInfo);
227 sptr<IExecutorCallback> callbackObj;
228 FillTestIExecutorCallback(parcel, callbackObj);
229 int32_t ret = g_executorImpl.Enroll(scheduleId, extraInfo, callbackObj);
230 cout << "ret is " << ret << endl;
231 EXPECT_EQ(ret, 0);
232 }
233
234 /**
235 * @tc.number: Security_IAM_Fingerprint_HDI_FUNC_0108
236 * @tc.name: Test Authenticate
237 * @tc.size: MediumTest
238 * @tc.type: Function
239 * @tc.level: Level1
240 */
241 HWTEST_F(UserIamFingerprintAuthTest, Security_IAM_Fingerprint_HDI_FUNC_0108, Function | MediumTest | Level1)
242 {
243 cout << "start test Authenticate" << endl;
244 uint64_t scheduleId = parcel.ReadUint64();
245 std::vector<uint64_t> templateIdList;
246 FillTestUint64Vector(parcel, templateIdList);
247 std::vector<uint8_t> extraInfo;
248 FillTestUint8Vector(parcel, extraInfo);
249 sptr<IExecutorCallback> callbackObj;
250 FillTestIExecutorCallback(parcel, callbackObj);
251 int32_t ret = g_executorImpl.Authenticate(scheduleId, templateIdList, extraInfo, callbackObj);
252 cout << "ret is " << ret << endl;
253 EXPECT_EQ(ret, 0);
254 }
255
256 /**
257 * @tc.number: Security_IAM_Fingerprint_HDI_FUNC_0109
258 * @tc.name: Test Identify
259 * @tc.size: MediumTest
260 * @tc.type: Function
261 * @tc.level: Level1
262 */
263 HWTEST_F(UserIamFingerprintAuthTest, Security_IAM_Fingerprint_HDI_FUNC_0109, Function | MediumTest | Level1)
264 {
265 cout << "start test Identify" << endl;
266 uint64_t scheduleId = parcel.ReadUint64();
267 std::vector<uint8_t> extraInfo;
268 FillTestUint8Vector(parcel, extraInfo);
269 sptr<IExecutorCallback> callbackObj;
270 FillTestIExecutorCallback(parcel, callbackObj);
271 int32_t ret = g_executorImpl.Identify(scheduleId, extraInfo, callbackObj);
272 cout << "ret is " << ret << endl;
273 EXPECT_EQ(ret, 0);
274 }
275
276 /**
277 * @tc.number: Security_IAM_Fingerprint_HDI_FUNC_0110
278 * @tc.name: Test GetExecutorList
279 * @tc.size: MediumTest
280 * @tc.type: Function
281 * @tc.level: Level1
282 */
283 HWTEST_F(UserIamFingerprintAuthTest, Security_IAM_Fingerprint_HDI_FUNC_0110, Function | MediumTest | Level1)
284 {
285 cout << "start test GetExecutorList" << endl;
286 FingerprintAuthInterfaceService fingerprint_Interface;
287 std::vector<sptr<IExecutor>> executorList;
288 int32_t ret = fingerprint_Interface.GetExecutorList(executorList);
289 cout << "ret is " << ret << endl;
290 EXPECT_EQ(ret, 0);
291 }