1 /* 2 * Copyright (c) 2025 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 "notification_capsule.h" 16 #include "notification_disable.h" 17 #include "notification_do_not_disturb_profile.h" 18 #include "notification_icon_button.h" 19 #include "notification_live_view_content.h" 20 #include "notification_local_live_view_button.h" 21 #include "notification_local_live_view_content.h" 22 #include "notification_operation_info.h" 23 #include "notification_unified_group_Info.h" 24 #include "resource_manager.h" 25 #include "notificationparcel_fuzzer.h" 26 #include <fuzzer/FuzzedDataProvider.h> 27 28 namespace OHOS { 29 namespace Notification { TestNotificationCapsuleParcel(FuzzedDataProvider * fdp)30 void TestNotificationCapsuleParcel(FuzzedDataProvider *fdp) 31 { 32 NotificationCapsule capsule; 33 Parcel parcel; 34 nlohmann::json jsonObject; 35 std::string stringData = fdp->ConsumeRandomLengthString(); 36 int32_t time = fdp->ConsumeIntegral<int32_t>(); 37 38 capsule.SetTitle(stringData); 39 capsule.GetTitle(); 40 capsule.SetBackgroundColor(stringData); 41 capsule.GetBackgroundColor(); 42 capsule.SetContent(stringData); 43 capsule.GetContent(); 44 capsule.SetTime(time); 45 capsule.GetTime(); 46 capsule.ToJson(jsonObject); 47 capsule.Dump(); 48 capsule.FromJson(jsonObject); 49 capsule.Marshalling(parcel); 50 capsule.Unmarshalling(parcel); 51 } 52 TestNotificationDoNotDisturbProfileParcel(FuzzedDataProvider * fdp)53 void TestNotificationDoNotDisturbProfileParcel(FuzzedDataProvider *fdp) 54 { 55 NotificationDoNotDisturbProfile disturbProfile; 56 Parcel parcel; 57 int64_t profileId = fdp->ConsumeIntegral<int64_t>(); 58 std::string profileName = fdp->ConsumeRandomLengthString(); 59 std::vector<NotificationBundleOption> trustList; 60 NotificationBundleOption bundleOption; 61 trustList.push_back(bundleOption); 62 63 disturbProfile.SetProfileId(profileId); 64 disturbProfile.GetProfileId(); 65 disturbProfile.SetProfileName(profileName); 66 disturbProfile.GetProfileName(); 67 disturbProfile.SetProfileTrustList(trustList); 68 disturbProfile.GetProfileTrustList(); 69 disturbProfile.Marshalling(parcel); 70 disturbProfile.ReadFromParcel(parcel); 71 disturbProfile.Unmarshalling(parcel); 72 std::string disturbProfileJson = disturbProfile.ToJson(); 73 disturbProfile.FromJson(disturbProfileJson); 74 } 75 TestNotificationDisableParcel(FuzzedDataProvider * fdp)76 void TestNotificationDisableParcel(FuzzedDataProvider *fdp) 77 { 78 NotificationDisable notificationDisable; 79 std::vector<std::string> bundleList; 80 bool disabled = fdp->ConsumeBool(); 81 Parcel parcel; 82 83 bundleList.emplace_back(fdp->ConsumeRandomLengthString()); 84 notificationDisable.SetDisabled(disabled); 85 notificationDisable.GetDisabled(); 86 notificationDisable.SetBundleList(bundleList); 87 notificationDisable.GetBundleList(); 88 89 notificationDisable.Marshalling(parcel); 90 notificationDisable.ReadFromParcel(parcel); 91 notificationDisable.Unmarshalling(parcel); 92 93 std::string jsonObj = notificationDisable.ToJson(); 94 notificationDisable.FromJson(jsonObj); 95 } 96 TestNotificationLiveViewContentParcel(FuzzedDataProvider * fdp)97 void TestNotificationLiveViewContentParcel(FuzzedDataProvider *fdp) 98 { 99 NotificationLiveViewContent liveViewContent; 100 PictureMap picMap; 101 nlohmann::json jsonObject; 102 auto extraInfo = std::make_shared<AAFwk::WantParams>(); 103 104 liveViewContent.SetVersion(fdp->ConsumeIntegral<uint32_t>()); 105 liveViewContent.GetVersion(); 106 liveViewContent.SetText(fdp->ConsumeRandomLengthString()); 107 liveViewContent.SetTitle(fdp->ConsumeRandomLengthString()); 108 liveViewContent.SetAdditionalText(fdp->ConsumeRandomLengthString()); 109 liveViewContent.SetIsOnlyLocalUpdate(fdp->ConsumeBool()); 110 liveViewContent.SetPicture(picMap); 111 liveViewContent.SetExtraInfo(extraInfo); 112 liveViewContent.Dump(); 113 liveViewContent.ToJson(jsonObject); 114 liveViewContent.FromJson(jsonObject); 115 } 116 TestNotificationLocalLiveViewButtonParcel(FuzzedDataProvider * fdp)117 void TestNotificationLocalLiveViewButtonParcel(FuzzedDataProvider *fdp) 118 { 119 NotificationLocalLiveViewButton button; 120 std::string buttonName = fdp->ConsumeRandomLengthString(); 121 auto pixelMapOne = std::make_shared<Media::PixelMap>(); 122 auto pixelMapTwo = std::make_shared<Media::PixelMap>(); 123 auto iconResource = std::make_shared<ResourceManager::Resource>(); 124 iconResource->id = fdp->ConsumeIntegral<int32_t>(); 125 iconResource->bundleName = fdp->ConsumeRandomLengthString(); 126 iconResource->moduleName = fdp->ConsumeRandomLengthString(); 127 Parcel parcel; 128 nlohmann::json jsonObj; 129 130 button.addSingleButtonName(buttonName); 131 button.GetAllButtonNames(); 132 button.addSingleButtonIcon(pixelMapOne); 133 button.addSingleButtonIcon(pixelMapTwo); 134 button.GetAllButtonIcons(); 135 button.addSingleButtonIconResource(iconResource); 136 button.GetAllButtonIconResource(); 137 button.Dump(); 138 139 button.ToJson(jsonObj); 140 button.FromJson(jsonObj); 141 142 button.Marshalling(parcel); 143 button.Unmarshalling(parcel); 144 button.ClearButtonIcons(); 145 button.ClearButtonIconsResource(); 146 } 147 TestNotificationLocalLiveViewContentParcel(FuzzedDataProvider * fdp)148 void TestNotificationLocalLiveViewContentParcel(FuzzedDataProvider *fdp) 149 { 150 NotificationLocalLiveViewContent content; 151 NotificationCapsule capsule; 152 NotificationLocalLiveViewButton button; 153 NotificationIconButton iconButton; 154 std::vector<NotificationIconButton> iconButtons; 155 NotificationProgress progress; 156 NotificationTime time; 157 Parcel parcel; 158 nlohmann::json jsonObj; 159 int32_t flag = fdp->ConsumeIntegral<int32_t>(); 160 iconButtons.push_back(iconButton); 161 content.SetType(fdp->ConsumeIntegral<int32_t>()); 162 content.GetType(); 163 content.SetCapsule(capsule); 164 content.GetCapsule(); 165 content.SetButton(button); 166 content.GetButton(); 167 content.SetCardButton(iconButtons); 168 content.GetCardButton(); 169 content.SetProgress(progress); 170 content.GetProgress(); 171 content.SetTime(time); 172 content.GetTime(); 173 content.addFlag(flag); 174 content.isFlagExist(flag); 175 content.isFlagExist(fdp->ConsumeIntegral<int32_t>()); 176 content.Dump(); 177 178 content.ToJson(jsonObj); 179 content.FromJson(jsonObj); 180 181 content.Marshalling(parcel); 182 content.Unmarshalling(parcel); 183 184 content.ClearButton(); 185 content.ClearCapsuleIcon(); 186 } 187 TestNotificationOperationInfoParcel(FuzzedDataProvider * fdp)188 void TestNotificationOperationInfoParcel(FuzzedDataProvider *fdp) 189 { 190 NotificationOperationInfo info; 191 std::string actionName = fdp->ConsumeRandomLengthString(); 192 std::string userInput = fdp->ConsumeRandomLengthString(); 193 std::string hashCode = fdp->ConsumeRandomLengthString(); 194 std::string eventId = fdp->ConsumeRandomLengthString(); 195 Parcel parcel; 196 197 info.SetActionName(actionName); 198 info.GetActionName(); 199 info.SetUserInput(userInput); 200 info.GetUserInput(); 201 info.SetHashCode(hashCode); 202 info.GetHashCode(); 203 info.SetEventId(eventId); 204 info.GetEventId(); 205 info.Dump(); 206 info.Marshalling(parcel); 207 info.Unmarshalling(parcel); 208 } 209 TestNotificationUnifiedGroupInfoParcel(FuzzedDataProvider * fdp)210 void TestNotificationUnifiedGroupInfoParcel(FuzzedDataProvider *fdp) 211 { 212 NotificationUnifiedGroupInfo info; 213 std::string key = fdp->ConsumeRandomLengthString(); 214 std::string title = fdp->ConsumeRandomLengthString(); 215 std::string content = fdp->ConsumeRandomLengthString(); 216 std::string sceneName = fdp->ConsumeRandomLengthString(); 217 auto extraInfo = std::make_shared<AAFwk::WantParams>(); 218 Parcel parcel; 219 220 info.SetKey(key); 221 info.GetKey(); 222 info.SetTitle(title); 223 info.GetTitle(); 224 info.SetContent(content); 225 info.GetContent(); 226 info.SetSceneName(sceneName); 227 info.GetSceneName(); 228 info.SetExtraInfo(extraInfo); 229 info.GetExtraInfo(); 230 info.Dump(); 231 info.Marshalling(parcel); 232 info.Unmarshalling(parcel); 233 } 234 DoSomethingInterestingWithMyAPI(FuzzedDataProvider * fdp)235 bool DoSomethingInterestingWithMyAPI(FuzzedDataProvider *fdp) 236 { 237 TestNotificationCapsuleParcel(fdp); 238 TestNotificationDoNotDisturbProfileParcel(fdp); 239 TestNotificationDisableParcel(fdp); 240 TestNotificationLiveViewContentParcel(fdp); 241 TestNotificationLocalLiveViewButtonParcel(fdp); 242 TestNotificationLocalLiveViewContentParcel(fdp); 243 TestNotificationOperationInfoParcel(fdp); 244 TestNotificationUnifiedGroupInfoParcel(fdp); 245 return true; 246 } 247 } 248 } 249 250 /* Fuzzer entry point */ LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)251extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) 252 { 253 /* Run your code on data */ 254 FuzzedDataProvider fdp(data, size); 255 OHOS::Notification::DoSomethingInterestingWithMyAPI(&fdp); 256 return 0; 257 } 258