diff --git a/services/distributedhardwarefwkservice/test/fuzztest/BUILD.gn b/services/distributedhardwarefwkservice/test/fuzztest/BUILD.gn index 8de46bca31df81236d969bf394a93d242a3d96ba..b0b83d57fcbcaf686a5b9b120425d4ccddba6558 100644 --- a/services/distributedhardwarefwkservice/test/fuzztest/BUILD.gn +++ b/services/distributedhardwarefwkservice/test/fuzztest/BUILD.gn @@ -18,6 +18,7 @@ group("fuzztest") { "accessmanager_fuzzer:fuzztest", "componentloader_fuzzer:fuzztest", "componentmanager_fuzzer:fuzztest", + "publisher_fuzzer:fuzztest", "resourcemanager_fuzzer:fuzztest", "task_fuzzer:fuzztest", "versioninfomanager_fuzzer:fuzztest", 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..a61257cad25c4eeb5ca720368febba7ba6808f88 --- /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 = [ + "${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", + "${innerkits_path}:libdhfwk_sdk", + ] + + 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..e98d3e7cf91b60512251441434679a41fdf4010c --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/publisher_fuzzer/publisher_fuzzer.cpp @@ -0,0 +1,79 @@ +/* + * 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 "publisher.h" +#include "distributed_hardware_errno.h" + +namespace OHOS { +namespace DistributedHardware { +namespace { + constexpr uint32_t SLEEP_TIME_US = 10 * 1000; + const uint32_t TOPIC_SIZE = 6; + const DHTopic topicFuzz[TOPIC_SIZE] = { + DHTopic::TOPIC_MIN, DHTopic::TOPIC_START_DSCREEN, DHTopic::TOPIC_SINK_PROJECT_WINDOW_INFO, + DHTopic::TOPIC_STOP_DSCREEN, DHTopic::TOPIC_DEV_OFFLINE, DHTopic::TOPIC_MAX + }; +} + +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); +} +} +} + +/* 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..36cd628c01266efb5fc957d7ecf38d0569e681e6 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/publisher_fuzzer/publisher_fuzzer.h @@ -0,0 +1,37 @@ +/* + * 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 "distributed_hardware_stub.h" + +namespace OHOS { +namespace DistributedHardware { +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 +