• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "simulateinputevent_fuzzer.h"
17 
18 #include "securec.h"
19 
20 #include "common_method.h"
21 #include "define_multimodal.h"
22 #include "input_manager.h"
23 #include "mmi_log.h"
24 
25 namespace OHOS {
26 namespace MMI {
27 namespace {
28 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "SimulateInputEventFuzzTest" };
29 } // namespace
30 
SimulateInputEventFuzzTest(const uint8_t * data,size_t size)31 bool SimulateInputEventFuzzTest(const uint8_t* data, size_t size)
32 {
33     auto injectDownEvent = KeyEvent::Create();
34     CHKPF(injectDownEvent);
35     size_t startPos = 0;
36     int32_t keyCode;
37     startPos += GetObject<int32_t>(keyCode, data + startPos, size - startPos);
38     injectDownEvent->SetKeyCode(keyCode);
39     injectDownEvent->SetKeyAction(KeyEvent::KEY_ACTION_DOWN);
40     int64_t downTime;
41     startPos += GetObject<int64_t>(downTime, data + startPos, size - startPos);
42     KeyEvent::KeyItem kitDown;
43     kitDown.SetDownTime(downTime);
44     int32_t keyCodePressed;
45     startPos += GetObject<int32_t>(keyCodePressed, data + startPos, size - startPos);
46     kitDown.SetKeyCode(keyCodePressed);
47     kitDown.SetPressed(true);
48     injectDownEvent->AddPressedKeyItems(kitDown);
49     InputManager::GetInstance()->SimulateInputEvent(injectDownEvent);
50 
51     auto injectUpEvent = KeyEvent::Create();
52     CHKPF(injectUpEvent);
53     startPos += GetObject<int64_t>(downTime, data + startPos, size - startPos);
54     KeyEvent::KeyItem kitUp;
55     kitUp.SetDownTime(downTime);
56     kitUp.SetKeyCode(keyCodePressed);
57     kitUp.SetPressed(false);
58     injectUpEvent->SetKeyCode(keyCode);
59     injectUpEvent->SetKeyAction(KeyEvent::KEY_ACTION_UP);
60     injectUpEvent->RemoveReleasedKeyItems(kitUp);
61     InputManager::GetInstance()->SimulateInputEvent(injectUpEvent);
62 
63     auto pointerDownEvent = PointerEvent::Create();
64     CHKPF(pointerDownEvent);
65     PointerEvent::PointerItem downitem;
66     downitem.SetPointerId(0);   // test code,set the PointerId = 0
67     int32_t physicalX;
68     startPos += GetObject<int32_t>(physicalX, data + startPos, size - startPos);
69     downitem.SetDisplayX(physicalX);   // test code,set the DisplayX = 823
70     int32_t physicalY;
71     startPos += GetObject<int32_t>(physicalY, data + startPos, size - startPos);
72     downitem.SetDisplayY(physicalY);   // test code,set the DisplayY = 723
73     int32_t pressure;
74     startPos += GetObject<int32_t>(pressure, data + startPos, size - startPos);
75     downitem.SetPressure(pressure);    // test code,set the Pressure = 5
76     downitem.SetDeviceId(1);    // test code,set the DeviceId = 1
77     pointerDownEvent->AddPointerItem(downitem);
78     pointerDownEvent->SetId(std::numeric_limits<int32_t>::max());
79     pointerDownEvent->SetPointerAction(PointerEvent::POINTER_ACTION_DOWN);
80     pointerDownEvent->SetPointerId(0);  // test code,set the PointerId = 1
81     pointerDownEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
82     MMI_HILOGD("Call InputManager::SimulatePointerEvent");
83     InputManager::GetInstance()->SimulateInputEvent(pointerDownEvent);
84 
85     auto pointerUpEvent = PointerEvent::Create();
86     CHKPF(pointerUpEvent);
87     PointerEvent::PointerItem upitem;
88     upitem.SetPointerId(0);   // test code,set the PointerId = 0
89     upitem.SetDisplayX(physicalX);   // test code,set the DisplayX = 823
90     upitem.SetDisplayY(physicalY);   // test code,set the DisplayY = 723
91     upitem.SetPressure(pressure);    // test code,set the Pressure = 5
92     upitem.SetDeviceId(1);    // test code,set the DeviceId = 1
93     pointerUpEvent->AddPointerItem(upitem);
94     pointerUpEvent->SetId(std::numeric_limits<int32_t>::max());
95     pointerUpEvent->SetPointerAction(PointerEvent::POINTER_ACTION_UP);
96     pointerUpEvent->SetPointerId(0);  // test code,set the PointerId = 1
97     pointerUpEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
98     MMI_HILOGD("Call InputManager::SimulatePointerEvent");
99     InputManager::GetInstance()->SimulateInputEvent(pointerUpEvent);
100     return true;
101 }
102 } // MMI
103 } // OHOS
104 
105 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)106 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
107 {
108     /* Run your code on data */
109     OHOS::MMI::SimulateInputEventFuzzTest(data, size);
110     return 0;
111 }
112 
113