diff --git a/services/common/include/bundle_active_service.h b/services/common/include/bundle_active_service.h index 5e86c33e3e02ae7d8a0327d92311db4249bea507..5ed716f3fa53f23667a6d73625960734f1cdcbde 100644 --- a/services/common/include/bundle_active_service.h +++ b/services/common/include/bundle_active_service.h @@ -233,6 +233,7 @@ private: sptr powerStateCallback_; #endif bool ready_ {false}; + int32_t engMode_ = 0; int32_t ConvertIntervalType(const int32_t intervalType); void InitNecessaryState(); void InitService(); diff --git a/services/common/src/bundle_active_service.cpp b/services/common/src/bundle_active_service.cpp index 134ffb4c96d7768bccb0e4412af71498e82779cd..d68f5704beec910d1040c2993b2004766f23c825 100644 --- a/services/common/src/bundle_active_service.cpp +++ b/services/common/src/bundle_active_service.cpp @@ -51,7 +51,6 @@ static constexpr int32_t NO_DUMP_PARAM_NUMS = 0; const int32_t PACKAGE_USAGE_PARAM = 6; const int32_t MODULE_USAGE_PARAM = 4; const std::string NEEDED_PERMISSION = "ohos.permission.BUNDLE_ACTIVE_INFO"; -const int32_t ENG_MODE = OHOS::system::GetIntParameter("const.debuggable", 0); const bool REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility(DelayedSingleton::GetInstance().get()); @@ -154,6 +153,7 @@ void BundleActiveService::InitService() bundleActiveCore_->InitBundleGroupController(); SubscribeAppState(); SubscribeContinuousTask(); + engMode_ = OHOS::system::GetIntParameter("const.debuggable", 0); } OHOS::sptr BundleActiveService::GetAppManagerInstance() @@ -715,7 +715,7 @@ void BundleActiveService::SerModuleProperties(const HapModuleInfo& hapModuleInfo bool BundleActiveService::AllowDump() { - if (ENG_MODE == 0) { + if (engMode_ == 0) { BUNDLE_ACTIVE_LOGE("Not eng mode"); return false; } diff --git a/test/fuzztest/bundleactivecommon_fuzzer/bundleactivecommon_fuzzer.cpp b/test/fuzztest/bundleactivecommon_fuzzer/bundleactivecommon_fuzzer.cpp index 1cb8599efd4ebf412611b84cf209595ef5dd7654..e3675f4bba10628ee66ecfd3441f03bf1a7e482a 100644 --- a/test/fuzztest/bundleactivecommon_fuzzer/bundleactivecommon_fuzzer.cpp +++ b/test/fuzztest/bundleactivecommon_fuzzer/bundleactivecommon_fuzzer.cpp @@ -14,7 +14,6 @@ */ #include "bundleactivecommon_fuzzer.h" - #include "accesstoken_kit.h" #include "app_mgr_interface.h" @@ -27,7 +26,12 @@ #include "iapp_group_callback.h" #include "bundle_active_config_reader.h" #include - +#include "bundle_active_open_callback.h" +#include "accesstoken_kit.h" +#include "token_setproc.h" +#include "nativetoken_kit.h" +#include "ibundle_active_service.h" + namespace OHOS { namespace DeviceUsageStats { using HapModuleInfo = OHOS::AppExecFwk::HapModuleInfo; @@ -38,7 +42,14 @@ namespace DeviceUsageStats { 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 char* CONFIG_TEST5_PATH = "/sys_prod/etc/device_usage_statistics/device_usage_statistics_config_test5.json"; + const char* CONFIG_TEST6_PATH = "/sys_prod/etc/device_usage_statistics/device_usage_statistics_config_test6.json"; + const char* CONFIG_TEST7_PATH = "/sys_prod/etc/device_usage_statistics/device_usage_statistics_config_test7.json"; + const char* CONFIG_TEST8_PATH = "/sys_prod/etc/device_usage_statistics/1.json"; const int32_t MAX_CODE = 20; + const int32_t TEST_UID = 100; + const int64_t TEST_END_TIME = 100000; + bool BundleActiveConfigReaderTest(FuzzedDataProvider* fdp) { auto bundleActiveConfigReader = std::make_shared(); @@ -49,9 +60,14 @@ namespace DeviceUsageStats { bundleActiveConfigReader->LoadApplicationUsePeriodically(CONFIG_TEST2_PATH); bundleActiveConfigReader->LoadApplicationUsePeriodically(CONFIG_TEST3_PATH); bundleActiveConfigReader->LoadApplicationUsePeriodically(CONFIG_TEST4_PATH); + bundleActiveConfigReader->LoadApplicationUsePeriodically(CONFIG_TEST5_PATH); + bundleActiveConfigReader->LoadApplicationUsePeriodically(CONFIG_TEST_PATH); Json::Value root; bundleActiveConfigReader->GetJsonFromFile(filePath.c_str(), root); bundleActiveConfigReader->GetJsonFromFile(CONFIG_TEST_PATH, root); + bundleActiveConfigReader->GetJsonFromFile(CONFIG_TEST6_PATH, root); + bundleActiveConfigReader->GetJsonFromFile(CONFIG_TEST7_PATH, root); + bundleActiveConfigReader->GetJsonFromFile(CONFIG_TEST8_PATH, root); std::string fullPath = ""; bundleActiveConfigReader->ConvertFullPath(filePath, fullPath); bundleActiveConfigReader->ConvertFullPath(CONFIG_TEST_PATH, fullPath); @@ -61,6 +77,7 @@ namespace DeviceUsageStats { bool BundleActiveServiceTest(FuzzedDataProvider* fdp) { auto bundleActiveService = std::make_shared(); + bundleActiveService->bundleActiveCore_ = std::make_shared(); int32_t uid = fdp->ConsumeIntegral(); bundleActiveService->GetNameAndIndexForUid(uid); BundleActiveEvent event; @@ -112,6 +129,7 @@ namespace DeviceUsageStats { bool BundleActiveServiceOtherTest(FuzzedDataProvider* fdp) { auto bundleActiveService = std::make_shared(); + bundleActiveService->bundleActiveCore_ = std::make_shared(); BundleActiveEvent event; int32_t userId = fdp->ConsumeIntegral(); std::string bundleName = fdp->ConsumeRandomLengthString(); @@ -133,9 +151,14 @@ namespace DeviceUsageStats { BundleActivePackageStats packageStats2; packageStats2.bundleName_ = bundleName; BundleActivePackageStats packageStats3; - packageStats3.bundleName_ = fdp->ConsumeRandomLengthString(); + packageStats3.bundleName_ = "test"; bundleActiveService->MergeSamePackageStats(packageStats1, packageStats2); bundleActiveService->MergeSamePackageStats(packageStats1, packageStats3); + packageStats.clear(); + packageStats.push_back(packageStats1); + packageStats.push_back(packageStats2); + packageStats.push_back(packageStats3); + bundleActiveService->MergePackageStats(packageStats); return true; } @@ -159,6 +182,98 @@ namespace DeviceUsageStats { bundleActiveServiceProxy->QueryModuleUsageRecords(maxNum, bundleActiveModuleRecord, userId); return true; } + + bool BundleActiveServiceDumpTest(FuzzedDataProvider* fdp) + { + OHOS::DelayedSingleton::GetInstance()->DumpNativeTokenGet(); + auto bundleActiveService = std::make_shared(); + bundleActiveService->bundleActiveCore_ = std::make_shared(); + bundleActiveService->engMode_ = 1; + int32_t fd = fdp->ConsumeIntegral(); + std::vector args; + bundleActiveService->Dump(fd, args); + args.clear(); + args = {to_utf16("-h")}; + bundleActiveService->Dump(fd, args); + args.clear(); + args = {to_utf16("-A"), to_utf16("Events"), to_utf16("0"), to_utf16("1000000"), to_utf16("100")}; + auto userData = bundleActiveService->bundleActiveCore_->GetUserDataAndInitializeIfNeeded(100, 0, false); + userData->currentStats_[BundleActivePeriodStats::PERIOD_DAILY] = std::make_shared(); + userData->currentStats_[BundleActivePeriodStats::PERIOD_DAILY]->userId_ = TEST_UID; + userData->currentStats_[BundleActivePeriodStats::PERIOD_DAILY]->beginTime_ = 0; + userData->currentStats_[BundleActivePeriodStats::PERIOD_DAILY]->endTime_ = TEST_END_TIME; + BundleActiveEvent event; + event.timeStamp_ = 1; + userData->currentStats_[BundleActivePeriodStats::PERIOD_DAILY]->events_.events_.push_back(event); + bundleActiveService->Dump(fd, args); + args.clear(); + args = {to_utf16("-D")}; + bundleActiveService->Dump(fd, args); + std::vector dumpOption{"-A", "PackageUsage"}; + std::vector dumpInfo; + bundleActiveService->ShellDump(dumpOption, dumpInfo); + dumpOption.clear(); + dumpOption = {"-A", "ModuleUsage", "100", "100"}; + userData->GetOrCreateModuleRecord(event); + bundleActiveService->ShellDump(dumpOption, dumpInfo); + dumpOption.clear(); + dumpOption = {"-A", "Events", "0", "1000000", "1000"}; + bundleActiveService->ShellDump(dumpOption, dumpInfo); + bundleActiveService->bundleActiveCore_ = nullptr; + bundleActiveService->ShellDump(dumpOption, dumpInfo); + return true; + } + bool BundleActiveServiceOnRemoteRequestTest(FuzzedDataProvider* fdp) + { + auto bundleActiveService = std::make_shared(); + bundleActiveService->bundleActiveCore_ = std::make_shared(); + auto userData = bundleActiveService->bundleActiveCore_->GetUserDataAndInitializeIfNeeded(100, 0, false); + userData->currentStats_[BundleActivePeriodStats::PERIOD_DAILY] = std::make_shared(); + userData->currentStats_[BundleActivePeriodStats::PERIOD_DAILY]->userId_ = TEST_UID; + userData->currentStats_[BundleActivePeriodStats::PERIOD_DAILY]->beginTime_ = 0; + userData->currentStats_[BundleActivePeriodStats::PERIOD_DAILY]->endTime_ = TEST_END_TIME; + BundleActiveEvent event; + event.timeStamp_ = 1; + userData->currentStats_[BundleActivePeriodStats::PERIOD_DAILY]->events_.events_.push_back(event); + userData->GetOrCreateModuleRecord(event); + MessageParcel fuzzData; + fuzzData.WriteInterfaceToken(BundleActiveServiceStub::GetDescriptor()); + fuzzData.WriteInt32(0); + fuzzData.WriteInt64(0); + fuzzData.WriteInt64(TEST_END_TIME); + fuzzData.WriteInt32(TEST_UID); + MessageParcel fuzzReply; + MessageOption fuzzOption; + bundleActiveService->OnRemoteRequest( + static_cast(IBundleActiveServiceIpcCode::COMMAND_QUERY_BUNDLE_STATS_INFO_BY_INTERVAL), + fuzzData, fuzzReply, fuzzOption); + bundleActiveService->OnRemoteRequest( + static_cast(IBundleActiveServiceIpcCode::COMMAND_QUERY_BUNDLE_STATS_INFOS), + fuzzData, fuzzReply, fuzzOption); + MessageParcel fuzzData2; + bundleActiveService->OnRemoteRequest(static_cast(IBundleActiveServiceIpcCode::COMMAND_REPORT_EVENT), + fuzzData2, fuzzReply, fuzzOption); + MessageParcel fuzzData3; + fuzzData3.WriteInt64(0); + fuzzData3.WriteInt64(TEST_END_TIME); + fuzzData3.WriteInt32(TEST_UID); + bundleActiveService->OnRemoteRequest( + static_cast(IBundleActiveServiceIpcCode::COMMAND_QUERY_BUNDLE_EVENTS), + fuzzData3, fuzzReply, fuzzOption); + bundleActiveService->OnRemoteRequest( + static_cast(IBundleActiveServiceIpcCode::COMMAND_QUERY_CURRENT_BUNDLE_EVENTS), + fuzzData3, fuzzReply, fuzzOption); + bundleActiveService->OnRemoteRequest( + static_cast(IBundleActiveServiceIpcCode::COMMAND_QUERY_DEVICE_EVENT_STATS), + fuzzData3, fuzzReply, fuzzOption); + MessageParcel fuzzData4; + fuzzData4.WriteInt64(100); + fuzzData4.WriteInt32(TEST_UID); + bundleActiveService->OnRemoteRequest( + static_cast(IBundleActiveServiceIpcCode::COMMAND_QUERY_MODULE_USAGE_RECORDS), + fuzzData4, fuzzReply, fuzzOption); + return true; + } } // namespace DeviceUsageStats } // namespace OHOS @@ -167,9 +282,11 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { FuzzedDataProvider fdp(data, size); OHOS::DeviceUsageStats::BundleActiveConfigReaderTest(&fdp); + OHOS::DelayedSingleton::GetInstance()->CommonNativeTokenGet(); OHOS::DeviceUsageStats::BundleActiveServiceTest(&fdp); OHOS::DeviceUsageStats::BundleActiveServiceOtherTest(&fdp); OHOS::DeviceUsageStats::BundleActiveServiceProxyTest(&fdp); return 0; } - + + \ No newline at end of file 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 index 962633f97c6d3118b4efc4ac0c51ef2532fdc022..0cbb8d6d8bae614c0f60e1a1c08a96a019bc9b62 100644 --- a/test/fuzztest/bundleactivecommon_fuzzer/resource/config/device_usage_statistics_config.json +++ b/test/fuzztest/bundleactivecommon_fuzzer/resource/config/device_usage_statistics_config.json @@ -1,7 +1,7 @@ { "application_use_periodically": { "MinUseTimes": 1, - "MAX_USE_TIMES": 10, + "MaxUseTimes": 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 index 7a73a41bfdf76d6f793007240d80983a52f15f97..fbfea7312fb69f7b0177e2c1dece0cf26755955b 100644 --- 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 @@ -1,2 +1,4 @@ + { + "MaxUseTimes": 10 } \ 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 index 633bb8701a1f0151277b43dfd27fd4387f1431e3..133665d484e98e7fcae2202188d6943b37f61f18 100644 --- 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 @@ -1,6 +1,6 @@ { "application_use_periodically": { - "MAX_USE_TIMES": 10, + "MaxUseTimes": 10, "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 index ad07e7ad313ffcfa191dbeca8da38257ef01b4f8..462eb68689c94bac8fb1562bb872144859ba1085 100644 --- 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 @@ -1,6 +1,6 @@ { "application_use_periodically": { "MinUseTimes": 1, - "MAX_USE_TIMES": 10 + "MaxUseTimes": 10 } } \ No newline at end of file diff --git a/test/fuzztest/bundleactivecommon_fuzzer/resource/config/device_usage_statistics_config_test5json b/test/fuzztest/bundleactivecommon_fuzzer/resource/config/device_usage_statistics_config_test5json new file mode 100644 index 0000000000000000000000000000000000000000..445498394c2d2b5b1adf535ae7ee8c66e9717363 --- /dev/null +++ b/test/fuzztest/bundleactivecommon_fuzzer/resource/config/device_usage_statistics_config_test5json @@ -0,0 +1,4 @@ +{ + "application_use_periodically": { + } +} \ No newline at end of file diff --git a/test/fuzztest/bundleactivecommon_fuzzer/resource/config/device_usage_statistics_config_test6.json b/test/fuzztest/bundleactivecommon_fuzzer/resource/config/device_usage_statistics_config_test6.json new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/test/fuzztest/bundleactivecommon_fuzzer/resource/config/device_usage_statistics_config_test7.json b/test/fuzztest/bundleactivecommon_fuzzer/resource/config/device_usage_statistics_config_test7.json new file mode 100644 index 0000000000000000000000000000000000000000..acb7cd637277435ddd47da3ec255e894ca76afbc --- /dev/null +++ b/test/fuzztest/bundleactivecommon_fuzzer/resource/config/device_usage_statistics_config_test7.json @@ -0,0 +1,3 @@ +{ + 1 +} \ 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 index 79c8179284ccb8186ef1ba4082a52a6ee28759a9..2e6e23a5f2e52e44f967547b1cd232e4bb3075ae 100644 --- a/test/fuzztest/bundleactivecommon_fuzzer/resource/ohos_test.xml +++ b/test/fuzztest/bundleactivecommon_fuzzer/resource/ohos_test.xml @@ -19,11 +19,13 @@