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
33using namespace OHOS::Telephony;
34namespace OHOS {
35constexpr int32_t CALL_STATE_NUM = 8;
36constexpr int32_t BOOL_NUM = 2;
37
38void 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
62void 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
85void 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
106void 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
130void 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
152void 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    if (memcpy_s(contactInfo.ringtonePath, FILE_PATH_MAX_LEN, message.c_str(), length) != EOK) {
169        return;
170    }
171
172    callDataBaseHelper->UnRegisterObserver();
173    callDataBaseHelper->Insert(values);
174    callDataBaseHelper->Query(contactInfo, predicates);
175    callDataBaseHelper->Delete(predicates);
176}
177
178void MissedCallNotificationFunc(const uint8_t *data, size_t size)
179{
180    if (!IsServiceInited()) {
181        return;
182    }
183
184    std::shared_ptr<MissedCallNotification> notification = std::make_shared<MissedCallNotification>();
185    DialParaInfo paraInfo;
186    sptr<CallBase> callObjectPtr = std::make_unique<CSCall>(paraInfo).release();
187    TelCallState priorState = static_cast<TelCallState>(size % CALL_STATE_NUM);
188    TelCallState nextState = static_cast<TelCallState>(size % CALL_STATE_NUM);
189    std::string message(reinterpret_cast<const char *>(data), size);
190    DisconnectedDetails details;
191
192    notification->NewCallCreated(callObjectPtr);
193    notification->PublishMissedCallEvent(callObjectPtr);
194    notification->PublishMissedCallNotification(callObjectPtr);
195    notification->CancelMissedCallsNotification(static_cast<int32_t>(size));
196    notification->CallDestroyed(details);
197    notification->IncomingCallActivated(callObjectPtr);
198    notification->IncomingCallHungUp(callObjectPtr, true, message);
199    notification->CallStateUpdated(callObjectPtr, priorState, nextState);
200}
201
202void RejectCallSmsFunc(const uint8_t *data, size_t size)
203{
204    if (!IsServiceInited()) {
205        return;
206    }
207
208    std::shared_ptr<RejectCallSms> notification = std::make_shared<RejectCallSms>();
209    DialParaInfo paraInfo;
210    int32_t slotId = static_cast<int32_t>(size % 2);
211    sptr<CallBase> callObjectPtr = std::make_unique<CSCall>(paraInfo).release();
212    TelCallState priorState = static_cast<TelCallState>(size % CALL_STATE_NUM);
213    TelCallState nextState = static_cast<TelCallState>(size % CALL_STATE_NUM);
214    std::string message(reinterpret_cast<const char *>(data), size);
215    std::string desAddr(reinterpret_cast<const char *>(data), size);
216    std::u16string desAddrU16 = Str8ToStr16(desAddr);
217    std::u16string messageU16 = Str8ToStr16(message);
218    DisconnectedDetails details;
219
220    notification->NewCallCreated(callObjectPtr);
221    notification->SendMessage(slotId, desAddrU16, messageU16);
222    notification->CallDestroyed(details);
223    notification->IncomingCallActivated(callObjectPtr);
224    notification->IncomingCallHungUp(callObjectPtr, true, message);
225    notification->CallStateUpdated(callObjectPtr, priorState, nextState);
226}
227
228void DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
229{
230    if (data == nullptr || size == 0) {
231        return;
232    }
233
234    IncomingCallNotificationFunc(data, size);
235    IncomingCallWakeupFunc(data, size);
236    ProximitySensorFunc(data, size);
237    StatusBarFunc(data, size);
238    WiredHeadsetHandlerFunc(data, size);
239    CallDataRdbObserverFunc(data, size);
240    MissedCallNotificationFunc(data, size);
241    RejectCallSmsFunc(data, size);
242}
243} // namespace OHOS
244
245/* Fuzzer entry point */
246extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
247{
248    OHOS::AddCallTokenFuzzer token;
249    /* Run your code on data */
250    OHOS::DoSomethingInterestingWithMyAPI(data, size);
251    return 0;
252}
253