diff --git a/test/BUILD.gn b/test/BUILD.gn index c2926b127391248b7490452fcf3a04e88354b2c7..e90eac777b785f3403b86f5e74801a102d0282cc 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -39,6 +39,9 @@ group("audio_fuzz_test") { deps = [ "fuzztest/audiomanager_fuzzer:fuzztest", "fuzztest/audiopolicy_fuzzer:fuzztest", + "fuzztest/audiopolicyanother_fuzzer:fuzztest", "fuzztest/audioserver_fuzzer:fuzztest", + "fuzztest/audioserverbalance_fuzzer:fuzztest", + "fuzztest/audiostreamcollector_fuzzer:fuzztest", ] } diff --git a/test/fuzztest/audiomanager_fuzzer/audio_manager_fuzzer.cpp b/test/fuzztest/audiomanager_fuzzer/audio_manager_fuzzer.cpp index d3c94e38f7a2990adca00fae010f644bee868cc2..8392be84ff7a92f7727871a3cd2cf27a54669ee0 100644 --- a/test/fuzztest/audiomanager_fuzzer/audio_manager_fuzzer.cpp +++ b/test/fuzztest/audiomanager_fuzzer/audio_manager_fuzzer.cpp @@ -34,7 +34,6 @@ const int32_t LIMITSIZE = 4; void AudioManagerFuzzTest(const uint8_t* data, size_t size) { if ((data == nullptr) || (size < LIMITSIZE)) { - std::cout << "Invalid data" << std::endl; return; } @@ -57,7 +56,6 @@ void AudioManagerFuzzTest(const uint8_t* data, size_t size) void AudioStreamManagerFuzzTest(const uint8_t* data, size_t size) { if ((data == nullptr) || (size < LIMITSIZE)) { - std::cout << "Invalid data" << std::endl; return; } diff --git a/test/fuzztest/audiopolicy_fuzzer/BUILD.gn b/test/fuzztest/audiopolicy_fuzzer/BUILD.gn index 46dcd82043927dc3caa9a53a89842edfd801fd93..8af7ed373b0d0aa91a0504a60c1286caf59b549e 100644 --- a/test/fuzztest/audiopolicy_fuzzer/BUILD.gn +++ b/test/fuzztest/audiopolicy_fuzzer/BUILD.gn @@ -35,7 +35,6 @@ ohos_fuzztest("AudioPolicyFuzzTest") { deps = [ "$hdf_uhdf_path/hdi:libhdi", "//foundation/barrierfree/accessibility/interfaces/innerkits/acfwk:accessibilityconfig", - "//foundation/multimedia/audio_framework/services/audio_policy:audio_policy_client", "//foundation/multimedia/audio_framework/services/audio_policy:audio_policy_service", ] external_deps = [ diff --git a/test/fuzztest/audiopolicy_fuzzer/audio_policy_fuzzer.cpp b/test/fuzztest/audiopolicy_fuzzer/audio_policy_fuzzer.cpp index d2945f2092303f8f6c60dd7e2d44593fca0384a5..3f3810cfc3054fdaa1fc6f56d7c039d98ca1a4e6 100644 --- a/test/fuzztest/audiopolicy_fuzzer/audio_policy_fuzzer.cpp +++ b/test/fuzztest/audiopolicy_fuzzer/audio_policy_fuzzer.cpp @@ -14,48 +14,55 @@ */ #include -#include "audio_policy_server.h" #include #include +#include "audio_policy_server.h" #include "message_parcel.h" using namespace std; namespace OHOS { - constexpr int32_t OFFSET = 4; - const std::u16string FORMMGR_INTERFACE_TOKEN = u"IAudioPolicy"; - const int32_t SYSTEM_ABILITY_ID = 3009; - const bool RUN_ON_CREATE = false; - const int32_t LIMITSIZE = 4; - namespace AudioStandard { - uint32_t Convert2Uint32(const uint8_t *ptr) - { - if (ptr == nullptr) { - return 0; - } - // 将第0个数字左移24位,将第1个数字左移16位,将第2个数字左移8位,第3个数字不左移 - return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]); - } - void AudioPolicyFuzzTest(const uint8_t *rawData, size_t size) - { - if (rawData == nullptr || size < LIMITSIZE) { - std::cout << "Invalid data" << std::endl; - return; - } - uint32_t code = Convert2Uint32(rawData); - rawData = rawData + OFFSET; - size = size - OFFSET; +namespace AudioStandard { +constexpr int32_t OFFSET = 4; +const std::u16string FORMMGR_INTERFACE_TOKEN = u"IAudioPolicy"; +const int32_t SYSTEM_ABILITY_ID = 3009; +const bool RUN_ON_CREATE = false; +const int32_t LIMITSIZE = 4; +const int32_t SHIFT_LEFT_8 = 8; +const int32_t SHIFT_LEFT_16 = 16; +const int32_t SHIFT_LEFT_24 = 24; + +uint32_t Convert2Uint32(const uint8_t *ptr) +{ + if (ptr == nullptr) { + return 0; + } + /* Move the 0th digit to the left by 24 bits, the 1st digit to the left by 16 bits, + the 2nd digit to the left by 8 bits, and the 3rd digit not to the left */ + return (ptr[0] << SHIFT_LEFT_24) | (ptr[1] << SHIFT_LEFT_16) | (ptr[2] << SHIFT_LEFT_8) | (ptr[3]); +} - MessageParcel data; - data.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN); - data.WriteBuffer(rawData, size); - data.RewindRead(0); - MessageParcel reply; - MessageOption option; - std::shared_ptr AudioPolicyServerPtr = - std::make_shared(SYSTEM_ABILITY_ID, RUN_ON_CREATE); - AudioPolicyServerPtr->OnRemoteRequest(code, data, reply, option); - } - } // namespace AudioStandard +void AudioPolicyFuzzTest(const uint8_t *rawData, size_t size) +{ + if (rawData == nullptr || size < LIMITSIZE) { + return; + } + uint32_t code = Convert2Uint32(rawData); + rawData = rawData + OFFSET; + size = size - OFFSET; + + MessageParcel data; + data.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN); + data.WriteBuffer(rawData, size); + data.RewindRead(0); + + MessageParcel reply; + MessageOption option; + std::shared_ptr AudioPolicyServerPtr = + std::make_shared(SYSTEM_ABILITY_ID, RUN_ON_CREATE); + + AudioPolicyServerPtr->OnRemoteRequest(code, data, reply, option); +} +} // namespace AudioStandard } // namesapce OHOS /* Fuzzer entry point */ diff --git a/test/fuzztest/audiopolicyanother_fuzzer/BUILD.gn b/test/fuzztest/audiopolicyanother_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..e0cc0ff8117bdcb7d8548e2d0953d6db557f0771 --- /dev/null +++ b/test/fuzztest/audiopolicyanother_fuzzer/BUILD.gn @@ -0,0 +1,55 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/config/features.gni") +import("//build/test.gni") +import("//drivers/hdf_core/adapter/uhdf2/uhdf.gni") + +ohos_fuzztest("AudioPolicyAnotherFuzzTest") { + module_out_path = "multimedia_audio_framework/audiopolicyanother_fuzzer" + fuzz_config_file = "//foundation/multimedia/audio_framework/test/fuzztest/audiopolicyanother_fuzzer" + + include_dirs = [ "//foundation/multimedia/audio_framework/services/audio_policy/server/include" ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + configs = [ "//foundation/multimedia/audio_framework/services/audio_policy:audio_policy_public_config" ] + + sources = [ "audio_policy_another_fuzzer.cpp" ] + + deps = [ + "$hdf_uhdf_path/hdi:libhdi", + "//foundation/barrierfree/accessibility/interfaces/innerkits/acfwk:accessibilityconfig", + "//foundation/multimedia/audio_framework/services/audio_policy:audio_policy_service", + ] + external_deps = [ + "ability_base:want", + "access_token:libaccesstoken_sdk", + "access_token:libtoken_setproc", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + ] +} + +group("fuzztest") { + testonly = true + deps = [ ":AudioPolicyAnotherFuzzTest" ] +} diff --git a/test/fuzztest/audiopolicyanother_fuzzer/audio_policy_another_fuzzer.cpp b/test/fuzztest/audiopolicyanother_fuzzer/audio_policy_another_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ff8876960b0b08d4c0054e2caed28a0ddb132c5d --- /dev/null +++ b/test/fuzztest/audiopolicyanother_fuzzer/audio_policy_another_fuzzer.cpp @@ -0,0 +1,182 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include "audio_info.h" +#include "audio_policy_server.h" +#include "message_parcel.h" + +using namespace std; + +namespace OHOS { +namespace AudioStandard { +const std::u16string FORMMGR_INTERFACE_TOKEN = u"IAudioPolicy"; +const int32_t SYSTEM_ABILITY_ID = 3009; +const bool RUN_ON_CREATE = false; +const int32_t LIMITSIZE = 4; + +void AudioVolumeFuzzTest(const uint8_t *rawData, size_t size) +{ + if (rawData == nullptr || size < LIMITSIZE) { + return; + } + std::shared_ptr AudioPolicyServerPtr = + std::make_shared(SYSTEM_ABILITY_ID, RUN_ON_CREATE); + + AudioStreamType streamType = *reinterpret_cast(rawData); + float volume = *reinterpret_cast(rawData); + int32_t streamId = *reinterpret_cast(rawData); + bool mute = *reinterpret_cast(rawData); + AudioPolicyServerPtr->SetStreamVolume(streamType, volume); + AudioPolicyServerPtr->GetStreamVolume(streamType); + AudioPolicyServerPtr->SetLowPowerVolume(streamId, volume); + AudioPolicyServerPtr->GetLowPowerVolume(streamId); + AudioPolicyServerPtr->GetSingleStreamVolume(streamId); + AudioPolicyServerPtr->SetStreamMute(streamType, mute); + AudioPolicyServerPtr->GetStreamMute(streamType); + AudioPolicyServerPtr->IsStreamActive(streamType); +} + +void AudioDeviceFuzzTest(const uint8_t *rawData, size_t size) +{ + if (rawData == nullptr || size < LIMITSIZE) { + return; + } + std::shared_ptr AudioPolicyServerPtr = + std::make_shared(SYSTEM_ABILITY_ID, RUN_ON_CREATE); + + MessageParcel data; + data.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN); + data.WriteBuffer(rawData, size); + data.RewindRead(0); + + InternalDeviceType deviceType = *reinterpret_cast(rawData); + bool active = *reinterpret_cast(rawData); + AudioPolicyServerPtr->SetDeviceActive(deviceType, active); + AudioPolicyServerPtr->IsDeviceActive(deviceType); + + AudioRingerMode ringMode = *reinterpret_cast(rawData); + AudioPolicyServerPtr->SetRingerMode(ringMode); + + int32_t ltonetype = *reinterpret_cast(rawData); + AudioPolicyServerPtr->GetToneConfig(ltonetype); + + AudioScene audioScene = *reinterpret_cast(rawData); + AudioPolicyServerPtr->SetAudioScene(audioScene); + + bool mute = *reinterpret_cast(rawData); + AudioPolicyServerPtr->SetMicrophoneMute(mute); + + int32_t clientId = *reinterpret_cast(rawData); + sptr object = data.ReadRemoteObject(); + AudioPolicyServerPtr->SetRingerModeCallback(clientId, object); + AudioPolicyServerPtr->UnsetRingerModeCallback(clientId); + + DeviceFlag flag = *reinterpret_cast(rawData); + AudioPolicyServerPtr->SetDeviceChangeCallback(clientId, flag, object); + AudioPolicyServerPtr->UnsetDeviceChangeCallback(clientId); +} + +void AudioInterruptFuzzTest(const uint8_t *rawData, size_t size) +{ + if (rawData == nullptr || size < LIMITSIZE) { + return; + } + std::shared_ptr AudioPolicyServerPtr = + std::make_shared(SYSTEM_ABILITY_ID, RUN_ON_CREATE); + + MessageParcel data; + data.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN); + data.WriteBuffer(rawData, size); + data.RewindRead(0); + + sptr object = data.ReadRemoteObject(); + uint32_t sessionID = *reinterpret_cast(rawData); + AudioPolicyServerPtr->SetAudioInterruptCallback(sessionID, object); + AudioPolicyServerPtr->UnsetAudioInterruptCallback(sessionID); + + uint32_t clientID = *reinterpret_cast(rawData); + AudioPolicyServerPtr->SetAudioManagerInterruptCallback(clientID, object); + + AudioInterrupt audioInterrupt; + audioInterrupt.contentType = *reinterpret_cast(rawData); + audioInterrupt.streamUsage = *reinterpret_cast(rawData); + audioInterrupt.streamType = *reinterpret_cast(rawData); + AudioPolicyServerPtr->RequestAudioFocus(clientID, audioInterrupt); + AudioPolicyServerPtr->AbandonAudioFocus(clientID, audioInterrupt); +} + +void AudioPolicyFuzzTest(const uint8_t *rawData, size_t size) +{ + if (rawData == nullptr || size < LIMITSIZE) { + return; + } + std::shared_ptr AudioPolicyServerPtr = + std::make_shared(SYSTEM_ABILITY_ID, RUN_ON_CREATE); + + MessageParcel data; + data.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN); + data.WriteBuffer(rawData, size); + data.RewindRead(0); + + sptr object = data.ReadRemoteObject(); + int32_t clientPid = *reinterpret_cast(rawData); + AudioPolicyServerPtr->SetVolumeKeyEventCallback(clientPid, object); + AudioPolicyServerPtr->UnsetVolumeKeyEventCallback(clientPid); + + uint32_t sessionID = *reinterpret_cast(rawData); + AudioPolicyServerPtr->OnSessionRemoved(sessionID); + + int32_t clientUID = *reinterpret_cast(rawData); + AudioPolicyServerPtr->RegisterAudioRendererEventListener(clientUID, object); + AudioPolicyServerPtr->UnregisterAudioRendererEventListener(clientUID); + AudioPolicyServerPtr->RegisterAudioCapturerEventListener(clientUID, object); + AudioPolicyServerPtr->UnregisterAudioCapturerEventListener(clientUID); + + AudioPolicyServer::DeathRecipientId id = + *reinterpret_cast(rawData); + AudioPolicyServerPtr->RegisterClientDeathRecipient(object, id); + + int pid = *reinterpret_cast(rawData); + AudioPolicyServerPtr->RegisteredTrackerClientDied(pid); + + AudioStreamInfo audioStreamInfo = {}; + audioStreamInfo.samplingRate = *reinterpret_cast(rawData); + audioStreamInfo.channels = *reinterpret_cast(rawData); + audioStreamInfo.format = *reinterpret_cast(rawData); + audioStreamInfo.encoding = *reinterpret_cast(rawData); + AudioPolicyServerPtr->IsAudioRendererLowLatencySupported(audioStreamInfo); + + int32_t clientUid = *reinterpret_cast(rawData); + StreamSetState streamSetState = *reinterpret_cast(rawData); + AudioStreamType audioStreamType = *reinterpret_cast(rawData); + AudioPolicyServerPtr->UpdateStreamState(clientUid, streamSetState, audioStreamType); +} +} // namespace AudioStandard +} // namesapce OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + OHOS::AudioStandard::AudioVolumeFuzzTest(data, size); + OHOS::AudioStandard::AudioDeviceFuzzTest(data, size); + OHOS::AudioStandard::AudioInterruptFuzzTest(data, size); + OHOS::AudioStandard::AudioPolicyFuzzTest(data, size); + return 0; +} + diff --git a/test/fuzztest/audiopolicyanother_fuzzer/project.xml b/test/fuzztest/audiopolicyanother_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..6e8ad2cfde8f8bda4beb6cabbe7efd8bc3c54eec --- /dev/null +++ b/test/fuzztest/audiopolicyanother_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/audioserver_fuzzer/BUILD.gn b/test/fuzztest/audioserver_fuzzer/BUILD.gn index 4e76b26a35b387da36ab2fbbabb9447baabde08c..63f0f75e9f67a109149ee9543fc393ddcf8e01a0 100644 --- a/test/fuzztest/audioserver_fuzzer/BUILD.gn +++ b/test/fuzztest/audioserver_fuzzer/BUILD.gn @@ -29,16 +29,12 @@ ohos_fuzztest("AudioServerFuzzTest") { "-fno-omit-frame-pointer", ] - sources = [ - "audio_server_balance_fuzzer.cpp", - "audio_server_fuzzer.cpp", - ] + sources = [ "audio_server_fuzzer.cpp" ] configs = [ "//foundation/multimedia/audio_framework/services/audio_service:audio_service_config" ] deps = [ "$hdf_uhdf_path/hdi:libhdi", - "//foundation/multimedia/audio_framework/services/audio_service:audio_client", "//foundation/multimedia/audio_framework/services/audio_service:audio_service", ] external_deps = [ diff --git a/test/fuzztest/audioserver_fuzzer/audio_server_fuzzer.cpp b/test/fuzztest/audioserver_fuzzer/audio_server_fuzzer.cpp index 5be1386fbae0aedf762c47c937cf49eae3ab60f4..79936c2c07f9700589d02ff17ef3c258d29ba166 100644 --- a/test/fuzztest/audioserver_fuzzer/audio_server_fuzzer.cpp +++ b/test/fuzztest/audioserver_fuzzer/audio_server_fuzzer.cpp @@ -14,54 +14,60 @@ */ #include -#include "audio_server.h" #include #include +#include "audio_server.h" #include "message_parcel.h" - using namespace std; namespace OHOS { +namespace AudioStandard { constexpr int32_t OFFSET = 4; - const std::u16string FORMMGR_INTERFACE_TOKEN = u"IStandardAudioService"; - const int32_t SYSTEM_ABILITY_ID = 3001; - const bool RUN_ON_CREATE = false; - namespace AudioStandard { - uint32_t Convert2Uint32(const uint8_t *ptr) - { - if (ptr == nullptr) { - return 0; - } - // 将第0个数字左移24位,将第1个数字左移16位,将第2个数字左移8位,第3个数字不左移 - return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]); - } - void AudioServerFuzzTest(const uint8_t *rawData, size_t size) - { - if (rawData == nullptr) { - std::cout << "Invalid data" << std::endl; - return; - } - uint32_t code = Convert2Uint32(rawData); - rawData = rawData + OFFSET; - size = size - OFFSET; +const std::u16string FORMMGR_INTERFACE_TOKEN = u"IStandardAudioService"; +const int32_t SYSTEM_ABILITY_ID = 3001; +const bool RUN_ON_CREATE = false; +const int32_t LIMITSIZE = 4; +const int32_t SHIFT_LEFT_8 = 8; +const int32_t SHIFT_LEFT_16 = 16; +const int32_t SHIFT_LEFT_24 = 24; + +uint32_t Convert2Uint32(const uint8_t *ptr) +{ + if (ptr == nullptr) { + return 0; + } + /* Move the 0th digit to the left by 24 bits, the 1st digit to the left by 16 bits, + the 2nd digit to the left by 8 bits, and the 3rd digit not to the left */ + return (ptr[0] << SHIFT_LEFT_24) | (ptr[1] << SHIFT_LEFT_16) | (ptr[2] << SHIFT_LEFT_8) | (ptr[3]); +} + +void AudioServerFuzzTest(const uint8_t *rawData, size_t size) +{ + if (rawData == nullptr || size < LIMITSIZE) { + return; + } + uint32_t code = Convert2Uint32(rawData); + rawData = rawData + OFFSET; + size = size - OFFSET; + + MessageParcel data; + data.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN); + data.WriteBuffer(rawData, size); + data.RewindRead(0); + MessageParcel reply; + MessageOption option; - MessageParcel data; - data.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN); - data.WriteBuffer(rawData, size); - data.RewindRead(0); - MessageParcel reply; - MessageOption option; - std::shared_ptr AudioServerPtr = - std::make_shared(SYSTEM_ABILITY_ID, RUN_ON_CREATE); - AudioServerPtr->OnRemoteRequest(code, data, reply, option); - } - } // namespace AudioStandard + std::shared_ptr AudioServerPtr = + std::make_shared(SYSTEM_ABILITY_ID, RUN_ON_CREATE); + AudioServerPtr->OnRemoteRequest(code, data, reply, option); +} +} // namespace AudioStandard } // namesapce OHOS /* Fuzzer entry point */ -extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { /* Run your code on data */ OHOS::AudioStandard::AudioServerFuzzTest(data, size); return 0; -} +} \ No newline at end of file diff --git a/test/fuzztest/audioserverbalance_fuzzer/BUILD.gn b/test/fuzztest/audioserverbalance_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..6a8ad2ffcac8bf253e3798dcbc4cbc4e5d53e667 --- /dev/null +++ b/test/fuzztest/audioserverbalance_fuzzer/BUILD.gn @@ -0,0 +1,51 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/config/features.gni") +import("//build/test.gni") +import("//drivers/hdf_core/adapter/uhdf2/uhdf.gni") + +ohos_fuzztest("AudioServerBalanceFuzzTest") { + module_out_path = "multimedia_audio_framework/audioserverbalance_fuzzer" + fuzz_config_file = "//foundation/multimedia/audio_framework/test/fuzztest/audioserverbalance_fuzzer" + + include_dirs = [ "//foundation/multimedia/audio_framework/services/audio_service/server/include" ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + + sources = [ "audio_server_balance_fuzzer.cpp" ] + + configs = [ "//foundation/multimedia/audio_framework/services/audio_service:audio_service_config" ] + + deps = [ + "$hdf_uhdf_path/hdi:libhdi", + "//foundation/multimedia/audio_framework/services/audio_service:audio_service", + ] + external_deps = [ + "access_token:libaccesstoken_sdk", + "c_utils:utils", + "ipc:ipc_core", + "power_manager:powermgr_client", + "safwk:system_ability_fwk", + ] +} + +group("fuzztest") { + testonly = true + deps = [ ":AudioServerBalanceFuzzTest" ] +} diff --git a/test/fuzztest/audioserver_fuzzer/audio_server_balance_fuzzer.cpp b/test/fuzztest/audioserverbalance_fuzzer/audio_server_balance_fuzzer.cpp similarity index 31% rename from test/fuzztest/audioserver_fuzzer/audio_server_balance_fuzzer.cpp rename to test/fuzztest/audioserverbalance_fuzzer/audio_server_balance_fuzzer.cpp index 41397641aad622a13a17483b5d2bcfd7f57fe0de..96bed59a4ebe03dc217d4eee8e6f5a8d17e67fa1 100644 --- a/test/fuzztest/audioserver_fuzzer/audio_server_balance_fuzzer.cpp +++ b/test/fuzztest/audioserverbalance_fuzzer/audio_server_balance_fuzzer.cpp @@ -1,83 +1,80 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include - -#include "audio_manager_base.h" -#include "audio_server.h" -#include "message_parcel.h" - -using namespace std; - -namespace OHOS { - const int32_t SYSTEM_ABILITY_ID = 3001; - const bool RUN_ON_CREATE = false; - namespace AudioStandard { - float Convert2Float(const uint8_t *ptr) - { - // 根据ptr的大小随机生成区间[-1, +1]内的float值 - float floatValue = static_cast(*ptr); - return floatValue / 128.0f - 1.0f; - } - - void AudioServerBalanceFuzzer(const uint8_t *rawData, size_t size, std::shared_ptr AudioServerPtr) - { - float balanceValue = Convert2Float(rawData); - MessageParcel data; - data.WriteFloat(balanceValue); - MessageParcel reply; - MessageOption option; - AudioServerPtr->OnRemoteRequest(AudioManagerStub::SET_AUDIO_BALANCE_VALUE, data, reply, option); - } - - bool Convert2Bool(const uint8_t *ptr) - { - // 根据ptr的值随机生成bool值 - return (ptr[0] & 1) ? true : false; - } - - void AudioServerMonoFuzzer(const uint8_t *rawData, size_t size, std::shared_ptr AudioServerPtr) - { - bool monoState = Convert2Bool(rawData); - MessageParcel data; - data.WriteBool(monoState); - MessageParcel reply; - MessageOption option; - AudioServerPtr->OnRemoteRequest(AudioManagerStub::SET_AUDIO_MONO_STATE, data, reply, option); - } - - void AudioServerBalanceFuzzTest(const uint8_t *rawData, size_t size) - { - if (rawData == nullptr) { - std::cout << "Invalid data" << std::endl; - return; - } - std::shared_ptr AudioServerPtr = - std::make_shared(SYSTEM_ABILITY_ID, RUN_ON_CREATE); - AudioServerBalanceFuzzer(rawData, size, AudioServerPtr); - AudioServerMonoFuzzer(rawData, size, AudioServerPtr); - } - } // namespace AudioStandard -} // namesapce OHOS - -/* Fuzzer entry point */ -extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) -{ - /* Run your code on data */ - OHOS::AudioStandard::AudioServerBalanceFuzzTest(data, size); - return 0; -} +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include "audio_manager_base.h" +#include "audio_server.h" +#include "message_parcel.h" +using namespace std; + +namespace OHOS { +namespace AudioStandard { +const int32_t SYSTEM_ABILITY_ID = 3001; +const bool RUN_ON_CREATE = false; +const int32_t LIMITSIZE = 4; + +float Convert2Float(const uint8_t *ptr) +{ + float floatValue = static_cast(*ptr); + return floatValue / 128.0f - 1.0f; +} + +void AudioServerBalanceFuzzer(const uint8_t *rawData, size_t size, std::shared_ptr AudioServerPtr) +{ + float balanceValue = Convert2Float(rawData); + MessageParcel data; + data.WriteFloat(balanceValue); + MessageParcel reply; + MessageOption option; + AudioServerPtr->OnRemoteRequest(AudioManagerStub::SET_AUDIO_BALANCE_VALUE, data, reply, option); +} + +bool Convert2Bool(const uint8_t *ptr) +{ + return (ptr[0] & 1) ? true : false; +} + +void AudioServerMonoFuzzer(const uint8_t *rawData, size_t size, std::shared_ptr AudioServerPtr) +{ + bool monoState = Convert2Bool(rawData); + MessageParcel data; + data.WriteBool(monoState); + MessageParcel reply; + MessageOption option; + AudioServerPtr->OnRemoteRequest(AudioManagerStub::SET_AUDIO_MONO_STATE, data, reply, option); +} + +void AudioServerBalanceFuzzTest(const uint8_t *rawData, size_t size) +{ + if (rawData == nullptr || size < LIMITSIZE) { + return; + } + std::shared_ptr AudioServerPtr = + std::make_shared(SYSTEM_ABILITY_ID, RUN_ON_CREATE); + AudioServerBalanceFuzzer(rawData, size, AudioServerPtr); + AudioServerMonoFuzzer(rawData, size, AudioServerPtr); +} +} // namespace AudioStandard +} // namesapce OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + OHOS::AudioStandard::AudioServerBalanceFuzzTest(data, size); + return 0; +} diff --git a/test/fuzztest/audioserverbalance_fuzzer/project.xml b/test/fuzztest/audioserverbalance_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..6e8ad2cfde8f8bda4beb6cabbe7efd8bc3c54eec --- /dev/null +++ b/test/fuzztest/audioserverbalance_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/audiostreamcollector_fuzzer/BUILD.gn b/test/fuzztest/audiostreamcollector_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..9ccb7fba5d06de7ad047e0adfdf2920bdf02d7f3 --- /dev/null +++ b/test/fuzztest/audiostreamcollector_fuzzer/BUILD.gn @@ -0,0 +1,55 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/config/features.gni") +import("//build/test.gni") +import("//drivers/hdf_core/adapter/uhdf2/uhdf.gni") + +ohos_fuzztest("AudioStreamCollectorFuzzTest") { + module_out_path = "multimedia_audio_framework/audiostreamcollector_fuzzer" + fuzz_config_file = "//foundation/multimedia/audio_framework/test/fuzztest/audiostreamcollector_fuzzer" + + include_dirs = [ "//foundation/multimedia/audio_framework/services/audio_policy/server/include" ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + configs = [ "//foundation/multimedia/audio_framework/services/audio_policy:audio_policy_public_config" ] + + sources = [ "audio_stream_collector_fuzzer.cpp" ] + + deps = [ + "$hdf_uhdf_path/hdi:libhdi", + "//foundation/barrierfree/accessibility/interfaces/innerkits/acfwk:accessibilityconfig", + "//foundation/multimedia/audio_framework/services/audio_policy:audio_policy_service", + ] + external_deps = [ + "ability_base:want", + "access_token:libaccesstoken_sdk", + "access_token:libtoken_setproc", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + ] +} + +group("fuzztest") { + testonly = true + deps = [ ":AudioStreamCollectorFuzzTest" ] +} diff --git a/test/fuzztest/audiostreamcollector_fuzzer/audio_stream_collector_fuzzer.cpp b/test/fuzztest/audiostreamcollector_fuzzer/audio_stream_collector_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..674ea1939bbb8f164474725bb26fa284eab8f086 --- /dev/null +++ b/test/fuzztest/audiostreamcollector_fuzzer/audio_stream_collector_fuzzer.cpp @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include "audio_info.h" +#include "audio_stream_collector.h" +#include "message_parcel.h" +using namespace std; + +namespace OHOS { +namespace AudioStandard { +const std::u16string FORMMGR_INTERFACE_TOKEN = u"IAudioPolicy"; +const int32_t LIMITSIZE = 4; + +void AudioStreamCollectorFuzzTest(const uint8_t *rawData, size_t size) +{ + if (rawData == nullptr || size < LIMITSIZE) { + return; + } + + MessageParcel data; + data.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN); + data.WriteBuffer(rawData, size); + data.RewindRead(0); + + sptr object = data.ReadRemoteObject(); + int32_t clientUID = *reinterpret_cast(rawData); + bool hasBTPermission = *reinterpret_cast(rawData); + AudioStreamCollector::GetAudioStreamCollector() + .RegisterAudioRendererEventListener(clientUID, object, hasBTPermission); + AudioStreamCollector::GetAudioStreamCollector().UnregisterAudioRendererEventListener(clientUID); + AudioStreamCollector::GetAudioStreamCollector() + .RegisterAudioCapturerEventListener(clientUID, object, hasBTPermission); + AudioStreamCollector::GetAudioStreamCollector().UnregisterAudioCapturerEventListener(clientUID); + + int32_t uid = *reinterpret_cast(rawData); + AudioStreamCollector::GetAudioStreamCollector().RegisteredTrackerClientDied(uid); + AudioStreamCollector::GetAudioStreamCollector().RegisteredStreamListenerClientDied(uid); + + int32_t clientUid = *reinterpret_cast(rawData); + StreamSetStateEventInternal streamSetStateEventInternal = {}; + streamSetStateEventInternal.streamSetState = *reinterpret_cast(rawData); + streamSetStateEventInternal.audioStreamType = *reinterpret_cast(rawData); + AudioStreamCollector::GetAudioStreamCollector(). + UpdateStreamState(clientUid, streamSetStateEventInternal); + + int32_t streamId = *reinterpret_cast(rawData); + float volume = *reinterpret_cast(rawData); + AudioStreamCollector::GetAudioStreamCollector().SetLowPowerVolume(streamId, volume); + AudioStreamCollector::GetAudioStreamCollector().GetLowPowerVolume(streamId); + AudioStreamCollector::GetAudioStreamCollector().GetSingleStreamVolume(streamId); +} +} // namespace AudioStandard +} // namesapce OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + OHOS::AudioStandard::AudioStreamCollectorFuzzTest(data, size); + return 0; +} diff --git a/test/fuzztest/audiostreamcollector_fuzzer/project.xml b/test/fuzztest/audiostreamcollector_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..6e8ad2cfde8f8bda4beb6cabbe7efd8bc3c54eec --- /dev/null +++ b/test/fuzztest/audiostreamcollector_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + +