• 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 <fuzzer/FuzzedDataProvider.h>
17 #include "shiftapppointerevent_fuzzer.h"
18 
19 #include "input_manager.h"
20 #include "define_multimodal.h"
21 #include "mmi_service.h"
22 #include "mmi_log.h"
23 
24 
25 namespace OHOS {
26 constexpr uint32_t MAX_ENUM_VALUE = 10;
27 namespace MMI {
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 
ShiftAppPointerEvent(const uint8_t * data,const size_t size,size_t & startPos)42 bool ShiftAppPointerEvent(const uint8_t* data, const size_t size, size_t& startPos)
43 {
44     ShiftWindowParam param;
45     bool autoGenDown;
46     startPos += GetObject<ShiftWindowParam>(param, data + startPos, size - startPos);
47     startPos += GetObject<bool>(autoGenDown, data + startPos, size - startPos);
48     InputManager::GetInstance()->ShiftAppPointerEvent(param, autoGenDown);
49     return true;
50 }
51 
ShiftAppPointerEventNoHapTokenVerify(const uint8_t * data,const size_t size,size_t & startPos)52 bool ShiftAppPointerEventNoHapTokenVerify(const uint8_t* data, const size_t size, size_t& startPos)
53 {
54     ShiftWindowParam param;
55     bool autoGenDown;
56     startPos += GetObject<ShiftWindowParam>(param, data + startPos, size - startPos);
57     startPos += GetObject<bool>(autoGenDown, data + startPos, size - startPos);
58     WIN_MGR->ShiftAppPointerEvent(param, autoGenDown);
59     return true;
60 }
61 
ShiftAppPointerEventFuzzTest(const uint8_t * data,const size_t size)62 bool ShiftAppPointerEventFuzzTest(const uint8_t* data, const size_t size)
63 {
64     FuzzedDataProvider fdp(data, size);
65     int32_t sourceWindowId = fdp.ConsumeIntegral<int32_t>();
66     int32_t targetWindowId = fdp.ConsumeIntegral<int32_t>();
67     int32_t x = fdp.ConsumeIntegral<int32_t>();
68     int32_t y = fdp.ConsumeIntegral<int32_t>();
69     int32_t fingerId = fdp.ConsumeIntegral<int32_t>();
70     int32_t sourceType = fdp.ConsumeIntegral<int32_t>() % MAX_ENUM_VALUE;
71     bool autoGenDown = fdp.ConsumeBool();
72     OHOS::MMI::ShiftWindowParam param;
73     param.sourceWindowId = sourceWindowId;
74     param.targetWindowId = targetWindowId;
75     param.x = x;
76     param.y = y;
77     param.fingerId = fingerId;
78     param.sourceType = sourceType;
79     bool winRes = WIN_MGR->ShiftAppPointerEvent(param, autoGenDown);
80     bool mgrRes = InputManager::GetInstance()->ShiftAppPointerEvent(param, autoGenDown);
81     if (winRes && mgrRes) {
82         return true;
83     }
84     return false;
85 }
86 } // MMI
87 } // OHOS
88 
89 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)90 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
91 {
92     /* Run your code on data */
93     OHOS::MMI::ShiftAppPointerEventFuzzTest(data, size);
94     return 0;
95 }