• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_stream_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 = 14;
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     int waitCount = 0;
143     while (hpaeManager->IsMsgProcessing()) {
144         std::this_thread::sleep_for(std::chrono::milliseconds(TEST_SLEEP_TIME_20));
145     }
146     std::this_thread::sleep_for(std::chrono::milliseconds(TEST_SLEEP_TIME_40));
147 }
148 
GetRenderStreamInfo()149 HPAE::HpaeStreamInfo GetRenderStreamInfo()
150 {
151     HPAE::HpaeStreamInfo streamInfo;
152     streamInfo.channels = STEREO;
153     streamInfo.samplingRate = SAMPLE_RATE_44100;
154     streamInfo.format = SAMPLE_S16LE;
155     streamInfo.frameLen = FRAME_LENGTH;
156     streamInfo.sessionId = TEST_STREAM_SESSION_ID;
157     streamInfo.streamType = STREAM_MUSIC;
158     streamInfo.streamClassType = HPAE::HPAE_STREAM_CLASS_TYPE_PLAY;
159     return streamInfo;
160 }
161 
GetSourceAudioModeInfo(std::string name="mic")162 AudioModuleInfo GetSourceAudioModeInfo(std::string name = "mic")
163 {
164     AudioModuleInfo audioModuleInfo;
165     audioModuleInfo.lib = "libmodule-hdi-source.z.so";
166     audioModuleInfo.channels = "2";
167     audioModuleInfo.rate = "48000";
168     audioModuleInfo.name = name;
169     audioModuleInfo.adapterName = "file_io";
170     audioModuleInfo.className = "file_io";
171     audioModuleInfo.bufferSize = "3840";
172     audioModuleInfo.format = "s16le";
173     audioModuleInfo.fixedLatency = "1";
174     audioModuleInfo.offloadEnable = "0";
175     audioModuleInfo.networkId = "LocalDevice";
176     audioModuleInfo.fileName = g_rootPath + "source_" + audioModuleInfo.adapterName + "_" + audioModuleInfo.rate + "_" +
177                                audioModuleInfo.channels + "_" + audioModuleInfo.format + ".pcm";
178     std::stringstream typeValue;
179     typeValue << static_cast<int32_t>(DEVICE_TYPE_FILE_SOURCE);
180     audioModuleInfo.deviceType = typeValue.str();
181     return audioModuleInfo;
182 }
183 
GetCaptureStreamInfo()184 HPAE::HpaeStreamInfo GetCaptureStreamInfo()
185 {
186     HPAE::HpaeStreamInfo streamInfo;
187     streamInfo.channels = STEREO;
188     streamInfo.samplingRate = SAMPLE_RATE_48000;
189     streamInfo.format = SAMPLE_S16LE;
190     streamInfo.frameLen = FRAME_LENGTH;
191     streamInfo.sessionId = TEST_STREAM_SESSION_ID;
192     streamInfo.streamType = STREAM_MUSIC;
193     streamInfo.streamClassType = HPAE::HPAE_STREAM_CLASS_TYPE_RECORD;
194     return streamInfo;
195 }
196 
InitFuzzTest()197 void InitFuzzTest()
198 {
199     std::shared_ptr<HPAE::HpaeManager> hpaeManager_ = std::make_shared<HPAE::HpaeManager>();
200     hpaeManager_->Init();
201     hpaeManager_->IsInit();
202     sleep(1);
203     hpaeManager_->IsRunning();
204     hpaeManager_->DeInit();
205     hpaeManager_->IsInit();
206     sleep(1);
207     hpaeManager_->IsRunning();
208     hpaeManager_->DeInit();
209     hpaeManager_ = nullptr;
210 }
211 
HpaeRenderStreamManagerFuzzTest()212 void HpaeRenderStreamManagerFuzzTest()
213 {
214     std::shared_ptr<HPAE::HpaeManager> hpaeManager_ = std::make_shared<HPAE::HpaeManager>();
215     hpaeManager_->Init();
216 
217     std::shared_ptr<HpaeAudioServiceCallbackFuzzTest> callback = std::make_shared<HpaeAudioServiceCallbackFuzzTest>();
218     hpaeManager_->RegisterSerivceCallback(callback);
219     AudioModuleInfo audioModuleInfo = GetSinkAudioModeInfo();
220     hpaeManager_->OpenAudioPort(audioModuleInfo);
221     hpaeManager_->SetDefaultSink(audioModuleInfo.name);
222     HpaeStreamInfo streamInfo = GetRenderStreamInfo();
223     hpaeManager_->CreateStream(streamInfo);
224     bool mute = GetData<bool>();
225     bool isSync = GetData<bool>();
226     hpaeManager_->SetSinkMute(audioModuleInfo.name, true, true);
227     hpaeManager_->SetSinkMute(audioModuleInfo.name, false, true);
228     bool isSuspend = GetData<bool>();
229     hpaeManager_->SuspendAudioDevice(audioModuleInfo.name, true);
230     hpaeManager_->SuspendAudioDevice(audioModuleInfo.name, false);
231     hpaeManager_->GetAllSinkInputs();
232     hpaeManager_->Release(streamInfo.streamClassType, streamInfo.sessionId);
233 
234     hpaeManager_->GetAllSinkInputs();
235     WaitForMsgProcessing(hpaeManager_);
236     hpaeManager_->DeInit();
237     WaitForMsgProcessing(hpaeManager_);
238 }
239 
HpaeRenderStreamManagerFuzzTest2()240 void HpaeRenderStreamManagerFuzzTest2()
241 {
242     std::shared_ptr<HPAE::HpaeManager> hpaeManager_ = std::make_shared<HPAE::HpaeManager>();
243     hpaeManager_->Init();
244 
245     std::shared_ptr<HpaeAudioServiceCallbackFuzzTest> callback = std::make_shared<HpaeAudioServiceCallbackFuzzTest>();
246     hpaeManager_->RegisterSerivceCallback(callback);
247     AudioModuleInfo audioModuleInfo = GetSinkAudioModeInfo();
248     hpaeManager_->OpenAudioPort(audioModuleInfo);
249     hpaeManager_->SetDefaultSink(audioModuleInfo.name);
250     HpaeStreamInfo streamInfo = GetRenderStreamInfo();
251     hpaeManager_->CreateStream(streamInfo);
252     bool mute = GetData<bool>();
253     bool isSync = GetData<bool>();
254     hpaeManager_->SetSinkMute(audioModuleInfo.name, mute, isSync);
255     bool isSuspend = GetData<bool>();
256     hpaeManager_->SuspendAudioDevice(audioModuleInfo.name, isSuspend);
257 
258     hpaeManager_->GetAllSinkInputs();
259     uint32_t sessionId = GetData<uint32_t>();
260     uint32_t index = GetData<uint32_t>() % HpaeStreamClassTypeVec.size();
261     HpaeStreamClassType streamClassType = HpaeStreamClassTypeVec[index];
262     hpaeManager_->Release(streamClassType, sessionId);
263 
264     hpaeManager_->GetAllSinkInputs();
265     WaitForMsgProcessing(hpaeManager_);
266     hpaeManager_->DeInit();
267     WaitForMsgProcessing(hpaeManager_);
268 }
269 
HpaeRenderStreamManagerFuzzTest3()270 void HpaeRenderStreamManagerFuzzTest3()
271 {
272     std::shared_ptr<HPAE::HpaeManager> hpaeManager_ = std::make_shared<HPAE::HpaeManager>();
273     hpaeManager_->Init();
274     hpaeManager_->IsInit();
275 
276     AudioModuleInfo audioModuleInfo = GetSinkAudioModeInfo();
277     hpaeManager_->OpenAudioPort(audioModuleInfo);
278     hpaeManager_->SetDefaultSink(audioModuleInfo.name);
279     HpaeStreamInfo streamInfo = GetRenderStreamInfo();
280     hpaeManager_->CreateStream(streamInfo);
281 
282     uint32_t sessionId = GetData<uint32_t>();
283     uint32_t index = GetData<uint32_t>() % HpaeStreamClassTypeVec.size();
284     HpaeStreamClassType streamClassType = HpaeStreamClassTypeVec[index];
285 
286     HpaeSessionInfo sessionInfo;
287     hpaeManager_->GetSessionInfo(streamClassType, sessionId, sessionInfo);
288     hpaeManager_->Start(streamClassType, sessionId);
289     WaitForMsgProcessing(hpaeManager_);
290     hpaeManager_->GetSessionInfo(streamClassType, sessionId, sessionInfo);
291 
292     int32_t syncId = GetData<int32_t>();
293     hpaeManager_->StartWithSyncId(streamClassType, sessionId, syncId);
294     WaitForMsgProcessing(hpaeManager_);
295     hpaeManager_->GetSessionInfo(streamClassType, sessionId, sessionInfo);
296 
297     hpaeManager_->Pause(streamClassType, sessionId);
298     WaitForMsgProcessing(hpaeManager_);
299     hpaeManager_->GetSessionInfo(streamClassType, sessionId, sessionInfo);
300 
301     hpaeManager_->Stop(streamClassType, sessionId);
302     WaitForMsgProcessing(hpaeManager_);
303     hpaeManager_->GetSessionInfo(streamClassType, sessionId, sessionInfo);
304 
305     hpaeManager_->Release(streamClassType, sessionId);
306     hpaeManager_->Release(streamClassType, sessionId);
307     WaitForMsgProcessing(hpaeManager_);
308     hpaeManager_->GetSessionInfo(streamClassType, sessionId, sessionInfo);
309     WaitForMsgProcessing(hpaeManager_);
310     hpaeManager_->DeInit();
311     WaitForMsgProcessing(hpaeManager_);
312 }
313 
HpaeRenderStreamManagerFuzzTest4()314 void HpaeRenderStreamManagerFuzzTest4()
315 {
316     std::shared_ptr<HPAE::HpaeManager> hpaeManager_ = std::make_shared<HPAE::HpaeManager>();
317     hpaeManager_->Init();
318     hpaeManager_->IsInit();
319 
320     AudioModuleInfo audioModuleInfo = GetSinkAudioModeInfo();
321     hpaeManager_->OpenAudioPort(audioModuleInfo);
322     hpaeManager_->SetDefaultSink(audioModuleInfo.name);
323 
324     HpaeStreamInfo streamInfo = GetRenderStreamInfo();
325     hpaeManager_->CreateStream(streamInfo);
326     WaitForMsgProcessing(hpaeManager_);
327 
328     HpaeSessionInfo sessionInfo;
329     hpaeManager_->GetSessionInfo(streamInfo.streamClassType, streamInfo.sessionId, sessionInfo);
330 
331     hpaeManager_->Start(streamInfo.streamClassType, streamInfo.sessionId);
332     WaitForMsgProcessing(hpaeManager_);
333     hpaeManager_->GetSessionInfo(streamInfo.streamClassType, streamInfo.sessionId, sessionInfo);
334 
335     int32_t syncId = GetData<int32_t>();
336     hpaeManager_->StartWithSyncId(streamInfo.streamClassType, streamInfo.sessionId, syncId);
337     WaitForMsgProcessing(hpaeManager_);
338     hpaeManager_->GetSessionInfo(streamInfo.streamClassType, streamInfo.sessionId, sessionInfo);
339 
340     hpaeManager_->Pause(streamInfo.streamClassType, streamInfo.sessionId);
341     WaitForMsgProcessing(hpaeManager_);
342     hpaeManager_->GetSessionInfo(streamInfo.streamClassType, streamInfo.sessionId, sessionInfo);
343 
344     hpaeManager_->Stop(streamInfo.streamClassType, streamInfo.sessionId);
345     WaitForMsgProcessing(hpaeManager_);
346     hpaeManager_->GetSessionInfo(streamInfo.streamClassType, streamInfo.sessionId, sessionInfo);
347 
348     hpaeManager_->Release(streamInfo.streamClassType, streamInfo.sessionId);
349     WaitForMsgProcessing(hpaeManager_);
350     hpaeManager_->GetSessionInfo(streamInfo.streamClassType, streamInfo.sessionId, sessionInfo);
351     WaitForMsgProcessing(hpaeManager_);
352     hpaeManager_->DeInit();
353     WaitForMsgProcessing(hpaeManager_);
354 }
355 
HpaeCaptureStreamManagerFuzzTest()356 void HpaeCaptureStreamManagerFuzzTest()
357 {
358     std::shared_ptr<HPAE::HpaeManager> hpaeManager_ = std::make_shared<HPAE::HpaeManager>();
359     hpaeManager_->Init();
360     hpaeManager_->IsInit();
361 
362     std::shared_ptr<HpaeAudioServiceCallbackFuzzTest> callback = std::make_shared<HpaeAudioServiceCallbackFuzzTest>();
363     hpaeManager_->RegisterSerivceCallback(callback);
364     AudioModuleInfo audioModuleInfo = GetSourceAudioModeInfo();
365     hpaeManager_->OpenAudioPort(audioModuleInfo);
366     hpaeManager_->SetDefaultSource(audioModuleInfo.name);
367     HpaeStreamInfo streamInfo = GetCaptureStreamInfo();
368     hpaeManager_->CreateStream(streamInfo);
369     WaitForMsgProcessing(hpaeManager_);
370     int32_t fuzzPortId = GetData<int32_t>();
371     bool mute = GetData<bool>();
372     hpaeManager_->SetSourceOutputMute(fuzzPortId, mute);
373     hpaeManager_->GetAllSourceOutputs();
374     hpaeManager_->Release(streamInfo.streamClassType, streamInfo.sessionId);
375     WaitForMsgProcessing(hpaeManager_);
376     hpaeManager_->DeInit();
377     WaitForMsgProcessing(hpaeManager_);
378 }
379 
HpaeCaptureStreamManagerFuzzTest2()380 void HpaeCaptureStreamManagerFuzzTest2()
381 {
382     std::shared_ptr<HPAE::HpaeManager> hpaeManager_ = std::make_shared<HPAE::HpaeManager>();
383     hpaeManager_->Init();
384     hpaeManager_->IsInit();
385 
386     std::shared_ptr<HpaeAudioServiceCallbackFuzzTest> callback = std::make_shared<HpaeAudioServiceCallbackFuzzTest>();
387     hpaeManager_->RegisterSerivceCallback(callback);
388     AudioModuleInfo audioModuleInfo = GetSourceAudioModeInfo();
389     hpaeManager_->OpenAudioPort(audioModuleInfo);
390     hpaeManager_->SetDefaultSource(audioModuleInfo.name);
391     WaitForMsgProcessing(hpaeManager_);
392     int32_t portId = callback->GetPortId();
393     HpaeStreamInfo streamInfo = GetCaptureStreamInfo();
394     hpaeManager_->CreateStream(streamInfo);
395 
396     hpaeManager_->SetSourceOutputMute(portId, true);
397 
398     callback->GetSetSourceOutputMuteResult();
399     hpaeManager_->SetSourceOutputMute(portId, false);
400 
401     hpaeManager_->GetAllSourceOutputs();
402 
403     hpaeManager_->Release(streamInfo.streamClassType, streamInfo.sessionId);
404     WaitForMsgProcessing(hpaeManager_);
405     hpaeManager_->DeInit();
406     WaitForMsgProcessing(hpaeManager_);
407 }
408 
HpaeCaptureStreamManagerFuzzTest3()409 void HpaeCaptureStreamManagerFuzzTest3()
410 {
411     std::shared_ptr<HPAE::HpaeManager> hpaeManager_ = std::make_shared<HPAE::HpaeManager>();
412     hpaeManager_->Init();
413     hpaeManager_->IsInit();
414 
415     AudioModuleInfo audioModuleInfo = GetSourceAudioModeInfo();
416     hpaeManager_->OpenAudioPort(audioModuleInfo);
417     hpaeManager_->SetDefaultSource(audioModuleInfo.name);
418     HpaeStreamInfo streamInfo = GetCaptureStreamInfo();
419     hpaeManager_->CreateStream(streamInfo);
420     WaitForMsgProcessing(hpaeManager_);
421 
422     HpaeSessionInfo sessionInfo;
423     hpaeManager_->GetSessionInfo(streamInfo.streamClassType, streamInfo.sessionId, sessionInfo);
424     hpaeManager_->Start(streamInfo.streamClassType, streamInfo.sessionId);
425     WaitForMsgProcessing(hpaeManager_);
426     hpaeManager_->GetSessionInfo(streamInfo.streamClassType, streamInfo.sessionId, sessionInfo);
427 
428     hpaeManager_->Pause(streamInfo.streamClassType, streamInfo.sessionId);
429     WaitForMsgProcessing(hpaeManager_);
430     hpaeManager_->GetSessionInfo(streamInfo.streamClassType, streamInfo.sessionId, sessionInfo);
431 
432     hpaeManager_->Stop(streamInfo.streamClassType, streamInfo.sessionId);
433     WaitForMsgProcessing(hpaeManager_);
434     hpaeManager_->GetSessionInfo(streamInfo.streamClassType, streamInfo.sessionId, sessionInfo);
435 
436     hpaeManager_->Release(streamInfo.streamClassType, streamInfo.sessionId);
437     WaitForMsgProcessing(hpaeManager_);
438     hpaeManager_->GetSessionInfo(streamInfo.streamClassType, streamInfo.sessionId, sessionInfo);
439     hpaeManager_->DeInit();
440     WaitForMsgProcessing(hpaeManager_);
441 }
442 
HpaeRenderStreamManagerMoveFuzzTest()443 void HpaeRenderStreamManagerMoveFuzzTest()
444 {
445     std::shared_ptr<HPAE::HpaeManager> hpaeManager_ = std::make_shared<HPAE::HpaeManager>();
446     hpaeManager_->Init();
447     hpaeManager_->IsInit();
448 
449     AudioModuleInfo audioModuleInfo = GetSinkAudioModeInfo();
450     hpaeManager_->OpenAudioPort(audioModuleInfo);
451     hpaeManager_->SetDefaultSink(audioModuleInfo.name);
452     AudioModuleInfo audioModuleInfo1 = GetSinkAudioModeInfo("Speaker_File1");
453     hpaeManager_->OpenAudioPort(audioModuleInfo1);
454     HpaeStreamInfo streamInfo = GetRenderStreamInfo();
455     hpaeManager_->CreateStream(streamInfo);
456     float volume = GetData<float>();
457     hpaeManager_->SetClientVolume(streamInfo.sessionId, volume);
458     float loudnessGain = GetData<float>();
459     hpaeManager_->SetLoudnessGain(streamInfo.sessionId, loudnessGain);
460     int32_t rate = GetData<int32_t>();
461     hpaeManager_->SetRate(streamInfo.sessionId, rate);
462     WaitForMsgProcessing(hpaeManager_);
463 
464     HpaeSessionInfo sessionInfo;
465     hpaeManager_->GetSessionInfo(streamInfo.streamClassType, streamInfo.sessionId, sessionInfo);
466     hpaeManager_->MoveSinkInputByIndexOrName(streamInfo.sessionId, 1, "Speaker_File1");
467     hpaeManager_->Start(streamInfo.streamClassType, streamInfo.sessionId);
468     WaitForMsgProcessing(hpaeManager_);
469     hpaeManager_->GetSessionInfo(streamInfo.streamClassType, streamInfo.sessionId, sessionInfo);
470 
471     hpaeManager_->MoveSinkInputByIndexOrName(streamInfo.sessionId, 0, "Speaker_File");
472     hpaeManager_->Pause(streamInfo.streamClassType, streamInfo.sessionId);
473     WaitForMsgProcessing(hpaeManager_);
474     hpaeManager_->GetSessionInfo(streamInfo.streamClassType, streamInfo.sessionId, sessionInfo);
475 
476     hpaeManager_->MoveSinkInputByIndexOrName(streamInfo.sessionId, 1, "Speaker_File1");
477     hpaeManager_->Stop(streamInfo.streamClassType, streamInfo.sessionId);
478     WaitForMsgProcessing(hpaeManager_);
479     hpaeManager_->GetSessionInfo(streamInfo.streamClassType, streamInfo.sessionId, sessionInfo);
480 
481     hpaeManager_->MoveSinkInputByIndexOrName(streamInfo.sessionId, 0, "Speaker_File");
482     hpaeManager_->Release(streamInfo.streamClassType, streamInfo.sessionId);
483 
484     WaitForMsgProcessing(hpaeManager_);
485     hpaeManager_->GetSessionInfo(streamInfo.streamClassType, streamInfo.sessionId, sessionInfo);
486     hpaeManager_->DeInit();
487     WaitForMsgProcessing(hpaeManager_);
488 }
489 
HpaeRenderStreamManagerMoveFuzzTest2()490 void HpaeRenderStreamManagerMoveFuzzTest2()
491 {
492     std::shared_ptr<HPAE::HpaeManager> hpaeManager_ = std::make_shared<HPAE::HpaeManager>();
493     hpaeManager_->Init();
494     hpaeManager_->IsInit();
495 
496     AudioModuleInfo audioModuleInfo = GetSinkAudioModeInfo();
497     hpaeManager_->OpenAudioPort(audioModuleInfo);
498     hpaeManager_->SetDefaultSink(audioModuleInfo.name);
499     AudioModuleInfo audioModuleInfo1 = GetSinkAudioModeInfo("Speaker_File1");
500     hpaeManager_->OpenAudioPort(audioModuleInfo1);
501     HpaeStreamInfo streamInfo = GetRenderStreamInfo();
502     hpaeManager_->CreateStream(streamInfo);
503     WaitForMsgProcessing(hpaeManager_);
504     uint32_t sessionId = GetData<uint32_t>();
505     float volume = GetData<float>();
506     hpaeManager_->SetClientVolume(sessionId, volume);
507     float loudnessGain = GetData<float>();
508     hpaeManager_->SetLoudnessGain(sessionId, loudnessGain);
509     int32_t rate = GetData<int32_t>();
510     hpaeManager_->SetRate(sessionId, rate);
511     WaitForMsgProcessing(hpaeManager_);
512     HpaeSessionInfo sessionInfo;
513     uint32_t index = GetData<uint32_t>() % HpaeStreamClassTypeVec.size();
514     HpaeStreamClassType streamClassType = HpaeStreamClassTypeVec[index];
515     hpaeManager_->GetSessionInfo(streamClassType, sessionId, sessionInfo);
516     uint32_t sinkIndex = GetData<uint32_t>();
517     hpaeManager_->MoveSinkInputByIndexOrName(sessionId, sinkIndex, "Speaker_File1");
518     hpaeManager_->Start(streamClassType, sessionId);
519     WaitForMsgProcessing(hpaeManager_);
520     hpaeManager_->GetSessionInfo(streamClassType, sessionId, sessionInfo);
521 
522     sinkIndex = GetData<uint32_t>();
523     hpaeManager_->MoveSinkInputByIndexOrName(sessionId, sinkIndex, "Speaker_File");
524     hpaeManager_->Pause(streamClassType, sessionId);
525     WaitForMsgProcessing(hpaeManager_);
526     hpaeManager_->GetSessionInfo(streamClassType, sessionId, sessionInfo);
527 
528     uint32_t sinkIndex2 = GetData<uint32_t>();
529     sinkIndex = GetData<uint32_t>();
530     hpaeManager_->MoveSinkInputByIndexOrName(sessionId, sinkIndex, "Speaker_File1");
531     hpaeManager_->Stop(streamClassType, sessionId);
532     WaitForMsgProcessing(hpaeManager_);
533     hpaeManager_->GetSessionInfo(streamClassType, sessionId, sessionInfo);
534 
535     sinkIndex = GetData<uint32_t>();
536     hpaeManager_->MoveSinkInputByIndexOrName(sessionId, sinkIndex, "Speaker_File");
537     hpaeManager_->Release(streamClassType, sessionId);
538 
539     WaitForMsgProcessing(hpaeManager_);
540     hpaeManager_->GetSessionInfo(streamClassType, sessionId, sessionInfo);
541     hpaeManager_->DeInit();
542     WaitForMsgProcessing(hpaeManager_);
543 }
544 
HpaeCaptureStreamManagerMoveTest()545 void HpaeCaptureStreamManagerMoveTest()
546 {
547     std::shared_ptr<HPAE::HpaeManager> hpaeManager_ = std::make_shared<HPAE::HpaeManager>();
548     hpaeManager_->Init();
549     hpaeManager_->IsInit();
550 
551     AudioModuleInfo audioModuleInfo = GetSourceAudioModeInfo();
552     hpaeManager_->OpenAudioPort(audioModuleInfo);
553     AudioModuleInfo audioModuleInfo1 = GetSourceAudioModeInfo("mic1");
554     hpaeManager_->OpenAudioPort(audioModuleInfo1);
555     hpaeManager_->SetDefaultSource(audioModuleInfo.name);
556     HpaeStreamInfo streamInfo = GetCaptureStreamInfo();
557     hpaeManager_->CreateStream(streamInfo);
558     WaitForMsgProcessing(hpaeManager_);
559     HpaeSessionInfo sessionInfo;
560     hpaeManager_->GetSessionInfo(streamInfo.streamClassType, streamInfo.sessionId, sessionInfo);
561 
562     hpaeManager_->MoveSourceOutputByIndexOrName(streamInfo.sessionId, 1, "mic1");
563     hpaeManager_->Start(streamInfo.streamClassType, streamInfo.sessionId);
564     WaitForMsgProcessing(hpaeManager_);
565     hpaeManager_->GetSessionInfo(streamInfo.streamClassType, streamInfo.sessionId, sessionInfo);
566 
567     hpaeManager_->MoveSourceOutputByIndexOrName(streamInfo.sessionId, 0, "mic");
568     hpaeManager_->Pause(streamInfo.streamClassType, streamInfo.sessionId);
569     WaitForMsgProcessing(hpaeManager_);
570     hpaeManager_->GetSessionInfo(streamInfo.streamClassType, streamInfo.sessionId, sessionInfo);
571 
572     hpaeManager_->MoveSourceOutputByIndexOrName(streamInfo.sessionId, 1, "mic1");
573     hpaeManager_->Stop(streamInfo.streamClassType, streamInfo.sessionId);
574     WaitForMsgProcessing(hpaeManager_);
575     hpaeManager_->GetSessionInfo(streamInfo.streamClassType, streamInfo.sessionId, sessionInfo);
576 
577     hpaeManager_->MoveSourceOutputByIndexOrName(streamInfo.sessionId, 0, "mic");
578     hpaeManager_->Release(streamInfo.streamClassType, streamInfo.sessionId);
579     WaitForMsgProcessing(hpaeManager_);
580     hpaeManager_->GetSessionInfo(streamInfo.streamClassType, streamInfo.sessionId, sessionInfo);
581     hpaeManager_->DeInit();
582     WaitForMsgProcessing(hpaeManager_);
583 }
584 
HpaeCaptureStreamManagerMoveTest2()585 void HpaeCaptureStreamManagerMoveTest2()
586 {
587     std::shared_ptr<HPAE::HpaeManager> hpaeManager_ = std::make_shared<HPAE::HpaeManager>();
588     hpaeManager_->Init();
589     hpaeManager_->IsInit();
590 
591     AudioModuleInfo audioModuleInfo = GetSourceAudioModeInfo();
592     hpaeManager_->OpenAudioPort(audioModuleInfo);
593     AudioModuleInfo audioModuleInfo1 = GetSourceAudioModeInfo("mic1");
594     hpaeManager_->OpenAudioPort(audioModuleInfo1);
595     hpaeManager_->SetDefaultSource(audioModuleInfo.name);
596     HpaeStreamInfo streamInfo = GetCaptureStreamInfo();
597     hpaeManager_->CreateStream(streamInfo);
598     WaitForMsgProcessing(hpaeManager_);
599     uint32_t sessionId = GetData<uint32_t>();
600     HpaeSessionInfo sessionInfo;
601     uint32_t index = GetData<uint32_t>() % HpaeStreamClassTypeVec.size();
602     HpaeStreamClassType streamClassType = HpaeStreamClassTypeVec[index];
603     hpaeManager_->GetSessionInfo(streamClassType, sessionId, sessionInfo);
604     uint32_t sourceIndex = GetData<uint32_t>();
605     hpaeManager_->MoveSourceOutputByIndexOrName(sessionId, sourceIndex, "mic1");
606     hpaeManager_->Start(streamClassType, sessionId);
607     WaitForMsgProcessing(hpaeManager_);
608     hpaeManager_->GetSessionInfo(streamClassType, sessionId, sessionInfo);
609 
610     sourceIndex = GetData<uint32_t>();
611     hpaeManager_->MoveSourceOutputByIndexOrName(sessionId, sourceIndex, "mic");
612     hpaeManager_->Pause(streamClassType, sessionId);
613     WaitForMsgProcessing(hpaeManager_);
614     hpaeManager_->GetSessionInfo(streamClassType, sessionId, sessionInfo);
615     sourceIndex = GetData<uint32_t>();
616     hpaeManager_->MoveSourceOutputByIndexOrName(sessionId, sourceIndex, "mic1");
617     hpaeManager_->Stop(streamClassType, sessionId);
618     WaitForMsgProcessing(hpaeManager_);
619     hpaeManager_->GetSessionInfo(streamClassType, sessionId, sessionInfo);
620     sourceIndex = GetData<uint32_t>();
621     hpaeManager_->MoveSourceOutputByIndexOrName(sessionId, sourceIndex, "mic");
622     hpaeManager_->Release(streamClassType, sessionId);
623     WaitForMsgProcessing(hpaeManager_);
624     hpaeManager_->GetSessionInfo(streamClassType, sessionId, sessionInfo);
625     hpaeManager_->DeInit();
626     WaitForMsgProcessing(hpaeManager_);
627 }
628 
HpaeManagerDumpStreamInfoTest()629 void HpaeManagerDumpStreamInfoTest()
630 {
631     std::shared_ptr<HPAE::HpaeManager> hpaeManager_ = std::make_shared<HPAE::HpaeManager>();
632     hpaeManager_->Init();
633 
634     hpaeManager_->IsInit();
635     std::shared_ptr<HpaeAudioServiceCallbackFuzzTest> callback = std::make_shared<HpaeAudioServiceCallbackFuzzTest>();
636     hpaeManager_->RegisterSerivceCallback(callback);
637 
638     AudioModuleInfo sinkAudioModuleInfo = GetSinkAudioModeInfo();
639     hpaeManager_->OpenAudioPort(sinkAudioModuleInfo);
640     WaitForMsgProcessing(hpaeManager_);
641     int32_t sinkPortId = callback->GetPortId();
642     AudioModuleInfo sourceAudioModuleInfo = GetSourceAudioModeInfo();
643     hpaeManager_->OpenAudioPort(sourceAudioModuleInfo);
644     WaitForMsgProcessing(hpaeManager_);
645     int32_t sourcePortId = callback->GetPortId();
646 
647     std::shared_ptr<HpaeAudioServiceDumpCallbackUnitTest> dumpCallback =
648         std::make_shared<HpaeAudioServiceDumpCallbackUnitTest>();
649     hpaeManager_->RegisterHpaeDumpCallback(dumpCallback);
650 
651     HpaeStreamInfo rendererStreamInfo = GetRenderStreamInfo();
652     hpaeManager_->CreateStream(rendererStreamInfo);
653     hpaeManager_->DumpSinkInputsInfo();
654     WaitForMsgProcessing(hpaeManager_);
655     dumpCallback->GetSinkInputsSize();
656     hpaeManager_->ShouldNotSkipProcess(rendererStreamInfo.streamClassType, rendererStreamInfo.sessionId);
657 
658     HpaeStreamInfo capturerStreamInfo = GetCaptureStreamInfo();
659     capturerStreamInfo.deviceName = sourceAudioModuleInfo.name;
660     hpaeManager_->CreateStream(capturerStreamInfo);
661     hpaeManager_->DumpSourceOutputsInfo();
662     WaitForMsgProcessing(hpaeManager_);
663     dumpCallback->GetSourceOutputsSize();
664 
665     hpaeManager_->ShouldNotSkipProcess(capturerStreamInfo.streamClassType, capturerStreamInfo.sessionId);
666 
667     hpaeManager_->ShouldNotSkipProcess(HPAE_STREAM_CLASS_TYPE_INVALID, TEST_STREAM_SESSION_ID);
668     hpaeManager_->CloseAudioPort(sinkPortId);
669     hpaeManager_->CloseAudioPort(sourcePortId);
670     WaitForMsgProcessing(hpaeManager_);
671     hpaeManager_->DeInit();
672     WaitForMsgProcessing(hpaeManager_);
673 }
674 
HpaeManagerDumpStreamInfoTest2()675 void HpaeManagerDumpStreamInfoTest2()
676 {
677     std::shared_ptr<HPAE::HpaeManager> hpaeManager_ = std::make_shared<HPAE::HpaeManager>();
678     hpaeManager_->Init();
679 
680     hpaeManager_->IsInit();
681     std::shared_ptr<HpaeAudioServiceCallbackFuzzTest> callback = std::make_shared<HpaeAudioServiceCallbackFuzzTest>();
682     hpaeManager_->RegisterSerivceCallback(callback);
683 
684     AudioModuleInfo sinkAudioModuleInfo = GetSinkAudioModeInfo();
685     hpaeManager_->OpenAudioPort(sinkAudioModuleInfo);
686     WaitForMsgProcessing(hpaeManager_);
687     int32_t sinkPortId = GetData<int32_t>();
688     AudioModuleInfo sourceAudioModuleInfo = GetSourceAudioModeInfo();
689     hpaeManager_->OpenAudioPort(sourceAudioModuleInfo);
690     WaitForMsgProcessing(hpaeManager_);
691     int32_t sourcePortId = GetData<int32_t>();
692 
693     std::shared_ptr<HpaeAudioServiceDumpCallbackUnitTest> dumpCallback =
694         std::make_shared<HpaeAudioServiceDumpCallbackUnitTest>();
695     hpaeManager_->RegisterHpaeDumpCallback(dumpCallback);
696     HpaeStreamInfo rendererStreamInfo = GetRenderStreamInfo();
697     hpaeManager_->CreateStream(rendererStreamInfo);
698 
699     hpaeManager_->DumpSinkInputsInfo();
700     WaitForMsgProcessing(hpaeManager_);
701     dumpCallback->GetSinkInputsSize();
702     uint32_t sessionId = GetData<uint32_t>();
703     uint32_t index = GetData<uint32_t>() % HpaeStreamClassTypeVec.size();
704     HpaeStreamClassType streamClassType = HpaeStreamClassTypeVec[index];
705 
706     hpaeManager_->ShouldNotSkipProcess(streamClassType, sessionId);
707     HpaeStreamInfo capturerStreamInfo = GetCaptureStreamInfo();
708     capturerStreamInfo.deviceName = sourceAudioModuleInfo.name;
709     hpaeManager_->CreateStream(capturerStreamInfo);
710     hpaeManager_->DumpSourceOutputsInfo();
711     dumpCallback->GetSourceOutputsSize();
712     sessionId = GetData<uint32_t>();
713     index = GetData<uint32_t>() % HpaeStreamClassTypeVec.size();
714     streamClassType = HpaeStreamClassTypeVec[index];
715     hpaeManager_->ShouldNotSkipProcess(streamClassType, sessionId);
716 
717     hpaeManager_->CloseAudioPort(sinkPortId);
718     hpaeManager_->CloseAudioPort(sourcePortId);
719     WaitForMsgProcessing(hpaeManager_);
720     hpaeManager_->DeInit();
721     WaitForMsgProcessing(hpaeManager_);
722 }
723 
724 TestFuncs g_testFuncs[TESTSIZE] = {
725     InitFuzzTest,
726     HpaeRenderStreamManagerFuzzTest,
727     HpaeRenderStreamManagerFuzzTest2,
728     HpaeRenderStreamManagerFuzzTest3,
729     HpaeRenderStreamManagerFuzzTest4,
730     HpaeCaptureStreamManagerFuzzTest,
731     HpaeCaptureStreamManagerFuzzTest2,
732     HpaeCaptureStreamManagerFuzzTest3,
733     HpaeRenderStreamManagerMoveFuzzTest,
734     HpaeRenderStreamManagerMoveFuzzTest2,
735     HpaeCaptureStreamManagerMoveTest,
736     HpaeCaptureStreamManagerMoveTest2,
737     HpaeManagerDumpStreamInfoTest,
738     HpaeManagerDumpStreamInfoTest2,
739 };
740 
FuzzTest(const uint8_t * rawData,size_t size)741 bool FuzzTest(const uint8_t* rawData, size_t size)
742 {
743     if (rawData == nullptr) {
744         return false;
745     }
746 
747     // initialize data
748     RAW_DATA = rawData;
749     g_dataSize = size;
750     g_pos = 0;
751 
752     uint32_t code = GetData<uint32_t>();
753     uint32_t len = GetArrLength(g_testFuncs);
754     if (len > 0) {
755         g_testFuncs[code % len]();
756     } else {
757         AUDIO_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
758     }
759 
760     return true;
761 }
762 } // namespace AudioStandard
763 } // namesapce OHOS
764 
765 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)766 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
767 {
768     if (size < OHOS::AudioStandard::THRESHOLD) {
769         return 0;
770     }
771 
772     OHOS::AudioStandard::FuzzTest(data, size);
773     return 0;
774 }
775