• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2024-2025 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 "audio_log.h"
17 #include "audio_server_proxy.h"
18 using namespace std;
19 
20 namespace OHOS {
21 namespace AudioStandard {
22 
23 static const uint8_t* RAW_DATA = nullptr;
24 static size_t g_dataSize = 0;
25 static size_t g_pos;
26 const size_t THRESHOLD = 10;
27 typedef void (*TestPtr)();
28 
29 const vector<DeviceType> g_testDeviceTypes = {
30     DEVICE_TYPE_NONE,
31     DEVICE_TYPE_INVALID,
32     DEVICE_TYPE_EARPIECE,
33     DEVICE_TYPE_SPEAKER,
34     DEVICE_TYPE_WIRED_HEADSET,
35     DEVICE_TYPE_WIRED_HEADPHONES,
36     DEVICE_TYPE_BLUETOOTH_SCO,
37     DEVICE_TYPE_BLUETOOTH_A2DP,
38     DEVICE_TYPE_BLUETOOTH_A2DP_IN,
39     DEVICE_TYPE_MIC,
40     DEVICE_TYPE_WAKEUP,
41     DEVICE_TYPE_USB_HEADSET,
42     DEVICE_TYPE_DP,
43     DEVICE_TYPE_REMOTE_CAST,
44     DEVICE_TYPE_USB_DEVICE,
45     DEVICE_TYPE_ACCESSORY,
46     DEVICE_TYPE_REMOTE_DAUDIO,
47     DEVICE_TYPE_HDMI,
48     DEVICE_TYPE_LINE_DIGITAL,
49     DEVICE_TYPE_NEARLINK,
50     DEVICE_TYPE_NEARLINK_IN,
51     DEVICE_TYPE_FILE_SINK,
52     DEVICE_TYPE_FILE_SOURCE,
53     DEVICE_TYPE_EXTERN_CABLE,
54     DEVICE_TYPE_DEFAULT,
55     DEVICE_TYPE_USB_ARM_HEADSET,
56     DEVICE_TYPE_MAX
57 };
58 
59 template<class T>
GetArrLength(T & arr)60 uint32_t GetArrLength(T& arr)
61 {
62     if (arr == nullptr) {
63         AUDIO_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
64         return 0;
65     }
66     return sizeof(arr) / sizeof(arr[0]);
67 }
68 
69 template<class T>
GetData()70 T GetData()
71 {
72     T object {};
73     size_t objectSize = sizeof(object);
74     if (g_dataSize < g_pos) {
75         return object;
76     }
77     if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
78         return object;
79     }
80     errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
81     if (ret != EOK) {
82         return {};
83     }
84     g_pos += objectSize;
85     return object;
86 }
87 
AudioServerProxyGetEffectOffloadEnabledProxyFuzzTest()88 void AudioServerProxyGetEffectOffloadEnabledProxyFuzzTest()
89 {
90     AudioServerProxy &audioServerProxy = AudioServerProxy::GetInstance();
91     audioServerProxy.GetEffectOffloadEnabledProxy();
92 }
93 
AudioServerProxyUpdateDualToneStateProxyFuzzTest()94 void AudioServerProxyUpdateDualToneStateProxyFuzzTest()
95 {
96     AudioServerProxy &audioServerProxy = AudioServerProxy::GetInstance();
97     bool enable = GetData<bool>();
98     int32_t sessionId = GetData<int32_t>();
99     audioServerProxy.UpdateDualToneStateProxy(enable, sessionId);
100 }
101 
AudioServerProxyUpdateSessionConnectionStateProxyFuzzTest()102 void AudioServerProxyUpdateSessionConnectionStateProxyFuzzTest()
103 {
104     AudioServerProxy &audioServerProxy = AudioServerProxy::GetInstance();
105     int32_t sessionID = GetData<int32_t>();
106     int32_t state = GetData<int32_t>();
107     audioServerProxy.UpdateSessionConnectionStateProxy(sessionID, state);
108 }
109 
AudioServerProxyCheckRemoteDeviceStateProxyFuzzTest()110 void AudioServerProxyCheckRemoteDeviceStateProxyFuzzTest()
111 {
112     static const vector<DeviceRole> testDeviceRoles = {
113         DEVICE_ROLE_NONE,
114         INPUT_DEVICE,
115         OUTPUT_DEVICE,
116         DEVICE_ROLE_MAX,
117     };
118     if (testDeviceRoles.size() == 0) {
119         return;
120     }
121     AudioServerProxy &audioServerProxy = AudioServerProxy::GetInstance();
122     string networkId = "testNetworkId";
123     bool isStartDevice = GetData<bool>();
124     DeviceRole deviceRole = testDeviceRoles[GetData<uint32_t>() % testDeviceRoles.size()];
125     audioServerProxy.CheckRemoteDeviceStateProxy(networkId, deviceRole, isStartDevice);
126 }
127 
AudioServerProxyResetAudioEndpointProxyFuzzTest()128 void AudioServerProxyResetAudioEndpointProxyFuzzTest()
129 {
130     AudioServerProxy &audioServerProxy = AudioServerProxy::GetInstance();
131     audioServerProxy.ResetAudioEndpointProxy();
132 }
133 
AudioServerProxyOffloadSetVolumeProxyFuzzTest()134 void AudioServerProxyOffloadSetVolumeProxyFuzzTest()
135 {
136     AudioServerProxy &audioServerProxy = AudioServerProxy::GetInstance();
137     float volume = GetData<float>();
138     std::string deviceClass = "testDeviceClass";
139     std::string networkId = "testNetworkId";
140     audioServerProxy.OffloadSetVolumeProxy(volume, deviceClass, networkId);
141 }
142 
AudioServerProxyUnsetOffloadModeProxyFuzzTest()143 void AudioServerProxyUnsetOffloadModeProxyFuzzTest()
144 {
145     AudioServerProxy &audioServerProxy = AudioServerProxy::GetInstance();
146     uint32_t sessionId = GetData<uint32_t>();
147     audioServerProxy.UnsetOffloadModeProxy(sessionId);
148 }
149 
AudioServerProxySetOffloadModeProxyFuzzTest()150 void AudioServerProxySetOffloadModeProxyFuzzTest()
151 {
152     AudioServerProxy &audioServerProxy = AudioServerProxy::GetInstance();
153     uint32_t sessionId = GetData<uint32_t>();
154     int32_t state = GetData<int32_t>();
155     bool isAppBack = GetData<bool>();
156     audioServerProxy.SetOffloadModeProxy(sessionId, state, isAppBack);
157 }
158 
AudioServerProxyCheckHibernateStateProxyFuzzTest()159 void AudioServerProxyCheckHibernateStateProxyFuzzTest()
160 {
161     AudioServerProxy &audioServerProxy = AudioServerProxy::GetInstance();
162     bool hibernate = GetData<bool>();
163     audioServerProxy.CheckHibernateStateProxy(hibernate);
164 }
165 
AudioServerProxySetAudioEnhancePropertyProxyFuzzTest()166 void AudioServerProxySetAudioEnhancePropertyProxyFuzzTest()
167 {
168     if (g_testDeviceTypes.size() == 0) {
169         return;
170     }
171     DeviceType deviceType = g_testDeviceTypes[GetData<uint32_t>() % g_testDeviceTypes.size()];
172     AudioEnhancePropertyArray propertyArray;
173     AudioServerProxy &audioServerProxy = AudioServerProxy::GetInstance();
174     audioServerProxy.SetAudioEnhancePropertyProxy(propertyArray, deviceType);
175 }
176 
AudioServerProxySetSinkMuteForSwitchDeviceProxyFuzzTest()177 void AudioServerProxySetSinkMuteForSwitchDeviceProxyFuzzTest()
178 {
179     AudioServerProxy &audioServerProxy = AudioServerProxy::GetInstance();
180     std::string devceClass = "testDeviceClass";
181     int32_t durationUs = GetData<int32_t>();
182     bool mute = GetData<bool>();
183     audioServerProxy.SetSinkMuteForSwitchDeviceProxy(devceClass, durationUs, mute);
184 }
185 
AudioServerProxySuspendRenderSinkProxyFuzzTest()186 void AudioServerProxySuspendRenderSinkProxyFuzzTest()
187 {
188     AudioServerProxy &audioServerProxy = AudioServerProxy::GetInstance();
189     std::string sinkName = "testSinkName";
190     audioServerProxy.SuspendRenderSinkProxy(sinkName);
191 }
192 
AudioServerProxyNotifyDeviceInfoProxyFuzzTest()193 void AudioServerProxyNotifyDeviceInfoProxyFuzzTest()
194 {
195     AudioServerProxy &audioServerProxy = AudioServerProxy::GetInstance();
196     std::string networkId = "testNetworkId";
197     bool connected = GetData<bool>();
198     audioServerProxy.NotifyDeviceInfoProxy(networkId, connected);
199 }
200 
AudioServerProxyCreatePlaybackCapturerManagerProxyFuzzTest()201 void AudioServerProxyCreatePlaybackCapturerManagerProxyFuzzTest()
202 {
203     AudioServerProxy &audioServerProxy = AudioServerProxy::GetInstance();
204     audioServerProxy.CreatePlaybackCapturerManagerProxy();
205 }
206 
AudioServerProxySetAudioEffectPropertyProxyFuzzTest()207 void AudioServerProxySetAudioEffectPropertyProxyFuzzTest()
208 {
209     if (g_testDeviceTypes.size() == 0) {
210         return;
211     }
212     AudioServerProxy &audioServerProxy = AudioServerProxy::GetInstance();
213     DeviceType deviceType = g_testDeviceTypes[GetData<uint32_t>() % g_testDeviceTypes.size()];
214     AudioEffectPropertyArrayV3 propertyArrayV3;
215     audioServerProxy.SetAudioEffectPropertyProxy(propertyArrayV3, deviceType);
216     AudioEffectPropertyArray propertyArray;
217     audioServerProxy.SetAudioEffectPropertyProxy(propertyArray);
218 }
219 
AudioServerProxySetRotationToEffectProxyFuzzTest()220 void AudioServerProxySetRotationToEffectProxyFuzzTest()
221 {
222     AudioServerProxy &audioServerProxy = AudioServerProxy::GetInstance();
223     uint32_t rotate = GetData<uint32_t>();
224     audioServerProxy.SetRotationToEffectProxy(rotate);
225 }
226 
AudioServerProxySetAudioMonoStateProxyFuzzTest()227 void AudioServerProxySetAudioMonoStateProxyFuzzTest()
228 {
229     AudioServerProxy &audioServerProxy = AudioServerProxy::GetInstance();
230     bool audioMono = GetData<bool>();
231     audioServerProxy.SetAudioMonoStateProxy(audioMono);
232 }
233 
AudioServerProxySetAudioBalanceValueProxyFuzzTest()234 void AudioServerProxySetAudioBalanceValueProxyFuzzTest()
235 {
236     AudioServerProxy &audioServerProxy = AudioServerProxy::GetInstance();
237     float audioBalance = GetData<float>();
238     audioServerProxy.SetAudioBalanceValueProxy(audioBalance);
239 }
240 
AudioServerProxyNotifyAccountsChangedFuzzTest()241 void AudioServerProxyNotifyAccountsChangedFuzzTest()
242 {
243     AudioServerProxy &audioServerProxy = AudioServerProxy::GetInstance();
244     audioServerProxy.NotifyAccountsChanged();
245     audioServerProxy.NotifySettingsDataReady();
246 }
247 
AudioServerProxyCreateHdiSinkPortProxyFuzzTest()248 void AudioServerProxyCreateHdiSinkPortProxyFuzzTest()
249 {
250     AudioServerProxy &audioServerProxy = AudioServerProxy::GetInstance();
251     std::string deviceClass = "testDeviceClass";
252     std::string idInfo = "testIdInfo";
253     IAudioSinkAttr attr;
254     audioServerProxy.CreateHdiSinkPortProxy(deviceClass, idInfo, attr);
255 }
256 
AudioServerProxyCreateHdiSourcePortProxyFuzzTest()257 void AudioServerProxyCreateHdiSourcePortProxyFuzzTest()
258 {
259     AudioServerProxy &audioServerProxy = AudioServerProxy::GetInstance();
260     std::string deviceClass = "testDeviceClass";
261     std::string idInfo = "testIdInfo";
262     IAudioSourceAttr attr;
263     audioServerProxy.CreateHdiSourcePortProxy(deviceClass, idInfo, attr);
264 }
265 
AudioServerProxyDestroyHdiPortProxyFuzzTest()266 void AudioServerProxyDestroyHdiPortProxyFuzzTest()
267 {
268     AudioServerProxy &audioServerProxy = AudioServerProxy::GetInstance();
269     uint32_t id = GetData<uint32_t>();
270     audioServerProxy.DestroyHdiPortProxy(id);
271 }
272 
AudioServerProxySetDeviceConnectedFlagFuzzTest()273 void AudioServerProxySetDeviceConnectedFlagFuzzTest()
274 {
275     AudioServerProxy &audioServerProxy = AudioServerProxy::GetInstance();
276     bool flag = GetData<bool>();
277     audioServerProxy.SetDeviceConnectedFlag(flag);
278 }
279 
AudioServerProxySetLatestMuteStateFuzzTest()280 void AudioServerProxySetLatestMuteStateFuzzTest()
281 {
282     AudioServerProxy &audioServerProxy = AudioServerProxy::GetInstance();
283     uint32_t sessionId = GetData<uint32_t>();
284     bool muteFlag = GetData<bool>();
285     audioServerProxy.SetLatestMuteState(sessionId, muteFlag);
286 }
287 
AudioServerProxyGetAudioEnhancePropertyProxyFuzzTest()288 void AudioServerProxyGetAudioEnhancePropertyProxyFuzzTest()
289 {
290     if (g_testDeviceTypes.size() == 0) {
291         return;
292     }
293     DeviceType deviceType = g_testDeviceTypes[GetData<uint32_t>() % g_testDeviceTypes.size()];
294     AudioEnhancePropertyArray propertyArray;
295     AudioServerProxy &audioServerProxy = AudioServerProxy::GetInstance();
296     audioServerProxy.GetAudioEnhancePropertyProxy(propertyArray, deviceType);
297 }
298 
AudioServerProxyGetAudioEffectPropertyProxyFuzzTest()299 void AudioServerProxyGetAudioEffectPropertyProxyFuzzTest()
300 {
301     AudioServerProxy &audioServerProxy = AudioServerProxy::GetInstance();
302     AudioEffectPropertyArrayV3 propertyArrayV3;
303     audioServerProxy.GetAudioEffectPropertyProxy(propertyArrayV3);
304 }
305 
AudioServerProxyIsAcousticEchoCancelerSupportedFuzzTest()306 void AudioServerProxyIsAcousticEchoCancelerSupportedFuzzTest()
307 {
308     AudioServerProxy &audioServerProxy = AudioServerProxy::GetInstance();
309     SourceType sourceType = SOURCE_TYPE_VOICE_RECOGNITION;
310     audioServerProxy.IsAcousticEchoCancelerSupported(sourceType);
311 }
312 
AudioServerProxySetKaraokeParametersFuzzTest()313 void AudioServerProxySetKaraokeParametersFuzzTest()
314 {
315     AudioServerProxy &audioServerProxy = AudioServerProxy::GetInstance();
316     std::string parameters = "testParameters";
317     audioServerProxy.SetKaraokeParameters(parameters);
318 }
319 
AudioServerProxyIsAudioLoopbackSupportedFuzzTest()320 void AudioServerProxyIsAudioLoopbackSupportedFuzzTest()
321 {
322     AudioServerProxy &audioServerProxy = AudioServerProxy::GetInstance();
323     AudioLoopbackMode mode = LOOPBACK_HARDWARE;
324     audioServerProxy.IsAudioLoopbackSupported(mode);
325 }
326 
AudioServerProxySetSessionMuteStateFuzzTest()327 void AudioServerProxySetSessionMuteStateFuzzTest()
328 {
329     AudioServerProxy &audioServerProxy = AudioServerProxy::GetInstance();
330     uint32_t sessionId = GetData<uint32_t>();
331     bool insert = GetData<bool>();
332     bool muteFlag = GetData<bool>();
333     audioServerProxy.SetSessionMuteState(sessionId, insert, muteFlag);
334 }
335 
AudioServerProxySetActiveOutputDeviceProxyFuzzTest()336 void AudioServerProxySetActiveOutputDeviceProxyFuzzTest()
337 {
338     if (g_testDeviceTypes.size() == 0) {
339         return;
340     }
341     AudioServerProxy &audioServerProxy = AudioServerProxy::GetInstance();
342     DeviceType deviceType = g_testDeviceTypes[GetData<uint32_t>() % g_testDeviceTypes.size()];
343     audioServerProxy.SetActiveOutputDeviceProxy(deviceType);
344 }
345 
AudioServerProxyForceStopAudioStreamProxyFuzzTest()346 void AudioServerProxyForceStopAudioStreamProxyFuzzTest()
347 {
348     AudioServerProxy &audioServerProxy = AudioServerProxy::GetInstance();
349     StopAudioType audioType = STOP_RENDER;
350     audioServerProxy.ForceStopAudioStreamProxy(audioType);
351 }
352 
353 TestPtr g_testPtrs[] = {
354     AudioServerProxyGetEffectOffloadEnabledProxyFuzzTest,
355     AudioServerProxyUpdateDualToneStateProxyFuzzTest,
356     AudioServerProxyUpdateSessionConnectionStateProxyFuzzTest,
357     AudioServerProxyCheckRemoteDeviceStateProxyFuzzTest,
358     AudioServerProxyResetAudioEndpointProxyFuzzTest,
359     AudioServerProxyOffloadSetVolumeProxyFuzzTest,
360     AudioServerProxyUnsetOffloadModeProxyFuzzTest,
361     AudioServerProxySetOffloadModeProxyFuzzTest,
362     AudioServerProxyCheckHibernateStateProxyFuzzTest,
363     AudioServerProxySetAudioEnhancePropertyProxyFuzzTest,
364     AudioServerProxySetSinkMuteForSwitchDeviceProxyFuzzTest,
365     AudioServerProxySuspendRenderSinkProxyFuzzTest,
366     AudioServerProxyNotifyDeviceInfoProxyFuzzTest,
367     AudioServerProxyCreatePlaybackCapturerManagerProxyFuzzTest,
368     AudioServerProxySetAudioEffectPropertyProxyFuzzTest,
369     AudioServerProxySetRotationToEffectProxyFuzzTest,
370     AudioServerProxySetAudioMonoStateProxyFuzzTest,
371     AudioServerProxySetAudioBalanceValueProxyFuzzTest,
372     AudioServerProxyNotifyAccountsChangedFuzzTest,
373     AudioServerProxyCreateHdiSinkPortProxyFuzzTest,
374     AudioServerProxyCreateHdiSourcePortProxyFuzzTest,
375     AudioServerProxyDestroyHdiPortProxyFuzzTest,
376     AudioServerProxySetDeviceConnectedFlagFuzzTest,
377     AudioServerProxySetLatestMuteStateFuzzTest,
378     AudioServerProxyGetAudioEnhancePropertyProxyFuzzTest,
379     AudioServerProxyGetAudioEffectPropertyProxyFuzzTest,
380     AudioServerProxyIsAcousticEchoCancelerSupportedFuzzTest,
381     AudioServerProxySetKaraokeParametersFuzzTest,
382     AudioServerProxyIsAudioLoopbackSupportedFuzzTest,
383     AudioServerProxySetSessionMuteStateFuzzTest,
384     AudioServerProxySetActiveOutputDeviceProxyFuzzTest,
385     AudioServerProxyForceStopAudioStreamProxyFuzzTest,
386 };
387 
FuzzTest(const uint8_t * rawData,size_t size)388 void FuzzTest(const uint8_t* rawData, size_t size)
389 {
390     if (rawData == nullptr) {
391         return;
392     }
393 
394     RAW_DATA = rawData;
395     g_dataSize = size;
396     g_pos = 0;
397 
398     uint32_t code = GetData<uint32_t>();
399     uint32_t len = GetArrLength(g_testPtrs);
400     if (len > 0) {
401         g_testPtrs[code % len]();
402     } else {
403         AUDIO_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
404     }
405     return;
406 }
407 
408 } // namespace AudioStandard
409 } // namesapce OHOS
410 
411 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)412 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
413 {
414     if (size < OHOS::AudioStandard::THRESHOLD) {
415         return 0;
416     }
417     OHOS::AudioStandard::FuzzTest(data, size);
418     return 0;
419 }