122736c2fSopenharmony_ci/*
222736c2fSopenharmony_ci * Copyright (c) 2024 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 "systemcmdchannelstub_fuzzer.h"
1722736c2fSopenharmony_ci
1822736c2fSopenharmony_ci#include <cstddef>
1922736c2fSopenharmony_ci#include <cstdint>
2022736c2fSopenharmony_ci
2122736c2fSopenharmony_ci#include "system_cmd_channel_stub.h"
2222736c2fSopenharmony_ci#include "message_parcel.h"
2322736c2fSopenharmony_ci
2422736c2fSopenharmony_ciusing namespace OHOS::MiscServices;
2522736c2fSopenharmony_cinamespace OHOS {
2622736c2fSopenharmony_ciconstexpr size_t THRESHOLD = 10;
2722736c2fSopenharmony_ciconstexpr int32_t OFFSET = 4;
2822736c2fSopenharmony_ciconstexpr int32_t PRIVATEDATAVALUE = 100;
2922736c2fSopenharmony_ciconst std::u16string AGENTSTUB_INTERFACE_TOKEN = u"ohos.miscservices.inputmethod.ISystemCmdChannel";
3022736c2fSopenharmony_ciuint32_t ConvertToUint32(const uint8_t *ptr)
3122736c2fSopenharmony_ci{
3222736c2fSopenharmony_ci    if (ptr == nullptr) {
3322736c2fSopenharmony_ci        return 0;
3422736c2fSopenharmony_ci    }
3522736c2fSopenharmony_ci    uint32_t bigVar = (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
3622736c2fSopenharmony_ci    return bigVar;
3722736c2fSopenharmony_ci}
3822736c2fSopenharmony_cibool FuzzSystemCmdChannelStub(const uint8_t *rawData, size_t size)
3922736c2fSopenharmony_ci{
4022736c2fSopenharmony_ci    InputType fuzzedBool = static_cast<InputType>(rawData[0] % 2);
4122736c2fSopenharmony_ci    auto fuzzedUint32 = static_cast<uint32_t>(size);
4222736c2fSopenharmony_ci
4322736c2fSopenharmony_ci    uint32_t code = ConvertToUint32(rawData);
4422736c2fSopenharmony_ci    rawData = rawData + OFFSET;
4522736c2fSopenharmony_ci    size = size - OFFSET;
4622736c2fSopenharmony_ci
4722736c2fSopenharmony_ci    SysPanelStatus sysPanelStatus = {fuzzedBool, 0, fuzzedUint32, fuzzedUint32};
4822736c2fSopenharmony_ci
4922736c2fSopenharmony_ci    std::unordered_map <std::string, PrivateDataValue> privateCommand;
5022736c2fSopenharmony_ci    PrivateDataValue privateDataValue1 = std::string("stringValue");
5122736c2fSopenharmony_ci    PrivateDataValue privateDataValue2 = static_cast<int32_t>(fuzzedBool);
5222736c2fSopenharmony_ci    PrivateDataValue privateDataValue3 = PRIVATEDATAVALUE;
5322736c2fSopenharmony_ci    privateCommand.emplace("value1", privateDataValue1);
5422736c2fSopenharmony_ci    privateCommand.emplace("value2", privateDataValue2);
5522736c2fSopenharmony_ci    privateCommand.emplace("value3", privateDataValue3);
5622736c2fSopenharmony_ci
5722736c2fSopenharmony_ci    MessageParcel data;
5822736c2fSopenharmony_ci    data.WriteInterfaceToken(AGENTSTUB_INTERFACE_TOKEN);
5922736c2fSopenharmony_ci    data.WriteBuffer(rawData, size);
6022736c2fSopenharmony_ci    data.RewindRead(0);
6122736c2fSopenharmony_ci    MessageParcel reply;
6222736c2fSopenharmony_ci    MessageOption option;
6322736c2fSopenharmony_ci
6422736c2fSopenharmony_ci    sptr <SystemCmdChannelStub> stub = new SystemCmdChannelStub();
6522736c2fSopenharmony_ci    stub->SendPrivateCommand(privateCommand);
6622736c2fSopenharmony_ci    stub->NotifyPanelStatus(sysPanelStatus);
6722736c2fSopenharmony_ci    stub->OnRemoteRequest(code, data, reply, option);
6822736c2fSopenharmony_ci    return true;
6922736c2fSopenharmony_ci}
7022736c2fSopenharmony_ci} // namespace OHOS
7122736c2fSopenharmony_ci/* Fuzzer entry point */
7222736c2fSopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
7322736c2fSopenharmony_ci{
7422736c2fSopenharmony_ci    if (size < OHOS::THRESHOLD) {
7522736c2fSopenharmony_ci        return 0;
7622736c2fSopenharmony_ci    }
7722736c2fSopenharmony_ci    /* Run your code on data */
7822736c2fSopenharmony_ci    OHOS::FuzzSystemCmdChannelStub(data, size);
7922736c2fSopenharmony_ci    return 0;
8022736c2fSopenharmony_ci}