1 /*
2 * Copyright (C) 2022-2023 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 "enroll_context.h"
17
18 #include "mock_context.h"
19 #include "mock_credential_info.h"
20 #include "mock_enrollment.h"
21 #include "mock_schedule_node.h"
22 #include "mock_update_pin_param_info.h"
23
24 using namespace testing;
25 using namespace testing::ext;
26 namespace OHOS {
27 namespace UserIam {
28 namespace UserAuth {
29 class EnrollContextTest : public testing::Test {
30 public:
31 static void SetUpTestCase();
32
33 static void TearDownTestCase();
34
35 void SetUp() override;
36
37 void TearDown() override;
38 };
39
SetUpTestCase()40 void EnrollContextTest::SetUpTestCase()
41 {
42 }
43
TearDownTestCase()44 void EnrollContextTest::TearDownTestCase()
45 {
46 }
47
SetUp()48 void EnrollContextTest::SetUp()
49 {
50 }
51
TearDown()52 void EnrollContextTest::TearDown()
53 {
54 }
55
56 HWTEST_F(EnrollContextTest, EnrollContextTest_NullHdi, TestSize.Level0)
57 {
58 const uint64_t testContestId = 2;
59 const int32_t testResultCode = 7;
60 const auto finalResult = Common::MakeShared<Attributes>();
61 ASSERT_NE(finalResult, nullptr);
62 std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
63 ASSERT_NE(contextCallback, nullptr);
64 EXPECT_CALL(*contextCallback, OnResult(_, _)).Times(Exactly(1));
65 // Error: enroll is null
66 std::shared_ptr<Enrollment> enroll = nullptr;
67
68 auto oriContext = Common::MakeShared<EnrollContext>(testContestId, enroll, contextCallback, true);
69 ASSERT_NE(oriContext, nullptr);
70 std::shared_ptr<Context> context = oriContext;
71 std::shared_ptr<ScheduleNodeCallback> nodeCallback = oriContext;
72
73 ASSERT_EQ(context->Start(), false);
74 ASSERT_EQ(context->Stop(), false);
75 nodeCallback->OnScheduleStoped(testResultCode, finalResult);
76 }
77
78 HWTEST_F(EnrollContextTest, EnrollContextTest_NullCallback, TestSize.Level0)
79 {
80 const uint64_t testContestId = 2;
81 const ExecutorRole testRole = static_cast<ExecutorRole>(3);
82 const int32_t testModuleType = 4;
83 const std::vector<uint8_t> testAcquireMsg = {4, 5, 6};
84 const int32_t testResultCode = 7;
85 const auto finalResult = Common::MakeShared<Attributes>();
86 ASSERT_NE(finalResult, nullptr);
87
88 std::shared_ptr<MockEnrollment> mockEnroll = Common::MakeShared<MockEnrollment>();
89 ASSERT_NE(mockEnroll, nullptr);
90 // Error: contextCallback is null
91 std::shared_ptr<ContextCallback> contextCallback = nullptr;
92
93 auto oriContext = Common::MakeShared<EnrollContext>(testContestId, mockEnroll, contextCallback, true);
94 ASSERT_NE(oriContext, nullptr);
95 std::shared_ptr<ScheduleNodeCallback> nodeCallback = oriContext;
96
97 nodeCallback->OnScheduleStarted();
98 nodeCallback->OnScheduleProcessed(testRole, testModuleType, testAcquireMsg);
99 nodeCallback->OnScheduleStoped(testResultCode, finalResult);
100 }
101
102 HWTEST_F(EnrollContextTest, EnrollContextTest_BasicInfo, TestSize.Level0)
103 {
104 const uint64_t testContestId = 2;
105
106 std::shared_ptr<MockEnrollment> mockEnroll = Common::MakeShared<MockEnrollment>();
107 ASSERT_NE(mockEnroll, nullptr);
108 std::shared_ptr<ContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
109 ASSERT_NE(contextCallback, nullptr);
110
111 auto oriContext = Common::MakeShared<EnrollContext>(testContestId, mockEnroll, contextCallback, true);
112 ASSERT_NE(oriContext, nullptr);
113 std::shared_ptr<Context> context = oriContext;
114
115 ASSERT_EQ(context->GetContextId(), testContestId);
116 ASSERT_EQ(context->GetContextType(), CONTEXT_ENROLL);
117 }
118
119 HWTEST_F(EnrollContextTest, EnrollContextTest_Start_001, TestSize.Level0)
120 {
121 static const uint64_t testContestId = 2;
122
123 std::shared_ptr<MockEnrollment> mockEnroll = Common::MakeShared<MockEnrollment>();
124 ASSERT_NE(mockEnroll, nullptr);
125 EXPECT_CALL(*mockEnroll, GetLatestError()).Times(1);
126 EXPECT_CALL(*mockEnroll, Start(_, _))
127 .Times(Exactly(1))
128 .WillOnce([](std::vector<std::shared_ptr<ScheduleNode>> &scheduleList,
__anon6505430c0102(std::vector<std::shared_ptr<ScheduleNode>> &scheduleList, std::shared_ptr<ScheduleNodeCallback> callback) 129 std::shared_ptr<ScheduleNodeCallback> callback) {
130 // Error: process enrollment start fail
131 return false;
132 });
133 std::shared_ptr<ContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
134 ASSERT_NE(contextCallback, nullptr);
135 std::shared_ptr<Context> context =
136 Common::MakeShared<EnrollContext>(testContestId, mockEnroll, contextCallback, true);
137 ASSERT_NE(context, nullptr);
138 ASSERT_EQ(context->Start(), false);
139 }
140
141 HWTEST_F(EnrollContextTest, EnrollContextTest_Start_002, TestSize.Level0)
142 {
143 static const uint64_t testContestId = 2;
144
145 std::shared_ptr<MockEnrollment> mockEnroll = Common::MakeShared<MockEnrollment>();
146 ASSERT_NE(mockEnroll, nullptr);
147 EXPECT_CALL(*mockEnroll, Start(_, _))
148 .Times(Exactly(1))
149 .WillOnce([](std::vector<std::shared_ptr<ScheduleNode>> &scheduleList,
__anon6505430c0202(std::vector<std::shared_ptr<ScheduleNode>> &scheduleList, std::shared_ptr<ScheduleNodeCallback> callback) 150 std::shared_ptr<ScheduleNodeCallback> callback) {
151 // Error: scheduleNodeList size = 0
152 return true;
153 });
154 std::shared_ptr<ContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
155 ASSERT_NE(contextCallback, nullptr);
156 std::shared_ptr<Context> context =
157 Common::MakeShared<EnrollContext>(testContestId, mockEnroll, contextCallback, true);
158 ASSERT_NE(context, nullptr);
159 ASSERT_EQ(context->Start(), false);
160 }
161
162 HWTEST_F(EnrollContextTest, EnrollContextTest_Start_003, TestSize.Level0)
163 {
164 static const uint64_t testContestId = 2;
165
166 std::shared_ptr<MockEnrollment> mockEnroll = Common::MakeShared<MockEnrollment>();
167 ASSERT_NE(mockEnroll, nullptr);
168 EXPECT_CALL(*mockEnroll, Start(_, _))
169 .Times(Exactly(1))
170 .WillOnce([](std::vector<std::shared_ptr<ScheduleNode>> &scheduleList,
__anon6505430c0302(std::vector<std::shared_ptr<ScheduleNode>> &scheduleList, std::shared_ptr<ScheduleNodeCallback> callback) 171 std::shared_ptr<ScheduleNodeCallback> callback) {
172 // Error: scheduleNodeList size = 2
173 scheduleList.push_back(Common::MakeShared<MockScheduleNode>());
174 scheduleList.push_back(Common::MakeShared<MockScheduleNode>());
175 return true;
176 });
177 std::shared_ptr<ContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
178 ASSERT_NE(contextCallback, nullptr);
179 std::shared_ptr<Context> context =
180 Common::MakeShared<EnrollContext>(testContestId, mockEnroll, contextCallback, true);
181 ASSERT_NE(context, nullptr);
182 ASSERT_EQ(context->Start(), false);
183 }
184
185 HWTEST_F(EnrollContextTest, EnrollContextTest_Start_004, TestSize.Level0)
186 {
187 static const uint64_t testContestId = 2;
188
189 std::shared_ptr<MockEnrollment> mockEnroll = Common::MakeShared<MockEnrollment>();
190 ASSERT_NE(mockEnroll, nullptr);
191 EXPECT_CALL(*mockEnroll, Start(_, _))
192 .Times(Exactly(1))
193 .WillOnce([](std::vector<std::shared_ptr<ScheduleNode>> &scheduleList,
__anon6505430c0402(std::vector<std::shared_ptr<ScheduleNode>> &scheduleList, std::shared_ptr<ScheduleNodeCallback> callback) 194 std::shared_ptr<ScheduleNodeCallback> callback) {
195 // Error: schedule node start fail
196 auto scheduleNode = Common::MakeShared<MockScheduleNode>();
197 EXPECT_NE(scheduleNode, nullptr);
198
199 EXPECT_CALL(*scheduleNode, StartSchedule()).Times(Exactly(1)).WillOnce(Return(false));
200 scheduleList.push_back(scheduleNode);
201 return true;
202 });
203 std::shared_ptr<ContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
204 ASSERT_NE(contextCallback, nullptr);
205 std::shared_ptr<Context> context =
206 Common::MakeShared<EnrollContext>(testContestId, mockEnroll, contextCallback, true);
207 ASSERT_NE(context, nullptr);
208 ASSERT_EQ(context->Start(), false);
209 }
210
211 HWTEST_F(EnrollContextTest, EnrollContextTest_Start_005, TestSize.Level0)
212 {
213 static const uint64_t testContestId = 2;
214 static const uint64_t testScheduleId = 3;
215
216 std::shared_ptr<MockEnrollment> mockEnroll = Common::MakeShared<MockEnrollment>();
217 ASSERT_NE(mockEnroll, nullptr);
218 EXPECT_CALL(*mockEnroll, Start(_, _))
219 .Times(Exactly(1))
220 .WillOnce([](std::vector<std::shared_ptr<ScheduleNode>> &scheduleList,
__anon6505430c0502(std::vector<std::shared_ptr<ScheduleNode>> &scheduleList, std::shared_ptr<ScheduleNodeCallback> callback) 221 std::shared_ptr<ScheduleNodeCallback> callback) {
222 // Success
223 EXPECT_EQ(scheduleList.size(), 0U);
224 auto scheduleNode = Common::MakeShared<MockScheduleNode>();
225 EXPECT_NE(scheduleNode, nullptr);
226 EXPECT_CALL(*scheduleNode, StartSchedule()).Times(Exactly(1)).WillOnce(Return(true));
227 EXPECT_CALL(*scheduleNode, GetScheduleId()).WillRepeatedly(Return(testScheduleId));
228 scheduleList.push_back(scheduleNode);
229 return true;
230 });
231 std::shared_ptr<ContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
232 ASSERT_NE(contextCallback, nullptr);
233 std::shared_ptr<Context> context =
234 Common::MakeShared<EnrollContext>(testContestId, mockEnroll, contextCallback, true);
235 ASSERT_NE(context, nullptr);
236 ASSERT_EQ(context->Start(), true);
237 ASSERT_EQ(context->Start(), false);
238 auto node = context->GetScheduleNode(testScheduleId);
239 ASSERT_NE(node, nullptr);
240 node = context->GetScheduleNode(testScheduleId + 1);
241 ASSERT_EQ(node, nullptr);
242 }
243
244 HWTEST_F(EnrollContextTest, EnrollContextTest_Stop_001, TestSize.Level0)
245 {
246 static const uint64_t testContestId = 2;
247
248 std::shared_ptr<MockEnrollment> mockEnroll = Common::MakeShared<MockEnrollment>();
249 ASSERT_NE(mockEnroll, nullptr);
250 EXPECT_CALL(*mockEnroll, GetLatestError()).Times(1);
__anon6505430c0602() 251 EXPECT_CALL(*mockEnroll, Cancel()).Times(Exactly(1)).WillOnce([]() {
252 // Error: enrollment cancel fail
253 return false;
254 });
255 std::shared_ptr<ContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
256 ASSERT_NE(contextCallback, nullptr);
257 std::shared_ptr<Context> context =
258 Common::MakeShared<EnrollContext>(testContestId, mockEnroll, contextCallback, true);
259 ASSERT_NE(context, nullptr);
260 ASSERT_EQ(context->Stop(), false);
261 }
262
263 HWTEST_F(EnrollContextTest, EnrollContextTest_Stop_002, TestSize.Level0)
264 {
265 static const uint64_t testContestId = 2;
266 std::shared_ptr<MockEnrollment> mockEnroll = Common::MakeShared<MockEnrollment>();
267 ASSERT_NE(mockEnroll, nullptr);
__anon6505430c0702() 268 EXPECT_CALL(*mockEnroll, Cancel()).Times(Exactly(1)).WillOnce([]() { return true; });
269 std::shared_ptr<ContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
270 ASSERT_NE(contextCallback, nullptr);
271 std::shared_ptr<Context> context =
272 Common::MakeShared<EnrollContext>(testContestId, mockEnroll, contextCallback, true);
273 ASSERT_NE(context, nullptr);
274 ASSERT_EQ(context->Stop(), true);
275 }
276
277 HWTEST_F(EnrollContextTest, EnrollContextTest_Stop_003, TestSize.Level0)
278 {
279 static const uint64_t testContestId = 2;
280
281 std::shared_ptr<MockEnrollment> mockEnroll = Common::MakeShared<MockEnrollment>();
282 ASSERT_NE(mockEnroll, nullptr);
283 EXPECT_CALL(*mockEnroll, Start(_, _)).Times(1);
284 ON_CALL(*mockEnroll, Start)
285 .WillByDefault(
286 [](std::vector<std::shared_ptr<ScheduleNode>> &scheduleList,
__anon6505430c0802(std::vector<std::shared_ptr<ScheduleNode>> &scheduleList, std::shared_ptr<ScheduleNodeCallback> callback) 287 std::shared_ptr<ScheduleNodeCallback> callback) {
288 scheduleList.push_back(nullptr);
289 return true;
290 }
291 );
__anon6505430c0902() 292 EXPECT_CALL(*mockEnroll, Cancel()).Times(Exactly(1)).WillOnce([]() {
293 return true;
294 });
295 std::shared_ptr<ContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
296 ASSERT_NE(contextCallback, nullptr);
297 std::shared_ptr<Context> context =
298 Common::MakeShared<EnrollContext>(testContestId, mockEnroll, contextCallback, true);
299 ASSERT_NE(context, nullptr);
300 ASSERT_EQ(context->Start(), false);
301 ASSERT_EQ(context->Stop(), true);
302 }
303
304 HWTEST_F(EnrollContextTest, EnrollContextTest_Stop_004, TestSize.Level0)
305 {
306 static const uint64_t testContestId = 2;
307
308 std::shared_ptr<MockEnrollment> mockEnroll = Common::MakeShared<MockEnrollment>();
309 ASSERT_NE(mockEnroll, nullptr);
310 EXPECT_CALL(*mockEnroll, Start(_, _)).Times(1);
311 ON_CALL(*mockEnroll, Start)
312 .WillByDefault(
313 [](std::vector<std::shared_ptr<ScheduleNode>> &scheduleList,
__anon6505430c0a02(std::vector<std::shared_ptr<ScheduleNode>> &scheduleList, std::shared_ptr<ScheduleNodeCallback> callback) 314 std::shared_ptr<ScheduleNodeCallback> callback) {
315 auto scheduleNode = Common::MakeShared<MockScheduleNode>();
316 EXPECT_NE(scheduleNode, nullptr);
317 EXPECT_CALL(*scheduleNode, StartSchedule()).Times(1);
318 EXPECT_CALL(*scheduleNode, StopSchedule()).Times(1);
319 scheduleList.push_back(scheduleNode);
320 return true;
321 }
322 );
323 EXPECT_CALL(*mockEnroll, GetLatestError()).Times(1);
__anon6505430c0b02() 324 EXPECT_CALL(*mockEnroll, Cancel()).Times(Exactly(1)).WillOnce([]() {
325 return false;
326 });
327 std::shared_ptr<ContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
328 ASSERT_NE(contextCallback, nullptr);
329 std::shared_ptr<Context> context =
330 Common::MakeShared<EnrollContext>(testContestId, mockEnroll, contextCallback, true);
331 ASSERT_NE(context, nullptr);
332 ASSERT_EQ(context->Start(), false);
333 ASSERT_EQ(context->Stop(), false);
334 }
335
336 HWTEST_F(EnrollContextTest, EnrollContextTest_OnScheduleStarted, TestSize.Level0)
337 {
338 static const uint64_t testContestId = 2;
339
340 std::shared_ptr<MockEnrollment> mockEnroll = Common::MakeShared<MockEnrollment>();
341 ASSERT_NE(mockEnroll, nullptr);
342 std::shared_ptr<ContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
343 ASSERT_NE(contextCallback, nullptr);
344
345 std::shared_ptr<ScheduleNodeCallback> nodeCallback =
346 Common::MakeShared<EnrollContext>(testContestId, mockEnroll, contextCallback, true);
347 ASSERT_NE(nodeCallback, nullptr);
348 nodeCallback->OnScheduleStarted();
349 }
350
351 HWTEST_F(EnrollContextTest, EnrollContextTest_OnScheduleProcessed, TestSize.Level0)
352 {
353 static const uint64_t testContestId = 2;
354 const ExecutorRole testRole = static_cast<ExecutorRole>(3);
355 const int32_t testModuleType = 4;
356 const std::vector<uint8_t> testAcquireMsg = {4, 5, 6};
357
358 std::shared_ptr<MockEnrollment> mockEnroll = Common::MakeShared<MockEnrollment>();
359 ASSERT_NE(mockEnroll, nullptr);
360 auto contextCallback = Common::MakeShared<MockContextCallback>();
361 ASSERT_NE(contextCallback, nullptr);
362 EXPECT_CALL(*contextCallback, OnAcquireInfo(_, _, _))
363 .WillOnce(
__anon6505430c0c02(ExecutorRole src, int32_t moduleType, const std::vector<uint8_t> &acquireMsg) 364 [](ExecutorRole src, int32_t moduleType, const std::vector<uint8_t> &acquireMsg) {
365 EXPECT_EQ(moduleType, 4);
366 }
367 );
368
369 std::shared_ptr<ScheduleNodeCallback> nodeCallback =
370 Common::MakeShared<EnrollContext>(testContestId, mockEnroll, contextCallback, true);
371 ASSERT_NE(nodeCallback, nullptr);
372 nodeCallback->OnScheduleProcessed(testRole, testModuleType, testAcquireMsg);
373 }
374
375 HWTEST_F(EnrollContextTest, EnrollContextTest_OnScheduleStoped_001, TestSize.Level0)
376 {
377 static const uint64_t testContestId = 2;
378 static const int32_t testResultCode = 7;
379
380 std::shared_ptr<MockEnrollment> mockEnroll = Common::MakeShared<MockEnrollment>();
381 ASSERT_NE(mockEnroll, nullptr);
382 std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
383 ASSERT_NE(contextCallback, nullptr);
384 EXPECT_CALL(*contextCallback, OnResult(_, _))
385 .Times(Exactly(1))
__anon6505430c0d02(int32_t resultCode, const Attributes &finalResult) 386 .WillOnce([](int32_t resultCode, const Attributes &finalResult) {
387 EXPECT_EQ(resultCode, testResultCode);
388 });
389
390 std::shared_ptr<ScheduleNodeCallback> nodeCallback =
391 Common::MakeShared<EnrollContext>(testContestId, mockEnroll, contextCallback, true);
392 ASSERT_NE(nodeCallback, nullptr);
393 // Error: result is null when testResultCode is not success
394 std::shared_ptr<Attributes> result = nullptr;
395 nodeCallback->OnScheduleStoped(testResultCode, result);
396 }
397
398 HWTEST_F(EnrollContextTest, EnrollContextTest_OnScheduleStoped_002, TestSize.Level0)
399 {
400 static const uint64_t testContestId = 2;
401 static const int32_t testResultCode = ResultCode::SUCCESS;
402
403 std::shared_ptr<MockEnrollment> mockEnroll = Common::MakeShared<MockEnrollment>();
404 ASSERT_NE(mockEnroll, nullptr);
405 std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
406 ASSERT_NE(contextCallback, nullptr);
407 EXPECT_CALL(*contextCallback, OnResult(_, _))
408 .Times(Exactly(1))
__anon6505430c0e02(int32_t resultCode, const Attributes &finalResult) 409 .WillOnce([](int32_t resultCode, const Attributes &finalResult) {
410 EXPECT_EQ(resultCode, ResultCode::GENERAL_ERROR);
411 });
412
413 std::shared_ptr<ScheduleNodeCallback> nodeCallback =
414 Common::MakeShared<EnrollContext>(testContestId, mockEnroll, contextCallback, true);
415 ASSERT_NE(nodeCallback, nullptr);
416 // Error: result is null when testResultCode is success
417 std::shared_ptr<Attributes> result = nullptr;
418 nodeCallback->OnScheduleStoped(testResultCode, result);
419 }
420
421 HWTEST_F(EnrollContextTest, EnrollContextTest_OnScheduleStoped_003, TestSize.Level0)
422 {
423 static const uint64_t testContestId = 2;
424 static const int32_t testResultCode = ResultCode::SUCCESS;
425
426 std::shared_ptr<MockEnrollment> mockEnroll = Common::MakeShared<MockEnrollment>();
427 ASSERT_NE(mockEnroll, nullptr);
428 std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
429 ASSERT_NE(contextCallback, nullptr);
430 EXPECT_CALL(*contextCallback, OnResult(_, _))
431 .Times(Exactly(1))
__anon6505430c0f02(int32_t resultCode, const Attributes &finalResult) 432 .WillOnce([](int32_t resultCode, const Attributes &finalResult) {
433 EXPECT_EQ(resultCode, ResultCode::GENERAL_ERROR);
434 });
435
436 std::shared_ptr<ScheduleNodeCallback> nodeCallback =
437 Common::MakeShared<EnrollContext>(testContestId, mockEnroll, contextCallback, true);
438 ASSERT_NE(nodeCallback, nullptr);
439 // Error: ATTR_RESULT_CODE is not set
440 std::shared_ptr<Attributes> result = Common::MakeShared<Attributes>();
441 ASSERT_NE(result, nullptr);
442 nodeCallback->OnScheduleStoped(testResultCode, result);
443 }
444
445 HWTEST_F(EnrollContextTest, EnrollContextTest_OnScheduleStoped_004, TestSize.Level0)
446 {
447 static const uint64_t testContestId = 2;
448 static const int32_t testResultCode = ResultCode::SUCCESS;
449 static const std::vector<uint8_t> testScheduleResult = {3, 4, 5, 6};
450
451 std::shared_ptr<MockEnrollment> mockEnroll = Common::MakeShared<MockEnrollment>();
452 ASSERT_NE(mockEnroll, nullptr);
453 EXPECT_CALL(*mockEnroll, GetLatestError()).Times(1);
454 EXPECT_CALL(*mockEnroll, Update(_, _, _, _, _))
455 .Times(Exactly(1))
456 .WillOnce([](const std::vector<uint8_t> &scheduleResult, uint64_t &credentialId,
457 std::shared_ptr<CredentialInfoInterface> &info, std::shared_ptr<UpdatePinParamInterface> &pinInfo,
__anon6505430c1002(const std::vector<uint8_t> &scheduleResult, uint64_t &credentialId, std::shared_ptr<CredentialInfoInterface> &info, std::shared_ptr<UpdatePinParamInterface> &pinInfo, std::optional<uint64_t> &secUserId) 458 std::optional<uint64_t> &secUserId) {
459 EXPECT_EQ(scheduleResult, testScheduleResult);
460 auto pinInfoTemp004 = Common::MakeShared<MockUpdatePinParamInfo>();
461 EXPECT_NE(pinInfoTemp004, nullptr);
462 std::vector<uint8_t> test = {1, 2, 3};
463 EXPECT_CALL(*pinInfoTemp004, GetOldCredentialId()).WillOnce(Return(0));
464 EXPECT_CALL(*pinInfoTemp004, GetOldRootSecret()).WillOnce(Return(test));
465 EXPECT_CALL(*pinInfoTemp004, GetRootSecret()).WillOnce(Return(test));
466 EXPECT_CALL(*pinInfoTemp004, GetAuthToken()).WillOnce(Return(test));
467 pinInfo = pinInfoTemp004;
468 return false;
469 });
470 std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
471 ASSERT_NE(contextCallback, nullptr);
472 EXPECT_CALL(*contextCallback, OnResult(_, _))
473 .Times(Exactly(1))
__anon6505430c1102(int32_t resultCode, const Attributes &finalResult) 474 .WillOnce([](int32_t resultCode, const Attributes &finalResult) {
475 EXPECT_EQ(resultCode, ResultCode::GENERAL_ERROR);
476 });
477
478 std::shared_ptr<ScheduleNodeCallback> nodeCallback =
479 Common::MakeShared<EnrollContext>(testContestId, mockEnroll, contextCallback, true);
480 ASSERT_NE(nodeCallback, nullptr);
481 // Error: enroll_->Update return false
482 std::shared_ptr<Attributes> result = Common::MakeShared<Attributes>();
483 ASSERT_NE(result, nullptr);
484 bool ret1 = result->SetUint8ArrayValue(Attributes::ATTR_SIGNATURE, testScheduleResult);
485 ASSERT_EQ(ret1, true);
486
487 bool ret2 = result->SetUint8ArrayValue(Attributes::ATTR_RESULT, testScheduleResult);
488 ASSERT_EQ(ret2, true);
489 nodeCallback->OnScheduleStoped(testResultCode, result);
490 }
491
492 HWTEST_F(EnrollContextTest, EnrollContextTest_OnScheduleStoped_005, TestSize.Level0)
493 {
494 static const uint64_t testContestId = 2;
495 static const std::vector<uint8_t> testScheduleResult = {3, 4, 5, 6};
496 static const uint64_t testCredentialId = 7;
497
498 std::shared_ptr<MockEnrollment> mockEnroll = Common::MakeShared<MockEnrollment>();
499 ASSERT_NE(mockEnroll, nullptr);
500 EXPECT_CALL(*mockEnroll, Update(_, _, _, _, _))
501 .Times(Exactly(1))
502 .WillOnce([](const std::vector<uint8_t> &scheduleResult, uint64_t &credentialId,
503 std::shared_ptr<CredentialInfoInterface> &info, std::shared_ptr<UpdatePinParamInterface> &pinInfo,
__anon6505430c1202(const std::vector<uint8_t> &scheduleResult, uint64_t &credentialId, std::shared_ptr<CredentialInfoInterface> &info, std::shared_ptr<UpdatePinParamInterface> &pinInfo, std::optional<uint64_t> &secUserId) 504 std::optional<uint64_t> &secUserId) {
505 EXPECT_EQ(scheduleResult, testScheduleResult);
506 credentialId = testCredentialId;
507 info = nullptr;
508 auto pinInfoTemp005 = Common::MakeShared<MockUpdatePinParamInfo>();
509 EXPECT_NE(pinInfoTemp005, nullptr);
510 std::vector<uint8_t> test = {1, 2, 3};
511 EXPECT_CALL(*pinInfoTemp005, GetOldCredentialId()).WillOnce(Return(0));
512 EXPECT_CALL(*pinInfoTemp005, GetOldRootSecret()).WillOnce(Return(test));
513 EXPECT_CALL(*pinInfoTemp005, GetRootSecret()).WillOnce(Return(test));
514 EXPECT_CALL(*pinInfoTemp005, GetAuthToken()).WillOnce(Return(test));
515 pinInfo = pinInfoTemp005;
516 return true;
517 });
518 std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
519 ASSERT_NE(contextCallback, nullptr);
520 EXPECT_CALL(*contextCallback, OnResult(_, _))
521 .Times(Exactly(1))
__anon6505430c1302(int32_t resultCode, const Attributes &finalResult) 522 .WillOnce([](int32_t resultCode, const Attributes &finalResult) {
523 EXPECT_EQ(resultCode, ResultCode::SUCCESS);
524 uint64_t credentialId;
525 bool ret = finalResult.GetUint64Value(Attributes::ATTR_CREDENTIAL_ID, credentialId);
526 EXPECT_EQ(ret, true);
527 EXPECT_EQ(testCredentialId, credentialId);
528 });
529
530 std::shared_ptr<ScheduleNodeCallback> nodeCallback =
531 Common::MakeShared<EnrollContext>(testContestId, mockEnroll, contextCallback, true);
532 ASSERT_NE(nodeCallback, nullptr);
533 // Success
534 std::shared_ptr<Attributes> result = Common::MakeShared<Attributes>();
535 ASSERT_NE(result, nullptr);
536 bool ret1 = result->SetUint8ArrayValue(Attributes::ATTR_SIGNATURE, testScheduleResult);
537 ASSERT_EQ(ret1, true);
538
539 bool ret2 = result->SetUint8ArrayValue(Attributes::ATTR_RESULT, testScheduleResult);
540 ASSERT_EQ(ret2, true);
541 nodeCallback->OnScheduleStoped(ResultCode::SUCCESS, result);
542 }
543
544 HWTEST_F(EnrollContextTest, EnrollContextTest_OnScheduleStoped_006, TestSize.Level0)
545 {
546 static const uint64_t testContestId = 2;
547 static const std::vector<uint8_t> testScheduleResult = {3, 4, 5, 6};
548 static const uint64_t testCredentialId = 7;
549
550 std::shared_ptr<MockEnrollment> mockEnroll = Common::MakeShared<MockEnrollment>();
551 ASSERT_NE(mockEnroll, nullptr);
552 EXPECT_CALL(*mockEnroll, Update(_, _, _, _, _))
553 .Times(Exactly(1))
554 .WillOnce([](const std::vector<uint8_t> &scheduleResult, uint64_t &credentialId,
555 std::shared_ptr<CredentialInfoInterface> &info, std::shared_ptr<UpdatePinParamInterface> &pinInfo,
__anon6505430c1402(const std::vector<uint8_t> &scheduleResult, uint64_t &credentialId, std::shared_ptr<CredentialInfoInterface> &info, std::shared_ptr<UpdatePinParamInterface> &pinInfo, std::optional<uint64_t> &secUserId) 556 std::optional<uint64_t> &secUserId) {
557 EXPECT_EQ(scheduleResult, testScheduleResult);
558 credentialId = testCredentialId;
559 auto credInfo = Common::MakeShared<MockCredentialInfo>();
560 EXPECT_NE(credInfo, nullptr);
561 info = credInfo;
562 auto pinInfoTemp006 = Common::MakeShared<MockUpdatePinParamInfo>();
563 EXPECT_NE(pinInfoTemp006, nullptr);
564 std::vector<uint8_t> test = {1, 2, 3};
565 EXPECT_CALL(*pinInfoTemp006, GetOldCredentialId()).WillOnce(Return(0));
566 EXPECT_CALL(*pinInfoTemp006, GetOldRootSecret()).WillOnce(Return(test));
567 EXPECT_CALL(*pinInfoTemp006, GetRootSecret()).WillOnce(Return(test));
568 EXPECT_CALL(*pinInfoTemp006, GetAuthToken()).WillOnce(Return(test));
569 pinInfo = pinInfoTemp006;
570 secUserId = 0;
571 return true;
572 });
573 std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
574 ASSERT_NE(contextCallback, nullptr);
575 EXPECT_CALL(*contextCallback, OnResult(_, _)).Times(1);
576
577 std::shared_ptr<ScheduleNodeCallback> nodeCallback =
578 Common::MakeShared<EnrollContext>(testContestId, mockEnroll, contextCallback, true);
579 ASSERT_NE(nodeCallback, nullptr);
580 // Success
581 std::shared_ptr<Attributes> result = Common::MakeShared<Attributes>();
582 ASSERT_NE(result, nullptr);
583 bool ret1 = result->SetUint8ArrayValue(Attributes::ATTR_SIGNATURE, testScheduleResult);
584 ASSERT_EQ(ret1, true);
585
586 bool ret2 = result->SetUint8ArrayValue(Attributes::ATTR_RESULT, testScheduleResult);
587 ASSERT_EQ(ret2, true);
588 nodeCallback->OnScheduleStoped(ResultCode::SUCCESS, result);
589 }
590 } // namespace UserAuth
591 } // namespace UserIam
592 } // namespace OHOS
593