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 "mmi_log.h"
17 #include "input_device_manager.h"
18 #include "inputdevicemanager_fuzzer.h"
19
20 #include "securec.h"
21
22 #undef MMI_LOG_TAG
23 #define MMI_LOG_TAG "InputDeviceManagerFuzzTest"
24
25 namespace OHOS {
26 namespace MMI {
27 namespace OHOS {
28 template<class T>
GetObject(T & object,const uint8_t * data,size_t size)29 size_t GetObject(T &object, const uint8_t *data, size_t size)
30 {
31 size_t objectSize = sizeof(object);
32 if (objectSize > size) {
33 return 0;
34 }
35 errno_t ret = memcpy_s(&object, objectSize, data, objectSize);
36 if (ret != EOK) {
37 return 0;
38 }
39 return objectSize;
40 }
41
InputDeviceManagerFuzzTest(const uint8_t * data,size_t size)42 bool InputDeviceManagerFuzzTest(const uint8_t *data, size_t size)
43 {
44 size_t startPos = 0;
45 int32_t rowsBefore;
46 startPos += GetObject<int32_t>(rowsBefore, data + startPos, size - startPos);
47 int32_t id = 1;
48 int32_t deviceId = 1;
49 int32_t keyboardType = 1;
50 std::string type = "hello";
51 bool checked = true;
52 bool enable = true;
53 bool hasPointerDevice = true;
54 bool isVisible = true;
55 bool isHotPlug = true;
56 libinput_device* deviceOrigin = nullptr;
57 std::shared_ptr<InputDevice> inputDevice;
58 std::shared_ptr<IDeviceObserver> observer;
59 std::shared_ptr<InputDevice> devicePtr = std::make_shared<InputDevice>();
60 struct libinput_device* structDevice = nullptr;
61 std::vector<int32_t> keyCodes = {1};
62 std::vector<bool> keystroke = {true};
63 std::vector<std::string> args = {"hello"};
64 SessionPtr session;
65 InputDeviceManager::InputDeviceInfo inDevice;
66
67 INPUT_DEV_MGR->GetInputDevice(deviceId, checked);
68 INPUT_DEV_MGR->FillInputDevice(inputDevice, deviceOrigin);
69 INPUT_DEV_MGR->GetInputDeviceIds();
70 INPUT_DEV_MGR->SupportKeys(deviceId, keyCodes, keystroke);
71 INPUT_DEV_MGR->IsMatchKeys(structDevice, keyCodes);
72 INPUT_DEV_MGR->GetDeviceConfig(deviceId, keyboardType);
73 INPUT_DEV_MGR->GetKeyboardBusMode(deviceId);
74 INPUT_DEV_MGR->GetDeviceSupportKey(deviceId, keyboardType);
75 INPUT_DEV_MGR->GetKeyboardType(deviceId, keyboardType);
76 INPUT_DEV_MGR->AddDevListener(session);
77 INPUT_DEV_MGR->RemoveDevListener(session);
78 #ifdef OHOS_BUILD_ENABLE_POINTER_DRAWING
79 INPUT_DEV_MGR->HasPointerDevice();
80 #endif // OHOS_BUILD_ENABLE_POINTER_DRAWING
81 INPUT_DEV_MGR->HasTouchDevice();
82 INPUT_DEV_MGR->NotifyDevCallback(deviceId, inDevice);
83 INPUT_DEV_MGR->ScanPointerDevice();
84 INPUT_DEV_MGR->IsKeyboardDevice(devicePtr);
85 INPUT_DEV_MGR->Attach(observer);
86 INPUT_DEV_MGR->Detach(observer);
87 INPUT_DEV_MGR->NotifyPointerDevice(hasPointerDevice, isVisible, isHotPlug);
88 INPUT_DEV_MGR->IsRemote(id);
89 INPUT_DEV_MGR->GetVendorConfig(deviceId);
90 INPUT_DEV_MGR->OnEnableInputDevice(enable);
91 INPUT_DEV_MGR->AddVirtualInputDevice(devicePtr, deviceId);
92 INPUT_DEV_MGR->RemoveVirtualInputDevice(deviceId);
93 INPUT_DEV_MGR->MakeVirtualDeviceInfo(devicePtr, inDevice);
94 INPUT_DEV_MGR->GenerateVirtualDeviceId(deviceId);
95 INPUT_DEV_MGR->NotifyDevRemoveCallback(deviceId, inDevice);
96 INPUT_DEV_MGR->NotifyMessage(session, id, type);
97 INPUT_DEV_MGR->InitSessionLostCallback();
98 INPUT_DEV_MGR->OnSessionLost(session);
99 INPUT_DEV_MGR->GetTouchPadIds();
100 INPUT_DEV_MGR->IsTouchableDevice(devicePtr);
101 return true;
102 }
103 } // namespace OHOS
104
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)105 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
106 {
107 /* Run your code on data */
108 if (data == nullptr) {
109 return 0;
110 }
111
112 OHOS::InputDeviceManagerFuzzTest(data, size);
113 return 0;
114 }
115 } // namespace MMI
116 } // namespace OHOS