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