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