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