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 "common_method.h"
21 #include "input_manager.h"
22 #include "mmi_log.h"
23
24 namespace OHOS {
25 namespace MMI {
26 namespace {
27 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "SubscribeKeyEventFuzzTest" };
28 constexpr int32_t DEFAULT_PREKEY_COUNT = 4;
29 } // namespace
30
31 class InputEventConsumerTest : public IInputEventConsumer {
32 public:
OnInputEvent(std::shared_ptr<KeyEvent> keyEvent) const33 virtual void OnInputEvent(std::shared_ptr<KeyEvent> keyEvent) const override {};
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<AxisEvent> axisEvent) const38 virtual void OnInputEvent(std::shared_ptr<AxisEvent> axisEvent) const override {};
39 };
40
SubscribeKeyEventFuzzTest(const uint8_t * data,size_t size)41 void SubscribeKeyEventFuzzTest(const uint8_t* data, size_t size)
42 {
43 std::shared_ptr<KeyOption> keyOption = std::make_shared<KeyOption>();
44 std::set<int32_t> prekeys;
45 size_t startPos = 0;
46 for (int32_t i = 0; i < DEFAULT_PREKEY_COUNT; i++) {
47 int32_t preKey;
48 startPos += GetObject<int32_t>(preKey, data + startPos, size - startPos);
49 prekeys.insert(preKey);
50 }
51 keyOption->SetPreKeys(prekeys);
52 int32_t keyDown;
53 startPos += GetObject<int32_t>(keyDown, data + startPos, size - startPos);
54 keyOption->SetFinalKeyDown(keyDown != 0);
55 int32_t keyDownDuration;
56 startPos += GetObject<int32_t>(keyDownDuration, data + startPos, size - startPos);
57 keyOption->SetFinalKeyDownDuration(keyDownDuration);
58 int32_t finalKey;
59 startPos += GetObject<int32_t>(finalKey, data + startPos, size - startPos);
60 keyOption->SetFinalKey(finalKey);
61 auto fun = [](std::shared_ptr<KeyEvent> event) {
62 MMI_HILOGD("Subscribe keyevent success");
63 };
64 int32_t subscribeId = InputManager::GetInstance()->SubscribeKeyEvent(keyOption, fun);
65 InputManager::GetInstance()->UnsubscribeKeyEvent(subscribeId);
66 }
67 } // MMI
68 } // OHOS
69
70 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)71 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
72 {
73 /* Run your code on data */
74 OHOS::MMI::SubscribeKeyEventFuzzTest(data, size);
75 return 0;
76 }
77
78