From 96bf8f6800dbbe8badbabcaa235830cd60828f24 Mon Sep 17 00:00:00 2001 From: "kongwei (A)" Date: Tue, 5 Aug 2025 12:40:10 +0800 Subject: [PATCH] fix: add fuzz test Signed-off-by: yanghang --- test/fuzztest/BUILD.gn | 1 + test/fuzztest/thermalservice_fuzzer/BUILD.gn | 76 ++++++++++++++++ .../thermalservice_fuzzer/corpus/init | 14 +++ .../thermalservice_fuzzer/project.xml | 25 ++++++ .../thermalservice_fuzzer_test.cpp | 89 +++++++++++++++++++ 5 files changed, 205 insertions(+) create mode 100644 test/fuzztest/thermalservice_fuzzer/BUILD.gn create mode 100644 test/fuzztest/thermalservice_fuzzer/corpus/init create mode 100644 test/fuzztest/thermalservice_fuzzer/project.xml create mode 100644 test/fuzztest/thermalservice_fuzzer/thermalservice_fuzzer_test.cpp diff --git a/test/fuzztest/BUILD.gn b/test/fuzztest/BUILD.gn index fb3a77b..a78bd20 100644 --- a/test/fuzztest/BUILD.gn +++ b/test/fuzztest/BUILD.gn @@ -27,6 +27,7 @@ group("fuzztest") { "thermalactioncallback_fuzzer:ThermalActionCallbackFuzzTest", "thermaldump_fuzzer:ThermalDumpFuzzTest", "thermallevelcallback_fuzzer:ThermalLevelCallbackFuzzTest", + "thermalservice_fuzzer:ThermalServiceFuzzTest", "thermaltempcallback_fuzzer:ThermalTempCallbackFuzzTest", "updatethermalstate_fuzzer:UpdateThermalStateFuzzTest", ] diff --git a/test/fuzztest/thermalservice_fuzzer/BUILD.gn b/test/fuzztest/thermalservice_fuzzer/BUILD.gn new file mode 100644 index 0000000..0ce457f --- /dev/null +++ b/test/fuzztest/thermalservice_fuzzer/BUILD.gn @@ -0,0 +1,76 @@ +# 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. + +import("//build/config/features.gni") +import("//build/test.gni") +import("../../../thermalmgr.gni") + +module_output_path = "thermal_manager/thermal_manager" + +##############################fuzztest########################################## +ohos_fuzztest("ThermalServiceFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = + "${thermal_manager_path}/test/fuzztest/thermalservice_fuzzer" + + include_dirs = [ + "./", + "${thermal_inner_api}/native/include", + "${utils_path}/native/include", + "../thermal_utils", + ] + + configs = [ "${utils_path}:coverage_flags" ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ + "./thermalservice_fuzzer_test.cpp", + ] + deps = [ + "${thermal_manager_path}/services:thermalservice", + "${thermal_service_zidl}:thermalmgr_proxy", + "${thermal_service_zidl}:thermalmgr_stub", + "${utils_path}:thermal_utils", + ] + + external_deps = [ + "ability_runtime:wantagent_innerkits", + "c_utils:utils", + "drivers_interface_thermal:libthermal_proxy_1.1", + "hdf_core:libhdi", + "hdf_core:libpub_utils", + "hilog:libhilog", + "ipc:ipc_core", + "libxml2:libxml2", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + "time_service:time_client", + ] + + defines = [] + if (has_thermal_airplane_manager_part) { + defines += [ "HAS_THERMAL_AIRPLANE_MANAGER_PART" ] + external_deps += [ "netmanager_base:net_conn_manager_if" ] + } + + if (defined(global_parts_info) && + defined(global_parts_info.powermgr_battery_manager)) { + defines += [ "BATTERY_MANAGER_ENABLE" ] + external_deps += [ "battery_manager:batterysrv_client" ] + } +} diff --git a/test/fuzztest/thermalservice_fuzzer/corpus/init b/test/fuzztest/thermalservice_fuzzer/corpus/init new file mode 100644 index 0000000..65af8ee --- /dev/null +++ b/test/fuzztest/thermalservice_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# 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/test/fuzztest/thermalservice_fuzzer/project.xml b/test/fuzztest/thermalservice_fuzzer/project.xml new file mode 100644 index 0000000..2d2bab9 --- /dev/null +++ b/test/fuzztest/thermalservice_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 180 + + 4096 + + diff --git a/test/fuzztest/thermalservice_fuzzer/thermalservice_fuzzer_test.cpp b/test/fuzztest/thermalservice_fuzzer/thermalservice_fuzzer_test.cpp new file mode 100644 index 0000000..ce2c4b9 --- /dev/null +++ b/test/fuzztest/thermalservice_fuzzer/thermalservice_fuzzer_test.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. + */ + +/* This files contains faultlog fuzzer test modules. */ + +#define FUZZ_PROJECT_NAME "thermalservice_fuzzer" +#define private public +#define protected public + +#include "thermal_fuzzer_test.h" +#include "ithermal_srv.h" +#include "thermal_service.h" + +using namespace OHOS::PowerMgr; +using namespace OHOS; + +namespace { +sptr g_service = nullptr; +void ThermalServiceFuzzTest001(const uint8_t* data, size_t size) +{ + g_service = ThermalService::GetInstance(); + g_service->InitSystemTestModules(); + g_service->GetBaseinfoObj()->Init(); + g_service->InitThermalObserver(); + g_service->GetObserver()->InitSensorTypeMap(); + g_service->OnStart(); + std::string deviceId = ""; + g_service->OnAddSystemAbility(COMMON_EVENT_SERVICE_ID, deviceId); + g_service->OnAddSystemAbility(SOC_PERF_SERVICE_SA_ID, deviceId); + g_service->OnAddSystemAbility(POWER_MANAGER_BATT_SERVICE_ID, deviceId); + g_service->RegisterBootCompletedCallback(); + std::vector typeList; + sptr tempCallback = nullptr; + g_service->SubscribeThermalTempCallback(typeList, tempCallback); + g_service->UnSubscribeThermalTempCallback(tempCallback); + sptr levelCallback = nullptr; + g_service->SubscribeThermalLevelCallback(levelCallback); + g_service->UnSubscribeThermalLevelCallback(levelCallback); + std::vector actionList; + actionList.push_back("cpu_big"); + std::string desc = ""; + sptr actionCallback = nullptr; + g_service->SubscribeThermalActionCallback(actionList, desc, actionCallback); + g_service->UnSubscribeThermalActionCallback(actionCallback); + g_service->RegisterThermalHdiCallback(); + g_service->UnRegisterThermalHdiCallback(); + HdfThermalCallbackInfo event; + ThermalZoneInfo info; + info.type = "battery"; + info.temp = 0; + event.info.push_back(info); + g_service->HandleThermalCallbackEvent(event); + TypeTempMap tempMap; + g_service->isTempReport_ = false; + g_service->HandleTempEmulation(tempMap); + g_service->RegisterFanHdiCallback(); + g_service->HandleFanCallbackEvent(event); + int fd = 0; + std::vector args; + args.push_back(u"-h"); + g_service->Dump(fd, args); + g_service->isBootCompleted_ = true; + g_service->Dump(fd, args); + g_service->ready_ = false; + g_service->OnStop(); + g_service->EnableMock("", nullptr); + g_service->DestroyInstance(); +} +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + ThermalServiceFuzzTest001(data, size); + return 0; +} -- Gitee