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 #include "callnotification_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <securec.h>
21 #define private public
22 #include "addcalltoken_fuzzer.h"
23 #include "call_data_base_helper.h"
24 #include "cs_call.h"
25 #include "incoming_call_notification.h"
26 #include "incoming_call_wake_up.h"
27 #include "missed_call_notification.h"
28 #include "proximity_sensor.h"
29 #include "reject_call_sms.h"
30 #include "status_bar.h"
31 #include "wired_headset.h"
32 
33 using namespace OHOS::Telephony;
34 namespace OHOS {
35 constexpr int32_t CALL_STATE_NUM = 8;
36 constexpr int32_t BOOL_NUM = 2;
37 
IncomingCallNotificationFunc(const uint8_t * data,size_t size)38 void IncomingCallNotificationFunc(const uint8_t *data, size_t size)
39 {
40     if (!IsServiceInited()) {
41         return;
42     }
43 
44     std::shared_ptr<IncomingCallNotification> notification = std::make_shared<IncomingCallNotification>();
45     DialParaInfo paraInfo;
46     sptr<CallBase> callObjectPtr = std::make_unique<CSCall>(paraInfo).release();
47     TelCallState priorState = static_cast<TelCallState>(size % CALL_STATE_NUM);
48     TelCallState nextState = static_cast<TelCallState>(size % CALL_STATE_NUM);
49     std::string message(reinterpret_cast<const char *>(data), size);
50     DisconnectedDetails details;
51 
52     notification->NewCallCreated(callObjectPtr);
53     notification->CallStateUpdated(callObjectPtr, priorState, nextState);
54     notification->IncomingCallActivated(callObjectPtr);
55     notification->IncomingCallHungUp(callObjectPtr, true, message);
56     notification->CallDestroyed(details);
57     notification->PublishIncomingCallNotification(callObjectPtr);
58     notification->CancelIncomingCallNotification();
59     notification->IsFullScreen();
60 }
61 
IncomingCallWakeupFunc(const uint8_t * data,size_t size)62 void IncomingCallWakeupFunc(const uint8_t *data, size_t size)
63 {
64     if (!IsServiceInited()) {
65         return;
66     }
67 
68     std::shared_ptr<IncomingCallWakeup> notification = std::make_shared<IncomingCallWakeup>();
69     DialParaInfo paraInfo;
70     sptr<CallBase> callObjectPtr = std::make_unique<CSCall>(paraInfo).release();
71     TelCallState priorState = static_cast<TelCallState>(size % CALL_STATE_NUM);
72     TelCallState nextState = static_cast<TelCallState>(size % CALL_STATE_NUM);
73     std::string message(reinterpret_cast<const char *>(data), size);
74     DisconnectedDetails details;
75 
76     notification->NewCallCreated(callObjectPtr);
77     notification->WakeupDevice(callObjectPtr);
78     notification->IsPowerAbilityExist();
79     notification->CallDestroyed(details);
80     notification->IncomingCallActivated(callObjectPtr);
81     notification->IncomingCallHungUp(callObjectPtr, true, message);
82     notification->CallStateUpdated(callObjectPtr, priorState, nextState);
83 }
84 
ProximitySensorFunc(const uint8_t * data,size_t size)85 void ProximitySensorFunc(const uint8_t *data, size_t size)
86 {
87     if (!IsServiceInited()) {
88         return;
89     }
90 
91     std::shared_ptr<ProximitySensor> notification = std::make_shared<ProximitySensor>();
92     DialParaInfo paraInfo;
93     sptr<CallBase> callObjectPtr = std::make_unique<CSCall>(paraInfo).release();
94     TelCallState priorState = static_cast<TelCallState>(size % CALL_STATE_NUM);
95     TelCallState nextState = static_cast<TelCallState>(size % CALL_STATE_NUM);
96     std::string message(reinterpret_cast<const char *>(data), size);
97     DisconnectedDetails details;
98 
99     notification->CallDestroyed(details);
100     notification->NewCallCreated(callObjectPtr);
101     notification->IncomingCallActivated(callObjectPtr);
102     notification->IncomingCallHungUp(callObjectPtr, true, message);
103     notification->CallStateUpdated(callObjectPtr, priorState, nextState);
104 }
105 
StatusBarFunc(const uint8_t * data,size_t size)106 void StatusBarFunc(const uint8_t *data, size_t size)
107 {
108     if (!IsServiceInited()) {
109         return;
110     }
111 
112     std::shared_ptr<StatusBar> notification = std::make_shared<StatusBar>();
113     DialParaInfo paraInfo;
114     sptr<CallBase> callObjectPtr = std::make_unique<CSCall>(paraInfo).release();
115     TelCallState priorState = static_cast<TelCallState>(size % CALL_STATE_NUM);
116     TelCallState nextState = static_cast<TelCallState>(size % CALL_STATE_NUM);
117     std::string message(reinterpret_cast<const char *>(data), size);
118     DisconnectedDetails details;
119     int32_t isDisplayMute = static_cast<int32_t>(size % BOOL_NUM);
120 
121     notification->UpdateMuteIcon(isDisplayMute);
122     notification->UpdateSpeakerphoneIcon(isDisplayMute);
123     notification->NewCallCreated(callObjectPtr);
124     notification->CallDestroyed(details);
125     notification->IncomingCallActivated(callObjectPtr);
126     notification->IncomingCallHungUp(callObjectPtr, true, message);
127     notification->CallStateUpdated(callObjectPtr, priorState, nextState);
128 }
129 
WiredHeadsetHandlerFunc(const uint8_t * data,size_t size)130 void WiredHeadsetHandlerFunc(const uint8_t *data, size_t size)
131 {
132     if (!IsServiceInited()) {
133         return;
134     }
135 
136     std::shared_ptr<WiredHeadset> notification = std::make_shared<WiredHeadset>();
137     DialParaInfo paraInfo;
138     sptr<CallBase> callObjectPtr = std::make_unique<CSCall>(paraInfo).release();
139     TelCallState priorState = static_cast<TelCallState>(size % CALL_STATE_NUM);
140     TelCallState nextState = static_cast<TelCallState>(size % CALL_STATE_NUM);
141     std::string message(reinterpret_cast<const char *>(data), size);
142     DisconnectedDetails details;
143 
144     notification->Init();
145     notification->NewCallCreated(callObjectPtr);
146     notification->CallDestroyed(details);
147     notification->IncomingCallActivated(callObjectPtr);
148     notification->IncomingCallHungUp(callObjectPtr, true, message);
149     notification->CallStateUpdated(callObjectPtr, priorState, nextState);
150 }
151 
CallDataRdbObserverFunc(const uint8_t * data,size_t size)152 void CallDataRdbObserverFunc(const uint8_t *data, size_t size)
153 {
154     if (!IsServiceInited()) {
155         return;
156     }
157 
158     std::string message(reinterpret_cast<const char *>(data), size);
159     std::shared_ptr<CallDataBaseHelper> callDataBaseHelper = DelayedSingleton<CallDataBaseHelper>::GetInstance();
160     DataShare::DataShareValuesBucket values;
161     DataShare::DataSharePredicates predicates;
162     DialParaInfo paraInfo;
163     sptr<CallBase> callObjectPtr = std::make_unique<CSCall>(paraInfo).release();
164     ContactInfo contactInfo;
165     contactInfo.name = message;
166     contactInfo.number = message;
167     int32_t length = message.length() > FILE_PATH_MAX_LEN ? FILE_PATH_MAX_LEN : message.length();
168     length = message.length() > FILE_PATH_MAX_LEN ? FILE_PATH_MAX_LEN : message.length();
169     if (memcpy_s(contactInfo.ringtonePath, FILE_PATH_MAX_LEN, message.c_str(), length) != EOK) {
170         return;
171     }
172 
173     callDataBaseHelper->UnRegisterObserver();
174     callDataBaseHelper->Insert(values);
175     callDataBaseHelper->Query(contactInfo, predicates);
176     callDataBaseHelper->Delete(predicates);
177 }
178 
MissedCallNotificationFunc(const uint8_t * data,size_t size)179 void MissedCallNotificationFunc(const uint8_t *data, size_t size)
180 {
181     if (!IsServiceInited()) {
182         return;
183     }
184 
185     std::shared_ptr<MissedCallNotification> notification = std::make_shared<MissedCallNotification>();
186     DialParaInfo paraInfo;
187     sptr<CallBase> callObjectPtr = std::make_unique<CSCall>(paraInfo).release();
188     TelCallState priorState = static_cast<TelCallState>(size % CALL_STATE_NUM);
189     TelCallState nextState = static_cast<TelCallState>(size % CALL_STATE_NUM);
190     std::string message(reinterpret_cast<const char *>(data), size);
191     DisconnectedDetails details;
192 
193     notification->NewCallCreated(callObjectPtr);
194     notification->PublishMissedCallEvent(callObjectPtr);
195     notification->PublishMissedCallNotification(callObjectPtr);
196     notification->CancelMissedCallsNotification(static_cast<int32_t>(size));
197     notification->CallDestroyed(details);
198     notification->IncomingCallActivated(callObjectPtr);
199     notification->IncomingCallHungUp(callObjectPtr, true, message);
200     notification->CallStateUpdated(callObjectPtr, priorState, nextState);
201 }
202 
RejectCallSmsFunc(const uint8_t * data,size_t size)203 void RejectCallSmsFunc(const uint8_t *data, size_t size)
204 {
205     if (!IsServiceInited()) {
206         return;
207     }
208 
209     std::shared_ptr<RejectCallSms> notification = std::make_shared<RejectCallSms>();
210     DialParaInfo paraInfo;
211     int32_t slotId = static_cast<int32_t>(size % 2);
212     sptr<CallBase> callObjectPtr = std::make_unique<CSCall>(paraInfo).release();
213     TelCallState priorState = static_cast<TelCallState>(size % CALL_STATE_NUM);
214     TelCallState nextState = static_cast<TelCallState>(size % CALL_STATE_NUM);
215     std::string message(reinterpret_cast<const char *>(data), size);
216     std::string desAddr(reinterpret_cast<const char *>(data), size);
217     std::u16string desAddrU16 = Str8ToStr16(desAddr);
218     std::u16string messageU16 = Str8ToStr16(message);
219     DisconnectedDetails details;
220 
221     notification->NewCallCreated(callObjectPtr);
222     notification->SendMessage(slotId, desAddrU16, messageU16);
223     notification->CallDestroyed(details);
224     notification->IncomingCallActivated(callObjectPtr);
225     notification->IncomingCallHungUp(callObjectPtr, true, message);
226     notification->CallStateUpdated(callObjectPtr, priorState, nextState);
227 }
228 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)229 void DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
230 {
231     if (data == nullptr || size == 0) {
232         return;
233     }
234 
235     IncomingCallNotificationFunc(data, size);
236     IncomingCallWakeupFunc(data, size);
237     ProximitySensorFunc(data, size);
238     StatusBarFunc(data, size);
239     WiredHeadsetHandlerFunc(data, size);
240     CallDataRdbObserverFunc(data, size);
241     MissedCallNotificationFunc(data, size);
242     RejectCallSmsFunc(data, size);
243 }
244 } // namespace OHOS
245 
246 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)247 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
248 {
249     OHOS::AddCallTokenFuzzer token;
250     /* Run your code on data */
251     OHOS::DoSomethingInterestingWithMyAPI(data, size);
252     return 0;
253 }
254