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         g_pos = 0;
91         uint32_t code = GetData<uint32_t>();
92         PowerMgr::PowerState state = static_cast<PowerMgr::PowerState>(code);
93         auto bundleActiveCore = std::make_shared<BundleActiveCore>();
94         BundleActivePowerStateCallbackService BundleActivePowerStateCallbackService(bundleActiveCore);
95         BundleActivePowerStateCallbackService.OnPowerStateChanged(state);
96         return true;
97     }
98 
BundleActiveClientFuzzTest(const uint8_t * data,size_t size)99     bool BundleActiveClientFuzzTest(const uint8_t* data, size_t size)
100     {
101         g_data = data;
102         g_size = size;
103         g_pos = 0;
104         bool result = false;
105         int32_t userId = GetData<int32_t>();
106         std::string inputBundleName = GetStringFromData(size);
107         sptr<IAppGroupCallback> appGroupCallback = nullptr;
108         int32_t intervalType = GetData<int32_t>();
109         int64_t beginTime = GetData<int64_t>();
110         int64_t endTime = GetData<int64_t>();
111 
112         DelayedSingleton<BundleActiveClient>::GetInstance()->GetBundleActiveProxy();
113         DelayedSingleton<BundleActiveClient>::GetInstance()->RegisterAppGroupCallBack(appGroupCallback);
114         DelayedSingleton<BundleActiveClient>::GetInstance()->UnRegisterAppGroupCallBack(appGroupCallback);
115         DelayedSingleton<BundleActiveClient>::GetInstance()->IsBundleIdle(result, inputBundleName, userId);
116         DelayedSingleton<BundleActiveClient>::GetInstance()->IsBundleUsePeriod(result, inputBundleName, userId);
117         BundleActiveEvent event(g_defaultBundleName, g_defaultMoudleName, g_defaultFormName,
118         DEFAULT_DIMENSION, DEFAULT_FORMID, BundleActiveEvent::FORM_IS_CLICKED);
119         DelayedSingleton<BundleActiveClient>::GetInstance()->ReportEvent(event, userId);
120 
121         std::vector<BundleActivePackageStats> packageStats;
122         DelayedSingleton<BundleActiveClient>::GetInstance()->QueryBundleStatsInfoByInterval(packageStats,
123         intervalType, beginTime, endTime, userId);
124 
125         std::vector<BundleActiveEvent> bundleActiveEvent;
126         DelayedSingleton<BundleActiveClient>::GetInstance()->QueryBundleEvents(bundleActiveEvent,
127         beginTime, endTime, userId);
128 
129         int32_t newGroup = GetData<int32_t>();
130         std::string bundleName = GetStringFromData(size);
131         DelayedSingleton<BundleActiveClient>::GetInstance()->SetAppGroup(bundleName, newGroup, userId);
132 
133         std::vector<BundleActiveEventStats> eventStats;
134         DelayedSingleton<BundleActiveClient>::GetInstance()->QueryDeviceEventStats(beginTime, endTime,
135         eventStats, userId);
136         DelayedSingleton<BundleActiveClient>::GetInstance()->QueryNotificationEventStats(beginTime, endTime,
137         eventStats, userId);
138         return true;
139     }
140 
BundleActiveEventListFuzzTest(const uint8_t * data,size_t size)141     bool BundleActiveEventListFuzzTest(const uint8_t* data, size_t size)
142     {
143         g_data = data;
144         g_size = size;
145         g_pos = 0;
146         BundleActiveEventList right;
147         int64_t resultData = GetData<int64_t>();
148         auto combiner = std::make_shared<BundleActiveEventList>();
149         BundleActiveEvent event;
150         event.bundleName_ = GetStringFromData(size);
151         event.continuousTaskAbilityName_ = GetStringFromData(size);
152         event.timeStamp_ = GetData<int64_t>();
153 
154         combiner->Size();
155         combiner->FindBestIndex(resultData);
156         combiner->Insert(event);
157         combiner->Merge(right);
158         combiner->Clear();
159         return true;
160     }
161 
BundleActiveStatsCombinerFuzzTest(const uint8_t * data,size_t size)162     bool BundleActiveStatsCombinerFuzzTest(const uint8_t* data, size_t size)
163     {
164         g_data = data;
165         g_size = size;
166         g_pos = 0;
167         auto combiner = std::make_shared<BundleActiveStatsCombiner<BundleActivePackageStats>>();
168         auto stats = std::make_shared<BundleActivePeriodStats>();
169         auto packageStat = std::make_shared<BundleActivePackageStats>();
170         stats->bundleStats_.emplace("normal", packageStat);
171         packageStat = nullptr;
172         stats->bundleStats_.emplace("default", packageStat);
173         int64_t beginTime = GetData<int64_t>();
174         std::vector<BundleActivePackageStats> accumulatedResult;
175         combiner->combine(stats, accumulatedResult, beginTime);
176 
177         auto eventCombiner = std::make_shared<BundleActiveStatsCombiner<BundleActiveEvent>>();
178         std::vector<BundleActiveEvent> activeEventResult;
179         eventCombiner->combine(stats, activeEventResult, beginTime);
180         return true;
181     }
182 } // namespace DeviceUsageStats
183 } // namespace OHOS
184 
185 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)186 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
187 {
188     /* Run your code on data */
189     if (data == nullptr) {
190         return 0;
191     }
192 
193     if (size < OHOS::DeviceUsageStats::U32_AT_SIZE) {
194         return 0;
195     }
196 
197     OHOS::DeviceUsageStats::DoSomethingInterestingWithMyAPI(data, size);
198     OHOS::DeviceUsageStats::BundleActiveClientFuzzTest(data, size);
199     OHOS::DeviceUsageStats::BundleActiveEventListFuzzTest(data, size);
200     OHOS::DeviceUsageStats::BundleActiveStatsCombinerFuzzTest(data, size);
201     return 0;
202 }