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 #undef private
20 #undef protected
21 #include "ansmanagerstubannextwo_fuzzer.h"
22
23 namespace OHOS {
24 namespace {
25 constexpr uint8_t ENABLE = 2;
26 constexpr uint8_t SLOT_TYPE_NUM = 5;
27 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)28 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
29 {
30 Notification::AnsManagerStub ansManagerStub;
31 MessageParcel datas;
32 MessageParcel reply;
33 ansManagerStub.HandleGetEnabledForBundleSlot(datas, reply);
34 ansManagerStub.HandleDistributedSetEnabledWithoutApp(datas, reply);
35 ansManagerStub.HandleDistributedGetEnabledWithoutApp(datas, reply);
36 std::string stringData(data);
37 sptr<Notification::NotificationRequest> notification = new Notification::NotificationRequest();
38 ansManagerStub.Publish(stringData, notification);
39 ansManagerStub.PublishToDevice(notification, stringData);
40 int notificationId = 1;
41 ansManagerStub.Cancel(notificationId, stringData);
42 ansManagerStub.CancelAll();
43 int32_t notificationIds = static_cast<int32_t>(GetU32Data(data));
44 int32_t userId = static_cast<int32_t>(GetU32Data(data));
45 ansManagerStub.CancelAsBundle(notificationIds, stringData, userId);
46 uint8_t type = *data % SLOT_TYPE_NUM;
47 Notification::NotificationConstant::SlotType slotType = Notification::NotificationConstant::SlotType(type);
48 ansManagerStub.AddSlotByType(slotType);
49 sptr<Notification::NotificationSlot> slot = new Notification::NotificationSlot();
50 std::vector<sptr<Notification::NotificationSlot>> slots;
51 slots.emplace_back(slot);
52 ansManagerStub.AddSlots(slots);
53 ansManagerStub.RemoveSlotByType(slotType);
54 ansManagerStub.RemoveAllSlots();
55 ansManagerStub.GetSlotByType(slotType, slot);
56 ansManagerStub.GetSlots(slots);
57 sptr<Notification::NotificationBundleOption> bundleOption = new Notification::NotificationBundleOption();
58 uint64_t num = 1;
59 ansManagerStub.GetSlotNumAsBundle(bundleOption, num);
60 sptr<Notification::NotificationRequest> notificationer = new Notification::NotificationRequest();
61 std::vector<sptr<Notification::NotificationRequest>> notifications;
62 notifications.emplace_back(notificationer);
63 ansManagerStub.GetActiveNotifications(notifications);
64 ansManagerStub.GetActiveNotificationNums(num);
65 sptr<Notification::Notification> notificatione = new Notification::Notification();
66 std::vector<sptr<Notification::Notification>> notificationes;
67 notificationes.emplace_back(notificatione);
68 ansManagerStub.GetAllActiveNotifications(notificationes);
69 std::vector<std::string> key;
70 key.emplace_back(stringData);
71 ansManagerStub.GetSpecialActiveNotifications(key, notificationes);
72 ansManagerStub.SetNotificationAgent(stringData);
73 ansManagerStub.GetNotificationAgent(stringData);
74 bool canPublish = *data % ENABLE;
75 ansManagerStub.CanPublishAsBundle(stringData, canPublish);
76 ansManagerStub.PublishAsBundle(notificationer, stringData);
77 ansManagerStub.SetNotificationBadgeNum(notificationId);
78 ansManagerStub.GetBundleImportance(notificationId);
79 ansManagerStub.HasNotificationPolicyAccessPermission(canPublish);
80 return ansManagerStub.SetPrivateNotificationsAllowed(canPublish);
81 }
82 }
83
84 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)85 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
86 {
87 /* Run your code on data */
88 char *ch = ParseData(data, size);
89 if (ch != nullptr && size >= GetU32Size()) {
90 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
91 free(ch);
92 ch = nullptr;
93 }
94 return 0;
95 }
96