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_idm_service_test.h"
17
18 #include <future>
19
20 #include "iam_common_defines.h"
21 #include "iam_ptr.h"
22
23 #include "context_pool.h"
24 #include "executor_messenger_service.h"
25 #include "mock_context.h"
26 #include "mock_ipc_common.h"
27 #include "mock_iuser_auth_interface.h"
28 #include "mock_resource_node.h"
29 #include "mock_user_idm_callback.h"
30 #include "resource_node_pool.h"
31 #include "user_idm_service.h"
32
33 namespace OHOS {
34 namespace UserIam {
35 namespace UserAuth {
36 using namespace testing;
37 using namespace testing::ext;
38
39 using HdiCredentialInfo = OHOS::HDI::UserAuth::V1_0::CredentialInfo;
40 using HdiEnrolledInfo = OHOS::HDI::UserAuth::V1_0::EnrolledInfo;
41 using HdiExecutorInfo = OHOS::HDI::UserAuth::V1_0::ExecutorInfo;
42 using HdiAuthType = OHOS::HDI::UserAuth::V1_0::AuthType;
43 using HdiPinSubType = OHOS::HDI::UserAuth::V1_0::PinSubType;
44 using HdiEnrollParam = OHOS::HDI::UserAuth::V1_0::EnrollParam;
45 using HdiScheduleInfo = OHOS::HDI::UserAuth::V1_0::ScheduleInfo;
46
SetUpTestCase()47 void UserIdmServiceTest::SetUpTestCase()
48 {
49 }
50
TearDownTestCase()51 void UserIdmServiceTest::TearDownTestCase()
52 {
53 }
54
SetUp()55 void UserIdmServiceTest::SetUp()
56 {
57 MockIUserAuthInterface::Holder::GetInstance().Reset();
58 }
59
TearDown()60 void UserIdmServiceTest::TearDown()
61 {
62 MockIUserAuthInterface::Holder::GetInstance().Reset();
63 }
64
65 HWTEST_F(UserIdmServiceTest, UserIdmServiceOpenSession_001, TestSize.Level0)
66 {
67 UserIdmService service(123123, true);
68 int32_t testUserId = 0;
69 std::vector<uint8_t> testChallenge = {1, 2, 3, 4};
70 auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
71 EXPECT_NE(mockHdi, nullptr);
72 EXPECT_CALL(*mockHdi, OpenSession(_, _)).Times(1);
73 ON_CALL(*mockHdi, OpenSession)
74 .WillByDefault(
__anonf7366b450102(int32_t userId, std::vector<uint8_t> &challenge) 75 [&testChallenge](int32_t userId, std::vector<uint8_t> &challenge) {
76 challenge = testChallenge;
77 return HDF_SUCCESS;
78 }
79 );
80 std::vector<uint8_t> challenge;
81 IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
82 int32_t ret = service.OpenSession(testUserId, challenge);
83 EXPECT_EQ(ret, SUCCESS);
84 EXPECT_THAT(challenge, ElementsAreArray(testChallenge));
85 IpcCommon::DeleteAllPermission();
86 }
87
88 HWTEST_F(UserIdmServiceTest, UserIdmServiceOpenSession_002, TestSize.Level0)
89 {
90 UserIdmService service(123123, true);
91 int32_t testUserId = 0;
92 std::vector<uint8_t> testChallenge = {1, 2, 3, 4};
93
94 std::vector<uint8_t> challenge;
95 int32_t ret = service.OpenSession(testUserId, challenge);
96 EXPECT_EQ(ret, CHECK_PERMISSION_FAILED);
97
98 IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
99 auto context = Common::MakeShared<MockContext>();
100 EXPECT_NE(context, nullptr);
101 EXPECT_CALL(*context, GetContextType()).WillRepeatedly(Return(CONTEXT_ENROLL));
102 EXPECT_CALL(*context, GetContextId()).WillRepeatedly(Return(2345));
103 EXPECT_CALL(*context, Stop()).WillRepeatedly(Return(true));
104 EXPECT_TRUE(ContextPool::Instance().Insert(context));
105
106 auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
107 EXPECT_NE(mockHdi, nullptr);
108 EXPECT_CALL(*mockHdi, OpenSession(_, _))
109 .Times(3)
110 .WillOnce(Return(HDF_SUCCESS))
111 .WillOnce(Return(HDF_SUCCESS))
112 .WillOnce(Return(HDF_FAILURE));
113
114 EXPECT_EQ(service.OpenSession(testUserId, challenge), SUCCESS);
115 EXPECT_EQ(service.OpenSession(testUserId, challenge), SUCCESS);
116 EXPECT_EQ(service.OpenSession(testUserId, challenge), GENERAL_ERROR);
117
118 IpcCommon::DeleteAllPermission();
119 }
120
121 HWTEST_F(UserIdmServiceTest, UserIdmServiceCloseSession, TestSize.Level0)
122 {
123 UserIdmService service(123123, true);
124 int32_t testUserId = 3546;
125
126 service.CloseSession(testUserId);
127 auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
128 EXPECT_NE(mockHdi, nullptr);
129 EXPECT_CALL(*mockHdi, CloseSession(_))
130 .Times(2)
131 .WillOnce(Return(HDF_SUCCESS))
132 .WillOnce(Return(HDF_FAILURE));
133 IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
134 service.CloseSession(testUserId);
135 service.CloseSession(testUserId);
136 IpcCommon::DeleteAllPermission();
137 }
138
139 HWTEST_F(UserIdmServiceTest, UserIdmServiceGetCredentialInfo001, TestSize.Level0)
140 {
141 UserIdmService service(123123, true);
142 int32_t testUserId = 0;
143 AuthType testAuthType = PIN;
144
145 sptr<IdmGetCredInfoCallbackInterface> testCallback = new MockIdmGetCredentialInfoCallback();
146 EXPECT_NE(testCallback, nullptr);
147 auto *tempCallback = static_cast<MockIdmGetCredentialInfoCallback *>(testCallback.GetRefPtr());
148 EXPECT_NE(tempCallback, nullptr);
149 EXPECT_CALL(*tempCallback, OnCredentialInfos(_, _)).Times(2);
150
151 auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
152 EXPECT_NE(mockHdi, nullptr);
153 EXPECT_CALL(*mockHdi, GetCredential(_, _, _)).WillOnce(Return(HDF_FAILURE));
154 int32_t ret = service.GetCredentialInfo(testUserId, testAuthType, testCallback);
155 EXPECT_EQ(ret, CHECK_PERMISSION_FAILED);
156 IpcCommon::AddPermission(USE_USER_IDM_PERMISSION);
157 ret = service.GetCredentialInfo(testUserId, testAuthType, testCallback);
158 EXPECT_EQ(ret, NOT_ENROLLED);
159 IpcCommon::DeleteAllPermission();
160 }
161
162 HWTEST_F(UserIdmServiceTest, UserIdmServiceGetCredentialInfo002, TestSize.Level0)
163 {
164 UserIdmService service(123123, true);
165 int32_t testUserId = 0;
166 AuthType testAuthType = PIN;
167 sptr<IdmGetCredInfoCallbackInterface> testCallback = nullptr;
168 int32_t ret = service.GetCredentialInfo(testUserId, testAuthType, testCallback);
169 EXPECT_EQ(ret, INVALID_PARAMETERS);
170 }
171
172 HWTEST_F(UserIdmServiceTest, UserIdmServiceGetCredentialInfo003, TestSize.Level0)
173 {
174 UserIdmService service(123123, true);
175 int32_t testUserId = 0;
176 AuthType testAuthType = PIN;
177 sptr<IdmGetCredInfoCallbackInterface> testCallback = new MockIdmGetCredentialInfoCallback();
178 EXPECT_NE(testCallback, nullptr);
179 auto *tempCallback = static_cast<MockIdmGetCredentialInfoCallback *>(testCallback.GetRefPtr());
180 EXPECT_NE(tempCallback, nullptr);
181 EXPECT_CALL(*tempCallback, OnCredentialInfos(_, _)).Times(2);
182
183 auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
184 EXPECT_NE(mockHdi, nullptr);
185 EXPECT_CALL(*mockHdi, GetCredential(_, _, _)).Times(2);
186 ON_CALL(*mockHdi, GetCredential)
187 .WillByDefault(
__anonf7366b450202(int32_t userId, HdiAuthType authType, std::vector<HdiCredentialInfo> &infos) 188 [](int32_t userId, HdiAuthType authType, std::vector<HdiCredentialInfo> &infos) {
189 HdiCredentialInfo tempInfo = {
190 .credentialId = 1,
191 .executorIndex = 2,
192 .templateId = 3,
193 .authType = static_cast<HdiAuthType>(1),
194 .executorMatcher = 2,
195 .executorSensorHint = 3,
196 };
197 infos.push_back(tempInfo);
198 return HDF_SUCCESS;
199 }
200 );
201 EXPECT_CALL(*mockHdi, GetUserInfo(_, _, _, _))
202 .Times(2)
203 .WillOnce(Return(HDF_FAILURE))
204 .WillOnce(
__anonf7366b450302(int32_t userId, uint64_t &secureUid, HdiPinSubType &pinSubType, std::vector<HdiEnrolledInfo> &infos) 205 [](int32_t userId, uint64_t &secureUid, HdiPinSubType &pinSubType, std::vector<HdiEnrolledInfo> &infos) {
206 HdiEnrolledInfo info = {
207 .enrolledId = 0,
208 .authType = static_cast<HdiAuthType>(1),
209 };
210 infos.push_back(info);
211 pinSubType = static_cast<HdiPinSubType>(10000);
212 secureUid = 4542;
213 return HDF_SUCCESS;
214 }
215 );
216
217 IpcCommon::AddPermission(USE_USER_IDM_PERMISSION);
218 int32_t ret = service.GetCredentialInfo(testUserId, testAuthType, testCallback);
219 EXPECT_EQ(ret, INVALID_PARAMETERS);
220 ret = service.GetCredentialInfo(testUserId, testAuthType, testCallback);
221 EXPECT_EQ(ret, SUCCESS);
222 IpcCommon::DeleteAllPermission();
223 }
224
225 HWTEST_F(UserIdmServiceTest, UserIdmServiceGetSecInfo001, TestSize.Level0)
226 {
227 UserIdmService service(123123, true);
228 int32_t testUserId = 0;
229
230 sptr<IdmGetSecureUserInfoCallbackInterface> testCallback = new MockIdmGetSecureUserInfoCallback();
231 EXPECT_NE(testCallback, nullptr);
232 auto *tempCallback = static_cast<MockIdmGetSecureUserInfoCallback *>(testCallback.GetRefPtr());
233 EXPECT_NE(tempCallback, nullptr);
234 EXPECT_CALL(*tempCallback, OnSecureUserInfo(_)).Times(2);
235
236 auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
237 EXPECT_NE(mockHdi, nullptr);
238 EXPECT_CALL(*mockHdi, GetUserInfo(_, _, _, _)).WillOnce(Return(HDF_FAILURE));
239
240 int32_t ret = service.GetSecInfo(testUserId, testCallback);
241 EXPECT_EQ(ret, CHECK_PERMISSION_FAILED);
242 IpcCommon::AddPermission(USE_USER_IDM_PERMISSION);
243 ret = service.GetSecInfo(testUserId, testCallback);
244 EXPECT_EQ(ret, INVALID_PARAMETERS);
245 IpcCommon::DeleteAllPermission();
246 }
247
248 HWTEST_F(UserIdmServiceTest, UserIdmServiceGetSecInfo002, TestSize.Level0)
249 {
250 UserIdmService service(123123, true);
251 int32_t testUserId = 0;
252 sptr<IdmGetSecureUserInfoCallbackInterface> testCallback = nullptr;
253 IpcCommon::AddPermission(USE_USER_IDM_PERMISSION);
254 int32_t ret = service.GetSecInfo(testUserId, testCallback);
255 EXPECT_EQ(ret, INVALID_PARAMETERS);
256
257 auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
258 EXPECT_NE(mockHdi, nullptr);
259 EXPECT_CALL(*mockHdi, GetUserInfo(_, _, _, _))
260 .Times(2)
261 .WillOnce(Return(HDF_FAILURE))
262 .WillOnce(
__anonf7366b450402(int32_t userId, uint64_t &secureUid, HdiPinSubType &pinSubType, std::vector<HdiEnrolledInfo> &infos) 263 [](int32_t userId, uint64_t &secureUid, HdiPinSubType &pinSubType, std::vector<HdiEnrolledInfo> &infos) {
264 HdiEnrolledInfo info = {
265 .enrolledId = 0,
266 .authType = static_cast<HdiAuthType>(1),
267 };
268 infos.push_back(info);
269 pinSubType = static_cast<HdiPinSubType>(10000);
270 secureUid = 4542;
271 return HDF_SUCCESS;
272 }
273 );
274
275 testCallback = new MockIdmGetSecureUserInfoCallback();
276 EXPECT_NE(testCallback, nullptr);
277 auto *tempCallback = static_cast<MockIdmGetSecureUserInfoCallback *>(testCallback.GetRefPtr());
278 EXPECT_NE(tempCallback, nullptr);
279 EXPECT_CALL(*tempCallback, OnSecureUserInfo(_)).Times(2);
280 ret = service.GetSecInfo(testUserId, testCallback);
281 EXPECT_EQ(ret, INVALID_PARAMETERS);
282 ret = service.GetSecInfo(testUserId, testCallback);
283 EXPECT_EQ(ret, SUCCESS);
284 IpcCommon::DeleteAllPermission();
285 }
286
287 HWTEST_F(UserIdmServiceTest, UserIdmServiceAddCredential001, TestSize.Level0)
288 {
289 UserIdmService service(123123, true);
290 int32_t testUserId = 15457;
291 UserIdmInterface::CredentialPara testCredPara = {};
292 testCredPara.authType = PIN;
293 testCredPara.pinType = PIN_SIX;
294 testCredPara.token = {1, 2, 3, 4};
295 sptr<IdmCallbackInterface> testCallback = nullptr;
296 service.AddCredential(testUserId, testCredPara, testCallback, false);
297 }
298
299 HWTEST_F(UserIdmServiceTest, UserIdmServiceAddCredential002, TestSize.Level0)
300 {
301 UserIdmService service(123123, true);
302 int32_t testUserId = 15457;
303 UserIdmInterface::CredentialPara testCredPara = {};
304 testCredPara.authType = PIN;
305 testCredPara.pinType = PIN_SIX;
306 testCredPara.token = {1, 2, 3, 4};
307 sptr<IdmCallbackInterface> testCallback = new MockIdmCallback();
308 EXPECT_NE(testCallback, nullptr);
309 auto *tempCallback = static_cast<MockIdmCallback *>(testCallback.GetRefPtr());
310 EXPECT_NE(tempCallback, nullptr);
311 EXPECT_CALL(*tempCallback, OnResult(_, _))
312 .Times(2)
313 .WillOnce(
__anonf7366b450502(int32_t result, const Attributes &extraInfo) 314 [](int32_t result, const Attributes &extraInfo) {
315 EXPECT_EQ(result, CHECK_PERMISSION_FAILED);
316 }
317 )
318 .WillOnce(
__anonf7366b450602(int32_t result, const Attributes &extraInfo) 319 [](int32_t result, const Attributes &extraInfo) {
320 EXPECT_EQ(result, HDF_FAILURE);
321 }
322 );
323 auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
324 EXPECT_NE(mockHdi, nullptr);
325 EXPECT_CALL(*mockHdi, BeginEnrollment(_, _, _, _)).WillRepeatedly(Return(HDF_FAILURE));
326
327 service.AddCredential(testUserId, testCredPara, testCallback, false);
328 IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
329 service.AddCredential(testUserId, testCredPara, testCallback, false);
330 IpcCommon::DeleteAllPermission();
331 }
332
333 HWTEST_F(UserIdmServiceTest, UserIdmServiceAddCredential003, TestSize.Level0)
334 {
335 UserIdmService service(123123, true);
336 int32_t testUserId = 15457;
337 UserIdmInterface::CredentialPara testCredPara = {};
338 testCredPara.authType = PIN;
339 testCredPara.pinType = PIN_SIX;
340 testCredPara.token = {1, 2, 3, 4};
341 std::shared_ptr<Context> context = nullptr;
342
343 sptr<IdmCallbackInterface> testCallback = new MockIdmCallback();
344 EXPECT_NE(testCallback, nullptr);
345 auto *tempCallback = static_cast<MockIdmCallback *>(testCallback.GetRefPtr());
346 EXPECT_NE(tempCallback, nullptr);
347 EXPECT_CALL(*tempCallback, OnResult(_, _))
348 .WillOnce(
__anonf7366b450702(int32_t result, const Attributes &extraInfo) 349 [&context](int32_t result, const Attributes &extraInfo) {
350 EXPECT_EQ(result, SUCCESS);
351 if (context != nullptr) {
352 context->Stop();
353 }
354 }
355 );
356
357 auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
358 EXPECT_NE(mockHdi, nullptr);
359 EXPECT_CALL(*mockHdi, BeginEnrollment(_, _, _, _))
360 .WillOnce(
361 [&context](int32_t userId, const std::vector<uint8_t> &authToken, const HdiEnrollParam ¶m,
__anonf7366b450802(int32_t userId, const std::vector<uint8_t> &authToken, const HdiEnrollParam ¶m, HdiScheduleInfo &info) 362 HdiScheduleInfo &info) {
363 HdiExecutorInfo executorInfo = {};
364 executorInfo.executorIndex = 60;
365 info.executors.push_back(executorInfo);
366 info.scheduleId = 20;
367 info.authType = HdiAuthType::PIN;
368 auto contextList = ContextPool::Instance().Select(CONTEXT_ENROLL);
369 if (!contextList.empty()) {
370 context = contextList[0].lock();
371 }
372 return HDF_SUCCESS;
373 }
374 );
375
376 EXPECT_CALL(*mockHdi, UpdateEnrollmentResult(_, _, _)).WillOnce(Return(HDF_SUCCESS));
377 std::promise<void> promise;
378 EXPECT_CALL(*mockHdi, CancelEnrollment(_))
379 .WillOnce(
__anonf7366b450902(int32_t userId) 380 [&promise](int32_t userId) {
381 promise.set_value();
382 return HDF_SUCCESS;
383 }
384 );
385
386 auto resourceNode = Common::MakeShared<MockResourceNode>();
387 EXPECT_NE(resourceNode, nullptr);
388 EXPECT_CALL(*resourceNode, GetExecutorIndex()).WillRepeatedly(Return(60));
389 EXPECT_CALL(*resourceNode, GetAuthType()).WillRepeatedly(Return(PIN));
390 EXPECT_CALL(*resourceNode, GetExecutorRole()).WillRepeatedly(Return(ALL_IN_ONE));
391 EXPECT_CALL(*resourceNode, GetExecutorMatcher()).WillRepeatedly(Return(0));
392 EXPECT_CALL(*resourceNode, GetExecutorPublicKey()).WillRepeatedly(Return(std::vector<uint8_t>()));
393 EXPECT_CALL(*resourceNode, BeginExecute(_, _, _))
394 .WillOnce(
__anonf7366b450a02(uint64_t scheduleId, const std::vector<uint8_t> &publicKey, const Attributes &command) 395 [](uint64_t scheduleId, const std::vector<uint8_t> &publicKey, const Attributes &command) {
396 auto messenger = ExecutorMessengerService::GetInstance();
397 EXPECT_NE(messenger, nullptr);
398 auto finalResult = Common::MakeShared<Attributes>();
399 EXPECT_NE(finalResult, nullptr);
400 std::vector<uint8_t> scheduleResult = {1, 2, 3, 4};
401 EXPECT_TRUE(finalResult->SetUint8ArrayValue(Attributes::ATTR_RESULT, scheduleResult));
402 EXPECT_EQ(messenger->Finish(20, ALL_IN_ONE, SUCCESS, finalResult), SUCCESS);
403 return SUCCESS;
404 }
405 );
406
407 EXPECT_TRUE(ResourceNodePool::Instance().Insert(resourceNode));
408
409 IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
410 service.AddCredential(testUserId, testCredPara, testCallback, false);
411 promise.get_future().get();
412
413 EXPECT_TRUE(ResourceNodePool::Instance().Delete(60));
414 IpcCommon::DeleteAllPermission();
415 }
416
417 HWTEST_F(UserIdmServiceTest, UserIdmServiceUpdateCredential001, TestSize.Level0)
418 {
419 UserIdmService service(123123, true);
420 int32_t testUserId = 1548545;
421 UserIdmInterface::CredentialPara testCredPara = {};
422 testCredPara.authType = FACE;
423 testCredPara.pinType = PIN_SIX;
424 testCredPara.token = {1, 2, 3, 4};
425 sptr<IdmCallbackInterface> testCallback = new MockIdmCallback();
426 EXPECT_NE(testCallback, nullptr);
427 auto *tempCallback = static_cast<MockIdmCallback *>(testCallback.GetRefPtr());
428 EXPECT_NE(tempCallback, nullptr);
429 EXPECT_CALL(*tempCallback, OnResult(_, _))
430 .WillOnce(
__anonf7366b450b02(int32_t result, const Attributes &extraInfo) 431 [](int32_t result, const Attributes &extraInfo) {
432 EXPECT_EQ(result, NOT_ENROLLED);
433 }
434 );
435 auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
436 EXPECT_NE(mockHdi, nullptr);
437 EXPECT_CALL(*mockHdi, GetCredential(_, _, _)).WillRepeatedly(Return(HDF_SUCCESS));
438 IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
439 service.UpdateCredential(testUserId, testCredPara, testCallback);
440 IpcCommon::DeleteAllPermission();
441 }
442
443 HWTEST_F(UserIdmServiceTest, UserIdmServiceUpdateCredential002, TestSize.Level0)
444 {
445 UserIdmService service(123123, true);
446 int32_t testUserId = 1548545;
447 UserIdmInterface::CredentialPara testCredPara = {};
448 testCredPara.authType = FACE;
449 testCredPara.pinType = PIN_SIX;
450 sptr<IdmCallbackInterface> testCallback = nullptr;
451
452 IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
453 service.UpdateCredential(testUserId, testCredPara, testCallback);
454
455 testCallback = new MockIdmCallback();
456 EXPECT_NE(testCallback, nullptr);
457 auto *tempCallback = static_cast<MockIdmCallback *>(testCallback.GetRefPtr());
458 EXPECT_NE(tempCallback, nullptr);
459 EXPECT_CALL(*tempCallback, OnResult(_, _))
460 .Times(2)
461 .WillOnce(
__anonf7366b450c02(int32_t result, const Attributes &extraInfo) 462 [](int32_t result, const Attributes &extraInfo) {
463 EXPECT_EQ(result, GENERAL_ERROR);
464 }
465 )
466 .WillOnce(
__anonf7366b450d02(int32_t result, const Attributes &extraInfo) 467 [](int32_t result, const Attributes &extraInfo) {
468 EXPECT_EQ(result, HDF_FAILURE);
469 }
470 );
471
472 auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
473 EXPECT_NE(mockHdi, nullptr);
474 EXPECT_CALL(*mockHdi, GetCredential(_, _, _))
475 .WillOnce(
__anonf7366b450e02(int32_t userId, HdiAuthType authType, std::vector<HdiCredentialInfo> &infos) 476 [](int32_t userId, HdiAuthType authType, std::vector<HdiCredentialInfo> &infos) {
477 HdiCredentialInfo tempInfo = {
478 .credentialId = 1,
479 .executorIndex = 2,
480 .templateId = 3,
481 .authType = static_cast<HdiAuthType>(2),
482 .executorMatcher = 2,
483 .executorSensorHint = 3,
484 };
485 infos.push_back(tempInfo);
486 return HDF_SUCCESS;
487 }
488 );
489
490 EXPECT_CALL(*mockHdi, BeginEnrollment(_, _, _, _)).WillOnce(Return(HDF_FAILURE));
491
492 service.UpdateCredential(testUserId, testCredPara, testCallback);
493
494 testCredPara.token = {1, 2, 3, 4};
495 service.UpdateCredential(testUserId, testCredPara, testCallback);
496 IpcCommon::DeleteAllPermission();
497 }
498
499 HWTEST_F(UserIdmServiceTest, UserIdmServiceCancel001, TestSize.Level0)
500 {
501 UserIdmService service(123123, true);
502 int32_t testUserId = 154835;
503 int32_t ret = service.Cancel(testUserId);
504 EXPECT_EQ(ret, CHECK_PERMISSION_FAILED);
505 IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
506 ret = service.Cancel(testUserId);
507 EXPECT_EQ(ret, GENERAL_ERROR);
508 IpcCommon::DeleteAllPermission();
509 }
510
511 HWTEST_F(UserIdmServiceTest, UserIdmServiceCancel002, TestSize.Level0)
512 {
513 UserIdmService service(123123, true);
514 int32_t testUserId = 69874;
515 auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
516 EXPECT_NE(mockHdi, nullptr);
517 EXPECT_CALL(*mockHdi, OpenSession(_, _)).WillOnce(Return(HDF_SUCCESS));
518
519 IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
520 std::vector<uint8_t> challenge;
521 int32_t ret = service.OpenSession(testUserId, challenge);
522 EXPECT_EQ(ret, SUCCESS);
523 ret = service.Cancel(testUserId);
524 EXPECT_EQ(ret, GENERAL_ERROR);
525 IpcCommon::DeleteAllPermission();
526 }
527
528 HWTEST_F(UserIdmServiceTest, UserIdmServiceCancel003, TestSize.Level0)
529 {
530 UserIdmService service(123123, true);
531 int32_t testUserId = 96874;
532 auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
533 EXPECT_NE(mockHdi, nullptr);
534 EXPECT_CALL(*mockHdi, OpenSession(_, _)).WillOnce(Return(HDF_SUCCESS));
535
536 IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
537 std::vector<uint8_t> challenge;
538 int32_t ret = service.OpenSession(testUserId, challenge);
539 EXPECT_EQ(ret, SUCCESS);
540
541 auto context = Common::MakeShared<MockContext>();
542 EXPECT_NE(context, nullptr);
543 EXPECT_CALL(*context, GetContextType()).WillRepeatedly(Return(CONTEXT_ENROLL));
544 EXPECT_CALL(*context, GetContextId()).WillRepeatedly(Return(2345));
545 EXPECT_CALL(*context, Stop()).WillRepeatedly(Return(true));
546 EXPECT_TRUE(ContextPool::Instance().Insert(context));
547
548 ret = service.Cancel(testUserId);
549 EXPECT_EQ(ret, SUCCESS);
550 IpcCommon::DeleteAllPermission();
551 }
552
553 HWTEST_F(UserIdmServiceTest, UserIdmServiceEnforceDelUser001, TestSize.Level0)
554 {
555 UserIdmService service(123123, true);
556 int32_t testUserId = 15485;
557 sptr<IdmCallbackInterface> testCallback = new MockIdmCallback();
558 EXPECT_NE(testCallback, nullptr);
559 auto *tempCallback = static_cast<MockIdmCallback *>(testCallback.GetRefPtr());
560 EXPECT_NE(tempCallback, nullptr);
561 EXPECT_CALL(*tempCallback, OnResult(_, _))
562 .Times(2)
563 .WillOnce(
__anonf7366b450f02(int32_t result, const Attributes &extraInfo) 564 [](int32_t result, const Attributes &extraInfo) {
565 EXPECT_EQ(result, CHECK_PERMISSION_FAILED);
566 }
567 )
568 .WillOnce(
__anonf7366b451002(int32_t result, const Attributes &extraInfo) 569 [](int32_t result, const Attributes &extraInfo) {
570 EXPECT_EQ(result, INVALID_PARAMETERS);
571 }
572 );
573
574 auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
575 EXPECT_NE(mockHdi, nullptr);
576 EXPECT_CALL(*mockHdi, GetUserInfo(_, _, _, _)).WillOnce(Return(HDF_FAILURE));
577
578 int32_t ret = service.EnforceDelUser(testUserId, testCallback);
579 EXPECT_EQ(ret, CHECK_PERMISSION_FAILED);
580 IpcCommon::AddPermission(ENFORCE_USER_IDM);
581 ret = service.EnforceDelUser(testUserId, testCallback);
582 EXPECT_EQ(ret, INVALID_PARAMETERS);
583 IpcCommon::DeleteAllPermission();
584 }
585
586 HWTEST_F(UserIdmServiceTest, UserIdmServiceEnforceDelUser002, TestSize.Level0)
587 {
588 UserIdmService service(123123, true);
589 int32_t testUserId = 15485;
590 sptr<IdmCallbackInterface> testCallback = nullptr;
591 int32_t ret = service.EnforceDelUser(testUserId, testCallback);
592 EXPECT_EQ(ret, INVALID_PARAMETERS);
593 }
594
595 HWTEST_F(UserIdmServiceTest, UserIdmServiceEnforceDelUser003, TestSize.Level0)
596 {
597 UserIdmService service(123123, true);
598 int32_t testUserId = 15485;
599 sptr<IdmCallbackInterface> testCallback = new MockIdmCallback();
600 EXPECT_NE(testCallback, nullptr);
601 auto *tempCallback = static_cast<MockIdmCallback *>(testCallback.GetRefPtr());
602 EXPECT_NE(tempCallback, nullptr);
603 EXPECT_CALL(*tempCallback, OnResult(_, _))
604 .Times(2)
605 .WillOnce(
__anonf7366b451102(int32_t result, const Attributes &extraInfo) 606 [](int32_t result, const Attributes &extraInfo) {
607 EXPECT_EQ(result, HDF_FAILURE);
608 }
609 )
610 .WillOnce(
__anonf7366b451202(int32_t result, const Attributes &extraInfo) 611 [](int32_t result, const Attributes &extraInfo) {
612 EXPECT_EQ(result, SUCCESS);
613 }
614 );
615
616 auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
617 EXPECT_NE(mockHdi, nullptr);
618 EXPECT_CALL(*mockHdi, GetUserInfo(_, _, _, _))
619 .WillRepeatedly(
__anonf7366b451302(int32_t userId, uint64_t &secureUid, HdiPinSubType &pinSubType, std::vector<HdiEnrolledInfo> &infos) 620 [](int32_t userId, uint64_t &secureUid, HdiPinSubType &pinSubType, std::vector<HdiEnrolledInfo> &infos) {
621 HdiEnrolledInfo info = {
622 .enrolledId = 0,
623 .authType = static_cast<HdiAuthType>(1),
624 };
625 infos.push_back(info);
626 pinSubType = static_cast<HdiPinSubType>(10000);
627 secureUid = 4542;
628 return HDF_SUCCESS;
629 }
630 );
631
632 EXPECT_CALL(*mockHdi, EnforceDeleteUser(_, _))
633 .Times(2)
634 .WillOnce(Return(HDF_FAILURE))
635 .WillOnce(
__anonf7366b451402(int32_t userId, std::vector<HdiCredentialInfo> &deletedInfos) 636 [](int32_t userId, std::vector<HdiCredentialInfo> &deletedInfos) {
637 HdiCredentialInfo info = {};
638 info.authType = static_cast<HdiAuthType>(1);
639 info.credentialId = 10;
640 info.executorIndex = 20;
641 info.executorMatcher = 30;
642 info.executorSensorHint = 40;
643 info.templateId = 50;
644 deletedInfos.emplace_back(info);
645 return HDF_SUCCESS;
646 }
647 );
648
649 IpcCommon::AddPermission(ENFORCE_USER_IDM);
650 int32_t ret = service.EnforceDelUser(testUserId, testCallback);
651 EXPECT_EQ(ret, -1);
652 ret = service.EnforceDelUser(testUserId, testCallback);
653 EXPECT_EQ(ret, SUCCESS);
654 IpcCommon::DeleteAllPermission();
655 }
656
657 HWTEST_F(UserIdmServiceTest, UserIdmServiceDelUser001, TestSize.Level0)
658 {
659 UserIdmService service(123123, true);
660 int32_t testUserId = 15486465;
661 std::vector<uint8_t> testAuthToken = {1, 2, 3, 4};
662
663 sptr<IdmCallbackInterface> testCallback = new MockIdmCallback();
664 EXPECT_NE(testCallback, nullptr);
665 auto *tempCallback = static_cast<MockIdmCallback *>(testCallback.GetRefPtr());
666 EXPECT_NE(tempCallback, nullptr);
667 EXPECT_CALL(*tempCallback, OnResult(_, _))
668 .Times(2)
669 .WillOnce(
__anonf7366b451502(int32_t result, const Attributes &extraInfo) 670 [](int32_t result, const Attributes &extraInfo) {
671 EXPECT_EQ(result, CHECK_PERMISSION_FAILED);
672 }
673 )
674 .WillOnce(
__anonf7366b451602(int32_t result, const Attributes &extraInfo) 675 [](int32_t result, const Attributes &extraInfo) {
676 EXPECT_EQ(result, HDF_FAILURE);
677 }
678 );
679
680 auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
681 EXPECT_NE(mockHdi, nullptr);
682 EXPECT_CALL(*mockHdi, DeleteUser(_, _, _)).WillOnce(Return(HDF_FAILURE));
683
684 service.DelUser(testUserId, testAuthToken, testCallback);
685 IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
686 service.DelUser(testUserId, testAuthToken, testCallback);
687 IpcCommon::DeleteAllPermission();
688 }
689
690 HWTEST_F(UserIdmServiceTest, UserIdmServiceDelUser002, TestSize.Level0)
691 {
692 UserIdmService service(123123, true);
693 int32_t testUserId = 15486465;
694 std::vector<uint8_t> testAuthToken = {1, 2, 3, 4};
695 sptr<IdmCallbackInterface> testCallback = nullptr;
696 service.DelUser(testUserId, testAuthToken, testCallback);
697 }
698
699 HWTEST_F(UserIdmServiceTest, UserIdmServiceDelUser003, TestSize.Level0)
700 {
701 UserIdmService service(123123, true);
702 int32_t testUserId = 15486465;
703 std::vector<uint8_t> testAuthToken;
704 sptr<IdmCallbackInterface> testCallback = new MockIdmCallback();
705 EXPECT_NE(testCallback, nullptr);
706 auto *tempCallback = static_cast<MockIdmCallback *>(testCallback.GetRefPtr());
707 EXPECT_NE(tempCallback, nullptr);
708 EXPECT_CALL(*tempCallback, OnResult(_, _))
709 .WillOnce(
__anonf7366b451702(int32_t result, const Attributes &extraInfo) 710 [](int32_t result, const Attributes &extraInfo) {
711 EXPECT_EQ(result, INVALID_PARAMETERS);
712 }
713 );
714
715 IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
716 service.DelUser(testUserId, testAuthToken, testCallback);
717 IpcCommon::DeleteAllPermission();
718 }
719
720 HWTEST_F(UserIdmServiceTest, UserIdmServiceDelUser004, TestSize.Level0)
721 {
722 UserIdmService service(123123, true);
723 int32_t testUserId = 15486465;
724 std::vector<uint8_t> testAuthToken = {1, 2, 3, 4};
725 sptr<IdmCallbackInterface> testCallback = new MockIdmCallback();
726 EXPECT_NE(testCallback, nullptr);
727 auto *tempCallback = static_cast<MockIdmCallback *>(testCallback.GetRefPtr());
728 EXPECT_NE(tempCallback, nullptr);
729 EXPECT_CALL(*tempCallback, OnResult(_, _))
730 .WillOnce(
__anonf7366b451802(int32_t result, const Attributes &extraInfo) 731 [](int32_t result, const Attributes &extraInfo) {
732 EXPECT_EQ(result, SUCCESS);
733 }
734 );
735
736 auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
737 EXPECT_NE(mockHdi, nullptr);
738 EXPECT_CALL(*mockHdi, DeleteUser(_, _, _))
739 .WillOnce(
__anonf7366b451902(int32_t userId, const std::vector<uint8_t> &authToken, std::vector<HdiCredentialInfo> &deletedInfos) 740 [](int32_t userId, const std::vector<uint8_t> &authToken, std::vector<HdiCredentialInfo> &deletedInfos) {
741 HdiCredentialInfo info = {};
742 info.authType = static_cast<HdiAuthType>(1);
743 info.credentialId = 10;
744 info.executorIndex = 20;
745 info.executorMatcher = 30;
746 info.executorSensorHint = 40;
747 info.templateId = 50;
748 deletedInfos.emplace_back(info);
749 return HDF_SUCCESS;
750 }
751 );
752
753 IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
754 service.DelUser(testUserId, testAuthToken, testCallback);
755 IpcCommon::DeleteAllPermission();
756 }
757
758 HWTEST_F(UserIdmServiceTest, UserIdmServiceDelCredential001, TestSize.Level0)
759 {
760 UserIdmService service(123123, true);
761 int32_t testUserId = 1548865;
762 uint64_t testCredentialId = 23424;
763 std::vector<uint8_t> testAuthToken = {1, 2, 3, 4};
764
765 sptr<IdmCallbackInterface> testCallback = new MockIdmCallback();
766 EXPECT_NE(testCallback, nullptr);
767 auto *tempCallback = static_cast<MockIdmCallback *>(testCallback.GetRefPtr());
768 EXPECT_NE(tempCallback, nullptr);
769 EXPECT_CALL(*tempCallback, OnResult(_, _))
770 .Times(2)
771 .WillOnce(
__anonf7366b451a02(int32_t result, const Attributes &extraInfo) 772 [](int32_t result, const Attributes &extraInfo) {
773 EXPECT_EQ(result, CHECK_PERMISSION_FAILED);
774 }
775 )
776 .WillOnce(
__anonf7366b451b02(int32_t result, const Attributes &extraInfo) 777 [](int32_t result, const Attributes &extraInfo) {
778 EXPECT_EQ(result, HDF_FAILURE);
779 }
780 );
781
782 auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
783 EXPECT_NE(mockHdi, nullptr);
784 EXPECT_CALL(*mockHdi, DeleteCredential(_, _, _, _)).WillOnce(Return(HDF_FAILURE));
785
786 service.DelCredential(testUserId, testCredentialId, testAuthToken, testCallback);
787 IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
788 service.DelCredential(testUserId, testCredentialId, testAuthToken, testCallback);
789 IpcCommon::DeleteAllPermission();
790 }
791
792 HWTEST_F(UserIdmServiceTest, UserIdmServiceDelCredential002, TestSize.Level0)
793 {
794 UserIdmService service(123123, true);
795 int32_t testUserId = 1548865;
796 uint64_t testCredentialId = 23424;
797 std::vector<uint8_t> testAuthToken = {1, 2, 3, 4};
798 sptr<IdmCallbackInterface> testCallback = nullptr;
799 service.DelCredential(testUserId, testCredentialId, testAuthToken, testCallback);
800 }
801
802 HWTEST_F(UserIdmServiceTest, UserIdmServiceDelCredential003, TestSize.Level0)
803 {
804 UserIdmService service(123123, true);
805 int32_t testUserId = 1548865;
806 uint64_t testCredentialId = 23424;
807 std::vector<uint8_t> testAuthToken = {1, 2, 3, 4};
808
809 sptr<IdmCallbackInterface> testCallback = new MockIdmCallback();
810 EXPECT_NE(testCallback, nullptr);
811 auto *tempCallback = static_cast<MockIdmCallback *>(testCallback.GetRefPtr());
812 EXPECT_NE(tempCallback, nullptr);
813 EXPECT_CALL(*tempCallback, OnResult(_, _))
814 .WillOnce(
__anonf7366b451c02(int32_t result, const Attributes &extraInfo) 815 [](int32_t result, const Attributes &extraInfo) {
816 EXPECT_EQ(result, SUCCESS);
817 }
818 );
819
820 auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
821 EXPECT_NE(mockHdi, nullptr);
822 EXPECT_CALL(*mockHdi, DeleteCredential(_, _, _, _))
823 .WillOnce(
__anonf7366b451d02(int32_t userId, uint64_t credentialId, const std::vector<uint8_t> &authToken, HdiCredentialInfo &info) 824 [](int32_t userId, uint64_t credentialId, const std::vector<uint8_t> &authToken, HdiCredentialInfo &info) {
825 info.authType = static_cast<HdiAuthType>(1);
826 info.credentialId = 10;
827 info.executorIndex = 20;
828 info.executorMatcher = 30;
829 info.executorSensorHint = 40;
830 info.templateId = 50;
831 return HDF_SUCCESS;
832 }
833 );
834
835 IpcCommon::AddPermission(MANAGE_USER_IDM_PERMISSION);
836 service.DelCredential(testUserId, testCredentialId, testAuthToken, testCallback);
837 IpcCommon::DeleteAllPermission();
838 }
839
840 HWTEST_F(UserIdmServiceTest, UserIdmServiceTestDump, TestSize.Level0)
841 {
842 int testFd1 = -1;
843 int testFd2 = 1;
844 std::vector<std::u16string> testArgs;
845
846 UserIdmService service(123123, true);
847
848 auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
849 EXPECT_NE(mockHdi, nullptr);
850 EXPECT_CALL(*mockHdi, GetUserInfo(_, _, _, _))
851 .Times(2)
852 .WillOnce(Return(HDF_FAILURE))
853 .WillOnce(
__anonf7366b451e02(int32_t userId, uint64_t &secureUid, HdiPinSubType &pinSubType, std::vector<HdiEnrolledInfo> &infos) 854 [](int32_t userId, uint64_t &secureUid, HdiPinSubType &pinSubType, std::vector<HdiEnrolledInfo> &infos) {
855 HdiEnrolledInfo info = {
856 .enrolledId = 0,
857 .authType = static_cast<HdiAuthType>(1),
858 };
859 infos.push_back(info);
860 pinSubType = static_cast<HdiPinSubType>(10000);
861 secureUid = 4542;
862 return HDF_SUCCESS;
863 }
864 );
865
866 EXPECT_EQ(service.Dump(testFd1, testArgs), INVALID_PARAMETERS);
867 EXPECT_EQ(service.Dump(testFd2, testArgs), SUCCESS);
868 testArgs.push_back(u"-h");
869 EXPECT_EQ(service.Dump(testFd2, testArgs), SUCCESS);
870 testArgs.clear();
871 testArgs.push_back(u"-l");
872 EXPECT_EQ(service.Dump(testFd2, testArgs), SUCCESS);
873 EXPECT_EQ(service.Dump(testFd2, testArgs), SUCCESS);
874 testArgs.clear();
875 testArgs.push_back(u"-k");
876 EXPECT_EQ(service.Dump(testFd2, testArgs), GENERAL_ERROR);
877 }
878 } // namespace UserAuth
879 } // namespace UserIam
880 } // namespace OHOS