• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "pin_auth_hdi.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::V1_0;
23 
24 static ExecutorImpl g_executorImpl(make_shared<OHOS::UserIam::PinAuth::PinAuth>());
25 static OHOS::Parcel parcel;
26 int32_t Expectedvalue = NULL;
27 
SetUpTestCase()28 void UserIamPinAuthTest::SetUpTestCase()
29 {
30 }
31 
TearDownTestCase()32 void UserIamPinAuthTest::TearDownTestCase()
33 {
34 }
35 
SetUp()36 void UserIamPinAuthTest::SetUp()
37 {
38 }
39 
TearDown()40 void UserIamPinAuthTest::TearDown()
41 {
42 }
43 
44 class DummyIExecutorCallback : public IExecutorCallback {
45 public:
DummyIExecutorCallback(int32_t onResultResult,int32_t onGetDataResult)46     DummyIExecutorCallback(int32_t onResultResult, int32_t onGetDataResult)
47         : onResultResult_(onResultResult), onGetDataResult_(onGetDataResult)
48     {
49     }
50 
OnResult(int32_t result,const std::vector<uint8_t> & extraInfo)51     int32_t OnResult(int32_t result, const std::vector<uint8_t> &extraInfo) override
52     {
53         cout << "result is " << result << " extraInfo len is " << extraInfo.size() << endl;
54         return onResultResult_;
55     }
56 
OnGetData(uint64_t scheduleId,const std::vector<uint8_t> & salt,uint64_t authSubType)57     int32_t OnGetData(uint64_t scheduleId, const std::vector<uint8_t> &salt, uint64_t authSubType) override
58     {
59         cout << "scheduleId is " << scheduleId << endl;
60         cout << " salt len is " << salt.size() << endl;
61         cout << " authSubType is " << authSubType << endl;
62         return onGetDataResult_;
63     }
64 
65 private:
66     int32_t onResultResult_;
67     int32_t onGetDataResult_;
68 };
69 
FillTestExecutorInfo(Parcel & parcel,ExecutorInfo & executorInfo)70 static void FillTestExecutorInfo(Parcel &parcel, ExecutorInfo &executorInfo)
71 {
72     executorInfo.sensorId = parcel.ReadUint16();
73     executorInfo.executorType = parcel.ReadUint32();
74     executorInfo.executorRole = static_cast<ExecutorRole>(parcel.ReadInt32());
75     executorInfo.authType = static_cast<AuthType>(parcel.ReadInt32());
76     executorInfo.esl = static_cast<ExecutorSecureLevel>(parcel.ReadInt32());
77     FillTestUint8Vector(parcel, executorInfo.publicKey);
78     FillTestUint8Vector(parcel, executorInfo.extraInfo);
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 }
88 
FillTestIExecutorCallback(Parcel & parcel,sptr<IExecutorCallback> & callbackObj)89 static void FillTestIExecutorCallback(Parcel &parcel, sptr<IExecutorCallback> &callbackObj)
90 {
91     bool isNull = parcel.ReadBool();
92     if (isNull) {
93         callbackObj = nullptr;
94     } else {
95         callbackObj = new (std::nothrow) DummyIExecutorCallback(parcel.ReadInt32(), parcel.ReadInt32());
96         if (callbackObj == nullptr) {
97             cout << "callbackObj construct fail" << endl;
98         }
99     }
100 }
101 
102 /**
103  * @tc.number: Security_IAM_PinAuth_HDI_FUNC_0101
104  * @tc.name: Test GetExecutorInfo
105  * @tc.size: MediumTest
106  * @tc.type: Function
107  * @tc.level: Level1
108  */
109 HWTEST_F(UserIamPinAuthTest, Security_IAM_PinAuth_HDI_FUNC_0101, Function | MediumTest | Level1)
110 {
111     cout << "start GetExecutorInfo" << endl;
112     ExecutorInfo executorInfo;
113     FillTestExecutorInfo(parcel, executorInfo);
114     int32_t ret = g_executorImpl.GetExecutorInfo(executorInfo);
115     cout << "ret is " << ret << endl;
116     ASSERT_EQ(ret != Expectedvalue, true);
117 }
118 
119 /**
120  * @tc.number: Security_IAM_PinAuth_HDI_FUNC_0102
121  * @tc.name: Test GetTemplateInfo
122  * @tc.size: MediumTest
123  * @tc.type: Function
124  * @tc.level: Level1
125  */
126 HWTEST_F(UserIamPinAuthTest, Security_IAM_PinAuth_HDI_FUNC_0102, Function | MediumTest | Level1)
127 {
128     cout << "start GetTemplateInfo" << endl;
129     uint64_t templateId = parcel.ReadUint64();
130     TemplateInfo templateInfo;
131     FillTestTemplateInfo(parcel, templateInfo);
132     int32_t ret = g_executorImpl.GetTemplateInfo(templateId, templateInfo);
133     cout << "ret is " << ret << endl;
134     ASSERT_EQ(ret != Expectedvalue, true);
135 }
136 
137 /**
138  * @tc.number: Security_IAM_PinAuth_HDI_FUNC_0103
139  * @tc.name: Test OnRegisterFinish
140  * @tc.size: MediumTest
141  * @tc.type: Function
142  * @tc.level: Level1
143  */
144 HWTEST_F(UserIamPinAuthTest, Security_IAM_PinAuth_HDI_FUNC_0103, Function | MediumTest | Level1)
145 {
146     cout << "start OnRegisterFinish" << endl;
147     std::vector<uint64_t> templateIdList;
148     FillTestUint64Vector(parcel, templateIdList);
149     std::vector<uint8_t> frameworkPublicKey;
150     FillTestUint8Vector(parcel, frameworkPublicKey);
151     std::vector<uint8_t> extraInfo;
152     FillTestUint8Vector(parcel, extraInfo);
153     int32_t ret = g_executorImpl.OnRegisterFinish(templateIdList, frameworkPublicKey, extraInfo);
154     cout << "ret is " << ret << endl;
155     EXPECT_EQ(ret, 0);
156 }
157 
158 /**
159  * @tc.number: Security_IAM_PinAuth_HDI_FUNC_0104
160  * @tc.name: Test OnSetData
161  * @tc.size: MediumTest
162  * @tc.type: Function
163  * @tc.level: Level1
164  */
165 HWTEST_F(UserIamPinAuthTest, Security_IAM_PinAuth_HDI_FUNC_0104, Function | MediumTest | Level1)
166 {
167     cout << "start OnSetData" << endl;
168     uint64_t scheduleId = parcel.ReadUint64();
169     uint64_t authSubType = parcel.ReadUint64();
170     std::vector<uint8_t> data;
171     FillTestUint8Vector(parcel, data);
172     int32_t ret = g_executorImpl.OnSetData(scheduleId, authSubType, data);
173     cout << "ret is " << ret << endl;
174     ASSERT_EQ(ret != Expectedvalue, true);
175 }
176 
177 /**
178  * @tc.number: Security_IAM_PinAuth_HDI_FUNC_0105
179  * @tc.name: Test Enroll
180  * @tc.size: MediumTest
181  * @tc.type: Function
182  * @tc.level: Level1
183  */
184 HWTEST_F(UserIamPinAuthTest, Security_IAM_PinAuth_HDI_FUNC_0105, Function | MediumTest | Level1)
185 {
186     cout << "start Enroll" << endl;
187     uint64_t scheduleId = parcel.ReadUint64();
188     std::vector<uint8_t> extraInfo;
189     sptr<IExecutorCallback> callbackObj;
190     FillTestIExecutorCallback(parcel, callbackObj);
191     int32_t ret = g_executorImpl.Enroll(scheduleId, extraInfo, callbackObj);
192     cout << "ret is " << ret << endl;
193     EXPECT_EQ(ret, 0);
194 }
195 
196 /**
197  * @tc.number: Security_IAM_PinAuth_HDI_FUNC_0106
198  * @tc.name: Test Authenticate
199  * @tc.size: MediumTest
200  * @tc.type: Function
201  * @tc.level: Level1
202  */
203 HWTEST_F(UserIamPinAuthTest, Security_IAM_PinAuth_HDI_FUNC_0106, Function | MediumTest | Level1)
204 {
205     cout << "start Authenticate" << endl;
206     uint64_t scheduleId = parcel.ReadUint64();
207     uint64_t templateId = parcel.ReadUint64();
208     std::vector<uint8_t> extraInfo;
209     FillTestUint8Vector(parcel, extraInfo);
210     sptr<IExecutorCallback> callbackObj;
211     FillTestIExecutorCallback(parcel, callbackObj);
212     int32_t ret = g_executorImpl.Authenticate(scheduleId, templateId, extraInfo, callbackObj);
213     cout << "ret is " << ret << endl;
214     ASSERT_EQ(ret, 0);
215 }
216 
217 /**
218  * @tc.number: Security_IAM_PinAuth_HDI_FUNC_0107
219  * @tc.name: Test Delete
220  * @tc.size: MediumTest
221  * @tc.type: Function
222  * @tc.level: Level1
223  */
224 HWTEST_F(UserIamPinAuthTest, Security_IAM_PinAuth_HDI_FUNC_0107, Function | MediumTest | Level1)
225 {
226     cout << "start Delete" << endl;
227     uint64_t templateId = parcel.ReadUint64();
228     int32_t ret = g_executorImpl.Delete(templateId);
229     cout << "ret is " << ret << endl;
230     ASSERT_EQ(ret != Expectedvalue, true);
231 }
232 
233 /**
234  * @tc.number: Security_IAM_PinAuth_HDI_FUNC_0108
235  * @tc.name: Test Cancel
236  * @tc.size: MediumTest
237  * @tc.type: Function
238  * @tc.level: Level1
239  */
240 HWTEST_F(UserIamPinAuthTest, Security_IAM_PinAuth_HDI_FUNC_0108, Function | MediumTest | Level1)
241 {
242     cout << "start Cancel" << endl;
243     uint64_t scheduleId = parcel.ReadUint64();
244     int32_t ret = g_executorImpl.Cancel(scheduleId);
245     cout << "ret is " << ret << endl;
246     EXPECT_EQ(ret, 0);
247 }
248 
249 /**
250  * @tc.number: Security_IAM_PinAuth_HDI_FUNC_0109
251  * @tc.name: Test SendCommand
252  * @tc.size: MediumTest
253  * @tc.type: Function
254  * @tc.level: Level1
255  */
256 HWTEST_F(UserIamPinAuthTest, Security_IAM_PinAuth_HDI_FUNC_0109, Function | MediumTest | Level1)
257 {
258     cout << "start SendCommand" << endl;
259     int32_t commandId = parcel.ReadInt32();
260     std::vector<uint8_t> extraInfo;
261     FillTestUint8Vector(parcel, extraInfo);
262     sptr<IExecutorCallback> callbackObj;
263     FillTestIExecutorCallback(parcel, callbackObj);
264     int32_t ret = g_executorImpl.SendCommand(commandId, extraInfo, callbackObj);
265     cout << "ret is " << ret << endl;
266     EXPECT_EQ(ret, 0);
267 }
268