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 "inputsetpowerstatus_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 status;
25094332d3Sopenharmony_ci};
26094332d3Sopenharmony_ci
27094332d3Sopenharmony_cinamespace OHOS {
28094332d3Sopenharmony_ci    bool InputSetPowerStatusFuzzTest(const uint8_t* data, size_t size)
29094332d3Sopenharmony_ci    {
30094332d3Sopenharmony_ci        bool result = false;
31094332d3Sopenharmony_ci        int32_t ret;
32094332d3Sopenharmony_ci        const int MAX_DEVICES = 32;
33094332d3Sopenharmony_ci        InputDevDesc sta[MAX_DEVICES];
34094332d3Sopenharmony_ci        IInputInterface *g_inputInterface;
35094332d3Sopenharmony_ci        const struct AllParameters *params = reinterpret_cast<const struct AllParameters *>(data);
36094332d3Sopenharmony_ci
37094332d3Sopenharmony_ci        (void)memset_s(sta, MAX_DEVICES * sizeof(InputDevDesc), 0, MAX_DEVICES * sizeof(InputDevDesc));
38094332d3Sopenharmony_ci        ret = GetInputInterface(&g_inputInterface);
39094332d3Sopenharmony_ci        if (ret != INPUT_SUCCESS) {
40094332d3Sopenharmony_ci            HDF_LOGE("%s: get input hdi failed, ret %d", __func__, ret);
41094332d3Sopenharmony_ci        }
42094332d3Sopenharmony_ci
43094332d3Sopenharmony_ci        ret = g_inputInterface->iInputManager->ScanInputDevice(sta, MAX_DEVICES);
44094332d3Sopenharmony_ci        if (ret) {
45094332d3Sopenharmony_ci            HDF_LOGE("%s: scan device failed, ret %d", __func__, ret);
46094332d3Sopenharmony_ci        }
47094332d3Sopenharmony_ci        for (int32_t i = 0; i < MAX_DEVICES; i++) {
48094332d3Sopenharmony_ci            if (sta[i].devIndex == 0) {
49094332d3Sopenharmony_ci                break;
50094332d3Sopenharmony_ci            }
51094332d3Sopenharmony_ci
52094332d3Sopenharmony_ci            ret = g_inputInterface->iInputManager->OpenInputDevice(sta[i].devIndex);
53094332d3Sopenharmony_ci            if (ret != INPUT_SUCCESS) {
54094332d3Sopenharmony_ci                HDF_LOGE("%s: open input device failed, ret %d", __func__, ret);
55094332d3Sopenharmony_ci            }
56094332d3Sopenharmony_ci        }
57094332d3Sopenharmony_ci
58094332d3Sopenharmony_ci        ret = g_inputInterface->iInputController->SetPowerStatus(params->devIndex, params->status);
59094332d3Sopenharmony_ci        if (ret == INPUT_SUCCESS) {
60094332d3Sopenharmony_ci            result = true;
61094332d3Sopenharmony_ci        }
62094332d3Sopenharmony_ci
63094332d3Sopenharmony_ci        for (int32_t i = 0; i < MAX_DEVICES; i++) {
64094332d3Sopenharmony_ci            if (sta[i].devIndex == 0) {
65094332d3Sopenharmony_ci                break;
66094332d3Sopenharmony_ci            }
67094332d3Sopenharmony_ci
68094332d3Sopenharmony_ci            ret = g_inputInterface->iInputManager->CloseInputDevice(sta[i].devIndex);
69094332d3Sopenharmony_ci            if (ret != INPUT_SUCCESS) {
70094332d3Sopenharmony_ci                HDF_LOGE("%s: close input device failed, ret %d", __func__, ret);
71094332d3Sopenharmony_ci            }
72094332d3Sopenharmony_ci        }
73094332d3Sopenharmony_ci
74094332d3Sopenharmony_ci        ReleaseInputInterface(&g_inputInterface);
75094332d3Sopenharmony_ci        return result;
76094332d3Sopenharmony_ci    }
77094332d3Sopenharmony_ci}
78094332d3Sopenharmony_ci
79094332d3Sopenharmony_ci/* Fuzzer entry point */
80094332d3Sopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
81094332d3Sopenharmony_ci{
82094332d3Sopenharmony_ci    if (data == nullptr) {
83094332d3Sopenharmony_ci        return 0;
84094332d3Sopenharmony_ci    }
85094332d3Sopenharmony_ci
86094332d3Sopenharmony_ci    if (size < sizeof(struct AllParameters)) {
87094332d3Sopenharmony_ci        return 0;
88094332d3Sopenharmony_ci    }
89094332d3Sopenharmony_ci    OHOS::InputSetPowerStatusFuzzTest(data, size);
90094332d3Sopenharmony_ci    return 0;
91094332d3Sopenharmony_ci}
92094332d3Sopenharmony_ci
93