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