15b8fca66Sopenharmony_ci/*
25b8fca66Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
35b8fca66Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
45b8fca66Sopenharmony_ci * you may not use this file except in compliance with the License.
55b8fca66Sopenharmony_ci * You may obtain a copy of the License at
65b8fca66Sopenharmony_ci *
75b8fca66Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
85b8fca66Sopenharmony_ci *
95b8fca66Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
105b8fca66Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
115b8fca66Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
125b8fca66Sopenharmony_ci * See the License for the specific language governing permissions and
135b8fca66Sopenharmony_ci * limitations under the License.
145b8fca66Sopenharmony_ci */
155b8fca66Sopenharmony_ci#include "telephonyobserver_fuzzer.h"
165b8fca66Sopenharmony_ci
175b8fca66Sopenharmony_ci#include <cstddef>
185b8fca66Sopenharmony_ci#include <cstdint>
195b8fca66Sopenharmony_ci#include "addstateregistrytoken_fuzzer.h"
205b8fca66Sopenharmony_ci#define private public
215b8fca66Sopenharmony_ci#include "securec.h"
225b8fca66Sopenharmony_ci#include "system_ability_definition.h"
235b8fca66Sopenharmony_ci#include "telephony_observer.h"
245b8fca66Sopenharmony_ci#include "telephony_state_registry_service.h"
255b8fca66Sopenharmony_ci
265b8fca66Sopenharmony_ciusing namespace OHOS::Telephony;
275b8fca66Sopenharmony_cinamespace OHOS {
285b8fca66Sopenharmony_cistatic bool g_isInited = false;
295b8fca66Sopenharmony_ciconstexpr int32_t BOOL_NUM = 2;
305b8fca66Sopenharmony_ciconstexpr int32_t SLOT_NUM = 2;
315b8fca66Sopenharmony_ciconstexpr int32_t ROAMING_NUM = 4;
325b8fca66Sopenharmony_ciconstexpr int32_t REG_NUM = 6;
335b8fca66Sopenharmony_ciconstexpr int32_t NR_NUM = 7;
345b8fca66Sopenharmony_ciconstexpr int32_t RADIO_NUM = 13;
355b8fca66Sopenharmony_ciTelephonyObserver telephonyObserver;
365b8fca66Sopenharmony_ci
375b8fca66Sopenharmony_cibool IsServiceInited()
385b8fca66Sopenharmony_ci{
395b8fca66Sopenharmony_ci    if (!g_isInited) {
405b8fca66Sopenharmony_ci        DelayedSingleton<TelephonyStateRegistryService>::GetInstance()->OnStart();
415b8fca66Sopenharmony_ci        if (DelayedSingleton<TelephonyStateRegistryService>::GetInstance()->GetServiceRunningState() ==
425b8fca66Sopenharmony_ci            static_cast<int32_t>(ServiceRunningState::STATE_RUNNING)) {
435b8fca66Sopenharmony_ci            g_isInited = true;
445b8fca66Sopenharmony_ci        }
455b8fca66Sopenharmony_ci    }
465b8fca66Sopenharmony_ci    return g_isInited;
475b8fca66Sopenharmony_ci}
485b8fca66Sopenharmony_ci
495b8fca66Sopenharmony_civoid OnRemoteRequest(const uint8_t *data, size_t size)
505b8fca66Sopenharmony_ci{
515b8fca66Sopenharmony_ci    if (!IsServiceInited()) {
525b8fca66Sopenharmony_ci        return;
535b8fca66Sopenharmony_ci    }
545b8fca66Sopenharmony_ci    MessageParcel dataMessageParcel;
555b8fca66Sopenharmony_ci    if (!dataMessageParcel.WriteInterfaceToken(TelephonyObserver::GetDescriptor())) {
565b8fca66Sopenharmony_ci        return;
575b8fca66Sopenharmony_ci    }
585b8fca66Sopenharmony_ci    dataMessageParcel.WriteBuffer(data, size);
595b8fca66Sopenharmony_ci    dataMessageParcel.RewindRead(0);
605b8fca66Sopenharmony_ci    uint32_t code = static_cast<uint32_t>(size);
615b8fca66Sopenharmony_ci    MessageParcel reply;
625b8fca66Sopenharmony_ci    MessageOption option;
635b8fca66Sopenharmony_ci    telephonyObserver.OnRemoteRequest(code, dataMessageParcel, reply, option);
645b8fca66Sopenharmony_ci}
655b8fca66Sopenharmony_ci
665b8fca66Sopenharmony_civoid CallStateUpdatedInner(const uint8_t *data, size_t size)
675b8fca66Sopenharmony_ci{
685b8fca66Sopenharmony_ci    if (!IsServiceInited()) {
695b8fca66Sopenharmony_ci        return;
705b8fca66Sopenharmony_ci    }
715b8fca66Sopenharmony_ci    int32_t slotId = static_cast<int32_t>(size % SLOT_NUM);
725b8fca66Sopenharmony_ci    int32_t callState = static_cast<int32_t>(size);
735b8fca66Sopenharmony_ci    std::string phoneNumber(reinterpret_cast<const char *>(data), size);
745b8fca66Sopenharmony_ci    MessageParcel dataMessageParcel;
755b8fca66Sopenharmony_ci    dataMessageParcel.WriteInt32(slotId);
765b8fca66Sopenharmony_ci    dataMessageParcel.WriteInt32(callState);
775b8fca66Sopenharmony_ci    dataMessageParcel.WriteString16(Str8ToStr16(phoneNumber));
785b8fca66Sopenharmony_ci    dataMessageParcel.RewindRead(0);
795b8fca66Sopenharmony_ci    MessageParcel reply;
805b8fca66Sopenharmony_ci    telephonyObserver.OnCallStateUpdatedInner(dataMessageParcel, reply);
815b8fca66Sopenharmony_ci}
825b8fca66Sopenharmony_ci
835b8fca66Sopenharmony_civoid SignalInfoUpdatedInner(const uint8_t *data, size_t size)
845b8fca66Sopenharmony_ci{
855b8fca66Sopenharmony_ci    if (!IsServiceInited()) {
865b8fca66Sopenharmony_ci        return;
875b8fca66Sopenharmony_ci    }
885b8fca66Sopenharmony_ci    int32_t slotId = static_cast<int32_t>(size % SLOT_NUM);
895b8fca66Sopenharmony_ci    MessageParcel dataMessageParcel;
905b8fca66Sopenharmony_ci    dataMessageParcel.WriteInt32(slotId);
915b8fca66Sopenharmony_ci    dataMessageParcel.RewindRead(0);
925b8fca66Sopenharmony_ci    MessageParcel reply;
935b8fca66Sopenharmony_ci    telephonyObserver.OnSignalInfoUpdatedInner(dataMessageParcel, reply);
945b8fca66Sopenharmony_ci}
955b8fca66Sopenharmony_ci
965b8fca66Sopenharmony_civoid NetworkStateUpdatedInner(const uint8_t *data, size_t size)
975b8fca66Sopenharmony_ci{
985b8fca66Sopenharmony_ci    if (!IsServiceInited()) {
995b8fca66Sopenharmony_ci        return;
1005b8fca66Sopenharmony_ci    }
1015b8fca66Sopenharmony_ci    int32_t slotId = static_cast<int32_t>(size % SLOT_NUM);
1025b8fca66Sopenharmony_ci    MessageParcel dataMessageParcel;
1035b8fca66Sopenharmony_ci    dataMessageParcel.WriteInt32(slotId);
1045b8fca66Sopenharmony_ci    auto networkState = std::make_shared<NetworkState>();
1055b8fca66Sopenharmony_ci    if (networkState == nullptr) {
1065b8fca66Sopenharmony_ci        return;
1075b8fca66Sopenharmony_ci    }
1085b8fca66Sopenharmony_ci    networkState->isEmergency_ = static_cast<int32_t>(size % BOOL_NUM);
1095b8fca66Sopenharmony_ci    std::string mOperatorNumeric(reinterpret_cast<const char *>(data), size);
1105b8fca66Sopenharmony_ci    std::string mFullName(reinterpret_cast<const char *>(data), size);
1115b8fca66Sopenharmony_ci    std::string mShortName(reinterpret_cast<const char *>(data), size);
1125b8fca66Sopenharmony_ci    networkState->psOperatorInfo_.operatorNumeric = mOperatorNumeric;
1135b8fca66Sopenharmony_ci    networkState->psOperatorInfo_.fullName = mFullName;
1145b8fca66Sopenharmony_ci    networkState->psOperatorInfo_.shortName = mShortName;
1155b8fca66Sopenharmony_ci    networkState->csOperatorInfo_.operatorNumeric = mOperatorNumeric;
1165b8fca66Sopenharmony_ci    networkState->csOperatorInfo_.fullName = mFullName;
1175b8fca66Sopenharmony_ci    networkState->csOperatorInfo_.shortName = mShortName;
1185b8fca66Sopenharmony_ci    networkState->csRoaming_ = static_cast<RoamingType>(size % ROAMING_NUM);
1195b8fca66Sopenharmony_ci    networkState->psRoaming_ = static_cast<RoamingType>(size % ROAMING_NUM);
1205b8fca66Sopenharmony_ci    networkState->psRegStatus_ = static_cast<RegServiceState>(size % REG_NUM);
1215b8fca66Sopenharmony_ci    networkState->csRegStatus_ = static_cast<RegServiceState>(size % REG_NUM);
1225b8fca66Sopenharmony_ci    networkState->psRadioTech_ = static_cast<RadioTech>(size % RADIO_NUM);
1235b8fca66Sopenharmony_ci    networkState->lastPsRadioTech_ = static_cast<RadioTech>(size % RADIO_NUM);
1245b8fca66Sopenharmony_ci    networkState->lastCfgTech_ = static_cast<RadioTech>(size % RADIO_NUM);
1255b8fca66Sopenharmony_ci    networkState->csRadioTech_ = static_cast<RadioTech>(size % RADIO_NUM);
1265b8fca66Sopenharmony_ci    networkState->cfgTech_ = static_cast<RadioTech>(size % RADIO_NUM);
1275b8fca66Sopenharmony_ci    networkState->nrState_ = static_cast<NrState>(size % NR_NUM);
1285b8fca66Sopenharmony_ci    networkState->Marshalling(dataMessageParcel);
1295b8fca66Sopenharmony_ci    dataMessageParcel.RewindRead(0);
1305b8fca66Sopenharmony_ci    MessageParcel reply;
1315b8fca66Sopenharmony_ci    telephonyObserver.OnNetworkStateUpdatedInner(dataMessageParcel, reply);
1325b8fca66Sopenharmony_ci}
1335b8fca66Sopenharmony_ci
1345b8fca66Sopenharmony_civoid CellInfoUpdatedInner(const uint8_t *data, size_t size)
1355b8fca66Sopenharmony_ci{
1365b8fca66Sopenharmony_ci    if (!IsServiceInited()) {
1375b8fca66Sopenharmony_ci        return;
1385b8fca66Sopenharmony_ci    }
1395b8fca66Sopenharmony_ci    int32_t slotId = static_cast<int32_t>(size % SLOT_NUM);
1405b8fca66Sopenharmony_ci    MessageParcel dataMessageParcel;
1415b8fca66Sopenharmony_ci    dataMessageParcel.WriteInt32(slotId);
1425b8fca66Sopenharmony_ci    dataMessageParcel.RewindRead(0);
1435b8fca66Sopenharmony_ci    MessageParcel reply;
1445b8fca66Sopenharmony_ci    telephonyObserver.OnCellInfoUpdatedInner(dataMessageParcel, reply);
1455b8fca66Sopenharmony_ci}
1465b8fca66Sopenharmony_ci
1475b8fca66Sopenharmony_civoid DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
1485b8fca66Sopenharmony_ci{
1495b8fca66Sopenharmony_ci    if (data == nullptr || size == 0) {
1505b8fca66Sopenharmony_ci        return;
1515b8fca66Sopenharmony_ci    }
1525b8fca66Sopenharmony_ci    OnRemoteRequest(data, size);
1535b8fca66Sopenharmony_ci    CallStateUpdatedInner(data, size);
1545b8fca66Sopenharmony_ci    SignalInfoUpdatedInner(data, size);
1555b8fca66Sopenharmony_ci    NetworkStateUpdatedInner(data, size);
1565b8fca66Sopenharmony_ci    CellInfoUpdatedInner(data, size);
1575b8fca66Sopenharmony_ci    return;
1585b8fca66Sopenharmony_ci}
1595b8fca66Sopenharmony_ci} // namespace OHOS
1605b8fca66Sopenharmony_ci
1615b8fca66Sopenharmony_ci/* Fuzzer entry point */
1625b8fca66Sopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
1635b8fca66Sopenharmony_ci{
1645b8fca66Sopenharmony_ci    OHOS::AddStateRegistryTokenFuzzer token;
1655b8fca66Sopenharmony_ci    /* Run your code on data */
1665b8fca66Sopenharmony_ci    OHOS::DoSomethingInterestingWithMyAPI(data, size);
1675b8fca66Sopenharmony_ci    return 0;
1685b8fca66Sopenharmony_ci}
169