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 "simple_auth_context.h"
17
18 #include <future>
19
20 #include "mock_authentication.h"
21 #include "mock_context.h"
22 #include "mock_resource_node.h"
23 #include "mock_schedule_node.h"
24 #include "schedule_node_impl.h"
25
26 using namespace testing;
27 using namespace testing::ext;
28 namespace OHOS {
29 namespace UserIam {
30 namespace UserAuth {
31 class SimpleAuthContextTest : public testing::Test {
32 public:
33 static void SetUpTestCase();
34
35 static void TearDownTestCase();
36
37 void SetUp() override;
38
39 void TearDown() override;
40 };
41
SetUpTestCase()42 void SimpleAuthContextTest::SetUpTestCase()
43 {
44 }
45
TearDownTestCase()46 void SimpleAuthContextTest::TearDownTestCase()
47 {
48 }
49
SetUp()50 void SimpleAuthContextTest::SetUp()
51 {
52 }
53
TearDown()54 void SimpleAuthContextTest::TearDown()
55 {
56 }
57
58 HWTEST_F(SimpleAuthContextTest, SimpleAuthContextTest_NullHdi, TestSize.Level0)
59 {
60 const uint64_t testContestId = 2;
61 const int32_t testResultCode = 7;
62 const auto finalResult = Common::MakeShared<Attributes>();
63 ASSERT_NE(finalResult, nullptr);
64 std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
65 ASSERT_NE(contextCallback, nullptr);
66 EXPECT_CALL(*contextCallback, OnResult(_, _)).Times(Exactly(1));
67 // Error: auth is null
68 std::shared_ptr<Authentication> auth = nullptr;
69
70 auto oriContext = Common::MakeShared<SimpleAuthContext>(testContestId, auth, contextCallback);
71 ASSERT_NE(oriContext, nullptr);
72 std::shared_ptr<Context> context = oriContext;
73 std::shared_ptr<ScheduleNodeCallback> nodeCallback = oriContext;
74
75 ASSERT_EQ(context->Start(), false);
76 ASSERT_EQ(context->Stop(), false);
77 nodeCallback->OnScheduleStoped(testResultCode, finalResult);
78 }
79
80 HWTEST_F(SimpleAuthContextTest, SimpleAuthContextTest_NullCallback, TestSize.Level0)
81 {
82 const uint64_t testContestId = 2;
83 const ExecutorRole testRole = static_cast<ExecutorRole>(3);
84 const int32_t testModuleType = 4;
85 const std::vector<uint8_t> testAcquireMsg = {4, 5, 6};
86 const int32_t testResultCode = 7;
87 const auto finalResult = Common::MakeShared<Attributes>();
88 ASSERT_NE(finalResult, nullptr);
89
90 std::shared_ptr<MockAuthentication> mockAuth = Common::MakeShared<MockAuthentication>();
91 ASSERT_NE(mockAuth, nullptr);
92 // Error: contextCallback is null
93 std::shared_ptr<ContextCallback> contextCallback = nullptr;
94
95 auto oriContext = Common::MakeShared<SimpleAuthContext>(testContestId, mockAuth, contextCallback);
96 ASSERT_NE(oriContext, nullptr);
97 std::shared_ptr<ScheduleNodeCallback> nodeCallback = oriContext;
98
99 nodeCallback->OnScheduleStarted();
100 nodeCallback->OnScheduleProcessed(testRole, testModuleType, testAcquireMsg);
101 nodeCallback->OnScheduleStoped(testResultCode, finalResult);
102 }
103
104 HWTEST_F(SimpleAuthContextTest, SimpleAuthContextTest_BasicInfo, TestSize.Level0)
105 {
106 const uint64_t testContestId = 2;
107
108 std::shared_ptr<MockAuthentication> mockAuth = Common::MakeShared<MockAuthentication>();
109 ASSERT_NE(mockAuth, nullptr);
110 std::shared_ptr<ContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
111 ASSERT_NE(contextCallback, nullptr);
112
113 auto oriContext = Common::MakeShared<SimpleAuthContext>(testContestId, mockAuth, contextCallback);
114 ASSERT_NE(oriContext, nullptr);
115 std::shared_ptr<Context> context = oriContext;
116
117 ASSERT_EQ(context->GetContextId(), testContestId);
118 ASSERT_EQ(context->GetContextType(), CONTEXT_SIMPLE_AUTH);
119 }
120
121 HWTEST_F(SimpleAuthContextTest, SimpleAuthContextTest_Start_001, TestSize.Level0)
122 {
123 static const uint64_t testContestId = 2;
124
125 std::shared_ptr<MockAuthentication> mockAuth = Common::MakeShared<MockAuthentication>();
126 ASSERT_NE(mockAuth, nullptr);
127 EXPECT_CALL(*mockAuth, GetLatestError()).Times(1);
128 EXPECT_CALL(*mockAuth, Start(_, _))
129 .Times(Exactly(1))
130 .WillOnce([](std::vector<std::shared_ptr<ScheduleNode>> &scheduleList,
__anon10a2daf30102(std::vector<std::shared_ptr<ScheduleNode>> &scheduleList, std::shared_ptr<ScheduleNodeCallback> callback) 131 std::shared_ptr<ScheduleNodeCallback> callback) {
132 // Error: process authentication start fail
133 return false;
134 });
135 std::shared_ptr<ContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
136 ASSERT_NE(contextCallback, nullptr);
137 std::shared_ptr<Context> context = Common::MakeShared<SimpleAuthContext>(testContestId, mockAuth, contextCallback);
138 ASSERT_NE(context, nullptr);
139 ASSERT_EQ(context->Start(), false);
140 }
141
142 HWTEST_F(SimpleAuthContextTest, SimpleAuthContextTest_Start_002, TestSize.Level0)
143 {
144 static const uint64_t testContestId = 2;
145
146 std::shared_ptr<MockAuthentication> mockAuth = Common::MakeShared<MockAuthentication>();
147 ASSERT_NE(mockAuth, nullptr);
148 EXPECT_CALL(*mockAuth, Start(_, _))
149 .Times(Exactly(1))
150 .WillOnce([](std::vector<std::shared_ptr<ScheduleNode>> &scheduleList,
__anon10a2daf30202(std::vector<std::shared_ptr<ScheduleNode>> &scheduleList, std::shared_ptr<ScheduleNodeCallback> callback) 151 std::shared_ptr<ScheduleNodeCallback> callback) {
152 // Error: scheduleNodeList size = 0
153 return true;
154 });
155 std::shared_ptr<ContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
156 ASSERT_NE(contextCallback, nullptr);
157 std::shared_ptr<Context> context = Common::MakeShared<SimpleAuthContext>(testContestId, mockAuth, contextCallback);
158 ASSERT_NE(context, nullptr);
159 ASSERT_EQ(context->Start(), false);
160 }
161
162 HWTEST_F(SimpleAuthContextTest, SimpleAuthContextTest_Start_003, TestSize.Level0)
163 {
164 static const uint64_t testContestId = 2;
165
166 std::shared_ptr<MockAuthentication> mockAuth = Common::MakeShared<MockAuthentication>();
167 ASSERT_NE(mockAuth, nullptr);
168 EXPECT_CALL(*mockAuth, Start(_, _))
169 .Times(Exactly(1))
170 .WillOnce([](std::vector<std::shared_ptr<ScheduleNode>> &scheduleList,
__anon10a2daf30302(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 = Common::MakeShared<SimpleAuthContext>(testContestId, mockAuth, contextCallback);
180 ASSERT_NE(context, nullptr);
181 ASSERT_EQ(context->Start(), false);
182 }
183
184 HWTEST_F(SimpleAuthContextTest, SimpleAuthContextTest_Start_004, TestSize.Level0)
185 {
186 static const uint64_t testContestId = 2;
187
188 std::shared_ptr<MockAuthentication> mockAuth = Common::MakeShared<MockAuthentication>();
189 ASSERT_NE(mockAuth, nullptr);
190 EXPECT_CALL(*mockAuth, Start(_, _))
191 .Times(Exactly(1))
192 .WillOnce([](std::vector<std::shared_ptr<ScheduleNode>> &scheduleList,
__anon10a2daf30402(std::vector<std::shared_ptr<ScheduleNode>> &scheduleList, std::shared_ptr<ScheduleNodeCallback> callback) 193 std::shared_ptr<ScheduleNodeCallback> callback) {
194 // Error: schedule node start fail
195 auto scheduleNode = Common::MakeShared<MockScheduleNode>();
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<SimpleAuthContext>(testContestId, mockAuth, contextCallback);
203 ASSERT_NE(context, nullptr);
204 ASSERT_EQ(context->Start(), false);
205 }
206
207 HWTEST_F(SimpleAuthContextTest, SimpleAuthContextTest_Start_005, TestSize.Level0)
208 {
209 static const uint64_t testContestId = 2;
210 static const uint64_t testScheduleId = 3;
211
212 std::shared_ptr<MockAuthentication> mockAuth = Common::MakeShared<MockAuthentication>();
213 ASSERT_NE(mockAuth, nullptr);
214 EXPECT_CALL(*mockAuth, Start(_, _))
215 .Times(Exactly(1))
216 .WillOnce([](std::vector<std::shared_ptr<ScheduleNode>> &scheduleList,
__anon10a2daf30502(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_CALL(*scheduleNode, StartSchedule()).Times(Exactly(1)).WillOnce(Return(true));
222 EXPECT_CALL(*scheduleNode, GetScheduleId()).WillRepeatedly(Return(testScheduleId));
223 scheduleList.push_back(scheduleNode);
224 return true;
225 });
226 std::shared_ptr<ContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
227 ASSERT_NE(contextCallback, nullptr);
228 std::shared_ptr<Context> context = Common::MakeShared<SimpleAuthContext>(testContestId, mockAuth, contextCallback);
229 ASSERT_NE(context, nullptr);
230 ASSERT_EQ(context->Start(), true);
231 ASSERT_EQ(context->Start(), false);
232 auto node = context->GetScheduleNode(testScheduleId);
233 ASSERT_NE(node, nullptr);
234 node = context->GetScheduleNode(testScheduleId + 1);
235 ASSERT_EQ(node, nullptr);
236 }
237
238 HWTEST_F(SimpleAuthContextTest, SimpleAuthContextTest_Stop_001, TestSize.Level0)
239 {
240 static const uint64_t testContestId = 2;
241
242 std::shared_ptr<MockAuthentication> mockAuth = Common::MakeShared<MockAuthentication>();
243 ASSERT_NE(mockAuth, nullptr);
244 EXPECT_CALL(*mockAuth, GetLatestError()).Times(1);
__anon10a2daf30602() 245 EXPECT_CALL(*mockAuth, Cancel()).Times(Exactly(1)).WillOnce([]() {
246 // Error: authentication cancel fail
247 return false;
248 });
249 std::shared_ptr<ContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
250 ASSERT_NE(contextCallback, nullptr);
251 std::shared_ptr<Context> context = Common::MakeShared<SimpleAuthContext>(testContestId, mockAuth, contextCallback);
252 ASSERT_NE(context, nullptr);
253 ASSERT_EQ(context->Stop(), false);
254 }
255
256 HWTEST_F(SimpleAuthContextTest, SimpleAuthContextTest_Stop_002, TestSize.Level0)
257 {
258 static const uint64_t testContestId = 2;
259
260 std::shared_ptr<MockAuthentication> mockAuth = Common::MakeShared<MockAuthentication>();
261 ASSERT_NE(mockAuth, nullptr);
__anon10a2daf30702() 262 EXPECT_CALL(*mockAuth, 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<SimpleAuthContext>(testContestId, mockAuth, contextCallback);
266 ASSERT_NE(context, nullptr);
267 ASSERT_EQ(context->Stop(), true);
268 }
269
270 HWTEST_F(SimpleAuthContextTest, SimpleAuthContextTest_Stop_003, TestSize.Level0)
271 {
272 static const uint64_t testContestId = 2;
273
274 std::shared_ptr<MockAuthentication> mockAuth = Common::MakeShared<MockAuthentication>();
275 ASSERT_NE(mockAuth, nullptr);
276 EXPECT_CALL(*mockAuth, Start(_, _)).Times(1);
277 ON_CALL(*mockAuth, Start)
278 .WillByDefault(
279 [](std::vector<std::shared_ptr<ScheduleNode>> &scheduleList,
__anon10a2daf30802(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 );
__anon10a2daf30902() 285 EXPECT_CALL(*mockAuth, 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<SimpleAuthContext>(testContestId, mockAuth, contextCallback);
291 ASSERT_NE(context, nullptr);
292 ASSERT_EQ(context->Start(), false);
293 ASSERT_EQ(context->Stop(), true);
294 }
295
296 HWTEST_F(SimpleAuthContextTest, SimpleAuthContextTest_Stop_004, TestSize.Level0)
297 {
298 static const uint64_t testContestId = 2;
299
300 std::shared_ptr<MockAuthentication> mockAuth = Common::MakeShared<MockAuthentication>();
301 ASSERT_NE(mockAuth, nullptr);
302 EXPECT_CALL(*mockAuth, Start(_, _)).Times(1);
303 ON_CALL(*mockAuth, Start)
304 .WillByDefault(
305 [](std::vector<std::shared_ptr<ScheduleNode>> &scheduleList,
__anon10a2daf30a02(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(*mockAuth, GetLatestError()).Times(1);
__anon10a2daf30b02() 316 EXPECT_CALL(*mockAuth, 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<SimpleAuthContext>(testContestId, mockAuth, contextCallback);
322 ASSERT_NE(context, nullptr);
323 ASSERT_EQ(context->Start(), false);
324 ASSERT_EQ(context->Stop(), false);
325 }
326
327 HWTEST_F(SimpleAuthContextTest, SimpleAuthContextTest_OnScheduleStarted, TestSize.Level0)
328 {
329 static const uint64_t testContestId = 2;
330
331 std::shared_ptr<MockAuthentication> mockAuth = Common::MakeShared<MockAuthentication>();
332 ASSERT_NE(mockAuth, 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<SimpleAuthContext>(testContestId, mockAuth, contextCallback);
338 ASSERT_NE(nodeCallback, nullptr);
339 nodeCallback->OnScheduleStarted();
340 }
341
342 HWTEST_F(SimpleAuthContextTest, SimpleAuthContextTest_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<MockAuthentication> mockAuth = Common::MakeShared<MockAuthentication>();
350 ASSERT_NE(mockAuth, nullptr);
351 auto contextCallback = Common::MakeShared<MockContextCallback>();
352 ASSERT_NE(contextCallback, nullptr);
353 EXPECT_CALL(*contextCallback, OnAcquireInfo(_, _, _))
354 .WillOnce(
__anon10a2daf30c02(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<SimpleAuthContext>(testContestId, mockAuth, contextCallback);
362 ASSERT_NE(nodeCallback, nullptr);
363 nodeCallback->OnScheduleProcessed(testRole, testModuleType, testAcquireMsg);
364 }
365
366 HWTEST_F(SimpleAuthContextTest, SimpleAuthContextTest_OnScheduleStoped_001, TestSize.Level0)
367 {
368 static const uint64_t testContestId = 2;
369 static const int32_t testResultCode = 7;
370
371 std::shared_ptr<MockAuthentication> mockAuth = Common::MakeShared<MockAuthentication>();
372 ASSERT_NE(mockAuth, nullptr);
373 std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
374 ASSERT_NE(contextCallback, nullptr);
375 EXPECT_CALL(*contextCallback, OnResult(_, _))
376 .Times(Exactly(1))
__anon10a2daf30d02(int32_t resultCode, const Attributes &finalResult) 377 .WillOnce([](int32_t resultCode, const Attributes &finalResult) { EXPECT_EQ(resultCode, testResultCode); });
378
379 std::shared_ptr<ScheduleNodeCallback> nodeCallback =
380 Common::MakeShared<SimpleAuthContext>(testContestId, mockAuth, contextCallback);
381 ASSERT_NE(nodeCallback, nullptr);
382 // Error: result is null when testResultCode is not success
383 std::shared_ptr<Attributes> result = nullptr;
384 nodeCallback->OnScheduleStoped(testResultCode, result);
385 }
386
387 HWTEST_F(SimpleAuthContextTest, SimpleAuthContextTest_OnScheduleStoped_002, TestSize.Level0)
388 {
389 static const uint64_t testContestId = 2;
390 static const int32_t testResultCode = ResultCode::SUCCESS;
391
392 std::shared_ptr<MockAuthentication> mockAuth = Common::MakeShared<MockAuthentication>();
393 ASSERT_NE(mockAuth, nullptr);
394 std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
395 ASSERT_NE(contextCallback, nullptr);
396 EXPECT_CALL(*contextCallback, OnResult(_, _))
397 .Times(Exactly(1))
__anon10a2daf30e02(int32_t resultCode, const Attributes &finalResult) 398 .WillOnce([](int32_t resultCode, const Attributes &finalResult) {
399 EXPECT_EQ(resultCode, ResultCode::GENERAL_ERROR);
400 });
401
402 std::shared_ptr<ScheduleNodeCallback> nodeCallback =
403 Common::MakeShared<SimpleAuthContext>(testContestId, mockAuth, contextCallback);
404 ASSERT_NE(nodeCallback, nullptr);
405 // Error: result is null when testResultCode is success
406 std::shared_ptr<Attributes> result = nullptr;
407 nodeCallback->OnScheduleStoped(testResultCode, result);
408 }
409
410 HWTEST_F(SimpleAuthContextTest, SimpleAuthContextTest_OnScheduleStoped_003, TestSize.Level0)
411 {
412 static const uint64_t testContestId = 2;
413 static const int32_t testResultCode = ResultCode::SUCCESS;
414
415 std::shared_ptr<MockAuthentication> mockAuth = Common::MakeShared<MockAuthentication>();
416 ASSERT_NE(mockAuth, nullptr);
417 std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
418 ASSERT_NE(contextCallback, nullptr);
419 EXPECT_CALL(*contextCallback, OnResult(_, _))
420 .Times(Exactly(1))
__anon10a2daf30f02(int32_t resultCode, const Attributes &finalResult) 421 .WillOnce([](int32_t resultCode, const Attributes &finalResult) {
422 EXPECT_EQ(resultCode, ResultCode::GENERAL_ERROR);
423 });
424
425 std::shared_ptr<ScheduleNodeCallback> nodeCallback =
426 Common::MakeShared<SimpleAuthContext>(testContestId, mockAuth, contextCallback);
427 ASSERT_NE(nodeCallback, nullptr);
428 // Error: ATTR_RESULT_CODE is not set
429 std::shared_ptr<Attributes> result = Common::MakeShared<Attributes>();
430 nodeCallback->OnScheduleStoped(testResultCode, result);
431 }
432
433 HWTEST_F(SimpleAuthContextTest, SimpleAuthContextTest_OnScheduleStoped_004, TestSize.Level0)
434 {
435 static const uint64_t testContestId = 2;
436 static const int32_t testResultCode = ResultCode::SUCCESS;
437 static const std::vector<uint8_t> testScheduleResult = {3, 4, 5, 6};
438
439 std::shared_ptr<MockAuthentication> mockAuth = Common::MakeShared<MockAuthentication>();
440 ASSERT_NE(mockAuth, nullptr);
441 EXPECT_CALL(*mockAuth, GetLatestError()).Times(1);
442 EXPECT_CALL(*mockAuth, Update(_, _))
443 .Times(Exactly(1))
__anon10a2daf31002(const std::vector<uint8_t> &scheduleResult, Authentication::AuthResultInfo &resultInfo) 444 .WillOnce([](const std::vector<uint8_t> &scheduleResult, Authentication::AuthResultInfo &resultInfo) {
445 EXPECT_EQ(scheduleResult, testScheduleResult);
446 return false;
447 });
448 std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
449 ASSERT_NE(contextCallback, nullptr);
450 EXPECT_CALL(*contextCallback, OnResult(_, _))
451 .Times(Exactly(1))
__anon10a2daf31102(int32_t resultCode, const Attributes &finalResult) 452 .WillOnce([](int32_t resultCode, const Attributes &finalResult) {
453 EXPECT_EQ(resultCode, ResultCode::GENERAL_ERROR);
454 });
455
456 std::shared_ptr<ScheduleNodeCallback> nodeCallback =
457 Common::MakeShared<SimpleAuthContext>(testContestId, mockAuth, contextCallback);
458 ASSERT_NE(nodeCallback, nullptr);
459 // Error: auth_->Update return false
460 std::shared_ptr<Attributes> result = Common::MakeShared<Attributes>();
461 ASSERT_NE(result, nullptr);
462 bool ret = result->SetUint8ArrayValue(Attributes::ATTR_RESULT, testScheduleResult);
463 ASSERT_EQ(ret, true);
464 nodeCallback->OnScheduleStoped(testResultCode, result);
465 }
466
MockForContextCallback(std::shared_ptr<MockContextCallback> contextCallback)467 static void MockForContextCallback(std::shared_ptr<MockContextCallback> contextCallback)
468 {
469 static const int32_t testResultCode = 1;
470 static const int32_t testFreezingTime = 8;
471 static const int32_t testRemainTimes = 9;
472 static std::vector<uint8_t> testSignature = {10, 11, 12, 13};
473 EXPECT_CALL(*contextCallback, OnResult(_, _))
474 .Times(Exactly(1))
475 .WillOnce([](int32_t resultCode, const Attributes &finalResult) {
476 EXPECT_EQ(resultCode, testResultCode);
477 uint32_t attrResultCode;
478 int32_t freezingTime;
479 int32_t remainTimes;
480 vector<uint8_t> signature;
481 bool ret = finalResult.GetUint32Value(Attributes::ATTR_RESULT_CODE, attrResultCode);
482 EXPECT_EQ(ret, true);
483 ret = finalResult.GetInt32Value(Attributes::ATTR_FREEZING_TIME, freezingTime);
484 EXPECT_EQ(ret, true);
485 ret = finalResult.GetInt32Value(Attributes::ATTR_REMAIN_TIMES, remainTimes);
486 EXPECT_EQ(ret, true);
487 ret = finalResult.GetUint8ArrayValue(Attributes::ATTR_SIGNATURE, signature);
488 EXPECT_EQ(ret, true);
489
490 EXPECT_EQ(resultCode, testResultCode);
491 EXPECT_EQ(freezingTime, testFreezingTime);
492 EXPECT_EQ(remainTimes, testRemainTimes);
493 EXPECT_EQ(signature, testSignature);
494 });
495 }
496
497 HWTEST_F(SimpleAuthContextTest, SimpleAuthContextTest_OnScheduleStoped_005, TestSize.Level0)
498 {
499 static const uint64_t testContestId = 2;
500 static const std::vector<uint8_t> testScheduleResult = {3, 4, 5, 6};
501 static const int32_t testResultCode = 1;
502 static const int32_t testFreezingTime = 8;
503 static const int32_t testRemainTimes = 9;
504 static const std::vector<uint8_t> testSignature = {10, 11, 12, 13};
505
506 std::shared_ptr<MockAuthentication> mockAuth = Common::MakeShared<MockAuthentication>();
507 ASSERT_NE(mockAuth, nullptr);
508 EXPECT_CALL(*mockAuth, Update(_, _))
509 .Times(Exactly(1))
__anon10a2daf31302(const std::vector<uint8_t> &scheduleResult, Authentication::AuthResultInfo &resultInfo) 510 .WillOnce([](const std::vector<uint8_t> &scheduleResult, Authentication::AuthResultInfo &resultInfo) {
511 EXPECT_EQ(scheduleResult, testScheduleResult);
512 resultInfo.result = testResultCode;
513 resultInfo.freezingTime = testFreezingTime;
514 resultInfo.remainTimes = testRemainTimes;
515 resultInfo.token = testSignature;
516 return true;
517 });
518 std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
519 ASSERT_NE(contextCallback, nullptr);
520 MockForContextCallback(contextCallback);
521
522 std::shared_ptr<ScheduleNodeCallback> nodeCallback =
523 Common::MakeShared<SimpleAuthContext>(testContestId, mockAuth, contextCallback);
524 ASSERT_NE(nodeCallback, nullptr);
525 // Success
526 std::shared_ptr<Attributes> result = Common::MakeShared<Attributes>();
527 ASSERT_NE(result, nullptr);
528 bool ret = result->SetUint8ArrayValue(Attributes::ATTR_RESULT, testScheduleResult);
529 ASSERT_EQ(ret, true);
530 nodeCallback->OnScheduleStoped(testResultCode, result);
531 }
532
533 HWTEST_F(SimpleAuthContextTest, SimpleAuthContextTest_OnScheduleStoped_006, TestSize.Level0)
534 {
535 static const uint64_t testContestId = 2;
536 static const std::vector<uint8_t> testScheduleResult = {3, 4, 5, 6};
537 static const int32_t testResultCode = 7;
538 static const int32_t testFreezingTime = 8;
539 static const int32_t testRemainTimes = 9;
540 static const std::vector<uint8_t> testSignature = {10, 11, 12, 13};
541
542 std::shared_ptr<MockAuthentication> mockAuth = Common::MakeShared<MockAuthentication>();
543 ASSERT_NE(mockAuth, nullptr);
544 EXPECT_CALL(*mockAuth, Update(_, _))
545 .Times(Exactly(1))
__anon10a2daf31402(const std::vector<uint8_t> &scheduleResult, Authentication::AuthResultInfo &resultInfo) 546 .WillOnce([](const std::vector<uint8_t> &scheduleResult, Authentication::AuthResultInfo &resultInfo) {
547 EXPECT_EQ(scheduleResult, testScheduleResult);
548 resultInfo.result = testResultCode;
549 resultInfo.freezingTime = testFreezingTime;
550 resultInfo.remainTimes = testRemainTimes;
551 resultInfo.token = testSignature;
552 resultInfo.rootSecret = {1, 2, 3, 4};
553 return true;
554 });
555 std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
556 ASSERT_NE(contextCallback, nullptr);
557 EXPECT_CALL(*contextCallback, OnResult(_, _)).Times(1);
558
559 std::shared_ptr<ScheduleNodeCallback> nodeCallback =
560 Common::MakeShared<SimpleAuthContext>(testContestId, mockAuth, contextCallback);
561 ASSERT_NE(nodeCallback, nullptr);
562 // Success
563 std::shared_ptr<Attributes> result = Common::MakeShared<Attributes>();
564 ASSERT_NE(result, nullptr);
565 bool ret = result->SetUint8ArrayValue(Attributes::ATTR_RESULT, testScheduleResult);
566 ASSERT_EQ(ret, true);
567 nodeCallback->OnScheduleStoped(testResultCode, result);
568 }
569
570 HWTEST_F(SimpleAuthContextTest, SimpleAuthContextTest_ContextFree, TestSize.Level0)
571 {
572 static const uint64_t testContestId = 2;
573
574 std::shared_ptr<MockAuthentication> mockAuth = Common::MakeShared<MockAuthentication>();
575 ASSERT_NE(mockAuth, nullptr);
576 EXPECT_CALL(*mockAuth, Start(_, _))
577 .Times(Exactly(1))
578 .WillOnce([](std::vector<std::shared_ptr<ScheduleNode>> &scheduleList,
__anon10a2daf31502(std::vector<std::shared_ptr<ScheduleNode>> &scheduleList, std::shared_ptr<ScheduleNodeCallback> callback) 579 std::shared_ptr<ScheduleNodeCallback> callback) {
580 EXPECT_EQ(scheduleList.size(), 0U);
581 auto faceAllInOne = MockResourceNode::CreateWithExecuteIndex(1, FACE, ALL_IN_ONE);
582 auto builder = ScheduleNode::Builder::New(faceAllInOne, faceAllInOne);
583 builder->SetScheduleCallback(callback);
584 auto scheduleNode = builder->Build();
585 EXPECT_NE(scheduleNode, nullptr);
586 scheduleList.push_back(scheduleNode);
587 return true;
588 });
589 std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
590 ASSERT_NE(contextCallback, nullptr);
591
592 std::promise<void> promise;
593 EXPECT_CALL(*contextCallback, OnResult(_, _))
594 .Times(Exactly(1))
__anon10a2daf31602(int32_t resultCode, const Attributes &finalResult) 595 .WillOnce([&promise](int32_t resultCode, const Attributes &finalResult) {
596 EXPECT_EQ(resultCode, ResultCode::GENERAL_ERROR);
597 promise.set_value();
598 });
599
600 std::weak_ptr<Context> weakContext;
601 {
602 std::shared_ptr<Context> context = Common::MakeShared<SimpleAuthContext>(testContestId,
603 mockAuth, contextCallback);
604 ASSERT_NE(context, nullptr);
605 weakContext = context;
606 context->Start();
607 promise.get_future().get();
608 }
609 std::this_thread::sleep_for(std::chrono::milliseconds(500));
610 ASSERT_EQ(weakContext.lock(), nullptr);
611 }
612 } // namespace UserAuth
613 } // namespace UserIam
614 } // namespace OHOS
615