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 "stubhandleallocsocketfd_fuzzer.h"
17
18#include "mmi_service.h"
19#include "multimodal_input_connect_stub.h"
20
21#undef MMI_LOG_TAG
22#define MMI_LOG_TAG "StubHandleAllocSocketFdFuzzTest"
23
24class UDSSession;
25using SessionPtr = std::shared_ptr<UDSSession>;
26
27namespace OHOS {
28namespace MMI {
29const std::u16string FORMMGR_INTERFACE_TOKEN { u"ohos.multimodalinput.IConnectManager" };
30EpollEventType event_type = EPOLL_EVENT_SIGNAL;
31InputHandlerType handlerType = NONE;
32HandleEventType eventType = HANDLE_EVENT_TYPE_NONE;
33int32_t tmpfd = 1;
34uint32_t tmp32 = 1;
35int32_t type = 1;
36int64_t number = 1;
37int32_t infoId = 1;
38int32_t userId = 1;
39int32_t deviceId = 1;
40uint32_t frameCount = 1;
41uint32_t vsyncCount = 1;
42int64_t infraredFrequency = 1;
43std::vector<int32_t> vecInt = { 1, 2, 3 };
44std::vector<bool> vecBool = { 1 };
45std::vector<std::u16string> argString = { u"hello", u"worid" };
46std::vector<int64_t> patternInt = { 1, 2, 3 };
47std::map<int32_t, int32_t> mpInt = { { 1, 2 }, { 2, 2 }, { 3, 2 } };
48std::vector<InfraredFrequency> requencysInf = { { 1, 2 }, { 2, 2 } };
49int32_t g_tmpDate = 1;
50void* g_pixelMapPtr = &g_tmpDate;
51bool g_isAuthorize = true;
52bool g_isNativeInject = true;
53bool g_switchFlag = true;
54bool g_enableFlag = true;
55bool g_rotateSwitchFlag = true;
56bool g_stateFlag = true;
57std::string message = "hello";
58std::string businessId = "hello";
59std::string flag = "hello";
60std::shared_ptr<InputDevice> inputDevice;
61std::shared_ptr<InputDevice> device;
62std::shared_ptr<PointerEvent> pointerEvent;
63std::shared_ptr<KeyOption> p_option;
64std::shared_ptr<KeyEvent> keyEvent;
65
66bool StubHandleAllocSocketFdFuzzTest(const uint8_t *data, size_t size)
67{
68    MessageParcel datas;
69    if (!datas.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN) ||
70        !datas.WriteBuffer(data, size) || !datas.RewindRead(0)) {
71        return false;
72    }
73    MessageParcel reply;
74    MessageOption option;
75    MMIService::GetInstance()->AddEpoll(event_type, tmpfd);
76    MMIService::GetInstance()->DelEpoll(event_type, tmpfd);
77    MMIService::GetInstance()->InitLibinputService();
78    MMIService::GetInstance()->InitDelegateTasks();
79    MMIService::GetInstance()->AddAppDebugListener();
80    MMIService::GetInstance()->SetMouseScrollRows(tmpfd);
81    MMIService::GetInstance()->SetMouseIcon(tmpfd, g_pixelMapPtr);
82    MMIService::GetInstance()->ReadMouseScrollRows(tmpfd);
83    MMIService::GetInstance()->MarkProcessed(tmpfd, tmpfd);
84    MMIService::GetInstance()->ReadPointerColor(tmpfd);
85    MMIService::GetInstance()->OnSupportKeys(tmpfd, vecInt, vecBool);
86    MMIService::GetInstance()->OnGetDeviceIds(vecInt);
87    MMIService::GetInstance()->OnGetDevice(tmpfd, inputDevice);
88    MMIService::GetInstance()->OnGetKeyboardType(tmpfd, tmpfd);
89    MMIService::GetInstance()->SetKeyboardRepeatDelay(tmpfd);
90    MMIService::GetInstance()->CheckRemoveInput(tmpfd, handlerType, eventType, tmpfd, tmp32);
91    MMIService::GetInstance()->RemoveInputHandler(handlerType, eventType, tmpfd, tmp32);
92    MMIService::GetInstance()->MarkEventConsumed(tmpfd);
93    MMIService::GetInstance()->MoveMouseEvent(tmpfd, tmpfd);
94    MMIService::GetInstance()->InjectKeyEvent(keyEvent, g_isNativeInject);
95    MMIService::GetInstance()->CheckInjectKeyEvent(keyEvent, tmpfd, g_isNativeInject);
96    MMIService::GetInstance()->OnGetKeyState(vecInt, mpInt);
97    MMIService::GetInstance()->InjectPointerEvent(pointerEvent, g_isNativeInject);
98    MMIService::GetInstance()->OnAddSystemAbility(tmpfd, "deviceId");
99    MMIService::GetInstance()->SubscribeKeyEvent(tmpfd, p_option);
100    MMIService::GetInstance()->UnsubscribeKeyEvent(tmpfd);
101    MMIService::GetInstance()->SubscribeSwitchEvent(tmpfd, tmpfd);
102    MMIService::GetInstance()->SetDisplayBind(tmpfd, tmpfd, message);
103    MMIService::GetInstance()->SetFunctionKeyState(tmpfd, g_enableFlag);
104    MMIService::GetInstance()->SetPointerLocation(tmpfd, tmpfd);
105    MMIService::GetInstance()->AddReloadDeviceTimer();
106    MMIService::GetInstance()->Dump(tmpfd, argString);
107    MMIService::GetInstance()->OnGetWindowPid(tmpfd, tmpfd);
108    MMIService::GetInstance()->GetWindowPid(tmpfd);
109    MMIService::GetInstance()->SetKeyDownDuration(businessId, tmpfd);
110    MMIService::GetInstance()->ReadTouchpadScrollSwich(g_switchFlag);
111    MMIService::GetInstance()->ReadTouchpadScrollDirection(g_switchFlag);
112    MMIService::GetInstance()->ReadTouchpadTapSwitch(g_switchFlag);
113    MMIService::GetInstance()->ReadTouchpadPointerSpeed(tmpfd);
114    MMIService::GetInstance()->ReadTouchpadPinchSwitch(g_switchFlag);
115    MMIService::GetInstance()->ReadTouchpadSwipeSwitch(g_switchFlag);
116    MMIService::GetInstance()->ReadTouchpadRightMenuType(tmpfd);
117    MMIService::GetInstance()->ReadTouchpadRotateSwitch(g_rotateSwitchFlag);
118    MMIService::GetInstance()->SetTouchpadScrollSwitch(g_switchFlag);
119    MMIService::GetInstance()->GetTouchpadScrollSwitch(g_switchFlag);
120    MMIService::GetInstance()->SetTouchpadScrollDirection(g_stateFlag);
121    MMIService::GetInstance()->GetTouchpadScrollDirection(g_stateFlag);
122    MMIService::GetInstance()->SetTouchpadTapSwitch(g_switchFlag);
123    MMIService::GetInstance()->GetTouchpadTapSwitch(g_switchFlag);
124    MMIService::GetInstance()->SetTouchpadPointerSpeed(tmpfd);
125    MMIService::GetInstance()->GetTouchpadPointerSpeed(tmpfd);
126    MMIService::GetInstance()->SetTouchpadPinchSwitch(g_switchFlag);
127    MMIService::GetInstance()->GetTouchpadPinchSwitch(g_switchFlag);
128    MMIService::GetInstance()->SetTouchpadSwipeSwitch(g_switchFlag);
129    MMIService::GetInstance()->GetTouchpadSwipeSwitch(g_switchFlag);
130    MMIService::GetInstance()->SetTouchpadRightClickType(type);
131    MMIService::GetInstance()->SetTouchpadRotateSwitch(g_rotateSwitchFlag);
132    MMIService::GetInstance()->GetTouchpadRotateSwitch(g_rotateSwitchFlag);
133    MMIService::GetInstance()->GetKeyState(vecInt, mpInt);
134    MMIService::GetInstance()->Authorize(g_isAuthorize);
135    MMIService::GetInstance()->OnAuthorize(g_isAuthorize);
136    MMIService::GetInstance()->CancelInjection();
137    MMIService::GetInstance()->OnCancelInjection();
138    MMIService::GetInstance()->GetInfraredFrequencies(requencysInf);
139    MMIService::GetInstance()->TransmitInfrared(number, patternInt);
140    MMIService::GetInstance()->SetPixelMapData(infoId, g_pixelMapPtr);
141    MMIService::GetInstance()->SetCurrentUser(userId);
142    MMIService::GetInstance()->AddVirtualInputDevice(device, deviceId);
143    MMIService::GetInstance()->RemoveVirtualInputDevice(tmpfd);
144    MMIService::GetInstance()->EnableHardwareCursorStats(g_enableFlag);
145    MMIService::GetInstance()->GetHardwareCursorStats(frameCount, vsyncCount);
146#ifdef OHOS_BUILD_ENABLE_MAGICCURSOR
147    MMIService::GetInstance()->GetPointerSnapshot(g_pixelMapPtr);
148#endif // OHOS_BUILD_ENABLE_MAGICCURSOR
149    MMIService::GetInstance()->state_ = ServiceRunningState::STATE_RUNNING;
150    MMIService::GetInstance()->OnRemoteRequest(
151        static_cast<uint32_t>(MultimodalinputConnectInterfaceCode::ALLOC_SOCKET_FD), datas, reply, option);
152    return true;
153}
154} // MMI
155} // OHOS
156
157/* Fuzzer entry point */
158extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
159{
160    /* Run your code on data */
161    if (data == nullptr) {
162        return 0;
163    }
164
165    OHOS::MMI::StubHandleAllocSocketFdFuzzTest(data, size);
166    return 0;
167}
168