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 #define private public
17 #define protected public
18 #include "notification_helper.h"
19 #undef private
20 #undef protected
21 #include "notificationhelper_fuzzer.h"
22 #include <fuzzer/FuzzedDataProvider.h>
23
24 namespace OHOS {
25
DoSomethingInterestingWithMyAPI(FuzzedDataProvider * fdp)26 bool DoSomethingInterestingWithMyAPI(FuzzedDataProvider *fdp)
27 {
28 Notification::NotificationHelper notificationHelper;
29 std::string stringData = fdp->ConsumeRandomLengthString();
30 int32_t intData = fdp->ConsumeIntegral<int32_t>();
31 // test IsSoundEnabled function
32 std::string representativeBundle = stringData;
33 Notification::NotificationRequest notification;
34 notification.SetOwnerUid(intData);
35 notification.SetCreatorUid(intData);
36 notification.SetSlotType(Notification::NotificationConstant::SlotType::LIVE_VIEW);
37 auto content = std::make_shared<Notification::NotificationLiveViewContent>();
38 notification.SetContent(std::make_shared<Notification::NotificationContent>(content));
39 notificationHelper.PublishNotificationAsBundle(representativeBundle, notification);
40 notificationHelper.RemoveNotifications();
41
42 bool enabled = fdp->ConsumeBool();
43 notificationHelper.SetNotificationsEnabledForAllBundles(stringData, enabled);
44 Notification::NotificationBundleOption bundleOption;
45 bundleOption.SetBundleName(stringData);
46 bundleOption.SetUid(intData);
47 uint32_t flag = 0;
48 notificationHelper.GetNotificationSlotFlagsAsBundle(bundleOption, flag);
49 notificationHelper.SetNotificationSlotFlagsAsBundle(bundleOption, intData);
50 notificationHelper.CancelAsBundle(bundleOption, intData);
51 return true;
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 FuzzedDataProvider fdp(data, size);
59 OHOS::DoSomethingInterestingWithMyAPI(&fdp);
60 return 0;
61 }
62