diff --git a/BUILD.gn b/BUILD.gn index 3ba65c5b4c6ac18ac1d23d579bf53a74692984ae..5e72de747ca4ccba8eace73b28e93d9caed4bc40 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 0000000000000000000000000000000000000000..82bbb01275412492dca0b67faf0186fe35e15c65 --- /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 0000000000000000000000000000000000000000..1cb8599efd4ebf412611b84cf209595ef5dd7654 --- /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 0000000000000000000000000000000000000000..1dac6c4a8802152a562f45acd84aedb500ba96ed --- /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 0000000000000000000000000000000000000000..e4ceac1bcd4e3b3427eb63cea0c28304064333cc --- /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 0000000000000000000000000000000000000000..c2d902c65c4a1b3fa9e2078efdc3c286a5bcf1a1 --- /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 0000000000000000000000000000000000000000..962633f97c6d3118b4efc4ac0c51ef2532fdc022 --- /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 0000000000000000000000000000000000000000..7a73a41bfdf76d6f793007240d80983a52f15f97 --- /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 0000000000000000000000000000000000000000..633bb8701a1f0151277b43dfd27fd4387f1431e3 --- /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 0000000000000000000000000000000000000000..6913456740ea1b1fedb3d5b131dca0413d8ae8e4 --- /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 0000000000000000000000000000000000000000..ad07e7ad313ffcfa191dbeca8da38257ef01b4f8 --- /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 0000000000000000000000000000000000000000..79c8179284ccb8186ef1ba4082a52a6ee28759a9 --- /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 e0687eb4c72ef34fee155f91ac125ed4c8a16bfe..f5aa379d1b3e622eb7bcdba26ec6b992b24fda71 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 5f648edfc41193d782339bb8154dcc1e7713e754..185899c1b9bb4eee4ccf373c10afab9996ed106b 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;