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_proxy_test.h"
17
18 #include "iam_ptr.h"
19 #include "user_auth_proxy.h"
20 #include "mock_remote_object.h"
21 #include "mock_user_auth_service.h"
22 #include "mock_user_auth_client_callback.h"
23 #include "mock_user_auth_callback_service.h"
24 #include "mock_iuser_auth_widget_callback.h"
25 #include "user_auth_callback_service.h"
26 #include "widget_callback_service.h"
27
28 namespace OHOS {
29 namespace UserIam {
30 namespace UserAuth {
31 using namespace testing;
32 using namespace testing::ext;
33
SetUpTestCase()34 void UserAuthProxyTest::SetUpTestCase()
35 {
36 }
37
TearDownTestCase()38 void UserAuthProxyTest::TearDownTestCase()
39 {
40 }
41
SetUp()42 void UserAuthProxyTest::SetUp()
43 {
44 }
45
TearDown()46 void UserAuthProxyTest::TearDown()
47 {
48 }
49
50 HWTEST_F(UserAuthProxyTest, UserAuthProxyGetEnrolledState, TestSize.Level0)
51 {
52 int32_t testApiVersion = 0;
53 AuthType testAuthType = FACE;
54 uint16_t credentialDigest = 23962;
55 uint16_t credentialCount = 1;
56 EnrolledState enrolledState;
57 sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
58 EXPECT_NE(obj, nullptr);
59 auto proxy = Common::MakeShared<UserAuthProxy>(obj);
60 EXPECT_NE(proxy, nullptr);
61 auto service = Common::MakeShared<MockUserAuthService>();
62 EXPECT_NE(service, nullptr);
63 EXPECT_CALL(*service, GetEnrolledState(_, _, _))
64 .Times(Exactly(1))
65 .WillOnce([testApiVersion, testAuthType, credentialDigest, credentialCount](int32_t apiVersion,
__anoneec003360102(int32_t apiVersion, AuthType authType, EnrolledState &enrolledState) 66 AuthType authType, EnrolledState &enrolledState) {
67 EXPECT_EQ(testApiVersion, apiVersion);
68 EXPECT_EQ(testAuthType, authType);
69 enrolledState.credentialDigest = credentialDigest;
70 enrolledState.credentialCount = credentialCount;
71 return SUCCESS;
72 });
73 EXPECT_CALL(*obj, SendRequest(_, _, _, _)).Times(1);
74 ON_CALL(*obj, SendRequest)
__anoneec003360202(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) 75 .WillByDefault([&service](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
76 service->OnRemoteRequest(code, data, reply, option);
77 return SUCCESS;
78 });
79 proxy->GetEnrolledState(testApiVersion, testAuthType, enrolledState);
80 EXPECT_EQ(credentialDigest, enrolledState.credentialDigest);
81 EXPECT_EQ(credentialCount, enrolledState.credentialCount);
82 }
83
84
85 HWTEST_F(UserAuthProxyTest, UserAuthProxyGetAvailableStatus, TestSize.Level0)
86 {
87 static const int32_t testApiVersion = 0;
88 static const AuthType testAuthType = FACE;
89 static const AuthTrustLevel testAuthTrustLevel = ATL3;
90 static const int32_t testUserId = 100;
91 sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
92 EXPECT_NE(obj, nullptr);
93 auto proxy = Common::MakeShared<UserAuthProxy>(obj);
94 EXPECT_NE(proxy, nullptr);
95 auto service = Common::MakeShared<MockUserAuthService>();
96 EXPECT_NE(service, nullptr);
97 EXPECT_CALL(*service, GetAvailableStatus(_, _, _, _))
98 .Times(Exactly(1))
__anoneec003360302(int32_t apiVersion, int32_t userId, AuthType authType, AuthTrustLevel authTrustLevel) 99 .WillOnce([](int32_t apiVersion, int32_t userId, AuthType authType, AuthTrustLevel authTrustLevel) {
100 return SUCCESS;
101 });
102 EXPECT_CALL(*obj, SendRequest(_, _, _, _)).Times(2);
103 ON_CALL(*obj, SendRequest)
__anoneec003360402(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) 104 .WillByDefault([&service](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
105 service->OnRemoteRequest(code, data, reply, option);
106 return SUCCESS;
107 });
108 EXPECT_EQ(proxy->GetAvailableStatus(testApiVersion, testUserId, testAuthType, testAuthTrustLevel), SUCCESS);
109 EXPECT_EQ(proxy->GetAvailableStatus(testApiVersion, testAuthType, testAuthTrustLevel), SUCCESS);
110 }
111
112 HWTEST_F(UserAuthProxyTest, UserAuthProxyGetProperty, TestSize.Level0)
113 {
114 static const int32_t testUserId = 200;
115 static const AuthType testAuthType = FACE;
116 std::vector<Attributes::AttributeKey> testKeys = {Attributes::ATTR_RESULT_CODE, Attributes::ATTR_SIGNATURE,
117 Attributes::ATTR_SCHEDULE_MODE};
118
119 sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
120 EXPECT_NE(obj, nullptr);
121 auto proxy = Common::MakeShared<UserAuthProxy>(obj);
122 EXPECT_NE(proxy, nullptr);
123 auto getPropCallback = Common::MakeShared<MockGetPropCallback>();
124 EXPECT_NE(getPropCallback, nullptr);
125 sptr<GetExecutorPropertyCallbackInterface> testCallback(
126 new (std::nothrow) GetExecutorPropertyCallbackService(getPropCallback));
127 auto service = Common::MakeShared<MockUserAuthService>();
128 EXPECT_NE(service, nullptr);
129 EXPECT_CALL(*service, GetProperty(_, _, _, _))
130 .Times(Exactly(1))
131 .WillOnce([&testCallback](std::optional<int32_t> userId, AuthType authType,
__anoneec003360502(std::optional<int32_t> userId, AuthType authType, const std::vector<Attributes::AttributeKey> &keys, sptr<GetExecutorPropertyCallbackInterface> &callback) 132 const std::vector<Attributes::AttributeKey> &keys, sptr<GetExecutorPropertyCallbackInterface> &callback) {
133 EXPECT_TRUE(userId.has_value());
134 EXPECT_EQ(userId.value(), testUserId);
135 EXPECT_EQ(authType, testAuthType);
136 EXPECT_THAT(keys, ElementsAre(Attributes::ATTR_RESULT_CODE, Attributes::ATTR_SIGNATURE,
137 Attributes::ATTR_SCHEDULE_MODE));
138 EXPECT_EQ(callback, testCallback);
139 });
140 EXPECT_CALL(*obj, SendRequest(_, _, _, _)).Times(1);
141 ON_CALL(*obj, SendRequest)
__anoneec003360602(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) 142 .WillByDefault([&service](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
143 service->OnRemoteRequest(code, data, reply, option);
144 return SUCCESS;
145 });
146 proxy->GetProperty(testUserId, testAuthType, testKeys, testCallback);
147 }
148
149 HWTEST_F(UserAuthProxyTest, UserAuthProxySetProperty, TestSize.Level0)
150 {
151 static const AuthType testAuthType = FACE;
152
153 Attributes testAttr;
154 EXPECT_EQ(testAttr.SetInt32Value(Attributes::ATTR_RESULT_CODE, 1), true);
155
156 sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
157 EXPECT_NE(obj, nullptr);
158 auto proxy = Common::MakeShared<UserAuthProxy>(obj);
159 EXPECT_NE(proxy, nullptr);
160 auto setPropCallback = Common::MakeShared<MockSetPropCallback>();
161 EXPECT_NE(setPropCallback, nullptr);
162 sptr<SetExecutorPropertyCallbackInterface> testCallback(
163 new (std::nothrow) SetExecutorPropertyCallbackService(setPropCallback));
164 auto service = Common::MakeShared<MockUserAuthService>();
165 EXPECT_NE(service, nullptr);
166 EXPECT_CALL(*service, SetProperty(_, _, _, _))
167 .Times(Exactly(1))
168 .WillOnce([&testCallback](int32_t userId, AuthType authType,
__anoneec003360702(int32_t userId, AuthType authType, const Attributes &attributes, sptr<SetExecutorPropertyCallbackInterface> &callback) 169 const Attributes &attributes, sptr<SetExecutorPropertyCallbackInterface> &callback) {
170 EXPECT_EQ(userId, 0);
171 EXPECT_EQ(authType, testAuthType);
172 int32_t resultCode;
173 EXPECT_EQ(attributes.GetInt32Value(Attributes::ATTR_RESULT_CODE, resultCode), true);
174 EXPECT_EQ(resultCode, 1);
175 EXPECT_EQ(callback, testCallback);
176 });
177 EXPECT_CALL(*obj, SendRequest(_, _, _, _)).Times(1);
178 ON_CALL(*obj, SendRequest)
__anoneec003360802(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) 179 .WillByDefault([&service](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
180 service->OnRemoteRequest(code, data, reply, option);
181 return SUCCESS;
182 });
183 proxy->SetProperty(0, testAuthType, testAttr, testCallback);
184 }
185
186 HWTEST_F(UserAuthProxyTest, UserAuthProxyAuth, TestSize.Level0)
187 {
188 static const int32_t testApiVersion = 0;
189 static const AuthType testAuthType = FACE;
190 static const AuthTrustLevel testAtl = ATL1;
191 const std::vector<uint8_t> testChallenge = {1, 2, 3, 4};
192
193 sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
194 EXPECT_NE(obj, nullptr);
195 auto proxy = Common::MakeShared<UserAuthProxy>(obj);
196 EXPECT_NE(proxy, nullptr);
197 auto authCallback = Common::MakeShared<MockAuthenticationCallback>();
198 EXPECT_NE(authCallback, nullptr);
199 sptr<UserAuthCallbackInterface> testCallback(new (std::nothrow) UserAuthCallbackService(authCallback));
200 auto service = Common::MakeShared<MockUserAuthService>();
201 EXPECT_NE(service, nullptr);
202 EXPECT_CALL(*service, Auth(_, _, _, _, _))
203 .Times(Exactly(1))
204 .WillOnce([&testCallback](int32_t apiVersion, const std::vector<uint8_t> &challenge,
__anoneec003360902(int32_t apiVersion, const std::vector<uint8_t> &challenge, AuthType authType, AuthTrustLevel authTrustLevel, sptr<UserAuthCallbackInterface> &callback) 205 AuthType authType, AuthTrustLevel authTrustLevel, sptr<UserAuthCallbackInterface> &callback) {
206 EXPECT_EQ(apiVersion, testApiVersion);
207 EXPECT_THAT(challenge, ElementsAre(1, 2, 3, 4));
208 EXPECT_EQ(authType, testAuthType);
209 EXPECT_EQ(authTrustLevel, testAtl);
210 EXPECT_EQ(callback, testCallback);
211 return 0;
212 });
213 EXPECT_CALL(*obj, SendRequest(_, _, _, _)).Times(1);
214 ON_CALL(*obj, SendRequest)
__anoneec003360a02(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) 215 .WillByDefault([&service](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
216 service->OnRemoteRequest(code, data, reply, option);
217 return SUCCESS;
218 });
219 proxy->Auth(testApiVersion, testChallenge, testAuthType, testAtl, testCallback);
220 }
221
222 HWTEST_F(UserAuthProxyTest, UserAuthProxyAuthUser, TestSize.Level0)
223 {
224 AuthParamInner testAuthParamInner = {
225 .userId = 200,
226 .challenge = {1, 2, 3, 4},
227 .authType = FACE,
228 .authTrustLevel = ATL1,
229 };
230 std::optional<RemoteAuthParam> testRemoteAuthParam = std::nullopt;
231
232 sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
233 EXPECT_NE(obj, nullptr);
234 auto proxy = Common::MakeShared<UserAuthProxy>(obj);
235 EXPECT_NE(proxy, nullptr);
236 auto authCallback = Common::MakeShared<MockAuthenticationCallback>();
237 EXPECT_NE(authCallback, nullptr);
238 sptr<UserAuthCallbackInterface> testCallback(new (std::nothrow) UserAuthCallbackService(authCallback));
239 auto service = Common::MakeShared<MockUserAuthService>();
240 EXPECT_NE(service, nullptr);
241 EXPECT_CALL(*service, AuthUser(_, _, _))
242 .Times(Exactly(1))
243 .WillOnce([&testCallback, &testAuthParamInner](AuthParamInner &authParam,
__anoneec003360b02(AuthParamInner &authParam, std::optional<RemoteAuthParam> &remoteAuthParam, sptr<UserAuthCallbackInterface> &callback) 244 std::optional<RemoteAuthParam> &remoteAuthParam, sptr<UserAuthCallbackInterface> &callback) {
245 EXPECT_EQ(authParam.userId, testAuthParamInner.userId);
246 EXPECT_THAT(authParam.challenge, ElementsAre(1, 2, 3, 4));
247 EXPECT_EQ(authParam.authType, testAuthParamInner.authType);
248 EXPECT_EQ(authParam.authTrustLevel, testAuthParamInner.authTrustLevel);
249 EXPECT_EQ(callback, testCallback);
250 return 0;
251 });
252 EXPECT_CALL(*obj, SendRequest(_, _, _, _)).Times(1);
253 ON_CALL(*obj, SendRequest)
__anoneec003360c02(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) 254 .WillByDefault([&service](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
255 service->OnRemoteRequest(code, data, reply, option);
256 return SUCCESS;
257 });
258 proxy->AuthUser(testAuthParamInner, testRemoteAuthParam, testCallback);
259 }
260
261 HWTEST_F(UserAuthProxyTest, UserAuthProxyCancelAuthOrIdentify, TestSize.Level0)
262 {
263 static const uint64_t testContextId = 200;
264
265 sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
266 EXPECT_NE(obj, nullptr);
267 auto proxy = Common::MakeShared<UserAuthProxy>(obj);
268 EXPECT_NE(proxy, nullptr);
269 auto service = Common::MakeShared<MockUserAuthService>();
270 EXPECT_NE(service, nullptr);
271 EXPECT_CALL(*service, CancelAuthOrIdentify(_))
272 .Times(Exactly(1))
__anoneec003360d02(uint64_t contextId) 273 .WillOnce([](uint64_t contextId) {
274 EXPECT_EQ(contextId, testContextId);
275 return SUCCESS;
276 });
277 EXPECT_CALL(*obj, SendRequest(_, _, _, _)).Times(1);
278 ON_CALL(*obj, SendRequest)
__anoneec003360e02(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) 279 .WillByDefault([&service](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
280 service->OnRemoteRequest(code, data, reply, option);
281 return SUCCESS;
282 });
283 proxy->CancelAuthOrIdentify(testContextId);
284 }
285
286 HWTEST_F(UserAuthProxyTest, UserAuthProxyIdentify, TestSize.Level0)
287 {
288 static const AuthType testAuthType = FACE;
289 const std::vector<uint8_t> testChallenge = {1, 2, 3, 4};
290
291 sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
292 EXPECT_NE(obj, nullptr);
293 auto proxy = Common::MakeShared<UserAuthProxy>(obj);
294 EXPECT_NE(proxy, nullptr);
295 auto identifyCallback = Common::MakeShared<MockIdentificationCallback>();
296 EXPECT_NE(identifyCallback, nullptr);
297 sptr<UserAuthCallbackInterface> testCallback(new (std::nothrow) UserAuthCallbackService(identifyCallback));
298 auto service = Common::MakeShared<MockUserAuthService>();
299 EXPECT_NE(service, nullptr);
300 EXPECT_CALL(*service, Identify(_, _, _))
301 .Times(Exactly(1))
302 .WillOnce([&testCallback](const std::vector<uint8_t> &challenge, AuthType authType,
__anoneec003360f02(const std::vector<uint8_t> &challenge, AuthType authType, sptr<UserAuthCallbackInterface> &callback) 303 sptr<UserAuthCallbackInterface> &callback) {
304 EXPECT_THAT(challenge, ElementsAre(1, 2, 3, 4));
305 EXPECT_EQ(authType, testAuthType);
306 EXPECT_EQ(callback, testCallback);
307 return 0;
308 });
309 EXPECT_CALL(*obj, SendRequest(_, _, _, _)).Times(1);
310 ON_CALL(*obj, SendRequest)
__anoneec003361002(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) 311 .WillByDefault([&service](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
312 service->OnRemoteRequest(code, data, reply, option);
313 return SUCCESS;
314 });
315 proxy->Identify(testChallenge, testAuthType, testCallback);
316 }
317
318 HWTEST_F(UserAuthProxyTest, UserAuthProxyAuthWidget001, TestSize.Level0)
319 {
320 static const int32_t testApiVersion = 0;
321 AuthParamInner authParam;
322 WidgetParam widgetParam;
323
324 sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
325 EXPECT_NE(obj, nullptr);
326 auto proxy = Common::MakeShared<UserAuthProxy>(obj);
327 EXPECT_NE(proxy, nullptr);
328 auto identifyCallback = Common::MakeShared<MockIdentificationCallback>();
329 EXPECT_NE(identifyCallback, nullptr);
330 sptr<UserAuthCallbackInterface> testCallback =
331 new (std::nothrow) UserAuthCallbackService(identifyCallback);
332 auto service = Common::MakeShared<MockUserAuthService>();
333 EXPECT_NE(service, nullptr);
334 EXPECT_CALL(*service, AuthWidget(_, _, _, _))
335 .Times(Exactly(1))
336 .WillOnce([&testCallback](int32_t apiVersion, const AuthParamInner &authParam, const WidgetParam &widgetParam,
__anoneec003361102(int32_t apiVersion, const AuthParamInner &authParam, const WidgetParam &widgetParam, sptr<UserAuthCallbackInterface> &callback) 337 sptr<UserAuthCallbackInterface> &callback) {
338 EXPECT_EQ(apiVersion, testApiVersion);
339 EXPECT_EQ(callback, testCallback);
340 return 0;
341 });
342 EXPECT_CALL(*obj, SendRequest(_, _, _, _)).Times(1);
343 ON_CALL(*obj, SendRequest)
__anoneec003361202(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) 344 .WillByDefault([&service](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
345 service->OnRemoteRequest(code, data, reply, option);
346 return SUCCESS;
347 });
348 proxy->AuthWidget(testApiVersion, authParam, widgetParam, testCallback);
349 }
350
351 HWTEST_F(UserAuthProxyTest, UserAuthProxyAuthWidget002, TestSize.Level0)
352 {
353 static const int32_t testApiVersion = 0;
354 AuthParamInner authParam;
355 WidgetParam widgetParam;
356
357 sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
358 EXPECT_NE(obj, nullptr);
359 auto proxy = Common::MakeShared<UserAuthProxy>(obj);
360 EXPECT_NE(proxy, nullptr);
361 sptr<UserAuthCallbackInterface> testCallback(nullptr);
362 proxy->AuthWidget(testApiVersion, authParam, widgetParam, testCallback);
363 }
364
365 HWTEST_F(UserAuthProxyTest, UserAuthProxyNotice001, TestSize.Level0)
366 {
367 static const NoticeType testNoticeType = NoticeType::WIDGET_NOTICE;
368 static const std::string testEventData = "notice";
369
370 sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
371 EXPECT_NE(obj, nullptr);
372 auto proxy = Common::MakeShared<UserAuthProxy>(obj);
373 EXPECT_NE(proxy, nullptr);
374
375 auto service = Common::MakeShared<MockUserAuthService>();
376 EXPECT_NE(service, nullptr);
377 EXPECT_CALL(*service, Notice(_, _))
378 .Times(Exactly(1))
__anoneec003361302(NoticeType noticeType, const std::string &eventData) 379 .WillOnce([](NoticeType noticeType, const std::string &eventData) {
380 EXPECT_EQ(noticeType, testNoticeType);
381 EXPECT_EQ(eventData, testEventData);
382 return 0;
383 });
384 EXPECT_CALL(*obj, SendRequest(_, _, _, _)).Times(1);
385 ON_CALL(*obj, SendRequest)
__anoneec003361402(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) 386 .WillByDefault([&service](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
387 service->OnRemoteRequest(code, data, reply, option);
388 return SUCCESS;
389 });
390 proxy->Notice(testNoticeType, testEventData);
391 }
392
393 HWTEST_F(UserAuthProxyTest, UserAuthProxyRegisterWidgetCallback001, TestSize.Level0)
394 {
395 static const int32_t testVersion = 0;
396
397 sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
398 EXPECT_NE(obj, nullptr);
399 auto proxy = Common::MakeShared<UserAuthProxy>(obj);
400 EXPECT_NE(proxy, nullptr);
401
402 auto identifyCallback = Common::MakeShared<MockIUserAuthWidgetCallback>();
403 EXPECT_NE(identifyCallback, nullptr);
404 sptr<WidgetCallbackInterface> testCallback =
405 new (std::nothrow) WidgetCallbackService(identifyCallback);
406 auto service = Common::MakeShared<MockUserAuthService>();
407 EXPECT_NE(service, nullptr);
408 EXPECT_CALL(*service, RegisterWidgetCallback(_, _))
409 .Times(Exactly(1))
__anoneec003361502(int32_t version, sptr<WidgetCallbackInterface> &callback) 410 .WillOnce([&testCallback](int32_t version, sptr<WidgetCallbackInterface> &callback) {
411 EXPECT_EQ(version, testVersion);
412 EXPECT_EQ(callback, testCallback);
413 return 0;
414 });
415 EXPECT_CALL(*obj, SendRequest(_, _, _, _)).Times(1);
416 ON_CALL(*obj, SendRequest)
__anoneec003361602(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) 417 .WillByDefault([&service](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
418 service->OnRemoteRequest(code, data, reply, option);
419 return SUCCESS;
420 });
421 proxy->RegisterWidgetCallback(testVersion, testCallback);
422 }
423
424 HWTEST_F(UserAuthProxyTest, UserAuthProxyRegisterWidgetCallback002, TestSize.Level0)
425 {
426 static const int32_t testVersion = 0;
427
428 sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
429 EXPECT_NE(obj, nullptr);
430 auto proxy = Common::MakeShared<UserAuthProxy>(obj);
431 EXPECT_NE(proxy, nullptr);
432 sptr<WidgetCallbackInterface> callback(nullptr);
433 EXPECT_EQ(proxy->RegisterWidgetCallback(testVersion, callback), GENERAL_ERROR);
434 }
435
436 HWTEST_F(UserAuthProxyTest, UserAuthProxyRegistUserAuthSuccessEventListener001, TestSize.Level0)
437 {
438 sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
439 EXPECT_NE(obj, nullptr);
440 auto proxy = Common::MakeShared<UserAuthProxy>(obj);
441 EXPECT_NE(proxy, nullptr);
442
443 sptr<AuthEventListenerInterface> testCallback = new (std::nothrow) MockAuthEventListenerService();
444 auto service = Common::MakeShared<MockUserAuthService>();
445 EXPECT_NE(service, nullptr);
446 EXPECT_CALL(*service, RegistUserAuthSuccessEventListener(_, _))
447 .Times(Exactly(1))
__anoneec003361702(const std::vector<AuthType> &authType, const sptr<AuthEventListenerInterface> &callback) 448 .WillOnce([](const std::vector<AuthType> &authType, const sptr<AuthEventListenerInterface> &callback) {
449 return SUCCESS;
450 });
451 EXPECT_CALL(*obj, SendRequest(_, _, _, _)).Times(1);
452 ON_CALL(*obj, SendRequest)
__anoneec003361802(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) 453 .WillByDefault([&service](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
454 service->OnRemoteRequest(code, data, reply, option);
455 return SUCCESS;
456 });
457 std::vector<AuthType> authTypeList;
458 authTypeList.push_back(AuthType::PIN);
459 authTypeList.push_back(AuthType::FACE);
460 authTypeList.push_back(AuthType::FINGERPRINT);
461 proxy->RegistUserAuthSuccessEventListener(authTypeList, testCallback);
462 }
463
464 HWTEST_F(UserAuthProxyTest, UserAuthProxyUnRegistUserAuthSuccessEventListener001, TestSize.Level0)
465 {
466 sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
467 EXPECT_NE(obj, nullptr);
468 auto proxy = Common::MakeShared<UserAuthProxy>(obj);
469 EXPECT_NE(proxy, nullptr);
470
471 sptr<AuthEventListenerInterface> testCallback = new (std::nothrow) MockAuthEventListenerService();
472 auto service = Common::MakeShared<MockUserAuthService>();
473 EXPECT_NE(service, nullptr);
474 EXPECT_CALL(*service, UnRegistUserAuthSuccessEventListener(_))
475 .Times(Exactly(1))
__anoneec003361902(const sptr<AuthEventListenerInterface> &callback) 476 .WillOnce([](const sptr<AuthEventListenerInterface> &callback) {
477 return SUCCESS;
478 });
479 EXPECT_CALL(*obj, SendRequest(_, _, _, _)).Times(1);
480 ON_CALL(*obj, SendRequest)
__anoneec003361a02(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) 481 .WillByDefault([&service](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
482 service->OnRemoteRequest(code, data, reply, option);
483 return SUCCESS;
484 });
485 proxy->UnRegistUserAuthSuccessEventListener(testCallback);
486 }
487
488 HWTEST_F(UserAuthProxyTest, UserAuthProxySetGlobalConfigParam001, TestSize.Level0)
489 {
490 sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
491 EXPECT_NE(obj, nullptr);
492 auto proxy = Common::MakeShared<UserAuthProxy>(obj);
493 EXPECT_NE(proxy, nullptr);
494
495 auto service = Common::MakeShared<MockUserAuthService>();
496 EXPECT_NE(service, nullptr);
497 EXPECT_CALL(*service, SetGlobalConfigParam(_))
498 .Times(Exactly(1))
__anoneec003361b02(const GlobalConfigParam ¶m) 499 .WillOnce([](const GlobalConfigParam ¶m) {
500 return SUCCESS;
501 });
502 EXPECT_CALL(*obj, SendRequest(_, _, _, _)).Times(1);
503 ON_CALL(*obj, SendRequest)
__anoneec003361c02(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) 504 .WillByDefault([&service](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
505 service->OnRemoteRequest(code, data, reply, option);
506 return SUCCESS;
507 });
508 GlobalConfigParam param = {};
509 proxy->SetGlobalConfigParam(param);
510 }
511
512
513 HWTEST_F(UserAuthProxyTest, UserAuthProxyGetPropertyById, TestSize.Level0)
514 {
515 static const uint64_t testCredentialId = 1;
516 std::vector<Attributes::AttributeKey> testKeys = {Attributes::ATTR_RESULT_CODE, Attributes::ATTR_SIGNATURE,
517 Attributes::ATTR_SCHEDULE_MODE};
518
519 sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
520 EXPECT_NE(obj, nullptr);
521 auto proxy = Common::MakeShared<UserAuthProxy>(obj);
522 EXPECT_NE(proxy, nullptr);
523 auto getPropCallback = Common::MakeShared<MockGetPropCallback>();
524 EXPECT_NE(getPropCallback, nullptr);
525 sptr<GetExecutorPropertyCallbackInterface> testCallback(
526 new (std::nothrow) GetExecutorPropertyCallbackService(getPropCallback));
527 auto service = Common::MakeShared<MockUserAuthService>();
528 EXPECT_NE(service, nullptr);
529 EXPECT_CALL(*service, GetPropertyById(_, _, _))
530 .Times(Exactly(1))
531 .WillOnce([&testCallback](uint64_t credentialId,
__anoneec003361d02(uint64_t credentialId, const std::vector<Attributes::AttributeKey> &keys, sptr<GetExecutorPropertyCallbackInterface> &callback) 532 const std::vector<Attributes::AttributeKey> &keys, sptr<GetExecutorPropertyCallbackInterface> &callback) {
533 EXPECT_EQ(credentialId, testCredentialId);
534 EXPECT_THAT(keys, ElementsAre(Attributes::ATTR_RESULT_CODE, Attributes::ATTR_SIGNATURE,
535 Attributes::ATTR_SCHEDULE_MODE));
536 EXPECT_EQ(callback, testCallback);
537 });
538 EXPECT_CALL(*obj, SendRequest(_, _, _, _)).Times(1);
539 ON_CALL(*obj, SendRequest)
__anoneec003361e02(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) 540 .WillByDefault([&service](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
541 service->OnRemoteRequest(code, data, reply, option);
542 return SUCCESS;
543 });
544 proxy->GetPropertyById(testCredentialId, testKeys, testCallback);
545 }
546 } // namespace UserAuth
547 } // namespace UserIam
548 } // namespace OHOS