diff --git a/interfaces/inner_kits/native_cpp/test/sinkfuzztest/sinkservicegetdumpinfo_fuzzer/sinkservicegetdumpinfo_fuzzer.cpp b/interfaces/inner_kits/native_cpp/test/sinkfuzztest/sinkservicegetdumpinfo_fuzzer/sinkservicegetdumpinfo_fuzzer.cpp index d05e13e26281c20379ed200e06afb6aec9a999ed..d5ccad1eec3838ae595d0d40ff179f44b4a21ec9 100644 --- a/interfaces/inner_kits/native_cpp/test/sinkfuzztest/sinkservicegetdumpinfo_fuzzer/sinkservicegetdumpinfo_fuzzer.cpp +++ b/interfaces/inner_kits/native_cpp/test/sinkfuzztest/sinkservicegetdumpinfo_fuzzer/sinkservicegetdumpinfo_fuzzer.cpp @@ -16,14 +16,17 @@ #include "sinkservicegetdumpinfo_fuzzer.h" #include "distributed_camera_sink_service.h" #include "distributed_camera_constants.h" +#include "fuzzer/FuzzedDataProvider.h" namespace OHOS { namespace DistributedHardware { void SinkServiceGetDumpInfoFuzzTest(const uint8_t* data, size_t size) { + FuzzedDataProvider fdp(data, size); + int serviceId = fdp.ConsumeIntegral(); + bool isDistributed = fdp.ConsumeBool(); auto sinkService = std::make_shared( - DISTRIBUTED_HARDWARE_CAMERA_SINK_SA_ID, true); - + serviceId, isDistributed); CameraDumpInfo camDump; sinkService->GetCamDumpInfo(camDump); } diff --git a/interfaces/inner_kits/native_cpp/test/sinkfuzztest/sinkservicepausedistributedhardware_fuzzer/sinkservicepausedistributedhardware_fuzzer.cpp b/interfaces/inner_kits/native_cpp/test/sinkfuzztest/sinkservicepausedistributedhardware_fuzzer/sinkservicepausedistributedhardware_fuzzer.cpp index 573c54b07577104dfad276098c4e7cda1e6803b5..1122f9acaf890acd1d84b8bc7887e4c5a3d46119 100644 --- a/interfaces/inner_kits/native_cpp/test/sinkfuzztest/sinkservicepausedistributedhardware_fuzzer/sinkservicepausedistributedhardware_fuzzer.cpp +++ b/interfaces/inner_kits/native_cpp/test/sinkfuzztest/sinkservicepausedistributedhardware_fuzzer/sinkservicepausedistributedhardware_fuzzer.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-2025 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 @@ -22,29 +22,25 @@ #include "distributed_camera_sink_service.h" #include "if_system_ability_manager.h" #include "iservice_registry.h" +#include "fuzzer/FuzzedDataProvider.h" namespace OHOS { namespace DistributedHardware { void SinkServicePauseDistributedHardwareFuzzTest(const uint8_t* data, size_t size) { - if ((data == nullptr) || (size == 0)) { - return; - } + FuzzedDataProvider fdp(data, size); - std::string networkId(reinterpret_cast(data), size); + std::string networkId = fdp.ConsumeRemainingBytesAsString(); std::shared_ptr sinkService = - std::make_shared(DISTRIBUTED_HARDWARE_CAMERA_SINK_SA_ID, true); + std::make_shared(DISTRIBUTED_HARDWARE_CAMERA_SINK_SA_ID, fdp.ConsumeBool()); sinkService->PauseDistributedHardware(networkId); } } } -/* Fuzzer entry point */ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { - /* Run your code on data */ OHOS::DistributedHardware::SinkServicePauseDistributedHardwareFuzzTest(data, size); return 0; -} - +} \ No newline at end of file diff --git a/interfaces/inner_kits/native_cpp/test/sinkfuzztest/sinkservicereleasesink_fuzzer/sinkservicereleasesink_fuzzer.cpp b/interfaces/inner_kits/native_cpp/test/sinkfuzztest/sinkservicereleasesink_fuzzer/sinkservicereleasesink_fuzzer.cpp index d0d1a3b84b97e0121bbc4ea211ae3331d7d41c6b..73641836e88b5b659acc56987618864ce091c999 100644 --- a/interfaces/inner_kits/native_cpp/test/sinkfuzztest/sinkservicereleasesink_fuzzer/sinkservicereleasesink_fuzzer.cpp +++ b/interfaces/inner_kits/native_cpp/test/sinkfuzztest/sinkservicereleasesink_fuzzer/sinkservicereleasesink_fuzzer.cpp @@ -17,18 +17,21 @@ #include "distributed_camera_sink_service.h" #include "dcamera_sink_callback.h" #include "distributed_camera_constants.h" +#include "fuzzer/FuzzedDataProvider.h" namespace OHOS { namespace DistributedHardware { void SinkServiceReleaseSinkFuzzTest(const uint8_t* data, size_t size) { - auto sinkService = std::make_shared( - DISTRIBUTED_HARDWARE_CAMERA_SINK_SA_ID, true); + FuzzedDataProvider fdp(data, size); - std::string params = "fuzz_test_params"; + int saId = DISTRIBUTED_HARDWARE_CAMERA_SINK_SA_ID; + bool isDistributed = fdp.ConsumeBool(); + + auto sinkService = std::make_shared(saId, isDistributed); + std::string params = fdp.ConsumeRandomLengthString(); sptr callback(new DCameraSinkCallback()); sinkService->InitSink(params, callback); - sinkService->ReleaseSink(); } } @@ -38,5 +41,4 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { OHOS::DistributedHardware::SinkServiceReleaseSinkFuzzTest(data, size); return 0; -} - +} \ No newline at end of file diff --git a/interfaces/inner_kits/native_cpp/test/sinkfuzztest/sinkserviceresumedistributedhardware_fuzzer/sinkserviceresumedistributedhardware_fuzzer.cpp b/interfaces/inner_kits/native_cpp/test/sinkfuzztest/sinkserviceresumedistributedhardware_fuzzer/sinkserviceresumedistributedhardware_fuzzer.cpp index f6ec4454b54c4da20fcbd08a2561597160105223..80f40c9c6f774304a24cf9d570683023593f7e37 100644 --- a/interfaces/inner_kits/native_cpp/test/sinkfuzztest/sinkserviceresumedistributedhardware_fuzzer/sinkserviceresumedistributedhardware_fuzzer.cpp +++ b/interfaces/inner_kits/native_cpp/test/sinkfuzztest/sinkserviceresumedistributedhardware_fuzzer/sinkserviceresumedistributedhardware_fuzzer.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-2025 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 @@ -18,6 +18,7 @@ #include #include +#include "fuzzer/FuzzedDataProvider.h" #include "distributed_camera_constants.h" #include "distributed_camera_sink_service.h" #include "if_system_ability_manager.h" @@ -27,24 +28,22 @@ namespace OHOS { namespace DistributedHardware { void SinkServiceResumeDistributedHardwareFuzzTest(const uint8_t* data, size_t size) { - if ((data == nullptr) || (size == 0)) { - return; - } - - std::string networkId(reinterpret_cast(data), size); + FuzzedDataProvider fdp(data, size); + bool initResult = fdp.ConsumeBool(); std::shared_ptr sinkService = - std::make_shared(DISTRIBUTED_HARDWARE_CAMERA_SINK_SA_ID, true); + std::make_shared(DISTRIBUTED_HARDWARE_CAMERA_SINK_SA_ID, initResult); + std::string networkId = fdp.ConsumeRemainingBytesAsString(); + if (sinkService == nullptr) { + return; + } sinkService->ResumeDistributedHardware(networkId); } } } -/* Fuzzer entry point */ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { - /* Run your code on data */ OHOS::DistributedHardware::SinkServiceResumeDistributedHardwareFuzzTest(data, size); return 0; -} - +} \ No newline at end of file diff --git a/interfaces/inner_kits/native_cpp/test/sinkfuzztest/sinkservicestopcapture_fuzzer/sinkservicestopcapture_fuzzer.cpp b/interfaces/inner_kits/native_cpp/test/sinkfuzztest/sinkservicestopcapture_fuzzer/sinkservicestopcapture_fuzzer.cpp index 81b497c024b1169bf3414ba23ca78a73e31d3e1d..057b2a4e2280a6ebe87505ddb0ed4e2b44aa2e52 100644 --- a/interfaces/inner_kits/native_cpp/test/sinkfuzztest/sinkservicestopcapture_fuzzer/sinkservicestopcapture_fuzzer.cpp +++ b/interfaces/inner_kits/native_cpp/test/sinkfuzztest/sinkservicestopcapture_fuzzer/sinkservicestopcapture_fuzzer.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 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 @@ -14,9 +14,10 @@ */ #include "sinkservicestopcapture_fuzzer.h" - +#include "fuzzer/FuzzedDataProvider.h" #include #include +#include #include "dcamera_sink_controller.h" #include "dcamera_sink_access_control.h" @@ -30,11 +31,9 @@ namespace OHOS { namespace DistributedHardware { void SinkServiceStopCaptureFuzzTest(const uint8_t* data, size_t size) { - if (data == nullptr) { - return; - } + FuzzedDataProvider fdp(data, size); - std::string dhId = "1"; + std::string dhId = fdp.ConsumeRemainingBytesAsString(); std::shared_ptr sinkService = std::make_shared(DISTRIBUTED_HARDWARE_CAMERA_SINK_SA_ID, true); sptr sinkCallback(new DCameraSinkCallback()); @@ -48,11 +47,8 @@ void SinkServiceStopCaptureFuzzTest(const uint8_t* data, size_t size) } } -/* Fuzzer entry point */ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { - /* Run your code on data */ OHOS::DistributedHardware::SinkServiceStopCaptureFuzzTest(data, size); return 0; -} - +} \ No newline at end of file diff --git a/interfaces/inner_kits/native_cpp/test/sinkfuzztest/sinkservicestopdistributedhardware_fuzzer/sinkservicestopdistributedhardware_fuzzer.cpp b/interfaces/inner_kits/native_cpp/test/sinkfuzztest/sinkservicestopdistributedhardware_fuzzer/sinkservicestopdistributedhardware_fuzzer.cpp index c23959b598ec9303eecefdb4bbba33932204e85a..69a624dab3ad3bbab845222415a95e444b94d265 100644 --- a/interfaces/inner_kits/native_cpp/test/sinkfuzztest/sinkservicestopdistributedhardware_fuzzer/sinkservicestopdistributedhardware_fuzzer.cpp +++ b/interfaces/inner_kits/native_cpp/test/sinkfuzztest/sinkservicestopdistributedhardware_fuzzer/sinkservicestopdistributedhardware_fuzzer.cpp @@ -16,18 +16,18 @@ #include "sinkservicestopdistributedhardware_fuzzer.h" #include "distributed_camera_sink_service.h" #include "distributed_camera_constants.h" +#include "fuzzer/FuzzedDataProvider.h" namespace OHOS { namespace DistributedHardware { void SinkServiceStopDistributedHardwareFuzzTest(const uint8_t* data, size_t size) { - if (data == nullptr || size == 0) { - return; - } + FuzzedDataProvider fdp(data, size); + bool enableFeature = fdp.ConsumeBool(); auto sinkService = std::make_shared( - DISTRIBUTED_HARDWARE_CAMERA_SINK_SA_ID, true); + DISTRIBUTED_HARDWARE_CAMERA_SINK_SA_ID, enableFeature); - std::string networkId(reinterpret_cast(data), size); + std::string networkId = fdp.ConsumeRemainingBytesAsString(); sinkService->StopDistributedHardware(networkId); } } @@ -37,5 +37,4 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { OHOS::DistributedHardware::SinkServiceStopDistributedHardwareFuzzTest(data, size); return 0; -} - +} \ No newline at end of file diff --git a/interfaces/inner_kits/native_cpp/test/sinkfuzztest/sinkservicesubscribelocalhardware_fuzzer/sinkservicesubscribelocalhardware_fuzzer.cpp b/interfaces/inner_kits/native_cpp/test/sinkfuzztest/sinkservicesubscribelocalhardware_fuzzer/sinkservicesubscribelocalhardware_fuzzer.cpp index b0c3d9ee37cef986890f4b8e7acb9c964fc521d1..91c0fdb6bf1bd8a56ad940a79ca02e812db6a7d7 100644 --- a/interfaces/inner_kits/native_cpp/test/sinkfuzztest/sinkservicesubscribelocalhardware_fuzzer/sinkservicesubscribelocalhardware_fuzzer.cpp +++ b/interfaces/inner_kits/native_cpp/test/sinkfuzztest/sinkservicesubscribelocalhardware_fuzzer/sinkservicesubscribelocalhardware_fuzzer.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-2025 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 @@ -14,7 +14,7 @@ */ #include "sinkservicesubscribelocalhardware_fuzzer.h" - +#include "fuzzer/FuzzedDataProvider.h" #include #include @@ -23,17 +23,16 @@ #include "distributed_camera_sink_service.h" #include "if_system_ability_manager.h" #include "iservice_registry.h" +#include namespace OHOS { namespace DistributedHardware { void SinkServiceSubscribeLocalHardwareFuzzTest(const uint8_t* data, size_t size) { - if ((data == nullptr) || (size == 0)) { - return; - } + FuzzedDataProvider fdp(data, size); - std::string params(reinterpret_cast(data), size); - std::string dhId = "1"; + std::string dhId = fdp.ConsumeRandomLengthString(64); + std::string params = fdp.ConsumeRemainingBytesAsString(); std::shared_ptr sinkService = std::make_shared(DISTRIBUTED_HARDWARE_CAMERA_SINK_SA_ID, true); sptr sinkCallback(new DCameraSinkCallback()); @@ -45,11 +44,8 @@ void SinkServiceSubscribeLocalHardwareFuzzTest(const uint8_t* data, size_t size) } } -/* Fuzzer entry point */ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { - /* Run your code on data */ OHOS::DistributedHardware::SinkServiceSubscribeLocalHardwareFuzzTest(data, size); return 0; -} - +} \ No newline at end of file diff --git a/interfaces/inner_kits/native_cpp/test/sinkfuzztest/sinkserviceunsubscribelocalhardware_fuzzer/sinkserviceunsubscribelocalhardware_fuzzer.cpp b/interfaces/inner_kits/native_cpp/test/sinkfuzztest/sinkserviceunsubscribelocalhardware_fuzzer/sinkserviceunsubscribelocalhardware_fuzzer.cpp index 0948b9569cc24869ffff882c5dc74eb687f6bc6d..9f7fc6f22758a03e963a5017132fda27c936ac47 100644 --- a/interfaces/inner_kits/native_cpp/test/sinkfuzztest/sinkserviceunsubscribelocalhardware_fuzzer/sinkserviceunsubscribelocalhardware_fuzzer.cpp +++ b/interfaces/inner_kits/native_cpp/test/sinkfuzztest/sinkserviceunsubscribelocalhardware_fuzzer/sinkserviceunsubscribelocalhardware_fuzzer.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Copyright (c) 2023-2025 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 @@ -23,32 +23,36 @@ #include "distributed_camera_sink_service.h" #include "if_system_ability_manager.h" #include "iservice_registry.h" +#include "fuzzer/FuzzedDataProvider.h" namespace OHOS { namespace DistributedHardware { void SinkServiceUnsubscribeLocalHardwareFuzzTest(const uint8_t* data, size_t size) { - if (data == nullptr) { - return; - } - - std::string dhId = "1"; + FuzzedDataProvider fdp(data, size); + bool isSystemAbility = fdp.ConsumeBool(); std::shared_ptr sinkService = - std::make_shared(DISTRIBUTED_HARDWARE_CAMERA_SINK_SA_ID, true); + std::make_shared(DISTRIBUTED_HARDWARE_CAMERA_SINK_SA_ID, isSystemAbility); sptr sinkCallback(new DCameraSinkCallback()); - std::shared_ptr sinkDevice = std::make_shared(dhId, sinkCallback); - sinkService->camerasMap_.emplace(dhId, sinkDevice); - - sinkService->UnsubscribeLocalHardware(dhId); + const int maxDhIdLen = 32; + std::string dhId_to_insert = fdp.ConsumeRandomLengthString(maxDhIdLen); + if (!dhId_to_insert.empty()) { + std::shared_ptr sinkDevice = std::make_shared(dhId_to_insert, sinkCallback); + sinkService->camerasMap_.emplace(dhId_to_insert, sinkDevice); + } + std::string dhId_to_unsubscribe; + if (!dhId_to_insert.empty() && fdp.ConsumeBool()) { + dhId_to_unsubscribe = dhId_to_insert; + } else { + dhId_to_unsubscribe = fdp.ConsumeRandomLengthString(maxDhIdLen); + } + sinkService->UnsubscribeLocalHardware(dhId_to_unsubscribe); } } } -/* Fuzzer entry point */ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { - /* Run your code on data */ OHOS::DistributedHardware::SinkServiceUnsubscribeLocalHardwareFuzzTest(data, size); return 0; -} - +} \ No newline at end of file diff --git a/interfaces/inner_kits/native_cpp/test/sinkfuzztest/softbusonsinkbytesreceived_fuzzer/softbusonsinkbytesreceived_fuzzer.cpp b/interfaces/inner_kits/native_cpp/test/sinkfuzztest/softbusonsinkbytesreceived_fuzzer/softbusonsinkbytesreceived_fuzzer.cpp index 806f05d93ae2c7e60951542131ee389cc4f8efdf..27affd5320fbbc10612fa14534ae48fc3a763f7e 100644 --- a/interfaces/inner_kits/native_cpp/test/sinkfuzztest/softbusonsinkbytesreceived_fuzzer/softbusonsinkbytesreceived_fuzzer.cpp +++ b/interfaces/inner_kits/native_cpp/test/sinkfuzztest/softbusonsinkbytesreceived_fuzzer/softbusonsinkbytesreceived_fuzzer.cpp @@ -22,22 +22,32 @@ namespace OHOS { namespace DistributedHardware { void SoftbusOnSinkBytesReceivedFuzzTest(const uint8_t* data, size_t size) { - if ((data == nullptr) || (size < sizeof(uint32_t))) { - return; - } - FuzzedDataProvider fdp(data, size); - int32_t sessionId = fdp.ConsumeIntegral(); - int32_t socket = 1; - const void *receivedData = reinterpret_cast(data); - uint32_t dataLen = fdp.ConsumeIntegral(); + size_t part1_size = fdp.ConsumeIntegralInRange(0, fdp.remaining_bytes()); + std::vector part1_data = fdp.ConsumeBytes(part1_size); + FuzzedDataProvider fdp1(part1_data.data(), part1_data.size()); + + int32_t sessionId = fdp1.ConsumeIntegral(); + int32_t socket = fdp1.ConsumeIntegral(); + auto session = std::make_shared(); DCameraSoftbusAdapter::GetInstance().sinkSocketSessionMap_[socket] = session; - DCameraSoftbusAdapter::GetInstance().SinkOnBytes(sessionId, receivedData, dataLen); - std::string testStr = "test_suffix"; - std::string randomSuffix = fdp.ConsumeRandomLengthString(10); - std::string randomReplacement = fdp.ConsumeRandomLengthString(10); + std::vector receivedData = fdp1.ConsumeRemainingBytes(); + + DCameraSoftbusAdapter::GetInstance().SinkOnBytes( + sessionId, + receivedData.data(), + static_cast(receivedData.size()) + ); + + std::vector part2_data = fdp.ConsumeRemainingBytes(); + FuzzedDataProvider fdp2(part2_data.data(), part2_data.size()); + + std::string testStr = fdp2.ConsumeRandomLengthString(256); + std::string randomSuffix = fdp2.ConsumeRandomLengthString(32); + std::string randomReplacement = fdp2.ConsumeRandomLengthString(32); + DCameraSoftbusAdapter::GetInstance().ReplaceSuffix(testStr, randomSuffix, randomReplacement); } } diff --git a/interfaces/inner_kits/native_cpp/test/sinkfuzztest/softbusonsinksessionclosed_fuzzer/softbusonsinksessionclosed_fuzzer.cpp b/interfaces/inner_kits/native_cpp/test/sinkfuzztest/softbusonsinksessionclosed_fuzzer/softbusonsinksessionclosed_fuzzer.cpp index 4b21d191f9060d8fac15fed4502130ad9e5063c2..b502c9fe56d1c7e3ba7417c414a23900ba808c8b 100644 --- a/interfaces/inner_kits/native_cpp/test/sinkfuzztest/softbusonsinksessionclosed_fuzzer/softbusonsinksessionclosed_fuzzer.cpp +++ b/interfaces/inner_kits/native_cpp/test/sinkfuzztest/softbusonsinksessionclosed_fuzzer/softbusonsinksessionclosed_fuzzer.cpp @@ -14,30 +14,42 @@ */ #include "softbusonsinksessionclosed_fuzzer.h" - #include "dcamera_softbus_adapter.h" +#include "fuzzer/FuzzedDataProvider.h" namespace OHOS { namespace DistributedHardware { +const ShutdownReason VALID_SHUTDOWN_REASONS[] = { + SHUTDOWN_REASON_UNKNOWN, /**< Shutdown for unknown reason */ + SHUTDOWN_REASON_LOCAL, /**< Shutdown by local process */ + SHUTDOWN_REASON_PEER, /**< Shutdown by peer process */ + SHUTDOWN_REASON_LNN_CHANGED, /**< Shutdown for LNN changed */ + SHUTDOWN_REASON_CONN_CHANGED, /**< Shutdown for CONN Changed */ + SHUTDOWN_REASON_TIMEOUT, /**< Shutdown for timeout */ + SHUTDOWN_REASON_SEND_FILE_ERR, /**< Shutdown for sending file error */ + SHUTDOWN_REASON_RECV_FILE_ERR, /**< Shutdown for receiving file error */ + SHUTDOWN_REASON_RECV_DATA_ERR, /**< Shutdown for receiving data error */ + SHUTDOWN_REASON_UNEXPECTED, /**< Shutdown for unexpected reason */ + SHUTDOWN_REASON_SERVICE_DIED, /**< Shutdown for death service */ + SHUTDOWN_REASON_LNN_OFFLINE, /**< Shutdown for offline */ + SHUTDOWN_REASON_LINK_DOWN, /**< Shutdown for link down */ +}; + void SoftbusOnSinkSessionClosedFuzzTest(const uint8_t* data, size_t size) { - if ((data == nullptr) || (size < sizeof(int32_t))) { - return; - } + FuzzedDataProvider fdp(data, size); - int32_t sessionId = *(reinterpret_cast(data)); + int32_t sessionId = fdp.ConsumeIntegral(); + ShutdownReason reason = fdp.PickValueInArray(VALID_SHUTDOWN_REASONS); - DCameraSoftbusAdapter::GetInstance().SinkOnShutDown(sessionId, ShutdownReason::SHUTDOWN_REASON_LOCAL); - DCameraSoftbusAdapter::GetInstance().SourceOnShutDown(sessionId, ShutdownReason::SHUTDOWN_REASON_LOCAL); + DCameraSoftbusAdapter::GetInstance().SinkOnShutDown(sessionId, reason); + DCameraSoftbusAdapter::GetInstance().SourceOnShutDown(sessionId, reason); } } } -/* Fuzzer entry point */ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { - /* Run your code on data */ OHOS::DistributedHardware::SoftbusOnSinkSessionClosedFuzzTest(data, size); return 0; -} - +} \ No newline at end of file diff --git a/interfaces/inner_kits/native_cpp/test/sinkfuzztest/softbusonsinksessionopened_fuzzer/softbusonsinksessionopened_fuzzer.cpp b/interfaces/inner_kits/native_cpp/test/sinkfuzztest/softbusonsinksessionopened_fuzzer/softbusonsinksessionopened_fuzzer.cpp index f63e3cbcd385735b69e8753811425e794ddf06cf..ce9f50f59dc4ad8e26165609d01688fb90404b08 100644 --- a/interfaces/inner_kits/native_cpp/test/sinkfuzztest/softbusonsinksessionopened_fuzzer/softbusonsinksessionopened_fuzzer.cpp +++ b/interfaces/inner_kits/native_cpp/test/sinkfuzztest/softbusonsinksessionopened_fuzzer/softbusonsinksessionopened_fuzzer.cpp @@ -14,32 +14,83 @@ */ #include "softbusonsinksessionopened_fuzzer.h" - #include "dcamera_softbus_adapter.h" +#include "fuzzer/FuzzedDataProvider.h" +#include +#include +#include namespace OHOS { namespace DistributedHardware { +const TransDataType VALID_DATA_TYPES[] = { + DATA_TYPE_MESSAGE, /**< Message */ + DATA_TYPE_BYTES, /**< Bytes */ + DATA_TYPE_FILE, /**< File */ + DATA_TYPE_RAW_STREAM, /**< Raw data stream */ + DATA_TYPE_VIDEO_STREAM, /**< Video data stream*/ + DATA_TYPE_AUDIO_STREAM, /**< Audio data stream*/ + DATA_TYPE_SLICE_STREAM, /**< Video slice stream*/ + DATA_TYPE_BUTT, +}; + +const ShutdownReason VALID_SHUTDOWN_REASONS[] = { + SHUTDOWN_REASON_UNKNOWN, /**< Shutdown for unknown reason */ + SHUTDOWN_REASON_LOCAL, /**< Shutdown by local process */ + SHUTDOWN_REASON_PEER, /**< Shutdown by peer process */ + SHUTDOWN_REASON_LNN_CHANGED, /**< Shutdown for LNN changed */ + SHUTDOWN_REASON_CONN_CHANGED, /**< Shutdown for CONN Changed */ + SHUTDOWN_REASON_TIMEOUT, /**< Shutdown for timeout */ + SHUTDOWN_REASON_SEND_FILE_ERR, /**< Shutdown for sending file error */ + SHUTDOWN_REASON_RECV_FILE_ERR, /**< Shutdown for receiving file error */ + SHUTDOWN_REASON_RECV_DATA_ERR, /**< Shutdown for receiving data error */ + SHUTDOWN_REASON_UNEXPECTED, /**< Shutdown for unexpected reason */ + SHUTDOWN_REASON_SERVICE_DIED, /**< Shutdown for death service */ + SHUTDOWN_REASON_LNN_OFFLINE, /**< Shutdown for offline */ + SHUTDOWN_REASON_LINK_DOWN, /**< Shutdown for link down */ +}; + void SoftbusOnSinkSessionOpenedFuzzTest(const uint8_t* data, size_t size) { - if ((data == nullptr) || (size < sizeof(int32_t))) { - return; - } + FuzzedDataProvider fdp(data, size); + int32_t sessionId = fdp.ConsumeIntegral(); + const int maxLen = 32; + std::string peerSessionNameStr = fdp.ConsumeRandomLengthString(maxLen); + std::vector nameBuf(peerSessionNameStr.begin(), peerSessionNameStr.end()); + nameBuf.push_back('\0'); + + std::string peerDevIdStr = fdp.ConsumeRandomLengthString(maxLen); + std::vector networkId_buf(peerDevIdStr.begin(), peerDevIdStr.end()); + networkId_buf.push_back('\0'); + + std::string pkgNameStr = fdp.ConsumeRandomLengthString(maxLen); + std::vector pkgNameBuf(pkgNameStr.begin(), pkgNameStr.end()); + pkgNameBuf.push_back('\0'); + + TransDataType dataType = fdp.PickValueInArray(VALID_DATA_TYPES); - int32_t sessionId = *(reinterpret_cast(data)); - std::string peerSessionName(reinterpret_cast(data), size); - std::string peerDevId(reinterpret_cast(data), size); - std::string pkgName = "ohos.dhardware.dcamera"; PeerSocketInfo socketInfo = { - .name = const_cast(peerSessionName.c_str()), - .networkId = const_cast(peerDevId.c_str()), - .pkgName = const_cast(pkgName.c_str()), - .dataType = TransDataType::DATA_TYPE_BYTES, + .name = nameBuf.data(), + .networkId = networkId_buf.data(), + .pkgName = pkgNameBuf.data(), + .dataType = dataType, }; + DCameraSoftbusAdapter::GetInstance().SinkOnBind(sessionId, socketInfo); - DCameraSoftbusAdapter::GetInstance().SinkOnShutDown(sessionId, ShutdownReason::SHUTDOWN_REASON_LOCAL); - uint32_t Len = static_cast(size); - DCameraSoftbusAdapter::GetInstance().SinkOnBytes(sessionId, data, Len); - DCameraSoftbusAdapter::GetInstance().SinkOnMessage(sessionId, data, Len); + + ShutdownReason shutdownReason = fdp.PickValueInArray(VALID_SHUTDOWN_REASONS); + DCameraSoftbusAdapter::GetInstance().SinkOnShutDown(sessionId, shutdownReason); + + size_t remainingSize = fdp.remaining_bytes(); + std::vector remainingBytes = fdp.ConsumeBytes(remainingSize); + + if (!remainingBytes.empty()) { + uint32_t remainingLen = static_cast(remainingBytes.size()); + if (fdp.ConsumeBool()) { + DCameraSoftbusAdapter::GetInstance().SinkOnBytes(sessionId, remainingBytes.data(), remainingLen); + } else { + DCameraSoftbusAdapter::GetInstance().SinkOnMessage(sessionId, remainingBytes.data(), remainingLen); + } + } } } } @@ -50,5 +101,4 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) /* Run your code on data */ OHOS::DistributedHardware::SoftbusOnSinkSessionOpenedFuzzTest(data, size); return 0; -} - +} \ No newline at end of file diff --git a/interfaces/inner_kits/native_cpp/test/sourcefuzztest/onsourcelocalcamsrvdied_fuzzer/onsourcelocalcamsrvdied_fuzzer.cpp b/interfaces/inner_kits/native_cpp/test/sourcefuzztest/onsourcelocalcamsrvdied_fuzzer/onsourcelocalcamsrvdied_fuzzer.cpp index d5473d44efaf11a7a40f209bd9049917d2bd0a16..ddcac6b25e74e02a9d710b7a127bf7c37a170cd0 100644 --- a/interfaces/inner_kits/native_cpp/test/sourcefuzztest/onsourcelocalcamsrvdied_fuzzer/onsourcelocalcamsrvdied_fuzzer.cpp +++ b/interfaces/inner_kits/native_cpp/test/sourcefuzztest/onsourcelocalcamsrvdied_fuzzer/onsourcelocalcamsrvdied_fuzzer.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 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 @@ -17,31 +17,46 @@ #include #include - +#include #include "dcamera_source_handler.h" #include "dcamera_source_handler_ipc.h" #include "distributed_camera_constants.h" #include "if_system_ability_manager.h" #include "iservice_registry.h" - +#include "fuzzer/FuzzedDataProvider.h" +#include "ipc_object_stub.h" namespace OHOS { namespace DistributedHardware { +class MockRemoteObject : public OHOS::IPCObjectStub { +public: + explicit MockRemoteObject() : IPCObjectStub(u"mock.remote.object") {} + ~MockRemoteObject() = default; + + int OnRemoteRequest(uint32_t code, OHOS::MessageParcel& data, + OHOS::MessageParcel& reply, OHOS::MessageOption& option) override + { + return 0; + } +}; + void OnSourceLocalCamSrvDiedFuzzTest(const uint8_t* data, size_t size) { - if ((data == nullptr) || (size < (sizeof(int32_t)))) { - return; - } + FuzzedDataProvider fdp(data, size); + sptr knownObject = new MockRemoteObject(); + sptr unknownObject = new MockRemoteObject(); + sptr nullSptr = nullptr; - int32_t saId = *(reinterpret_cast(data)); - sptr samgr = - SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (samgr == nullptr) { - return; - } - sptr remoteObject = samgr->GetSystemAbility(saId); - wptr remote (remoteObject); + const std::array, 3> choices = { + knownObject, + unknownObject, + nullSptr + }; + + int choice = fdp.PickValueInArray({0, 1, 2}); + + wptr objectToDie = choices[choice]; - DCameraSourceHandlerIpc::GetInstance().OnSourceLocalCamSrvDied(remote); + DCameraSourceHandlerIpc::GetInstance().OnSourceLocalCamSrvDied(objectToDie); } } } diff --git a/interfaces/inner_kits/native_cpp/test/sourcefuzztest/softbusonsourcestreamreceived_fuzzer/softbusonsourcestreamreceived_fuzzer.cpp b/interfaces/inner_kits/native_cpp/test/sourcefuzztest/softbusonsourcestreamreceived_fuzzer/softbusonsourcestreamreceived_fuzzer.cpp index f5cdd82057234a03b572d5d34b642031a43915e8..07261a3ab0d0be116b6d715622bae9df1b0c0c9d 100644 --- a/interfaces/inner_kits/native_cpp/test/sourcefuzztest/softbusonsourcestreamreceived_fuzzer/softbusonsourcestreamreceived_fuzzer.cpp +++ b/interfaces/inner_kits/native_cpp/test/sourcefuzztest/softbusonsourcestreamreceived_fuzzer/softbusonsourcestreamreceived_fuzzer.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-2025 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 @@ -14,31 +14,51 @@ */ #include "softbusonsourcestreamreceived_fuzzer.h" - #include "dcamera_softbus_adapter.h" +#include "fuzzer/FuzzedDataProvider.h" +#include +#include namespace OHOS { namespace DistributedHardware { void SoftbusOnSourceStreamReceivedFuzzTest(const uint8_t* data, size_t size) { - if ((data == nullptr) || (size < sizeof(int64_t))) { - return; + FuzzedDataProvider fdp(data, size); + + size_t receivedDataLength = fdp.ConsumeIntegralInRange(0, fdp.remaining_bytes()); + + std::vector receivedDataBytes = fdp.ConsumeBytes(receivedDataLength); + std::vector receivedDataCharBuf(receivedDataBytes.begin(), receivedDataBytes.end()); + StreamData receivedData; + receivedData.buf = receivedDataCharBuf.data(); + receivedData.bufLen = static_cast(receivedDataCharBuf.size()); + + size_t extLength = fdp.remaining_bytes(); + std::vector extBytes = fdp.ConsumeBytes(extLength); + std::vector extCharBuf(extBytes.begin(), extBytes.end()); + StreamData ext; + ext.buf = extCharBuf.data(); + ext.bufLen = static_cast(extCharBuf.size()); + + StreamFrameInfo param = {}; + const int16_t tvCOuntMax = 16; + param.frameType = fdp.ConsumeIntegral(); + param.timeStamp = fdp.ConsumeIntegral(); + param.seqNum = fdp.ConsumeIntegral(); + param.seqSubNum = fdp.ConsumeIntegral(); + param.level = fdp.ConsumeIntegral(); + param.bitMap = fdp.ConsumeIntegral(); + param.tvCount = fdp.ConsumeIntegralInRange(0, tvCOuntMax); + std::vector tvListBuffer(param.tvCount); + for (int32_t i = 0; i < param.tvCount; ++i) { + tvListBuffer[i].type = fdp.ConsumeIntegral(); + + tvListBuffer[i].value = fdp.ConsumeIntegral(); } + param.tvList = tvListBuffer.data(); - int32_t sessionId = *(reinterpret_cast(data)); - const StreamData receivedData = { - const_cast(reinterpret_cast(data)), static_cast(size) - }; - const StreamData ext = { - const_cast(reinterpret_cast(data)), static_cast(size) - }; - const StreamFrameInfo param = { - *(reinterpret_cast(data)), *(reinterpret_cast(data)), - *(reinterpret_cast(data)), *(reinterpret_cast(data)), - *(reinterpret_cast(data)), *(reinterpret_cast(data)), - *(reinterpret_cast(data)), nullptr - }; - int32_t socket = 1; + int32_t sessionId = fdp.ConsumeIntegral(); + int32_t socket = fdp.ConsumeIntegral(); auto session = std::make_shared(); DCameraSoftbusAdapter::GetInstance().sourceSocketSessionMap_[socket] = session; @@ -47,11 +67,8 @@ void SoftbusOnSourceStreamReceivedFuzzTest(const uint8_t* data, size_t size) } } -/* Fuzzer entry point */ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { - /* Run your code on data */ OHOS::DistributedHardware::SoftbusOnSourceStreamReceivedFuzzTest(data, size); return 0; -} - +} \ No newline at end of file