• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "user_auth_client_test.h"
17 
18 #include "iam_ptr.h"
19 #include "modal_callback_service.h"
20 #include "user_auth_client.h"
21 #include "user_auth_client_impl.h"
22 
23 namespace OHOS {
24 namespace UserIam {
25 namespace UserAuth {
26 using namespace testing;
27 using namespace testing::ext;
28 
SetUpTestCase()29 void UserAuthClientTest::SetUpTestCase()
30 {
31 }
32 
TearDownTestCase()33 void UserAuthClientTest::TearDownTestCase()
34 {
35 }
36 
SetUp()37 void UserAuthClientTest::SetUp()
38 {
39 }
40 
TearDown()41 void UserAuthClientTest::TearDown()
42 {
43 }
44 
45 HWTEST_F(UserAuthClientTest, UserAuthClientGetEnrolledState001, TestSize.Level0)
46 {
47     AuthType testAuthType = FACE;
48     int32_t testApiVersion = 0;
49     EnrolledState testEnrolledState = {};
50 
51     IpcClientUtils::ResetObj();
52     int32_t ret = UserAuthClientImpl::Instance().GetEnrolledState(testApiVersion, testAuthType, testEnrolledState);
53     EXPECT_EQ(ret, GENERAL_ERROR);
54 }
55 
56 HWTEST_F(UserAuthClientTest, UserAuthClientGetEnrolledState002, TestSize.Level0)
57 {
58     AuthType testAuthType = FACE;
59     int32_t testApiVersion = 0;
60     EnrolledState testEnrolledState = {};
61 
62     uint16_t credentialDigest = 23962;
63     uint16_t credentialCount = 1;
64 
65     auto service = Common::MakeShared<MockUserAuthService>();
66     EXPECT_NE(service, nullptr);
67     EXPECT_CALL(*service, GetEnrolledState(_, _, _)).Times(1);
68     ON_CALL(*service, GetEnrolledState)
69         .WillByDefault(
70             [&testApiVersion, &testAuthType, &credentialDigest, &credentialCount](int32_t apiVersion, AuthType authType,
__anonf8df1b5b0102(int32_t apiVersion, AuthType authType, EnrolledState &enrolledState) 71                 EnrolledState &enrolledState) {
72                 EXPECT_EQ(apiVersion, testApiVersion);
73                 EXPECT_EQ(authType, testAuthType);
74 
75                 enrolledState.credentialDigest = credentialDigest;
76                 enrolledState.credentialCount = credentialCount;
77                 return SUCCESS;
78             }
79         );
80     sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
81     sptr<IRemoteObject::DeathRecipient> dr(nullptr);
82     CallRemoteObject(service, obj, dr);
83     int32_t ret = UserAuthClientImpl::Instance().GetEnrolledState(testApiVersion, testAuthType, testEnrolledState);
84     EXPECT_EQ(ret, SUCCESS);
85     EXPECT_EQ(testEnrolledState.credentialDigest, credentialDigest);
86     EXPECT_EQ(testEnrolledState.credentialCount, credentialCount);
87 
88     EXPECT_NE(dr, nullptr);
89     dr->OnRemoteDied(obj);
90     IpcClientUtils::ResetObj();
91 }
92 
93 HWTEST_F(UserAuthClientTest, UserAuthClientGetAvailableStatus001, TestSize.Level0)
94 {
95     AuthType testAuthType = FACE;
96     AuthTrustLevel testAtl = ATL1;
97 
98     IpcClientUtils::ResetObj();
99     int32_t ret = UserAuthClientImpl::Instance().GetAvailableStatus(testAuthType, testAtl);
100     EXPECT_EQ(ret, GENERAL_ERROR);
101 }
102 
103 HWTEST_F(UserAuthClientTest, UserAuthClientGetAvailableStatus002, TestSize.Level0)
104 {
105     int32_t testApiVersion = 10000;
106     AuthType testAuthType = FACE;
107     AuthTrustLevel testAtl = ATL1;
108 
109     auto service = Common::MakeShared<MockUserAuthService>();
110     EXPECT_NE(service, nullptr);
111     EXPECT_CALL(*service, GetAvailableStatus(_, _, _, _)).Times(1);
112     ON_CALL(*service, GetAvailableStatus)
113         .WillByDefault(
114             [&testApiVersion, &testAuthType, &testAtl](int32_t apiVersion, int32_t userId, AuthType authType,
__anonf8df1b5b0202(int32_t apiVersion, int32_t userId, AuthType authType, AuthTrustLevel authTrustLevel) 115                 AuthTrustLevel authTrustLevel) {
116                 return SUCCESS;
117             }
118         );
119     sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
120     sptr<IRemoteObject::DeathRecipient> dr(nullptr);
121     CallRemoteObject(service, obj, dr);
122     int32_t ret = UserAuthClientImpl::Instance().GetAvailableStatus(testApiVersion, testAuthType, testAtl);
123     EXPECT_EQ(ret, SUCCESS);
124     EXPECT_NE(dr, nullptr);
125     dr->OnRemoteDied(obj);
126     IpcClientUtils::ResetObj();
127 }
128 
129 HWTEST_F(UserAuthClientTest, UserAuthClientGetProperty001, TestSize.Level0)
130 {
131     int32_t testUserId = 200;
132     GetPropertyRequest testRequest = {};
133 
134     std::shared_ptr<MockGetPropCallback> testCallback = nullptr;
135     UserAuthClient::GetInstance().GetProperty(testUserId, testRequest, testCallback);
136 
137     IpcClientUtils::ResetObj();
138     testCallback = Common::MakeShared<MockGetPropCallback>();
139     EXPECT_NE(testCallback, nullptr);
140     EXPECT_CALL(*testCallback, OnResult(_, _)).Times(1);
141     UserAuthClient::GetInstance().GetProperty(testUserId, testRequest, testCallback);
142 }
143 
144 HWTEST_F(UserAuthClientTest, UserAuthClientGetProperty002, TestSize.Level0)
145 {
146     int32_t testUserId = 200;
147     GetPropertyRequest testRequest = {};
148     testRequest.authType = FACE;
149     testRequest.keys = {Attributes::ATTR_RESULT_CODE, Attributes::ATTR_SIGNATURE};
150 
151     auto testCallback = Common::MakeShared<MockGetPropCallback>();
152     EXPECT_NE(testCallback, nullptr);
153     EXPECT_CALL(*testCallback, OnResult(_, _)).Times(1);
154 
155     auto service = Common::MakeShared<MockUserAuthService>();
156     EXPECT_NE(service, nullptr);
157     EXPECT_CALL(*service, GetProperty(_, _, _, _)).Times(1);
158     ON_CALL(*service, GetProperty)
159         .WillByDefault(
160             [&testUserId, &testRequest](int32_t userId, AuthType authType,
161                 const std::vector<Attributes::AttributeKey> &keys,
__anonf8df1b5b0302(int32_t userId, AuthType authType, const std::vector<Attributes::AttributeKey> &keys, sptr<GetExecutorPropertyCallbackInterface> &callback) 162                 sptr<GetExecutorPropertyCallbackInterface> &callback) {
163                 EXPECT_EQ(userId, testUserId);
164                 EXPECT_EQ(authType, testRequest.authType);
165                 EXPECT_THAT(keys, ElementsAreArray(testRequest.keys));
166                 if (callback != nullptr) {
167                     Attributes extraInfo;
168                     callback->OnGetExecutorPropertyResult(SUCCESS, extraInfo);
169                 }
170             }
171         );
172     sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
173     sptr<IRemoteObject::DeathRecipient> dr(nullptr);
174     CallRemoteObject(service, obj, dr);
175     UserAuthClient::GetInstance().GetProperty(testUserId, testRequest, testCallback);
176     EXPECT_NE(dr, nullptr);
177     dr->OnRemoteDied(obj);
178     IpcClientUtils::ResetObj();
179 }
180 
181 HWTEST_F(UserAuthClientTest, UserAuthClientSetProperty001, TestSize.Level0)
182 {
183     int32_t testUserId = 200;
184     SetPropertyRequest testRequest = {};
185     std::shared_ptr<MockSetPropCallback> testCallback = nullptr;
186     UserAuthClient::GetInstance().SetProperty(testUserId, testRequest, testCallback);
187 
188     IpcClientUtils::ResetObj();
189     testCallback = Common::MakeShared<MockSetPropCallback>();
190     EXPECT_NE(testCallback, nullptr);
191     EXPECT_CALL(*testCallback, OnResult(_, _)).Times(1);
192     UserAuthClient::GetInstance().SetProperty(testUserId, testRequest, testCallback);
193 }
194 
195 HWTEST_F(UserAuthClientTest, UserAuthClientSetProperty002, TestSize.Level0)
196 {
197     int32_t testUserId = 200;
198     SetPropertyRequest testRequest = {};
199     testRequest.authType = PIN;
200     testRequest.mode = PROPERTY_INIT_ALGORITHM;
201     EXPECT_EQ(testRequest.attrs.SetInt32Value(static_cast<Attributes::AttributeKey>(testRequest.mode), FAIL), true);
202     auto testCallback = Common::MakeShared<MockSetPropCallback>();
203     EXPECT_NE(testCallback, nullptr);
204     EXPECT_CALL(*testCallback, OnResult(_, _)).Times(1);
205 
206     auto service = Common::MakeShared<MockUserAuthService>();
207     EXPECT_NE(service, nullptr);
208     EXPECT_CALL(*service, SetProperty(_, _, _, _)).Times(1);
209     ON_CALL(*service, SetProperty)
210         .WillByDefault(
211             [&testUserId, &testRequest](int32_t userId, AuthType authType, const Attributes &attributes,
__anonf8df1b5b0402(int32_t userId, AuthType authType, const Attributes &attributes, sptr<SetExecutorPropertyCallbackInterface> &callback) 212                 sptr<SetExecutorPropertyCallbackInterface> &callback) {
213                 EXPECT_EQ(userId, testUserId);
214                 EXPECT_EQ(authType, testRequest.authType);
215                 if (callback != nullptr) {
216                     callback->OnSetExecutorPropertyResult(SUCCESS);
217                 }
218             }
219         );
220     sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
221     sptr<IRemoteObject::DeathRecipient> dr(nullptr);
222     CallRemoteObject(service, obj, dr);
223 
224     UserAuthClient::GetInstance().SetProperty(testUserId, testRequest, testCallback);
225     EXPECT_NE(dr, nullptr);
226     dr->OnRemoteDied(obj);
227     IpcClientUtils::ResetObj();
228 }
229 
230 HWTEST_F(UserAuthClientTest, UserAuthClientBeginNorthAuthentication001, TestSize.Level0)
231 {
232     int32_t testApiVersion = 8;
233     std::vector<uint8_t> testChallenge = {1, 2, 3, 4, 3, 2, 1, 0};
234     AuthType testAuthType = PIN;
235     AuthTrustLevel testAtl = ATL1;
236     std::shared_ptr<MockAuthenticationCallback> testCallback = nullptr;
237     uint64_t contextId = UserAuthClientImpl::Instance().BeginNorthAuthentication(testApiVersion, testChallenge,
238         testAuthType, testAtl, testCallback);
239     EXPECT_EQ(contextId, 0);
240 
241     IpcClientUtils::ResetObj();
242     testCallback = Common::MakeShared<MockAuthenticationCallback>();
243     EXPECT_NE(testCallback, nullptr);
244     EXPECT_CALL(*testCallback, OnResult(_, _)).Times(1);
245     contextId = UserAuthClientImpl::Instance().BeginNorthAuthentication(testApiVersion, testChallenge,
246         testAuthType, testAtl, testCallback);
247     EXPECT_EQ(contextId, 0);
248 }
249 
250 HWTEST_F(UserAuthClientTest, UserAuthClientBeginNorthAuthentication002, TestSize.Level0)
251 {
252     int32_t testApiVersion = 9;
253     std::vector<uint8_t> testChallenge = {1, 2, 3, 4, 3, 2, 1, 0};
254     AuthType testAuthType = PIN;
255     AuthTrustLevel testAtl = ATL1;
256     auto testCallback = Common::MakeShared<MockAuthenticationCallback>();
257     EXPECT_NE(testCallback, nullptr);
258     EXPECT_CALL(*testCallback, OnResult(_, _)).Times(1);
259 
260     uint64_t testContextId = 15858;
261 
262     auto service = Common::MakeShared<MockUserAuthService>();
263     EXPECT_NE(service, nullptr);
264     EXPECT_CALL(*service, Auth(_, _, _, _, _)).Times(1);
265     ON_CALL(*service, Auth)
266         .WillByDefault(
267             [&testApiVersion, &testChallenge, &testAuthType, &testAtl, &testContextId](int32_t apiVersion,
268                 const std::vector<uint8_t> &challenge, AuthType authType, AuthTrustLevel authTrustLevel,
__anonf8df1b5b0502(int32_t apiVersion, const std::vector<uint8_t> &challenge, AuthType authType, AuthTrustLevel authTrustLevel, sptr<UserAuthCallbackInterface> &callback) 269                 sptr<UserAuthCallbackInterface> &callback) {
270                 EXPECT_EQ(apiVersion, testApiVersion);
271                 EXPECT_THAT(challenge, ElementsAreArray(testChallenge));
272                 EXPECT_EQ(authType, testAuthType);
273                 EXPECT_EQ(authTrustLevel, testAtl);
274                 if (callback != nullptr) {
275                     Attributes extraInfo;
276                     callback->OnResult(SUCCESS, extraInfo);
277                 }
278                 return testContextId;
279             }
280         );
281     sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
282     sptr<IRemoteObject::DeathRecipient> dr(nullptr);
283     CallRemoteObject(service, obj, dr);
284 
285     uint64_t contextId = UserAuthClientImpl::Instance().BeginNorthAuthentication(testApiVersion, testChallenge,
286         testAuthType, testAtl, testCallback);
287     EXPECT_EQ(contextId, testContextId);
288     EXPECT_NE(dr, nullptr);
289     dr->OnRemoteDied(obj);
290     IpcClientUtils::ResetObj();
291 }
292 
293 HWTEST_F(UserAuthClientTest, UserAuthClientBeginAuthentication001, TestSize.Level0)
294 {
295     AuthParam testAuthParam = {
296         .userId = 84548,
297         .challenge = {1, 2, 3, 4, 8, 7, 5, 4},
298         .authType = PIN,
299         .authTrustLevel = ATL1
300     };
301     std::shared_ptr<MockAuthenticationCallback> testCallback = nullptr;
302     uint64_t contextId = UserAuthClient::GetInstance().BeginAuthentication(testAuthParam, testCallback);
303     EXPECT_EQ(contextId, 0);
304 
305     IpcClientUtils::ResetObj();
306     testCallback = Common::MakeShared<MockAuthenticationCallback>();
307     EXPECT_NE(testCallback, nullptr);
308     EXPECT_CALL(*testCallback, OnResult(_, _)).Times(1);
309     contextId = UserAuthClient::GetInstance().BeginAuthentication(testAuthParam, testCallback);
310     EXPECT_EQ(contextId, 0);
311 }
312 
313 HWTEST_F(UserAuthClientTest, UserAuthClientBeginAuthentication002, TestSize.Level0)
314 {
315     AuthParam testAuthParam = {
316         .userId = 84548,
317         .challenge = {1, 2, 3, 4, 8, 7, 5, 4},
318         .authType = PIN,
319         .authTrustLevel = ATL1,
320     };
321     auto testCallback = Common::MakeShared<MockAuthenticationCallback>();
322     EXPECT_NE(testCallback, nullptr);
323     EXPECT_CALL(*testCallback, OnResult(_, _)).Times(1);
324 
325     uint64_t testContextId = 15858;
326 
327     auto service = Common::MakeShared<MockUserAuthService>();
328     EXPECT_NE(service, nullptr);
329     EXPECT_CALL(*service, AuthUser(_, _, _)).Times(1);
330     ON_CALL(*service, AuthUser)
331         .WillByDefault(
332             [&testAuthParam, &testContextId](AuthParamInner &authParam,
__anonf8df1b5b0602(AuthParamInner &authParam, std::optional<RemoteAuthParam> &remoteAuthParam, sptr<UserAuthCallbackInterface> &callback) 333             std::optional<RemoteAuthParam> &remoteAuthParam, sptr<UserAuthCallbackInterface> &callback) {
334                 EXPECT_EQ(authParam.userId, testAuthParam.userId);
335                 EXPECT_THAT(authParam.challenge, ElementsAreArray(testAuthParam.challenge));
336                 EXPECT_EQ(authParam.authType, testAuthParam.authType);
337                 EXPECT_EQ(authParam.authTrustLevel, testAuthParam.authTrustLevel);
338                 if (callback != nullptr) {
339                     Attributes extraInfo;
340                     callback->OnResult(SUCCESS, extraInfo);
341                 }
342                 return testContextId;
343             }
344         );
345     sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
346     sptr<IRemoteObject::DeathRecipient> dr(nullptr);
347     CallRemoteObject(service, obj, dr);
348 
349     uint64_t contextId = UserAuthClient::GetInstance().BeginAuthentication(testAuthParam, testCallback);
350     EXPECT_EQ(contextId, testContextId);
351     EXPECT_NE(dr, nullptr);
352     dr->OnRemoteDied(obj);
353     IpcClientUtils::ResetObj();
354 }
355 
356 HWTEST_F(UserAuthClientTest, UserAuthClientImplBeginAuthentication001, TestSize.Level0)
357 {
358     std::optional<RemoteAuthParam> remoteAuthParam = {};
359     RemoteAuthParam param = {};
360     param.verifierNetworkId = "123";
361     param.collectorNetworkId = "1233324321423412344134";
362     remoteAuthParam = param;
363     AuthParam testAuthParam = {
364         .userId = 84548,
365         .challenge = {1, 2, 3, 4, 8, 7, 5, 4},
366         .authType = PIN,
367         .authTrustLevel = ATL1,
368         .remoteAuthParam = remoteAuthParam
369     };
370     std::shared_ptr<MockAuthenticationCallback> testCallback = nullptr;
371     testCallback = Common::MakeShared<MockAuthenticationCallback>();
372     uint64_t contextId = UserAuthClientImpl::GetInstance().BeginAuthentication(testAuthParam, testCallback);
373     EXPECT_EQ(contextId, 0);
374 }
375 
376 HWTEST_F(UserAuthClientTest, UserAuthClientImplPrepareRemoteAuth, TestSize.Level0)
377 {
378     const std::string networkId = "123";
379     std::shared_ptr<PrepareRemoteAuthCallback> testCallback = nullptr;
380     uint64_t contextId = UserAuthClientImpl::GetInstance().PrepareRemoteAuth(networkId, testCallback);
381     EXPECT_EQ(contextId, 2);
382 }
383 
384 HWTEST_F(UserAuthClientTest, UserAuthClientCancelAuthentication001, TestSize.Level0)
385 {
386     uint64_t testContextId = 12345562;
387 
388     IpcClientUtils::ResetObj();
389     int32_t ret = UserAuthClient::GetInstance().CancelAuthentication(testContextId);
390     EXPECT_EQ(ret, GENERAL_ERROR);
391 }
392 
393 HWTEST_F(UserAuthClientTest, UserAuthClientCancelAuthentication002, TestSize.Level0)
394 {
395     uint64_t testContextId = 12345562;
396     int32_t testCancelReason = 0;
397 
398     auto service = Common::MakeShared<MockUserAuthService>();
399     EXPECT_NE(service, nullptr);
400     EXPECT_CALL(*service, CancelAuthOrIdentify(_, _)).Times(1);
401     ON_CALL(*service, CancelAuthOrIdentify)
402         .WillByDefault(
__anonf8df1b5b0702(uint64_t contextId, int32_t cancelReason) 403             [&testContextId, &testCancelReason](uint64_t contextId, int32_t cancelReason) {
404                 EXPECT_EQ(contextId, testContextId);
405                 EXPECT_EQ(cancelReason, testCancelReason);
406                 return SUCCESS;
407             }
408         );
409     sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
410     sptr<IRemoteObject::DeathRecipient> dr(nullptr);
411     CallRemoteObject(service, obj, dr);
412 
413     int32_t ret = UserAuthClient::GetInstance().CancelAuthentication(testContextId);
414     EXPECT_EQ(ret, SUCCESS);
415     EXPECT_NE(dr, nullptr);
416     dr->OnRemoteDied(obj);
417     IpcClientUtils::ResetObj();
418 }
419 
420 HWTEST_F(UserAuthClientTest, UserAuthClientBeginIdentification_1001, TestSize.Level0)
421 {
422     std::vector<uint8_t> testChallenge = {4, 5, 6, 7, 3, 4, 1, 2};
423     AuthType testAuthType = FACE;
424     std::shared_ptr<MockIdentificationCallback> testCallback = nullptr;
425     uint64_t contextId = UserAuthClient::GetInstance().BeginIdentification(testChallenge, testAuthType, testCallback);
426     EXPECT_EQ(contextId, 0);
427 
428     IpcClientUtils::ResetObj();
429     testCallback = Common::MakeShared<MockIdentificationCallback>();
430     EXPECT_NE(testCallback, nullptr);
431     EXPECT_CALL(*testCallback, OnResult(_, _)).Times(1);
432     contextId = UserAuthClient::GetInstance().BeginIdentification(testChallenge, testAuthType, testCallback);
433     EXPECT_EQ(contextId, 0);
434 }
435 
436 HWTEST_F(UserAuthClientTest, UserAuthClientBeginIdentification_1002, TestSize.Level0)
437 {
438     std::vector<uint8_t> testChallenge = {4, 5, 6, 7, 3, 4, 1, 2};
439     AuthType testAuthType = FACE;
440     auto testCallback = Common::MakeShared<MockIdentificationCallback>();
441     EXPECT_NE(testCallback, nullptr);
442     EXPECT_CALL(*testCallback, OnResult(_, _)).Times(1);
443 
444     uint64_t testContextId = 548781;
445 
446     auto service = Common::MakeShared<MockUserAuthService>();
447     EXPECT_NE(service, nullptr);
448     EXPECT_CALL(*service, Identify(_, _, _)).Times(1);
449     ON_CALL(*service, Identify)
450         .WillByDefault(
451             [&testChallenge, &testAuthType, &testContextId](const std::vector<uint8_t> &challenge,
__anonf8df1b5b0802(const std::vector<uint8_t> &challenge, AuthType authType, sptr<UserAuthCallbackInterface> &callback) 452                 AuthType authType, sptr<UserAuthCallbackInterface> &callback) {
453                 EXPECT_THAT(challenge, ElementsAreArray(testChallenge));
454                 EXPECT_EQ(authType, testAuthType);
455                 if (callback != nullptr) {
456                     Attributes extraInfo;
457                     callback->OnResult(SUCCESS, extraInfo);
458                 }
459                 return testContextId;
460             }
461         );
462     sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
463     sptr<IRemoteObject::DeathRecipient> dr(nullptr);
464     CallRemoteObject(service, obj, dr);
465 
466     uint64_t contextId = UserAuthClient::GetInstance().BeginIdentification(testChallenge, testAuthType, testCallback);
467     EXPECT_EQ(contextId, testContextId);
468     EXPECT_NE(dr, nullptr);
469     dr->OnRemoteDied(obj);
470     IpcClientUtils::ResetObj();
471 }
472 
473 HWTEST_F(UserAuthClientTest, UserAuthClientCancelIdentification001, TestSize.Level0)
474 {
475     uint64_t testContextId = 1221215;
476 
477     IpcClientUtils::ResetObj();
478     int32_t ret = UserAuthClient::GetInstance().CancelIdentification(testContextId);
479     EXPECT_EQ(ret, GENERAL_ERROR);
480 }
481 
482 HWTEST_F(UserAuthClientTest, UserAuthClientCancelIdentification002, TestSize.Level0)
483 {
484     uint64_t testContextId = 1221215;
485     int32_t testCancelReason = 0;
486 
487     auto service = Common::MakeShared<MockUserAuthService>();
488     EXPECT_NE(service, nullptr);
489     EXPECT_CALL(*service, CancelAuthOrIdentify(_, _)).Times(1);
490     ON_CALL(*service, CancelAuthOrIdentify)
491         .WillByDefault(
__anonf8df1b5b0902(uint64_t contextId, int32_t cancelReason) 492             [&testContextId, &testCancelReason](uint64_t contextId, int32_t cancelReason) {
493                 EXPECT_EQ(contextId, testContextId);
494                 EXPECT_EQ(cancelReason, testCancelReason);
495                 return SUCCESS;
496             }
497         );
498     sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
499     sptr<IRemoteObject::DeathRecipient> dr(nullptr);
500     CallRemoteObject(service, obj, dr);
501 
502     int32_t ret = UserAuthClient::GetInstance().CancelIdentification(testContextId);
503     EXPECT_EQ(ret, SUCCESS);
504     EXPECT_NE(dr, nullptr);
505     dr->OnRemoteDied(obj);
506     IpcClientUtils::ResetObj();
507 }
508 
509 HWTEST_F(UserAuthClientTest, UserAuthClientGetVersion001, TestSize.Level0)
510 {
511     IpcClientUtils::ResetObj();
512     int32_t version = -1;
513     int32_t ret = UserAuthClientImpl::Instance().GetVersion(version);
514     EXPECT_EQ(ret, GENERAL_ERROR);
515     IpcClientUtils::ResetObj();
516 }
517 
518 HWTEST_F(UserAuthClientTest, UserAuthClientGetVersion002, TestSize.Level0)
519 {
520     int32_t testVersion = 20000;
521 
522     auto service = Common::MakeShared<MockUserAuthService>();
523     EXPECT_NE(service, nullptr);
524     EXPECT_CALL(*service, GetVersion(_)).Times(1);
525     ON_CALL(*service, GetVersion)
526         .WillByDefault(
__anonf8df1b5b0a02(int32_t &version) 527             [&testVersion](int32_t &version) {
528                 version = testVersion;
529                 return SUCCESS;
530             }
531         );
532 
533     sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
534     sptr<IRemoteObject::DeathRecipient> dr(nullptr);
535     CallRemoteObject(service, obj, dr);
536     int32_t version;
537     int32_t result = UserAuthClientImpl::Instance().GetVersion(version);
538     EXPECT_EQ(result, SUCCESS);
539     EXPECT_EQ(version, testVersion);
540     EXPECT_NE(dr, nullptr);
541     dr->OnRemoteDied(obj);
542     IpcClientUtils::ResetObj();
543 }
544 
545 HWTEST_F(UserAuthClientTest, UserAuthClientGetVersion003, TestSize.Level0)
546 {
547     sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
548     EXPECT_NE(obj, nullptr);
549     EXPECT_CALL(*obj, IsProxyObject()).WillRepeatedly(Return(true));
550 
551     sptr<IRemoteObject::DeathRecipient> dr(nullptr);
552     EXPECT_CALL(*obj, RemoveDeathRecipient(_)).WillRepeatedly(Return(true));
553     EXPECT_CALL(*obj, AddDeathRecipient(_))
554         .WillOnce(Return(false))
__anonf8df1b5b0b02(const sptr<IRemoteObject::DeathRecipient> &recipient) 555         .WillRepeatedly([&dr](const sptr<IRemoteObject::DeathRecipient> &recipient) {
556             dr = recipient;
557             return true;
558         });
559 
560     EXPECT_CALL(*obj, SendRequest(_, _, _, _)).WillRepeatedly(Return(OHOS::NO_ERROR));
561 
562     IpcClientUtils::SetObj(obj);
563 
564     int32_t version;
565     EXPECT_EQ(UserAuthClientImpl::Instance().GetVersion(version), GENERAL_ERROR);
566     EXPECT_EQ(UserAuthClientImpl::Instance().GetVersion(version), GENERAL_ERROR);
567     EXPECT_EQ(UserAuthClientImpl::Instance().GetVersion(version), GENERAL_ERROR);
568 
569     EXPECT_NE(dr, nullptr);
570     sptr<IRemoteObject> remote(nullptr);
571     dr->OnRemoteDied(remote);
572     dr->OnRemoteDied(obj);
573     IpcClientUtils::ResetObj();
574 }
575 
576 HWTEST_F(UserAuthClientTest, UserAuthClientBeginWidgetAuth001, TestSize.Level0)
577 {
578     static const int32_t apiVersion = 0;
579     WidgetAuthParam authParam;
580     WidgetParam widgetParam;
581     std::shared_ptr<MockAuthenticationCallback> testCallback = nullptr;
582     testCallback = Common::MakeShared<MockAuthenticationCallback>();
583     uint64_t widgetAuth = UserAuthClientImpl::Instance().BeginWidgetAuth(apiVersion, authParam,
584     widgetParam, testCallback);
585     EXPECT_EQ(widgetAuth, 0);
586     widgetAuth = UserAuthClientImpl::Instance().BeginWidgetAuth(authParam, widgetParam, testCallback);
587     EXPECT_EQ(widgetAuth, 0);
588 }
589 
590 HWTEST_F(UserAuthClientTest, UserAuthClientBeginWidgetAuth002, TestSize.Level0)
591 {
592     static const int32_t apiVersion = 0;
593     WidgetAuthParam authParam;
594     WidgetParam widgetParam;
595     std::shared_ptr<MockAuthenticationCallback> testCallback = nullptr;
596     uint64_t widgetAuth = UserAuthClientImpl::Instance().BeginWidgetAuth(apiVersion, authParam,
597     widgetParam, testCallback);
598     EXPECT_EQ(widgetAuth, 0);
599 }
600 
601 HWTEST_F(UserAuthClientTest, UserAuthClientBeginWidgetAuth003, TestSize.Level0)
602 {
603     int32_t testVersion = 0;
604     AuthParamInner testParam = {};
605     testParam.challenge = {0};
606     testParam.authTypes = {ALL};
607     WidgetParamInner testWidgetParamInner = {};
608     testWidgetParamInner.title = "title";
609     auto testCallback = Common::MakeShared<MockAuthenticationCallback>();
610     EXPECT_NE(testCallback, nullptr);
611 
612     uint64_t testContextVersion = 1;
613     auto service = Common::MakeShared<MockUserAuthService>();
614     EXPECT_NE(service, nullptr);
615     EXPECT_CALL(*service, AuthWidget(_, _, _, _, _)).WillRepeatedly(Return(true));
616     ON_CALL(*service, AuthWidget)
617         .WillByDefault(
618             [&testVersion, &testParam, &testWidgetParamInner, &testContextVersion](int32_t apiVersion,
619             const AuthParamInner &authParam, const WidgetParamInner &widgetParam,
__anonf8df1b5b0c02(int32_t apiVersion, const AuthParamInner &authParam, const WidgetParamInner &widgetParam, sptr<UserAuthCallbackInterface> &callback, sptr<ModalCallbackInterface> &modalCallback) 620             sptr<UserAuthCallbackInterface> &callback, sptr<ModalCallbackInterface> &modalCallback) {
621                 EXPECT_EQ(apiVersion, testVersion);
622                 EXPECT_EQ(authParam.authTypes, testParam.authTypes);
623                 EXPECT_EQ(widgetParam.title, testWidgetParamInner.title);
624                 if (callback != nullptr) {
625                     Attributes extraInfo;
626                     callback->OnResult(static_cast<int32_t>(ResultCode::GENERAL_ERROR), extraInfo);
627                 }
628                 return testContextVersion;
629             }
630         );
631 
632     sptr<MockRemoteObject> obj = new MockRemoteObject();
633     sptr<IRemoteObject::DeathRecipient> dr(nullptr);
634     CallRemoteObject(service, obj, dr);
635     WidgetAuthParam testAuthParam = {};
636     WidgetParam testWidgetParam = {};
637     testWidgetParam.title = "title";
638     uint64_t widgetAuth = UserAuthClientImpl::Instance().BeginWidgetAuth(testVersion, testAuthParam,
639         testWidgetParam, testCallback);
640     EXPECT_EQ(widgetAuth, testContextVersion);
641     dr->OnRemoteDied(obj);
642     IpcClientUtils::ResetObj();
643 }
644 
645 
646 HWTEST_F(UserAuthClientTest, UserAuthClientSetWidgetCallback001, TestSize.Level0)
647 {
648     static const int32_t apiVersion = 0;
649     auto testCallback = Common::MakeShared<MockIUserAuthWidgetCallback>();
650     int32_t widgetCallback = UserAuthClientImpl::Instance().SetWidgetCallback(apiVersion, testCallback);
651     EXPECT_NE(widgetCallback, SUCCESS);
652 }
653 
654 HWTEST_F(UserAuthClientTest, UserAuthClientSetWidgetCallback002, TestSize.Level0)
655 {
656     static const int32_t apiVersion = 0;
657     std::shared_ptr<IUserAuthWidgetCallback> testCallback = nullptr;
658     int32_t widgetCallback = UserAuthClientImpl::Instance().SetWidgetCallback(apiVersion, testCallback);
659     EXPECT_EQ(widgetCallback, GENERAL_ERROR);
660 }
661 
662 HWTEST_F(UserAuthClientTest, UserAuthClientSetWidgetCallback003, TestSize.Level0)
663 {
664     int32_t testVersion = 0;
665     auto testCallback = Common::MakeShared<MockIUserAuthWidgetCallback>();
666     EXPECT_NE(testCallback, nullptr);
667 
668     uint64_t testContextVersion = 1;
669     auto service = Common::MakeShared<MockUserAuthService>();
670     EXPECT_NE(service, nullptr);
671     EXPECT_CALL(*service, RegisterWidgetCallback(_, _)).WillRepeatedly(Return(true));
672     ON_CALL(*service, RegisterWidgetCallback)
673         .WillByDefault(
__anonf8df1b5b0d02(int32_t version, sptr<WidgetCallbackInterface> &callback) 674             [&testVersion, &testContextVersion](int32_t version, sptr<WidgetCallbackInterface> &callback) {
675                 EXPECT_EQ(version, testVersion);
676                 return testContextVersion;
677             }
678         );
679 
680     sptr<MockRemoteObject> obj = new MockRemoteObject();
681     sptr<IRemoteObject::DeathRecipient> dr(nullptr);
682     CallRemoteObject(service, obj, dr);
683     uint64_t widgetAuth = UserAuthClientImpl::Instance().SetWidgetCallback(testVersion, testCallback);
684     EXPECT_EQ(widgetAuth, testContextVersion);
685     dr->OnRemoteDied(obj);
686     IpcClientUtils::ResetObj();
687 }
688 
689 HWTEST_F(UserAuthClientTest, UserAuthClientNotice001, TestSize.Level0)
690 {
691     int32_t notice = UserAuthClientImpl::Instance().Notice(NoticeType::WIDGET_NOTICE, "notice");
692     EXPECT_NE(notice, SUCCESS);
693 }
694 
695 HWTEST_F(UserAuthClientTest, UserAuthClientNotice002, TestSize.Level0)
696 {
697     int32_t notice = UserAuthClientImpl::Instance().Notice((enum NoticeType)0, "notice");
698     EXPECT_EQ(notice, GENERAL_ERROR);
699 }
700 
CallRemoteObject(const std::shared_ptr<MockUserAuthService> service,const sptr<MockRemoteObject> & obj,sptr<IRemoteObject::DeathRecipient> & dr)701 void UserAuthClientTest::CallRemoteObject(const std::shared_ptr<MockUserAuthService> service,
702     const sptr<MockRemoteObject> &obj, sptr<IRemoteObject::DeathRecipient> &dr)
703 {
704     EXPECT_NE(obj, nullptr);
705     EXPECT_CALL(*obj, IsProxyObject()).WillRepeatedly(Return(true));
706     EXPECT_CALL(*obj, RemoveDeathRecipient(_)).WillRepeatedly(Return(true));
707     EXPECT_CALL(*obj, AddDeathRecipient(_))
708         .WillRepeatedly([&dr](const sptr<IRemoteObject::DeathRecipient> &recipient) {
709             dr = recipient;
710             return true;
711         });
712 
713     IpcClientUtils::SetObj(obj);
714     EXPECT_CALL(*obj, SendRequest(_, _, _, _)).Times(1);
715     ON_CALL(*obj, SendRequest)
716         .WillByDefault([&service](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
717             service->OnRemoteRequest(code, data, reply, option);
718             return OHOS::NO_ERROR;
719         });
720 }
721 
722 HWTEST_F(UserAuthClientTest, UserAuthClientRegistUserAuthSuccessEventListener001, TestSize.Level0)
723 {
724     std::vector<AuthType> authTypeList;
725     authTypeList.push_back(AuthType::PIN);
726     authTypeList.push_back(AuthType::FACE);
727     authTypeList.push_back(AuthType::FINGERPRINT);
728 
729     sptr<AuthEventListenerInterface> testCallback = new MockAuthEventListenerService();
730     EXPECT_NE(testCallback, nullptr);
731 
732     auto service = Common::MakeShared<MockUserAuthService>();
733     EXPECT_NE(service, nullptr);
734     EXPECT_CALL(*service, RegistUserAuthSuccessEventListener(_, _)).Times(1);
735     ON_CALL(*service, RegistUserAuthSuccessEventListener)
736         .WillByDefault(
__anonf8df1b5b1002(const std::vector<AuthType> &authType, const sptr<AuthEventListenerInterface> &callback) 737             [](const std::vector<AuthType> &authType, const sptr<AuthEventListenerInterface> &callback) {
738                 return SUCCESS;
739             }
740         );
741     sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
742     sptr<IRemoteObject::DeathRecipient> dr(nullptr);
743     CallRemoteObject(service, obj, dr);
744     int32_t ret = UserAuthClientImpl::Instance().RegistUserAuthSuccessEventListener(authTypeList, testCallback);
745     EXPECT_EQ(ret, SUCCESS);
746     dr->OnRemoteDied(obj);
747     IpcClientUtils::ResetObj();
748 }
749 
750 HWTEST_F(UserAuthClientTest, UserAuthClientRegistUserAuthSuccessEventListener002, TestSize.Level0)
751 {
752     std::vector<AuthType> authTypeList;
753     authTypeList.push_back(AuthType::PIN);
754     authTypeList.push_back(AuthType::FACE);
755     authTypeList.push_back(AuthType::FINGERPRINT);
756 
757     int32_t ret = UserAuthClientImpl::Instance().RegistUserAuthSuccessEventListener(authTypeList, nullptr);
758     EXPECT_EQ(ret, GENERAL_ERROR);
759 }
760 
761 HWTEST_F(UserAuthClientTest, UserAuthClientRegistUserAuthSuccessEventListener003, TestSize.Level0)
762 {
763     std::vector<AuthType> authTypeList;
764     authTypeList.push_back(AuthType::PIN);
765     authTypeList.push_back(AuthType::FACE);
766     authTypeList.push_back(AuthType::FINGERPRINT);
767 
768     sptr<AuthEventListenerInterface> testCallback = new MockAuthEventListenerService();
769     EXPECT_NE(testCallback, nullptr);
770 
771     int32_t ret = UserAuthClientImpl::Instance().RegistUserAuthSuccessEventListener(authTypeList, testCallback);
772     EXPECT_EQ(ret, GENERAL_ERROR);
773 }
774 
775 HWTEST_F(UserAuthClientTest, UserAuthClientUnRegistUserAuthSuccessEventListener001, TestSize.Level0)
776 {
777     sptr<AuthEventListenerInterface> testCallback = new MockAuthEventListenerService();
778     EXPECT_NE(testCallback, nullptr);
779 
780     auto service = Common::MakeShared<MockUserAuthService>();
781     EXPECT_NE(service, nullptr);
782     EXPECT_CALL(*service, UnRegistUserAuthSuccessEventListener(_)).Times(1);
783     ON_CALL(*service, UnRegistUserAuthSuccessEventListener)
784         .WillByDefault(
__anonf8df1b5b1102(const sptr<AuthEventListenerInterface> &callback) 785             [](const sptr<AuthEventListenerInterface> &callback) {
786                 return SUCCESS;
787             }
788         );
789     sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
790     sptr<IRemoteObject::DeathRecipient> dr(nullptr);
791     CallRemoteObject(service, obj, dr);
792     int32_t ret = UserAuthClientImpl::Instance().UnRegistUserAuthSuccessEventListener(testCallback);
793     EXPECT_EQ(ret, SUCCESS);
794     dr->OnRemoteDied(obj);
795     IpcClientUtils::ResetObj();
796 }
797 
798 HWTEST_F(UserAuthClientTest, UserAuthClientUnRegistUserAuthSuccessEventListener002, TestSize.Level0)
799 {
800     int32_t ret = UserAuthClientImpl::Instance().UnRegistUserAuthSuccessEventListener(nullptr);
801     EXPECT_EQ(ret, GENERAL_ERROR);
802 }
803 
804 HWTEST_F(UserAuthClientTest, UserAuthClientUnRegistUserAuthSuccessEventListener003, TestSize.Level0)
805 {
806     sptr<AuthEventListenerInterface> testCallback = new MockAuthEventListenerService();
807     EXPECT_NE(testCallback, nullptr);
808 
809     int32_t ret = UserAuthClientImpl::Instance().UnRegistUserAuthSuccessEventListener(testCallback);
810     EXPECT_EQ(ret, GENERAL_ERROR);
811 }
812 
813 HWTEST_F(UserAuthClientTest, UserAuthClientSetGlobalConfigParam001, TestSize.Level0)
814 {
815     GlobalConfigParam param = {};
816     int32_t ret = UserAuthClient::GetInstance().SetGlobalConfigParam(param);
817     EXPECT_EQ(ret, GENERAL_ERROR);
818 }
819 
820 HWTEST_F(UserAuthClientTest, UserAuthClientSetGlobalConfigParam002, TestSize.Level0)
821 {
822     GlobalConfigParam param = {};
823     auto service = Common::MakeShared<MockUserAuthService>();
824     EXPECT_NE(service, nullptr);
825     sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
826     sptr<IRemoteObject::DeathRecipient> dr(nullptr);
827     CallRemoteObject(service, obj, dr);
828 
829     EXPECT_EQ(UserAuthClient::GetInstance().SetGlobalConfigParam(param), INVALID_PARAMETERS);
830     param.type = ENABLE_STATUS;
831     EXPECT_EQ(UserAuthClient::GetInstance().SetGlobalConfigParam(param), SUCCESS);
832     EXPECT_NE(dr, nullptr);
833     dr->OnRemoteDied(obj);
834     IpcClientUtils::ResetObj();
835 }
836 
837 HWTEST_F(UserAuthClientTest, UserAuthClientGetNorthAvailableStatus, TestSize.Level0)
838 {
839     AuthType testAuthType = FACE;
840     int32_t testApiVersion = 0;
841     AuthTrustLevel testAtl = ATL1;
842     int32_t ret = UserAuthClientImpl::Instance().GetNorthAvailableStatus(testApiVersion, testAuthType, testAtl);
843     EXPECT_EQ(ret, GENERAL_ERROR);
844 }
845 
846 
847 HWTEST_F(UserAuthClientTest, UserAuthClientGetPropertyById001, TestSize.Level0)
848 {
849     uint64_t testCredentialId = 1;
850     std::vector<Attributes::AttributeKey> testKeys;
851 
852     std::shared_ptr<MockGetPropCallback> testCallback = nullptr;
853     UserAuthClient::GetInstance().GetPropertyById(testCredentialId, testKeys, testCallback);
854 
855     IpcClientUtils::ResetObj();
856     testCallback = Common::MakeShared<MockGetPropCallback>();
857     EXPECT_NE(testCallback, nullptr);
858     EXPECT_CALL(*testCallback, OnResult(_, _)).Times(1);
859     UserAuthClient::GetInstance().GetPropertyById(testCredentialId, testKeys, testCallback);
860 }
861 
862 HWTEST_F(UserAuthClientTest, UserAuthClientGetPropertyById002, TestSize.Level0)
863 {
864     uint64_t testCredentialId = 1;
865     std::vector<Attributes::AttributeKey> testKeys = {Attributes::ATTR_RESULT_CODE, Attributes::ATTR_SIGNATURE};
866 
867     auto testCallback = Common::MakeShared<MockGetPropCallback>();
868     EXPECT_NE(testCallback, nullptr);
869     EXPECT_CALL(*testCallback, OnResult(_, _)).Times(1);
870 
871     auto service = Common::MakeShared<MockUserAuthService>();
872     EXPECT_NE(service, nullptr);
873     EXPECT_CALL(*service, GetPropertyById(_, _, _)).Times(1);
874     ON_CALL(*service, GetPropertyById)
875         .WillByDefault(
876             [&testCredentialId, &testKeys](uint64_t credentialId, const std::vector<Attributes::AttributeKey> &keys,
__anonf8df1b5b1202(uint64_t credentialId, const std::vector<Attributes::AttributeKey> &keys, sptr<GetExecutorPropertyCallbackInterface> &callback) 877             sptr<GetExecutorPropertyCallbackInterface> &callback) {
878                 EXPECT_EQ(credentialId, testCredentialId);
879                 EXPECT_THAT(keys, ElementsAreArray(testKeys));
880                 if (callback != nullptr) {
881                     Attributes extraInfo;
882                     callback->OnGetExecutorPropertyResult(SUCCESS, extraInfo);
883                 }
884             }
885         );
886     sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
887     sptr<IRemoteObject::DeathRecipient> dr(nullptr);
888     CallRemoteObject(service, obj, dr);
889     UserAuthClient::GetInstance().GetPropertyById(testCredentialId, testKeys, testCallback);
890     EXPECT_NE(dr, nullptr);
891     dr->OnRemoteDied(obj);
892     IpcClientUtils::ResetObj();
893 }
894 } // namespace UserAuth
895 } // namespace UserIam
896 } // namespace OHOS