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