1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <iostream>
17 #include <cstddef>
18 #include <cstdint>
19 #include "audio_info.h"
20 #include "audio_stream_collector.h"
21 #include "message_parcel.h"
22 using namespace std;
23
24 namespace OHOS {
25 namespace AudioStandard {
26 const std::u16string FORMMGR_INTERFACE_TOKEN = u"IAudioPolicy";
27 const int32_t LIMITSIZE = 4;
28
AudioStreamCollectorFuzzTest(const uint8_t * rawData,size_t size)29 void AudioStreamCollectorFuzzTest(const uint8_t *rawData, size_t size)
30 {
31 if (rawData == nullptr || size < LIMITSIZE) {
32 return;
33 }
34
35 MessageParcel data;
36 data.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN);
37 data.WriteBuffer(rawData, size);
38 data.RewindRead(0);
39
40 sptr<IRemoteObject> object = data.ReadRemoteObject();
41 int32_t clientUID = *reinterpret_cast<const int32_t *>(rawData);
42 bool hasBTPermission = *reinterpret_cast<const bool *>(rawData);
43 AudioStreamCollector::GetAudioStreamCollector()
44 .RegisterAudioRendererEventListener(clientUID, object, hasBTPermission);
45 AudioStreamCollector::GetAudioStreamCollector().UnregisterAudioRendererEventListener(clientUID);
46 AudioStreamCollector::GetAudioStreamCollector()
47 .RegisterAudioCapturerEventListener(clientUID, object, hasBTPermission);
48 AudioStreamCollector::GetAudioStreamCollector().UnregisterAudioCapturerEventListener(clientUID);
49
50 int32_t uid = *reinterpret_cast<const int32_t *>(rawData);
51 AudioStreamCollector::GetAudioStreamCollector().RegisteredTrackerClientDied(uid);
52 AudioStreamCollector::GetAudioStreamCollector().RegisteredStreamListenerClientDied(uid);
53
54 int32_t clientUid = *reinterpret_cast<const int32_t *>(rawData);
55 StreamSetStateEventInternal streamSetStateEventInternal = {};
56 streamSetStateEventInternal.streamSetState = *reinterpret_cast<const StreamSetState *>(rawData);
57 streamSetStateEventInternal.audioStreamType = *reinterpret_cast<const AudioStreamType *>(rawData);
58 AudioStreamCollector::GetAudioStreamCollector().
59 UpdateStreamState(clientUid, streamSetStateEventInternal);
60
61 int32_t streamId = *reinterpret_cast<const int32_t *>(rawData);
62 float volume = *reinterpret_cast<const float *>(rawData);
63 AudioStreamCollector::GetAudioStreamCollector().SetLowPowerVolume(streamId, volume);
64 AudioStreamCollector::GetAudioStreamCollector().GetLowPowerVolume(streamId);
65 AudioStreamCollector::GetAudioStreamCollector().GetSingleStreamVolume(streamId);
66 }
67 } // namespace AudioStandard
68 } // namesapce OHOS
69
70 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)71 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
72 {
73 /* Run your code on data */
74 OHOS::AudioStandard::AudioStreamCollectorFuzzTest(data, size);
75 return 0;
76 }
77