122736c2fSopenharmony_ci/*
222736c2fSopenharmony_ci * Copyright (c) 2022-2022 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
1622736c2fSopenharmony_ci#include "perusersession_fuzzer.h"
1722736c2fSopenharmony_ci
1822736c2fSopenharmony_ci#define private public
1922736c2fSopenharmony_ci#define protected public
2022736c2fSopenharmony_ci#include "peruser_session.h"
2122736c2fSopenharmony_ci#undef private
2222736c2fSopenharmony_ci
2322736c2fSopenharmony_ci#include <cstddef>
2422736c2fSopenharmony_ci#include <cstdint>
2522736c2fSopenharmony_ci#include <memory>
2622736c2fSopenharmony_ci#include <string_ex.h>
2722736c2fSopenharmony_ci
2822736c2fSopenharmony_ci#include "global.h"
2922736c2fSopenharmony_ci#include "i_input_method_agent.h"
3022736c2fSopenharmony_ci#include "i_input_method_core.h"
3122736c2fSopenharmony_ci#include "input_client_proxy.h"
3222736c2fSopenharmony_ci#include "input_client_stub.h"
3322736c2fSopenharmony_ci#include "input_method_ability.h"
3422736c2fSopenharmony_ci#include "input_method_agent_proxy.h"
3522736c2fSopenharmony_ci#include "input_method_agent_stub.h"
3622736c2fSopenharmony_ci#include "input_method_core_proxy.h"
3722736c2fSopenharmony_ci#include "input_method_core_stub.h"
3822736c2fSopenharmony_ci#include "input_method_info.h"
3922736c2fSopenharmony_ci#include "input_method_property.h"
4022736c2fSopenharmony_ci#include "iremote_broker.h"
4122736c2fSopenharmony_ci#include "message_parcel.h"
4222736c2fSopenharmony_ci#include "input_method_types.h"
4322736c2fSopenharmony_ci
4422736c2fSopenharmony_ciusing namespace OHOS::MiscServices;
4522736c2fSopenharmony_cinamespace OHOS {
4622736c2fSopenharmony_ciconstexpr size_t THRESHOLD = 10;
4722736c2fSopenharmony_ciconstexpr int32_t MAIN_USER_ID = 100;
4822736c2fSopenharmony_ci
4922736c2fSopenharmony_ciuint32_t ConvertToUint32(const uint8_t *ptr)
5022736c2fSopenharmony_ci{
5122736c2fSopenharmony_ci    if (ptr == nullptr) {
5222736c2fSopenharmony_ci        return 0;
5322736c2fSopenharmony_ci    }
5422736c2fSopenharmony_ci    uint32_t bigVar = (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
5522736c2fSopenharmony_ci    return bigVar;
5622736c2fSopenharmony_ci}
5722736c2fSopenharmony_ci
5822736c2fSopenharmony_cibool InitializeClientInfo(InputClientInfo &clientInfo)
5922736c2fSopenharmony_ci{
6022736c2fSopenharmony_ci    sptr<IInputClient> clientStub = new (std::nothrow) InputClientStub();
6122736c2fSopenharmony_ci    if (clientStub == nullptr) {
6222736c2fSopenharmony_ci        IMSA_HILOGE("failed to create client");
6322736c2fSopenharmony_ci        return false;
6422736c2fSopenharmony_ci    }
6522736c2fSopenharmony_ci    sptr<InputDeathRecipient> deathRecipient = new (std::nothrow) InputDeathRecipient();
6622736c2fSopenharmony_ci    if (deathRecipient == nullptr) {
6722736c2fSopenharmony_ci        IMSA_HILOGE("failed to new deathRecipient");
6822736c2fSopenharmony_ci        return false;
6922736c2fSopenharmony_ci    }
7022736c2fSopenharmony_ci    clientInfo = { .userID = MAIN_USER_ID, .client = clientStub, .deathRecipient = deathRecipient };
7122736c2fSopenharmony_ci    return true;
7222736c2fSopenharmony_ci}
7322736c2fSopenharmony_ci
7422736c2fSopenharmony_cibool FuzzPerUserSession(const uint8_t *rawData, size_t size)
7522736c2fSopenharmony_ci{
7622736c2fSopenharmony_ci    Property property;
7722736c2fSopenharmony_ci    SubProperty subProperty;
7822736c2fSopenharmony_ci    InputClientInfo clientInfo;
7922736c2fSopenharmony_ci    if (!InitializeClientInfo(clientInfo)) {
8022736c2fSopenharmony_ci        return false;
8122736c2fSopenharmony_ci    }
8222736c2fSopenharmony_ci    auto client = iface_cast<IInputClient>(clientInfo.client->AsObject());
8322736c2fSopenharmony_ci    sptr<InputMethodCoreStub> coreStub = new (std::nothrow) InputMethodCoreStub();
8422736c2fSopenharmony_ci    if (coreStub == nullptr) {
8522736c2fSopenharmony_ci        return false;
8622736c2fSopenharmony_ci    }
8722736c2fSopenharmony_ci    auto core = iface_cast<IInputMethodCore>(coreStub->AsObject());
8822736c2fSopenharmony_ci    sptr<InputMethodAgentStub> agentStub = new (std::nothrow) InputMethodAgentStub();
8922736c2fSopenharmony_ci    if (agentStub == nullptr) {
9022736c2fSopenharmony_ci        return false;
9122736c2fSopenharmony_ci    }
9222736c2fSopenharmony_ci    auto agent = iface_cast<IInputMethodAgent>(agentStub);
9322736c2fSopenharmony_ci    static std::shared_ptr<PerUserSession> userSessions = std::make_shared<PerUserSession>(MAIN_USER_ID);
9422736c2fSopenharmony_ci
9522736c2fSopenharmony_ci    userSessions->OnRegisterProxyIme(core, agent->AsObject());
9622736c2fSopenharmony_ci    int32_t type = 4;
9722736c2fSopenharmony_ci    userSessions->OnUnRegisteredProxyIme(static_cast<UnRegisteredType>(type), core);
9822736c2fSopenharmony_ci    userSessions->IsProxyImeEnable();
9922736c2fSopenharmony_ci
10022736c2fSopenharmony_ci    userSessions->OnPrepareInput(clientInfo);
10122736c2fSopenharmony_ci    userSessions->OnSetCoreAndAgent(core, agent->AsObject());
10222736c2fSopenharmony_ci    userSessions->OnShowCurrentInput();
10322736c2fSopenharmony_ci    sptr<IRemoteObject> agentObject = nullptr;
10422736c2fSopenharmony_ci    clientInfo.isShowKeyboard = false;
10522736c2fSopenharmony_ci    userSessions->OnStartInput(clientInfo, agentObject);
10622736c2fSopenharmony_ci    clientInfo.isShowKeyboard = true;
10722736c2fSopenharmony_ci    userSessions->OnStartInput(clientInfo, agentObject);
10822736c2fSopenharmony_ci    userSessions->NotifyImeChangeToClients(property, subProperty);
10922736c2fSopenharmony_ci    userSessions->OnHideCurrentInput();
11022736c2fSopenharmony_ci    userSessions->OnHideInput(client);
11122736c2fSopenharmony_ci    userSessions->OnReleaseInput(client);
11222736c2fSopenharmony_ci    return true;
11322736c2fSopenharmony_ci}
11422736c2fSopenharmony_ci} // namespace OHOS
11522736c2fSopenharmony_ci/* Fuzzer entry point */
11622736c2fSopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
11722736c2fSopenharmony_ci{
11822736c2fSopenharmony_ci    if (size < OHOS::THRESHOLD) {
11922736c2fSopenharmony_ci        return 0;
12022736c2fSopenharmony_ci    }
12122736c2fSopenharmony_ci    /* Run your code on data */
12222736c2fSopenharmony_ci    OHOS::FuzzPerUserSession(data, size);
12322736c2fSopenharmony_ci    return 0;
12422736c2fSopenharmony_ci}