122736c2fSopenharmony_ci/*
222736c2fSopenharmony_ci * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
322736c2fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
422736c2fSopenharmony_ci * you may not use this file except in compliance with the License.
522736c2fSopenharmony_ci * You may obtain a copy of the License at
622736c2fSopenharmony_ci *
722736c2fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
822736c2fSopenharmony_ci *
922736c2fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1022736c2fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1122736c2fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1222736c2fSopenharmony_ci * See the License for the specific language governing permissions and
1322736c2fSopenharmony_ci * limitations under the License.
1422736c2fSopenharmony_ci */
1522736c2fSopenharmony_ci#define private public
1622736c2fSopenharmony_ci#define protected public
1722736c2fSopenharmony_ci#include "input_method_controller.h"
1822736c2fSopenharmony_ci#include "input_method_system_ability_proxy.h"
1922736c2fSopenharmony_ci#undef private
2022736c2fSopenharmony_ci
2122736c2fSopenharmony_ci#include "inputmethodcontroller_fuzzer.h"
2222736c2fSopenharmony_ci
2322736c2fSopenharmony_ci#include <cstddef>
2422736c2fSopenharmony_ci#include <cstdint>
2522736c2fSopenharmony_ci
2622736c2fSopenharmony_ci#include "global.h"
2722736c2fSopenharmony_ci#include "input_attribute.h"
2822736c2fSopenharmony_ci#include "key_event.h"
2922736c2fSopenharmony_ci#include "message_parcel.h"
3022736c2fSopenharmony_ci#include "text_listener.h"
3122736c2fSopenharmony_ci
3222736c2fSopenharmony_ciusing namespace OHOS::MiscServices;
3322736c2fSopenharmony_cinamespace OHOS {
3422736c2fSopenharmony_civoid TestListInputMethod(sptr<InputMethodController> imc)
3522736c2fSopenharmony_ci{
3622736c2fSopenharmony_ci    std::vector<Property> properties = {};
3722736c2fSopenharmony_ci    imc->ListInputMethod(properties);
3822736c2fSopenharmony_ci    imc->ListInputMethod(false, properties);
3922736c2fSopenharmony_ci    imc->ListInputMethod(true, properties);
4022736c2fSopenharmony_ci    imc->DisplayOptionalInputMethod();
4122736c2fSopenharmony_ci}
4222736c2fSopenharmony_ci
4322736c2fSopenharmony_civoid TestListInputMethodSubtype(sptr<InputMethodController> imc, const std::string &fuzzedString, uint32_t fuzzedUint32)
4422736c2fSopenharmony_ci{
4522736c2fSopenharmony_ci    std::vector<SubProperty> subProperties = {};
4622736c2fSopenharmony_ci    Property property;
4722736c2fSopenharmony_ci    property.name = fuzzedString;
4822736c2fSopenharmony_ci    property.id = fuzzedString;
4922736c2fSopenharmony_ci    property.label = fuzzedString;
5022736c2fSopenharmony_ci    property.icon = fuzzedString;
5122736c2fSopenharmony_ci    property.iconId = fuzzedUint32;
5222736c2fSopenharmony_ci    imc->ListInputMethodSubtype(property, subProperties);
5322736c2fSopenharmony_ci}
5422736c2fSopenharmony_ci
5522736c2fSopenharmony_civoid TestDispatchKeyEvent(sptr<InputMethodController> imc, int32_t fuzzedInt32)
5622736c2fSopenharmony_ci{
5722736c2fSopenharmony_ci    sptr<OnTextChangedListener> textListener = new TextListener();
5822736c2fSopenharmony_ci    imc->Attach(textListener);
5922736c2fSopenharmony_ci
6022736c2fSopenharmony_ci    std::shared_ptr<MMI::KeyEvent> keyEvent = MMI::KeyEvent::Create();
6122736c2fSopenharmony_ci    keyEvent->SetKeyAction(fuzzedInt32);
6222736c2fSopenharmony_ci    keyEvent->SetKeyCode(fuzzedInt32);
6322736c2fSopenharmony_ci    imc->DispatchKeyEvent(keyEvent, [](std::shared_ptr<MMI::KeyEvent> &keyEvent, bool isConsumed) {});
6422736c2fSopenharmony_ci}
6522736c2fSopenharmony_ci
6622736c2fSopenharmony_civoid TestOnSelectionChange(sptr<InputMethodController> imc, std::u16string fuzzedU16String, int fuzzedInt,
6722736c2fSopenharmony_ci    double fuzzedDouble)
6822736c2fSopenharmony_ci{
6922736c2fSopenharmony_ci    sptr<OnTextChangedListener> textListener = new TextListener();
7022736c2fSopenharmony_ci    imc->Attach(textListener);
7122736c2fSopenharmony_ci
7222736c2fSopenharmony_ci    CursorInfo cursorInfo;
7322736c2fSopenharmony_ci    cursorInfo.height = fuzzedDouble;
7422736c2fSopenharmony_ci    cursorInfo.left = fuzzedDouble;
7522736c2fSopenharmony_ci    cursorInfo.top = fuzzedDouble;
7622736c2fSopenharmony_ci    cursorInfo.width = fuzzedDouble;
7722736c2fSopenharmony_ci    imc->OnCursorUpdate(cursorInfo);
7822736c2fSopenharmony_ci
7922736c2fSopenharmony_ci    imc->OnSelectionChange(fuzzedU16String, fuzzedInt, fuzzedInt);
8022736c2fSopenharmony_ci}
8122736c2fSopenharmony_ci
8222736c2fSopenharmony_civoid TestOnConfigurationChange(sptr<InputMethodController> imc)
8322736c2fSopenharmony_ci{
8422736c2fSopenharmony_ci    sptr<OnTextChangedListener> textListener = new TextListener();
8522736c2fSopenharmony_ci    imc->Attach(textListener);
8622736c2fSopenharmony_ci
8722736c2fSopenharmony_ci    Configuration info;
8822736c2fSopenharmony_ci    EnterKeyType keyType = EnterKeyType::DONE;
8922736c2fSopenharmony_ci    info.SetEnterKeyType(keyType);
9022736c2fSopenharmony_ci    TextInputType textInputType = TextInputType::DATETIME;
9122736c2fSopenharmony_ci    info.SetTextInputType(textInputType);
9222736c2fSopenharmony_ci    imc->OnConfigurationChange(info);
9322736c2fSopenharmony_ci    int32_t enterKeyType;
9422736c2fSopenharmony_ci    int32_t inputPattern;
9522736c2fSopenharmony_ci    imc->GetEnterKeyType(enterKeyType);
9622736c2fSopenharmony_ci    imc->GetInputPattern(inputPattern);
9722736c2fSopenharmony_ci}
9822736c2fSopenharmony_ci
9922736c2fSopenharmony_civoid TestSwitchInputMethod(SwitchTrigger fuzzedTrigger, sptr<InputMethodController> imc,
10022736c2fSopenharmony_ci    const std::string &fuzzedString)
10122736c2fSopenharmony_ci{
10222736c2fSopenharmony_ci    imc->SwitchInputMethod(fuzzedTrigger, fuzzedString, fuzzedString);
10322736c2fSopenharmony_ci    imc->ShowOptionalInputMethod();
10422736c2fSopenharmony_ci}
10522736c2fSopenharmony_ci
10622736c2fSopenharmony_civoid TestSetCallingWindow(sptr<InputMethodController> imc, uint32_t fuzzedUInt32)
10722736c2fSopenharmony_ci{
10822736c2fSopenharmony_ci    sptr<OnTextChangedListener> textListener = new TextListener();
10922736c2fSopenharmony_ci    imc->Attach(textListener);
11022736c2fSopenharmony_ci
11122736c2fSopenharmony_ci    imc->SetCallingWindow(fuzzedUInt32);
11222736c2fSopenharmony_ci    imc->ShowSoftKeyboard();
11322736c2fSopenharmony_ci    imc->HideSoftKeyboard();
11422736c2fSopenharmony_ci}
11522736c2fSopenharmony_ci
11622736c2fSopenharmony_civoid TestShowSomething(sptr<InputMethodController> imc)
11722736c2fSopenharmony_ci{
11822736c2fSopenharmony_ci    sptr<OnTextChangedListener> textListener = new TextListener();
11922736c2fSopenharmony_ci    imc->Attach(textListener);
12022736c2fSopenharmony_ci    imc->ShowCurrentInput();
12122736c2fSopenharmony_ci    imc->HideCurrentInput();
12222736c2fSopenharmony_ci
12322736c2fSopenharmony_ci    imc->ShowTextInput();
12422736c2fSopenharmony_ci    imc->HideTextInput();
12522736c2fSopenharmony_ci
12622736c2fSopenharmony_ci    imc->GetCurrentInputMethod();
12722736c2fSopenharmony_ci    imc->GetCurrentInputMethodSubtype();
12822736c2fSopenharmony_ci
12922736c2fSopenharmony_ci    imc->StopInputSession();
13022736c2fSopenharmony_ci    imc->Close();
13122736c2fSopenharmony_ci}
13222736c2fSopenharmony_ci
13322736c2fSopenharmony_civoid TestUpdateListenEventFlag(sptr<InputMethodController> imc, uint32_t fuzzedUint32)
13422736c2fSopenharmony_ci{
13522736c2fSopenharmony_ci    imc->UpdateListenEventFlag(static_cast<uint32_t>(fuzzedUint32), static_cast<uint32_t>(fuzzedUint32), true);
13622736c2fSopenharmony_ci    imc->UpdateListenEventFlag(static_cast<uint32_t>(fuzzedUint32), static_cast<uint32_t>(fuzzedUint32), false);
13722736c2fSopenharmony_ci}
13822736c2fSopenharmony_ci
13922736c2fSopenharmony_civoid TestAttach(sptr<InputMethodController> imc, int32_t fuzzedInt32)
14022736c2fSopenharmony_ci{
14122736c2fSopenharmony_ci    sptr<OnTextChangedListener> textListener = new TextListener();
14222736c2fSopenharmony_ci    InputAttribute inputAttribute;
14322736c2fSopenharmony_ci    inputAttribute.inputPattern = fuzzedInt32;
14422736c2fSopenharmony_ci    inputAttribute.enterKeyType = fuzzedInt32;
14522736c2fSopenharmony_ci    inputAttribute.inputOption = fuzzedInt32;
14622736c2fSopenharmony_ci    imc->Attach(textListener, true, inputAttribute);
14722736c2fSopenharmony_ci    imc->Attach(textListener, false, inputAttribute);
14822736c2fSopenharmony_ci}
14922736c2fSopenharmony_ci
15022736c2fSopenharmony_civoid FUZZHideInput(sptr<InputMethodController> imc)
15122736c2fSopenharmony_ci{
15222736c2fSopenharmony_ci    sptr<IInputClient> client = new (std::nothrow) InputClientStub();
15322736c2fSopenharmony_ci    imc->HideInput(client);
15422736c2fSopenharmony_ci    imc->RequestHideInput();
15522736c2fSopenharmony_ci}
15622736c2fSopenharmony_ci
15722736c2fSopenharmony_civoid FUZZShowInput(sptr<InputMethodController> imc)
15822736c2fSopenharmony_ci{
15922736c2fSopenharmony_ci    sptr<IInputClient> client = new (std::nothrow) InputClientStub();
16022736c2fSopenharmony_ci    imc->ShowInput(client);
16122736c2fSopenharmony_ci    imc->RequestShowInput();
16222736c2fSopenharmony_ci}
16322736c2fSopenharmony_ci
16422736c2fSopenharmony_civoid FUZZRestore(sptr<InputMethodController> imc)
16522736c2fSopenharmony_ci{
16622736c2fSopenharmony_ci    imc->RestoreListenEventFlag();
16722736c2fSopenharmony_ci    imc->RestoreListenInfoInSaDied();
16822736c2fSopenharmony_ci    imc->RestoreAttachInfoInSaDied();
16922736c2fSopenharmony_ci}
17022736c2fSopenharmony_ci
17122736c2fSopenharmony_civoid InputType(sptr<InputMethodController> imc)
17222736c2fSopenharmony_ci{
17322736c2fSopenharmony_ci    imc->IsInputTypeSupported(InputType::CAMERA_INPUT);
17422736c2fSopenharmony_ci    imc->IsInputTypeSupported(InputType::SECURITY_INPUT);
17522736c2fSopenharmony_ci    imc->StartInputType(InputType::CAMERA_INPUT);
17622736c2fSopenharmony_ci    imc->StartInputType(InputType::SECURITY_INPUT);
17722736c2fSopenharmony_ci}
17822736c2fSopenharmony_ci
17922736c2fSopenharmony_civoid FUZZIsPanelShown(sptr<InputMethodController> imc, const uint8_t *data)
18022736c2fSopenharmony_ci{
18122736c2fSopenharmony_ci    PanelInfo panelInfo = { .panelType = SOFT_KEYBOARD, .panelFlag = FLG_FIXED };
18222736c2fSopenharmony_ci    bool flag = static_cast<bool>(data[0] % 2);
18322736c2fSopenharmony_ci    imc->IsPanelShown(panelInfo, flag);
18422736c2fSopenharmony_ci}
18522736c2fSopenharmony_ci
18622736c2fSopenharmony_civoid FUZZPrintLogIfAceTimeout(sptr<InputMethodController> imc, int64_t start)
18722736c2fSopenharmony_ci{
18822736c2fSopenharmony_ci    imc->PrintLogIfAceTimeout(start);
18922736c2fSopenharmony_ci}
19022736c2fSopenharmony_ci} // namespace OHOS
19122736c2fSopenharmony_ci
19222736c2fSopenharmony_ci/* Fuzzer entry point */
19322736c2fSopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
19422736c2fSopenharmony_ci{
19522736c2fSopenharmony_ci    /* Run your code on data */
19622736c2fSopenharmony_ci    std::string fuzzedString(data, data + size);
19722736c2fSopenharmony_ci    std::u16string fuzzedU16String = u"insert text";
19822736c2fSopenharmony_ci
19922736c2fSopenharmony_ci    auto fuzzedInt = static_cast<int>(size);
20022736c2fSopenharmony_ci    auto fuzzedInt32 = static_cast<int32_t>(size);
20122736c2fSopenharmony_ci    auto fuzzedUint32 = static_cast<uint32_t>(size);
20222736c2fSopenharmony_ci    auto fuzzedint64 = static_cast<int64_t>(size);
20322736c2fSopenharmony_ci    auto fuzzedDouble = static_cast<double>(size);
20422736c2fSopenharmony_ci    auto fuzzedTrigger = static_cast<SwitchTrigger>(size);
20522736c2fSopenharmony_ci
20622736c2fSopenharmony_ci    OHOS::sptr<InputMethodController> imc = InputMethodController::GetInstance();
20722736c2fSopenharmony_ci
20822736c2fSopenharmony_ci    OHOS::TestListInputMethod(imc);
20922736c2fSopenharmony_ci    OHOS::TestListInputMethodSubtype(imc, fuzzedString, fuzzedUint32);
21022736c2fSopenharmony_ci    OHOS::TestOnSelectionChange(imc, fuzzedU16String, fuzzedInt, fuzzedDouble);
21122736c2fSopenharmony_ci    OHOS::TestOnConfigurationChange(imc);
21222736c2fSopenharmony_ci    OHOS::TestSwitchInputMethod(fuzzedTrigger, imc, fuzzedString);
21322736c2fSopenharmony_ci    OHOS::TestSetCallingWindow(imc, fuzzedUint32);
21422736c2fSopenharmony_ci    OHOS::TestDispatchKeyEvent(imc, fuzzedInt32);
21522736c2fSopenharmony_ci    OHOS::TestShowSomething(imc);
21622736c2fSopenharmony_ci    OHOS::FUZZHideInput(imc);
21722736c2fSopenharmony_ci    OHOS::FUZZShowInput(imc);
21822736c2fSopenharmony_ci    OHOS::FUZZRestore(imc);
21922736c2fSopenharmony_ci    OHOS::InputType(imc);
22022736c2fSopenharmony_ci    OHOS::FUZZIsPanelShown(imc, data);
22122736c2fSopenharmony_ci    OHOS::FUZZPrintLogIfAceTimeout(imc, fuzzedint64);
22222736c2fSopenharmony_ci    OHOS::TestUpdateListenEventFlag(imc, fuzzedUint32);
22322736c2fSopenharmony_ci    return 0;
22422736c2fSopenharmony_ci}
225