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