/* * Copyright (c) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "bundle_info.h" #include "notification_live_view_content.h" #include "notification_record.h" #include #include #include #define private public #define protected public #include "advanced_notification_service.h" #undef private #undef protected #include "advancednotificationservice_fuzzer.h" #include "ans_dialog_callback_proxy.h" #include "ans_subscriber_stub.h" #include "ans_permission_def.h" #include "notification_request.h" constexpr uint8_t SLOT_TYPE_NUM = 5; constexpr uint8_t ENABLE = 2; namespace OHOS { bool DoSomethingInterestingWithMyAPI(FuzzData fuzzData) { auto service = std::make_shared(); service->InitPublishProcess(); service->CreateDialogManager(); std::string stringData = fuzzData.GenerateRandomString(); sptr notification = new Notification::NotificationRequest(); notification->SetOwnerUid(fuzzData.GenerateRandomInt32()); notification->SetCreatorUid(fuzzData.GenerateRandomInt32()); notification->SetSlotType(Notification::NotificationConstant::SlotType::LIVE_VIEW); auto content = std::make_shared(); notification->SetContent(std::make_shared(content)); service->Publish(stringData, notification); int notificationId = fuzzData.GenerateRandomInt32(); service->Cancel(notificationId, stringData, fuzzData.GenerateRandomInt32()); service->CancelAll(fuzzData.GenerateRandomInt32()); int32_t userId = fuzzData.GenerateRandomInt32(); service->CancelAsBundle(notificationId, stringData, userId); uint8_t type = fuzzData.GetData() % SLOT_TYPE_NUM; Notification::NotificationConstant::SlotType slotType = Notification::NotificationConstant::SlotType(type); service->AddSlotByType(slotType); std::vector> slots; service->AddSlots(slots); service->RemoveSlotByType(slotType); service->RemoveAllSlots(); sptr slot = new Notification::NotificationSlot(); service->GetSlotByType(slotType, slot); service->GetSlots(slots); sptr bundleOption = new Notification::NotificationBundleOption(); sptr buttonOption = new Notification::NotificationButtonOption(); bundleOption->SetBundleName(fuzzData.GenerateRandomString()); bundleOption->SetUid(fuzzData.GenerateRandomInt32()); uint64_t num = fuzzData.GetData(); service->CancelAsBundle(bundleOption, fuzzData.GenerateRandomInt32()); service->CancelAsBundleWithAgent(bundleOption, fuzzData.GenerateRandomInt32()); service->GetSlotNumAsBundle(bundleOption, num); std::vector> notifications; service->GetActiveNotifications(notifications, fuzzData.GenerateRandomInt32()); service->GetActiveNotificationNums(num); std::vector> notificationss; service->GetAllActiveNotifications(notificationss); std::vector key; service->GetSpecialActiveNotifications(key, notificationss); service->SetNotificationAgent(stringData); service->GetNotificationAgent(stringData); bool canPublish = fuzzData.GenerateRandomBool(); service->CanPublishAsBundle(stringData, canPublish); service->PublishAsBundle(notification, stringData); service->SetNotificationBadgeNum(num); int importance = fuzzData.GenerateRandomInt32(); service->GetBundleImportance(importance); bool granted = fuzzData.GenerateRandomBool(); service->HasNotificationPolicyAccessPermission(granted); int32_t removeReason = fuzzData.GenerateRandomInt32(); service->RemoveNotification(bundleOption, notificationId, stringData, removeReason); service->RemoveAllNotifications(bundleOption); service->Delete(stringData, removeReason); service->DeleteByBundle(bundleOption); service->DeleteAll(); service->GetSlotsByBundle(bundleOption, slots); service->UpdateSlots(bundleOption, slots); bool enabled = fuzzData.GenerateRandomBool(); service->SetNotificationsEnabledForBundle(stringData, enabled); service->SetNotificationsEnabledForAllBundles(stringData, enabled); service->SetNotificationsEnabledForSpecialBundle(stringData, bundleOption, enabled); service->SetShowBadgeEnabledForBundle(bundleOption, enabled); service->GetShowBadgeEnabledForBundle(bundleOption, enabled); service->GetShowBadgeEnabled(enabled); sptr subscriber = new Notification::AnsSubscriberStub(); sptr info = new Notification::NotificationSubscribeInfo(); service->Subscribe(subscriber, info); service->Unsubscribe(subscriber, info); bool allowed = fuzzData.GenerateRandomBool(); service->IsAllowedNotify(allowed); service->IsAllowedNotifySelf(bundleOption, allowed); service->IsAllowedNotifyForBundle(bundleOption, allowed); service->IsSpecialBundleAllowedNotify(bundleOption, allowed); service->CancelGroup(stringData, fuzzData.GenerateRandomInt32()); service->RemoveGroupByBundle(bundleOption, stringData); sptr date = new Notification::NotificationDoNotDisturbDate(); service->SetDoNotDisturbDate(date); service->GetDoNotDisturbDate(date); bool doesSupport = fuzzData.GenerateRandomBool(); service->DoesSupportDoNotDisturbMode(doesSupport); service->IsDistributedEnabled(enabled); service->EnableDistributedByBundle(bundleOption, enabled); service->EnableDistributedSelf(enabled); service->EnableDistributed(enabled); service->IsDistributedEnableByBundle(bundleOption, enabled); Notification::NotificationConstant::RemindType remindType; service->GetDeviceRemindType(remindType); sptr request = new Notification::NotificationRequest(); service->PublishContinuousTaskNotification(request); service->CancelContinuousTaskNotification(stringData, notificationId); sptr reminder = new Notification::ReminderRequest(); service->PublishReminder(reminder); int32_t reminderId = fuzzData.GenerateRandomInt32(); service->CancelReminder(reminderId); std::vector> reminders; service->GetValidReminders(reminders); service->CancelAllReminders(); uint64_t excludeDate = fuzzData.GetData(); service->AddExcludeDate(reminderId, excludeDate); service->DelExcludeDates(reminderId); std::vector excludeDates; service->GetExcludeDates(reminderId, excludeDates); bool support = fuzzData.GenerateRandomBool(); service->IsSupportTemplate(stringData, support); service->IsSpecialUserAllowedNotify(userId, allowed); int32_t deviceIds = fuzzData.GenerateRandomInt32(); service->SetNotificationsEnabledByUser(deviceIds, enabled); service->DeleteAllByUser(userId); service->SetDoNotDisturbDate(date); service->GetDoNotDisturbDate(date); service->SetEnabledForBundleSlot(bundleOption, slotType, enabled, false); service->GetEnabledForBundleSlot(bundleOption, slotType, enabled); std::vector dumpInfo; service->ShellDump(stringData, stringData, userId, userId, dumpInfo); service->SetSyncNotificationEnabledWithoutApp(userId, enabled); service->GetSyncNotificationEnabledWithoutApp(userId, enabled); int32_t badgeNum = fuzzData.GenerateRandomInt32(); service->SetBadgeNumber(badgeNum, fuzzData.GenerateRandomInt32()); sptr dialogCallback = new Notification::AnsDialogCallbackProxy(nullptr); sptr callerToken = new Notification::AnsSubscriberStub(); std::shared_ptr groupInfo; service->RequestEnableNotification(stringData, dialogCallback, callerToken); bool enable = fuzzData.GenerateRandomBool(); std::string bundleName = fuzzData.GenerateRandomString(); std::string phoneNumber = fuzzData.GenerateRandomString(); std::string groupName = fuzzData.GenerateRandomString(); std::string deviceType = fuzzData.GenerateRandomString(); std::string localSwitch = fuzzData.GenerateRandomString(); std::vector> recordList; bool isNative = fuzzData.GenerateRandomBool(); service->CanPopEnableNotificationDialog(nullptr, enable, bundleName); std::vector keys; std::string key1 = fuzzData.GenerateRandomString(); keys.emplace_back(fuzzData.GenerateRandomString()); service->RemoveNotifications(keys, fuzzData.GenerateRandomInt32()); service->SetBadgeNumberByBundle(bundleOption, fuzzData.GenerateRandomInt32()); service->SetDistributedEnabledByBundle(bundleOption, fuzzData.GenerateRandomString(), fuzzData.GenerateRandomBool()); service->IsDistributedEnableByBundle(bundleOption, enable); service->SetDefaultNotificationEnabled(bundleOption, enabled); service->ExcuteCancelAll(bundleOption, fuzzData.GenerateRandomInt32()); service->ExcuteDelete(stringData, fuzzData.GenerateRandomInt32()); service->HandleBadgeEnabledChanged(bundleOption, enabled); service->RemoveSystemLiveViewNotifications(bundleName, fuzzData.GenerateRandomInt32()); service->RemoveSystemLiveViewNotificationsOfSa(fuzzData.GenerateRandomInt32()); service->TriggerLocalLiveView(bundleOption, fuzzData.GenerateRandomInt32(), buttonOption); service->RemoveNotificationBySlot(bundleOption, slot, fuzzData.GenerateRandomInt32()); service->IsNeedSilentInDoNotDisturbMode(phoneNumber, fuzzData.GenerateRandomInt32()); service->CheckNeedSilent(phoneNumber, fuzzData.GenerateRandomInt32(), fuzzData.GenerateRandomInt32()); service->ExcuteCancelGroupCancel(bundleOption, groupName, fuzzData.GenerateRandomInt32()); service->RemoveNotificationFromRecordList(recordList); service->UpdateUnifiedGroupInfo(key1, groupInfo); service->PublishNotificationBySa(request); service->IsDistributedEnabledByBundle(bundleOption, deviceType, enabled); service->DuplicateMsgControl(request); service->DeleteDuplicateMsgs(bundleOption); service->RemoveExpiredUniqueKey(); service->SetSmartReminderEnabled(deviceType, enabled); service->IsSmartReminderEnabled(deviceType, enabled); service->SetTargetDeviceStatus(deviceType, fuzzData.GenerateRandomInt32()); service->ClearAllNotificationGroupInfo(localSwitch); OHOS::DoTestForAdvancedNotificationUtils(service, fuzzData); OHOS::DoTestForAdvancedNotificationService(service, fuzzData); return true; } bool DoTestForAdvancedNotificationUtils(std::shared_ptr service, FuzzData fuzzData) { std::string randomString = fuzzData.GenerateRandomString(); int32_t randomInt32 = fuzzData.GenerateRandomInt32(); int64_t randomInt64 = fuzzData.GetData(); sptr bundleOption = new Notification::NotificationBundleOption(); bundleOption->SetBundleName(randomString); bundleOption->SetUid(randomInt32); sptr targetBundleOption = nullptr; sptr request = new Notification::NotificationRequest(); request->SetSlotType(Notification::NotificationConstant::SlotType::LIVE_VIEW); auto content = std::make_shared(); request->SetContent(std::make_shared(content)); request->SetOwnerUid(randomInt32); request->SetCreatorUid(randomInt32); auto flag = std::make_shared(); request->SetFlags(flag); service->GetAppTargetBundle(bundleOption, targetBundleOption); std::vector infos; infos.emplace_back(randomString); service->SetAgentNotification(request, randomString); service->ActiveNotificationDump(randomString, randomInt32, randomInt32, infos); service->RecentNotificationDump(randomString, randomInt32, randomInt32, infos); service->OnBundleRemoved(bundleOption); service->OnBundleDataAdd(bundleOption); service->OnBundleDataUpdate(bundleOption); service->GetBundlesOfActiveUser(); service->InitNotificationEnableList(); AppExecFwk::BundleInfo bundleInfo; service->GetBundleInfoByNotificationBundleOption(bundleOption, bundleInfo); std::shared_ptr record = service->MakeNotificationRecord(request, bundleOption); record->slot = new Notification::NotificationSlot(Notification::NotificationConstant::SlotType::LIVE_VIEW); service->PrePublishNotificationBySa(request, randomInt32, randomString); service->SetRequestBundleInfo(request, randomInt32, randomString); std::vector args; args.emplace_back(fuzzData.GetData()); service->Dump(randomInt32, args); service->OnResourceRemove(randomInt32); service->CheckApiCompatibility(bundleOption); service->OnBundleDataCleared(bundleOption); service->CheckPublishWithoutApp(randomInt32, request); service->GetLocalNotificationKeys(bundleOption); service->OnDistributedPublish(randomString, randomString, request); service->OnDistributedUpdate(randomString, randomString, request); service->OnDistributedDelete(randomString, randomString, randomString, randomInt32); service->AdjustDateForDndTypeOnce(randomInt64, randomInt64); return true; } bool DoTestForAdvancedNotificationService(std::shared_ptr service, FuzzData fuzzData) { std::string randomString = fuzzData.GenerateRandomString(); int32_t randomInt32 = fuzzData.GenerateRandomInt32(); int64_t randomInt64 = fuzzData.GetData(); sptr bundleOption = new Notification::NotificationBundleOption(); bundleOption->SetBundleName(randomString); bundleOption->SetUid(randomInt32); sptr targetBundleOption = nullptr; sptr request = new Notification::NotificationRequest(); request->SetSlotType(Notification::NotificationConstant::SlotType::LIVE_VIEW); auto content = std::make_shared(); request->SetContent(std::make_shared(content)); request->SetOwnerUid(randomInt32); request->SetCreatorUid(randomInt32); auto flag = std::make_shared(); request->SetFlags(flag); std::shared_ptr record = service->MakeNotificationRecord(request, bundleOption); record->slot = new Notification::NotificationSlot(Notification::NotificationConstant::SlotType::LIVE_VIEW); service->StartFinishTimer(record, randomInt64, randomInt32); service->StartUpdateTimer(record, randomInt64, randomInt32); service->StartArchiveTimer(record); service->PublishPreparedNotification(request, bundleOption, fuzzData.GenerateRandomBool()); service->QueryDoNotDisturbProfile(randomInt32, randomString, randomString); service->CheckDoNotDisturbProfile(record); service->DoNotDisturbUpdataReminderFlags(record); service->UpdateSlotAuthInfo(record); service->Filter(record, fuzzData.GenerateRandomBool()); service->ChangeNotificationByControlFlags(record, fuzzData.GenerateRandomBool()); service->CheckPublishPreparedNotification(record, fuzzData.GenerateRandomBool()); service->UpdateInNotificationList(record); service->PublishFlowControl(record); service->IsNeedPushCheck(request); return true; } } /* Fuzzer entry point */ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { /* Run your code on data */ if (data != nullptr && size >= GetU32Size()) { OHOS::FuzzData fuzzData(data, size); std::vector requestPermission = { OHOS::Notification::OHOS_PERMISSION_NOTIFICATION_CONTROLLER, OHOS::Notification::OHOS_PERMISSION_NOTIFICATION_AGENT_CONTROLLER, OHOS::Notification::OHOS_PERMISSION_SET_UNREMOVABLE_NOTIFICATION }; SystemHapTokenGet(requestPermission); OHOS::DoSomethingInterestingWithMyAPI(fuzzData); } return 0; }