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
23 namespace OHOS {
24
DoSomethingInterestingWithMyAPI(FuzzData fuzzData)25 bool DoSomethingInterestingWithMyAPI(FuzzData fuzzData)
26 {
27 Notification::NotificationHelper notificationHelper;
28 // test IsSoundEnabled function
29 std::string representativeBundle = fuzzData.GenerateRandomString();
30 Notification::NotificationRequest notification;
31 notification.SetOwnerUid(fuzzData.GenerateRandomInt32());
32 notification.SetCreatorUid(fuzzData.GenerateRandomInt32());
33 notification.SetSlotType(Notification::NotificationConstant::SlotType::LIVE_VIEW);
34 auto content = std::make_shared<Notification::NotificationLiveViewContent>();
35 notification.SetContent(std::make_shared<Notification::NotificationContent>(content));
36 notificationHelper.PublishNotificationAsBundle(representativeBundle, notification);
37 notificationHelper.RemoveNotifications();
38 int32_t intData = fuzzData.GenerateRandomInt32();
39 bool enabled = fuzzData.GenerateRandomBool();
40 notificationHelper.SetNotificationsEnabledForAllBundles(intData, enabled);
41 Notification::NotificationBundleOption bundleOption;
42 bundleOption.SetBundleName(fuzzData.GenerateRandomString());
43 bundleOption.SetUid(fuzzData.GenerateRandomInt32());
44 uint32_t flag = 0;
45 notificationHelper.GetNotificationSlotFlagsAsBundle(bundleOption, flag);
46 notificationHelper.SetNotificationSlotFlagsAsBundle(bundleOption, intData);
47 notificationHelper.CancelAsBundle(bundleOption, intData);
48 return true;
49 }
50 }
51
52 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)53 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
54 {
55 if (data != nullptr && size >= GetU32Size()) {
56 OHOS::FuzzData fuzzData(data, size);
57 OHOS::DoSomethingInterestingWithMyAPI(fuzzData);
58 }
59 return 0;
60 }
61