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);
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);
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);
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,
__anon57dbc4040102(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 = Common::MakeShared<EnrollContext>(testContestId, mockEnroll, contextCallback);
136 ASSERT_NE(context, nullptr);
137 ASSERT_EQ(context->Start(), false);
138 }
139
140 HWTEST_F(EnrollContextTest, EnrollContextTest_Start_002, TestSize.Level0)
141 {
142 static const uint64_t testContestId = 2;
143
144 std::shared_ptr<MockEnrollment> mockEnroll = Common::MakeShared<MockEnrollment>();
145 ASSERT_NE(mockEnroll, nullptr);
146 EXPECT_CALL(*mockEnroll, Start(_, _))
147 .Times(Exactly(1))
148 .WillOnce([](std::vector<std::shared_ptr<ScheduleNode>> &scheduleList,
__anon57dbc4040202(std::vector<std::shared_ptr<ScheduleNode>> &scheduleList, std::shared_ptr<ScheduleNodeCallback> callback) 149 std::shared_ptr<ScheduleNodeCallback> callback) {
150 // Error: scheduleNodeList size = 0
151 return true;
152 });
153 std::shared_ptr<ContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
154 ASSERT_NE(contextCallback, nullptr);
155 std::shared_ptr<Context> context = Common::MakeShared<EnrollContext>(testContestId, mockEnroll, contextCallback);
156 ASSERT_NE(context, nullptr);
157 ASSERT_EQ(context->Start(), false);
158 }
159
160 HWTEST_F(EnrollContextTest, EnrollContextTest_Start_003, TestSize.Level0)
161 {
162 static const uint64_t testContestId = 2;
163
164 std::shared_ptr<MockEnrollment> mockEnroll = Common::MakeShared<MockEnrollment>();
165 ASSERT_NE(mockEnroll, nullptr);
166 EXPECT_CALL(*mockEnroll, Start(_, _))
167 .Times(Exactly(1))
168 .WillOnce([](std::vector<std::shared_ptr<ScheduleNode>> &scheduleList,
__anon57dbc4040302(std::vector<std::shared_ptr<ScheduleNode>> &scheduleList, std::shared_ptr<ScheduleNodeCallback> callback) 169 std::shared_ptr<ScheduleNodeCallback> callback) {
170 // Error: scheduleNodeList size = 2
171 scheduleList.push_back(Common::MakeShared<MockScheduleNode>());
172 scheduleList.push_back(Common::MakeShared<MockScheduleNode>());
173 return true;
174 });
175 std::shared_ptr<ContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
176 ASSERT_NE(contextCallback, nullptr);
177 std::shared_ptr<Context> context = Common::MakeShared<EnrollContext>(testContestId, mockEnroll, contextCallback);
178 ASSERT_NE(context, nullptr);
179 ASSERT_EQ(context->Start(), false);
180 }
181
182 HWTEST_F(EnrollContextTest, EnrollContextTest_Start_004, TestSize.Level0)
183 {
184 static const uint64_t testContestId = 2;
185
186 std::shared_ptr<MockEnrollment> mockEnroll = Common::MakeShared<MockEnrollment>();
187 ASSERT_NE(mockEnroll, nullptr);
188 EXPECT_CALL(*mockEnroll, Start(_, _))
189 .Times(Exactly(1))
190 .WillOnce([](std::vector<std::shared_ptr<ScheduleNode>> &scheduleList,
__anon57dbc4040402(std::vector<std::shared_ptr<ScheduleNode>> &scheduleList, std::shared_ptr<ScheduleNodeCallback> callback) 191 std::shared_ptr<ScheduleNodeCallback> callback) {
192 // Error: schedule node start fail
193 auto scheduleNode = Common::MakeShared<MockScheduleNode>();
194 EXPECT_NE(scheduleNode, nullptr);
195
196 EXPECT_CALL(*scheduleNode, StartSchedule()).Times(Exactly(1)).WillOnce(Return(false));
197 scheduleList.push_back(scheduleNode);
198 return true;
199 });
200 std::shared_ptr<ContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
201 ASSERT_NE(contextCallback, nullptr);
202 std::shared_ptr<Context> context = Common::MakeShared<EnrollContext>(testContestId, mockEnroll, contextCallback);
203 ASSERT_NE(context, nullptr);
204 ASSERT_EQ(context->Start(), false);
205 }
206
207 HWTEST_F(EnrollContextTest, EnrollContextTest_Start_005, TestSize.Level0)
208 {
209 static const uint64_t testContestId = 2;
210 static const uint64_t testScheduleId = 3;
211
212 std::shared_ptr<MockEnrollment> mockEnroll = Common::MakeShared<MockEnrollment>();
213 ASSERT_NE(mockEnroll, nullptr);
214 EXPECT_CALL(*mockEnroll, Start(_, _))
215 .Times(Exactly(1))
216 .WillOnce([](std::vector<std::shared_ptr<ScheduleNode>> &scheduleList,
__anon57dbc4040502(std::vector<std::shared_ptr<ScheduleNode>> &scheduleList, std::shared_ptr<ScheduleNodeCallback> callback) 217 std::shared_ptr<ScheduleNodeCallback> callback) {
218 // Success
219 EXPECT_EQ(scheduleList.size(), 0U);
220 auto scheduleNode = Common::MakeShared<MockScheduleNode>();
221 EXPECT_NE(scheduleNode, nullptr);
222 EXPECT_CALL(*scheduleNode, StartSchedule()).Times(Exactly(1)).WillOnce(Return(true));
223 EXPECT_CALL(*scheduleNode, GetScheduleId()).WillRepeatedly(Return(testScheduleId));
224 scheduleList.push_back(scheduleNode);
225 return true;
226 });
227 std::shared_ptr<ContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
228 ASSERT_NE(contextCallback, nullptr);
229 std::shared_ptr<Context> context = Common::MakeShared<EnrollContext>(testContestId, mockEnroll, contextCallback);
230 ASSERT_NE(context, nullptr);
231 ASSERT_EQ(context->Start(), true);
232 ASSERT_EQ(context->Start(), false);
233 auto node = context->GetScheduleNode(testScheduleId);
234 ASSERT_NE(node, nullptr);
235 node = context->GetScheduleNode(testScheduleId + 1);
236 ASSERT_EQ(node, nullptr);
237 }
238
239 HWTEST_F(EnrollContextTest, EnrollContextTest_Stop_001, TestSize.Level0)
240 {
241 static const uint64_t testContestId = 2;
242
243 std::shared_ptr<MockEnrollment> mockEnroll = Common::MakeShared<MockEnrollment>();
244 ASSERT_NE(mockEnroll, nullptr);
245 EXPECT_CALL(*mockEnroll, GetLatestError()).Times(1);
__anon57dbc4040602() 246 EXPECT_CALL(*mockEnroll, Cancel()).Times(Exactly(1)).WillOnce([]() {
247 // Error: enrollment cancel fail
248 return false;
249 });
250 std::shared_ptr<ContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
251 ASSERT_NE(contextCallback, nullptr);
252 std::shared_ptr<Context> context = Common::MakeShared<EnrollContext>(testContestId, mockEnroll, contextCallback);
253 ASSERT_NE(context, nullptr);
254 ASSERT_EQ(context->Stop(), false);
255 }
256
257 HWTEST_F(EnrollContextTest, EnrollContextTest_Stop_002, TestSize.Level0)
258 {
259 static const uint64_t testContestId = 2;
260 std::shared_ptr<MockEnrollment> mockEnroll = Common::MakeShared<MockEnrollment>();
261 ASSERT_NE(mockEnroll, nullptr);
__anon57dbc4040702() 262 EXPECT_CALL(*mockEnroll, Cancel()).Times(Exactly(1)).WillOnce([]() { return true; });
263 std::shared_ptr<ContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
264 ASSERT_NE(contextCallback, nullptr);
265 std::shared_ptr<Context> context = Common::MakeShared<EnrollContext>(testContestId, mockEnroll, contextCallback);
266 ASSERT_NE(context, nullptr);
267 ASSERT_EQ(context->Stop(), true);
268 }
269
270 HWTEST_F(EnrollContextTest, EnrollContextTest_Stop_003, TestSize.Level0)
271 {
272 static const uint64_t testContestId = 2;
273
274 std::shared_ptr<MockEnrollment> mockEnroll = Common::MakeShared<MockEnrollment>();
275 ASSERT_NE(mockEnroll, nullptr);
276 EXPECT_CALL(*mockEnroll, Start(_, _)).Times(1);
277 ON_CALL(*mockEnroll, Start)
278 .WillByDefault(
279 [](std::vector<std::shared_ptr<ScheduleNode>> &scheduleList,
__anon57dbc4040802(std::vector<std::shared_ptr<ScheduleNode>> &scheduleList, std::shared_ptr<ScheduleNodeCallback> callback) 280 std::shared_ptr<ScheduleNodeCallback> callback) {
281 scheduleList.push_back(nullptr);
282 return true;
283 }
284 );
__anon57dbc4040902() 285 EXPECT_CALL(*mockEnroll, Cancel()).Times(Exactly(1)).WillOnce([]() {
286 return true;
287 });
288 std::shared_ptr<ContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
289 ASSERT_NE(contextCallback, nullptr);
290 std::shared_ptr<Context> context = Common::MakeShared<EnrollContext>(testContestId, mockEnroll, contextCallback);
291 ASSERT_NE(context, nullptr);
292 ASSERT_EQ(context->Start(), false);
293 ASSERT_EQ(context->Stop(), true);
294 }
295
296 HWTEST_F(EnrollContextTest, EnrollContextTest_Stop_004, TestSize.Level0)
297 {
298 static const uint64_t testContestId = 2;
299
300 std::shared_ptr<MockEnrollment> mockEnroll = Common::MakeShared<MockEnrollment>();
301 ASSERT_NE(mockEnroll, nullptr);
302 EXPECT_CALL(*mockEnroll, Start(_, _)).Times(1);
303 ON_CALL(*mockEnroll, Start)
304 .WillByDefault(
305 [](std::vector<std::shared_ptr<ScheduleNode>> &scheduleList,
__anon57dbc4040a02(std::vector<std::shared_ptr<ScheduleNode>> &scheduleList, std::shared_ptr<ScheduleNodeCallback> callback) 306 std::shared_ptr<ScheduleNodeCallback> callback) {
307 auto scheduleNode = Common::MakeShared<MockScheduleNode>();
308 EXPECT_NE(scheduleNode, nullptr);
309 EXPECT_CALL(*scheduleNode, StartSchedule()).Times(1);
310 EXPECT_CALL(*scheduleNode, StopSchedule()).Times(1);
311 scheduleList.push_back(scheduleNode);
312 return true;
313 }
314 );
315 EXPECT_CALL(*mockEnroll, GetLatestError()).Times(1);
__anon57dbc4040b02() 316 EXPECT_CALL(*mockEnroll, Cancel()).Times(Exactly(1)).WillOnce([]() {
317 return false;
318 });
319 std::shared_ptr<ContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
320 ASSERT_NE(contextCallback, nullptr);
321 std::shared_ptr<Context> context = Common::MakeShared<EnrollContext>(testContestId, mockEnroll, contextCallback);
322 ASSERT_NE(context, nullptr);
323 ASSERT_EQ(context->Start(), false);
324 ASSERT_EQ(context->Stop(), false);
325 }
326
327 HWTEST_F(EnrollContextTest, EnrollContextTest_OnScheduleStarted, TestSize.Level0)
328 {
329 static const uint64_t testContestId = 2;
330
331 std::shared_ptr<MockEnrollment> mockEnroll = Common::MakeShared<MockEnrollment>();
332 ASSERT_NE(mockEnroll, nullptr);
333 std::shared_ptr<ContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
334 ASSERT_NE(contextCallback, nullptr);
335
336 std::shared_ptr<ScheduleNodeCallback> nodeCallback =
337 Common::MakeShared<EnrollContext>(testContestId, mockEnroll, contextCallback);
338 ASSERT_NE(nodeCallback, nullptr);
339 nodeCallback->OnScheduleStarted();
340 }
341
342 HWTEST_F(EnrollContextTest, EnrollContextTest_OnScheduleProcessed, TestSize.Level0)
343 {
344 static const uint64_t testContestId = 2;
345 const ExecutorRole testRole = static_cast<ExecutorRole>(3);
346 const int32_t testModuleType = 4;
347 const std::vector<uint8_t> testAcquireMsg = {4, 5, 6};
348
349 std::shared_ptr<MockEnrollment> mockEnroll = Common::MakeShared<MockEnrollment>();
350 ASSERT_NE(mockEnroll, nullptr);
351 auto contextCallback = Common::MakeShared<MockContextCallback>();
352 ASSERT_NE(contextCallback, nullptr);
353 EXPECT_CALL(*contextCallback, OnAcquireInfo(_, _, _))
354 .WillOnce(
__anon57dbc4040c02(ExecutorRole src, int32_t moduleType, const std::vector<uint8_t> &acquireMsg) 355 [](ExecutorRole src, int32_t moduleType, const std::vector<uint8_t> &acquireMsg) {
356 EXPECT_EQ(moduleType, 4);
357 }
358 );
359
360 std::shared_ptr<ScheduleNodeCallback> nodeCallback =
361 Common::MakeShared<EnrollContext>(testContestId, mockEnroll, contextCallback);
362 ASSERT_NE(nodeCallback, nullptr);
363 nodeCallback->OnScheduleProcessed(testRole, testModuleType, testAcquireMsg);
364 }
365
366 HWTEST_F(EnrollContextTest, EnrollContextTest_OnScheduleStoped_001, TestSize.Level0)
367 {
368 static const uint64_t testContestId = 2;
369 static const int32_t testResultCode = 7;
370
371 std::shared_ptr<MockEnrollment> mockEnroll = Common::MakeShared<MockEnrollment>();
372 ASSERT_NE(mockEnroll, nullptr);
373 std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
374 ASSERT_NE(contextCallback, nullptr);
375 EXPECT_CALL(*contextCallback, OnResult(_, _))
376 .Times(Exactly(1))
__anon57dbc4040d02(int32_t resultCode, const Attributes &finalResult) 377 .WillOnce([](int32_t resultCode, const Attributes &finalResult) {
378 EXPECT_EQ(resultCode, testResultCode);
379 });
380
381 std::shared_ptr<ScheduleNodeCallback> nodeCallback =
382 Common::MakeShared<EnrollContext>(testContestId, mockEnroll, contextCallback);
383 ASSERT_NE(nodeCallback, nullptr);
384 // Error: result is null when testResultCode is not success
385 std::shared_ptr<Attributes> result = nullptr;
386 nodeCallback->OnScheduleStoped(testResultCode, result);
387 }
388
389 HWTEST_F(EnrollContextTest, EnrollContextTest_OnScheduleStoped_002, TestSize.Level0)
390 {
391 static const uint64_t testContestId = 2;
392 static const int32_t testResultCode = ResultCode::SUCCESS;
393
394 std::shared_ptr<MockEnrollment> mockEnroll = Common::MakeShared<MockEnrollment>();
395 ASSERT_NE(mockEnroll, nullptr);
396 std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
397 ASSERT_NE(contextCallback, nullptr);
398 EXPECT_CALL(*contextCallback, OnResult(_, _))
399 .Times(Exactly(1))
__anon57dbc4040e02(int32_t resultCode, const Attributes &finalResult) 400 .WillOnce([](int32_t resultCode, const Attributes &finalResult) {
401 EXPECT_EQ(resultCode, ResultCode::GENERAL_ERROR);
402 });
403
404 std::shared_ptr<ScheduleNodeCallback> nodeCallback =
405 Common::MakeShared<EnrollContext>(testContestId, mockEnroll, contextCallback);
406 ASSERT_NE(nodeCallback, nullptr);
407 // Error: result is null when testResultCode is success
408 std::shared_ptr<Attributes> result = nullptr;
409 nodeCallback->OnScheduleStoped(testResultCode, result);
410 }
411
412 HWTEST_F(EnrollContextTest, EnrollContextTest_OnScheduleStoped_003, TestSize.Level0)
413 {
414 static const uint64_t testContestId = 2;
415 static const int32_t testResultCode = ResultCode::SUCCESS;
416
417 std::shared_ptr<MockEnrollment> mockEnroll = Common::MakeShared<MockEnrollment>();
418 ASSERT_NE(mockEnroll, nullptr);
419 std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
420 ASSERT_NE(contextCallback, nullptr);
421 EXPECT_CALL(*contextCallback, OnResult(_, _))
422 .Times(Exactly(1))
__anon57dbc4040f02(int32_t resultCode, const Attributes &finalResult) 423 .WillOnce([](int32_t resultCode, const Attributes &finalResult) {
424 EXPECT_EQ(resultCode, ResultCode::GENERAL_ERROR);
425 });
426
427 std::shared_ptr<ScheduleNodeCallback> nodeCallback =
428 Common::MakeShared<EnrollContext>(testContestId, mockEnroll, contextCallback);
429 ASSERT_NE(nodeCallback, nullptr);
430 // Error: ATTR_RESULT_CODE is not set
431 std::shared_ptr<Attributes> result = Common::MakeShared<Attributes>();
432 ASSERT_NE(result, nullptr);
433 nodeCallback->OnScheduleStoped(testResultCode, result);
434 }
435
436 HWTEST_F(EnrollContextTest, EnrollContextTest_OnScheduleStoped_004, TestSize.Level0)
437 {
438 static const uint64_t testContestId = 2;
439 static const int32_t testResultCode = ResultCode::SUCCESS;
440 static const std::vector<uint8_t> testScheduleResult = {3, 4, 5, 6};
441
442 std::shared_ptr<MockEnrollment> mockEnroll = Common::MakeShared<MockEnrollment>();
443 ASSERT_NE(mockEnroll, nullptr);
444 EXPECT_CALL(*mockEnroll, GetLatestError()).Times(1);
445 EXPECT_CALL(*mockEnroll, Update(_, _, _, _, _))
446 .Times(Exactly(1))
447 .WillOnce([](const std::vector<uint8_t> &scheduleResult, uint64_t &credentialId,
448 std::shared_ptr<CredentialInfoInterface> &info, std::shared_ptr<UpdatePinParamInterface> &pinInfo,
__anon57dbc4041002(const std::vector<uint8_t> &scheduleResult, uint64_t &credentialId, std::shared_ptr<CredentialInfoInterface> &info, std::shared_ptr<UpdatePinParamInterface> &pinInfo, std::optional<uint64_t> &secUserId) 449 std::optional<uint64_t> &secUserId) {
450 EXPECT_EQ(scheduleResult, testScheduleResult);
451 auto pinInfoTemp004 = Common::MakeShared<MockUpdatePinParamInfo>();
452 EXPECT_NE(pinInfoTemp004, nullptr);
453 std::vector<uint8_t> test = {1, 2, 3};
454 EXPECT_CALL(*pinInfoTemp004, GetOldCredentialId()).WillOnce(Return(0));
455 EXPECT_CALL(*pinInfoTemp004, GetOldRootSecret()).WillOnce(Return(test));
456 EXPECT_CALL(*pinInfoTemp004, GetRootSecret()).WillOnce(Return(test));
457 EXPECT_CALL(*pinInfoTemp004, GetAuthToken()).WillOnce(Return(test));
458 pinInfo = pinInfoTemp004;
459 return false;
460 });
461 std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
462 ASSERT_NE(contextCallback, nullptr);
463 EXPECT_CALL(*contextCallback, OnResult(_, _))
464 .Times(Exactly(1))
__anon57dbc4041102(int32_t resultCode, const Attributes &finalResult) 465 .WillOnce([](int32_t resultCode, const Attributes &finalResult) {
466 EXPECT_EQ(resultCode, ResultCode::GENERAL_ERROR);
467 });
468
469 std::shared_ptr<ScheduleNodeCallback> nodeCallback =
470 Common::MakeShared<EnrollContext>(testContestId, mockEnroll, contextCallback);
471 ASSERT_NE(nodeCallback, nullptr);
472 // Error: enroll_->Update return false
473 std::shared_ptr<Attributes> result = Common::MakeShared<Attributes>();
474 ASSERT_NE(result, nullptr);
475 bool ret1 = result->SetUint8ArrayValue(Attributes::ATTR_SIGNATURE, testScheduleResult);
476 ASSERT_EQ(ret1, true);
477
478 bool ret2 = result->SetUint8ArrayValue(Attributes::ATTR_RESULT, testScheduleResult);
479 ASSERT_EQ(ret2, true);
480 nodeCallback->OnScheduleStoped(testResultCode, result);
481 }
482
483 HWTEST_F(EnrollContextTest, EnrollContextTest_OnScheduleStoped_005, TestSize.Level0)
484 {
485 static const uint64_t testContestId = 2;
486 static const std::vector<uint8_t> testScheduleResult = {3, 4, 5, 6};
487 static const uint64_t testCredentialId = 7;
488
489 std::shared_ptr<MockEnrollment> mockEnroll = Common::MakeShared<MockEnrollment>();
490 ASSERT_NE(mockEnroll, nullptr);
491 EXPECT_CALL(*mockEnroll, Update(_, _, _, _, _))
492 .Times(Exactly(1))
493 .WillOnce([](const std::vector<uint8_t> &scheduleResult, uint64_t &credentialId,
494 std::shared_ptr<CredentialInfoInterface> &info, std::shared_ptr<UpdatePinParamInterface> &pinInfo,
__anon57dbc4041202(const std::vector<uint8_t> &scheduleResult, uint64_t &credentialId, std::shared_ptr<CredentialInfoInterface> &info, std::shared_ptr<UpdatePinParamInterface> &pinInfo, std::optional<uint64_t> &secUserId) 495 std::optional<uint64_t> &secUserId) {
496 EXPECT_EQ(scheduleResult, testScheduleResult);
497 credentialId = testCredentialId;
498 info = nullptr;
499 auto pinInfoTemp005 = Common::MakeShared<MockUpdatePinParamInfo>();
500 EXPECT_NE(pinInfoTemp005, nullptr);
501 std::vector<uint8_t> test = {1, 2, 3};
502 EXPECT_CALL(*pinInfoTemp005, GetOldCredentialId()).WillOnce(Return(0));
503 EXPECT_CALL(*pinInfoTemp005, GetOldRootSecret()).WillOnce(Return(test));
504 EXPECT_CALL(*pinInfoTemp005, GetRootSecret()).WillOnce(Return(test));
505 EXPECT_CALL(*pinInfoTemp005, GetAuthToken()).WillOnce(Return(test));
506 pinInfo = pinInfoTemp005;
507 return true;
508 });
509 std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
510 ASSERT_NE(contextCallback, nullptr);
511 EXPECT_CALL(*contextCallback, OnResult(_, _))
512 .Times(Exactly(1))
__anon57dbc4041302(int32_t resultCode, const Attributes &finalResult) 513 .WillOnce([](int32_t resultCode, const Attributes &finalResult) {
514 EXPECT_EQ(resultCode, ResultCode::SUCCESS);
515 uint64_t credentialId;
516 bool ret = finalResult.GetUint64Value(Attributes::ATTR_CREDENTIAL_ID, credentialId);
517 EXPECT_EQ(ret, true);
518 EXPECT_EQ(testCredentialId, credentialId);
519 });
520
521 std::shared_ptr<ScheduleNodeCallback> nodeCallback =
522 Common::MakeShared<EnrollContext>(testContestId, mockEnroll, contextCallback);
523 ASSERT_NE(nodeCallback, nullptr);
524 // Success
525 std::shared_ptr<Attributes> result = Common::MakeShared<Attributes>();
526 ASSERT_NE(result, nullptr);
527 bool ret1 = result->SetUint8ArrayValue(Attributes::ATTR_SIGNATURE, testScheduleResult);
528 ASSERT_EQ(ret1, true);
529
530 bool ret2 = result->SetUint8ArrayValue(Attributes::ATTR_RESULT, testScheduleResult);
531 ASSERT_EQ(ret2, true);
532 nodeCallback->OnScheduleStoped(ResultCode::SUCCESS, result);
533 }
534
535 HWTEST_F(EnrollContextTest, EnrollContextTest_OnScheduleStoped_006, TestSize.Level0)
536 {
537 static const uint64_t testContestId = 2;
538 static const std::vector<uint8_t> testScheduleResult = {3, 4, 5, 6};
539 static const uint64_t testCredentialId = 7;
540
541 std::shared_ptr<MockEnrollment> mockEnroll = Common::MakeShared<MockEnrollment>();
542 ASSERT_NE(mockEnroll, nullptr);
543 EXPECT_CALL(*mockEnroll, Update(_, _, _, _, _))
544 .Times(Exactly(1))
545 .WillOnce([](const std::vector<uint8_t> &scheduleResult, uint64_t &credentialId,
546 std::shared_ptr<CredentialInfoInterface> &info, std::shared_ptr<UpdatePinParamInterface> &pinInfo,
__anon57dbc4041402(const std::vector<uint8_t> &scheduleResult, uint64_t &credentialId, std::shared_ptr<CredentialInfoInterface> &info, std::shared_ptr<UpdatePinParamInterface> &pinInfo, std::optional<uint64_t> &secUserId) 547 std::optional<uint64_t> &secUserId) {
548 EXPECT_EQ(scheduleResult, testScheduleResult);
549 credentialId = testCredentialId;
550 auto credInfo = Common::MakeShared<MockCredentialInfo>();
551 EXPECT_NE(credInfo, nullptr);
552 info = credInfo;
553 auto pinInfoTemp006 = Common::MakeShared<MockUpdatePinParamInfo>();
554 EXPECT_NE(pinInfoTemp006, nullptr);
555 std::vector<uint8_t> test = {1, 2, 3};
556 EXPECT_CALL(*pinInfoTemp006, GetOldCredentialId()).WillOnce(Return(0));
557 EXPECT_CALL(*pinInfoTemp006, GetOldRootSecret()).WillOnce(Return(test));
558 EXPECT_CALL(*pinInfoTemp006, GetRootSecret()).WillOnce(Return(test));
559 EXPECT_CALL(*pinInfoTemp006, GetAuthToken()).WillOnce(Return(test));
560 pinInfo = pinInfoTemp006;
561 secUserId = 0;
562 return true;
563 });
564 std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
565 ASSERT_NE(contextCallback, nullptr);
566 EXPECT_CALL(*contextCallback, OnResult(_, _)).Times(1);
567
568 std::shared_ptr<ScheduleNodeCallback> nodeCallback =
569 Common::MakeShared<EnrollContext>(testContestId, mockEnroll, contextCallback);
570 ASSERT_NE(nodeCallback, nullptr);
571 // Success
572 std::shared_ptr<Attributes> result = Common::MakeShared<Attributes>();
573 ASSERT_NE(result, nullptr);
574 bool ret1 = result->SetUint8ArrayValue(Attributes::ATTR_SIGNATURE, testScheduleResult);
575 ASSERT_EQ(ret1, true);
576
577 bool ret2 = result->SetUint8ArrayValue(Attributes::ATTR_RESULT, testScheduleResult);
578 ASSERT_EQ(ret2, true);
579 nodeCallback->OnScheduleStoped(ResultCode::SUCCESS, result);
580 }
581 } // namespace UserAuth
582 } // namespace UserIam
583 } // namespace OHOS
584