From 4d20980db22ec12048b7cd6ee5ec19484f52189e Mon Sep 17 00:00:00 2001 From: hemenghao Date: Sat, 7 Jun 2025 10:08:27 +0800 Subject: [PATCH] =?UTF-8?q?fuzz=E8=A1=A5=E5=85=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hemenghao --- test/fuzztest/BUILD.gn | 1 + .../enableupgrademanager_fuzzer/BUILD.gn | 74 ++++++++++++++++++ .../enableupgrademanager_fuzzer/corpus/init | 14 ++++ .../enableupgrademanager_fuzzer.cpp | 77 +++++++++++++++++++ .../enableupgrademanager_fuzzer.h | 21 +++++ .../enableupgrademanager_fuzzer/project.xml | 25 ++++++ .../imfhisyseventreport_fuzzer.cpp | 41 ++++++++++ .../settingsdatautils_fuzzer.cpp | 6 ++ .../src/input_method_controller_test.cpp | 18 +++++ 9 files changed, 277 insertions(+) create mode 100644 test/fuzztest/enableupgrademanager_fuzzer/BUILD.gn create mode 100644 test/fuzztest/enableupgrademanager_fuzzer/corpus/init create mode 100644 test/fuzztest/enableupgrademanager_fuzzer/enableupgrademanager_fuzzer.cpp create mode 100644 test/fuzztest/enableupgrademanager_fuzzer/enableupgrademanager_fuzzer.h create mode 100644 test/fuzztest/enableupgrademanager_fuzzer/project.xml diff --git a/test/fuzztest/BUILD.gn b/test/fuzztest/BUILD.gn index 9dfa5ec0f..8eb6ba1f4 100644 --- a/test/fuzztest/BUILD.gn +++ b/test/fuzztest/BUILD.gn @@ -21,6 +21,7 @@ group("fuzztest") { "controlchannelstub_fuzzer:fuzztest", "corestub_fuzzer:fuzztest", "datachannelstub_fuzzer:fuzztest", + "enableupgrademanager_fuzzer:fuzztest", "imeenabledinfomanager_fuzzer:fuzztest", "imeeventmonitormanager_fuzzer:fuzztest", "imesystemchannel_fuzzer:fuzztest", diff --git a/test/fuzztest/enableupgrademanager_fuzzer/BUILD.gn b/test/fuzztest/enableupgrademanager_fuzzer/BUILD.gn new file mode 100644 index 000000000..5c508db9d --- /dev/null +++ b/test/fuzztest/enableupgrademanager_fuzzer/BUILD.gn @@ -0,0 +1,74 @@ +# 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("//base/inputmethod/imf/inputmethod.gni") + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") + +##############################fuzztest########################################## +ohos_fuzztest("EnableUpgradeManagerFuzzTest") { + module_out_path = "imf/imf" + + fuzz_config_file = "//base/inputmethod/imf/test/fuzztest/enableupgrademanager_fuzzer" + + include_dirs = [ + "${inputmethod_path}/common/include", + "${inputmethod_path}/frameworks/native/inputmethod_ability/include", + "${inputmethod_path}/frameworks/native/inputmethod_controller/include", + "${inputmethod_path}/services/adapter/settings_data_provider/common/include", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + + configs = [ "${inputmethod_path}/interfaces/inner_api/inputmethod_controller:inputmethod_client_native_public_config" ] + sources = [ + "enableupgrademanager_fuzzer.cpp" + ] + + deps = [ + "${inputmethod_path}/interfaces/inner_api/inputmethod_ability:inputmethod_ability_static", + "${inputmethod_path}/services/adapter/settings_data_provider:settings_data_static", + "${inputmethod_path}/services:inputmethod_service_static", + "${inputmethod_path}/test/fuzztest/systemabilitystubfuzztest/common:imf_sa_stub_fuzztest_common_static", + ] + + external_deps = [ + "cJSON:cjson", + "c_utils:utils", + "data_share:datashare_common", + "data_share:datashare_consumer", + "graphic_2d:librender_service_base", + "hilog:libhilog", + "input:libmmi-client", + "ipc:ipc_single", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [] + deps += [ + # deps file + ":EnableUpgradeManagerFuzzTest", + ] +} +############################################################################### \ No newline at end of file diff --git a/test/fuzztest/enableupgrademanager_fuzzer/corpus/init b/test/fuzztest/enableupgrademanager_fuzzer/corpus/init new file mode 100644 index 000000000..65af8ee8d --- /dev/null +++ b/test/fuzztest/enableupgrademanager_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/enableupgrademanager_fuzzer/enableupgrademanager_fuzzer.cpp b/test/fuzztest/enableupgrademanager_fuzzer/enableupgrademanager_fuzzer.cpp new file mode 100644 index 000000000..d62732d5c --- /dev/null +++ b/test/fuzztest/enableupgrademanager_fuzzer/enableupgrademanager_fuzzer.cpp @@ -0,0 +1,77 @@ +/* + * 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 "enableupgrademanager_fuzzer.h" + +#include +#include +#define private public +#define protected public +#include "enable_upgrade_manager.h" +#undef private + +using namespace OHOS::MiscServices; +namespace OHOS { + +__attribute__((no_sanitize("cfi"))) void FuzzAgentStub(const uint8_t *data, size_t size) +{ + static std::vector imeInfos; + static std::set bundleNames; + static std::vector bundleNamesVec; + static std::vector enabledInfos; + std::string fuzzedString(reinterpret_cast(data), size); + auto fuzzInt32 = static_cast(size); + auto fuzzUint32 = static_cast(size); + auto fuzzedBool = static_cast(data[0] % 2); + int32_t userId = fuzzInt32; + FullImeInfo imeInfo = + { .isNewIme = fuzzedBool, .tokenId = fuzzUint32, .appId = fuzzedString, .versionCode = fuzzUint32 }; + imeInfos.push_back(imeInfo); + bundleNames.insert(fuzzedString); + bundleNamesVec.push_back(fuzzedString); + ImeEnabledInfo imeEnabeleInfo; + imeEnabeleInfo.bundleName = fuzzedString; + imeEnabeleInfo.extensionName = fuzzedString; + + ImeEnabledCfg newEnabledCfg; + newEnabledCfg.version = fuzzedString; + newEnabledCfg.enabledInfos = enabledInfos; + ImePersistInfo persisInfo; + persisInfo.userId = userId; + persisInfo.currentIme = fuzzedString; + persisInfo.currentSubName = fuzzedString; + persisInfo.tempScreenLockIme = fuzzedString; + persisInfo.isDefaultImeSet = fuzzedBool; + EnabledStatus initStatus = EnabledStatus::DISABLED; + EnableUpgradeManager::GetInstance().Upgrade(userId, imeInfos); + EnableUpgradeManager::GetInstance().GetEnabledTable(userId, bundleNames); + EnableUpgradeManager::GetInstance().GetFullExperienceTable(userId, bundleNames); + EnableUpgradeManager::GetInstance().MergeTwoTable(userId, enabledInfos); + EnableUpgradeManager::GetInstance().PaddedByBundleMgr(userId, imeInfos, enabledInfos); + EnableUpgradeManager::GetInstance().UpdateGlobalEnabledTable(userId, newEnabledCfg); + EnableUpgradeManager::GetInstance().GetGlobalTableUserId(fuzzedString); + EnableUpgradeManager::GetInstance().GenerateGlobalContent(userId, bundleNamesVec); + EnableUpgradeManager::GetInstance().GetImePersistCfg(userId, persisInfo); + EnableUpgradeManager::GetInstance().PaddedByImePersistCfg(userId, enabledInfos); + EnableUpgradeManager::GetInstance().ComputeEnabledStatus(fuzzedString, initStatus); +} +} // namespace OHOS +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + OHOS::FuzzAgentStub(data, size); + return 0; +} \ No newline at end of file diff --git a/test/fuzztest/enableupgrademanager_fuzzer/enableupgrademanager_fuzzer.h b/test/fuzztest/enableupgrademanager_fuzzer/enableupgrademanager_fuzzer.h new file mode 100644 index 000000000..48398846f --- /dev/null +++ b/test/fuzztest/enableupgrademanager_fuzzer/enableupgrademanager_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 TEST_FUZZTEST_ENABLE_UPGRADE_MANAGER_FUZZER_H +#define TEST_FUZZTEST_ENABLE_UPGRADE_MANAGER_FUZZER_H + +#define FUZZ_PROJECT_NAME "enableupgrademanager_fuzzer" + +#endif // TEST_FUZZTEST_ENABLE_UPGRADE_MANAGER_FUZZER_H \ No newline at end of file diff --git a/test/fuzztest/enableupgrademanager_fuzzer/project.xml b/test/fuzztest/enableupgrademanager_fuzzer/project.xml new file mode 100644 index 000000000..66e1dcac4 --- /dev/null +++ b/test/fuzztest/enableupgrademanager_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/imfhisyseventreport_fuzzer/imfhisyseventreport_fuzzer.cpp b/test/fuzztest/imfhisyseventreport_fuzzer/imfhisyseventreport_fuzzer.cpp index 342dde487..ff870e5d7 100644 --- a/test/fuzztest/imfhisyseventreport_fuzzer/imfhisyseventreport_fuzzer.cpp +++ b/test/fuzztest/imfhisyseventreport_fuzzer/imfhisyseventreport_fuzzer.cpp @@ -21,6 +21,7 @@ #include "imf_hisysevent_reporter.h" #include "imf_hisysevent_util.h" #include "imsa_hisysevent_reporter.h" +#include "inputmethod_sysevent.h" #undef private #include @@ -143,6 +144,44 @@ void TestRecordBaseTextOperationStatistics(const uint8_t *data, size_t size) ImaHiSysEventReporter::GetInstance().RecordBaseTextOperationStatistics(*info); ImaHiSysEventReporter::GetInstance().RecordImeStartInputStatistics(*info); } + +void TestIntervalIndex(const uint8_t *data, size_t size) +{ + auto fuzzInt32 = static_cast(size); + ImaHiSysEventReporter::GetInstance().GetBaseTextOperationSucceedIntervalIndex(fuzzInt32); + ImaHiSysEventReporter::GetInstance().ReportStatisticsEvent(); + ImaHiSysEventReporter::GetInstance().ModImeCbTimeConsumeInfo(fuzzInt32); +} + +void TestInputMethodSysEvent(const uint8_t *data, size_t size) +{ + auto fuzzInt32 = static_cast(size); + auto fuzzUint32 = static_cast(size); + std::string fuzzedString(reinterpret_cast(data), size); + using TimerCallback = std::function; + TimerCallback tc; + InputMethodSysEvent::GetInstance().ServiceFaultReporter(fuzzedString, fuzzInt32); + InputMethodSysEvent::GetInstance().ImeUsageBehaviourReporter(); + InputMethodSysEvent::GetInstance().GetOperateInfo(fuzzInt32); + InputMethodSysEvent::GetInstance().StartTimer(tc, fuzzUint32); + InputMethodSysEvent::GetInstance().StartTimerForReport(); + InputMethodSysEvent::GetInstance().ReportSystemShortCut(fuzzedString); +} + +void TestOnDemandStartStopSa(const uint8_t *data, size_t size) +{ + auto fuzzInt32 = static_cast(size); + auto fuzzUint32 = static_cast(size); + std::string fuzzedString(reinterpret_cast(data), size); + using TimerCallback = std::function; + TimerCallback tc; + InputMethodSysEvent::GetInstance().ServiceFaultReporter(fuzzedString, fuzzInt32); + InputMethodSysEvent::GetInstance().ImeUsageBehaviourReporter(); + InputMethodSysEvent::GetInstance().GetOperateInfo(fuzzInt32); + InputMethodSysEvent::GetInstance().StartTimer(tc, fuzzUint32); + InputMethodSysEvent::GetInstance().StartTimerForReport(); + InputMethodSysEvent::GetInstance().ReportSystemShortCut(fuzzedString); +} } // namespace OHOS /* Fuzzer entry point */ @@ -158,5 +197,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) OHOS::TestStartInput(data, size); OHOS::TestBaseTextOperation(data, size); OHOS::TestRecordBaseTextOperationStatistics(data, size); + OHOS::TestIntervalIndex(data, size); + OHOS::TestInputMethodSysEvent(data, size); return 0; } \ No newline at end of file diff --git a/test/fuzztest/settingsdatautils_fuzzer/settingsdatautils_fuzzer.cpp b/test/fuzztest/settingsdatautils_fuzzer/settingsdatautils_fuzzer.cpp index b59566984..3076d93dd 100644 --- a/test/fuzztest/settingsdatautils_fuzzer/settingsdatautils_fuzzer.cpp +++ b/test/fuzztest/settingsdatautils_fuzzer/settingsdatautils_fuzzer.cpp @@ -55,6 +55,12 @@ void FuzzGenerateTargetUri(const std::string &key) { SettingsDataUtils::GetInstance().GenerateTargetUri(SETTING_URI_PROXY, key); } + + +void FuzzSetStringValue(const std::string &key) +{ + SettingsDataUtils::GetInstance().SetStringValue(SETTING_URI_PROXY, key, key); +} } // namespace OHOS /* Fuzzer entry point */ diff --git a/test/unittest/cpp_test/src/input_method_controller_test.cpp b/test/unittest/cpp_test/src/input_method_controller_test.cpp index 77f18263d..eab96a729 100644 --- a/test/unittest/cpp_test/src/input_method_controller_test.cpp +++ b/test/unittest/cpp_test/src/input_method_controller_test.cpp @@ -2025,5 +2025,23 @@ HWTEST_F(InputMethodControllerTest, GetWindowScaleCoordinate, TestSize.Level0) EXPECT_EQ(x, 101); EXPECT_EQ(y, 101); } + +/** + * @tc.name: TestImcOptionalInputMethod + * @tc.desc: Test ImcOptionalInputMethod + * @tc.type: FUNC + */ +HWTEST_F(InputMethodControllerTest, TestImcOptionalInputMethod, TestSize.Level0) +{ + IMSA_HILOGI("ImeProxyTest::TestImcOptionalInputMethod"); + Property property; + std::vector subProps; + auto ret = inputMethodController_->ListInputMethodSubtype(property, subProps); + EXPECT_NE(ret, ErrorCode::NO_ERROR); + ret = inputMethodController_->ShowOptionalInputMethod(); + EXPECT_NE(ret, ErrorCode::NO_ERROR); + ret = inputMethodController_->DisplayOptionalInputMethod(); + EXPECT_NE(ret, ErrorCode::NO_ERROR); +} } // namespace MiscServices } // namespace OHOS -- Gitee