1 /*
2 * Copyright (C) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "user_auth_stub_test.h"
17
18 #include "iam_common_defines.h"
19 #include "mock_user_auth_callback.h"
20 #include "mock_user_auth_service.h"
21
22 namespace OHOS {
23 namespace UserIam {
24 namespace UserAuth {
25 using namespace testing;
26 using namespace testing::ext;
27
SetUpTestCase()28 void UserAuthStubTest::SetUpTestCase()
29 {
30 }
31
TearDownTestCase()32 void UserAuthStubTest::TearDownTestCase()
33 {
34 }
35
SetUp()36 void UserAuthStubTest::SetUp()
37 {
38 }
39
TearDown()40 void UserAuthStubTest::TearDown()
41 {
42 }
43
44 HWTEST_F(UserAuthStubTest, UserAuthStubGetAvailableStatusStub001, TestSize.Level0)
45 {
46 MessageParcel data;
47 MessageParcel reply;
48 MessageOption option(MessageOption::TF_SYNC);
49 uint32_t code = UserAuthInterface::USER_AUTH_GET_AVAILABLE_STATUS;
50
51 EXPECT_TRUE(data.WriteInterfaceToken(UserAuthInterface::GetDescriptor()));
52
53 MockUserAuthService service;
54 EXPECT_EQ(READ_PARCEL_ERROR, service.OnRemoteRequest(code, data, reply, option));
55 }
56
57 HWTEST_F(UserAuthStubTest, UserAuthStubGetAvailableStatusStub002, TestSize.Level0)
58 {
59 MockUserAuthService service;
60 AuthType testAuthType = FACE;
61 AuthTrustLevel testAuthTrustLevel = ATL3;
62 int32_t testApiVersion = 8;
63 EXPECT_CALL(service, GetAvailableStatus(_, _, _)).Times(1);
64 ON_CALL(service, GetAvailableStatus)
65 .WillByDefault(
66 [&testAuthType, &testAuthTrustLevel, &testApiVersion](int32_t apiVersion, AuthType authType,
__anon7b82abc90102(int32_t apiVersion, AuthType authType, AuthTrustLevel authTrustLevel) 67 AuthTrustLevel authTrustLevel) {
68 EXPECT_EQ(apiVersion, testApiVersion);
69 EXPECT_EQ(authType, testAuthType);
70 EXPECT_EQ(authTrustLevel, testAuthTrustLevel);
71 return SUCCESS;
72 }
73 );
74
75 MessageParcel data;
76 MessageParcel reply;
77 MessageOption option(MessageOption::TF_SYNC);
78 uint32_t code = UserAuthInterface::USER_AUTH_GET_AVAILABLE_STATUS;
79
80 EXPECT_TRUE(data.WriteInterfaceToken(UserAuthInterface::GetDescriptor()));
81 EXPECT_TRUE(data.WriteInt32(testAuthType));
82 EXPECT_TRUE(data.WriteUint32(testAuthTrustLevel));
83 EXPECT_TRUE(data.WriteInt32(testApiVersion));
84
85 EXPECT_EQ(SUCCESS, service.OnRemoteRequest(code, data, reply, option));
86 int32_t result = FAIL;
87 EXPECT_TRUE(reply.ReadInt32(result));
88 EXPECT_EQ(result, SUCCESS);
89 }
90
91 HWTEST_F(UserAuthStubTest, UserAuthStubGetPropertyStub001, TestSize.Level0)
92 {
93 MessageParcel data;
94 MessageParcel reply;
95 MessageOption option(MessageOption::TF_SYNC);
96 uint32_t code = UserAuthInterface::USER_AUTH_GET_PROPERTY;
97
98 EXPECT_TRUE(data.WriteInterfaceToken(UserAuthInterface::GetDescriptor()));
99
100 MockUserAuthService service;
101 EXPECT_EQ(READ_PARCEL_ERROR, service.OnRemoteRequest(code, data, reply, option));
102 }
103
104 HWTEST_F(UserAuthStubTest, UserAuthStubGetPropertyStub002, TestSize.Level0)
105 {
106 int32_t testUserId = 1232666;
107 AuthType testAuthType = FACE;
108 std::vector<Attributes::AttributeKey> testAttrKeys = {Attributes::ATTR_RESULT_CODE, Attributes::ATTR_SIGNATURE,
109 Attributes::ATTR_SCHEDULE_MODE};
110 std::vector<uint32_t> tempKeys;
111 for (auto &attrKey : testAttrKeys) {
112 tempKeys.push_back(static_cast<uint32_t>(attrKey));
113 }
114 sptr<MockGetExecutorPropertyCallback> callback = new MockGetExecutorPropertyCallback();
115 EXPECT_NE(callback, nullptr);
116 MockUserAuthService service;
117 EXPECT_CALL(service, GetProperty(_, _, _, _)).Times(1);
118 ON_CALL(service, GetProperty)
119 .WillByDefault(
120 [&testUserId, &testAuthType, &testAttrKeys](int32_t userId, AuthType authType,
121 const std::vector<Attributes::AttributeKey> &keys,
__anon7b82abc90202(int32_t userId, AuthType authType, const std::vector<Attributes::AttributeKey> &keys, sptr<GetExecutorPropertyCallbackInterface> &callback) 122 sptr<GetExecutorPropertyCallbackInterface> &callback) {
123 EXPECT_EQ(userId, testUserId);
124 EXPECT_EQ(authType, testAuthType);
125 EXPECT_THAT(keys, ElementsAreArray(testAttrKeys));
126 if (callback != nullptr) {
127 Attributes attr;
128 callback->OnGetExecutorPropertyResult(SUCCESS, attr);
129 }
130 }
131 );
132 EXPECT_CALL(*callback, OnGetExecutorPropertyResult(_, _)).Times(1);
133
134 MessageParcel data;
135 MessageParcel reply;
136 MessageOption option(MessageOption::TF_SYNC);
137 uint32_t code = UserAuthInterface::USER_AUTH_GET_PROPERTY;
138
139 EXPECT_TRUE(data.WriteInterfaceToken(UserAuthInterface::GetDescriptor()));
140 EXPECT_TRUE(data.WriteInt32(testUserId));
141 EXPECT_TRUE(data.WriteInt32(testAuthType));
142 EXPECT_TRUE(data.WriteUInt32Vector(tempKeys));
143 EXPECT_NE(callback->AsObject(), nullptr);
144 EXPECT_TRUE(data.WriteRemoteObject(callback->AsObject()));
145
146 EXPECT_EQ(SUCCESS, service.OnRemoteRequest(code, data, reply, option));
147 }
148
149 HWTEST_F(UserAuthStubTest, UserAuthStubSetPropertyStub001, TestSize.Level0)
150 {
151 MessageParcel data;
152 MessageParcel reply;
153 MessageOption option(MessageOption::TF_SYNC);
154 uint32_t code = UserAuthInterface::USER_AUTH_SET_PROPERTY;
155
156 EXPECT_TRUE(data.WriteInterfaceToken(UserAuthInterface::GetDescriptor()));
157
158 MockUserAuthService service;
159 EXPECT_EQ(READ_PARCEL_ERROR, service.OnRemoteRequest(code, data, reply, option));
160 }
161
162 HWTEST_F(UserAuthStubTest, UserAuthStubSetPropertyStub002, TestSize.Level0)
163 {
164 int32_t testUserId = 132282;
165 AuthType testAuthType = FACE;
166 Attributes attributes;
167
168 uint64_t testTemplateId = 3364734;
169 EXPECT_TRUE(attributes.SetUint64Value(Attributes::ATTR_TEMPLATE_ID, testTemplateId));
170
171 sptr<MockSetExecutorPropertyCallback> callback = new MockSetExecutorPropertyCallback();
172 EXPECT_NE(callback, nullptr);
173 MockUserAuthService service;
174 EXPECT_CALL(service, SetProperty(_, _, _, _)).Times(1);
175 ON_CALL(service, SetProperty)
176 .WillByDefault(
177 [&testUserId, &testAuthType, &testTemplateId](int32_t userId, AuthType authType,
__anon7b82abc90302(int32_t userId, AuthType authType, const Attributes &attributes, sptr<SetExecutorPropertyCallbackInterface> &callback) 178 const Attributes &attributes, sptr<SetExecutorPropertyCallbackInterface> &callback) {
179 EXPECT_EQ(userId, testUserId);
180 EXPECT_EQ(authType, testAuthType);
181 uint64_t tempTemplateId = 0;
182 EXPECT_TRUE(attributes.GetUint64Value(Attributes::ATTR_TEMPLATE_ID, tempTemplateId));
183 EXPECT_EQ(tempTemplateId, testTemplateId);
184 if (callback != nullptr) {
185 callback->OnSetExecutorPropertyResult(SUCCESS);
186 }
187 }
188 );
189 EXPECT_CALL(*callback, OnSetExecutorPropertyResult(_)).Times(1);
190
191 MessageParcel data;
192 MessageParcel reply;
193 MessageOption option(MessageOption::TF_SYNC);
194 uint32_t code = UserAuthInterface::USER_AUTH_SET_PROPERTY;
195
196 EXPECT_TRUE(data.WriteInterfaceToken(UserAuthInterface::GetDescriptor()));
197 EXPECT_TRUE(data.WriteInt32(testUserId));
198 EXPECT_TRUE(data.WriteInt32(testAuthType));
199 EXPECT_TRUE(data.WriteUInt8Vector(attributes.Serialize()));
200 EXPECT_NE(callback->AsObject(), nullptr);
201 EXPECT_TRUE(data.WriteRemoteObject(callback->AsObject()));
202
203 EXPECT_EQ(SUCCESS, service.OnRemoteRequest(code, data, reply, option));
204 }
205
206 HWTEST_F(UserAuthStubTest, UserAuthStubAuthStub001, TestSize.Level0)
207 {
208 MessageParcel data;
209 MessageParcel reply;
210 MessageOption option(MessageOption::TF_SYNC);
211 uint32_t code = UserAuthInterface::USER_AUTH_AUTH;
212
213 EXPECT_TRUE(data.WriteInterfaceToken(UserAuthInterface::GetDescriptor()));
214
215 MockUserAuthService service;
216 EXPECT_EQ(READ_PARCEL_ERROR, service.OnRemoteRequest(code, data, reply, option));
217 }
218
219 HWTEST_F(UserAuthStubTest, UserAuthStubAuthStub002, TestSize.Level0)
220 {
221 int32_t testApiVersion = 9;
222 std::vector<uint8_t> testChallenge = {1, 2, 4, 5};
223 AuthType testAuthType = FACE;
224 AuthTrustLevel testAtl = ATL2;
225 uint64_t testContextId = 2346782;
226
227 sptr<MockUserAuthCallback> callback = new MockUserAuthCallback();
228 EXPECT_NE(callback, nullptr);
229 MockUserAuthService service;
230 EXPECT_CALL(service, Auth(_, _, _, _, _)).Times(1);
231 ON_CALL(service, Auth)
232 .WillByDefault(
233 [&testChallenge, &testAuthType, &testAtl, &testContextId, &testApiVersion](int32_t apiVersion,
234 const std::vector<uint8_t> &challenge, AuthType authType, AuthTrustLevel authTrustLevel,
__anon7b82abc90402(int32_t apiVersion, const std::vector<uint8_t> &challenge, AuthType authType, AuthTrustLevel authTrustLevel, sptr<UserAuthCallbackInterface> &callback) 235 sptr<UserAuthCallbackInterface> &callback) {
236 EXPECT_EQ(apiVersion, testApiVersion);
237 EXPECT_THAT(challenge, ElementsAreArray(testChallenge));
238 EXPECT_EQ(authType, testAuthType);
239 EXPECT_EQ(authTrustLevel, testAtl);
240 if (callback != nullptr) {
241 Attributes attr;
242 callback->OnResult(SUCCESS, attr);
243 }
244 return testContextId;
245 }
246 );
247 EXPECT_CALL(*callback, OnResult(_, _)).Times(1);
248
249 MessageParcel data;
250 MessageParcel reply;
251 MessageOption option(MessageOption::TF_SYNC);
252 uint32_t code = UserAuthInterface::USER_AUTH_AUTH;
253
254 EXPECT_TRUE(data.WriteInterfaceToken(UserAuthInterface::GetDescriptor()));
255 EXPECT_TRUE(data.WriteUInt8Vector(testChallenge));
256 EXPECT_TRUE(data.WriteInt32(testAuthType));
257 EXPECT_TRUE(data.WriteUint32(testAtl));
258 EXPECT_NE(callback->AsObject(), nullptr);
259 EXPECT_TRUE(data.WriteRemoteObject(callback->AsObject()));
260 EXPECT_TRUE(data.WriteInt32(testApiVersion));
261
262 EXPECT_EQ(SUCCESS, service.OnRemoteRequest(code, data, reply, option));
263 uint64_t contextId = 0;
264 EXPECT_TRUE(reply.ReadUint64(contextId));
265 EXPECT_EQ(contextId, testContextId);
266 }
267
268 HWTEST_F(UserAuthStubTest, UserAuthStubAuthUserStub001, TestSize.Level0)
269 {
270 MessageParcel data;
271 MessageParcel reply;
272 MessageOption option(MessageOption::TF_SYNC);
273 uint32_t code = UserAuthInterface::USER_AUTH_AUTH_USER;
274
275 EXPECT_TRUE(data.WriteInterfaceToken(UserAuthInterface::GetDescriptor()));
276
277 MockUserAuthService service;
278 EXPECT_EQ(READ_PARCEL_ERROR, service.OnRemoteRequest(code, data, reply, option));
279 }
280
281 HWTEST_F(UserAuthStubTest, UserAuthStubAuthUserStub002, TestSize.Level0)
282 {
283 int32_t testUserId = 3467;
284 std::vector<uint8_t> testChallenge = {1, 2, 5, 9};
285 AuthType testAuthType = FACE;
286 AuthTrustLevel testAtl = ATL2;
287 uint64_t testContextId = 2346728;
288
289 sptr<MockUserAuthCallback> callback = new MockUserAuthCallback();
290 EXPECT_NE(callback, nullptr);
291 MockUserAuthService service;
292 EXPECT_CALL(service, AuthUser(_, _, _, _, _)).Times(1);
293 ON_CALL(service, AuthUser)
294 .WillByDefault(
295 [&testUserId, &testChallenge, &testAuthType, &testAtl, &testContextId](int32_t userId,
296 const std::vector<uint8_t> &challenge, AuthType authType, AuthTrustLevel authTrustLevel,
__anon7b82abc90502(int32_t userId, const std::vector<uint8_t> &challenge, AuthType authType, AuthTrustLevel authTrustLevel, sptr<UserAuthCallbackInterface> &callback) 297 sptr<UserAuthCallbackInterface> &callback) {
298 EXPECT_EQ(userId, testUserId);
299 EXPECT_THAT(challenge, ElementsAreArray(testChallenge));
300 EXPECT_EQ(authType, testAuthType);
301 EXPECT_EQ(authTrustLevel, testAtl);
302 if (callback != nullptr) {
303 Attributes attr;
304 callback->OnResult(SUCCESS, attr);
305 }
306 return testContextId;
307 }
308 );
309 EXPECT_CALL(*callback, OnResult(_, _)).Times(1);
310
311 MessageParcel data;
312 MessageParcel reply;
313 MessageOption option(MessageOption::TF_SYNC);
314 uint32_t code = UserAuthInterface::USER_AUTH_AUTH_USER;
315
316 EXPECT_TRUE(data.WriteInterfaceToken(UserAuthInterface::GetDescriptor()));
317 EXPECT_TRUE(data.WriteInt32(testUserId));
318 EXPECT_TRUE(data.WriteUInt8Vector(testChallenge));
319 EXPECT_TRUE(data.WriteInt32(testAuthType));
320 EXPECT_TRUE(data.WriteUint32(testAtl));
321 EXPECT_NE(callback->AsObject(), nullptr);
322 EXPECT_TRUE(data.WriteRemoteObject(callback->AsObject()));
323
324 EXPECT_EQ(SUCCESS, service.OnRemoteRequest(code, data, reply, option));
325 uint64_t contextId = 0;
326 EXPECT_TRUE(reply.ReadUint64(contextId));
327 EXPECT_EQ(contextId, testContextId);
328 }
329
330 HWTEST_F(UserAuthStubTest, UserAuthStubIdentifyStub001, TestSize.Level0)
331 {
332 MessageParcel data;
333 MessageParcel reply;
334 MessageOption option(MessageOption::TF_SYNC);
335 uint32_t code = UserAuthInterface::USER_AUTH_IDENTIFY;
336
337 EXPECT_TRUE(data.WriteInterfaceToken(UserAuthInterface::GetDescriptor()));
338
339 MockUserAuthService service;
340 EXPECT_EQ(READ_PARCEL_ERROR, service.OnRemoteRequest(code, data, reply, option));
341 }
342
343 HWTEST_F(UserAuthStubTest, UserAuthStubIdentifyStub002, TestSize.Level0)
344 {
345 std::vector<uint8_t> testChallenge = {1, 2, 5, 8, 9};
346 AuthType testAuthType = FACE;
347 uint64_t testContextId = 76374284;
348
349 sptr<MockUserAuthCallback> callback = new MockUserAuthCallback();
350 EXPECT_NE(callback, nullptr);
351 MockUserAuthService service;
352 EXPECT_CALL(service, Identify(_, _, _)).Times(1);
353 ON_CALL(service, Identify)
354 .WillByDefault(
355 [&testChallenge, &testAuthType, &testContextId](const std::vector<uint8_t> &challenge, AuthType authType,
__anon7b82abc90602(const std::vector<uint8_t> &challenge, AuthType authType, sptr<UserAuthCallbackInterface> &callback) 356 sptr<UserAuthCallbackInterface> &callback) {
357 EXPECT_THAT(challenge, ElementsAreArray(testChallenge));
358 EXPECT_EQ(authType, testAuthType);
359 if (callback != nullptr) {
360 Attributes attr;
361 callback->OnResult(SUCCESS, attr);
362 }
363 return testContextId;
364 }
365 );
366 EXPECT_CALL(*callback, OnResult(_, _)).Times(1);
367
368 MessageParcel data;
369 MessageParcel reply;
370 MessageOption option(MessageOption::TF_SYNC);
371 uint32_t code = UserAuthInterface::USER_AUTH_IDENTIFY;
372
373 EXPECT_TRUE(data.WriteInterfaceToken(UserAuthInterface::GetDescriptor()));
374 EXPECT_TRUE(data.WriteUInt8Vector(testChallenge));
375 EXPECT_TRUE(data.WriteInt32(testAuthType));
376 EXPECT_NE(callback->AsObject(), nullptr);
377 EXPECT_TRUE(data.WriteRemoteObject(callback->AsObject()));
378
379 EXPECT_EQ(SUCCESS, service.OnRemoteRequest(code, data, reply, option));
380 uint64_t contextId = 0;
381 EXPECT_TRUE(reply.ReadUint64(contextId));
382 EXPECT_EQ(contextId, testContextId);
383 }
384
385 HWTEST_F(UserAuthStubTest, UserAuthStubCancelAuthOrIdentifyStub001, TestSize.Level0)
386 {
387 MessageParcel data;
388 MessageParcel reply;
389 MessageOption option(MessageOption::TF_SYNC);
390 uint32_t code = UserAuthInterface::USER_AUTH_CANCEL_AUTH;
391
392 EXPECT_TRUE(data.WriteInterfaceToken(UserAuthInterface::GetDescriptor()));
393
394 MockUserAuthService service;
395 EXPECT_EQ(READ_PARCEL_ERROR, service.OnRemoteRequest(code, data, reply, option));
396 }
397
398 HWTEST_F(UserAuthStubTest, UserAuthStubCancelAuthOrIdentifyStub002, TestSize.Level0)
399 {
400 uint64_t testContextId = 9346248;
401
402 MockUserAuthService service;
403 EXPECT_CALL(service, CancelAuthOrIdentify(_)).Times(1);
404 ON_CALL(service, CancelAuthOrIdentify)
405 .WillByDefault(
__anon7b82abc90702(uint64_t contextId) 406 [&testContextId](uint64_t contextId) {
407 EXPECT_EQ(contextId, testContextId);
408 return SUCCESS;
409 }
410 );
411
412 MessageParcel data;
413 MessageParcel reply;
414 MessageOption option(MessageOption::TF_SYNC);
415 uint32_t code = UserAuthInterface::USER_AUTH_CANCEL_AUTH;
416
417 EXPECT_TRUE(data.WriteInterfaceToken(UserAuthInterface::GetDescriptor()));
418 EXPECT_TRUE(data.WriteUint64(testContextId));
419
420 EXPECT_EQ(SUCCESS, service.OnRemoteRequest(code, data, reply, option));
421 int32_t result = FAIL;
422 EXPECT_TRUE(reply.ReadInt32(result));
423 EXPECT_EQ(result, SUCCESS);
424 }
425
426 HWTEST_F(UserAuthStubTest, UserAuthStubGetVersionStub, TestSize.Level0)
427 {
428 int32_t testVersion = 1000;
429
430 MockUserAuthService service;
431 EXPECT_CALL(service, GetVersion(_)).Times(1);
432 ON_CALL(service, GetVersion)
433 .WillByDefault(
__anon7b82abc90802(int32_t &version) 434 [&testVersion](int32_t &version) {
435 version = testVersion;
436 return SUCCESS;
437 }
438 );
439
440 MessageParcel data;
441 MessageParcel reply;
442 MessageOption option(MessageOption::TF_SYNC);
443 uint32_t code = UserAuthInterface::USER_AUTH_GET_VERSION;
444
445 EXPECT_TRUE(data.WriteInterfaceToken(UserAuthInterface::GetDescriptor()));
446
447 EXPECT_EQ(SUCCESS, service.OnRemoteRequest(code, data, reply, option));
448 int32_t version = -1;
449 EXPECT_TRUE(reply.ReadInt32(version));
450 EXPECT_EQ(version, testVersion);
451 int32_t result;
452 EXPECT_TRUE(reply.ReadInt32(result));
453 EXPECT_EQ(result, SUCCESS);
454 }
455 } // namespace UserAuth
456 } // namespace UserIam
457 } // namespace OHOS