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 #define private public
21 #define protected public
22 #include "input_method_ability.h"
23 #undef private
24
25 #include "fuzzer/FuzzedDataProvider.h"
26 #include "input_client_service_impl.h"
27 #include "input_method_engine_listener_impl.h"
28 #include "ime_mirror_manager.h"
29
30 using namespace OHOS::MiscServices;
31 namespace OHOS {
32 constexpr int32_t MAIN_USER_ID = 100;
33 class KeyboardListenerImpl : public KeyboardListener {
OnKeyEvent(int32_t keyCode,int32_t keyStatus,sptr<KeyEventConsumerProxy> & consumer)34 bool OnKeyEvent(int32_t keyCode, int32_t keyStatus, sptr<KeyEventConsumerProxy> &consumer)
35 {
36 return true;
37 }
OnKeyEvent(const std::shared_ptr<MMI::KeyEvent> & keyEvent,sptr<KeyEventConsumerProxy> & consumer)38 bool OnKeyEvent(const std::shared_ptr<MMI::KeyEvent> &keyEvent, sptr<KeyEventConsumerProxy> &consumer)
39 {
40 return true;
41 }
OnDealKeyEvent(const std::shared_ptr<MMI::KeyEvent> & keyEvent,uint64_t cbId,const sptr<IRemoteObject> & channelObject)42 bool OnDealKeyEvent(
43 const std::shared_ptr<MMI::KeyEvent> &keyEvent, uint64_t cbId, const sptr<IRemoteObject> &channelObject)
44 {
45 return true;
46 }
OnCursorUpdate(int32_t positionX,int32_t positionY,int32_t height)47 void OnCursorUpdate(int32_t positionX, int32_t positionY, int32_t height) { }
OnSelectionChange(int32_t oldBegin,int32_t oldEnd,int32_t newBegin,int32_t newEnd)48 void OnSelectionChange(int32_t oldBegin, int32_t oldEnd, int32_t newBegin, int32_t newEnd) { }
OnTextChange(const std::string & text)49 void OnTextChange(const std::string &text) { }
OnEditorAttributeChange(const InputAttribute & inputAttribute)50 void OnEditorAttributeChange(const InputAttribute &inputAttribute) { }
51 };
52
InitializeClientInfo(InputClientInfo & clientInfo)53 bool InitializeClientInfo(InputClientInfo &clientInfo)
54 {
55 sptr<IInputClient> clientStub = new (std::nothrow) InputClientServiceImpl();
56 if (clientStub == nullptr) {
57 IMSA_HILOGE("failed to create client");
58 return false;
59 }
60 sptr<InputDeathRecipient> deathRecipient = new (std::nothrow) InputDeathRecipient();
61 if (deathRecipient == nullptr) {
62 IMSA_HILOGE("failed to new deathRecipient");
63 return false;
64 }
65 clientInfo = { .userID = MAIN_USER_ID, .client = clientStub, .deathRecipient = deathRecipient };
66 return true;
67 }
68
TestInsertText(const std::string & fuzzedString)69 void TestInsertText(const std::string &fuzzedString)
70 {
71 InputMethodAbility::GetInstance().InsertText(std::move(fuzzedString));
72 }
73
TestSetImeListener()74 void TestSetImeListener()
75 {
76 auto engineListener = std::make_shared<InputMethodEngineListenerImpl>();
77 InputMethodAbility::GetInstance().SetImeListener(engineListener);
78 }
79
TestSetKdListener()80 void TestSetKdListener()
81 {
82 InputMethodAbility::GetInstance().SetKdListener(nullptr);
83 }
84
TestDeleteForward(int32_t fuzzedInt32)85 void TestDeleteForward(int32_t fuzzedInt32)
86 {
87 InputMethodAbility::GetInstance().DeleteForward(fuzzedInt32);
88 }
89
TestDeleteBackward(int32_t fuzzedInt32)90 void TestDeleteBackward(int32_t fuzzedInt32)
91 {
92 InputMethodAbility::GetInstance().DeleteBackward(fuzzedInt32);
93 }
94
TestSendExtendAction(int32_t fuzzedInt32)95 void TestSendExtendAction(int32_t fuzzedInt32)
96 {
97 InputMethodAbility::GetInstance().SendExtendAction(fuzzedInt32);
98 }
99
TestHideKeyboardSelf()100 void TestHideKeyboardSelf()
101 {
102 InputMethodAbility::GetInstance().HideKeyboardSelf();
103 }
104
TestGetTextBeforeCursor(int32_t fuzzedInt32)105 void TestGetTextBeforeCursor(int32_t fuzzedInt32)
106 {
107 std::u16string text;
108 InputMethodAbility::GetInstance().GetTextBeforeCursor(fuzzedInt32, text);
109 }
110
TestGetTextAfterCursor(int32_t fuzzedInt32)111 void TestGetTextAfterCursor(int32_t fuzzedInt32)
112 {
113 std::u16string text;
114 InputMethodAbility::GetInstance().GetTextAfterCursor(fuzzedInt32, text);
115 }
116
TestSendFunctionKey(int32_t fuzzedInt32)117 void TestSendFunctionKey(int32_t fuzzedInt32)
118 {
119 InputMethodAbility::GetInstance().SendFunctionKey(fuzzedInt32);
120 }
121
TestMoveCursor(int32_t fuzzedInt32)122 void TestMoveCursor(int32_t fuzzedInt32)
123 {
124 InputMethodAbility::GetInstance().MoveCursor(fuzzedInt32);
125 }
126
TestDispatchKeyEvent(int32_t fuzzedInt32)127 void TestDispatchKeyEvent(int32_t fuzzedInt32)
128 {
129 std::shared_ptr<MMI::KeyEvent> keyEvent = MMI::KeyEvent::Create();
130 keyEvent->SetKeyCode(fuzzedInt32);
131 keyEvent->SetKeyAction(fuzzedInt32);
132 InputMethodAbility::GetInstance().DispatchKeyEvent(keyEvent, fuzzedInt32, nullptr);
133 }
134
TestSetCallingWindow(int32_t fuzzedInt32)135 void TestSetCallingWindow(int32_t fuzzedInt32)
136 {
137 InputMethodAbility::GetInstance().SetCallingWindow(fuzzedInt32);
138 }
139
TestGetEnterKeyType()140 void TestGetEnterKeyType()
141 {
142 int32_t keyType;
143 InputMethodAbility::GetInstance().GetEnterKeyType(keyType);
144 }
145
TestGetInputPattern()146 void TestGetInputPattern()
147 {
148 int32_t inputPattern;
149 InputMethodAbility::GetInstance().GetInputPattern(inputPattern);
150 }
151
TestCallingDisplayIdChanged(uint64_t fuzzedUint64)152 void TestCallingDisplayIdChanged(uint64_t fuzzedUint64)
153 {
154 InputMethodAbility::GetInstance().OnCallingDisplayIdChanged(fuzzedUint64);
155 }
156
TestRegisterProxyIme(uint64_t fuzzedUint64)157 void TestRegisterProxyIme(uint64_t fuzzedUint64)
158 {
159 InputMethodAbility::GetInstance().RegisterProxyIme(fuzzedUint64);
160 }
161
TestUnregisterProxyIme(uint64_t fuzzedUint64)162 void TestUnregisterProxyIme(uint64_t fuzzedUint64)
163 {
164 InputMethodAbility::GetInstance().UnregisterProxyIme(fuzzedUint64);
165 }
166
TestStartInput(const InputClientInfo & clientInfo,bool isBindFromClient)167 void TestStartInput(const InputClientInfo &clientInfo, bool isBindFromClient)
168 {
169 InputMethodAbility::GetInstance().StartInput(clientInfo, isBindFromClient);
170 }
171
TestIsDisplayChanged(uint64_t oldDisplayId,uint64_t newDisplayId)172 void TestIsDisplayChanged(uint64_t oldDisplayId, uint64_t newDisplayId)
173 {
174 InputMethodAbility::GetInstance().IsDisplayChanged(oldDisplayId, newDisplayId);
175 }
176
TestOnSelectionChange(std::u16string text,int32_t oldBegin,int32_t oldEnd,int32_t newBegin,int32_t newEnd)177 void TestOnSelectionChange(std::u16string text, int32_t oldBegin, int32_t oldEnd,
178 int32_t newBegin, int32_t newEnd)
179 {
180 InputMethodAbility::GetInstance().OnSelectionChange(text, oldBegin, oldEnd, newBegin, newEnd);
181 }
182
TestOperationKeyboard(int32_t cmdId,uint32_t sessionId)183 void TestOperationKeyboard(int32_t cmdId, uint32_t sessionId)
184 {
185 InputMethodAbility::GetInstance().HideKeyboardImplWithoutLock(cmdId, sessionId);
186 InputMethodAbility::GetInstance().ShowKeyboardImplWithLock(cmdId);
187 }
TestInterfaceCoverage(int32_t dataInt32,bool dataBool,std::u16string & text,int64_t consumeTime)188 void TestInterfaceCoverage(int32_t dataInt32, bool dataBool, std::u16string &text, int64_t consumeTime)
189 {
190 InputMethodAbility::GetInstance().SelectByMovement(dataInt32);
191 InputMethodAbility::GetInstance().GetEnterKeyType(dataInt32);
192 InputMethodAbility::GetInstance().GetSecurityMode(dataInt32);
193 InputMethodAbility::GetInstance().FinishTextPreview();
194 InputMethodAbility::GetInstance().GetTextBeforeCursor(dataInt32, text);
195 InputMethodAbility::GetInstance().ReportBaseTextOperation(dataInt32, dataInt32, consumeTime);
196 }
197
TestImeMirrorManager(FuzzedDataProvider & provider)198 void TestImeMirrorManager(FuzzedDataProvider &provider)
199 {
200 ImeMirrorManager mgr;
201 mgr.SetImeMirrorEnable(provider.ConsumeBool());
202 mgr.IsImeMirrorEnable();
203 mgr.SubscribeSaStart([]() { }, provider.ConsumeIntegral<int32_t>());
204 mgr.UnSubscribeSaStart(provider.ConsumeIntegral<int32_t>());
205
206 InputMethodAbility::GetInstance().BindImeMirror();
207 InputMethodAbility::GetInstance().UnbindImeMirror();
208 }
209 } // namespace OHOS
210
211 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)212 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
213 {
214 /* Run your code on data */
215 FuzzedDataProvider provider(data, size);
216 std::string fuzzedString(reinterpret_cast<const char *>(data), size);
217 auto fuzzedInt32 = provider.ConsumeIntegral<int32_t>();
218 auto fuzzedUint64 = provider.ConsumeIntegral<uint64_t>();
219 auto fuzzedInt64 = provider.ConsumeIntegral<int64_t>();
220 InputClientInfo clientInfo;
221 if (!OHOS::InitializeClientInfo(clientInfo)) {
222 return false;
223 }
224 auto fuzzedBool = static_cast<bool>(data[0] % 2);
225
226 std::u16string fuzzedU16String = u"insert text";
227
228 OHOS::TestInsertText(fuzzedString);
229
230 OHOS::TestSetImeListener();
231
232 OHOS::TestSetKdListener();
233
234 OHOS::TestDeleteForward(fuzzedInt32);
235 OHOS::TestDeleteBackward(fuzzedInt32);
236 OHOS::TestSendExtendAction(fuzzedInt32);
237
238 OHOS::TestHideKeyboardSelf();
239
240 OHOS::TestGetTextBeforeCursor(fuzzedInt32);
241 OHOS::TestGetTextAfterCursor(fuzzedInt32);
242
243 OHOS::TestSendFunctionKey(fuzzedInt32);
244 OHOS::TestMoveCursor(fuzzedInt32);
245
246 OHOS::TestDispatchKeyEvent(fuzzedInt32);
247
248 OHOS::TestSetCallingWindow(fuzzedInt32);
249
250 OHOS::TestGetEnterKeyType();
251 OHOS::TestGetInputPattern();
252 OHOS::TestCallingDisplayIdChanged(fuzzedUint64);
253 OHOS::TestRegisterProxyIme(fuzzedUint64);
254 OHOS::TestUnregisterProxyIme(fuzzedUint64);
255 OHOS::TestStartInput(clientInfo, fuzzedBool);
256 OHOS::TestIsDisplayChanged(fuzzedUint64, fuzzedUint64);
257 OHOS::TestOnSelectionChange(fuzzedU16String, fuzzedInt32, fuzzedInt32, fuzzedInt32, fuzzedInt32);
258 OHOS::TestOperationKeyboard(fuzzedInt32, fuzzedInt32);
259 OHOS::TestInterfaceCoverage(fuzzedInt32, fuzzedBool, fuzzedU16String, fuzzedInt64);
260 OHOS::TestImeMirrorManager(provider);
261 return 0;
262 }