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 "supportkeys_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, "SupportKeysFuzzTest" };
27 constexpr int32_t FUZZ_FST_DATA = 0;
28 constexpr int32_t MAX_SUPPORT_KEY = 5;
29 } // namespace
30
31 template<class T>
GetObject(const uint8_t * data,size_t size,T & object)32 size_t GetObject(const uint8_t *data, size_t size, T &object)
33 {
34 size_t objectSize = sizeof(object);
35 if (objectSize > size) {
36 return 0;
37 }
38 errno_t ret = memcpy_s(&object, objectSize, data, objectSize);
39 if (ret != EOK) {
40 return 0;
41 }
42 return objectSize;
43 }
44
SupportKeysFuzzTest(const uint8_t * data,size_t size)45 void SupportKeysFuzzTest(const uint8_t* data, size_t size)
46 {
47 int32_t id = data[FUZZ_FST_DATA];
48 size_t startPos = 0;
49 std::vector<int32_t> keycodes;
50 for (int32_t i = 0; i < MAX_SUPPORT_KEY; i++) {
51 int32_t preKey;
52 startPos += GetObject<int32_t>(data + startPos, size - startPos, preKey);
53 keycodes.push_back(preKey);
54 }
55 auto fun = [](std::vector<bool> isSupport) {
56 MMI_HILOGD("Support key success");
57 };
58 InputManager::GetInstance()->SupportKeys(id, keycodes, fun);
59 }
60 } // namespace MMI
61 } // namespace OHOS
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 /* Run your code on data */
67 OHOS::MMI::SupportKeysFuzzTest(data, size);
68 return 0;
69 }