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