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