1/*
2 * Copyright (c) 2022 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#include "updateactivemachine_fuzzer.h"
17
18#define private public
19#define protected public
20
21#include "active.h"
22#include "adddatatoken_fuzzer.h"
23#include "statemachine_fuzzer.h"
24
25namespace OHOS {
26using namespace OHOS::Telephony;
27using namespace AppExecFwk;
28using namespace OHOS::EventFwk;
29static int32_t SIM_COUNT = 2;
30bool g_flag = false;
31
32void UpdateActiveMachineFuzz(const uint8_t *data, size_t size)
33{
34    std::shared_ptr<StateMachineFuzzer> machine = std::make_shared<StateMachineFuzzer>();
35    if (machine == nullptr) {
36        return;
37    }
38    int32_t slotId = static_cast<int32_t>(size % SIM_COUNT);
39    std::shared_ptr<CellularDataStateMachine> cellularMachine = machine->CreateCellularDataConnect(slotId);
40    if (cellularMachine == nullptr) {
41        return;
42    }
43    sptr<Active> active =
44        std::make_unique<Active>(std::weak_ptr<CellularDataStateMachine>(cellularMachine), "Active").release();
45    if (active == nullptr) {
46        return;
47    }
48    cellularMachine->Init();
49
50    std::int32_t intValue = static_cast<int32_t>(size);
51    std::unique_ptr<uint8_t> object = std::make_unique<uint8_t>(*data);
52    if (object == nullptr) {
53        return;
54    }
55
56    InnerEvent::Pointer event = InnerEvent::Get(intValue, object);
57    active->ProcessConnectDone(event);
58    active->ProcessDisconnectDone(event);
59    active->ProcessLostConnection(event);
60    active->ProcessLinkCapabilityChanged(event);
61    active->ProcessDataConnectionRoamOn(event);
62    active->ProcessDataConnectionRoamOff(event);
63    active->ProcessDataConnectionVoiceCallStartedOrEnded(event);
64    active->ProcessDataConnectionComplete(event);
65    active->RefreshTcpBufferSizes();
66    active->RefreshConnectionBandwidths();
67}
68
69void UpdateActiveMachineWithMyAPI(const uint8_t *data, size_t size)
70{
71    if (data == nullptr || size == 0) {
72        return;
73    }
74
75    if (!g_flag) {
76        UpdateActiveMachineFuzz(data, size);
77        g_flag = true;
78    }
79}
80} // namespace OHOS
81
82/* Fuzzer entry point */
83extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
84{
85    OHOS::AddDataTokenFuzzer token;
86    /* Run your code on data */
87    OHOS::UpdateActiveMachineWithMyAPI(data, size);
88    return 0;
89}