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