diff --git a/bundle.json b/bundle.json index dce07b821405036303ffcc5e34cd2aa96c496b1f..d06408362570a42ca4965c2ea74207ada2ab0225 100644 --- a/bundle.json +++ b/bundle.json @@ -47,7 +47,10 @@ "test": [ "//foundation/distributedhardware/distributedhardwarefwk/utils/test/unittest:utils_test", "//foundation/distributedhardware/distributedhardwarefwk/services/distributedhardwarefwkserviceimpl/test/unittest:svrimpl_test", - "//foundation/distributedhardware/distributedhardwarefwk/services/distributedhardwarefwkservice/test/unittest:test" + "//foundation/distributedhardware/distributedhardwarefwk/services/distributedhardwarefwkserviceimpl/test/fuzztest:fuzztest", + "//foundation/distributedhardware/distributedhardwarefwk/services/distributedhardwarefwkservice/test/unittest:test", + "//foundation/distributedhardware/distributedhardwarefwk/services/distributedhardwarefwkservice/test/fuzztest:fuzztest", + "//foundation/distributedhardware/distributedhardwarefwk/utils/test/fuzztest:fuzztest" ] } } diff --git a/services/distributedhardwarefwkservice/test/fuzztest/BUILD.gn b/services/distributedhardwarefwkservice/test/fuzztest/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..0ec6525c9fdfa13d8a9deba49ede5692dfb6b899 --- /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 0000000000000000000000000000000000000000..743157d59a4711559f2c010ad95e6398d067ca47 --- /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 0000000000000000000000000000000000000000..6b7212c8a6a9ee6e433ea43d0c2c2e96568ad4c4 --- /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 0000000000000000000000000000000000000000..85e7ef2c1cc6471e288306f6e3dcea5287a78b0e --- /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 0000000000000000000000000000000000000000..0bf3b6b14bc48472d4211b754d3543d5fbd0464b --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/test_AccessManager_fuzzer/test_AccessManager_fuzzer.cpp @@ -0,0 +1,57 @@ +/* + * 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 uint16_t TEST_DEV_TYPE_PAD = 0x11; +constexpr uint16_t STR_LEN = 32; + +bool AccessManagerFuzzTest(const uint8_t* data, size_t size) +{ + if (size > (STR_LEN * 2)) { + std::string networkId(reinterpret_cast(data), STR_LEN); + std::string uuid(reinterpret_cast(data + STR_LEN), STR_LEN); + + DistributedHardwareManagerFactory::GetInstance().SendOnLineEvent( + networkId, uuid, TEST_DEV_TYPE_PAD); + 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::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 0000000000000000000000000000000000000000..8b53c031d4ac0669c69cffe51caf794a92304b4b --- /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 0000000000000000000000000000000000000000..12fece32f744854a8c5a0c54e741d308cb74ef72 --- /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 0000000000000000000000000000000000000000..02bf0bd517bb67ca43ae72e20d9d05d511970007 --- /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 0000000000000000000000000000000000000000..6b7212c8a6a9ee6e433ea43d0c2c2e96568ad4c4 --- /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 0000000000000000000000000000000000000000..85e7ef2c1cc6471e288306f6e3dcea5287a78b0e --- /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 0000000000000000000000000000000000000000..12aa73350bfdf356cf3e2d2e58dcf59ecf59c6d7 --- /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 0000000000000000000000000000000000000000..1d8391e7f432341d40d8b92bdca8ee1702581156 --- /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 0000000000000000000000000000000000000000..1c137120a778ba2020d8a313d8c5f663b63b1a44 --- /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 0000000000000000000000000000000000000000..6b7212c8a6a9ee6e433ea43d0c2c2e96568ad4c4 --- /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 0000000000000000000000000000000000000000..85e7ef2c1cc6471e288306f6e3dcea5287a78b0e --- /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 0000000000000000000000000000000000000000..b628f35513e72eee3f8ec0d50a200921b20fafe2 --- /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 0000000000000000000000000000000000000000..32b39bf12eaceb318394fce7a4fc76ee9e27a6d4 --- /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 0000000000000000000000000000000000000000..bcc2a768857f8e279e601be8cb5cf2df548e072d --- /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 0000000000000000000000000000000000000000..1b3d2342647aad256ae14ab4fac5bc1a2d524375 --- /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 0000000000000000000000000000000000000000..6b7212c8a6a9ee6e433ea43d0c2c2e96568ad4c4 --- /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 0000000000000000000000000000000000000000..85e7ef2c1cc6471e288306f6e3dcea5287a78b0e --- /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 0000000000000000000000000000000000000000..c59f44244d0d06c2bf5dcc156fe51ce72271bf1f --- /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 0000000000000000000000000000000000000000..6f07d777a5f3d628de7b48fa5560c4c69996f2a9 --- /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" +