From 8c51a398f29adfeda3902024770d3ae3c9185544 Mon Sep 17 00:00:00 2001 From: torrizo Date: Mon, 26 May 2025 19:55:52 -0700 Subject: [PATCH] add fuzz Signed-off-by: torrizo --- test/BUILD.gn | 1 + test/interfacesfuzztest/BUILD.gn | 20 ++ .../devicemanagerimpl_fuzzer/BUILD.gn | 96 +++++++ .../devicemanagerimpl_fuzzer/corpus/init | 13 + .../device_manager_impl_fuzzer.cpp | 265 ++++++++++++++++++ .../device_manager_impl_fuzzer.h | 21 ++ .../devicemanagerimpl_fuzzer/project.xml | 25 ++ 7 files changed, 441 insertions(+) create mode 100755 test/interfacesfuzztest/BUILD.gn create mode 100755 test/interfacesfuzztest/devicemanagerimpl_fuzzer/BUILD.gn create mode 100755 test/interfacesfuzztest/devicemanagerimpl_fuzzer/corpus/init create mode 100755 test/interfacesfuzztest/devicemanagerimpl_fuzzer/device_manager_impl_fuzzer.cpp create mode 100755 test/interfacesfuzztest/devicemanagerimpl_fuzzer/device_manager_impl_fuzzer.h create mode 100755 test/interfacesfuzztest/devicemanagerimpl_fuzzer/project.xml diff --git a/test/BUILD.gn b/test/BUILD.gn index b5cd94a6b..c856a29d0 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -19,6 +19,7 @@ group("test") { deps += [ "benchmarktest:benchmarktest", "commonfuzztest:fuzztest", + "interfacesfuzztest:fuzztest", "servicesfuzztest:fuzztest", "softbusfuzztest:fuzztest", "unittest:unittest", diff --git a/test/interfacesfuzztest/BUILD.gn b/test/interfacesfuzztest/BUILD.gn new file mode 100755 index 000000000..b278cf842 --- /dev/null +++ b/test/interfacesfuzztest/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 = [ + "devicemanagerimpl_fuzzer:fuzztest", + ] +} diff --git a/test/interfacesfuzztest/devicemanagerimpl_fuzzer/BUILD.gn b/test/interfacesfuzztest/devicemanagerimpl_fuzzer/BUILD.gn new file mode 100755 index 000000000..a706fd85f --- /dev/null +++ b/test/interfacesfuzztest/devicemanagerimpl_fuzzer/BUILD.gn @@ -0,0 +1,96 @@ +# 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("DeviceManagerImplFuzzTest") { + module_out_path = fuzz_test_output_path + fuzz_config_file = + "${devicemanager_path}/test/interfacesfuzztest/devicemanagerimpl_fuzzer" + + include_dirs = [ + "${common_path}/include", + "${common_path}/include/ipc", + "${common_path}/include/ipc/model", + "${devicemanager_path}/interfaces/kits/js/include", + "${devicemanager_path}/test/unittest/mock", + "${innerkits_path}/native_cpp/include", + "${innerkits_path}/native_cpp/include/ipc/standard", + "${innerkits_path}/native_cpp/include/ipc", + "${innerkits_path}/native_cpp/include/notify", + "${utils_path}/include", + "${utils_path}/include/crypto", + "${utils_path}/include/ipc/standard", + "${utils_path}/include/kvadapter", + "${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", + "${servicesimpl_path}/include/ipc/standard", + "${servicesimpl_path}/include/dependency/hichain", + "${servicesimpl_path}/include/devicestate", + "${servicesimpl_path}/include/ability", + "${servicesimpl_path}/include/config", + ] + + cflags = [ + "-g", + "-O0", + "-Dprivate=public", + "-Dprotected=public", + "-Werror", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "device_manager_impl_fuzzer.cpp" ] + + deps = [ + "${innerkits_path}/native_cpp:devicemanagersdk", + "${utils_path}:devicemanagerutils", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"DeviceManagerImplFuzzTest\"", + "LOG_DOMAIN=0xD004110", + ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "cJSON:cjson_static", + "c_utils:utils", + "device_auth:deviceauth_sdk", + "ipc:ipc_single", + "napi:ace_napi", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + "selinux_adapter:librestorecon", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + + deps = [ ":DeviceManagerImplFuzzTest" ] +} +############################################################################### diff --git a/test/interfacesfuzztest/devicemanagerimpl_fuzzer/corpus/init b/test/interfacesfuzztest/devicemanagerimpl_fuzzer/corpus/init new file mode 100755 index 000000000..79c5ba7b7 --- /dev/null +++ b/test/interfacesfuzztest/devicemanagerimpl_fuzzer/corpus/init @@ -0,0 +1,13 @@ +# Copyright (c) 2020-2021 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 diff --git a/test/interfacesfuzztest/devicemanagerimpl_fuzzer/device_manager_impl_fuzzer.cpp b/test/interfacesfuzztest/devicemanagerimpl_fuzzer/device_manager_impl_fuzzer.cpp new file mode 100755 index 000000000..0588d12c3 --- /dev/null +++ b/test/interfacesfuzztest/devicemanagerimpl_fuzzer/device_manager_impl_fuzzer.cpp @@ -0,0 +1,265 @@ +/* + * 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 + #include + #include + + #include "device_manager_impl.h" + + namespace OHOS { + namespace DistributedHardware { + + namespace { + + } + + void StopAuthenticateDeviceTest(const uint8_t* data, size_t size) + { + if ((data == nullptr) || (size = 0)) { + return; + } + FuzzedDataProvider fdp(data, size); + std::string pkgName = fdp.ConsumeRandomLengthString(); + DeviceManagerImpl::GetInstance().StopAuthenticateDevice(pkgName); + DeviceManagerImpl::GetInstance().OnDmServiceDied(); + } + + void UnBindDeviceTest(const uint8_t* data, size_t size) + { + if ((data == nullptr) || (size = 0)) { + return; + } + FuzzedDataProvider fdp(data, size); + std::string pkgName = fdp.ConsumeRandomLengthString(); + std::string deviceId = fdp.ConsumeRandomLengthString(); + DeviceManagerImpl::GetInstance().UnBindDevice(pkgName, deviceId); + } + + void ShiftLNNGearTest(const uint8_t* data, size_t size) + { + if ((data == nullptr) || (size = 0)) { + return; + } + FuzzedDataProvider fdp(data, size); + std::string pkgName = fdp.ConsumeRandomLengthString(); + DeviceManagerImpl::GetInstance().ShiftLNNGear(pkgName); + } + + void RegDevTrustChangeCallbackTest(const uint8_t* data, size_t size) + { + if ((data == nullptr) || (size = 0)) { + return; + } + FuzzedDataProvider fdp(data, size); + std::shared_ptr callback = nullptr; + std::string pkgName = fdp.ConsumeRandomLengthString(); + DeviceManagerImpl::GetInstance().RegDevTrustChangeCallback(pkgName, callback); + } + + void GetNetworkIdByUdidTest(const uint8_t* data, size_t size) + { + if ((data == nullptr) || (size = 0)) { + return; + } + FuzzedDataProvider fdp(data, size); + std::string pkgName = fdp.ConsumeRandomLengthString(); + std::string udid = fdp.ConsumeRandomLengthString(); + std::string networkId = fdp.ConsumeRandomLengthString(); + DeviceManagerImpl::GetInstance().GetNetworkIdByUdid(pkgName, udid, networkId); + } + + void RegisterCredentialAuthStatusCallbackTest(const uint8_t* data, size_t size) + { + if ((data == nullptr) || (size = 0)) { + return; + } + FuzzedDataProvider fdp(data, size); + std::string pkgName = fdp.ConsumeRandomLengthString(); + std::shared_ptr callback = nullptr; + DeviceManagerImpl::GetInstance().RegisterCredentialAuthStatusCallback(pkgName, callback); + } + + void GetAllTrustedDeviceListTest(const uint8_t* data, size_t size) + { + if ((data == nullptr) || (size = 0)) { + return; + } + FuzzedDataProvider fdp(data, size); + std::string pkgName = fdp.ConsumeRandomLengthString(); + std::string extra = fdp.ConsumeRandomLengthString(); + std::vector deviceList; + DeviceManagerImpl::GetInstance().GetAllTrustedDeviceList(pkgName, extra, deviceList); + } + + void RegisterSinkBindCallbackTest(const uint8_t* data, size_t size) + { + if ((data == nullptr) || (size = 0)) { + return; + } + FuzzedDataProvider fdp(data, size); + std::string pkgName = fdp.ConsumeRandomLengthString(); + std::shared_ptr callback = nullptr; + DeviceManagerImpl::GetInstance().RegisterSinkBindCallback(pkgName, callback); + } + + void GetDeviceProfileInfoListTest(const uint8_t* data, size_t size) + { + if ((data == nullptr) || (size = 0)) { + return; + } + FuzzedDataProvider fdp(data, size); + std::string pkgName = fdp.ConsumeRandomLengthString(); + DmDeviceProfileInfoFilterOptions filterOptions; + std::shared_ptr callback = nullptr; + DeviceManagerImpl::GetInstance().GetDeviceProfileInfoList(pkgName, filterOptions, callback); + } + + void GetDeviceIconInfoTest(const uint8_t* data, size_t size) + { + if ((data == nullptr) || (size = 0)) { + return; + } + FuzzedDataProvider fdp(data, size); + std::string pkgName = fdp.ConsumeRandomLengthString(); + DmDeviceProfileInfoFilterOptions filterOptions; + std::shared_ptr callback = nullptr; + DeviceManagerImpl::GetInstance().GetDeviceProfileInfoList(pkgName, filterOptions, callback); + } + + void PutDeviceProfileInfoListTest(const uint8_t* data, size_t size) + { + if ((data == nullptr) || (size = 0)) { + return; + } + FuzzedDataProvider fdp(data, size); + std::string pkgName = fdp.ConsumeRandomLengthString(); + std::vector deviceProfileInfoList; + DeviceManagerImpl::GetInstance().PutDeviceProfileInfoList(pkgName, deviceProfileInfoList); + } + + void GetLocalDisplayDeviceNameTest(const uint8_t* data, size_t size) + { + if ((data == nullptr) || (size < sizeof(int32_t))) { + return; + } + FuzzedDataProvider fdp(data, size); + std::string pkgName = fdp.ConsumeRandomLengthString(); + std::string displayName = fdp.ConsumeRandomLengthString(); + int32_t maxNameLength = fdp.ConsumeIntegral(); + DeviceManagerImpl::GetInstance().GetLocalDisplayDeviceName(pkgName, maxNameLength, displayName); + } + + void GetDeviceNetworkIdListTest(const uint8_t* data, size_t size) + { + if ((data == nullptr) || (size = 0)) { + return; + } + FuzzedDataProvider fdp(data, size); + std::string bundleName = fdp.ConsumeRandomLengthString(); + NetworkIdQueryFilter queryFilter; + std::vector networkIds; + DeviceManagerImpl::GetInstance().GetDeviceNetworkIdList(bundleName, queryFilter, networkIds); + } + + void SetLocalDeviceNameTest(const uint8_t* data, size_t size) + { + if ((data == nullptr) || (size = 0)) { + return; + } + FuzzedDataProvider fdp(data, size); + std::string pkgName = fdp.ConsumeRandomLengthString(); + std::string deviceName = fdp.ConsumeRandomLengthString(); + std::shared_ptr callback = nullptr; + DeviceManagerImpl::GetInstance().SetLocalDeviceName(pkgName, deviceName, callback); + } + + void SetRemoteDeviceNameTest(const uint8_t* data, size_t size) + { + if ((data == nullptr) || (size = 0)) { + return; + } + FuzzedDataProvider fdp(data, size); + std::string pkgName = fdp.ConsumeRandomLengthString(); + std::string deviceId = fdp.ConsumeRandomLengthString(); + std::string deviceName = fdp.ConsumeRandomLengthString(); + std::shared_ptr callback = nullptr; + DeviceManagerImpl::GetInstance().SetRemoteDeviceName(pkgName, deviceId, deviceName, callback); + } + + void RestoreLocalDeviceNameTest(const uint8_t* data, size_t size) + { + if ((data == nullptr) || (size = 0)) { + return; + } + FuzzedDataProvider fdp(data, size); + std::string pkgName = fdp.ConsumeRandomLengthString(); + DeviceManagerImpl::GetInstance().RestoreLocalDeviceName(pkgName); + } + + void GetLocalServiceInfoByBundleNameAndPinExchangeTypeTest(const uint8_t* data, size_t size) + { + if ((data == nullptr) || (size < sizeof(int32_t))) { + return; + } + FuzzedDataProvider fdp(data, size); + std::string bundleName = fdp.ConsumeRandomLengthString(); + int32_t maxNameLength = fdp.ConsumeIntegral(); + DMLocalServiceInfo info; + DeviceManagerImpl::GetInstance(). + GetLocalServiceInfoByBundleNameAndPinExchangeType(bundleName, maxNameLength, info); + } + + void UnRegisterPinHolderCallbackTest(const uint8_t* data, size_t size) + { + if ((data == nullptr) || (size = 0)) { + return; + } + FuzzedDataProvider fdp(data, size); + std::string pkgName = fdp.ConsumeRandomLengthString(); + DeviceManagerImpl::GetInstance().UnRegisterPinHolderCallback(pkgName); + } + } + } + + /* Fuzzer entry point */ + extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) + { + /* Run your code on data */ + OHOS::DistributedHardware::StopAuthenticateDeviceTest(data, size); + OHOS::DistributedHardware::UnBindDeviceTest(data, size); + OHOS::DistributedHardware::ShiftLNNGearTest(data, size); + OHOS::DistributedHardware::RegDevTrustChangeCallbackTest(data, size); + OHOS::DistributedHardware::GetNetworkIdByUdidTest(data, size); + OHOS::DistributedHardware::RegisterCredentialAuthStatusCallbackTest(data, size); + OHOS::DistributedHardware::GetAllTrustedDeviceListTest(data, size); + OHOS::DistributedHardware::RegisterSinkBindCallbackTest(data, size); + OHOS::DistributedHardware::GetDeviceProfileInfoListTest(data, size); + OHOS::DistributedHardware::GetDeviceIconInfoTest(data, size); + OHOS::DistributedHardware::PutDeviceProfileInfoListTest(data, size); + OHOS::DistributedHardware::GetLocalDisplayDeviceNameTest(data, size); + OHOS::DistributedHardware::GetDeviceNetworkIdListTest(data, size); + OHOS::DistributedHardware::SetLocalDeviceNameTest(data, size); + OHOS::DistributedHardware::SetRemoteDeviceNameTest(data, size); + OHOS::DistributedHardware::RestoreLocalDeviceNameTest(data, size); + OHOS::DistributedHardware::GetLocalServiceInfoByBundleNameAndPinExchangeTypeTest(data, size); + OHOS::DistributedHardware::UnRegisterPinHolderCallbackTest(data, size); + return 0; + } + \ No newline at end of file diff --git a/test/interfacesfuzztest/devicemanagerimpl_fuzzer/device_manager_impl_fuzzer.h b/test/interfacesfuzztest/devicemanagerimpl_fuzzer/device_manager_impl_fuzzer.h new file mode 100755 index 000000000..123bb1846 --- /dev/null +++ b/test/interfacesfuzztest/devicemanagerimpl_fuzzer/device_manager_impl_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 DEVICE_MANAGER_IMPL_FUZZER_TEST_H +#define DEVICE_MANAGER_IMPL_FUZZER_TEST_H + +#define FUZZ_PROJECT_NAME "devicemanagerimpl_fuzzer" + +#endif // DEVICE_MANAGER_IMPL_FUZZER_TEST_H diff --git a/test/interfacesfuzztest/devicemanagerimpl_fuzzer/project.xml b/test/interfacesfuzztest/devicemanagerimpl_fuzzer/project.xml new file mode 100755 index 000000000..afd90798d --- /dev/null +++ b/test/interfacesfuzztest/devicemanagerimpl_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + \ No newline at end of file -- Gitee