1 /*
2 * Copyright (c) 2025 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 #include "service_unsubscribe_fuzzer.h"
16
17 #include <fuzzer/FuzzedDataProvider.h>
18 #include "advanced_notification_service.h"
19 #include "ans_permission_def.h"
20 #include "mock_notification_request.h"
21 #include "mock_notification_subscribe_info.h"
22 #include "notification_subscriber.h"
23 #include "ans_subscriber_listener.h"
24
25 namespace OHOS {
26 namespace Notification {
27
28 class TestSubscriber : public NotificationSubscriber {
29 public:
OnDisconnected()30 void OnDisconnected() override
31 {}
OnDied()32 void OnDied() override
33 {}
OnUpdate(const std::shared_ptr<NotificationSortingMap> & sortingMap)34 void OnUpdate(const std::shared_ptr<NotificationSortingMap> &sortingMap) override
35 {}
OnDoNotDisturbDateChange(const std::shared_ptr<NotificationDoNotDisturbDate> & date)36 void OnDoNotDisturbDateChange(const std::shared_ptr<NotificationDoNotDisturbDate> &date) override
37 {}
OnConnected()38 void OnConnected() override
39 {}
OnEnabledNotificationChanged(const std::shared_ptr<EnabledNotificationCallbackData> & callbackData)40 void OnEnabledNotificationChanged(const std::shared_ptr<EnabledNotificationCallbackData> &callbackData) override
41 {}
OnCanceled(const std::shared_ptr<Notification> & request,const std::shared_ptr<NotificationSortingMap> & sortingMap,int deleteReason)42 void OnCanceled(const std::shared_ptr<Notification> &request,
43 const std::shared_ptr<NotificationSortingMap> &sortingMap, int deleteReason) override
44 {}
OnBadgeChanged(const std::shared_ptr<BadgeNumberCallbackData> & badgeData)45 void OnBadgeChanged(const std::shared_ptr<BadgeNumberCallbackData> &badgeData) override
46 {}
OnBadgeEnabledChanged(const sptr<EnabledNotificationCallbackData> & callbackData)47 void OnBadgeEnabledChanged(const sptr<EnabledNotificationCallbackData> &callbackData) override
48 {}
OnConsumed(const std::shared_ptr<Notification> & request,const std::shared_ptr<NotificationSortingMap> & sortingMap)49 void OnConsumed(const std::shared_ptr<Notification> &request,
50 const std::shared_ptr<NotificationSortingMap> &sortingMap) override
51 {}
52
OnBatchCanceled(const std::vector<std::shared_ptr<Notification>> & requestList,const std::shared_ptr<NotificationSortingMap> & sortingMap,int32_t deleteReason)53 void OnBatchCanceled(const std::vector<std::shared_ptr<Notification>> &requestList,
54 const std::shared_ptr<NotificationSortingMap> &sortingMap, int32_t deleteReason) override
55 {}
56
HasOnBatchCancelCallback()57 bool HasOnBatchCancelCallback() override
58 {
59 return true;
60 }
61 };
62
DoSomethingInterestingWithMyAPI(FuzzedDataProvider * fuzzData)63 bool DoSomethingInterestingWithMyAPI(FuzzedDataProvider *fuzzData)
64 {
65 auto service = AdvancedNotificationService::GetInstance();
66 std::shared_ptr<NotificationSubscriber> subscriber = std::make_shared<TestSubscriber>();
67 sptr<IAnsSubscriber> listener = new (std::nothrow) SubscriberListener(subscriber);
68
69 sptr<NotificationSubscribeInfo> subscribeInfo = ObjectBuilder<NotificationSubscribeInfo>::Build(fuzzData);
70 service->Unsubscribe(listener, subscribeInfo);
71 return true;
72 }
73 }
74 }
75
76 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)77 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
78 {
79 /* Run your code on data */
80 FuzzedDataProvider fdp(data, size);
81 std::vector<std::string> requestPermission = {
82 OHOS::Notification::OHOS_PERMISSION_NOTIFICATION_CONTROLLER,
83 OHOS::Notification::OHOS_PERMISSION_NOTIFICATION_AGENT_CONTROLLER,
84 OHOS::Notification::OHOS_PERMISSION_SET_UNREMOVABLE_NOTIFICATION
85 };
86 MockRandomToken(&fdp, requestPermission);
87 OHOS::Notification::DoSomethingInterestingWithMyAPI(&fdp);
88 return 0;
89 }
90