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 
18 #include "notification_helper.h"
19 
20 constexpr uint8_t ENABLE = 2;
21 constexpr uint8_t SLOT_LEVEL_NUM = 6;
22 constexpr uint8_t SLOT_VISIBLENESS_TYPE_NUM = 4;
23 constexpr uint8_t SLOT_TYPE_NUM = 5;
24 
25 namespace OHOS {
DoSomethingInterestingWithMyAPI(const char * data,size_t size)26     bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
27     {
28         std::string stringData(data);
29 
30         Notification::NotificationSlot slot;
31         slot.SetDescription(stringData);
32         slot.SetEnableLight(*data % ENABLE);
33         slot.SetEnableVibration(*data % ENABLE);
34         slot.SetLedLightColor(GetU32Data(data));
35 
36         uint8_t level = *data % SLOT_LEVEL_NUM;
37         Notification::NotificationSlot::NotificationLevel notificatoinLevel =
38             Notification::NotificationSlot::NotificationLevel(level);
39         slot.SetLevel(notificatoinLevel);
40 
41         uint8_t visibleness = *data % SLOT_VISIBLENESS_TYPE_NUM;
42         Notification::NotificationConstant::VisiblenessType visiblenessType =
43             Notification::NotificationConstant::VisiblenessType(visibleness);
44         slot.SetLockscreenVisibleness(visiblenessType);
45 
46         uint8_t type = *data % SLOT_TYPE_NUM;
47         Notification::NotificationConstant::SlotType slotType = Notification::NotificationConstant::SlotType(type);
48         slot.SetType(slotType);
49 
50         std::vector<Notification::NotificationSlot> slots;
51         slots.emplace_back(slot);
52         return Notification::NotificationHelper::AddNotificationSlots(slots) == 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     char *ch = ParseData(data, size);
61     if (ch != nullptr && size >= GetU32Size()) {
62         OHOS::DoSomethingInterestingWithMyAPI(ch, size);
63         free(ch);
64         ch = nullptr;
65     }
66     return 0;
67 }
68