1 /*
2  * Copyright (c) 2023 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 "setkeydownduration_fuzzer.h"
17 
18 #include "securec.h"
19 
20 #include "input_manager.h"
21 #include "mmi_log.h"
22 
23 #undef MMI_LOG_TAG
24 #define MMI_LOG_TAG "SetKeyDownDurationFuzzTest"
25 
26 namespace OHOS {
27 namespace MMI {
GetObject(T & object,const uint8_t * data,size_t size)28 template <class T> size_t GetObject(T &object, const uint8_t *data, size_t size)
29 {
30     size_t objectSize = sizeof(object);
31     if (objectSize > size) {
32         return 0;
33     }
34     errno_t ret = memcpy_s(&object, objectSize, data, objectSize);
35     if (ret != EOK) {
36         return 0;
37     }
38     return objectSize;
39 }
40 
GetString(const uint8_t * data,size_t size,char * object,size_t objectSize)41 size_t GetString(const uint8_t *data, size_t size, char *object, size_t objectSize)
42 {
43     if (objectSize > size) {
44         return 0;
45     }
46     errno_t ret = memcpy_s(&object, objectSize, data, objectSize);
47     if (ret != EOK) {
48         return 0;
49     }
50     return objectSize;
51 }
52 
SetKeyDownDurationFuzzTest(const uint8_t * data,size_t size)53 void SetKeyDownDurationFuzzTest(const uint8_t *data, size_t size)
54 {
55     size_t startPos = 0;
56     size_t stringSize = 4;
57     char businessId[] = "businessId";
58     startPos += GetString(data + startPos, size - startPos, businessId, stringSize);
59     int32_t delay;
60     MMI_HILOGD("SetKeyDownDurationFuzzTest start");
61     startPos += GetObject<int32_t>(delay, data + startPos, size - startPos);
62     InputManager::GetInstance()->SetKeyDownDuration(businessId, delay);
63 }
64 } // MMI
65 } // OHOS
66 
67 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)68 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
69 {
70     /* Run your code on data */
71     OHOS::MMI::SetKeyDownDurationFuzzTest(data, size);
72     return 0;
73 }