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 #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
UpdateInActiveMachineFuzz(const uint8_t * data,size_t size)35 void UpdateInActiveMachineFuzz(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 cellularMachine->Init();
48
49 sptr<Inactive> inactive =
50 std::make_unique<Inactive>(std::weak_ptr<CellularDataStateMachine>(cellularMachine), "Inactive").release();
51 sptr<Activating> activating =
52 std::make_unique<Activating>(std::weak_ptr<CellularDataStateMachine>(cellularMachine), "Activating").release();
53
54 if (inactive == nullptr || activating == nullptr) {
55 TELEPHONY_LOGE("memory allocation failed");
56 return;
57 }
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 inactive->SetDeActiveApnTypeId(intValue);
67 inactive->StateProcess(event);
68
69 activating->StateProcess(event);
70 activating->RilErrorResponse(event);
71 activating->ProcessConnectTimeout(event);
72 activating->StateProcess(event);
73 }
74
UpdateActiveMachineWithMyAPI(const uint8_t * data,size_t size)75 void UpdateActiveMachineWithMyAPI(const uint8_t *data, size_t size)
76 {
77 if (data == nullptr || size == 0) {
78 return;
79 }
80 if (!g_flag) {
81 UpdateInActiveMachineFuzz(data, size);
82 g_flag = true;
83 }
84 }
85 } // namespace OHOS
86
87 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)88 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
89 {
90 OHOS::AddDataTokenFuzzer token;
91 /* Run your code on data */
92 OHOS::UpdateActiveMachineWithMyAPI(data, size);
93 return 0;
94 }