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