1 /*
2 * Copyright (c) 2021 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 "datetime_ex.h"
17 #include "../include/notificationfuzzconfigparser.h"
18 #include "../include/notificationfuzztestmanager.h"
19 #include "../include/notificationgetparam.h"
20
21 using namespace OHOS::AppExecFwk;
22 using namespace OHOS::EventFwk;
23 namespace OHOS {
24 namespace Notification {
25 std::shared_ptr<NotificationFuzzTestManager> NotificationFuzzTestManager::instance = nullptr;
26 // RegisterNotificationHelper
RegisterNotificationHelper()27 void NotificationFuzzTestManager::RegisterNotificationHelper()
28 {
29 callFunctionMap_.emplace("NotificationHelperAddNotificationSlot",
30 []() { OHOS::Notification::NotificationHelper::AddNotificationSlot(*GetParamNotificationSlot()); });
31
32 callFunctionMap_.emplace("NotificationHelperAddSlotByType",
33 []() { OHOS::Notification::NotificationHelper::AddSlotByType(GetParamSlotType()); });
34
35 callFunctionMap_.emplace("NotificationHelperAddNotificationSlots",
36 []() { OHOS::Notification::NotificationHelper::AddNotificationSlots(GetParamNotificationSlotVector()); });
37
38 callFunctionMap_.emplace("NotificationHelperRemoveNotificationSlot",
39 []() { OHOS::Notification::NotificationHelper::RemoveNotificationSlot(GetParamSlotType()); });
40
41 callFunctionMap_.emplace(
42 "NotificationHelperRemoveAllSlots", []() { OHOS::Notification::NotificationHelper::RemoveAllSlots(); });
43
44 callFunctionMap_.emplace("NotificationHelperGetNotificationSlot", []() {
45 sptr<OHOS::Notification::NotificationSlot> param = GetParamNotificationSlotSptr();
46 OHOS::Notification::NotificationHelper::GetNotificationSlot(GetParamSlotType(), param);
47 });
48
49 callFunctionMap_.emplace("NotificationHelperGetNotificationSlots", []() {
50 std::vector<sptr<OHOS::Notification::NotificationSlot>> param = GetParamNotificationSlotSptrVector();
51 OHOS::Notification::NotificationHelper::GetNotificationSlots(param);
52 });
53
54 callFunctionMap_.emplace("NotificationHelperGetNotificationSlotNumAsBundle", []() {
55 int param = GetIntParam();
56 OHOS::Notification::NotificationHelper::GetNotificationSlotNumAsBundle(
57 *GetParamNotificationBundleOption(), param);
58 });
59
60 callFunctionMap_.emplace("NotificationHelperPublishNotificationNotificationRequest",
61 []() { OHOS::Notification::NotificationHelper::PublishNotification(*GetParamNotificationRequest()); });
62
63 callFunctionMap_.emplace("NotificationHelperPublishNotificationstringNotificationRequest", []() {
64 OHOS::Notification::NotificationHelper::PublishNotification(GetStringParam(), *GetParamNotificationRequest());
65 });
66
67 callFunctionMap_.emplace("NotificationHelperPublishNotificationNotificationRequeststring", []() {
68 OHOS::Notification::NotificationHelper::PublishNotification(*GetParamNotificationRequest(), GetStringParam());
69 });
70
71 callFunctionMap_.emplace("NotificationHelperCancelNotificationint32_t",
72 []() { OHOS::Notification::NotificationHelper::CancelNotification(GetS32Param()); });
73
74 callFunctionMap_.emplace("NotificationHelperCancelNotificationstringint32_t",
75 []() { OHOS::Notification::NotificationHelper::CancelNotification(GetStringParam(), GetS32Param()); });
76
77 callFunctionMap_.emplace("NotificationHelperCancelAllNotifications",
78 []() { OHOS::Notification::NotificationHelper::CancelAllNotifications(); });
79
80 callFunctionMap_.emplace("NotificationHelperGetActiveNotificationNums", []() {
81 int32_t notificationId = GetS32Param();
82 OHOS::Notification::NotificationHelper::GetActiveNotificationNums(notificationId);
83 });
84
85 callFunctionMap_.emplace("NotificationHelperGetActiveNotifications", []() {
86 std::vector<sptr<OHOS::Notification::NotificationRequest>> param = GetParamNotificationRequestVector();
87 OHOS::Notification::NotificationHelper::GetActiveNotifications(param);
88 });
89
90 callFunctionMap_.emplace("NotificationHelperGetCurrentAppSorting", []() {
91 sptr<OHOS::Notification::NotificationSortingMap> sortingMap = GetParamNotificationSortingMapSptr();
92 OHOS::Notification::NotificationHelper::GetCurrentAppSorting(sortingMap);
93 });
94
95 callFunctionMap_.emplace("NotificationHelperSetNotificationAgent",
96 []() { OHOS::Notification::NotificationHelper::SetNotificationAgent(GetStringParam()); });
97
98 callFunctionMap_.emplace("NotificationHelperGetNotificationAgent", []() {
99 std::string agent = GetStringParam();
100 OHOS::Notification::NotificationHelper::GetNotificationAgent(agent);
101 });
102
103 callFunctionMap_.emplace("NotificationHelperCanPublishNotificationAsBundle", []() {
104 bool canPublish = GetBoolParam();
105 OHOS::Notification::NotificationHelper::CanPublishNotificationAsBundle(GetStringParam(), canPublish);
106 });
107
108 callFunctionMap_.emplace("NotificationHelperPublishNotificationAsBundle", []() {
109 OHOS::Notification::NotificationHelper::PublishNotificationAsBundle(
110 GetStringParam(), *GetParamNotificationRequest());
111 });
112
113 callFunctionMap_.emplace("NotificationHelperSetNotificationBadgeNum",
114 []() { OHOS::Notification::NotificationHelper::SetNotificationBadgeNum(); });
115
116 callFunctionMap_.emplace("NotificationHelperSetNotificationBadgeNumint32_t", []() {
117 int32_t num = GetS32Param();
118 OHOS::Notification::NotificationHelper::SetNotificationBadgeNum(num);
119 });
120
121 callFunctionMap_.emplace("NotificationHelperIsAllowedNotify", []() {
122 bool allowed = GetBoolParam();
123 OHOS::Notification::NotificationHelper::IsAllowedNotify(allowed);
124 });
125
126 callFunctionMap_.emplace("NotificationHelperAreNotificationsSuspended", []() {
127 bool suspended = GetBoolParam();
128 OHOS::Notification::NotificationHelper::AreNotificationsSuspended(suspended);
129 });
130
131 callFunctionMap_.emplace("NotificationHelperHasNotificationPolicyAccessPermission", []() {
132 bool hasPermission = GetBoolParam();
133 OHOS::Notification::NotificationHelper::HasNotificationPolicyAccessPermission(hasPermission);
134 });
135
136 callFunctionMap_.emplace("NotificationHelperGetBundleImportance", []() {
137 OHOS::Notification::NotificationSlot::NotificationLevel importance = GetParamNotificationLevel();
138 OHOS::Notification::NotificationHelper::GetBundleImportance(importance);
139 });
140
141 callFunctionMap_.emplace("NotificationHelperSubscribeNotificationNotificationSubscriber", []() {
142 TestAnsSubscriber::mutex.lock();
143 std::shared_ptr<OHOS::Notification::NotificationSubscriber> subscriber = GetParamNotificationSubscriber();
144 struct tm start = {0};
145 OHOS::GetSystemCurrentTime(&start);
146 OHOS::Notification::NotificationHelper::SubscribeNotification(*subscriber);
147 struct tm end = {0};
148 int64_t timeout = 0;
149 while (!TestAnsSubscriber::mutex.try_lock()) {
150 OHOS::GetSystemCurrentTime(&end);
151 timeout = OHOS::GetSecondsBetween(start, end);
152 if (timeout >= 5L) {
153 break;
154 }
155 }
156 TestAnsSubscriber::mutex.unlock();
157 TestAnsSubscriber::mutex.lock();
158 OHOS::GetSystemCurrentTime(&start);
159 OHOS::Notification::NotificationHelper::UnSubscribeNotification(*subscriber);
160 while (!TestAnsSubscriber::mutex.try_lock()) {
161 OHOS::GetSystemCurrentTime(&end);
162 timeout = OHOS::GetSecondsBetween(start, end);
163 if (timeout >= 5L) {
164 break;
165 }
166 }
167 TestAnsSubscriber::mutex.unlock();
168 });
169
170 callFunctionMap_.emplace(
171 "NotificationHelperSubscribeNotificationNotificationSubscriberNotificationSubscribeInfo", []() {
172 TestAnsSubscriber::mutex.lock();
173 std::shared_ptr<OHOS::Notification::NotificationSubscriber> subscriber = GetParamNotificationSubscriber();
174 std::shared_ptr<OHOS::Notification::NotificationSubscribeInfo> subscribeInfo =
175 GetParamNotificationSubscribeInfo();
176 struct tm start = {0};
177 OHOS::GetSystemCurrentTime(&start);
178 OHOS::Notification::NotificationHelper::SubscribeNotification(*subscriber, *subscribeInfo);
179 struct tm end = {0};
180 int64_t timeout = 0;
181 while (!TestAnsSubscriber::mutex.try_lock()) {
182 OHOS::GetSystemCurrentTime(&end);
183 timeout = OHOS::GetSecondsBetween(start, end);
184 if (timeout >= 5L) {
185 break;
186 }
187 }
188 TestAnsSubscriber::mutex.unlock();
189 TestAnsSubscriber::mutex.lock();
190 OHOS::GetSystemCurrentTime(&start);
191 OHOS::Notification::NotificationHelper::UnSubscribeNotification(*subscriber, *subscribeInfo);
192 while (!TestAnsSubscriber::mutex.try_lock()) {
193 OHOS::GetSystemCurrentTime(&end);
194 timeout = OHOS::GetSecondsBetween(start, end);
195 if (timeout >= 5L) {
196 break;
197 }
198 }
199 TestAnsSubscriber::mutex.unlock();
200 });
201
202 callFunctionMap_.emplace("NotificationHelperUnSubscribeNotificationNotificationSubscriber", []() {
203 TestAnsSubscriber::mutex.lock();
204 std::shared_ptr<OHOS::Notification::NotificationSubscriber> subscriber = GetParamNotificationSubscriber();
205 struct tm start = {0};
206 OHOS::GetSystemCurrentTime(&start);
207 OHOS::Notification::NotificationHelper::SubscribeNotification(*subscriber);
208 struct tm end = {0};
209 int64_t timeout = 0;
210 while (!TestAnsSubscriber::mutex.try_lock()) {
211 OHOS::GetSystemCurrentTime(&end);
212 timeout = OHOS::GetSecondsBetween(start, end);
213 if (timeout >= 5L) {
214 break;
215 }
216 }
217 TestAnsSubscriber::mutex.unlock();
218 TestAnsSubscriber::mutex.lock();
219 OHOS::GetSystemCurrentTime(&start);
220 OHOS::Notification::NotificationHelper::UnSubscribeNotification(*subscriber);
221 while (!TestAnsSubscriber::mutex.try_lock()) {
222 OHOS::GetSystemCurrentTime(&end);
223 timeout = OHOS::GetSecondsBetween(start, end);
224 if (timeout >= 5L) {
225 break;
226 }
227 }
228 TestAnsSubscriber::mutex.unlock();
229 });
230
231 callFunctionMap_.emplace(
232 "NotificationHelperUnSubscribeNotificationNotificationSubscriberNotificationSubscribeInfo", []() {
233 TestAnsSubscriber::mutex.lock();
234 std::shared_ptr<OHOS::Notification::NotificationSubscriber> subscriber = GetParamNotificationSubscriber();
235 std::shared_ptr<OHOS::Notification::NotificationSubscribeInfo> subscribeInfo =
236 GetParamNotificationSubscribeInfo();
237 struct tm start = {0};
238 OHOS::GetSystemCurrentTime(&start);
239 OHOS::Notification::NotificationHelper::SubscribeNotification(*subscriber, *subscribeInfo);
240 struct tm end = {0};
241 int64_t timeout = 0;
242 while (!TestAnsSubscriber::mutex.try_lock()) {
243 OHOS::GetSystemCurrentTime(&end);
244 timeout = OHOS::GetSecondsBetween(start, end);
245 if (timeout >= 5L) {
246 break;
247 }
248 }
249 TestAnsSubscriber::mutex.unlock();
250 TestAnsSubscriber::mutex.lock();
251 OHOS::GetSystemCurrentTime(&start);
252 OHOS::Notification::NotificationHelper::UnSubscribeNotification(*subscriber, *subscribeInfo);
253 while (!TestAnsSubscriber::mutex.try_lock()) {
254 OHOS::GetSystemCurrentTime(&end);
255 timeout = OHOS::GetSecondsBetween(start, end);
256 if (timeout >= 5L) {
257 break;
258 }
259 }
260 TestAnsSubscriber::mutex.unlock();
261 });
262
263 callFunctionMap_.emplace("NotificationHelperRemoveNotificationstring",
264 []() { OHOS::Notification::NotificationHelper::RemoveNotification(GetStringParam()); });
265
266 callFunctionMap_.emplace("NotificationHelperRemoveNotificationNotificationBundleOptionint32_tstring", []() {
267 OHOS::Notification::NotificationHelper::RemoveNotification(
268 *GetParamNotificationBundleOption(), GetS32Param(), GetStringParam());
269 });
270
271 callFunctionMap_.emplace("NotificationHelperRemoveAllNotifications",
272 []() { OHOS::Notification::NotificationHelper::RemoveAllNotifications(*GetParamNotificationBundleOption()); });
273
274 callFunctionMap_.emplace("NotificationHelperRemoveNotifications",
275 []() { OHOS::Notification::NotificationHelper::RemoveNotifications(); });
276
277 callFunctionMap_.emplace("NotificationHelperRemoveNotificationsByBundle", []() {
278 OHOS::Notification::NotificationHelper::RemoveNotificationsByBundle(*GetParamNotificationBundleOption());
279 });
280
281 callFunctionMap_.emplace("NotificationHelperGetNotificationSlotsForBundle", []() {
282 std::vector<sptr<OHOS::Notification::NotificationSlot>> param = GetParamNotificationSlotSptrVector();
283 OHOS::Notification::NotificationHelper::GetNotificationSlotsForBundle(
284 *GetParamNotificationBundleOption(), param);
285 });
286
287 callFunctionMap_.emplace("NotificationHelperUpdateNotificationSlots", []() {
288 OHOS::Notification::NotificationHelper::UpdateNotificationSlots(
289 *GetParamNotificationBundleOption(), GetParamNotificationSlotSptrVector());
290 });
291
292 callFunctionMap_.emplace("NotificationHelperGetAllActiveNotificationsvectorsptrNotification", []() {
293 std::vector<sptr<OHOS::Notification::Notification>> notification = GetParamNotificationSptrVector();
294 OHOS::Notification::NotificationHelper::GetAllActiveNotifications(notification);
295 });
296
297 callFunctionMap_.emplace("NotificationHelperGetAllActiveNotificationsstringNotification", []() {
298 std::vector<sptr<OHOS::Notification::Notification>> notification = GetParamNotificationSptrVector();
299 OHOS::Notification::NotificationHelper::GetAllActiveNotifications(GetStringVectorParam(), notification);
300 });
301
302 callFunctionMap_.emplace("NotificationHelperIsAllowedNotify", []() {
303 bool param = GetBoolParam();
304 OHOS::Notification::NotificationHelper::IsAllowedNotify(*GetParamNotificationBundleOption(), param);
305 });
306
307 callFunctionMap_.emplace("NotificationHelperSetNotificationsEnabledForAllBundles", []() {
308 OHOS::Notification::NotificationHelper::SetNotificationsEnabledForAllBundles(GetStringParam(), GetBoolParam());
309 });
310
311 callFunctionMap_.emplace("NotificationHelperSetNotificationsEnabledForDefaultBundle", []() {
312 OHOS::Notification::NotificationHelper::SetNotificationsEnabledForDefaultBundle(
313 GetStringParam(), GetBoolParam());
314 });
315
316 callFunctionMap_.emplace("NotificationHelperSetNotificationsEnabledForSpecifiedBundle", []() {
317 std::string param = GetStringParam();
318 OHOS::Notification::NotificationHelper::SetNotificationsEnabledForSpecifiedBundle(
319 *GetParamNotificationBundleOption(), param, GetBoolParam());
320 });
321
322 callFunctionMap_.emplace("NotificationHelperSetShowBadgeEnabledForBundle", []() {
323 OHOS::Notification::NotificationHelper::SetShowBadgeEnabledForBundle(
324 *GetParamNotificationBundleOption(), GetBoolParam());
325 });
326
327 callFunctionMap_.emplace("NotificationHelperGetShowBadgeEnabledForBundle", []() {
328 bool param = GetBoolParam();
329 OHOS::Notification::NotificationHelper::GetShowBadgeEnabledForBundle(
330 *GetParamNotificationBundleOption(), param);
331 });
332
333 callFunctionMap_.emplace("NotificationHelperGetShowBadgeEnabled", []() {
334 bool param = GetBoolParam();
335 OHOS::Notification::NotificationHelper::GetShowBadgeEnabled(param);
336 });
337
338 callFunctionMap_.emplace("NotificationHelperSetDoNotDisturbDate",
339 []() { OHOS::Notification::NotificationHelper::SetDoNotDisturbDate(*GetParamNotificationDoNotDisturbDate()); });
340
341 callFunctionMap_.emplace("NotificationHelperGetDoNotDisturbDate", []() {
342 OHOS::Notification::NotificationDoNotDisturbDate doNotDisturbDate;
343 OHOS::Notification::NotificationHelper::GetDoNotDisturbDate(doNotDisturbDate);
344 });
345 }
346
347 // RegisterNotificationSorting
RegisterNotificationSorting()348 void NotificationFuzzTestManager::RegisterNotificationSorting()
349 {
350 callFunctionMap_.emplace("NotificationSortingGetRanking", []() {
351 std::shared_ptr<OHOS::Notification::NotificationSorting> temp = GetParamNotificationSorting();
352 temp->GetRanking();
353 });
354
355 callFunctionMap_.emplace("NotificationSortingGetKey", []() {
356 std::shared_ptr<OHOS::Notification::NotificationSorting> temp = GetParamNotificationSorting();
357 temp->GetKey();
358 });
359
360 callFunctionMap_.emplace("NotificationSortingGetImportance", []() {
361 std::shared_ptr<OHOS::Notification::NotificationSorting> temp = GetParamNotificationSorting();
362 temp->GetImportance();
363 });
364
365 callFunctionMap_.emplace("NotificationSortingGetSlot", []() {
366 std::shared_ptr<OHOS::Notification::NotificationSorting> temp = GetParamNotificationSorting();
367 temp->GetSlot();
368 });
369
370 callFunctionMap_.emplace("NotificationSortingGetVisiblenessOverride", []() {
371 std::shared_ptr<OHOS::Notification::NotificationSorting> temp = GetParamNotificationSorting();
372 temp->GetVisiblenessOverride();
373 });
374
375 callFunctionMap_.emplace("NotificationSortingIsDisplayBadge", []() {
376 std::shared_ptr<OHOS::Notification::NotificationSorting> temp = GetParamNotificationSorting();
377 temp->IsDisplayBadge();
378 });
379
380 callFunctionMap_.emplace("NotificationSortingIsHiddenNotification", []() {
381 std::shared_ptr<OHOS::Notification::NotificationSorting> temp = GetParamNotificationSorting();
382 temp->IsHiddenNotification();
383 });
384
385 callFunctionMap_.emplace("NotificationSortingGetGroupKeyOverride", []() {
386 std::shared_ptr<OHOS::Notification::NotificationSorting> temp = GetParamNotificationSorting();
387 temp->GetGroupKeyOverride();
388 });
389 }
390
391 // RegisterNotificationSortingMap
RegisterNotificationSortingMap()392 void NotificationFuzzTestManager::RegisterNotificationSortingMap()
393 {
394 callFunctionMap_.emplace("NotificationSortingMapGetKey", []() {
395 std::shared_ptr<OHOS::Notification::NotificationSortingMap> temp = GetParamNotificationSortingMap();
396 temp->GetKey();
397 });
398
399 callFunctionMap_.emplace("NotificationSortingMapGetNotificationSorting", []() {
400 std::shared_ptr<OHOS::Notification::NotificationSortingMap> temp = GetParamNotificationSortingMap();
401 const std::string key = GetStringParam();
402 OHOS::Notification::NotificationSorting sorting = *(GetParamNotificationSorting().get());
403 temp->GetNotificationSorting(key, sorting);
404 });
405 }
406
407 // RegisterNotificationSubscribeInfo
RegisterNotificationSubscribeInfo()408 void NotificationFuzzTestManager::RegisterNotificationSubscribeInfo()
409 {
410 callFunctionMap_.emplace("NotificationSubscribeInfoAddAppName", []() {
411 std::shared_ptr<OHOS::Notification::NotificationSubscribeInfo> temp = GetParamNotificationSubscribeInfo();
412 std::string appName = GetStringParam();
413 temp->AddAppName(appName);
414 });
415
416 callFunctionMap_.emplace("NotificationSubscribeInfoAddAppNames", []() {
417 std::shared_ptr<OHOS::Notification::NotificationSubscribeInfo> temp = GetParamNotificationSubscribeInfo();
418 const std::vector<std::string> appNames = GetStringVectorParam();
419 temp->AddAppNames(appNames);
420 });
421
422 callFunctionMap_.emplace("NotificationSubscribeInfoGetAppNames", []() {
423 std::shared_ptr<OHOS::Notification::NotificationSubscribeInfo> temp = GetParamNotificationSubscribeInfo();
424 temp->GetAppNames();
425 });
426 }
427
428 // RegisterNotificationSubscriber
RegisterNotificationSubscriber()429 void NotificationFuzzTestManager::RegisterNotificationSubscriber()
430 {
431 callFunctionMap_.emplace("NotificationSubscriberOnCanceledNotificationNotificationSortingMapdeleteReason", []() {
432 std::shared_ptr<OHOS::Notification::NotificationSubscriber> temp = GetParamNotificationSubscriber();
433 const std::shared_ptr<OHOS::Notification::Notification> request = GetParamNotification();
434 const std::shared_ptr<OHOS::Notification::NotificationSortingMap> sortingMap = GetParamNotificationSortingMap();
435 int deleteReason = GetIntParam();
436 temp->OnCanceled(request, sortingMap, deleteReason);
437 });
438
439 callFunctionMap_.emplace("NotificationSubscriberOnSubscribeResult", []() {
440 std::shared_ptr<OHOS::Notification::NotificationSubscriber> temp = GetParamNotificationSubscriber();
441 temp->OnConnected();
442 });
443
444 callFunctionMap_.emplace("NotificationSubscriberOnConsumedNotification", []() {
445 std::shared_ptr<OHOS::Notification::NotificationSubscriber> temp = GetParamNotificationSubscriber();
446 const std::shared_ptr<OHOS::Notification::Notification> request = GetParamNotification();
447 temp->OnConsumed(request);
448 });
449
450 callFunctionMap_.emplace("NotificationSubscriberOnConsumedNotificationNotificationSortingMap", []() {
451 std::shared_ptr<OHOS::Notification::NotificationSubscriber> temp = GetParamNotificationSubscriber();
452 std::shared_ptr<OHOS::Notification::Notification> request = GetParamNotification();
453 std::shared_ptr<OHOS::Notification::NotificationSortingMap> sortingMap = GetParamNotificationSortingMap();
454 temp->OnConsumed(request, sortingMap);
455 });
456
457 callFunctionMap_.emplace("NotificationSubscriberOnUnsubscribeResult", []() {
458 std::shared_ptr<OHOS::Notification::NotificationSubscriber> temp = GetParamNotificationSubscriber();
459 temp->OnDisconnected();
460 });
461
462 callFunctionMap_.emplace("NotificationSubscriberOnUpdate", []() {
463 std::shared_ptr<OHOS::Notification::NotificationSubscriber> temp = GetParamNotificationSubscriber();
464 const std::shared_ptr<OHOS::Notification::NotificationSortingMap> sortingMap = GetParamNotificationSortingMap();
465 temp->OnUpdate(sortingMap);
466 });
467
468 callFunctionMap_.emplace("NotificationSubscriberOnDied", []() {
469 std::shared_ptr<OHOS::Notification::NotificationSubscriber> temp = GetParamNotificationSubscriber();
470 temp->OnDied();
471 });
472
473 callFunctionMap_.emplace("NotificationSubscriberOnDisturbModeChanged", []() {
474 std::shared_ptr<OHOS::Notification::NotificationSubscriber> temp = GetParamNotificationSubscriber();
475 temp->OnDoNotDisturbDateChange(GetParamNotificationDoNotDisturbDate());
476 });
477 }
478
479 // RegisterWantAgentHelper
RegisterWantAgentHelper()480 void NotificationFuzzTestManager::RegisterWantAgentHelper()
481 {
482 callFunctionMap_.emplace("WantAgentHelperGetWantAgentAppExecFwkparamsInfo", []() {
483 std::shared_ptr<AbilityRuntime::Context> context = OHOS::AbilityRuntime::Context::GetApplicationContext();
484 const OHOS::AbilityRuntime::WantAgent::WantAgentInfo paramsInfo = *GetParamWantAgentInfo();
485 std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> wantAgent = nullptr;
486 OHOS::AbilityRuntime::AbilityRuntime::WantAgent::WantAgentHelper::GetWantAgent(context, paramsInfo, wantAgent);
487 });
488
489 callFunctionMap_.emplace("WantAgentHelperGetWantAgentparamsInfo", []() {
490 const OHOS::AbilityRuntime::WantAgent::WantAgentInfo paramsInfo = *GetParamWantAgentInfo();
491 OHOS::AbilityRuntime::AbilityRuntime::WantAgent::WantAgentHelper::GetWantAgent(paramsInfo);
492 });
493
494 callFunctionMap_.emplace("WantAgentHelperGetType", []() {
495 const std::shared_ptr<OHOS::AbilityRuntime::AbilityRuntime::WantAgent::WantAgent> agent = GetParamWantAgent();
496 OHOS::AbilityRuntime::AbilityRuntime::WantAgent::WantAgentHelper::GetType(agent);
497 });
498
499 callFunctionMap_.emplace("WantAgentHelperTriggerWantAgent", []() {
500 const std::shared_ptr<OHOS::AbilityRuntime::AbilityRuntime::WantAgent::WantAgent> agent = GetParamWantAgent();
501 const std::shared_ptr<OHOS::AbilityRuntime::WantAgent::CompletedCallback> callback =
502 GetParamCompletedCallback();
503 OHOS::AbilityRuntime::WantAgent::TriggerInfo paramsInfo(*GetParamTriggerInfo());
504 OHOS::AbilityRuntime::AbilityRuntime::WantAgent::WantAgentHelper::TriggerWantAgent(agent, callback, paramsInfo);
505 });
506
507 callFunctionMap_.emplace("WantAgentHelperCancel", []() {
508 const std::shared_ptr<OHOS::AbilityRuntime::AbilityRuntime::WantAgent::WantAgent> agent = GetParamWantAgent();
509 OHOS::AbilityRuntime::AbilityRuntime::WantAgent::WantAgentHelper::Cancel(agent);
510 });
511
512 callFunctionMap_.emplace("WantAgentHelperJudgeEquality", []() {
513 const std::shared_ptr<OHOS::AbilityRuntime::AbilityRuntime::WantAgent::WantAgent> agent = GetParamWantAgent();
514 const std::shared_ptr<OHOS::AbilityRuntime::AbilityRuntime::WantAgent::WantAgent> otherAgent
515 = GetParamWantAgent();
516 OHOS::AbilityRuntime::AbilityRuntime::WantAgent::WantAgentHelper::IsEquals(agent, otherAgent);
517 });
518
519 callFunctionMap_.emplace("WantAgentHelperGetBundleName", []() {
520 const std::shared_ptr<OHOS::AbilityRuntime::AbilityRuntime::WantAgent::WantAgent> agent = GetParamWantAgent();
521 std::string bundleName = "";
522 OHOS::AbilityRuntime::AbilityRuntime::WantAgent::WantAgentHelper::GetBundleName(agent, bundleName);
523 });
524
525 callFunctionMap_.emplace("WantAgentHelperGetUid", []() {
526 const std::shared_ptr<OHOS::AbilityRuntime::AbilityRuntime::WantAgent::WantAgent> agent = GetParamWantAgent();
527 int32_t uid = -1;
528 OHOS::AbilityRuntime::AbilityRuntime::WantAgent::WantAgentHelper::GetUid(agent, uid);
529 });
530
531 callFunctionMap_.emplace("WantAgentHelperGetWant", []() {
532 const std::shared_ptr<OHOS::AbilityRuntime::AbilityRuntime::WantAgent::WantAgent> agent = GetParamWantAgent();
533 OHOS::AbilityRuntime::AbilityRuntime::WantAgent::WantAgentHelper::GetWant(agent);
534 });
535
536 callFunctionMap_.emplace("WantAgentHelperRegisterCancelListener", []() {
537 const std::shared_ptr<OHOS::AbilityRuntime::WantAgent::CancelListener> cancelListener;
538 const std::shared_ptr<OHOS::AbilityRuntime::AbilityRuntime::WantAgent::WantAgent> agent = GetParamWantAgent();
539 OHOS::AbilityRuntime::AbilityRuntime::WantAgent::WantAgentHelper::
540 RegisterCancelListener(cancelListener, agent);
541 });
542
543 callFunctionMap_.emplace("WantAgentHelperUnregisterCancelListener", []() {
544 const std::shared_ptr<OHOS::AbilityRuntime::WantAgent::CancelListener> cancelListener
545 = GetParamCancelListener();
546 const std::shared_ptr<OHOS::AbilityRuntime::AbilityRuntime::WantAgent::WantAgent> agent = GetParamWantAgent();
547 OHOS::AbilityRuntime::AbilityRuntime::WantAgent::WantAgentHelper::
548 UnregisterCancelListener(cancelListener, agent);
549 });
550 }
551
552 // RegisterLauncherService
RegisterLauncherService()553 void NotificationFuzzTestManager::RegisterLauncherService()
554 {
555 callFunctionMap_.emplace("LauncherServiceGetAbilityList", []() {
556 std::shared_ptr<OHOS::AppExecFwk::LauncherService> temp = GetParamLauncherService();
557 const std::string bundleName = GetStringParam();
558 const int userId = GetIntParam();
559 std::vector<OHOS::AppExecFwk::LauncherAbilityInfo> launcherAbilityInfos = GetParamLauncherAbilityInfoVector();
560 temp->GetAbilityList(bundleName, userId, launcherAbilityInfos);
561 });
562
563 callFunctionMap_.emplace("LauncherServiceGetAbilityInfo", []() {
564 std::shared_ptr<OHOS::AppExecFwk::LauncherService> temp = GetParamLauncherService();
565 const Want want = *GetParamWant();
566 const int userI = GetIntParam();
567 OHOS::AppExecFwk::LauncherAbilityInfo launcherAbilityInfo = *GetParamLauncherAbilityInfo();
568 temp->GetAbilityInfo(want, userI, launcherAbilityInfo);
569 });
570
571 callFunctionMap_.emplace("LauncherServiceGetApplicationInfo", []() {
572 std::shared_ptr<OHOS::AppExecFwk::LauncherService> temp = GetParamLauncherService();
573 const std::string bundleName = GetStringParam();
574 const ApplicationFlag flags = GetParamApplicationFlag();
575 const int userId = GetIntParam();
576 ApplicationInfo applicationInfo = GetParamApplicationInfo();
577 temp->GetApplicationInfo(bundleName, flags, userId, applicationInfo);
578 });
579
580 callFunctionMap_.emplace("LauncherServiceIsBundleEnabled", []() {
581 std::shared_ptr<OHOS::AppExecFwk::LauncherService> temp = GetParamLauncherService();
582 const std::string bundleName = GetStringParam();
583 temp->IsBundleEnabled(bundleName);
584 });
585
586 callFunctionMap_.emplace("LauncherServiceIsAbilityEnabled", []() {
587 std::shared_ptr<OHOS::AppExecFwk::LauncherService> temp = GetParamLauncherService();
588 const OHOS::AppExecFwk::AbilityInfo abilityInfo = GetParamAbilityInfo();
589 temp->IsAbilityEnabled(abilityInfo);
590 });
591
592 callFunctionMap_.emplace("LauncherServiceRegisterCallback", []() {
593 std::shared_ptr<OHOS::AppExecFwk::LauncherService> temp = GetParamLauncherService();
594 const sptr<IBundleStatusCallback> callback = GetParamIBundleStatusCallback();
595 temp->RegisterCallback(callback);
596 });
597
598 callFunctionMap_.emplace("LauncherServiceUnRegisterCallback", []() {
599 std::shared_ptr<OHOS::AppExecFwk::LauncherService> temp = GetParamLauncherService();
600 temp->UnRegisterCallback();
601 });
602 }
603
604 // RegisterIAbilityContinuation
RegisterIAbilityContinuation()605 void NotificationFuzzTestManager::RegisterIAbilityContinuation()
606 {
607 callFunctionMap_.emplace("IAbilityContinuationOnStartContinuation", []() {
608 std::shared_ptr<OHOS::AppExecFwk::IAbilityContinuation> temp = GetParamAbility();
609 temp->OnStartContinuation();
610 });
611
612 callFunctionMap_.emplace("IAbilityContinuationOnSaveData", []() {
613 std::shared_ptr<OHOS::AppExecFwk::IAbilityContinuation> temp = GetParamAbility();
614 WantParams saveData = *GetParamWantParams();
615 temp->OnSaveData(saveData);
616 });
617
618 callFunctionMap_.emplace("IAbilityContinuationOnRestoreData", []() {
619 std::shared_ptr<OHOS::AppExecFwk::IAbilityContinuation> temp = GetParamAbility();
620 OHOS::AppExecFwk::WantParams restoreData = *GetParamWantParams();
621 temp->OnRestoreData(restoreData);
622 });
623
624 callFunctionMap_.emplace("IAbilityContinuationOnCompleteContinuation", []() {
625 std::shared_ptr<OHOS::AppExecFwk::IAbilityContinuation> temp = GetParamAbility();
626 int result = GetIntParam();
627 temp->OnCompleteContinuation(result);
628 });
629 }
630
631 // RegisterAbility
RegisterAbility()632 void NotificationFuzzTestManager::RegisterAbility()
633 {
634 callFunctionMap_.emplace("AbilityOnRequestPermissionsFromUserResult", []() {
635 std::shared_ptr<OHOS::AppExecFwk::Ability> temp = GetParamAbility();
636 int requestCode = GetIntParam();
637 const std::vector<std::string> permissions = GetStringVectorParam();
638 const std::vector<int> grantResults = GetIntVectorParam();
639 temp->OnRequestPermissionsFromUserResult(requestCode, permissions, grantResults);
640 });
641
642 callFunctionMap_.emplace("AbilityStartAbility", []() {
643 std::shared_ptr<OHOS::AppExecFwk::Ability> temp = GetParamAbility();
644 const Want want = *GetParamWant();
645 temp->StartAbility(want);
646 });
647 }
648
649 // RegisterAbilityContext
RegisterAbilityContext()650 void NotificationFuzzTestManager::RegisterAbilityContext()
651 {
652 callFunctionMap_.emplace("AbilityStartAbility", []() {
653 std::shared_ptr<OHOS::AppExecFwk::Ability> temp = GetParamAbility();
654 const AAFwk::Want want = *GetParamWant();
655 int requestCode = GetIntParam();
656 temp->StartAbility(want, requestCode);
657 });
658 }
659
660 // RegisterAbilityLifecycleCallbacks
RegisterAbilityLifecycleCallbacks()661 void NotificationFuzzTestManager::RegisterAbilityLifecycleCallbacks()
662 {
663 callFunctionMap_.emplace("AbilityLifecycleCallbacksOnAbilityActive", []() {
664 std::shared_ptr<OHOS::AppExecFwk::AbilityLifecycleCallbacks> temp = GetParamAbilityLifecycleCallbacks();
665 const std::shared_ptr<Ability> ability = GetParamAbility();
666 temp->OnAbilityActive(ability);
667 });
668 }
669
670 // RegisterIAbilityManager
RegisterIAbilityManager()671 void NotificationFuzzTestManager::RegisterIAbilityManager()
672 {
673 callFunctionMap_.emplace("IAbilityManagerMoveMissionToEnd", []() {
674 std::shared_ptr<OHOS::AAFwk::IAbilityManager> temp = GetParamIAbilityManager();
675 const sptr<IRemoteObject> token = GetParamSptrRemote();
676 const bool nonFirs = GetBoolParam();
677 temp->MoveMissionToEnd(token, nonFirs);
678 });
679
680 callFunctionMap_.emplace("IAbilityManagerIsFirstInMission", []() {
681 std::shared_ptr<OHOS::AAFwk::IAbilityManager> temp = GetParamIAbilityManager();
682 const sptr<IRemoteObject> token = GetParamSptrRemote();
683 temp->IsFirstInMission(token);
684 });
685
686 callFunctionMap_.emplace("IAbilityManagerPowerOn", []() {
687 std::shared_ptr<OHOS::AAFwk::IAbilityManager> temp = GetParamIAbilityManager();
688 temp->PowerOn();
689 });
690
691 callFunctionMap_.emplace("IAbilityManagerPowerOff", []() {
692 std::shared_ptr<OHOS::AAFwk::IAbilityManager> temp = GetParamIAbilityManager();
693 temp->PowerOff();
694 });
695 }
696
NotificationFuzzTestManager()697 NotificationFuzzTestManager::NotificationFuzzTestManager()
698 {
699 RegisterNotificationHelper();
700 RegisterNotificationSorting();
701 RegisterNotificationSortingMap();
702 RegisterNotificationSubscribeInfo();
703 RegisterNotificationSubscriber();
704 RegisterWantAgentHelper();
705 RegisterLauncherService();
706 RegisterIAbilityContinuation();
707 RegisterAbility();
708 RegisterAbilityContext();
709 RegisterContext();
710 RegisterAbilityLifecycleCallbacks();
711 RegisterIAbilityManager();
712 }
713
SetJsonFunction(std::string functionName)714 void NotificationFuzzTestManager::SetJsonFunction(std::string functionName)
715 {
716 remainderMap_.emplace(functionName, cycle_);
717 }
718
SetCycle(uint16_t cycle)719 void NotificationFuzzTestManager::SetCycle(uint16_t cycle)
720 {
721 cycle_ = cycle;
722 for_each(remainderMap_.begin(), remainderMap_.end(), [cycle](std::unordered_map<std::string, int>::reference temp) {
723 temp.second = cycle;
724 });
725 }
726
GetRandomInt(int minNum,int maxNum)727 int GetRandomInt(int minNum, int maxNum)
728 {
729 return GetU16Param() % (maxNum - minNum + 1) + minNum;
730 }
731
StartFuzzTest()732 void NotificationFuzzTestManager::StartFuzzTest()
733 {
734 std::cout << __func__ << std::endl;
735 OHOS::NotificationFuzzConfigParser jsonParser;
736 OHOS::FuzzTestData tempData;
737
738 std::cout << "parseFromFile start" << std::endl;
739 jsonParser.ParseFromFile4FuzzTest(FUZZ_TEST_CONFIG_FILE_PATH, tempData);
740 std::cout << "flag :" << tempData.mainLoopFlag << std::endl;
741 for_each(tempData.methodVec.begin(), tempData.methodVec.end(), [this](std::vector<std::string>::reference s) {
742 SetJsonFunction(s);
743 });
744 SetCycle(tempData.mainLoopFlag);
745
746 std::vector<std::string> index;
747 std::unordered_map<std::string, int>::iterator it = remainderMap_.begin();
748 while (it != remainderMap_.end()) {
749 if (it->second <= 0) {
750 it = remainderMap_.erase(it);
751 } else {
752 index.push_back(it->first);
753 ++it;
754 }
755 }
756
757 std::cout << remainderMap_.size() << "--------fuzz test start1111--------" << callFunctionMap_.size() << std::endl;
758 for (; remainderMap_.size() > 0;) {
759 std::string functionName;
760 int offset = GetRandomInt(0, index.size() - 1);
761 functionName = index[offset];
762 if (callFunctionMap_.find(functionName) != callFunctionMap_.end()) {
763 std::cout << "call function : " << functionName << std::endl;
764 callFunctionMap_[functionName]();
765 std::cout << "function end :" << functionName << std::endl;
766 } else {
767 std::cout << "can't find function : " << functionName << std::endl;
768 }
769 remainderMap_[functionName]--;
770 if (remainderMap_[functionName] <= 0) {
771 remainderMap_.erase(functionName);
772 index.erase(index.begin() + offset);
773 };
774 }
775 std::cout << remainderMap_.size() << "--------fuzz test end--------" << std::endl;
776 }
777 } // namespace Notification
778 } // namespace OHOS
779