diff --git a/test/servicesfuzztest/dmsoftbuslistener_fuzzer/BUILD.gn b/test/servicesfuzztest/dmsoftbuslistener_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..8ededc4949a33b0bc3cfc52d2be972da6ff8daaf --- /dev/null +++ b/test/servicesfuzztest/dmsoftbuslistener_fuzzer/BUILD.gn @@ -0,0 +1,94 @@ +# Copyright (c) 2022-2024 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/device_manager/device_manager.gni") + +##############################fuzztest########################################## +ohos_fuzztest("DmSoftbusListenerFuzzTest") { + module_out_path = fuzz_test_output_path + fuzz_config_file = + "${devicemanager_path}/test/servicesfuzztest/dmsoftbuslistener_fuzzer" + + include_dirs = [ + "${utils_path}/include", + "${innerkits_path}/native_cpp/include", + "${innerkits_path}/native_cpp/include/ipc/standard", + "${innerkits_path}/native_cpp/include/ipc", + "${innerkits_path}/native_cpp/include/notify", + "${common_path}/include", + "${common_path}/include/ipc", + "${common_path}/include/ipc/model", + "${utils_path}/include/ipc/standard", + "${servicesimpl_path}/include", + "${servicesimpl_path}/include/dependency/timer", + "${servicesimpl_path}/include/discovery", + "${servicesimpl_path}/include/dependency/softbus", + "${servicesimpl_path}/include/authentication", + "${servicesimpl_path}/include/adapter", + "${servicesimpl_path}/include/ipc/standard", + "${servicesimpl_path}/include/dependency/hichain", + "${servicesimpl_path}/include/devicestate", + "${devicemanager_path}/test/unittest/mock", + "${servicesimpl_path}/include/ability", + "${servicesimpl_path}/include/config", + ] + + cflags = [ + "-g", + "-O0", + "-Dprivate=public", + "-Dprotected=public", + "-Werror", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + + sources = [ "dm_softbus_listener_fuzzer.cpp" ] + + deps = [ + "${devicemanager_path}/services/service:devicemanagerservicetest", + "${json_path}:devicemanagerjson", + "${utils_path}:devicemanagerutilstest", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"DmSoftbusListenerFuzzTest\"", + "LOG_DOMAIN=0xD004110", + ] + + external_deps = [ + "ability_base:want", + "cJSON:cjson", + "device_auth:deviceauth_sdk", + "device_info_manager:distributed_device_profile_common", + "device_info_manager:distributed_device_profile_sdk", + "dsoftbus:softbus_client", + "ffrt:libffrt", + "hilog:libhilog", + "init:libbegetutil", + "ipc:ipc_single", + "safwk:system_ability_fwk", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + + deps = [ ":DmSoftbusListenerFuzzTest" ] +} +############################################################################### diff --git a/test/servicesfuzztest/dmsoftbuslistener_fuzzer/corpus/init b/test/servicesfuzztest/dmsoftbuslistener_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..d9719cafab50ed61d354cfa865b56390a0df320f --- /dev/null +++ b/test/servicesfuzztest/dmsoftbuslistener_fuzzer/corpus/init @@ -0,0 +1,13 @@ +# Copyright (c) 2023 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/test/servicesfuzztest/dmsoftbuslistener_fuzzer/dm_softbus_listener_fuzzer.cpp b/test/servicesfuzztest/dmsoftbuslistener_fuzzer/dm_softbus_listener_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..15fa538c376e8d0da080438499d77a26831966d2 --- /dev/null +++ b/test/servicesfuzztest/dmsoftbuslistener_fuzzer/dm_softbus_listener_fuzzer.cpp @@ -0,0 +1,91 @@ +/* + * 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 +#include +#include +#include + +#include "dm_constants.h" +#include "dm_device_info.h" +#include "device_manager_service.h" +#include "device_manager_impl.h" +#include "device_manager.h" +#include "device_manager_callback.h" +#include "dm_softbus_listener_fuzzer.h" +#include "softbus_listener.h" + +namespace OHOS { +namespace DistributedHardware { +void DmSoftbusListenerFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size < (sizeof(uint16_t) + (sizeof(int32_t) + sizeof(int32_t))) || + (size > DM_MAX_DEVICE_ID_LEN))) { + return; + } + FuzzedDataProvider fdp(data, size); + DmDeviceInfo deviceInfo; + deviceInfo.deviceTypeId = fdp.ConsumeIntegral(); + deviceInfo.range = fdp.ConsumeIntegral(); + deviceInfo.networkType = fdp.ConsumeIntegral(); + deviceInfo.extraData = fdp.ConsumeRandomLengthString(); + SoftbusListener::DeviceNameChange(deviceInfo); + SoftbusListener::DeviceScreenStatusChange(deviceInfo); + int32_t errcode = fdp.ConsumeIntegral(); + uint16_t deviceTypeId = fdp.ConsumeIntegral(); + string proofInfoStr = fdp.ConsumeRandomLengthString(); + SoftbusListener::CredentialAuthStatusProcess(proofInfoStr, deviceTypeId, errcode); + const char* proofInfo = proofInfoStr.c_str(); + DevUserInfo localDevUserInfo; + localDevUserInfo.deviceId = fdp.ConsumeRandomLengthString(); + localDevUserInfo.userId = fdp.ConsumeIntegral(); + DevUserInfo remoteDevUserInfo; + remoteDevUserInfo.deviceId = fdp.ConsumeRandomLengthString(); + remoteDevUserInfo.userId = fdp.ConsumeIntegral(); + string remoteAclList = fdp.ConsumeRandomLengthString(); + SoftbusListener::OnSyncLocalAclList(localDevUserInfo, remoteDevUserInfo, remoteAclList); + string aclList = fdp.ConsumeRandomLengthString(); + SoftbusListener::OnGetAclListHash(localDevUserInfo, remoteDevUserInfo, aclList); + uint32_t proofLen = fdp.ConsumeIntegral(); + SoftbusListener::OnCredentialAuthStatus(proofInfo, proofLen, deviceTypeId, errcode); + uint16_t typeValue = fdp.ConsumeIntegral(); + NodeStatusType type = static_cast(typeValue); + NodeStatus status; + status.basicInfo.deviceTypeId = fdp.ConsumeIntegral(); + status.basicInfo.osType = fdp.ConsumeIntegral(); + status.reserved[0] = fdp.ConsumeIntegral(); + SoftbusListener::OnDeviceScreenStatusChanged(type, &status); + SoftbusListener listener; + std::string networkIdStr = fdp.ConsumeRandomLengthString(); + int32_t networkType = fdp.ConsumeIntegral(); + listener.GetNetworkTypeByNetworkId(networkIdStr.c_str(), networkType); + std::string name = fdp.ConsumeRandomLengthString(); + SoftbusListener::CloseDmRadarHelperObj(name); + std::string msg = fdp.ConsumeRandomLengthString(); + listener.SendAclChangedBroadcast(msg); + int32_t screenStatus = fdp.ConsumeIntegral(); + listener.GetDeviceScreenStatus(networkId, screenStatus); + listener.DeleteCacheDeviceInfo(); +} +} +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::DistributedHardware::DmSoftbusListenerFuzzTest(data, size); + return 0; +} diff --git a/test/servicesfuzztest/dmsoftbuslistener_fuzzer/dm_softbus_listener_fuzzer.h b/test/servicesfuzztest/dmsoftbuslistener_fuzzer/dm_softbus_listener_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..cfa39a6720f4c22fcbee7e01d329ee9a9a0ce8b5 --- /dev/null +++ b/test/servicesfuzztest/dmsoftbuslistener_fuzzer/dm_softbus_listener_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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. + */ + +#ifndef DM_SOFTBUS_LISTENER_FUZZER_H +#define DM_SOFTBUS_LISTENER_FUZZER_H + +#define FUZZ_PROJECT_NAME "dm_softbus_listener_fuzzer" + +#endif // DM_SOFTBUS_LISTENER_FUZZER_H diff --git a/test/servicesfuzztest/dmsoftbuslistener_fuzzer/project.xml b/test/servicesfuzztest/dmsoftbuslistener_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..bac4974e9068af3c045fbb5c3a588aa79e47faaf --- /dev/null +++ b/test/servicesfuzztest/dmsoftbuslistener_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + \ No newline at end of file