1 /* 2 * Copyright (c) 2022 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 "addnotificationslots_fuzzer.h" 17 #include "notification_helper.h" 18 #include <fuzzer/FuzzedDataProvider.h> 19 20 constexpr uint8_t SLOT_LEVEL_NUM = 6; 21 constexpr uint8_t SLOT_VISIBLENESS_TYPE_NUM = 4; 22 constexpr uint8_t SLOT_TYPE_NUM = 5; 23 24 namespace OHOS { DoSomethingInterestingWithMyAPI(FuzzedDataProvider * fdp)25 bool DoSomethingInterestingWithMyAPI(FuzzedDataProvider* fdp) 26 { 27 std::string stringData = fdp->ConsumeRandomLengthString(); 28 Notification::NotificationSlot slot; 29 slot.SetDescription(stringData); 30 slot.SetEnableLight(fdp->ConsumeBool()); 31 slot.SetEnableVibration(fdp->ConsumeBool()); 32 slot.SetLedLightColor(fdp->ConsumeIntegral<uint32_t>()); 33 34 uint8_t level = fdp->ConsumeIntegral<uint8_t>() % SLOT_LEVEL_NUM; 35 Notification::NotificationSlot::NotificationLevel notificatoinLevel = 36 Notification::NotificationSlot::NotificationLevel(level); 37 slot.SetLevel(notificatoinLevel); 38 39 uint8_t visibleness = fdp->ConsumeIntegral<uint8_t>() % SLOT_VISIBLENESS_TYPE_NUM; 40 Notification::NotificationConstant::VisiblenessType visiblenessType = 41 Notification::NotificationConstant::VisiblenessType(visibleness); 42 slot.SetLockscreenVisibleness(visiblenessType); 43 44 uint8_t type = fdp->ConsumeIntegral<uint8_t>() % SLOT_TYPE_NUM; 45 Notification::NotificationConstant::SlotType slotType = Notification::NotificationConstant::SlotType(type); 46 slot.SetType(slotType); 47 48 std::vector<Notification::NotificationSlot> slots; 49 slots.emplace_back(slot); 50 return Notification::NotificationHelper::AddNotificationSlots(slots) == ERR_OK; 51 } 52 } 53 54 /* Fuzzer entry point */ LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)55extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) 56 { 57 /* Run your code on data */ 58 FuzzedDataProvider fdp(data, size); 59 OHOS::DoSomethingInterestingWithMyAPI(&fdp); 60 return 0; 61 } 62