• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "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,
__anon69ebc4fc0102(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,
__anon69ebc4fc0202(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,
__anon69ebc4fc0302(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,
__anon69ebc4fc0402(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,
__anon69ebc4fc0502(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()).Times(Exactly(2)).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);
__anon69ebc4fc0602() 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);
__anon69ebc4fc0702() 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,
__anon69ebc4fc0802(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         );
__anon69ebc4fc0902() 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,
__anon69ebc4fc0a02(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);
__anon69ebc4fc0b02() 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     EXPECT_EQ(0, 0);
345 }
346 
347 HWTEST_F(SimpleAuthContextTest, SimpleAuthContextTest_OnScheduleStoped_001, TestSize.Level0)
348 {
349     static const uint64_t testContestId = 2;
350     static const int32_t testResultCode = 7;
351 
352     std::shared_ptr<MockAuthentication> mockAuth = Common::MakeShared<MockAuthentication>();
353     ASSERT_NE(mockAuth, nullptr);
354     std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
355     ASSERT_NE(contextCallback, nullptr);
356     EXPECT_CALL(*contextCallback, OnResult(_, _))
357         .Times(Exactly(1))
__anon69ebc4fc0c02(int32_t resultCode, const Attributes &finalResult) 358         .WillOnce([](int32_t resultCode, const Attributes &finalResult) { EXPECT_EQ(resultCode, testResultCode); });
359 
360     std::shared_ptr<ScheduleNodeCallback> nodeCallback =
361         Common::MakeShared<SimpleAuthContext>(testContestId, mockAuth, contextCallback);
362     ASSERT_NE(nodeCallback, nullptr);
363     // Error: result is null when testResultCode is not success
364     std::shared_ptr<Attributes> result = nullptr;
365     nodeCallback->OnScheduleStoped(testResultCode, result);
366 }
367 
368 HWTEST_F(SimpleAuthContextTest, SimpleAuthContextTest_OnScheduleStoped_002, TestSize.Level0)
369 {
370     static const uint64_t testContestId = 2;
371     static const int32_t testResultCode = ResultCode::SUCCESS;
372 
373     std::shared_ptr<MockAuthentication> mockAuth = Common::MakeShared<MockAuthentication>();
374     ASSERT_NE(mockAuth, nullptr);
375     std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
376     ASSERT_NE(contextCallback, nullptr);
377     EXPECT_CALL(*contextCallback, OnResult(_, _))
378         .Times(Exactly(1))
__anon69ebc4fc0d02(int32_t resultCode, const Attributes &finalResult) 379         .WillOnce([](int32_t resultCode, const Attributes &finalResult) {
380             EXPECT_EQ(resultCode, ResultCode::GENERAL_ERROR);
381         });
382 
383     std::shared_ptr<ScheduleNodeCallback> nodeCallback =
384         Common::MakeShared<SimpleAuthContext>(testContestId, mockAuth, contextCallback);
385     ASSERT_NE(nodeCallback, nullptr);
386     // Error: result is null when testResultCode is success
387     std::shared_ptr<Attributes> result = nullptr;
388     nodeCallback->OnScheduleStoped(testResultCode, result);
389 }
390 
391 HWTEST_F(SimpleAuthContextTest, SimpleAuthContextTest_OnScheduleStoped_003, TestSize.Level0)
392 {
393     static const uint64_t testContestId = 2;
394     static const int32_t testResultCode = ResultCode::SUCCESS;
395 
396     std::shared_ptr<MockAuthentication> mockAuth = Common::MakeShared<MockAuthentication>();
397     ASSERT_NE(mockAuth, nullptr);
398     std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
399     ASSERT_NE(contextCallback, nullptr);
400     EXPECT_CALL(*contextCallback, OnResult(_, _))
401         .Times(Exactly(1))
__anon69ebc4fc0e02(int32_t resultCode, const Attributes &finalResult) 402         .WillOnce([](int32_t resultCode, const Attributes &finalResult) {
403             EXPECT_EQ(resultCode, ResultCode::GENERAL_ERROR);
404         });
405 
406     std::shared_ptr<ScheduleNodeCallback> nodeCallback =
407         Common::MakeShared<SimpleAuthContext>(testContestId, mockAuth, contextCallback);
408     ASSERT_NE(nodeCallback, nullptr);
409     // Error: ATTR_RESULT_CODE is not set
410     std::shared_ptr<Attributes> result = Common::MakeShared<Attributes>();
411     nodeCallback->OnScheduleStoped(testResultCode, result);
412 }
413 
414 HWTEST_F(SimpleAuthContextTest, SimpleAuthContextTest_OnScheduleStoped_004, TestSize.Level0)
415 {
416     static const uint64_t testContestId = 2;
417     static const int32_t testResultCode = ResultCode::SUCCESS;
418     static const std::vector<uint8_t> testScheduleResult = {3, 4, 5, 6};
419 
420     std::shared_ptr<MockAuthentication> mockAuth = Common::MakeShared<MockAuthentication>();
421     ASSERT_NE(mockAuth, nullptr);
422     EXPECT_CALL(*mockAuth, GetLatestError()).Times(1);
423     EXPECT_CALL(*mockAuth, Update(_, _))
424         .Times(Exactly(1))
__anon69ebc4fc0f02(const std::vector<uint8_t> &scheduleResult, Authentication::AuthResultInfo &resultInfo) 425         .WillOnce([](const std::vector<uint8_t> &scheduleResult, Authentication::AuthResultInfo &resultInfo) {
426             EXPECT_EQ(scheduleResult, testScheduleResult);
427             return false;
428         });
429     std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
430     ASSERT_NE(contextCallback, nullptr);
431     EXPECT_CALL(*contextCallback, OnResult(_, _))
432         .Times(Exactly(1))
__anon69ebc4fc1002(int32_t resultCode, const Attributes &finalResult) 433         .WillOnce([](int32_t resultCode, const Attributes &finalResult) {
434             EXPECT_EQ(resultCode, ResultCode::GENERAL_ERROR);
435         });
436 
437     std::shared_ptr<ScheduleNodeCallback> nodeCallback =
438         Common::MakeShared<SimpleAuthContext>(testContestId, mockAuth, contextCallback);
439     ASSERT_NE(nodeCallback, nullptr);
440     // Error: auth_->Update return false
441     std::shared_ptr<Attributes> result = Common::MakeShared<Attributes>();
442     ASSERT_NE(result, nullptr);
443     bool ret = result->SetUint8ArrayValue(Attributes::ATTR_RESULT, testScheduleResult);
444     ASSERT_EQ(ret, true);
445     nodeCallback->OnScheduleStoped(testResultCode, result);
446 }
447 
448 HWTEST_F(SimpleAuthContextTest, SimpleAuthContextTest_OnScheduleStoped_005, TestSize.Level0)
449 {
450     static const uint64_t testContestId = 2;
451     static const std::vector<uint8_t> testScheduleResult = {3, 4, 5, 6};
452     static const int32_t testResultCode = 7;
453     static const int32_t testFreezingTime = 8;
454     static const int32_t testRemainTimes = 9;
455     static const std::vector<uint8_t> testSignature = {10, 11, 12, 13};
456 
457     std::shared_ptr<MockAuthentication> mockAuth = Common::MakeShared<MockAuthentication>();
458     ASSERT_NE(mockAuth, nullptr);
459     EXPECT_CALL(*mockAuth, Update(_, _))
460         .Times(Exactly(1))
__anon69ebc4fc1102(const std::vector<uint8_t> &scheduleResult, Authentication::AuthResultInfo &resultInfo) 461         .WillOnce([](const std::vector<uint8_t> &scheduleResult, Authentication::AuthResultInfo &resultInfo) {
462             EXPECT_EQ(scheduleResult, testScheduleResult);
463             resultInfo.result = testResultCode;
464             resultInfo.freezingTime = testFreezingTime;
465             resultInfo.remainTimes = testRemainTimes;
466             resultInfo.token = testSignature;
467             return true;
468         });
469     std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
470     ASSERT_NE(contextCallback, nullptr);
471     EXPECT_CALL(*contextCallback, OnResult(_, _))
472         .Times(Exactly(1))
__anon69ebc4fc1202(int32_t resultCode, const Attributes &finalResult) 473         .WillOnce([](int32_t resultCode, const Attributes &finalResult) {
474             EXPECT_EQ(resultCode, testResultCode);
475             uint32_t attrResultCode;
476             int32_t freezingTime;
477             int32_t remainTimes;
478             vector<uint8_t> signature;
479             bool ret = finalResult.GetUint32Value(Attributes::ATTR_RESULT_CODE, attrResultCode);
480             EXPECT_EQ(ret, true);
481             ret = finalResult.GetInt32Value(Attributes::ATTR_FREEZING_TIME, freezingTime);
482             EXPECT_EQ(ret, true);
483             ret = finalResult.GetInt32Value(Attributes::ATTR_REMAIN_TIMES, remainTimes);
484             EXPECT_EQ(ret, true);
485             ret = finalResult.GetUint8ArrayValue(Attributes::ATTR_SIGNATURE, signature);
486             EXPECT_EQ(ret, true);
487 
488             EXPECT_EQ(resultCode, testResultCode);
489             EXPECT_EQ(freezingTime, testFreezingTime);
490             EXPECT_EQ(remainTimes, testRemainTimes);
491             EXPECT_EQ(signature, testSignature);
492         });
493 
494     std::shared_ptr<ScheduleNodeCallback> nodeCallback =
495         Common::MakeShared<SimpleAuthContext>(testContestId, mockAuth, contextCallback);
496     ASSERT_NE(nodeCallback, nullptr);
497     // Success
498     std::shared_ptr<Attributes> result = Common::MakeShared<Attributes>();
499     ASSERT_NE(result, nullptr);
500     bool ret = result->SetUint8ArrayValue(Attributes::ATTR_RESULT, testScheduleResult);
501     ASSERT_EQ(ret, true);
502     nodeCallback->OnScheduleStoped(testResultCode, result);
503 }
504 
505 HWTEST_F(SimpleAuthContextTest, SimpleAuthContextTest_OnScheduleStoped_006, TestSize.Level0)
506 {
507     static const uint64_t testContestId = 2;
508     static const std::vector<uint8_t> testScheduleResult = {3, 4, 5, 6};
509     static const int32_t testResultCode = 7;
510     static const int32_t testFreezingTime = 8;
511     static const int32_t testRemainTimes = 9;
512     static const std::vector<uint8_t> testSignature = {10, 11, 12, 13};
513 
514     std::shared_ptr<MockAuthentication> mockAuth = Common::MakeShared<MockAuthentication>();
515     ASSERT_NE(mockAuth, nullptr);
516     EXPECT_CALL(*mockAuth, Update(_, _))
517         .Times(Exactly(1))
__anon69ebc4fc1302(const std::vector<uint8_t> &scheduleResult, Authentication::AuthResultInfo &resultInfo) 518         .WillOnce([](const std::vector<uint8_t> &scheduleResult, Authentication::AuthResultInfo &resultInfo) {
519             EXPECT_EQ(scheduleResult, testScheduleResult);
520             resultInfo.result = testResultCode;
521             resultInfo.freezingTime = testFreezingTime;
522             resultInfo.remainTimes = testRemainTimes;
523             resultInfo.token = testSignature;
524             resultInfo.rootSecret = {1, 2, 3, 4};
525             return true;
526         });
527     std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
528     ASSERT_NE(contextCallback, nullptr);
529     EXPECT_CALL(*contextCallback, OnResult(_, _)).Times(1);
530 
531     std::shared_ptr<ScheduleNodeCallback> nodeCallback =
532         Common::MakeShared<SimpleAuthContext>(testContestId, mockAuth, contextCallback);
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_ContextFree, TestSize.Level0)
543 {
544     static const uint64_t testContestId = 2;
545 
546     std::shared_ptr<MockAuthentication> mockAuth = Common::MakeShared<MockAuthentication>();
547     ASSERT_NE(mockAuth, nullptr);
548     EXPECT_CALL(*mockAuth, Start(_, _))
549         .Times(Exactly(1))
550         .WillOnce([](std::vector<std::shared_ptr<ScheduleNode>> &scheduleList,
__anon69ebc4fc1402(std::vector<std::shared_ptr<ScheduleNode>> &scheduleList, std::shared_ptr<ScheduleNodeCallback> callback) 551                       std::shared_ptr<ScheduleNodeCallback> callback) {
552             EXPECT_EQ(scheduleList.size(), 0U);
553             auto faceAllInOne = MockResourceNode::CreateWithExecuteIndex(1, FACE, ALL_IN_ONE);
554             auto builder = ScheduleNode::Builder::New(faceAllInOne, faceAllInOne);
555             builder->SetScheduleCallback(callback);
556             auto scheduleNode = builder->Build();
557             EXPECT_NE(scheduleNode, nullptr);
558             scheduleList.push_back(scheduleNode);
559             return true;
560         });
561     std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
562     ASSERT_NE(contextCallback, nullptr);
563 
564     std::promise<void> promise;
565     EXPECT_CALL(*contextCallback, OnResult(_, _))
566         .Times(Exactly(1))
__anon69ebc4fc1502(int32_t resultCode, const Attributes &finalResult) 567         .WillOnce([&promise](int32_t resultCode, const Attributes &finalResult) {
568             EXPECT_EQ(resultCode, ResultCode::GENERAL_ERROR);
569             promise.set_value();
570         });
571 
572     std::weak_ptr<Context> weakContext;
573     {
574         std::shared_ptr<Context> context = Common::MakeShared<SimpleAuthContext>(testContestId,
575             mockAuth, contextCallback);
576         ASSERT_NE(context, nullptr);
577         weakContext = context;
578         context->Start();
579         promise.get_future().get();
580     }
581     std::this_thread::sleep_for(std::chrono::milliseconds(500));
582     ASSERT_EQ(weakContext.lock(), nullptr);
583 }
584 } // namespace UserAuth
585 } // namespace UserIam
586 } // namespace OHOS
587