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