• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "bundleactivecommon_fuzzer.h"
17 
18 #include "accesstoken_kit.h"
19 #include "app_mgr_interface.h"
20 
21 #include "system_ability_definition.h"
22 #include "iservice_registry.h"
23 #include "bundle_active_service.h"
24 #include "bundle_active_service_proxy.h"
25 #include "bundle_active_service_stub.h"
26 #include "bundle_active_client.h"
27 #include "iapp_group_callback.h"
28 #include "bundle_active_config_reader.h"
29 #include <fuzzer/FuzzedDataProvider.h>
30 
31 namespace OHOS {
32 namespace DeviceUsageStats {
33     using HapModuleInfo = OHOS::AppExecFwk::HapModuleInfo;
34     using AbilityInfo = OHOS::AppExecFwk::AbilityInfo;
35     using ApplicationInfo = OHOS::AppExecFwk::ApplicationInfo;
36     const char* CONFIG_TEST_PATH = "/sys_prod/etc/device_usage_statistics/device_usage_statistics_config.json";
37     const char* CONFIG_TEST1_PATH = "/sys_prod/etc/device_usage_statistics/device_usage_statistics_config_test1.json";
38     const char* CONFIG_TEST2_PATH = "/sys_prod/etc/device_usage_statistics/device_usage_statistics_config_test2.json";
39     const char* CONFIG_TEST3_PATH = "/sys_prod/etc/device_usage_statistics/device_usage_statistics_config_test3.json";
40     const char* CONFIG_TEST4_PATH = "/sys_prod/etc/device_usage_statistics/device_usage_statistics_config_test4.json";
41     const int32_t MAX_CODE = 20;
BundleActiveConfigReaderTest(FuzzedDataProvider * fdp)42     bool BundleActiveConfigReaderTest(FuzzedDataProvider* fdp)
43     {
44         auto bundleActiveConfigReader = std::make_shared<BundleActiveConfigReader>();
45         std::string filePath = fdp->ConsumeRandomLengthString();
46         bundleActiveConfigReader->LoadConfigFile(filePath.c_str());
47         cJSON *root = nullptr;
48         bundleActiveConfigReader->GetJsonFromFile(filePath.c_str(), root);
49         bundleActiveConfigReader->GetJsonFromFile(CONFIG_TEST_PATH, root);
50         std::string fullPath = "";
51         bundleActiveConfigReader->ConvertFullPath(filePath, fullPath);
52         bundleActiveConfigReader->ConvertFullPath(CONFIG_TEST_PATH, fullPath);
53         return true;
54     }
55 
BundleActiveServiceTest(FuzzedDataProvider * fdp)56     bool BundleActiveServiceTest(FuzzedDataProvider* fdp)
57     {
58         auto bundleActiveService = std::make_shared<BundleActiveService>();
59         int32_t uid = fdp->ConsumeIntegral<int32_t>();
60         bundleActiveService->GetNameAndIndexForUid(uid);
61         BundleActiveEvent event;
62         int32_t userId = fdp->ConsumeIntegral<int32_t>();
63         bundleActiveService->ReportEvent(event, userId);
64         bool isBundleIdle = false;
65         std::string bundleName = fdp->ConsumeRandomLengthString();
66         bundleActiveService->IsBundleIdle(isBundleIdle, bundleName, userId);
67         bool isBundleUserPeriod = false;
68         bundleActiveService->IsBundleUsePeriod(isBundleUserPeriod, bundleName, userId);
69         std::vector<BundleActivePackageStats> packageStats;
70         int32_t intervalType = fdp->ConsumeIntegral<int32_t>();
71         int64_t beginTime = fdp->ConsumeIntegral<int64_t>();
72         int64_t endTime = fdp->ConsumeIntegral<int64_t>();
73         bundleActiveService->QueryBundleStatsInfoByInterval(packageStats, intervalType, beginTime, endTime, userId);
74         std::vector<BundleActiveEvent> events;
75         bundleActiveService->QueryBundleEvents(events, beginTime, endTime, userId);
76         std::vector<BundleActiveEventStats> eventStats;
77         eventStats.clear();
78         int32_t newGroup = fdp->ConsumeIntegral<int32_t>();
79         bundleActiveService->SetAppGroup(bundleName, newGroup, userId);
80         packageStats.clear();
81         bundleActiveService->QueryBundleStatsInfos(packageStats, intervalType, beginTime, endTime);
82         bundleActiveService->QueryNotificationEventStats(beginTime, endTime, eventStats, userId);
83         BundleActiveModuleRecord moduleRecord;
84         moduleRecord.bundleName_ = fdp->ConsumeRandomLengthString();
85         moduleRecord.userId_ = userId;
86         bundleActiveService->QueryModuleRecordInfos(moduleRecord);
87         HapModuleInfo hapModuleInfo;
88         ApplicationInfo appInfo;
89         AbilityInfo abilityInfo;
90         bundleActiveService->SerModuleProperties(hapModuleInfo, appInfo, abilityInfo, moduleRecord);
91         int32_t fd = fdp->ConsumeIntegral<int32_t>();
92         std::vector<std::u16string> args;
93         bundleActiveService->Dump(fd, args);
94         uint32_t fuzzCode = fdp->ConsumeIntegral<uint32_t>();
95         std::string data = fdp->ConsumeRandomLengthString();
96         MessageParcel fuzzData;
97         fuzzData.WriteInterfaceToken(BundleActiveServiceStub::GetDescriptor());
98         fuzzData.WriteBuffer(data.c_str(), data.size());
99         fuzzData.RewindRead(0);
100         MessageParcel fuzzReply;
101         MessageOption fuzzOption;
102         bundleActiveService->OnRemoteRequest(fuzzCode % MAX_CODE, fuzzData, fuzzReply, fuzzOption);
103         std::vector<BundleActiveHighFrequencyPeriod> appFreqHours;
104         bundleActiveService->QueryHighFrequencyPeriodBundle(appFreqHours, userId);
105         return true;
106     }
107 
BundleActiveServiceOtherTest(FuzzedDataProvider * fdp)108     bool BundleActiveServiceOtherTest(FuzzedDataProvider* fdp)
109     {
110         auto bundleActiveService = std::make_shared<BundleActiveService>();
111         BundleActiveEvent event;
112         int32_t userId = fdp->ConsumeIntegral<int32_t>();
113         std::string bundleName = fdp->ConsumeRandomLengthString();
114         std::vector<BundleActivePackageStats> packageStats;
115         int32_t intervalType = fdp->ConsumeIntegral<int32_t>();
116         int64_t beginTime = fdp->ConsumeIntegral<int64_t>();
117         int64_t endTime = fdp->ConsumeIntegral<int64_t>();
118         std::vector<BundleActiveEvent> events;
119         std::vector<BundleActiveEventStats> eventStats;
120         bundleActiveService->QueryCurrentBundleEvents(events, beginTime, endTime);
121         eventStats.clear();
122         int32_t appGroup = 0;
123         bundleActiveService->QueryAppGroup(appGroup, bundleName, userId);
124         packageStats.clear();
125         bundleActiveService->QueryBundleStatsInfos(packageStats, intervalType, beginTime, endTime);
126         bundleActiveService->QueryDeviceEventStats(beginTime, endTime, eventStats, userId);
127         BundleActivePackageStats packageStats1;
128         packageStats1.bundleName_ = bundleName;
129         BundleActivePackageStats packageStats2;
130         packageStats2.bundleName_ = bundleName;
131         BundleActivePackageStats packageStats3;
132         packageStats3.bundleName_ = fdp->ConsumeRandomLengthString();
133         bundleActiveService->MergeSamePackageStats(packageStats1, packageStats2);
134         bundleActiveService->MergeSamePackageStats(packageStats1, packageStats3);
135         return true;
136     }
137 
BundleActiveServiceProxyTest(FuzzedDataProvider * fdp)138     bool BundleActiveServiceProxyTest(FuzzedDataProvider* fdp)
139     {
140         DelayedSingleton<BundleActiveClient>::GetInstance()->GetBundleActiveProxy();
141         auto bundleActiveServiceProxy = DelayedSingleton<BundleActiveClient>::GetInstance()->bundleActiveProxy_;
142         if (bundleActiveServiceProxy == nullptr) {
143             return true;
144         }
145         std::vector<BundleActivePackageStats> bundleActivePackageStats;
146         int32_t intervalType = fdp->ConsumeIntegral<int32_t>();
147         int64_t beginTime = fdp->ConsumeIntegral<int64_t>();
148         int64_t endTime = fdp->ConsumeIntegral<int64_t>();
149         bundleActiveServiceProxy->QueryBundleStatsInfos(bundleActivePackageStats, intervalType, beginTime, endTime);
150         std::vector<BundleActiveEvent> bundleActiveEvent;
151         bundleActiveServiceProxy->QueryCurrentBundleEvents(bundleActiveEvent, beginTime, endTime);
152         int32_t appGroup = fdp->ConsumeIntegral<int32_t>();
153         std::string bundleName = fdp->ConsumeRandomLengthString();
154         int32_t userId = fdp->ConsumeIntegral<int32_t>();
155         bundleActiveServiceProxy->QueryAppGroup(appGroup, bundleName, userId);
156         int32_t maxNum = fdp->ConsumeIntegral<int32_t>();
157         std::vector<BundleActiveModuleRecord> bundleActiveModuleRecord;
158         bundleActiveServiceProxy->QueryModuleUsageRecords(maxNum, bundleActiveModuleRecord, userId);
159         return true;
160     }
161 } // namespace DeviceUsageStats
162 } // namespace OHOS
163 
164 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)165 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
166 {
167     FuzzedDataProvider fdp(data, size);
168     OHOS::DeviceUsageStats::BundleActiveConfigReaderTest(&fdp);
169     OHOS::DeviceUsageStats::BundleActiveServiceTest(&fdp);
170     OHOS::DeviceUsageStats::BundleActiveServiceOtherTest(&fdp);
171     OHOS::DeviceUsageStats::BundleActiveServiceProxyTest(&fdp);
172     return 0;
173 }
174 
175