1/*
2 * Copyright (c) 2022-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 "inputmethodability_fuzzer.h"
17
18#include <utility>
19
20#include "input_method_ability.h"
21#include "input_method_engine_listener_impl.h"
22
23using namespace OHOS::MiscServices;
24namespace OHOS {
25class KeyboardListenerImpl : public KeyboardListener {
26    bool OnKeyEvent(int32_t keyCode, int32_t keyStatus, sptr<KeyEventConsumerProxy> &consumer)
27    {
28        return true;
29    }
30    bool OnKeyEvent(const std::shared_ptr<MMI::KeyEvent> &keyEvent, sptr<KeyEventConsumerProxy> &consumer)
31    {
32        return true;
33    }
34    bool OnDealKeyEvent(const std::shared_ptr<MMI::KeyEvent> &keyEvent, sptr<KeyEventConsumerProxy> &consumer)
35    {
36        return true;
37    }
38    void OnCursorUpdate(int32_t positionX, int32_t positionY, int32_t height)
39    {
40    }
41    void OnSelectionChange(int32_t oldBegin, int32_t oldEnd, int32_t newBegin, int32_t newEnd)
42    {
43    }
44    void OnTextChange(const std::string &text)
45    {
46    }
47    void OnEditorAttributeChange(const InputAttribute &inputAttribute)
48    {
49    }
50};
51
52void TestInsertText(const std::string &fuzzedString)
53{
54    sptr<InputMethodAbility> ability = InputMethodAbility::GetInstance();
55    ability->InsertText(std::move(fuzzedString));
56}
57
58void TestSetImeListener()
59{
60    sptr<InputMethodAbility> ability = InputMethodAbility::GetInstance();
61    auto engineListener = std::make_shared<InputMethodEngineListenerImpl>();
62    ability->SetImeListener(engineListener);
63}
64
65void TestSetKdListener()
66{
67    sptr<InputMethodAbility> ability = InputMethodAbility::GetInstance();
68    ability->SetKdListener(nullptr);
69}
70
71void TestDeleteForward(int32_t fuzzedInt32)
72{
73    sptr<InputMethodAbility> ability = InputMethodAbility::GetInstance();
74    ability->DeleteForward(fuzzedInt32);
75}
76
77void TestDeleteBackward(int32_t fuzzedInt32)
78{
79    sptr<InputMethodAbility> ability = InputMethodAbility::GetInstance();
80    ability->DeleteBackward(fuzzedInt32);
81}
82
83void TestSendExtendAction(int32_t fuzzedInt32)
84{
85    sptr<InputMethodAbility> ability = InputMethodAbility::GetInstance();
86    ability->SendExtendAction(fuzzedInt32);
87}
88
89void TestHideKeyboardSelf()
90{
91    sptr<InputMethodAbility> ability = InputMethodAbility::GetInstance();
92    ability->HideKeyboardSelf();
93}
94
95void TestGetTextBeforeCursor(int32_t fuzzedInt32)
96{
97    sptr<InputMethodAbility> ability = InputMethodAbility::GetInstance();
98    std::u16string text;
99    ability->GetTextBeforeCursor(fuzzedInt32, text);
100}
101
102void TestGetTextAfterCursor(int32_t fuzzedInt32)
103{
104    sptr<InputMethodAbility> ability = InputMethodAbility::GetInstance();
105    std::u16string text;
106    ability->GetTextAfterCursor(fuzzedInt32, text);
107}
108
109void TestSendFunctionKey(int32_t fuzzedInt32)
110{
111    sptr<InputMethodAbility> ability = InputMethodAbility::GetInstance();
112    ability->SendFunctionKey(fuzzedInt32);
113}
114
115void TestMoveCursor(int32_t fuzzedInt32)
116{
117    sptr<InputMethodAbility> ability = InputMethodAbility::GetInstance();
118    ability->MoveCursor(fuzzedInt32);
119}
120
121void TestDispatchKeyEvent(int32_t fuzzedInt32)
122{
123    sptr<InputMethodAbility> ability = InputMethodAbility::GetInstance();
124    std::shared_ptr<MMI::KeyEvent> keyEvent = MMI::KeyEvent::Create();
125    keyEvent->SetKeyCode(fuzzedInt32);
126    keyEvent->SetKeyAction(fuzzedInt32);
127    sptr<KeyEventConsumerProxy> consumer = new (std::nothrow) KeyEventConsumerProxy(nullptr);
128    ability->DispatchKeyEvent(keyEvent, consumer);
129}
130
131void TestSetCallingWindow(int32_t fuzzedInt32)
132{
133    sptr<InputMethodAbility> ability = InputMethodAbility::GetInstance();
134    ability->SetCallingWindow(fuzzedInt32);
135}
136
137void TestGetEnterKeyType()
138{
139    sptr<InputMethodAbility> ability = InputMethodAbility::GetInstance();
140    int32_t keyType;
141    ability->GetEnterKeyType(keyType);
142}
143
144void TestGetInputPattern()
145{
146    sptr<InputMethodAbility> ability = InputMethodAbility::GetInstance();
147    int32_t inputPattern;
148    ability->GetInputPattern(inputPattern);
149}
150} // namespace OHOS
151
152/* Fuzzer entry point */
153extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
154{
155    /* Run your code on data */
156    std::string fuzzedString(reinterpret_cast<const char *>(data), size);
157    auto fuzzedInt32 = static_cast<int32_t>(size);
158
159    OHOS::TestInsertText(fuzzedString);
160
161    OHOS::TestSetImeListener();
162
163    OHOS::TestSetKdListener();
164
165    OHOS::TestDeleteForward(fuzzedInt32);
166    OHOS::TestDeleteBackward(fuzzedInt32);
167    OHOS::TestSendExtendAction(fuzzedInt32);
168
169    OHOS::TestHideKeyboardSelf();
170
171    OHOS::TestGetTextBeforeCursor(fuzzedInt32);
172    OHOS::TestGetTextAfterCursor(fuzzedInt32);
173
174    OHOS::TestSendFunctionKey(fuzzedInt32);
175    OHOS::TestMoveCursor(fuzzedInt32);
176
177    OHOS::TestDispatchKeyEvent(fuzzedInt32);
178
179    OHOS::TestSetCallingWindow(fuzzedInt32);
180
181    OHOS::TestGetEnterKeyType();
182    OHOS::TestGetInputPattern();
183    return 0;
184}