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 "pin_auth_hdi_test.h"
17 #include "iam_hat_test.h"
18
19 using namespace std;
20 using namespace testing::ext;
21 using namespace OHOS::UserIam::Common;
22 using namespace OHOS::HDI::PinAuth;
23 using namespace OHOS::HDI::PinAuth::V3_0;
24 using Property = OHOS::HDI::PinAuth::V3_0::Property;
25 using HdiExecutorRole = OHOS::HDI::PinAuth::V3_0::ExecutorRole;
26 using HdiAuthType = OHOS::HDI::PinAuth::V3_0::AuthType;
27
28 static AllInOneImpl g_executorImpl(make_shared<OHOS::UserIam::PinAuth::PinAuth>());
29 static OHOS::Parcel parcel;
30 int32_t Expectedvalue = 0;
31
SetUpTestCase()32 void UserIamPinAuthTest::SetUpTestCase()
33 {
34 }
35
TearDownTestCase()36 void UserIamPinAuthTest::TearDownTestCase()
37 {
38 }
39
SetUp()40 void UserIamPinAuthTest::SetUp()
41 {
42 }
43
TearDown()44 void UserIamPinAuthTest::TearDown()
45 {
46 }
47
48 class DummyIExecutorCallback : public IExecutorCallback {
49 public:
DummyIExecutorCallback(int32_t onResultResult,int32_t onGetDataResult,int32_t onGetDataV1Result,int32_t onTipResult,int32_t onMessageResult)50 DummyIExecutorCallback(int32_t onResultResult, int32_t onGetDataResult, int32_t onGetDataV1Result,
51 int32_t onTipResult, int32_t onMessageResult)
52 : onResultResult_(onResultResult), onGetDataResult_(onGetDataResult), onTipResult_(onTipResult),
53 onMessageResult_(onMessageResult)
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 onResultResult_;
61 }
62
OnGetData(const std::vector<uint8_t> & algoParameter,uint64_t authSubType,uint32_t algoVersion,const std::vector<uint8_t> & challenge,const std::string & complexityReg)63 int32_t OnGetData(const std::vector<uint8_t>& algoParameter, uint64_t authSubType, uint32_t algoVersion,
64 const std::vector<uint8_t>& challenge, const std::string &complexityReg) override
65 {
66 cout << "algoVersion is " << algoVersion << endl;
67 cout << " algoParameter len is " << algoParameter.size() << endl;
68 cout << " authSubType is " << authSubType << endl;
69 return onGetDataResult_;
70 }
71
OnTip(int32_t tip,const std::vector<uint8_t> & extraInfo)72 int32_t OnTip(int32_t tip, const std::vector<uint8_t>& extraInfo) override
73 {
74 return onTipResult_;
75 }
76
OnMessage(int32_t destRole,const std::vector<uint8_t> & msg)77 int32_t OnMessage(int32_t destRole, const std::vector<uint8_t>& msg) override
78 {
79 return onMessageResult_;
80 }
81
82 private:
83 int32_t onResultResult_;
84 int32_t onGetDataResult_;
85 int32_t onTipResult_;
86 int32_t onMessageResult_;
87 };
88
FillTestExecutorInfo(Parcel & parcel,ExecutorInfo & executorInfo)89 static void FillTestExecutorInfo(Parcel &parcel, ExecutorInfo &executorInfo)
90 {
91 executorInfo.sensorId = parcel.ReadUint16();
92 executorInfo.executorMatcher = parcel.ReadUint32();
93 executorInfo.executorRole = static_cast<HdiExecutorRole>(parcel.ReadInt32());
94 executorInfo.authType = static_cast<HdiAuthType>(parcel.ReadInt32());
95 executorInfo.esl = static_cast<ExecutorSecureLevel>(parcel.ReadInt32());
96 FillTestUint8Vector(parcel, executorInfo.publicKey);
97 FillTestUint8Vector(parcel, executorInfo.extraInfo);
98 }
99
FillTestIExecutorCallback(Parcel & parcel,sptr<IExecutorCallback> & callbackObj)100 static void FillTestIExecutorCallback(Parcel &parcel, sptr<IExecutorCallback> &callbackObj)
101 {
102 bool isNull = parcel.ReadBool();
103 if (isNull) {
104 callbackObj = nullptr;
105 } else {
106 callbackObj = new (std::nothrow) DummyIExecutorCallback(parcel.ReadInt32(),
107 parcel.ReadInt32(), parcel.ReadInt32(), parcel.ReadInt32(), parcel.ReadInt32());
108 if (callbackObj == nullptr) {
109 cout << "callbackObj construct fail" << endl;
110 }
111 }
112 }
113
FillTestGetPropertyTypeVector(Parcel & parcel,std::vector<int32_t> & types)114 void FillTestGetPropertyTypeVector(Parcel &parcel, std::vector<int32_t> &types)
115 {
116 std::vector<uint32_t> propertyTypeUint32;
117 FillTestUint32Vector(parcel, propertyTypeUint32);
118 for (const auto& val : propertyTypeUint32) {
119 types.push_back(static_cast<HdiGetPropertyType>(val));
120 }
121
122 cout << "success" << endl;
123 }
124
125 /**
126 * @tc.number: Security_IAM_PinAuth_HDI_FUNC_0101
127 * @tc.name: Test GetExecutorInfo
128 * @tc.size: MediumTest
129 * @tc.type: Function
130 * @tc.level: Level1
131 */
132 HWTEST_F(UserIamPinAuthTest, Security_IAM_PinAuth_HDI_FUNC_0101, Function | MediumTest | Level1)
133 {
134 cout << "start GetExecutorInfo" << endl;
135 ExecutorInfo executorInfo;
136 FillTestExecutorInfo(parcel, executorInfo);
137 int32_t ret = g_executorImpl.GetExecutorInfo(executorInfo);
138 cout << "ret is " << ret << endl;
139 ASSERT_EQ(ret != Expectedvalue, true);
140 }
141
142 /**
143 * @tc.number: Security_IAM_PinAuth_HDI_FUNC_0103
144 * @tc.name: Test OnRegisterFinish
145 * @tc.size: MediumTest
146 * @tc.type: Function
147 * @tc.level: Level1
148 */
149 HWTEST_F(UserIamPinAuthTest, Security_IAM_PinAuth_HDI_FUNC_0103, Function | MediumTest | Level1)
150 {
151 cout << "start OnRegisterFinish" << endl;
152 std::vector<uint64_t> templateIdList;
153 FillTestUint64Vector(parcel, templateIdList);
154 std::vector<uint8_t> frameworkPublicKey;
155 FillTestUint8Vector(parcel, frameworkPublicKey);
156 frameworkPublicKey.resize(32);
157 std::vector<uint8_t> extraInfo;
158 FillTestUint8Vector(parcel, extraInfo);
159 int32_t ret = g_executorImpl.OnRegisterFinish(templateIdList, frameworkPublicKey, extraInfo);
160 cout << "ret is " << ret << endl;
161 EXPECT_EQ(ret, 0);
162 }
163
164 /**
165 * @tc.number: Security_IAM_PinAuth_HDI_FUNC_0104
166 * @tc.name: Test OnSetData
167 * @tc.size: MediumTest
168 * @tc.type: Function
169 * @tc.level: Level1
170 */
171 HWTEST_F(UserIamPinAuthTest, Security_IAM_PinAuth_HDI_FUNC_0104, Function | MediumTest | Level1)
172 {
173 cout << "start OnSetData" << endl;
174 uint64_t scheduleId = parcel.ReadUint64();
175 uint64_t authSubType = parcel.ReadUint64();
176 std::vector<uint8_t> data;
177 FillTestUint8Vector(parcel, data);
178 uint32_t pinLength = 0;
179 int32_t resultCode = 0;
180 int32_t ret = g_executorImpl.SetData(scheduleId, authSubType, data, pinLength, resultCode);
181 cout << "ret is " << ret << endl;
182 ASSERT_EQ(ret != Expectedvalue, true);
183 }
184
185 /**
186 * @tc.number: Security_IAM_PinAuth_HDI_FUNC_0105
187 * @tc.name: Test Enroll
188 * @tc.size: MediumTest
189 * @tc.type: Function
190 * @tc.level: Level1
191 */
192 HWTEST_F(UserIamPinAuthTest, Security_IAM_PinAuth_HDI_FUNC_0105, Function | MediumTest | Level1)
193 {
194 cout << "start Enroll" << endl;
195 uint64_t scheduleId = parcel.ReadUint64();
196 std::vector<uint8_t> extraInfo;
197 sptr<IExecutorCallback> callbackObj;
198 FillTestIExecutorCallback(parcel, callbackObj);
199 int32_t ret = g_executorImpl.Enroll(scheduleId, extraInfo, callbackObj);
200 cout << "ret is " << ret << endl;
201 EXPECT_EQ(ret, 0);
202 }
203
204 /**
205 * @tc.number: Security_IAM_PinAuth_HDI_FUNC_0106
206 * @tc.name: Test Authenticate
207 * @tc.size: MediumTest
208 * @tc.type: Function
209 * @tc.level: Level1
210 */
211 HWTEST_F(UserIamPinAuthTest, Security_IAM_PinAuth_HDI_FUNC_0106, Function | MediumTest | Level1)
212 {
213 cout << "start Authenticate" << endl;
214 uint64_t scheduleId = parcel.ReadUint64();
215 uint64_t templateId = parcel.ReadUint64();
216 std::vector<uint64_t> templateIdList;
217 templateIdList.push_back(templateId);
218 std::vector<uint8_t> extraInfo;
219 FillTestUint8Vector(parcel, extraInfo);
220 sptr<IExecutorCallback> callbackObj;
221 FillTestIExecutorCallback(parcel, callbackObj);
222 int32_t ret = g_executorImpl.Authenticate(scheduleId, templateIdList, extraInfo, callbackObj);
223 cout << "ret is " << ret << endl;
224 ASSERT_EQ(ret, 0);
225 }
226
227 /**
228 * @tc.number: Security_IAM_PinAuth_HDI_FUNC_0107
229 * @tc.name: Test Delete
230 * @tc.size: MediumTest
231 * @tc.type: Function
232 * @tc.level: Level1
233 */
234 HWTEST_F(UserIamPinAuthTest, Security_IAM_PinAuth_HDI_FUNC_0107, Function | MediumTest | Level1)
235 {
236 cout << "start Delete" << endl;
237 uint64_t templateId = parcel.ReadUint64();
238 int32_t ret = g_executorImpl.Delete(templateId);
239 cout << "ret is " << ret << endl;
240 ASSERT_EQ(ret != Expectedvalue, true);
241 }
242
243 /**
244 * @tc.number: Security_IAM_PinAuth_HDI_FUNC_0108
245 * @tc.name: Test Cancel
246 * @tc.size: MediumTest
247 * @tc.type: Function
248 * @tc.level: Level1
249 */
250 HWTEST_F(UserIamPinAuthTest, Security_IAM_PinAuth_HDI_FUNC_0108, Function | MediumTest | Level1)
251 {
252 cout << "start Cancel" << endl;
253 uint64_t scheduleId = parcel.ReadUint64();
254 int32_t ret = g_executorImpl.Cancel(scheduleId);
255 cout << "ret is " << ret << endl;
256 EXPECT_EQ(ret, 0);
257 }
258
259 /**
260 * @tc.number: Security_IAM_PinAuth_HDI_FUNC_0110
261 * @tc.name: Test GetExecutorList
262 * @tc.size: MediumTest
263 * @tc.type: Function
264 * @tc.level: Level1
265 */
266 HWTEST_F(UserIamPinAuthTest, Security_IAM_PinAuth_HDI_FUNC_0110, Function | MediumTest | Level1)
267 {
268 cout << "start GetExecutorList" << endl;
269 PinAuthInterfaceService g_pinAuthInterFaceService;
270 std::vector<sptr<IAllInOneExecutor>> allInOneExecutors;
271 std::vector<sptr<IVerifier>> verifiers;
272 std::vector<sptr<ICollector>> collectors;
273 int32_t ret = g_pinAuthInterFaceService.GetExecutorList(allInOneExecutors, verifiers, collectors);
274 cout << "ret is " << ret << endl;
275 EXPECT_EQ(ret, 0);
276 }
277
278 /**
279 * @tc.number: Security_IAM_PinAuth_HDI_NEW_FUNC_0101
280 * @tc.name: Test GetProperty
281 * @tc.size: MediumTest
282 * @tc.type: Function
283 * @tc.level: Level1
284 */
285 HWTEST_F(UserIamPinAuthTest, Security_IAM_PinAuth_HDI_NEW_FUNC_0101, Function | MediumTest | Level1)
286 {
287 cout << "start GetProperty" << endl;
288 std::vector<uint64_t> templateIdList;
289 FillTestUint64Vector(parcel, templateIdList);
290 std::vector<int32_t> propertyTypes;
291 FillTestGetPropertyTypeVector(parcel, propertyTypes);
292 Property property;
293
294 int32_t ret = g_executorImpl.GetProperty(templateIdList, propertyTypes, property);
295
296 cout << "ret is " << ret << endl;
297 ASSERT_EQ(ret != Expectedvalue, true);
298 }
299
300 /**
301 * @tc.number: Security_IAM_PinAuth_HDI_NEW_FUNC_0102
302 * @tc.name: Test GetExecutorListV1_1
303 * @tc.size: MediumTest
304 * @tc.type: Function
305 * @tc.level: Level1
306 */
307 HWTEST_F(UserIamPinAuthTest, Security_IAM_PinAuth_HDI_NEW_FUNC_0102, Function | MediumTest | Level1)
308 {
309 cout << "start GetExecutorListV1_1" << endl;
310 PinAuthInterfaceService pin_Interface;
311
312 std::vector<sptr<IAllInOneExecutor>> allInOneExecutors;
313 std::vector<sptr<IVerifier>> verifiers;
314 std::vector<sptr<ICollector>> collectors;
315 int32_t ret = pin_Interface.GetExecutorList(allInOneExecutors, verifiers, collectors);
316
317 cout << "ret is " << ret << endl;
318 ASSERT_EQ(ret, 0);
319 }
320