1 /*
2 * Copyright (C) 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 "widget_context.h"
17
18 #include <future>
19
20 #include "mock_context.h"
21
22 #include "schedule_node_impl.h"
23 #include "widget_context_callback_impl.h"
24 #include "relative_timer.h"
25
26 using namespace testing;
27 using namespace testing::ext;
28 namespace OHOS {
29 namespace UserIam {
30 namespace UserAuth {
31 namespace {
32 auto &timer = RelativeTimer::GetInstance();
33 }
34 class WidgetContextTest : public testing::Test {
35 public:
36 static void SetUpTestCase();
37
38 static void TearDownTestCase();
39
40 void SetUp() override;
41
42 void TearDown() override;
43 };
44
SetUpTestCase()45 void WidgetContextTest::SetUpTestCase()
46 {
47 }
48
TearDownTestCase()49 void WidgetContextTest::TearDownTestCase()
50 {
51 }
52
SetUp()53 void WidgetContextTest::SetUp()
54 {
55 }
56
TearDown()57 void WidgetContextTest::TearDown()
58 {
59 }
60
CreateWidgetContext(uint64_t contextId,ContextFactory::AuthWidgetContextPara para)61 std::shared_ptr<WidgetContext> CreateWidgetContext(uint64_t contextId, ContextFactory::AuthWidgetContextPara para)
62 {
63 std::shared_ptr<ContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
64 return Common::MakeShared<WidgetContext>(contextId, para, contextCallback);
65 }
66
67 HWTEST_F(WidgetContextTest, WidgetContextTestStart, TestSize.Level0)
68 {
69 uint64_t contextId = 1;
70 ContextFactory::AuthWidgetContextPara para;
71 auto widgetContext = CreateWidgetContext(contextId, para);
72 EXPECT_TRUE(widgetContext->Start());
73 EXPECT_FALSE(widgetContext->Start());
74 auto handler = ThreadHandler::GetSingleThreadInstance();
__anonf90c979c0202() 75 handler->EnsureTask([]() {});
76 }
77
78 HWTEST_F(WidgetContextTest, WidgetContextTestStop, TestSize.Level0)
79 {
80 uint64_t contextId = 1;
81 ContextFactory::AuthWidgetContextPara para;
82 auto widgetContext = CreateWidgetContext(contextId, para);
83 EXPECT_TRUE(widgetContext->Stop());
84 auto handler = ThreadHandler::GetSingleThreadInstance();
__anonf90c979c0302() 85 handler->EnsureTask([]() {});
86 }
87
88 HWTEST_F(WidgetContextTest, WidgetContextTestGetScheduleNode, TestSize.Level0)
89 {
90 uint64_t contextId = 1;
91 ContextFactory::AuthWidgetContextPara para;
92 auto widgetContext = CreateWidgetContext(contextId, para);
93 EXPECT_EQ(widgetContext->GetScheduleNode(contextId), nullptr);
94 auto handler = ThreadHandler::GetSingleThreadInstance();
__anonf90c979c0402() 95 handler->EnsureTask([]() {});
96 }
97
98 HWTEST_F(WidgetContextTest, WidgetContextTestGetLatestError, TestSize.Level0)
99 {
100 uint64_t contextId = 1;
101 ContextFactory::AuthWidgetContextPara para;
102 auto widgetContext = CreateWidgetContext(contextId, para);
103 EXPECT_EQ(widgetContext->GetLatestError(), ResultCode::GENERAL_ERROR);
104 auto handler = ThreadHandler::GetSingleThreadInstance();
__anonf90c979c0502() 105 handler->EnsureTask([]() {});
106 }
107
108 HWTEST_F(WidgetContextTest, WidgetContextTestGetContextType, TestSize.Level0)
109 {
110 uint64_t contextId = 1;
111 ContextFactory::AuthWidgetContextPara para;
112 auto widgetContext = CreateWidgetContext(contextId, para);
113 EXPECT_EQ(widgetContext->GetContextType(), WIDGET_AUTH_CONTEXT);
114 auto handler = ThreadHandler::GetSingleThreadInstance();
__anonf90c979c0602() 115 handler->EnsureTask([]() {});
116 }
117
118 HWTEST_F(WidgetContextTest, WidgetContextTestGetTokenId, TestSize.Level0)
119 {
120 uint64_t contextId = 1;
121 ContextFactory::AuthWidgetContextPara para;
122 auto widgetContext = CreateWidgetContext(contextId, para);
123 EXPECT_EQ(widgetContext->GetTokenId(), (uint32_t)0);
124 auto handler = ThreadHandler::GetSingleThreadInstance();
__anonf90c979c0702() 125 handler->EnsureTask([]() {});
126 }
127
128 HWTEST_F(WidgetContextTest, WidgetContextTestAuthResult_0001, TestSize.Level0)
129 {
130 uint64_t contextId = 1;
131 ContextFactory::AuthWidgetContextPara para;
132 auto widgetContext = CreateWidgetContext(contextId, para);
133 EXPECT_NE(widgetContext, nullptr);
134 Attributes finalResult;
135 widgetContext->AuthResult(ResultCode::SUCCESS, 1, finalResult);
136 auto handler = ThreadHandler::GetSingleThreadInstance();
__anonf90c979c0802() 137 handler->EnsureTask([]() {});
138 }
139
140 HWTEST_F(WidgetContextTest, WidgetContextTestAuthResult_0002, TestSize.Level0)
141 {
142 uint64_t contextId = 1;
143 ContextFactory::AuthWidgetContextPara para;
144 auto widgetContext = CreateWidgetContext(contextId, para);
145 EXPECT_NE(widgetContext, nullptr);
146 Attributes finalResult;
147 finalResult.SetInt32Value(Attributes::ATTR_REMAIN_TIMES, 1);
148 widgetContext->AuthResult(ResultCode::SUCCESS, 1, finalResult);
149 auto handler = ThreadHandler::GetSingleThreadInstance();
__anonf90c979c0902() 150 handler->EnsureTask([]() {});
151 }
152
153 HWTEST_F(WidgetContextTest, WidgetContextTestAuthResult_0003, TestSize.Level0)
154 {
155 uint64_t contextId = 1;
156 ContextFactory::AuthWidgetContextPara para;
157 auto widgetContext = CreateWidgetContext(contextId, para);
158 EXPECT_NE(widgetContext, nullptr);
159 Attributes finalResult;
160 finalResult.SetInt32Value(Attributes::ATTR_REMAIN_TIMES, 1);
161 finalResult.SetInt32Value(Attributes::ATTR_FREEZING_TIME, 1);
162 widgetContext->AuthResult(ResultCode::SUCCESS, 1, finalResult);
163 auto handler = ThreadHandler::GetSingleThreadInstance();
__anonf90c979c0a02() 164 handler->EnsureTask([]() {});
165 }
166
167 HWTEST_F(WidgetContextTest, WidgetContextTestAuthResult_0004, TestSize.Level0)
168 {
169 uint64_t contextId = 1;
170 ContextFactory::AuthWidgetContextPara para;
171 auto widgetContext = CreateWidgetContext(contextId, para);
172 EXPECT_NE(widgetContext, nullptr);
173 EXPECT_TRUE(widgetContext->Start());
174 Attributes finalResult;
175 finalResult.SetInt32Value(Attributes::ATTR_REMAIN_TIMES, 1);
176 finalResult.SetInt32Value(Attributes::ATTR_FREEZING_TIME, 1);
177 widgetContext->AuthResult(ResultCode::SUCCESS, 1, finalResult);
178 auto handler = ThreadHandler::GetSingleThreadInstance();
__anonf90c979c0b02() 179 handler->EnsureTask([]() {});
180 }
181
182 HWTEST_F(WidgetContextTest, WidgetContextTestAuthResult_0005, TestSize.Level0)
183 {
184 uint64_t contextId = 1;
185 ContextFactory::AuthWidgetContextPara para;
186 auto widgetContext = CreateWidgetContext(contextId, para);
187 EXPECT_NE(widgetContext, nullptr);
188 EXPECT_TRUE(widgetContext->Start());
189 Attributes finalResult;
190 finalResult.SetInt32Value(Attributes::ATTR_REMAIN_TIMES, 1);
191 finalResult.SetInt32Value(Attributes::ATTR_FREEZING_TIME, 1);
192 widgetContext->AuthResult(ResultCode::GENERAL_ERROR, 1, finalResult);
193 auto handler = ThreadHandler::GetSingleThreadInstance();
__anonf90c979c0c02() 194 handler->EnsureTask([]() {});
195 }
196
197 HWTEST_F(WidgetContextTest, WidgetContextTestLaunchWidget_001, TestSize.Level0)
198 {
199 uint64_t contextId = 1;
200 ContextFactory::AuthWidgetContextPara para;
201 auto widgetContext = CreateWidgetContext(contextId, para);
202 widgetContext->LaunchWidget();
203 EXPECT_NE(widgetContext, nullptr);
204 auto handler = ThreadHandler::GetSingleThreadInstance();
__anonf90c979c0d02() 205 handler->EnsureTask([]() {});
206 }
207
208 HWTEST_F(WidgetContextTest, WidgetContextTestLaunchWidget_002, TestSize.Level0)
209 {
210 uint64_t contextId = 1;
211 ContextFactory::AuthWidgetContextPara para;
212 ContextFactory::AuthProfile contextPara;
213 para.authProfileMap[PIN] = contextPara;
214 auto widgetContext = CreateWidgetContext(contextId, para);
215 widgetContext->LaunchWidget();
216 EXPECT_NE(widgetContext, nullptr);
217 auto handler = ThreadHandler::GetSingleThreadInstance();
__anonf90c979c0e02() 218 handler->EnsureTask([]() {});
219 }
220
221 HWTEST_F(WidgetContextTest, WidgetContextTestLaunchWidget_003, TestSize.Level0)
222 {
223 uint64_t contextId = 1;
224 ContextFactory::AuthWidgetContextPara para;
225 ContextFactory::AuthProfile authProfile;
226 authProfile.sensorInfo = "1";
227 para.authProfileMap[AuthType::FINGERPRINT] = authProfile;
228 auto widgetContext = CreateWidgetContext(contextId, para);
229 widgetContext->LaunchWidget();
230 EXPECT_NE(widgetContext, nullptr);
231 auto handler = ThreadHandler::GetSingleThreadInstance();
__anonf90c979c0f02() 232 handler->EnsureTask([]() {});
233 }
234
235 HWTEST_F(WidgetContextTest, WidgetContextTestEndAuthAsCancel, TestSize.Level0)
236 {
237 uint64_t contextId = 1;
238 ContextFactory::AuthWidgetContextPara para;
239 auto widgetContext = CreateWidgetContext(contextId, para);
240 widgetContext->EndAuthAsCancel();
241 EXPECT_NE(widgetContext, nullptr);
242 auto handler = ThreadHandler::GetSingleThreadInstance();
__anonf90c979c1002() 243 handler->EnsureTask([]() {});
244 }
245
246 HWTEST_F(WidgetContextTest, WidgetContextTestEndAuthAsNaviPin, TestSize.Level0)
247 {
248 uint64_t contextId = 1;
249 ContextFactory::AuthWidgetContextPara para;
250 auto widgetContext = CreateWidgetContext(contextId, para);
251 widgetContext->EndAuthAsNaviPin();
252 EXPECT_NE(widgetContext, nullptr);
253 auto handler = ThreadHandler::GetSingleThreadInstance();
__anonf90c979c1102() 254 handler->EnsureTask([]() {});
255 }
256
257 HWTEST_F(WidgetContextTest, WidgetContextTestEndAuthAsWidgetParaInvalid, TestSize.Level0)
258 {
259 uint64_t contextId = 1;
260 ContextFactory::AuthWidgetContextPara para;
261 auto widgetContext = CreateWidgetContext(contextId, para);
262 widgetContext->EndAuthAsWidgetParaInvalid();
263 EXPECT_NE(widgetContext, nullptr);
264 auto handler = ThreadHandler::GetSingleThreadInstance();
__anonf90c979c1202() 265 handler->EnsureTask([]() {});
266 }
267
268 HWTEST_F(WidgetContextTest, WidgetContextTestStopAuthList_001, TestSize.Level0)
269 {
270 uint64_t contextId = 1;
271 ContextFactory::AuthWidgetContextPara para;
272 auto widgetContext = CreateWidgetContext(contextId, para);
273 std::vector<AuthType> authTypeList;
274 widgetContext->StopAuthList(authTypeList);
275 EXPECT_NE(widgetContext, nullptr);
276 auto handler = ThreadHandler::GetSingleThreadInstance();
__anonf90c979c1302() 277 handler->EnsureTask([]() {});
278 }
279
280 HWTEST_F(WidgetContextTest, WidgetContextTestStopAuthList_002, TestSize.Level0)
281 {
282 uint64_t contextId = 1;
283 ContextFactory::AuthWidgetContextPara para;
284 auto widgetContext = CreateWidgetContext(contextId, para);
285 std::vector<AuthType> authTypeList = {ALL, PIN, FACE};
286 widgetContext->StopAuthList(authTypeList);
287 EXPECT_NE(widgetContext, nullptr);
288 auto handler = ThreadHandler::GetSingleThreadInstance();
__anonf90c979c1402() 289 handler->EnsureTask([]() {});
290 }
291
292 HWTEST_F(WidgetContextTest, WidgetContextTestStopAuthList_003, TestSize.Level0)
293 {
294 uint64_t contextId = 1;
295 ContextFactory::AuthWidgetContextPara para;
296 para.challenge = {0, 1};
297 para.atl = ATL2;
298 std::shared_ptr<ContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
299 auto widgetContext = CreateWidgetContext(contextId, para);
300 std::set<AuthType> authTypeList;
301 authTypeList.insert(FACE);
302 authTypeList.insert(ALL);
303 widgetContext->ExecuteAuthList(authTypeList, false, AuthIntent::DEFAULT);
304
305 std::vector<AuthType> testTypeList = {ALL, PIN, FACE};
306 widgetContext->StopAuthList(testTypeList);
307 EXPECT_NE(widgetContext, nullptr);
308 auto handler = ThreadHandler::GetSingleThreadInstance();
__anonf90c979c1502() 309 handler->EnsureTask([]() {});
310 }
311
312 HWTEST_F(WidgetContextTest, WidgetContextTestSuccessAuth_001, TestSize.Level0)
313 {
314 uint64_t contextId = 1;
315 ContextFactory::AuthWidgetContextPara para;
316 auto widgetContext = CreateWidgetContext(contextId, para);
317 AuthType authType = ALL;
318 widgetContext->SuccessAuth(authType);
319 EXPECT_NE(widgetContext, nullptr);
320 auto handler = ThreadHandler::GetSingleThreadInstance();
__anonf90c979c1602() 321 handler->EnsureTask([]() {});
322 }
323
324 HWTEST_F(WidgetContextTest, WidgetContextTestSuccessAuth_002, TestSize.Level0)
325 {
326 uint64_t contextId = 1;
327 ContextFactory::AuthWidgetContextPara para;
328 auto widgetContext = CreateWidgetContext(contextId, para);
329 AuthType authType = ALL;
330 widgetContext->LaunchWidget();
331 widgetContext->SuccessAuth(authType);
332 EXPECT_NE(widgetContext, nullptr);
333 auto handler = ThreadHandler::GetSingleThreadInstance();
__anonf90c979c1702() 334 handler->EnsureTask([]() {});
335 }
336
337 HWTEST_F(WidgetContextTest, WidgetContextTestSuccessAuth_003, TestSize.Level0)
338 {
339 uint64_t contextId = 1;
340 ContextFactory::AuthWidgetContextPara para;
341 para.challenge = {0, 1};
342 para.atl = ATL2;
343 std::shared_ptr<ContextCallback> contextCallback = Common::MakeShared<MockContextCallback>();
344 auto widgetContext = CreateWidgetContext(contextId, para);
345 std::set<AuthType> authTypeList;
346 authTypeList.insert(FACE);
347 widgetContext->ExecuteAuthList(authTypeList, true, AuthIntent::DEFAULT);
348 AuthType authType = ALL;
349 widgetContext->SuccessAuth(authType);
350 EXPECT_NE(widgetContext, nullptr);
351 auto handler = ThreadHandler::GetSingleThreadInstance();
__anonf90c979c1802() 352 handler->EnsureTask([]() {});
353 }
354
355 HWTEST_F(WidgetContextTest, WidgetContextTestExecuteAuthList_0001, TestSize.Level0)
356 {
357 uint64_t contextId = 1;
358 ContextFactory::AuthWidgetContextPara para;
359 auto widgetContext = Common::MakeShared<WidgetContext>(contextId, para, nullptr);
360 std::set<AuthType> authTypeList;
361 widgetContext->ExecuteAuthList(authTypeList, false, AuthIntent::DEFAULT);
362 EXPECT_NE(widgetContext, nullptr);
363 auto handler = ThreadHandler::GetSingleThreadInstance();
__anonf90c979c1902() 364 handler->EnsureTask([]() {});
365 }
366
367 HWTEST_F(WidgetContextTest, WidgetContextTestExecuteAuthList_0002, TestSize.Level0)
368 {
369 uint64_t contextId = 1;
370 ContextFactory::AuthWidgetContextPara para;
371 auto widgetContext = Common::MakeShared<WidgetContext>(contextId, para, nullptr);
372 std::set<AuthType> authTypeList;
373 authTypeList.insert(AuthType::PIN);
374 widgetContext->ExecuteAuthList(authTypeList, true, AuthIntent::DEFAULT);
375 EXPECT_NE(widgetContext, nullptr);
376 auto handler = ThreadHandler::GetSingleThreadInstance();
__anonf90c979c1a02() 377 handler->EnsureTask([]() {});
378 }
379
380 HWTEST_F(WidgetContextTest, WidgetContextTestExecuteAuthList_0003, TestSize.Level0)
381 {
382 uint64_t contextId = 1;
383 ContextFactory::AuthWidgetContextPara para;
384 para.challenge = {0, 1};
385 para.atl = AuthTrustLevel::ATL1;
386 auto contextCallback = Common::MakeShared<MockContextCallback>();
387 auto widgetContext = Common::MakeShared<WidgetContext>(contextId, para, contextCallback);
388 std::set<AuthType> authTypeList;
389 authTypeList.insert(AuthType::PIN);
390 widgetContext->ExecuteAuthList(authTypeList, false, AuthIntent::DEFAULT);
391 EXPECT_NE(widgetContext, nullptr);
392 auto handler = ThreadHandler::GetSingleThreadInstance();
__anonf90c979c1b02() 393 handler->EnsureTask([]() {});
394 }
395
396 HWTEST_F(WidgetContextTest, WidgetContextTestAuthWidgetReloadInit, TestSize.Level0)
397 {
398 uint64_t contextId = 1;
399 ContextFactory::AuthWidgetContextPara para;
400 auto widgetContext = CreateWidgetContext(contextId, para);
401 widgetContext->AuthWidgetReloadInit();
402 EXPECT_NE(widgetContext, nullptr);
403 auto handler = ThreadHandler::GetSingleThreadInstance();
__anonf90c979c1c02() 404 handler->EnsureTask([]() {});
405 }
406
407 HWTEST_F(WidgetContextTest, WidgetContextTestAuthWidgetReload_0001, TestSize.Level0)
408 {
409 uint64_t contextId = 1;
410 ContextFactory::AuthWidgetContextPara para;
411 auto widgetContext = CreateWidgetContext(contextId, para);
412 uint32_t orientation = 1;
413 uint32_t needRotate = 1;
414 uint32_t alreadyLoad = 1;
415 AuthType rotateAuthType = PIN;
416 widgetContext->AuthWidgetReload(orientation, needRotate, alreadyLoad, rotateAuthType);
417 EXPECT_NE(widgetContext, nullptr);
418 auto handler = ThreadHandler::GetSingleThreadInstance();
__anonf90c979c1d02() 419 handler->EnsureTask([]() {});
420 }
421
422 HWTEST_F(WidgetContextTest, WidgetContextTestAuthWidgetReload_0002, TestSize.Level0)
423 {
424 uint64_t contextId = 1;
425 ContextFactory::AuthWidgetContextPara para;
426 auto widgetContext = CreateWidgetContext(contextId, para);
427 uint32_t orientation = 2;
428 uint32_t needRotate = 1;
429 uint32_t alreadyLoad = 1;
430 AuthType rotateAuthType = FINGERPRINT;
431 widgetContext->AuthWidgetReload(orientation, needRotate, alreadyLoad, rotateAuthType);
432 EXPECT_NE(widgetContext, nullptr);
433 auto handler = ThreadHandler::GetSingleThreadInstance();
__anonf90c979c1e02() 434 handler->EnsureTask([]() {});
435 }
436
437 HWTEST_F(WidgetContextTest, WidgetContextTestAuthWidgetReload_0003, TestSize.Level0)
438 {
439 uint64_t contextId = 1;
440 ContextFactory::AuthWidgetContextPara para;
441 auto widgetContext = CreateWidgetContext(contextId, para);
442 uint32_t orientation = 3;
443 uint32_t needRotate = 1;
444 uint32_t alreadyLoad = 1;
445 AuthType rotateAuthType = FACE;
446 widgetContext->AuthWidgetReload(orientation, needRotate, alreadyLoad, rotateAuthType);
447 EXPECT_NE(widgetContext, nullptr);
448 auto handler = ThreadHandler::GetSingleThreadInstance();
__anonf90c979c1f02() 449 handler->EnsureTask([]() {});
450 }
451 } // namespace UserAuth
452 } // namespace UserIam
453 } // namespace OHOS
454