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