From 636d8a9f11020354f055bc5be9f34bdcf510a7b5 Mon Sep 17 00:00:00 2001 From: fengyang Date: Tue, 22 Apr 2025 19:41:52 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E5=85=85FUZZ=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: fengyang --- BUILD.gn | 1 + .../bundleactivecommon_fuzzer/BUILD.gn | 103 +++++++++++ .../bundleactivecommon_fuzzer.cpp | 175 ++++++++++++++++++ .../bundleactivecommon_fuzzer.h | 23 +++ .../bundleactivecommon_fuzzer/corpus/init | 14 ++ .../bundleactivecommon_fuzzer/project.xml | 24 +++ .../device_usage_statistics_config.json | 7 + .../device_usage_statistics_config_test1.json | 2 + .../device_usage_statistics_config_test2.json | 6 + .../device_usage_statistics_config_test3.json | 6 + .../device_usage_statistics_config_test4.json | 6 + .../resource/ohos_test.xml | 36 ++++ .../bundleactiveobserver_fuzzer.cpp | 2 + .../bundleactiveonremoterequest_fuzzer.cpp | 2 +- 14 files changed, 406 insertions(+), 1 deletion(-) create mode 100644 test/fuzztest/bundleactivecommon_fuzzer/BUILD.gn create mode 100644 test/fuzztest/bundleactivecommon_fuzzer/bundleactivecommon_fuzzer.cpp create mode 100644 test/fuzztest/bundleactivecommon_fuzzer/bundleactivecommon_fuzzer.h create mode 100644 test/fuzztest/bundleactivecommon_fuzzer/corpus/init create mode 100644 test/fuzztest/bundleactivecommon_fuzzer/project.xml create mode 100644 test/fuzztest/bundleactivecommon_fuzzer/resource/config/device_usage_statistics_config.json create mode 100644 test/fuzztest/bundleactivecommon_fuzzer/resource/config/device_usage_statistics_config_test1.json create mode 100644 test/fuzztest/bundleactivecommon_fuzzer/resource/config/device_usage_statistics_config_test2.json create mode 100644 test/fuzztest/bundleactivecommon_fuzzer/resource/config/device_usage_statistics_config_test3.json create mode 100644 test/fuzztest/bundleactivecommon_fuzzer/resource/config/device_usage_statistics_config_test4.json create mode 100644 test/fuzztest/bundleactivecommon_fuzzer/resource/ohos_test.xml diff --git a/BUILD.gn b/BUILD.gn index 3ba65c5..5e72de7 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -439,6 +439,7 @@ group("test_all") { deps = [ "${usage_statistics_path}/interfaces/test/unittest/device_usage_statistics_jsunittest:js_unittest", "${usage_statistics_path}/test/fuzztest/appgroupcallbackstub_fuzzer:fuzztest", + "${usage_statistics_path}/test/fuzztest/bundleactivecommon_fuzzer:fuzztest", "${usage_statistics_path}/test/fuzztest/bundleactiveobserver_fuzzer:fuzztest", "${usage_statistics_path}/test/fuzztest/bundleactiveonremoterequest_fuzzer:fuzztest", "${usage_statistics_path}/test/fuzztest/bundleactivepowerstatecallbackproxy_fuzzer:fuzztest", diff --git a/test/fuzztest/bundleactivecommon_fuzzer/BUILD.gn b/test/fuzztest/bundleactivecommon_fuzzer/BUILD.gn new file mode 100644 index 0000000..82bbb01 --- /dev/null +++ b/test/fuzztest/bundleactivecommon_fuzzer/BUILD.gn @@ -0,0 +1,103 @@ +# 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("../../../device_usage_statistics.gni") + +##############################fuzztest########################################## +module_output_path = "device_usage_statistics/device_usage_statistics" + +config("module_private_config") { + include_dirs = [ + "${usage_statistics_path}/interfaces/innerkits/include", + "${usage_statistics_path}/services/common/include", + "${usage_statistics_path}/services/packageusage/include", + "${usage_statistics_path}/services/packagegroup/include", + "${usage_statistics_path}/interfaces/kits/bundlestats/napi/include", + "${usage_statistics_path}/utils/include", + "../common", + ] +} + +ohos_fuzztest("BundleActiveCommonFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = + "${usage_statistics_path}/test/fuzztest/bundleactivecommon_fuzzer" + resource_config_file = "${usage_statistics_path}/test/fuzztest/bundleactivecommon_fuzzer/resource/ohos_test.xml" + configs = [ ":module_private_config" ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + "-Dprivate=public", + "-Dprotected=public", + ] + sources = [ + "../common/fuzztest_helper.cpp", + "bundleactivecommon_fuzzer.cpp", + ] + + public_deps = [ + "${usage_statistics_path}:usagestatservice_static", + "${usage_statistics_path}:usagestatsinner", + ] + external_deps = [ + "ability_runtime:app_manager", + "ability_runtime:wantagent_innerkits", + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "access_token:libtokenid_sdk", + "cJSON:cjson", + "cJSON:cjson_static", + "ffrt:libffrt", + "hilog:libhilog", + "init:libbegetutil", + "ipc:ipc_single", + "jsoncpp:jsoncpp", + "relational_store:native_rdb", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + "selinux_adapter:librestorecon", + "time_service:time_client", + ] + + defines = [] + + if (bgtaskmgr_enable) { + external_deps += [ "background_task_mgr:bgtaskmgr_innerkits" ] + defines += [ "BGTASKMGR_ENABLE" ] + } + if (os_account_part_enabled) { + cflags_cc = [ "-DOS_ACCOUNT_PART_ENABLED" ] + external_deps += [ "os_account:os_account_innerkits" ] + } + if (device_usage_statistics_with_powermgr_power_manager_enable) { + defines += [ "DEVICE_USAGES_STATISTICS_POWERMANGER_ENABLE" ] + external_deps += [ "power_manager:powermgr_client" ] + } +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [] + deps += [ + # deps file + ":BundleActiveCommonFuzzTest", + ] +} +############################################################################### diff --git a/test/fuzztest/bundleactivecommon_fuzzer/bundleactivecommon_fuzzer.cpp b/test/fuzztest/bundleactivecommon_fuzzer/bundleactivecommon_fuzzer.cpp new file mode 100644 index 0000000..1cb8599 --- /dev/null +++ b/test/fuzztest/bundleactivecommon_fuzzer/bundleactivecommon_fuzzer.cpp @@ -0,0 +1,175 @@ +/* + * 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 "bundleactivecommon_fuzzer.h" + +#include "accesstoken_kit.h" +#include "app_mgr_interface.h" + +#include "system_ability_definition.h" +#include "iservice_registry.h" +#include "bundle_active_service.h" +#include "bundle_active_service_proxy.h" +#include "bundle_active_service_stub.h" +#include "bundle_active_client.h" +#include "iapp_group_callback.h" +#include "bundle_active_config_reader.h" +#include + +namespace OHOS { +namespace DeviceUsageStats { + using HapModuleInfo = OHOS::AppExecFwk::HapModuleInfo; + using AbilityInfo = OHOS::AppExecFwk::AbilityInfo; + using ApplicationInfo = OHOS::AppExecFwk::ApplicationInfo; + const char* CONFIG_TEST_PATH = "/sys_prod/etc/device_usage_statistics/device_usage_statistics_config.json"; + const char* CONFIG_TEST1_PATH = "/sys_prod/etc/device_usage_statistics/device_usage_statistics_config_test1.json"; + const char* CONFIG_TEST2_PATH = "/sys_prod/etc/device_usage_statistics/device_usage_statistics_config_test2.json"; + const char* CONFIG_TEST3_PATH = "/sys_prod/etc/device_usage_statistics/device_usage_statistics_config_test3.json"; + const char* CONFIG_TEST4_PATH = "/sys_prod/etc/device_usage_statistics/device_usage_statistics_config_test4.json"; + const int32_t MAX_CODE = 20; + bool BundleActiveConfigReaderTest(FuzzedDataProvider* fdp) + { + auto bundleActiveConfigReader = std::make_shared(); + std::string filePath = fdp->ConsumeRandomLengthString(); + bundleActiveConfigReader->LoadConfig(); + bundleActiveConfigReader->LoadApplicationUsePeriodically(filePath.c_str()); + bundleActiveConfigReader->LoadApplicationUsePeriodically(CONFIG_TEST1_PATH); + bundleActiveConfigReader->LoadApplicationUsePeriodically(CONFIG_TEST2_PATH); + bundleActiveConfigReader->LoadApplicationUsePeriodically(CONFIG_TEST3_PATH); + bundleActiveConfigReader->LoadApplicationUsePeriodically(CONFIG_TEST4_PATH); + Json::Value root; + bundleActiveConfigReader->GetJsonFromFile(filePath.c_str(), root); + bundleActiveConfigReader->GetJsonFromFile(CONFIG_TEST_PATH, root); + std::string fullPath = ""; + bundleActiveConfigReader->ConvertFullPath(filePath, fullPath); + bundleActiveConfigReader->ConvertFullPath(CONFIG_TEST_PATH, fullPath); + return true; + } + + bool BundleActiveServiceTest(FuzzedDataProvider* fdp) + { + auto bundleActiveService = std::make_shared(); + int32_t uid = fdp->ConsumeIntegral(); + bundleActiveService->GetNameAndIndexForUid(uid); + BundleActiveEvent event; + int32_t userId = fdp->ConsumeIntegral(); + bundleActiveService->ReportEvent(event, userId); + bool isBundleIdle = false; + std::string bundleName = fdp->ConsumeRandomLengthString(); + bundleActiveService->IsBundleIdle(isBundleIdle, bundleName, userId); + bool isBundleUserPeriod = false; + bundleActiveService->IsBundleUsePeriod(isBundleUserPeriod, bundleName, userId); + std::vector packageStats; + int32_t intervalType = fdp->ConsumeIntegral(); + int64_t beginTime = fdp->ConsumeIntegral(); + int64_t endTime = fdp->ConsumeIntegral(); + bundleActiveService->QueryBundleStatsInfoByInterval(packageStats, intervalType, beginTime, endTime, userId); + std::vector events; + bundleActiveService->QueryBundleEvents(events, beginTime, endTime, userId); + std::vector eventStats; + eventStats.clear(); + int32_t newGroup = fdp->ConsumeIntegral(); + bundleActiveService->SetAppGroup(bundleName, newGroup, userId); + packageStats.clear(); + bundleActiveService->QueryBundleStatsInfos(packageStats, intervalType, beginTime, endTime); + bundleActiveService->QueryNotificationEventStats(beginTime, endTime, eventStats, userId); + BundleActiveModuleRecord moduleRecord; + moduleRecord.bundleName_ = fdp->ConsumeRandomLengthString(); + moduleRecord.userId_ = userId; + bundleActiveService->QueryModuleRecordInfos(moduleRecord); + HapModuleInfo hapModuleInfo; + ApplicationInfo appInfo; + AbilityInfo abilityInfo; + bundleActiveService->SerModuleProperties(hapModuleInfo, appInfo, abilityInfo, moduleRecord); + int32_t fd = fdp->ConsumeIntegral(); + std::vector args; + bundleActiveService->Dump(fd, args); + uint32_t fuzzCode = fdp->ConsumeIntegral(); + std::string data = fdp->ConsumeRandomLengthString(); + MessageParcel fuzzData; + fuzzData.WriteInterfaceToken(BundleActiveServiceStub::GetDescriptor()); + fuzzData.WriteBuffer(data.c_str(), data.size()); + fuzzData.RewindRead(0); + MessageParcel fuzzReply; + MessageOption fuzzOption; + bundleActiveService->OnRemoteRequest(fuzzCode % MAX_CODE, + fuzzData, fuzzReply, fuzzOption); + return true; + } + + bool BundleActiveServiceOtherTest(FuzzedDataProvider* fdp) + { + auto bundleActiveService = std::make_shared(); + BundleActiveEvent event; + int32_t userId = fdp->ConsumeIntegral(); + std::string bundleName = fdp->ConsumeRandomLengthString(); + std::vector packageStats; + int32_t intervalType = fdp->ConsumeIntegral(); + int64_t beginTime = fdp->ConsumeIntegral(); + int64_t endTime = fdp->ConsumeIntegral(); + std::vector events; + std::vector eventStats; + bundleActiveService->QueryCurrentBundleEvents(events, beginTime, endTime); + eventStats.clear(); + int32_t appGroup = 0; + bundleActiveService->QueryAppGroup(appGroup, bundleName, userId); + packageStats.clear(); + bundleActiveService->QueryBundleStatsInfos(packageStats, intervalType, beginTime, endTime); + bundleActiveService->QueryDeviceEventStats(beginTime, endTime, eventStats, userId); + BundleActivePackageStats packageStats1; + packageStats1.bundleName_ = bundleName; + BundleActivePackageStats packageStats2; + packageStats2.bundleName_ = bundleName; + BundleActivePackageStats packageStats3; + packageStats3.bundleName_ = fdp->ConsumeRandomLengthString(); + bundleActiveService->MergeSamePackageStats(packageStats1, packageStats2); + bundleActiveService->MergeSamePackageStats(packageStats1, packageStats3); + return true; + } + + bool BundleActiveServiceProxyTest(FuzzedDataProvider* fdp) + { + DelayedSingleton::GetInstance()->GetBundleActiveProxy(); + auto bundleActiveServiceProxy = DelayedSingleton::GetInstance()->bundleActiveProxy_; + std::vector bundleActivePackageStats; + int32_t intervalType = fdp->ConsumeIntegral(); + int64_t beginTime = fdp->ConsumeIntegral(); + int64_t endTime = fdp->ConsumeIntegral(); + bundleActiveServiceProxy->QueryBundleStatsInfos(bundleActivePackageStats, intervalType, beginTime, endTime); + std::vector bundleActiveEvent; + bundleActiveServiceProxy->QueryCurrentBundleEvents(bundleActiveEvent, beginTime, endTime); + int32_t appGroup = fdp->ConsumeIntegral(); + std::string bundleName = fdp->ConsumeRandomLengthString(); + int32_t userId = fdp->ConsumeIntegral(); + bundleActiveServiceProxy->QueryAppGroup(appGroup, bundleName, userId); + int32_t maxNum = fdp->ConsumeIntegral(); + std::vector bundleActiveModuleRecord; + bundleActiveServiceProxy->QueryModuleUsageRecords(maxNum, bundleActiveModuleRecord, userId); + return true; + } +} // namespace DeviceUsageStats +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + FuzzedDataProvider fdp(data, size); + OHOS::DeviceUsageStats::BundleActiveConfigReaderTest(&fdp); + OHOS::DeviceUsageStats::BundleActiveServiceTest(&fdp); + OHOS::DeviceUsageStats::BundleActiveServiceOtherTest(&fdp); + OHOS::DeviceUsageStats::BundleActiveServiceProxyTest(&fdp); + return 0; +} + diff --git a/test/fuzztest/bundleactivecommon_fuzzer/bundleactivecommon_fuzzer.h b/test/fuzztest/bundleactivecommon_fuzzer/bundleactivecommon_fuzzer.h new file mode 100644 index 0000000..1dac6c4 --- /dev/null +++ b/test/fuzztest/bundleactivecommon_fuzzer/bundleactivecommon_fuzzer.h @@ -0,0 +1,23 @@ +/* + * 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_BUNDLEACTIVECOMMON_FUZZER_H +#define TEST_FUZZTEST_BUNDLEACTIVECOMMON_FUZZER_H + +#define FUZZ_PROJECT_NAME "bundleactivecommon_fuzzer" + +#include "fuzztest_helper.h" + +#endif diff --git a/test/fuzztest/bundleactivecommon_fuzzer/corpus/init b/test/fuzztest/bundleactivecommon_fuzzer/corpus/init new file mode 100644 index 0000000..e4ceac1 --- /dev/null +++ b/test/fuzztest/bundleactivecommon_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/bundleactivecommon_fuzzer/project.xml b/test/fuzztest/bundleactivecommon_fuzzer/project.xml new file mode 100644 index 0000000..c2d902c --- /dev/null +++ b/test/fuzztest/bundleactivecommon_fuzzer/project.xml @@ -0,0 +1,24 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/bundleactivecommon_fuzzer/resource/config/device_usage_statistics_config.json b/test/fuzztest/bundleactivecommon_fuzzer/resource/config/device_usage_statistics_config.json new file mode 100644 index 0000000..962633f --- /dev/null +++ b/test/fuzztest/bundleactivecommon_fuzzer/resource/config/device_usage_statistics_config.json @@ -0,0 +1,7 @@ +{ + "application_use_periodically": { + "MinUseTimes": 1, + "MAX_USE_TIMES": 10, + "MinUseDays": 3 + } +} \ No newline at end of file diff --git a/test/fuzztest/bundleactivecommon_fuzzer/resource/config/device_usage_statistics_config_test1.json b/test/fuzztest/bundleactivecommon_fuzzer/resource/config/device_usage_statistics_config_test1.json new file mode 100644 index 0000000..7a73a41 --- /dev/null +++ b/test/fuzztest/bundleactivecommon_fuzzer/resource/config/device_usage_statistics_config_test1.json @@ -0,0 +1,2 @@ +{ +} \ No newline at end of file diff --git a/test/fuzztest/bundleactivecommon_fuzzer/resource/config/device_usage_statistics_config_test2.json b/test/fuzztest/bundleactivecommon_fuzzer/resource/config/device_usage_statistics_config_test2.json new file mode 100644 index 0000000..633bb87 --- /dev/null +++ b/test/fuzztest/bundleactivecommon_fuzzer/resource/config/device_usage_statistics_config_test2.json @@ -0,0 +1,6 @@ +{ + "application_use_periodically": { + "MAX_USE_TIMES": 10, + "MinUseDays": 3 + } +} \ No newline at end of file diff --git a/test/fuzztest/bundleactivecommon_fuzzer/resource/config/device_usage_statistics_config_test3.json b/test/fuzztest/bundleactivecommon_fuzzer/resource/config/device_usage_statistics_config_test3.json new file mode 100644 index 0000000..6913456 --- /dev/null +++ b/test/fuzztest/bundleactivecommon_fuzzer/resource/config/device_usage_statistics_config_test3.json @@ -0,0 +1,6 @@ +{ + "application_use_periodically": { + "MinUseTimes": 1, + "MinUseDays": 3 + } +} \ No newline at end of file diff --git a/test/fuzztest/bundleactivecommon_fuzzer/resource/config/device_usage_statistics_config_test4.json b/test/fuzztest/bundleactivecommon_fuzzer/resource/config/device_usage_statistics_config_test4.json new file mode 100644 index 0000000..ad07e7a --- /dev/null +++ b/test/fuzztest/bundleactivecommon_fuzzer/resource/config/device_usage_statistics_config_test4.json @@ -0,0 +1,6 @@ +{ + "application_use_periodically": { + "MinUseTimes": 1, + "MAX_USE_TIMES": 10 + } +} \ No newline at end of file diff --git a/test/fuzztest/bundleactivecommon_fuzzer/resource/ohos_test.xml b/test/fuzztest/bundleactivecommon_fuzzer/resource/ohos_test.xml new file mode 100644 index 0000000..79c8179 --- /dev/null +++ b/test/fuzztest/bundleactivecommon_fuzzer/resource/ohos_test.xml @@ -0,0 +1,36 @@ + + + + + + + + + + \ No newline at end of file diff --git a/test/fuzztest/bundleactiveobserver_fuzzer/bundleactiveobserver_fuzzer.cpp b/test/fuzztest/bundleactiveobserver_fuzzer/bundleactiveobserver_fuzzer.cpp index e0687eb..f5aa379 100644 --- a/test/fuzztest/bundleactiveobserver_fuzzer/bundleactiveobserver_fuzzer.cpp +++ b/test/fuzztest/bundleactiveobserver_fuzzer/bundleactiveobserver_fuzzer.cpp @@ -55,6 +55,8 @@ namespace DeviceUsageStats { DelayedSingleton::GetInstance()->IsBundleIdle(result, inputBundlName, userId); DelayedSingleton::GetInstance()->IsBundleUsePeriod(result, inputBundlName, userId); DelayedSingleton::GetInstance()->GetApplicationUsePeriodicallyConfig(); + DelayedSingleton::GetInstance()->LoadApplicationUsePeriodically( + inputBundlName.c_str()); return true; } diff --git a/test/fuzztest/bundleactiveonremoterequest_fuzzer/bundleactiveonremoterequest_fuzzer.cpp b/test/fuzztest/bundleactiveonremoterequest_fuzzer/bundleactiveonremoterequest_fuzzer.cpp index 5f648ed..185899c 100644 --- a/test/fuzztest/bundleactiveonremoterequest_fuzzer/bundleactiveonremoterequest_fuzzer.cpp +++ b/test/fuzztest/bundleactiveonremoterequest_fuzzer/bundleactiveonremoterequest_fuzzer.cpp @@ -25,7 +25,7 @@ namespace OHOS { namespace DeviceUsageStats { constexpr uint32_t U32_AT_SIZE = 4; - constexpr uint32_t MAX_CODE = 14; // current max code is 14 + constexpr uint32_t MAX_CODE = 20; // current max code is 20 constexpr uint8_t TWENTYFOUR = 24; constexpr uint8_t SIXTEEN = 16; constexpr uint8_t EIGHT = 8; -- Gitee