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_client.h"
17 
18 #include <future>
19 #include "auth_common.h"
20 #include "iam_check.h"
21 #include "iam_ptr.h"
22 #include "widget_json.h"
23 #include "widget_callback_interface.h"
24 
25 #include "mock_authentication.h"
26 #include "mock_context.h"
27 #include "mock_resource_node.h"
28 #include "mock_schedule_node.h"
29 #include "mock_widget_schedule_node.h"
30 #include "mock_widget_callback_interface.h"
31 #include "schedule_node_impl.h"
32 #include "user_auth_callback_proxy.h"
33 #include "widget_schedule_node.h"
34 #include "widget_callback_proxy.h"
35 
36 using namespace testing;
37 using namespace testing::ext;
38 namespace OHOS {
39 namespace UserIam {
40 namespace UserAuth {
41 
42 class WidgetClientTest : public testing::Test {
43 public:
44     static void SetUpTestCase();
45 
46     static void TearDownTestCase();
47 
48     void SetUp() override;
49 
50     void TearDown() override;
51 
52     std::shared_ptr<WidgetScheduleNode> BuildSchedule();
53 };
54 
SetUpTestCase()55 void WidgetClientTest::SetUpTestCase()
56 {
57 }
58 
TearDownTestCase()59 void WidgetClientTest::TearDownTestCase()
60 {
61 }
62 
SetUp()63 void WidgetClientTest::SetUp()
64 {
65 }
66 
TearDown()67 void WidgetClientTest::TearDown()
68 {
69 }
70 
BuildSchedule()71 std::shared_ptr<WidgetScheduleNode> WidgetClientTest::BuildSchedule()
72 {
73     auto schedule = Common::MakeShared<MockWidgetScheduleNode>();
74     EXPECT_NE(schedule, nullptr);
75     EXPECT_CALL(*schedule, StartSchedule).WillRepeatedly(Return(true));
76     EXPECT_CALL(*schedule, StopSchedule).WillRepeatedly(Return(true));
77     EXPECT_CALL(*schedule, StartAuthList).WillRepeatedly(Return(true));
78     EXPECT_CALL(*schedule, StopAuthList).WillRepeatedly(Return(true));
79     EXPECT_CALL(*schedule, SuccessAuth).WillRepeatedly(Return(true));
80     EXPECT_CALL(*schedule, NaviPinAuth).WillRepeatedly(Return(true));
81     EXPECT_CALL(*schedule, WidgetReload).WillRepeatedly(Return(true));
82     return schedule;
83 }
84 
85 HWTEST_F(WidgetClientTest, WidgetClientTestSetWidgetSchedule_0001, TestSize.Level0)
86 {
87     auto schedule = BuildSchedule();
88     WidgetClient::Instance().SetWidgetSchedule(schedule);
89     EXPECT_NE(schedule, nullptr);
90 }
91 
92 HWTEST_F(WidgetClientTest, WidgetClientTestSetWidgetSchedule_0002, TestSize.Level0)
93 {
94     std::shared_ptr<WidgetScheduleNode> nullSchedule(nullptr);
95     WidgetClient::Instance().SetWidgetSchedule(nullSchedule);
96     EXPECT_EQ(nullSchedule, nullptr);
97 }
98 
99 HWTEST_F(WidgetClientTest, WidgetClientTestSetWidgetParam, TestSize.Level0)
100 {
101     WidgetParam widgetParam;
102     WidgetClient::Instance().SetWidgetParam(widgetParam);
103     EXPECT_EQ(widgetParam.title, "");
104 }
105 
106 HWTEST_F(WidgetClientTest, WidgetClientTestSetWidgetCallback, TestSize.Level0)
107 {
108     sptr<WidgetCallbackInterface> testCallback = nullptr;
109     WidgetClient::Instance().SetWidgetCallback(testCallback);
110     EXPECT_EQ(testCallback, nullptr);
111 }
112 
113 HWTEST_F(WidgetClientTest, WidgetClientTestSetAuthTokenId, TestSize.Level0)
114 {
115     uint32_t tokenId = 1;
116     WidgetClient::Instance().SetAuthTokenId(tokenId);
117     EXPECT_EQ(WidgetClient::Instance().GetAuthTokenId(), tokenId);
118 }
119 
120 HWTEST_F(WidgetClientTest, WidgetClientTestOnNotice_0001, TestSize.Level0)
121 {
122     std::string eventData = "";
123     WidgetClient::Instance().Reset();
124     EXPECT_EQ(WidgetClient::Instance().OnNotice((NoticeType)0, eventData), ResultCode::INVALID_PARAMETERS);
125 }
126 
127 HWTEST_F(WidgetClientTest, WidgetClientTestOnNotice_0002, TestSize.Level0)
128 {
129     std::string eventData = "";
130     WidgetClient::Instance().Reset();
131     EXPECT_EQ(WidgetClient::Instance().OnNotice(NoticeType::WIDGET_NOTICE, eventData), ResultCode::INVALID_PARAMETERS);
132 }
133 
134 HWTEST_F(WidgetClientTest, WidgetClientTestOnNotice_0003, TestSize.Level0)
135 {
136     std::string eventData = "invalid_json_string";
137     WidgetClient::Instance().Reset();
138     EXPECT_EQ(WidgetClient::Instance().OnNotice(NoticeType::WIDGET_NOTICE, eventData), ResultCode::INVALID_PARAMETERS);
139 }
140 
141 HWTEST_F(WidgetClientTest, WidgetClientTestOnNotice_0004, TestSize.Level0)
142 {
143     WidgetNotice widgetNotice;
144     nlohmann::json root = widgetNotice;
145     std::string eventData = root.dump();
146     WidgetClient::Instance().Reset();
147     EXPECT_EQ(WidgetClient::Instance().OnNotice(NoticeType::WIDGET_NOTICE, eventData), ResultCode::INVALID_PARAMETERS);
148 }
149 
150 HWTEST_F(WidgetClientTest, WidgetClientTestOnNotice_0005, TestSize.Level0)
151 {
152     WidgetNotice widgetNotice;
153     widgetNotice.widgetContextId = 1;
154     widgetNotice.event = "INVALID_EVENT_AUTH_TYPE";
155     nlohmann::json root = widgetNotice;
156     std::string eventData = root.dump();
157     WidgetClient::Instance().Reset();
158     EXPECT_EQ(WidgetClient::Instance().OnNotice(NoticeType::WIDGET_NOTICE, eventData), ResultCode::INVALID_PARAMETERS);
159 }
160 
161 HWTEST_F(WidgetClientTest, WidgetClientTestOnNotice_0006, TestSize.Level0)
162 {
163     WidgetNotice widgetNotice;
164     widgetNotice.widgetContextId = 1;
165     widgetNotice.event = NOTICE_EVENT_AUTH_READY;
166     nlohmann::json root = widgetNotice;
167     std::string eventData = root.dump();
168     WidgetClient::Instance().Reset();
169     EXPECT_EQ(WidgetClient::Instance().OnNotice(NoticeType::WIDGET_NOTICE, eventData), ResultCode::GENERAL_ERROR);
170 }
171 
172 HWTEST_F(WidgetClientTest, WidgetClientTestOnNotice_0007, TestSize.Level0)
173 {
174     WidgetNotice widgetNotice;
175     widgetNotice.widgetContextId = 1;
176     widgetNotice.event = NOTICE_EVENT_AUTH_READY;
177     nlohmann::json root = widgetNotice;
178     std::string eventData = root.dump();
179     WidgetClient::Instance().Reset();
180     std::vector<AuthType> authTypeList;
181     WidgetClient::Instance().SetAuthTypeList(authTypeList);
182     WidgetClient::Instance().SetWidgetSchedule(BuildSchedule());
183     EXPECT_EQ(WidgetClient::Instance().OnNotice(NoticeType::WIDGET_NOTICE, eventData), ResultCode::INVALID_PARAMETERS);
184 }
185 
186 HWTEST_F(WidgetClientTest, WidgetClientTestOnNotice_0008, TestSize.Level0)
187 {
188     WidgetNotice widgetNotice;
189     widgetNotice.widgetContextId = 1;
190     widgetNotice.event = NOTICE_EVENT_AUTH_READY;
191     nlohmann::json root = widgetNotice;
192     std::string eventData = root.dump();
193     WidgetClient::Instance().Reset();
194     std::vector<AuthType> authTypeList;
195     authTypeList.emplace_back(AuthType::PIN);
196     WidgetClient::Instance().SetAuthTypeList(authTypeList);
197     WidgetClient::Instance().SetWidgetSchedule(BuildSchedule());
198     EXPECT_EQ(WidgetClient::Instance().OnNotice(NoticeType::WIDGET_NOTICE, eventData), ResultCode::INVALID_PARAMETERS);
199 }
200 
201 HWTEST_F(WidgetClientTest, WidgetClientTestOnNotice_0009, TestSize.Level0)
202 {
203     WidgetNotice widgetNotice;
204     widgetNotice.widgetContextId = 1;
205     widgetNotice.event = NOTICE_EVENT_AUTH_READY;
206     widgetNotice.typeList.push_back("all");
207     nlohmann::json root = widgetNotice;
208     std::string eventData = root.dump();
209     WidgetClient::Instance().Reset();
210     std::vector<AuthType> authTypeList;
211     authTypeList.emplace_back(AuthType::PIN);
212     WidgetClient::Instance().SetAuthTypeList(authTypeList);
213     WidgetClient::Instance().SetWidgetSchedule(BuildSchedule());
214     EXPECT_EQ(WidgetClient::Instance().OnNotice(NoticeType::WIDGET_NOTICE, eventData), ResultCode::INVALID_PARAMETERS);
215 }
216 
217 HWTEST_F(WidgetClientTest, WidgetClientTestOnNotice_0010, TestSize.Level0)
218 {
219     WidgetNotice widgetNotice;
220     widgetNotice.widgetContextId = 1;
221     widgetNotice.event = NOTICE_EVENT_AUTH_READY;
222     widgetNotice.typeList.push_back("pin");
223     nlohmann::json root = widgetNotice;
224     std::string eventData = root.dump();
225     WidgetClient::Instance().Reset();
226     std::vector<AuthType> authTypeList;
227     authTypeList.emplace_back(AuthType::FACE);
228     WidgetClient::Instance().SetAuthTypeList(authTypeList);
229     WidgetClient::Instance().SetWidgetSchedule(BuildSchedule());
230     EXPECT_EQ(WidgetClient::Instance().OnNotice(NoticeType::WIDGET_NOTICE, eventData), ResultCode::INVALID_PARAMETERS);
231 }
232 
233 HWTEST_F(WidgetClientTest, WidgetClientTestOnNotice_0011, TestSize.Level0)
234 {
235     WidgetNotice widgetNotice;
236     widgetNotice.widgetContextId = 1;
237     widgetNotice.event = NOTICE_EVENT_AUTH_READY;
238     widgetNotice.typeList.push_back("pin");
239     nlohmann::json root = widgetNotice;
240     std::string eventData = root.dump();
241     WidgetClient::Instance().Reset();
242     std::vector<AuthType> authTypeList;
243     authTypeList.emplace_back(AuthType::PIN);
244     WidgetClient::Instance().SetAuthTypeList(authTypeList);
245     WidgetClient::Instance().SetWidgetSchedule(BuildSchedule());
246     EXPECT_EQ(WidgetClient::Instance().OnNotice(NoticeType::WIDGET_NOTICE, eventData), ResultCode::SUCCESS);
247 }
248 
249 HWTEST_F(WidgetClientTest, WidgetClientTestOnNotice_0012, TestSize.Level0)
250 {
251     WidgetNotice widgetNotice;
252     widgetNotice.widgetContextId = 1;
253     widgetNotice.event = NOTICE_EVENT_CANCEL_AUTH;
254     widgetNotice.typeList.push_back("all");
255     nlohmann::json root = widgetNotice;
256     std::string eventData = root.dump();
257     WidgetClient::Instance().Reset();
258     std::vector<AuthType> authTypeList;
259     authTypeList.emplace_back(AuthType::PIN);
260     WidgetClient::Instance().SetAuthTypeList(authTypeList);
261     WidgetClient::Instance().SetWidgetSchedule(BuildSchedule());
262     EXPECT_EQ(WidgetClient::Instance().OnNotice(NoticeType::WIDGET_NOTICE, eventData), ResultCode::SUCCESS);
263 }
264 
265 HWTEST_F(WidgetClientTest, WidgetClientTestOnNotice_0013, TestSize.Level0)
266 {
267     WidgetNotice widgetNotice;
268     widgetNotice.widgetContextId = 1;
269     widgetNotice.event = NOTICE_EVENT_CANCEL_AUTH;
270     widgetNotice.typeList.push_back("pin");
271     nlohmann::json root = widgetNotice;
272     std::string eventData = root.dump();
273     WidgetClient::Instance().Reset();
274     std::vector<AuthType> authTypeList;
275     authTypeList.emplace_back(AuthType::PIN);
276     authTypeList.emplace_back(AuthType::FACE);
277     WidgetClient::Instance().SetAuthTypeList(authTypeList);
278     WidgetClient::Instance().SetWidgetSchedule(BuildSchedule());
279     EXPECT_EQ(WidgetClient::Instance().OnNotice(NoticeType::WIDGET_NOTICE, eventData), ResultCode::SUCCESS);
280 }
281 
282 HWTEST_F(WidgetClientTest, WidgetClientTestOnNotice_0014, TestSize.Level0)
283 {
284     WidgetNotice widgetNotice;
285     widgetNotice.widgetContextId = 1;
286     widgetNotice.event = NOTICE_EVENT_CANCEL_AUTH;
287     widgetNotice.typeList.push_back("pin");
288     widgetNotice.typeList.push_back("face");
289     nlohmann::json root = widgetNotice;
290     std::string eventData = root.dump();
291     WidgetClient::Instance().Reset();
292     std::vector<AuthType> authTypeList;
293     authTypeList.emplace_back(AuthType::PIN);
294     authTypeList.emplace_back(AuthType::FACE);
295     authTypeList.emplace_back(AuthType::FINGERPRINT);
296     WidgetClient::Instance().SetAuthTypeList(authTypeList);
297     WidgetClient::Instance().SetWidgetSchedule(BuildSchedule());
298     EXPECT_EQ(WidgetClient::Instance().OnNotice(NoticeType::WIDGET_NOTICE, eventData), ResultCode::SUCCESS);
299 }
300 
301 HWTEST_F(WidgetClientTest, WidgetClientTestOnNotice_0015, TestSize.Level0)
302 {
303     WidgetNotice widgetNotice;
304     widgetNotice.widgetContextId = 1;
305     widgetNotice.event = NOTICE_EVENT_CANCEL_AUTH;
306     widgetNotice.typeList.push_back("pin");
307     nlohmann::json root = widgetNotice;
308     std::string eventData = root.dump();
309     WidgetClient::Instance().Reset();
310     std::vector<AuthType> authTypeList;
311     authTypeList.emplace_back(AuthType::PIN);
312     WidgetClient::Instance().SetAuthTypeList(authTypeList);
313     WidgetClient::Instance().SetWidgetSchedule(BuildSchedule());
314     EXPECT_EQ(WidgetClient::Instance().OnNotice(NoticeType::WIDGET_NOTICE, eventData), ResultCode::SUCCESS);
315 }
316 
317 HWTEST_F(WidgetClientTest, WidgetClientTestOnNotice_0016, TestSize.Level0)
318 {
319     WidgetNotice widgetNotice;
320     widgetNotice.widgetContextId = 1;
321     widgetNotice.event = NOTICE_EVENT_USER_NAVIGATION;
322     widgetNotice.typeList.push_back("pin");
323     nlohmann::json root = widgetNotice;
324     std::string eventData = root.dump();
325     WidgetClient::Instance().Reset();
326     std::vector<AuthType> authTypeList;
327     authTypeList.emplace_back(AuthType::PIN);
328     WidgetClient::Instance().SetAuthTypeList(authTypeList);
329     WidgetClient::Instance().SetWidgetSchedule(BuildSchedule());
330     EXPECT_EQ(WidgetClient::Instance().OnNotice(NoticeType::WIDGET_NOTICE, eventData), ResultCode::SUCCESS);
331 }
332 
333 HWTEST_F(WidgetClientTest, WidgetClientTestOnNotice_0017, TestSize.Level0)
334 {
335     WidgetNotice widgetNotice;
336     widgetNotice.widgetContextId = 1;
337     widgetNotice.event = NOTICE_EVENT_CANCEL_AUTH;
338     widgetNotice.typeList.push_back("all");
339     widgetNotice.typeList.push_back("face");
340     nlohmann::json root = widgetNotice;
341     std::string eventData = root.dump();
342     WidgetClient::Instance().Reset();
343     std::vector<AuthType> authTypeList;
344     authTypeList.emplace_back(AuthType::PIN);
345     WidgetClient::Instance().SetAuthTypeList(authTypeList);
346     WidgetClient::Instance().SetWidgetSchedule(BuildSchedule());
347     EXPECT_EQ(WidgetClient::Instance().OnNotice(NoticeType::WIDGET_NOTICE, eventData), ResultCode::INVALID_PARAMETERS);
348 }
349 
350 HWTEST_F(WidgetClientTest, WidgetClientTestReportWidgetResult_0001, TestSize.Level0)
351 {
352     WidgetClient::Instance().Reset();
353     WidgetClient::Instance().SetSensorInfo("fake senor info");
354     WidgetClient::Instance().ReportWidgetResult(1, AuthType::FINGERPRINT, 1, 1);
355     EXPECT_EQ(WidgetClient::Instance().GetAuthTokenId(), 0);
356 }
357 
358 HWTEST_F(WidgetClientTest, WidgetClientTestReportWidgetResult_0002, TestSize.Level0)
359 {
360     WidgetClient::Instance().Reset();
361     WidgetClient::Instance().ReportWidgetResult(1, AuthType::FINGERPRINT, 1, 1);
362     EXPECT_EQ(WidgetClient::Instance().GetAuthTokenId(), 0);
363 }
364 
365 HWTEST_F(WidgetClientTest, WidgetClientTestReportWidgetResult_0003, TestSize.Level0)
366 {
367     WidgetClient::Instance().Reset();
368     WidgetClient::Instance().SetSensorInfo("fake senor info");
369     WidgetClient::Instance().ReportWidgetResult(1, AuthType::PIN, 1, 1);
370     EXPECT_EQ(WidgetClient::Instance().GetAuthTokenId(), 0);
371 }
372 
373 HWTEST_F(WidgetClientTest, WidgetClientTestReportWidgetResult_0004, TestSize.Level0)
374 {
375     WidgetClient::Instance().Reset();
376     std::vector<AuthType> authTypeList;
377     authTypeList.emplace_back(AuthType::PIN);
378     WidgetClient::Instance().SetAuthTypeList(authTypeList);
379     WidgetClient::Instance().SetPinSubType(PinSubType::PIN_NUMBER);
380     WidgetClient::Instance().ReportWidgetResult(1, AuthType::PIN, 1, 1);
381     EXPECT_EQ(WidgetClient::Instance().GetAuthTokenId(), 0);
382 }
383 
384 HWTEST_F(WidgetClientTest, WidgetClientTestReportWidgetResult_0005, TestSize.Level0)
385 {
386     WidgetClient::Instance().Reset();
387     std::vector<AuthType> authTypeList;
388     authTypeList.emplace_back(AuthType::PIN);
389     WidgetClient::Instance().SetAuthTypeList(authTypeList);
390     WidgetClient::Instance().SetPinSubType(PinSubType::PIN_NUMBER);
391     sptr<MockWidgetCallbackInterface> widgetCallback(new (std::nothrow) MockWidgetCallbackInterface);
392     EXPECT_NE(widgetCallback, nullptr);
393     EXPECT_CALL(*widgetCallback, SendCommand);
394     WidgetClient::Instance().SetWidgetCallback(widgetCallback);
395     WidgetClient::Instance().ReportWidgetResult(1, AuthType::PIN, 1, 1);
396     EXPECT_EQ(WidgetClient::Instance().GetAuthTokenId(), 0);
397 }
398 
399 HWTEST_F(WidgetClientTest, WidgetClientTestForceStopAuth_0001, TestSize.Level0)
400 {
401     WidgetClient::Instance().Reset();
402     WidgetClient::Instance().ForceStopAuth();
403     EXPECT_EQ(WidgetClient::Instance().GetAuthTokenId(), 0);
404 }
405 
406 HWTEST_F(WidgetClientTest, WidgetClientTestForceStopAuth_0002, TestSize.Level0)
407 {
408     uint64_t contextId = 6;
409     WidgetClient::Instance().Reset();
410     WidgetClient::Instance().SetWidgetContextId(contextId);
411     WidgetClient::Instance().SetWidgetSchedule(BuildSchedule());
412     WidgetClient::Instance().ForceStopAuth();
413     EXPECT_EQ(WidgetClient::Instance().GetAuthTokenId(), 0);
414 }
415 
416 HWTEST_F(WidgetClientTest, WidgetClientTestSetPinSubType, TestSize.Level0)
417 {
418     WidgetClient::Instance().SetPinSubType(PinSubType::PIN_SIX);
419     WidgetClient::Instance().SetPinSubType(PinSubType::PIN_NUMBER);
420     WidgetClient::Instance().SetPinSubType(PinSubType::PIN_MIXED);
421     WidgetClient::Instance().SetPinSubType(PinSubType::PIN_MAX);
422     WidgetClient::Instance().SetPinSubType((PinSubType)123);
423     EXPECT_EQ(WidgetClient::Instance().GetAuthTokenId(), 0);
424 }
425 } // namespace UserAuth
426 } // namespace UserIam
427 } // namespace OHOS
428