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