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