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 "ans_manager_stub.h"
19 #include "ans_permission_def.h"
20 #undef private
21 #undef protected
22 #include "ansmanagerstubannexthree_fuzzer.h"
23 
24 namespace OHOS {
25     namespace {
26         constexpr uint8_t ENABLE = 2;
27         constexpr uint8_t SLOT_TYPE_NUM = 5;
28     }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)29     bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
30     {
31         Notification::AnsManagerStub ansManagerStub;
32         bool allow = *data % ENABLE;
33         sptr<Notification::NotificationBundleOption> bundleOption = new Notification::NotificationBundleOption();
34         int notificationId = 1;
35         std::string stringData(data);
36         int32_t removeReason = static_cast<int32_t>(GetU32Data(data));
37         ansManagerStub.RemoveNotification(bundleOption, notificationId, stringData, removeReason);
38         ansManagerStub.RemoveAllNotifications(bundleOption);
39         ansManagerStub.Delete(stringData, removeReason);
40         ansManagerStub.DeleteByBundle(bundleOption);
41         ansManagerStub.DeleteAll();
42         sptr<Notification::NotificationSlot> slot = new Notification::NotificationSlot();
43         std::vector<sptr<Notification::NotificationSlot>> slots;
44         slots.emplace_back(slot);
45         ansManagerStub.GetSlotsByBundle(bundleOption, slots);
46         ansManagerStub.SetNotificationsEnabledForSpecialBundle(stringData, bundleOption, allow);
47         ansManagerStub.SetShowBadgeEnabledForBundle(bundleOption, allow);
48         ansManagerStub.GetShowBadgeEnabledForBundle(bundleOption, allow);
49         ansManagerStub.GetShowBadgeEnabled(allow);
50         ansManagerStub.IsAllowedNotify(allow);
51         ansManagerStub.IsAllowedNotifySelf(allow);
52         ansManagerStub.IsSpecialBundleAllowedNotify(bundleOption, allow);
53         ansManagerStub.CancelGroup(stringData, 0);
54         ansManagerStub.RemoveGroupByBundle(bundleOption, stringData);
55         ansManagerStub.DoesSupportDoNotDisturbMode(allow);
56         ansManagerStub.IsDistributedEnabled(allow);
57         ansManagerStub.EnableDistributed(allow);
58         ansManagerStub.EnableDistributedByBundle(bundleOption, allow);
59         ansManagerStub.EnableDistributedSelf(allow);
60         ansManagerStub.IsDistributedEnableByBundle(bundleOption, allow);
61         int32_t remindType = static_cast<int32_t>(*data % SLOT_TYPE_NUM);
62         Notification::NotificationConstant::RemindType remind =
63             Notification::NotificationConstant::RemindType(remindType);
64         ansManagerStub.GetDeviceRemindType(remind);
65         sptr<Notification::NotificationRequest> request = new Notification::NotificationRequest();
66         ansManagerStub.PublishContinuousTaskNotification(request);
67         ansManagerStub.CancelContinuousTaskNotification(stringData, removeReason);
68         ansManagerStub.CancelReminder(removeReason);
69         return ansManagerStub.CancelAllReminders();
70     }
71 }
72 
73 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)74 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
75 {
76     /* Run your code on data */
77     char *ch = ParseData(data, size);
78     if (ch != nullptr && size >= GetU32Size()) {
79         std::vector<std::string> requestPermission = {
80             OHOS::Notification::OHOS_PERMISSION_NOTIFICATION_CONTROLLER,
81             OHOS::Notification::OHOS_PERMISSION_NOTIFICATION_AGENT_CONTROLLER,
82             OHOS::Notification::OHOS_PERMISSION_SET_UNREMOVABLE_NOTIFICATION
83         };
84         SystemHapTokenGet(requestPermission);
85         OHOS::DoSomethingInterestingWithMyAPI(ch, size);
86         free(ch);
87         ch = nullptr;
88     }
89     return 0;
90 }
91