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 "getcellulardatastate_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #define private public
21 #include "adddatatoken_fuzzer.h"
22 #include "cellular_data_service.h"
23 #include "cellular_data_service_stub.h"
24 #include "system_ability_definition.h"
25
26 using namespace OHOS::Telephony;
27 namespace OHOS {
28 static bool g_isInited = false;
29 constexpr int32_t SLOT_NUM = 2;
30
IsServiceInited()31 bool IsServiceInited()
32 {
33 if (!g_isInited) {
34 DelayedSingleton<CellularDataService>::GetInstance()->OnStart();
35 if (DelayedSingleton<CellularDataService>::GetInstance()->GetServiceRunningState() ==
36 static_cast<int32_t>(ServiceRunningState::STATE_RUNNING)) {
37 g_isInited = true;
38 }
39 }
40 return g_isInited;
41 }
42
OnRemoteRequest(const uint8_t * data,size_t size)43 void OnRemoteRequest(const uint8_t *data, size_t size)
44 {
45 if (!IsServiceInited()) {
46 return;
47 }
48
49 MessageParcel dataMessageParcel;
50 if (!dataMessageParcel.WriteInterfaceToken(CellularDataServiceStub::GetDescriptor())) {
51 return;
52 }
53 dataMessageParcel.WriteBuffer(data, size);
54 dataMessageParcel.RewindRead(0);
55 uint32_t code = static_cast<uint32_t>(size);
56 MessageParcel reply;
57 MessageOption option;
58 DelayedSingleton<CellularDataService>::GetInstance()->OnRemoteRequest(code, dataMessageParcel, reply, option);
59 }
60
EnableCellularData(const uint8_t * data,size_t size)61 void EnableCellularData(const uint8_t *data, size_t size)
62 {
63 if (!IsServiceInited()) {
64 return;
65 }
66
67 MessageParcel dataMessageParcel;
68 dataMessageParcel.WriteBuffer(data, size);
69 dataMessageParcel.RewindRead(0);
70 MessageParcel reply;
71 DelayedSingleton<CellularDataService>::GetInstance()->OnEnableCellularData(dataMessageParcel, reply);
72 }
73
GetCellularDataState(const uint8_t * data,size_t size)74 void GetCellularDataState(const uint8_t *data, size_t size)
75 {
76 if (!IsServiceInited()) {
77 return;
78 }
79
80 MessageParcel dataMessageParcel;
81 dataMessageParcel.WriteBuffer(data, size);
82 dataMessageParcel.RewindRead(0);
83 MessageParcel reply;
84 DelayedSingleton<CellularDataService>::GetInstance()->OnGetCellularDataState(dataMessageParcel, reply);
85 }
86
IsCellularDataEnabled(const uint8_t * data,size_t size)87 void IsCellularDataEnabled(const uint8_t *data, size_t size)
88 {
89 if (!IsServiceInited()) {
90 return;
91 }
92
93 MessageParcel dataMessageParcel;
94 dataMessageParcel.WriteBuffer(data, size);
95 dataMessageParcel.RewindRead(0);
96 MessageParcel reply;
97 DelayedSingleton<CellularDataService>::GetInstance()->OnIsCellularDataEnabled(dataMessageParcel, reply);
98 }
99
IsCellularDataRoamingEnabled(const uint8_t * data,size_t size)100 void IsCellularDataRoamingEnabled(const uint8_t *data, size_t size)
101 {
102 if (!IsServiceInited()) {
103 return;
104 }
105
106 int32_t slotId = static_cast<int32_t>(size % SLOT_NUM);
107 MessageParcel dataMessageParcel;
108 dataMessageParcel.WriteInt32(slotId);
109 dataMessageParcel.WriteBuffer(data, size);
110 dataMessageParcel.RewindRead(0);
111 MessageParcel reply;
112 DelayedSingleton<CellularDataService>::GetInstance()->OnIsCellularDataRoamingEnabled(dataMessageParcel, reply);
113 }
114
GetDefaultCellularDataSlotId(const uint8_t * data,size_t size)115 void GetDefaultCellularDataSlotId(const uint8_t *data, size_t size)
116 {
117 if (!IsServiceInited()) {
118 return;
119 }
120
121 MessageParcel dataMessageParcel;
122 dataMessageParcel.WriteBuffer(data, size);
123 dataMessageParcel.RewindRead(0);
124 MessageParcel reply;
125 DelayedSingleton<CellularDataService>::GetInstance()->OnGetDefaultCellularDataSlotId(dataMessageParcel, reply);
126 }
127
EnableCellularDataRoaming(const uint8_t * data,size_t size)128 void EnableCellularDataRoaming(const uint8_t *data, size_t size)
129 {
130 if (!IsServiceInited()) {
131 return;
132 }
133
134 int32_t slotId = static_cast<int32_t>(size % SLOT_NUM);
135 MessageParcel dataMessageParcel;
136 dataMessageParcel.WriteInt32(slotId);
137 dataMessageParcel.WriteBuffer(data, size);
138 dataMessageParcel.RewindRead(0);
139 MessageParcel reply;
140 DelayedSingleton<CellularDataService>::GetInstance()->OnEnableCellularDataRoaming(dataMessageParcel, reply);
141 }
142
SetDefaultCellularDataSlotId(const uint8_t * data,size_t size)143 void SetDefaultCellularDataSlotId(const uint8_t *data, size_t size)
144 {
145 if (!IsServiceInited()) {
146 return;
147 }
148
149 int32_t slotId = static_cast<int32_t>(size % SLOT_NUM);
150 MessageParcel dataMessageParcel;
151 dataMessageParcel.WriteInt32(slotId);
152 dataMessageParcel.WriteBuffer(data, size);
153 dataMessageParcel.RewindRead(0);
154 MessageParcel reply;
155 DelayedSingleton<CellularDataService>::GetInstance()->OnSetDefaultCellularDataSlotId(dataMessageParcel, reply);
156 }
157
HasInternetCapability(const uint8_t * data,size_t size)158 void HasInternetCapability(const uint8_t *data, size_t size)
159 {
160 if (!IsServiceInited()) {
161 return;
162 }
163
164 int32_t slotId = static_cast<int32_t>(size % SLOT_NUM);
165 MessageParcel dataMessageParcel;
166 dataMessageParcel.WriteInt32(slotId);
167 dataMessageParcel.WriteBuffer(data, size);
168 dataMessageParcel.RewindRead(0);
169 MessageParcel reply;
170 DelayedSingleton<CellularDataService>::GetInstance()->OnHasInternetCapability(dataMessageParcel, reply);
171 }
172
ClearCellularDataConnections(const uint8_t * data,size_t size)173 void ClearCellularDataConnections(const uint8_t *data, size_t size)
174 {
175 if (!IsServiceInited()) {
176 return;
177 }
178
179 int32_t slotId = static_cast<int32_t>(size % SLOT_NUM);
180 MessageParcel dataMessageParcel;
181 dataMessageParcel.WriteInt32(slotId);
182 dataMessageParcel.WriteBuffer(data, size);
183 dataMessageParcel.RewindRead(0);
184 MessageParcel reply;
185 DelayedSingleton<CellularDataService>::GetInstance()->OnClearCellularDataConnections(dataMessageParcel, reply);
186 }
187
GetCellularDataFlowType(const uint8_t * data,size_t size)188 void GetCellularDataFlowType(const uint8_t *data, size_t size)
189 {
190 if (!IsServiceInited()) {
191 return;
192 }
193
194 MessageParcel dataMessageParcel;
195 dataMessageParcel.WriteBuffer(data, size);
196 dataMessageParcel.RewindRead(0);
197 MessageParcel reply;
198 DelayedSingleton<CellularDataService>::GetInstance()->OnGetCellularDataFlowType(dataMessageParcel, reply);
199 }
200
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)201 void DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
202 {
203 if (data == nullptr || size == 0) {
204 return;
205 }
206
207 OnRemoteRequest(data, size);
208 EnableCellularData(data, size);
209 GetCellularDataState(data, size);
210 IsCellularDataEnabled(data, size);
211 IsCellularDataRoamingEnabled(data, size);
212 GetDefaultCellularDataSlotId(data, size);
213 GetCellularDataFlowType(data, size);
214 EnableCellularDataRoaming(data, size);
215 SetDefaultCellularDataSlotId(data, size);
216 HasInternetCapability(data, size);
217 ClearCellularDataConnections(data, size);
218 }
219 } // namespace OHOS
220
221 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)222 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
223 {
224 OHOS::AddDataTokenFuzzer token;
225 /* Run your code on data */
226 OHOS::DoSomethingInterestingWithMyAPI(data, size);
227 return 0;
228 }
229