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
26 namespace OHOS {
27 using namespace OHOS::Telephony;
28 using namespace AppExecFwk;
29 using namespace OHOS::EventFwk;
30 static int32_t SIM_COUNT = 2;
31 bool g_flag = false;
32
UpdateDisconnectMachineFuzz(const uint8_t * data,size_t size)33 void UpdateDisconnectMachineFuzz(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 sptr<Disconnecting> disconnecting =
45 std::make_unique<Disconnecting>(std::weak_ptr<CellularDataStateMachine>(cellularMachine), "Disconnecting")
46 .release();
47 sptr<Default> defaultStatus =
48 std::make_unique<Default>(std::weak_ptr<CellularDataStateMachine>(cellularMachine), "Default").release();
49
50 if (disconnecting == nullptr || defaultStatus == nullptr) {
51 TELEPHONY_LOGE("memory allocation failed");
52 return;
53 }
54 cellularMachine->Init();
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 disconnecting->StateProcess(event);
64 disconnecting->ProcessDisconnectTimeout(event);
65
66 defaultStatus->StateProcess(event);
67 defaultStatus->ProcessConnectDone(event);
68 defaultStatus->ProcessDisconnectAllDone(event);
69 defaultStatus->ProcessDataConnectionDrsOrRatChanged(event);
70 defaultStatus->ProcessDataConnectionRoamOn(event);
71 defaultStatus->ProcessDataConnectionRoamOff(event);
72
73 if (machine->stateMachineEventLoop_ != nullptr) {
74 machine->stateMachineEventLoop_->Stop();
75 }
76 }
77
UpdateActiveMachineWithMyAPI(const uint8_t * data,size_t size)78 void UpdateActiveMachineWithMyAPI(const uint8_t *data, size_t size)
79 {
80 if (data == nullptr || size == 0) {
81 return;
82 }
83 if (!g_flag) {
84 UpdateDisconnectMachineFuzz(data, size);
85 g_flag = true;
86 }
87 }
88 } // namespace OHOS
89
90 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)91 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
92 {
93 OHOS::AddDataTokenFuzzer token;
94 /* Run your code on data */
95 OHOS::UpdateActiveMachineWithMyAPI(data, size);
96 return 0;
97 }