1 /*
2 * Copyright (c) 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 #include "hpae_manager_control_fuzzer.h"
16
17 #include <string>
18 #include <thread>
19 #include <chrono>
20 #include <cstdio>
21 #include <fstream>
22 #include <streambuf>
23 #include <algorithm>
24 #include <unistd.h>
25 #include "audio_errors.h"
26 #include "test_case_common.h"
27 #include "hpae_audio_service_dump_callback_unit_test.h"
28
29 namespace OHOS {
30 namespace AudioStandard {
31 using namespace std;
32 using namespace OHOS::AudioStandard::HPAE;
33
34 static const uint8_t* RAW_DATA = nullptr;
35 static size_t g_dataSize = 0;
36 static size_t g_pos;
37 const size_t THRESHOLD = 10;
38 const uint8_t TESTSIZE = 9;
39 static int32_t NUM_2 = 2;
40 static std::string g_rootPath = "/data/";
41 constexpr int32_t TEST_SLEEP_TIME_20 = 20;
42 constexpr int32_t TEST_SLEEP_TIME_40 = 40;
43 constexpr int32_t FRAME_LENGTH = 882;
44 constexpr int32_t TEST_STREAM_SESSION_ID = 123456;
45
46 typedef void (*TestFuncs)();
47
48 vector<AudioSpatializationSceneType> AudioSpatializationSceneTypeVec {
49 SPATIALIZATION_SCENE_TYPE_DEFAULT,
50 SPATIALIZATION_SCENE_TYPE_MUSIC,
51 SPATIALIZATION_SCENE_TYPE_MOVIE,
52 SPATIALIZATION_SCENE_TYPE_AUDIOBOOK,
53 SPATIALIZATION_SCENE_TYPE_MAX,
54 };
55
56 vector<DeviceType> DeviceTypeVec = {
57 DEVICE_TYPE_NONE,
58 DEVICE_TYPE_INVALID,
59 DEVICE_TYPE_EARPIECE,
60 DEVICE_TYPE_SPEAKER,
61 DEVICE_TYPE_WIRED_HEADSET,
62 DEVICE_TYPE_WIRED_HEADPHONES,
63 DEVICE_TYPE_BLUETOOTH_SCO,
64 DEVICE_TYPE_BLUETOOTH_A2DP,
65 DEVICE_TYPE_BLUETOOTH_A2DP_IN,
66 DEVICE_TYPE_MIC,
67 DEVICE_TYPE_WAKEUP,
68 DEVICE_TYPE_USB_HEADSET,
69 DEVICE_TYPE_DP,
70 DEVICE_TYPE_REMOTE_CAST,
71 DEVICE_TYPE_USB_DEVICE,
72 DEVICE_TYPE_ACCESSORY,
73 DEVICE_TYPE_REMOTE_DAUDIO,
74 DEVICE_TYPE_HDMI,
75 DEVICE_TYPE_LINE_DIGITAL,
76 DEVICE_TYPE_NEARLINK,
77 DEVICE_TYPE_NEARLINK_IN,
78 DEVICE_TYPE_FILE_SINK,
79 DEVICE_TYPE_FILE_SOURCE,
80 DEVICE_TYPE_EXTERN_CABLE,
81 DEVICE_TYPE_DEFAULT,
82 DEVICE_TYPE_USB_ARM_HEADSET,
83 DEVICE_TYPE_MAX,
84 };
85
86 vector<HpaeStreamClassType> HpaeStreamClassTypeVec = {
87 HPAE_STREAM_CLASS_TYPE_INVALID,
88 HPAE_STREAM_CLASS_TYPE_PLAY,
89 HPAE_STREAM_CLASS_TYPE_RECORD,
90 };
91
92 template<class T>
GetData()93 T GetData()
94 {
95 T object {};
96 size_t objectSize = sizeof(object);
97 if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
98 return object;
99 }
100 errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
101 if (ret != EOK) {
102 return {};
103 }
104 g_pos += objectSize;
105 return object;
106 }
107
108 template<class T>
GetArrLength(T & arr)109 uint32_t GetArrLength(T& arr)
110 {
111 if (arr == nullptr) {
112 AUDIO_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
113 return 0;
114 }
115 return sizeof(arr) / sizeof(arr[0]);
116 }
117
GetSinkAudioModeInfo(std::string name="Speaker_File")118 AudioModuleInfo GetSinkAudioModeInfo(std::string name = "Speaker_File")
119 {
120 AudioModuleInfo audioModuleInfo;
121 audioModuleInfo.lib = "libmodule-hdi-sink.z.so";
122 audioModuleInfo.channels = "2";
123 audioModuleInfo.rate = "48000";
124 audioModuleInfo.name = name;
125 audioModuleInfo.adapterName = "file_io";
126 audioModuleInfo.className = "file_io";
127 audioModuleInfo.bufferSize = "7680";
128 audioModuleInfo.format = "s32le";
129 audioModuleInfo.fixedLatency = "1";
130 audioModuleInfo.offloadEnable = "0";
131 audioModuleInfo.networkId = "LocalDevice";
132 audioModuleInfo.fileName = g_rootPath + audioModuleInfo.adapterName + "_" + audioModuleInfo.rate + "_" +
133 audioModuleInfo.channels + "_" + audioModuleInfo.format + ".pcm";
134 std::stringstream typeValue;
135 typeValue << static_cast<int32_t>(DEVICE_TYPE_SPEAKER);
136 audioModuleInfo.deviceType = typeValue.str();
137 return audioModuleInfo;
138 }
139
WaitForMsgProcessing(std::shared_ptr<HPAE::HpaeManager> & hpaeManager)140 void WaitForMsgProcessing(std::shared_ptr<HPAE::HpaeManager> &hpaeManager)
141 {
142 while (hpaeManager->IsMsgProcessing()) {
143 std::this_thread::sleep_for(std::chrono::milliseconds(TEST_SLEEP_TIME_20));
144 }
145 std::this_thread::sleep_for(std::chrono::milliseconds(TEST_SLEEP_TIME_40));
146 }
147
GetRenderStreamInfo()148 HPAE::HpaeStreamInfo GetRenderStreamInfo()
149 {
150 HPAE::HpaeStreamInfo streamInfo;
151 streamInfo.channels = STEREO;
152 streamInfo.samplingRate = SAMPLE_RATE_44100;
153 streamInfo.format = SAMPLE_S16LE;
154 streamInfo.frameLen = FRAME_LENGTH;
155 streamInfo.sessionId = TEST_STREAM_SESSION_ID;
156 streamInfo.streamType = STREAM_MUSIC;
157 streamInfo.streamClassType = HPAE::HPAE_STREAM_CLASS_TYPE_PLAY;
158 return streamInfo;
159 }
160
GetSourceAudioModeInfo(std::string name="mic")161 AudioModuleInfo GetSourceAudioModeInfo(std::string name = "mic")
162 {
163 AudioModuleInfo audioModuleInfo;
164 audioModuleInfo.lib = "libmodule-hdi-source.z.so";
165 audioModuleInfo.channels = "2";
166 audioModuleInfo.rate = "48000";
167 audioModuleInfo.name = name;
168 audioModuleInfo.adapterName = "file_io";
169 audioModuleInfo.className = "file_io";
170 audioModuleInfo.bufferSize = "3840";
171 audioModuleInfo.format = "s16le";
172 audioModuleInfo.fixedLatency = "1";
173 audioModuleInfo.offloadEnable = "0";
174 audioModuleInfo.networkId = "LocalDevice";
175 audioModuleInfo.fileName = g_rootPath + "source_" + audioModuleInfo.adapterName + "_" + audioModuleInfo.rate + "_" +
176 audioModuleInfo.channels + "_" + audioModuleInfo.format + ".pcm";
177 std::stringstream typeValue;
178 typeValue << static_cast<int32_t>(DEVICE_TYPE_FILE_SOURCE);
179 audioModuleInfo.deviceType = typeValue.str();
180 return audioModuleInfo;
181 }
182
GetCaptureStreamInfo()183 HPAE::HpaeStreamInfo GetCaptureStreamInfo()
184 {
185 HPAE::HpaeStreamInfo streamInfo;
186 streamInfo.channels = STEREO;
187 streamInfo.samplingRate = SAMPLE_RATE_48000;
188 streamInfo.format = SAMPLE_S16LE;
189 streamInfo.frameLen = FRAME_LENGTH;
190 streamInfo.sessionId = TEST_STREAM_SESSION_ID;
191 streamInfo.streamType = STREAM_MUSIC;
192 streamInfo.streamClassType = HPAE::HPAE_STREAM_CLASS_TYPE_RECORD;
193 return streamInfo;
194 }
195
HpaeRenderManagerReloadTest()196 void HpaeRenderManagerReloadTest()
197 {
198 std::shared_ptr<HPAE::HpaeManager> hpaeManager_ = std::make_shared<HPAE::HpaeManager>();
199 hpaeManager_->Init();
200
201 std::shared_ptr<HpaeAudioServiceCallbackFuzzTest> callback = std::make_shared<HpaeAudioServiceCallbackFuzzTest>();
202 hpaeManager_->RegisterSerivceCallback(callback);
203 AudioModuleInfo audioModuleInfo = GetSinkAudioModeInfo();
204 hpaeManager_->ReloadAudioPort(audioModuleInfo);
205 WaitForMsgProcessing(hpaeManager_);
206 int32_t portId = callback->GetPortId();
207
208 hpaeManager_->ReloadAudioPort(audioModuleInfo);
209 WaitForMsgProcessing(hpaeManager_);
210 portId = callback->GetPortId();
211
212 hpaeManager_->CloseAudioPort(portId);
213 WaitForMsgProcessing(hpaeManager_);
214 callback->GetCloseAudioPortResult();
215
216 hpaeManager_->ReloadAudioPort(audioModuleInfo);
217 WaitForMsgProcessing(hpaeManager_);
218 portId = callback->GetPortId();
219 hpaeManager_->DeInit();
220 WaitForMsgProcessing(hpaeManager_);
221 }
222
HpaeRenderManagerReloadTest2()223 void HpaeRenderManagerReloadTest2()
224 {
225 std::shared_ptr<HPAE::HpaeManager> hpaeManager_ = std::make_shared<HPAE::HpaeManager>();
226 hpaeManager_->Init();
227
228 std::shared_ptr<HpaeAudioServiceCallbackFuzzTest> callback = std::make_shared<HpaeAudioServiceCallbackFuzzTest>();
229 hpaeManager_->RegisterSerivceCallback(callback);
230 AudioModuleInfo audioModuleInfo = GetSourceAudioModeInfo();
231 hpaeManager_->ReloadAudioPort(audioModuleInfo);
232 WaitForMsgProcessing(hpaeManager_);
233 int32_t portId = callback->GetPortId();
234
235 hpaeManager_->ReloadAudioPort(audioModuleInfo);
236 WaitForMsgProcessing(hpaeManager_);
237 portId = callback->GetPortId();
238
239 hpaeManager_->CloseAudioPort(portId);
240 WaitForMsgProcessing(hpaeManager_);
241 callback->GetCloseAudioPortResult();
242
243 hpaeManager_->ReloadAudioPort(audioModuleInfo);
244 WaitForMsgProcessing(hpaeManager_);
245 portId = callback->GetPortId();
246 hpaeManager_->DeInit();
247 WaitForMsgProcessing(hpaeManager_);
248 }
249
HpaeManagerGetSinkAndSourceInfoTest()250 void HpaeManagerGetSinkAndSourceInfoTest()
251 {
252 std::shared_ptr<HPAE::HpaeManager> hpaeManager_ = std::make_shared<HPAE::HpaeManager>();
253 hpaeManager_->Init();
254 std::shared_ptr<HpaeAudioServiceCallbackFuzzTest> callback = std::make_shared<HpaeAudioServiceCallbackFuzzTest>();
255 hpaeManager_->RegisterSerivceCallback(callback);
256 HpaeSinkInfo sinkInfo;
257 HpaeSourceInfo sourceInfo;
258 int32_t ret = -1;
259 hpaeManager_->GetSinkInfoByIdx(0, [&sinkInfo, &ret](const HpaeSinkInfo &sinkInfoRet, int32_t result) {
260 sinkInfo = sinkInfoRet;
261 ret = result;
262 });
263 WaitForMsgProcessing(hpaeManager_);
264
265 ret = -1;
266 hpaeManager_->GetSourceInfoByIdx(0, [&sourceInfo, &ret](const HpaeSourceInfo &sourceInfoRet, int32_t result) {
267 sourceInfo = sourceInfoRet;
268 ret = result;
269 });
270
271 AudioModuleInfo audioModuleInfo1 = GetSinkAudioModeInfo();
272 hpaeManager_->OpenAudioPort(audioModuleInfo1);
273 WaitForMsgProcessing(hpaeManager_);
274 int32_t portId = callback->GetPortId();
275 ret = -1;
276 hpaeManager_->GetSinkInfoByIdx(portId, [&sinkInfo, &ret](const HpaeSinkInfo &sinkInfoRet, int32_t result) {
277 sinkInfo = sinkInfoRet;
278 ret = result;
279 });
280 hpaeManager_->CloseAudioPort(portId);
281 WaitForMsgProcessing(hpaeManager_);
282
283 AudioModuleInfo audioModuleInfo2 = GetSourceAudioModeInfo();
284 hpaeManager_->OpenAudioPort(audioModuleInfo2);
285 WaitForMsgProcessing(hpaeManager_);
286 portId = callback->GetPortId();
287 ret = -1;
288 hpaeManager_->GetSourceInfoByIdx(portId, [&sourceInfo, &ret](const HpaeSourceInfo &sourceInfoRet, int32_t result) {
289 sourceInfo = sourceInfoRet;
290 ret = result;
291 });
292 hpaeManager_->CloseAudioPort(portId);
293 WaitForMsgProcessing(hpaeManager_);
294 hpaeManager_->DeInit();
295 WaitForMsgProcessing(hpaeManager_);
296 }
297
HpaeManagerGetSinkAndSourceInfoTest2()298 void HpaeManagerGetSinkAndSourceInfoTest2()
299 {
300 std::shared_ptr<HPAE::HpaeManager> hpaeManager_ = std::make_shared<HPAE::HpaeManager>();
301 hpaeManager_->Init();
302 std::shared_ptr<HpaeAudioServiceCallbackFuzzTest> callback = std::make_shared<HpaeAudioServiceCallbackFuzzTest>();
303 hpaeManager_->RegisterSerivceCallback(callback);
304 HpaeSinkInfo sinkInfo;
305 HpaeSourceInfo sourceInfo;
306 int32_t ret = -1;
307 hpaeManager_->GetSinkInfoByIdx(0, [&sinkInfo, &ret](const HpaeSinkInfo &sinkInfoRet, int32_t result) {
308 sinkInfo = sinkInfoRet;
309 ret = result;
310 });
311 WaitForMsgProcessing(hpaeManager_);
312
313 ret = -1;
314 hpaeManager_->GetSourceInfoByIdx(0, [&sourceInfo, &ret](const HpaeSourceInfo &sourceInfoRet, int32_t result) {
315 sourceInfo = sourceInfoRet;
316 ret = result;
317 });
318
319 AudioModuleInfo audioModuleInfo1 = GetSinkAudioModeInfo();
320 hpaeManager_->OpenAudioPort(audioModuleInfo1);
321 WaitForMsgProcessing(hpaeManager_);
322 int32_t portId = GetData<int32_t>();
323 ret = -1;
324 hpaeManager_->GetSinkInfoByIdx(portId, [&sinkInfo, &ret](const HpaeSinkInfo &sinkInfoRet, int32_t result) {
325 sinkInfo = sinkInfoRet;
326 ret = result;
327 });
328 hpaeManager_->CloseAudioPort(portId);
329 WaitForMsgProcessing(hpaeManager_);
330
331 AudioModuleInfo audioModuleInfo2 = GetSourceAudioModeInfo();
332 hpaeManager_->OpenAudioPort(audioModuleInfo2);
333 WaitForMsgProcessing(hpaeManager_);
334 portId = GetData<int32_t>();
335 ret = -1;
336 hpaeManager_->GetSourceInfoByIdx(portId, [&sourceInfo, &ret](const HpaeSourceInfo &sourceInfoRet, int32_t result) {
337 sourceInfo = sourceInfoRet;
338 ret = result;
339 });
340 hpaeManager_->CloseAudioPort(portId);
341 WaitForMsgProcessing(hpaeManager_);
342 hpaeManager_->DeInit();
343 WaitForMsgProcessing(hpaeManager_);
344 }
345
HpaeManagerEffectLiveTest()346 void HpaeManagerEffectLiveTest()
347 {
348 std::shared_ptr<HPAE::HpaeManager> hpaeManager_ = std::make_shared<HPAE::HpaeManager>();
349 hpaeManager_->Init();
350 hpaeManager_->NotifySettingsDataReady();
351 hpaeManager_->NotifyAccountsChanged();
352 hpaeManager_->IsAcousticEchoCancelerSupported(SOURCE_TYPE_LIVE);
353 std::vector<std::string> subKeys;
354 std::vector<std::pair<std::string, std::string>> result;
355 subKeys.push_back("live_effect_supported");
356 hpaeManager_->GetEffectLiveParameter(subKeys, result);
357 std::vector<std::pair<std::string, std::string>> params;
358 params.push_back({"live_effect_enable", "NRON"});
359 hpaeManager_->SetEffectLiveParameter(params);
360 WaitForMsgProcessing(hpaeManager_);
361 hpaeManager_->DeInit();
362 WaitForMsgProcessing(hpaeManager_);
363 }
364
HpaeManagerEffectTest()365 void HpaeManagerEffectTest()
366 {
367 std::shared_ptr<HPAE::HpaeManager> hpaeManager_ = std::make_shared<HPAE::HpaeManager>();
368 hpaeManager_->Init();
369 AudioModuleInfo audioModuleInfo = GetSinkAudioModeInfo();
370 hpaeManager_->OpenAudioPort(audioModuleInfo);
371 hpaeManager_->SetDefaultSink(audioModuleInfo.name);
372 AudioModuleInfo audioModuleInfo1 = GetSinkAudioModeInfo("Speaker_File1");
373 hpaeManager_->OpenAudioPort(audioModuleInfo1);
374 HpaeStreamInfo streamInfo = GetRenderStreamInfo();
375 hpaeManager_->CreateStream(streamInfo);
376 WaitForMsgProcessing(hpaeManager_);
377
378 HpaeSessionInfo sessionInfo;
379 hpaeManager_->GetSessionInfo(streamInfo.streamClassType, streamInfo.sessionId, sessionInfo);
380 hpaeManager_->SetRate(streamInfo.sessionId, RENDER_RATE_DOUBLE);
381
382 int32_t effectMode = 0;
383 hpaeManager_->GetAudioEffectMode(streamInfo.sessionId, effectMode);
384 int32_t privacyType = 0;
385 hpaeManager_->SetPrivacyType(streamInfo.sessionId, privacyType);
386 hpaeManager_->GetPrivacyType(streamInfo.sessionId, privacyType);
387 hpaeManager_->GetWritableSize(streamInfo.sessionId);
388 hpaeManager_->UpdateSpatializationState(streamInfo.sessionId + 1, true, false);
389 hpaeManager_->UpdateSpatializationState(streamInfo.sessionId, true, false);
390 hpaeManager_->UpdateMaxLength(streamInfo.sessionId, TEST_SLEEP_TIME_20);
391 hpaeManager_->SetOffloadRenderCallbackType(streamInfo.sessionId, CB_FLUSH_COMPLETED);
392
393 hpaeManager_->Release(streamInfo.streamClassType, streamInfo.sessionId);
394 WaitForMsgProcessing(hpaeManager_);
395 hpaeManager_->GetSessionInfo(streamInfo.streamClassType, streamInfo.sessionId, sessionInfo);
396 hpaeManager_->CloseOutAudioPort("Speaker_File1");
397 hpaeManager_->CloseOutAudioPort("Speaker_File");
398 WaitForMsgProcessing(hpaeManager_);
399 hpaeManager_->DeInit();
400 WaitForMsgProcessing(hpaeManager_);
401 }
402
HpaeManagerEffectTest2()403 void HpaeManagerEffectTest2()
404 {
405 std::shared_ptr<HPAE::HpaeManager> hpaeManager_ = std::make_shared<HPAE::HpaeManager>();
406 hpaeManager_->Init();
407 AudioModuleInfo audioModuleInfo = GetSinkAudioModeInfo();
408 hpaeManager_->OpenAudioPort(audioModuleInfo);
409 hpaeManager_->SetDefaultSink(audioModuleInfo.name);
410 AudioModuleInfo audioModuleInfo1 = GetSinkAudioModeInfo("Speaker_File1");
411 hpaeManager_->OpenAudioPort(audioModuleInfo1);
412
413 AudioSpatializationState stateInfo;
414 stateInfo.headTrackingEnabled = false;
415 stateInfo.spatializationEnabled = false;
416 hpaeManager_->UpdateSpatializationState(stateInfo);
417 hpaeManager_->UpdateSpatialDeviceType(EARPHONE_TYPE_INEAR);
418
419 AudioEffectPropertyArrayV3 propertyV3;
420 hpaeManager_->GetAudioEffectProperty(propertyV3);
421 WaitForMsgProcessing(hpaeManager_);
422 hpaeManager_->SetAudioEffectProperty(propertyV3);
423
424 AudioEffectPropertyArray property;
425 hpaeManager_->GetAudioEffectProperty(property);
426 WaitForMsgProcessing(hpaeManager_);
427 hpaeManager_->UpdateEffectBtOffloadSupported(true);
428 hpaeManager_->SetOutputDevice(TEST_STREAM_SESSION_ID, DEVICE_TYPE_SPEAKER);
429
430 hpaeManager_->SetMicrophoneMuteInfo(false);
431
432 hpaeManager_->GetAudioEnhanceProperty(propertyV3, DEVICE_TYPE_SPEAKER);
433 WaitForMsgProcessing(hpaeManager_);
434 hpaeManager_->SetAudioEnhanceProperty(propertyV3, DEVICE_TYPE_SPEAKER);
435
436 AudioEnhancePropertyArray propertyEn;
437 hpaeManager_->GetAudioEnhanceProperty(propertyEn, DEVICE_TYPE_SPEAKER);
438 WaitForMsgProcessing(hpaeManager_);
439 hpaeManager_->SetAudioEnhanceProperty(propertyEn, DEVICE_TYPE_SPEAKER);
440
441 hpaeManager_->UpdateExtraSceneType("123", "456", "789");
442 WaitForMsgProcessing(hpaeManager_);
443 hpaeManager_->DeInit();
444 WaitForMsgProcessing(hpaeManager_);
445 }
446
GetAllSinksFuzzTest()447 void GetAllSinksFuzzTest()
448 {
449 std::shared_ptr<HPAE::HpaeManager> hpaeManager_ = std::make_shared<HPAE::HpaeManager>();
450 hpaeManager_->Init();
451 hpaeManager_->IsInit();
452
453 std::shared_ptr<HpaeAudioServiceCallbackFuzzTest> callback = std::make_shared<HpaeAudioServiceCallbackFuzzTest>();
454 hpaeManager_->RegisterSerivceCallback(callback);
455 std::shared_ptr<HpaeAudioServiceDumpCallbackUnitTest> dumpCallback =
456 std::make_shared<HpaeAudioServiceDumpCallbackUnitTest>();
457 hpaeManager_->RegisterHpaeDumpCallback(dumpCallback);
458
459 AudioModuleInfo audioModuleInfo = GetSinkAudioModeInfo();
460 hpaeManager_->OpenAudioPort(audioModuleInfo);
461 hpaeManager_->SetDefaultSink(audioModuleInfo.name);
462 WaitForMsgProcessing(hpaeManager_);
463
464 hpaeManager_->ReloadRenderManager(audioModuleInfo);
465 hpaeManager_->DumpSinkInfo(audioModuleInfo.name);
466 hpaeManager_->DumpSinkInfo("virtual1");
467 WaitForMsgProcessing(hpaeManager_);
468
469 uint32_t sinkSourceIndex = GetData<uint32_t>();
470 hpaeManager_->OpenVirtualAudioPort(audioModuleInfo, sinkSourceIndex);
471 AudioModuleInfo audioModuleInfo1 = GetSinkAudioModeInfo("Speaker_File1");
472 hpaeManager_->OpenVirtualAudioPort(audioModuleInfo1, sinkSourceIndex);
473 WaitForMsgProcessing(hpaeManager_);
474
475 hpaeManager_->GetAllSinks();
476
477 audioModuleInfo1 = GetSourceAudioModeInfo();
478 hpaeManager_->OpenAudioPort(audioModuleInfo1);
479 WaitForMsgProcessing(hpaeManager_);
480
481 hpaeManager_->DumpSourceInfo(audioModuleInfo1.name);
482 hpaeManager_->DumpSourceInfo("virtual1");
483
484 HpaeDeviceInfo devicesInfo_;
485 hpaeManager_->DumpAllAvailableDevice(devicesInfo_);
486 WaitForMsgProcessing(hpaeManager_);
487 hpaeManager_->DeInit();
488 WaitForMsgProcessing(hpaeManager_);
489 }
490
GetAllSinksFuzzTest2()491 void GetAllSinksFuzzTest2()
492 {
493 std::shared_ptr<HPAE::HpaeManager> hpaeManager_ = std::make_shared<HPAE::HpaeManager>();
494 hpaeManager_->Init();
495 hpaeManager_->IsInit();
496 std::shared_ptr<HpaeAudioServiceCallbackFuzzTest> callback = std::make_shared<HpaeAudioServiceCallbackFuzzTest>();
497 hpaeManager_->RegisterSerivceCallback(callback);
498 std::shared_ptr<HpaeAudioServiceDumpCallbackUnitTest> dumpCallback =
499 std::make_shared<HpaeAudioServiceDumpCallbackUnitTest>();
500 hpaeManager_->RegisterHpaeDumpCallback(dumpCallback);
501
502 AudioModuleInfo audioModuleInfo = GetSinkAudioModeInfo();
503 hpaeManager_->OpenAudioPort(audioModuleInfo);
504 hpaeManager_->SetDefaultSink(audioModuleInfo.name);
505 WaitForMsgProcessing(hpaeManager_);
506
507 hpaeManager_->ReloadRenderManager(audioModuleInfo);
508 hpaeManager_->DumpSinkInfo(audioModuleInfo.name);
509 hpaeManager_->DumpSinkInfo("virtual1");
510 WaitForMsgProcessing(hpaeManager_);
511 uint32_t sinkSourceIndex = GetData<uint32_t>();
512 hpaeManager_->OpenVirtualAudioPort(audioModuleInfo, sinkSourceIndex);
513 AudioModuleInfo audioModuleInfo1 = GetSinkAudioModeInfo();
514 hpaeManager_->OpenVirtualAudioPort(audioModuleInfo1, sinkSourceIndex);
515 WaitForMsgProcessing(hpaeManager_);
516
517 hpaeManager_->GetAllSinks();
518 WaitForMsgProcessing(hpaeManager_);
519
520 audioModuleInfo1 = GetSourceAudioModeInfo();
521 hpaeManager_->OpenAudioPort(audioModuleInfo1);
522
523 hpaeManager_->DumpSourceInfo(audioModuleInfo1.name);
524 hpaeManager_->DumpSourceInfo("virtual1");
525
526 HpaeDeviceInfo devicesInfo_;
527 hpaeManager_->DumpAllAvailableDevice(devicesInfo_);
528 WaitForMsgProcessing(hpaeManager_);
529 hpaeManager_->DeInit();
530 WaitForMsgProcessing(hpaeManager_);
531 }
532
533 TestFuncs g_testFuncs[TESTSIZE] = {
534 HpaeRenderManagerReloadTest,
535 HpaeRenderManagerReloadTest2,
536 HpaeManagerGetSinkAndSourceInfoTest,
537 HpaeManagerGetSinkAndSourceInfoTest2,
538 HpaeManagerEffectLiveTest,
539 HpaeManagerEffectTest,
540 HpaeManagerEffectTest2,
541 GetAllSinksFuzzTest,
542 GetAllSinksFuzzTest2,
543 };
544
FuzzTest(const uint8_t * rawData,size_t size)545 bool FuzzTest(const uint8_t* rawData, size_t size)
546 {
547 if (rawData == nullptr) {
548 return false;
549 }
550
551 // initialize data
552 RAW_DATA = rawData;
553 g_dataSize = size;
554 g_pos = 0;
555
556 uint32_t code = GetData<uint32_t>();
557 uint32_t len = GetArrLength(g_testFuncs);
558 if (len > 0) {
559 g_testFuncs[code % len]();
560 } else {
561 AUDIO_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
562 }
563
564 return true;
565 }
566 } // namespace AudioStandard
567 } // namesapce OHOS
568
569 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)570 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
571 {
572 if (size < OHOS::AudioStandard::THRESHOLD) {
573 return 0;
574 }
575
576 OHOS::AudioStandard::FuzzTest(data, size);
577 return 0;
578 }
579