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 #include "widget_json.h"
16 #include "context_factory.h"
17
18 #include <future>
19 #include <gmock/gmock.h>
20
21 #include "iam_common_defines.h"
22 #include "user_auth_common_defines.h"
23
24 using namespace std;
25 using namespace testing;
26 using namespace testing::ext;
27
28 namespace OHOS {
29 namespace UserIam {
30 namespace UserAuth {
31 class WidgetJsonTest : 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 WidgetJsonTest::SetUpTestCase()
43 {
44 }
45
TearDownTestCase()46 void WidgetJsonTest::TearDownTestCase()
47 {
48 }
49
SetUp()50 void WidgetJsonTest::SetUp()
51 {
52 }
53
TearDown()54 void WidgetJsonTest::TearDown()
55 {
56 }
57
58 struct TestAuthProfile {
59 int32_t pinSubType {0};
60 std::string sensorInfo;
61 int32_t remainTimes {0};
62 int32_t freezingTime {0};
63 };
64
CreatePara(ContextFactory::AuthWidgetContextPara & para)65 void CreatePara(ContextFactory::AuthWidgetContextPara ¶)
66 {
67 para.widgetParam.title = "widgetParam";
68 para.widgetParam.windowMode = DIALOG_BOX;
69 para.widgetParam.navigationButtonText = "navigationButtonText";
70 ContextFactory::AuthProfile authProfile;
71 para.authProfileMap.insert(pair<AuthType, ContextFactory::AuthProfile>(ALL, authProfile));
72 para.authProfileMap.insert(pair<AuthType, ContextFactory::AuthProfile>(PIN, authProfile));
73 para.authProfileMap.insert(pair<AuthType, ContextFactory::AuthProfile>(FACE, authProfile));
74 para.authProfileMap.insert(pair<AuthType,
75 ContextFactory::AuthProfile>(FINGERPRINT, authProfile));
76 }
77
78 HWTEST_F(WidgetJsonTest, WidgetJsonStr2AuthType_001, TestSize.Level0)
79 {
80 std::string strAt = "pin";
81 EXPECT_EQ(Str2AuthType(strAt), PIN);
82 }
83
84 HWTEST_F(WidgetJsonTest, WidgetJsonStr2AuthType_002, TestSize.Level0)
85 {
86 std::string strAt = "fingerprint";
87 EXPECT_EQ(Str2AuthType(strAt), FINGERPRINT);
88 }
89
90 HWTEST_F(WidgetJsonTest, WidgetJsonStr2AuthType_003, TestSize.Level0)
91 {
92 std::string strAt = "face";
93 EXPECT_EQ(Str2AuthType(strAt), FACE);
94 }
95
96 HWTEST_F(WidgetJsonTest, WidgetJsonStr2AuthType_004, TestSize.Level0)
97 {
98 std::string strAt = "all";
99 EXPECT_EQ(Str2AuthType(strAt), ALL);
100 }
101
102 HWTEST_F(WidgetJsonTest, WidgetJsonStr2AuthType_005, TestSize.Level0)
103 {
104 std::string strAt = "asdf";
105 EXPECT_EQ(Str2AuthType(strAt), ALL);
106 }
107
108 HWTEST_F(WidgetJsonTest, WidgetJsonAuthType2Str, TestSize.Level0)
109 {
110 AuthType authType = static_cast<AuthType>(100);
111 std::string type = AuthType2Str(authType);
112 EXPECT_EQ(type, "");
113 }
114
115 HWTEST_F(WidgetJsonTest, WidgetJsonWinModeType2Str_001, TestSize.Level0)
116 {
117 EXPECT_EQ(WinModeType2Str(DIALOG_BOX), "DIALOG_BOX");
118 }
119
120 HWTEST_F(WidgetJsonTest, WidgetJsonWinModeType2Str_002, TestSize.Level0)
121 {
122 EXPECT_EQ(WinModeType2Str(FULLSCREEN), "FULLSCREEN");
123 }
124
125 HWTEST_F(WidgetJsonTest, WidgetJsonPinSubType2Str_001, TestSize.Level0)
126 {
127 EXPECT_EQ(PinSubType2Str(PinSubType::PIN_SIX), "PIN_SIX");
128 }
129
130 HWTEST_F(WidgetJsonTest, WidgetJsonPinSubType2Str_002, TestSize.Level0)
131 {
132 EXPECT_EQ(PinSubType2Str(PinSubType::PIN_NUMBER), "PIN_NUMBER");
133 }
134
135 HWTEST_F(WidgetJsonTest, WidgetJsonPinSubType2Str_003, TestSize.Level0)
136 {
137 EXPECT_EQ(PinSubType2Str(PinSubType::PIN_MIXED), "PIN_MIXED");
138 }
139
140 HWTEST_F(WidgetJsonTest, WidgetJsonPinSubType2Str_004, TestSize.Level0)
141 {
142 EXPECT_EQ(PinSubType2Str(PinSubType::PIN_MAX), "PIN_MAX");
143 }
144
145 HWTEST_F(WidgetJsonTest, WidgetJsonto_json_001, TestSize.Level0)
146 {
147 WidgetCommand widgetCommand;
148 widgetCommand.widgetContextId = 1;
149 ContextFactory::AuthWidgetContextPara para;
150 CreatePara(para);
151 widgetCommand.title = para.widgetParam.title;
152 widgetCommand.windowModeType = WinModeType2Str(para.widgetParam.windowMode);
153 widgetCommand.navigationButtonText = para.widgetParam.navigationButtonText;
154 auto it = para.authProfileMap.find(AuthType::PIN);
155 if (it != para.authProfileMap.end()) {
156 widgetCommand.pinSubType = PinSubType2Str(static_cast<PinSubType>(it->second.pinSubType));
157 }
158 std::vector<std::string> typeList;
159 for (auto &item : para.authProfileMap) {
160 auto &at = item.first;
161 auto &profile = item.second;
162 typeList.push_back(AuthType2Str(at));
163 WidgetCommand::Cmd cmd {
164 .event = CMD_NOTIFY_AUTH_START,
165 .version = "1",
166 .type = AuthType2Str(at)
167 };
168 if (at == AuthType::FINGERPRINT && !profile.sensorInfo.empty()) {
169 cmd.sensorInfo = profile.sensorInfo;
170 }
171 cmd.remainAttempts = profile.remainTimes;
172 cmd.lockoutDuration = profile.freezingTime;
173 widgetCommand.cmdList.push_back(cmd);
174 }
175 widgetCommand.typeList = typeList;
176 nlohmann::json root = widgetCommand;
177 std::string cmdData = root.dump();
178 auto result = nlohmann::json::parse(cmdData.c_str());
179 auto widgetContextId = result["widgetContextId"];
180 EXPECT_EQ(widgetContextId, 1);
181 }
182
183 HWTEST_F(WidgetJsonTest, WidgetJsonto_json_002, TestSize.Level0)
184 {
185 WidgetCommand widgetCommand;
186 widgetCommand.widgetContextId = 1;
187 widgetCommand.pinSubType = "pinSubType";
188 ContextFactory::AuthWidgetContextPara para;
189 CreatePara(para);
190 widgetCommand.title = para.widgetParam.title;
191 std::vector<std::string> typeList;
192 for (auto &item : para.authProfileMap) {
193 auto &at = item.first;
194 typeList.push_back(AuthType2Str(at));
195 WidgetCommand::Cmd cmd {
196 .event = CMD_NOTIFY_AUTH_RESULT,
197 .version = "1",
198 .type = AuthType2Str(at)
199 };
200 cmd.sensorInfo = "sensorInfo";
201 cmd.remainAttempts = -1;
202 cmd.lockoutDuration = -1;
203 widgetCommand.cmdList.push_back(cmd);
204 }
205 widgetCommand.typeList = typeList;
206 nlohmann::json root = widgetCommand;
207 std::string cmdData = root.dump();
208 auto result = nlohmann::json::parse(cmdData.c_str());
209 auto pinSubType = result["pinSubType"];
210 EXPECT_EQ(pinSubType, "pinSubType");
211 }
212
213 HWTEST_F(WidgetJsonTest, WidgetJsonto_json_003, TestSize.Level0)
214 {
215 WidgetCmdParameters widgetCmdParameters;
216 widgetCmdParameters.uiExtensionType = "sysDialog/userAuth";
217 widgetCmdParameters.useriamCmdData.widgetContextId = 1;
218 ContextFactory::AuthWidgetContextPara para;
219 CreatePara(para);
220 widgetCmdParameters.useriamCmdData.title = para.widgetParam.title;
221 widgetCmdParameters.useriamCmdData.windowModeType = WinModeType2Str(para.widgetParam.windowMode);
222 widgetCmdParameters.useriamCmdData.navigationButtonText = para.widgetParam.navigationButtonText;
223 auto it = para.authProfileMap.find(AuthType::PIN);
224 if (it != para.authProfileMap.end()) {
225 widgetCmdParameters.useriamCmdData.pinSubType = PinSubType2Str(static_cast<PinSubType>(it->second.pinSubType));
226 }
227 std::vector<std::string> typeList;
228 for (auto &item : para.authProfileMap) {
229 auto &at = item.first;
230 auto &profile = item.second;
231 typeList.push_back(AuthType2Str(at));
232 WidgetCommand::Cmd cmd {
233 .event = CMD_NOTIFY_AUTH_START,
234 .version = "1",
235 .type = AuthType2Str(at)
236 };
237 if (at == AuthType::FINGERPRINT && !profile.sensorInfo.empty()) {
238 cmd.sensorInfo = profile.sensorInfo;
239 }
240 cmd.remainAttempts = profile.remainTimes;
241 cmd.lockoutDuration = profile.freezingTime;
242 widgetCmdParameters.useriamCmdData.cmdList.push_back(cmd);
243 }
244 widgetCmdParameters.useriamCmdData.typeList = typeList;
245 nlohmann::json root = widgetCmdParameters;
246 std::string cmdData = root.dump();
247 auto result = nlohmann::json::parse(cmdData.c_str());
248 auto uiExtensionType = result["ability.want.params.uiExtensionType"];
249 EXPECT_EQ(uiExtensionType, "sysDialog/userAuth");
250 }
251
252 HWTEST_F(WidgetJsonTest, WidgetJsonto_json_004, TestSize.Level0)
253 {
254 WidgetCmdParameters widgetCmdParameters;
255 widgetCmdParameters.uiExtensionType = "sysDialog/userAuth";
256 widgetCmdParameters.useriamCmdData.widgetContextId = 1;
257 widgetCmdParameters.useriamCmdData.pinSubType = "pinSubType";
258 ContextFactory::AuthWidgetContextPara para;
259 CreatePara(para);
260 widgetCmdParameters.useriamCmdData.title = para.widgetParam.title;
261 std::vector<std::string> typeList;
262 for (auto &item : para.authProfileMap) {
263 auto &at = item.first;
264 typeList.push_back(AuthType2Str(at));
265 WidgetCommand::Cmd cmd {
266 .event = CMD_NOTIFY_AUTH_RESULT,
267 .version = "1",
268 .type = AuthType2Str(at)
269 };
270 cmd.sensorInfo = "sensorInfo";
271 widgetCmdParameters.useriamCmdData.cmdList.push_back(cmd);
272 }
273 widgetCmdParameters.useriamCmdData.typeList = typeList;
274 nlohmann::json root = widgetCmdParameters;
275 std::string cmdData = root.dump();
276 auto result = nlohmann::json::parse(cmdData.c_str());
277 auto uiExtensionType = result["ability.want.params.uiExtensionType"];
278 EXPECT_EQ(uiExtensionType, "sysDialog/userAuth");
279 }
280
281 HWTEST_F(WidgetJsonTest, WidgetJsonto_json_005, TestSize.Level0)
282 {
283 WidgetNotice widgetNotice;
284 widgetNotice.widgetContextId = 1;
285 widgetNotice.event = CMD_NOTIFY_AUTH_START;
286 widgetNotice.typeList.push_back("pin");
287 widgetNotice.typeList.push_back("face");
288 widgetNotice.typeList.push_back("fingerprint");
289 widgetNotice.typeList.push_back("all");
290 widgetNotice.version = "1";
291 auto authTypeList = widgetNotice.AuthTypeList();
292 EXPECT_EQ(authTypeList[0], AuthType::PIN);
293 nlohmann::json root = widgetNotice;
294 std::string cmdData = root.dump();
295 auto result = nlohmann::json::parse(cmdData.c_str());
296 auto version = result["version"];
297 EXPECT_EQ(version, "1");
298 }
299
300 HWTEST_F(WidgetJsonTest, WidgetJsonto_json_006, TestSize.Level0)
301 {
302 auto root = nlohmann::json::parse("", nullptr, false);
303 WidgetNotice notice = root.get<WidgetNotice>();
304 EXPECT_EQ(notice.widgetContextId, static_cast<uint64_t>(0));
305 }
306
307 HWTEST_F(WidgetJsonTest, WidgetJsonto_json_007, TestSize.Level0)
308 {
309 const std::string data = "{\"widgetContextId\":\"1\", \"event\":1, \"version\":1}";
310 auto root = nlohmann::json::parse(data, nullptr, false);
311 WidgetNotice notice = root.get<WidgetNotice>();
312 EXPECT_EQ(notice.widgetContextId, static_cast<uint64_t>(0));
313 }
314
315 HWTEST_F(WidgetJsonTest, WidgetJsonto_json_008, TestSize.Level0)
316 {
317 const std::string data = "{\"widgetContextId\":1, \"event\":\"EVENT_AUTH_TYPE_READY\", \"version\":\"1\"}";
318 auto root = nlohmann::json::parse(data, nullptr, false);
319 WidgetNotice notice = root.get<WidgetNotice>();
320 EXPECT_EQ(notice.widgetContextId, static_cast<uint64_t>(1));
321 EXPECT_EQ(notice.event, "EVENT_AUTH_TYPE_READY");
322 EXPECT_EQ(notice.version, "1");
323 }
324
325 HWTEST_F(WidgetJsonTest, WidgetJsonto_json_009, TestSize.Level0)
326 {
327 auto root = nlohmann::json::parse("{\"payload\":123}", nullptr, false);
328 WidgetNotice notice = root.get<WidgetNotice>();
329 EXPECT_EQ(notice.typeList.size(), static_cast<size_t>(0));
330 }
331
332 HWTEST_F(WidgetJsonTest, WidgetJsonto_json_010, TestSize.Level0)
333 {
334 auto root = nlohmann::json::parse("{\"payload\":{\"type\":123}}", nullptr, false);
335 WidgetNotice notice = root.get<WidgetNotice>();
336 EXPECT_EQ(notice.typeList.size(), static_cast<size_t>(0));
337 }
338
339 HWTEST_F(WidgetJsonTest, WidgetJsonto_json_011, TestSize.Level0)
340 {
341 auto root = nlohmann::json::parse("{\"payload\":{\"type\":[\"pin\", 123]}}", nullptr, false);
342 WidgetNotice notice = root.get<WidgetNotice>();
343 EXPECT_EQ(notice.typeList.size(), static_cast<size_t>(0));
344 }
345
346 HWTEST_F(WidgetJsonTest, WidgetJsonto_json_012, TestSize.Level0)
347 {
348 auto root = nlohmann::json::parse("{\"payload\":{\"type\":[\"pin\"]}}", nullptr, false);
349 WidgetNotice notice = root.get<WidgetNotice>();
350 EXPECT_EQ(notice.typeList.size(), static_cast<size_t>(1));
351 EXPECT_EQ(notice.typeList[0], "pin");
352 }
353
354 HWTEST_F(WidgetJsonTest, WidgetJsonto_json_013, TestSize.Level0)
355 {
356 auto root = nlohmann::json::parse("{\"payload\":{\"endAfterFirstFail\":123}}", nullptr, false);
357 WidgetNotice notice = root.get<WidgetNotice>();
358 EXPECT_EQ(notice.endAfterFirstFail, false);
359 }
360
361 HWTEST_F(WidgetJsonTest, WidgetJsonto_json_014, TestSize.Level0)
362 {
363 auto root = nlohmann::json::parse("{\"payload\":{\"endAfterFirstFail\":true}}", nullptr, false);
364 WidgetNotice notice = root.get<WidgetNotice>();
365 EXPECT_EQ(notice.endAfterFirstFail, true);
366 }
367 } // namespace UserAuth
368 } // namespace UserIam
369 } // namespace OHOS