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 "user_idm_service_test.h"
17 
18 #include <future>
19 
20 #include "iam_common_defines.h"
21 #include "iam_ptr.h"
22 
23 #include "context_pool.h"
24 #include "executor_messenger_service.h"
25 #include "mock_context.h"
26 #include "mock_ipc_common.h"
27 #include "mock_iuser_auth_interface.h"
28 #include "mock_resource_node.h"
29 #include "mock_user_idm_callback.h"
30 #include "resource_node_pool.h"
31 #include "user_idm_service.h"
32 
33 namespace OHOS {
34 namespace UserIam {
35 namespace UserAuth {
36 using namespace testing;
37 using namespace testing::ext;
SetUpTestCase()38 void UserIdmServiceTest::SetUpTestCase()
39 {
40 }
41 
TearDownTestCase()42 void UserIdmServiceTest::TearDownTestCase()
43 {
44 }
45 
SetUp()46 void UserIdmServiceTest::SetUp()
47 {
48     MockIUserAuthInterface::Holder::GetInstance().Reset();
49 }
50 
TearDown()51 void UserIdmServiceTest::TearDown()
52 {
53     MockIUserAuthInterface::Holder::GetInstance().Reset();
54 }
55 
56 HWTEST_F(UserIdmServiceTest, UserIdmServiceOpenSession_001, TestSize.Level0)
57 {
58     UserIdmService service(123123, true);
59     int32_t testUserId = 0;
60     std::vector<uint8_t> testChallenge = {1, 2, 3, 4};
61     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
62     EXPECT_NE(mockHdi, nullptr);
63     EXPECT_CALL(*mockHdi, OpenSession(_, _)).Times(1);
64     ON_CALL(*mockHdi, OpenSession)
65         .WillByDefault(
__anone7116c9c0102(int32_t userId, std::vector<uint8_t> &challenge) 66             [&testChallenge](int32_t userId, std::vector<uint8_t> &challenge) {
67                 challenge = testChallenge;
68                 return HDF_SUCCESS;
69             }
70         );
71     std::vector<uint8_t> challenge;
72     IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
73     int32_t ret = service.OpenSession(testUserId, challenge);
74     EXPECT_EQ(ret, SUCCESS);
75     EXPECT_THAT(challenge, ElementsAreArray(testChallenge));
76     IpcCommon::DeleteAllPermission();
77 }
78 
79 HWTEST_F(UserIdmServiceTest, UserIdmServiceOpenSession_002, TestSize.Level0)
80 {
81     UserIdmService service(123123, true);
82     int32_t testUserId = 0;
83     std::vector<uint8_t> testChallenge = {1, 2, 3, 4};
84 
85     std::vector<uint8_t> challenge;
86     int32_t ret = service.OpenSession(testUserId, challenge);
87     EXPECT_EQ(ret, CHECK_PERMISSION_FAILED);
88 
89     IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
90     auto context = Common::MakeShared<MockContext>();
91     EXPECT_NE(context, nullptr);
92     EXPECT_CALL(*context, GetContextType()).WillRepeatedly(Return(CONTEXT_ENROLL));
93     EXPECT_CALL(*context, GetContextId()).WillRepeatedly(Return(2345));
94     EXPECT_CALL(*context, Stop()).WillRepeatedly(Return(true));
95     EXPECT_TRUE(ContextPool::Instance().Insert(context));
96 
97     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
98     EXPECT_NE(mockHdi, nullptr);
99     EXPECT_CALL(*mockHdi, OpenSession(_, _))
100         .Times(3)
101         .WillOnce(Return(HDF_SUCCESS))
102         .WillOnce(Return(HDF_SUCCESS))
103         .WillOnce(Return(HDF_FAILURE));
104 
105     EXPECT_EQ(service.OpenSession(testUserId, challenge), SUCCESS);
106     EXPECT_EQ(service.OpenSession(testUserId, challenge), SUCCESS);
107     EXPECT_EQ(service.OpenSession(testUserId, challenge), GENERAL_ERROR);
108 
109     IpcCommon::DeleteAllPermission();
110 }
111 
112 HWTEST_F(UserIdmServiceTest, UserIdmServiceCloseSession, TestSize.Level0)
113 {
114     UserIdmService service(123123, true);
115     int32_t testUserId = 3546;
116 
117     service.CloseSession(testUserId);
118     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
119     EXPECT_NE(mockHdi, nullptr);
120     EXPECT_CALL(*mockHdi, CloseSession(_))
121         .Times(2)
122         .WillOnce(Return(HDF_SUCCESS))
123         .WillOnce(Return(HDF_FAILURE));
124     IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
125     service.CloseSession(testUserId);
126     service.CloseSession(testUserId);
127     IpcCommon::DeleteAllPermission();
128 }
129 
130 HWTEST_F(UserIdmServiceTest, UserIdmServiceGetCredentialInfo001, TestSize.Level0)
131 {
132     UserIdmService service(123123, true);
133     int32_t testUserId = 0;
134     AuthType testAuthType = PIN;
135 
136     sptr<MockIdmGetCredentialInfoCallback> testCallback(new (std::nothrow) MockIdmGetCredentialInfoCallback());
137     EXPECT_NE(testCallback, nullptr);
138     EXPECT_CALL(*testCallback, OnCredentialInfos(_)).Times(2);
139 
140     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
141     EXPECT_NE(mockHdi, nullptr);
142     EXPECT_CALL(*mockHdi, GetCredential(_, _, _)).WillOnce(Return(HDF_FAILURE));
143     int32_t ret = service.GetCredentialInfo(testUserId, testAuthType, testCallback);
144     EXPECT_EQ(ret, CHECK_PERMISSION_FAILED);
145     IpcCommon::AddPermission(USE_USER_IDM_PERMISSION);
146     ret = service.GetCredentialInfo(testUserId, testAuthType, testCallback);
147     EXPECT_EQ(ret, GENERAL_ERROR);
148     IpcCommon::DeleteAllPermission();
149 }
150 
151 HWTEST_F(UserIdmServiceTest, UserIdmServiceGetCredentialInfo002, TestSize.Level0)
152 {
153     UserIdmService service(123123, true);
154     int32_t testUserId = 0;
155     AuthType testAuthType = PIN;
156     sptr<IdmGetCredInfoCallbackInterface> testCallback(nullptr);
157     int32_t ret = service.GetCredentialInfo(testUserId, testAuthType, testCallback);
158     EXPECT_EQ(ret, INVALID_PARAMETERS);
159 }
160 
161 HWTEST_F(UserIdmServiceTest, UserIdmServiceGetCredentialInfo003, TestSize.Level0)
162 {
163     UserIdmService service(123123, true);
164     int32_t testUserId = 0;
165     AuthType testAuthType = PIN;
166     sptr<MockIdmGetCredentialInfoCallback> testCallback(new (std::nothrow) MockIdmGetCredentialInfoCallback());
167     EXPECT_NE(testCallback, nullptr);
168     EXPECT_CALL(*testCallback, OnCredentialInfos(_)).Times(2);
169 
170     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
171     EXPECT_NE(mockHdi, nullptr);
172     EXPECT_CALL(*mockHdi, GetCredential(_, _, _)).Times(2);
173     ON_CALL(*mockHdi, GetCredential)
174         .WillByDefault(
__anone7116c9c0202(int32_t userId, int32_t authType, std::vector<HdiCredentialInfo> &infos) 175             [](int32_t userId, int32_t authType, std::vector<HdiCredentialInfo> &infos) {
176                 HdiCredentialInfo tempInfo = {
177                     .credentialId = 1,
178                     .executorIndex = 2,
179                     .templateId = 3,
180                     .authType = static_cast<HdiAuthType>(1),
181                     .executorMatcher = 2,
182                     .executorSensorHint = 3,
183                 };
184                 infos.push_back(tempInfo);
185                 return HDF_SUCCESS;
186             }
187         );
188     IpcCommon::AddPermission(USE_USER_IDM_PERMISSION);
189     int32_t ret = service.GetCredentialInfo(testUserId, testAuthType, testCallback);
190     EXPECT_EQ(ret, SUCCESS);
191     ret = service.GetCredentialInfo(testUserId, testAuthType, testCallback);
192     EXPECT_EQ(ret, SUCCESS);
193     IpcCommon::DeleteAllPermission();
194 }
195 
196 HWTEST_F(UserIdmServiceTest, UserIdmServiceGetCredentialInfo004, TestSize.Level0)
197 {
198     UserIdmService service(123123, true);
199     int32_t testUserId = 0;
200     AuthType testAuthType = PIN;
201     sptr<MockIdmGetCredentialInfoCallback> testCallback(new (std::nothrow) MockIdmGetCredentialInfoCallback());
202     EXPECT_NE(testCallback, nullptr);
203     EXPECT_CALL(*testCallback, OnCredentialInfos(_)).Times(1);
204 
205     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
206     EXPECT_NE(mockHdi, nullptr);
207     EXPECT_CALL(*mockHdi, GetCredential(_, _, _))
208         .WillOnce(
__anone7116c9c0302(int32_t userId, int32_t authType, std::vector<HdiCredentialInfo> &infos) 209             [](int32_t userId, int32_t authType, std::vector<HdiCredentialInfo> &infos) {
210                 HdiCredentialInfo tempInfo = {
211                     .credentialId = 1,
212                     .executorIndex = 2,
213                     .templateId = 3,
214                     .authType = static_cast<HdiAuthType>(2),
215                     .executorMatcher = 2,
216                     .executorSensorHint = 3,
217                 };
218                 infos.push_back(tempInfo);
219                 return HDF_SUCCESS;
220             }
221         );
222 
223     IpcCommon::AddPermission(USE_USER_IDM_PERMISSION);
224     int32_t ret = service.GetCredentialInfo(testUserId, testAuthType, testCallback);
225     EXPECT_EQ(ret, SUCCESS);
226     IpcCommon::DeleteAllPermission();
227 }
228 
229 HWTEST_F(UserIdmServiceTest, UserIdmServiceGetSecInfo001, TestSize.Level0)
230 {
231     UserIdmService service(123123, true);
232     int32_t testUserId = 0;
233 
234     sptr<MockIdmGetSecureUserInfoCallback> testCallback(new (std::nothrow) MockIdmGetSecureUserInfoCallback());
235     EXPECT_NE(testCallback, nullptr);
236     EXPECT_CALL(*testCallback, OnSecureUserInfo(_)).Times(2);
237 
238     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
239     EXPECT_NE(mockHdi, nullptr);
240     EXPECT_CALL(*mockHdi, GetUserInfo(_, _, _, _)).WillOnce(Return(HDF_FAILURE));
241 
242     int32_t ret = service.GetSecInfo(testUserId, testCallback);
243     EXPECT_EQ(ret, CHECK_PERMISSION_FAILED);
244     IpcCommon::AddPermission(USE_USER_IDM_PERMISSION);
245     ret = service.GetSecInfo(testUserId, testCallback);
246     EXPECT_EQ(ret, GENERAL_ERROR);
247     IpcCommon::DeleteAllPermission();
248 }
249 
250 HWTEST_F(UserIdmServiceTest, UserIdmServiceGetSecInfo002, TestSize.Level0)
251 {
252     UserIdmService service(123123, true);
253     int32_t testUserId = 0;
254     sptr<MockIdmGetSecureUserInfoCallback> testCallback(nullptr);
255     IpcCommon::AddPermission(USE_USER_IDM_PERMISSION);
256     int32_t ret = service.GetSecInfo(testUserId, testCallback);
257     EXPECT_EQ(ret, INVALID_PARAMETERS);
258 
259     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
260     EXPECT_NE(mockHdi, nullptr);
261     EXPECT_CALL(*mockHdi, GetUserInfo(_, _, _, _))
262         .Times(2)
263         .WillOnce(Return(HDF_FAILURE))
264         .WillOnce(
__anone7116c9c0402(int32_t userId, uint64_t &secureUid, int32_t& pinSubType, std::vector<HdiEnrolledInfo> &infos) 265             [](int32_t userId, uint64_t &secureUid, int32_t& pinSubType, std::vector<HdiEnrolledInfo> &infos) {
266                 HdiEnrolledInfo info = {
267                     .enrolledId = 0,
268                     .authType = static_cast<HdiAuthType>(1),
269                 };
270                 infos.push_back(info);
271                 pinSubType = static_cast<HdiPinSubType>(10000);
272                 secureUid = 4542;
273                 return HDF_SUCCESS;
274             }
275         );
276 
277     testCallback = sptr<MockIdmGetSecureUserInfoCallback>(new (std::nothrow) MockIdmGetSecureUserInfoCallback());
278     EXPECT_NE(testCallback, nullptr);
279     EXPECT_CALL(*testCallback, OnSecureUserInfo(_)).Times(2);
280     ret = service.GetSecInfo(testUserId, testCallback);
281     EXPECT_EQ(ret, GENERAL_ERROR);
282     ret = service.GetSecInfo(testUserId, testCallback);
283     EXPECT_EQ(ret, SUCCESS);
284     IpcCommon::DeleteAllPermission();
285 }
286 
287 HWTEST_F(UserIdmServiceTest, UserIdmServiceAddCredential001, TestSize.Level0)
288 {
289     UserIdmService service(123123, true);
290     int32_t testUserId = 15457;
291     UserIdmInterface::CredentialPara testCredPara = {};
292     testCredPara.authType = PIN;
293     testCredPara.pinType = PIN_SIX;
294     testCredPara.token = {1, 2, 3, 4};
295     sptr<IdmCallbackInterface> testCallback(nullptr);
296     service.AddCredential(testUserId, testCredPara, testCallback, false);
297 }
298 
299 HWTEST_F(UserIdmServiceTest, UserIdmServiceAddCredential002, TestSize.Level0)
300 {
301     UserIdmService service(123123, true);
302     int32_t testUserId = 15457;
303     UserIdmInterface::CredentialPara testCredPara = {};
304     testCredPara.authType = PIN;
305     testCredPara.pinType = PIN_SIX;
306     testCredPara.token = {1, 2, 3, 4};
307     sptr<MockIdmCallback> testCallback(new (std::nothrow) MockIdmCallback());
308     EXPECT_NE(testCallback, nullptr);
309     EXPECT_CALL(*testCallback, OnResult(_, _))
310         .Times(2)
311         .WillOnce(
__anone7116c9c0502(int32_t result, const Attributes &extraInfo) 312             [](int32_t result, const Attributes &extraInfo) {
313                 EXPECT_EQ(result, CHECK_PERMISSION_FAILED);
314             }
315         )
316         .WillOnce(
__anone7116c9c0602(int32_t result, const Attributes &extraInfo) 317             [](int32_t result, const Attributes &extraInfo) {
318                 EXPECT_EQ(result, HDF_FAILURE);
319             }
320         );
321     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
322     EXPECT_NE(mockHdi, nullptr);
323     EXPECT_CALL(*mockHdi, BeginEnrollment(_, _, _)).WillRepeatedly(Return(HDF_FAILURE));
324 
325     service.AddCredential(testUserId, testCredPara, testCallback, false);
326     IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
327     service.AddCredential(testUserId, testCredPara, testCallback, false);
328     IpcCommon::DeleteAllPermission();
329 }
330 
MockForAddCredentialHdi(std::shared_ptr<Context> & context,std::promise<void> & promise)331 static void MockForAddCredentialHdi(std::shared_ptr<Context> &context, std::promise<void> &promise)
332 {
333     const uint32_t testExecutorIndex = 60;
334     const uint32_t testscheduleId = 20;
335     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
336     EXPECT_NE(mockHdi, nullptr);
337     EXPECT_CALL(*mockHdi, BeginEnrollment(_, _, _))
338         .WillOnce([&context](const std::vector<uint8_t> &authToken, const HdiEnrollParam &param,
339             HdiScheduleInfo &info) {
340             info.executorIndexes.push_back(testExecutorIndex);
341             std::vector<uint8_t> executorMessages;
342             executorMessages.resize(1);
343             info.executorMessages.push_back(executorMessages);
344             info.scheduleId = testscheduleId;
345             info.authType = HdiAuthType::FACE;
346             auto contextList = ContextPool::Instance().Select(CONTEXT_ENROLL);
347             if (!contextList.empty()) {
348                 context = contextList[0].lock();
349             }
350             return HDF_SUCCESS;
351         });
352 
353     EXPECT_CALL(*mockHdi, UpdateEnrollmentResult(_, _, _)).WillOnce(Return(HDF_SUCCESS));
354     EXPECT_CALL(*mockHdi, CancelEnrollment(_))
355         .WillOnce([&promise](int32_t userId) {
356             promise.set_value();
357             return HDF_SUCCESS;
358         });
359 }
360 
MockForIdmResourceNode(std::shared_ptr<MockResourceNode> & resourceNode)361 static void MockForIdmResourceNode(std::shared_ptr<MockResourceNode> &resourceNode)
362 {
363     const uint32_t testScheduleId = 20;
364     const uint32_t testExecutorIndex = 60;
365     EXPECT_CALL(*resourceNode, GetExecutorIndex()).WillRepeatedly(Return(testExecutorIndex));
366     EXPECT_CALL(*resourceNode, GetAuthType()).WillRepeatedly(Return(FACE));
367     EXPECT_CALL(*resourceNode, GetExecutorRole()).WillRepeatedly(Return(ALL_IN_ONE));
368     EXPECT_CALL(*resourceNode, GetExecutorMatcher()).WillRepeatedly(Return(0));
369     EXPECT_CALL(*resourceNode, GetExecutorPublicKey()).WillRepeatedly(Return(std::vector<uint8_t>()));
370     EXPECT_CALL(*resourceNode, BeginExecute(_, _, _))
371         .WillOnce([](uint64_t scheduleId, const std::vector<uint8_t> &publicKey, const Attributes &command) {
372             auto messenger = ExecutorMessengerService::GetInstance();
373             EXPECT_NE(messenger, nullptr);
374             auto finalResult = Common::MakeShared<Attributes>();
375             EXPECT_NE(finalResult, nullptr);
376             std::vector<uint8_t> scheduleResult = {1, 2, 3, 4};
377             EXPECT_TRUE(finalResult->SetUint8ArrayValue(Attributes::ATTR_RESULT, scheduleResult));
378             EXPECT_EQ(messenger->Finish(testScheduleId, SUCCESS, finalResult), SUCCESS);
379             return SUCCESS;
380         });
381 }
382 
383 HWTEST_F(UserIdmServiceTest, UserIdmServiceAddCredential003, TestSize.Level0)
384 {
385     UserIdmService service(123123, true);
386     int32_t testUserId = 15457;
387     UserIdmInterface::CredentialPara testCredPara = {};
388     testCredPara.authType = FACE;
389     testCredPara.pinType = PIN_SIX;
390     testCredPara.token = {1, 2, 3, 4};
391     std::shared_ptr<Context> context = nullptr;
392 
393     sptr<MockIdmCallback> testCallback(new (std::nothrow) MockIdmCallback());
394     EXPECT_NE(testCallback, nullptr);
395     EXPECT_CALL(*testCallback, OnResult(_, _))
396         .WillOnce(
__anone7116c9c0a02(int32_t result, const Attributes &extraInfo) 397             [&context](int32_t result, const Attributes &extraInfo) {
398                 EXPECT_EQ(result, SUCCESS);
399                 if (context != nullptr) {
400                     context->Stop();
401                 }
402             }
403         );
404     std::promise<void> promise;
405     MockForAddCredentialHdi(context, promise);
406 
407     auto resourceNode = Common::MakeShared<MockResourceNode>();
408     EXPECT_NE(resourceNode, nullptr);
409     MockForIdmResourceNode(resourceNode);
410 
411     EXPECT_TRUE(ResourceNodePool::Instance().Insert(resourceNode));
412 
413     IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
414     service.AddCredential(testUserId, testCredPara, testCallback, false);
415     promise.get_future().get();
416 
417     EXPECT_TRUE(ResourceNodePool::Instance().Delete(60));
418     IpcCommon::DeleteAllPermission();
419 }
420 
421 HWTEST_F(UserIdmServiceTest, UserIdmServiceUpdateCredential001, TestSize.Level0)
422 {
423     UserIdmService service(123123, true);
424     int32_t testUserId = 1548545;
425     UserIdmInterface::CredentialPara testCredPara = {};
426     testCredPara.authType = FACE;
427     testCredPara.pinType = PIN_SIX;
428     testCredPara.token = {1, 2, 3, 4};
429     sptr<MockIdmCallback> testCallback(new (std::nothrow) MockIdmCallback());
430     EXPECT_NE(testCallback, nullptr);
431     EXPECT_CALL(*testCallback, OnResult(_, _))
432         .WillOnce(
__anone7116c9c0b02(int32_t result, const Attributes &extraInfo) 433             [](int32_t result, const Attributes &extraInfo) {
434                 EXPECT_EQ(result, NOT_ENROLLED);
435             }
436         );
437     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
438     EXPECT_NE(mockHdi, nullptr);
439     EXPECT_CALL(*mockHdi, GetCredential(_, _, _)).WillRepeatedly(Return(HDF_SUCCESS));
440     IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
441     service.UpdateCredential(testUserId, testCredPara, testCallback);
442     IpcCommon::DeleteAllPermission();
443 }
444 
445 HWTEST_F(UserIdmServiceTest, UserIdmServiceUpdateCredential002, TestSize.Level0)
446 {
447     UserIdmService service(123123, true);
448     int32_t testUserId = 1548545;
449     UserIdmInterface::CredentialPara testCredPara = {};
450     testCredPara.authType = FACE;
451     testCredPara.pinType = PIN_SIX;
452     sptr<MockIdmCallback> testCallback(nullptr);
453 
454     IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
455     service.UpdateCredential(testUserId, testCredPara, testCallback);
456 
457     testCallback = sptr<MockIdmCallback>(new (std::nothrow) MockIdmCallback());
458     EXPECT_NE(testCallback, nullptr);
459     EXPECT_CALL(*testCallback, OnResult(_, _))
460         .WillOnce(
__anone7116c9c0c02(int32_t result, const Attributes &extraInfo) 461             [](int32_t result, const Attributes &extraInfo) {
462                 EXPECT_EQ(result, HDF_FAILURE);
463             }
464         );
465 
466     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
467     EXPECT_NE(mockHdi, nullptr);
468     EXPECT_CALL(*mockHdi, GetCredential(_, _, _))
469         .WillOnce(
__anone7116c9c0d02(int32_t userId, int32_t authType, std::vector<HdiCredentialInfo> &infos) 470             [](int32_t userId, int32_t authType, std::vector<HdiCredentialInfo> &infos) {
471                 HdiCredentialInfo tempInfo = {
472                     .credentialId = 1,
473                     .executorIndex = 2,
474                     .templateId = 3,
475                     .authType = static_cast<HdiAuthType>(2),
476                     .executorMatcher = 2,
477                     .executorSensorHint = 3,
478                 };
479                 infos.push_back(tempInfo);
480                 return HDF_SUCCESS;
481             }
482         );
483 
484     EXPECT_CALL(*mockHdi, BeginEnrollment(_, _, _)).WillOnce(Return(HDF_FAILURE));
485 
486     testCredPara.token = {1, 2, 3, 4};
487     service.UpdateCredential(testUserId, testCredPara, testCallback);
488     IpcCommon::DeleteAllPermission();
489 }
490 
491 HWTEST_F(UserIdmServiceTest, UserIdmServiceCancel001, TestSize.Level0)
492 {
493     UserIdmService service(123123, true);
494     int32_t testUserId = 154835;
495     int32_t ret = service.Cancel(testUserId);
496     EXPECT_EQ(ret, CHECK_PERMISSION_FAILED);
497     IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
498     ret = service.Cancel(testUserId);
499     EXPECT_EQ(ret, GENERAL_ERROR);
500     IpcCommon::DeleteAllPermission();
501 }
502 
503 HWTEST_F(UserIdmServiceTest, UserIdmServiceCancel002, TestSize.Level0)
504 {
505     UserIdmService service(123123, true);
506     int32_t testUserId = 69874;
507     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
508     EXPECT_NE(mockHdi, nullptr);
509     EXPECT_CALL(*mockHdi, OpenSession(_, _)).WillOnce(Return(HDF_SUCCESS));
510 
511     IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
512     std::vector<uint8_t> challenge;
513     int32_t ret = service.OpenSession(testUserId, challenge);
514     EXPECT_EQ(ret, SUCCESS);
515     ret = service.Cancel(testUserId);
516     EXPECT_EQ(ret, GENERAL_ERROR);
517     IpcCommon::DeleteAllPermission();
518 }
519 
520 HWTEST_F(UserIdmServiceTest, UserIdmServiceCancel003, TestSize.Level0)
521 {
522     UserIdmService service(123123, true);
523     int32_t testUserId = 96874;
524     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
525     EXPECT_NE(mockHdi, nullptr);
526     EXPECT_CALL(*mockHdi, OpenSession(_, _)).WillOnce(Return(HDF_SUCCESS));
527 
528     IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
529     std::vector<uint8_t> challenge;
530     int32_t ret = service.OpenSession(testUserId, challenge);
531     EXPECT_EQ(ret, SUCCESS);
532 
533     auto context = Common::MakeShared<MockContext>();
534     EXPECT_NE(context, nullptr);
535     EXPECT_CALL(*context, GetContextType()).WillRepeatedly(Return(CONTEXT_ENROLL));
536     EXPECT_CALL(*context, GetContextId()).WillRepeatedly(Return(2345));
537     EXPECT_CALL(*context, Stop()).WillRepeatedly(Return(true));
538     EXPECT_TRUE(ContextPool::Instance().Insert(context));
539 
540     ret = service.Cancel(testUserId);
541     EXPECT_EQ(ret, SUCCESS);
542     IpcCommon::DeleteAllPermission();
543 }
544 
545 HWTEST_F(UserIdmServiceTest, UserIdmServiceEnforceDelUser001, TestSize.Level0)
546 {
547     UserIdmService service(123123, true);
548     int32_t testUserId = 15485;
549     sptr<MockIdmCallback> testCallback(new (std::nothrow) MockIdmCallback());
550     EXPECT_NE(testCallback, nullptr);
551     EXPECT_CALL(*testCallback, OnResult(_, _))
552         .Times(2)
553         .WillOnce(
__anone7116c9c0e02(int32_t result, const Attributes &extraInfo) 554             [](int32_t result, const Attributes &extraInfo) {
555                 EXPECT_EQ(result, CHECK_PERMISSION_FAILED);
556             }
557         )
558         .WillOnce(
__anone7116c9c0f02(int32_t result, const Attributes &extraInfo) 559             [](int32_t result, const Attributes &extraInfo) {
560                 EXPECT_EQ(result, GENERAL_ERROR);
561             }
562         );
563 
564     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
565     EXPECT_NE(mockHdi, nullptr);
566     EXPECT_CALL(*mockHdi, GetUserInfo(_, _, _, _)).WillOnce(Return(HDF_FAILURE));
567 
568     int32_t ret = service.EnforceDelUser(testUserId, testCallback);
569     EXPECT_EQ(ret, CHECK_PERMISSION_FAILED);
570     IpcCommon::AddPermission(ENFORCE_USER_IDM);
571     ret = service.EnforceDelUser(testUserId, testCallback);
572     EXPECT_EQ(ret, GENERAL_ERROR);
573     IpcCommon::DeleteAllPermission();
574 }
575 
576 HWTEST_F(UserIdmServiceTest, UserIdmServiceEnforceDelUser002, TestSize.Level0)
577 {
578     UserIdmService service(123123, true);
579     int32_t testUserId = 15485;
580     sptr<IdmCallbackInterface> testCallback(nullptr);
581     int32_t ret = service.EnforceDelUser(testUserId, testCallback);
582     EXPECT_EQ(ret, INVALID_PARAMETERS);
583 }
584 
MockForDelUserHdi()585 static void MockForDelUserHdi()
586 {
587     const uint32_t testAuthType = 1;
588     const uint32_t testCredentialId = 10;
589     const uint32_t testExecutorIndex = 20;
590     const uint32_t testExecutorMatcher = 30;
591     const uint32_t testExecutorSensorHint = 40;
592     const uint32_t testTemplateId = 50;
593     const uint32_t testSecureUid = 4542;
594     const uint32_t testTimes = 2;
595     const uint32_t testPinSubType = 10000;
596     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
597     EXPECT_NE(mockHdi, nullptr);
598     EXPECT_CALL(*mockHdi, GetUserInfo(_, _, _, _))
599         .WillRepeatedly([](int32_t userId, uint64_t &secureUid, int32_t& pinSubType,
600             std::vector<HdiEnrolledInfo> &infos) {
601             HdiEnrolledInfo info = {
602                 .enrolledId = 0,
603                 .authType = static_cast<HdiAuthType>(1),
604             };
605             infos.push_back(info);
606             pinSubType = static_cast<HdiPinSubType>(testPinSubType);
607             secureUid = testSecureUid;
608             return HDF_SUCCESS;
609         });
610 
611     EXPECT_CALL(*mockHdi, EnforceDeleteUser(_, _))
612         .Times(testTimes)
613         .WillOnce(Return(HDF_FAILURE))
614         .WillOnce([](int32_t userId, std::vector<HdiCredentialInfo> &deletedInfos) {
615             HdiCredentialInfo info = {};
616             info.authType = static_cast<HdiAuthType>(testAuthType);
617             info.credentialId = testCredentialId;
618             info.executorIndex = testExecutorIndex;
619             info.executorMatcher = testExecutorMatcher;
620             info.executorSensorHint = testExecutorSensorHint;
621             info.templateId = testTemplateId;
622             deletedInfos.emplace_back(info);
623             return HDF_SUCCESS;
624         });
625 }
626 
627 HWTEST_F(UserIdmServiceTest, UserIdmServiceEnforceDelUser003, TestSize.Level0)
628 {
629     UserIdmService service(123123, true);
630     int32_t testUserId = 15485;
631     sptr<MockIdmCallback> testCallback(new (std::nothrow) MockIdmCallback());
632     EXPECT_NE(testCallback, nullptr);
633     EXPECT_CALL(*testCallback, OnResult(_, _))
634         .Times(2)
635         .WillOnce(
__anone7116c9c1202(int32_t result, const Attributes &extraInfo) 636             [](int32_t result, const Attributes &extraInfo) {
637                 EXPECT_EQ(result, HDF_FAILURE);
638             }
639         )
640         .WillOnce(
__anone7116c9c1302(int32_t result, const Attributes &extraInfo) 641             [](int32_t result, const Attributes &extraInfo) {
642                 EXPECT_EQ(result, SUCCESS);
643             }
644         );
645     MockForDelUserHdi();
646     IpcCommon::AddPermission(ENFORCE_USER_IDM);
647     int32_t ret = service.EnforceDelUser(testUserId, testCallback);
648     EXPECT_EQ(ret, -1);
649     ret = service.EnforceDelUser(testUserId, testCallback);
650     EXPECT_EQ(ret, SUCCESS);
651     IpcCommon::DeleteAllPermission();
652 }
653 
654 HWTEST_F(UserIdmServiceTest, UserIdmServiceDelUser001, TestSize.Level0)
655 {
656     UserIdmService service(123123, true);
657     int32_t testUserId = 15486465;
658     std::vector<uint8_t> testAuthToken = {1, 2, 3, 4};
659 
660     sptr<MockIdmCallback> testCallback(new (std::nothrow) MockIdmCallback());
661     EXPECT_NE(testCallback, nullptr);
662     EXPECT_CALL(*testCallback, OnResult(_, _))
663         .Times(2)
664         .WillOnce(
__anone7116c9c1402(int32_t result, const Attributes &extraInfo) 665             [](int32_t result, const Attributes &extraInfo) {
666                 EXPECT_EQ(result, CHECK_PERMISSION_FAILED);
667             }
668         )
669         .WillOnce(
__anone7116c9c1502(int32_t result, const Attributes &extraInfo) 670             [](int32_t result, const Attributes &extraInfo) {
671                 EXPECT_EQ(result, HDF_FAILURE);
672             }
673         );
674 
675     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
676     EXPECT_NE(mockHdi, nullptr);
677     EXPECT_CALL(*mockHdi, DeleteUser(_, _, _, _)).WillOnce(Return(HDF_FAILURE));
678 
679     service.DelUser(testUserId, testAuthToken, testCallback);
680     IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
681     service.DelUser(testUserId, testAuthToken, testCallback);
682     IpcCommon::DeleteAllPermission();
683 }
684 
685 HWTEST_F(UserIdmServiceTest, UserIdmServiceDelUser002, TestSize.Level0)
686 {
687     UserIdmService service(123123, true);
688     int32_t testUserId = 15486465;
689     std::vector<uint8_t> testAuthToken = {1, 2, 3, 4};
690     sptr<IdmCallbackInterface> testCallback(nullptr);
691     service.DelUser(testUserId, testAuthToken, testCallback);
692 }
693 
694 HWTEST_F(UserIdmServiceTest, UserIdmServiceDelUser003, TestSize.Level0)
695 {
696     UserIdmService service(123123, true);
697     int32_t testUserId = 15486465;
698     std::vector<uint8_t> testAuthToken;
699     sptr<MockIdmCallback> testCallback(new (std::nothrow) MockIdmCallback());
700     EXPECT_NE(testCallback, nullptr);
701     EXPECT_CALL(*testCallback, OnResult(_, _))
702         .WillOnce(
__anone7116c9c1602(int32_t result, const Attributes &extraInfo) 703             [](int32_t result, const Attributes &extraInfo) {
704                 EXPECT_EQ(result, SUCCESS);
705             }
706         );
707 
708     IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
709     service.DelUser(testUserId, testAuthToken, testCallback);
710     IpcCommon::DeleteAllPermission();
711 }
712 
713 HWTEST_F(UserIdmServiceTest, UserIdmServiceDelUser004, TestSize.Level0)
714 {
715     UserIdmService service(123123, true);
716     int32_t testUserId = 15486465;
717     std::vector<uint8_t> testAuthToken = {1, 2, 3, 4};
718     sptr<MockIdmCallback> testCallback(new (std::nothrow) MockIdmCallback());
719     EXPECT_NE(testCallback, nullptr);
720     EXPECT_CALL(*testCallback, OnResult(_, _))
721         .WillOnce(
__anone7116c9c1702(int32_t result, const Attributes &extraInfo) 722             [](int32_t result, const Attributes &extraInfo) {
723                 EXPECT_EQ(result, SUCCESS);
724             }
725         );
726 
727     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
728     EXPECT_NE(mockHdi, nullptr);
729     EXPECT_CALL(*mockHdi, DeleteUser(_, _, _, _))
730         .WillOnce(
731             [](int32_t userId, const std::vector<uint8_t> &authToken, std::vector<HdiCredentialInfo> &deletedInfos,
__anone7116c9c1802(int32_t userId, const std::vector<uint8_t> &authToken, std::vector<HdiCredentialInfo> &deletedInfos, std::vector<uint8_t> &rootSecret) 732             std::vector<uint8_t> &rootSecret) {
733                 HdiCredentialInfo info = {};
734                 info.authType = static_cast<HdiAuthType>(1);
735                 info.credentialId = 10;
736                 info.executorIndex = 20;
737                 info.executorMatcher = 30;
738                 info.executorSensorHint = 40;
739                 info.templateId = 50;
740                 deletedInfos.emplace_back(info);
741                 return HDF_SUCCESS;
742             }
743         );
744 
745     IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
746     service.DelUser(testUserId, testAuthToken, testCallback);
747     IpcCommon::DeleteAllPermission();
748 }
749 
750 HWTEST_F(UserIdmServiceTest, UserIdmServiceDelCredential001, TestSize.Level0)
751 {
752     UserIdmService service(123123, true);
753     int32_t testUserId = 1548865;
754     uint64_t testCredentialId = 23424;
755     std::vector<uint8_t> testAuthToken = {1, 2, 3, 4};
756 
757     sptr<MockIdmCallback> testCallback(new (std::nothrow) MockIdmCallback());
758     EXPECT_NE(testCallback, nullptr);
759     EXPECT_CALL(*testCallback, OnResult(_, _))
760         .Times(2)
761         .WillOnce(
__anone7116c9c1902(int32_t result, const Attributes &extraInfo) 762             [](int32_t result, const Attributes &extraInfo) {
763                 EXPECT_EQ(result, CHECK_PERMISSION_FAILED);
764             }
765         )
766         .WillOnce(
__anone7116c9c1a02(int32_t result, const Attributes &extraInfo) 767             [](int32_t result, const Attributes &extraInfo) {
768                 EXPECT_EQ(result, HDF_FAILURE);
769             }
770         );
771 
772     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
773     EXPECT_NE(mockHdi, nullptr);
774     EXPECT_CALL(*mockHdi, DeleteCredential(_, _, _, _)).WillOnce(Return(HDF_FAILURE));
775 
776     service.DelCredential(testUserId, testCredentialId, testAuthToken, testCallback);
777     IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
778     service.DelCredential(testUserId, testCredentialId, testAuthToken, testCallback);
779     IpcCommon::DeleteAllPermission();
780 }
781 
782 HWTEST_F(UserIdmServiceTest, UserIdmServiceDelCredential002, TestSize.Level0)
783 {
784     UserIdmService service(123123, true);
785     int32_t testUserId = 1548865;
786     uint64_t testCredentialId = 23424;
787     std::vector<uint8_t> testAuthToken = {1, 2, 3, 4};
788     sptr<IdmCallbackInterface> testCallback(nullptr);
789     service.DelCredential(testUserId, testCredentialId, testAuthToken, testCallback);
790 }
791 
792 HWTEST_F(UserIdmServiceTest, UserIdmServiceDelCredential003, TestSize.Level0)
793 {
794     UserIdmService service(123123, true);
795     int32_t testUserId = 1548865;
796     uint64_t testCredentialId = 23424;
797     std::vector<uint8_t> testAuthToken = {1, 2, 3, 4};
798 
799     sptr<MockIdmCallback> testCallback(new (std::nothrow) MockIdmCallback());
800     EXPECT_NE(testCallback, nullptr);
801     EXPECT_CALL(*testCallback, OnResult(_, _))
802         .WillOnce(
__anone7116c9c1b02(int32_t result, const Attributes &extraInfo) 803             [](int32_t result, const Attributes &extraInfo) {
804                 EXPECT_EQ(result, SUCCESS);
805             }
806         );
807 
808     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
809     EXPECT_NE(mockHdi, nullptr);
810     EXPECT_CALL(*mockHdi, DeleteCredential(_, _, _, _))
811         .WillOnce(
__anone7116c9c1c02(int32_t userId, uint64_t credentialId, const std::vector<uint8_t> &authToken, HdiCredentialInfo &info) 812             [](int32_t userId, uint64_t credentialId, const std::vector<uint8_t> &authToken, HdiCredentialInfo &info) {
813                 info.authType = static_cast<HdiAuthType>(1);
814                 info.credentialId = 10;
815                 info.executorIndex = 20;
816                 info.executorMatcher = 30;
817                 info.executorSensorHint = 40;
818                 info.templateId = 50;
819                 return HDF_SUCCESS;
820             }
821         );
822 
823     IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
824     service.DelCredential(testUserId, testCredentialId, testAuthToken, testCallback);
825     IpcCommon::DeleteAllPermission();
826 }
827 
828 HWTEST_F(UserIdmServiceTest, UserIdmServiceTestDump, TestSize.Level0)
829 {
830     int testFd1 = -1;
831     int testFd2 = 1;
832     std::vector<std::u16string> testArgs;
833 
834     UserIdmService service(123123, true);
835 
836     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
837     EXPECT_NE(mockHdi, nullptr);
838     EXPECT_CALL(*mockHdi, GetUserInfo(_, _, _, _))
839         .Times(2)
840         .WillOnce(Return(HDF_FAILURE))
841         .WillOnce(
__anone7116c9c1d02(int32_t userId, uint64_t &secureUid, int32_t &pinSubType, std::vector<HdiEnrolledInfo> &infos) 842             [](int32_t userId, uint64_t &secureUid, int32_t &pinSubType, std::vector<HdiEnrolledInfo> &infos) {
843                 HdiEnrolledInfo info = {
844                     .enrolledId = 0,
845                     .authType = static_cast<HdiAuthType>(1),
846                 };
847                 infos.push_back(info);
848                 pinSubType = static_cast<HdiPinSubType>(10000);
849                 secureUid = 4542;
850                 return HDF_SUCCESS;
851             }
852         );
853 
854     EXPECT_EQ(service.Dump(testFd1, testArgs), INVALID_PARAMETERS);
855     EXPECT_EQ(service.Dump(testFd2, testArgs), SUCCESS);
856     testArgs.push_back(u"-h");
857     EXPECT_EQ(service.Dump(testFd2, testArgs), SUCCESS);
858     testArgs.clear();
859     testArgs.push_back(u"-l");
860     EXPECT_EQ(service.Dump(testFd2, testArgs), GENERAL_ERROR);
861     EXPECT_EQ(service.Dump(testFd2, testArgs), SUCCESS);
862     testArgs.clear();
863     testArgs.push_back(u"-k");
864     EXPECT_EQ(service.Dump(testFd2, testArgs), GENERAL_ERROR);
865 }
866 
867 HWTEST_F(UserIdmServiceTest, UserIdmServiceClearRedundancyCredential001, TestSize.Level0)
868 {
869     UserIdmService service(123123, true);
870     sptr<MockIdmCallback> testCallback(new (std::nothrow) MockIdmCallback());
871     EXPECT_NE(testCallback, nullptr);
872     EXPECT_CALL(*testCallback, OnResult(_, _))
873         .Times(2)
874         .WillOnce(
__anone7116c9c1e02(int32_t result, const Attributes &extraInfo) 875             [](int32_t result, const Attributes &extraInfo) {
876                 EXPECT_EQ(result, CHECK_PERMISSION_FAILED);
877             }
878         )
879         .WillOnce(
__anone7116c9c1f02(int32_t result, const Attributes &extraInfo) 880             [](int32_t result, const Attributes &extraInfo) {
881                 EXPECT_EQ(result, SUCCESS);
882             }
883         );
884 
885     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
886     EXPECT_NE(mockHdi, nullptr);
887     EXPECT_CALL(*mockHdi, GetAllExtUserInfo(_)).WillOnce(Return(HDF_FAILURE));
888 
889     service.ClearRedundancyCredential(testCallback);
890     IpcCommon::AddPermission(CLEAR_REDUNDANCY_PERMISSION);
891     service.ClearRedundancyCredential(testCallback);
892     IpcCommon::DeleteAllPermission();
893 }
894 
895 HWTEST_F(UserIdmServiceTest, UserIdmServiceClearRedundancyCredential002, TestSize.Level0)
896 {
897     UserIdmService service(123123, true);
898     sptr<MockIdmCallback> testCallback(new (std::nothrow) MockIdmCallback());
899     EXPECT_NE(testCallback, nullptr);
900     EXPECT_CALL(*testCallback, OnResult(_, _))
901         .Times(2)
902         .WillOnce(
__anone7116c9c2002(int32_t result, const Attributes &extraInfo) 903             [](int32_t result, const Attributes &extraInfo) {
904                 EXPECT_EQ(result, HDF_SUCCESS);
905             }
906         )
907         .WillOnce(
__anone7116c9c2102(int32_t result, const Attributes &extraInfo) 908             [](int32_t result, const Attributes &extraInfo) {
909                 EXPECT_EQ(result, HDF_SUCCESS);
910             }
911         );
912 
913     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
914     EXPECT_NE(mockHdi, nullptr);
915     EXPECT_CALL(*mockHdi, GetAllExtUserInfo(_))
916         .WillRepeatedly(
__anone7116c9c2202(std::vector<ExtUserInfo> &userInfos) 917             [](std::vector<ExtUserInfo> &userInfos) {
918                 ExtUserInfo info = {
919                     .userId = 100,
920                 };
921                 userInfos.push_back(info);
922                 return HDF_SUCCESS;
923             }
924         );
925 
926     EXPECT_CALL(*mockHdi, EnforceDeleteUser(_, _))
927         .Times(2)
928         .WillOnce(Return(HDF_FAILURE))
929         .WillOnce(
__anone7116c9c2302(int32_t userId, std::vector<HdiCredentialInfo> &deletedInfos) 930             [](int32_t userId, std::vector<HdiCredentialInfo> &deletedInfos) {
931                 HdiCredentialInfo info = {};
932                 info.authType = static_cast<HdiAuthType>(1);
933                 info.credentialId = 10;
934                 info.executorIndex = 20;
935                 info.executorMatcher = 30;
936                 info.executorSensorHint = 40;
937                 info.templateId = 50;
938                 deletedInfos.emplace_back(info);
939                 return HDF_SUCCESS;
940             }
941         );
942 
943     IpcCommon::AddPermission(CLEAR_REDUNDANCY_PERMISSION);
944     service.ClearRedundancyCredential(testCallback);
945     service.ClearRedundancyCredential(testCallback);
946     IpcCommon::DeleteAllPermission();
947 }
948 } // namespace UserAuth
949 } // namespace UserIam
950 } // namespace OHOS