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 <cstring>
18 #include "securec.h"
19 #include "wifip2pimpl_fuzzer.h"
20 #include "wifi_fuzz_common_func.h"
21 #include "src/wifi_p2p_impl.h"
22 #include "wifi_p2p.h"
23
24 namespace OHOS {
25 namespace Wifi {
26 static std::shared_ptr<WifiP2p> WifiP2pPtr = WifiP2p::GetInstance(WIFI_P2P_ABILITY_ID);
RequestServiceTest(const uint8_t * data,size_t size)27 void RequestServiceTest(const uint8_t* data, size_t size)
28 {
29 WifiP2pDevice device;
30 WifiP2pServiceRequest request;
31 if (size >= THREE) {
32 int index = 0;
33 std::string deviceName = std::string(reinterpret_cast<const char*>(data), size);
34 std::string networkName = std::string(reinterpret_cast<const char*>(data), size);
35 std::string mDeviceAddress = std::string(reinterpret_cast<const char*>(data), size);
36 std::string primaryDeviceType = std::string(reinterpret_cast<const char*>(data), size);
37 std::string secondaryDeviceType = std::string(reinterpret_cast<const char*>(data), size);
38 unsigned int supportWpsConfigMethods = static_cast<unsigned int>(data[index++]);
39 int deviceCapabilitys = static_cast<int>(data[index++]);
40 int groupCapabilitys = static_cast<int>(data[index++]);
41 device.SetDeviceName(deviceName);
42 device.SetNetworkName(networkName);
43 device.SetDeviceAddress(mDeviceAddress);
44 device.SetPrimaryDeviceType(primaryDeviceType);
45 device.SetSecondaryDeviceType(secondaryDeviceType);
46 device.SetWpsConfigMethod(supportWpsConfigMethods);
47 device.SetDeviceCapabilitys(deviceCapabilitys);
48 device.SetGroupCapabilitys(groupCapabilitys);
49 }
50 WifiP2pPtr->RequestService(device, request);
51 }
52
PutLocalP2pServiceTest(const uint8_t * data,size_t size)53 void PutLocalP2pServiceTest(const uint8_t* data, size_t size)
54 {
55 WifiP2pServiceInfo srvInfo;
56 std::string serviceName = std::string(reinterpret_cast<const char*>(data), size);
57 std::string mDeviceAddress = std::string(reinterpret_cast<const char*>(data), size);
58 srvInfo.SetServiceName(serviceName);
59 srvInfo.SetDeviceAddress(mDeviceAddress);
60 WifiP2pPtr->PutLocalP2pService(srvInfo);
61 }
62
DeleteLocalP2pServiceTest(const uint8_t * data,size_t size)63 void DeleteLocalP2pServiceTest(const uint8_t* data, size_t size)
64 {
65 WifiP2pServiceInfo srvInfo;
66 std::string serviceName = std::string(reinterpret_cast<const char*>(data), size);
67 std::string mDeviceAddress = std::string(reinterpret_cast<const char*>(data), size);
68 srvInfo.SetServiceName(serviceName);
69 srvInfo.SetDeviceAddress(mDeviceAddress);
70 WifiP2pPtr->DeleteLocalP2pService(srvInfo);
71 }
72
QueryP2pLinkedInfoTest(const uint8_t * data,size_t size)73 void QueryP2pLinkedInfoTest(const uint8_t* data, size_t size)
74 {
75 if (size == 0) {
76 return;
77 }
78 WifiP2pLinkedInfo linkedInfo;
79 bool isP2pGroupOwner = (static_cast<int>(data[0]) % TWO) ? true : false;
80 std::string groupOwnerAddress = std::string(reinterpret_cast<const char*>(data), size);
81 linkedInfo.SetIsGroupOwner(isP2pGroupOwner);
82 linkedInfo.SetIsGroupOwnerAddress(groupOwnerAddress);
83 WifiP2pPtr->QueryP2pLinkedInfo(linkedInfo);
84 }
85
GetP2pDiscoverStatusTest(const uint8_t * data,size_t size)86 void GetP2pDiscoverStatusTest(const uint8_t* data, size_t size)
87 {
88 if (size == 0) {
89 return;
90 }
91 int status = static_cast<int>(data[0]);
92 WifiP2pPtr->GetP2pDiscoverStatus(status);
93 }
94
QueryP2pServicesTest(const uint8_t * data,size_t size)95 void QueryP2pServicesTest(const uint8_t* data, size_t size)
96 {
97 std::vector<WifiP2pServiceInfo> services;
98 WifiP2pPtr->QueryP2pServices(services);
99 }
100
GetSupportedFeaturesTest(const uint8_t * data,size_t size)101 void GetSupportedFeaturesTest(const uint8_t* data, size_t size)
102 {
103 if (size < FOUR) {
104 return;
105 }
106 long features = static_cast<long>(OHOS::Wifi::U32_AT(data));
107 WifiP2pPtr->GetSupportedFeatures(features);
108 }
109
IsFeatureSupportedTest(const uint8_t * data,size_t size)110 void IsFeatureSupportedTest(const uint8_t* data, size_t size)
111 {
112 if (size < FOUR) {
113 return;
114 }
115 long features = static_cast<long>(OHOS::Wifi::U32_AT(data));
116 WifiP2pPtr->IsFeatureSupported(features);
117 }
118
SetP2pDeviceNameTest(const uint8_t * data,size_t size)119 void SetP2pDeviceNameTest(const uint8_t* data, size_t size)
120 {
121 std::string deviceName = std::string(reinterpret_cast<const char*>(data), size);
122 WifiP2pPtr->SetP2pDeviceName(deviceName);
123 }
124
SetP2pWfdInfoTest(const uint8_t * data,size_t size)125 void SetP2pWfdInfoTest(const uint8_t* data, size_t size)
126 {
127 WifiP2pWfdInfo wfdInfo;
128 if (size >= FOUR) {
129 int index = 0;
130 bool wfdEnabled = (static_cast<int>(data[index++]) % TWO) ? true : false;
131 int deviceInfo = static_cast<int>(data[index++]);
132 int ctrlPort = static_cast<int>(data[index++]);
133 int maxThroughput = static_cast<int>(data[index++]);
134 wfdInfo.SetWfdEnabled(wfdEnabled);
135 wfdInfo.SetDeviceInfo(deviceInfo);
136 wfdInfo.SetCtrlPort(ctrlPort);
137 wfdInfo.SetMaxThroughput(maxThroughput);
138 }
139 WifiP2pPtr->SetP2pWfdInfo(wfdInfo);
140 }
141
142
WifiHotSpotImplFuzzTest(const uint8_t * data,size_t size)143 bool WifiHotSpotImplFuzzTest(const uint8_t* data, size_t size)
144 {
145 RequestServiceTest(data, size);
146 PutLocalP2pServiceTest(data, size);
147 DeleteLocalP2pServiceTest(data, size);
148 QueryP2pLinkedInfoTest(data, size);
149 GetP2pDiscoverStatusTest(data, size);
150 QueryP2pServicesTest(data, size);
151 GetSupportedFeaturesTest(data, size);
152 IsFeatureSupportedTest(data, size);
153 SetP2pDeviceNameTest(data, size);
154 SetP2pWfdInfoTest(data, size);
155 return true;
156 }
157 } // namespace Wifi
158 } // namespace OHOS
159
160 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)161 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
162 {
163 OHOS::Wifi::WifiHotSpotImplFuzzTest(data, size);
164 return 0;
165 }