• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "user_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 "mock_context.h"
25 #include "mock_iuser_auth_interface.h"
26 #include "mock_ipc_common.h"
27 #include "mock_user_auth_callback.h"
28 #include "mock_resource_node.h"
29 #include "resource_node_pool.h"
30 #include "user_auth_service.h"
31 
32 namespace OHOS {
33 namespace UserIam {
34 namespace UserAuth {
35 using namespace testing;
36 using namespace testing::ext;
37 
38 using HdiCredentialInfo = OHOS::HDI::UserAuth::V1_0::CredentialInfo;
39 using HdiEnrolledInfo = OHOS::HDI::UserAuth::V1_0::EnrolledInfo;
40 using HdiAuthType = OHOS::HDI::UserAuth::V1_0::AuthType;
41 using HdiScheduleInfo = OHOS::HDI::UserAuth::V1_0::ScheduleInfo;
42 using HdiAuthSolution = OHOS::HDI::UserAuth::V1_0::AuthSolution;
43 using HdiExecutorInfo = OHOS::HDI::UserAuth::V1_0::ExecutorInfo;
44 
SetUpTestCase()45 void UserAuthServiceTest::SetUpTestCase()
46 {
47 }
48 
TearDownTestCase()49 void UserAuthServiceTest::TearDownTestCase()
50 {
51 }
52 
SetUp()53 void UserAuthServiceTest::SetUp()
54 {
55     MockIUserAuthInterface::Holder::GetInstance().Reset();
56 }
57 
TearDown()58 void UserAuthServiceTest::TearDown()
59 {
60     MockIUserAuthInterface::Holder::GetInstance().Reset();
61 }
62 
63 HWTEST_F(UserAuthServiceTest, UserAuthServiceGetAvailableStatus001, TestSize.Level0)
64 {
65     UserAuthService service(100, true);
66     int32_t testApiVersion = 8;
67     AuthType testAuthType = FACE;
68     AuthTrustLevel testAuthTrustLevel = ATL3;
69     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
70     EXPECT_NE(mockHdi, nullptr);
71     EXPECT_CALL(*mockHdi, GetAuthTrustLevel(_, _, _))
72         .Times(1)
73         .WillOnce(
__anon64bbd17d0102(int32_t userId, HdiAuthType authType, uint32_t &authTrustLevel) 74             [](int32_t userId, HdiAuthType authType, uint32_t &authTrustLevel) {
75                 authTrustLevel = ATL1;
76                 return HDF_SUCCESS;
77             }
78         );
79     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
80     EXPECT_NE(SUCCESS, service.GetAvailableStatus(testApiVersion, testAuthType, testAuthTrustLevel));
81     IpcCommon::DeleteAllPermission();
82 }
83 
84 HWTEST_F(UserAuthServiceTest, UserAuthServiceGetAvailableStatus002, TestSize.Level0)
85 {
86     UserAuthService service(100, true);
87     int32_t testApiVersion = 8;
88     AuthType testAuthType = FACE;
89     AuthTrustLevel testAuthTrustLevel = static_cast<AuthTrustLevel>(90000);
90 
91     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
92     EXPECT_EQ(TRUST_LEVEL_NOT_SUPPORT, service.GetAvailableStatus(testApiVersion, testAuthType, testAuthTrustLevel));
93 
94     testAuthTrustLevel = ATL2;
95     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
96     EXPECT_NE(mockHdi, nullptr);
97     EXPECT_CALL(*mockHdi, GetAuthTrustLevel(_, _, _)).Times(1);
98     ON_CALL(*mockHdi, GetAuthTrustLevel)
99         .WillByDefault(
__anon64bbd17d0202(int32_t userId, HdiAuthType authType, uint32_t &authTrustLevel) 100             [](int32_t userId, HdiAuthType authType, uint32_t &authTrustLevel) {
101                 authTrustLevel = static_cast<AuthTrustLevel>(0);
102                 return SUCCESS;
103             }
104         );
105     EXPECT_EQ(TRUST_LEVEL_NOT_SUPPORT, service.GetAvailableStatus(testApiVersion, testAuthType, testAuthTrustLevel));
106     IpcCommon::DeleteAllPermission();
107 }
108 
109 HWTEST_F(UserAuthServiceTest, UserAuthServiceGetAvailableStatus003, TestSize.Level0)
110 {
111     UserAuthService service(100, true);
112     int32_t testApiVersion = 8;
113     AuthType testAuthType = FACE;
114     AuthTrustLevel testAuthTrustLevel = ATL2;
115 
116     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
117     EXPECT_NE(mockHdi, nullptr);
__anon64bbd17d0302() 118     EXPECT_CALL(*mockHdi, GetAuthTrustLevel(_, _, _)).WillRepeatedly([]() {
119         return NOT_ENROLLED;
120     });
121     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
122     EXPECT_EQ(NOT_ENROLLED, service.GetAvailableStatus(testApiVersion, testAuthType, testAuthTrustLevel));
123 
124     testApiVersion = 9;
125     EXPECT_EQ(NOT_ENROLLED, service.GetAvailableStatus(testApiVersion, testAuthType, testAuthTrustLevel));
126     IpcCommon::DeleteAllPermission();
127 }
128 
129 HWTEST_F(UserAuthServiceTest, UserAuthServiceGetAvailableStatus004, TestSize.Level0)
130 {
131     int32_t testApiVersion = 8;
132     AuthType testAuthType = PIN;
133     AuthTrustLevel testAuthTrustLevel = ATL2;
134 
135     auto service = Common::MakeShared<UserAuthService>(100, true);
136     EXPECT_NE(service, nullptr);
137     int32_t ret = service->GetAvailableStatus(testApiVersion, testAuthType, testAuthTrustLevel);
138     EXPECT_EQ(ret, CHECK_PERMISSION_FAILED);
139 
140     testAuthType = FACE;
141     ret = service->GetAvailableStatus(testApiVersion, testAuthType, testAuthTrustLevel);
142     EXPECT_EQ(ret, CHECK_PERMISSION_FAILED);
143 
144     IpcCommon::AddPermission(ACCESS_BIOMETRIC_PERMISSION);
145     testAuthTrustLevel = static_cast<AuthTrustLevel>(0);
146     ret = service->GetAvailableStatus(testApiVersion, testAuthType, testAuthTrustLevel);
147     EXPECT_EQ(ret, TRUST_LEVEL_NOT_SUPPORT);
148     IpcCommon::DeleteAllPermission();
149 }
150 
151 HWTEST_F(UserAuthServiceTest, UserAuthServiceGetProperty001, TestSize.Level0)
152 {
153     UserAuthService service(100, true);
154     int32_t testUserId = 123;
155     AuthType testAuthType = FACE;
156     std::vector<Attributes::AttributeKey> testKeys = {Attributes::ATTR_RESULT_CODE, Attributes::ATTR_SIGNATURE};
157     sptr<GetExecutorPropertyCallbackInterface> testCallback = new MockGetExecutorPropertyCallback();
158     EXPECT_NE(testCallback, nullptr);
159     auto *tempCallback = static_cast<MockGetExecutorPropertyCallback *>(testCallback.GetRefPtr());
160     EXPECT_NE(tempCallback, nullptr);
161     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
162     EXPECT_NE(mockHdi, nullptr);
163     EXPECT_CALL(*tempCallback, OnGetExecutorPropertyResult(_, _)).Times(2);
164     EXPECT_CALL(*mockHdi, GetCredential(_, _, _)).Times(1);
165     service.GetProperty(testUserId, testAuthType, testKeys, testCallback);
166     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
167     service.GetProperty(testUserId, testAuthType, testKeys, testCallback);
168     IpcCommon::DeleteAllPermission();
169 }
170 
171 HWTEST_F(UserAuthServiceTest, UserAuthServiceGetProperty002, TestSize.Level0)
172 {
173     UserAuthService service(100, true);
174     int32_t testUserId = 123;
175     AuthType testAuthType = FACE;
176     std::vector<Attributes::AttributeKey> testKeys = {Attributes::ATTR_RESULT_CODE, Attributes::ATTR_SIGNATURE};
177     sptr<GetExecutorPropertyCallbackInterface> testCallback = nullptr;
178     service.GetProperty(testUserId, testAuthType, testKeys, testCallback);
179 
180     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
181     EXPECT_NE(mockHdi, nullptr);
182     EXPECT_CALL(*mockHdi, GetCredential(_, _, _)).Times(2);
183     ON_CALL(*mockHdi, GetCredential)
184         .WillByDefault(
__anon64bbd17d0402(int32_t userId, HdiAuthType authType, std::vector<HdiCredentialInfo> &infos) 185             [](int32_t userId, HdiAuthType authType, std::vector<HdiCredentialInfo> &infos) {
186                 HdiCredentialInfo tempInfo = {
187                     .credentialId = 1,
188                     .executorIndex = 2,
189                     .templateId = 3,
190                     .authType = static_cast<HdiAuthType>(1),
191                     .executorMatcher = 2,
192                     .executorSensorHint = 3,
193                 };
194                 infos.push_back(tempInfo);
195                 return HDF_SUCCESS;
196             }
197         );
198     auto resourceNode = MockResourceNode::CreateWithExecuteIndex(2);
199     EXPECT_NE(resourceNode, nullptr);
200     EXPECT_TRUE(ResourceNodePool::Instance().Insert(resourceNode));
201     MockResourceNode *node = static_cast<MockResourceNode *>(resourceNode.get());
202     EXPECT_CALL(*node, GetProperty(_, _))
203         .Times(2)
204         .WillOnce(Return(FAIL))
205         .WillOnce(Return(SUCCESS));
206     testCallback = new MockGetExecutorPropertyCallback();
207     EXPECT_NE(testCallback, nullptr);
208     auto *tempCallback = static_cast<MockGetExecutorPropertyCallback *>(testCallback.GetRefPtr());
209     EXPECT_NE(tempCallback, nullptr);
210     EXPECT_CALL(*tempCallback, OnGetExecutorPropertyResult(_, _)).Times(2);
211     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
212     service.GetProperty(testUserId, testAuthType, testKeys, testCallback);
213     service.GetProperty(testUserId, testAuthType, testKeys, testCallback);
214     EXPECT_TRUE(ResourceNodePool::Instance().Delete(2));
215     IpcCommon::DeleteAllPermission();
216 }
217 
218 HWTEST_F(UserAuthServiceTest, UserAuthServiceSetProperty001, TestSize.Level0)
219 {
220     UserAuthService service(100, true);
221     int32_t testUserId = 124;
222     AuthType testAuthType = FACE;
223     Attributes testAttr;
224     sptr<SetExecutorPropertyCallbackInterface> testCallback = new MockSetExecutorPropertyCallback();
225     EXPECT_NE(testCallback, nullptr);
226     auto *tempCallback = static_cast<MockSetExecutorPropertyCallback *>(testCallback.GetRefPtr());
227     EXPECT_NE(tempCallback, nullptr);
228     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
229     EXPECT_NE(mockHdi, nullptr);
230     EXPECT_CALL(*tempCallback, OnSetExecutorPropertyResult(_)).Times(2);
231     EXPECT_CALL(*mockHdi, GetCredential(_, _, _)).Times(1);
232     service.SetProperty(testUserId, testAuthType, testAttr, testCallback);
233     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
234     service.SetProperty(testUserId, testAuthType, testAttr, testCallback);
235     IpcCommon::DeleteAllPermission();
236 }
237 
238 HWTEST_F(UserAuthServiceTest, UserAuthServiceSetProperty002, TestSize.Level0)
239 {
240     UserAuthService service(100, true);
241     int32_t testUserId = 124;
242     AuthType testAuthType = FACE;
243     Attributes testAttr;
244     sptr<SetExecutorPropertyCallbackInterface> testCallback = nullptr;
245     service.SetProperty(testUserId, testAuthType, testAttr, testCallback);
246 
247     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
248     EXPECT_NE(mockHdi, nullptr);
249     EXPECT_CALL(*mockHdi, GetCredential(_, _, _)).Times(2);
250     ON_CALL(*mockHdi, GetCredential)
251         .WillByDefault(
__anon64bbd17d0502(int32_t userId, HdiAuthType authType, std::vector<HdiCredentialInfo> &infos) 252             [](int32_t userId, HdiAuthType authType, std::vector<HdiCredentialInfo> &infos) {
253                 HdiCredentialInfo tempInfo = {
254                     .credentialId = 1,
255                     .executorIndex = 2,
256                     .templateId = 3,
257                     .authType = static_cast<HdiAuthType>(1),
258                     .executorMatcher = 2,
259                     .executorSensorHint = 3,
260                 };
261                 infos.push_back(tempInfo);
262                 return HDF_SUCCESS;
263             }
264         );
265     auto resourceNode = MockResourceNode::CreateWithExecuteIndex(2);
266     EXPECT_NE(resourceNode, nullptr);
267     EXPECT_TRUE(ResourceNodePool::Instance().Insert(resourceNode));
268     MockResourceNode *node = static_cast<MockResourceNode *>(resourceNode.get());
269     EXPECT_CALL(*node, SetProperty(_))
270         .Times(2)
271         .WillOnce(Return(FAIL))
272         .WillOnce(Return(SUCCESS));
273     testCallback = new MockSetExecutorPropertyCallback();
274     EXPECT_NE(testCallback, nullptr);
275     auto *tempCallback = static_cast<MockSetExecutorPropertyCallback *>(testCallback.GetRefPtr());
276     EXPECT_NE(tempCallback, nullptr);
277     EXPECT_CALL(*tempCallback, OnSetExecutorPropertyResult(_)).Times(2);
278     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
279     service.SetProperty(testUserId, testAuthType, testAttr, testCallback);
280     service.SetProperty(testUserId, testAuthType, testAttr, testCallback);
281     EXPECT_TRUE(ResourceNodePool::Instance().Delete(2));
282     IpcCommon::DeleteAllPermission();
283 }
284 
285 HWTEST_F(UserAuthServiceTest, UserAuthServiceAuth001, TestSize.Level0)
286 {
287     UserAuthService service(100, true);
288     int32_t testApiVersion = 9;
289     std::vector<uint8_t> testChallenge = {1, 2, 3, 4};
290     AuthType testAuthType = FACE;
291     AuthTrustLevel testAuthTrustLevel = ATL2;
292     sptr<UserAuthCallbackInterface> testCallback = new MockUserAuthCallback();
293     EXPECT_NE(testCallback, nullptr);
294     auto *tempCallback = static_cast<MockUserAuthCallback *>(testCallback.GetRefPtr());
295     EXPECT_NE(tempCallback, nullptr);
296     EXPECT_CALL(*tempCallback, OnResult(_, _))
297         .WillOnce(
__anon64bbd17d0602(int32_t result, const Attributes &extraInfo) 298             [](int32_t result, const Attributes &extraInfo) {
299                 EXPECT_EQ(result, HDF_FAILURE);
300             }
301         );
302 
303     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
304     EXPECT_NE(mockHdi, nullptr);
305     EXPECT_CALL(*mockHdi, BeginAuthentication(_, _, _)).WillOnce(Return(HDF_FAILURE));
306 
307     IpcCommon::AddPermission(ACCESS_BIOMETRIC_PERMISSION);
308     uint64_t contextId = service.Auth(testApiVersion, testChallenge, testAuthType, testAuthTrustLevel, testCallback);
309     EXPECT_EQ(contextId, 0);
310     IpcCommon::DeleteAllPermission();
311 }
312 
313 HWTEST_F(UserAuthServiceTest, UserAuthServiceAuth002, TestSize.Level0)
314 {
315     UserAuthService service(100, true);
316     int32_t testApiVersion = 9;
317     std::vector<uint8_t> testChallenge = {1, 2, 3, 4};
318     AuthType testAuthType = FACE;
319     AuthTrustLevel testAuthTrustLevel = ATL2;
320     sptr<UserAuthCallbackInterface> testCallback = nullptr;
321     IpcCommon::AddPermission(ACCESS_BIOMETRIC_PERMISSION);
322     uint64_t contextId = service.Auth(testApiVersion, testChallenge, testAuthType, testAuthTrustLevel, testCallback);
323     EXPECT_EQ(contextId, 0);
324 
325     testCallback = new MockUserAuthCallback();
326     EXPECT_NE(testCallback, nullptr);
327     auto *tempCallback = static_cast<MockUserAuthCallback *>(testCallback.GetRefPtr());
328     EXPECT_NE(tempCallback, nullptr);
329     EXPECT_CALL(*tempCallback, OnResult(_, _)).Times(2);
330     testAuthTrustLevel = static_cast<AuthTrustLevel>(90000);
331     contextId = service.Auth(testApiVersion, testChallenge, testAuthType, testAuthTrustLevel, testCallback);
332     EXPECT_EQ(contextId, 0);
333 
334     testAuthType = PIN;
335     testAuthTrustLevel = ATL1;
336     contextId = service.Auth(testApiVersion, testChallenge, testAuthType, testAuthTrustLevel, testCallback);
337     EXPECT_EQ(contextId, 0);
338     IpcCommon::DeleteAllPermission();
339 }
340 
341 HWTEST_F(UserAuthServiceTest, UserAuthServiceAuth003, TestSize.Level0)
342 {
343     UserAuthService service(100, true);
344     int32_t testApiVersion = 9;
345     std::vector<uint8_t> testChallenge = {1, 2, 3, 4};
346     AuthType testAuthType = PIN;
347     AuthTrustLevel testAuthTrustLevel = ATL2;
348     sptr<UserAuthCallbackInterface> testCallback = new MockUserAuthCallback();
349     EXPECT_NE(testCallback, nullptr);
350     auto *tempCallback = static_cast<MockUserAuthCallback *>(testCallback.GetRefPtr());
351     EXPECT_NE(tempCallback, nullptr);
352     EXPECT_CALL(*tempCallback, OnResult(_, _)).Times(4);
353 
354     uint64_t contextId = service.Auth(testApiVersion, testChallenge, testAuthType, testAuthTrustLevel, testCallback);
355     EXPECT_EQ(contextId, 0);
356 
357     testAuthType = FACE;
358     contextId = service.Auth(testApiVersion, testChallenge, testAuthType, testAuthTrustLevel, testCallback);
359     EXPECT_EQ(contextId, 0);
360 
361     IpcCommon::AddPermission(ACCESS_BIOMETRIC_PERMISSION);
362     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
363     EXPECT_NE(mockHdi, nullptr);
364     EXPECT_CALL(*mockHdi, BeginAuthentication(_, _, _)).Times(2).WillRepeatedly(Return(NOT_ENROLLED));
365     contextId = service.Auth(testApiVersion, testChallenge, testAuthType, testAuthTrustLevel, testCallback);
366     EXPECT_EQ(contextId, 0);
367 
368     testApiVersion = 8;
369     contextId = service.Auth(testApiVersion, testChallenge, testAuthType, testAuthTrustLevel, testCallback);
370     EXPECT_EQ(contextId, 0);
371 
372     IpcCommon::DeleteAllPermission();
373 }
374 
375 HWTEST_F(UserAuthServiceTest, UserAuthServiceAuth004, TestSize.Level0)
376 {
377     UserAuthService service(100, true);
378     int32_t testApiVersion = 9;
379     std::vector<uint8_t> testChallenge = {1, 2, 3, 4};
380     AuthType testAuthType = FACE;
381     AuthTrustLevel testAuthTrustLevel = ATL2;
382     std::shared_ptr<Context> context = nullptr;
383 
384     sptr<UserAuthCallbackInterface> testCallback = new MockUserAuthCallback();
385     EXPECT_NE(testCallback, nullptr);
386     auto *tempCallback = static_cast<MockUserAuthCallback *>(testCallback.GetRefPtr());
387     EXPECT_NE(tempCallback, nullptr);
388     EXPECT_CALL(*tempCallback, OnResult(_, _))
389         .WillOnce(
__anon64bbd17d0702(int32_t result, const Attributes &extraInfo) 390             [&context](int32_t result, const Attributes &extraInfo) {
391                 EXPECT_EQ(result, SUCCESS);
392                 if (context != nullptr) {
393                     context->Stop();
394                 }
395             }
396         );
397 
398     IpcCommon::AddPermission(ACCESS_BIOMETRIC_PERMISSION);
399     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
400     EXPECT_NE(mockHdi, nullptr);
401     EXPECT_CALL(*mockHdi, BeginAuthentication(_, _, _))
402         .WillRepeatedly(
__anon64bbd17d0802(uint64_t contextId, const HdiAuthSolution &param, std::vector<HdiScheduleInfo> &scheduleInfos) 403             [&context](uint64_t contextId, const HdiAuthSolution &param, std::vector<HdiScheduleInfo> &scheduleInfos) {
404                 HdiScheduleInfo scheduleInfo = {};
405                 scheduleInfo.authType = HdiAuthType::FACE;
406                 scheduleInfo.scheduleId = 20;
407                 HdiExecutorInfo executorInfo = {};
408                 executorInfo.executorIndex = 60;
409                 scheduleInfo.executors.push_back(executorInfo);
410                 scheduleInfos.push_back(scheduleInfo);
411                 context = ContextPool::Instance().Select(contextId).lock();
412                 return HDF_SUCCESS;
413             }
414         );
415 
416     EXPECT_CALL(*mockHdi, UpdateAuthenticationResult(_, _, _)).WillOnce(Return(HDF_SUCCESS));
417     std::promise<void> promise;
418     EXPECT_CALL(*mockHdi, CancelAuthentication(_))
419         .WillOnce(
__anon64bbd17d0902(uint64_t contextId) 420             [&promise](uint64_t contextId) {
421                 promise.set_value();
422                 return HDF_SUCCESS;
423             }
424         );
425 
426     auto resourceNode = Common::MakeShared<MockResourceNode>();
427     EXPECT_NE(resourceNode, nullptr);
428     EXPECT_CALL(*resourceNode, GetExecutorIndex()).WillRepeatedly(Return(60));
429     EXPECT_CALL(*resourceNode, GetAuthType()).WillRepeatedly(Return(FACE));
430     EXPECT_CALL(*resourceNode, GetExecutorRole()).WillRepeatedly(Return(ALL_IN_ONE));
431     EXPECT_CALL(*resourceNode, GetExecutorMatcher()).WillRepeatedly(Return(0));
432     EXPECT_CALL(*resourceNode, GetExecutorPublicKey()).WillRepeatedly(Return(std::vector<uint8_t>()));
433     EXPECT_CALL(*resourceNode, BeginExecute(_, _, _))
434         .WillOnce(
__anon64bbd17d0a02(uint64_t scheduleId, const std::vector<uint8_t> &publicKey, const Attributes &command) 435             [](uint64_t scheduleId, const std::vector<uint8_t> &publicKey, const Attributes &command) {
436                 auto messenger = ExecutorMessengerService::GetInstance();
437                 EXPECT_NE(messenger, nullptr);
438                 auto finalResult = Common::MakeShared<Attributes>();
439                 EXPECT_NE(finalResult, nullptr);
440                 std::vector<uint8_t> scheduleResult = {1, 2, 3, 4};
441                 EXPECT_TRUE(finalResult->SetUint8ArrayValue(Attributes::ATTR_RESULT, scheduleResult));
442                 EXPECT_EQ(messenger->Finish(20, ALL_IN_ONE, SUCCESS, finalResult), SUCCESS);
443                 return SUCCESS;
444             }
445         );
446 
447     EXPECT_TRUE(ResourceNodePool::Instance().Insert(resourceNode));
448 
449     service.Auth(testApiVersion, testChallenge, testAuthType, testAuthTrustLevel, testCallback);
450     promise.get_future().get();
451 
452     EXPECT_TRUE(ResourceNodePool::Instance().Delete(60));
453     IpcCommon::DeleteAllPermission();
454 }
455 
456 HWTEST_F(UserAuthServiceTest, UserAuthServiceAuthUser001, TestSize.Level0)
457 {
458     UserAuthService service(100, true);
459     int32_t testUserId = 125;
460     std::vector<uint8_t> testChallenge = {1, 2, 3, 4};
461     AuthType testAuthType = FACE;
462     AuthTrustLevel testAuthTrustLevel = ATL2;
463     sptr<UserAuthCallbackInterface> testCallback = new MockUserAuthCallback();
464     EXPECT_NE(testCallback, nullptr);
465     auto *tempCallback = static_cast<MockUserAuthCallback *>(testCallback.GetRefPtr());
466     EXPECT_NE(tempCallback, nullptr);
467     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
468     EXPECT_NE(mockHdi, nullptr);
469     EXPECT_CALL(*tempCallback, OnResult(_, _)).Times(1);
470     EXPECT_CALL(*mockHdi, BeginAuthentication(_, _, _)).Times(1);
471     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
472     uint64_t contextId = service.AuthUser(testUserId, testChallenge, testAuthType, testAuthTrustLevel, testCallback);
473     EXPECT_EQ(contextId, 0);
474     IpcCommon::DeleteAllPermission();
475 }
476 
477 HWTEST_F(UserAuthServiceTest, UserAuthServiceAuthUser002, TestSize.Level0)
478 {
479     UserAuthService service(100, true);
480     int32_t testUserId = 125;
481     std::vector<uint8_t> testChallenge = {1, 2, 3, 4};
482     AuthType testAuthType = FACE;
483     AuthTrustLevel testAuthTrustLevel = ATL2;
484     sptr<UserAuthCallbackInterface> testCallback = nullptr;
485     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
486     uint64_t contextId = service.AuthUser(testUserId, testChallenge, testAuthType, testAuthTrustLevel, testCallback);
487     EXPECT_EQ(contextId, 0);
488 
489     testCallback = new MockUserAuthCallback();
490     EXPECT_NE(testCallback, nullptr);
491     auto *tempCallback = static_cast<MockUserAuthCallback *>(testCallback.GetRefPtr());
492     EXPECT_NE(tempCallback, nullptr);
493     EXPECT_CALL(*tempCallback, OnResult(_, _)).Times(1);
494     testAuthTrustLevel = static_cast<AuthTrustLevel>(90000);
495     contextId = service.AuthUser(testUserId, testChallenge, testAuthType, testAuthTrustLevel, testCallback);
496     EXPECT_EQ(contextId, 0);
497     IpcCommon::DeleteAllPermission();
498 }
499 
500 HWTEST_F(UserAuthServiceTest, UserAuthServiceAuthUser003, TestSize.Level0)
501 {
502     UserAuthService service(100, true);
503     int32_t testUserId = 125;
504     std::vector<uint8_t> testChallenge = {1, 2, 3, 4};
505     AuthType testAuthType = FACE;
506     AuthTrustLevel testAuthTrustLevel = ATL2;
507     std::shared_ptr<Context> context = nullptr;
508 
509     sptr<UserAuthCallbackInterface> testCallback = new MockUserAuthCallback();
510     EXPECT_NE(testCallback, nullptr);
511     auto *tempCallback = static_cast<MockUserAuthCallback *>(testCallback.GetRefPtr());
512     EXPECT_NE(tempCallback, nullptr);
513     EXPECT_CALL(*tempCallback, OnResult(_, _))
514         .Times(2)
515         .WillOnce(
__anon64bbd17d0b02(int32_t result, const Attributes &extraInfo) 516             [](int32_t result, const Attributes &extraInfo) {
517                 EXPECT_EQ(result, CHECK_PERMISSION_FAILED);
518             }
519         )
520         .WillOnce(
__anon64bbd17d0c02(int32_t result, const Attributes &extraInfo) 521             [&context](int32_t result, const Attributes &extraInfo) {
522                 EXPECT_EQ(result, SUCCESS);
523                 if (context != nullptr) {
524                     context->Stop();
525                 }
526             }
527         );
528 
529     uint64_t contextId = service.AuthUser(testUserId, testChallenge, testAuthType, testAuthTrustLevel, testCallback);
530     EXPECT_EQ(contextId, 0);
531 
532     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
533     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
534     EXPECT_NE(mockHdi, nullptr);
535     EXPECT_CALL(*mockHdi, BeginAuthentication(_, _, _))
536         .WillRepeatedly(
__anon64bbd17d0d02(uint64_t contextId, const HdiAuthSolution &param, std::vector<HdiScheduleInfo> &scheduleInfos) 537             [&context](uint64_t contextId, const HdiAuthSolution &param, std::vector<HdiScheduleInfo> &scheduleInfos) {
538                 HdiScheduleInfo scheduleInfo = {};
539                 scheduleInfo.authType = HdiAuthType::FACE;
540                 scheduleInfo.scheduleId = 30;
541                 HdiExecutorInfo executorInfo = {};
542                 executorInfo.executorIndex = 60;
543                 scheduleInfo.executors.push_back(executorInfo);
544                 scheduleInfos.push_back(scheduleInfo);
545                 context = ContextPool::Instance().Select(contextId).lock();
546                 return HDF_SUCCESS;
547             }
548         );
549 
550     EXPECT_CALL(*mockHdi, UpdateAuthenticationResult(_, _, _)).WillOnce(Return(HDF_SUCCESS));
551     std::promise<void> promise;
552     EXPECT_CALL(*mockHdi, CancelAuthentication(_))
553         .WillOnce(
__anon64bbd17d0e02(uint64_t contextId) 554             [&promise](uint64_t contextId) {
555                 promise.set_value();
556                 return HDF_SUCCESS;
557             }
558         );
559 
560     auto resourceNode = Common::MakeShared<MockResourceNode>();
561     EXPECT_NE(resourceNode, nullptr);
562     EXPECT_CALL(*resourceNode, GetExecutorIndex()).WillRepeatedly(Return(60));
563     EXPECT_CALL(*resourceNode, GetAuthType()).WillRepeatedly(Return(FACE));
564     EXPECT_CALL(*resourceNode, GetExecutorRole()).WillRepeatedly(Return(ALL_IN_ONE));
565     EXPECT_CALL(*resourceNode, GetExecutorMatcher()).WillRepeatedly(Return(0));
566     EXPECT_CALL(*resourceNode, GetExecutorPublicKey()).WillRepeatedly(Return(std::vector<uint8_t>()));
567     EXPECT_CALL(*resourceNode, BeginExecute(_, _, _))
568         .WillOnce(
__anon64bbd17d0f02(uint64_t scheduleId, const std::vector<uint8_t> &publicKey, const Attributes &command) 569             [](uint64_t scheduleId, const std::vector<uint8_t> &publicKey, const Attributes &command) {
570                 auto messenger = ExecutorMessengerService::GetInstance();
571                 EXPECT_NE(messenger, nullptr);
572                 auto finalResult = Common::MakeShared<Attributes>();
573                 EXPECT_NE(finalResult, nullptr);
574                 std::vector<uint8_t> scheduleResult = {1, 2, 3, 4};
575                 EXPECT_TRUE(finalResult->SetUint8ArrayValue(Attributes::ATTR_RESULT, scheduleResult));
576                 EXPECT_EQ(messenger->Finish(30, ALL_IN_ONE, SUCCESS, finalResult), SUCCESS);
577                 return SUCCESS;
578             }
579         );
580 
581     EXPECT_TRUE(ResourceNodePool::Instance().Insert(resourceNode));
582 
583     contextId = service.AuthUser(testUserId, testChallenge, testAuthType, testAuthTrustLevel, testCallback);
584     promise.get_future().get();
585 
586     EXPECT_TRUE(ResourceNodePool::Instance().Delete(60));
587     IpcCommon::DeleteAllPermission();
588 }
589 
590 HWTEST_F(UserAuthServiceTest, UserAuthServiceIdentify001, TestSize.Level0)
591 {
592     UserAuthService service(100, true);
593     std::vector<uint8_t> testChallenge = {1, 2, 3, 4};
594     AuthType testAuthType = FACE;
595     sptr<UserAuthCallbackInterface> testCallback = new MockUserAuthCallback();
596     EXPECT_NE(testCallback, nullptr);
597     auto *tempCallback = static_cast<MockUserAuthCallback *>(testCallback.GetRefPtr());
598     EXPECT_NE(tempCallback, nullptr);
599     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
600     EXPECT_NE(mockHdi, nullptr);
601     EXPECT_CALL(*tempCallback, OnResult(_, _)).Times(1);
602     EXPECT_CALL(*mockHdi, BeginIdentification(_, _, _, _, _)).Times(1);
603     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
604     uint64_t contextId = service.Identify(testChallenge, testAuthType, testCallback);
605     EXPECT_EQ(contextId, 0);
606     IpcCommon::DeleteAllPermission();
607 }
608 
609 HWTEST_F(UserAuthServiceTest, UserAuthServiceIdentify002, TestSize.Level0)
610 {
611     UserAuthService service(100, true);
612     std::vector<uint8_t> testChallenge = {1, 2, 3, 4};
613     AuthType testAuthType = FACE;
614     sptr<UserAuthCallbackInterface> testCallback = nullptr;
615     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
616     uint64_t contextId = service.Identify(testChallenge, testAuthType, testCallback);
617     EXPECT_EQ(contextId, 0);
618 
619     testCallback = new MockUserAuthCallback();
620     EXPECT_NE(testCallback, nullptr);
621     auto *tempCallback = static_cast<MockUserAuthCallback *>(testCallback.GetRefPtr());
622     EXPECT_NE(tempCallback, nullptr);
623     EXPECT_CALL(*tempCallback, OnResult(_, _)).Times(1);
624     testAuthType = PIN;
625     contextId = service.Identify(testChallenge, testAuthType, testCallback);
626     EXPECT_EQ(contextId, 0);
627     IpcCommon::DeleteAllPermission();
628 }
629 
630 HWTEST_F(UserAuthServiceTest, UserAuthServiceIdentify003, TestSize.Level0)
631 {
632     UserAuthService service(100, true);
633     std::vector<uint8_t> testChallenge = {1, 2, 3, 4};
634     AuthType testAuthType = FACE;
635     std::shared_ptr<Context> context = nullptr;
636 
637     sptr<UserAuthCallbackInterface> testCallback = new MockUserAuthCallback();
638     EXPECT_NE(testCallback, nullptr);
639     auto *tempCallback = static_cast<MockUserAuthCallback *>(testCallback.GetRefPtr());
640     EXPECT_NE(tempCallback, nullptr);
641     EXPECT_CALL(*tempCallback, OnResult(_, _))
642         .Times(2)
643         .WillOnce(
__anon64bbd17d1002(int32_t result, const Attributes &extraInfo) 644             [](int32_t result, const Attributes &extraInfo) {
645                 EXPECT_EQ(result, CHECK_PERMISSION_FAILED);
646             }
647         )
648         .WillOnce(
__anon64bbd17d1102(int32_t result, const Attributes &extraInfo) 649             [&context](int32_t result, const Attributes &extraInfo) {
650                 EXPECT_EQ(result, SUCCESS);
651                 if (context != nullptr) {
652                     context->Stop();
653                 }
654             }
655         );
656 
657     uint64_t contextId = service.Identify(testChallenge, testAuthType, testCallback);
658     EXPECT_EQ(contextId, 0);
659 
660     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
661     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
662     EXPECT_NE(mockHdi, nullptr);
663     EXPECT_CALL(*mockHdi, BeginIdentification(_, _, _, _, _))
664         .WillRepeatedly(
665             [&context](uint64_t contextId, HdiAuthType authType, const std::vector<uint8_t> &challenge,
__anon64bbd17d1202(uint64_t contextId, HdiAuthType authType, const std::vector<uint8_t> &challenge, uint32_t executorId, HdiScheduleInfo &scheduleInfo) 666                 uint32_t executorId, HdiScheduleInfo &scheduleInfo) {
667                 scheduleInfo.authType = HdiAuthType::FACE;
668                 scheduleInfo.scheduleId = 50;
669                 HdiExecutorInfo executorInfo = {};
670                 executorInfo.executorIndex = 60;
671                 scheduleInfo.executors.push_back(executorInfo);
672                 context = ContextPool::Instance().Select(contextId).lock();
673                 return HDF_SUCCESS;
674             }
675         );
676 
677     EXPECT_CALL(*mockHdi, UpdateIdentificationResult(_, _, _)).WillOnce(Return(HDF_SUCCESS));
678     std::promise<void> promise;
679     EXPECT_CALL(*mockHdi, CancelIdentification(_))
680         .WillOnce(
__anon64bbd17d1302(uint64_t contextId) 681             [&promise](uint64_t contextId) {
682                 promise.set_value();
683                 return HDF_SUCCESS;
684             }
685         );
686 
687     auto resourceNode = Common::MakeShared<MockResourceNode>();
688     EXPECT_NE(resourceNode, nullptr);
689     EXPECT_CALL(*resourceNode, GetExecutorIndex()).WillRepeatedly(Return(60));
690     EXPECT_CALL(*resourceNode, GetAuthType()).WillRepeatedly(Return(FACE));
691     EXPECT_CALL(*resourceNode, GetExecutorRole()).WillRepeatedly(Return(ALL_IN_ONE));
692     EXPECT_CALL(*resourceNode, GetExecutorMatcher()).WillRepeatedly(Return(0));
693     EXPECT_CALL(*resourceNode, GetExecutorPublicKey()).WillRepeatedly(Return(std::vector<uint8_t>()));
694     EXPECT_CALL(*resourceNode, BeginExecute(_, _, _))
695         .WillOnce(
__anon64bbd17d1402(uint64_t scheduleId, const std::vector<uint8_t> &publicKey, const Attributes &command) 696             [](uint64_t scheduleId, const std::vector<uint8_t> &publicKey, const Attributes &command) {
697                 auto messenger = ExecutorMessengerService::GetInstance();
698                 EXPECT_NE(messenger, nullptr);
699                 auto finalResult = Common::MakeShared<Attributes>();
700                 EXPECT_NE(finalResult, nullptr);
701                 std::vector<uint8_t> scheduleResult = {1, 2, 3, 4};
702                 EXPECT_TRUE(finalResult->SetUint8ArrayValue(Attributes::ATTR_RESULT, scheduleResult));
703                 EXPECT_EQ(messenger->Finish(50, ALL_IN_ONE, SUCCESS, finalResult), SUCCESS);
704                 return SUCCESS;
705             }
706         );
707 
708     EXPECT_TRUE(ResourceNodePool::Instance().Insert(resourceNode));
709 
710     contextId = service.Identify(testChallenge, testAuthType, testCallback);
711     promise.get_future().get();
712 
713     EXPECT_TRUE(ResourceNodePool::Instance().Delete(60));
714     IpcCommon::DeleteAllPermission();
715 }
716 
717 HWTEST_F(UserAuthServiceTest, UserAuthServiceCancelAuthOrIdentify_001, TestSize.Level0)
718 {
719     UserAuthService service(100, true);
720     uint64_t testContextId = 12355236;
721     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
722     EXPECT_EQ(service.CancelAuthOrIdentify(testContextId), GENERAL_ERROR);
723     IpcCommon::DeleteAllPermission();
724 }
725 
726 HWTEST_F(UserAuthServiceTest, UserAuthServiceCancelAuthOrIdentify_002, TestSize.Level0)
727 {
728     UserAuthService service(100, true);
729     uint64_t testContextId = 12355236;
730     EXPECT_EQ(service.CancelAuthOrIdentify(testContextId), CHECK_PERMISSION_FAILED);
731 
732     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
733     auto context = Common::MakeShared<MockContext>();
734     EXPECT_NE(context, nullptr);
735     EXPECT_CALL(*context, GetContextId()).WillRepeatedly(Return(testContextId));
736     EXPECT_CALL(*context, GetLatestError()).WillRepeatedly(Return(GENERAL_ERROR));
737     EXPECT_CALL(*context, Stop())
738         .Times(2)
739         .WillOnce(Return(false))
740         .WillOnce(Return(true));
741 
742     EXPECT_TRUE(ContextPool::Instance().Insert(context));
743 
744     EXPECT_EQ(service.CancelAuthOrIdentify(testContextId), GENERAL_ERROR);
745     EXPECT_EQ(service.CancelAuthOrIdentify(testContextId), SUCCESS);
746 
747     EXPECT_TRUE(ContextPool::Instance().Delete(testContextId));
748     IpcCommon::DeleteAllPermission();
749 }
750 
751 HWTEST_F(UserAuthServiceTest, UserAuthServiceGetVersion, TestSize.Level0)
752 {
753     UserAuthService service(100, true);
754     int32_t version = -1;
755     EXPECT_EQ(service.GetVersion(version), CHECK_PERMISSION_FAILED);
756     IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION);
757     EXPECT_EQ(service.GetVersion(version), SUCCESS);
758     EXPECT_EQ(version, 1);
759     IpcCommon::DeleteAllPermission();
760 }
761 } // namespace UserAuth
762 } // namespace UserIam
763 } // namespace OHOS