diff --git a/frameworks/native/session/test/fuzztest/avsessioncontrollerstub_fuzzer/avsession_controllerstub_fuzzer.cpp b/frameworks/native/session/test/fuzztest/avsessioncontrollerstub_fuzzer/avsession_controllerstub_fuzzer.cpp index 6216f7e618352630d39fb0c68b8d01b289dd9beb..ff97c81c2695e720fa74606a7b45b7b74aa57e13 100644 --- a/frameworks/native/session/test/fuzztest/avsessioncontrollerstub_fuzzer/avsession_controllerstub_fuzzer.cpp +++ b/frameworks/native/session/test/fuzztest/avsessioncontrollerstub_fuzzer/avsession_controllerstub_fuzzer.cpp @@ -31,43 +31,77 @@ using namespace std; using namespace OHOS; using namespace OHOS::AVSession; +class AVSessionControllerStubDemo; -static constexpr int32_t MAX_CODE_TEST = 5; +static constexpr int32_t MAX_CODE_TEST = 21; static constexpr int32_t MAX_CODE_LEN = 512; static constexpr int32_t MIN_SIZE_NUM = 4; +shared_ptr g_AVSessionControllerStubDemo(nullptr); -void AvSessionControllerFuzzer::FuzzOnRemoteRequest(const uint8_t* data, size_t size) -{ - if ((data == nullptr) || (size > MAX_CODE_LEN) || (size < MIN_SIZE_NUM)) { - return; - } - uint32_t code = *(reinterpret_cast(data)); - if (code >= MAX_CODE_TEST) { - return; - } - AVSessionDescriptor descriptor; - sptr testAVSessionItem = new(std::nothrow) AVSessionItem(descriptor); - if (!testAVSessionItem) { - SLOGI("testAVSessionController is null"); - return; - } - int32_t pid = *(reinterpret_cast(data)); - sptr avControllerItem = new(std::nothrow) AVControllerItem(pid, testAVSessionItem); +class AVSessionControllerStubDemo : public AVSessionControllerStub { +public: + int32_t GetAVCallState(AVCallState &avCallState) override { return isSuccess ? AVSESSION_SUCCESS : 0; }; + int32_t GetAVCallMetaData(AVCallMetaData &avCallMetaData) override { return isSuccess ? AVSESSION_SUCCESS : 0; }; + int32_t GetAVPlaybackState(AVPlaybackState &state) override { return isSuccess ? AVSESSION_SUCCESS : 0; }; + int32_t GetAVMetaData(AVMetaData &data) override { return isSuccess ? AVSESSION_SUCCESS : 0; }; + int32_t SendAVKeyEvent(const OHOS::MMI::KeyEvent &keyEvent) override { return isSuccess ? AVSESSION_SUCCESS : 0; }; + int32_t GetLaunchAbility(OHOS::AbilityRuntime::WantAgent::WantAgent &ability) override + { + return isSuccess ? AVSESSION_SUCCESS : 0; + }; + int32_t GetValidCommands(std::vector &cmds) override { return isSuccess ? AVSESSION_SUCCESS : 0; }; + int32_t IsSessionActive(bool &isActive) override { return isSuccess ? AVSESSION_SUCCESS : 0; }; + int32_t SendControlCommand(const AVControlCommand &cmd) override { return isSuccess ? AVSESSION_SUCCESS : 0; }; + int32_t SendCommonCommand(const std::string &commonCommand, const OHOS::AAFwk::WantParams &commandArgs) override + { + return isSuccess ? AVSESSION_SUCCESS : 0; + }; + int32_t RegisterCallback(const std::shared_ptr &callback) override + { + return isSuccess ? AVSESSION_SUCCESS : 0; + }; + int32_t SetAVCallMetaFilter(const AVCallMetaData::AVCallMetaMaskType &filter) override + { + return isSuccess ? AVSESSION_SUCCESS : 0; + }; + int32_t SetAVCallStateFilter(const AVCallState::AVCallStateMaskType &filter) override + { + return isSuccess ? AVSESSION_SUCCESS : 0; + }; + int32_t SetMetaFilter(const AVMetaData::MetaMaskType &filter) override + { + return isSuccess ? AVSESSION_SUCCESS : 0; + }; + int32_t SetPlaybackFilter(const AVPlaybackState::PlaybackStateMaskType &filter) override + { + return isSuccess ? AVSESSION_SUCCESS : 0; + }; + int32_t GetAVQueueItems(std::vector &items) override { return isSuccess ? AVSESSION_SUCCESS : 0; }; + int32_t GetAVQueueTitle(std::string &title) override { return isSuccess ? AVSESSION_SUCCESS : 0; }; + int32_t SkipToQueueItem(int32_t &itemId) override { return isSuccess ? AVSESSION_SUCCESS : 0; }; + int32_t GetExtras(OHOS::AAFwk::WantParams &extras) override { return isSuccess ? AVSESSION_SUCCESS : 0; }; + int32_t Destroy() override { return isSuccess ? AVSESSION_SUCCESS : 0; }; + std::string GetSessionId() override { return ""; }; + int64_t GetRealPlaybackPosition() override { return isSuccess ? AVSESSION_SUCCESS : 0; }; + bool IsDestroy() override { return true; }; + int32_t RegisterCallbackInner(const OHOS::sptr &callback) override + { + return isSuccess ? AVSESSION_SUCCESS : 0; + }; + bool isSuccess = true; +}; - if (!avControllerItem) { - SLOGI("aVSessionController is null"); - return; - } +void AvSessionControllerFuzzer::FuzzOnRemoteRequest(int32_t code, const uint8_t* data, size_t size) +{ MessageParcel dataMessageParcel; MessageParcel reply; MessageOption option; - if (!dataMessageParcel.WriteInterfaceToken(avControllerItem->GetDescriptor())) { - return; - } + size -= sizeof(uint32_t); dataMessageParcel.WriteBuffer(data + sizeof(uint32_t), size); dataMessageParcel.RewindRead(0); - int32_t ret = avControllerItem->OnRemoteRequest(code, dataMessageParcel, reply, option); + dataMessageParcel.WriteInterfaceToken(OHOS::AVSession::IAVControllerCallback::GetDescriptor()); + int32_t ret = g_AVSessionControllerStubDemo->OnRemoteRequest(code, dataMessageParcel, reply, option); if (ret == 0) { SLOGI("OnRemoteRequest ERR_NONE"); } @@ -75,18 +109,6 @@ void AvSessionControllerFuzzer::FuzzOnRemoteRequest(const uint8_t* data, size_t void AvSessionControllerFuzzer::FuzzTests(const uint8_t* data, size_t size) { - if ((data == nullptr) || (size > MAX_CODE_LEN) || (size < MIN_SIZE_NUM)) { - return; - } - AVSessionDescriptor descriptor; - sptr testAVSessionItem = new(std::nothrow) AVSessionItem(descriptor); - if (!testAVSessionItem) { - SLOGI("testAVSessionItem is null"); - return; - } - int32_t pid = *(reinterpret_cast(data)); - sptr avControllerItem = new(std::nothrow) AVControllerItem(pid, testAVSessionItem); - AVCallState avCallState; int32_t numberDate = *(reinterpret_cast(data)); std::string dataToS(std::to_string(numberDate)); @@ -94,24 +116,13 @@ void AvSessionControllerFuzzer::FuzzTests(const uint8_t* data, size_t size) avCallState.SetAVCallState(callState); bool mute = std::stoi(dataToS); avCallState.SetAVCallMuted(mute); - avControllerItem->GetAVCallState(avCallState); + g_AVSessionControllerStubDemo->GetAVCallState(avCallState); AVCallMetaData avCallMetaData; std::string strCallMetaData(dataToS); avCallMetaData.SetName(strCallMetaData); avCallMetaData.SetPhoneNumber(strCallMetaData); - avControllerItem->GetAVCallMetaData(avCallMetaData); -} - - -void OHOS::AVSession::AvSessionControllerOnRemoteRequest(const uint8_t* data, size_t size) -{ - auto aVSessionController = std::make_unique(); - if (aVSessionController == nullptr) { - SLOGI("aVSessionController is null"); - return; - } - aVSessionController->FuzzOnRemoteRequest(data, size); + g_AVSessionControllerStubDemo->GetAVCallMetaData(avCallMetaData); } void OHOS::AVSession::AvSessionControllerTests(const uint8_t* data, size_t size) @@ -122,13 +133,21 @@ void OHOS::AVSession::AvSessionControllerTests(const uint8_t* data, size_t size) return; } aVSessionController->FuzzTests(data, size); + for (uint32_t i = 0; i <= MAX_CODE_TEST; i++) { + aVSessionController->FuzzOnRemoteRequest(i, data, size); + } } /* Fuzzer entry point */ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { - /* Run your code on data */ - OHOS::AVSession::AvSessionControllerOnRemoteRequest(data, size); + if ((data == nullptr) || (size > MAX_CODE_LEN) || (size < MIN_SIZE_NUM)) { + return 0; + } + g_AVSessionControllerStubDemo = make_shared(); + if (g_AVSessionControllerStubDemo == nullptr) { + return 0; + } OHOS::AVSession::AvSessionControllerTests(data, size); return 0; } \ No newline at end of file diff --git a/frameworks/native/session/test/fuzztest/avsessioncontrollerstub_fuzzer/avsession_controllerstub_fuzzer.h b/frameworks/native/session/test/fuzztest/avsessioncontrollerstub_fuzzer/avsession_controllerstub_fuzzer.h index 9d6d95f0d3666e521a148a1e7c59150c269bb0ef..c50ce666d381254adb58115e74c61944f995dd1c 100644 --- a/frameworks/native/session/test/fuzztest/avsessioncontrollerstub_fuzzer/avsession_controllerstub_fuzzer.h +++ b/frameworks/native/session/test/fuzztest/avsessioncontrollerstub_fuzzer/avsession_controllerstub_fuzzer.h @@ -18,14 +18,13 @@ #include #include namespace OHOS::AVSession { -void AvSessionControllerOnRemoteRequest(const uint8_t* data, size_t size); void AvSessionControllerTests(const uint8_t* data, size_t size); class AvSessionControllerFuzzer { public: AvSessionControllerFuzzer() = default; ~AvSessionControllerFuzzer() = default; - void FuzzOnRemoteRequest(const uint8_t* data, size_t size); + void FuzzOnRemoteRequest(int32_t code, const uint8_t* data, size_t size); void FuzzTests(const uint8_t* data, size_t size); }; } diff --git a/frameworks/native/session/test/fuzztest/avsessionservicestub_fuzzer/avsession_servicestub_fuzzer.cpp b/frameworks/native/session/test/fuzztest/avsessionservicestub_fuzzer/avsession_servicestub_fuzzer.cpp index 69c713dfe233276162de296418f247d3aa8ebee6..a2395f55ce0e11ab80c98e45c8aa2be2200f9b38 100644 --- a/frameworks/native/session/test/fuzztest/avsessionservicestub_fuzzer/avsession_servicestub_fuzzer.cpp +++ b/frameworks/native/session/test/fuzztest/avsessionservicestub_fuzzer/avsession_servicestub_fuzzer.cpp @@ -31,32 +31,74 @@ using namespace std; using namespace OHOS; using namespace OHOS::AVSession; +class AVSessionServiceStubDemo; static constexpr int32_t MAX_CODE_TEST = 24; static constexpr int32_t MAX_CODE_LEN = 512; static constexpr int32_t MIN_SIZE_NUM = 4; +shared_ptr g_AVSessionServiceStubDemo(nullptr); +class AVSessionServiceStubDemo : public AVSessionServiceStub { +public: + OHOS::sptr CreateSessionInner(const std::string &tag, int32_t type, + const OHOS::AppExecFwk::ElementName &elementName) override { return nullptr; }; + int32_t CreateSessionInner(const std::string &tag, int32_t type, const OHOS::AppExecFwk::ElementName &elementName, + OHOS::sptr &session) override { return 0; }; + int32_t GetAllSessionDescriptors(std::vector &descriptors) override { return 0; }; + int32_t GetSessionDescriptorsBySessionId(const std::string &sessionId, + AVSessionDescriptor &descriptor) override { return isSuccess ? AVSESSION_SUCCESS : 0; }; + int32_t GetHistoricalSessionDescriptors(int32_t maxSize, std::vector &descriptors) override + { + return 0; + }; + int32_t GetHistoricalAVQueueInfos(int32_t maxSize, int32_t maxAppSize, + std::vector &avQueueInfos) override { return 0; }; + int32_t StartAVPlayback(const std::string &bundleName, const std::string &assetId) override { return 0; }; + bool IsAudioPlaybackAllowed(const int32_t uid, const int32_t pid) override { return 0; }; + int32_t CreateControllerInner(const std::string &sessionId, OHOS::sptr &object) override + { + return isSuccess ? AVSESSION_SUCCESS : 0; + }; + int32_t RegisterSessionListener(const OHOS::sptr &listener) override { return 0; }; + int32_t RegisterSessionListenerForAllUsers(const OHOS::sptr &listener) override { return 0; }; + int32_t SendSystemAVKeyEvent(const OHOS::MMI::KeyEvent &keyEvent) override { return 0; }; + int32_t SendSystemControlCommand(const AVControlCommand &command) override { return 0; }; + int32_t RegisterClientDeathObserver(const OHOS::sptr &observer) override { return 0; }; + int32_t CastAudio(const SessionToken &token, + const std::vector &descriptors) override { return 0; }; + int32_t CastAudioForAll(const std::vector &descriptors) override + { + return 0; + }; + int32_t ProcessCastAudioCommand(const RemoteServiceCommand command, const std::string &input, + std::string &output) override { return isSuccess ? AVSESSION_SUCCESS : 0; }; +#ifdef CASTPLUS_CAST_ENGINE_ENABLE + int32_t GetAVCastControllerInner(const std::string &sessionId, OHOS::sptr &object) override + { + return 0; + }; + int32_t StartCast(const SessionToken &sessionToken, const OutputDeviceInfo &outputDeviceInfo) override + { + return 0; + }; + int32_t StopCast(const SessionToken &sessionToken) override { return 0; }; + int32_t checkEnableCast(bool enable) override { return 0; }; +#endif + + int32_t Close() override { return 0; }; + + bool isSuccess = true; +}; void AvSessionServiceFuzzer::FuzzOnRemoteRequest(int32_t code, const uint8_t* data, size_t size) { - if ((data == nullptr) || (size > MAX_CODE_LEN) || (size < MIN_SIZE_NUM)) { - return; - } - if (code >= MAX_CODE_TEST) { - return; - } - sptr avSessionService = new AVSessionService(AVSESSION_SERVICE_ID); - if (avSessionService == nullptr) { - return; - } - MessageParcel dataMessageParcel; MessageParcel reply; MessageOption option; - dataMessageParcel.WriteInterfaceToken(avSessionService->GetDescriptor()); + dataMessageParcel.WriteInterfaceToken(IAVSessionService::GetDescriptor()); size -= sizeof(uint32_t); dataMessageParcel.WriteBuffer(data + sizeof(uint32_t), size); dataMessageParcel.RewindRead(0); - int32_t ret = avSessionService->OnRemoteRequest(code, dataMessageParcel, reply, option); + int32_t ret = g_AVSessionServiceStubDemo->OnRemoteRequest(code, dataMessageParcel, reply, option); if (ret == 0) { SLOGI("OnRemoteRequest ERR_NONE"); } @@ -64,23 +106,21 @@ void AvSessionServiceFuzzer::FuzzOnRemoteRequest(int32_t code, const uint8_t* da void AvSessionServiceFuzzer::FuzzTests(const uint8_t* data, size_t size) { - sptr avSessionService = new AVSessionService(AVSESSION_SERVICE_ID); - MessageParcel dataMessageParcel; OutputDeviceInfo outputDeviceInfo; - avSessionService->CheckBeforeHandleStartCast(dataMessageParcel, outputDeviceInfo); + g_AVSessionServiceStubDemo->CheckBeforeHandleStartCast(dataMessageParcel, outputDeviceInfo); OHOS::MessageParcel reply; AVQueueInfo aVQueueInfo; std::vector avQueueInfos = {aVQueueInfo}; - avSessionService->MarshallingAVQueueInfos(reply, avQueueInfos); + g_AVSessionServiceStubDemo->MarshallingAVQueueInfos(reply, avQueueInfos); unsigned char *buffer = new unsigned char[255]; if (buffer != nullptr) { - avSessionService->AVQueueInfoImgToBuffer(avQueueInfos, buffer); + g_AVSessionServiceStubDemo->AVQueueInfoImgToBuffer(avQueueInfos, buffer); delete[] buffer; } - avSessionService->AVQueueInfoImgToBuffer(avQueueInfos, buffer); + g_AVSessionServiceStubDemo->AVQueueInfoImgToBuffer(avQueueInfos, buffer); std::shared_ptr mediaPixelMap = std::make_shared(); std::vector imgBuffer = {1, 2, 3}; mediaPixelMap->SetInnerImgBuffer(imgBuffer); @@ -88,7 +128,7 @@ void AvSessionServiceFuzzer::FuzzTests(const uint8_t* data, size_t size) avQueueInfos = {aVQueueInfo}; buffer = new unsigned char[255]; if (buffer != nullptr) { - avSessionService->AVQueueInfoImgToBuffer(avQueueInfos, buffer); + g_AVSessionServiceStubDemo->AVQueueInfoImgToBuffer(avQueueInfos, buffer); delete[] buffer; } @@ -101,21 +141,17 @@ void AvSessionServiceFuzzer::FuzzTests(const uint8_t* data, size_t size) deviceInfo.supportedProtocols_ = 1; deviceInfo.authenticationStatus_ = 1; outputDeviceInfo.deviceInfos_.push_back(deviceInfo); - avSessionService->CheckBeforeHandleStartCast(dataMessageParcel, outputDeviceInfo); -} - -void OHOS::AVSession::AvSessionServiceOnRemoteRequest(int32_t code, const uint8_t* data, size_t size) -{ - auto aVSessionService = std::make_unique(); - - aVSessionService->FuzzOnRemoteRequest(code, data, size); + g_AVSessionServiceStubDemo->CheckBeforeHandleStartCast(dataMessageParcel, outputDeviceInfo); } void OHOS::AVSession::AvSessionServiceTests(const uint8_t* data, size_t size) { auto aVSessionService = std::make_unique(); - aVSessionService->FuzzTests(data, size); + + for (uint32_t i = 0; i <= MAX_CODE_TEST; i++) { + aVSessionService->FuzzOnRemoteRequest(i, data, size); + } } /* Fuzzer entry point */ @@ -125,9 +161,12 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) if ((data == nullptr) || (size > MAX_CODE_LEN) || (size < MIN_SIZE_NUM)) { return 0; } - for (uint32_t i = 0; i <= MAX_CODE_TEST; i++) { - OHOS::AVSession::AvSessionServiceOnRemoteRequest(i, data, size); + + g_AVSessionServiceStubDemo = make_shared(); + if (g_AVSessionServiceStubDemo == nullptr) { + return -1; } + OHOS::AVSession::AvSessionServiceTests(data, size); return 0; } \ No newline at end of file diff --git a/frameworks/native/session/test/fuzztest/avsessionservicestub_fuzzer/avsession_servicestub_fuzzer.h b/frameworks/native/session/test/fuzztest/avsessionservicestub_fuzzer/avsession_servicestub_fuzzer.h index bc1193ba2cbb432ebbd46f073fdbeae6f91e0f72..114ece751745fb7f18ade833b21dbf9d097221f3 100644 --- a/frameworks/native/session/test/fuzztest/avsessionservicestub_fuzzer/avsession_servicestub_fuzzer.h +++ b/frameworks/native/session/test/fuzztest/avsessionservicestub_fuzzer/avsession_servicestub_fuzzer.h @@ -18,7 +18,6 @@ #include #include namespace OHOS::AVSession { -void AvSessionServiceOnRemoteRequest(int32_t code, const uint8_t* data, size_t size); void AvSessionServiceTests(const uint8_t* data, size_t size); class AvSessionServiceFuzzer { diff --git a/frameworks/native/session/test/fuzztest/avsessionstub_fuzzer/avsession_stub_fuzzer.cpp b/frameworks/native/session/test/fuzztest/avsessionstub_fuzzer/avsession_stub_fuzzer.cpp index c482bb3d6305cf33bf46802297b2890f6e79b9fb..7faa24199f3bfc62f38587c129738c7a728b3cb2 100644 --- a/frameworks/native/session/test/fuzztest/avsessionstub_fuzzer/avsession_stub_fuzzer.cpp +++ b/frameworks/native/session/test/fuzztest/avsessionstub_fuzzer/avsession_stub_fuzzer.cpp @@ -32,13 +32,56 @@ using namespace std; using namespace OHOS; using namespace OHOS::AVSession; +class AVSessionStubDemo; static constexpr int32_t MAX_CODE_TEST = 30; static constexpr int32_t MAX_CODE_LEN = 512; static constexpr int32_t MIN_SIZE_NUM = 4; -static char g_testSessionTag[] = "test"; -static std::shared_ptr g_AVSessionService; +shared_ptr g_AVSessionStubDemo(nullptr); +class AVSessionStubDemo : public AVSessionStub { +public: + std::string GetSessionId() override { return ""; }; + std::string GetSessionType() override { return ""; }; + int32_t GetAVMetaData(AVMetaData &meta) override { return 0; }; + int32_t SetAVMetaData(const AVMetaData &meta) override { return 0; }; + int32_t SetAVCallMetaData(const AVCallMetaData &meta) override { return 0; }; + int32_t SetAVCallState(const AVCallState &avCallState) override { return 0; }; + int32_t GetAVPlaybackState(AVPlaybackState &state) override { return 0; }; + int32_t SetAVPlaybackState(const AVPlaybackState &state) override { return 0; }; + int32_t GetAVQueueItems(std::vector &items) override { return 0; }; + int32_t SetAVQueueItems(const std::vector &items) override { return 0; }; + int32_t GetAVQueueTitle(std::string &title) override { return 0; }; + int32_t SetAVQueueTitle(const std::string &title) override { return 0; }; + int32_t SetLaunchAbility(const OHOS::AbilityRuntime::WantAgent::WantAgent &ability) override { return 0; }; + int32_t GetExtras(OHOS::AAFwk::WantParams &extras) override { return 0; }; + int32_t SetExtras(const OHOS::AAFwk::WantParams &extras) override { return 0; }; + std::shared_ptr GetController() override { return nullptr; }; + int32_t RegisterCallback(const std::shared_ptr &callback) override { return 0; }; + int32_t Activate() override { return 0; }; + int32_t Deactivate() override { return 0; }; + bool IsActive() override { return true; }; + int32_t Destroy() override { return 0; }; + int32_t AddSupportCommand(const int32_t cmd) override { return 0; }; + int32_t DeleteSupportCommand(const int32_t cmd) override { return 0; }; + int32_t SetSessionEvent(const std::string &event, const OHOS::AAFwk::WantParams &args) override { return 0; }; + +#ifdef CASTPLUS_CAST_ENGINE_ENABLE + std::shared_ptr GetAVCastController() override { return nullptr; }; + int32_t ReleaseCast() override { return 0; }; + int32_t StartCastDisplayListener() override { return 0; }; + int32_t StopCastDisplayListener() override { return 0; }; + int32_t GetAllCastDisplays(std::vector &castDisplays) override { return 0; }; +#endif + +protected: + int32_t RegisterCallbackInner(const OHOS::sptr &callback) override { return 0; }; + OHOS::sptr GetControllerInner() override { return nullptr; }; + +#ifdef CASTPLUS_CAST_ENGINE_ENABLE + OHOS::sptr GetAVCastControllerInner() override { return nullptr; }; +#endif +}; void AvSessionFuzzer::FuzzOnRemoteRequest(int32_t code, const uint8_t* data, size_t size) { @@ -48,18 +91,6 @@ void AvSessionFuzzer::FuzzOnRemoteRequest(int32_t code, const uint8_t* data, siz if (code >= MAX_CODE_TEST) { return; } - g_AVSessionService = std::make_shared(OHOS::AVSESSION_SERVICE_ID); - OHOS::AppExecFwk::ElementName elementName; - std::string bundleName(reinterpret_cast(data), size); - std::string abilityName(reinterpret_cast(data), size); - elementName.SetBundleName(bundleName); - elementName.SetAbilityName(abilityName); - OHOS::sptr avSessionItem = g_AVSessionService->CreateSessionInner( - g_testSessionTag, AVSession::SESSION_TYPE_AUDIO, false, elementName); - if (avSessionItem == nullptr) { - return; - } - MessageParcel dataMessageParcel; MessageParcel reply; MessageOption option; @@ -67,11 +98,11 @@ void AvSessionFuzzer::FuzzOnRemoteRequest(int32_t code, const uint8_t* data, siz size -= sizeof(uint32_t); dataMessageParcel.WriteBuffer(data + sizeof(uint32_t), size); dataMessageParcel.RewindRead(0); - int32_t ret = avSessionItem->OnRemoteRequest(code, dataMessageParcel, reply, option); + int32_t ret = g_AVSessionStubDemo->OnRemoteRequest(code, dataMessageParcel, reply, option); if (ret == 0) { SLOGI("OnRemoteRequest ERR_NONE"); } - avSessionItem->Destroy(); + g_AVSessionStubDemo->Destroy(); } void OHOS::AVSession::AvSessionOnRemoteRequest(int32_t code, const uint8_t* data, size_t size) @@ -87,6 +118,9 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) if ((data == nullptr) || (size > MAX_CODE_LEN) || (size < MIN_SIZE_NUM)) { return 0; } + + g_AVSessionStubDemo = make_shared(); + for (uint32_t i = 0; i <= MAX_CODE_TEST; i++) { OHOS::AVSession::AvSessionOnRemoteRequest(i, data, size); } diff --git a/frameworks/native/session/test/fuzztest/sessionlistenerstub_fuzzer/session_listenerstub_fuzzer.cpp b/frameworks/native/session/test/fuzztest/sessionlistenerstub_fuzzer/session_listenerstub_fuzzer.cpp index a9fa43f22d06a2fa105cf5cfcbf5034fce915ffc..3627131fc1551a4a882d015ecf686a4d39720f1c 100644 --- a/frameworks/native/session/test/fuzztest/sessionlistenerstub_fuzzer/session_listenerstub_fuzzer.cpp +++ b/frameworks/native/session/test/fuzztest/sessionlistenerstub_fuzzer/session_listenerstub_fuzzer.cpp @@ -20,72 +20,51 @@ #include "session_listener_client.h" #include "session_listenerstub_fuzzer.h" -using namespace std; -using namespace OHOS; -using namespace OHOS::AVSession; - +namespace OHOS::AVSession{ static constexpr int32_t MAX_CODE_NUM = 6; static constexpr int32_t MAX_CODE_LEN = 512; static constexpr int32_t MIN_SIZE_NUM = 4; +std::shared_ptr g_SessionListenerStubClient(nullptr); -int32_t SessionListenerStubFuzzer::OnRemoteRequest(int32_t code, uint8_t* data, size_t size) +void SessionListenerStubFuzzer::OnRemoteRequest(int32_t code, uint8_t* data, size_t size) { - if ((data == nullptr) || (size > MAX_CODE_LEN) || (size < MIN_SIZE_NUM)) { - return 0; - } - if (code >= MAX_CODE_NUM) { - return 0; - } - size -= sizeof(uint32_t); - std::shared_ptr testSessionListener = std::make_shared(); - sptr sessionListenerStubClient = new SessionListenerClient(testSessionListener); + MessageParcel dataMessageParcel; - dataMessageParcel.WriteInterfaceToken(sessionListenerStubClient->GetDescriptor()); + dataMessageParcel.WriteInterfaceToken(ISessionListener::GetDescriptor()); dataMessageParcel.WriteBuffer(data + sizeof(uint32_t), size); dataMessageParcel.RewindRead(0); MessageParcel reply; MessageOption option; - int32_t ret = sessionListenerStubClient->OnRemoteRequest(code, dataMessageParcel, reply, option); - return ret; + g_SessionListenerStubClient->OnRemoteRequest(code, dataMessageParcel, reply, option); } void SessionListenerStubFuzzer::FuzzTests(const uint8_t* data, size_t size) { - std::shared_ptr testSessionListener = std::make_shared(); - - sptr sessionListenerStubClient = new SessionListenerClient(testSessionListener); int32_t uid = *(reinterpret_cast(data)); - sessionListenerStubClient->OnAudioSessionChecked(uid); + g_SessionListenerStubClient->OnAudioSessionChecked(uid); OutputDeviceInfo outputDeviceInfo; - OHOS::AVSession::DeviceInfo deviceInfo; + DeviceInfo deviceInfo; deviceInfo.castCategory_ = 1; deviceInfo.deviceId_ = "deviceId"; outputDeviceInfo.deviceInfos_.push_back(deviceInfo); - sessionListenerStubClient->OnDeviceAvailable(outputDeviceInfo); + g_SessionListenerStubClient->OnDeviceAvailable(outputDeviceInfo); std::string deviceId(reinterpret_cast(data), size); - sessionListenerStubClient->OnDeviceOffline(deviceId); + g_SessionListenerStubClient->OnDeviceOffline(deviceId); } -int32_t OHOS::AVSession::SessionListenerStubRemoteRequestTest(int32_t code, uint8_t* data, size_t size) -{ - auto sessionListenerStub = std::make_unique(); - if (sessionListenerStub == nullptr) { - return false; - } - return sessionListenerStub->OnRemoteRequest(code, data, size); -} - -void OHOS::AVSession::SessionListenerStubRemoteRequestTests(const uint8_t* data, size_t size) +void SessionListenerStubTest(uint8_t* data, size_t size) { auto sessionListenerStubFuzzer = std::make_unique(); if (sessionListenerStubFuzzer == nullptr) { - SLOGI("sessionListenerStubFuzzer is null"); return; } sessionListenerStubFuzzer->FuzzTests(data, size); + for (uint32_t i = 0; i <= MAX_CODE_NUM; i++) { + sessionListenerStubFuzzer->OnRemoteRequest(i, data, size); + } } /* Fuzzer entry point */ @@ -95,9 +74,11 @@ extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size) if ((data == nullptr) || (size > MAX_CODE_LEN) || (size < MIN_SIZE_NUM)) { return 0; } - for (uint32_t i = 0; i <= MAX_CODE_NUM; i++) { - OHOS::AVSession::SessionListenerStubRemoteRequestTest(i, data, size); - } - OHOS::AVSession::SessionListenerStubRemoteRequestTests(data, size); + + std::shared_ptr testSessionListener = std::make_shared(); + g_SessionListenerStubClient = std::make_shared(testSessionListener); + + SessionListenerStubTest(data, size); return 0; } +} \ No newline at end of file diff --git a/frameworks/native/session/test/fuzztest/sessionlistenerstub_fuzzer/session_listenerstub_fuzzer.h b/frameworks/native/session/test/fuzztest/sessionlistenerstub_fuzzer/session_listenerstub_fuzzer.h index 1598e3e321faa1b38d11d6aeb2e0c0c0d10cf1bd..c527e73da88f640ac32e61f4ea8ccd4c8ef7542c 100644 --- a/frameworks/native/session/test/fuzztest/sessionlistenerstub_fuzzer/session_listenerstub_fuzzer.h +++ b/frameworks/native/session/test/fuzztest/sessionlistenerstub_fuzzer/session_listenerstub_fuzzer.h @@ -50,14 +50,13 @@ class TestSessionListener : public SessionListener { SLOGI("Enter into TestISessionListener::OnDeviceOffline."); } }; -int32_t SessionListenerStubRemoteRequestTest(int32_t code, uint8_t* data, size_t size); -void SessionListenerStubRemoteRequestTests(const uint8_t* data, size_t size); +void SessionListenerStubTest(uint8_t* data, size_t size); class SessionListenerStubFuzzer { public: SessionListenerStubFuzzer() = default; ~SessionListenerStubFuzzer() = default; - int32_t OnRemoteRequest(int32_t code, uint8_t* data, size_t size); + void OnRemoteRequest(int32_t code, uint8_t* data, size_t size); void FuzzTests(const uint8_t* data, size_t size); }; } diff --git a/services/session/server/test/fuzztest/avsessionservice_fuzzer/avsessionservice_fuzzer.cpp b/services/session/server/test/fuzztest/avsessionservice_fuzzer/avsessionservice_fuzzer.cpp index 00afdc9eb9b0ad166ca7cf0da7bc3809d152012c..c3c0c5c4580b1a85f2edd289d23d3106d907d0dc 100644 --- a/services/session/server/test/fuzztest/avsessionservice_fuzzer/avsessionservice_fuzzer.cpp +++ b/services/session/server/test/fuzztest/avsessionservice_fuzzer/avsessionservice_fuzzer.cpp @@ -247,20 +247,11 @@ void CreateNewControllerForSessionTest(const uint8_t* data, size_t size, void AvSessionServiceControllerTest(const uint8_t* data, size_t size, sptr service) { - std::string tag(reinterpret_cast(data), size); - int32_t type = 0; - std::string bundleName(reinterpret_cast(data), size); - std::string abilityName(reinterpret_cast(data), size); - sptr avSessionItemObj = service->CreateSessionInner(tag, type, elementName); - sptr avSessionItem = (sptr&)avSessionItemObj; - if (!avSessionItem) { - return; - } - ResourceAutoDestroy> avSessionItemRelease(avSessionItem); - service->AddAvQueueInfoToFile(*avSessionItem); + ResourceAutoDestroy> avSessionItemRelease(avsessionHere_); + service->AddAvQueueInfoToFile(*avsessionHere_); sptr avControllerItemObj; std::string sessionId(reinterpret_cast(data), size); - uint32_t ret = service->CreateControllerInner(avSessionItem->GetSessionId(), avControllerItemObj); + uint32_t ret = service->CreateControllerInner(avsessionHere_->GetSessionId(), avControllerItemObj); if (ret != AVSESSION_SUCCESS) { return; } @@ -270,7 +261,7 @@ void AvSessionServiceControllerTest(const uint8_t* data, size_t size, } ResourceAutoDestroy> avControllerItemRelease(avControllerItem); service->HandleControllerRelease(*avControllerItem); - service->HandleSessionRelease(avSessionItem->GetSessionId()); + service->HandleSessionRelease(avsessionHere_->GetSessionId()); int32_t uid = *(reinterpret_cast(data)); service->NotifyAudioSessionCheckTrigger(uid); @@ -280,17 +271,8 @@ void AvSessionServiceControllerTest(const uint8_t* data, size_t size, void AvSessionServiceCastTest(const uint8_t* data, size_t size, sptr service) { - std::string tag(reinterpret_cast(data), size); - int32_t type = 0; - std::string bundleName(reinterpret_cast(data), size); - std::string abilityName(reinterpret_cast(data), size); - sptr avSessionItemObj = service->CreateSessionInner(tag, type, elementName); - sptr avSessionItem = (sptr&)avSessionItemObj; - if (!avSessionItem) { - return; - } SessionToken token; - token.sessionId = avSessionItem->GetSessionId(); + token.sessionId = avsessionHere_->GetSessionId(); token.pid = *(reinterpret_cast(data)); token.uid = *(reinterpret_cast(data)); @@ -897,10 +879,7 @@ void AvSessionServiceTest(const uint8_t* data, size_t size) SLOGE("service is null, return"); return; } - elementName.SetBundleName(g_testAnotherBundleName); - elementName.SetAbilityName(g_testAnotherAbilityName); - avsessionHere_ = avsessionService_->CreateSessionInner( - g_testSessionTag, AVSession::SESSION_TYPE_AUDIO, false, elementName); + AvSessionServiceSystemAbilityTest(data, size, avsessionService_); AvSessionServiceGetAVQueueInfosTest(data, size, avsessionService_); AvSessionServiceGetDescriptorsTest(data, size, avsessionService_); @@ -993,6 +972,10 @@ int32_t AVSessionServiceStubRemoteRequestTest(const uint8_t* data, size_t size) extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { /* Run your code on data */ + elementName.SetBundleName(g_testAnotherBundleName); + elementName.SetAbilityName(g_testAnotherAbilityName); + OHOS::sptr avsessionHere_ = avsessionService_->CreateSessionInner( + g_testSessionTag, AVSession::SESSION_TYPE_AUDIO, false, elementName); MockGetTrustedDeviceList(deviceList); AvSessionServiceExternalCallTest(data, size); AvSessionServiceTest(data, size);