1 /*
2 * Copyright (c) 2024 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 <iostream>
17 #include <cstddef>
18 #include <cstdint>
19 #include "audio_policy_server.h"
20 #include "audio_policy_service.h"
21 #include "audio_device_info.h"
22 #include "message_parcel.h"
23 #include "accesstoken_kit.h"
24 #include "nativetoken_kit.h"
25 #include "token_setproc.h"
26 #include "access_token.h"
27 #include "audio_policy_utils.h"
28 using namespace std;
29
30 namespace OHOS {
31 namespace AudioStandard {
32 const int32_t A2DP_PLAYING = 2;
33 const int32_t A2DP_STOPPED = 1;
34 const int32_t SYSTEM_ABILITY_ID = 3009;
35 const bool RUN_ON_CREATE = false;
36 bool g_hasServerInit = false;
37 bool g_hasPermission = false;
38 const std::u16string FORMMGR_INTERFACE_TOKEN = u"IAudioPolicy";
39 static const uint8_t *RAW_DATA = nullptr;
40 static size_t g_dataSize = 0;
41 static size_t g_pos;
42 const size_t THRESHOLD = 10;
43
44 /*
45 * describe: get data from outside untrusted data(RAW_DATA) which size is according to sizeof(T)
46 * tips: only support basic type
47 */
48 template<class T>
GetData()49 T GetData()
50 {
51 T object {};
52 size_t objectSize = sizeof(object);
53 if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
54 return object;
55 }
56 errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
57 if (ret != EOK) {
58 return {};
59 }
60 g_pos += objectSize;
61 return object;
62 }
63
64 template<class T>
GetArrLength(T & arr)65 uint32_t GetArrLength(T& arr)
66 {
67 if (arr == nullptr) {
68 AUDIO_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
69 return 0;
70 }
71 return sizeof(arr) / sizeof(arr[0]);
72 }
73
GetServerPtr()74 AudioPolicyServer* GetServerPtr()
75 {
76 static AudioPolicyServer server(SYSTEM_ABILITY_ID, RUN_ON_CREATE);
77 if (!g_hasServerInit) {
78 server.OnStart();
79 server.OnAddSystemAbility(AUDIO_DISTRIBUTED_SERVICE_ID, "");
80 #ifdef FEATURE_MULTIMODALINPUT_INPUT
81 server.OnAddSystemAbility(MULTIMODAL_INPUT_SERVICE_ID, "");
82 #endif
83 server.OnAddSystemAbility(BLUETOOTH_HOST_SYS_ABILITY_ID, "");
84 server.OnAddSystemAbility(POWER_MANAGER_SERVICE_ID, "");
85 server.OnAddSystemAbility(SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN, "");
86 server.audioPolicyService_.SetDefaultDeviceLoadFlag(true);
87 g_hasServerInit = true;
88 }
89 return &server;
90 }
91
AudioFuzzTestGetPermission()92 void AudioFuzzTestGetPermission()
93 {
94 if (!g_hasPermission) {
95 uint64_t tokenId;
96 constexpr int perNum = 10;
97 const char *perms[perNum] = {
98 "ohos.permission.MICROPHONE",
99 "ohos.permission.MANAGE_INTELLIGENT_VOICE",
100 "ohos.permission.MANAGE_AUDIO_CONFIG",
101 "ohos.permission.MICROPHONE_CONTROL",
102 "ohos.permission.MODIFY_AUDIO_SETTINGS",
103 "ohos.permission.ACCESS_NOTIFICATION_POLICY",
104 "ohos.permission.USE_BLUETOOTH",
105 "ohos.permission.CAPTURE_VOICE_DOWNLINK_AUDIO",
106 "ohos.permission.RECORD_VOICE_CALL",
107 "ohos.permission.MANAGE_SYSTEM_AUDIO_EFFECTS",
108 };
109
110 NativeTokenInfoParams infoInstance = {
111 .dcapsNum = 0,
112 .permsNum = 10,
113 .aclsNum = 0,
114 .dcaps = nullptr,
115 .perms = perms,
116 .acls = nullptr,
117 .processName = "audiofuzztest",
118 .aplStr = "system_basic",
119 };
120 tokenId = GetAccessTokenId(&infoInstance);
121 SetSelfTokenID(tokenId);
122 OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
123 g_hasPermission = true;
124 }
125 }
126
AudioPolicyServiceEnhanceOneFuzzTest()127 void AudioPolicyServiceEnhanceOneFuzzTest()
128 {
129 PowerMgr::PowerState state = PowerMgr::PowerState::STAND_BY;
130 GetServerPtr()->audioPolicyService_.HandlePowerStateChanged(state);
131
132 sptr<AudioRendererFilter> audioRendererFilter = new(std::nothrow) AudioRendererFilter();
133 if (audioRendererFilter == nullptr) {return;}
134 audioRendererFilter->uid = getuid();
135 audioRendererFilter->rendererInfo.rendererFlags = STREAM_FLAG_FAST;
136 audioRendererFilter->rendererInfo.streamUsage = STREAM_USAGE_MUSIC;
137
138 AudioCapturerInfo capturerInfo;
139 AudioStreamInfo streamInfo;
140 uint32_t sessionId = GetData<uint32_t>();
141 GetServerPtr()->audioPolicyService_.NotifyCapturerAdded(capturerInfo, streamInfo, sessionId);
142
143 DeviceType deviceType = GetData<DeviceType>();
144 GetServerPtr()->audioPolicyService_.audioActiveDevice_.SetCurrentInputDeviceType(deviceType);
145 }
146
AudioPolicyServiceEnhanceTwoFuzzTest()147 void AudioPolicyServiceEnhanceTwoFuzzTest()
148 {
149 GetServerPtr()->audioPolicyService_.audioDeviceCommon_.BluetoothScoDisconectForRecongnition();
150
151 AudioStreamInfo audioStreamInfo;
152 GetServerPtr()->audioPolicyService_.audioDeviceCommon_.LoadA2dpModule(DEVICE_TYPE_BLUETOOTH_A2DP,
153 audioStreamInfo, "", "", SOURCE_TYPE_VOICE_RECOGNITION);
154
155 DeviceType deviceType = GetData<DeviceType>();
156 std::string networkId = "LocalDevice";
157 bool isRemote = GetData<DeviceType>();
158 GetServerPtr()->audioPolicyService_.audioDeviceStatus_.ActivateNewDevice(networkId, deviceType, isRemote);
159
160 DeviceBlockStatus status = GetData<DeviceBlockStatus>();
161 GetServerPtr()->audioPolicyService_.OnMicrophoneBlockedUpdate(deviceType, status);
162 GetServerPtr()->audioPolicyService_.audioDeviceLock_.OnBlockedStatusUpdated(deviceType, status);
163 GetServerPtr()->audioPolicyService_.ResetToSpeaker(DEVICE_TYPE_BLUETOOTH_SCO);
164 GetServerPtr()->audioPolicyService_.ResetToSpeaker(DEVICE_TYPE_NONE);
165
166 AudioDeviceDescriptor updatedDesc;
167 GetServerPtr()->audioPolicyService_.OnDeviceStatusUpdated(updatedDesc, isRemote);
168 AudioStreamDeviceChangeReasonExt reason = GetData<AudioStreamDeviceChangeReasonExt::ExtEnum>();
169 std::shared_ptr<AudioDeviceDescriptor> fuzzAudioDeviceDescriptorSptr = std::make_shared<AudioDeviceDescriptor>();
170 std::vector<std::shared_ptr<AudioDeviceDescriptor>> selectedDesc;
171 selectedDesc.push_back(fuzzAudioDeviceDescriptorSptr);
172 GetServerPtr()->audioPolicyService_.audioDeviceStatus_.UpdateDeviceList(updatedDesc, isRemote,
173 selectedDesc, reason);
174
175 std::string macAddress = "";
176 std::unordered_map<uint32_t, bool> sessionIDToSpatialization;
177 GetServerPtr()->audioPolicyService_.UpdateA2dpOffloadFlagBySpatialService(macAddress, sessionIDToSpatialization);
178 GetServerPtr()->audioPolicyService_.audioRouteMap_.RemoveDeviceInRouterMap(networkId);
179 GetServerPtr()->audioPolicyService_.audioRouteMap_.RemoveDeviceInFastRouterMap(networkId);
180 GetServerPtr()->audioPolicyService_.audioDeviceStatus_.HandleOfflineDistributedDevice();
181 }
182
AudioPolicyServiceEnhanceThreeFuzzTest()183 void AudioPolicyServiceEnhanceThreeFuzzTest()
184 {
185 DStatusInfo statusInfo;
186 std::shared_ptr<AudioDeviceDescriptor> fuzzAudioDeviceDescriptorSptr = std::make_shared<AudioDeviceDescriptor>();
187 std::vector<std::shared_ptr<AudioDeviceDescriptor>> descForCb;
188 descForCb.push_back(fuzzAudioDeviceDescriptorSptr);
189 GetServerPtr()->audioPolicyService_.OnDeviceStatusUpdated(statusInfo, true);
190 GetServerPtr()->audioPolicyService_.OnMonoAudioConfigChanged(true);
191 GetServerPtr()->audioPolicyService_.UpdateDescWhenNoBTPermission(descForCb);
192 DeviceBlockStatus status = GetData<DeviceBlockStatus>();
193 GetServerPtr()->audioPolicyService_.audioDeviceStatus_.TriggerMicrophoneBlockedCallback(descForCb, status);
194 float audioBalance = GetData<float>();
195 GetServerPtr()->audioPolicyService_.OnAudioBalanceChanged(audioBalance);
196 std::string macAddress = "";
197 GetServerPtr()->audioPolicyService_.audioVolumeManager_.SetAbsVolumeSceneAsync(macAddress, true);
198 GetServerPtr()->audioPolicyService_.SetDeviceAbsVolumeSupported(macAddress, true);
199 GetServerPtr()->audioPolicyService_.SetNormalVoipFlag(true);
200
201 std::vector<AudioPin> audioPin = {
202 AUDIO_PIN_NONE,
203 AUDIO_PIN_OUT_SPEAKER,
204 AUDIO_PIN_OUT_HEADSET,
205 AUDIO_PIN_OUT_LINEOUT,
206 AUDIO_PIN_OUT_HDMI,
207 AUDIO_PIN_OUT_USB,
208 AUDIO_PIN_OUT_USB_EXT,
209 AUDIO_PIN_OUT_DAUDIO_DEFAULT,
210 AUDIO_PIN_IN_MIC,
211 AUDIO_PIN_IN_HS_MIC,
212 AUDIO_PIN_IN_LINEIN,
213 AUDIO_PIN_IN_USB_EXT,
214 AUDIO_PIN_IN_DAUDIO_DEFAULT,
215 AUDIO_PIN_OUT_DP,
216 };
217 uint32_t audioPinInt = GetData<uint32_t>() % audioPin.size();
218 AudioPin pin = audioPin[audioPinInt];
219 AudioPolicyUtils::GetInstance().GetDeviceRole(pin);
220
221 std::vector<DeviceType> DeviceTypeVec = {
222 DEVICE_TYPE_EARPIECE,
223 DEVICE_TYPE_SPEAKER,
224 DEVICE_TYPE_BLUETOOTH_A2DP,
225 DEVICE_TYPE_FILE_SINK,
226 DEVICE_TYPE_USB_ARM_HEADSET,
227 DEVICE_TYPE_WIRED_HEADSET,
228 DEVICE_TYPE_USB_HEADSET,
229 DEVICE_TYPE_BLUETOOTH_SCO,
230 DEVICE_TYPE_DEFAULT,
231 };
232 uint32_t deviceTypeInt = GetData<uint32_t>() % DeviceTypeVec.size();
233 DeviceType deviceType = DeviceTypeVec[deviceTypeInt];
234 GetServerPtr()->audioPolicyService_.audioActiveDevice_.UpdateInputDeviceInfo(deviceType);
235 }
236
AudioPolicyServiceEnhanceFourFuzzTest()237 void AudioPolicyServiceEnhanceFourFuzzTest()
238 {
239 GetServerPtr()->audioPolicyService_.ResumeStreamState();
240
241 int32_t clientUid = GetData<int32_t>();
242 int32_t sessionId = GetData<int32_t>();
243 StreamSetStateEventInternal streamSetStateEventInternal;
244 GetServerPtr()->audioPolicyService_.UpdateStreamState(clientUid, streamSetStateEventInternal);
245 GetServerPtr()->audioPolicyService_.GetUid(sessionId);
246
247 std::vector<AudioPin> audioPin = {
248 AUDIO_PIN_NONE,
249 AUDIO_PIN_OUT_SPEAKER,
250 AUDIO_PIN_OUT_DAUDIO_DEFAULT,
251 AUDIO_PIN_OUT_HEADSET,
252 AUDIO_PIN_OUT_LINEOUT,
253 AUDIO_PIN_OUT_HDMI,
254 AUDIO_PIN_OUT_USB,
255 AUDIO_PIN_OUT_USB_EXT,
256 AUDIO_PIN_OUT_USB_HEADSET,
257 AUDIO_PIN_IN_USB_HEADSET,
258 AUDIO_PIN_IN_MIC,
259 AUDIO_PIN_IN_DAUDIO_DEFAULT,
260 AUDIO_PIN_IN_HS_MIC,
261 AUDIO_PIN_IN_LINEIN,
262 AUDIO_PIN_IN_USB_EXT,
263 };
264 uint32_t audioPinInt = GetData<uint32_t>() % audioPin.size();
265 AudioPin hdiPin = audioPin[audioPinInt];
266 GetServerPtr()->audioPolicyService_.audioDeviceStatus_.GetDeviceTypeFromPin(hdiPin);
267
268 AudioDeviceDescriptor deviceInfo;
269 AudioProcessConfig config;
270 config.audioMode = AUDIO_MODE_PLAYBACK;
271 bool lockFlag = GetData<bool>();
272 GetServerPtr()->audioPolicyService_.GetProcessDeviceInfo(config, lockFlag, deviceInfo);
273
274 AudioProcessConfig processInfoConfig;
275 processInfoConfig.audioMode = AUDIO_MODE_RECORD;
276 GetServerPtr()->audioPolicyService_.GetProcessDeviceInfo(processInfoConfig, lockFlag, deviceInfo);
277
278 AudioProcessConfig processConfig;
279 processConfig.audioMode = AUDIO_MODE_RECORD;
280 processConfig.capturerInfo.sourceType = SOURCE_TYPE_VOICE_COMMUNICATION;
281 GetServerPtr()->audioPolicyService_.GetProcessDeviceInfo(processConfig, lockFlag, deviceInfo);
282
283 int32_t type = GetData<int32_t>();
284 std::shared_ptr<AudioDeviceDescriptor> fuzzAudioDeviceDescriptorSptr = std::make_shared<AudioDeviceDescriptor>();
285 std::vector<std::shared_ptr<AudioDeviceDescriptor>> preferredDeviceList;
286 preferredDeviceList.push_back(fuzzAudioDeviceDescriptorSptr);
287 GetServerPtr()->audioPolicyService_.GetVoipDeviceInfo(processConfig, deviceInfo, type, preferredDeviceList);
288
289 std::shared_ptr<AudioSharedMemory> buffer;
290 GetServerPtr()->audioPolicyService_.InitSharedVolume(buffer);
291 }
292
AudioPolicyServiceEnhanceFiveFuzzTest()293 void AudioPolicyServiceEnhanceFiveFuzzTest()
294 {
295 Volume vol;
296 AudioVolumeType streamType = GetData<AudioVolumeType>();
297 DeviceType deviceType = GetData<DeviceType>();
298 GetServerPtr()->audioPolicyService_.GetSharedVolume(streamType, deviceType, vol);
299
300 uint64_t sessionID = GetData<uint32_t>();
301 GetServerPtr()->audioPolicyService_.audioCapturerSession_.OnCapturerSessionRemoved(sessionID);
302 GetServerPtr()->audioPolicyService_.audioCapturerSession_.HandleRemainingSource();
303 AudioDeviceDescriptor inputDevice, outputDevice;
304 inputDevice.deviceType_ = DEVICE_TYPE_DEFAULT;
305 outputDevice.deviceType_ = deviceType;
306 GetServerPtr()->audioPolicyService_.audioCapturerSession_.ReloadSourceForDeviceChange(
307 inputDevice, outputDevice, "test");
308 }
309
AudioPolicyServiceEnhanceSixFuzzTest()310 void AudioPolicyServiceEnhanceSixFuzzTest()
311 {
312 AudioEnhancePropertyArray oldPropertyArray;
313 AudioEnhancePropertyArray newPropertyArray;
314 GetServerPtr()->audioPolicyService_.audioCapturerSession_.ReloadSourceForEffect(oldPropertyArray, newPropertyArray);
315
316 std::shared_ptr<AudioDeviceDescriptor> fuzzAudioDeviceDescriptorSptr = std::make_shared<AudioDeviceDescriptor>();
317 std::vector<std::shared_ptr<AudioDeviceDescriptor>> descs;
318 descs.push_back(fuzzAudioDeviceDescriptorSptr);
319 AudioDeviceUsage usage = GetData<AudioDeviceUsage>();
320 GetServerPtr()->audioPolicyService_.DeviceFilterByUsageInner(usage, descs);
321
322 uint32_t delayValue = GetData<uint32_t>();
323 uint64_t sendDataSize = GetData<uint64_t>();
324 uint32_t timeStamp = GetData<uint32_t>();
325 GetServerPtr()->audioPolicyService_.OffloadGetRenderPosition(delayValue, sendDataSize, timeStamp);
326
327 uint32_t uid = GetData<uint32_t>();
328 std::string bundleName = "";
329 GetServerPtr()->audioPolicyService_.GetAndSaveClientType(uid, bundleName);
330 }
331
AudioPolicyServiceEnhanceSevenFuzzTest()332 void AudioPolicyServiceEnhanceSevenFuzzTest()
333 {
334 AudioDeviceDescriptor desc;
335 desc.deviceCategory_ = BT_UNWEAR_HEADPHONE;
336 AudioStreamDeviceChangeReasonExt reason = AudioStreamDeviceChangeReason::UNKNOWN;
337 GetServerPtr()->audioPolicyService_.audioDeviceStatus_.OnPreferredStateUpdated(desc, CATEGORY_UPDATE, reason);
338
339 AudioDeviceDescriptor descUpdated;
340 descUpdated.deviceCategory_ = BT_UNWEAR_HEADPHONE;
341 descUpdated.deviceType_ = DEVICE_TYPE_BLUETOOTH_SCO;
342 descUpdated.connectState_ = DEACTIVE_CONNECTED;
343 GetServerPtr()->audioPolicyService_.OnDeviceInfoUpdated(desc, ENABLE_UPDATE);
344 AudioPolicyUtils::GetInstance().WriteServiceStartupError("Audio Tone Load Configuration failed");
345
346 uint32_t sessionID = GetData<uint32_t>();
347 MessageParcel data;
348 data.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN);
349 data.WriteBuffer(RAW_DATA, g_dataSize);
350 data.RewindRead(0);
351 sptr<IRemoteObject> object = data.ReadRemoteObject();
352 GetServerPtr()->audioPolicyService_.SetAudioConcurrencyCallback(sessionID, object);
353
354 GetServerPtr()->audioPolicyService_.audioVolumeManager_.ringerModeMute_ = false;
355 GetServerPtr()->audioPolicyService_.ResetRingerModeMute();
356
357 std::string deviceAddress = "";
358 vector<int32_t> sessionIds = {0};
359 GetServerPtr()->audioPolicyService_.audioA2dpOffloadManager_->ConnectA2dpOffload(deviceAddress, sessionIds);
360
361 A2dpOffloadConnectionState currentOffloadConnectionState = GetData<A2dpOffloadConnectionState>();
362 GetServerPtr()->audioPolicyService_.audioA2dpOffloadManager_->
363 audioA2dpOffloadFlag_.SetCurrentOffloadConnectedState(currentOffloadConnectionState);
364
365 int32_t sessionId = GetData<uint32_t>();
366 GetServerPtr()->audioPolicyService_.audioA2dpOffloadManager_->IsA2dpOffloadConnecting(sessionId);
367
368 EventFwk::CommonEventData eventData;
369 GetServerPtr()->audioPolicyService_.OnReceiveEvent(eventData);
370 GetServerPtr()->audioPolicyService_.UnsetAudioDeviceAnahsCallback();
371
372 int32_t streamId = GetData<int32_t>();
373 GetServerPtr()->audioPolicyService_.GetLowPowerVolume(streamId);
374 GetServerPtr()->audioPolicyService_.GetSingleStreamVolume(streamId);
375 }
376
AudioPolicyServiceEnhanceEightFuzzTest()377 void AudioPolicyServiceEnhanceEightFuzzTest()
378 {
379 std::shared_ptr<AudioDeviceDescriptor> remote = std::make_shared<AudioDeviceDescriptor>();
380 CastType type = GetData<CastType>();
381 GetServerPtr()->audioPolicyService_.ConfigDistributedRoutingRole(remote, type);
382 GetServerPtr()->audioPolicyService_.audioDeviceCommon_.SwitchActiveA2dpDevice(remote);
383
384 AudioProcessConfig config;
385 GetServerPtr()->audioPolicyService_.SetWakeUpAudioCapturerFromAudioServer(config);
386 GetServerPtr()->audioPolicyService_.NotifyWakeUpCapturerRemoved();
387 GetServerPtr()->audioPolicyService_.IsAbsVolumeSupported();
388 GetServerPtr()->audioPolicyService_.CloseWakeUpAudioCapturer();
389
390 AudioModuleInfo moduleInfo;
391 AudioStreamInfo audioStreamInfo;
392 GetServerPtr()->audioPolicyService_.audioDeviceCommon_.ReloadA2dpAudioPort(moduleInfo,
393 DEVICE_TYPE_BLUETOOTH_A2DP, audioStreamInfo, "", "", SOURCE_TYPE_MIC);
394
395 InternalDeviceType internalDeviceType = GetData<InternalDeviceType>();
396 bool active = GetData<bool>();
397 GetServerPtr()->audioPolicyService_.SetDeviceActive(internalDeviceType, active);
398
399 std::string anahsShowType = "";
400 GetServerPtr()->audioPolicyService_.OnUpdateAnahsSupport(anahsShowType);
401
402 DeviceType deviceType = GetData<DeviceType>();
403 std::string macAddress = "";
404 std::string deviceName = "";
405 AudioStreamInfo streamInfo;
406 GetServerPtr()->audioPolicyService_.audioDeviceStatus_.ReloadA2dpOffloadOnDeviceChanged(
407 deviceType, macAddress, deviceName, streamInfo);
408 }
409
AudioPolicyServiceEnhanceNineFuzzTest()410 void AudioPolicyServiceEnhanceNineFuzzTest()
411 {
412 DStatusInfo statusInfo;
413 std::vector<std::shared_ptr<AudioDeviceDescriptor>> descForCb;
414 GetServerPtr()->audioPolicyService_.audioDeviceStatus_.HandleDistributedDeviceUpdate(statusInfo, descForCb);
415 GetServerPtr()->audioPolicyDump_.GetEffectManagerInfo();
416 GetServerPtr()->audioPolicyService_.audioConfigManager_.OnVoipConfigParsed(true);
417 std::unordered_map<AdaptersType, AudioAdapterInfo> adapterInfoMap;
418 GetServerPtr()->audioPolicyService_.audioConfigManager_.GetAudioAdapterInfos(adapterInfoMap);
419 std::unordered_map<std::string, std::string> volumeGroupData;
420 GetServerPtr()->audioPolicyService_.audioConfigManager_.GetVolumeGroupData(volumeGroupData);
421 GetServerPtr()->audioPolicyService_.audioConfigManager_.GetInterruptGroupData(volumeGroupData);
422 std::unordered_map<ClassType, std::list<AudioModuleInfo>> deviceClassInfo;
423 GetServerPtr()->audioPolicyService_.audioConfigManager_.GetDeviceClassInfo(deviceClassInfo);
424 GlobalConfigs globalConfigs;
425 GetServerPtr()->audioPolicyService_.audioConfigManager_.GetGlobalConfigs(globalConfigs);
426 GetServerPtr()->audioPolicyService_.audioConfigManager_.GetVoipConfig();
427 int32_t clientId = GetData<int32_t>();
428 MessageParcel data;
429 data.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN);
430 data.WriteBuffer(RAW_DATA, g_dataSize);
431 data.RewindRead(0);
432 sptr<IRemoteObject> object = data.ReadRemoteObject();
433 AudioDeviceUsage usage = GetData<AudioDeviceUsage>();
434 GetServerPtr()->audioPolicyService_.SetAvailableDeviceChangeCallback(clientId, usage, object, true);
435 GetServerPtr()->audioPolicyService_.UnsetAvailableDeviceChangeCallback(clientId, usage);
436 GetServerPtr()->audioPolicyService_.audioActiveDevice_.currentActiveDevice_.deviceType_ = DEVICE_TYPE_FILE_SINK;
437 std::vector<AudioChannel> audioChannel = {
438 MONO,
439 STEREO,
440 CHANNEL_3,
441 CHANNEL_4,
442 CHANNEL_5,
443 CHANNEL_6,
444 };
445 uint32_t audioChannelInt = GetData<uint32_t>() % audioChannel.size();
446 uint32_t channelCount = audioChannel[audioChannelInt];
447 std::vector<DeviceType> DeviceTypeVec = {
448 DEVICE_TYPE_FILE_SINK,
449 DEVICE_TYPE_FILE_SOURCE,
450 DEVICE_TYPE_BLUETOOTH_A2DP,
451 };
452 uint32_t deviceTypeInt = GetData<uint32_t>() % DeviceTypeVec.size();
453 DeviceType deviceType = DeviceTypeVec[deviceTypeInt];
454 GetServerPtr()->audioPolicyService_.ReconfigureAudioChannel(channelCount, deviceType);
455 std::shared_ptr<AudioDeviceDescriptor> deviceDescriptor = std::make_shared<AudioDeviceDescriptor>();
456 SourceOutput sourceOutput;
457 GetServerPtr()->audioPolicyService_.audioDeviceStatus_.WriteInputDeviceChangedSysEvents(deviceDescriptor,
458 sourceOutput);
459 }
460
AudioPolicyServiceEnhanceTenFuzzTest()461 void AudioPolicyServiceEnhanceTenFuzzTest()
462 {
463 OHOS::AudioStandard::AudioPolicyServiceEnhanceNineFuzzTest();
464 int32_t notificationId = GetData<int32_t>();
465 GetServerPtr()->audioPolicyService_.audioVolumeManager_.CancelSafeVolumeNotification(notificationId);
466 int64_t activateSessionId = GetData<int64_t>();
467 GetServerPtr()->audioPolicyService_.CheckStreamMode(activateSessionId);
468
469 GetServerPtr()->audioPolicyService_.GetConverterConfig();
470
471 std::string dumpString = "";
472 GetServerPtr()->audioPolicyDump_.DevicesInfoDump(dumpString);
473 GetServerPtr()->audioPolicyDump_.GetMicrophoneDescriptorsDump(dumpString);
474 GetServerPtr()->audioPolicyDump_.AudioPolicyParserDump(dumpString);
475 GetServerPtr()->audioPolicyDump_.XmlParsedDataMapDump(dumpString);
476 GetServerPtr()->audioPolicyDump_.EffectManagerInfoDump(dumpString);
477 GetServerPtr()->audioPolicyDump_.MicrophoneMuteInfoDump(dumpString);
478
479 AudioEffectPropertyArray effectPropertyArray;
480 GetServerPtr()->audioPolicyService_.GetSupportedAudioEffectProperty(effectPropertyArray);
481 GetServerPtr()->audioPolicyService_.GetAudioEffectProperty(effectPropertyArray);
482 GetServerPtr()->audioPolicyService_.SetAudioEffectProperty(effectPropertyArray);
483
484 AudioEnhancePropertyArray enhancePropertyArray;
485 GetServerPtr()->audioPolicyService_.GetAudioEnhanceProperty(enhancePropertyArray);
486 GetServerPtr()->audioPolicyService_.SetAudioEnhanceProperty(enhancePropertyArray);
487
488 uint32_t rotate = GetData<uint32_t>();
489 GetServerPtr()->audioPolicyService_.SetRotationToEffect(rotate);
490
491 uint32_t sessionID = GetData<uint32_t>();
492 DeviceType deviceTypeSet = GetData<DeviceType>();
493 StreamUsage streamUsage = GetData<StreamUsage>();
494 bool isRunning = GetData<bool>();
495 GetServerPtr()->audioPolicyService_.
496 SetDefaultOutputDevice(deviceTypeSet, sessionID, streamUsage, isRunning);
497 GetServerPtr()->audioPolicyService_.audioA2dpOffloadManager_->WaitForConnectionCompleted();
498 }
499
AudioPolicyServiceEnhanceElevenFuzzTest()500 void AudioPolicyServiceEnhanceElevenFuzzTest()
501 {
502 std::string deviceAddress = "deviceAddress";
503 int32_t playingState = GetData<int32_t>();
504 GetServerPtr()->
505 audioPolicyService_.audioA2dpOffloadManager_->OnA2dpPlayingStateChanged(deviceAddress, playingState);
506 std::string deviceAddressEnmpy = "";
507 int32_t playingStateOne = 0;
508 GetServerPtr()->
509 audioPolicyService_.audioA2dpOffloadManager_->OnA2dpPlayingStateChanged(deviceAddressEnmpy, playingStateOne);
510 int32_t playingStateTwo = A2DP_STOPPED;
511 GetServerPtr()->
512 audioPolicyService_.audioA2dpOffloadManager_->OnA2dpPlayingStateChanged(deviceAddressEnmpy, playingStateTwo);
513 int32_t playingStateThree = A2DP_PLAYING;
514 A2dpOffloadConnectionState currentOffloadConnectionState = GetData<A2dpOffloadConnectionState>();
515 GetServerPtr()->audioPolicyService_.audioA2dpOffloadManager_->
516 audioA2dpOffloadFlag_.SetCurrentOffloadConnectedState(currentOffloadConnectionState);
517 GetServerPtr()->
518 audioPolicyService_.audioA2dpOffloadManager_->OnA2dpPlayingStateChanged(deviceAddressEnmpy, playingStateThree);
519 }
520
521 typedef void (*TestFuncs[11])();
522
523 TestFuncs g_testFuncs = {
524 AudioPolicyServiceEnhanceOneFuzzTest,
525 AudioPolicyServiceEnhanceTwoFuzzTest,
526 AudioPolicyServiceEnhanceThreeFuzzTest,
527 AudioPolicyServiceEnhanceFourFuzzTest,
528 AudioPolicyServiceEnhanceFiveFuzzTest,
529 AudioPolicyServiceEnhanceSixFuzzTest,
530 AudioPolicyServiceEnhanceSevenFuzzTest,
531 AudioPolicyServiceEnhanceEightFuzzTest,
532 AudioPolicyServiceEnhanceNineFuzzTest,
533 AudioPolicyServiceEnhanceTenFuzzTest,
534 AudioPolicyServiceEnhanceElevenFuzzTest,
535 };
536
FuzzTest(const uint8_t * rawData,size_t size)537 bool FuzzTest(const uint8_t* rawData, size_t size)
538 {
539 if (rawData == nullptr) {
540 return false;
541 }
542
543 // initialize data
544 RAW_DATA = rawData;
545 g_dataSize = size;
546 g_pos = 0;
547
548 uint32_t code = GetData<uint32_t>();
549 uint32_t len = GetArrLength(g_testFuncs);
550 if (len > 0) {
551 g_testFuncs[code % len]();
552 } else {
553 AUDIO_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
554 }
555
556 return true;
557 }
558 } // namespace AudioStandard
559 } // namesapce OHOS
560
LLVMFuzzerInitialize(int * argc,char *** argv)561 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
562 {
563 OHOS::AudioStandard::AudioFuzzTestGetPermission();
564 return 0;
565 }
566
567 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)568 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
569 {
570 if (size < OHOS::AudioStandard::THRESHOLD) {
571 return 0;
572 }
573
574 OHOS::AudioStandard::FuzzTest(data, size);
575 return 0;
576 }
577