From 003185615db41db191a7253babef107a50f3d05e Mon Sep 17 00:00:00 2001 From: wangbin Date: Sat, 7 Jun 2025 18:02:07 +0800 Subject: [PATCH] =?UTF-8?q?fuzz=E8=A6=86=E7=9B=96=E7=8E=87=E6=8F=90?= =?UTF-8?q?=E5=8D=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangbin --- .../networkdelegatenormalimpl_fuzzer/BUILD.gn | 67 +++++++++++ .../corpus/init | 16 +++ .../networkdelegatenormalimpl_fuzzer.cpp | 89 +++++++++++++++ .../networkdelegatenormalimpl_fuzzer.h | 21 ++++ .../project.xml | 25 +++++ .../test/fuzztest/screenlock_fuzzer/BUILD.gn | 55 +++++++++ .../fuzztest/screenlock_fuzzer/corpus/init | 16 +++ .../fuzztest/screenlock_fuzzer/project.xml | 25 +++++ .../screenlock_fuzzer/screenlock_fuzzer.cpp | 106 ++++++++++++++++++ .../screenlock_fuzzer/screenlock_fuzzer.h | 21 ++++ .../adapter/test/BUILD.gn | 9 +- 11 files changed, 448 insertions(+), 2 deletions(-) create mode 100644 services/distributeddataservice/adapter/network/test/fuzztest/networkdelegatenormalimpl_fuzzer/BUILD.gn create mode 100644 services/distributeddataservice/adapter/network/test/fuzztest/networkdelegatenormalimpl_fuzzer/corpus/init create mode 100644 services/distributeddataservice/adapter/network/test/fuzztest/networkdelegatenormalimpl_fuzzer/networkdelegatenormalimpl_fuzzer.cpp create mode 100644 services/distributeddataservice/adapter/network/test/fuzztest/networkdelegatenormalimpl_fuzzer/networkdelegatenormalimpl_fuzzer.h create mode 100644 services/distributeddataservice/adapter/network/test/fuzztest/networkdelegatenormalimpl_fuzzer/project.xml create mode 100644 services/distributeddataservice/adapter/screenlock/test/fuzztest/screenlock_fuzzer/BUILD.gn create mode 100644 services/distributeddataservice/adapter/screenlock/test/fuzztest/screenlock_fuzzer/corpus/init create mode 100644 services/distributeddataservice/adapter/screenlock/test/fuzztest/screenlock_fuzzer/project.xml create mode 100644 services/distributeddataservice/adapter/screenlock/test/fuzztest/screenlock_fuzzer/screenlock_fuzzer.cpp create mode 100644 services/distributeddataservice/adapter/screenlock/test/fuzztest/screenlock_fuzzer/screenlock_fuzzer.h diff --git a/services/distributeddataservice/adapter/network/test/fuzztest/networkdelegatenormalimpl_fuzzer/BUILD.gn b/services/distributeddataservice/adapter/network/test/fuzztest/networkdelegatenormalimpl_fuzzer/BUILD.gn new file mode 100644 index 000000000..6dcd6a6d2 --- /dev/null +++ b/services/distributeddataservice/adapter/network/test/fuzztest/networkdelegatenormalimpl_fuzzer/BUILD.gn @@ -0,0 +1,67 @@ +# Copyright (c) 2025 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/distributeddatamgr/datamgr_service/datamgr_service.gni") + +##############################fuzztest########################################## +ohos_fuzztest("NetworkDelegateNormalImplFuzzTest") { + module_out_path = "datamgr_service/datamgr_service" + + fuzz_config_file = "${data_service_path}/adapter/network/test/fuzztest/networkdelegatenormalimpl_fuzzer" + + sources = [ + "${data_service_path}/framework/network/network_delegate.cpp", + "networkdelegatenormalimpl_fuzzer.cpp", + ] + + cflags = [ + "-Dprivate=public", + "-Dprotected=public", + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + + include_dirs = [ + "${data_service_path}/adapter/include/communicator", + "${data_service_path}/framework/include/network", + "${data_service_path}/adapter/include/utils", + "${data_service_path}/framework/include", + "${data_service_path}/adapter/network/src", + "${data_service_path}/adapter/include", + ] + + deps = [ + "${data_service_path}/adapter/communicator:distributeddata_communicator", + ] + + external_deps = [ + "c_utils:utils", + "device_manager:devicemanagersdk", + "hilog:libhilog", + "ipc:ipc_core", + "kv_store:datamgr_common", + "netmanager_base:net_conn_manager_if", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + + deps = [ ":NetworkDelegateNormalImplFuzzTest" ] +} +############################################################################### diff --git a/services/distributeddataservice/adapter/network/test/fuzztest/networkdelegatenormalimpl_fuzzer/corpus/init b/services/distributeddataservice/adapter/network/test/fuzztest/networkdelegatenormalimpl_fuzzer/corpus/init new file mode 100644 index 000000000..7ade8a0fa --- /dev/null +++ b/services/distributeddataservice/adapter/network/test/fuzztest/networkdelegatenormalimpl_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 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/distributeddataservice/adapter/network/test/fuzztest/networkdelegatenormalimpl_fuzzer/networkdelegatenormalimpl_fuzzer.cpp b/services/distributeddataservice/adapter/network/test/fuzztest/networkdelegatenormalimpl_fuzzer/networkdelegatenormalimpl_fuzzer.cpp new file mode 100644 index 000000000..f06ce4a59 --- /dev/null +++ b/services/distributeddataservice/adapter/network/test/fuzztest/networkdelegatenormalimpl_fuzzer/networkdelegatenormalimpl_fuzzer.cpp @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2025 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 "network_delegate.h" +#include "network_delegate_normal_impl.h" +#include "network_delegate_normal_impl.cpp" +#include + +using namespace OHOS::DistributedData; + +namespace OHOS { +void GetNetworkTypeFuzz(FuzzedDataProvider &provider) +{ + NetworkDelegateNormalImpl delegate; + delegate.Init(); + bool retrieve = provider.ConsumeIntegral(); + delegate.GetNetworkType(retrieve); + + sptr observer = new (std::nothrow) NetConnCallbackObserver(delegate); + sptr netHandle = new (std::nothrow) NetHandle(); + sptr netAllCap = new (std::nothrow) NetAllCapabilities(); + observer->NetCapabilitiesChange(netHandle, netAllCap); + netAllCap->netCaps_.insert(NetManagerStandard::NET_CAPABILITY_VALIDATED); + observer->NetCapabilitiesChange(netHandle, netAllCap); + observer->NetAvailable(netHandle); + observer->NetUnavailable(); +} + +void GetTaskFuzz(FuzzedDataProvider &provider) +{ + NetworkDelegateNormalImpl delegate; + delegate.Init(); + uint32_t retry = provider.ConsumeIntegral(); + delegate.GetTask(retry); + + sptr observer = new (std::nothrow) NetConnCallbackObserver(delegate); + sptr netHandle = new (std::nothrow) NetHandle(); + sptr info = new (std::nothrow) NetLinkInfo(); + observer->NetConnectionPropertiesChange(netHandle, info); + observer->NetLost(netHandle); + delegate.RegOnNetworkChange(); +} + +void BindExecutorFuzz(FuzzedDataProvider &provider) +{ + NetworkDelegateNormalImpl delegate; + delegate.Init(); + size_t max = provider.ConsumeIntegralInRange(12, 15); + size_t min = provider.ConsumeIntegralInRange(5, 8); + auto executor = std::make_shared(max, min); + delegate.BindExecutor(executor); + delegate.IsNetworkAvailable(); +} + +void NetBlockStatusChangeFuzz(FuzzedDataProvider &provider) +{ + NetworkDelegateNormalImpl delegate; + delegate.Init(); + sptr observer = new (std::nothrow) NetConnCallbackObserver(delegate); + sptr netHandle = nullptr; + bool blocked = provider.ConsumeIntegral(); + observer->NetBlockStatusChange(netHandle, blocked); + delegate.RefreshNet(); +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + FuzzedDataProvider provider(data, size); + OHOS::GetNetworkTypeFuzz(provider); + OHOS::GetTaskFuzz(provider); + OHOS::BindExecutorFuzz(provider); + OHOS::NetBlockStatusChangeFuzz(provider); + return 0; +} \ No newline at end of file diff --git a/services/distributeddataservice/adapter/network/test/fuzztest/networkdelegatenormalimpl_fuzzer/networkdelegatenormalimpl_fuzzer.h b/services/distributeddataservice/adapter/network/test/fuzztest/networkdelegatenormalimpl_fuzzer/networkdelegatenormalimpl_fuzzer.h new file mode 100644 index 000000000..1ae55e23e --- /dev/null +++ b/services/distributeddataservice/adapter/network/test/fuzztest/networkdelegatenormalimpl_fuzzer/networkdelegatenormalimpl_fuzzer.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 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 NETWORKDELEGATENORMALIMPL_FUZZER_H +#define NETWORKDELEGATENORMALIMPL_FUZZER_H + +#define FUZZ_PROJECT_NAME "networkdelegatenormalimpl_fuzzer" + +#endif // NETWORKDELEGATENORMALIMPL_FUZZER_H \ No newline at end of file diff --git a/services/distributeddataservice/adapter/network/test/fuzztest/networkdelegatenormalimpl_fuzzer/project.xml b/services/distributeddataservice/adapter/network/test/fuzztest/networkdelegatenormalimpl_fuzzer/project.xml new file mode 100644 index 000000000..a37a23b15 --- /dev/null +++ b/services/distributeddataservice/adapter/network/test/fuzztest/networkdelegatenormalimpl_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + \ No newline at end of file diff --git a/services/distributeddataservice/adapter/screenlock/test/fuzztest/screenlock_fuzzer/BUILD.gn b/services/distributeddataservice/adapter/screenlock/test/fuzztest/screenlock_fuzzer/BUILD.gn new file mode 100644 index 000000000..ef3c097db --- /dev/null +++ b/services/distributeddataservice/adapter/screenlock/test/fuzztest/screenlock_fuzzer/BUILD.gn @@ -0,0 +1,55 @@ +# Copyright (c) 2025 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/distributeddatamgr/datamgr_service/datamgr_service.gni") + +##############################fuzztest########################################## +ohos_fuzztest("ScreenlockFuzzTest") { + module_out_path = "datamgr_service/datamgr_service" + + fuzz_config_file = + "${data_service_path}/adapter/screenlock/test/fuzztest/screenlock_fuzzer" + + sources = [ "screenlock_fuzzer.cpp" ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + "-Dprivate=public", + "-Dprotected=public", + ] + + deps = [ + "${data_service_path}/adapter/screenlock:distributeddata_screenlock", + "${data_service_path}/framework:distributeddatasvcfwk", + ] + + external_deps = [ + "c_utils:utils", + "common_event_service:cesfwk_innerkits", + "hilog:libhilog", + "kv_store:datamgr_common", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + + deps = [ ":ScreenlockFuzzTest" ] +} +############################################################################### diff --git a/services/distributeddataservice/adapter/screenlock/test/fuzztest/screenlock_fuzzer/corpus/init b/services/distributeddataservice/adapter/screenlock/test/fuzztest/screenlock_fuzzer/corpus/init new file mode 100644 index 000000000..7ade8a0fa --- /dev/null +++ b/services/distributeddataservice/adapter/screenlock/test/fuzztest/screenlock_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 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/distributeddataservice/adapter/screenlock/test/fuzztest/screenlock_fuzzer/project.xml b/services/distributeddataservice/adapter/screenlock/test/fuzztest/screenlock_fuzzer/project.xml new file mode 100644 index 000000000..a37a23b15 --- /dev/null +++ b/services/distributeddataservice/adapter/screenlock/test/fuzztest/screenlock_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + \ No newline at end of file diff --git a/services/distributeddataservice/adapter/screenlock/test/fuzztest/screenlock_fuzzer/screenlock_fuzzer.cpp b/services/distributeddataservice/adapter/screenlock/test/fuzztest/screenlock_fuzzer/screenlock_fuzzer.cpp new file mode 100644 index 000000000..0d6bb47b4 --- /dev/null +++ b/services/distributeddataservice/adapter/screenlock/test/fuzztest/screenlock_fuzzer/screenlock_fuzzer.cpp @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2025 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 "screenlock_fuzzer.h" +#include "screenlock/screen_lock.h" + +using namespace OHOS::DistributedData; +namespace OHOS { +class ScreenLockObserver : public ScreenManager::Observer { +public: + void OnScreenUnlocked(int32_t user) override + { + } + + std::string GetName() override + { + return name_; + } + + void SetName(const std::string &name) + { + name_ = name; + } + +private: + std::string name_ = "screenTestObserver"; +}; + +void SubscribeFuzz(FuzzedDataProvider &provider) +{ + auto screenLock = std::make_shared(); + screenLock->Subscribe(nullptr); + auto observer = std::make_shared(); + std::string name = provider.ConsumeRandomLengthString(); + observer->SetName(name); + screenLock->Subscribe(observer); + screenLock->IsLocked(); +} + +void UnsubscribeFuzz(FuzzedDataProvider &provider) +{ + auto screenLock = std::make_shared(); + auto observer = std::make_shared(); + std::string name = provider.ConsumeRandomLengthString(); + observer->SetName(name); + screenLock->Subscribe(observer); + screenLock->Unsubscribe(observer); + screenLock->UnsubscribeScreenEvent(); +} + +void GetTaskFuzz(FuzzedDataProvider &provider) +{ + auto screenLock = std::make_shared(); + int retry = provider.ConsumeIntegralInRange(1, 20); + screenLock->GetTask(retry); +} + +void NotifyScreenUnlockedFuzz(FuzzedDataProvider &provider) +{ + auto screenLock = std::make_shared(); + int user = provider.ConsumeIntegral(); + screenLock->NotifyScreenUnlocked(user); +} + +void OnReceiveEventFuzz(FuzzedDataProvider &provider) +{ + OHOS::EventFwk::MatchingSkills matchingSkills; + std::string reserved = provider.ConsumeRandomLengthString(); + matchingSkills.AddEvent(reserved); + OHOS::EventFwk::CommonEventSubscribeInfo info(matchingSkills); + int priority = provider.ConsumeIntegral(); + info.SetPriority(priority); + info.SetPermission("ohos.permission.SUBSCRIBE_SCREEN_LOCK"); + auto subscriber = std::make_shared(info); + OHOS::AAFwk::Want want; + want.SetAction(CommonEventSupport::COMMON_EVENT_HWID_LOGIN); + CommonEventData commonEventData(want); + CommonEventData event(want); + subscriber->OnReceiveEvent(event); +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + FuzzedDataProvider provider(data, size); + OHOS::SubscribeFuzz(provider); + OHOS::UnsubscribeFuzz(provider); + OHOS::GetTaskFuzz(provider); + OHOS::NotifyScreenUnlockedFuzz(provider); + OHOS::OnReceiveEventFuzz(provider); + return 0; +} \ No newline at end of file diff --git a/services/distributeddataservice/adapter/screenlock/test/fuzztest/screenlock_fuzzer/screenlock_fuzzer.h b/services/distributeddataservice/adapter/screenlock/test/fuzztest/screenlock_fuzzer/screenlock_fuzzer.h new file mode 100644 index 000000000..2ae3c12cd --- /dev/null +++ b/services/distributeddataservice/adapter/screenlock/test/fuzztest/screenlock_fuzzer/screenlock_fuzzer.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 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 SCREENLOCK_FUZZER_H +#define SCREENLOCK_FUZZER_H + +#define FUZZ_PROJECT_NAME "screenlock_fuzzer" + +#endif // SCREENLOCK_FUZZER_H \ No newline at end of file diff --git a/services/distributeddataservice/adapter/test/BUILD.gn b/services/distributeddataservice/adapter/test/BUILD.gn index db76543eb..8442da87f 100755 --- a/services/distributeddataservice/adapter/test/BUILD.gn +++ b/services/distributeddataservice/adapter/test/BUILD.gn @@ -24,7 +24,8 @@ group("unittest") { "../dfx/test:unittest", ] - if (defined(global_parts_info) && defined(global_parts_info.theme_screenlock_mgr)) { + if (defined(global_parts_info) && + defined(global_parts_info.theme_screenlock_mgr)) { deps += [ "${data_service_path}/adapter/screenlock/test:unittest" ] } @@ -36,5 +37,9 @@ group("unittest") { group("fuzztest") { testonly = true - deps = [ "../communicator/test/fuzztest:fuzztest" ] + deps = [ + "../communicator/test/fuzztest:fuzztest", + "../network/test/fuzztest/networkdelegatenormalimpl_fuzzer:fuzztest", + "../screenlock/test/fuzztest/screenlock_fuzzer:fuzztest", + ] } -- Gitee