From 68f0c9aeebaa1d5f12fa6358435c06cf5f4d6174 Mon Sep 17 00:00:00 2001 From: dl Date: Wed, 23 Mar 2022 11:24:58 +0800 Subject: [PATCH 1/2] distributedhardwarefwkservice fuzz Signed-off-by: wanderer-dl122 --- .../test/fuzztest/BUILD.gn | 18 +++++ .../test_AccessManager_fuzzer/BUILD.gn | 63 ++++++++++++++++++ .../test_AccessManager_fuzzer/corpus/init | 1 + .../test_AccessManager_fuzzer/project.xml | 25 +++++++ .../test_AccessManager_fuzzer.cpp | 65 +++++++++++++++++++ .../test_AccessManager_fuzzer.h | 24 +++++++ 6 files changed, 196 insertions(+) create mode 100644 services/distributedhardwarefwkservice/test/fuzztest/BUILD.gn create mode 100644 services/distributedhardwarefwkservice/test/fuzztest/test_AccessManager_fuzzer/BUILD.gn create mode 100644 services/distributedhardwarefwkservice/test/fuzztest/test_AccessManager_fuzzer/corpus/init create mode 100644 services/distributedhardwarefwkservice/test/fuzztest/test_AccessManager_fuzzer/project.xml create mode 100644 services/distributedhardwarefwkservice/test/fuzztest/test_AccessManager_fuzzer/test_AccessManager_fuzzer.cpp create mode 100644 services/distributedhardwarefwkservice/test/fuzztest/test_AccessManager_fuzzer/test_AccessManager_fuzzer.h diff --git a/services/distributedhardwarefwkservice/test/fuzztest/BUILD.gn b/services/distributedhardwarefwkservice/test/fuzztest/BUILD.gn new file mode 100644 index 00000000..0ec6525c --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/BUILD.gn @@ -0,0 +1,18 @@ +# Copyright (c) 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. + +group("fuzztest") { + testonly = true + + deps = [ "test_AccessManager_fuzzer:fuzztest", ] +} diff --git a/services/distributedhardwarefwkservice/test/fuzztest/test_AccessManager_fuzzer/BUILD.gn b/services/distributedhardwarefwkservice/test/fuzztest/test_AccessManager_fuzzer/BUILD.gn new file mode 100644 index 00000000..dcee2021 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/test_AccessManager_fuzzer/BUILD.gn @@ -0,0 +1,63 @@ +# Copyright (c) 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/test.gni") +import("//foundation/distributedhardware/distributedhardwarefwk/distributedhardwarefwk.gni") + +##############################fuzztest########################################## +ohos_fuzztest("test_AccessManager_fuzzer") { + module_out_path = "distributed_hardware_fwk/test_AccessManager_fuzzer" + + include_dirs = [ + "//utils/native/base/include", + "//utils/system/safwk/native/include", + "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp/include", + "//foundation/distributedhardware/devicemanager/common/include", + "${utils_path}/include", + "${utils_path}/include/log", + "${common_path}/utils/include", + "${common_path}/log/include", + "${services_path}/distributedhardwarefwkservice/include", + "${services_path}/distributedhardwarefwkservice/include/accessmanager", + ] + cflags = ["-g","-O0","-Wno-unused-variable","-fno-omit-frame-pointer"] + sources = [ + "test_AccessManager_fuzzer.cpp", + ] + + deps = [ + "${utils_path}:distributedhardwareutils", + "//foundation/distributedhardware/distributedhardwarefwk/services/distributedhardwarefwkservice:distributedhardwarefwksvr", + "//foundation/distributedschedule/safwk/interfaces/innerkits/safwk:system_ability_fwk", + "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp:devicemanagersdk", + ] + + external_deps = [ + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_standard:samgr_proxy", + "startup_l2:syspara", + ] +} +############################################################################### +group("fuzztest") { + testonly = true + + deps = [ ":test_AccessManager_fuzzer" ] + +} +############################################################################### diff --git a/services/distributedhardwarefwkservice/test/fuzztest/test_AccessManager_fuzzer/corpus/init b/services/distributedhardwarefwkservice/test/fuzztest/test_AccessManager_fuzzer/corpus/init new file mode 100644 index 00000000..6b7212c8 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/test_AccessManager_fuzzer/corpus/init @@ -0,0 +1 @@ +FUZZ \ No newline at end of file diff --git a/services/distributedhardwarefwkservice/test/fuzztest/test_AccessManager_fuzzer/project.xml b/services/distributedhardwarefwkservice/test/fuzztest/test_AccessManager_fuzzer/project.xml new file mode 100644 index 00000000..85e7ef2c --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/test_AccessManager_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/services/distributedhardwarefwkservice/test/fuzztest/test_AccessManager_fuzzer/test_AccessManager_fuzzer.cpp b/services/distributedhardwarefwkservice/test/fuzztest/test_AccessManager_fuzzer/test_AccessManager_fuzzer.cpp new file mode 100644 index 00000000..8a0f36bc --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/test_AccessManager_fuzzer/test_AccessManager_fuzzer.cpp @@ -0,0 +1,65 @@ +/* + * Copyright (c) 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. + */ + +#include "test_AccessManager_fuzzer.h" + +#include +#include +#include +#include +#include +#include + +#include "access_manager.h" +#include "distributed_hardware_errno.h" +#include "distributed_hardware_manager_factory.h" + +constexpr int32_t INTERVAL_TIME_MS = 100; +constexpr uint16_t TEST_DEV_TYPE_PAD = 0x11; +constexpr uint16_t STR_LEN = 32; + +namespace OHOS { +namespace DistributedHardware { +bool SendOnOffLineEvent(const uint8_t* data, size_t size) +{ + if (size > sizeof(uint32_t) + STR_LEN * 2) { + std::string networkId(reinterpret_cast(data), STR_LEN); + std::string uuid(reinterpret_cast(data + STR_LEN), STR_LEN); + + auto ret = DistributedHardwareManagerFactory::GetInstance().SendOnLineEvent( + networkId, uuid, TEST_DEV_TYPE_PAD); + if (ret != DH_FWK_SUCCESS) { + return false; + } + + std::this_thread::sleep_for(std::chrono::milliseconds(INTERVAL_TIME_MS)); + + ret = DistributedHardwareManagerFactory::GetInstance().SendOffLineEvent( + networkId, uuid, TEST_DEV_TYPE_PAD); + + return (ret == DH_FWK_SUCCESS); + } +} +} +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::DistributedHardware::SendOnOffLineEvent(data, size); + return 0; +} + diff --git a/services/distributedhardwarefwkservice/test/fuzztest/test_AccessManager_fuzzer/test_AccessManager_fuzzer.h b/services/distributedhardwarefwkservice/test/fuzztest/test_AccessManager_fuzzer/test_AccessManager_fuzzer.h new file mode 100644 index 00000000..8b53c031 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/test_AccessManager_fuzzer/test_AccessManager_fuzzer.h @@ -0,0 +1,24 @@ +/* + * Copyright (c) 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. + */ + +#include +#include +#include +#include +#include +#include + +#define FUZZ_PROJECT_NAME "test_AccessManager_fuzzer" + -- Gitee From c1eb250a9e0f7a60b10186beb38517fad6fd7d72 Mon Sep 17 00:00:00 2001 From: dl Date: Wed, 23 Mar 2022 11:24:58 +0800 Subject: [PATCH 2/2] add distributedhardwarefwk fuzz Signed-off-by: wanderer-dl122 --- .../test/fuzztest/BUILD.gn | 18 ++++ .../test_AccessManager_fuzzer/BUILD.gn | 69 ++++++++++++ .../test_AccessManager_fuzzer/corpus/init | 1 + .../test_AccessManager_fuzzer/project.xml | 25 +++++ .../test_AccessManager_fuzzer.cpp | 67 ++++++++++++ .../test_AccessManager_fuzzer.h | 24 +++++ .../test/fuzztest/BUILD.gn | 21 ++++ .../test_ComponentManager_fuzzer/BUILD.gn | 58 ++++++++++ .../test_ComponentManager_fuzzer/corpus/init | 1 + .../test_ComponentManager_fuzzer/project.xml | 25 +++++ .../test_ComponentManager_fuzzer.cpp | 58 ++++++++++ .../test_ComponentManager_fuzzer.h | 24 +++++ .../test_ResourceManager_fuzzer/BUILD.gn | 67 ++++++++++++ .../test_ResourceManager_fuzzer/corpus/init | 1 + .../test_ResourceManager_fuzzer/project.xml | 25 +++++ .../test_ResourceManager_fuzzer.cpp | 102 ++++++++++++++++++ .../test_ResourceManager_fuzzer.h | 24 +++++ utils/test/fuzztest/BUILD.gn | 18 ++++ .../test/fuzztest/test_Utils_fuzzer/BUILD.gn | 43 ++++++++ .../fuzztest/test_Utils_fuzzer/corpus/init | 1 + .../fuzztest/test_Utils_fuzzer/project.xml | 25 +++++ .../test_Utils_fuzzer/test_Utils_fuzzer.cpp | 54 ++++++++++ .../test_Utils_fuzzer/test_Utils_fuzzer.h | 24 +++++ 23 files changed, 775 insertions(+) create mode 100644 services/distributedhardwarefwkservice/test/fuzztest/BUILD.gn create mode 100644 services/distributedhardwarefwkservice/test/fuzztest/test_AccessManager_fuzzer/BUILD.gn create mode 100644 services/distributedhardwarefwkservice/test/fuzztest/test_AccessManager_fuzzer/corpus/init create mode 100644 services/distributedhardwarefwkservice/test/fuzztest/test_AccessManager_fuzzer/project.xml create mode 100644 services/distributedhardwarefwkservice/test/fuzztest/test_AccessManager_fuzzer/test_AccessManager_fuzzer.cpp create mode 100644 services/distributedhardwarefwkservice/test/fuzztest/test_AccessManager_fuzzer/test_AccessManager_fuzzer.h create mode 100644 services/distributedhardwarefwkserviceimpl/test/fuzztest/BUILD.gn create mode 100644 services/distributedhardwarefwkserviceimpl/test/fuzztest/test_ComponentManager_fuzzer/BUILD.gn create mode 100644 services/distributedhardwarefwkserviceimpl/test/fuzztest/test_ComponentManager_fuzzer/corpus/init create mode 100644 services/distributedhardwarefwkserviceimpl/test/fuzztest/test_ComponentManager_fuzzer/project.xml create mode 100644 services/distributedhardwarefwkserviceimpl/test/fuzztest/test_ComponentManager_fuzzer/test_ComponentManager_fuzzer.cpp create mode 100644 services/distributedhardwarefwkserviceimpl/test/fuzztest/test_ComponentManager_fuzzer/test_ComponentManager_fuzzer.h create mode 100644 services/distributedhardwarefwkserviceimpl/test/fuzztest/test_ResourceManager_fuzzer/BUILD.gn create mode 100644 services/distributedhardwarefwkserviceimpl/test/fuzztest/test_ResourceManager_fuzzer/corpus/init create mode 100644 services/distributedhardwarefwkserviceimpl/test/fuzztest/test_ResourceManager_fuzzer/project.xml create mode 100644 services/distributedhardwarefwkserviceimpl/test/fuzztest/test_ResourceManager_fuzzer/test_ResourceManager_fuzzer.cpp create mode 100644 services/distributedhardwarefwkserviceimpl/test/fuzztest/test_ResourceManager_fuzzer/test_ResourceManager_fuzzer.h create mode 100644 utils/test/fuzztest/BUILD.gn create mode 100644 utils/test/fuzztest/test_Utils_fuzzer/BUILD.gn create mode 100644 utils/test/fuzztest/test_Utils_fuzzer/corpus/init create mode 100644 utils/test/fuzztest/test_Utils_fuzzer/project.xml create mode 100644 utils/test/fuzztest/test_Utils_fuzzer/test_Utils_fuzzer.cpp create mode 100644 utils/test/fuzztest/test_Utils_fuzzer/test_Utils_fuzzer.h diff --git a/services/distributedhardwarefwkservice/test/fuzztest/BUILD.gn b/services/distributedhardwarefwkservice/test/fuzztest/BUILD.gn new file mode 100644 index 00000000..0ec6525c --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/BUILD.gn @@ -0,0 +1,18 @@ +# Copyright (c) 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. + +group("fuzztest") { + testonly = true + + deps = [ "test_AccessManager_fuzzer:fuzztest", ] +} diff --git a/services/distributedhardwarefwkservice/test/fuzztest/test_AccessManager_fuzzer/BUILD.gn b/services/distributedhardwarefwkservice/test/fuzztest/test_AccessManager_fuzzer/BUILD.gn new file mode 100644 index 00000000..743157d5 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/test_AccessManager_fuzzer/BUILD.gn @@ -0,0 +1,69 @@ +# Copyright (c) 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/test.gni") +import("//foundation/distributedhardware/distributedhardwarefwk/distributedhardwarefwk.gni") + +##############################fuzztest########################################## +ohos_fuzztest("test_AccessManager_fuzzer") { + module_out_path = "distributed_hardware_fwk/test_AccessManager_fuzzer" + + include_dirs = [ + "//utils/native/base/include", + "//utils/system/safwk/native/include", + "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp/include", + "//foundation/distributedhardware/devicemanager/common/include", + "${utils_path}/include", + "${utils_path}/include/log", + "${common_path}/utils/include", + "${common_path}/log/include", + "${services_path}/distributedhardwarefwkservice/include", + "${services_path}/distributedhardwarefwkservice/include/accessmanager", + ] + cflags = ["-g","-O0","-Wno-unused-variable","-fno-omit-frame-pointer"] + sources = [ + "test_AccessManager_fuzzer.cpp", + ] + + deps = [ + "${utils_path}:distributedhardwareutils", + "//foundation/distributedhardware/distributedhardwarefwk/services/distributedhardwarefwkservice:distributedhardwarefwksvr", + "//foundation/distributedschedule/safwk/interfaces/innerkits/safwk:system_ability_fwk", + "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp:devicemanagersdk", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"test_AccessManager_fuzzer\"", + "LOG_DOMAIN=0xD004100", + ] + + external_deps = [ + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_standard:samgr_proxy", + "startup_l2:syspara", + ] +} +############################################################################### +group("fuzztest") { + testonly = true + + deps = [ ":test_AccessManager_fuzzer" ] + +} +############################################################################### diff --git a/services/distributedhardwarefwkservice/test/fuzztest/test_AccessManager_fuzzer/corpus/init b/services/distributedhardwarefwkservice/test/fuzztest/test_AccessManager_fuzzer/corpus/init new file mode 100644 index 00000000..6b7212c8 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/test_AccessManager_fuzzer/corpus/init @@ -0,0 +1 @@ +FUZZ \ No newline at end of file diff --git a/services/distributedhardwarefwkservice/test/fuzztest/test_AccessManager_fuzzer/project.xml b/services/distributedhardwarefwkservice/test/fuzztest/test_AccessManager_fuzzer/project.xml new file mode 100644 index 00000000..85e7ef2c --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/test_AccessManager_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/services/distributedhardwarefwkservice/test/fuzztest/test_AccessManager_fuzzer/test_AccessManager_fuzzer.cpp b/services/distributedhardwarefwkservice/test/fuzztest/test_AccessManager_fuzzer/test_AccessManager_fuzzer.cpp new file mode 100644 index 00000000..e8df0aaf --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/test_AccessManager_fuzzer/test_AccessManager_fuzzer.cpp @@ -0,0 +1,67 @@ +/* + * Copyright (c) 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. + */ + +#include "test_AccessManager_fuzzer.h" + +#include +#include +#include +#include +#include +#include + +#include "access_manager.h" +#include "distributed_hardware_errno.h" +#include "distributed_hardware_manager_factory.h" + +namespace OHOS { +namespace DistributedHardware { +constexpr int32_t INTERVAL_TIME_MS = 100; +constexpr uint16_t TEST_DEV_TYPE_PAD = 0x11; +constexpr uint16_t STR_LEN = 32; + +bool AccessManagerFuzzTest(const uint8_t* data, size_t size) +{ + if (size > sizeof(uint32_t) + STR_LEN * 2) { + std::string networkId(reinterpret_cast(data), STR_LEN); + std::string uuid(reinterpret_cast(data + STR_LEN), STR_LEN); + + auto ret = DistributedHardwareManagerFactory::GetInstance().SendOnLineEvent( + networkId, uuid, TEST_DEV_TYPE_PAD); + if (ret != DH_FWK_SUCCESS) { + return false; + } + + std::this_thread::sleep_for(std::chrono::milliseconds(INTERVAL_TIME_MS)); + + ret = DistributedHardwareManagerFactory::GetInstance().SendOffLineEvent( + networkId, uuid, TEST_DEV_TYPE_PAD); + + return (ret == DH_FWK_SUCCESS); + } else { + return false; + } +} +} +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::DistributedHardware::AccessManagerFuzzTest(data, size); + return 0; +} + diff --git a/services/distributedhardwarefwkservice/test/fuzztest/test_AccessManager_fuzzer/test_AccessManager_fuzzer.h b/services/distributedhardwarefwkservice/test/fuzztest/test_AccessManager_fuzzer/test_AccessManager_fuzzer.h new file mode 100644 index 00000000..8b53c031 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/test_AccessManager_fuzzer/test_AccessManager_fuzzer.h @@ -0,0 +1,24 @@ +/* + * Copyright (c) 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. + */ + +#include +#include +#include +#include +#include +#include + +#define FUZZ_PROJECT_NAME "test_AccessManager_fuzzer" + diff --git a/services/distributedhardwarefwkserviceimpl/test/fuzztest/BUILD.gn b/services/distributedhardwarefwkserviceimpl/test/fuzztest/BUILD.gn new file mode 100644 index 00000000..12fece32 --- /dev/null +++ b/services/distributedhardwarefwkserviceimpl/test/fuzztest/BUILD.gn @@ -0,0 +1,21 @@ +# Copyright (c) 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. + +group("fuzztest") { + testonly = true + + deps = [ + "test_ComponentManager_fuzzer:fuzztest", + "test_ResourceManager_fuzzer:fuzztest" + ] +} diff --git a/services/distributedhardwarefwkserviceimpl/test/fuzztest/test_ComponentManager_fuzzer/BUILD.gn b/services/distributedhardwarefwkserviceimpl/test/fuzztest/test_ComponentManager_fuzzer/BUILD.gn new file mode 100644 index 00000000..02bf0bd5 --- /dev/null +++ b/services/distributedhardwarefwkserviceimpl/test/fuzztest/test_ComponentManager_fuzzer/BUILD.gn @@ -0,0 +1,58 @@ +# Copyright (c) 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/test.gni") +import("//foundation/distributedhardware/distributedhardwarefwk/distributedhardwarefwk.gni") + +##############################fuzztest########################################## +ohos_fuzztest("test_ComponentManager_fuzzer") { + module_out_path = "distributed_hardware_fwk/test_ComponentManager_fuzzer" + + include_dirs = [ + "${utils_path}/include", + "${utils_path}/include/log", + "${services_path}/distributedhardwarefwkserviceimpl/include", + "${services_path}/distributedhardwarefwkserviceimpl/include/componentmanager", + "${services_path}/distributedhardwarefwkserviceimpl/include/utils", + "${common_path}/utils/include", + "${common_path}/log/include", + "//utils/native/base/include", + ] + cflags = ["-g","-O0","-Wno-unused-variable","-fno-omit-frame-pointer"] + sources = [ + "test_ComponentManager_fuzzer.cpp", + ] + + deps = [ + "//foundation/distributedhardware/distributedhardwarefwk/utils:distributedhardwareutils", + "//foundation/distributedhardware/distributedhardwarefwk/services/distributedhardwarefwkserviceimpl:distributedhardwarefwksvr_impl" + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"test_ComponentManager_fuzzer\"", + "LOG_DOMAIN=0xD004100", + ] + + external_deps = [ + "ipc:ipc_core", + ] +} +############################################################################### +group("fuzztest") { + testonly = true + deps = [ ":test_ComponentManager_fuzzer" ] +} +############################################################################### diff --git a/services/distributedhardwarefwkserviceimpl/test/fuzztest/test_ComponentManager_fuzzer/corpus/init b/services/distributedhardwarefwkserviceimpl/test/fuzztest/test_ComponentManager_fuzzer/corpus/init new file mode 100644 index 00000000..6b7212c8 --- /dev/null +++ b/services/distributedhardwarefwkserviceimpl/test/fuzztest/test_ComponentManager_fuzzer/corpus/init @@ -0,0 +1 @@ +FUZZ \ No newline at end of file diff --git a/services/distributedhardwarefwkserviceimpl/test/fuzztest/test_ComponentManager_fuzzer/project.xml b/services/distributedhardwarefwkserviceimpl/test/fuzztest/test_ComponentManager_fuzzer/project.xml new file mode 100644 index 00000000..85e7ef2c --- /dev/null +++ b/services/distributedhardwarefwkserviceimpl/test/fuzztest/test_ComponentManager_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/services/distributedhardwarefwkserviceimpl/test/fuzztest/test_ComponentManager_fuzzer/test_ComponentManager_fuzzer.cpp b/services/distributedhardwarefwkserviceimpl/test/fuzztest/test_ComponentManager_fuzzer/test_ComponentManager_fuzzer.cpp new file mode 100644 index 00000000..12aa7335 --- /dev/null +++ b/services/distributedhardwarefwkserviceimpl/test/fuzztest/test_ComponentManager_fuzzer/test_ComponentManager_fuzzer.cpp @@ -0,0 +1,58 @@ +/* + * Copyright (c) 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. + */ + +#include "test_ComponentManager_fuzzer.h" + +#include +#include + +#include "component_disable.h" +#include "component_enable.h" +#include "component_manager.h" +#include "constants.h" +#include "distributed_hardware_errno.h" +#include "distributed_hardware_log.h" + +namespace OHOS { +namespace DistributedHardware { +constexpr uint16_t STR_LEN = 32; + +bool ComponentManagerFuzzTest(const uint8_t* data, size_t size) +{ + if (size > (STR_LEN * 3)) { + std::string networkId(reinterpret_cast(data), STR_LEN); + std::string uuid(reinterpret_cast(data + STR_LEN), STR_LEN); + std::string dhId(reinterpret_cast(data + STR_LEN * 2), STR_LEN); + + ComponentManager::GetInstance().Init(); + ComponentManager::GetInstance().Enable(networkId, uuid, dhId); + ComponentManager::GetInstance().Disable(networkId, uuid, dhId); + ComponentManager::GetInstance().UnInit(); + return true; + } else { + return false; + } +} +} +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::DistributedHardware::ComponentManagerFuzzTest(data, size); + return 0; +} + diff --git a/services/distributedhardwarefwkserviceimpl/test/fuzztest/test_ComponentManager_fuzzer/test_ComponentManager_fuzzer.h b/services/distributedhardwarefwkserviceimpl/test/fuzztest/test_ComponentManager_fuzzer/test_ComponentManager_fuzzer.h new file mode 100644 index 00000000..1d8391e7 --- /dev/null +++ b/services/distributedhardwarefwkserviceimpl/test/fuzztest/test_ComponentManager_fuzzer/test_ComponentManager_fuzzer.h @@ -0,0 +1,24 @@ +/* + * Copyright (c) 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. + */ + +#include +#include +#include +#include +#include +#include + +#define FUZZ_PROJECT_NAME "test_ComponentManager_fuzzer" + diff --git a/services/distributedhardwarefwkserviceimpl/test/fuzztest/test_ResourceManager_fuzzer/BUILD.gn b/services/distributedhardwarefwkserviceimpl/test/fuzztest/test_ResourceManager_fuzzer/BUILD.gn new file mode 100644 index 00000000..1c137120 --- /dev/null +++ b/services/distributedhardwarefwkserviceimpl/test/fuzztest/test_ResourceManager_fuzzer/BUILD.gn @@ -0,0 +1,67 @@ +# Copyright (c) 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/test.gni") +import("//foundation/distributedhardware/distributedhardwarefwk/distributedhardwarefwk.gni") + +##############################fuzztest########################################## +ohos_fuzztest("test_ResourceManager_fuzzer") { + module_out_path = "distributed_hardware_fwk/test_ResourceManager_fuzzer" + + include_dirs = [ + "${utils_path}/include", + "${utils_path}/include/log", + "${utils_path}/include/eventbus", + "${services_path}/distributedhardwarefwkserviceimpl/include", + "${services_path}/distributedhardwarefwkserviceimpl/include/resourcemanager", + "${services_path}/distributedhardwarefwkserviceimpl/include/utils", + "${common_path}/utils/include", + "${common_path}/log/include", + "//utils/native/base/include", + "//third_party/json/include", + "//utils/native/base/include", + "//utils/system/safwk/native/include" + ] + cflags = ["-g","-O0","-Wno-unused-variable","-fno-omit-frame-pointer"] + sources = [ + "test_ResourceManager_fuzzer.cpp", + ] + + deps = [ + "//utils/native/base:utils", + "//foundation/distributedhardware/distributedhardwarefwk/utils:distributedhardwareutils", + "//foundation/distributedhardware/distributedhardwarefwk/services/distributedhardwarefwkserviceimpl:distributedhardwarefwksvr_impl" + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"test_ResourceManager_fuzzer\"", + "LOG_DOMAIN=0xD004100", + ] + + external_deps = [ + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "eventhandler:libeventhandler", + "hiviewdfx_hilog_native:libhilog", + "distributeddatamgr:distributeddata_inner" + ] +} +############################################################################### +group("fuzztest") { + testonly = true + deps = [ ":test_ResourceManager_fuzzer" ] +} +############################################################################### diff --git a/services/distributedhardwarefwkserviceimpl/test/fuzztest/test_ResourceManager_fuzzer/corpus/init b/services/distributedhardwarefwkserviceimpl/test/fuzztest/test_ResourceManager_fuzzer/corpus/init new file mode 100644 index 00000000..6b7212c8 --- /dev/null +++ b/services/distributedhardwarefwkserviceimpl/test/fuzztest/test_ResourceManager_fuzzer/corpus/init @@ -0,0 +1 @@ +FUZZ \ No newline at end of file diff --git a/services/distributedhardwarefwkserviceimpl/test/fuzztest/test_ResourceManager_fuzzer/project.xml b/services/distributedhardwarefwkserviceimpl/test/fuzztest/test_ResourceManager_fuzzer/project.xml new file mode 100644 index 00000000..85e7ef2c --- /dev/null +++ b/services/distributedhardwarefwkserviceimpl/test/fuzztest/test_ResourceManager_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/services/distributedhardwarefwkserviceimpl/test/fuzztest/test_ResourceManager_fuzzer/test_ResourceManager_fuzzer.cpp b/services/distributedhardwarefwkserviceimpl/test/fuzztest/test_ResourceManager_fuzzer/test_ResourceManager_fuzzer.cpp new file mode 100644 index 00000000..b628f355 --- /dev/null +++ b/services/distributedhardwarefwkserviceimpl/test/fuzztest/test_ResourceManager_fuzzer/test_ResourceManager_fuzzer.cpp @@ -0,0 +1,102 @@ +/* + * Copyright (c) 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. + */ + +#include "test_ResourceManager_fuzzer.h" + +#include +#include +#include +#include + +#include "capability_info.h" +#include "capability_info_manager.h" +#include "dh_context.h" +#include "distributed_hardware_errno.h" +#include "distributed_hardware_log.h" + +namespace OHOS { +namespace DistributedHardware { +constexpr uint16_t TEST_SIZE = 2; +constexpr uint16_t STR_LEN = 32; +constexpr uint16_t DHTYPE_SIZE = 5; +const DHType dhTypeFuzz[DHTYPE_SIZE] = { + DHType::CAMERA, DHType::MIC, DHType::SPEAKER, DHType::DISPLAY, DHType::BUTTON +}; + +struct CapabilityInfoFuzz { + char dhId_[STR_LEN]; + char deviceId_[STR_LEN]; + char deviceName_[STR_LEN]; + uint16_t deviceType_; + uint8_t dhType_; + char dhAttrs_[STR_LEN]; +}; + +void CreateCapabilityInfo(const uint8_t* data, std::shared_ptr &capInfo) +{ + const CapabilityInfoFuzz *capInfoFuzz = + reinterpret_cast(data); + + std::string dhId(capInfoFuzz->dhId_); + std::string devId(capInfoFuzz->deviceId_); + std::string devName(capInfoFuzz->deviceName_); + uint16_t devType = capInfoFuzz->deviceType_; + DHType dhType = dhTypeFuzz[capInfoFuzz->dhType_ % DHTYPE_SIZE]; + std::string dhAttrs(capInfoFuzz->dhAttrs_); + + capInfo = std::make_shared(dhId, devId, devName, devType, dhType, dhAttrs); +} + +bool ResourceManagerFuzzTest(const uint8_t* data, size_t size) +{ + if (size > sizeof(CapabilityInfoFuzz) * TEST_SIZE) { + std::shared_ptr capInfo1; + std::shared_ptr capInfo2; + std::vector> resInfos; + + CreateCapabilityInfo(data, capInfo1); + CreateCapabilityInfo(data + sizeof(CapabilityInfoFuzz), capInfo2); + resInfos.emplace_back(capInfo1); + resInfos.emplace_back(capInfo2); + CapabilityInfoManager::GetInstance()->AddCapability(resInfos); + + std::shared_ptr info; + CapabilityInfoManager::GetInstance()->GetCapability(capInfo1->GetDeviceId(), capInfo1->GetDHId(), info); + CapabilityInfoManager::GetInstance()->GetCapability(capInfo2->GetDeviceId(), capInfo2->GetDHId(), info); + CapabilityInfoManager::GetInstance()->GetDataByKey(capInfo1->GetKey(), info); + CapabilityInfoManager::GetInstance()->GetDataByKey(capInfo2->GetKey(), info); + + CapabilityInfoMap capMap; + CapabilityInfoManager::GetInstance()->GetDataByKeyPrefix(capInfo1->GetDeviceId(), capMap); + CapabilityInfoManager::GetInstance()->GetDataByKeyPrefix(capInfo1->GetDeviceId(), capMap); + + CapabilityInfoManager::GetInstance()->RemoveCapabilityInfoByKey(capInfo1->GetKey()); + CapabilityInfoManager::GetInstance()->RemoveCapabilityInfoByKey(capInfo2->GetKey()); + return true; + } else { + return false; + } +} +} +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::DistributedHardware::ResourceManagerFuzzTest(data, size); + return 0; +} + diff --git a/services/distributedhardwarefwkserviceimpl/test/fuzztest/test_ResourceManager_fuzzer/test_ResourceManager_fuzzer.h b/services/distributedhardwarefwkserviceimpl/test/fuzztest/test_ResourceManager_fuzzer/test_ResourceManager_fuzzer.h new file mode 100644 index 00000000..32b39bf1 --- /dev/null +++ b/services/distributedhardwarefwkserviceimpl/test/fuzztest/test_ResourceManager_fuzzer/test_ResourceManager_fuzzer.h @@ -0,0 +1,24 @@ +/* + * Copyright (c) 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. + */ + +#include +#include +#include +#include +#include +#include + +#define FUZZ_PROJECT_NAME "test_ResourceManager_fuzzer" + diff --git a/utils/test/fuzztest/BUILD.gn b/utils/test/fuzztest/BUILD.gn new file mode 100644 index 00000000..bcc2a768 --- /dev/null +++ b/utils/test/fuzztest/BUILD.gn @@ -0,0 +1,18 @@ +# Copyright (c) 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. + +group("fuzztest") { + testonly = true + + deps = [ "test_Utils_fuzzer:fuzztest", ] +} diff --git a/utils/test/fuzztest/test_Utils_fuzzer/BUILD.gn b/utils/test/fuzztest/test_Utils_fuzzer/BUILD.gn new file mode 100644 index 00000000..1b3d2342 --- /dev/null +++ b/utils/test/fuzztest/test_Utils_fuzzer/BUILD.gn @@ -0,0 +1,43 @@ +# Copyright (c) 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/test.gni") +import("//foundation/distributedhardware/distributedhardwarefwk/distributedhardwarefwk.gni") + +##############################fuzztest########################################## +ohos_fuzztest("test_Utils_fuzzer") { + module_out_path = "distributed_hardware_fwk/test_Utils_fuzzer" + + include_dirs = [ + "${utils_path}/include", + "${utils_path}/include/log", + "${common_path}/utils/include", + "//utils/native/base/include", + ] + cflags = ["-g","-O0","-Wno-unused-variable","-fno-omit-frame-pointer"] + sources = [ + "test_Utils_fuzzer.cpp", + ] + + deps = [ + "//foundation/distributedhardware/distributedhardwarefwk/utils:distributedhardwareutils" + ] +} +############################################################################### +group("fuzztest") { + testonly = true + deps = [ ":test_Utils_fuzzer", ] +} +############################################################################### diff --git a/utils/test/fuzztest/test_Utils_fuzzer/corpus/init b/utils/test/fuzztest/test_Utils_fuzzer/corpus/init new file mode 100644 index 00000000..6b7212c8 --- /dev/null +++ b/utils/test/fuzztest/test_Utils_fuzzer/corpus/init @@ -0,0 +1 @@ +FUZZ \ No newline at end of file diff --git a/utils/test/fuzztest/test_Utils_fuzzer/project.xml b/utils/test/fuzztest/test_Utils_fuzzer/project.xml new file mode 100644 index 00000000..85e7ef2c --- /dev/null +++ b/utils/test/fuzztest/test_Utils_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/utils/test/fuzztest/test_Utils_fuzzer/test_Utils_fuzzer.cpp b/utils/test/fuzztest/test_Utils_fuzzer/test_Utils_fuzzer.cpp new file mode 100644 index 00000000..c59f4424 --- /dev/null +++ b/utils/test/fuzztest/test_Utils_fuzzer/test_Utils_fuzzer.cpp @@ -0,0 +1,54 @@ +/* + * Copyright (c) 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. + */ + +#include "test_Utils_fuzzer.h" + +#include +#include + +#include "anonymous_string.h" +#include "dh_utils_tool.h" + + +namespace OHOS { +namespace DistributedHardware { +constexpr uint16_t STR_LEN = 32; + +bool UtilsFuzzerTet(const uint8_t* data, size_t size) +{ + if (size > STR_LEN) { + std::string str1(reinterpret_cast(data), STR_LEN); + std::string anonyStr1 = GetAnonyString(str1); + + std::string str2(reinterpret_cast(data), (*data) % STR_LEN); + std::string anonyStr2 = GetAnonyString(str2); + + int32_t i = *(reinterpret_cast(data)); + std::string anonyStri = GetAnonyInt32(i); + return true; + } + return false; +} +} +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::DistributedHardware::UtilsFuzzerTet(data, size); + return 0; +} + diff --git a/utils/test/fuzztest/test_Utils_fuzzer/test_Utils_fuzzer.h b/utils/test/fuzztest/test_Utils_fuzzer/test_Utils_fuzzer.h new file mode 100644 index 00000000..6f07d777 --- /dev/null +++ b/utils/test/fuzztest/test_Utils_fuzzer/test_Utils_fuzzer.h @@ -0,0 +1,24 @@ +/* + * Copyright (c) 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. + */ + +#include +#include +#include +#include +#include +#include + +#define FUZZ_PROJECT_NAME "test_Utils_fuzzer" + -- Gitee