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