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 "pointerevent_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 "PointerEventFuzzTest"
25
26 namespace OHOS {
27 namespace MMI {
PointEventGetFuncFuzzTest_Add(PointerEvent & pointEvent)28 void PointEventGetFuncFuzzTest_Add(PointerEvent &pointEvent)
29 {
30 pointEvent.IsValidCheckMouseFunc();
31 pointEvent.IsValidCheckMouse();
32 pointEvent.IsValidCheckTouchFunc();
33 pointEvent.IsValidCheckTouch();
34 pointEvent.IsValid();
35 pointEvent.ClearBuffer();
36 pointEvent.GetBuffer();
37 pointEvent.GetDispatchTimes();
38 pointEvent.GetHandlerEventType();
39
40 #ifdef OHOS_BUILD_ENABLE_ANCO
41 pointEvent.GetAncoDeal();
42 #endif
43
44 pointEvent.GetAutoToVirtualScreen();
45 pointEvent.GetFixedMode();
46 pointEvent.GetFixedModeStr();
47 pointEvent.GetScrollRows();
48 pointEvent.RemoveAllPointerItems();
49 pointEvent.ToString();
50
51 PointerEvent::Create();
52 PointerEvent pointEvent2 = PointerEvent(pointEvent);
53 pointEvent2.ToString();
54 }
PointEventGetFuncFuzzTest(PointerEvent & pointEvent)55 void PointEventGetFuncFuzzTest(PointerEvent &pointEvent)
56 {
57 Parcel parcel;
58 pointEvent.WriteToParcel(parcel);
59 pointEvent.Marshalling(parcel);
60 pointEvent.ReadFromParcel(parcel);
61 pointEvent.Unmarshalling(parcel);
62 pointEvent.ReadFixedModeFromParcel(parcel);
63 pointEvent.ReadAxisFromParcel(parcel);
64 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
65 pointEvent.ReadEnhanceDataFromParcel(parcel);
66 #endif
67 pointEvent.ReadBufferFromParcel(parcel);
68 pointEvent.DumpPointerAction();
69 pointEvent.GetPointerAction();
70 pointEvent.GetThrowAngle();
71 pointEvent.GetThrowSpeed();
72 pointEvent.GetOriginPointerAction();
73 pointEvent.GetHandOption();
74 pointEvent.GetPointerId();
75 pointEvent.GetPressedButtons();
76 pointEvent.ClearButtonPressed();
77 pointEvent.GetPointerCount();
78 pointEvent.GetPointerIds();
79 pointEvent.GetAllPointerItems();
80 pointEvent.GetButtonId();
81 pointEvent.GetFingerCount();
82 pointEvent.GetZOrder();
83 pointEvent.ClearAxisValue();
84 pointEvent.GetVelocity();
85 pointEvent.GetPressedKeys();
86 pointEvent.GetAxisEventType();
87 pointEvent.GetPullId();
88 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
89 pointEvent.GetEnhanceData();
90 #endif
91
92 #ifdef OHOS_BUILD_ENABLE_FINGERPRINT
93 pointEvent.GetFingerprintDistanceX();
94 pointEvent.GetFingerprintDistanceY();
95 #endif
96 PointEventGetFuncFuzzTest_Add(pointEvent);
97 }
98
PointerEventFuzzTest_Add(FuzzedDataProvider & provider,PointerEvent & pointEvent)99 void PointerEventFuzzTest_Add(FuzzedDataProvider &provider, PointerEvent &pointEvent)
100 {
101 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
102 uint8_t data = provider.ConsumeIntegral<uint8_t>();
103 std::vector<uint8_t> enhanceData;
104 enhanceData.push_back(data);
105 pointEvent.SetEnhanceData(enhanceData);
106 #endif
107
108 #ifdef OHOS_BUILD_ENABLE_FINGERPRINT
109 double fingerPrintDistanceX = provider.ConsumeFloatingPoint<double>();
110 pointEvent.SetFingerprintDistanceX(fingerPrintDistanceX);
111
112 double fingerPrintDistanceY = provider.ConsumeFloatingPoint<double>();
113 pointEvent.SetFingerprintDistanceY(fingerPrintDistanceY);
114 #endif
115
116 uint8_t buf = provider.ConsumeIntegral<uint8_t>();
117 std::vector<uint8_t> buffer;
118 enhanceData.push_back(buf);
119 pointEvent.SetBuffer(buffer);
120
121 int32_t dispatchTimes = provider.ConsumeIntegral<int32_t>();
122 pointEvent.SetDispatchTimes(dispatchTimes);
123
124 int32_t pressedkey = provider.ConsumeIntegral<int32_t>();
125 std::vector<int32_t> pressedKeys;
126 pressedKeys.push_back(pressedkey);
127 pointEvent.SetPressedKeys(pressedKeys);
128
129 uint32_t handleEventType = provider.ConsumeIntegral<uint32_t>();
130 pointEvent.SetHandlerEventType(handleEventType);
131
132 #ifdef OHOS_BUILD_ENABLE_ANCO
133 bool ancodeal = provider.ConsumeBool();
134 pointEvent.SetAncoDeal(ancodeal);
135 #endif
136 bool autoToVirtualScreen = provider.ConsumeBool();
137 pointEvent.SetAutoToVirtualScreen(autoToVirtualScreen);
138
139 pointEvent.SetFixedMode(PointerEvent::FixedMode::SCREEN_MODE_UNKNOWN);
140
141 int32_t action = provider.ConsumeIntegral<int32_t>();
142 pointEvent.ActionToShortStr(action);
143
144 int32_t scrollRows = provider.ConsumeIntegral<int32_t>();
145 pointEvent.SetScrollRows(scrollRows);
146 }
147
PointerEventFuzzTest(const uint8_t * data,size_t size)148 bool PointerEventFuzzTest(const uint8_t *data, size_t size)
149 {
150 FuzzedDataProvider provider(data, size);
151 int32_t eventType = provider.ConsumeIntegral<int32_t>();
152 PointerEvent pointEvent(eventType);
153 pointEvent.Reset();
154
155 double throwAngle = provider.ConsumeFloatingPoint<double>();
156 pointEvent.SetThrowAngle(throwAngle);
157
158 double throwSpeed = provider.ConsumeFloatingPoint<double>();
159 pointEvent.SetThrowSpeed(throwSpeed);
160
161 int32_t pointerAction = provider.ConsumeIntegral<int32_t>();
162 pointEvent.SetPointerAction(pointerAction);
163
164 int32_t originPointerAction = provider.ConsumeIntegral<int32_t>();
165 pointEvent.SetOriginPointerAction(originPointerAction);
166
167 int32_t handOption = provider.ConsumeIntegral<int32_t>();
168 pointEvent.SetHandOption(handOption);
169
170 int32_t pointerId = provider.ConsumeIntegral<int32_t>();
171 pointEvent.SetPointerId(pointerId);
172
173 PointerEvent::PointerItem item;
174 pointEvent.AddPointerItem(item);
175 pointEvent.UpdatePointerItem(pointerId, item);
176 pointEvent.GetPointerItem(pointerId, item);
177 pointEvent.GetOriginPointerItem(pointerId, item);
178 pointEvent.RemovePointerItem(pointerId);
179
180 int32_t buttonId = provider.ConsumeIntegral<int32_t>();
181 pointEvent.SetButtonId(buttonId);
182 pointEvent.SetButtonPressed(buttonId);
183 pointEvent.IsButtonPressed(buttonId);
184 pointEvent.DeleteReleaseButton(buttonId);
185
186 int32_t fingerCount = provider.ConsumeIntegral<int32_t>();
187 pointEvent.SetFingerCount(fingerCount);
188
189 float zOrder = provider.ConsumeFloatingPoint<float>();
190 pointEvent.SetZOrder(zOrder);
191
192 double axisValue = provider.ConsumeFloatingPoint<double>();
193 PointerEvent::AxisType axisType = PointerEvent::AxisType::AXIS_TYPE_UNKNOWN;
194 pointEvent.SetAxisValue(axisType, axisValue);
195 pointEvent.GetAxisValue(axisType);
196
197 int32_t axes = provider.ConsumeIntegral<int32_t>();
198 pointEvent.HasAxis(axes, axisType);
199 pointEvent.ClearAxisStatus(axisType);
200
201 double velocity = provider.ConsumeFloatingPoint<double>();
202 pointEvent.SetVelocity(velocity);
203
204 int32_t axisEventType = provider.ConsumeIntegral<int32_t>();
205 pointEvent.SetAxisEventType(axisEventType);
206
207 int32_t pullId = provider.ConsumeIntegral<int32_t>();
208 pointEvent.SetPullId(pullId);
209
210 PointerEventFuzzTest_Add(provider, pointEvent);
211
212 PointEventGetFuncFuzzTest(pointEvent);
213 MMI_HILOGD("PointerEventFuzzTest");
214 return true;
215 }
216 } // MMI
217 } // OHOS
218
219 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)220 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
221 {
222 /* Run your code on data */
223 if (data == nullptr) {
224 return 0;
225 }
226
227 OHOS::MMI::PointerEventFuzzTest(data, size);
228 return 0;
229 }
230