From 21f9d7126cea54fafe194a515a3ce992b302bcf9 Mon Sep 17 00:00:00 2001 From: litiangang4 Date: Fri, 19 May 2023 14:15:43 +0800 Subject: [PATCH] add Fuzz Signed-off-by: litiangang4 --- .../add_white_list_infos_call_back_test.cpp | 6 +- .../distributed_input_sinkmanager_test.cpp | 7 -- test/fuzztest/BUILD.gn | 4 +- .../dinputsinkhandler_fuzzer/BUILD.gn | 84 ++++++++++++++++ .../dinputsinkhandler_fuzzer/corpus/init | 14 +++ .../dinput_sink_handler_fuzzer.cpp | 82 ++++++++++++++++ .../dinput_sink_handler_fuzzer.h | 22 +++++ .../dinputsinkhandler_fuzzer/project.xml | 25 +++++ .../dinputsourcehandler_fuzzer/BUILD.gn | 84 ++++++++++++++++ .../dinputsourcehandler_fuzzer/corpus/init | 14 +++ .../dinput_source_handler_fuzzer.cpp | 96 +++++++++++++++++++ .../dinput_source_handler_fuzzer.h | 22 +++++ .../dinputsourcehandler_fuzzer/project.xml | 25 +++++ 13 files changed, 472 insertions(+), 13 deletions(-) create mode 100644 test/fuzztest/dinputsinkhandler_fuzzer/BUILD.gn create mode 100644 test/fuzztest/dinputsinkhandler_fuzzer/corpus/init create mode 100644 test/fuzztest/dinputsinkhandler_fuzzer/dinput_sink_handler_fuzzer.cpp create mode 100644 test/fuzztest/dinputsinkhandler_fuzzer/dinput_sink_handler_fuzzer.h create mode 100644 test/fuzztest/dinputsinkhandler_fuzzer/project.xml create mode 100644 test/fuzztest/dinputsourcehandler_fuzzer/BUILD.gn create mode 100644 test/fuzztest/dinputsourcehandler_fuzzer/corpus/init create mode 100644 test/fuzztest/dinputsourcehandler_fuzzer/dinput_source_handler_fuzzer.cpp create mode 100644 test/fuzztest/dinputsourcehandler_fuzzer/dinput_source_handler_fuzzer.h create mode 100644 test/fuzztest/dinputsourcehandler_fuzzer/project.xml diff --git a/interfaces/ipc/test/addwhitelistinfoscallbackunittest/add_white_list_infos_call_back_test.cpp b/interfaces/ipc/test/addwhitelistinfoscallbackunittest/add_white_list_infos_call_back_test.cpp index a89a270..0ca566b 100644 --- a/interfaces/ipc/test/addwhitelistinfoscallbackunittest/add_white_list_infos_call_back_test.cpp +++ b/interfaces/ipc/test/addwhitelistinfoscallbackunittest/add_white_list_infos_call_back_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 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 @@ -353,10 +353,6 @@ HWTEST_F(AddWhiteListInfosCallbackTest, DistributedInputSinkStub01, testing::ext ret = sinkProxy.RegisterGetSinkScreenInfosCallback(sinkScreenCb); EXPECT_EQ(DH_SUCCESS, ret); - SrcScreenInfo srcScreenInfo {"devid_test", "uuid_test", 1, 1, 1860, 980, "srcphyid", 1, 980, 490}; - ret = sinkProxy.NotifyStartDScreen(srcScreenInfo); - EXPECT_EQ(DH_SUCCESS, ret); - std::string srcScreenInfoKey = "srcScreenInfoKey_test"; ret = sinkProxy.NotifyStopDScreen(srcScreenInfoKey); EXPECT_EQ(DH_SUCCESS, ret); diff --git a/services/sink/sinkmanager/test/sinkmanagerunittest/distributed_input_sinkmanager_test.cpp b/services/sink/sinkmanager/test/sinkmanagerunittest/distributed_input_sinkmanager_test.cpp index c60c8d3..8820003 100644 --- a/services/sink/sinkmanager/test/sinkmanagerunittest/distributed_input_sinkmanager_test.cpp +++ b/services/sink/sinkmanager/test/sinkmanagerunittest/distributed_input_sinkmanager_test.cpp @@ -305,13 +305,6 @@ HWTEST_F(DistributedInputSinkManagerTest, UpdateSinkScreenInfoCache_01, testing: EXPECT_EQ(DH_SUCCESS, ret); } -HWTEST_F(DistributedInputSinkManagerTest, NotifyStartDScreen_01, testing::ext::TestSize.Level1) -{ - SrcScreenInfo srcScreenInfo {"devid_test", "uuid_test", 1, 1, 1860, 980, "srcphyid", 1, 980, 490}; - int32_t ret = sinkManager_->NotifyStartDScreen(srcScreenInfo); - EXPECT_EQ(DH_SUCCESS, ret); -} - HWTEST_F(DistributedInputSinkManagerTest, NotifyStopDScreen_01, testing::ext::TestSize.Level1) { std::string srcScreenInfoKey = ""; diff --git a/test/fuzztest/BUILD.gn b/test/fuzztest/BUILD.gn index a0c8a78..09f319e 100644 --- a/test/fuzztest/BUILD.gn +++ b/test/fuzztest/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2022 Huawei Device Co., Ltd. +# Copyright (c) 2022-2023 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 @@ -15,6 +15,8 @@ group("fuzztest") { testonly = true deps = [ + "dinputsinkhandler_fuzzer:fuzztest", + "dinputsourcehandler_fuzzer:fuzztest", "distributedinputclient_fuzzer:fuzztest", "distributedinputkit_fuzzer:fuzztest", "distributedinputsinktransport_fuzzer:fuzztest", diff --git a/test/fuzztest/dinputsinkhandler_fuzzer/BUILD.gn b/test/fuzztest/dinputsinkhandler_fuzzer/BUILD.gn new file mode 100644 index 0000000..908aaa1 --- /dev/null +++ b/test/fuzztest/dinputsinkhandler_fuzzer/BUILD.gn @@ -0,0 +1,84 @@ +# Copyright (c) 2021-2023 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("../../../distributedinput.gni") + +##############################fuzztest########################################## +ohos_fuzztest("DinputSinkHandlerFuzzTest") { + module_out_path = "distributed_input/system" + + fuzz_config_file = + "${distributedinput_path}/test/fuzztest/dinputsinkhandler_fuzzer" + + include_dirs = [ + "include", + "${distributedinput_path}/interfaces/inner_kits/include", + "${distributedinput_path}/interfaces/ipc/include", + "${distributedinput_path}/services/source/sourcemanager/include", + "${distributedinput_path}/services/sink/sinkmanager/include", + "${distributedinput_path}/frameworks/include", + "${distributedinput_path}/sourcehandler/include", + "${distributedinput_path}/sinkhandler/include", + "${distributedinput_path}/inputdevicehandler/include", + "${service_common}/include", + "${common_path}/include", + "${fwk_common_path}/log/include", + "${fwk_common_path}/utils/include", + "${fwk_utils_path}/include/log", + "${fwk_utils_path}/include", + "//third_party/json/include", + "${fwk_interfaces_path}/include", + "${fwk_interfaces_path}/include/ipc", + "${services_source_path}/inputinject/include", + "${utils_path}/include", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + + sources = [ "dinput_sink_handler_fuzzer.cpp" ] + + deps = [ + "${distributedinput_path}/sinkhandler:libdinput_sink_handler", + "//third_party/libevdev:libevdev", + ] + + external_deps = [ + "c_utils:utils", + "dsoftbus:softbus_client", + "eventhandler:libeventhandler", + "ipc:ipc_core", + "safwk:system_ability_fwk", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"DinputSinkHandlerFuzzTest\"", + "LOG_DOMAIN=0xD004100", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + + deps = [ ":DinputSinkHandlerFuzzTest" ] +} +############################################################################### diff --git a/test/fuzztest/dinputsinkhandler_fuzzer/corpus/init b/test/fuzztest/dinputsinkhandler_fuzzer/corpus/init new file mode 100644 index 0000000..e4ceac1 --- /dev/null +++ b/test/fuzztest/dinputsinkhandler_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 2023 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/test/fuzztest/dinputsinkhandler_fuzzer/dinput_sink_handler_fuzzer.cpp b/test/fuzztest/dinputsinkhandler_fuzzer/dinput_sink_handler_fuzzer.cpp new file mode 100644 index 0000000..2e66a6b --- /dev/null +++ b/test/fuzztest/dinputsinkhandler_fuzzer/dinput_sink_handler_fuzzer.cpp @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2023 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 "dinput_sink_handler_fuzzer.h" + +#include +#include +#include +#include +#include + +#include + +#include "constants_dinput.h" +#include "distributed_input_handler.h" +#include "distributed_input_kit.h" +#include "distributed_input_sink_handler.h" +#include "distributed_input_source_handler.h" +#include "i_distributed_sink_input.h" +#include "i_distributed_source_input.h" + +namespace OHOS { +namespace DistributedHardware { +void InitSinkFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size == 0)) { + return; + } + std::string params(reinterpret_cast(data), size); + DistributedInput::DistributedInputSinkHandler::GetInstance().InitSink(params); +} + +void ReleaseSinkFuzzTest(const uint8_t* data, size_t size) +{ + (void)data; + (void)size; + DistributedInput::DistributedInputSinkHandler::GetInstance().ReleaseSink(); +} + +void SubscribeLocalHardwareFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size == 0)) { + return; + } + std::string dhId(reinterpret_cast(data), size); + std::string params(reinterpret_cast(data), size); + DistributedInput::DistributedInputSinkHandler::GetInstance().SubscribeLocalHardware(dhId, params); +} + +void UnsubscribeLocalHardwareFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size == 0)) { + return; + } + std::string dhId(reinterpret_cast(data), size); + DistributedInput::DistributedInputSinkHandler::GetInstance().UnsubscribeLocalHardware(dhId); +} +} // 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::InitSinkFuzzTest(data, size); + OHOS::DistributedHardware::ReleaseSinkFuzzTest(data, size); + OHOS::DistributedHardware::SubscribeLocalHardwareFuzzTest(data, size); + OHOS::DistributedHardware::UnsubscribeLocalHardwareFuzzTest(data, size); + return 0; +} \ No newline at end of file diff --git a/test/fuzztest/dinputsinkhandler_fuzzer/dinput_sink_handler_fuzzer.h b/test/fuzztest/dinputsinkhandler_fuzzer/dinput_sink_handler_fuzzer.h new file mode 100644 index 0000000..5dd97ac --- /dev/null +++ b/test/fuzztest/dinputsinkhandler_fuzzer/dinput_sink_handler_fuzzer.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2023 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 DINPUT_SINK_HANDLER_FUZZER_H +#define DINPUT_SINK_HANDLER_FUZZER_H + +#define FUZZ_PROJECT_NAME "dinputsinkhandler_fuzzer" + +#endif // DINPUT_SINK_HANDLER_FUZZER_H \ No newline at end of file diff --git a/test/fuzztest/dinputsinkhandler_fuzzer/project.xml b/test/fuzztest/dinputsinkhandler_fuzzer/project.xml new file mode 100644 index 0000000..4fdbc40 --- /dev/null +++ b/test/fuzztest/dinputsinkhandler_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/dinputsourcehandler_fuzzer/BUILD.gn b/test/fuzztest/dinputsourcehandler_fuzzer/BUILD.gn new file mode 100644 index 0000000..76f3e60 --- /dev/null +++ b/test/fuzztest/dinputsourcehandler_fuzzer/BUILD.gn @@ -0,0 +1,84 @@ +# Copyright (c) 2023 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("../../../distributedinput.gni") + +##############################fuzztest########################################## +ohos_fuzztest("DinputSourceHandlerFuzzTest") { + module_out_path = "distributed_input/system" + + fuzz_config_file = + "${distributedinput_path}/test/fuzztest/dinputsourcehandler_fuzzer" + + include_dirs = [ + "include", + "${distributedinput_path}/interfaces/inner_kits/include", + "${distributedinput_path}/interfaces/ipc/include", + "${distributedinput_path}/services/source/sourcemanager/include", + "${distributedinput_path}/services/sink/sinkmanager/include", + "${distributedinput_path}/frameworks/include", + "${distributedinput_path}/sourcehandler/include", + "${distributedinput_path}/sinkhandler/include", + "${distributedinput_path}/inputdevicehandler/include", + "${service_common}/include", + "${common_path}/include", + "${fwk_common_path}/log/include", + "${fwk_common_path}/utils/include", + "${fwk_utils_path}/include/log", + "${fwk_utils_path}/include", + "//third_party/json/include", + "${fwk_interfaces_path}/include", + "${fwk_interfaces_path}/include/ipc", + "${services_source_path}/inputinject/include", + "${utils_path}/include", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + + sources = [ "dinput_source_handler_fuzzer.cpp" ] + + deps = [ + "${distributedinput_path}/sourcehandler:libdinput_source_handler", + "//third_party/libevdev:libevdev", + ] + + external_deps = [ + "c_utils:utils", + "dsoftbus:softbus_client", + "eventhandler:libeventhandler", + "ipc:ipc_core", + "safwk:system_ability_fwk", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"DinputSourceHandlerFuzzTest\"", + "LOG_DOMAIN=0xD004100", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + + deps = [ ":DinputSourceHandlerFuzzTest" ] +} +############################################################################### diff --git a/test/fuzztest/dinputsourcehandler_fuzzer/corpus/init b/test/fuzztest/dinputsourcehandler_fuzzer/corpus/init new file mode 100644 index 0000000..e4ceac1 --- /dev/null +++ b/test/fuzztest/dinputsourcehandler_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 2023 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/test/fuzztest/dinputsourcehandler_fuzzer/dinput_source_handler_fuzzer.cpp b/test/fuzztest/dinputsourcehandler_fuzzer/dinput_source_handler_fuzzer.cpp new file mode 100644 index 0000000..f5a5ba6 --- /dev/null +++ b/test/fuzztest/dinputsourcehandler_fuzzer/dinput_source_handler_fuzzer.cpp @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2023 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 "dinput_source_handler_fuzzer.h" + +#include +#include +#include +#include +#include + +#include + +#include "constants_dinput.h" +#include "distributed_input_handler.h" +#include "distributed_input_kit.h" +#include "distributed_input_sink_handler.h" +#include "distributed_input_source_handler.h" +#include "i_distributed_sink_input.h" +#include "i_distributed_source_input.h" + +namespace OHOS { +namespace DistributedHardware { +class TestRegisterInputCallback : public OHOS::DistributedHardware::RegisterCallback { +public: + TestRegisterInputCallback() = default; + virtual ~TestRegisterInputCallback() = default; + virtual int32_t OnRegisterResult(const std::string &devId, const std::string &dhId, int32_t status, + const std::string &data) override + { + return 0; + } +}; + +class TestUnregisterInputCallback : public OHOS::DistributedHardware::UnregisterCallback { +public: + TestUnregisterInputCallback() = default; + virtual ~TestUnregisterInputCallback() = default; + virtual int32_t OnUnregisterResult(const std::string &devId, const std::string &dhId, int32_t status, + const std::string &data) override + { + return 0; + } +}; + +void InitSourceFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size == 0)) { + return; + } + std::string params(reinterpret_cast(data), size); + DistributedInput::DistributedInputSourceHandler::GetInstance().InitSource(params); +} + +void ReleaseSourceFuzzTest(const uint8_t* data, size_t size) +{ + (void)data; + (void)size; + DistributedInput::DistributedInputSourceHandler::GetInstance().ReleaseSource(); +} + +void ConfigDistributedHardwareFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size == 0)) { + return; + } + std::string devId(reinterpret_cast(data), size); + std::string dhId(reinterpret_cast(data), size); + std::string key(reinterpret_cast(data), size); + std::string value(reinterpret_cast(data), size); + DistributedInput::DistributedInputSourceHandler::GetInstance().ConfigDistributedHardware(devId, dhId, key, value); +} +} // 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::InitSourceFuzzTest(data, size); + OHOS::DistributedHardware::ReleaseSourceFuzzTest(data, size); + OHOS::DistributedHardware::ConfigDistributedHardwareFuzzTest(data, size); + return 0; +} \ No newline at end of file diff --git a/test/fuzztest/dinputsourcehandler_fuzzer/dinput_source_handler_fuzzer.h b/test/fuzztest/dinputsourcehandler_fuzzer/dinput_source_handler_fuzzer.h new file mode 100644 index 0000000..dda0d85 --- /dev/null +++ b/test/fuzztest/dinputsourcehandler_fuzzer/dinput_source_handler_fuzzer.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2023 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 DINPUT_SOURCE_HANDLER_FUZZER_H +#define DINPUT_SOURCE_HANDLER_FUZZER_H + +#define FUZZ_PROJECT_NAME "dinputsourcehandler_fuzzer" + +#endif // DINPUT_SOURCE_HANDLER_FUZZER_H \ No newline at end of file diff --git a/test/fuzztest/dinputsourcehandler_fuzzer/project.xml b/test/fuzztest/dinputsourcehandler_fuzzer/project.xml new file mode 100644 index 0000000..4fdbc40 --- /dev/null +++ b/test/fuzztest/dinputsourcehandler_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + -- Gitee