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 "pointer_event.h"
17 #include "pointeritem_fuzzer.h"
18 #include "fuzzer/FuzzedDataProvider.h"
19 #include "mmi_log.h"
20
21 #include "securec.h"
22
23 #undef MMI_LOG_TAG
24 #define MMI_LOG_TAG "PointerItemFuzzTest"
25
26 namespace OHOS {
27 namespace MMI {
28
PointerItemGetFuncFuzzTest(PointerEvent::PointerItem & item)29 void PointerItemGetFuncFuzzTest(PointerEvent::PointerItem &item)
30 {
31 item.GetPointerId();
32 item.GetDownTime();
33 item.IsPressed();
34 item.GetDisplayX();
35 item.GetDisplayY();
36 item.GetWindowX();
37 item.GetWindowY();
38 item.GetDisplayXPos();
39 item.GetDisplayYPos();
40 item.GetWindowXPos();
41 item.GetWindowYPos();
42 item.GetFixedDisplayX();
43 item.GetFixedDisplayY();
44 item.GetFixedDisplayXPos();
45 item.GetFixedDisplayYPos();
46 item.GetWidth();
47 item.GetHeight();
48 item.GetTiltX();
49 item.GetTiltY();
50 item.GetToolDisplayX();
51 item.GetToolDisplayY();
52 item.GetToolWindowX();
53 item.GetToolWindowY();
54 item.GetGlobalX();
55 item.GetGlobalY();
56 item.IsValidGlobalXY();
57 item.GetToolWidth();
58 item.GetToolHeight();
59 item.GetPressure();
60 item.GetMoveFlag();
61 item.GetLongAxis();
62 item.GetShortAxis();
63 item.GetDeviceId();
64 item.GetToolType();
65 item.GetTargetWindowId();
66 item.GetRawDx();
67 item.GetRawDy();
68 item.GetOriginPointerId();
69 item.GetRawDisplayX();
70 item.GetRawDisplayY();
71 item.GetBlobId();
72 item.GetTwist();
73 item.IsCanceled();
74 item.GetOrientation();
75
76 Parcel parcel;
77 item.WriteToParcel(parcel);
78 item.ReadFromParcel(parcel);
79 }
80
PointerItemOtherFuzzTest(FuzzedDataProvider & provider,PointerEvent::PointerItem & item)81 void PointerItemOtherFuzzTest(FuzzedDataProvider &provider, PointerEvent::PointerItem &item)
82 {
83 int32_t toolDisplayX = provider.ConsumeIntegral<int32_t>();
84 item.SetToolDisplayX(toolDisplayX);
85
86 int32_t toolDisplayY = provider.ConsumeIntegral<int32_t>();
87 item.SetToolDisplayY(toolDisplayY);
88
89 int32_t toolWindowX = provider.ConsumeIntegral<int32_t>();
90 item.SetToolWindowX(toolWindowX);
91
92 int32_t toolWindowY = provider.ConsumeIntegral<int32_t>();
93 item.SetToolWindowY(toolWindowY);
94
95 double globalX = provider.ConsumeFloatingPoint<double>();
96 item.SetGlobalX(globalX);
97
98 double globalY = provider.ConsumeFloatingPoint<double>();
99 item.SetGlobalY(globalY);
100
101 int32_t toolWidth = provider.ConsumeIntegral<int32_t>();
102 item.SetToolWidth(toolWidth);
103
104 int32_t toolHeight = provider.ConsumeIntegral<int32_t>();
105 item.SetToolHeight(toolHeight);
106
107 double pressure = provider.ConsumeFloatingPoint<double>();
108 item.SetPressure(pressure);
109
110 int32_t moveFlag = provider.ConsumeIntegral<int32_t>();
111 item.SetMoveFlag(moveFlag);
112
113 int32_t longAxis = provider.ConsumeIntegral<int32_t>();
114 item.SetLongAxis(longAxis);
115
116 int32_t shortAxis = provider.ConsumeIntegral<int32_t>();
117 item.SetShortAxis(shortAxis);
118
119 int32_t deviceId = provider.ConsumeIntegral<int32_t>();
120 item.SetDeviceId(deviceId);
121
122 int32_t toolType = provider.ConsumeIntegral<int32_t>();
123 item.SetToolType(toolType);
124
125 int32_t targetWindowId = provider.ConsumeIntegral<int32_t>();
126 item.SetTargetWindowId(targetWindowId);
127
128 int32_t rawDx = provider.ConsumeIntegral<int32_t>();
129 item.SetRawDx(rawDx);
130
131 int32_t rawDy = provider.ConsumeIntegral<int32_t>();
132 item.SetRawDy(rawDy);
133
134 int32_t originPointerId = provider.ConsumeIntegral<int32_t>();
135 item.SetOriginPointerId(originPointerId);
136
137 int32_t rawDisplayX = provider.ConsumeIntegral<int32_t>();
138 item.SetRawDisplayX(rawDisplayX);
139
140 int32_t rawDisplayY = provider.ConsumeIntegral<int32_t>();
141 item.SetRawDisplayY(rawDisplayY);
142
143 int32_t blobId = provider.ConsumeIntegral<int32_t>();
144 item.SetBlobId(blobId);
145
146 int32_t twist = provider.ConsumeIntegral<int32_t>();
147 item.SetTwist(twist);
148
149 bool canceled = provider.ConsumeBool();
150 item.SetCanceled(canceled);
151
152 int32_t orientation = provider.ConsumeIntegral<int32_t>();
153 item.SetOrientation(orientation);
154 }
155
PointerItemFuzzTest(const uint8_t * data,size_t size)156 bool PointerItemFuzzTest(const uint8_t *data, size_t size)
157 {
158 FuzzedDataProvider provider(data, size);
159 PointerEvent::PointerItem item;
160 int32_t pointerId = provider.ConsumeIntegral<int32_t>();
161 item.SetPointerId(pointerId);
162
163 int64_t downTime = provider.ConsumeIntegral<int64_t>();
164 item.SetDownTime(downTime);
165
166 bool pressed = provider.ConsumeBool();
167 item.SetPressed(pressed);
168
169 int32_t displayX = provider.ConsumeIntegral<int32_t>();
170 item.SetDisplayX(displayX);
171
172 int32_t displayY = provider.ConsumeIntegral<int32_t>();
173 item.SetDisplayY(displayY);
174
175 int32_t windowX = provider.ConsumeIntegral<int32_t>();
176 item.SetWindowX(windowX);
177
178 int32_t windowY = provider.ConsumeIntegral<int32_t>();
179 item.SetWindowY(windowY);
180
181 double displayXPos = provider.ConsumeFloatingPoint<double>();
182 item.SetDisplayXPos(displayXPos);
183
184 double displayYPos = provider.ConsumeFloatingPoint<double>();
185 item.SetDisplayYPos(displayYPos);
186
187 double windowXPos = provider.ConsumeFloatingPoint<double>();
188 item.SetWindowXPos(windowXPos);
189
190 double windowYPos = provider.ConsumeFloatingPoint<double>();
191 item.SetWindowYPos(windowYPos);
192
193 int32_t fixedDisplayX = provider.ConsumeIntegral<int32_t>();
194 item.SetFixedDisplayX(fixedDisplayX);
195
196 int32_t fixedDisplayY = provider.ConsumeIntegral<int32_t>();
197 item.SetFixedDisplayY(fixedDisplayY);
198
199 double fixedDisplayXPos = provider.ConsumeFloatingPoint<double>();
200 item.SetFixedDisplayXPos(fixedDisplayXPos);
201
202 double fixedDisplayYPos = provider.ConsumeFloatingPoint<double>();
203 item.SetFixedDisplayYPos(fixedDisplayYPos);
204
205 int32_t width = provider.ConsumeIntegral<int32_t>();
206 item.SetWidth(width);
207
208 int32_t height = provider.ConsumeIntegral<int32_t>();
209 item.SetHeight(height);
210
211 double titleX = provider.ConsumeFloatingPoint<double>();
212 item.SetTiltX(titleX);
213
214 double titleY = provider.ConsumeFloatingPoint<double>();
215 item.SetTiltY(titleY);
216
217 PointerItemOtherFuzzTest(provider, item);
218
219 PointerItemGetFuncFuzzTest(item);
220 MMI_HILOGD("PointerItemFuzzTest");
221 return true;
222 }
223 } // MMI
224 } // OHOS
225
226 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)227 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
228 {
229 /* Run your code on data */
230 if (data == nullptr) {
231 return 0;
232 }
233
234 OHOS::MMI::PointerItemFuzzTest(data, size);
235 return 0;
236 }
237