• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022-2024 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_auth_service_test.h"
17 
18 #include <future>
19 
20 #include "iam_common_defines.h"
21 #include "iam_ptr.h"
22 
23 #include "executor_messenger_service.h"
24 #include "accesstoken_kit.h"
25 #include "mock_event_listener.h"
26 #include "mock_context.h"
27 #include "mock_iuser_auth_interface.h"
28 #include "mock_ipc_common.h"
29 #include "mock_user_auth_callback.h"
30 #include "mock_user_auth_service.h"
31 #include "mock_resource_node.h"
32 #include "mock_widget_callback_interface.h"
33 #include "resource_node_pool.h"
34 #include "user_auth_service.h"
35 
36 namespace OHOS {
37 namespace UserIam {
38 namespace UserAuth {
39 using namespace testing;
40 using namespace testing::ext;
SetUpTestCase()41 void UserAuthServiceTest::SetUpTestCase()
42 {
43 }
44 
TearDownTestCase()45 void UserAuthServiceTest::TearDownTestCase()
46 {
47 }
48 
SetUp()49 void UserAuthServiceTest::SetUp()
50 {
51     MockIUserAuthInterface::Holder::GetInstance().Reset();
52 }
53 
TearDown()54 void UserAuthServiceTest::TearDown()
55 {
56     MockIUserAuthInterface::Holder::GetInstance().Reset();
57 }
58 
59 HWTEST_F(UserAuthServiceTest, UserAuthServiceGetEnrolledState001, TestSize.Level0)
60 {
61     UserAuthService service;
62     int32_t testApiVersion = 12;
63     AuthType testAuthType = FACE;
64     IpcEnrolledState testEnrolledState;
65     uint16_t expectCredentialDigest = 23962;
66     uint16_t expectCredentialCount = 1;
67     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
68     EXPECT_NE(mockHdi, nullptr);
69     EXPECT_CALL(*mockHdi, GetEnrolledState(_, _, _))
70         .Times(1)
71         .WillOnce(
72             [expectCredentialDigest, expectCredentialCount](int32_t userId, int32_t authType,
__anona123a6f50102(int32_t userId, int32_t authType, HdiEnrolledState &hdiEnrolledState) 73                 HdiEnrolledState &hdiEnrolledState) {
74                 hdiEnrolledState.credentialDigest = expectCredentialDigest;
75                 hdiEnrolledState.credentialCount = expectCredentialCount;
76                 return HDF_SUCCESS;
77             }
78         );
79     IpcCommon::AddPermission(ACCESS_BIOMETRIC_PERMISSION);
80     int32_t funcResult = SUCCESS;
81     int32_t ret = service.GetEnrolledState(testApiVersion, testAuthType, testEnrolledState, funcResult);
82     EXPECT_EQ(funcResult, SUCCESS);
83     EXPECT_EQ(ret, SUCCESS);
84     EXPECT_EQ(expectCredentialDigest, testEnrolledState.credentialDigest);
85     EXPECT_EQ(expectCredentialCount, testEnrolledState.credentialCount);
86     IpcCommon::DeleteAllPermission();
87 }
88 
89 HWTEST_F(UserAuthServiceTest, UserAuthServiceGetEnrolledState002, TestSize.Level0)
90 {
91     UserAuthService service;
92     int32_t testApiVersion = 12;
93     AuthType testAuthType = FACE;
94     IpcEnrolledState testEnrolledState;
95     uint16_t expectCredentialDigest = 0;
96     uint16_t expectCredentialCount = 0;
97     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
98     EXPECT_NE(mockHdi, nullptr);
99     EXPECT_CALL(*mockHdi, GetEnrolledState(_, _, _)).WillOnce(Return(GENERAL_ERROR));
100     IpcCommon::AddPermission(ACCESS_BIOMETRIC_PERMISSION);
101     int32_t funcResult = SUCCESS;
102     int32_t ret = service.GetEnrolledState(testApiVersion, testAuthType, testEnrolledState, funcResult);
103     EXPECT_EQ(funcResult, GENERAL_ERROR);
104     EXPECT_EQ(ret, SUCCESS);
105     EXPECT_EQ(expectCredentialDigest, testEnrolledState.credentialDigest);
106     EXPECT_EQ(expectCredentialCount, testEnrolledState.credentialCount);
107     IpcCommon::DeleteAllPermission();
108 }
109 
110 HWTEST_F(UserAuthServiceTest, UserAuthServiceGetEnrolledState003, TestSize.Level0)
111 {
112     UserAuthService service;
113     int32_t testApiVersion = 10;
114     AuthType testAuthType = FACE;
115     IpcEnrolledState testEnrolledState;
116     uint16_t expectCredentialDigest = 0;
117     uint16_t expectCredentialCount = 0;
118     IpcCommon::AddPermission(ACCESS_BIOMETRIC_PERMISSION);
119     int32_t funcResult = SUCCESS;
120     int32_t ret = service.GetEnrolledState(testApiVersion, testAuthType, testEnrolledState, funcResult);
121     EXPECT_EQ(funcResult, TYPE_NOT_SUPPORT);
122     EXPECT_EQ(ret, SUCCESS);
123     EXPECT_EQ(expectCredentialDigest, testEnrolledState.credentialDigest);
124     EXPECT_EQ(expectCredentialCount, testEnrolledState.credentialCount);
125     IpcCommon::DeleteAllPermission();
126 }
127 
128 HWTEST_F(UserAuthServiceTest, UserAuthServiceGetEnrolledState004, TestSize.Level0)
129 {
130     UserAuthService service;
131     int32_t testApiVersion = 10;
132     AuthType testAuthType = FACE;
133     IpcEnrolledState testEnrolledState;
134     uint16_t expectCredentialDigest = 0;
135     uint16_t expectCredentialCount = 0;
136     int32_t funcResult = SUCCESS;
137     int32_t ret = service.GetEnrolledState(testApiVersion, testAuthType, testEnrolledState, funcResult);
138     EXPECT_EQ(funcResult, CHECK_PERMISSION_FAILED);
139     EXPECT_EQ(ret, SUCCESS);
140     EXPECT_EQ(expectCredentialDigest, testEnrolledState.credentialDigest);
141     EXPECT_EQ(expectCredentialCount, testEnrolledState.credentialCount);
142 }
143 
144 HWTEST_F(UserAuthServiceTest, UserAuthServiceGetAvailableStatus001, TestSize.Level0)
145 {
146     UserAuthService service;
147     int32_t testApiVersion = 10000;
148     AuthType testAuthType = FACE;
149     AuthTrustLevel testAuthTrustLevel = ATL3;
150     int32_t testUserId = 100;
151     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
152     EXPECT_NE(mockHdi, nullptr);
153     EXPECT_CALL(*mockHdi, GetAvailableStatus(_, _, _, _))
154         .Times(1)
155         .WillOnce(
__anona123a6f50202(int32_t userId, int32_t authType, uint32_t authTrustLevel, int32_t &checkRet) 156             [](int32_t userId, int32_t authType, uint32_t authTrustLevel, int32_t &checkRet) {
157                 checkRet = SUCCESS;
158                 return HDF_SUCCESS;
159             }
160         );
161     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
162     int32_t funcResult = SUCCESS;
163     int32_t ret = service.GetAvailableStatus(testApiVersion, testUserId, testAuthType, testAuthTrustLevel, funcResult);
164     EXPECT_EQ(funcResult, SUCCESS);
165     EXPECT_EQ(ret, SUCCESS);
166     IpcCommon::DeleteAllPermission();
167 }
168 
169 HWTEST_F(UserAuthServiceTest, UserAuthServiceGetAvailableStatus002, TestSize.Level0)
170 {
171     UserAuthService service;
172     int32_t testApiVersion = 2;
173     AuthType testAuthType = PIN;
174     int32_t testUserId = 100;
175     AuthTrustLevel testAuthTrustLevel = static_cast<AuthTrustLevel>(90000);
176 
177     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
178     int32_t funcResult = SUCCESS;
179     int32_t ret = service.GetAvailableStatus(testApiVersion, testUserId, testAuthType,
180         testAuthTrustLevel, funcResult);
181     EXPECT_EQ(funcResult, TRUST_LEVEL_NOT_SUPPORT);
182     EXPECT_EQ(ret, SUCCESS);
183     testApiVersion = 10000;
184     testAuthType = FACE;
185     ret = service.GetAvailableStatus(testApiVersion, testUserId, testAuthType,
186         testAuthTrustLevel, funcResult);
187     EXPECT_EQ(funcResult, TRUST_LEVEL_NOT_SUPPORT);
188     EXPECT_EQ(ret, SUCCESS);
189 
190     testAuthTrustLevel = ATL2;
191     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
192     EXPECT_NE(mockHdi, nullptr);
193     EXPECT_CALL(*mockHdi, GetAvailableStatus(_, _, _, _)).Times(1);
194     ON_CALL(*mockHdi, GetAvailableStatus)
195         .WillByDefault(
__anona123a6f50302(int32_t userId, int32_t authType, uint32_t authTrustLevel, int32_t &checkRet) 196             [](int32_t userId, int32_t authType, uint32_t authTrustLevel, int32_t &checkRet) {
197                 checkRet = TRUST_LEVEL_NOT_SUPPORT;
198                 return SUCCESS;
199             }
200         );
201     ret = service.GetAvailableStatus(testApiVersion, testUserId, testAuthType,
202         testAuthTrustLevel, funcResult);
203     EXPECT_EQ(funcResult, TRUST_LEVEL_NOT_SUPPORT);
204     EXPECT_EQ(ret, SUCCESS);
205     IpcCommon::DeleteAllPermission();
206 }
207 
208 HWTEST_F(UserAuthServiceTest, UserAuthServiceGetAvailableStatus003, TestSize.Level0)
209 {
210     UserAuthService service;
211     int32_t testApiVersion = 10000;
212     AuthType testAuthType = FACE;
213     AuthTrustLevel testAuthTrustLevel = ATL2;
214     int32_t testUserId = 100;
215 
216     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
217     EXPECT_NE(mockHdi, nullptr);
__anona123a6f50402() 218     EXPECT_CALL(*mockHdi, GetAvailableStatus(_, _, _, _)).WillRepeatedly([]() {
219         return HDF_FAILURE;
220     });
221     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
222     int32_t funcResult = SUCCESS;
223     int32_t ret = service.GetAvailableStatus(testApiVersion, testUserId, testAuthType, testAuthTrustLevel, funcResult);
224     EXPECT_EQ(funcResult, GENERAL_ERROR);
225     EXPECT_EQ(ret, SUCCESS);
226     testApiVersion = 9;
227     ret = service.GetAvailableStatus(testApiVersion, testUserId, testAuthType, testAuthTrustLevel, funcResult);
228     EXPECT_EQ(funcResult, GENERAL_ERROR);
229     EXPECT_EQ(ret, SUCCESS);
230     IpcCommon::DeleteAllPermission();
231 }
232 
233 HWTEST_F(UserAuthServiceTest, UserAuthServiceGetAvailableStatus004, TestSize.Level0)
234 {
235     int32_t testApiVersion = 10000;
236     AuthType testAuthType = PIN;
237     AuthTrustLevel testAuthTrustLevel = ATL2;
238     int32_t testUserId = 100;
239 
240     auto service = Common::MakeShared<UserAuthService>();
241     EXPECT_NE(service, nullptr);
242     int32_t funcResult = SUCCESS;
243     int32_t ret = service->GetAvailableStatus(testApiVersion, testUserId, testAuthType, testAuthTrustLevel,
244         funcResult);
245     EXPECT_EQ(funcResult, CHECK_PERMISSION_FAILED);
246     EXPECT_EQ(ret, SUCCESS);
247 
248     ret = service->GetAvailableStatus(testApiVersion, testAuthType, testAuthTrustLevel, funcResult);
249     EXPECT_EQ(funcResult, CHECK_PERMISSION_FAILED);
250     EXPECT_EQ(ret, SUCCESS);
251 
252     testAuthType = FACE;
253     ret = service->GetAvailableStatus(testUserId, testApiVersion, testAuthType, testAuthTrustLevel,
254         funcResult);
255     EXPECT_EQ(funcResult, CHECK_PERMISSION_FAILED);
256     EXPECT_EQ(ret, SUCCESS);
257 
258     IpcCommon::AddPermission(ACCESS_BIOMETRIC_PERMISSION);
259     testAuthTrustLevel = static_cast<AuthTrustLevel>(0);
260     ret = service->GetAvailableStatus(testApiVersion, testUserId, testAuthType, testAuthTrustLevel,
261         funcResult);
262     EXPECT_EQ(funcResult, CHECK_PERMISSION_FAILED);
263     EXPECT_EQ(ret, SUCCESS);
264 
265     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
266     testAuthTrustLevel = static_cast<AuthTrustLevel>(0);
267     ret = service->GetAvailableStatus(testApiVersion, testUserId, testAuthType, testAuthTrustLevel,
268         funcResult);
269     EXPECT_EQ(funcResult, TRUST_LEVEL_NOT_SUPPORT);
270     EXPECT_EQ(ret, SUCCESS);
271     IpcCommon::DeleteAllPermission();
272 }
273 
274 HWTEST_F(UserAuthServiceTest, UserAuthServiceGetAvailableStatus005, TestSize.Level0)
275 {
276     int32_t testApiVersion = 10000;
277     AuthType testAuthType = PIN;
278     AuthTrustLevel testAuthTrustLevel = ATL2;
279 
280     auto service = Common::MakeShared<UserAuthService>();
281     EXPECT_NE(service, nullptr);
282     int32_t funcResult = SUCCESS;
283     int32_t ret = service->GetAvailableStatus(testApiVersion, testAuthType, testAuthTrustLevel,
284         funcResult);
285     EXPECT_EQ(funcResult, CHECK_PERMISSION_FAILED);
286     EXPECT_EQ(ret, SUCCESS);
287 
288     ret = service->GetAvailableStatus(testApiVersion, testAuthType, testAuthTrustLevel,
289         funcResult);
290     EXPECT_EQ(funcResult, CHECK_PERMISSION_FAILED);
291     EXPECT_EQ(ret, SUCCESS);
292 
293     testAuthType = FACE;
294     ret = service->GetAvailableStatus(testApiVersion, testAuthType, testAuthTrustLevel,
295         funcResult);
296     EXPECT_EQ(funcResult, CHECK_PERMISSION_FAILED);
297     EXPECT_EQ(ret, SUCCESS);
298 
299     IpcCommon::AddPermission(ACCESS_BIOMETRIC_PERMISSION);
300     testAuthTrustLevel = static_cast<AuthTrustLevel>(0);
301     ret = service->GetAvailableStatus(testApiVersion, testAuthType, testAuthTrustLevel,
302         funcResult);
303     EXPECT_EQ(funcResult, TRUST_LEVEL_NOT_SUPPORT);
304     EXPECT_EQ(ret, SUCCESS);
305     testAuthTrustLevel = ATL2;
306     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
307     testApiVersion = 2;
308     testAuthType = PIN;
309     ret = service->GetAvailableStatus(testApiVersion, testAuthType, testAuthTrustLevel,
310         funcResult);
311     EXPECT_EQ(funcResult, TYPE_NOT_SUPPORT);
312     EXPECT_EQ(ret, SUCCESS);
313     IpcCommon::DeleteAllPermission();
314 }
315 
316 HWTEST_F(UserAuthServiceTest, UserAuthServiceGetProperty001, TestSize.Level0)
317 {
318     UserAuthService service;
319     int32_t testUserId = 123;
320     AuthType testAuthType = PIN;
321     std::vector<uint32_t> testKeys = {Attributes::ATTR_PIN_SUB_TYPE, Attributes::ATTR_SIGNATURE};
322     sptr<MockGetExecutorPropertyCallback> testCallback(new (std::nothrow) MockGetExecutorPropertyCallback());
323     EXPECT_NE(testCallback, nullptr);
324     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
325     EXPECT_NE(mockHdi, nullptr);
326     EXPECT_CALL(*mockHdi, GetCredential(_, _, _)).Times(1);
327     EXPECT_CALL(*testCallback, OnGetExecutorPropertyResult(_, _)).Times(2);
328     sptr<IGetExecutorPropertyCallback> callbackInterface = testCallback;
329     int32_t ret = service.GetProperty(testUserId, testAuthType, testKeys, callbackInterface);
330     EXPECT_EQ(ret, SUCCESS);
331     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
332     ret = service.GetProperty(testUserId, testAuthType, testKeys, callbackInterface);
333     EXPECT_EQ(ret, SUCCESS);
334     IpcCommon::DeleteAllPermission();
335 }
336 
337 HWTEST_F(UserAuthServiceTest, UserAuthServiceGetProperty002, TestSize.Level0)
338 {
339     UserAuthService service;
340     int32_t testUserId = 123;
341     AuthType testAuthType = PIN;
342     std::vector<uint32_t> testKeys = {Attributes::ATTR_PIN_SUB_TYPE, Attributes::ATTR_SIGNATURE};
343     sptr<MockGetExecutorPropertyCallback> testCallback(nullptr);
344     sptr<IGetExecutorPropertyCallback> callbackInterface = testCallback;
345     int32_t ret = service.GetProperty(testUserId, testAuthType, testKeys, callbackInterface);
346     EXPECT_EQ(ret, INVALID_PARAMETERS);
347 
348     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
349     EXPECT_NE(mockHdi, nullptr);
350     EXPECT_CALL(*mockHdi, GetCredential(_, _, _)).Times(2);
351     ON_CALL(*mockHdi, GetCredential)
352         .WillByDefault(
__anona123a6f50502(int32_t userId, int32_t authType, std::vector<HdiCredentialInfo> &infos) 353             [](int32_t userId, int32_t authType, std::vector<HdiCredentialInfo> &infos) {
354                 HdiCredentialInfo tempInfo = {
355                     .credentialId = 1,
356                     .executorIndex = 2,
357                     .templateId = 3,
358                     .authType = static_cast<HdiAuthType>(1),
359                     .executorMatcher = 2,
360                     .executorSensorHint = 3,
361                 };
362                 infos.push_back(tempInfo);
363                 return HDF_SUCCESS;
364             }
365         );
366     auto resourceNode = MockResourceNode::CreateWithExecuteIndex(2);
367     EXPECT_NE(resourceNode, nullptr);
368     EXPECT_TRUE(ResourceNodePool::Instance().Insert(resourceNode));
369     MockResourceNode *node = static_cast<MockResourceNode *>(resourceNode.get());
370     EXPECT_CALL(*node, GetProperty(_, _))
371         .Times(0)
372         .WillOnce(Return(FAIL))
373         .WillOnce(Return(SUCCESS));
374     testCallback = sptr<MockGetExecutorPropertyCallback>(new (std::nothrow) MockGetExecutorPropertyCallback());
375     EXPECT_NE(testCallback, nullptr);
376     EXPECT_CALL(*testCallback, OnGetExecutorPropertyResult(_, _)).Times(2);
377     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
378     callbackInterface = testCallback;
379     ret = service.GetProperty(testUserId, testAuthType, testKeys, callbackInterface);
380     EXPECT_EQ(ret, SUCCESS);
381     ret = service.GetProperty(testUserId, testAuthType, testKeys, callbackInterface);
382     EXPECT_EQ(ret, SUCCESS);
383     EXPECT_TRUE(ResourceNodePool::Instance().Delete(2));
384     IpcCommon::DeleteAllPermission();
385 }
386 
387 HWTEST_F(UserAuthServiceTest, UserAuthServiceGetProperty003, TestSize.Level0)
388 {
389     UserAuthService service;
390     int32_t testUserId = 123;
391     AuthType testAuthType = PIN;
392     std::vector<uint32_t> testKeys = {Attributes::ATTR_REMAIN_TIMES, Attributes::ATTR_SIGNATURE};
393     sptr<MockGetExecutorPropertyCallback> testCallback(nullptr);
394     sptr<IGetExecutorPropertyCallback> callbackInterface = testCallback;
395     int32_t ret = service.GetProperty(testUserId, testAuthType, testKeys, callbackInterface);
396     EXPECT_EQ(ret, INVALID_PARAMETERS);
397 
398     testKeys = {Attributes::ATTR_FREEZING_TIME, Attributes::ATTR_SIGNATURE};
399     ret = service.GetProperty(testUserId, testAuthType, testKeys, callbackInterface);
400     EXPECT_EQ(ret, INVALID_PARAMETERS);
401 
402     testKeys = {Attributes::ATTR_NEXT_FAIL_LOCKOUT_DURATION, Attributes::ATTR_SIGNATURE};
403     service.GetProperty(testUserId, testAuthType, testKeys, callbackInterface);
404 
405     testKeys = {Attributes::ATTR_SIGNATURE};
406     service.GetProperty(testUserId, testAuthType, testKeys, callbackInterface);
407 
408     auto resourceNode = MockResourceNode::CreateWithExecuteIndex(2);
409     EXPECT_NE(resourceNode, nullptr);
410     EXPECT_TRUE(ResourceNodePool::Instance().Insert(resourceNode));
411     MockResourceNode *node = static_cast<MockResourceNode *>(resourceNode.get());
412     EXPECT_CALL(*node, GetProperty(_, _))
413         .Times(0)
414         .WillOnce(Return(FAIL))
415         .WillOnce(Return(SUCCESS));
416     testCallback = sptr<MockGetExecutorPropertyCallback>(new (std::nothrow) MockGetExecutorPropertyCallback());
417     EXPECT_NE(testCallback, nullptr);
418     EXPECT_CALL(*testCallback, OnGetExecutorPropertyResult(_, _)).Times(2);
419     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
420     callbackInterface = testCallback;
421     ret = service.GetProperty(testUserId, testAuthType, testKeys, callbackInterface);
422     EXPECT_EQ(ret, SUCCESS);
423     ret = service.GetProperty(testUserId, testAuthType, testKeys, callbackInterface);
424     EXPECT_EQ(ret, SUCCESS);
425     EXPECT_TRUE(ResourceNodePool::Instance().Delete(2));
426     IpcCommon::DeleteAllPermission();
427 }
428 
429 HWTEST_F(UserAuthServiceTest, UserAuthServiceGetProperty004, TestSize.Level0)
430 {
431     UserAuthService service;
432     int32_t testUserId = 123;
433     AuthType testAuthType = PIN;
434     std::vector<uint32_t> testKeys = {Attributes::ATTR_PIN_SUB_TYPE, Attributes::ATTR_SIGNATURE};
435     sptr<MockGetExecutorPropertyCallback> testCallback(nullptr);
436     sptr<IGetExecutorPropertyCallback> callbackInterface = testCallback;
437     int32_t ret = service.GetProperty(testUserId, testAuthType, testKeys, callbackInterface);
438     EXPECT_EQ(ret, INVALID_PARAMETERS);
439 
440     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
441     EXPECT_NE(mockHdi, nullptr);
442     EXPECT_CALL(*mockHdi, GetCredential(_, _, _)).Times(2);
443     ON_CALL(*mockHdi, GetCredential)
444         .WillByDefault(
__anona123a6f50602(int32_t userId, int32_t authType, std::vector<HdiCredentialInfo> &infos) 445             [](int32_t userId, int32_t authType, std::vector<HdiCredentialInfo> &infos) {
446                 HdiCredentialInfo tempInfo = {
447                     .credentialId = 1,
448                     .executorIndex = 2,
449                     .templateId = 3,
450                     .authType = static_cast<HdiAuthType>(1),
451                     .executorMatcher = 2,
452                     .executorSensorHint = 3,
453                 };
454                 infos.push_back(tempInfo);
455                 return HDF_SUCCESS;
456             }
457         );
458     auto resourceNode = MockResourceNode::CreateWithExecuteIndex(2, FACE, ALL_IN_ONE);
459     EXPECT_NE(resourceNode, nullptr);
460     EXPECT_TRUE(ResourceNodePool::Instance().Insert(resourceNode));
461     MockResourceNode *node = static_cast<MockResourceNode *>(resourceNode.get());
462     EXPECT_CALL(*node, GetProperty(_, _))
463         .Times(0)
464         .WillOnce(Return(FAIL))
465         .WillOnce(Return(SUCCESS));
466     testCallback = sptr<MockGetExecutorPropertyCallback>(new (std::nothrow) MockGetExecutorPropertyCallback());
467     EXPECT_NE(testCallback, nullptr);
468     EXPECT_CALL(*testCallback, OnGetExecutorPropertyResult(_, _)).Times(2);
469     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
470     callbackInterface = testCallback;
471     ret = service.GetProperty(testUserId, testAuthType, testKeys, callbackInterface);
472     EXPECT_EQ(ret, SUCCESS);
473     ret = service.GetProperty(testUserId, testAuthType, testKeys, callbackInterface);
474     EXPECT_EQ(ret, SUCCESS);
475     EXPECT_TRUE(ResourceNodePool::Instance().Delete(2));
476     IpcCommon::DeleteAllPermission();
477 }
478 
479 HWTEST_F(UserAuthServiceTest, UserAuthServiceGetProperty005, TestSize.Level0)
480 {
481     UserAuthService service;
482     int32_t testUserId = 123;
483     AuthType testAuthType = PIN;
484     std::vector<uint32_t> testKeys = {Attributes::ATTR_PIN_SUB_TYPE, Attributes::ATTR_SIGNATURE};
485     sptr<MockGetExecutorPropertyCallback> testCallback(nullptr);
486     sptr<IGetExecutorPropertyCallback> callbackInterface = testCallback;
487     int32_t ret = service.GetProperty(testUserId, testAuthType, testKeys, callbackInterface);
488     EXPECT_EQ(ret, INVALID_PARAMETERS);
489 
490     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
491     EXPECT_NE(mockHdi, nullptr);
492     EXPECT_CALL(*mockHdi, GetCredential(_, _, _)).Times(1);
493     ON_CALL(*mockHdi, GetCredential)
494         .WillByDefault(
__anona123a6f50702(int32_t userId, int32_t authType, std::vector<HdiCredentialInfo> &infos) 495             [](int32_t userId, int32_t authType, std::vector<HdiCredentialInfo> &infos) {
496                 HdiCredentialInfo tempInfo = {
497                     .credentialId = 1,
498                     .executorIndex = 2,
499                     .templateId = 3,
500                     .authType = static_cast<HdiAuthType>(1),
501                     .executorMatcher = 2,
502                     .executorSensorHint = 3,
503                 };
504                 infos.push_back(tempInfo);
505                 return HDF_SUCCESS;
506             }
507         );
508     auto resourceNode = MockResourceNode::CreateWithExecuteIndex(2, PIN, ALL_IN_ONE);
509     EXPECT_NE(resourceNode, nullptr);
510     EXPECT_TRUE(ResourceNodePool::Instance().Insert(resourceNode));
511     MockResourceNode *node = static_cast<MockResourceNode *>(resourceNode.get());
512     EXPECT_CALL(*node, GetProperty(_, _))
513         .Times(1)
514         .WillOnce(Return(FAIL))
515         .WillOnce(Return(SUCCESS));
516     testCallback = sptr<MockGetExecutorPropertyCallback>(new (std::nothrow) MockGetExecutorPropertyCallback());
517     EXPECT_NE(testCallback, nullptr);
518     EXPECT_CALL(*testCallback, OnGetExecutorPropertyResult(_, _)).Times(1);
519     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
520     callbackInterface = testCallback;
521     ret = service.GetProperty(testUserId, testAuthType, testKeys, callbackInterface);
522     EXPECT_EQ(ret, SUCCESS);
523     EXPECT_TRUE(ResourceNodePool::Instance().Delete(2));
524     IpcCommon::DeleteAllPermission();
525 }
526 
527 HWTEST_F(UserAuthServiceTest, UserAuthServiceGetProperty006, TestSize.Level0)
528 {
529     UserAuthService service;
530     int32_t testUserId = 123;
531     AuthType testAuthType = FACE;
532     std::vector<uint32_t> testKeys = {
533         Attributes::ATTR_PIN_SUB_TYPE,
534         Attributes::ATTR_SIGNATURE,
535         Attributes::ATTR_NEXT_FAIL_LOCKOUT_DURATION
536     };
537     sptr<MockGetExecutorPropertyCallback> testCallback(nullptr);
538     sptr<IGetExecutorPropertyCallback> callbackInterface = testCallback;
539     int32_t ret = service.GetProperty(testUserId, testAuthType, testKeys, callbackInterface);
540     EXPECT_EQ(ret, INVALID_PARAMETERS);
541     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
542     EXPECT_NE(mockHdi, nullptr);
543     EXPECT_CALL(*mockHdi, GetCredential(_, _, _)).Times(1);
544     ON_CALL(*mockHdi, GetCredential)
545         .WillByDefault(
__anona123a6f50802(int32_t userId, int32_t authType, std::vector<HdiCredentialInfo> &infos) 546             [](int32_t userId, int32_t authType, std::vector<HdiCredentialInfo> &infos) {
547                 HdiCredentialInfo tempInfo = {
548                     .credentialId = 1,
549                     .executorIndex = 2,
550                     .templateId = 3,
551                     .authType = static_cast<HdiAuthType>(2),
552                     .executorMatcher = 2,
553                     .executorSensorHint = 3,
554                 };
555                 infos.push_back(tempInfo);
556                 return HDF_SUCCESS;
557             }
558         );
559     auto resourceNode = MockResourceNode::CreateWithExecuteIndex(2, FACE, ALL_IN_ONE);
560     EXPECT_NE(resourceNode, nullptr);
561     EXPECT_TRUE(ResourceNodePool::Instance().Insert(resourceNode));
562     MockResourceNode *node = static_cast<MockResourceNode *>(resourceNode.get());
563     EXPECT_CALL(*node, GetProperty(_, _))
564         .Times(1)
565         .WillOnce(Return(FAIL))
566         .WillOnce(Return(SUCCESS));
567     testCallback = sptr<MockGetExecutorPropertyCallback>(new (std::nothrow) MockGetExecutorPropertyCallback());
568     EXPECT_NE(testCallback, nullptr);
569     EXPECT_CALL(*testCallback, OnGetExecutorPropertyResult(_, _)).Times(1);
570     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
571     callbackInterface = testCallback;
572     ret = service.GetProperty(testUserId, testAuthType, testKeys, callbackInterface);
573     EXPECT_EQ(ret, SUCCESS);
574     EXPECT_TRUE(ResourceNodePool::Instance().Delete(2));
575     IpcCommon::DeleteAllPermission();
576 }
577 
578 HWTEST_F(UserAuthServiceTest, UserAuthServiceSetProperty001, TestSize.Level0)
579 {
580     UserAuthService service;
581     int32_t testUserId = 124;
582     AuthType testAuthType = PIN;
583     Attributes testAttr;
584     sptr<MockSetExecutorPropertyCallback> testCallback(new (std::nothrow) MockSetExecutorPropertyCallback());
585     EXPECT_NE(testCallback, nullptr);
586     EXPECT_CALL(*testCallback, OnSetExecutorPropertyResult(_)).Times(2);
587     sptr<ISetExecutorPropertyCallback> callbackInterface = testCallback;
588     int32_t ret = service.SetProperty(testUserId, testAuthType, testAttr.Serialize(), callbackInterface);
589     EXPECT_EQ(ret, CHECK_PERMISSION_FAILED);
590     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
591     ret = service.SetProperty(testUserId, testAuthType, testAttr.Serialize(), callbackInterface);
592     EXPECT_EQ(ret, SUCCESS);
593     IpcCommon::DeleteAllPermission();
594 }
595 
596 HWTEST_F(UserAuthServiceTest, UserAuthServiceSetProperty002, TestSize.Level0)
597 {
598     UserAuthService service;
599     int32_t testUserId = 124;
600     AuthType testAuthType = PIN;
601     Attributes testAttr;
602     sptr<MockSetExecutorPropertyCallback> testCallback(nullptr);
603     sptr<ISetExecutorPropertyCallback> callbackInterface = testCallback;
604     int32_t ret = service.SetProperty(testUserId, testAuthType, testAttr.Serialize(), callbackInterface);
605     EXPECT_EQ(ret, INVALID_PARAMETERS);
606 
607     auto resourceNode = MockResourceNode::CreateWithExecuteIndex(2);
608     EXPECT_NE(resourceNode, nullptr);
609     EXPECT_TRUE(ResourceNodePool::Instance().Insert(resourceNode));
610     MockResourceNode *node = static_cast<MockResourceNode *>(resourceNode.get());
611     EXPECT_CALL(*node, SetProperty(_))
612         .Times(0)
613         .WillOnce(Return(FAIL))
614         .WillOnce(Return(SUCCESS));
615     testCallback = sptr<MockSetExecutorPropertyCallback>(new (std::nothrow) MockSetExecutorPropertyCallback());
616     EXPECT_NE(testCallback, nullptr);
617     EXPECT_CALL(*testCallback, OnSetExecutorPropertyResult(_)).Times(2);
618     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
619     callbackInterface = testCallback;
620     ret = service.SetProperty(testUserId, testAuthType, testAttr.Serialize(), callbackInterface);
621     EXPECT_EQ(ret, SUCCESS);
622     ret = service.SetProperty(testUserId, testAuthType, testAttr.Serialize(), callbackInterface);
623     EXPECT_EQ(ret, SUCCESS);
624     EXPECT_TRUE(ResourceNodePool::Instance().Delete(2));
625     IpcCommon::DeleteAllPermission();
626 }
627 
628 HWTEST_F(UserAuthServiceTest, UserAuthServiceAuth001, TestSize.Level0)
629 {
630     UserAuthService service;
631     int32_t testApiVersion = 9;
632     IpcAuthParamInner ipcAuthParamInner = {};
633     ipcAuthParamInner.challenge = {1, 2, 3, 4};
634     ipcAuthParamInner.authType = FACE;
635     ipcAuthParamInner.authTrustLevel = ATL2;
636     sptr<MockUserAuthCallback> testCallback(new (std::nothrow) MockUserAuthCallback());
637     EXPECT_NE(testCallback, nullptr);
638     EXPECT_CALL(*testCallback, OnResult(_, _))
639         .WillOnce(
__anona123a6f50902(int32_t result, const std::vector<uint8_t> &extraInfo) 640             [](int32_t result, const std::vector<uint8_t> &extraInfo) {
641                 EXPECT_EQ(result, HDF_FAILURE);
642                 return SUCCESS;
643             }
644         );
645 
646     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
647     EXPECT_NE(mockHdi, nullptr);
648     EXPECT_CALL(*mockHdi, BeginAuthentication(_, _, _)).WillOnce(Return(HDF_FAILURE));
649 
650     IpcCommon::AddPermission(ACCESS_BIOMETRIC_PERMISSION);
651     sptr<IIamCallback> callbackInterface = testCallback;
652     uint64_t contextId = 0;
653     int32_t ret = service.Auth(testApiVersion, ipcAuthParamInner, callbackInterface, contextId);
654     EXPECT_EQ(ret, GENERAL_ERROR);
655     EXPECT_EQ(contextId, 0);
656     IpcCommon::DeleteAllPermission();
657 }
658 
659 HWTEST_F(UserAuthServiceTest, UserAuthServiceAuth002, TestSize.Level0)
660 {
661     UserAuthService service;
662     int32_t testApiVersion = 9;
663     IpcAuthParamInner ipcAuthParamInner = {};
664     ipcAuthParamInner.challenge = {1, 2, 3, 4};
665     ipcAuthParamInner.authType = FACE;
666     ipcAuthParamInner.authTrustLevel = ATL2;
667     sptr<MockUserAuthCallback> testCallback(nullptr);
668     IpcCommon::AddPermission(ACCESS_BIOMETRIC_PERMISSION);
669     sptr<IIamCallback> callbackInterface = testCallback;
670     uint64_t contextId = 0;
671     int32_t ret = service.Auth(testApiVersion, ipcAuthParamInner, callbackInterface, contextId);
672     EXPECT_EQ(ret, GENERAL_ERROR);
673     EXPECT_EQ(contextId, 0);
674 
675     testCallback = sptr<MockUserAuthCallback>(new (std::nothrow) MockUserAuthCallback());
676     EXPECT_NE(testCallback, nullptr);
677     EXPECT_CALL(*testCallback, OnResult(_, _)).Times(2);
678     ipcAuthParamInner.authTrustLevel = static_cast<AuthTrustLevel>(90000);
679     callbackInterface = testCallback;
680     ret = service.Auth(testApiVersion, ipcAuthParamInner, callbackInterface, contextId);
681     EXPECT_EQ(ret, TRUST_LEVEL_NOT_SUPPORT);
682     EXPECT_EQ(contextId, 0);
683 
684     ipcAuthParamInner.authType = PIN;
685     ipcAuthParamInner.authTrustLevel = ATL1;
686     ret = service.Auth(testApiVersion, ipcAuthParamInner, callbackInterface, contextId);
687     EXPECT_EQ(ret, TYPE_NOT_SUPPORT);
688     EXPECT_EQ(contextId, 0);
689     IpcCommon::DeleteAllPermission();
690 }
691 
692 HWTEST_F(UserAuthServiceTest, UserAuthServiceAuth003, TestSize.Level0)
693 {
694     UserAuthService service;
695     int32_t testApiVersion = 9;
696     IpcAuthParamInner ipcAuthParamInner = {};
697     ipcAuthParamInner.challenge = {1, 2, 3, 4};
698     ipcAuthParamInner.authType = PIN;
699     ipcAuthParamInner.authTrustLevel = ATL2;
700     sptr<MockUserAuthCallback> testCallback(new (std::nothrow) MockUserAuthCallback());
701     EXPECT_NE(testCallback, nullptr);
702     EXPECT_CALL(*testCallback, OnResult(_, _)).Times(4);
703 
704     sptr<IIamCallback> callbackInterface = testCallback;
705     uint64_t contextId = 0;
706     int32_t ret = service.Auth(testApiVersion, ipcAuthParamInner, callbackInterface, contextId);
707     EXPECT_EQ(ret, CHECK_PERMISSION_FAILED);
708     EXPECT_EQ(contextId, 0);
709 
710     ipcAuthParamInner.authType = FACE;
711     ret = service.Auth(testApiVersion, ipcAuthParamInner, callbackInterface, contextId);
712     EXPECT_EQ(ret, CHECK_PERMISSION_FAILED);
713     EXPECT_EQ(contextId, 0);
714 
715     IpcCommon::AddPermission(ACCESS_BIOMETRIC_PERMISSION);
716     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
717     EXPECT_NE(mockHdi, nullptr);
718     EXPECT_CALL(*mockHdi, BeginAuthentication(_, _, _)).Times(2).WillRepeatedly(Return(NOT_ENROLLED));
719     ret = service.Auth(testApiVersion, ipcAuthParamInner, callbackInterface, contextId);
720     EXPECT_EQ(ret, GENERAL_ERROR);
721     EXPECT_EQ(contextId, 0);
722 
723     testApiVersion = 8;
724     ret = service.Auth(testApiVersion, ipcAuthParamInner, callbackInterface, contextId);
725     EXPECT_EQ(ret, GENERAL_ERROR);
726     EXPECT_EQ(contextId, 0);
727 
728     IpcCommon::DeleteAllPermission();
729 }
730 
MockForUserAuthHdi(std::shared_ptr<Context> & context,std::promise<void> & promise)731 static void MockForUserAuthHdi(std::shared_ptr<Context> &context, std::promise<void> &promise)
732 {
733     const uint32_t testScheduleId = 20;
734     const uint32_t testExecutorIndex = 60;
735     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
736     EXPECT_NE(mockHdi, nullptr);
737     EXPECT_CALL(*mockHdi, BeginAuthentication(_, _, _))
738         .WillRepeatedly([&context](uint64_t contextId, const HdiAuthParam &param,
739             std::vector<HdiScheduleInfo> &scheduleInfos) {
740             HdiScheduleInfo scheduleInfo = {};
741             scheduleInfo.authType = HdiAuthType::FACE;
742             scheduleInfo.scheduleId = testScheduleId;
743             scheduleInfo.executorIndexes.push_back(testExecutorIndex);
744             std::vector<uint8_t> executorMessages;
745             executorMessages.resize(1);
746             scheduleInfo.executorMessages.push_back(executorMessages);
747             scheduleInfos.push_back(scheduleInfo);
748             context = ContextPool::Instance().Select(contextId).lock();
749             return HDF_SUCCESS;
750         });
751 
752     EXPECT_CALL(*mockHdi, UpdateAuthenticationResult(_, _, _, _)).WillOnce(Return(HDF_SUCCESS));
753     EXPECT_CALL(*mockHdi, CancelAuthentication(_))
754         .WillOnce([&promise](uint64_t contextId) {
755             promise.set_value();
756             return HDF_SUCCESS;
757         });
758 }
759 
MockForAuthResourceNode(std::shared_ptr<MockResourceNode> & resourceNode)760 static void MockForAuthResourceNode(std::shared_ptr<MockResourceNode> &resourceNode)
761 {
762     const uint32_t testScheduleId = 20;
763     const uint32_t testExecutorIndex = 60;
764     EXPECT_CALL(*resourceNode, GetExecutorIndex()).WillRepeatedly(Return(testExecutorIndex));
765     EXPECT_CALL(*resourceNode, GetAuthType()).WillRepeatedly(Return(FACE));
766     EXPECT_CALL(*resourceNode, GetExecutorRole()).WillRepeatedly(Return(ALL_IN_ONE));
767     EXPECT_CALL(*resourceNode, GetExecutorMatcher()).WillRepeatedly(Return(0));
768     EXPECT_CALL(*resourceNode, GetExecutorPublicKey()).WillRepeatedly(Return(std::vector<uint8_t>()));
769     EXPECT_CALL(*resourceNode, BeginExecute(_, _, _))
770         .WillOnce([](uint64_t scheduleId, const std::vector<uint8_t> &publicKey, const Attributes &command) {
771             auto messenger = ExecutorMessengerService::GetInstance();
772             EXPECT_NE(messenger, nullptr);
773             auto finalResult = Common::MakeShared<Attributes>();
774             EXPECT_NE(finalResult, nullptr);
775             std::vector<uint8_t> scheduleResult = {1, 2, 3, 4};
776             EXPECT_TRUE(finalResult->SetUint8ArrayValue(Attributes::ATTR_RESULT, scheduleResult));
777             EXPECT_EQ(messenger->Finish(testScheduleId, SUCCESS, finalResult->Serialize()), SUCCESS);
778             return SUCCESS;
779         });
780 }
781 
782 HWTEST_F(UserAuthServiceTest, UserAuthServiceAuth004, TestSize.Level0)
783 {
784     UserAuthService service;
785     int32_t testApiVersion = 9;
786     IpcAuthParamInner ipcAuthParamInner = {};
787     ipcAuthParamInner.challenge = {1, 2, 3, 4};
788     ipcAuthParamInner.authType = FACE;
789     ipcAuthParamInner.authTrustLevel = ATL2;
790     std::shared_ptr<Context> context = nullptr;
791 
792     sptr<MockUserAuthCallback> testCallback(new (std::nothrow) MockUserAuthCallback());
793     EXPECT_NE(testCallback, nullptr);
794     EXPECT_CALL(*testCallback, OnResult(_, _))
795         .WillOnce(
__anona123a6f50d02(int32_t result, const std::vector<uint8_t> &extraInfo) 796             [&context](int32_t result, const std::vector<uint8_t>  &extraInfo) {
797                 EXPECT_EQ(result, SUCCESS);
798                 if (context != nullptr) {
799                     context->Stop();
800                 }
801                 return SUCCESS;
802             }
803         );
804 
805     IpcCommon::AddPermission(ACCESS_BIOMETRIC_PERMISSION);
806     std::promise<void> promise;
807     MockForUserAuthHdi(context, promise);
808 
809     auto resourceNode = Common::MakeShared<MockResourceNode>();
810     EXPECT_NE(resourceNode, nullptr);
811     MockForAuthResourceNode(resourceNode);
812 
813     EXPECT_TRUE(ResourceNodePool::Instance().Insert(resourceNode));
814 
815     sptr<IIamCallback> callbackInterface = testCallback;
816     uint64_t contextId = 0;
817     int32_t ret = service.Auth(testApiVersion, ipcAuthParamInner, callbackInterface, contextId);
818     EXPECT_EQ(ret, SUCCESS);
819     EXPECT_NE(contextId, 0);
820     promise.get_future().get();
821 
822     EXPECT_TRUE(ResourceNodePool::Instance().Delete(60));
823     IpcCommon::DeleteAllPermission();
824 }
825 
826 HWTEST_F(UserAuthServiceTest, UserAuthServiceAuthUser001, TestSize.Level0)
827 {
828     UserAuthService service;
829     IpcAuthParamInner authParam = {
830         .userId = 125,
831         .challenge = {1, 2, 3, 4},
832         .authType = FACE,
833         .authTrustLevel = ATL2,
834     };
835     IpcRemoteAuthParam remoteAuthParam = {
836         .isHasRemoteAuthParam = false,
837     };
838     sptr<MockUserAuthCallback> testCallback(new (std::nothrow) MockUserAuthCallback());
839     EXPECT_NE(testCallback, nullptr);
840     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
841     EXPECT_NE(mockHdi, nullptr);
842     EXPECT_CALL(*testCallback, OnResult(_, _)).Times(1);
843     EXPECT_CALL(*mockHdi, BeginAuthentication(_, _, _)).Times(1);
844     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
845     sptr<IIamCallback> callbackInterface = testCallback;
846     uint64_t contextId = 0;
847     int32_t ret = service.AuthUser(authParam, remoteAuthParam, callbackInterface, contextId);
848     EXPECT_EQ(ret, GENERAL_ERROR);
849     EXPECT_EQ(contextId, 0);
850     IpcCommon::DeleteAllPermission();
851 }
852 
853 HWTEST_F(UserAuthServiceTest, UserAuthServiceAuthUser002, TestSize.Level0)
854 {
855     UserAuthService service;
856     IpcAuthParamInner authParam = {
857         .userId = 125,
858         .challenge = {1, 2, 3, 4},
859         .authType = FACE,
860         .authTrustLevel = ATL2,
861     };
862     IpcRemoteAuthParam remoteAuthParam = {
863         .isHasRemoteAuthParam = false,
864     };
865     sptr<MockUserAuthCallback> testCallback(nullptr);
866     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
867     sptr<IIamCallback> callbackInterface = testCallback;
868     uint64_t contextId = 0;
869     int32_t ret = service.AuthUser(authParam, remoteAuthParam, callbackInterface, contextId);
870     EXPECT_EQ(ret, GENERAL_ERROR);
871     EXPECT_EQ(contextId, 0);
872 
873     testCallback = sptr<MockUserAuthCallback>(new (std::nothrow) MockUserAuthCallback());
874     EXPECT_NE(testCallback, nullptr);
875     EXPECT_CALL(*testCallback, OnResult(_, _)).Times(1);
876     authParam.authTrustLevel = static_cast<AuthTrustLevel>(90000);
877     callbackInterface = testCallback;
878     ret = service.AuthUser(authParam, remoteAuthParam, callbackInterface, contextId);
879     EXPECT_EQ(ret, CHECK_PERMISSION_FAILED);
880     EXPECT_EQ(contextId, 0);
881     IpcCommon::DeleteAllPermission();
882 }
883 
884 HWTEST_F(UserAuthServiceTest, UserAuthServiceAuthUser003, TestSize.Level0)
885 {
886     UserAuthService service;
887     IpcAuthParamInner authParam = {
888         .userId = 125,
889         .challenge = {1, 2, 3, 4},
890         .authType = FACE,
891         .authTrustLevel = ATL2,
892     };
893     IpcRemoteAuthParam remoteAuthParam = {
894         .isHasRemoteAuthParam = false,
895     };
896     std::shared_ptr<Context> context = nullptr;
897 
898     sptr<MockUserAuthCallback> testCallback(new (std::nothrow) MockUserAuthCallback());
899     EXPECT_NE(testCallback, nullptr);
900     EXPECT_CALL(*testCallback, OnResult(_, _))
901         .Times(2)
902         .WillOnce(
__anona123a6f50e02(int32_t result, const std::vector<uint8_t> &extraInfo) 903             [](int32_t result, const std::vector<uint8_t> &extraInfo) {
904                 EXPECT_EQ(result, CHECK_PERMISSION_FAILED);
905                 return SUCCESS;
906             }
907         )
908         .WillOnce(
__anona123a6f50f02(int32_t result, const std::vector<uint8_t> &extraInfo) 909             [&context](int32_t result, const std::vector<uint8_t> &extraInfo) {
910                 EXPECT_EQ(result, SUCCESS);
911                 if (context != nullptr) {
912                     context->Stop();
913                 }
914                 return SUCCESS;
915             }
916         );
917 
918     sptr<IIamCallback> callbackInterface = testCallback;
919     uint64_t contextId = 0;
920     int32_t ret = service.AuthUser(authParam, remoteAuthParam, callbackInterface, contextId);
921     EXPECT_EQ(ret, CHECK_PERMISSION_FAILED);
922     EXPECT_EQ(contextId, 0);
923 
924     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
925     std::promise<void> promise;
926     MockForUserAuthHdi(context, promise);
927 
928     auto resourceNode = Common::MakeShared<MockResourceNode>();
929     EXPECT_NE(resourceNode, nullptr);
930     MockForAuthResourceNode(resourceNode);
931 
932     EXPECT_TRUE(ResourceNodePool::Instance().Insert(resourceNode));
933 
934     ret = service.AuthUser(authParam, remoteAuthParam, callbackInterface, contextId);
935     EXPECT_EQ(ret, SUCCESS);
936     EXPECT_NE(contextId, 0);
937     promise.get_future().get();
938 
939     EXPECT_TRUE(ResourceNodePool::Instance().Delete(60));
940     IpcCommon::DeleteAllPermission();
941 }
942 
943 HWTEST_F(UserAuthServiceTest, UserAuthServiceAuthUser004, TestSize.Level0)
944 {
945     UserAuthService service;
946     IpcAuthParamInner authParam = {
947         .userId = 125,
948         .challenge = {1, 2, 3, 4},
949         .authType = FACE,
950         .authTrustLevel = ATL2,
951     };
952     IpcRemoteAuthParam remoteAuthParam = {
953         .isHasRemoteAuthParam = true,
954         .verifierNetworkId = "123",
955         .collectorNetworkId = "1233324321423412344134",
956     };
957     sptr<MockUserAuthCallback> testCallback(new (std::nothrow) MockUserAuthCallback());
958     EXPECT_NE(testCallback, nullptr);
959     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
960     EXPECT_NE(mockHdi, nullptr);
961     EXPECT_CALL(*testCallback, OnResult(_, _)).Times(1);
962     EXPECT_CALL(*mockHdi, BeginAuthentication(_, _, _)).Times(0);
963     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
964     sptr<IIamCallback> callbackInterface = testCallback;
965     uint64_t contextId = 0;
966     int32_t ret = service.AuthUser(authParam, remoteAuthParam, callbackInterface, contextId);
967     EXPECT_EQ(ret, GENERAL_ERROR);
968     EXPECT_EQ(contextId, 0);
969     IpcCommon::DeleteAllPermission();
970 }
971 
972 HWTEST_F(UserAuthServiceTest, UserAuthServiceAuthUser005, TestSize.Level0)
973 {
974     UserAuthService service;
975     IpcAuthParamInner authParam = {
976         .userId = 125,
977         .challenge = {1, 2, 3, 4},
978         .authType = PIN,
979         .authTrustLevel = ATL2,
980     };
981     IpcRemoteAuthParam remoteAuthParam = {
982         .isHasRemoteAuthParam = true,
983         .verifierNetworkId = "123",
984         .collectorNetworkId = "1233324321423412344134",
985     };
986     sptr<MockUserAuthCallback> testCallback(new (std::nothrow) MockUserAuthCallback());
987     EXPECT_NE(testCallback, nullptr);
988     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
989     EXPECT_NE(mockHdi, nullptr);
990     EXPECT_CALL(*testCallback, OnResult(_, _)).Times(1);
991     EXPECT_CALL(*mockHdi, BeginAuthentication(_, _, _)).Times(0);
992     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
993     sptr<IIamCallback> callbackInterface = testCallback;
994     uint64_t contextId = 0;
995     int32_t  ret = service.AuthUser(authParam, remoteAuthParam, callbackInterface, contextId);
996     EXPECT_EQ(ret, GENERAL_ERROR);
997     EXPECT_EQ(contextId, 0);
998     IpcCommon::DeleteAllPermission();
999 }
1000 
1001 HWTEST_F(UserAuthServiceTest, UserAuthServiceAuthUser006, TestSize.Level0)
1002 {
1003     UserAuthService service;
1004     IpcAuthParamInner authParam = {
1005         .userId = -1,
1006         .challenge = {1, 2, 3, 4},
1007         .authType = PIN,
1008         .authTrustLevel = ATL2,
1009     };
1010     IpcRemoteAuthParam remoteAuthParam = {
1011         .isHasRemoteAuthParam = true,
1012         .verifierNetworkId = "123",
1013         .collectorNetworkId = "1233324321423412344134",
1014     };
1015     sptr<MockUserAuthCallback> testCallback(new (std::nothrow) MockUserAuthCallback());
1016     EXPECT_NE(testCallback, nullptr);
1017     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
1018     EXPECT_NE(mockHdi, nullptr);
1019     EXPECT_CALL(*testCallback, OnResult(_, _)).Times(1);
1020     EXPECT_CALL(*mockHdi, BeginAuthentication(_, _, _)).Times(0);
1021     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
1022     sptr<IIamCallback> callbackInterface = testCallback;
1023     uint64_t contextId = 0;
1024     int32_t ret = service.AuthUser(authParam, remoteAuthParam, callbackInterface, contextId);
1025     EXPECT_EQ(ret, GENERAL_ERROR);
1026     EXPECT_EQ(contextId, 0);
1027     IpcCommon::DeleteAllPermission();
1028 }
1029 
1030 HWTEST_F(UserAuthServiceTest, UserAuthServiceAuthUser007, TestSize.Level0)
1031 {
1032     UserAuthService service;
1033     IpcAuthParamInner authParam = {
1034         .userId = -1,
1035         .challenge = {1, 2, 3, 4},
1036         .authType = PIN,
1037         .authTrustLevel = ATL2,
1038     };
1039     IpcRemoteAuthParam remoteAuthParam = {
1040         .isHasRemoteAuthParam = true,
1041         .verifierNetworkId = "123",
1042         .collectorNetworkId = "1233324321423412344134",
1043         .collectorTokenId = 123123,
1044     };
1045     sptr<MockUserAuthCallback> testCallback(new (std::nothrow) MockUserAuthCallback());
1046     EXPECT_NE(testCallback, nullptr);
1047     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
1048     EXPECT_NE(mockHdi, nullptr);
1049     EXPECT_CALL(*testCallback, OnResult(_, _)).Times(1);
1050     EXPECT_CALL(*mockHdi, BeginAuthentication(_, _, _)).Times(0);
1051     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
1052     sptr<IIamCallback> callbackInterface = testCallback;
1053     uint64_t contextId = 0;
1054     int32_t ret = service.AuthUser(authParam, remoteAuthParam, callbackInterface, contextId);
1055     EXPECT_EQ(ret, GENERAL_ERROR);
1056     EXPECT_EQ(contextId, 0);
1057     IpcCommon::DeleteAllPermission();
1058 }
1059 
1060 HWTEST_F(UserAuthServiceTest, UserAuthServiceInitRemoteAuthParam_001, TestSize.Level0)
1061 {
1062     UserAuthService service;
1063     IpcRemoteAuthParam ipcRemoteAuthParam = {
1064         .isHasVerifierNetworkId = true,
1065         .isHasRemoteAuthParam = true,
1066         .isHasCollectorNetworkId = true,
1067         .isHasCollectorTokenId = true,
1068         .verifierNetworkId = "123",
1069         .collectorNetworkId = "1233324321423412344134",
1070         .collectorTokenId = 123123,
1071     };
1072     std::optional<RemoteAuthParam> remoteAuthParam = {};
1073     RemoteAuthParam param = {};
1074     param.verifierNetworkId = "123";
1075     param.collectorNetworkId = "1233324321423412344134";
1076     remoteAuthParam = param;
1077     EXPECT_NO_THROW(service.InitRemoteAuthParam(ipcRemoteAuthParam, remoteAuthParam));
1078 }
1079 
1080 HWTEST_F(UserAuthServiceTest, UserAuthServiceProcessPinExpired_001, TestSize.Level0)
1081 {
1082     UserAuthService service;
1083     int ret = PIN_EXPIRED;
1084     AuthParamInner authParam = {
1085         .authTypes = {FACE, PIN, PRIVATE_PIN},
1086         .authTrustLevel = ATL2,
1087         .isUserIdSpecified = true,
1088     };
1089     std::vector<AuthType> validType = {FACE, PIN, PRIVATE_PIN};
1090     ContextFactory::AuthWidgetContextPara para = {
1091         .isPinExpired = true
1092     };
1093     EXPECT_NO_THROW(service.ProcessPinExpired(ret, authParam, validType, para));
1094 }
1095 
1096 HWTEST_F(UserAuthServiceTest, UserAuthServiceIdentify001, TestSize.Level0)
1097 {
1098     UserAuthService service;
1099     std::vector<uint8_t> testChallenge = {1, 2, 3, 4};
1100     AuthType testAuthType = FACE;
1101     sptr<MockUserAuthCallback> testCallback(new (std::nothrow) MockUserAuthCallback());
1102     EXPECT_NE(testCallback, nullptr);
1103     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
1104     EXPECT_NE(mockHdi, nullptr);
1105     EXPECT_CALL(*testCallback, OnResult(_, _)).Times(1);
1106     EXPECT_CALL(*mockHdi, BeginIdentification(_, _, _, _, _)).Times(1);
1107     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
1108     sptr<IIamCallback> callbackInterface = testCallback;
1109     uint64_t contextId = 0;
1110     int32_t ret = service.Identify(testChallenge, testAuthType, callbackInterface, contextId);
1111     EXPECT_EQ(ret, GENERAL_ERROR);
1112     EXPECT_EQ(contextId, 0);
1113     IpcCommon::DeleteAllPermission();
1114 }
1115 
1116 HWTEST_F(UserAuthServiceTest, UserAuthServiceIdentify002, TestSize.Level0)
1117 {
1118     UserAuthService service;
1119     std::vector<uint8_t> testChallenge = {1, 2, 3, 4};
1120     AuthType testAuthType = FACE;
1121     sptr<MockUserAuthCallback> testCallback(nullptr);
1122     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
1123     sptr<IIamCallback> callbackInterface = testCallback;
1124     uint64_t contextId = 0;
1125     int32_t ret = service.Identify(testChallenge, testAuthType, callbackInterface, contextId);
1126     EXPECT_EQ(ret, INVALID_PARAMETERS);
1127     EXPECT_EQ(contextId, 0);
1128 
1129     testCallback = sptr<MockUserAuthCallback>(new (std::nothrow) MockUserAuthCallback());
1130     EXPECT_NE(testCallback, nullptr);
1131     EXPECT_CALL(*testCallback, OnResult(_, _)).Times(1);
1132     testAuthType = PIN;
1133     callbackInterface = testCallback;
1134     ret =  service.Identify(testChallenge, testAuthType, callbackInterface, contextId);
1135     EXPECT_EQ(ret, GENERAL_ERROR);
1136     EXPECT_EQ(contextId, 0);
1137     IpcCommon::DeleteAllPermission();
1138 }
1139 
MockForIdentifyHdi(std::shared_ptr<Context> & context,std::promise<void> & promise)1140 static void MockForIdentifyHdi(std::shared_ptr<Context> &context, std::promise<void> &promise)
1141 {
1142     const uint32_t testExecutorIndex = 60;
1143     const uint32_t testscheduleId = 20;
1144     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
1145     EXPECT_NE(mockHdi, nullptr);
1146     EXPECT_CALL(*mockHdi, BeginIdentification(_, _, _, _, _))
1147         .WillRepeatedly([&context](uint64_t contextId, int32_t authType, const std::vector<uint8_t> &challenge,
1148             uint32_t executorId, HdiScheduleInfo &scheduleInfo) {
1149             scheduleInfo.authType = HdiAuthType::FACE;
1150             scheduleInfo.scheduleId = testscheduleId;
1151             scheduleInfo.executorIndexes.push_back(testExecutorIndex);
1152             std::vector<uint8_t> executorMessages;
1153             executorMessages.resize(1);
1154             scheduleInfo.executorMessages.push_back(executorMessages);
1155             context = ContextPool::Instance().Select(contextId).lock();
1156             return HDF_SUCCESS;
1157         });
1158 
1159     EXPECT_CALL(*mockHdi, UpdateIdentificationResult(_, _, _)).WillOnce(Return(HDF_SUCCESS));
1160     EXPECT_CALL(*mockHdi, CancelIdentification(_))
1161         .WillOnce([&promise](uint64_t contextId) {
1162             promise.set_value();
1163             return HDF_SUCCESS;
1164         });
1165 }
1166 
1167 HWTEST_F(UserAuthServiceTest, UserAuthServiceIdentify003, TestSize.Level0)
1168 {
1169     UserAuthService service;
1170     std::vector<uint8_t> testChallenge = {1, 2, 3, 4};
1171     AuthType testAuthType = FACE;
1172     std::shared_ptr<Context> context = nullptr;
1173 
1174     sptr<MockUserAuthCallback> testCallback(new (std::nothrow) MockUserAuthCallback());
1175     EXPECT_NE(testCallback, nullptr);
1176     EXPECT_CALL(*testCallback, OnResult(_, _))
1177         .Times(2)
1178         .WillOnce(
__anona123a6f51202(int32_t result, const std::vector<uint8_t> &extraInfo) 1179             [](int32_t result, const std::vector<uint8_t> &extraInfo) {
1180                 EXPECT_EQ(result, CHECK_PERMISSION_FAILED);
1181                 return SUCCESS;
1182             }
1183         )
1184         .WillOnce(
__anona123a6f51302(int32_t result, const std::vector<uint8_t> &extraInfo) 1185             [&context](int32_t result, const std::vector<uint8_t> &extraInfo) {
1186                 EXPECT_EQ(result, SUCCESS);
1187                 if (context != nullptr) {
1188                     context->Stop();
1189                 }
1190                 return SUCCESS;
1191             }
1192         );
1193 
1194     sptr<IIamCallback> callbackInterface = testCallback;
1195     uint64_t contextId = 0;
1196     int32_t ret = service.Identify(testChallenge, testAuthType, callbackInterface, contextId);
1197     EXPECT_EQ(ret, CHECK_PERMISSION_FAILED);
1198     EXPECT_EQ(contextId, 0);
1199 
1200     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
1201     std::promise<void> promise;
1202     MockForIdentifyHdi(context, promise);
1203 
1204     auto resourceNode = Common::MakeShared<MockResourceNode>();
1205     EXPECT_NE(resourceNode, nullptr);
1206     MockForAuthResourceNode(resourceNode);
1207 
1208     EXPECT_TRUE(ResourceNodePool::Instance().Insert(resourceNode));
1209 
1210     contextId = service.Identify(testChallenge, testAuthType, callbackInterface, contextId);
1211     EXPECT_EQ(ret, CHECK_PERMISSION_FAILED);
1212     promise.get_future().get();
1213 
1214     EXPECT_TRUE(ResourceNodePool::Instance().Delete(60));
1215     IpcCommon::DeleteAllPermission();
1216 }
1217 
1218 HWTEST_F(UserAuthServiceTest, UserAuthServiceCancelAuthOrIdentify_001, TestSize.Level0)
1219 {
1220     UserAuthService service;
1221     uint64_t testContextId = 12355236;
1222     int32_t cancelReason = 0;
1223     EXPECT_EQ(service.CancelAuthOrIdentify(testContextId, cancelReason), CHECK_PERMISSION_FAILED);
1224 
1225     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
1226     EXPECT_EQ(service.CancelAuthOrIdentify(testContextId, cancelReason), INVALID_CONTEXT_ID);
1227     IpcCommon::DeleteAllPermission();
1228 }
1229 
1230 HWTEST_F(UserAuthServiceTest, UserAuthServiceCancelAuthOrIdentify_002, TestSize.Level0)
1231 {
1232     UserAuthService service;
1233     uint64_t testContextId = 0x5678;
1234     uint32_t tokenId = 0x1234;
1235     int32_t cancelReason = 0;
1236 
1237     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
1238     IpcCommon::SetAccessTokenId(0, true);
1239     auto context = Common::MakeShared<MockContext>();
1240     EXPECT_NE(context, nullptr);
1241     EXPECT_CALL(*context, GetContextId()).WillRepeatedly(Return(testContextId));
1242     EXPECT_CALL(*context, GetLatestError()).WillRepeatedly(Return(GENERAL_ERROR));
1243     EXPECT_CALL(*context, GetTokenId()).WillRepeatedly(Return(tokenId));
1244     EXPECT_CALL(*context, Stop())
1245         .WillOnce(Return(false))
1246         .WillRepeatedly(Return(true));
1247 
1248     EXPECT_TRUE(ContextPool::Instance().Insert(context));
1249 
1250     EXPECT_EQ(service.CancelAuthOrIdentify(testContextId, cancelReason), CHECK_PERMISSION_FAILED);
1251     IpcCommon::SetAccessTokenId(tokenId, true);
1252 
1253     EXPECT_EQ(service.CancelAuthOrIdentify(testContextId, cancelReason), GENERAL_ERROR);
1254     EXPECT_EQ(service.CancelAuthOrIdentify(testContextId, cancelReason), SUCCESS);
1255     EXPECT_TRUE(ContextPool::Instance().Delete(testContextId));
1256     IpcCommon::DeleteAllPermission();
1257 }
1258 
1259 HWTEST_F(UserAuthServiceTest, UserAuthServiceGetVersion, TestSize.Level0)
1260 {
1261     UserAuthService service;
1262     int32_t version = -1;
1263     EXPECT_EQ(service.GetVersion(version), CHECK_PERMISSION_FAILED);
1264     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
1265     EXPECT_EQ(service.GetVersion(version), SUCCESS);
1266     EXPECT_EQ(version, 1);
1267     IpcCommon::DeleteAllPermission();
1268 }
1269 
1270 HWTEST_F(UserAuthServiceTest, UserAuthServiceStartRemoteAuthInvokerContext, TestSize.Level0)
1271 {
1272     UserAuthService service;
1273     AuthParamInner authParam = {
1274         .userId = 125,
1275         .challenge = {1, 2, 3, 4},
1276         .authType = FACE,
1277         .authTrustLevel = ATL2,
1278     };
1279     RemoteAuthInvokerContextParam remoteAuthInvokerContextParam;
1280     remoteAuthInvokerContextParam.connectionName = "";
1281     remoteAuthInvokerContextParam.verifierNetworkId = "123";
1282     remoteAuthInvokerContextParam.collectorNetworkId = "123123123";
1283     remoteAuthInvokerContextParam.tokenId = 123;
1284     remoteAuthInvokerContextParam.collectorTokenId = 123123;
1285     remoteAuthInvokerContextParam.callerName = "4123";
1286     remoteAuthInvokerContextParam.callerType = Security::AccessToken::TOKEN_HAP;
1287     std::shared_ptr<ContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
1288     ASSERT_NE(contextCallback, nullptr);
1289     EXPECT_EQ(service.StartRemoteAuthInvokerContext(authParam, remoteAuthInvokerContextParam, contextCallback),
1290     SUCCESS);
1291     IpcCommon::DeleteAllPermission();
1292 }
1293 
1294 HWTEST_F(UserAuthServiceTest, UserAuthServicePrepareRemoteAuth_001, TestSize.Level0)
1295 {
1296     UserAuthService service;
1297     const std::string networkId = "12312312313";
1298     sptr<MockUserAuthCallback> testCallback(new (std::nothrow) MockUserAuthCallback());
1299     EXPECT_NE(testCallback, nullptr);
1300     sptr<IIamCallback> callbackInterface = testCallback;
1301     EXPECT_EQ(service.PrepareRemoteAuth(networkId, callbackInterface), SUCCESS);
1302     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
1303     EXPECT_EQ(service.PrepareRemoteAuth(networkId, callbackInterface), SUCCESS);
1304     IpcCommon::DeleteAllPermission();
1305 }
1306 
1307 HWTEST_F(UserAuthServiceTest, UserAuthServicePrepareRemoteAuth_002, TestSize.Level0)
1308 {
1309     UserAuthService service;
1310     const std::string networkId = "";
1311     sptr<MockUserAuthCallback> testCallback(new (std::nothrow) MockUserAuthCallback());
1312     sptr<IIamCallback> callbackInterface = testCallback;
1313     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
1314     EXPECT_EQ(service.PrepareRemoteAuth(networkId, callbackInterface), SUCCESS);
1315     IpcCommon::DeleteAllPermission();
1316 }
1317 
1318 HWTEST_F(UserAuthServiceTest, UserAuthServiceCompleteRemoteAuthParam_001, TestSize.Level0)
1319 {
1320     UserAuthService service;
1321     const std::string localNetworkId = "1234567891123456789212345678931234567894123456789512345678961234";
1322     RemoteAuthParam remoteAuthParam = {};
1323     remoteAuthParam.verifierNetworkId = "1234567891123456789212345678931234567894123456789512345678961234";
1324     remoteAuthParam.collectorNetworkId = "1234567891123456789212345678931234567894123456789512345678961234";
1325     EXPECT_EQ(service.CompleteRemoteAuthParam(remoteAuthParam, localNetworkId), true);
1326 }
1327 
1328 HWTEST_F(UserAuthServiceTest, UserAuthServiceCompleteRemoteAuthParam_002, TestSize.Level0)
1329 {
1330     UserAuthService service;
1331     const std::string localNetworkId = "1234567891123456789212345678931234567894123456789512345678961234";
1332     RemoteAuthParam remoteAuthParam = {};
1333     EXPECT_EQ(service.CompleteRemoteAuthParam(remoteAuthParam, localNetworkId), false);
1334     remoteAuthParam.verifierNetworkId = "123";
1335     EXPECT_EQ(service.CompleteRemoteAuthParam(remoteAuthParam, localNetworkId), false);
1336     remoteAuthParam.verifierNetworkId = "1234567891123456789212345678931234567894123456789512345678961234";
1337     EXPECT_EQ(service.CompleteRemoteAuthParam(remoteAuthParam, localNetworkId), true);
1338     remoteAuthParam.collectorNetworkId = "123";
1339     EXPECT_EQ(service.CompleteRemoteAuthParam(remoteAuthParam, localNetworkId), false);
1340 }
1341 
1342 HWTEST_F(UserAuthServiceTest, UserAuthServiceCompleteRemoteAuthParam_003, TestSize.Level0)
1343 {
1344     UserAuthService service;
1345     const std::string localNetworkId = "1234567891123456789212345678931234567894123456789512345678961234";
1346     RemoteAuthParam remoteAuthParam = {};
1347     remoteAuthParam.collectorNetworkId = "1234567891123456789212345678931234567894123456789512345678961234";
1348     EXPECT_EQ(service.CompleteRemoteAuthParam(remoteAuthParam, localNetworkId), true);
1349     remoteAuthParam.verifierNetworkId = "1234567891123456789212345678931234567894123456789512345678961233";
1350     remoteAuthParam.collectorNetworkId = "1234567891123456789212345678931234567894123456789512345678961233";
1351     EXPECT_EQ(service.CompleteRemoteAuthParam(remoteAuthParam, localNetworkId), false);
1352     remoteAuthParam.verifierNetworkId = "1234567891123456789212345678931234567894123456789512345678961234";
1353     remoteAuthParam.collectorNetworkId = "1234567891123456789212345678931234567894123456789512345678961234";
1354     EXPECT_EQ(service.CompleteRemoteAuthParam(remoteAuthParam, localNetworkId), true);
1355     remoteAuthParam.verifierNetworkId = "1234567891123456789212345678931234567894123456789512345678961233";
1356     remoteAuthParam.collectorNetworkId = "1234567891123456789212345678931234567894123456789512345678961234";
1357     EXPECT_EQ(service.CompleteRemoteAuthParam(remoteAuthParam, localNetworkId), true);
1358     remoteAuthParam.verifierNetworkId = "1234567891123456789212345678931234567894123456789512345678961234";
1359     remoteAuthParam.collectorNetworkId = "1234567891123456789212345678931234567894123456789512345678961233";
1360     EXPECT_EQ(service.CompleteRemoteAuthParam(remoteAuthParam, localNetworkId), true);
1361 }
1362 
1363 HWTEST_F(UserAuthServiceTest, UserAuthServiceGetPropertyById001, TestSize.Level0)
1364 {
1365     UserAuthService service;
1366     uint64_t testCredentialId = 1;
1367     std::vector<uint32_t> testKeys = {Attributes::ATTR_PIN_SUB_TYPE, Attributes::ATTR_SIGNATURE};
1368     sptr<MockGetExecutorPropertyCallback> testCallback(new (std::nothrow) MockGetExecutorPropertyCallback());
1369     EXPECT_NE(testCallback, nullptr);
1370     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
1371     EXPECT_NE(mockHdi, nullptr);
1372     EXPECT_CALL(*mockHdi, GetCredentialById(_, _)).Times(1);
1373     EXPECT_CALL(*testCallback, OnGetExecutorPropertyResult(_, _)).Times(2);
1374     sptr<IGetExecutorPropertyCallback> callbackInterface = testCallback;
1375     int32_t ret = service.GetPropertyById(testCredentialId, testKeys, callbackInterface);
1376     EXPECT_EQ(ret, SUCCESS);
1377     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
1378     ret = service.GetPropertyById(testCredentialId, testKeys, callbackInterface);
1379     EXPECT_EQ(ret, SUCCESS);
1380     IpcCommon::DeleteAllPermission();
1381 }
1382 
1383 HWTEST_F(UserAuthServiceTest, UserAuthServiceGetPropertyById002, TestSize.Level0)
1384 {
1385     UserAuthService service;
1386     uint64_t testCredentialId = 1;
1387     std::vector<uint32_t> testKeys = {Attributes::ATTR_PIN_SUB_TYPE, Attributes::ATTR_SIGNATURE};
1388     sptr<MockGetExecutorPropertyCallback> testCallback(nullptr);
1389     sptr<IGetExecutorPropertyCallback> callbackInterface = testCallback;
1390     int32_t ret = service.GetPropertyById(testCredentialId, testKeys, callbackInterface);
1391     EXPECT_EQ(ret, INVALID_PARAMETERS);
1392 
1393     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
1394     EXPECT_NE(mockHdi, nullptr);
1395     EXPECT_CALL(*mockHdi, GetCredentialById(_, _)).Times(2);
1396     ON_CALL(*mockHdi, GetCredentialById)
1397         .WillByDefault(
__anona123a6f51402(uint64_t credentialId, HdiCredentialInfo &info) 1398             [](uint64_t credentialId, HdiCredentialInfo &info) {
1399                 HdiCredentialInfo tempInfo = {
1400                     .credentialId = 1,
1401                     .executorIndex = 2,
1402                     .templateId = 3,
1403                     .authType = static_cast<HdiAuthType>(1),
1404                     .executorMatcher = 2,
1405                     .executorSensorHint = 3,
1406                 };
1407                 info = tempInfo;
1408                 return HDF_SUCCESS;
1409             }
1410         );
1411     auto resourceNode = MockResourceNode::CreateWithExecuteIndex(2);
1412     EXPECT_NE(resourceNode, nullptr);
1413     EXPECT_TRUE(ResourceNodePool::Instance().Insert(resourceNode));
1414     MockResourceNode *node = static_cast<MockResourceNode *>(resourceNode.get());
1415     EXPECT_CALL(*node, GetProperty(_, _))
1416         .Times(0)
1417         .WillOnce(Return(FAIL))
1418         .WillOnce(Return(SUCCESS));
1419     testCallback = sptr<MockGetExecutorPropertyCallback>(new (std::nothrow) MockGetExecutorPropertyCallback());
1420     EXPECT_NE(testCallback, nullptr);
1421     EXPECT_CALL(*testCallback, OnGetExecutorPropertyResult(_, _)).Times(2);
1422     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
1423     callbackInterface = testCallback;
1424     ret = service.GetPropertyById(testCredentialId, testKeys, callbackInterface);
1425     EXPECT_EQ(ret, SUCCESS);
1426     ret = service.GetPropertyById(testCredentialId, testKeys, callbackInterface);
1427     EXPECT_EQ(ret, SUCCESS);
1428     EXPECT_TRUE(ResourceNodePool::Instance().Delete(2));
1429     IpcCommon::DeleteAllPermission();
1430 }
1431 
1432 HWTEST_F(UserAuthServiceTest, UserAuthServiceGetPropertyById003, TestSize.Level0)
1433 {
1434     UserAuthService service;
1435     uint64_t testCredentialId = 1;
1436     std::vector<uint32_t> testKeys = {Attributes::ATTR_REMAIN_TIMES, Attributes::ATTR_SIGNATURE};
1437     sptr<MockGetExecutorPropertyCallback> testCallback(nullptr);
1438     sptr<IGetExecutorPropertyCallback> callbackInterface = testCallback;
1439     int32_t ret = service.GetPropertyById(testCredentialId, testKeys, callbackInterface);
1440     EXPECT_EQ(ret, INVALID_PARAMETERS);
1441 
1442     testKeys = {Attributes::ATTR_FREEZING_TIME, Attributes::ATTR_SIGNATURE};
1443     ret = service.GetPropertyById(testCredentialId, testKeys, callbackInterface);
1444     EXPECT_EQ(ret, INVALID_PARAMETERS);
1445 
1446     testKeys = {Attributes::ATTR_NEXT_FAIL_LOCKOUT_DURATION, Attributes::ATTR_SIGNATURE};
1447     ret = service.GetPropertyById(testCredentialId, testKeys, callbackInterface);
1448     EXPECT_EQ(ret, INVALID_PARAMETERS);
1449 
1450     testKeys = {Attributes::ATTR_SIGNATURE};
1451     ret = service.GetPropertyById(testCredentialId, testKeys, callbackInterface);
1452     EXPECT_EQ(ret, INVALID_PARAMETERS);
1453 
1454     auto resourceNode = MockResourceNode::CreateWithExecuteIndex(2);
1455     EXPECT_NE(resourceNode, nullptr);
1456     EXPECT_TRUE(ResourceNodePool::Instance().Insert(resourceNode));
1457     MockResourceNode *node = static_cast<MockResourceNode *>(resourceNode.get());
1458     EXPECT_CALL(*node, GetProperty(_, _))
1459         .Times(0)
1460         .WillOnce(Return(FAIL))
1461         .WillOnce(Return(SUCCESS));
1462     testCallback = sptr<MockGetExecutorPropertyCallback>(new (std::nothrow) MockGetExecutorPropertyCallback());
1463     EXPECT_NE(testCallback, nullptr);
1464     EXPECT_CALL(*testCallback, OnGetExecutorPropertyResult(_, _)).Times(1);
1465     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
1466     callbackInterface = testCallback;
1467     ret = service.GetPropertyById(testCredentialId, testKeys, callbackInterface);
1468     EXPECT_EQ(ret, SUCCESS);
1469     EXPECT_TRUE(ResourceNodePool::Instance().Delete(2));
1470     IpcCommon::DeleteAllPermission();
1471 }
1472 
1473 HWTEST_F(UserAuthServiceTest, UserAuthServiceGetPropertyById004, TestSize.Level0)
1474 {
1475     UserAuthService service;
1476     uint64_t testCredentialId = 1;
1477     std::vector<uint32_t> testKeys = {Attributes::ATTR_PIN_SUB_TYPE, Attributes::ATTR_SIGNATURE};
1478     sptr<MockGetExecutorPropertyCallback> testCallback(nullptr);
1479     sptr<IGetExecutorPropertyCallback> callbackInterface = testCallback;
1480     int32_t ret = service.GetPropertyById(testCredentialId, testKeys, callbackInterface);
1481     EXPECT_EQ(ret, INVALID_PARAMETERS);
1482 
1483     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
1484     EXPECT_NE(mockHdi, nullptr);
1485     EXPECT_CALL(*mockHdi, GetCredentialById(_, _)).Times(1);
1486     ON_CALL(*mockHdi, GetCredentialById)
1487         .WillByDefault(
__anona123a6f51502(uint64_t credentialId, HdiCredentialInfo &info) 1488             [](uint64_t credentialId, HdiCredentialInfo &info) {
1489                 HdiCredentialInfo tempInfo = {
1490                     .credentialId = 1,
1491                     .executorIndex = 2,
1492                     .templateId = 3,
1493                     .authType = static_cast<HdiAuthType>(1),
1494                     .executorMatcher = 2,
1495                     .executorSensorHint = 3,
1496                 };
1497                 info = tempInfo;
1498                 return HDF_SUCCESS;
1499             }
1500         );
1501     auto resourceNode = MockResourceNode::CreateWithExecuteIndex(2, FACE, ALL_IN_ONE);
1502     EXPECT_NE(resourceNode, nullptr);
1503     EXPECT_TRUE(ResourceNodePool::Instance().Insert(resourceNode));
1504     MockResourceNode *node = static_cast<MockResourceNode *>(resourceNode.get());
1505     EXPECT_CALL(*node, GetProperty(_, _))
1506         .Times(0)
1507         .WillOnce(Return(FAIL))
1508         .WillOnce(Return(SUCCESS));
1509     testCallback = sptr<MockGetExecutorPropertyCallback>(new (std::nothrow) MockGetExecutorPropertyCallback());
1510     EXPECT_NE(testCallback, nullptr);
1511     EXPECT_CALL(*testCallback, OnGetExecutorPropertyResult(_, _)).Times(1);
1512     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
1513     callbackInterface = testCallback;
1514     ret = service.GetPropertyById(testCredentialId, testKeys, callbackInterface);
1515     EXPECT_EQ(ret, SUCCESS);
1516     EXPECT_TRUE(ResourceNodePool::Instance().Delete(2));
1517     IpcCommon::DeleteAllPermission();
1518 }
1519 
1520 HWTEST_F(UserAuthServiceTest, UserAuthServiceGetPropertyById005, TestSize.Level0)
1521 {
1522     UserAuthService service;
1523     uint64_t testCredentialId = 1;
1524     std::vector<uint32_t> testKeys = {Attributes::ATTR_PIN_SUB_TYPE, Attributes::ATTR_SIGNATURE};
1525     sptr<MockGetExecutorPropertyCallback> testCallback(nullptr);
1526     sptr<IGetExecutorPropertyCallback> callbackInterface = testCallback;
1527     int32_t ret = service.GetPropertyById(testCredentialId, testKeys, callbackInterface);
1528     EXPECT_EQ(ret, INVALID_PARAMETERS);
1529     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
1530     EXPECT_NE(mockHdi, nullptr);
1531     EXPECT_CALL(*mockHdi, GetCredentialById(_, _)).Times(1);
1532     ON_CALL(*mockHdi, GetCredentialById)
1533         .WillByDefault(
__anona123a6f51602(uint64_t credentialId, HdiCredentialInfo &info) 1534             [](uint64_t credentialId, HdiCredentialInfo &info) {
1535                 HdiCredentialInfo tempInfo = {
1536                     .credentialId = 1,
1537                     .executorIndex = 2,
1538                     .templateId = 3,
1539                     .authType = static_cast<HdiAuthType>(1),
1540                     .executorMatcher = 2,
1541                     .executorSensorHint = 3,
1542                 };
1543                 info = tempInfo;
1544                 return HDF_SUCCESS;
1545             }
1546         );
1547     auto resourceNode = MockResourceNode::CreateWithExecuteIndex(2, PIN, ALL_IN_ONE);
1548     EXPECT_NE(resourceNode, nullptr);
1549     EXPECT_TRUE(ResourceNodePool::Instance().Insert(resourceNode));
1550     MockResourceNode *node = static_cast<MockResourceNode *>(resourceNode.get());
1551     EXPECT_CALL(*node, GetProperty(_, _))
1552         .Times(1)
1553         .WillOnce(Return(FAIL))
1554         .WillOnce(Return(SUCCESS));
1555     testCallback = sptr<MockGetExecutorPropertyCallback>(new (std::nothrow) MockGetExecutorPropertyCallback());
1556     EXPECT_NE(testCallback, nullptr);
1557     EXPECT_CALL(*testCallback, OnGetExecutorPropertyResult(_, _)).Times(1);
1558     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
1559     callbackInterface = testCallback;
1560     ret = service.GetPropertyById(testCredentialId, testKeys, callbackInterface);
1561     EXPECT_EQ(ret, SUCCESS);
1562     EXPECT_TRUE(ResourceNodePool::Instance().Delete(2));
1563     IpcCommon::DeleteAllPermission();
1564 }
1565 
1566 HWTEST_F(UserAuthServiceTest, UserAuthServiceGetPropertyById006, TestSize.Level0)
1567 {
1568     UserAuthService service;
1569     uint64_t testCredentialId = 1;
1570     std::vector<uint32_t> testKeys = {
1571         Attributes::ATTR_PIN_SUB_TYPE,
1572         Attributes::ATTR_SIGNATURE,
1573         Attributes::ATTR_NEXT_FAIL_LOCKOUT_DURATION
1574     };
1575     sptr<MockGetExecutorPropertyCallback> testCallback(nullptr);
1576     sptr<IGetExecutorPropertyCallback> callbackInterface = testCallback;
1577     int32_t ret = service.GetPropertyById(testCredentialId, testKeys, callbackInterface);
1578     EXPECT_EQ(ret, INVALID_PARAMETERS);
1579     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
1580     EXPECT_NE(mockHdi, nullptr);
1581     EXPECT_CALL(*mockHdi, GetCredentialById(_, _)).Times(1);
1582     ON_CALL(*mockHdi, GetCredentialById)
1583         .WillByDefault(
__anona123a6f51702(uint64_t credentialId, HdiCredentialInfo &info) 1584             [](uint64_t credentialId, HdiCredentialInfo &info) {
1585                 HdiCredentialInfo tempInfo = {
1586                     .credentialId = 1,
1587                     .executorIndex = 2,
1588                     .templateId = 3,
1589                     .authType = static_cast<HdiAuthType>(2),
1590                     .executorMatcher = 2,
1591                     .executorSensorHint = 3,
1592                 };
1593                 info = tempInfo;
1594                 return HDF_SUCCESS;
1595             }
1596         );
1597     auto resourceNode = MockResourceNode::CreateWithExecuteIndex(2, FACE, ALL_IN_ONE);
1598     EXPECT_NE(resourceNode, nullptr);
1599     EXPECT_TRUE(ResourceNodePool::Instance().Insert(resourceNode));
1600     MockResourceNode *node = static_cast<MockResourceNode *>(resourceNode.get());
1601     EXPECT_CALL(*node, GetProperty(_, _))
1602         .Times(1)
1603         .WillOnce(Return(FAIL))
1604         .WillOnce(Return(SUCCESS));
1605     testCallback = sptr<MockGetExecutorPropertyCallback>(new (std::nothrow) MockGetExecutorPropertyCallback());
1606     EXPECT_NE(testCallback, nullptr);
1607     EXPECT_CALL(*testCallback, OnGetExecutorPropertyResult(_, _)).Times(1);
1608     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
1609     callbackInterface = testCallback;
1610     ret = service.GetPropertyById(testCredentialId, testKeys, callbackInterface);
1611     EXPECT_EQ(ret, SUCCESS);
1612     EXPECT_TRUE(ResourceNodePool::Instance().Delete(2));
1613     IpcCommon::DeleteAllPermission();
1614 }
1615 
1616 HWTEST_F(UserAuthServiceTest, UserAuthServiceProcessAuthParamForRemoteAuth_InvalidAuthType, TestSize.Level0)
1617 {
1618     UserAuthService service;
1619     AuthParamInner authParam = {};
1620     Authentication::AuthenticationPara para = {};
1621     RemoteAuthParam remoteAuthParam = {};
1622     std::string localNetworkId = "local123";
1623     para.authType = AuthType::FACE;
1624     EXPECT_FALSE(service.ProcessAuthParamForRemoteAuth(authParam, para, remoteAuthParam, localNetworkId));
1625     para.authType = AuthType::PIN;
1626     authParam.userId = -1;
1627     EXPECT_FALSE(service.ProcessAuthParamForRemoteAuth(authParam, para, remoteAuthParam, localNetworkId));
1628     authParam.userId = 100;
1629     EXPECT_FALSE(service.ProcessAuthParamForRemoteAuth(authParam, para, remoteAuthParam, localNetworkId));
1630     localNetworkId = "1234567891123456789212345678931234567894123456789512345678961234";
1631     remoteAuthParam.verifierNetworkId = "1234567891123456789212345678931234567894123456789512345678961234";
1632     remoteAuthParam.collectorNetworkId = "1234567891123456789212345678931234567894123456789512345678961234";
1633     para.tokenId = 789;
1634     EXPECT_TRUE(service.ProcessAuthParamForRemoteAuth(authParam, para, remoteAuthParam, localNetworkId));
1635 }
1636 } // namespace UserAuth
1637 } // namespace UserIam
1638 } // namespace OHOS