1d95e75fdSopenharmony_ci/*
2d95e75fdSopenharmony_ci * Copyright (C) 2023 Huawei Device Co., Ltd.
3d95e75fdSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4d95e75fdSopenharmony_ci * you may not use this file except in compliance with the License.
5d95e75fdSopenharmony_ci * You may obtain a copy of the License at
6d95e75fdSopenharmony_ci *
7d95e75fdSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8d95e75fdSopenharmony_ci *
9d95e75fdSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10d95e75fdSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11d95e75fdSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12d95e75fdSopenharmony_ci * See the License for the specific language governing permissions and
13d95e75fdSopenharmony_ci * limitations under the License.
14d95e75fdSopenharmony_ci */
15d95e75fdSopenharmony_ci
16d95e75fdSopenharmony_ci#include "satelliteclient_fuzzer.h"
17d95e75fdSopenharmony_ci
18d95e75fdSopenharmony_ci#include <cstddef>
19d95e75fdSopenharmony_ci#include <cstdint>
20d95e75fdSopenharmony_ci#define private public
21d95e75fdSopenharmony_ci#include "addcellularcalltoken_fuzzer.h"
22d95e75fdSopenharmony_ci#include "cellular_call_service.h"
23d95e75fdSopenharmony_ci#include "satellite_call_client.h"
24d95e75fdSopenharmony_ci#include "satellite_call_proxy.h"
25d95e75fdSopenharmony_ci#include "securec.h"
26d95e75fdSopenharmony_ci#include "system_ability_definition.h"
27d95e75fdSopenharmony_ci
28d95e75fdSopenharmony_ciusing namespace OHOS::Telephony;
29d95e75fdSopenharmony_cinamespace OHOS {
30d95e75fdSopenharmony_cistatic bool g_isInited = false;
31d95e75fdSopenharmony_ciconstexpr int32_t SERVICE_STATE_RUNNING = 1;
32d95e75fdSopenharmony_ciconst uint32_t TELEPHONY_SATELLITE_SYS_ABILITY_ID = 4012;
33d95e75fdSopenharmony_cienum {
34d95e75fdSopenharmony_ci    NUM_ONE = 1,
35d95e75fdSopenharmony_ci    NUM_TWO,
36d95e75fdSopenharmony_ci    NUM_THREE,
37d95e75fdSopenharmony_ci    NUM_FOUR,
38d95e75fdSopenharmony_ci    NUM_FIVE,
39d95e75fdSopenharmony_ci    NUM_SIX,
40d95e75fdSopenharmony_ci    NUM_SEVEN,
41d95e75fdSopenharmony_ci    NUM_EIGHT,
42d95e75fdSopenharmony_ci};
43d95e75fdSopenharmony_ci
44d95e75fdSopenharmony_cibool IsServiceInited()
45d95e75fdSopenharmony_ci{
46d95e75fdSopenharmony_ci    auto service = DelayedSingleton<CellularCallService>::GetInstance();
47d95e75fdSopenharmony_ci    if (service == nullptr) {
48d95e75fdSopenharmony_ci        return g_isInited;
49d95e75fdSopenharmony_ci    }
50d95e75fdSopenharmony_ci    if (service->GetServiceRunningState() != SERVICE_STATE_RUNNING) {
51d95e75fdSopenharmony_ci        service->OnStart();
52d95e75fdSopenharmony_ci    }
53d95e75fdSopenharmony_ci    if (!g_isInited && service->GetServiceRunningState() == SERVICE_STATE_RUNNING) {
54d95e75fdSopenharmony_ci        g_isInited = true;
55d95e75fdSopenharmony_ci    }
56d95e75fdSopenharmony_ci    return g_isInited;
57d95e75fdSopenharmony_ci}
58d95e75fdSopenharmony_ci
59d95e75fdSopenharmony_civoid TestSatelliteCallClientWithCallInfo(
60d95e75fdSopenharmony_ci    const uint8_t *data, size_t size, const std::shared_ptr<SatelliteCallClient> &satelliteCallClient)
61d95e75fdSopenharmony_ci{
62d95e75fdSopenharmony_ci    SatelliteCallInfo callInfo;
63d95e75fdSopenharmony_ci    if (memset_s(&callInfo, sizeof(struct SatelliteCallInfo), 0x00, sizeof(struct SatelliteCallInfo)) != EOK) {
64d95e75fdSopenharmony_ci        return;
65d95e75fdSopenharmony_ci    }
66d95e75fdSopenharmony_ci    size_t length = std::min(static_cast<size_t>(sizeof(callInfo.phoneNum) - 1), size);
67d95e75fdSopenharmony_ci    std::string number(reinterpret_cast<const char *>(data), length);
68d95e75fdSopenharmony_ci    int32_t mode = static_cast<int32_t>(size % NUM_THREE);
69d95e75fdSopenharmony_ci    int32_t slotId = static_cast<int32_t>(size % NUM_TWO);
70d95e75fdSopenharmony_ci    int32_t index = static_cast<int32_t>(size % NUM_THREE);
71d95e75fdSopenharmony_ci    if (strcpy_s(callInfo.phoneNum, sizeof(callInfo.phoneNum), number.c_str()) != EOK) {
72d95e75fdSopenharmony_ci        return;
73d95e75fdSopenharmony_ci    }
74d95e75fdSopenharmony_ci    callInfo.slotId = slotId;
75d95e75fdSopenharmony_ci    callInfo.index = index;
76d95e75fdSopenharmony_ci    satelliteCallClient->Dial(callInfo, static_cast<CLIRMode>(mode));
77d95e75fdSopenharmony_ci    satelliteCallClient->HangUp(slotId, index);
78d95e75fdSopenharmony_ci    satelliteCallClient->Answer(slotId);
79d95e75fdSopenharmony_ci    satelliteCallClient->Reject(slotId);
80d95e75fdSopenharmony_ci
81d95e75fdSopenharmony_ci    int32_t callType = static_cast<int32_t>(size % NUM_TWO);
82d95e75fdSopenharmony_ci    satelliteCallClient->GetSatelliteCallsDataRequest(slotId, callType);
83d95e75fdSopenharmony_ci}
84d95e75fdSopenharmony_ci
85d95e75fdSopenharmony_civoid TestSatelliteCallProxyWithCallInfo(const uint8_t *data, size_t size, const sptr<SatelliteCallInterface> &proxy)
86d95e75fdSopenharmony_ci{
87d95e75fdSopenharmony_ci    SatelliteCallInfo callInfo;
88d95e75fdSopenharmony_ci    if (memset_s(&callInfo, sizeof(struct SatelliteCallInfo), 0x00, sizeof(struct SatelliteCallInfo)) != EOK) {
89d95e75fdSopenharmony_ci        return;
90d95e75fdSopenharmony_ci    }
91d95e75fdSopenharmony_ci    size_t length = std::min(static_cast<size_t>(sizeof(callInfo.phoneNum) - 1), size);
92d95e75fdSopenharmony_ci    std::string number(reinterpret_cast<const char *>(data), length);
93d95e75fdSopenharmony_ci    int32_t mode = static_cast<int32_t>(size % NUM_THREE);
94d95e75fdSopenharmony_ci    int32_t slotId = static_cast<int32_t>(size % NUM_TWO);
95d95e75fdSopenharmony_ci    int32_t index = static_cast<int32_t>(size % NUM_THREE);
96d95e75fdSopenharmony_ci    if (strcpy_s(callInfo.phoneNum, sizeof(callInfo.phoneNum), number.c_str()) != EOK) {
97d95e75fdSopenharmony_ci        return;
98d95e75fdSopenharmony_ci    }
99d95e75fdSopenharmony_ci    callInfo.slotId = slotId;
100d95e75fdSopenharmony_ci    callInfo.index = index;
101d95e75fdSopenharmony_ci
102d95e75fdSopenharmony_ci    proxy->Dial(callInfo, static_cast<CLIRMode>(mode));
103d95e75fdSopenharmony_ci    proxy->HangUp(slotId, index);
104d95e75fdSopenharmony_ci    proxy->Answer(slotId);
105d95e75fdSopenharmony_ci    proxy->Reject(slotId);
106d95e75fdSopenharmony_ci
107d95e75fdSopenharmony_ci    proxy->GetSatelliteCallsDataRequest(slotId);
108d95e75fdSopenharmony_ci}
109d95e75fdSopenharmony_ci
110d95e75fdSopenharmony_civoid DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
111d95e75fdSopenharmony_ci{
112d95e75fdSopenharmony_ci    if (data == nullptr || size == 0) {
113d95e75fdSopenharmony_ci        return;
114d95e75fdSopenharmony_ci    }
115d95e75fdSopenharmony_ci    auto satelliteCallClient = DelayedSingleton<SatelliteCallClient>::GetInstance();
116d95e75fdSopenharmony_ci    if (satelliteCallClient == nullptr) {
117d95e75fdSopenharmony_ci        return;
118d95e75fdSopenharmony_ci    }
119d95e75fdSopenharmony_ci    if (!IsServiceInited()) {
120d95e75fdSopenharmony_ci        return;
121d95e75fdSopenharmony_ci    }
122d95e75fdSopenharmony_ci    TestSatelliteCallClientWithCallInfo(data, size, satelliteCallClient);
123d95e75fdSopenharmony_ci
124d95e75fdSopenharmony_ci    auto managerPtr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
125d95e75fdSopenharmony_ci    if (managerPtr == nullptr) {
126d95e75fdSopenharmony_ci        return;
127d95e75fdSopenharmony_ci    }
128d95e75fdSopenharmony_ci    auto remoteObjectPtr = managerPtr->CheckSystemAbility(TELEPHONY_SATELLITE_SYS_ABILITY_ID);
129d95e75fdSopenharmony_ci    if (remoteObjectPtr == nullptr) {
130d95e75fdSopenharmony_ci        return;
131d95e75fdSopenharmony_ci    }
132d95e75fdSopenharmony_ci    sptr<SatelliteCallInterface> proxy = iface_cast<SatelliteCallInterface>(remoteObjectPtr);
133d95e75fdSopenharmony_ci    if (proxy == nullptr) {
134d95e75fdSopenharmony_ci        return;
135d95e75fdSopenharmony_ci    }
136d95e75fdSopenharmony_ci    TestSatelliteCallProxyWithCallInfo(data, size, proxy);
137d95e75fdSopenharmony_ci    proxy.clear();
138d95e75fdSopenharmony_ci    proxy = nullptr;
139d95e75fdSopenharmony_ci}
140d95e75fdSopenharmony_ci} // namespace OHOS
141d95e75fdSopenharmony_ci
142d95e75fdSopenharmony_ci/* Fuzzer entry point */
143d95e75fdSopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
144d95e75fdSopenharmony_ci{
145d95e75fdSopenharmony_ci    OHOS::AddCellularCallTokenFuzzer token;
146d95e75fdSopenharmony_ci    /* Run your code on data */
147d95e75fdSopenharmony_ci    OHOS::DoSomethingInterestingWithMyAPI(data, size);
148d95e75fdSopenharmony_ci    return 0;
149d95e75fdSopenharmony_ci}
150