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 HdiProperty = 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 Expectedvalue1 = 0;
29
SetUpTestCase()30 void UserIamPinAuthTwoTest::SetUpTestCase()
31 {
32 }
33
TearDownTestCase()34 void UserIamPinAuthTwoTest::TearDownTestCase()
35 {
36 }
37
SetUp()38 void UserIamPinAuthTwoTest::SetUp()
39 {
40 }
41
TearDown()42 void UserIamPinAuthTwoTest::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
FillTestIExecutorCallback(Parcel &parcel, sptr<IExecutorCallback> &callbackObj)87 static void FillTestIExecutorCallback(Parcel &parcel, sptr<IExecutorCallback> &callbackObj)
88 {
89 bool isNull = parcel.ReadBool();
90 if (isNull) {
91 callbackObj = nullptr;
92 } else {
93 callbackObj = new (std::nothrow) DummyIExecutorCallback(parcel.ReadInt32(),
94 parcel.ReadInt32(), parcel.ReadInt32(), parcel.ReadInt32(), parcel.ReadInt32());
95 if (callbackObj == nullptr) {
96 cout << "callbackObj construct fail" << endl;
97 }
98 }
99 }
100
101 /**
102 * @tc.number: Security_IAM_PinAuth_HDI_NEW_FUNC_0104
103 * @tc.name: Test Authenticate
104 * @tc.size: MediumTest
105 * @tc.type: Function
106 * @tc.level: Level1
107 */
HWTEST_F(UserIamPinAuthTwoTest, Security_IAM_PinAuth_HDI_NEW_FUNC_0104, Function | MediumTest | Level1)108 HWTEST_F(UserIamPinAuthTwoTest, Security_IAM_PinAuth_HDI_NEW_FUNC_0104, Function | MediumTest | Level1)
109 {
110 cout << "start Authenticate" << endl;
111
112 uint64_t scheduleId = parcel.ReadUint64();
113 uint64_t templateId = parcel.ReadUint64();
114 std::vector<uint64_t> templateIdList;
115 templateIdList.push_back(templateId);
116 std::vector<uint8_t> extraInfo;
117 FillTestUint8Vector(parcel, extraInfo);
118 sptr<IExecutorCallback> callbackObj;
119 FillTestIExecutorCallback(parcel, callbackObj);
120 int32_t ret = g_executorImpl.Authenticate(scheduleId, templateIdList, extraInfo, callbackObj);
121
122 cout << "ret is " << ret << endl;
123 ASSERT_EQ(ret != Expectedvalue1, false);
124 }