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 "audiodevice_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #define private public
21 #include "addcalltoken_fuzzer.h"
22 #include "audio_control_manager.h"
23 #include "call_manager_service.h"
24 #include "cs_call.h"
25 #include "system_ability_definition.h"
26
27 using namespace OHOS::Telephony;
28 namespace OHOS {
29 static bool g_isInited = false;
30 constexpr int32_t AUDIO_INTERRUPT_STATE_NUM = 4;
31 constexpr int32_t BOOL_NUM = 2;
32 constexpr int32_t CALL_TYPE_NUM = 4;
33 constexpr int32_t RING_STATE_NUM = 2;
34 constexpr int32_t DIAL_TYPE = 3;
35 constexpr int32_t VIDIO_TYPE_NUM = 2;
36 constexpr int32_t TEL_CALL_STATE_NUM = 9;
37 constexpr int32_t AUDIO_DEVICE_NUM = 6;
38 constexpr int32_t AUDIO_EVENT = 30;
39 constexpr int32_t AUDIO_SCENE_NUM = 4;
40 constexpr int32_t AUDIO_VOLUME_TYPE_NUM = 13;
41
IsServiceInited()42 bool IsServiceInited()
43 {
44 if (!g_isInited) {
45 DelayedSingleton<CallManagerService>::GetInstance()->OnStart();
46 if (DelayedSingleton<CallManagerService>::GetInstance()->GetServiceRunningState() ==
47 static_cast<int32_t>(CallManagerService::ServiceRunningState::STATE_RUNNING)) {
48 g_isInited = true;
49 }
50 }
51 return g_isInited;
52 }
53
AudioControlManagerFunc(const uint8_t * data,size_t size)54 void AudioControlManagerFunc(const uint8_t *data, size_t size)
55 {
56 if (!IsServiceInited()) {
57 return;
58 }
59
60 std::shared_ptr<AudioControlManager> audioControlManager = DelayedSingleton<AudioControlManager>::GetInstance();
61 DialParaInfo paraInfo;
62 paraInfo.dialType = static_cast<DialType>(size % DIAL_TYPE);
63 paraInfo.callType = static_cast<CallType>(size % CALL_TYPE_NUM);
64 paraInfo.videoState = static_cast<VideoStateType>(size % VIDIO_TYPE_NUM);
65 paraInfo.callState = static_cast<TelCallState>(size % TEL_CALL_STATE_NUM);
66 sptr<CallBase> callObjectPtr = std::make_unique<CSCall>(paraInfo).release();
67 TelCallState priorState = static_cast<TelCallState>(size % TEL_CALL_STATE_NUM);
68 TelCallState nextState = static_cast<TelCallState>(size % TEL_CALL_STATE_NUM);
69 std::string message(reinterpret_cast<const char *>(data), size);
70 AudioInterruptState state = static_cast<AudioInterruptState>(size % AUDIO_INTERRUPT_STATE_NUM);
71 DisconnectedDetails details;
72 bool isMute = static_cast<bool>(size % BOOL_NUM);
73 RingState ringState = static_cast<RingState>(size % RING_STATE_NUM);
74 AudioDevice device = static_cast<AudioDevice>(size % AUDIO_DEVICE_NUM);
75
76 audioControlManager->SetAudioInterruptState(state);
77 audioControlManager->SetAudioDevice(device);
78 audioControlManager->CallDestroyed(details);
79 audioControlManager->NewCallCreated(callObjectPtr);
80 audioControlManager->IncomingCallActivated(callObjectPtr);
81 audioControlManager->IncomingCallHungUp(callObjectPtr, true, message);
82 audioControlManager->CallStateUpdated(callObjectPtr, priorState, nextState);
83 audioControlManager->HandleCallStateUpdated(callObjectPtr, priorState, nextState);
84 audioControlManager->HandleNextState(callObjectPtr, nextState);
85 audioControlManager->HandleNextState(callObjectPtr, priorState);
86 audioControlManager->HandleNewActiveCall(callObjectPtr);
87 audioControlManager->StopRingtone();
88 audioControlManager->GetInitAudioDevice();
89 audioControlManager->SetMute(isMute);
90 audioControlManager->MuteRinger();
91 audioControlManager->GetCallList();
92 audioControlManager->GetCurrentActiveCall();
93 audioControlManager->IsEmergencyCallExists();
94 audioControlManager->GetAudioInterruptState();
95 audioControlManager->SetVolumeAudible();
96 audioControlManager->SetRingState(ringState);
97 audioControlManager->SetLocalRingbackNeeded(isMute);
98 audioControlManager->IsAudioActivated();
99 audioControlManager->StopCallTone();
100 audioControlManager->IsCurrentRinging();
101 audioControlManager->StopRingback();
102 audioControlManager->StopWaitingTone();
103 }
104
AudioDeviceManagerFunc(const uint8_t * data,size_t size)105 void AudioDeviceManagerFunc(const uint8_t *data, size_t size)
106 {
107 if (!IsServiceInited()) {
108 return;
109 }
110
111 std::shared_ptr<AudioDeviceManager> audioDeviceManager = DelayedSingleton<AudioDeviceManager>::GetInstance();
112 AudioDevice device = static_cast<AudioDevice>(size % AUDIO_DEVICE_NUM);
113 AudioEvent event = static_cast<AudioEvent>(size % AUDIO_EVENT);
114 std::string bluetoothAddress(reinterpret_cast<const char *>(data), size);
115 bool available = static_cast<bool>(size % BOOL_NUM);
116
117 audioDeviceManager->Init();
118 audioDeviceManager->InitAudioDevice();
119 audioDeviceManager->ProcessEvent(event);
120 audioDeviceManager->SwitchDevice(event);
121 audioDeviceManager->ConnectBtScoWithAddress(bluetoothAddress);
122 audioDeviceManager->SwitchDevice(device);
123 audioDeviceManager->EnableBtSco();
124 audioDeviceManager->EnableWiredHeadset();
125 audioDeviceManager->EnableSpeaker();
126 audioDeviceManager->EnableEarpiece();
127 audioDeviceManager->DisableAll();
128 audioDeviceManager->IsBtScoDevEnable();
129 audioDeviceManager->IsSpeakerDevEnable();
130 audioDeviceManager->IsEarpieceDevEnable();
131 audioDeviceManager->IsWiredHeadsetDevEnable();
132 audioDeviceManager->SetBtScoDevEnable();
133 audioDeviceManager->SetSpeakerDevEnable();
134 audioDeviceManager->SetEarpieceDevEnable();
135 audioDeviceManager->SetWiredHeadsetDevEnable();
136 audioDeviceManager->GetCurrentAudioDevice();
137 audioDeviceManager->SetCurrentAudioDevice(device);
138 audioDeviceManager->IsEarpieceAvailable();
139 audioDeviceManager->IsSpeakerAvailable();
140 audioDeviceManager->IsBtScoConnected();
141 audioDeviceManager->IsWiredHeadsetConnected();
142 audioDeviceManager->SetSpeakerAvailable(available);
143 audioDeviceManager->SetWiredHeadsetAvailable(available);
144 audioDeviceManager->SetBtScoAvailable(available);
145 }
146
AudioProxyFunc(const uint8_t * data,size_t size)147 void AudioProxyFunc(const uint8_t *data, size_t size)
148 {
149 if (!IsServiceInited()) {
150 return;
151 }
152
153 std::shared_ptr<AudioProxy> audioProxy = DelayedSingleton<AudioProxy>::GetInstance();
154 std::shared_ptr<AudioControlManager> audioControlManager = DelayedSingleton<AudioControlManager>::GetInstance();
155 AudioStandard::AudioScene audioScene = static_cast<AudioStandard::AudioScene>(size % AUDIO_SCENE_NUM);
156 AudioStandard::AudioVolumeType audioVolumeType =
157 static_cast<AudioStandard::AudioVolumeType>(size % AUDIO_VOLUME_TYPE_NUM);
158 std::string phoneNum(reinterpret_cast<const char *>(data), size);
159 int32_t volume = static_cast<int32_t>(size);
160 bool isMute = static_cast<bool>(size % BOOL_NUM);
161
162 audioControlManager->GetCallBase(phoneNum);
163 audioProxy->SetAudioScene(audioScene);
164 audioProxy->SetVolumeAudible();
165 audioProxy->IsMicrophoneMute();
166 audioProxy->SetMicrophoneMute(isMute);
167 audioProxy->SetEarpieceDevActive();
168 audioProxy->SetSpeakerDevActive();
169 audioProxy->SetBluetoothDevActive();
170 audioProxy->SetWiredHeadsetDevActive();
171 audioProxy->GetRingerMode();
172 audioProxy->GetVolume(audioVolumeType);
173 audioProxy->SetVolume(audioVolumeType, volume);
174 audioProxy->SetMaxVolume(audioVolumeType);
175 audioProxy->IsStreamActive(audioVolumeType);
176 audioProxy->IsStreamMute(audioVolumeType);
177 audioProxy->GetMaxVolume(audioVolumeType);
178 audioProxy->GetMinVolume(audioVolumeType);
179 audioProxy->SetAudioDeviceChangeCallback();
180 audioProxy->IsVibrateMode();
181 audioProxy->StartVibrate();
182 audioProxy->CancelVibrate();
183 audioProxy->GetSystemRingtoneUri();
184 audioProxy->GetDefaultRingPath();
185 audioProxy->GetDefaultTonePath();
186 audioProxy->GetDefaultDtmfPath();
187 }
188
AudioSceneProcessorFunc(const uint8_t * data,size_t size)189 void AudioSceneProcessorFunc(const uint8_t *data, size_t size)
190 {
191 if (!IsServiceInited()) {
192 return;
193 }
194
195 std::shared_ptr<AudioSceneProcessor> audioSceneProcessor = DelayedSingleton<AudioSceneProcessor>::GetInstance();
196 std::shared_ptr<AudioControlManager> audioControlManager = DelayedSingleton<AudioControlManager>::GetInstance();
197 std::string phoneNum(reinterpret_cast<const char *>(data), size);
198
199 audioControlManager->IsNumberAllowed(phoneNum);
200 audioSceneProcessor->Init();
201 audioSceneProcessor->SwitchCS();
202 audioSceneProcessor->SwitchIMS();
203 audioSceneProcessor->SwitchOTT();
204 audioSceneProcessor->SwitchHolding();
205 audioSceneProcessor->SwitchInactive();
206 }
207
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)208 void DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
209 {
210 if (data == nullptr || size == 0) {
211 return;
212 }
213
214 AudioControlManagerFunc(data, size);
215 AudioDeviceManagerFunc(data, size);
216 AudioProxyFunc(data, size);
217 AudioSceneProcessorFunc(data, size);
218 }
219 } // namespace OHOS
220
221 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)222 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
223 {
224 OHOS::AddCallTokenFuzzer token;
225 /* Run your code on data */
226 OHOS::DoSomethingInterestingWithMyAPI(data, size);
227 return 0;
228 }
229