• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "input_method_ability.h"
21 #include "input_method_engine_listener_impl.h"
22 
23 using namespace OHOS::MiscServices;
24 namespace OHOS {
25 class KeyboardListenerImpl : public KeyboardListener {
OnKeyEvent(int32_t keyCode,int32_t keyStatus,sptr<KeyEventConsumerProxy> & consumer)26     bool OnKeyEvent(int32_t keyCode, int32_t keyStatus, sptr<KeyEventConsumerProxy> &consumer)
27     {
28         return true;
29     }
OnKeyEvent(const std::shared_ptr<MMI::KeyEvent> & keyEvent,sptr<KeyEventConsumerProxy> & consumer)30     bool OnKeyEvent(const std::shared_ptr<MMI::KeyEvent> &keyEvent, sptr<KeyEventConsumerProxy> &consumer)
31     {
32         return true;
33     }
OnDealKeyEvent(const std::shared_ptr<MMI::KeyEvent> & keyEvent,sptr<KeyEventConsumerProxy> & consumer)34     bool OnDealKeyEvent(const std::shared_ptr<MMI::KeyEvent> &keyEvent, sptr<KeyEventConsumerProxy> &consumer)
35     {
36         return true;
37     }
OnCursorUpdate(int32_t positionX,int32_t positionY,int32_t height)38     void OnCursorUpdate(int32_t positionX, int32_t positionY, int32_t height) { }
OnSelectionChange(int32_t oldBegin,int32_t oldEnd,int32_t newBegin,int32_t newEnd)39     void OnSelectionChange(int32_t oldBegin, int32_t oldEnd, int32_t newBegin, int32_t newEnd) { }
OnTextChange(const std::string & text)40     void OnTextChange(const std::string &text) { }
OnEditorAttributeChange(const InputAttribute & inputAttribute)41     void OnEditorAttributeChange(const InputAttribute &inputAttribute) { }
42 };
43 
TestInsertText(const std::string & fuzzedString)44 void TestInsertText(const std::string &fuzzedString)
45 {
46     sptr<InputMethodAbility> ability = InputMethodAbility::GetInstance();
47     ability->InsertText(std::move(fuzzedString));
48 }
49 
TestSetImeListener()50 void TestSetImeListener()
51 {
52     sptr<InputMethodAbility> ability = InputMethodAbility::GetInstance();
53     auto engineListener = std::make_shared<InputMethodEngineListenerImpl>();
54     ability->SetImeListener(engineListener);
55 }
56 
TestSetKdListener()57 void TestSetKdListener()
58 {
59     sptr<InputMethodAbility> ability = InputMethodAbility::GetInstance();
60     ability->SetKdListener(nullptr);
61 }
62 
TestDeleteForward(int32_t fuzzedInt32)63 void TestDeleteForward(int32_t fuzzedInt32)
64 {
65     sptr<InputMethodAbility> ability = InputMethodAbility::GetInstance();
66     ability->DeleteForward(fuzzedInt32);
67 }
68 
TestDeleteBackward(int32_t fuzzedInt32)69 void TestDeleteBackward(int32_t fuzzedInt32)
70 {
71     sptr<InputMethodAbility> ability = InputMethodAbility::GetInstance();
72     ability->DeleteBackward(fuzzedInt32);
73 }
74 
TestSendExtendAction(int32_t fuzzedInt32)75 void TestSendExtendAction(int32_t fuzzedInt32)
76 {
77     sptr<InputMethodAbility> ability = InputMethodAbility::GetInstance();
78     ability->SendExtendAction(fuzzedInt32);
79 }
80 
TestHideKeyboardSelf()81 void TestHideKeyboardSelf()
82 {
83     sptr<InputMethodAbility> ability = InputMethodAbility::GetInstance();
84     ability->HideKeyboardSelf();
85 }
86 
TestGetTextBeforeCursor(int32_t fuzzedInt32)87 void TestGetTextBeforeCursor(int32_t fuzzedInt32)
88 {
89     sptr<InputMethodAbility> ability = InputMethodAbility::GetInstance();
90     std::u16string text;
91     ability->GetTextBeforeCursor(fuzzedInt32, text);
92 }
93 
TestGetTextAfterCursor(int32_t fuzzedInt32)94 void TestGetTextAfterCursor(int32_t fuzzedInt32)
95 {
96     sptr<InputMethodAbility> ability = InputMethodAbility::GetInstance();
97     std::u16string text;
98     ability->GetTextAfterCursor(fuzzedInt32, text);
99 }
100 
TestSendFunctionKey(int32_t fuzzedInt32)101 void TestSendFunctionKey(int32_t fuzzedInt32)
102 {
103     sptr<InputMethodAbility> ability = InputMethodAbility::GetInstance();
104     ability->SendFunctionKey(fuzzedInt32);
105 }
106 
TestMoveCursor(int32_t fuzzedInt32)107 void TestMoveCursor(int32_t fuzzedInt32)
108 {
109     sptr<InputMethodAbility> ability = InputMethodAbility::GetInstance();
110     ability->MoveCursor(fuzzedInt32);
111 }
112 
TestDispatchKeyEvent(int32_t fuzzedInt32)113 void TestDispatchKeyEvent(int32_t fuzzedInt32)
114 {
115     sptr<InputMethodAbility> ability = InputMethodAbility::GetInstance();
116     std::shared_ptr<MMI::KeyEvent> keyEvent = MMI::KeyEvent::Create();
117     keyEvent->SetKeyCode(fuzzedInt32);
118     keyEvent->SetKeyAction(fuzzedInt32);
119     sptr<KeyEventConsumerProxy> consumer = new (std::nothrow) KeyEventConsumerProxy(nullptr);
120     ability->DispatchKeyEvent(keyEvent, consumer);
121 }
122 
TestSetCallingWindow(int32_t fuzzedInt32)123 void TestSetCallingWindow(int32_t fuzzedInt32)
124 {
125     sptr<InputMethodAbility> ability = InputMethodAbility::GetInstance();
126     ability->SetCallingWindow(fuzzedInt32);
127 }
128 
TestGetEnterKeyType()129 void TestGetEnterKeyType()
130 {
131     sptr<InputMethodAbility> ability = InputMethodAbility::GetInstance();
132     int32_t keyType;
133     ability->GetEnterKeyType(keyType);
134 }
135 
TestGetInputPattern()136 void TestGetInputPattern()
137 {
138     sptr<InputMethodAbility> ability = InputMethodAbility::GetInstance();
139     int32_t inputPattern;
140     ability->GetInputPattern(inputPattern);
141 }
142 
TestCallingDisplayIdChanged(uint64_t fuzzedUint64)143 void TestCallingDisplayIdChanged(uint64_t fuzzedUint64)
144 {
145     sptr<InputMethodAbility> ability = InputMethodAbility::GetInstance();
146     ability->OnCallingDisplayIdChanged(fuzzedUint64);
147 }
148 } // namespace OHOS
149 
150 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)151 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
152 {
153     /* Run your code on data */
154     std::string fuzzedString(reinterpret_cast<const char *>(data), size);
155     auto fuzzedInt32 = static_cast<int32_t>(size);
156     auto fuzzedUint64 = static_cast<uint64_t>(size);
157     OHOS::TestInsertText(fuzzedString);
158 
159     OHOS::TestSetImeListener();
160 
161     OHOS::TestSetKdListener();
162 
163     OHOS::TestDeleteForward(fuzzedInt32);
164     OHOS::TestDeleteBackward(fuzzedInt32);
165     OHOS::TestSendExtendAction(fuzzedInt32);
166 
167     OHOS::TestHideKeyboardSelf();
168 
169     OHOS::TestGetTextBeforeCursor(fuzzedInt32);
170     OHOS::TestGetTextAfterCursor(fuzzedInt32);
171 
172     OHOS::TestSendFunctionKey(fuzzedInt32);
173     OHOS::TestMoveCursor(fuzzedInt32);
174 
175     OHOS::TestDispatchKeyEvent(fuzzedInt32);
176 
177     OHOS::TestSetCallingWindow(fuzzedInt32);
178 
179     OHOS::TestGetEnterKeyType();
180     OHOS::TestGetInputPattern();
181     OHOS::TestCallingDisplayIdChanged(fuzzedUint64);
182     return 0;
183 }