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 "getdisplaybindinfo_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 "GetDisplayBindInfoFuzzTest"
25
26 namespace OHOS {
27 namespace MMI {
28 namespace {
29 constexpr int32_t DEFAULT_PREKEY_COUNT = 5;
30 } // namespace
31
32 template<class T>
GetObject(T & object,const uint8_t * data,size_t size)33 size_t GetObject(T &object, const uint8_t *data, size_t size)
34 {
35 size_t objectSize = sizeof(object);
36 if (objectSize > size) {
37 return 0;
38 }
39 errno_t ret = memcpy_s(&object, objectSize, data, objectSize);
40 if (ret != EOK) {
41 return 0;
42 }
43 return objectSize;
44 }
45
GetString(const uint8_t * data,size_t size,char * object,size_t objectSize)46 size_t GetString(const uint8_t *data, size_t size, char *object, size_t objectSize)
47 {
48 if (objectSize > size) {
49 return 0;
50 }
51 errno_t ret = memcpy_s(&object, objectSize, data, objectSize);
52 if (ret != EOK) {
53 return 0;
54 }
55 return objectSize;
56 }
57
GetDisplayBindInfoFuzzTest(const uint8_t * data,size_t size)58 void GetDisplayBindInfoFuzzTest(const uint8_t* data, size_t size)
59 {
60 std::vector<DisplayBindInfo> displayBindInfos;
61 size_t startPos = 0;
62 size_t stringSize = 4;
63 for (int32_t i = 0; i < DEFAULT_PREKEY_COUNT; i++) {
64 DisplayBindInfo displayBindInfo;
65 int32_t inputDeviceId;
66 startPos += GetObject<int32_t>(inputDeviceId, data + startPos, size - startPos);
67 displayBindInfo.inputDeviceId = inputDeviceId;
68 int32_t displayId;
69 startPos += GetObject<int32_t>(displayId, data + startPos, size - startPos);
70 displayBindInfo.displayId = displayId;
71 char inputDeviceName[] = "inputDeviceName";
72 startPos += GetString(data + startPos, size - startPos, inputDeviceName, stringSize);
73 displayBindInfo.inputDeviceName = inputDeviceName;
74 char displayName[] = "displayName";
75 startPos += GetString(data + startPos, size - startPos, displayName, stringSize);
76 displayBindInfo.displayName = displayName;
77 displayBindInfos.push_back(displayBindInfo);
78 }
79 MMI_HILOGD("GetDisplayBindInfo start");
80 InputManager::GetInstance()->GetDisplayBindInfo(displayBindInfos);
81 }
82 } // MMI
83 } // OHOS
84
85 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)86 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
87 {
88 /* Run your code on data */
89 OHOS::MMI::GetDisplayBindInfoFuzzTest(data, size);
90 return 0;
91 }
92
93