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
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
UpdateInActiveMachineFuzz(const uint8_t * data,size_t size)33 void 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->SetReason(DisConnectionReason::REASON_NORMAL);
65 inactive->StateProcess(event);
66
67 activating->StateProcess(event);
68 activating->RilErrorResponse(event);
69 activating->ProcessConnectTimeout(event);
70 activating->StateProcess(event);
71
72 if (machine->stateMachineEventLoop_ != nullptr) {
73 machine->stateMachineEventLoop_->Stop();
74 }
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 UpdateInActiveMachineFuzz(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 }