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 "inputunregisterreportcallback_fuzzer.h"
17 #include <securec.h>
18 #include "hdf_base.h"
19 #include "hdf_log.h"
20 #include "input_manager.h"
21
22 namespace OHOS {
InputUnregisterReportCallbackFuzzTest(const uint8_t * data,size_t size)23 bool InputUnregisterReportCallbackFuzzTest(const uint8_t* data, size_t size)
24 {
25 bool result = false;
26 int32_t ret;
27 const int MAX_DEVICES = 32;
28 InputDevDesc sta[MAX_DEVICES];
29 constexpr int32_t TOUCH_INDEX = 1;
30 InputEventCb g_callback;
31 IInputInterface *g_inputInterface;
32
33 (void)memset_s(sta, MAX_DEVICES * sizeof(InputDevDesc), 0, MAX_DEVICES * sizeof(InputDevDesc));
34 ret = GetInputInterface(&g_inputInterface);
35 if (ret != INPUT_SUCCESS) {
36 HDF_LOGE("%s: get input hdi failed, ret %d", __func__, ret);
37 }
38
39 ret = g_inputInterface->iInputManager->ScanInputDevice(sta, MAX_DEVICES);
40 if (ret) {
41 HDF_LOGE("%s: scan device failed, ret %d", __func__, ret);
42 }
43 for (int32_t i = 0; i < MAX_DEVICES; i++) {
44 if (sta[i].devIndex == 0) {
45 break;
46 }
47
48 ret = g_inputInterface->iInputManager->OpenInputDevice(sta[i].devIndex);
49 if (ret != INPUT_SUCCESS) {
50 HDF_LOGE("%s: open input device failed, ret %d", __func__, ret);
51 }
52 }
53
54 ret = g_inputInterface->iInputReporter->RegisterReportCallback(TOUCH_INDEX, &g_callback);
55 if (ret == INPUT_SUCCESS) {
56 result = true;
57 }
58
59 ret = g_inputInterface->iInputReporter->UnregisterReportCallback(*(uint32_t *)data);
60 if (ret == INPUT_SUCCESS) {
61 result = true;
62 }
63
64 for (int32_t i = 0; i < MAX_DEVICES; i++) {
65 if (sta[i].devIndex == 0) {
66 break;
67 }
68
69 ret = g_inputInterface->iInputManager->CloseInputDevice(sta[i].devIndex);
70 if (ret != INPUT_SUCCESS) {
71 HDF_LOGE("%s: close input device failed, ret %d", __func__, ret);
72 }
73 }
74
75 ReleaseInputInterface(g_inputInterface);
76 return result;
77 }
78 }
79
80 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)81 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
82 {
83 if (data == nullptr) {
84 return 0;
85 }
86 if (size < sizeof(uint32_t)) {
87 return 0;
88 }
89 OHOS::InputUnregisterReportCallbackFuzzTest(data, size);
90 return 0;
91 }
92
93