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 "inputmethodcontroller_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20
21 #include "global.h"
22 #include "input_attribute.h"
23 #include "input_method_controller.h"
24 #include "key_event.h"
25 #include "message_parcel.h"
26 #include "text_listener.h"
27
28
29 using namespace OHOS::MiscServices;
30 namespace OHOS {
31 class SettingListener : public InputMethodSettingListener {
OnImeChange(const Property & property,const SubProperty & subProperty)32 void OnImeChange(const Property &property, const SubProperty &subProperty)
33 {
34 }
OnPanelStatusChange(const InputWindowStatus & status,const std::vector<InputWindowInfo> & windowInfo)35 void OnPanelStatusChange(const InputWindowStatus &status, const std::vector<InputWindowInfo> &windowInfo)
36 {
37 }
38 };
39
TestListInputMethod(sptr<InputMethodController> imc)40 void TestListInputMethod(sptr<InputMethodController> imc)
41 {
42 std::vector<Property> properties = {};
43 imc->ListInputMethod(properties);
44 imc->ListInputMethod(false, properties);
45 imc->ListInputMethod(true, properties);
46 imc->DisplayOptionalInputMethod();
47 }
48
TestListInputMethodSubtype(sptr<InputMethodController> imc,const std::string & fuzzedString,int32_t fuzzedInt32)49 void TestListInputMethodSubtype(sptr<InputMethodController> imc, const std::string &fuzzedString, int32_t fuzzedInt32)
50 {
51 std::vector<SubProperty> subProperties = {};
52 Property property;
53 property.name = fuzzedString;
54 property.id = fuzzedString;
55 property.label = fuzzedString;
56 property.icon = fuzzedString;
57 property.iconId = fuzzedInt32;
58 imc->ListInputMethodSubtype(property, subProperties);
59 }
60
TestDispatchKeyEvent(sptr<InputMethodController> imc,int32_t fuzzedInt32)61 void TestDispatchKeyEvent(sptr<InputMethodController> imc, int32_t fuzzedInt32)
62 {
63 sptr<OnTextChangedListener> textListener = new TextListener();
64 imc->Attach(textListener);
65
66 std::shared_ptr<MMI::KeyEvent> keyEvent = MMI::KeyEvent::Create();
67 keyEvent->SetKeyAction(fuzzedInt32);
68 keyEvent->SetKeyCode(fuzzedInt32);
69 imc->DispatchKeyEvent(keyEvent, [](std::shared_ptr<MMI::KeyEvent> &keyEvent, bool isConsumed) {});
70 }
71
TestOnSelectionChange(sptr<InputMethodController> imc,std::u16string fuzzedU16String,int fuzzedInt,double fuzzedDouble)72 void TestOnSelectionChange(
73 sptr<InputMethodController> imc, std::u16string fuzzedU16String, int fuzzedInt, double fuzzedDouble)
74 {
75 sptr<OnTextChangedListener> textListener = new TextListener();
76 imc->Attach(textListener);
77
78 CursorInfo cursorInfo;
79 cursorInfo.height = fuzzedDouble;
80 cursorInfo.left = fuzzedDouble;
81 cursorInfo.top = fuzzedDouble;
82 cursorInfo.width = fuzzedDouble;
83 imc->OnCursorUpdate(cursorInfo);
84
85 imc->OnSelectionChange(fuzzedU16String, fuzzedInt, fuzzedInt);
86 }
87
TestOnConfigurationChange(sptr<InputMethodController> imc)88 void TestOnConfigurationChange(sptr<InputMethodController> imc)
89 {
90 sptr<OnTextChangedListener> textListener = new TextListener();
91 imc->Attach(textListener);
92
93 Configuration info;
94 EnterKeyType keyType = EnterKeyType::DONE;
95 info.SetEnterKeyType(keyType);
96 TextInputType textInputType = TextInputType::DATETIME;
97 info.SetTextInputType(textInputType);
98 imc->OnConfigurationChange(info);
99 int32_t enterKeyType;
100 int32_t inputPattern;
101 imc->GetEnterKeyType(enterKeyType);
102 imc->GetInputPattern(inputPattern);
103 }
104
TestSwitchInputMethod(SwitchTrigger fuzzedTrigger,sptr<InputMethodController> imc,std::string fuzzedString)105 void TestSwitchInputMethod(SwitchTrigger fuzzedTrigger, sptr<InputMethodController> imc, std::string fuzzedString)
106 {
107 imc->SwitchInputMethod(fuzzedTrigger, fuzzedString, fuzzedString);
108 imc->ShowOptionalInputMethod();
109 }
110
TestSetCallingWindow(sptr<InputMethodController> imc,uint32_t fuzzedUInt32)111 void TestSetCallingWindow(sptr<InputMethodController> imc, uint32_t fuzzedUInt32)
112 {
113 sptr<OnTextChangedListener> textListener = new TextListener();
114 imc->Attach(textListener);
115
116 imc->SetCallingWindow(fuzzedUInt32);
117 imc->ShowSoftKeyboard();
118 imc->HideSoftKeyboard();
119 }
120
TestShowSomething(sptr<InputMethodController> imc)121 void TestShowSomething(sptr<InputMethodController> imc)
122 {
123 sptr<OnTextChangedListener> textListener = new TextListener();
124 imc->Attach(textListener);
125 imc->ShowCurrentInput();
126 imc->HideCurrentInput();
127
128 imc->ShowTextInput();
129 imc->HideTextInput();
130
131 imc->GetCurrentInputMethod();
132 imc->GetCurrentInputMethodSubtype();
133
134 auto settingListener = std::make_shared<SettingListener>();
135 imc->SetSettingListener(settingListener);
136 imc->UpdateListenEventFlag("imeChange", true);
137
138 imc->StopInputSession();
139 imc->Close();
140 }
141
TestUpdateListenEventFlag(sptr<InputMethodController> imc,const std::string & fuzzedString)142 void TestUpdateListenEventFlag(sptr<InputMethodController> imc, const std::string &fuzzedString)
143 {
144 imc->UpdateListenEventFlag(fuzzedString, true);
145 imc->UpdateListenEventFlag(fuzzedString, false);
146 }
147
TestAttach(sptr<InputMethodController> imc,int32_t fuzzedInt32)148 void TestAttach(sptr<InputMethodController> imc, int32_t fuzzedInt32)
149 {
150 sptr<OnTextChangedListener> textListener = new TextListener();
151 InputAttribute inputAttribute;
152 inputAttribute.inputPattern = fuzzedInt32;
153 inputAttribute.enterKeyType = fuzzedInt32;
154 inputAttribute.inputOption = fuzzedInt32;
155 imc->Attach(textListener, true, inputAttribute);
156 imc->Attach(textListener, false, inputAttribute);
157 }
158 } // namespace OHOS
159
160 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)161 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
162 {
163 /* Run your code on data */
164 std::string fuzzedString(data, data + size);
165 std::u16string fuzzedU16String = u"insert text";
166
167 auto fuzzedInt = static_cast<int>(size);
168 auto fuzzedInt32 = static_cast<int32_t>(size);
169 auto fuzzedUInt32 = static_cast<uint32_t>(size);
170 auto fuzzedDouble = static_cast<double>(size);
171 auto fuzzedTrigger = static_cast<SwitchTrigger>(size);
172
173 OHOS::sptr<InputMethodController> imc = InputMethodController::GetInstance();
174
175 OHOS::TestListInputMethod(imc);
176 OHOS::TestListInputMethodSubtype(imc, fuzzedString, fuzzedInt32);
177 OHOS::TestOnSelectionChange(imc, fuzzedU16String, fuzzedInt, fuzzedDouble);
178 OHOS::TestOnConfigurationChange(imc);
179 OHOS::TestSwitchInputMethod(fuzzedTrigger, imc, fuzzedString);
180 OHOS::TestSetCallingWindow(imc, fuzzedUInt32);
181 OHOS::TestDispatchKeyEvent(imc, fuzzedInt32);
182 OHOS::TestShowSomething(imc);
183 OHOS::TestUpdateListenEventFlag(imc, fuzzedString);
184 return 0;
185 }
186