1/*
2 * Copyright (c) 2024 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#define private public
17#define protected public
18#include "enable_ime_data_parser.h"
19#undef private
20
21#include "enableimedataparse_fuzzer.h"
22
23using namespace OHOS::MiscServices;
24namespace OHOS {
25constexpr size_t THRESHOLD = 10;
26void FuzzInitialize(const int32_t userId)
27{
28    EnableImeDataParser::GetInstance()->Initialize(userId);
29}
30
31void FuzzOnUserChanged(const int32_t userId)
32{
33    EnableImeDataParser::GetInstance()->OnUserChanged(userId);
34}
35
36void FuzzCheckNeedSwitch(const std::string &key, SwitchInfo &switchInfo, const int32_t userId)
37{
38    EnableImeDataParser::GetInstance()->CheckNeedSwitch(key, switchInfo, userId);
39    EnableImeDataParser::GetInstance()->CheckNeedSwitch(switchInfo, userId);
40}
41
42void FuzzGetEnableData(const std::string &key, std::vector<std::string> &enableVec, const int32_t userId)
43{
44    EnableImeDataParser::GetInstance()->GetEnableData(key, enableVec, userId);
45}
46
47void FuzzParseEnableIme(const std::string &valueStr, int32_t userId, std::vector<std::string> &enableVec)
48{
49    EnableImeDataParser::GetInstance()->ParseEnableIme(valueStr, userId, enableVec);
50}
51
52void FuzzParseEnableKeyboard(const std::string &valueStr, int32_t userId, std::vector<std::string> &enableVec)
53{
54    EnableImeDataParser::GetInstance()->ParseEnableKeyboard(valueStr, userId, enableVec);
55}
56
57void FuzzCheckTargetEnableName(const std::string &key, const std::string &targetName, std::string &nextIme,
58                               const int32_t userId)
59{
60    EnableImeDataParser::GetInstance()->CheckTargetEnableName(key, targetName, nextIme, userId);
61}
62} // namespace OHOS
63/* Fuzzer entry point */
64extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
65{
66    if (size < OHOS::THRESHOLD) {
67        return 0;
68    }
69    /* Run your code on data */
70    const int32_t userId = static_cast<int32_t>(size);
71    std::string fuzzedString(reinterpret_cast<const char *>(data), size);
72    SwitchInfo switchInfo;
73    std::vector<std::string> enableVec;
74
75    OHOS::FuzzInitialize(userId);
76    OHOS::FuzzOnUserChanged(userId);
77    OHOS::FuzzCheckNeedSwitch(fuzzedString, switchInfo, userId);
78    OHOS::FuzzGetEnableData(fuzzedString, enableVec, userId);
79
80    OHOS::FuzzParseEnableIme(fuzzedString, userId, enableVec);
81    OHOS::FuzzParseEnableKeyboard(fuzzedString, userId, enableVec);
82    OHOS::FuzzCheckTargetEnableName(fuzzedString, fuzzedString, fuzzedString, userId);
83    return 0;
84}