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
20 namespace OHOS {
21 namespace {
22 constexpr uint8_t ENABLE = 2;
23 constexpr uint8_t SLOT_LEVEL_NUM = 6;
24 constexpr uint8_t SLOT_VISIBLENESS_TYPE_NUM = 4;
25 constexpr uint8_t SLOT_TYPE_NUM = 5;
26 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)27 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
28 {
29 std::string stringData(data);
30
31 Notification::NotificationSlot slot;
32 slot.SetDescription(stringData);
33 slot.SetEnableLight(*data % ENABLE);
34 slot.SetEnableVibration(*data % ENABLE);
35 slot.SetLedLightColor(GetU32Data(data));
36
37 uint8_t level = *data % SLOT_LEVEL_NUM;
38 Notification::NotificationSlot::NotificationLevel notificatoinLevel =
39 Notification::NotificationSlot::NotificationLevel(level);
40 slot.SetLevel(notificatoinLevel);
41
42 uint8_t visibleness = *data % SLOT_VISIBLENESS_TYPE_NUM;
43 Notification::NotificationConstant::VisiblenessType visiblenessType =
44 Notification::NotificationConstant::VisiblenessType(visibleness);
45 slot.SetLockscreenVisibleness(visiblenessType);
46
47 uint8_t type = *data % SLOT_TYPE_NUM;
48 Notification::NotificationConstant::SlotType slotType = Notification::NotificationConstant::SlotType(type);
49 slot.SetType(slotType);
50
51 return Notification::NotificationHelper::AddNotificationSlot(slot) == ERR_OK;
52 }
53 }
54
55 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)56 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
57 {
58 /* Run your code on data */
59 char *ch = ParseData(data, size);
60 if (ch != nullptr && size > GetU32Size()) {
61 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
62 free(ch);
63 ch = nullptr;
64 }
65 return 0;
66 }
67