diff --git a/bundle.json b/bundle.json index 72577798116f7f68f04779cbe2c781c4749d950a..a21ce304e91514acd9696198b0ba7492bacae38b 100644 --- a/bundle.json +++ b/bundle.json @@ -81,6 +81,7 @@ "//foundation/distributedhardware/distributed_camera/services/cameraservice/sinkservice/test/unittest:sink_service_test", "//foundation/distributedhardware/distributed_camera/services/cameraservice/sourceservice/test/unittest:source_service_test", "//foundation/distributedhardware/distributed_camera/services/cameraservice/base/test/unittest:services_base_test", + "//foundation/distributedhardware/distributed_camera/services/channel/test/fuzztest:fuzztest", "//foundation/distributedhardware/distributed_camera/services/channel/test/unittest:camera_channel_test", "//foundation/distributedhardware/distributed_camera/services/data_process/test/unittest:data_process_test", "//foundation/distributedhardware/distributed_camera/interfaces/inner_kits/native_cpp/test/sinkfuzztest:fuzztest", diff --git a/interfaces/inner_kits/native_cpp/test/sourcefuzztest/softbusadapter_fuzzer/softbusadapter_fuzzer.cpp b/interfaces/inner_kits/native_cpp/test/sourcefuzztest/softbusadapter_fuzzer/softbusadapter_fuzzer.cpp index 6e3cb98198311a04385874d5730f52ed1bb9cc90..0b96f9ca38d8b192815c182705d06f1c47f4dec0 100644 --- a/interfaces/inner_kits/native_cpp/test/sourcefuzztest/softbusadapter_fuzzer/softbusadapter_fuzzer.cpp +++ b/interfaces/inner_kits/native_cpp/test/sourcefuzztest/softbusadapter_fuzzer/softbusadapter_fuzzer.cpp @@ -184,6 +184,35 @@ void FuzzCloseSessionWithNetWorkId(const uint8_t *data, size_t size) std::string networkId = fuzzedData.ConsumeRandomLengthString(64); DCameraSoftbusAdapter::GetInstance().CloseSessionWithNetWorkId(networkId); } + +void DCameraSoftbusAdapterCheckOsTypeFuzzTest(const uint8_t* data, size_t size) +{ + if (data == nullptr || size == 0) { + return; + } + + int32_t tempStrLen = 64; + FuzzedDataProvider fuzzedData(data, size); + std::string networkId = fuzzedData.ConsumeRandomLengthString(tempStrLen); + bool isInvalid = false; + + DCameraSoftbusAdapter::GetInstance().CheckOsType(networkId, isInvalid); +} + +void DCameraSoftbusAdapterParseValueFromCjsonFuzzTest(const uint8_t* data, size_t size) +{ + if (data == nullptr || size == 0) { + return; + } + + int32_t tempJsonStrLen = 256; + int32_t tempStrLen = 64; + FuzzedDataProvider fuzzedData(data, size); + std::string jsonStr = fuzzedData.ConsumeRandomLengthString(tempJsonStrLen); + std::string key = fuzzedData.ConsumeRandomLengthString(tempStrLen); + + DCameraSoftbusAdapter::GetInstance().ParseValueFromCjson(jsonStr, key); +} } } @@ -201,5 +230,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) OHOS::DistributedHardware::FuzzSendSofbusBytes(data, size); OHOS::DistributedHardware::FuzzRecordSourceSocketSession(data, size); OHOS::DistributedHardware::FuzzCloseSessionWithNetWorkId(data, size); + OHOS::DistributedHardware::DCameraSoftbusAdapterCheckOsTypeFuzzTest(data, size); + OHOS::DistributedHardware::DCameraSoftbusAdapterParseValueFromCjsonFuzzTest(data, size); return 0; } diff --git a/services/channel/test/fuzztest/BUILD.gn b/services/channel/test/fuzztest/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..08964d12075a82ce8b292a728cd8ee1dae2727d9 --- /dev/null +++ b/services/channel/test/fuzztest/BUILD.gn @@ -0,0 +1,20 @@ +# Copyright (c) 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 +# +# 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. + +group("fuzztest") { + testonly = true + + deps = [ + "allconnectmanager_fuzzer:fuzztest", + ] +} \ No newline at end of file diff --git a/services/channel/test/fuzztest/allconnectmanager_fuzzer/BUILD.gn b/services/channel/test/fuzztest/allconnectmanager_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..ba1040ead505109140ac4b6d69d07d35adde1bdc --- /dev/null +++ b/services/channel/test/fuzztest/allconnectmanager_fuzzer/BUILD.gn @@ -0,0 +1,79 @@ +# Copyright (c) 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 +# +# 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/test.gni") +import( + "//foundation/distributedhardware/distributed_camera/distributedcamera.gni") + +##############################fuzztest########################################## +ohos_fuzztest("AllconnectManagerFuzzTest") { + module_out_path = "${fuzz_test_output_path}/allconnect_manager" + + fuzz_config_file = "${services_path}/channel/test/fuzztest/allconnectmanager_fuzzer" + + include_dirs = + [ "${services_path}/channel/include/allconnect" ] + + include_dirs += [ + "include", + "include/allconnect", + "${common_path}/include", + "${common_path}/include/constants", + "${common_path}/include/utils", + "${services_path}/channel/include", + "${services_path}/channel/include/allconnect", + "${services_path}/cameraservice/base/include", + "${services_path}/cameraservice/sinkservice/include/distributedcameramgr", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + "-Dprivate=public", + ] + + sources = [ "allconnectmanager_fuzzer.cpp" ] + + deps = [ + "${common_path}:distributed_camera_utils", + "${services_path}/channel:distributed_camera_channel", + "${services_path}/cameraservice/sinkservice:distributed_camera_sink", + "${services_path}/cameraservice/sourceservice:distributed_camera_source", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"AllconnectManagerFuzzTest\"", + "LOG_DOMAIN=0xD004150", + ] + + external_deps = [ + "c_utils:utils", + "distributed_hardware_fwk:distributedhardwareutils", + "hilog:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [ ":AllconnectManagerFuzzTest" ] +} +############################################################################### diff --git a/services/channel/test/fuzztest/allconnectmanager_fuzzer/allconnectmanager_fuzzer.cpp b/services/channel/test/fuzztest/allconnectmanager_fuzzer/allconnectmanager_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..68dce2470e91c88738eee6adaea03b588860f1ef --- /dev/null +++ b/services/channel/test/fuzztest/allconnectmanager_fuzzer/allconnectmanager_fuzzer.cpp @@ -0,0 +1,266 @@ +/* + * Copyright (c) 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 + * + * 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 "allconnectmanager_fuzzer.h" + +#include + +#include "distributed_camera_allconnect_manager.h" + +constexpr size_t maxStringLength = 64; +namespace OHOS { +namespace DistributedHardware { +void DCameraAllConnectManagerInitFuzzTest(const uint8_t* data, size_t size) +{ + if (data == nullptr || size == 0) { + return; + } + + FuzzedDataProvider fuzzedData(data, size); +#ifdef __LP64__ + constexpr const char *tmpAllConnectSoPath = "/system/lib64/"; +#else + constexpr const char *tmpAllConnectSoPath = "/system/lib/"; +#endif + std::string randomPath = fuzzedData.ConsumeRandomLengthString(PATH_MAX); + std::string allConnectSoPath = tmpAllConnectSoPath; + allConnectSoPath = randomPath; + + DCameraAllConnectManager::GetInstance().InitDCameraAllConnectManager(); +} + +void DCameraAllConnectManagerUnInitFuzzTest(const uint8_t* data, size_t size) +{ + if (data == nullptr || size == 0) { + return; + } + + FuzzedDataProvider fuzzedData(data, size); + void* randomDllHandle = reinterpret_cast(fuzzedData.ConsumeIntegral()); + const_cast(DCameraAllConnectManager::GetInstance().dllHandle_) = randomDllHandle; + + DCameraAllConnectManager::GetInstance().UnInitDCameraAllConnectManager(); +} + +void DCameraAllConnectManagerIsInitedFuzzTest(const uint8_t* data, size_t size) +{ + if (data == nullptr || size == 0) { + return; + } + + FuzzedDataProvider fuzzedData(data, size); + bool randomInitState = fuzzedData.ConsumeBool(); + const_cast(DCameraAllConnectManager::GetInstance().bInited_) = randomInitState; + + DCameraAllConnectManager::GetInstance().IsInited(); +} + +void DCameraAllConnectManagerPublishServiceStateFuzzTest(const uint8_t* data, size_t size) +{ + if (data == nullptr || size == 0) { + return; + } + + FuzzedDataProvider fuzzedData(data, size); + std::string peerNetworkId = fuzzedData.ConsumeRandomLengthString(maxStringLength); + std::string dhId = fuzzedData.ConsumeRandomLengthString(maxStringLength); + DCameraCollaborationBussinessStatus state = static_cast( + fuzzedData.ConsumeIntegralInRange(SCM_IDLE, SCM_CONNECTED)); + + DCameraAllConnectManager::GetInstance().PublishServiceState(peerNetworkId, dhId, state); +} + +void DCameraAllConnectManagerApplyAdvancedResourceFuzzTest(const uint8_t* data, size_t size) +{ + if (data == nullptr || size == 0) { + return; + } + + FuzzedDataProvider fuzzedData(data, size); + std::string peerNetworkId = fuzzedData.ConsumeRandomLengthString(maxStringLength); + DCameraCollaborationResourceRequestInfoSets resourceRequest; + resourceRequest.remoteHardwareListSize = fuzzedData.ConsumeIntegral(); + resourceRequest.localHardwareListSize = fuzzedData.ConsumeIntegral(); + resourceRequest.communicationRequest = nullptr; + + DCameraAllConnectManager::GetInstance().ApplyAdvancedResource(peerNetworkId, &resourceRequest); +} + +void DCameraAllConnectManagerGetAllConnectSoLoadFuzzTest(const uint8_t* data, size_t size) +{ + if (data == nullptr || size == 0) { + return; + } + + FuzzedDataProvider fuzzedData(data, size); +#ifdef __LP64__ + constexpr const char *tmpAllConnectSoPath = "/system/lib64/"; +#else + constexpr const char *tmpAllConnectSoPath = "/system/lib/"; +#endif + std::string randomPath = fuzzedData.ConsumeRandomLengthString(PATH_MAX + 10); + std::string allConnectSoPath = tmpAllConnectSoPath; + allConnectSoPath = randomPath; + + DCameraAllConnectManager::GetInstance().GetAllConnectSoLoad(); +} + +void DCameraAllConnectManagerRegisterLifecycleCallbackFuzzTest(const uint8_t* data, size_t size) +{ + if (data == nullptr || size == 0) { + return; + } + + FuzzedDataProvider fuzzedData(data, size); + std::string serviceName = fuzzedData.ConsumeRandomLengthString(maxStringLength); + const_cast(DCameraAllConnectManager::SERVICE_NAME) = serviceName; + + DCameraAllConnectManager::GetInstance().RegisterLifecycleCallback(); +} + +void DCameraAllConnectManagerUnRegisterLifecycleCallbackFuzzTest(const uint8_t* data, size_t size) +{ + if (data == nullptr || size == 0) { + return; + } + + FuzzedDataProvider fuzzedData(data, size); + std::string serviceName = fuzzedData.ConsumeRandomLengthString(maxStringLength); + const_cast(DCameraAllConnectManager::SERVICE_NAME) = serviceName; + + DCameraAllConnectManager::GetInstance().UnRegisterLifecycleCallback(); +} + +void DCameraAllConnectManagerOnStopFuzzTest(const uint8_t* data, size_t size) +{ + if (data == nullptr || size == 0) { + return; + } + + FuzzedDataProvider fuzzedData(data, size); + std::string peerNetworkId = fuzzedData.ConsumeRandomLengthString(maxStringLength); + + DCameraAllConnectManager::GetInstance().OnStop(peerNetworkId.c_str()); +} + +void DCameraAllConnectManagerRemoveSinkNetworkIdFuzzTest(const uint8_t* data, size_t size) +{ + if (data == nullptr || size == 0) { + return; + } + + FuzzedDataProvider fuzzedData(data, size); + int32_t sessionId = fuzzedData.ConsumeIntegral(); + + DCameraAllConnectManager::GetInstance().RemoveSinkNetworkId(sessionId); +} + +void DCameraAllConnectManagerRemoveSourceNetworkIdFuzzTest(const uint8_t* data, size_t size) +{ + if (data == nullptr || size == 0) { + return; + } + + FuzzedDataProvider fuzzedData(data, size); + int32_t sessionId = fuzzedData.ConsumeIntegral(); + DCameraAllConnectManager::GetInstance().RemoveSourceNetworkId(sessionId); +} + +void DCameraAllConnectManagerSetSourceNetworkIdFuzzTest(const uint8_t* data, size_t size) +{ + if (data == nullptr || size == 0) { + return; + } + + FuzzedDataProvider fuzzedData(data, size); + std::string networkId = fuzzedData.ConsumeRandomLengthString(maxStringLength); + int32_t socket = fuzzedData.ConsumeIntegral(); + + DCameraAllConnectManager::GetInstance().SetSourceNetworkId(networkId, socket); +} + +void DCameraAllConnectManagerSetSinkNetWorkIdFuzzTest(const uint8_t* data, size_t size) +{ + if (data == nullptr || size == 0) { + return; + } + + FuzzedDataProvider fuzzedData(data, size); + std::string networkId = fuzzedData.ConsumeRandomLengthString(maxStringLength); + int32_t socket = fuzzedData.ConsumeIntegral(); + + DCameraAllConnectManager::GetInstance().SetSinkNetWorkId(networkId, socket); +} + +void DCameraAllConnectManagerGetSinkDevIdBySocketFuzzTest(const uint8_t* data, size_t size) +{ + if (data == nullptr || size == 0) { + return; + } + + FuzzedDataProvider fuzzedData(data, size); + int32_t socket = fuzzedData.ConsumeIntegral(); + + DCameraAllConnectManager::GetInstance().GetSinkDevIdBySocket(socket); +} + +void DCameraAllConnectManagerGetSinkSocketByNetWorkIdFuzzTest(const uint8_t* data, size_t size) +{ + if (data == nullptr || size == 0) { + return; + } + + FuzzedDataProvider fuzzedData(data, size); + std::string networkId = fuzzedData.ConsumeRandomLengthString(maxStringLength); + + DCameraAllConnectManager::GetInstance().GetSinkSocketByNetWorkId(networkId); +} + +void DCameraAllConnectManagerGetSourceSocketByNetworkIdFuzzTest(const uint8_t* data, size_t size) +{ + if (data == nullptr || size == 0) { + return; + } + + FuzzedDataProvider fuzzedData(data, size); + std::string networkId = fuzzedData.ConsumeRandomLengthString(maxStringLength); + + DCameraAllConnectManager::GetInstance().GetSourceSocketByNetworkId(networkId); +} +} +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::DistributedHardware::DCameraAllConnectManagerInitFuzzTest(data, size); + OHOS::DistributedHardware::DCameraAllConnectManagerUnInitFuzzTest(data, size); + OHOS::DistributedHardware::DCameraAllConnectManagerIsInitedFuzzTest(data, size); + OHOS::DistributedHardware::DCameraAllConnectManagerPublishServiceStateFuzzTest(data, size); + OHOS::DistributedHardware::DCameraAllConnectManagerApplyAdvancedResourceFuzzTest(data, size); + OHOS::DistributedHardware::DCameraAllConnectManagerGetAllConnectSoLoadFuzzTest(data, size); + OHOS::DistributedHardware::DCameraAllConnectManagerRegisterLifecycleCallbackFuzzTest(data, size); + OHOS::DistributedHardware::DCameraAllConnectManagerUnRegisterLifecycleCallbackFuzzTest(data, size); + OHOS::DistributedHardware::DCameraAllConnectManagerOnStopFuzzTest(data, size); + OHOS::DistributedHardware::DCameraAllConnectManagerRemoveSinkNetworkIdFuzzTest(data, size); + OHOS::DistributedHardware::DCameraAllConnectManagerRemoveSourceNetworkIdFuzzTest(data, size); + OHOS::DistributedHardware::DCameraAllConnectManagerSetSourceNetworkIdFuzzTest(data, size); + OHOS::DistributedHardware::DCameraAllConnectManagerSetSinkNetWorkIdFuzzTest(data, size); + OHOS::DistributedHardware::DCameraAllConnectManagerGetSinkDevIdBySocketFuzzTest(data, size); + OHOS::DistributedHardware::DCameraAllConnectManagerGetSinkSocketByNetWorkIdFuzzTest(data, size); + OHOS::DistributedHardware::DCameraAllConnectManagerGetSourceSocketByNetworkIdFuzzTest(data, size); + return 0; +} \ No newline at end of file diff --git a/services/channel/test/fuzztest/allconnectmanager_fuzzer/allconnectmanager_fuzzer.h b/services/channel/test/fuzztest/allconnectmanager_fuzzer/allconnectmanager_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..257e34364af51c35d91c8b720c370c8e4cbcc855 --- /dev/null +++ b/services/channel/test/fuzztest/allconnectmanager_fuzzer/allconnectmanager_fuzzer.h @@ -0,0 +1,21 @@ +/* +* Copyright (c) 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 +* +* 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. +*/ + +#ifndef ALLCONNECTMANAGER_FUZZER_H +#define ALLCONNECTMANAGER_FUZZER_H + +#define FUZZ_PROJECT_NAME "allconnectmanager_fuzzer" + +#endif \ No newline at end of file diff --git a/services/channel/test/fuzztest/allconnectmanager_fuzzer/corpus/init b/services/channel/test/fuzztest/allconnectmanager_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..65af8ee8d11bf23407ea34d4de49f7cbb6a2b791 --- /dev/null +++ b/services/channel/test/fuzztest/allconnectmanager_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 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 +# +# 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. + +FUZZ \ No newline at end of file diff --git a/services/channel/test/fuzztest/allconnectmanager_fuzzer/project.xml b/services/channel/test/fuzztest/allconnectmanager_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..66e1dcac475475fb101b6f8670ec699e6e9696aa --- /dev/null +++ b/services/channel/test/fuzztest/allconnectmanager_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + +