• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "bundleactivepowerstatecallbackproxy_fuzzer.h"
17 #include "accesstoken_kit.h"
18 #include "app_mgr_interface.h"
19 
20 #include <securec.h>
21 #include "bundle_active_power_state_callback_service.h"
22 #include "power_state_callback_ipc_interface_code.h"
23 #include "power_state_machine_info.h"
24 #include "bundle_active_open_callback.h"
25 #include "bundle_active_service.h"
26 #include "bundle_active_client.h"
27 #include "bundle_active_stats_combiner.h"
28 #include "bundle_active_event_list.h"
29 #include "bundle_active_core.h"
30 
31 #ifndef errno_t
32 typedef int errno_t;
33 #endif
34 
35 #ifndef EOK
36 #define EOK 0
37 #endif
38 
39 namespace OHOS {
40 namespace DeviceUsageStats {
41     static std::string g_defaultBundleName = "com.ohos.camera";
42     static std::string g_defaultMoudleName = "defaultmoudlename";
43     static std::string g_defaultFormName = "defaultformname";
44     static int32_t DEFAULT_DIMENSION = 4;
45     static int64_t DEFAULT_FORMID = 1;
46     constexpr uint32_t U32_AT_SIZE = 4;
47 
48     const uint8_t* g_data = nullptr;
49     size_t g_size = 0;
50     size_t g_pos;
51 
52     template<class T>
GetData()53     T GetData()
54     {
55         T object {};
56         size_t objectSize = sizeof(object);
57         if (g_data == nullptr || objectSize > g_size - g_pos) {
58             return object;
59         }
60         errno_t ret = memcpy_s(&object, objectSize, g_data + g_pos, objectSize);
61         if (ret != EOK) {
62             return {};
63         }
64         g_pos += objectSize;
65         return object;
66     }
67 
GetStringFromData(int strlen)68     std::string GetStringFromData(int strlen)
69     {
70         if (strlen <= 0) {
71             return "";
72         }
73         char cstr[strlen];
74         cstr[strlen - 1] = '\0';
75         for (int i = 0; i < strlen - 1; i++) {
76             char tmp = GetData<char>();
77             if (tmp == '\0') {
78                 tmp = '1';
79             }
80             cstr[i] = tmp;
81         }
82         std::string str(cstr);
83         return str;
84     }
85 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)86     bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
87     {
88         g_data = data;
89         g_size = size;
90         uint32_t code = GetData<uint32_t>();
91         PowerMgr::PowerState state = static_cast<PowerMgr::PowerState>(code);
92         auto bundleActiveCore = std::make_shared<BundleActiveCore>();
93         BundleActivePowerStateCallbackService BundleActivePowerStateCallbackService(bundleActiveCore);
94         BundleActivePowerStateCallbackService.OnPowerStateChanged(state);
95         return true;
96     }
97 
BundleActiveClientFuzzTest(const uint8_t * data,size_t size)98     bool BundleActiveClientFuzzTest(const uint8_t* data, size_t size)
99     {
100         g_data = data;
101         g_size = size;
102         bool result = false;
103         int32_t userId = GetData<int32_t>();
104         std::string inputBundleName = GetStringFromData(size);
105         sptr<IAppGroupCallback> appGroupCallback = nullptr;
106         int32_t intervalType = GetData<int32_t>();
107         int64_t beginTime = GetData<int64_t>();
108         int64_t endTime = GetData<int64_t>();
109 
110         DelayedSingleton<BundleActiveClient>::GetInstance()->GetBundleActiveProxy();
111         DelayedSingleton<BundleActiveClient>::GetInstance()->RegisterAppGroupCallBack(appGroupCallback);
112         DelayedSingleton<BundleActiveClient>::GetInstance()->UnRegisterAppGroupCallBack(appGroupCallback);
113         DelayedSingleton<BundleActiveClient>::GetInstance()->IsBundleIdle(result, inputBundleName, userId);
114         DelayedSingleton<BundleActiveClient>::GetInstance()->IsBundleUsePeriod(result, inputBundleName, userId);
115         BundleActiveEvent event(g_defaultBundleName, g_defaultMoudleName, g_defaultFormName,
116         DEFAULT_DIMENSION, DEFAULT_FORMID, BundleActiveEvent::FORM_IS_CLICKED);
117         DelayedSingleton<BundleActiveClient>::GetInstance()->ReportEvent(event, userId);
118 
119         std::vector<BundleActivePackageStats> packageStats;
120         DelayedSingleton<BundleActiveClient>::GetInstance()->QueryBundleStatsInfoByInterval(packageStats,
121         intervalType, beginTime, endTime, userId);
122 
123         std::vector<BundleActiveEvent> bundleActiveEvent;
124         DelayedSingleton<BundleActiveClient>::GetInstance()->QueryBundleEvents(bundleActiveEvent,
125         beginTime, endTime, userId);
126 
127         int32_t newGroup = GetData<int32_t>();
128         std::string bundleName = GetStringFromData(size);
129         DelayedSingleton<BundleActiveClient>::GetInstance()->SetAppGroup(bundleName, newGroup, userId);
130 
131         std::vector<BundleActiveEventStats> eventStats;
132         DelayedSingleton<BundleActiveClient>::GetInstance()->QueryDeviceEventStats(beginTime, endTime,
133         eventStats, userId);
134         DelayedSingleton<BundleActiveClient>::GetInstance()->QueryNotificationEventStats(beginTime, endTime,
135         eventStats, userId);
136         std::vector<BundleActiveHighFrequencyPeriod> appFreqHours;
137         DelayedSingleton<BundleActiveClient>::GetInstance()->QueryHighFrequencyPeriodBundle(appFreqHours, userId);
138         int64_t latestUsedTime;
139         DelayedSingleton<BundleActiveClient>::GetInstance()->QueryBundleTodayLatestUsedTime(latestUsedTime,
140             bundleName, userId);
141         return true;
142     }
143 
BundleActiveEventListFuzzTest(const uint8_t * data,size_t size)144     bool BundleActiveEventListFuzzTest(const uint8_t* data, size_t size)
145     {
146         g_data = data;
147         g_size = size;
148         BundleActiveEventList right;
149         int64_t resultData = GetData<int64_t>();
150         auto combiner = std::make_shared<BundleActiveEventList>();
151         BundleActiveEvent event;
152         event.bundleName_ = GetStringFromData(size);
153         event.continuousTaskAbilityName_ = GetStringFromData(size);
154         event.timeStamp_ = GetData<int64_t>();
155 
156         combiner->Size();
157         combiner->FindBestIndex(resultData);
158         combiner->Insert(event);
159         combiner->Merge(right);
160         combiner->Clear();
161         return true;
162     }
163 
BundleActiveStatsCombinerFuzzTest(const uint8_t * data,size_t size)164     bool BundleActiveStatsCombinerFuzzTest(const uint8_t* data, size_t size)
165     {
166         g_data = data;
167         g_size = size;
168         auto combiner = std::make_shared<BundleActiveStatsCombiner<BundleActivePackageStats>>();
169         auto stats = std::make_shared<BundleActivePeriodStats>();
170         auto packageStat = std::make_shared<BundleActivePackageStats>();
171         stats->bundleStats_.emplace("normal", packageStat);
172         packageStat = nullptr;
173         stats->bundleStats_.emplace("default", packageStat);
174         int64_t beginTime = GetData<int64_t>();
175         std::vector<BundleActivePackageStats> accumulatedResult;
176         combiner->combine(stats, accumulatedResult, beginTime);
177 
178         auto eventCombiner = std::make_shared<BundleActiveStatsCombiner<BundleActiveEvent>>();
179         std::vector<BundleActiveEvent> activeEventResult;
180         eventCombiner->combine(stats, activeEventResult, beginTime);
181         return true;
182     }
SetGPos()183     bool SetGPos()
184     {
185         g_pos = 0;
186         return true;
187     }
188 } // namespace DeviceUsageStats
189 } // namespace OHOS
190 
191 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)192 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
193 {
194     /* Run your code on data */
195     if (data == nullptr) {
196         return 0;
197     }
198 
199     if (size < OHOS::DeviceUsageStats::U32_AT_SIZE) {
200         return 0;
201     }
202     OHOS::DeviceUsageStats::SetGPos();
203     OHOS::DeviceUsageStats::DoSomethingInterestingWithMyAPI(data, size);
204     OHOS::DeviceUsageStats::BundleActiveClientFuzzTest(data, size);
205     OHOS::DeviceUsageStats::BundleActiveEventListFuzzTest(data, size);
206     OHOS::DeviceUsageStats::BundleActiveStatsCombinerFuzzTest(data, size);
207     return 0;
208 }