1 /*
2  * Copyright (C) 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 "iam_ptr.h"
17 
18 #include "credential_info_impl.h"
19 #include "enrollment_impl.h"
20 #include "resource_node_pool.h"
21 #include "mock_iuser_auth_interface.h"
22 #include "mock_resource_node.h"
23 #include "mock_schedule_node_callback.h"
24 
25 namespace OHOS {
26 namespace UserIam {
27 namespace UserAuth {
28 using namespace testing;
29 using namespace testing::ext;
30 class EnrollmentImplTest : public testing::Test {
31 public:
32     static void SetUpTestCase();
33 
34     static void TearDownTestCase();
35 
36     void SetUp() override;
37 
38     void TearDown() override;
39 };
40 
SetUpTestCase()41 void EnrollmentImplTest::SetUpTestCase()
42 {
43 }
44 
TearDownTestCase()45 void EnrollmentImplTest::TearDownTestCase()
46 {
47 }
48 
SetUp()49 void EnrollmentImplTest::SetUp()
50 {
51     MockIUserAuthInterface::Holder::GetInstance().Reset();
52 }
53 
TearDown()54 void EnrollmentImplTest::TearDown()
55 {
56     MockIUserAuthInterface::Holder::GetInstance().Reset();
57 }
58 
59 HWTEST_F(EnrollmentImplTest, EnrollmentHdiError, TestSize.Level0)
60 {
61     Enrollment::EnrollmentPara para = {};
62     para.userId = 0x11;
63     para.callerName = "com.ohos.test";
64     para.sdkVersion = 11;
65     para.authType = FACE;
66     auto mock = MockIUserAuthInterface::Holder::GetInstance().Get();
67     EXPECT_CALL(*mock, BeginEnrollment(_, _, _)).WillRepeatedly(Return(1));
68 
69     auto enrollment = std::make_shared<EnrollmentImpl>(para);
70     std::vector<std::shared_ptr<ScheduleNode>> scheduleList;
71     EXPECT_FALSE(enrollment->Start(scheduleList, nullptr));
72 }
73 
74 HWTEST_F(EnrollmentImplTest, EnrollmentHdiEmpty, TestSize.Level0)
75 {
76     Enrollment::EnrollmentPara para = {};
77     para.userId = 0x11;
78     para.callerName = "com.ohos.test";
79     para.sdkVersion = 11;
80     para.authType = FACE;
81 
82     auto mock = MockIUserAuthInterface::Holder::GetInstance().Get();
83     EXPECT_CALL(*mock, BeginEnrollment(_, _, _)).WillRepeatedly(Return(0));
84 
85     auto enroll = std::make_shared<EnrollmentImpl>(para);
86     std::vector<std::shared_ptr<ScheduleNode>> scheduleList;
87     EXPECT_FALSE(enroll->Start(scheduleList, nullptr));
88 }
89 
90 /**
91  * @tc.name: enroll_update
92  * @tc.desc: verify hdi enroll
93  * @tc.type: FUNC
94  * @tc.require: issueI5NXMW
95  */
96 HWTEST_F(EnrollmentImplTest, EnrollmentUpdateHdiError, TestSize.Level0)
97 {
98     Enrollment::EnrollmentPara para = {};
99     para.userId = 0x11;
100     para.callerName = "com.ohos.test";
101     para.sdkVersion = 11;
102     para.authType = FACE;
103 
104     auto mock = MockIUserAuthInterface::Holder::GetInstance().Get();
105     EXPECT_CALL(*mock, UpdateEnrollmentResult(para.userId, _, _)).WillRepeatedly(Return(1));
106 
107     auto enroll = std::make_shared<EnrollmentImpl>(para);
108     std::vector<uint8_t> scheduleResult = {1, 2, 3};
109     uint64_t credentialId = 0;
110     std::shared_ptr<CredentialInfoInterface> info = nullptr;
111     std::shared_ptr<UpdatePinParamInterface> pinInfo = nullptr;
112     std::optional<uint64_t> secUserId = std::nullopt;
113     EXPECT_FALSE(enroll->Update(scheduleResult, credentialId, info, pinInfo, secUserId));
114 }
115 
116 HWTEST_F(EnrollmentImplTest, EnrollmentUpdateHdiSuccessful_001, TestSize.Level0)
117 {
118     Enrollment::EnrollmentPara para = {};
119     para.userId = 0x11;
120     para.callerName = "com.ohos.test";
121     para.sdkVersion = 11;
122     para.authType = FACE;
123     constexpr uint64_t credentialIdRet = 0x12;
124     std::vector<uint8_t> scheduleResult = {1, 2, 3};
125     auto mock = MockIUserAuthInterface::Holder::GetInstance().Get();
__anonc2056a850102(HdiEnrollResultInfo &infoRet) 126     auto fillUpInfos = [](HdiEnrollResultInfo &infoRet) {
127         HdiCredentialInfo oldInfo = {
128             .credentialId = 1,
129             .executorIndex = 2,
130             .templateId = 3,
131             .authType = static_cast<HdiAuthType>(0),
132             .executorMatcher = 5,
133             .executorSensorHint = 6,
134         };
135         HdiEnrollResultInfo info = {
136             .credentialId = credentialIdRet,
137             .oldInfo = oldInfo,
138         };
139         infoRet = info;
140     };
141     EXPECT_CALL(*mock, UpdateEnrollmentResult(para.userId, _, _))
142         .WillRepeatedly(DoAll(WithArg<2>(fillUpInfos), Return(0)));
143 
144     auto enroll = std::make_shared<EnrollmentImpl>(para);
145     enroll->SetIsUpdate(true);
146     HdiCredentialInfo oldInfo = {};
147     std::shared_ptr<CredentialInfoInterface> info = std::make_shared<CredentialInfoImpl>(para.userId, oldInfo);
148     uint64_t credentialId = 0;
149     std::shared_ptr<UpdatePinParamInterface> pinInfo = nullptr;
150     std::optional<uint64_t> secUserId = std::nullopt;
151     EXPECT_TRUE(enroll->Update(scheduleResult, credentialId, info, pinInfo, secUserId));
152 
153     // test return values
154     EXPECT_EQ(credentialId, credentialIdRet);
155     EXPECT_EQ(info->GetCredentialId(), 1U);
156     EXPECT_EQ(info->GetAuthType(), static_cast<AuthType>(0));
157     EXPECT_EQ(info->GetExecutorIndex(), 2U);
158     EXPECT_EQ(info->GetUserId(), 0x11);
159     EXPECT_EQ(info->GetTemplateId(), 3U);
160     EXPECT_EQ(info->GetExecutorMatcher(), 5U);
161     EXPECT_EQ(info->GetExecutorSensorHint(), 6U);
162 }
163 
164 HWTEST_F(EnrollmentImplTest, EnrollmentUpdateHdiSuccessful_002, TestSize.Level0)
165 {
166     Enrollment::EnrollmentPara para = {};
167     para.userId = 1206;
168     para.callerName = "com.ohos.test";
169     para.sdkVersion = 11;
170     para.authType = PIN;
171     auto mock = MockIUserAuthInterface::Holder::GetInstance().Get();
172     EXPECT_CALL(*mock, UpdateEnrollmentResult(_, _, _)).WillRepeatedly(Return(0));
173     auto enroll = std::make_shared<EnrollmentImpl>(para);
174     enroll->SetIsUpdate(false);
175 
176     std::vector<uint8_t> scheduleResult = {1, 2, 3};
177     std::shared_ptr<CredentialInfoInterface> info = nullptr;
178     std::shared_ptr<UpdatePinParamInterface> pinInfo = nullptr;
179     uint64_t credentialId = 0;
180     std::optional<uint64_t> secUserId = std::nullopt;
181     EXPECT_TRUE(enroll->Update(scheduleResult, credentialId, info, pinInfo, secUserId));
182 
183     EXPECT_CALL(*mock, GetUserInfo(_, _, _, _)).WillRepeatedly(Return(1));
184     enroll = std::make_shared<EnrollmentImpl>(para);
185     EXPECT_FALSE(enroll->Update(scheduleResult, credentialId, info, pinInfo, secUserId));
186 }
187 
188 HWTEST_F(EnrollmentImplTest, EnrollmentImplTestStart_001, TestSize.Level0)
189 {
190     Enrollment::EnrollmentPara para = {};
191     para.userId = 0x11;
192     para.callerName = "com.ohos.test";
193     para.sdkVersion = 11;
194     para.authType = FACE;
195     constexpr uint64_t executorIndex = 60;
196 
197     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
198     EXPECT_NE(mockHdi, nullptr);
199     EXPECT_CALL(*mockHdi, CancelEnrollment(_))
200         .Times(2)
201         .WillOnce(Return(HDF_SUCCESS))
202         .WillOnce(Return(HDF_FAILURE));
203     EXPECT_CALL(*mockHdi, BeginEnrollment(_, _, _))
204         .WillRepeatedly(
205             [](const std::vector<uint8_t> &authToken, const HdiEnrollParam &param,
__anonc2056a850202(const std::vector<uint8_t> &authToken, const HdiEnrollParam &param, HdiScheduleInfo &info) 206                 HdiScheduleInfo &info) {
207                 info.authType = HdiAuthType::FACE;
208                 info.executorMatcher = 10;
209                 info.executorIndexes.push_back(60);
210                 std::vector<uint8_t> executorMessages;
211                 executorMessages.resize(1);
212                 info.executorMessages.push_back(executorMessages);
213                 info.scheduleId = 20;
214                 info.scheduleMode = HdiScheduleMode::IDENTIFY;
215                 info.templateIds.push_back(30);
216                 return HDF_SUCCESS;
217             }
218         );
219     auto resourceNode = MockResourceNode::CreateWithExecuteIndex(executorIndex, FACE, ALL_IN_ONE);
220     EXPECT_NE(resourceNode, nullptr);
221     EXPECT_TRUE(ResourceNodePool::Instance().Insert(resourceNode));
222     auto enroll = std::make_shared<EnrollmentImpl>(para);
223     EXPECT_NE(enroll, nullptr);
224     std::vector<std::shared_ptr<ScheduleNode>> scheduleList;
225     auto callback = Common::MakeShared<MockScheduleNodeCallback>();
226     EXPECT_NE(callback, nullptr);
227     EXPECT_TRUE(enroll->Start(scheduleList, callback));
228     EXPECT_TRUE(enroll->Cancel());
229 
230     EXPECT_TRUE(enroll->Start(scheduleList, callback));
231     EXPECT_FALSE(enroll->Cancel());
232 
233     EXPECT_TRUE(ResourceNodePool::Instance().Delete(executorIndex));
234 }
235 
236 HWTEST_F(EnrollmentImplTest, EnrollmentImplTestStart_002, TestSize.Level0)
237 {
238     Enrollment::EnrollmentPara para = {};
239     para.userId = 34567;
240     para.callerName = "com.ohos.test";
241     para.sdkVersion = 11;
242     para.authType = PIN;
243     auto mock = MockIUserAuthInterface::Holder::GetInstance().Get();
244     EXPECT_CALL(*mock, BeginEnrollment(_, _, _)).WillRepeatedly(Return(1));
245     EXPECT_CALL(*mock, GetUserInfo(_, _, _, _))
246         .WillOnce(Return(1))
247         .WillRepeatedly(
248             [](int32_t userId, uint64_t &secureUid, int32_t &pinSubType,
__anonc2056a850302(int32_t userId, uint64_t &secureUid, int32_t &pinSubType, std::vector<HdiEnrolledInfo> &infos) 249                 std::vector<HdiEnrolledInfo> &infos) {
250                 secureUid = 1;
251                 pinSubType = static_cast<HdiPinSubType>(10000);
252                 HdiEnrolledInfo info = {
253                     .enrolledId = 200,
254                     .authType = static_cast<HdiAuthType>(1),
255                 };
256                 infos.push_back(info);
257                 return 0;
258             }
259         );
260 
261     auto enroll = std::make_shared<EnrollmentImpl>(para);
262     std::vector<std::shared_ptr<ScheduleNode>> scheduleList;
263     auto callback = Common::MakeShared<MockScheduleNodeCallback>();
264     EXPECT_FALSE(enroll->Start(scheduleList, callback));
265 
266     enroll->SetIsUpdate(true);
267     EXPECT_FALSE(enroll->Start(scheduleList, callback));
268 
269     enroll = std::make_shared<EnrollmentImpl>(para);
270     enroll->SetIsUpdate(true);
271     EXPECT_FALSE(enroll->Start(scheduleList, callback));
272     EXPECT_FALSE(enroll->Start(scheduleList, callback));
273 }
274 } // namespace UserAuth
275 } // namespace UserIam
276 } // namespace OHOS