• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <cstddef>
16 #include <cstdint>
17 #include "securec.h"
18 #include "wificdevice_fuzzer.h"
19 #include "wifi_fuzz_common_func.h"
20 #include "kits/c/wifi_device.h"
21 
22 
EnableWifiTest()23 static void EnableWifiTest()
24 {
25     EnableWifi();
26 }
EnableSemiWifiTest()27 static void EnableSemiWifiTest()
28 {
29     EnableSemiWifi();
30 }
DisableWifiTest()31 static void DisableWifiTest()
32 {
33     DisableWifi();
34 }
ScanTest()35 static void ScanTest()
36 {
37     Scan();
38 }
DisconnectTest()39 static void DisconnectTest()
40 {
41     Disconnect();
42 }
RemoveDeviceTest(const uint8_t * data,size_t size)43 static void RemoveDeviceTest(const uint8_t* data, size_t size)
44 {
45     if (size == 0) {
46         return;
47     }
48     int networkId = static_cast<int>(data[0]);
49     RemoveDevice(networkId);
50 }
DisableDeviceConfigTest(const uint8_t * data,size_t size)51 static void DisableDeviceConfigTest(const uint8_t* data, size_t size)
52 {
53     if (size == 0) {
54         return;
55     }
56     int networkId = static_cast<int>(data[0]);
57     DisableDeviceConfig(networkId);
58 }
EnableDeviceConfigTest(const uint8_t * data,size_t size)59 static void EnableDeviceConfigTest(const uint8_t* data, size_t size)
60 {
61     if (size == 0) {
62         return;
63     }
64     int networkId = static_cast<int>(data[0]);
65     EnableDeviceConfig(networkId);
66 }
ConnectToTest(const uint8_t * data,size_t size)67 static void ConnectToTest(const uint8_t* data, size_t size)
68 {
69     if (size == 0) {
70         return;
71     }
72     int networkId = static_cast<int>(data[0]);
73     ConnectTo(networkId);
74 }
AddDeviceConfigTest(const uint8_t * data,size_t size)75 static void AddDeviceConfigTest(const uint8_t* data, size_t size)
76 {
77     int index = 0;
78     WifiDeviceConfig config;
79     if (size >= sizeof(WifiDeviceConfig)) {
80         if (memcpy_s(config.ssid, WIFI_MAX_SSID_LEN, data, WIFI_MAX_SSID_LEN - 1) != EOK) {
81             return;
82         }
83         if (memcpy_s(config.bssid, WIFI_MAC_LEN, data, WIFI_MAC_LEN - 1) != EOK) {
84             return;
85         }
86         if (memcpy_s(config.preSharedKey, WIFI_MAX_KEY_LEN, data, WIFI_MAX_KEY_LEN - 1) != EOK) {
87             return;
88         }
89         config.securityType = static_cast<int>(data[index++]);
90         config.netId = static_cast<int>(data[index++]);
91         config.freq = static_cast<int>(data[index++]);
92         config.wapiPskType = static_cast<int>(data[index++]);
93         config.isHiddenSsid = static_cast<int>(data[index++]);
94         config.ipType = static_cast<IpType>(static_cast<int>(data[index++]) % TWO);
95     }
96     int result = static_cast<int>(data[index++]);
97     AddDeviceConfig(&config, &result);
98 }
AdvanceScanTest(const uint8_t * data,size_t size)99 static void AdvanceScanTest(const uint8_t* data, size_t size)
100 {
101     WifiScanParams params;
102     if (size >= sizeof(WifiScanParams)) {
103         if (memcpy_s(params.ssid, WIFI_MAX_SSID_LEN, data, WIFI_MAX_SSID_LEN - 1) != EOK) {
104             return;
105         }
106         if (memcpy_s(params.bssid, WIFI_MAC_LEN, data, WIFI_MAC_LEN - 1) != EOK) {
107             return;
108         }
109         int index = 0;
110         params.scanType = WIFI_FREQ_SCAN;
111         params.freqs = static_cast<int>(data[index++]);
112         params.band = static_cast<int>(data[index++]);
113         params.ssidLen = static_cast<int>(data[index++]);
114     }
115     AdvanceScan(&params);
116 }
GetSignalLevelTest(const uint8_t * data,size_t size)117 static void GetSignalLevelTest(const uint8_t* data, size_t size)
118 {
119     int rssi = 0;
120     int band = 0;
121     if (size >= TWO) {
122         int index = 0;
123         rssi = static_cast<int>(data[index++]);
124         band = static_cast<int>(data[index++]);
125     }
126     GetSignalLevel(rssi, band);
127 }
128 
GetScanInfoListTest(const uint8_t * data,size_t size)129 static void GetScanInfoListTest(const uint8_t* data, size_t size)
130 {
131     WifiScanInfo result;
132     unsigned int mSize;
133     if (size >= sizeof(WifiScanInfo)) {
134         if (memcpy_s(result.ssid, WIFI_MAX_SSID_LEN, data, WIFI_MAX_SSID_LEN - 1) != EOK) {
135             return;
136         }
137 
138         if (memcpy_s(result.bssid, WIFI_MAC_LEN, data + WIFI_MAX_SSID_LEN, WIFI_MAC_LEN) != EOK) {
139             return;
140         }
141         int index = 0;
142         result.securityType = static_cast<int>(data[index++]);
143         result.rssi = static_cast<int>(data[index++]);
144         result.band = static_cast<int>(data[index++]);
145         result.frequency = static_cast<int>(data[index++]);
146         result.channelWidth = static_cast<WifiChannelWidth>(static_cast<int>(data[index++]) % WIDTH_INVALID);
147         result.centerFrequency0 = static_cast<int>(data[index++]);
148         result.centerFrequency1 = static_cast<int>(data[index++]);
149         result.timestamp = static_cast<int64_t>(OHOS::Wifi::U32_AT(data));
150         mSize = static_cast<unsigned int>(data[0]);
151     }
152     (void)GetScanInfoList(&result, &mSize);
153 }
154 
GetDeviceConfigsTest(const uint8_t * data,size_t size)155 static void GetDeviceConfigsTest(const uint8_t* data, size_t size)
156 {
157     WifiDeviceConfig result;
158     unsigned int mSize;
159     if (size >= sizeof(WifiDeviceConfig)) {
160         if (memcpy_s(result.ssid, WIFI_MAX_SSID_LEN, data, WIFI_MAX_SSID_LEN - 1) != EOK) {
161             return;
162         }
163 
164         if (memcpy_s(result.bssid, WIFI_MAC_LEN, data + WIFI_MAX_SSID_LEN, WIFI_MAC_LEN) != EOK) {
165             return;
166         }
167 
168         if (memcpy_s(result.preSharedKey, WIFI_MAX_KEY_LEN, data + WIFI_MAX_SSID_LEN, WIFI_MAX_KEY_LEN - 1) != EOK) {
169             return;
170         }
171         int index = 0;
172         result.securityType = static_cast<int>(data[index++]);
173         result.netId = static_cast<int>(data[index++]);
174         result.freq = static_cast<unsigned int>(data[index++]);
175         result.wapiPskType = static_cast<int>(data[index++]);
176         result.ipType = static_cast<IpType>(static_cast<int>(data[index++]) % UNKNOWN);
177         result.staticIp.ipAddress = static_cast<unsigned int>(data[index++]);
178         result.staticIp.gateway = static_cast<unsigned int>(data[index++]);
179         result.staticIp.netmask = static_cast<unsigned int>(data[index++]);
180         result.isHiddenSsid = static_cast<int>(data[index++]);
181         mSize = static_cast<unsigned int>(data[0]);
182     }
183     (void)GetDeviceConfigs(&result, &mSize);
184 }
185 
ConnectToDeviceTest(const uint8_t * data,size_t size)186 static void ConnectToDeviceTest(const uint8_t* data, size_t size)
187 {
188     WifiDeviceConfig config;
189     if (size >= sizeof(WifiDeviceConfig)) {
190         if (memcpy_s(config.ssid, WIFI_MAX_SSID_LEN, data, WIFI_MAX_SSID_LEN - 1) != EOK) {
191             return;
192         }
193 
194         if (memcpy_s(config.bssid, WIFI_MAC_LEN, data + WIFI_MAX_SSID_LEN, WIFI_MAC_LEN) != EOK) {
195             return;
196         }
197 
198         if (memcpy_s(config.preSharedKey, WIFI_MAX_KEY_LEN, data + WIFI_MAX_SSID_LEN, WIFI_MAX_KEY_LEN - 1) != EOK) {
199             return;
200         }
201         int index = 0;
202         config.securityType = static_cast<int>(data[index++]);
203         config.netId = static_cast<int>(data[index++]);
204         config.freq = static_cast<unsigned int>(data[index++]);
205         config.wapiPskType = static_cast<int>(data[index++]);
206         config.ipType = static_cast<IpType>(static_cast<int>(data[index++]) % UNKNOWN);
207         config.staticIp.ipAddress = static_cast<unsigned int>(data[index++]);
208         config.staticIp.gateway = static_cast<unsigned int>(data[index++]);
209         config.staticIp.netmask = static_cast<unsigned int>(data[index++]);
210         config.isHiddenSsid = static_cast<int>(data[index++]);
211     }
212     (void)ConnectToDevice(&config);
213 }
214 
GetLinkedInfoTest(const uint8_t * data,size_t size)215 static void GetLinkedInfoTest(const uint8_t* data, size_t size)
216 {
217     WifiLinkedInfo result;
218     if (size >= sizeof(WifiLinkedInfo)) {
219         if (memcpy_s(result.ssid, WIFI_MAX_SSID_LEN, data, WIFI_MAX_SSID_LEN - 1) != EOK) {
220             return;
221         }
222 
223         if (memcpy_s(result.bssid, WIFI_MAC_LEN, data + WIFI_MAX_SSID_LEN, WIFI_MAC_LEN) != EOK) {
224             return;
225         }
226         int index = 0;
227         result.rssi = static_cast<int>(data[index++]);
228         result.band = static_cast<int>(data[index++]);
229         result.frequency = static_cast<int>(data[index++]);
230         result.connState = static_cast<WifiConnState>(static_cast<int>(data[index++]) % (WIFI_CONNECTED + 1));
231         result.disconnectedReason = static_cast<unsigned short>(data[index++]);
232         result.ipAddress = static_cast<unsigned int>(data[index++]);
233     }
234     (void)GetLinkedInfo(&result);
235 }
236 
GetDeviceMacAddressTest(const uint8_t * data,size_t size)237 static void GetDeviceMacAddressTest(const uint8_t* data, size_t size)
238 {
239     unsigned char result;
240     if (size > 0) {
241         result = static_cast<unsigned char>(data[0]);
242     }
243     (void)GetDeviceMacAddress(&result);
244 }
245 
GetWifiDetailStateTest(const uint8_t * data,size_t size)246 static void GetWifiDetailStateTest(const uint8_t* data, size_t size)
247 {
248     WifiDetailState state;
249     if (size > 0) {
250         state = static_cast<WifiDetailState>(data[0]);
251     }
252     (void)GetWifiDetailState(&state);
253 }
254 
GetIpInfoTest(const uint8_t * data,size_t size)255 static void GetIpInfoTest(const uint8_t* data, size_t size)
256 {
257     IpInfo info;
258     if (size >= sizeof(IpInfo)) {
259         int index = 0;
260         info.ipAddress = static_cast<unsigned int>(data[index++]);
261         info.netMask = static_cast<unsigned int>(data[index++]);
262         info.netGate = static_cast<unsigned int>(data[index++]);
263         info.dns1 = static_cast<unsigned int>(data[index++]);
264         info.dns2 = static_cast<unsigned int>(data[index++]);
265         info.serverAddress = static_cast<unsigned int>(data[index++]);
266         info.leaseDuration = static_cast<int>(data[index++]);
267     }
268     (void)GetIpInfo(&info);
269 }
270 
SetLowLatencyModeTest(const uint8_t * data,size_t size)271 static void SetLowLatencyModeTest(const uint8_t* data, size_t size)
272 {
273     int enabled = 0;
274     if (size == 0) {
275         return;
276     }
277     enabled = static_cast<int>(data[0]);
278     (void)SetLowLatencyMode(enabled);
279 }
280 
Get5GHzChannelListTest(const uint8_t * data,size_t size)281 static void Get5GHzChannelListTest(const uint8_t* data, size_t size)
282 {
283     int result = 0;
284     int sizet = 0;
285     if (size >= TWO) {
286         int index = 0;
287         result = static_cast<int>(data[index++]);
288         sizet = static_cast<int>(data[index++]);
289     }
290     (void)Get5GHzChannelList(&result, &sizet);
291 }
292 
IsBandTypeSupportedTest(const uint8_t * data,size_t size)293 static void IsBandTypeSupportedTest(const uint8_t* data, size_t size)
294 {
295     bool supported = true;
296     int bandType = static_cast<int>(data[0]);
297     (void)IsBandTypeSupported(bandType, &supported);
298 }
299 
300 namespace OHOS {
301 namespace Wifi {
WifiCDeviceFuzzerTest(const uint8_t * data,size_t size)302     bool WifiCDeviceFuzzerTest(const uint8_t* data, size_t size)
303     {
304         EnableWifiTest();
305         DisableWifiTest();
306         EnableSemiWifiTest();
307         ScanTest();
308         RemoveDeviceTest(data, size);
309         DisableDeviceConfigTest(data, size);
310         EnableDeviceConfigTest(data, size);
311         ConnectToTest(data, size);
312         DisconnectTest();
313         AddDeviceConfigTest(data, size);
314         AdvanceScanTest(data, size);
315         GetSignalLevelTest(data, size);
316         (void)IsWifiActive();
317         GetScanInfoListTest(data, size);
318         GetDeviceConfigsTest(data, size);
319         ConnectToDeviceTest(data, size);
320         GetLinkedInfoTest(data, size);
321         GetDeviceMacAddressTest(data, size);
322         GetWifiDetailStateTest(data, size);
323         GetIpInfoTest(data, size);
324         SetLowLatencyModeTest(data, size);
325         Get5GHzChannelListTest(data, size);
326         IsBandTypeSupportedTest(data, size);
327 
328         return true;
329     }
330 }  // namespace Wifi
331 }  // namespace OHOS
332 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)333 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
334 {
335     OHOS::Wifi::WifiCDeviceFuzzerTest(data, size);
336     return 0;
337 }
338 
339