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 "updatedisconnectmachine_fuzzer.h"
17
18 #define private public
19 #define protected public
20
21 #include "adddatatoken_fuzzer.h"
22 #include "default.h"
23 #include "disconnecting.h"
24 #include "statemachine_fuzzer.h"
25 #include <fuzzer/FuzzedDataProvider.h>
26
27 namespace OHOS {
28 using namespace OHOS::Telephony;
29 using namespace AppExecFwk;
30 using namespace OHOS::EventFwk;
31 constexpr int32_t SLOT_NUM_MAX = 3;
32 constexpr int32_t EVENT_ID_MAX = 255;
33 bool g_flag = false;
34
UpdateDisconnectMachineFuzz(const uint8_t * data,size_t size)35 void UpdateDisconnectMachineFuzz(const uint8_t *data, size_t size)
36 {
37 std::shared_ptr<StateMachineFuzzer> machine = std::make_shared<StateMachineFuzzer>();
38 if (machine == nullptr) {
39 return;
40 }
41 FuzzedDataProvider fdp(data, size);
42 int32_t slotId = fdp.ConsumeIntegralInRange<uint32_t>(0, SLOT_NUM_MAX);
43 std::shared_ptr<CellularDataStateMachine> cellularMachine = machine->CreateCellularDataConnect(slotId);
44 if (cellularMachine == nullptr) {
45 return;
46 }
47 sptr<Disconnecting> disconnecting =
48 std::make_unique<Disconnecting>(std::weak_ptr<CellularDataStateMachine>(cellularMachine), "Disconnecting")
49 .release();
50 sptr<Default> defaultStatus =
51 std::make_unique<Default>(std::weak_ptr<CellularDataStateMachine>(cellularMachine), "Default").release();
52
53 if (disconnecting == nullptr || defaultStatus == nullptr) {
54 TELEPHONY_LOGE("memory allocation failed");
55 return;
56 }
57 cellularMachine->Init();
58
59 std::int32_t intValue = fdp.ConsumeIntegralInRange<uint32_t>(0, EVENT_ID_MAX);
60 std::unique_ptr<uint8_t> object = std::make_unique<uint8_t>(*data);
61 if (object == nullptr) {
62 return;
63 }
64
65 InnerEvent::Pointer event = InnerEvent::Get(intValue, object);
66 disconnecting->StateProcess(event);
67 disconnecting->ProcessDisconnectTimeout(event);
68
69 defaultStatus->StateProcess(event);
70 defaultStatus->ProcessConnectDone(event);
71 defaultStatus->ProcessDisconnectAllDone(event);
72 defaultStatus->ProcessDataConnectionDrsOrRatChanged(event);
73 defaultStatus->ProcessDataConnectionRoamOn(event);
74 defaultStatus->ProcessDataConnectionRoamOff(event);
75 }
76
UpdateActiveMachineWithMyAPI(const uint8_t * data,size_t size)77 void UpdateActiveMachineWithMyAPI(const uint8_t *data, size_t size)
78 {
79 if (data == nullptr || size == 0) {
80 return;
81 }
82 if (!g_flag) {
83 UpdateDisconnectMachineFuzz(data, size);
84 g_flag = true;
85 }
86 }
87 } // namespace OHOS
88
89 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)90 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
91 {
92 OHOS::AddDataTokenFuzzer token;
93 /* Run your code on data */
94 OHOS::UpdateActiveMachineWithMyAPI(data, size);
95 return 0;
96 }