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