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