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