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