1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License.
5094332d3Sopenharmony_ci * You may obtain a copy of the License at
6094332d3Sopenharmony_ci *
7094332d3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8094332d3Sopenharmony_ci *
9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and
13094332d3Sopenharmony_ci * limitations under the License.
14094332d3Sopenharmony_ci */
15094332d3Sopenharmony_ci
16094332d3Sopenharmony_ci#include "runcapacitancetest_fuzzer.h"
17094332d3Sopenharmony_ci#include <securec.h>
18094332d3Sopenharmony_ci#include "hdf_base.h"
19094332d3Sopenharmony_ci#include "hdf_log.h"
20094332d3Sopenharmony_ci#include "input_manager.h"
21094332d3Sopenharmony_ci
22094332d3Sopenharmony_cistruct AllParameters {
23094332d3Sopenharmony_ci    uint32_t devIndex;
24094332d3Sopenharmony_ci    uint32_t testType;
25094332d3Sopenharmony_ci};
26094332d3Sopenharmony_ci
27094332d3Sopenharmony_cinamespace OHOS {
28094332d3Sopenharmony_ci    bool RunCapacitanceTestFuzzTest(const uint8_t* data, size_t size)
29094332d3Sopenharmony_ci    {
30094332d3Sopenharmony_ci        bool result = false;
31094332d3Sopenharmony_ci        int32_t ret;
32094332d3Sopenharmony_ci        uint32_t length = 32;
33094332d3Sopenharmony_ci        const int MAX_DEVICES = 32;
34094332d3Sopenharmony_ci        InputDevDesc sta[MAX_DEVICES];
35094332d3Sopenharmony_ci        char testresult[] = {0};
36094332d3Sopenharmony_ci        IInputInterface *g_inputInterface;
37094332d3Sopenharmony_ci        const struct AllParameters *params = reinterpret_cast<const struct AllParameters *>(data);
38094332d3Sopenharmony_ci
39094332d3Sopenharmony_ci        (void)memset_s(sta, MAX_DEVICES * sizeof(InputDevDesc), 0, MAX_DEVICES * sizeof(InputDevDesc));
40094332d3Sopenharmony_ci        ret = GetInputInterface(&g_inputInterface);
41094332d3Sopenharmony_ci        if (ret != INPUT_SUCCESS) {
42094332d3Sopenharmony_ci            HDF_LOGE("%s: get input hdi failed, ret %d", __func__, ret);
43094332d3Sopenharmony_ci        }
44094332d3Sopenharmony_ci
45094332d3Sopenharmony_ci        ret = g_inputInterface->iInputManager->ScanInputDevice(sta, MAX_DEVICES);
46094332d3Sopenharmony_ci        if (ret) {
47094332d3Sopenharmony_ci            HDF_LOGE("%s: scan device failed, ret %d", __func__, ret);
48094332d3Sopenharmony_ci        }
49094332d3Sopenharmony_ci        for (int32_t i = 0; i < MAX_DEVICES; i++) {
50094332d3Sopenharmony_ci            if (sta[i].devIndex == 0) {
51094332d3Sopenharmony_ci                break;
52094332d3Sopenharmony_ci            }
53094332d3Sopenharmony_ci
54094332d3Sopenharmony_ci            ret = g_inputInterface->iInputManager->OpenInputDevice(sta[i].devIndex);
55094332d3Sopenharmony_ci            if (ret != INPUT_SUCCESS) {
56094332d3Sopenharmony_ci                HDF_LOGE("%s: open input device failed, ret %d", __func__, ret);
57094332d3Sopenharmony_ci            }
58094332d3Sopenharmony_ci        }
59094332d3Sopenharmony_ci
60094332d3Sopenharmony_ci        ret = g_inputInterface->iInputController->RunCapacitanceTest(
61094332d3Sopenharmony_ci            params->devIndex, params->testType, testresult, length);
62094332d3Sopenharmony_ci        if (ret == INPUT_SUCCESS) {
63094332d3Sopenharmony_ci            result = true;
64094332d3Sopenharmony_ci        }
65094332d3Sopenharmony_ci
66094332d3Sopenharmony_ci        for (int32_t i = 0; i < MAX_DEVICES; i++) {
67094332d3Sopenharmony_ci            if (sta[i].devIndex == 0) {
68094332d3Sopenharmony_ci                break;
69094332d3Sopenharmony_ci            }
70094332d3Sopenharmony_ci
71094332d3Sopenharmony_ci            ret = g_inputInterface->iInputManager->CloseInputDevice(sta[i].devIndex);
72094332d3Sopenharmony_ci            if (ret != INPUT_SUCCESS) {
73094332d3Sopenharmony_ci                HDF_LOGE("%s: close input device failed, ret %d", __func__, ret);
74094332d3Sopenharmony_ci            }
75094332d3Sopenharmony_ci        }
76094332d3Sopenharmony_ci
77094332d3Sopenharmony_ci        ReleaseInputInterface(&g_inputInterface);
78094332d3Sopenharmony_ci        return result;
79094332d3Sopenharmony_ci    }
80094332d3Sopenharmony_ci}
81094332d3Sopenharmony_ci
82094332d3Sopenharmony_ci/* Fuzzer entry point */
83094332d3Sopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
84094332d3Sopenharmony_ci{
85094332d3Sopenharmony_ci    if (data == nullptr) {
86094332d3Sopenharmony_ci        return 0;
87094332d3Sopenharmony_ci    }
88094332d3Sopenharmony_ci
89094332d3Sopenharmony_ci    if (size < sizeof(struct AllParameters)) {
90094332d3Sopenharmony_ci        return 0;
91094332d3Sopenharmony_ci    }
92094332d3Sopenharmony_ci    OHOS::RunCapacitanceTestFuzzTest(data, size);
93094332d3Sopenharmony_ci    return 0;
94094332d3Sopenharmony_ci}
95094332d3Sopenharmony_ci
96