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