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_stub_test.h"
17 
18 #include "iam_common_defines.h"
19 #include "mock_auth_event_listener.h"
20 #include "mock_user_auth_callback.h"
21 #include "mock_user_auth_service.h"
22 
23 namespace OHOS {
24 namespace UserIam {
25 namespace UserAuth {
26 using namespace testing;
27 using namespace testing::ext;
28 
SetUpTestCase()29 void UserAuthStubTest::SetUpTestCase()
30 {
31 }
32 
TearDownTestCase()33 void UserAuthStubTest::TearDownTestCase()
34 {
35 }
36 
SetUp()37 void UserAuthStubTest::SetUp()
38 {
39 }
40 
TearDown()41 void UserAuthStubTest::TearDown()
42 {
43 }
44 
45 HWTEST_F(UserAuthStubTest, UserAuthStubGetEnrolledStateStub001, TestSize.Level0)
46 {
47     MessageParcel data;
48     MessageParcel reply;
49     MessageOption option(MessageOption::TF_SYNC);
50     uint32_t code = UserAuthInterfaceCode::USER_AUTH_GET_ENROLLED_STATE;
51 
52     EXPECT_TRUE(data.WriteInterfaceToken(UserAuthInterface::GetDescriptor()));
53 
54     MockUserAuthService service;
55     EXPECT_EQ(READ_PARCEL_ERROR, service.OnRemoteRequest(code, data, reply, option));
56 }
57 
58 HWTEST_F(UserAuthStubTest, UserAuthStubGetEnrolledStateStub002, TestSize.Level0)
59 {
60     MessageParcel data;
61     MessageParcel reply;
62     MockUserAuthService service;
63     int32_t testApiVersion = 12;
64     AuthType testAuthType = FACE;
65     uint64_t expectCredentialDigest = 23962;
66     uint16_t expectCredentialCount = 1;
67     EXPECT_CALL(service, GetEnrolledState(_, _, _)).Times(1);
68     ON_CALL(service, GetEnrolledState)
69         .WillByDefault(
70             [testApiVersion, testAuthType, expectCredentialDigest, expectCredentialCount](int32_t apiVersion,
__anon5dedf9810102(int32_t apiVersion, AuthType authType, EnrolledState &enrolledState) 71                 AuthType authType, EnrolledState &enrolledState) {
72                 EXPECT_EQ(apiVersion, testApiVersion);
73                 EXPECT_EQ(authType, testAuthType);
74                 enrolledState.credentialDigest = expectCredentialDigest;
75                 enrolledState.credentialCount = expectCredentialCount;
76                 return SUCCESS;
77             }
78         );
79 
80     MessageOption option(MessageOption::TF_SYNC);
81     uint32_t code = static_cast<uint32_t>(UserAuthInterfaceCode::USER_AUTH_GET_ENROLLED_STATE);
82 
83     EXPECT_TRUE(data.WriteInterfaceToken(UserAuthInterface::GetDescriptor()));
84     EXPECT_TRUE(data.WriteInt32(testApiVersion));
85     EXPECT_TRUE(data.WriteUint32(testAuthType));
86 
87     EXPECT_EQ(SUCCESS, service.OnRemoteRequest(code, data, reply, option));
88     int32_t result = FAIL;
89     EXPECT_TRUE(reply.ReadInt32(result));
90     EXPECT_EQ(SUCCESS, result);
91     uint64_t actualCredentialDigest;
92     EXPECT_TRUE(reply.ReadUint64(actualCredentialDigest));
93     EXPECT_EQ(expectCredentialDigest, actualCredentialDigest);
94     uint16_t actualCredentialCount;
95     EXPECT_TRUE(reply.ReadUint16(actualCredentialCount));
96     EXPECT_EQ(expectCredentialCount, actualCredentialCount);
97 }
98 
99 HWTEST_F(UserAuthStubTest, UserAuthStubGetAvailableStatusStub001, TestSize.Level0)
100 {
101     MessageParcel data;
102     MessageParcel reply;
103     MessageOption option(MessageOption::TF_SYNC);
104     uint32_t code = UserAuthInterfaceCode::USER_AUTH_GET_AVAILABLE_STATUS;
105 
106     EXPECT_TRUE(data.WriteInterfaceToken(UserAuthInterface::GetDescriptor()));
107 
108     MockUserAuthService service;
109     EXPECT_EQ(READ_PARCEL_ERROR, service.OnRemoteRequest(code, data, reply, option));
110 }
111 
112 HWTEST_F(UserAuthStubTest, UserAuthStubGetAvailableStatusStub002, TestSize.Level0)
113 {
114     MockUserAuthService service;
115     AuthType testAuthType = FACE;
116     AuthTrustLevel testAuthTrustLevel = ATL3;
117     int32_t testApiVersion = 8;
118     int32_t testUserId = 100;
__anon5dedf9810202() 119     EXPECT_CALL(service, GetAvailableStatus(_, _, _, _)).WillRepeatedly([]() {
120         return SUCCESS;
121     });
122 
123     MessageParcel data;
124     MessageParcel reply;
125     MessageOption option(MessageOption::TF_SYNC);
126     uint32_t code = static_cast<uint32_t>(UserAuthInterfaceCode::USER_AUTH_GET_AVAILABLE_STATUS);
127 
128     EXPECT_TRUE(data.WriteInterfaceToken(UserAuthInterface::GetDescriptor()));
129     EXPECT_TRUE(data.WriteBool(true));
130     EXPECT_TRUE(data.WriteInt32(testUserId));
131     EXPECT_TRUE(data.WriteInt32(testAuthType));
132     EXPECT_TRUE(data.WriteUint32(testAuthTrustLevel));
133     EXPECT_TRUE(data.WriteInt32(testApiVersion));
134 
135     EXPECT_EQ(SUCCESS, service.OnRemoteRequest(code, data, reply, option));
136     int32_t result = FAIL;
137     EXPECT_TRUE(reply.ReadInt32(result));
138     EXPECT_EQ(result, SUCCESS);
139 }
140 
141 HWTEST_F(UserAuthStubTest, UserAuthStubGetPropertyStub001, TestSize.Level0)
142 {
143     MessageParcel data;
144     MessageParcel reply;
145     MessageOption option(MessageOption::TF_SYNC);
146     uint32_t code = UserAuthInterfaceCode::USER_AUTH_GET_PROPERTY;
147 
148     EXPECT_TRUE(data.WriteInterfaceToken(UserAuthInterface::GetDescriptor()));
149 
150     MockUserAuthService service;
151     EXPECT_EQ(READ_PARCEL_ERROR, service.OnRemoteRequest(code, data, reply, option));
152 }
153 
154 HWTEST_F(UserAuthStubTest, UserAuthStubGetPropertyStub002, TestSize.Level0)
155 {
156     int32_t testUserId = 1232666;
157     AuthType testAuthType = FACE;
158     std::vector<Attributes::AttributeKey> testAttrKeys = {Attributes::ATTR_RESULT_CODE, Attributes::ATTR_SIGNATURE,
159         Attributes::ATTR_SCHEDULE_MODE};
160     std::vector<uint32_t> tempKeys;
161     for (auto &attrKey : testAttrKeys) {
162         tempKeys.push_back(static_cast<uint32_t>(attrKey));
163     }
164     sptr<MockGetExecutorPropertyCallback> callback(new (std::nothrow) MockGetExecutorPropertyCallback());
165     EXPECT_NE(callback, nullptr);
166     MockUserAuthService service;
167     EXPECT_CALL(service, GetProperty(_, _, _, _)).Times(1);
168     ON_CALL(service, GetProperty)
169         .WillByDefault(
170             [&testUserId, &testAuthType, &testAttrKeys](int32_t userId, AuthType authType,
171                 const std::vector<Attributes::AttributeKey> &keys,
__anon5dedf9810302(int32_t userId, AuthType authType, const std::vector<Attributes::AttributeKey> &keys, sptr<GetExecutorPropertyCallbackInterface> &callback) 172                 sptr<GetExecutorPropertyCallbackInterface> &callback) {
173                 EXPECT_EQ(userId, testUserId);
174                 EXPECT_EQ(authType, testAuthType);
175                 EXPECT_THAT(keys, ElementsAreArray(testAttrKeys));
176                 if (callback != nullptr) {
177                     Attributes attr;
178                     callback->OnGetExecutorPropertyResult(SUCCESS, attr);
179                 }
180             }
181         );
182     EXPECT_CALL(*callback, OnGetExecutorPropertyResult(_, _)).Times(1);
183 
184     MessageParcel data;
185     MessageParcel reply;
186     MessageOption option(MessageOption::TF_SYNC);
187     uint32_t code = UserAuthInterfaceCode::USER_AUTH_GET_PROPERTY;
188 
189     EXPECT_TRUE(data.WriteInterfaceToken(UserAuthInterface::GetDescriptor()));
190     EXPECT_TRUE(data.WriteInt32(testUserId));
191     EXPECT_TRUE(data.WriteInt32(testAuthType));
192     EXPECT_TRUE(data.WriteUInt32Vector(tempKeys));
193     EXPECT_NE(callback->AsObject(), nullptr);
194     EXPECT_TRUE(data.WriteRemoteObject(callback->AsObject()));
195 
196     EXPECT_EQ(SUCCESS, service.OnRemoteRequest(code, data, reply, option));
197 }
198 
199 HWTEST_F(UserAuthStubTest, UserAuthStubSetPropertyStub001, TestSize.Level0)
200 {
201     MessageParcel data;
202     MessageParcel reply;
203     MessageOption option(MessageOption::TF_SYNC);
204     uint32_t code = UserAuthInterfaceCode::USER_AUTH_SET_PROPERTY;
205 
206     EXPECT_TRUE(data.WriteInterfaceToken(UserAuthInterface::GetDescriptor()));
207 
208     MockUserAuthService service;
209     EXPECT_EQ(READ_PARCEL_ERROR, service.OnRemoteRequest(code, data, reply, option));
210 }
211 
212 HWTEST_F(UserAuthStubTest, UserAuthStubSetPropertyStub002, TestSize.Level0)
213 {
214     int32_t testUserId = 132282;
215     AuthType testAuthType = FACE;
216     Attributes attributes;
217 
218     uint64_t testTemplateId = 3364734;
219     EXPECT_TRUE(attributes.SetUint64Value(Attributes::ATTR_TEMPLATE_ID, testTemplateId));
220 
221     sptr<MockSetExecutorPropertyCallback> callback(new (std::nothrow) MockSetExecutorPropertyCallback());
222     EXPECT_NE(callback, nullptr);
223     MockUserAuthService service;
224     EXPECT_CALL(service, SetProperty(_, _, _, _)).Times(1);
225     ON_CALL(service, SetProperty)
226         .WillByDefault(
227             [&testUserId, &testAuthType, &testTemplateId](int32_t userId, AuthType authType,
__anon5dedf9810402(int32_t userId, AuthType authType, const Attributes &attributes, sptr<SetExecutorPropertyCallbackInterface> &callback) 228                 const Attributes &attributes, sptr<SetExecutorPropertyCallbackInterface> &callback) {
229                 EXPECT_EQ(userId, testUserId);
230                 EXPECT_EQ(authType, testAuthType);
231                 uint64_t tempTemplateId = 0;
232                 EXPECT_TRUE(attributes.GetUint64Value(Attributes::ATTR_TEMPLATE_ID, tempTemplateId));
233                 EXPECT_EQ(tempTemplateId, testTemplateId);
234                 if (callback != nullptr) {
235                     callback->OnSetExecutorPropertyResult(SUCCESS);
236                 }
237             }
238         );
239     EXPECT_CALL(*callback, OnSetExecutorPropertyResult(_)).Times(1);
240 
241     MessageParcel data;
242     MessageParcel reply;
243     MessageOption option(MessageOption::TF_SYNC);
244     uint32_t code = UserAuthInterfaceCode::USER_AUTH_SET_PROPERTY;
245 
246     EXPECT_TRUE(data.WriteInterfaceToken(UserAuthInterface::GetDescriptor()));
247     EXPECT_TRUE(data.WriteInt32(testUserId));
248     EXPECT_TRUE(data.WriteInt32(testAuthType));
249     EXPECT_TRUE(data.WriteUInt8Vector(attributes.Serialize()));
250     EXPECT_NE(callback->AsObject(), nullptr);
251     EXPECT_TRUE(data.WriteRemoteObject(callback->AsObject()));
252 
253     EXPECT_EQ(SUCCESS, service.OnRemoteRequest(code, data, reply, option));
254 }
255 
256 HWTEST_F(UserAuthStubTest, UserAuthStubAuthStub001, TestSize.Level0)
257 {
258     MessageParcel data;
259     MessageParcel reply;
260     MessageOption option(MessageOption::TF_SYNC);
261     uint32_t code = UserAuthInterfaceCode::USER_AUTH_AUTH;
262 
263     EXPECT_TRUE(data.WriteInterfaceToken(UserAuthInterface::GetDescriptor()));
264 
265     MockUserAuthService service;
266     EXPECT_EQ(READ_PARCEL_ERROR, service.OnRemoteRequest(code, data, reply, option));
267 }
268 
269 HWTEST_F(UserAuthStubTest, UserAuthStubAuthStub002, TestSize.Level0)
270 {
271     int32_t testUserId = 3467;
272     int32_t testApiVersion = 9;
273     std::vector<uint8_t> testChallenge = {1, 2, 4, 5};
274     AuthType testAuthType = FACE;
275     AuthTrustLevel testAtl = ATL2;
276     uint64_t testContextId = 2346782;
277 
278     sptr<MockUserAuthCallback> callback(new (std::nothrow) MockUserAuthCallback());
279     EXPECT_NE(callback, nullptr);
280     MockUserAuthService service;
281     EXPECT_CALL(service, Auth(_, _, _, _, _)).Times(1);
282     ON_CALL(service, Auth)
283         .WillByDefault(
284             [&testChallenge, &testAuthType, &testAtl, &testContextId, &testApiVersion](int32_t apiVersion,
285                 const std::vector<uint8_t> &challenge, AuthType authType, AuthTrustLevel authTrustLevel,
__anon5dedf9810502(int32_t apiVersion, const std::vector<uint8_t> &challenge, AuthType authType, AuthTrustLevel authTrustLevel, sptr<UserAuthCallbackInterface> &callback) 286                 sptr<UserAuthCallbackInterface> &callback) {
287                 EXPECT_EQ(apiVersion, testApiVersion);
288                 EXPECT_THAT(challenge, ElementsAreArray(testChallenge));
289                 EXPECT_EQ(authType, testAuthType);
290                 EXPECT_EQ(authTrustLevel, testAtl);
291                 if (callback != nullptr) {
292                     Attributes attr;
293                     callback->OnResult(SUCCESS, attr);
294                 }
295                 return testContextId;
296             }
297         );
298     EXPECT_CALL(*callback, OnResult(_, _)).Times(1);
299 
300     MessageParcel data;
301     MessageParcel reply;
302     MessageOption option(MessageOption::TF_SYNC);
303     uint32_t code = UserAuthInterfaceCode::USER_AUTH_AUTH;
304     std::vector<int32_t> testAuthTypeInts;
305     testAuthTypeInts.push_back(static_cast<AuthType>(1));
306     uint32_t authIntent = 0;
307     EXPECT_TRUE(data.WriteInterfaceToken(UserAuthInterface::GetDescriptor()));
308     EXPECT_TRUE(data.WriteInt32(testApiVersion));
309     EXPECT_TRUE(data.WriteInt32(testUserId));
310     EXPECT_TRUE(data.WriteUInt8Vector(testChallenge));
311     EXPECT_TRUE(data.WriteInt32(testAuthType));
312     EXPECT_TRUE(data.WriteInt32Vector(testAuthTypeInts));
313     EXPECT_TRUE(data.WriteUint32(testAtl));
314     EXPECT_TRUE(data.WriteUint32(authIntent));
315     EXPECT_NE(callback->AsObject(), nullptr);
316     EXPECT_TRUE(data.WriteRemoteObject(callback->AsObject()));
317 
318     EXPECT_EQ(SUCCESS, service.OnRemoteRequest(code, data, reply, option));
319     uint64_t contextId = 0;
320     EXPECT_TRUE(reply.ReadUint64(contextId));
321     EXPECT_EQ(contextId, testContextId);
322 }
323 
324 HWTEST_F(UserAuthStubTest, UserAuthStubAuthUserStub001, TestSize.Level0)
325 {
326     MessageParcel data;
327     MessageParcel reply;
328     MessageOption option(MessageOption::TF_SYNC);
329     uint32_t code = UserAuthInterfaceCode::USER_AUTH_AUTH_USER;
330 
331     EXPECT_TRUE(data.WriteInterfaceToken(UserAuthInterface::GetDescriptor()));
332 
333     MockUserAuthService service;
334     EXPECT_EQ(READ_PARCEL_ERROR, service.OnRemoteRequest(code, data, reply, option));
335 }
336 
337 HWTEST_F(UserAuthStubTest, UserAuthStubAuthUserStub002, TestSize.Level0)
338 {
339     int32_t testUserId = 3467;
340     std::vector<uint8_t> testChallenge = {1, 2, 5, 9};
341     AuthType testAuthType = FACE;
342     AuthTrustLevel testAtl = ATL2;
343     uint64_t testContextId = 2346728;
344 
345     sptr<MockUserAuthCallback> callback(new (std::nothrow) MockUserAuthCallback());
346     EXPECT_NE(callback, nullptr);
347     MockUserAuthService service;
348     EXPECT_CALL(service, AuthUser(_, _, _)).Times(1);
349     ON_CALL(service, AuthUser)
350         .WillByDefault(
351             [&testUserId, &testChallenge, &testAuthType, &testAtl, &testContextId](AuthParamInner &authParam,
__anon5dedf9810602(AuthParamInner &authParam, std::optional<RemoteAuthParam> &remoteAuthParam, sptr<UserAuthCallbackInterface> &callback) 352             std::optional<RemoteAuthParam> &remoteAuthParam, sptr<UserAuthCallbackInterface> &callback) {
353                 EXPECT_EQ(authParam.userId, testUserId);
354                 EXPECT_THAT(authParam.challenge, ElementsAreArray(testChallenge));
355                 EXPECT_EQ(authParam.authType, testAuthType);
356                 EXPECT_EQ(authParam.authTrustLevel, testAtl);
357                 if (callback != nullptr) {
358                     Attributes attr;
359                     callback->OnResult(SUCCESS, attr);
360                 }
361                 return testContextId;
362             }
363         );
364     EXPECT_CALL(*callback, OnResult(_, _)).Times(1);
365 
366     MessageParcel data;
367     MessageParcel reply;
368     MessageOption option(MessageOption::TF_SYNC);
369     uint32_t code = UserAuthInterfaceCode::USER_AUTH_AUTH_USER;
370     uint32_t testAuthTrustLevel = 0;
371     std::vector<int32_t> testAuthTypeInts;
372     testAuthTypeInts.push_back(static_cast<AuthType>(1));
373     uint32_t authIntent = 0;
374     EXPECT_TRUE(data.WriteInterfaceToken(UserAuthInterface::GetDescriptor()));
375     EXPECT_TRUE(data.WriteInt32(testUserId));
376     EXPECT_TRUE(data.WriteUInt8Vector(testChallenge));
377     EXPECT_TRUE(data.WriteInt32(testAuthType));
378     EXPECT_TRUE(data.WriteInt32Vector(testAuthTypeInts));
379     EXPECT_TRUE(data.WriteUint32(testAtl));
380     EXPECT_TRUE(data.WriteUint32(testAuthTrustLevel));
381     EXPECT_TRUE(data.WriteUint32(authIntent));
382     EXPECT_NE(callback->AsObject(), nullptr);
383     EXPECT_TRUE(data.WriteRemoteObject(callback->AsObject()));
384 
385     EXPECT_EQ(SUCCESS, service.OnRemoteRequest(code, data, reply, option));
386     uint64_t contextId = 0;
387     EXPECT_TRUE(reply.ReadUint64(contextId));
388     EXPECT_EQ(contextId, testContextId);
389 }
390 
391 HWTEST_F(UserAuthStubTest, UserAuthStubIdentifyStub001, TestSize.Level0)
392 {
393     MessageParcel data;
394     MessageParcel reply;
395     MessageOption option(MessageOption::TF_SYNC);
396     uint32_t code = UserAuthInterfaceCode::USER_AUTH_IDENTIFY;
397 
398     EXPECT_TRUE(data.WriteInterfaceToken(UserAuthInterface::GetDescriptor()));
399 
400     MockUserAuthService service;
401     EXPECT_EQ(READ_PARCEL_ERROR, service.OnRemoteRequest(code, data, reply, option));
402 }
403 
404 HWTEST_F(UserAuthStubTest, UserAuthStubIdentifyStub002, TestSize.Level0)
405 {
406     std::vector<uint8_t> testChallenge = {1, 2, 5, 8, 9};
407     AuthType testAuthType = FACE;
408     uint64_t testContextId = 76374284;
409 
410     sptr<MockUserAuthCallback> callback(new (std::nothrow) MockUserAuthCallback());
411     EXPECT_NE(callback, nullptr);
412     MockUserAuthService service;
413     EXPECT_CALL(service, Identify(_, _, _)).Times(1);
414     ON_CALL(service, Identify)
415         .WillByDefault(
416             [&testChallenge, &testAuthType, &testContextId](const std::vector<uint8_t> &challenge, AuthType authType,
__anon5dedf9810702(const std::vector<uint8_t> &challenge, AuthType authType, sptr<UserAuthCallbackInterface> &callback) 417                 sptr<UserAuthCallbackInterface> &callback) {
418                 EXPECT_THAT(challenge, ElementsAreArray(testChallenge));
419                 EXPECT_EQ(authType, testAuthType);
420                 if (callback != nullptr) {
421                     Attributes attr;
422                     callback->OnResult(SUCCESS, attr);
423                 }
424                 return testContextId;
425             }
426         );
427     EXPECT_CALL(*callback, OnResult(_, _)).Times(1);
428 
429     MessageParcel data;
430     MessageParcel reply;
431     MessageOption option(MessageOption::TF_SYNC);
432     uint32_t code = UserAuthInterfaceCode::USER_AUTH_IDENTIFY;
433 
434     EXPECT_TRUE(data.WriteInterfaceToken(UserAuthInterface::GetDescriptor()));
435     EXPECT_TRUE(data.WriteUInt8Vector(testChallenge));
436     EXPECT_TRUE(data.WriteInt32(testAuthType));
437     EXPECT_NE(callback->AsObject(), nullptr);
438     EXPECT_TRUE(data.WriteRemoteObject(callback->AsObject()));
439 
440     EXPECT_EQ(SUCCESS, service.OnRemoteRequest(code, data, reply, option));
441     uint64_t contextId = 0;
442     EXPECT_TRUE(reply.ReadUint64(contextId));
443     EXPECT_EQ(contextId, testContextId);
444 }
445 
446 HWTEST_F(UserAuthStubTest, UserAuthStubCancelAuthOrIdentifyStub001, TestSize.Level0)
447 {
448     MessageParcel data;
449     MessageParcel reply;
450     MessageOption option(MessageOption::TF_SYNC);
451     uint32_t code = UserAuthInterfaceCode::USER_AUTH_CANCEL_AUTH;
452 
453     EXPECT_TRUE(data.WriteInterfaceToken(UserAuthInterface::GetDescriptor()));
454 
455     MockUserAuthService service;
456     EXPECT_EQ(READ_PARCEL_ERROR, service.OnRemoteRequest(code, data, reply, option));
457 }
458 
459 HWTEST_F(UserAuthStubTest, UserAuthStubCancelAuthOrIdentifyStub002, TestSize.Level0)
460 {
461     uint64_t testContextId = 9346248;
462 
463     MockUserAuthService service;
464     EXPECT_CALL(service, CancelAuthOrIdentify(_)).Times(1);
465     ON_CALL(service, CancelAuthOrIdentify)
466         .WillByDefault(
__anon5dedf9810802(uint64_t contextId) 467             [&testContextId](uint64_t contextId) {
468                 EXPECT_EQ(contextId, testContextId);
469                 return SUCCESS;
470             }
471         );
472 
473     MessageParcel data;
474     MessageParcel reply;
475     MessageOption option(MessageOption::TF_SYNC);
476     uint32_t code = UserAuthInterfaceCode::USER_AUTH_CANCEL_AUTH;
477 
478     EXPECT_TRUE(data.WriteInterfaceToken(UserAuthInterface::GetDescriptor()));
479     EXPECT_TRUE(data.WriteUint64(testContextId));
480 
481     EXPECT_EQ(SUCCESS, service.OnRemoteRequest(code, data, reply, option));
482     int32_t result = FAIL;
483     EXPECT_TRUE(reply.ReadInt32(result));
484     EXPECT_EQ(result, SUCCESS);
485 }
486 
487 HWTEST_F(UserAuthStubTest, UserAuthStubGetVersionStub, TestSize.Level0)
488 {
489     int32_t testVersion = 1000;
490 
491     MockUserAuthService service;
492     EXPECT_CALL(service, GetVersion(_)).Times(1);
493     ON_CALL(service, GetVersion)
494         .WillByDefault(
__anon5dedf9810902(int32_t &version) 495             [&testVersion](int32_t &version) {
496                 version = testVersion;
497                 return SUCCESS;
498             }
499         );
500 
501     MessageParcel data;
502     MessageParcel reply;
503     MessageOption option(MessageOption::TF_SYNC);
504     uint32_t code = UserAuthInterfaceCode::USER_AUTH_GET_VERSION;
505 
506     EXPECT_TRUE(data.WriteInterfaceToken(UserAuthInterface::GetDescriptor()));
507 
508     EXPECT_EQ(SUCCESS, service.OnRemoteRequest(code, data, reply, option));
509     int32_t version = -1;
510     EXPECT_TRUE(reply.ReadInt32(version));
511     EXPECT_EQ(version, testVersion);
512     int32_t result;
513     EXPECT_TRUE(reply.ReadInt32(result));
514     EXPECT_EQ(result, SUCCESS);
515 }
516 
517 HWTEST_F(UserAuthStubTest, UserAuthStubRegistUserAuthSuccessEventListenerStub, TestSize.Level0)
518 {
519     MockUserAuthService service;
520     sptr<MockAuthEventListenerService> callback(new (std::nothrow) MockAuthEventListenerService());
521     EXPECT_NE(callback, nullptr);
522     EXPECT_CALL(service, RegistUserAuthSuccessEventListener(_, _)).Times(1);
523     ON_CALL(service, RegistUserAuthSuccessEventListener)
524         .WillByDefault(
__anon5dedf9810a02(const std::vector<AuthType> &authType, const sptr<AuthEventListenerInterface> &callback) 525             [](const std::vector<AuthType> &authType, const sptr<AuthEventListenerInterface> &callback) {
526                 return SUCCESS;
527             }
528         );
529 
530     MessageParcel data;
531     MessageParcel reply;
532     MessageOption option(MessageOption::TF_SYNC);
533     uint32_t code = UserAuthInterfaceCode::USER_AUTH_REG_EVENT_LISTENER;
534 
535     EXPECT_TRUE(data.WriteInterfaceToken(UserAuthInterface::GetDescriptor()));
536     std::vector<int32_t> authType;
537     authType.push_back(static_cast<int32_t>(PIN));
538     authType.push_back(static_cast<int32_t>(FACE));
539     authType.push_back(static_cast<int32_t>(FINGERPRINT));
540 
541     EXPECT_TRUE(data.WriteInt32Vector(authType));
542     EXPECT_NE(callback->AsObject(), nullptr);
543     EXPECT_TRUE(data.WriteRemoteObject(callback->AsObject()));
544     EXPECT_EQ(SUCCESS, service.OnRemoteRequest(code, data, reply, option));
545     int32_t result;
546     EXPECT_TRUE(reply.ReadInt32(result));
547     EXPECT_EQ(result, SUCCESS);
548 }
549 
550 HWTEST_F(UserAuthStubTest, UserAuthStubUnRegistUserAuthSuccessEventListenerStub, TestSize.Level0)
551 {
552     MockUserAuthService service;
553     sptr<MockAuthEventListenerService> callback(new (std::nothrow) MockAuthEventListenerService());
554     EXPECT_NE(callback, nullptr);
555     EXPECT_CALL(service, UnRegistUserAuthSuccessEventListener(_)).Times(1);
556     ON_CALL(service, UnRegistUserAuthSuccessEventListener)
557         .WillByDefault(
__anon5dedf9810b02(const sptr<AuthEventListenerInterface> &callback) 558             [](const sptr<AuthEventListenerInterface> &callback) {
559                 return SUCCESS;
560             }
561         );
562 
563     MessageParcel data;
564     MessageParcel reply;
565     MessageOption option(MessageOption::TF_SYNC);
566     uint32_t code = UserAuthInterfaceCode::USER_AUTH_UNREG_EVENT_LISTENER;
567 
568     EXPECT_TRUE(data.WriteInterfaceToken(UserAuthInterface::GetDescriptor()));
569     EXPECT_NE(callback->AsObject(), nullptr);
570     EXPECT_TRUE(data.WriteRemoteObject(callback->AsObject()));
571     EXPECT_EQ(SUCCESS, service.OnRemoteRequest(code, data, reply, option));
572     int32_t result;
573     EXPECT_TRUE(reply.ReadInt32(result));
574     EXPECT_EQ(result, SUCCESS);
575 }
576 
577 HWTEST_F(UserAuthStubTest, UserAuthStubGetPropertyByIdStub001, TestSize.Level0)
578 {
579     MessageParcel data;
580     MessageParcel reply;
581     MessageOption option(MessageOption::TF_SYNC);
582     uint32_t code = UserAuthInterfaceCode::USER_AUTH_GET_PROPERTY_BY_ID;
583 
584     EXPECT_TRUE(data.WriteInterfaceToken(UserAuthInterface::GetDescriptor()));
585 
586     MockUserAuthService service;
587     EXPECT_EQ(READ_PARCEL_ERROR, service.OnRemoteRequest(code, data, reply, option));
588 }
589 
590 HWTEST_F(UserAuthStubTest, UserAuthStubGetPropertyByIdStub002, TestSize.Level0)
591 {
592     uint64_t testCredentialId = 1;
593     std::vector<Attributes::AttributeKey> testAttrKeys = {Attributes::ATTR_RESULT_CODE, Attributes::ATTR_SIGNATURE,
594         Attributes::ATTR_SCHEDULE_MODE};
595     std::vector<uint32_t> tempKeys;
596     for (auto &attrKey : testAttrKeys) {
597         tempKeys.push_back(static_cast<uint32_t>(attrKey));
598     }
599     sptr<MockGetExecutorPropertyCallback> callback(new (std::nothrow) MockGetExecutorPropertyCallback());
600     EXPECT_NE(callback, nullptr);
601     MockUserAuthService service;
602     EXPECT_CALL(service, GetPropertyById(_, _, _)).Times(1);
603     ON_CALL(service, GetPropertyById)
604         .WillByDefault(
605             [&testCredentialId, &testAttrKeys](uint64_t credentialId, const std::vector<Attributes::AttributeKey> &keys,
__anon5dedf9810c02(uint64_t credentialId, const std::vector<Attributes::AttributeKey> &keys, sptr<GetExecutorPropertyCallbackInterface> &callback) 606             sptr<GetExecutorPropertyCallbackInterface> &callback) {
607                 EXPECT_EQ(credentialId, testCredentialId);
608                 EXPECT_THAT(keys, ElementsAreArray(testAttrKeys));
609                 if (callback != nullptr) {
610                     Attributes attr;
611                     callback->OnGetExecutorPropertyResult(SUCCESS, attr);
612                 }
613             }
614         );
615     EXPECT_CALL(*callback, OnGetExecutorPropertyResult(_, _)).Times(1);
616 
617     MessageParcel data;
618     MessageParcel reply;
619     MessageOption option(MessageOption::TF_SYNC);
620     uint32_t code = UserAuthInterfaceCode::USER_AUTH_GET_PROPERTY_BY_ID;
621 
622     EXPECT_TRUE(data.WriteInterfaceToken(UserAuthInterface::GetDescriptor()));
623     EXPECT_TRUE(data.WriteUint64(testCredentialId));
624     EXPECT_TRUE(data.WriteUInt32Vector(tempKeys));
625     EXPECT_NE(callback->AsObject(), nullptr);
626     EXPECT_TRUE(data.WriteRemoteObject(callback->AsObject()));
627 
628     EXPECT_EQ(SUCCESS, service.OnRemoteRequest(code, data, reply, option));
629 }
630 } // namespace UserAuth
631 } // namespace UserIam
632 } // namespace OHOS