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#define private public
16#define protected public
17#include "input_method_system_ability.h"
18#include "input_method_system_ability_proxy.h"
19#undef private
20
21#include <atomic>
22#include <cstddef>
23#include <cstdint>
24#include <string_ex.h>
25
26#include "accesstoken_kit.h"
27#include "global.h"
28#include "ime_cfg_manager.h"
29#include "input_method_controller.h"
30#include "iservice_registry.h"
31#include "message_parcel.h"
32#include "nativetoken_kit.h"
33#include "system_ability_definition.h"
34#include "inputmethodsystemability_fuzzer.h"
35#include "text_listener.h"
36#include "token_setproc.h"
37
38using namespace OHOS::MiscServices;
39namespace OHOS {
40constexpr const int32_t MSG_ID_USER_ONE = 50;
41constexpr const int32_t MSG_ID_USER_TWO = 60;
42void FuzzOnUser(int32_t userId, const std::string &packageName)
43{
44    //onUserStarted
45    MessageParcel *parcel = new MessageParcel();
46    DelayedSingleton<InputMethodSystemAbility>::GetInstance()->isScbEnable_ = false;
47    DelayedSingleton<InputMethodSystemAbility>::GetInstance()->userId_ = MSG_ID_USER_ONE;
48    parcel->WriteInt32(MSG_ID_USER_ONE);
49    auto msg = std::make_shared<Message>(MessageID::MSG_ID_USER_START, parcel);
50    DelayedSingleton<InputMethodSystemAbility>::GetInstance()->OnUserStarted(msg.get());
51
52    //onUserRemoved
53    MessageParcel *parcel1 = new MessageParcel();
54    parcel1->WriteInt32(MSG_ID_USER_TWO);
55    auto msg1 = std::make_shared<Message>(MessageID::MSG_ID_USER_REMOVED, parcel1);
56    DelayedSingleton<InputMethodSystemAbility>::GetInstance()->OnUserRemoved(msg1.get());
57
58    //HandlePackageEvent
59    MessageParcel *parcel2 = new (std::nothrow) MessageParcel();
60    auto bundleName = "testBundleName1";
61    DelayedSingleton<InputMethodSystemAbility>::GetInstance()->userId_ = MSG_ID_USER_TWO;
62    parcel2->WriteInt32(MSG_ID_USER_ONE);
63    parcel2->WriteString(bundleName);
64    auto msg2 = std::make_shared<Message>(MessageID::MSG_ID_PACKAGE_REMOVED, parcel2);
65    DelayedSingleton<InputMethodSystemAbility>::GetInstance()->HandlePackageEvent(msg2.get());
66
67    //OnPackageRemoved
68    DelayedSingleton<InputMethodSystemAbility>::GetInstance()->userId_ = userId;
69    DelayedSingleton<InputMethodSystemAbility>::GetInstance()->OnPackageRemoved(userId, bundleName);
70}
71
72} // namespace OHOS
73/* Fuzzer entry point */
74extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
75{
76    /* Run your code on data */
77    const int32_t userId = static_cast<int32_t>(size);
78    std::string fuzzedString(reinterpret_cast<const char *>(data), size);
79
80    OHOS::FuzzOnUser(userId, fuzzedString);
81    return 0;
82}
83