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, true);
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, true);
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, true);
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,
__anon9cd735dd0102(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 =
138 Common::MakeShared<SimpleAuthContext>(testContestId, mockAuth, contextCallback, true);
139 ASSERT_NE(context, nullptr);
140 ASSERT_EQ(context->Start(), false);
141 }
142
143 HWTEST_F(SimpleAuthContextTest, SimpleAuthContextTest_Start_002, TestSize.Level0)
144 {
145 static const uint64_t testContestId = 2;
146
147 std::shared_ptr<MockAuthentication> mockAuth = Common::MakeShared<MockAuthentication>();
148 ASSERT_NE(mockAuth, nullptr);
149 EXPECT_CALL(*mockAuth, Start(_, _))
150 .Times(Exactly(1))
151 .WillOnce([](std::vector<std::shared_ptr<ScheduleNode>> &scheduleList,
__anon9cd735dd0202(std::vector<std::shared_ptr<ScheduleNode>> &scheduleList, std::shared_ptr<ScheduleNodeCallback> callback) 152 std::shared_ptr<ScheduleNodeCallback> callback) {
153 // Error: scheduleNodeList size = 0
154 return true;
155 });
156 std::shared_ptr<ContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
157 ASSERT_NE(contextCallback, nullptr);
158 std::shared_ptr<Context> context =
159 Common::MakeShared<SimpleAuthContext>(testContestId, mockAuth, contextCallback, true);
160 ASSERT_NE(context, nullptr);
161 ASSERT_EQ(context->Start(), false);
162 }
163
164 HWTEST_F(SimpleAuthContextTest, SimpleAuthContextTest_Start_003, TestSize.Level0)
165 {
166 static const uint64_t testContestId = 2;
167
168 std::shared_ptr<MockAuthentication> mockAuth = Common::MakeShared<MockAuthentication>();
169 ASSERT_NE(mockAuth, nullptr);
170 EXPECT_CALL(*mockAuth, Start(_, _))
171 .Times(Exactly(1))
172 .WillOnce([](std::vector<std::shared_ptr<ScheduleNode>> &scheduleList,
__anon9cd735dd0302(std::vector<std::shared_ptr<ScheduleNode>> &scheduleList, std::shared_ptr<ScheduleNodeCallback> callback) 173 std::shared_ptr<ScheduleNodeCallback> callback) {
174 // Error: scheduleNodeList size = 2
175 scheduleList.push_back(Common::MakeShared<MockScheduleNode>());
176 scheduleList.push_back(Common::MakeShared<MockScheduleNode>());
177 return true;
178 });
179 std::shared_ptr<ContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
180 ASSERT_NE(contextCallback, nullptr);
181 std::shared_ptr<Context> context =
182 Common::MakeShared<SimpleAuthContext>(testContestId, mockAuth, contextCallback, true);
183 ASSERT_NE(context, nullptr);
184 ASSERT_EQ(context->Start(), false);
185 }
186
187 HWTEST_F(SimpleAuthContextTest, SimpleAuthContextTest_Start_004, TestSize.Level0)
188 {
189 static const uint64_t testContestId = 2;
190
191 std::shared_ptr<MockAuthentication> mockAuth = Common::MakeShared<MockAuthentication>();
192 ASSERT_NE(mockAuth, nullptr);
193 EXPECT_CALL(*mockAuth, Start(_, _))
194 .Times(Exactly(1))
195 .WillOnce([](std::vector<std::shared_ptr<ScheduleNode>> &scheduleList,
__anon9cd735dd0402(std::vector<std::shared_ptr<ScheduleNode>> &scheduleList, std::shared_ptr<ScheduleNodeCallback> callback) 196 std::shared_ptr<ScheduleNodeCallback> callback) {
197 // Error: schedule node start fail
198 auto scheduleNode = Common::MakeShared<MockScheduleNode>();
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<SimpleAuthContext>(testContestId, mockAuth, contextCallback, true);
207 ASSERT_NE(context, nullptr);
208 ASSERT_EQ(context->Start(), false);
209 }
210
211 HWTEST_F(SimpleAuthContextTest, SimpleAuthContextTest_Start_005, TestSize.Level0)
212 {
213 static const uint64_t testContestId = 2;
214 static const uint64_t testScheduleId = 3;
215
216 std::shared_ptr<MockAuthentication> mockAuth = Common::MakeShared<MockAuthentication>();
217 ASSERT_NE(mockAuth, nullptr);
218 EXPECT_CALL(*mockAuth, Start(_, _))
219 .Times(Exactly(1))
220 .WillOnce([](std::vector<std::shared_ptr<ScheduleNode>> &scheduleList,
__anon9cd735dd0502(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_CALL(*scheduleNode, StartSchedule()).Times(Exactly(1)).WillOnce(Return(true));
226 EXPECT_CALL(*scheduleNode, GetScheduleId()).WillRepeatedly(Return(testScheduleId));
227 scheduleList.push_back(scheduleNode);
228 return true;
229 });
230 std::shared_ptr<ContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
231 ASSERT_NE(contextCallback, nullptr);
232 std::shared_ptr<Context> context =
233 Common::MakeShared<SimpleAuthContext>(testContestId, mockAuth, contextCallback, true);
234 ASSERT_NE(context, nullptr);
235 ASSERT_EQ(context->Start(), true);
236 ASSERT_EQ(context->Start(), false);
237 auto node = context->GetScheduleNode(testScheduleId);
238 ASSERT_NE(node, nullptr);
239 node = context->GetScheduleNode(testScheduleId + 1);
240 ASSERT_EQ(node, nullptr);
241 }
242
243 HWTEST_F(SimpleAuthContextTest, SimpleAuthContextTest_Stop_001, TestSize.Level0)
244 {
245 static const uint64_t testContestId = 2;
246
247 std::shared_ptr<MockAuthentication> mockAuth = Common::MakeShared<MockAuthentication>();
248 ASSERT_NE(mockAuth, nullptr);
249 EXPECT_CALL(*mockAuth, GetLatestError()).Times(1);
__anon9cd735dd0602() 250 EXPECT_CALL(*mockAuth, Cancel()).Times(Exactly(1)).WillOnce([]() {
251 // Error: authentication cancel fail
252 return false;
253 });
254 std::shared_ptr<ContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
255 ASSERT_NE(contextCallback, nullptr);
256 std::shared_ptr<Context> context =
257 Common::MakeShared<SimpleAuthContext>(testContestId, mockAuth, contextCallback, true);
258 ASSERT_NE(context, nullptr);
259 ASSERT_EQ(context->Stop(), false);
260 }
261
262 HWTEST_F(SimpleAuthContextTest, SimpleAuthContextTest_Stop_002, TestSize.Level0)
263 {
264 static const uint64_t testContestId = 2;
265
266 std::shared_ptr<MockAuthentication> mockAuth = Common::MakeShared<MockAuthentication>();
267 ASSERT_NE(mockAuth, nullptr);
__anon9cd735dd0702() 268 EXPECT_CALL(*mockAuth, 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<SimpleAuthContext>(testContestId, mockAuth, contextCallback, true);
273 ASSERT_NE(context, nullptr);
274 ASSERT_EQ(context->Stop(), true);
275 }
276
277 HWTEST_F(SimpleAuthContextTest, SimpleAuthContextTest_Stop_003, TestSize.Level0)
278 {
279 static const uint64_t testContestId = 2;
280
281 std::shared_ptr<MockAuthentication> mockAuth = Common::MakeShared<MockAuthentication>();
282 ASSERT_NE(mockAuth, nullptr);
283 EXPECT_CALL(*mockAuth, Start(_, _)).Times(1);
284 ON_CALL(*mockAuth, Start)
285 .WillByDefault(
286 [](std::vector<std::shared_ptr<ScheduleNode>> &scheduleList,
__anon9cd735dd0802(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 );
__anon9cd735dd0902() 292 EXPECT_CALL(*mockAuth, 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<SimpleAuthContext>(testContestId, mockAuth, contextCallback, true);
299 ASSERT_NE(context, nullptr);
300 ASSERT_EQ(context->Start(), false);
301 ASSERT_EQ(context->Stop(), true);
302 }
303
304 HWTEST_F(SimpleAuthContextTest, SimpleAuthContextTest_Stop_004, TestSize.Level0)
305 {
306 static const uint64_t testContestId = 2;
307
308 std::shared_ptr<MockAuthentication> mockAuth = Common::MakeShared<MockAuthentication>();
309 ASSERT_NE(mockAuth, nullptr);
310 EXPECT_CALL(*mockAuth, Start(_, _)).Times(1);
311 ON_CALL(*mockAuth, Start)
312 .WillByDefault(
313 [](std::vector<std::shared_ptr<ScheduleNode>> &scheduleList,
__anon9cd735dd0a02(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(*mockAuth, GetLatestError()).Times(1);
__anon9cd735dd0b02() 324 EXPECT_CALL(*mockAuth, 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<SimpleAuthContext>(testContestId, mockAuth, contextCallback, true);
331 ASSERT_NE(context, nullptr);
332 ASSERT_EQ(context->Start(), false);
333 ASSERT_EQ(context->Stop(), false);
334 }
335
336 HWTEST_F(SimpleAuthContextTest, SimpleAuthContextTest_OnScheduleStarted, TestSize.Level0)
337 {
338 static const uint64_t testContestId = 2;
339
340 std::shared_ptr<MockAuthentication> mockAuth = Common::MakeShared<MockAuthentication>();
341 ASSERT_NE(mockAuth, 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<SimpleAuthContext>(testContestId, mockAuth, contextCallback, true);
347 ASSERT_NE(nodeCallback, nullptr);
348 nodeCallback->OnScheduleStarted();
349 }
350
351 HWTEST_F(SimpleAuthContextTest, SimpleAuthContextTest_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<MockAuthentication> mockAuth = Common::MakeShared<MockAuthentication>();
359 ASSERT_NE(mockAuth, nullptr);
360 auto contextCallback = Common::MakeShared<MockContextCallback>();
361 ASSERT_NE(contextCallback, nullptr);
362 EXPECT_CALL(*contextCallback, OnAcquireInfo(_, _, _))
363 .WillOnce(
__anon9cd735dd0c02(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<SimpleAuthContext>(testContestId, mockAuth, contextCallback, true);
371 ASSERT_NE(nodeCallback, nullptr);
372 nodeCallback->OnScheduleProcessed(testRole, testModuleType, testAcquireMsg);
373 }
374
375 HWTEST_F(SimpleAuthContextTest, SimpleAuthContextTest_OnScheduleStoped_001, TestSize.Level0)
376 {
377 static const uint64_t testContestId = 2;
378 static const int32_t testResultCode = 7;
379
380 std::shared_ptr<MockAuthentication> mockAuth = Common::MakeShared<MockAuthentication>();
381 ASSERT_NE(mockAuth, nullptr);
382 std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
383 ASSERT_NE(contextCallback, nullptr);
384 EXPECT_CALL(*contextCallback, OnResult(_, _))
385 .Times(Exactly(1))
__anon9cd735dd0d02(int32_t resultCode, const Attributes &finalResult) 386 .WillOnce([](int32_t resultCode, const Attributes &finalResult) { EXPECT_EQ(resultCode, testResultCode); });
387
388 std::shared_ptr<ScheduleNodeCallback> nodeCallback =
389 Common::MakeShared<SimpleAuthContext>(testContestId, mockAuth, contextCallback, true);
390 ASSERT_NE(nodeCallback, nullptr);
391 // Error: result is null when testResultCode is not success
392 std::shared_ptr<Attributes> result = nullptr;
393 nodeCallback->OnScheduleStoped(testResultCode, result);
394 }
395
396 HWTEST_F(SimpleAuthContextTest, SimpleAuthContextTest_OnScheduleStoped_002, TestSize.Level0)
397 {
398 static const uint64_t testContestId = 2;
399 static const int32_t testResultCode = ResultCode::SUCCESS;
400
401 std::shared_ptr<MockAuthentication> mockAuth = Common::MakeShared<MockAuthentication>();
402 ASSERT_NE(mockAuth, nullptr);
403 std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
404 ASSERT_NE(contextCallback, nullptr);
405 EXPECT_CALL(*contextCallback, OnResult(_, _))
406 .Times(Exactly(1))
__anon9cd735dd0e02(int32_t resultCode, const Attributes &finalResult) 407 .WillOnce([](int32_t resultCode, const Attributes &finalResult) {
408 EXPECT_EQ(resultCode, ResultCode::GENERAL_ERROR);
409 });
410
411 std::shared_ptr<ScheduleNodeCallback> nodeCallback =
412 Common::MakeShared<SimpleAuthContext>(testContestId, mockAuth, contextCallback, true);
413 ASSERT_NE(nodeCallback, nullptr);
414 // Error: result is null when testResultCode is success
415 std::shared_ptr<Attributes> result = nullptr;
416 nodeCallback->OnScheduleStoped(testResultCode, result);
417 }
418
419 HWTEST_F(SimpleAuthContextTest, SimpleAuthContextTest_OnScheduleStoped_003, TestSize.Level0)
420 {
421 static const uint64_t testContestId = 2;
422 static const int32_t testResultCode = ResultCode::SUCCESS;
423
424 std::shared_ptr<MockAuthentication> mockAuth = Common::MakeShared<MockAuthentication>();
425 ASSERT_NE(mockAuth, nullptr);
426 std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
427 ASSERT_NE(contextCallback, nullptr);
428 EXPECT_CALL(*contextCallback, OnResult(_, _))
429 .Times(Exactly(1))
__anon9cd735dd0f02(int32_t resultCode, const Attributes &finalResult) 430 .WillOnce([](int32_t resultCode, const Attributes &finalResult) {
431 EXPECT_EQ(resultCode, ResultCode::GENERAL_ERROR);
432 });
433
434 std::shared_ptr<ScheduleNodeCallback> nodeCallback =
435 Common::MakeShared<SimpleAuthContext>(testContestId, mockAuth, contextCallback, true);
436 ASSERT_NE(nodeCallback, nullptr);
437 // Error: ATTR_RESULT_CODE is not set
438 std::shared_ptr<Attributes> result = Common::MakeShared<Attributes>();
439 nodeCallback->OnScheduleStoped(testResultCode, result);
440 }
441
442 HWTEST_F(SimpleAuthContextTest, SimpleAuthContextTest_OnScheduleStoped_004, TestSize.Level0)
443 {
444 static const uint64_t testContestId = 2;
445 static const int32_t testResultCode = ResultCode::SUCCESS;
446 static const std::vector<uint8_t> testScheduleResult = {3, 4, 5, 6};
447
448 std::shared_ptr<MockAuthentication> mockAuth = Common::MakeShared<MockAuthentication>();
449 ASSERT_NE(mockAuth, nullptr);
450 EXPECT_CALL(*mockAuth, GetLatestError()).Times(1);
451 EXPECT_CALL(*mockAuth, Update(_, _))
452 .Times(Exactly(1))
__anon9cd735dd1002(const std::vector<uint8_t> &scheduleResult, Authentication::AuthResultInfo &resultInfo) 453 .WillOnce([](const std::vector<uint8_t> &scheduleResult, Authentication::AuthResultInfo &resultInfo) {
454 EXPECT_EQ(scheduleResult, testScheduleResult);
455 return false;
456 });
457 std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
458 ASSERT_NE(contextCallback, nullptr);
459 EXPECT_CALL(*contextCallback, OnResult(_, _))
460 .Times(Exactly(1))
__anon9cd735dd1102(int32_t resultCode, const Attributes &finalResult) 461 .WillOnce([](int32_t resultCode, const Attributes &finalResult) {
462 EXPECT_EQ(resultCode, ResultCode::GENERAL_ERROR);
463 });
464
465 std::shared_ptr<ScheduleNodeCallback> nodeCallback =
466 Common::MakeShared<SimpleAuthContext>(testContestId, mockAuth, contextCallback, true);
467 ASSERT_NE(nodeCallback, nullptr);
468 // Error: auth_->Update return false
469 std::shared_ptr<Attributes> result = Common::MakeShared<Attributes>();
470 ASSERT_NE(result, nullptr);
471 bool ret = result->SetUint8ArrayValue(Attributes::ATTR_RESULT, testScheduleResult);
472 ASSERT_EQ(ret, true);
473 nodeCallback->OnScheduleStoped(testResultCode, result);
474 }
475
MockForContextCallback(std::shared_ptr<MockContextCallback> contextCallback)476 static void MockForContextCallback(std::shared_ptr<MockContextCallback> contextCallback)
477 {
478 static const int32_t testResultCode = 1;
479 static const int32_t testFreezingTime = 8;
480 static const int32_t testRemainTimes = 9;
481 static std::vector<uint8_t> testSignature = {10, 11, 12, 13};
482 EXPECT_CALL(*contextCallback, OnResult(_, _))
483 .Times(Exactly(1))
484 .WillOnce([](int32_t resultCode, const Attributes &finalResult) {
485 EXPECT_EQ(resultCode, testResultCode);
486 uint32_t attrResultCode;
487 int32_t freezingTime;
488 int32_t remainTimes;
489 vector<uint8_t> signature;
490 bool ret = finalResult.GetUint32Value(Attributes::ATTR_RESULT_CODE, attrResultCode);
491 EXPECT_EQ(ret, true);
492 ret = finalResult.GetInt32Value(Attributes::ATTR_FREEZING_TIME, freezingTime);
493 EXPECT_EQ(ret, true);
494 ret = finalResult.GetInt32Value(Attributes::ATTR_REMAIN_TIMES, remainTimes);
495 EXPECT_EQ(ret, true);
496 ret = finalResult.GetUint8ArrayValue(Attributes::ATTR_SIGNATURE, signature);
497 EXPECT_EQ(ret, true);
498
499 EXPECT_EQ(resultCode, testResultCode);
500 EXPECT_EQ(freezingTime, testFreezingTime);
501 EXPECT_EQ(remainTimes, testRemainTimes);
502 EXPECT_EQ(signature, testSignature);
503 });
504 }
505
506 HWTEST_F(SimpleAuthContextTest, SimpleAuthContextTest_OnScheduleStoped_005, TestSize.Level0)
507 {
508 static const uint64_t testContestId = 2;
509 static const std::vector<uint8_t> testScheduleResult = {3, 4, 5, 6};
510 static const int32_t testResultCode = 1;
511 static const int32_t testFreezingTime = 8;
512 static const int32_t testRemainTimes = 9;
513 static const std::vector<uint8_t> testSignature = {10, 11, 12, 13};
514
515 std::shared_ptr<MockAuthentication> mockAuth = Common::MakeShared<MockAuthentication>();
516 ASSERT_NE(mockAuth, nullptr);
517 EXPECT_CALL(*mockAuth, Update(_, _))
518 .Times(Exactly(1))
__anon9cd735dd1302(const std::vector<uint8_t> &scheduleResult, Authentication::AuthResultInfo &resultInfo) 519 .WillOnce([](const std::vector<uint8_t> &scheduleResult, Authentication::AuthResultInfo &resultInfo) {
520 EXPECT_EQ(scheduleResult, testScheduleResult);
521 resultInfo.result = testResultCode;
522 resultInfo.freezingTime = testFreezingTime;
523 resultInfo.remainTimes = testRemainTimes;
524 resultInfo.token = testSignature;
525 return true;
526 });
527 std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
528 ASSERT_NE(contextCallback, nullptr);
529 MockForContextCallback(contextCallback);
530
531 std::shared_ptr<ScheduleNodeCallback> nodeCallback =
532 Common::MakeShared<SimpleAuthContext>(testContestId, mockAuth, contextCallback, true);
533 ASSERT_NE(nodeCallback, nullptr);
534 // Success
535 std::shared_ptr<Attributes> result = Common::MakeShared<Attributes>();
536 ASSERT_NE(result, nullptr);
537 bool ret = result->SetUint8ArrayValue(Attributes::ATTR_RESULT, testScheduleResult);
538 ASSERT_EQ(ret, true);
539 nodeCallback->OnScheduleStoped(testResultCode, result);
540 }
541
542 HWTEST_F(SimpleAuthContextTest, SimpleAuthContextTest_OnScheduleStoped_006, TestSize.Level0)
543 {
544 static const uint64_t testContestId = 2;
545 static const std::vector<uint8_t> testScheduleResult = {3, 4, 5, 6};
546 static const int32_t testResultCode = 7;
547 static const int32_t testFreezingTime = 8;
548 static const int32_t testRemainTimes = 9;
549 static const std::vector<uint8_t> testSignature = {10, 11, 12, 13};
550
551 std::shared_ptr<MockAuthentication> mockAuth = Common::MakeShared<MockAuthentication>();
552 ASSERT_NE(mockAuth, nullptr);
553 EXPECT_CALL(*mockAuth, Update(_, _))
554 .Times(Exactly(1))
__anon9cd735dd1402(const std::vector<uint8_t> &scheduleResult, Authentication::AuthResultInfo &resultInfo) 555 .WillOnce([](const std::vector<uint8_t> &scheduleResult, Authentication::AuthResultInfo &resultInfo) {
556 EXPECT_EQ(scheduleResult, testScheduleResult);
557 resultInfo.result = testResultCode;
558 resultInfo.freezingTime = testFreezingTime;
559 resultInfo.remainTimes = testRemainTimes;
560 resultInfo.token = testSignature;
561 resultInfo.rootSecret = {1, 2, 3, 4};
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<SimpleAuthContext>(testContestId, mockAuth, contextCallback, true);
570 ASSERT_NE(nodeCallback, nullptr);
571 // Success
572 std::shared_ptr<Attributes> result = Common::MakeShared<Attributes>();
573 ASSERT_NE(result, nullptr);
574 bool ret = result->SetUint8ArrayValue(Attributes::ATTR_RESULT, testScheduleResult);
575 ASSERT_EQ(ret, true);
576 nodeCallback->OnScheduleStoped(testResultCode, result);
577 }
578
579 HWTEST_F(SimpleAuthContextTest, SimpleAuthContextTest_ContextFree, TestSize.Level0)
580 {
581 static const uint64_t testContestId = 2;
582
583 std::shared_ptr<MockAuthentication> mockAuth = Common::MakeShared<MockAuthentication>();
584 ASSERT_NE(mockAuth, nullptr);
585 EXPECT_CALL(*mockAuth, Start(_, _))
586 .Times(Exactly(1))
587 .WillOnce([](std::vector<std::shared_ptr<ScheduleNode>> &scheduleList,
__anon9cd735dd1502(std::vector<std::shared_ptr<ScheduleNode>> &scheduleList, std::shared_ptr<ScheduleNodeCallback> callback) 588 std::shared_ptr<ScheduleNodeCallback> callback) {
589 EXPECT_EQ(scheduleList.size(), 0U);
590 auto faceAllInOne = MockResourceNode::CreateWithExecuteIndex(1, FACE, ALL_IN_ONE);
591 auto builder = ScheduleNode::Builder::New(faceAllInOne, faceAllInOne);
592 builder->SetScheduleCallback(callback);
593 auto scheduleNode = builder->Build();
594 EXPECT_NE(scheduleNode, nullptr);
595 scheduleList.push_back(scheduleNode);
596 return true;
597 });
598 std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
599 ASSERT_NE(contextCallback, nullptr);
600
601 std::promise<void> promise;
602 EXPECT_CALL(*contextCallback, OnResult(_, _))
603 .Times(Exactly(1))
__anon9cd735dd1602(int32_t resultCode, const Attributes &finalResult) 604 .WillOnce([&promise](int32_t resultCode, const Attributes &finalResult) {
605 EXPECT_EQ(resultCode, ResultCode::GENERAL_ERROR);
606 promise.set_value();
607 });
608
609 std::weak_ptr<Context> weakContext;
610 {
611 std::shared_ptr<Context> context = Common::MakeShared<SimpleAuthContext>(testContestId,
612 mockAuth, contextCallback, true);
613 ASSERT_NE(context, nullptr);
614 weakContext = context;
615 context->Start();
616 promise.get_future().get();
617 }
618 std::this_thread::sleep_for(std::chrono::milliseconds(500));
619 ASSERT_EQ(weakContext.lock(), nullptr);
620 }
621 } // namespace UserAuth
622 } // namespace UserIam
623 } // namespace OHOS
624