diff --git a/services/distributedhardwarefwkservice/test/fuzztest/BUILD.gn b/services/distributedhardwarefwkservice/test/fuzztest/BUILD.gn index 8de46bca31df81236d969bf394a93d242a3d96ba..454cf537949b1f0aeb77b32d3c4e9f6215edbf44 100644 --- a/services/distributedhardwarefwkservice/test/fuzztest/BUILD.gn +++ b/services/distributedhardwarefwkservice/test/fuzztest/BUILD.gn @@ -18,6 +18,10 @@ group("fuzztest") { "accessmanager_fuzzer:fuzztest", "componentloader_fuzzer:fuzztest", "componentmanager_fuzzer:fuzztest", + "dhmanagerfactory_fuzzer:fuzztest", + "enabledcompsdump_fuzzer:fuzztest", + "publisher_fuzzer:fuzztest", + "publisheritem_fuzzer:fuzztest", "resourcemanager_fuzzer:fuzztest", "task_fuzzer:fuzztest", "versioninfomanager_fuzzer:fuzztest", diff --git a/services/distributedhardwarefwkservice/test/fuzztest/accessmanager_fuzzer/accessmanager_fuzzer.cpp b/services/distributedhardwarefwkservice/test/fuzztest/accessmanager_fuzzer/accessmanager_fuzzer.cpp index 68c7338674b79de73e307cdb94815e1c85c899b8..0053086a84f869e219b8d4df5323799195e7efe3 100644 --- a/services/distributedhardwarefwkservice/test/fuzztest/accessmanager_fuzzer/accessmanager_fuzzer.cpp +++ b/services/distributedhardwarefwkservice/test/fuzztest/accessmanager_fuzzer/accessmanager_fuzzer.cpp @@ -29,21 +29,20 @@ namespace OHOS { namespace DistributedHardware { namespace { - constexpr uint16_t TEST_DEV_TYPE_PAD = 0x11; constexpr uint32_t SLEEP_TIME_US = 10 * 1000; } void AccessManagerFuzzTest(const uint8_t* data, size_t size) { - if ((data == nullptr) || (size <= 0)) { + if ((data == nullptr) || (size <= sizeof(DmDeviceInfo))) { return; } - std::string networkId(reinterpret_cast(data), size); - std::string uuid(reinterpret_cast(data), size); + + AccessManager::GetInstance()->Init(); + const DmDeviceInfo deviceInfo = *(reinterpret_cast(data)); + AccessManager::GetInstance()->OnDeviceReady(deviceInfo); - DistributedHardwareManagerFactory::GetInstance().SendOnLineEvent( - networkId, uuid, TEST_DEV_TYPE_PAD); usleep(SLEEP_TIME_US); } } diff --git a/services/distributedhardwarefwkservice/test/fuzztest/dhmanagerfactory_fuzzer/BUILD.gn b/services/distributedhardwarefwkservice/test/fuzztest/dhmanagerfactory_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..001292fbc01c205473b42143f76bda3fead35e3d --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/dhmanagerfactory_fuzzer/BUILD.gn @@ -0,0 +1,62 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/test.gni") +import( + "//foundation/distributedhardware/distributed_hardware_fwk/distributedhardwarefwk.gni") + +##############################fuzztest########################################## +ohos_fuzztest("DhManagerFactoryFuzzTest") { + module_out_path = "distributed_hardware_fwk/dhmanagerfactory" + fuzz_config_file = "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkservice/test/fuzztest/dhmanagerfactory_fuzzer" + + include_dirs = [ + "//commonlibrary/c_utils/base/include", + "//utils/system/safwk/native/include", + "//foundation/distributedhardware/device_manager/interfaces/inner_kits/native_cpp/include", + "//foundation/distributedhardware/device_manager/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 = [ "dhmanagerfactory_fuzzer.cpp" ] + + deps = [ "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkservice:distributedhardwarefwksvr" ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"DhManagerFactoryFuzzTest\"", + "LOG_DOMAIN=0xD004100", + ] + + external_deps = [ "init:libbegetutil" ] +} + +############################################################################### +group("fuzztest") { + testonly = true + + deps = [ ":DhManagerFactoryFuzzTest" ] +} +############################################################################### diff --git a/services/distributedhardwarefwkservice/test/fuzztest/dhmanagerfactory_fuzzer/corpus/init b/services/distributedhardwarefwkservice/test/fuzztest/dhmanagerfactory_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..8eb5a7d6eb6b7d71f0c70c244e5768d62bee6ac5 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/dhmanagerfactory_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +FUZZ \ No newline at end of file diff --git a/services/distributedhardwarefwkservice/test/fuzztest/dhmanagerfactory_fuzzer/dhmanagerfactory_fuzzer.cpp b/services/distributedhardwarefwkservice/test/fuzztest/dhmanagerfactory_fuzzer/dhmanagerfactory_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4c00873f27fc4b20583827c00e56d98afb311a98 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/dhmanagerfactory_fuzzer/dhmanagerfactory_fuzzer.cpp @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dhmanagerfactory_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 { +namespace { + constexpr uint16_t TEST_DEV_TYPE_PAD = 0x11; + constexpr uint32_t SLEEP_TIME_US = 10 * 1000; +} + +void DhManagerFactoryFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size <= 0)) { + return; + } + + std::string networkId(reinterpret_cast(data), size); + std::string uuid(reinterpret_cast(data), size); + + DistributedHardwareManagerFactory::GetInstance().SendOnLineEvent( + networkId, uuid, TEST_DEV_TYPE_PAD); + usleep(SLEEP_TIME_US); +} +} +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::DistributedHardware::DhManagerFactoryFuzzTest(data, size); + return 0; +} + diff --git a/services/distributedhardwarefwkservice/test/fuzztest/dhmanagerfactory_fuzzer/dhmanagerfactory_fuzzer.h b/services/distributedhardwarefwkservice/test/fuzztest/dhmanagerfactory_fuzzer/dhmanagerfactory_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..d4ca1e17cf086a2ed4b32ea246cb72ad2dedcdc3 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/dhmanagerfactory_fuzzer/dhmanagerfactory_fuzzer.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef TEST_DHMANAGER_FACTORY_FUZZER_H +#define TEST_DHMANAGER_FACTORY_FUZZER_H + +#define FUZZ_PROJECT_NAME "dhmanagerfactory_fuzzer" + +#endif + diff --git a/services/distributedhardwarefwkservice/test/fuzztest/dhmanagerfactory_fuzzer/project.xml b/services/distributedhardwarefwkservice/test/fuzztest/dhmanagerfactory_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..6e8ad2cfde8f8bda4beb6cabbe7efd8bc3c54eec --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/dhmanagerfactory_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/services/distributedhardwarefwkservice/test/fuzztest/enabledcompsdump_fuzzer/BUILD.gn b/services/distributedhardwarefwkservice/test/fuzztest/enabledcompsdump_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..3ea6a9325215322e99d6c5b7eb4587ee5dde9153 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/enabledcompsdump_fuzzer/BUILD.gn @@ -0,0 +1,62 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/test.gni") +import( + "//foundation/distributedhardware/distributed_hardware_fwk/distributedhardwarefwk.gni") + +##############################fuzztest########################################## +ohos_fuzztest("EnabledCompsDumpFuzzTest") { + module_out_path = "distributed_hardware_fwk/enabledcompsdump" + fuzz_config_file = "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkservice/test/fuzztest/enabledcompsdump_fuzzer" + + include_dirs = [ + "//commonlibrary/c_utils/base/include", + "//utils/system/safwk/native/include", + "//foundation/distributedhardware/device_manager/interfaces/inner_kits/native_cpp/include", + "//foundation/distributedhardware/device_manager/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/hidumphelper", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "enabledcompsdump_fuzzer.cpp" ] + + deps = [ "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkservice:distributedhardwarefwksvr" ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"EnabledCompsDumpFuzzTest\"", + "LOG_DOMAIN=0xD004100", + ] + + external_deps = [ "init:libbegetutil" ] +} + +############################################################################### +group("fuzztest") { + testonly = true + + deps = [ ":EnabledCompsDumpFuzzTest" ] +} +############################################################################### diff --git a/services/distributedhardwarefwkservice/test/fuzztest/enabledcompsdump_fuzzer/corpus/init b/services/distributedhardwarefwkservice/test/fuzztest/enabledcompsdump_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..8eb5a7d6eb6b7d71f0c70c244e5768d62bee6ac5 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/enabledcompsdump_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +FUZZ \ No newline at end of file diff --git a/services/distributedhardwarefwkservice/test/fuzztest/enabledcompsdump_fuzzer/enabledcompsdump_fuzzer.cpp b/services/distributedhardwarefwkservice/test/fuzztest/enabledcompsdump_fuzzer/enabledcompsdump_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9d95ed1ec0131c0dbdd23db80aa9b7fb0e650405 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/enabledcompsdump_fuzzer/enabledcompsdump_fuzzer.cpp @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "enabledcompsdump_fuzzer.h" + +#include +#include +#include +#include +#include +#include + +#include "distributed_hardware_errno.h" +#include "enabled_comps_dump.h" + +namespace OHOS { +namespace DistributedHardware { +namespace { + const uint32_t DH_TYPE_SIZE = 10; + const DHType dhTypeFuzz[DH_TYPE_SIZE] = { + DHType::CAMERA, DHType::AUDIO, DHType::SCREEN, DHType::VIRMODEM_MIC, + DHType::INPUT, DHType::A2D, DHType::GPS, DHType::HFP, DHType::VIRMODEM_SPEAKER + }; +} + +void EnableedCompsDumpFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size <= 0)) { + return; + } + + std::string networkId(reinterpret_cast(data), size); + std::string dhId(reinterpret_cast(data), size); + DHType dhType = dhTypeFuzz[data[0] % DH_TYPE_SIZE]; + + std::set compInfoSet {}; + EnabledCompsDump::GetInstance().DumpEnabledComp(networkId, dhType, dhId); + EnabledCompsDump::GetInstance().Dump(compInfoSet); + EnabledCompsDump::GetInstance().DumpDisabledComp(networkId, dhType, dhId); +} +} +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::DistributedHardware::EnableedCompsDumpFuzzTest(data, size); + return 0; +} + diff --git a/services/distributedhardwarefwkservice/test/fuzztest/enabledcompsdump_fuzzer/enabledcompsdump_fuzzer.h b/services/distributedhardwarefwkservice/test/fuzztest/enabledcompsdump_fuzzer/enabledcompsdump_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..7d6e0a890a406bd6bd8d5dec7fabbd8a1297ab01 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/enabledcompsdump_fuzzer/enabledcompsdump_fuzzer.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef TEST_ENABLEEDCOMPSDUMP_FUZZER_H +#define TEST_ENABLEEDCOMPSDUMP_FUZZER_H + +#define FUZZ_PROJECT_NAME "enableedcompsdump_fuzzer" + +#endif + diff --git a/services/distributedhardwarefwkservice/test/fuzztest/enabledcompsdump_fuzzer/project.xml b/services/distributedhardwarefwkservice/test/fuzztest/enabledcompsdump_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..6e8ad2cfde8f8bda4beb6cabbe7efd8bc3c54eec --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/enabledcompsdump_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/services/distributedhardwarefwkservice/test/fuzztest/publisher_fuzzer/BUILD.gn b/services/distributedhardwarefwkservice/test/fuzztest/publisher_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..3461705f2da39c54ba76573ea826eb0dd088b82d --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/publisher_fuzzer/BUILD.gn @@ -0,0 +1,80 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/test.gni") +import( + "//foundation/distributedhardware/distributed_hardware_fwk/distributedhardwarefwk.gni") + +##############################fuzztest########################################## +ohos_fuzztest("PublisherFuzzTest") { + module_out_path = "distributed_hardware_fwk/publisher" + fuzz_config_file = "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkservice/test/fuzztest/publisher_fuzzer" + + include_dirs = [ + "//commonlibrary/c_utils/base/include", + "//utils/system/safwk/native/include", + "//foundation/distributedhardware/device_manager/interfaces/inner_kits/native_cpp/include", + "//foundation/distributedhardware/device_manager/common/include", + "//utils/system/safwk/native/include", + "${utils_path}/include", + "${utils_path}/include/log", + "${common_path}/utils/include", + "${common_path}/log/include", + "${innerkits_path}/include", + "${innerkits_path}/include/ipc", + "${services_path}/distributedhardwarefwkservice/include", + "${services_path}/distributedhardwarefwkservice/include/utils", + "${services_path}/distributedhardwarefwkservice/include/publisher", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "publisher_fuzzer.cpp" ] + + deps = [ + "${innerkits_path}:libdhfwk_sdk", + "${utils_path}:distributedhardwareutils", + "//foundation/distributedhardware/device_manager/interfaces/inner_kits/native_cpp:devicemanagersdk", + "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkservice:distributedhardwarefwksvr", + "//foundation/systemabilitymgr/safwk/interfaces/innerkits/safwk:system_ability_fwk", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"PublisherFuzzTest\"", + "LOG_DOMAIN=0xD004100", + ] + + external_deps = [ + "c_utils:utils", + "eventhandler:libeventhandler", + "init:libbegetutil", + "ipc:ipc_core", + "kv_store:distributeddata_inner", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + + deps = [ ":PublisherFuzzTest" ] +} +############################################################################### diff --git a/services/distributedhardwarefwkservice/test/fuzztest/publisher_fuzzer/corpus/init b/services/distributedhardwarefwkservice/test/fuzztest/publisher_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..8eb5a7d6eb6b7d71f0c70c244e5768d62bee6ac5 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/publisher_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +FUZZ \ No newline at end of file diff --git a/services/distributedhardwarefwkservice/test/fuzztest/publisher_fuzzer/project.xml b/services/distributedhardwarefwkservice/test/fuzztest/publisher_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..6e8ad2cfde8f8bda4beb6cabbe7efd8bc3c54eec --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/publisher_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/services/distributedhardwarefwkservice/test/fuzztest/publisher_fuzzer/publisher_fuzzer.cpp b/services/distributedhardwarefwkservice/test/fuzztest/publisher_fuzzer/publisher_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2d1c6e7c5a7bb141995628b7a427f16c7dc39c65 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/publisher_fuzzer/publisher_fuzzer.cpp @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "publisher_fuzzer.h" + +#include +#include +#include +#include +#include +#include + +#include "distributed_hardware_errno.h" +#include "publisher.h" + +namespace OHOS { +namespace DistributedHardware { +namespace { + const uint32_t TOPIC_SIZE = 4; + const DHTopic topicFuzz[TOPIC_SIZE] = { + DHTopic::TOPIC_START_DSCREEN, DHTopic::TOPIC_SINK_PROJECT_WINDOW_INFO, + DHTopic::TOPIC_STOP_DSCREEN, DHTopic::TOPIC_DEV_OFFLINE + }; +} + +void MockPublisherListener::OnMessage(const DHTopic topic, const std::string &message) +{ + (void)topic; + (void)message; +} + +int32_t MockPublisherListener::OnRemoteRequest( + uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + (void)code; + (void)data; + (void)reply; + (void)option; + return DH_FWK_SUCCESS; +} + +void PublisherListenerFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size <= 0)) { + return; + } + + DHTopic topic = topicFuzz[data[0] % TOPIC_SIZE]; + sptr listener = new MockPublisherListener(); + std::string message(reinterpret_cast(data), size); + + Publisher::GetInstance().RegisterListener(topic, listener); + Publisher::GetInstance().PublishMessage(topic, message); + Publisher::GetInstance().UnregisterListener(topic, listener); +} +} // namespace DistributedHardware +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::DistributedHardware::PublisherListenerFuzzTest(data, size); + return 0; +} + diff --git a/services/distributedhardwarefwkservice/test/fuzztest/publisher_fuzzer/publisher_fuzzer.h b/services/distributedhardwarefwkservice/test/fuzztest/publisher_fuzzer/publisher_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..92e46232cdf64299f38da083a55fc8be6e09e156 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/publisher_fuzzer/publisher_fuzzer.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef TEST_PUBLISHER_FUZZER_H +#define TEST_PUBLISHER_FUZZER_H + +#define FUZZ_PROJECT_NAME "publisher_fuzzer" + +#include + +#include "iremote_stub.h" + +#include "ipublisher_listener.h" +#include "single_instance.h" + +namespace OHOS { +namespace DistributedHardware { + +#define REMOVE_NO_USE_CONSTRUCTOR(className) \ +private: \ + className(const className&) = delete; \ + className& operator= (const className&) = delete; \ + className(className&&) = delete; \ + className& operator= (className&&) = delete; \ + +class MockPublisherListener : public IRemoteStub { +public: + MockPublisherListener() = default; + virtual ~MockPublisherListener() = default; + void OnMessage(const DHTopic topic, const std::string &message) override; + int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif + diff --git a/services/distributedhardwarefwkservice/test/fuzztest/publisheritem_fuzzer/BUILD.gn b/services/distributedhardwarefwkservice/test/fuzztest/publisheritem_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..76a64101fe0a4da65b87eaf4f802a5d355e4d48d --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/publisheritem_fuzzer/BUILD.gn @@ -0,0 +1,75 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/test.gni") +import( + "//foundation/distributedhardware/distributed_hardware_fwk/distributedhardwarefwk.gni") + +##############################fuzztest########################################## +ohos_fuzztest("PublisherItemFuzzTest") { + module_out_path = "distributed_hardware_fwk/publisheritem" + fuzz_config_file = "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkservice/test/fuzztest/publisheritem_fuzzer" + + include_dirs = [ + "//commonlibrary/c_utils/base/include", + "//utils/system/safwk/native/include", + "//foundation/distributedhardware/device_manager/interfaces/inner_kits/native_cpp/include", + "//foundation/distributedhardware/device_manager/common/include", + "${utils_path}/include", + "${utils_path}/include/log", + "${common_path}/utils/include", + "${common_path}/log/include", + "${innerkits_path}/include", + "${innerkits_path}/include/ipc", + "${services_path}/distributedhardwarefwkservice/include", + "${services_path}/distributedhardwarefwkservice/include/publisher", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "publisheritem_fuzzer.cpp" ] + + deps = [ + "${innerkits_path}:libdhfwk_sdk", + "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkservice:distributedhardwarefwksvr", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"PublisherItemFuzzTest\"", + "LOG_DOMAIN=0xD004100", + ] + + external_deps = [ + "c_utils:utils", + "eventhandler:libeventhandler", + "init:libbegetutil", + "ipc:ipc_core", + "kv_store:distributeddata_inner", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + + deps = [ ":PublisherItemFuzzTest" ] +} +############################################################################### diff --git a/services/distributedhardwarefwkservice/test/fuzztest/publisheritem_fuzzer/corpus/init b/services/distributedhardwarefwkservice/test/fuzztest/publisheritem_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..8eb5a7d6eb6b7d71f0c70c244e5768d62bee6ac5 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/publisheritem_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +FUZZ \ No newline at end of file diff --git a/services/distributedhardwarefwkservice/test/fuzztest/publisheritem_fuzzer/project.xml b/services/distributedhardwarefwkservice/test/fuzztest/publisheritem_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..6e8ad2cfde8f8bda4beb6cabbe7efd8bc3c54eec --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/publisheritem_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/services/distributedhardwarefwkservice/test/fuzztest/publisheritem_fuzzer/publisheritem_fuzzer.cpp b/services/distributedhardwarefwkservice/test/fuzztest/publisheritem_fuzzer/publisheritem_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4320275455397e5a138b2737d438db6667a91554 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/publisheritem_fuzzer/publisheritem_fuzzer.cpp @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "publisheritem_fuzzer.h" + +#include +#include +#include +#include +#include +#include + +#include "publisher_item.h" +#include "distributed_hardware_errno.h" + +namespace OHOS { +namespace DistributedHardware { +namespace { + const uint32_t TOPIC_SIZE = 4; + const DHTopic topicFuzz[TOPIC_SIZE] = { + DHTopic::TOPIC_START_DSCREEN, DHTopic::TOPIC_SINK_PROJECT_WINDOW_INFO, + DHTopic::TOPIC_STOP_DSCREEN, DHTopic::TOPIC_DEV_OFFLINE + }; +} + +void MockPublisherItemListener::OnMessage(const DHTopic topic, const std::string &message) +{ + (void)topic; + (void)message; +} + +int32_t MockPublisherItemListener::OnRemoteRequest( + uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + (void)code; + (void)data; + (void)reply; + (void)option; + return DH_FWK_SUCCESS; +} + +void PublisherItemFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size <= 0)) { + return; + } + + DHTopic topic = topicFuzz[data[0] % TOPIC_SIZE]; + sptr listener = new MockPublisherItemListener(); + std::string message(reinterpret_cast(data), size); + + PublisherItem publisherItem(topic); + + publisherItem.AddListener(listener); + publisherItem.PublishMessage(message); + publisherItem.RemoveListener(listener); +} +} // namespace DistributedHardware +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::DistributedHardware::PublisherItemFuzzTest(data, size); + return 0; +} + diff --git a/services/distributedhardwarefwkservice/test/fuzztest/publisheritem_fuzzer/publisheritem_fuzzer.h b/services/distributedhardwarefwkservice/test/fuzztest/publisheritem_fuzzer/publisheritem_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..cbfb552d445b70b5f1a5afed3131bd079843d8f0 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/publisheritem_fuzzer/publisheritem_fuzzer.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef TEST_PUBLISHER_ITEM_FUZZER_H +#define TEST_PUBLISHER_ITEM_FUZZER_H + +#define FUZZ_PROJECT_NAME "publisheritem_fuzzer" + +#include + +#include "iremote_stub.h" + +#include "ipublisher_listener.h" + +namespace OHOS { +namespace DistributedHardware { + +#define REMOVE_NO_USE_CONSTRUCTOR(className) \ +private: \ + className(const className&) = delete; \ + className& operator= (const className&) = delete; \ + className(className&&) = delete; \ + className& operator= (className&&) = delete; \ + +class MockPublisherItemListener : public IRemoteStub { +public: + MockPublisherItemListener() = default; + virtual ~MockPublisherItemListener() = default; + void OnMessage(const DHTopic topic, const std::string &message) override; + int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif +