• 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 "mmi_log.h"
17 #include "input_event.h"
18 #include "inputevent_fuzzer.h"
19 
20 #include "securec.h"
21 
22 #undef MMI_LOG_TAG
23 #define MMI_LOG_TAG "InputEventFuzzTest"
24 
25 namespace OHOS {
26 namespace MMI {
27 namespace OHOS {
28 #define ODDEVENFLAG 2
29 template<class T>
GetObject(T & object,const uint8_t * data,size_t size)30 size_t GetObject(T &object, const uint8_t *data, size_t size)
31 {
32     size_t objectSize = sizeof(object);
33     if (objectSize > size) {
34         return 0;
35     }
36     errno_t ret = memcpy_s(&object, objectSize, data, objectSize);
37     if (ret != EOK) {
38         return 0;
39     }
40     return objectSize;
41 }
42 
InputEventGetFuncFuzzTest(InputEvent & inputEvent)43 void InputEventGetFuncFuzzTest(InputEvent &inputEvent)
44 {
45     Parcel parcel;
46     inputEvent.Marshalling(parcel);
47     inputEvent.ReadFromParcel(parcel);
48     inputEvent.Unmarshalling(parcel);
49 
50     inputEvent.GetId();
51     inputEvent.UpdateId();
52     inputEvent.GetActionTime();
53     inputEvent.GetSensorInputTime();
54     inputEvent.GetAction();
55     inputEvent.GetActionStartTime();
56     inputEvent.GetDeviceId();
57     inputEvent.GetSourceType();
58     inputEvent.GetTargetDisplayId();
59     inputEvent.GetAgentWindowId();
60     inputEvent.GetTargetWindowId();
61     inputEvent.GetEventType();
62     inputEvent.GetFlag();
63     inputEvent.IsMarkEnabled();
64     inputEvent.MarkProcessed();
65     inputEvent.ToString();
66 
67     InputEvent::Create();
68     InputEvent inputEvent2 = InputEvent(inputEvent);
69     inputEvent2.ToString();
70 }
71 
InputEventFuzzTest(const uint8_t * data,size_t size)72 bool InputEventFuzzTest(const uint8_t *data, size_t size)
73 {
74     size_t startPos = 0;
75     int32_t rowsBefore;
76     startPos += GetObject<int32_t>(rowsBefore, data + startPos, size - startPos);
77 
78     InputEvent inputEvent(rowsBefore);
79     inputEvent.Reset();
80 
81     startPos += GetObject<int32_t>(rowsBefore, data + startPos, size - startPos);
82     inputEvent.EventTypeToString(rowsBefore);
83 
84     startPos += GetObject<int32_t>(rowsBefore, data + startPos, size - startPos);
85     inputEvent.SetId(rowsBefore);
86 
87     startPos += GetObject<int32_t>(rowsBefore, data + startPos, size - startPos);
88     inputEvent.SetActionTime(rowsBefore);
89 
90     startPos += GetObject<int32_t>(rowsBefore, data + startPos, size - startPos);
91     inputEvent.SetSensorInputTime(rowsBefore);
92 
93     startPos += GetObject<int32_t>(rowsBefore, data + startPos, size - startPos);
94     inputEvent.SetAction(rowsBefore);
95 
96     startPos += GetObject<int32_t>(rowsBefore, data + startPos, size - startPos);
97     inputEvent.SetActionStartTime(rowsBefore);
98 
99     startPos += GetObject<int32_t>(rowsBefore, data + startPos, size - startPos);
100     inputEvent.SetDeviceId(rowsBefore);
101 
102     startPos += GetObject<int32_t>(rowsBefore, data + startPos, size - startPos);
103     inputEvent.SetSourceType(rowsBefore);
104 
105     inputEvent.DumpSourceType();
106 
107     startPos += GetObject<int32_t>(rowsBefore, data + startPos, size - startPos);
108     inputEvent.SetTargetDisplayId(rowsBefore);
109 
110     startPos += GetObject<int32_t>(rowsBefore, data + startPos, size - startPos);
111     inputEvent.SetAgentWindowId(rowsBefore);
112 
113     startPos += GetObject<int32_t>(rowsBefore, data + startPos, size - startPos);
114     inputEvent.SetTargetWindowId(rowsBefore);
115 
116     startPos += GetObject<int32_t>(rowsBefore, data + startPos, size - startPos);
117     inputEvent.HasFlag(rowsBefore);
118 
119     startPos += GetObject<int32_t>(rowsBefore, data + startPos, size - startPos);
120     inputEvent.IsFlag(rowsBefore);
121 
122     startPos += GetObject<int32_t>(rowsBefore, data + startPos, size - startPos);
123     inputEvent.AddFlag(rowsBefore);
124     inputEvent.ClearFlag();
125 
126     startPos += GetObject<int32_t>(rowsBefore, data + startPos, size - startPos);
127     inputEvent.ClearFlag(rowsBefore);
128 
129     startPos += GetObject<int32_t>(rowsBefore, data + startPos, size - startPos);
130     inputEvent.SetMarkEnabled(rowsBefore);
131 
132     std::function<void(int32_t, int64_t)> callback = [](int32_t, int64_t) {};
133     inputEvent.SetProcessedCallback(callback);
134 
135     startPos += GetObject<int32_t>(rowsBefore, data + startPos, size - startPos);
136 
137     startPos += GetObject<int32_t>(rowsBefore, data + startPos, size - startPos);
138     inputEvent.ActionToShortStr(rowsBefore);
139     InputEventGetFuncFuzzTest(inputEvent);
140 
141     return true;
142 }
143 } // namespace OHOS
144 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)145 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
146 {
147     /* Run your code on data */
148     if (data == nullptr) {
149         return 0;
150     }
151 
152     OHOS::InputEventFuzzTest(data, size);
153     return 0;
154 }
155 } // namespace MMI
156 } // namespace OHOS