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_device.h"
18 #include "inputdevice_fuzzer.h"
19
20 #include "securec.h"
21
22 #undef MMI_LOG_TAG
23 #define MMI_LOG_TAG "InputDeviceFuzzTest"
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
CreateAxisInfo(const uint8_t * data,size_t size,size_t & startPos,int32_t & rowsBefore)43 InputDevice::AxisInfo CreateAxisInfo(const uint8_t *data, size_t size, size_t &startPos, int32_t &rowsBefore)
44 {
45 InputDevice::AxisInfo axisInfo;
46 startPos += GetObject<int32_t>(rowsBefore, data + startPos, size - startPos);
47 axisInfo.SetAxisType(rowsBefore);
48
49 startPos += GetObject<int32_t>(rowsBefore, data + startPos, size - startPos);
50 axisInfo.SetMinimum(rowsBefore);
51
52 startPos += GetObject<int32_t>(rowsBefore, data + startPos, size - startPos);
53 axisInfo.SetMaximum(rowsBefore);
54
55 startPos += GetObject<int32_t>(rowsBefore, data + startPos, size - startPos);
56 axisInfo.SetFuzz(rowsBefore);
57
58 startPos += GetObject<int32_t>(rowsBefore, data + startPos, size - startPos);
59 axisInfo.SetFlat(rowsBefore);
60
61 startPos += GetObject<int32_t>(rowsBefore, data + startPos, size - startPos);
62 axisInfo.SetResolution(rowsBefore);
63
64 axisInfo.GetAxisType();
65 axisInfo.GetMinimum();
66 axisInfo.GetMaximum();
67 axisInfo.GetFuzz();
68 axisInfo.GetFlat();
69 axisInfo.GetResolution();
70 return axisInfo;
71 }
72
InputDeviceFuzzTest(const uint8_t * data,size_t size)73 bool InputDeviceFuzzTest(const uint8_t *data, size_t size)
74 {
75 size_t startPos = 0;
76 int32_t rowsBefore;
77 startPos += GetObject<int32_t>(rowsBefore, data + startPos, size - startPos);
78
79 InputDevice inputDevice;
80 inputDevice.SetId(rowsBefore);
81 std::string name = "testDevice";
82 inputDevice.SetName(name);
83
84 startPos += GetObject<int32_t>(rowsBefore, data + startPos, size - startPos);
85 inputDevice.SetType(rowsBefore);
86
87 startPos += GetObject<int32_t>(rowsBefore, data + startPos, size - startPos);
88 inputDevice.SetBus(rowsBefore);
89
90 startPos += GetObject<int32_t>(rowsBefore, data + startPos, size - startPos);
91 inputDevice.SetVersion(rowsBefore);
92
93 startPos += GetObject<int32_t>(rowsBefore, data + startPos, size - startPos);
94 inputDevice.SetProduct(rowsBefore);
95
96 startPos += GetObject<int32_t>(rowsBefore, data + startPos, size - startPos);
97 inputDevice.SetVendor(rowsBefore);
98
99 std::string phy = "testPhy";
100 inputDevice.SetPhys(phy);
101
102 std::string uniq = "testUniq";
103 inputDevice.SetUniq(uniq);
104
105 inputDevice.AddCapability(InputDeviceCapability::INPUT_DEV_CAP_KEYBOARD);
106
107 startPos += GetObject<int32_t>(rowsBefore, data + startPos, size - startPos);
108 inputDevice.HasCapability(rowsBefore);
109
110 InputDevice::AxisInfo axisInfo = CreateAxisInfo(data, size, startPos, rowsBefore);
111 inputDevice.AddAxisInfo(axisInfo);
112 std::vector<InputDevice::AxisInfo> axisInfos = inputDevice.GetAxisInfo();
113 axisInfos.push_back(axisInfo);
114 inputDevice.SetAxisInfo(axisInfos);
115
116 inputDevice.GetId();
117 inputDevice.GetName();
118 inputDevice.GetType();
119 inputDevice.GetBus();
120 inputDevice.GetVersion();
121 inputDevice.GetProduct();
122 inputDevice.GetVendor();
123 inputDevice.GetPhys();
124 inputDevice.GetUniq();
125 inputDevice.HasCapability(InputDeviceCapability::INPUT_DEV_CAP_KEYBOARD);
126 return true;
127 }
128 } // namespace OHOS
129
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)130 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
131 {
132 /* Run your code on data */
133 if (data == nullptr) {
134 return 0;
135 }
136
137 OHOS::InputDeviceFuzzTest(data, size);
138 return 0;
139 }
140 } // namespace MMI
141 } // namespace OHOS