• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_event_listener.h"
27 #include "mock_ipc_common.h"
28 #include "mock_iuser_auth_interface.h"
29 #include "mock_resource_node.h"
30 #include "mock_user_idm_callback.h"
31 #include "resource_node_pool.h"
32 #include "user_idm_service.h"
33 
34 namespace OHOS {
35 namespace UserIam {
36 namespace UserAuth {
37 using namespace testing;
38 using namespace testing::ext;
SetUpTestCase()39 void UserIdmServiceTest::SetUpTestCase()
40 {
41 }
42 
TearDownTestCase()43 void UserIdmServiceTest::TearDownTestCase()
44 {
45 }
46 
SetUp()47 void UserIdmServiceTest::SetUp()
48 {
49     MockIUserAuthInterface::Holder::GetInstance().Reset();
50 }
51 
TearDown()52 void UserIdmServiceTest::TearDown()
53 {
54     MockIUserAuthInterface::Holder::GetInstance().Reset();
55 }
56 
57 HWTEST_F(UserIdmServiceTest, UserIdmServiceOpenSession_001, TestSize.Level0)
58 {
59     UserIdmService service(123123, true);
60     int32_t testUserId = 0;
61     std::vector<uint8_t> testChallenge = {1, 2, 3, 4};
62     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
63     EXPECT_NE(mockHdi, nullptr);
64     EXPECT_CALL(*mockHdi, OpenSession(_, _)).Times(1);
65     ON_CALL(*mockHdi, OpenSession)
66         .WillByDefault(
__anond8f79fc60102(int32_t userId, std::vector<uint8_t> &challenge) 67             [&testChallenge](int32_t userId, std::vector<uint8_t> &challenge) {
68                 challenge = testChallenge;
69                 return HDF_SUCCESS;
70             }
71         );
72     std::vector<uint8_t> challenge;
73     IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
74     int32_t ret = service.OpenSession(testUserId, challenge);
75     EXPECT_EQ(ret, SUCCESS);
76     EXPECT_THAT(challenge, ElementsAreArray(testChallenge));
77     IpcCommon::DeleteAllPermission();
78 }
79 
80 HWTEST_F(UserIdmServiceTest, UserIdmServiceOpenSession_002, TestSize.Level0)
81 {
82     UserIdmService service(123123, true);
83     int32_t testUserId = 0;
84     uint64_t contextId = 1;
85     std::vector<uint8_t> testChallenge = {1, 2, 3, 4};
86 
87     std::vector<uint8_t> challenge;
88     int32_t ret = service.OpenSession(testUserId, challenge);
89     EXPECT_EQ(ret, CHECK_PERMISSION_FAILED);
90 
91     IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
92     auto context = Common::MakeShared<MockContext>();
93     EXPECT_NE(context, nullptr);
94     EXPECT_CALL(*context, GetContextType()).WillRepeatedly(Return(CONTEXT_ENROLL));
95     EXPECT_CALL(*context, GetContextId()).WillRepeatedly(Return(contextId));
96     EXPECT_CALL(*context, Stop()).WillRepeatedly(Return(true));
97     EXPECT_TRUE(ContextPool::Instance().Insert(context));
98 
99     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
100     EXPECT_NE(mockHdi, nullptr);
101     EXPECT_CALL(*mockHdi, OpenSession(_, _))
102         .Times(3)
103         .WillOnce(Return(HDF_SUCCESS))
104         .WillOnce(Return(HDF_SUCCESS))
105         .WillOnce(Return(HDF_FAILURE));
106 
107     EXPECT_EQ(service.OpenSession(testUserId, challenge), SUCCESS);
108     EXPECT_EQ(service.OpenSession(testUserId, challenge), SUCCESS);
109     EXPECT_EQ(service.OpenSession(testUserId, challenge), GENERAL_ERROR);
110     EXPECT_TRUE(ContextPool::Instance().Delete(contextId));
111 
112     IpcCommon::DeleteAllPermission();
113 }
114 
115 HWTEST_F(UserIdmServiceTest, UserIdmServiceCloseSession, TestSize.Level0)
116 {
117     UserIdmService service(123123, true);
118     int32_t testUserId = 3546;
119 
120     service.CloseSession(testUserId);
121     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
122     EXPECT_NE(mockHdi, nullptr);
123     EXPECT_CALL(*mockHdi, CloseSession(_))
124         .Times(2)
125         .WillOnce(Return(HDF_SUCCESS))
126         .WillOnce(Return(HDF_FAILURE));
127     IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
128     EXPECT_EQ(service.CloseSession(testUserId), SUCCESS);
129     EXPECT_EQ(service.CloseSession(testUserId), GENERAL_ERROR);
130     IpcCommon::DeleteAllPermission();
131 }
132 
133 HWTEST_F(UserIdmServiceTest, UserIdmServiceGetCredentialInfo001, TestSize.Level0)
134 {
135     UserIdmService service(123123, true);
136     int32_t testUserId = 0;
137     AuthType testAuthType = PIN;
138 
139     sptr<MockIdmGetCredentialInfoCallback> testCallback(new (std::nothrow) MockIdmGetCredentialInfoCallback());
140     EXPECT_NE(testCallback, nullptr);
141     EXPECT_CALL(*testCallback, OnCredentialInfos(_, _)).Times(2);
142 
143     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
144     EXPECT_NE(mockHdi, nullptr);
145     EXPECT_CALL(*mockHdi, GetCredential(_, _, _)).WillOnce(Return(HDF_FAILURE));
146     int32_t funcResult = SUCCESS;
147     int32_t ret = service.GetCredentialInfo(testUserId, static_cast<AuthType>(testAuthType), testCallback, funcResult);
148     EXPECT_EQ(funcResult, CHECK_PERMISSION_FAILED);
149     EXPECT_EQ(ret, SUCCESS);
150     IpcCommon::AddPermission(USE_USER_IDM_PERMISSION);
151     ret = service.GetCredentialInfo(testUserId, static_cast<AuthType>(testAuthType), testCallback, funcResult);
152     EXPECT_EQ(funcResult, GENERAL_ERROR);
153     EXPECT_EQ(ret, SUCCESS);
154     IpcCommon::DeleteAllPermission();
155 }
156 
157 HWTEST_F(UserIdmServiceTest, UserIdmServiceGetCredentialInfo002, TestSize.Level0)
158 {
159     UserIdmService service(123123, true);
160     int32_t testUserId = 0;
161     AuthType testAuthType = PIN;
162     sptr<IIdmGetCredInfoCallback> testCallback(nullptr);
163     int32_t funcResult = SUCCESS;
164     int32_t ret = service.GetCredentialInfo(testUserId, static_cast<AuthType>(testAuthType), testCallback, funcResult);
165     EXPECT_EQ(funcResult, INVALID_PARAMETERS);
166     EXPECT_EQ(ret, SUCCESS);
167 }
168 
169 HWTEST_F(UserIdmServiceTest, UserIdmServiceGetCredentialInfo003, TestSize.Level0)
170 {
171     UserIdmService service(123123, true);
172     int32_t testUserId = 0;
173     AuthType testAuthType = PIN;
174     sptr<MockIdmGetCredentialInfoCallback> testCallback(new (std::nothrow) MockIdmGetCredentialInfoCallback());
175     EXPECT_NE(testCallback, nullptr);
176     EXPECT_CALL(*testCallback, OnCredentialInfos(_, _)).Times(2);
177 
178     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
179     EXPECT_NE(mockHdi, nullptr);
180     EXPECT_CALL(*mockHdi, GetCredential(_, _, _)).Times(2);
181     ON_CALL(*mockHdi, GetCredential)
182         .WillByDefault(
__anond8f79fc60202(int32_t userId, int32_t authType, std::vector<HdiCredentialInfo> &infos) 183             [](int32_t userId, int32_t authType, std::vector<HdiCredentialInfo> &infos) {
184                 HdiCredentialInfo tempInfo = {
185                     .credentialId = 1,
186                     .executorIndex = 2,
187                     .templateId = 3,
188                     .authType = static_cast<HdiAuthType>(1),
189                     .executorMatcher = 2,
190                     .executorSensorHint = 3,
191                 };
192                 infos.push_back(tempInfo);
193                 return HDF_SUCCESS;
194             }
195         );
196     IpcCommon::AddPermission(USE_USER_IDM_PERMISSION);
197     int32_t funcResult = SUCCESS;
198     int32_t ret = service.GetCredentialInfo(testUserId, static_cast<AuthType>(testAuthType), testCallback, funcResult);
199     EXPECT_EQ(funcResult, SUCCESS);
200     EXPECT_EQ(ret, SUCCESS);
201     ret = service.GetCredentialInfo(testUserId, static_cast<AuthType>(testAuthType), testCallback, funcResult);
202     EXPECT_EQ(funcResult, SUCCESS);
203     EXPECT_EQ(ret, SUCCESS);
204     IpcCommon::DeleteAllPermission();
205 }
206 
207 HWTEST_F(UserIdmServiceTest, UserIdmServiceGetCredentialInfo004, TestSize.Level0)
208 {
209     UserIdmService service(123123, true);
210     int32_t testUserId = 0;
211     AuthType testAuthType = PIN;
212     sptr<MockIdmGetCredentialInfoCallback> testCallback(new (std::nothrow) MockIdmGetCredentialInfoCallback());
213     EXPECT_NE(testCallback, nullptr);
214     EXPECT_CALL(*testCallback, OnCredentialInfos(_, _)).Times(1);
215 
216     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
217     EXPECT_NE(mockHdi, nullptr);
218     EXPECT_CALL(*mockHdi, GetCredential(_, _, _))
219         .WillOnce(
__anond8f79fc60302(int32_t userId, int32_t authType, std::vector<HdiCredentialInfo> &infos) 220             [](int32_t userId, int32_t authType, std::vector<HdiCredentialInfo> &infos) {
221                 HdiCredentialInfo tempInfo = {
222                     .credentialId = 1,
223                     .executorIndex = 2,
224                     .templateId = 3,
225                     .authType = static_cast<HdiAuthType>(2),
226                     .executorMatcher = 2,
227                     .executorSensorHint = 3,
228                 };
229                 infos.push_back(tempInfo);
230                 return HDF_SUCCESS;
231             }
232         );
233 
234     IpcCommon::AddPermission(USE_USER_IDM_PERMISSION);
235     int32_t funcResult = SUCCESS;
236     int32_t ret = service.GetCredentialInfo(testUserId, static_cast<AuthType>(testAuthType), testCallback, funcResult);
237     EXPECT_EQ(funcResult, SUCCESS);
238     EXPECT_EQ(ret, SUCCESS);
239     IpcCommon::DeleteAllPermission();
240 }
241 
242 HWTEST_F(UserIdmServiceTest, UserIdmServiceGetSecInfo001, TestSize.Level0)
243 {
244     UserIdmService service(123123, true);
245     int32_t testUserId = 0;
246 
247     sptr<MockIdmGetSecureUserInfoCallback> testCallback(new (std::nothrow) MockIdmGetSecureUserInfoCallback());
248     EXPECT_NE(testCallback, nullptr);
249     EXPECT_CALL(*testCallback, OnSecureUserInfo(_, _)).Times(2);
250 
251     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
252     EXPECT_NE(mockHdi, nullptr);
253     EXPECT_CALL(*mockHdi, GetUserInfo(_, _, _, _)).WillOnce(Return(HDF_FAILURE));
254 
255     int32_t ret = service.GetSecInfo(testUserId, testCallback);
256     EXPECT_EQ(ret, CHECK_PERMISSION_FAILED);
257     IpcCommon::AddPermission(USE_USER_IDM_PERMISSION);
258     ret = service.GetSecInfo(testUserId, testCallback);
259     EXPECT_EQ(ret, GENERAL_ERROR);
260     IpcCommon::DeleteAllPermission();
261 }
262 
263 HWTEST_F(UserIdmServiceTest, UserIdmServiceGetSecInfo002, TestSize.Level0)
264 {
265     UserIdmService service(123123, true);
266     int32_t testUserId = 0;
267     sptr<MockIdmGetSecureUserInfoCallback> testCallback(nullptr);
268     IpcCommon::AddPermission(USE_USER_IDM_PERMISSION);
269     int32_t ret = service.GetSecInfo(testUserId, testCallback);
270     EXPECT_EQ(ret, INVALID_PARAMETERS);
271 
272     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
273     EXPECT_NE(mockHdi, nullptr);
274     EXPECT_CALL(*mockHdi, GetUserInfo(_, _, _, _))
275         .Times(2)
276         .WillOnce(Return(HDF_FAILURE))
277         .WillOnce(
__anond8f79fc60402(int32_t userId, uint64_t &secureUid, int32_t& pinSubType, std::vector<HdiEnrolledInfo> &infos) 278             [](int32_t userId, uint64_t &secureUid, int32_t& pinSubType, std::vector<HdiEnrolledInfo> &infos) {
279                 HdiEnrolledInfo info = {
280                     .enrolledId = 0,
281                     .authType = static_cast<HdiAuthType>(1),
282                 };
283                 infos.push_back(info);
284                 pinSubType = static_cast<HdiPinSubType>(10000);
285                 secureUid = 4542;
286                 return HDF_SUCCESS;
287             }
288         );
289 
290     testCallback = sptr<MockIdmGetSecureUserInfoCallback>(new (std::nothrow) MockIdmGetSecureUserInfoCallback());
291     EXPECT_NE(testCallback, nullptr);
292     EXPECT_CALL(*testCallback, OnSecureUserInfo(_, _)).Times(2);
293     ret = service.GetSecInfo(testUserId, testCallback);
294     EXPECT_EQ(ret, GENERAL_ERROR);
295     ret = service.GetSecInfo(testUserId, testCallback);
296     EXPECT_EQ(ret, SUCCESS);
297     IpcCommon::DeleteAllPermission();
298 }
299 
300 HWTEST_F(UserIdmServiceTest, UserIdmServiceAddCredential001, TestSize.Level0)
301 {
302     UserIdmService service(123123, true);
303     int32_t testUserId = 15457;
304     IpcCredentialPara testCredPara = {};
305     testCredPara.authType = PIN;
306     testCredPara.pinType = PIN_SIX;
307     testCredPara.token = {1, 2, 3, 4};
308     sptr<IIamCallback> testCallback(nullptr);
309     EXPECT_EQ(service.AddCredential(testUserId, testCredPara, testCallback, false), INVALID_PARAMETERS);
310 }
311 
312 HWTEST_F(UserIdmServiceTest, UserIdmServiceAddCredential002, TestSize.Level0)
313 {
314     UserIdmService service(123123, true);
315     int32_t testUserId = 15457;
316     IpcCredentialPara testCredPara = {};
317     testCredPara.authType = PIN;
318     testCredPara.pinType = PIN_SIX;
319     testCredPara.token = {1, 2, 3, 4};
320     sptr<MockIdmCallback> testCallback(new (std::nothrow) MockIdmCallback());
321     EXPECT_NE(testCallback, nullptr);
322     EXPECT_CALL(*testCallback, OnResult(_, _))
323         .Times(2)
324         .WillOnce(
__anond8f79fc60502(int32_t result, const std::vector<uint8_t> &extraInfo) 325             [](int32_t result, const std::vector<uint8_t> &extraInfo) {
326                 EXPECT_EQ(result, CHECK_PERMISSION_FAILED);
327                 return SUCCESS;
328             }
329         )
330         .WillOnce(
__anond8f79fc60602(int32_t result, const std::vector<uint8_t> &extraInfo) 331             [](int32_t result, const std::vector<uint8_t> &extraInfo) {
332                 EXPECT_EQ(result, HDF_FAILURE);
333                 return SUCCESS;
334             }
335         );
336     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
337     EXPECT_NE(mockHdi, nullptr);
338     EXPECT_CALL(*mockHdi, BeginEnrollment(_, _, _)).WillRepeatedly(Return(HDF_FAILURE));
339 
340     EXPECT_EQ(service.AddCredential(testUserId, testCredPara, testCallback, false), CHECK_PERMISSION_FAILED);
341     IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
342     EXPECT_EQ(service.AddCredential(testUserId, testCredPara, testCallback, false), GENERAL_ERROR);
343     IpcCommon::DeleteAllPermission();
344 }
345 
MockForAddCredentialHdi(std::shared_ptr<Context> & context,std::promise<void> & promise)346 static void MockForAddCredentialHdi(std::shared_ptr<Context> &context, std::promise<void> &promise)
347 {
348     const uint32_t testExecutorIndex = 60;
349     const uint32_t testscheduleId = 20;
350     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
351     EXPECT_NE(mockHdi, nullptr);
352     EXPECT_CALL(*mockHdi, BeginEnrollment(_, _, _))
353         .WillOnce([&context](const std::vector<uint8_t> &authToken, const HdiEnrollParam &param,
354             HdiScheduleInfo &info) {
355             info.executorIndexes.push_back(testExecutorIndex);
356             std::vector<uint8_t> executorMessages;
357             executorMessages.resize(1);
358             info.executorMessages.push_back(executorMessages);
359             info.scheduleId = testscheduleId;
360             info.authType = HdiAuthType::FACE;
361             auto contextList = ContextPool::Instance().Select(CONTEXT_ENROLL);
362             if (!contextList.empty()) {
363                 context = contextList[0].lock();
364             }
365             return HDF_SUCCESS;
366         });
367 
368     EXPECT_CALL(*mockHdi, UpdateEnrollmentResult(_, _, _)).WillOnce(Return(HDF_SUCCESS));
369     EXPECT_CALL(*mockHdi, CancelEnrollment(_))
370         .WillOnce([&promise](int32_t userId) {
371             promise.set_value();
372             return HDF_SUCCESS;
373         });
374 }
375 
MockForIdmResourceNode(std::shared_ptr<MockResourceNode> & resourceNode)376 static void MockForIdmResourceNode(std::shared_ptr<MockResourceNode> &resourceNode)
377 {
378     const uint32_t testScheduleId = 20;
379     const uint32_t testExecutorIndex = 60;
380     EXPECT_CALL(*resourceNode, GetExecutorIndex()).WillRepeatedly(Return(testExecutorIndex));
381     EXPECT_CALL(*resourceNode, GetAuthType()).WillRepeatedly(Return(FACE));
382     EXPECT_CALL(*resourceNode, GetExecutorRole()).WillRepeatedly(Return(ALL_IN_ONE));
383     EXPECT_CALL(*resourceNode, GetExecutorMatcher()).WillRepeatedly(Return(0));
384     EXPECT_CALL(*resourceNode, GetExecutorPublicKey()).WillRepeatedly(Return(std::vector<uint8_t>()));
385     EXPECT_CALL(*resourceNode, BeginExecute(_, _, _))
386         .WillOnce([](uint64_t scheduleId, const std::vector<uint8_t> &publicKey, const Attributes &command) {
387             auto messenger = ExecutorMessengerService::GetInstance();
388             EXPECT_NE(messenger, nullptr);
389             auto finalResult = Common::MakeShared<Attributes>();
390             EXPECT_NE(finalResult, nullptr);
391             std::vector<uint8_t> scheduleResult = {1, 2, 3, 4};
392             EXPECT_TRUE(finalResult->SetUint8ArrayValue(Attributes::ATTR_RESULT, scheduleResult));
393             EXPECT_EQ(messenger->Finish(testScheduleId, SUCCESS, finalResult->Serialize()), SUCCESS);
394             return SUCCESS;
395         });
396 }
397 
398 HWTEST_F(UserIdmServiceTest, UserIdmServiceAddCredential003, TestSize.Level0)
399 {
400     UserIdmService service(123123, true);
401     int32_t testUserId = 15457;
402     IpcCredentialPara testCredPara = {};
403     testCredPara.authType = FACE;
404     testCredPara.pinType = PIN_SIX;
405     testCredPara.token = {1, 2, 3, 4};
406     std::shared_ptr<Context> context = nullptr;
407 
408     sptr<MockIdmCallback> testCallback(new (std::nothrow) MockIdmCallback());
409     EXPECT_NE(testCallback, nullptr);
410     EXPECT_CALL(*testCallback, OnResult(_, _))
411         .WillOnce(
__anond8f79fc60a02(int32_t result, const std::vector<uint8_t> &extraInfo) 412             [&context](int32_t result, const std::vector<uint8_t> &extraInfo) {
413                 EXPECT_EQ(result, SUCCESS);
414                 if (context != nullptr) {
415                     context->Stop();
416                 }
417                 return SUCCESS;
418             }
419         );
420     std::promise<void> promise;
421     MockForAddCredentialHdi(context, promise);
422 
423     auto resourceNode = Common::MakeShared<MockResourceNode>();
424     EXPECT_NE(resourceNode, nullptr);
425     MockForIdmResourceNode(resourceNode);
426 
427     EXPECT_TRUE(ResourceNodePool::Instance().Insert(resourceNode));
428 
429     IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
430     EXPECT_EQ(service.AddCredential(testUserId, testCredPara, testCallback, false), SUCCESS);
431     promise.get_future().get();
432 
433     EXPECT_TRUE(ResourceNodePool::Instance().Delete(60));
434     IpcCommon::DeleteAllPermission();
435 }
436 
437 HWTEST_F(UserIdmServiceTest, UserIdmServiceUpdateCredential001, TestSize.Level0)
438 {
439     UserIdmService service(123123, true);
440     int32_t testUserId = 1548545;
441     IpcCredentialPara testCredPara = {};
442     testCredPara.authType = FACE;
443     testCredPara.pinType = PIN_SIX;
444     testCredPara.token = {1, 2, 3, 4};
445     sptr<MockIdmCallback> testCallback(new (std::nothrow) MockIdmCallback());
446     EXPECT_NE(testCallback, nullptr);
447     EXPECT_CALL(*testCallback, OnResult(_, _))
448         .WillOnce(
__anond8f79fc60b02(int32_t result, const std::vector<uint8_t> &extraInfo) 449             [](int32_t result, const std::vector<uint8_t> &extraInfo) {
450                 EXPECT_EQ(result, NOT_ENROLLED);
451                 return SUCCESS;
452             }
453         );
454     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
455     EXPECT_NE(mockHdi, nullptr);
456     EXPECT_CALL(*mockHdi, GetCredential(_, _, _)).WillRepeatedly(Return(HDF_SUCCESS));
457     IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
458     EXPECT_EQ(service.UpdateCredential(testUserId, testCredPara, testCallback), SUCCESS);
459     IpcCommon::DeleteAllPermission();
460 }
461 
462 HWTEST_F(UserIdmServiceTest, UserIdmServiceUpdateCredential002, TestSize.Level0)
463 {
464     UserIdmService service(123123, true);
465     int32_t testUserId = 1548545;
466     IpcCredentialPara testCredPara = {};
467     testCredPara.authType = FACE;
468     testCredPara.pinType = PIN_SIX;
469     sptr<MockIdmCallback> testCallback(nullptr);
470 
471     IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
472     EXPECT_EQ(service.UpdateCredential(testUserId, testCredPara, testCallback), INVALID_PARAMETERS);
473 
474     testCallback = sptr<MockIdmCallback>(new (std::nothrow) MockIdmCallback());
475     EXPECT_NE(testCallback, nullptr);
476     EXPECT_CALL(*testCallback, OnResult(_, _))
477         .WillOnce(
__anond8f79fc60c02(int32_t result, const std::vector<uint8_t> &extraInfo) 478             [](int32_t result, const std::vector<uint8_t> &extraInfo) {
479                 EXPECT_EQ(result, HDF_FAILURE);
480                 return SUCCESS;
481             }
482         );
483 
484     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
485     EXPECT_NE(mockHdi, nullptr);
486     EXPECT_CALL(*mockHdi, GetCredential(_, _, _))
487         .WillOnce(
__anond8f79fc60d02(int32_t userId, int32_t authType, std::vector<HdiCredentialInfo> &infos) 488             [](int32_t userId, int32_t authType, std::vector<HdiCredentialInfo> &infos) {
489                 HdiCredentialInfo tempInfo = {
490                     .credentialId = 1,
491                     .executorIndex = 2,
492                     .templateId = 3,
493                     .authType = static_cast<HdiAuthType>(2),
494                     .executorMatcher = 2,
495                     .executorSensorHint = 3,
496                 };
497                 infos.push_back(tempInfo);
498                 return HDF_SUCCESS;
499             }
500         );
501 
502     EXPECT_CALL(*mockHdi, BeginEnrollment(_, _, _)).WillOnce(Return(HDF_FAILURE));
503 
504     testCredPara.token = {1, 2, 3, 4};
505     EXPECT_EQ(service.UpdateCredential(testUserId, testCredPara, testCallback), GENERAL_ERROR);
506     IpcCommon::DeleteAllPermission();
507 }
508 
509 HWTEST_F(UserIdmServiceTest, UserIdmServiceCancel001, TestSize.Level0)
510 {
511     UserIdmService service(123123, true);
512     int32_t testUserId = 154835;
513     int32_t ret = service.Cancel(testUserId);
514     EXPECT_EQ(ret, CHECK_PERMISSION_FAILED);
515     IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
516     ret = service.Cancel(testUserId);
517     EXPECT_EQ(ret, GENERAL_ERROR);
518     IpcCommon::DeleteAllPermission();
519 }
520 
521 HWTEST_F(UserIdmServiceTest, UserIdmServiceCancel002, TestSize.Level0)
522 {
523     UserIdmService service(123123, true);
524     int32_t testUserId = 69874;
525     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
526     EXPECT_NE(mockHdi, nullptr);
527     EXPECT_CALL(*mockHdi, OpenSession(_, _)).WillOnce(Return(HDF_SUCCESS));
528 
529     IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
530     std::vector<uint8_t> challenge;
531     int32_t ret = service.OpenSession(testUserId, challenge);
532     EXPECT_EQ(ret, SUCCESS);
533     ret = service.Cancel(testUserId);
534     EXPECT_EQ(ret, GENERAL_ERROR);
535     IpcCommon::DeleteAllPermission();
536 }
537 
538 HWTEST_F(UserIdmServiceTest, UserIdmServiceCancel003, TestSize.Level0)
539 {
540     UserIdmService service(123123, true);
541     int32_t testUserId = 96874;
542     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
543     EXPECT_NE(mockHdi, nullptr);
544     EXPECT_CALL(*mockHdi, OpenSession(_, _)).WillOnce(Return(HDF_SUCCESS));
545 
546     IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
547     std::vector<uint8_t> challenge;
548     int32_t ret = service.OpenSession(testUserId, challenge);
549     EXPECT_EQ(ret, SUCCESS);
550 
551     auto context = Common::MakeShared<MockContext>();
552     EXPECT_NE(context, nullptr);
553     EXPECT_CALL(*context, GetContextType()).WillRepeatedly(Return(CONTEXT_ENROLL));
554     EXPECT_CALL(*context, GetContextId()).WillRepeatedly(Return(2345));
555     EXPECT_CALL(*context, GetUserId()).WillRepeatedly(Return(testUserId));
556     EXPECT_CALL(*context, Stop()).WillRepeatedly(Return(true));
557 
558     ret = service.Cancel(testUserId);
559     EXPECT_EQ(ret, GENERAL_ERROR);
560     IpcCommon::DeleteAllPermission();
561 }
562 
563 HWTEST_F(UserIdmServiceTest, UserIdmServiceEnforceDelUser001, TestSize.Level0)
564 {
565     UserIdmService service(123123, true);
566     int32_t testUserId = 15485;
567     sptr<MockIdmCallback> testCallback(new (std::nothrow) MockIdmCallback());
568     EXPECT_NE(testCallback, nullptr);
569     EXPECT_CALL(*testCallback, OnResult(_, _))
570         .Times(2)
571         .WillOnce(
__anond8f79fc60e02(int32_t result, const std::vector<uint8_t> &extraInfo) 572             [](int32_t result, const std::vector<uint8_t> &extraInfo) {
573                 EXPECT_EQ(result, CHECK_PERMISSION_FAILED);
574                 return SUCCESS;
575             }
576         )
577         .WillOnce(
__anond8f79fc60f02(int32_t result, const std::vector<uint8_t> &extraInfo) 578             [](int32_t result, const std::vector<uint8_t> &extraInfo) {
579                 EXPECT_EQ(result, GENERAL_ERROR);
580                 return SUCCESS;
581             }
582         );
583 
584     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
585     EXPECT_NE(mockHdi, nullptr);
586     EXPECT_CALL(*mockHdi, GetUserInfo(_, _, _, _)).WillOnce(Return(HDF_FAILURE));
587 
588     int32_t ret = service.EnforceDelUser(testUserId, testCallback);
589     EXPECT_EQ(ret, CHECK_PERMISSION_FAILED);
590     IpcCommon::AddPermission(ENFORCE_USER_IDM);
591     ret = service.EnforceDelUser(testUserId, testCallback);
592     EXPECT_EQ(ret, GENERAL_ERROR);
593     IpcCommon::DeleteAllPermission();
594 }
595 
596 HWTEST_F(UserIdmServiceTest, UserIdmServiceEnforceDelUser002, TestSize.Level0)
597 {
598     UserIdmService service(123123, true);
599     int32_t testUserId = 15485;
600     sptr<IIamCallback> testCallback(nullptr);
601     int32_t ret = service.EnforceDelUser(testUserId, testCallback);
602     EXPECT_EQ(ret, INVALID_PARAMETERS);
603 }
604 
MockForDelUserHdi()605 static void MockForDelUserHdi()
606 {
607     const uint32_t testAuthType = 1;
608     const uint32_t testCredentialId = 10;
609     const uint32_t testExecutorIndex = 20;
610     const uint32_t testExecutorMatcher = 30;
611     const uint32_t testExecutorSensorHint = 40;
612     const uint32_t testTemplateId = 50;
613     const uint32_t testSecureUid = 4542;
614     const uint32_t testTimes = 2;
615     const uint32_t testPinSubType = 10000;
616     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
617     EXPECT_NE(mockHdi, nullptr);
618     EXPECT_CALL(*mockHdi, GetUserInfo(_, _, _, _))
619         .WillRepeatedly([](int32_t userId, uint64_t &secureUid, int32_t& pinSubType,
620             std::vector<HdiEnrolledInfo> &infos) {
621             HdiEnrolledInfo info = {
622                 .enrolledId = 0,
623                 .authType = static_cast<HdiAuthType>(1),
624             };
625             infos.push_back(info);
626             pinSubType = static_cast<HdiPinSubType>(testPinSubType);
627             secureUid = testSecureUid;
628             return HDF_SUCCESS;
629         });
630 
631     EXPECT_CALL(*mockHdi, EnforceDeleteUser(_, _))
632         .Times(testTimes)
633         .WillOnce(Return(HDF_FAILURE))
634         .WillOnce([](int32_t userId, std::vector<HdiCredentialInfo> &deletedInfos) {
635             HdiCredentialInfo info = {};
636             info.authType = static_cast<HdiAuthType>(testAuthType);
637             info.credentialId = testCredentialId;
638             info.executorIndex = testExecutorIndex;
639             info.executorMatcher = testExecutorMatcher;
640             info.executorSensorHint = testExecutorSensorHint;
641             info.templateId = testTemplateId;
642             deletedInfos.emplace_back(info);
643             return HDF_SUCCESS;
644         });
645 }
646 
647 HWTEST_F(UserIdmServiceTest, UserIdmServiceEnforceDelUser003, TestSize.Level0)
648 {
649     UserIdmService service(123123, true);
650     int32_t testUserId = 15485;
651     sptr<MockIdmCallback> testCallback(new (std::nothrow) MockIdmCallback());
652     EXPECT_NE(testCallback, nullptr);
653     EXPECT_CALL(*testCallback, OnResult(_, _))
654         .Times(2)
655         .WillOnce(
__anond8f79fc61202(int32_t result, const std::vector<uint8_t> &extraInfo) 656             [](int32_t result, const std::vector<uint8_t> &extraInfo) {
657                 EXPECT_EQ(result, HDF_FAILURE);
658                 return SUCCESS;
659             }
660         )
661         .WillOnce(
__anond8f79fc61302(int32_t result, const std::vector<uint8_t> &extraInfo) 662             [](int32_t result, const std::vector<uint8_t> &extraInfo) {
663                 EXPECT_EQ(result, SUCCESS);
664                 return SUCCESS;
665             }
666         );
667     MockForDelUserHdi();
668     IpcCommon::AddPermission(ENFORCE_USER_IDM);
669     int32_t ret = service.EnforceDelUser(testUserId, testCallback);
670     EXPECT_EQ(ret, -1);
671     ret = service.EnforceDelUser(testUserId, testCallback);
672     EXPECT_EQ(ret, SUCCESS);
673     IpcCommon::DeleteAllPermission();
674 }
675 
676 HWTEST_F(UserIdmServiceTest, UserIdmServiceDelUser001, TestSize.Level0)
677 {
678     UserIdmService service(123123, true);
679     int32_t testUserId = 15486465;
680     std::vector<uint8_t> testAuthToken = {1, 2, 3, 4};
681 
682     sptr<MockIdmCallback> testCallback(new (std::nothrow) MockIdmCallback());
683     EXPECT_NE(testCallback, nullptr);
684     EXPECT_CALL(*testCallback, OnResult(_, _))
685         .Times(2)
686         .WillOnce(
__anond8f79fc61402(int32_t result, const std::vector<uint8_t> &extraInfo) 687             [](int32_t result, const std::vector<uint8_t> &extraInfo) {
688                 EXPECT_EQ(result, CHECK_PERMISSION_FAILED);
689                 return SUCCESS;
690             }
691         )
692         .WillOnce(
__anond8f79fc61502(int32_t result, const std::vector<uint8_t> &extraInfo) 693             [](int32_t result, const std::vector<uint8_t> &extraInfo) {
694                 EXPECT_EQ(result, HDF_FAILURE);
695                 return SUCCESS;
696             }
697         );
698 
699     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
700     EXPECT_NE(mockHdi, nullptr);
701     EXPECT_CALL(*mockHdi, DeleteUser(_, _, _, _)).WillOnce(Return(HDF_FAILURE));
702 
703     EXPECT_EQ(service.DelUser(testUserId, testAuthToken, testCallback), CHECK_PERMISSION_FAILED);
704     IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
705     EXPECT_EQ(service.DelUser(testUserId, testAuthToken, testCallback), HDF_FAILURE);
706     IpcCommon::DeleteAllPermission();
707 }
708 
709 HWTEST_F(UserIdmServiceTest, UserIdmServiceDelUser002, TestSize.Level0)
710 {
711     UserIdmService service(123123, true);
712     int32_t testUserId = 15486465;
713     std::vector<uint8_t> testAuthToken = {1, 2, 3, 4};
714     sptr<IIamCallback> testCallback(nullptr);
715     EXPECT_EQ(service.DelUser(testUserId, testAuthToken, testCallback), INVALID_PARAMETERS);
716 }
717 
718 HWTEST_F(UserIdmServiceTest, UserIdmServiceDelUser003, TestSize.Level0)
719 {
720     UserIdmService service(123123, true);
721     int32_t testUserId = 15486465;
722     std::vector<uint8_t> testAuthToken;
723     sptr<MockIdmCallback> testCallback(new (std::nothrow) MockIdmCallback());
724     EXPECT_NE(testCallback, nullptr);
725     EXPECT_CALL(*testCallback, OnResult(_, _))
726         .WillOnce(
__anond8f79fc61602(int32_t result, const std::vector<uint8_t> &extraInfo) 727             [](int32_t result, const std::vector<uint8_t> &extraInfo) {
728                 EXPECT_EQ(result, SUCCESS);
729                 return SUCCESS;
730             }
731         );
732 
733     IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
734     EXPECT_EQ(service.DelUser(testUserId, testAuthToken, testCallback), SUCCESS);
735     IpcCommon::DeleteAllPermission();
736 }
737 
738 HWTEST_F(UserIdmServiceTest, UserIdmServiceDelUser004, TestSize.Level0)
739 {
740     UserIdmService service(123123, true);
741     int32_t testUserId = 15486465;
742     std::vector<uint8_t> testAuthToken = {1, 2, 3, 4};
743     sptr<MockIdmCallback> testCallback(new (std::nothrow) MockIdmCallback());
744     EXPECT_NE(testCallback, nullptr);
745     EXPECT_CALL(*testCallback, OnResult(_, _))
746         .WillOnce(
__anond8f79fc61702(int32_t result, const std::vector<uint8_t> &extraInfo) 747             [](int32_t result, const std::vector<uint8_t> &extraInfo) {
748                 EXPECT_EQ(result, SUCCESS);
749                 return SUCCESS;
750             }
751         );
752 
753     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
754     EXPECT_NE(mockHdi, nullptr);
755     EXPECT_CALL(*mockHdi, DeleteUser(_, _, _, _))
756         .WillOnce(
757             [](int32_t userId, const std::vector<uint8_t> &authToken, std::vector<HdiCredentialInfo> &deletedInfos,
__anond8f79fc61802(int32_t userId, const std::vector<uint8_t> &authToken, std::vector<HdiCredentialInfo> &deletedInfos, std::vector<uint8_t> &rootSecret) 758             std::vector<uint8_t> &rootSecret) {
759                 HdiCredentialInfo info = {};
760                 info.authType = static_cast<HdiAuthType>(1);
761                 info.credentialId = 10;
762                 info.executorIndex = 20;
763                 info.executorMatcher = 30;
764                 info.executorSensorHint = 40;
765                 info.templateId = 50;
766                 deletedInfos.emplace_back(info);
767                 return HDF_SUCCESS;
768             }
769         );
770 
771     IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
772     EXPECT_EQ(service.DelUser(testUserId, testAuthToken, testCallback), SUCCESS);
773     IpcCommon::DeleteAllPermission();
774 }
775 
776 HWTEST_F(UserIdmServiceTest, UserIdmServiceDelCredential001, TestSize.Level0)
777 {
778     UserIdmService service(123123, true);
779     int32_t testUserId = 1548865;
780     uint64_t testCredentialId = 23424;
781     std::vector<uint8_t> testAuthToken = {1, 2, 3, 4};
782 
783     sptr<MockIdmCallback> testCallback(new (std::nothrow) MockIdmCallback());
784     EXPECT_NE(testCallback, nullptr);
785     EXPECT_CALL(*testCallback, OnResult(_, _))
786         .Times(2)
787         .WillOnce(
__anond8f79fc61902(int32_t result, const std::vector<uint8_t> &extraInfo) 788             [](int32_t result, const std::vector<uint8_t> &extraInfo) {
789                 EXPECT_EQ(result, CHECK_PERMISSION_FAILED);
790                 return SUCCESS;
791             }
792         )
793         .WillOnce(
__anond8f79fc61a02(int32_t result, const std::vector<uint8_t> &extraInfo) 794             [](int32_t result, const std::vector<uint8_t> &extraInfo) {
795                 EXPECT_EQ(result, GENERAL_ERROR);
796                 return SUCCESS;
797             }
798         );
799 
800     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
801     EXPECT_NE(mockHdi, nullptr);
802     EXPECT_CALL(*mockHdi, DeleteCredential(_, _, _, _)).WillOnce(Return(HDF_FAILURE));
803 
804     EXPECT_EQ(service.DelCredential(testUserId, testCredentialId, testAuthToken, testCallback),
805         CHECK_PERMISSION_FAILED);
806     IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
807     EXPECT_EQ(service.DelCredential(testUserId, testCredentialId, testAuthToken, testCallback),
808         GENERAL_ERROR);
809     IpcCommon::DeleteAllPermission();
810 }
811 
812 HWTEST_F(UserIdmServiceTest, UserIdmServiceDelCredential002, TestSize.Level0)
813 {
814     UserIdmService service(123123, true);
815     int32_t testUserId = 1548865;
816     uint64_t testCredentialId = 23424;
817     std::vector<uint8_t> testAuthToken = {1, 2, 3, 4};
818     sptr<IIamCallback> testCallback(nullptr);
819     EXPECT_EQ(service.DelCredential(testUserId, testCredentialId, testAuthToken, testCallback), INVALID_PARAMETERS);
820 }
821 
822 HWTEST_F(UserIdmServiceTest, UserIdmServiceDelCredential003, TestSize.Level0)
823 {
824     UserIdmService service(123123, true);
825     int32_t testUserId = 1548865;
826     uint64_t testCredentialId = 23424;
827     std::vector<uint8_t> testAuthToken = {1, 2, 3, 4};
828 
829     sptr<MockIdmCallback> testCallback(new (std::nothrow) MockIdmCallback());
830     EXPECT_NE(testCallback, nullptr);
831     EXPECT_CALL(*testCallback, OnResult(_, _))
832         .WillOnce(
__anond8f79fc61b02(int32_t result, const std::vector<uint8_t> &extraInfo) 833             [](int32_t result, const std::vector<uint8_t> &extraInfo) {
834                 EXPECT_EQ(result, SUCCESS);
835                 return SUCCESS;
836             }
837         );
838 
839     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
840     EXPECT_NE(mockHdi, nullptr);
841     EXPECT_CALL(*mockHdi, DeleteCredential(_, _, _, _))
842         .WillOnce(
843             [](int32_t userId, uint64_t credentialId, const std::vector<uint8_t> &authToken,
__anond8f79fc61c02(int32_t userId, uint64_t credentialId, const std::vector<uint8_t> &authToken, HdiCredentialOperateResult &operateResult) 844                 HdiCredentialOperateResult &operateResult) {
845                 operateResult.operateType = HdiCredentialOperateType::CREDENTIAL_DELETE;
846                 HdiCredentialInfo credentialInfo = {};
847                 credentialInfo.authType = static_cast<HdiAuthType>(1);
848                 credentialInfo.credentialId = 10;
849                 credentialInfo.executorIndex = 20;
850                 credentialInfo.executorMatcher = 30;
851                 credentialInfo.executorSensorHint = 40;
852                 credentialInfo.templateId = 50;
853                 operateResult.credentialInfos.push_back(credentialInfo);
854                 return HDF_SUCCESS;
855             }
856         );
857 
858     IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
859     EXPECT_EQ(service.DelCredential(testUserId, testCredentialId, testAuthToken, testCallback), SUCCESS);
860     IpcCommon::DeleteAllPermission();
861 }
862 
863 HWTEST_F(UserIdmServiceTest, UserIdmServiceTestDump, TestSize.Level0)
864 {
865     int testFd1 = -1;
866     int testFd2 = 1;
867     std::vector<std::u16string> testArgs;
868 
869     UserIdmService service(123123, true);
870 
871     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
872     EXPECT_NE(mockHdi, nullptr);
873     EXPECT_CALL(*mockHdi, GetUserInfo(_, _, _, _))
874         .Times(2)
875         .WillOnce(Return(HDF_FAILURE))
876         .WillOnce(
__anond8f79fc61d02(int32_t userId, uint64_t &secureUid, int32_t &pinSubType, std::vector<HdiEnrolledInfo> &infos) 877             [](int32_t userId, uint64_t &secureUid, int32_t &pinSubType, std::vector<HdiEnrolledInfo> &infos) {
878                 HdiEnrolledInfo info = {
879                     .enrolledId = 0,
880                     .authType = static_cast<HdiAuthType>(1),
881                 };
882                 infos.push_back(info);
883                 pinSubType = static_cast<HdiPinSubType>(10000);
884                 secureUid = 4542;
885                 return HDF_SUCCESS;
886             }
887         );
888 
889     EXPECT_EQ(service.Dump(testFd1, testArgs), INVALID_PARAMETERS);
890     EXPECT_EQ(service.Dump(testFd2, testArgs), SUCCESS);
891     testArgs.push_back(u"-h");
892     EXPECT_EQ(service.Dump(testFd2, testArgs), SUCCESS);
893     testArgs.clear();
894     testArgs.push_back(u"-l");
895     EXPECT_EQ(service.Dump(testFd2, testArgs), GENERAL_ERROR);
896     EXPECT_EQ(service.Dump(testFd2, testArgs), SUCCESS);
897     testArgs.clear();
898     testArgs.push_back(u"-k");
899     EXPECT_EQ(service.Dump(testFd2, testArgs), GENERAL_ERROR);
900 }
901 
902 HWTEST_F(UserIdmServiceTest, UserIdmServiceClearRedundancyCredential001, TestSize.Level0)
903 {
904     UserIdmService service(123123, true);
905     sptr<MockIdmCallback> testCallback(new (std::nothrow) MockIdmCallback());
906     EXPECT_NE(testCallback, nullptr);
907     EXPECT_CALL(*testCallback, OnResult(_, _))
908         .Times(2)
909         .WillOnce(
__anond8f79fc61e02(int32_t result, const std::vector<uint8_t> &extraInfo) 910             [](int32_t result, const std::vector<uint8_t> &extraInfo) {
911                 EXPECT_EQ(result, CHECK_PERMISSION_FAILED);
912                 return SUCCESS;
913             }
914         )
915         .WillOnce(
__anond8f79fc61f02(int32_t result, const std::vector<uint8_t> &extraInfo) 916             [](int32_t result, const std::vector<uint8_t> &extraInfo) {
917                 EXPECT_EQ(result, INVALID_HDI_INTERFACE);
918                 return SUCCESS;
919             }
920         );
921 
922     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
923     EXPECT_NE(mockHdi, nullptr);
924     EXPECT_CALL(*mockHdi, GetAllExtUserInfo(_)).WillOnce(Return(HDF_FAILURE));
925 
926     EXPECT_EQ(service.ClearRedundancyCredential(testCallback), CHECK_PERMISSION_FAILED);
927     IpcCommon::AddPermission(CLEAR_REDUNDANCY_PERMISSION);
928     EXPECT_EQ(service.ClearRedundancyCredential(testCallback), INVALID_HDI_INTERFACE);
929     IpcCommon::DeleteAllPermission();
930 }
931 
932 HWTEST_F(UserIdmServiceTest, UserIdmServiceClearRedundancyCredential002, TestSize.Level0)
933 {
934     UserIdmService service(123123, true);
935     sptr<MockIdmCallback> testCallback(new (std::nothrow) MockIdmCallback());
936     EXPECT_NE(testCallback, nullptr);
937     EXPECT_CALL(*testCallback, OnResult(_, _))
938         .Times(2)
939         .WillOnce(
__anond8f79fc62002(int32_t result, const std::vector<uint8_t> &extraInfo) 940             [](int32_t result, const std::vector<uint8_t> &extraInfo) {
941                 EXPECT_EQ(result, HDF_SUCCESS);
942                 return SUCCESS;
943             }
944         )
945         .WillOnce(
__anond8f79fc62102(int32_t result, const std::vector<uint8_t> &extraInfo) 946             [](int32_t result, const std::vector<uint8_t> &extraInfo) {
947                 EXPECT_EQ(result, HDF_SUCCESS);
948                 return SUCCESS;
949             }
950         );
951 
952     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
953     EXPECT_NE(mockHdi, nullptr);
954     EXPECT_CALL(*mockHdi, GetAllExtUserInfo(_))
955         .WillRepeatedly(
__anond8f79fc62202(std::vector<ExtUserInfo> &userInfos) 956             [](std::vector<ExtUserInfo> &userInfos) {
957                 ExtUserInfo info = { .userId = 100, };
958                 userInfos.push_back(info);
959                 return HDF_SUCCESS;
960             }
961         );
962 
963     EXPECT_CALL(*mockHdi, EnforceDeleteUser(_, _))
964         .Times(2)
965         .WillOnce(Return(HDF_FAILURE))
966         .WillOnce(
__anond8f79fc62302(int32_t userId, std::vector<HdiCredentialInfo> &deletedInfos) 967             [](int32_t userId, std::vector<HdiCredentialInfo> &deletedInfos) {
968                 HdiCredentialInfo info = {};
969                 info.authType = static_cast<HdiAuthType>(1);
970                 info.credentialId = 10;
971                 info.executorIndex = 20;
972                 info.executorMatcher = 30;
973                 info.executorSensorHint = 40;
974                 info.templateId = 50;
975                 deletedInfos.emplace_back(info);
976                 return HDF_SUCCESS;
977             }
978         );
979 
980     IpcCommon::AddPermission(CLEAR_REDUNDANCY_PERMISSION);
981     EXPECT_EQ(service.ClearRedundancyCredential(testCallback), SUCCESS);
982     EXPECT_EQ(service.ClearRedundancyCredential(testCallback), SUCCESS);
983     IpcCommon::DeleteAllPermission();
984 }
985 
986 HWTEST_F(UserIdmServiceTest, UserIdmServiceRegistEventListerner_001, TestSize.Level0)
987 {
988     UserIdmService service(123123, true);
989     sptr<IEventListenerCallback> testCallback = nullptr;
990     IpcCommon::AddPermission(IS_SYSTEM_APP);
991     EXPECT_EQ(service.RegistCredChangeEventListener(testCallback), ResultCode::INVALID_PARAMETERS);
992     IpcCommon::DeleteAllPermission();
993 }
994 
995 HWTEST_F(UserIdmServiceTest, UserIdmServiceRegistEventListerner_003, TestSize.Level0)
996 {
997     UserIdmService service(123123, true);
998     sptr<IEventListenerCallback> testCallback = new MockEventListener();
999     IpcCommon::AddPermission(IS_SYSTEM_APP);
1000     EXPECT_EQ(service.RegistCredChangeEventListener(testCallback),
1001         ResultCode::CHECK_PERMISSION_FAILED);
1002     IpcCommon::DeleteAllPermission();
1003 }
1004 
1005 HWTEST_F(UserIdmServiceTest, UserIdmServiceRegistEventListerner_004, TestSize.Level0)
1006 {
1007     UserIdmService service(123123, true);
1008     sptr<IEventListenerCallback> testCallback = new MockEventListener();
1009     IpcCommon::AddPermission(USE_USER_IDM_PERMISSION);
1010     EXPECT_EQ(service.RegistCredChangeEventListener(testCallback), ResultCode::GENERAL_ERROR);
1011     IpcCommon::DeleteAllPermission();
1012 }
1013 
1014 HWTEST_F(UserIdmServiceTest, UserIdmServiceUnRegistEventListerner_001, TestSize.Level0)
1015 {
1016     UserIdmService service(123123, true);
1017     sptr<IEventListenerCallback> testCallback = nullptr;
1018     IpcCommon::AddPermission(IS_SYSTEM_APP);
1019     EXPECT_EQ(service.UnRegistCredChangeEventListener(testCallback), ResultCode::INVALID_PARAMETERS);
1020     IpcCommon::DeleteAllPermission();
1021 }
1022 
1023 HWTEST_F(UserIdmServiceTest, UserIdmServiceUnRegistEventListerner_002, TestSize.Level0)
1024 {
1025     UserIdmService service(123123, true);
1026     sptr<IEventListenerCallback> testCallback = new MockEventListener();
1027     IpcCommon::AddPermission(IS_SYSTEM_APP);
1028     EXPECT_EQ(service.UnRegistCredChangeEventListener(testCallback), ResultCode::CHECK_PERMISSION_FAILED);
1029     IpcCommon::DeleteAllPermission();
1030 }
1031 
1032 HWTEST_F(UserIdmServiceTest, UserIdmServiceUnRegistEventListerner_003, TestSize.Level0)
1033 {
1034     UserIdmService service(123123, true);
1035     sptr<IEventListenerCallback> testCallback = new MockEventListener();
1036     IpcCommon::AddPermission(USE_USER_IDM_PERMISSION);
1037     EXPECT_EQ(service.UnRegistCredChangeEventListener(testCallback), ResultCode::GENERAL_ERROR);
1038     IpcCommon::DeleteAllPermission();
1039 }
1040 
1041 HWTEST_F(UserIdmServiceTest, UserIdmServiceUnRegistEventListerner_004, TestSize.Level0)
1042 {
1043     UserIdmService service(123123, true);
1044     sptr<IEventListenerCallback> testCallback = new MockEventListener();
1045     IpcCommon::AddPermission(USE_USER_IDM_PERMISSION);
1046     EXPECT_EQ(service.RegistCredChangeEventListener(testCallback), ResultCode::GENERAL_ERROR);
1047     EXPECT_EQ(service.UnRegistCredChangeEventListener(testCallback), ResultCode::GENERAL_ERROR);
1048     IpcCommon::DeleteAllPermission();
1049 }
1050 
1051 HWTEST_F(UserIdmServiceTest, UserIdmServiceGetCredentialInfoSync001, TestSize.Level0)
1052 {
1053     UserIdmService service(123123, true);
1054     int32_t testUserId = 0;
1055     AuthType testAuthType = PIN;
1056     std::vector<IpcCredentialInfo> credentialInfoList;
1057 
1058     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
1059     EXPECT_NE(mockHdi, nullptr);
1060     EXPECT_CALL(*mockHdi, GetCredential(_, _, _)).WillOnce(Return(HDF_FAILURE));
1061     int32_t ret = service.GetCredentialInfoSync(testUserId, testAuthType, credentialInfoList);
1062     EXPECT_EQ(ret, CHECK_PERMISSION_FAILED);
1063     IpcCommon::AddPermission(USE_USER_IDM_PERMISSION);
1064     ret = service.GetCredentialInfoSync(testUserId, testAuthType, credentialInfoList);
1065     EXPECT_EQ(ret, GENERAL_ERROR);
1066     IpcCommon::DeleteAllPermission();
1067 }
1068 
1069 HWTEST_F(UserIdmServiceTest, UserIdmServiceGetCredentialInfoSync002, TestSize.Level0)
1070 {
1071     UserIdmService service(123123, true);
1072     int32_t testUserId = 0;
1073     AuthType testAuthType = PIN;
1074     std::vector<IpcCredentialInfo> credentialInfoList;
1075 
1076     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
1077     EXPECT_NE(mockHdi, nullptr);
1078     EXPECT_CALL(*mockHdi, GetCredential(_, _, _)).Times(1);
1079     ON_CALL(*mockHdi, GetCredential)
1080         .WillByDefault(
__anond8f79fc62402(int32_t userId, int32_t authType, std::vector<HdiCredentialInfo> &infos) 1081             [](int32_t userId, int32_t authType, std::vector<HdiCredentialInfo> &infos) {
1082                 return HDF_SUCCESS;
1083             }
1084         );
1085     IpcCommon::AddPermission(USE_USER_IDM_PERMISSION);
1086     int32_t ret = service.GetCredentialInfoSync(testUserId, testAuthType, credentialInfoList);
1087     EXPECT_EQ(ret, SUCCESS);
1088     IpcCommon::DeleteAllPermission();
1089 }
1090 
1091 HWTEST_F(UserIdmServiceTest, UserIdmServiceGetCredentialInfoSync003, TestSize.Level0)
1092 {
1093     UserIdmService service(123123, true);
1094     int32_t testUserId = 0;
1095     AuthType testAuthType = PIN;
1096     std::vector<IpcCredentialInfo> credentialInfoList;
1097 
1098     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
1099     EXPECT_NE(mockHdi, nullptr);
1100     EXPECT_CALL(*mockHdi, GetCredential(_, _, _)).Times(1);
1101     ON_CALL(*mockHdi, GetCredential)
1102         .WillByDefault(
__anond8f79fc62502(int32_t userId, int32_t authType, std::vector<HdiCredentialInfo> &infos) 1103             [](int32_t userId, int32_t authType, std::vector<HdiCredentialInfo> &infos) {
1104                 HdiCredentialInfo tempInfo = {
1105                     .credentialId = 1,
1106                     .executorIndex = 2,
1107                     .templateId = 3,
1108                     .authType = static_cast<HdiAuthType>(1),
1109                     .executorMatcher = 2,
1110                     .executorSensorHint = 3,
1111                 };
1112                 infos.push_back(tempInfo);
1113                 return HDF_SUCCESS;
1114             }
1115         );
1116     IpcCommon::AddPermission(USE_USER_IDM_PERMISSION);
1117     int32_t ret = service.GetCredentialInfoSync(testUserId, testAuthType, credentialInfoList);
1118     EXPECT_EQ(ret, SUCCESS);
1119     IpcCommon::DeleteAllPermission();
1120 }
1121 
1122 HWTEST_F(UserIdmServiceTest, UserIdmServiceClearUnavailableCredential001, TestSize.Level0)
1123 {
1124     UserIdmService service(123123, true);
1125     int32_t testUserId = 1;
1126 
1127     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
1128     EXPECT_NE(mockHdi, nullptr);
1129     EXPECT_CALL(*mockHdi, ClearUnavailableCredential(_, _)).WillOnce(Return(HDF_FAILURE));
1130     service.ClearUnavailableCredential(testUserId);
1131 }
1132 
1133 HWTEST_F(UserIdmServiceTest, UserIdmServiceClearUnavailableCredential002, TestSize.Level0)
1134 {
1135     UserIdmService service(123123, true);
1136     int32_t testUserId = 1;
1137 
1138     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
1139     EXPECT_NE(mockHdi, nullptr);
1140     EXPECT_CALL(*mockHdi, ClearUnavailableCredential(_, _)).Times(1);
1141     ON_CALL(*mockHdi, ClearUnavailableCredential)
1142         .WillByDefault(
__anond8f79fc62602(const std::vector<int32_t>& userIds, std::vector<HdiCredentialInfo>& infos) 1143             [](const std::vector<int32_t>& userIds, std::vector<HdiCredentialInfo>& infos) {
1144                 return HDF_SUCCESS;
1145             }
1146         );
1147     service.ClearUnavailableCredential(testUserId);
1148 }
1149 
1150 HWTEST_F(UserIdmServiceTest, UserIdmServiceClearUnavailableCredential003, TestSize.Level0)
1151 {
1152     UserIdmService service(123123, true);
1153     int32_t testUserId = 1;
1154     std::vector<IpcCredentialInfo> credentialInfoList;
1155 
1156     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
1157     EXPECT_NE(mockHdi, nullptr);
1158     EXPECT_CALL(*mockHdi, ClearUnavailableCredential(_, _)).Times(1);
1159     ON_CALL(*mockHdi, ClearUnavailableCredential)
1160         .WillByDefault(
__anond8f79fc62702(const std::vector<int32_t>& userIds, std::vector<HdiCredentialInfo>& infos) 1161             [](const std::vector<int32_t>& userIds, std::vector<HdiCredentialInfo>& infos) {
1162                 HdiCredentialInfo tempInfo = {
1163                     .credentialId = 1,
1164                     .executorIndex = 2,
1165                     .templateId = 3,
1166                     .authType = static_cast<HdiAuthType>(1),
1167                     .executorMatcher = 2,
1168                     .executorSensorHint = 3,
1169                 };
1170                 infos.push_back(tempInfo);
1171                 return HDF_SUCCESS;
1172             }
1173         );
1174     service.ClearUnavailableCredential(testUserId);
1175 }
1176 
1177 HWTEST_F(UserIdmServiceTest, UserIdmServiceOpenSession, TestSize.Level0)
1178 {
1179     UserIdmService service(123123, true);
1180     int32_t testUserId = 1;
1181     std::vector<uint8_t> challenge;
1182     EXPECT_EQ(CHECK_PERMISSION_FAILED, service.OpenSession(testUserId, challenge));
1183     EXPECT_EQ(CHECK_PERMISSION_FAILED, service.CloseSession(testUserId));
1184 
1185     IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
1186     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
1187     EXPECT_CALL(*mockHdi, OpenSession(_, _))
1188         .WillOnce(Return(HDF_FAILURE))
1189         .WillRepeatedly(Return(HDF_SUCCESS));
1190     EXPECT_EQ(GENERAL_ERROR, service.OpenSession(testUserId, challenge));
1191     EXPECT_EQ(SUCCESS, service.OpenSession(testUserId, challenge));
1192 
1193     EXPECT_CALL(*mockHdi, CloseSession(_))
1194         .WillOnce(Return(HDF_FAILURE))
1195         .WillRepeatedly(Return(HDF_SUCCESS));
1196     EXPECT_EQ(GENERAL_ERROR, service.CloseSession(testUserId));
1197     EXPECT_EQ(SUCCESS, service.CloseSession(testUserId));
1198 }
1199 } // namespace UserAuth
1200 } // namespace UserIam
1201 } // namespace OHOS