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_subscriber_stub.h"
19 #include "ans_subscriber_local_live_view_stub.h"
20 #undef private
21 #undef protected
22 #include "ans_permission_def.h"
23 #include "anssubscriberstub_fuzzer.h"
24 #include "notification_request.h"
25 
26 namespace OHOS {
DoSomethingInterestingWithMyAPI(const char * data,size_t size)27     bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
28     {
29         Notification::AnsSubscriberStub ansSubscriberStub;
30         Notification::AnsSubscriberLocalLiveViewStub ansSubscriberLocalLiveViewStub;
31         uint32_t code = GetU32Data(data);
32         MessageParcel datas;
33         MessageParcel reply;
34         MessageOption flags;
35         // test OnRemoteRequest function
36         ansSubscriberStub.OnRemoteRequest(code, datas, reply, flags);
37         // test HandleOnConnected function
38         ansSubscriberStub.HandleOnConnected(datas, reply);
39         // test HandleOnDisconnected function
40         ansSubscriberStub.HandleOnDisconnected(datas, reply);
41         // test HandleOnConsumedMap function
42         ansSubscriberStub.HandleOnConsumedMap(datas, reply);
43         // test HandleOnCanceledMap function
44         ansSubscriberStub.HandleOnCanceledMap(datas, reply);
45         // test HandleOnUpdated function
46         ansSubscriberStub.HandleOnUpdated(datas, reply);
47         // test HandleOnDoNotDisturbDateChange function
48         ansSubscriberStub.HandleOnDoNotDisturbDateChange(datas, reply);
49         // test HandleOnEnabledNotificationChanged function
50         ansSubscriberStub.HandleOnEnabledNotificationChanged(datas, reply);
51         // test OnConnected function
52         ansSubscriberStub.OnConnected();
53         // test OnDisconnected function
54         ansSubscriberStub.OnDisconnected();
55         // test OnConsumed function
56         sptr<Notification::Notification> notification = new Notification::Notification();
57         sptr<Notification::NotificationSortingMap> notificationMap = new Notification::NotificationSortingMap();
58         ansSubscriberStub.OnConsumed(notification, notificationMap);
59         // test OnCanceled function
60         int32_t deleteReason = 1;
61         ansSubscriberStub.OnCanceled(notification, notificationMap, deleteReason);
62         // test OnUpdated function
63         ansSubscriberStub.OnUpdated(notificationMap);
64         // test OnDoNotDisturbDateChange function
65         sptr<Notification::NotificationDoNotDisturbDate> date = new Notification::NotificationDoNotDisturbDate();
66         ansSubscriberStub.OnDoNotDisturbDateChange(date);
67         // test OnEnabledNotificationChanged function
68         sptr<Notification::EnabledNotificationCallbackData> callbackData = new Notification::EnabledNotificationCallbackData();
69         // test HandleOnBadgeEnabledChanged function
70         ansSubscriberStub.HandleOnBadgeEnabledChanged(datas, reply);
71         // test HandleOnResponse function
72         ansSubscriberLocalLiveViewStub.HandleOnResponse(datas, reply);
73         // test HandleOnConsumedListMap function
74         ansSubscriberStub.HandleOnConsumedListMap(datas, reply);
75         // test HandleOnDisconnected function
76         ansSubscriberLocalLiveViewStub.HandleOnDisconnected(datas, reply);
77         // test HandleOnBadgeChanged function
78         ansSubscriberStub.HandleOnBadgeChanged(datas, reply);
79         // test HandleOnCanceledListMap function
80         ansSubscriberStub.HandleOnCanceledListMap(datas, reply);
81         // test HandleOnConnected function
82         ansSubscriberLocalLiveViewStub.HandleOnConnected(datas, reply);
83         return true;
84     }
85 }
86 
87 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)88 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
89 {
90     /* Run your code on data */
91     char *ch = ParseData(data, size);
92     if (ch != nullptr && size >= GetU32Size()) {
93         std::vector<std::string> requestPermission = {
94             OHOS::Notification::OHOS_PERMISSION_NOTIFICATION_CONTROLLER,
95             OHOS::Notification::OHOS_PERMISSION_NOTIFICATION_AGENT_CONTROLLER,
96             OHOS::Notification::OHOS_PERMISSION_SET_UNREMOVABLE_NOTIFICATION
97         };
98         SystemHapTokenGet(requestPermission);
99         OHOS::DoSomethingInterestingWithMyAPI(ch, size);
100         free(ch);
101         ch = nullptr;
102     }
103     return 0;
104 }
105