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 "subscribekeyevent_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 "SubscribeKeyEventFuzzTest"
25
26 namespace OHOS {
27 namespace MMI {
28 namespace {
29 constexpr int32_t DEFAULT_PREKEY_COUNT = 3;
30 } // namespace
31
32 class InputEventConsumerTest : public IInputEventConsumer {
33 public:
OnInputEvent(std::shared_ptr<PointerEvent> pointerEvent) const34 virtual void OnInputEvent(std::shared_ptr<PointerEvent> pointerEvent) const override
35 {
36 MMI_HILOGD("Report pointer event success");
37 };
OnInputEvent(std::shared_ptr<KeyEvent> keyEvent) const38 virtual void OnInputEvent(std::shared_ptr<KeyEvent> keyEvent) const override {};
OnInputEvent(std::shared_ptr<AxisEvent> axisEvent) const39 virtual void OnInputEvent(std::shared_ptr<AxisEvent> axisEvent) const override {};
40 };
41
42 template<class T>
GetObject(T & object,const uint8_t * data,size_t size)43 size_t GetObject(T &object, const uint8_t *data, size_t size)
44 {
45 size_t bodySize = sizeof(object);
46 if (bodySize > size) {
47 return 0;
48 }
49 errno_t retNum = memcpy_s(&object, bodySize, data, bodySize);
50 if (retNum != EOK) {
51 return 0;
52 }
53 return bodySize;
54 }
55
SubscribeKeyEventFuzzTest(const uint8_t * data,size_t size)56 void SubscribeKeyEventFuzzTest(const uint8_t* data, size_t size)
57 {
58 std::shared_ptr<KeyOption> keyOption = std::make_shared<KeyOption>();
59 std::set<int32_t> prekeys;
60 size_t startPos = 0;
61 for (int32_t i = 0; i < DEFAULT_PREKEY_COUNT; i++) {
62 int32_t preKey;
63 startPos += GetObject<int32_t>(preKey, data + startPos, size - startPos);
64 prekeys.insert(preKey);
65 }
66 keyOption->SetPreKeys(prekeys);
67 int32_t keyDown;
68 startPos += GetObject<int32_t>(keyDown, data + startPos, size - startPos);
69 keyOption->SetFinalKeyDown(keyDown != 0);
70 int32_t keyDownDuration;
71 startPos += GetObject<int32_t>(keyDownDuration, data + startPos, size - startPos);
72 keyOption->SetFinalKeyDownDuration(keyDownDuration);
73 int32_t finalKey;
74 startPos += GetObject<int32_t>(finalKey, data + startPos, size - startPos);
75 keyOption->SetFinalKey(finalKey);
76 auto fun = [](std::shared_ptr<KeyEvent> event) {
77 MMI_HILOGD("Subscribe keyevent success");
78 };
79 int32_t subscribeId = InputManager::GetInstance()->SubscribeKeyEvent(keyOption, fun);
80 InputManager::GetInstance()->UnsubscribeKeyEvent(subscribeId);
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::SubscribeKeyEventFuzzTest(data, size);
90 return 0;
91 }
92
93