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 "identify_context.h"
17
18 #include "mock_context.h"
19 #include "mock_identification.h"
20 #include "mock_schedule_node.h"
21
22 using namespace testing;
23 using namespace testing::ext;
24 namespace OHOS {
25 namespace UserIam {
26 namespace UserAuth {
27 class IdentifyContextTest : public testing::Test {
28 public:
29 static void SetUpTestCase();
30
31 static void TearDownTestCase();
32
33 void SetUp() override;
34
35 void TearDown() override;
36 };
37
SetUpTestCase()38 void IdentifyContextTest::SetUpTestCase()
39 {
40 }
41
TearDownTestCase()42 void IdentifyContextTest::TearDownTestCase()
43 {
44 }
45
SetUp()46 void IdentifyContextTest::SetUp()
47 {
48 }
49
TearDown()50 void IdentifyContextTest::TearDown()
51 {
52 }
53
54 HWTEST_F(IdentifyContextTest, IdentifyContextTest_NullHdi, TestSize.Level0)
55 {
56 const uint64_t testContestId = 2;
57 const int32_t testResultCode = 7;
58 const auto finalResult = Common::MakeShared<Attributes>();
59 ASSERT_NE(finalResult, nullptr);
60 std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
61 ASSERT_NE(contextCallback, nullptr);
62 EXPECT_CALL(*contextCallback, OnResult(_, _)).Times(Exactly(1));
63 // Error: identify is null
64 std::shared_ptr<Identification> identify = nullptr;
65
66 auto oriContext = Common::MakeShared<IdentifyContext>(testContestId, identify, contextCallback);
67 ASSERT_NE(oriContext, nullptr);
68 std::shared_ptr<Context> context = oriContext;
69 std::shared_ptr<ScheduleNodeCallback> nodeCallback = oriContext;
70
71 ASSERT_EQ(context->Start(), false);
72 ASSERT_EQ(context->Stop(), false);
73 nodeCallback->OnScheduleStoped(testResultCode, finalResult);
74 }
75
76 HWTEST_F(IdentifyContextTest, IdentifyContextTest_NullCallback, TestSize.Level0)
77 {
78 const uint64_t testContestId = 2;
79 const ExecutorRole testRole = static_cast<ExecutorRole>(3);
80 const int32_t testModuleType = 4;
81 const std::vector<uint8_t> testAcquireMsg = {4, 5, 6};
82 const int32_t testResultCode = 7;
83 const auto finalResult = Common::MakeShared<Attributes>();
84 ASSERT_NE(finalResult, nullptr);
85
86 std::shared_ptr<MockIdentification> mockIdentify = Common::MakeShared<MockIdentification>();
87 ASSERT_NE(mockIdentify, nullptr);
88 // Error: contextCallback is null
89 std::shared_ptr<ContextCallback> contextCallback = nullptr;
90
91 auto oriContext = Common::MakeShared<IdentifyContext>(testContestId, mockIdentify, contextCallback);
92 ASSERT_NE(oriContext, nullptr);
93 std::shared_ptr<ScheduleNodeCallback> nodeCallback = oriContext;
94
95 nodeCallback->OnScheduleStarted();
96 nodeCallback->OnScheduleProcessed(testRole, testModuleType, testAcquireMsg);
97 nodeCallback->OnScheduleStoped(testResultCode, finalResult);
98 }
99
100 HWTEST_F(IdentifyContextTest, IdentifyContextTest_BasicInfo, TestSize.Level0)
101 {
102 const uint64_t testContestId = 2;
103
104 std::shared_ptr<MockIdentification> mockIdentify = Common::MakeShared<MockIdentification>();
105 ASSERT_NE(mockIdentify, nullptr);
106 std::shared_ptr<ContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
107 ASSERT_NE(contextCallback, nullptr);
108
109 auto oriContext = Common::MakeShared<IdentifyContext>(testContestId, mockIdentify, contextCallback);
110 ASSERT_NE(oriContext, nullptr);
111 std::shared_ptr<Context> context = oriContext;
112
113 ASSERT_EQ(context->GetContextId(), testContestId);
114 ASSERT_EQ(context->GetContextType(), CONTEXT_IDENTIFY);
115 }
116
117 HWTEST_F(IdentifyContextTest, IdentifyContextTest_Start_001, TestSize.Level0)
118 {
119 static const uint64_t testContestId = 2;
120
121 std::shared_ptr<MockIdentification> mockIdentify = Common::MakeShared<MockIdentification>();
122 ASSERT_NE(mockIdentify, nullptr);
123 EXPECT_CALL(*mockIdentify, GetLatestError()).Times(1);
124 EXPECT_CALL(*mockIdentify, Start(_, _))
125 .Times(Exactly(1))
126 .WillOnce([](std::vector<std::shared_ptr<ScheduleNode>> &scheduleList,
__anona7a733dd0102(std::vector<std::shared_ptr<ScheduleNode>> &scheduleList, std::shared_ptr<ScheduleNodeCallback> callback) 127 std::shared_ptr<ScheduleNodeCallback> callback) {
128 // Error: process identification start fail
129 return false;
130 });
131 std::shared_ptr<ContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
132 ASSERT_NE(contextCallback, nullptr);
133 std::shared_ptr<Context> context = Common::MakeShared<IdentifyContext>(testContestId,
134 mockIdentify, contextCallback);
135 ASSERT_NE(context, nullptr);
136 ASSERT_EQ(context->Start(), false);
137 }
138
139 HWTEST_F(IdentifyContextTest, IdentifyContextTest_Start_002, TestSize.Level0)
140 {
141 static const uint64_t testContestId = 2;
142
143 std::shared_ptr<MockIdentification> mockIdentify = Common::MakeShared<MockIdentification>();
144 ASSERT_NE(mockIdentify, nullptr);
145 EXPECT_CALL(*mockIdentify, Start(_, _))
146 .Times(Exactly(1))
147 .WillOnce([](std::vector<std::shared_ptr<ScheduleNode>> &scheduleList,
__anona7a733dd0202(std::vector<std::shared_ptr<ScheduleNode>> &scheduleList, std::shared_ptr<ScheduleNodeCallback> callback) 148 std::shared_ptr<ScheduleNodeCallback> callback) {
149 // Error: scheduleNodeList size = 0
150 return true;
151 });
152 std::shared_ptr<ContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
153 ASSERT_NE(contextCallback, nullptr);
154 std::shared_ptr<Context> context = Common::MakeShared<IdentifyContext>(testContestId,
155 mockIdentify, contextCallback);
156 ASSERT_NE(context, nullptr);
157 ASSERT_EQ(context->Start(), false);
158 }
159
160 HWTEST_F(IdentifyContextTest, IdentifyContextTest_Start_003, TestSize.Level0)
161 {
162 static const uint64_t testContestId = 2;
163
164 std::shared_ptr<MockIdentification> mockIdentify = Common::MakeShared<MockIdentification>();
165 ASSERT_NE(mockIdentify, nullptr);
166 EXPECT_CALL(*mockIdentify, Start(_, _))
167 .Times(Exactly(1))
168 .WillOnce([](std::vector<std::shared_ptr<ScheduleNode>> &scheduleList,
__anona7a733dd0302(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<IdentifyContext>(testContestId,
178 mockIdentify, contextCallback);
179 ASSERT_NE(context, nullptr);
180 ASSERT_EQ(context->Start(), false);
181 }
182
183 HWTEST_F(IdentifyContextTest, IdentifyContextTest_Start_004, TestSize.Level0)
184 {
185 static const uint64_t testContestId = 2;
186
187 std::shared_ptr<MockIdentification> mockIdentify = Common::MakeShared<MockIdentification>();
188 ASSERT_NE(mockIdentify, nullptr);
189 EXPECT_CALL(*mockIdentify, Start(_, _))
190 .Times(Exactly(1))
191 .WillOnce([](std::vector<std::shared_ptr<ScheduleNode>> &scheduleList,
__anona7a733dd0402(std::vector<std::shared_ptr<ScheduleNode>> &scheduleList, std::shared_ptr<ScheduleNodeCallback> callback) 192 std::shared_ptr<ScheduleNodeCallback> callback) {
193 // Error: schedule node start fail
194 auto scheduleNode = Common::MakeShared<MockScheduleNode>();
195 EXPECT_CALL(*scheduleNode, StartSchedule()).Times(Exactly(1)).WillOnce(Return(false));
196 scheduleList.push_back(scheduleNode);
197 return true;
198 });
199 std::shared_ptr<ContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
200 ASSERT_NE(contextCallback, nullptr);
201 std::shared_ptr<Context> context = Common::MakeShared<IdentifyContext>(testContestId,
202 mockIdentify, contextCallback);
203 ASSERT_NE(context, nullptr);
204 ASSERT_EQ(context->Start(), false);
205 }
206
207 HWTEST_F(IdentifyContextTest, IdentifyContextTest_Start_005, TestSize.Level0)
208 {
209 static const uint64_t testContestId = 2;
210 static const uint64_t testScheduleId = 3;
211
212 std::shared_ptr<MockIdentification> mockIdentify = Common::MakeShared<MockIdentification>();
213 ASSERT_NE(mockIdentify, nullptr);
214 EXPECT_CALL(*mockIdentify, Start(_, _))
215 .Times(Exactly(1))
216 .WillOnce([](std::vector<std::shared_ptr<ScheduleNode>> &scheduleList,
__anona7a733dd0502(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<IdentifyContext>(testContestId,
229 mockIdentify, 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(IdentifyContextTest, IdentifyContextTest_Stop_001, TestSize.Level0)
240 {
241 static const uint64_t testContestId = 2;
242
243 std::shared_ptr<MockIdentification> mockIdentify = Common::MakeShared<MockIdentification>();
244 ASSERT_NE(mockIdentify, nullptr);
245 EXPECT_CALL(*mockIdentify, GetLatestError()).Times(1);
__anona7a733dd0602() 246 EXPECT_CALL(*mockIdentify, Cancel()).Times(Exactly(1)).WillOnce([]() {
247 // Error: identification 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<IdentifyContext>(testContestId,
253 mockIdentify, contextCallback);
254 ASSERT_NE(context, nullptr);
255 ASSERT_EQ(context->Stop(), false);
256 }
257
258 HWTEST_F(IdentifyContextTest, IdentifyContextTest_Stop_002, TestSize.Level0)
259 {
260 static const uint64_t testContestId = 2;
261
262 std::shared_ptr<MockIdentification> mockIdentify = Common::MakeShared<MockIdentification>();
263 ASSERT_NE(mockIdentify, nullptr);
__anona7a733dd0702() 264 EXPECT_CALL(*mockIdentify, Cancel()).Times(Exactly(1)).WillOnce([]() { return true; });
265 std::shared_ptr<ContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
266 ASSERT_NE(contextCallback, nullptr);
267 std::shared_ptr<Context> context = Common::MakeShared<IdentifyContext>(testContestId,
268 mockIdentify, contextCallback);
269 ASSERT_NE(context, nullptr);
270 ASSERT_EQ(context->Stop(), true);
271 }
272
273 HWTEST_F(IdentifyContextTest, IdentifyContextTest_OnScheduleStarted, TestSize.Level0)
274 {
275 static const uint64_t testContestId = 2;
276
277 std::shared_ptr<MockIdentification> mockIdentify = Common::MakeShared<MockIdentification>();
278 ASSERT_NE(mockIdentify, nullptr);
279 std::shared_ptr<ContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
280 ASSERT_NE(contextCallback, nullptr);
281
282 std::shared_ptr<ScheduleNodeCallback> nodeCallback =
283 Common::MakeShared<IdentifyContext>(testContestId, mockIdentify, contextCallback);
284 ASSERT_NE(nodeCallback, nullptr);
285 nodeCallback->OnScheduleStarted();
286 }
287
288 HWTEST_F(IdentifyContextTest, IdentifyContextTest_OnScheduleProcessed, TestSize.Level0)
289 {
290 EXPECT_EQ(0, 0);
291 }
292
293 HWTEST_F(IdentifyContextTest, IdentifyContextTest_OnScheduleStoped_001, TestSize.Level0)
294 {
295 static const uint64_t testContestId = 2;
296 static const int32_t testResultCode = 7;
297
298 std::shared_ptr<MockIdentification> mockIdentify = Common::MakeShared<MockIdentification>();
299 ASSERT_NE(mockIdentify, nullptr);
300 std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
301 ASSERT_NE(contextCallback, nullptr);
302 EXPECT_CALL(*contextCallback, OnResult(_, _))
303 .Times(Exactly(1))
__anona7a733dd0802(int32_t resultCode, const Attributes &finalResult) 304 .WillOnce([](int32_t resultCode, const Attributes &finalResult) {
305 EXPECT_EQ(resultCode, testResultCode);
306 });
307
308 std::shared_ptr<ScheduleNodeCallback> nodeCallback =
309 Common::MakeShared<IdentifyContext>(testContestId, mockIdentify, contextCallback);
310 ASSERT_NE(nodeCallback, nullptr);
311 // Error: result is null when testResultCode is not success
312 std::shared_ptr<Attributes> result = nullptr;
313 nodeCallback->OnScheduleStoped(testResultCode, result);
314 }
315
316 HWTEST_F(IdentifyContextTest, IdentifyContextTest_OnScheduleStoped_002, TestSize.Level0)
317 {
318 static const uint64_t testContestId = 2;
319 static const int32_t testResultCode = ResultCode::SUCCESS;
320
321 std::shared_ptr<MockIdentification> mockIdentify = Common::MakeShared<MockIdentification>();
322 ASSERT_NE(mockIdentify, nullptr);
323 std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
324 ASSERT_NE(contextCallback, nullptr);
325 EXPECT_CALL(*contextCallback, OnResult(_, _))
326 .Times(Exactly(1))
__anona7a733dd0902(int32_t resultCode, const Attributes &finalResult) 327 .WillOnce([](int32_t resultCode, const Attributes &finalResult) {
328 EXPECT_EQ(resultCode, ResultCode::GENERAL_ERROR);
329 });
330
331 std::shared_ptr<ScheduleNodeCallback> nodeCallback =
332 Common::MakeShared<IdentifyContext>(testContestId, mockIdentify, contextCallback);
333 ASSERT_NE(nodeCallback, nullptr);
334 // Error: result is null when testResultCode is success
335 std::shared_ptr<Attributes> result = nullptr;
336 nodeCallback->OnScheduleStoped(testResultCode, result);
337 }
338
339 HWTEST_F(IdentifyContextTest, IdentifyContextTest_OnScheduleStoped_003, TestSize.Level0)
340 {
341 static const uint64_t testContestId = 2;
342 static const int32_t testResultCode = ResultCode::SUCCESS;
343
344 std::shared_ptr<MockIdentification> mockIdentify = Common::MakeShared<MockIdentification>();
345 ASSERT_NE(mockIdentify, nullptr);
346 std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
347 ASSERT_NE(contextCallback, nullptr);
348 EXPECT_CALL(*contextCallback, OnResult(_, _))
349 .Times(Exactly(1))
__anona7a733dd0a02(int32_t resultCode, const Attributes &finalResult) 350 .WillOnce([](int32_t resultCode, const Attributes &finalResult) {
351 EXPECT_EQ(resultCode, ResultCode::GENERAL_ERROR);
352 });
353
354 std::shared_ptr<ScheduleNodeCallback> nodeCallback =
355 Common::MakeShared<IdentifyContext>(testContestId, mockIdentify, contextCallback);
356 ASSERT_NE(nodeCallback, nullptr);
357 // Error: ATTR_RESULT_CODE is not set
358 std::shared_ptr<Attributes> result = Common::MakeShared<Attributes>();
359 ASSERT_NE(result, nullptr);
360 nodeCallback->OnScheduleStoped(testResultCode, result);
361 }
362
363 HWTEST_F(IdentifyContextTest, IdentifyContextTest_OnScheduleStoped_004, TestSize.Level0)
364 {
365 static const uint64_t testContestId = 2;
366 static const int32_t testResultCode = ResultCode::SUCCESS;
367 static const std::vector<uint8_t> testScheduleResult = {3, 4, 5, 6};
368
369 std::shared_ptr<MockIdentification> mockIdentify = Common::MakeShared<MockIdentification>();
370 ASSERT_NE(mockIdentify, nullptr);
371 EXPECT_CALL(*mockIdentify, GetLatestError()).Times(1);
372 EXPECT_CALL(*mockIdentify, Update(_, _))
373 .Times(Exactly(1))
__anona7a733dd0b02(const std::vector<uint8_t> &scheduleResult, Identification::IdentifyResultInfo &resultInfo) 374 .WillOnce([](const std::vector<uint8_t> &scheduleResult, Identification::IdentifyResultInfo &resultInfo) {
375 EXPECT_EQ(scheduleResult, testScheduleResult);
376 return false;
377 });
378 std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
379 ASSERT_NE(contextCallback, nullptr);
380 EXPECT_CALL(*contextCallback, OnResult(_, _))
381 .Times(Exactly(1))
__anona7a733dd0c02(int32_t resultCode, const Attributes &finalResult) 382 .WillOnce([](int32_t resultCode, const Attributes &finalResult) {
383 EXPECT_EQ(resultCode, ResultCode::GENERAL_ERROR);
384 });
385
386 std::shared_ptr<ScheduleNodeCallback> nodeCallback =
387 Common::MakeShared<IdentifyContext>(testContestId, mockIdentify, contextCallback);
388 ASSERT_NE(nodeCallback, nullptr);
389 // Error: identify_->Update return false
390 std::shared_ptr<Attributes> result = Common::MakeShared<Attributes>();
391 ASSERT_NE(result, nullptr);
392 bool ret = result->SetUint8ArrayValue(Attributes::ATTR_RESULT, testScheduleResult);
393 ASSERT_EQ(ret, true);
394 nodeCallback->OnScheduleStoped(testResultCode, result);
395 }
396
397 HWTEST_F(IdentifyContextTest, IdentifyContextTest_OnScheduleStoped_005, TestSize.Level0)
398 {
399 static const uint64_t testContestId = 2;
400 static const std::vector<uint8_t> testScheduleResult = {3, 4, 5, 6};
401 static const int32_t testResultCode = 7;
402 static const int32_t testUserId = 8;
403 static const std::vector<uint8_t> testToken = {10, 11, 12, 13};
404
405 std::shared_ptr<MockIdentification> mockIdentify = Common::MakeShared<MockIdentification>();
406 ASSERT_NE(mockIdentify, nullptr);
407 EXPECT_CALL(*mockIdentify, Update(_, _))
408 .Times(Exactly(1))
__anona7a733dd0d02(const std::vector<uint8_t> &scheduleResult, Identification::IdentifyResultInfo &resultInfo) 409 .WillOnce([](const std::vector<uint8_t> &scheduleResult, Identification::IdentifyResultInfo &resultInfo) {
410 EXPECT_EQ(scheduleResult, testScheduleResult);
411 resultInfo.result = testResultCode;
412 resultInfo.userId = testUserId;
413 resultInfo.token = testToken;
414 return true;
415 });
416 std::shared_ptr<MockContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
417 ASSERT_NE(contextCallback, nullptr);
418 EXPECT_CALL(*contextCallback, OnResult(_, _))
419 .Times(Exactly(1))
__anona7a733dd0e02(int32_t resultCode, const Attributes &finalResult) 420 .WillOnce([](int32_t resultCode, const Attributes &finalResult) {
421 EXPECT_EQ(resultCode, testResultCode);
422 uint32_t attrResultCode;
423 int32_t userId;
424 vector<uint8_t> signature;
425 bool ret = finalResult.GetUint32Value(Attributes::ATTR_RESULT_CODE, attrResultCode);
426 EXPECT_EQ(ret, true);
427 ret = finalResult.GetInt32Value(Attributes::ATTR_USER_ID, userId);
428 EXPECT_EQ(ret, true);
429 ret = finalResult.GetUint8ArrayValue(Attributes::ATTR_SIGNATURE, signature);
430 EXPECT_EQ(ret, true);
431
432 EXPECT_EQ(resultCode, testResultCode);
433 EXPECT_EQ(userId, testUserId);
434 EXPECT_EQ(signature, testToken);
435 });
436
437 std::shared_ptr<ScheduleNodeCallback> nodeCallback =
438 Common::MakeShared<IdentifyContext>(testContestId, mockIdentify, contextCallback);
439 ASSERT_NE(nodeCallback, nullptr);
440 // Success
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 } // namespace UserAuth
448 } // namespace UserIam
449 } // namespace OHOS
450