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