1 /*
2  * Copyright (c) 2024 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 "softbusadapter_fuzzer.h"
17 
18 #include "dscreen_constants.h"
19 #include "dscreen_errcode.h"
20 #include "dscreen_util.h"
21 #include "isoftbus_listener.h"
22 #include "socket.h"
23 #include "softbus_adapter.h"
24 #include "softbus_bus_center.h"
25 #include "softbus_common.h"
26 
27 namespace OHOS {
28 namespace DistributedHardware {
29 namespace {
30     const std::string PEER_SESSION_NAME = "ohos.dhardware.dscreen.session8647073e02e7a78f09473aa324";
31     const std::string REMOTE_DEV_ID = "f6d4c0864707aefte7a78f09473aa122ff57fc81c00981fcf5be989e7d112324";
32     const std::string DSCREEN_PKG_NAME_TEST = "ohos.dhardware.dscreen";
33 }
SoftbusAdapterFuzzTest(const uint8_t * data,size_t size)34 void SoftbusAdapterFuzzTest(const uint8_t* data, size_t size)
35 {
36     if ((data == nullptr) || (size < sizeof(int32_t))) {
37         return;
38     }
39     int32_t sessionId = *(reinterpret_cast<const int32_t*>(data));
40 
41     PeerSocketInfo peerSocketInfo = {
42         .name = const_cast<char*>(PEER_SESSION_NAME.c_str()),
43         .networkId = const_cast<char*>(REMOTE_DEV_ID.c_str()),
44         .pkgName = const_cast<char*>(DSCREEN_PKG_NAME_TEST.c_str()),
45         .dataType = DATA_TYPE_BYTES
46     };
47     ShutdownReason reason = SHUTDOWN_REASON_UNKNOWN;
48 
49     void *adapterData = nullptr;
50     uint32_t dataLen = *(reinterpret_cast<const uint32_t*>(data));
51     StreamData *stream = nullptr;
52     StreamData *ext = nullptr;
53     StreamFrameInfo *info = nullptr;
54     unsigned int dataLen1 = *(reinterpret_cast<const unsigned int *>(data));
55 
56     std::shared_ptr<SoftbusAdapter> adapter = std::make_shared<SoftbusAdapter>();
57     adapter->sessListener_.OnBind(sessionId, peerSocketInfo);
58     adapter->sessListener_.OnShutdown(sessionId, reason);
59     adapter->sessListener_.OnBytes(sessionId, adapterData, dataLen);
60     adapter->sessListener_.OnStream(sessionId, stream, ext, info);
61     adapter->sessListener_.OnMessage(sessionId, adapterData, dataLen1);
62     adapter->SendSoftbusStream(sessionId, stream, ext, info);
63     adapter->SendSoftbusBytes(sessionId, adapterData, dataLen1);
64     adapter->CloseSoftbusSession(sessionId);
65 }
66 
SoftbusOnBytesReceivedFuzzTest(const uint8_t * data,size_t size)67 void SoftbusOnBytesReceivedFuzzTest(const uint8_t* data, size_t size)
68 {
69     if ((data == nullptr) || (size < (sizeof(int32_t)))) {
70         return;
71     }
72     int32_t sessionId = *(reinterpret_cast<const int32_t*>(data));
73     void *adapterData = nullptr;
74     uint32_t dataLen = *(reinterpret_cast<const uint32_t*>(data));
75     std::shared_ptr<SoftbusAdapter> adapter = std::make_shared<SoftbusAdapter>();
76     adapter->OnBytesReceived(sessionId, adapterData, dataLen);
77 }
78 
SoftbusOnMessageReceivedFuzzTest(const uint8_t * data,size_t size)79 void SoftbusOnMessageReceivedFuzzTest(const uint8_t* data, size_t size)
80 {
81     if ((data == nullptr) || (size < (sizeof(int32_t)))) {
82         return;
83     }
84     int32_t sessionId = *(reinterpret_cast<const int32_t*>(data));
85     void *adapterData = nullptr;
86     unsigned int dataLen = *(reinterpret_cast<const unsigned int *>(data));
87     std::shared_ptr<SoftbusAdapter> adapter = std::make_shared<SoftbusAdapter>();
88     adapter->OnMessageReceived(sessionId, adapterData, dataLen);
89 }
90 
SoftbusOnSessionClosedFuzzTest(const uint8_t * data,size_t size)91 void SoftbusOnSessionClosedFuzzTest(const uint8_t* data, size_t size)
92 {
93     if ((data == nullptr) || (size < (sizeof(int32_t)))) {
94         return;
95     }
96     int32_t sessionId = *(reinterpret_cast<const int32_t*>(data));
97     std::shared_ptr<SoftbusAdapter> adapter = std::make_shared<SoftbusAdapter>();
98     ShutdownReason reason = SHUTDOWN_REASON_UNKNOWN;
99     adapter->OnSoftbusSessionClosed(sessionId, reason);
100 }
101 
SoftbusOnSessionOpenedFuzzTest(const uint8_t * data,size_t size)102 void SoftbusOnSessionOpenedFuzzTest(const uint8_t* data, size_t size)
103 {
104     if ((data == nullptr) || (size < (sizeof(int32_t)))) {
105         return;
106     }
107     int32_t sessionId = *(reinterpret_cast<const int32_t*>(data));
108     PeerSocketInfo peerSocketInfo = {
109         .name = const_cast<char*>(PEER_SESSION_NAME.c_str()),
110         .networkId = const_cast<char*>(REMOTE_DEV_ID.c_str()),
111         .pkgName = const_cast<char*>(DSCREEN_PKG_NAME_TEST.c_str()),
112         .dataType = DATA_TYPE_BYTES
113     };
114     std::shared_ptr<SoftbusAdapter> adapter = std::make_shared<SoftbusAdapter>();
115     adapter->OnSoftbusSessionOpened(sessionId, peerSocketInfo);
116 }
117 
SoftbusOnStreamReceivedFuzzTest(const uint8_t * data,size_t size)118 void SoftbusOnStreamReceivedFuzzTest(const uint8_t* data, size_t size)
119 {
120     if ((data == nullptr) || (size < (sizeof(int32_t)))) {
121         return;
122     }
123     int32_t sessionId = *(reinterpret_cast<const int32_t*>(data));
124     StreamData *stream = nullptr;
125     StreamData *ext = nullptr;
126     StreamFrameInfo *info = nullptr;
127     std::shared_ptr<SoftbusAdapter> adapter = std::make_shared<SoftbusAdapter>();
128     adapter->OnStreamReceived(sessionId, stream, ext, info);
129 }
130 
SoftbusRegisterSoftbusListenerFuzzTest(const uint8_t * data,size_t size)131 void SoftbusRegisterSoftbusListenerFuzzTest(const uint8_t* data, size_t size)
132 {
133     if ((data == nullptr) || (size == 0)) {
134         return;
135     }
136     std::shared_ptr<ISoftbusListener> listener = std::make_shared<MockSoftbusListener>();
137     std::string sessionName(reinterpret_cast<const char*>(data), size);
138     std::string peerDevId(reinterpret_cast<const char*>(data), size);
139 
140     std::shared_ptr<SoftbusAdapter> softbusAdapter = std::make_shared<SoftbusAdapter>();
141     (void)softbusAdapter->RegisterSoftbusListener(listener, sessionName, peerDevId);
142     (void)softbusAdapter->UnRegisterSoftbusListener(sessionName, peerDevId);
143 }
144 
SoftbusUnRegisterSoftbusListenerFuzzTest(const uint8_t * data,size_t size)145 void SoftbusUnRegisterSoftbusListenerFuzzTest(const uint8_t* data, size_t size)
146 {
147     if ((data == nullptr) || (size == 0)) {
148         return;
149     }
150     std::string sessionName(reinterpret_cast<const char*>(data), size);
151     std::string peerDevId(reinterpret_cast<const char*>(data), size);
152 
153     std::shared_ptr<SoftbusAdapter> softbusAdapter = std::make_shared<SoftbusAdapter>();
154     (void)softbusAdapter->UnRegisterSoftbusListener(sessionName, peerDevId);
155 }
156 
SoftbusCreateSoftbusSessionServerFuzzTest(const uint8_t * data,size_t size)157 void SoftbusCreateSoftbusSessionServerFuzzTest(const uint8_t* data, size_t size)
158 {
159     if ((data == nullptr) || (size == 0)) {
160         return;
161     }
162     std::string pkgName(reinterpret_cast<const char*>(data), size);
163     std::string sessionName(reinterpret_cast<const char*>(data), size);
164     std::string peerDevId(reinterpret_cast<const char*>(data), size);
165 
166     std::shared_ptr<SoftbusAdapter> softbusAdapter = std::make_shared<SoftbusAdapter>();
167     (void)softbusAdapter->CreateSoftbusSessionServer(pkgName, sessionName, peerDevId);
168     (void)softbusAdapter->RemoveSoftbusSessionServer(pkgName, sessionName, peerDevId);
169 }
170 
SoftbusRemoveSoftbusSessionServerFuzzTest(const uint8_t * data,size_t size)171 void SoftbusRemoveSoftbusSessionServerFuzzTest(const uint8_t* data, size_t size)
172 {
173     if ((data == nullptr) || (size == 0)) {
174         return;
175     }
176     std::string pkgName(reinterpret_cast<const char*>(data), size);
177     std::string sessionName(reinterpret_cast<const char*>(data), size);
178     std::string peerDevId(reinterpret_cast<const char*>(data), size);
179 
180     std::shared_ptr<SoftbusAdapter> softbusAdapter = std::make_shared<SoftbusAdapter>();
181     (void)softbusAdapter->RemoveSoftbusSessionServer(pkgName, sessionName, peerDevId);
182 }
183 
SoftbusOpenSoftbusSessionFuzzTest(const uint8_t * data,size_t size)184 void SoftbusOpenSoftbusSessionFuzzTest(const uint8_t* data, size_t size)
185 {
186     if ((data == nullptr) || (size == 0)) {
187         return;
188     }
189     std::string mySessionName(reinterpret_cast<const char*>(data), size);
190     std::string peerSessionName(reinterpret_cast<const char*>(data), size);
191     std::string peerDevId(reinterpret_cast<const char*>(data), size);
192 
193     std::shared_ptr<SoftbusAdapter> softbusAdapter = std::make_shared<SoftbusAdapter>();
194     int32_t socketId = softbusAdapter->OpenSoftbusSession(mySessionName, peerSessionName, peerDevId);
195     (void)softbusAdapter->CloseSoftbusSession(socketId);
196 }
197 
198 }  // namespace DistributedHardware
199 }  // namespace OHOS
200 
201 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)202 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
203 {
204     /* Run your code on data */
205     OHOS::DistributedHardware::SoftbusAdapterFuzzTest(data, size);
206     OHOS::DistributedHardware::SoftbusOnBytesReceivedFuzzTest(data, size);
207     OHOS::DistributedHardware::SoftbusOnMessageReceivedFuzzTest(data, size);
208     OHOS::DistributedHardware::SoftbusOnSessionClosedFuzzTest(data, size);
209     OHOS::DistributedHardware::SoftbusOnSessionOpenedFuzzTest(data, size);
210     OHOS::DistributedHardware::SoftbusOnStreamReceivedFuzzTest(data, size);
211     OHOS::DistributedHardware::SoftbusRegisterSoftbusListenerFuzzTest(data, size);
212     OHOS::DistributedHardware::SoftbusUnRegisterSoftbusListenerFuzzTest(data, size);
213     OHOS::DistributedHardware::SoftbusCreateSoftbusSessionServerFuzzTest(data, size);
214     OHOS::DistributedHardware::SoftbusRemoveSoftbusSessionServerFuzzTest(data, size);
215     OHOS::DistributedHardware::SoftbusOpenSoftbusSessionFuzzTest(data, size);
216     return 0;
217 }
218 
Socket(SocketInfo info)219 extern "C" __attribute__((constructor)) int Socket(SocketInfo info)
220 {
221     return 0;
222 }
223 
Listen(int32_t socket,const QosTV qos[],uint32_t qosCount,const ISocketListener * listener)224 extern "C" __attribute__((constructor)) int Listen(int32_t socket, const QosTV qos[], uint32_t qosCount,
225     const ISocketListener *listener)
226 {
227     return 0;
228 }
229 
Bind(int32_t socket,const QosTV qos[],uint32_t qosCount,const ISocketListener * listener)230 extern "C" __attribute__((constructor)) int Bind(int32_t socket, const QosTV qos[], uint32_t qosCount,
231     const ISocketListener *listener)
232 {
233     return 0;
234 }
235 
Shutdown(int32_t socket)236 extern "C" __attribute__((constructor)) void Shutdown(int32_t socket) {}
237 
SendBytes(int32_t socket,const void * data,uint32_t len)238 extern "C" __attribute__((constructor)) int SendBytes(int32_t socket, const void *data, uint32_t len)
239 {
240     return 0;
241 }
242 
SendStream(int32_t socket,const StreamData * data,const StreamData * ext,const StreamFrameInfo * param)243 extern "C" __attribute__((constructor)) int SendStream(int32_t socket, const StreamData *data, const StreamData *ext,
244     const StreamFrameInfo *param)
245 {
246     return 0;
247 }
248