1 /*
2 * Copyright (c) 2023 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 "softbusclientstub_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20
21 #include "client_trans_channel_manager.h"
22 #include "message_parcel.h"
23 #include "securec.h"
24 #include "session.h"
25 #include "softbus_adapter_mem.h"
26 #include "softbus_client_stub.h"
27
28 namespace OHOS {
29 constexpr size_t FOO_MAX_LEN = 1024;
30 constexpr size_t U32_AT_SIZE = 4;
31
32 class TestEnv {
33 public:
TestEnv()34 TestEnv()
35 {
36 isInited_ = false;
37 ClientTransChannelInit();
38 isInited_ = true;
39 }
40
~TestEnv()41 ~TestEnv()
42 {
43 isInited_ = false;
44 ClientTransChannelDeinit();
45 }
46
IsInited(void)47 bool IsInited(void)
48 {
49 return isInited_;
50 }
51
52 private:
53 volatile bool isInited_;
54 };
55
56 enum SoftBusFuncId {
57 CLIENT_ON_CHANNEL_OPENED = 256,
58 CLIENT_ON_CHANNEL_OPENFAILED,
59 CLIENT_ON_CHANNEL_LINKDOWN,
60 CLIENT_ON_CHANNEL_CLOSED,
61 CLIENT_ON_CHANNEL_MSGRECEIVED,
62 CLIENT_ON_CHANNEL_QOSEVENT,
63
64 CLIENT_DISCOVERY_DEVICE_FOUND,
65
66 CLIENT_ON_JOIN_RESULT,
67 CLIENT_ON_JOIN_METANODE_RESULT,
68 CLIENT_ON_LEAVE_RESULT,
69 CLIENT_ON_LEAVE_METANODE_RESULT,
70 CLIENT_ON_NODE_DEVICE_NOT_TRUST,
71 CLIENT_ON_HICHAIN_PROOF_EXCEPTION,
72 CLIENT_ON_NODE_ONLINE_STATE_CHANGED,
73 CLIENT_ON_NODE_BASIC_INFO_CHANGED,
74 CLIENT_ON_LOCAL_NETWORK_ID_CHANGED,
75 CLIENT_ON_TIME_SYNC_RESULT,
76 CLIENT_ON_PUBLISH_LNN_RESULT,
77 CLIENT_ON_REFRESH_LNN_RESULT,
78 CLIENT_ON_REFRESH_DEVICE_FOUND,
79 CLIENT_ON_PERMISSION_CHANGE,
80 CLIENT_SET_CHANNEL_INFO,
81 CLIENT_ON_DATA_LEVEL_CHANGED,
82 CLIENT_ON_TRANS_LIMIT_CHANGE,
83 CLIENT_ON_CHANNEL_BIND,
84 };
85
86 const std::u16string SOFTBUS_CLIENT_STUB_INTERFACE_TOKEN = u"OHOS.ISoftBusClient";
87
InitOnChannelOpenedInnerMsg(int32_t channelType,const uint8_t * data,size_t size,MessageParcel & message)88 static void InitOnChannelOpenedInnerMsg(int32_t channelType, const uint8_t *data, size_t size, MessageParcel &message)
89 {
90 bool boolParam = (size % 2 == 0) ? true : false;
91 int32_t int32Param = *(reinterpret_cast<const int32_t *>(data));
92 char *charParam = const_cast<char *>(reinterpret_cast<const char *>(data));
93
94 message.WriteInterfaceToken(SOFTBUS_CLIENT_STUB_INTERFACE_TOKEN);
95
96 message.WriteCString(charParam);
97 message.WriteInt32(int32Param);
98 message.WriteInt32(channelType);
99 message.WriteUint64(size);
100 message.WriteInt32(int32Param);
101
102 message.WriteBool(boolParam);
103 message.WriteBool(boolParam);
104 message.WriteBool(boolParam);
105 message.WriteInt32(int32Param);
106 message.WriteInt32(int32Param);
107 message.WriteCString(charParam);
108 message.WriteUint32(size);
109 message.WriteRawData(data, size);
110 message.WriteCString(charParam);
111 message.WriteCString(charParam);
112 message.WriteInt32(int32Param);
113
114 if (channelType == CHANNEL_TYPE_UDP) {
115 message.WriteCString(charParam);
116 message.WriteInt32(int32Param);
117 message.WriteBool(boolParam);
118 if (boolParam) {
119 message.WriteInt32(int32Param);
120 message.WriteCString(charParam);
121 }
122 }
123
124 message.WriteInt32(int32Param);
125 message.WriteInt32(int32Param);
126 message.WriteInt32(int32Param);
127 message.WriteInt32(int32Param);
128 message.WriteUint32(size);
129 message.WriteInt32(int32Param);
130 message.WriteInt32(int32Param);
131 }
132
133 /*
134 * Due to FileDescriptor is invalid, CHANNEL_TYPE_TCP_DIRECT will read it and crash
135 * Do not add test case which channel type is CHANNEL_TYPE_TCP_DIRECT
136 */
OnChannelOpenedInnerTest(const uint8_t * data,size_t size)137 bool OnChannelOpenedInnerTest(const uint8_t *data, size_t size)
138 {
139 sptr<OHOS::SoftBusClientStub> softBusClientStub = new OHOS::SoftBusClientStub();
140 if (softBusClientStub == nullptr) {
141 return false;
142 }
143
144 MessageParcel reply;
145 MessageOption option;
146 MessageParcel dataNomal;
147 InitOnChannelOpenedInnerMsg(CHANNEL_TYPE_UNDEFINED, data, size, dataNomal);
148
149 MessageParcel dataUdp;
150 InitOnChannelOpenedInnerMsg(CHANNEL_TYPE_UDP, data, size, dataUdp);
151 softBusClientStub->OnRemoteRequest(CLIENT_ON_CHANNEL_OPENED, dataUdp, reply, option);
152 return true;
153 }
154
OnChannelOpenFailedInnerTest(const uint8_t * data,size_t size)155 bool OnChannelOpenFailedInnerTest(const uint8_t *data, size_t size)
156 {
157 sptr<OHOS::SoftBusClientStub> softBusClientStub = new OHOS::SoftBusClientStub();
158 if (softBusClientStub == nullptr) {
159 return false;
160 }
161
162 int32_t channelId = *(reinterpret_cast<const int32_t *>(data));
163 int32_t channelType = *(reinterpret_cast<const int32_t *>(data));
164 int32_t errCode = *(reinterpret_cast<const int32_t *>(data));
165
166 MessageParcel datas;
167 MessageParcel reply;
168 MessageOption option;
169 datas.WriteInterfaceToken(SOFTBUS_CLIENT_STUB_INTERFACE_TOKEN);
170 datas.WriteInt32(channelId);
171 datas.WriteInt32(channelType);
172 datas.WriteInt32(errCode);
173 softBusClientStub->OnRemoteRequest(CLIENT_ON_CHANNEL_OPENFAILED, datas, reply, option);
174
175 return true;
176 }
177
OnChannelLinkDownInnerTest(const uint8_t * data,size_t size)178 bool OnChannelLinkDownInnerTest(const uint8_t *data, size_t size)
179 {
180 sptr<OHOS::SoftBusClientStub> softBusClientStub = new OHOS::SoftBusClientStub();
181 if (softBusClientStub == nullptr) {
182 return false;
183 }
184
185 char *networkId = const_cast<char *>(reinterpret_cast<const char *>(data));
186 int32_t routeType = *(reinterpret_cast<const int32_t *>(data));
187
188 MessageParcel datas;
189 MessageParcel reply;
190 MessageOption option;
191
192 datas.WriteInterfaceToken(SOFTBUS_CLIENT_STUB_INTERFACE_TOKEN);
193 datas.WriteCString(networkId);
194 datas.WriteInt32(routeType);
195 softBusClientStub->OnRemoteRequest(CLIENT_ON_CHANNEL_LINKDOWN, datas, reply, option);
196
197 return true;
198 }
199
OnChannelClosedInnerTest(const uint8_t * data,size_t size)200 bool OnChannelClosedInnerTest(const uint8_t *data, size_t size)
201 {
202 sptr<OHOS::SoftBusClientStub> softBusClientStub = new OHOS::SoftBusClientStub();
203 if (softBusClientStub == nullptr) {
204 return false;
205 }
206
207 int32_t channelId = *(reinterpret_cast<const int32_t *>(data));
208 int32_t channelType = *(reinterpret_cast<const int32_t *>(data));
209 int32_t messageType = *(reinterpret_cast<const int32_t *>(data));
210
211 MessageParcel datas;
212 MessageParcel reply;
213 MessageOption option;
214 datas.WriteInterfaceToken(SOFTBUS_CLIENT_STUB_INTERFACE_TOKEN);
215 datas.WriteInt32(channelId);
216 datas.WriteInt32(channelType);
217 datas.WriteInt32(messageType);
218 softBusClientStub->OnRemoteRequest(CLIENT_ON_CHANNEL_CLOSED, datas, reply, option);
219
220 return true;
221 }
222
OnChannelMsgReceivedInnerTest(const uint8_t * data,size_t size)223 bool OnChannelMsgReceivedInnerTest(const uint8_t *data, size_t size)
224 {
225 sptr<OHOS::SoftBusClientStub> softBusClientStub = new OHOS::SoftBusClientStub();
226 if (softBusClientStub == nullptr) {
227 return false;
228 }
229
230 int32_t channelId = *(reinterpret_cast<const int32_t *>(data));
231 int32_t channelType = *(reinterpret_cast<const int32_t *>(data));
232 int32_t type = *(reinterpret_cast<const int32_t *>(data));
233
234 MessageParcel datas;
235 MessageParcel reply;
236 MessageOption option;
237 datas.WriteInterfaceToken(SOFTBUS_CLIENT_STUB_INTERFACE_TOKEN);
238 datas.WriteInt32(channelId);
239 datas.WriteInt32(channelType);
240 datas.WriteUint32(size);
241 datas.WriteRawData(data, size);
242 datas.WriteInt32(type);
243 softBusClientStub->OnRemoteRequest(CLIENT_ON_CHANNEL_MSGRECEIVED, datas, reply, option);
244
245 return true;
246 }
247
OnChannelQosEventInnerTest(const uint8_t * data,size_t size)248 bool OnChannelQosEventInnerTest(const uint8_t *data, size_t size)
249 {
250 sptr<OHOS::SoftBusClientStub> softBusClientStub = new OHOS::SoftBusClientStub();
251 if (softBusClientStub == nullptr) {
252 return false;
253 }
254
255 int32_t channelId = *(reinterpret_cast<const int32_t *>(data));
256 int32_t channelType = *(reinterpret_cast<const int32_t *>(data));
257 int32_t eventId = *(reinterpret_cast<const int32_t *>(data));
258 WifiChannelQuality wifiChannelInfo = {
259 .channel = size,
260 .score = size,
261 };
262 QosTv qosTv = {
263 .type = WIFI_CHANNEL_QUALITY,
264 .info.wifiChannelInfo = wifiChannelInfo,
265 };
266
267 MessageParcel datas;
268 MessageParcel reply;
269 MessageOption option;
270 datas.WriteInterfaceToken(SOFTBUS_CLIENT_STUB_INTERFACE_TOKEN);
271 datas.WriteInt32(channelId);
272 datas.WriteInt32(channelType);
273 datas.WriteInt32(eventId);
274 datas.WriteInt32(sizeof(qosTv));
275 datas.WriteRawData(&qosTv, sizeof(qosTv));
276 softBusClientStub->OnRemoteRequest(CLIENT_ON_CHANNEL_QOSEVENT, datas, reply, option);
277
278 return true;
279 }
280
OnDeviceFoundInnerTest(const uint8_t * data,size_t size)281 bool OnDeviceFoundInnerTest(const uint8_t *data, size_t size)
282 {
283 MessageParcel datas;
284 datas.WriteInterfaceToken(SOFTBUS_CLIENT_STUB_INTERFACE_TOKEN);
285 datas.WriteBuffer(data, size);
286 datas.RewindRead(0);
287 MessageParcel reply;
288 MessageOption option;
289 sptr<OHOS::SoftBusClientStub> softBusClientStub = new OHOS::SoftBusClientStub();
290 if (softBusClientStub == nullptr) {
291 return false;
292 }
293 softBusClientStub->OnRemoteRequest(CLIENT_DISCOVERY_DEVICE_FOUND, datas, reply, option);
294 return true;
295 }
296
OnJoinLNNResultInnerTest(const uint8_t * data,size_t size)297 bool OnJoinLNNResultInnerTest(const uint8_t *data, size_t size)
298 {
299 constexpr uint32_t addrTypeLen = 10;
300 constexpr int32_t retCode = 2;
301 const char *test = "test";
302 constexpr size_t len = 4;
303 MessageParcel datas;
304 datas.WriteInterfaceToken(SOFTBUS_CLIENT_STUB_INTERFACE_TOKEN);
305 datas.WriteUint32(addrTypeLen);
306 datas.WriteInt32(retCode);
307 datas.WriteRawData(test, len);
308 datas.WriteBuffer(data, size);
309 datas.RewindRead(0);
310 MessageParcel reply;
311 MessageOption option;
312 sptr<OHOS::SoftBusClientStub> softBusClientStub = new OHOS::SoftBusClientStub();
313 if (softBusClientStub == nullptr) {
314 return false;
315 }
316 softBusClientStub->OnRemoteRequest(CLIENT_ON_JOIN_RESULT, datas, reply, option);
317 return true;
318 }
319
OnJoinMetaNodeResultInnerTest(const uint8_t * data,size_t size)320 bool OnJoinMetaNodeResultInnerTest(const uint8_t *data, size_t size)
321 {
322 constexpr uint32_t addrTypeLen = 12;
323 constexpr int32_t retCode = 2;
324 const char *test = "test";
325 constexpr size_t len = 4;
326 MessageParcel datas;
327 datas.WriteInterfaceToken(SOFTBUS_CLIENT_STUB_INTERFACE_TOKEN);
328 datas.WriteUint32(addrTypeLen);
329 datas.WriteInt32(retCode);
330 datas.WriteRawData(test, len);
331 datas.WriteBuffer(data, size);
332 datas.RewindRead(0);
333 MessageParcel reply;
334 MessageOption option;
335 sptr<OHOS::SoftBusClientStub> softBusClientStub = new OHOS::SoftBusClientStub();
336 if (softBusClientStub == nullptr) {
337 return false;
338 }
339 softBusClientStub->OnRemoteRequest(CLIENT_ON_JOIN_METANODE_RESULT, datas, reply, option);
340 return true;
341 }
342
OnLeaveLNNResultInnerTest(const uint8_t * data,size_t size)343 bool OnLeaveLNNResultInnerTest(const uint8_t *data, size_t size)
344 {
345 constexpr int32_t intNum = 2;
346 MessageParcel datas;
347 datas.WriteInterfaceToken(SOFTBUS_CLIENT_STUB_INTERFACE_TOKEN);
348 datas.WriteBuffer(data, size);
349 datas.WriteInt32(intNum);
350 datas.RewindRead(0);
351 MessageParcel reply;
352 MessageOption option;
353 sptr<OHOS::SoftBusClientStub> softBusClientStub = new OHOS::SoftBusClientStub();
354 if (softBusClientStub == nullptr) {
355 return false;
356 }
357 softBusClientStub->OnRemoteRequest(CLIENT_ON_LEAVE_RESULT, datas, reply, option);
358 return true;
359 }
360
OnLeaveMetaNodeResultInnerTest(const uint8_t * data,size_t size)361 bool OnLeaveMetaNodeResultInnerTest(const uint8_t *data, size_t size)
362 {
363 constexpr int32_t intNum = 2;
364 MessageParcel datas;
365 datas.WriteInterfaceToken(SOFTBUS_CLIENT_STUB_INTERFACE_TOKEN);
366 datas.WriteBuffer(data, size);
367 datas.WriteInt32(intNum);
368 datas.RewindRead(0);
369 MessageParcel reply;
370 MessageOption option;
371 sptr<OHOS::SoftBusClientStub> softBusClientStub = new OHOS::SoftBusClientStub();
372 if (softBusClientStub == nullptr) {
373 return false;
374 }
375 softBusClientStub->OnRemoteRequest(CLIENT_ON_LEAVE_METANODE_RESULT, datas, reply, option);
376 return true;
377 }
378
OnNodeDeviceNotTrustedInnerTest(const uint8_t * data,size_t size)379 bool OnNodeDeviceNotTrustedInnerTest(const uint8_t *data, size_t size)
380 {
381 MessageParcel datas;
382 datas.WriteInterfaceToken(SOFTBUS_CLIENT_STUB_INTERFACE_TOKEN);
383 datas.WriteBuffer(data, size);
384 datas.RewindRead(0);
385 MessageParcel reply;
386 MessageOption option;
387 sptr<OHOS::SoftBusClientStub> softBusClientStub = new OHOS::SoftBusClientStub();
388 if (softBusClientStub == nullptr) {
389 return false;
390 }
391 softBusClientStub->OnRemoteRequest(CLIENT_ON_NODE_DEVICE_NOT_TRUST, datas, reply, option);
392 return true;
393 }
394
OnHichainProofExceptionInnerTest(const uint8_t * data,size_t size)395 bool OnHichainProofExceptionInnerTest(const uint8_t *data, size_t size)
396 {
397 MessageParcel datas;
398 datas.WriteInterfaceToken(SOFTBUS_CLIENT_STUB_INTERFACE_TOKEN);
399 datas.WriteBuffer(data, size);
400 datas.RewindRead(0);
401 MessageParcel reply;
402 MessageOption option;
403 sptr<OHOS::SoftBusClientStub> softBusClientStub = new OHOS::SoftBusClientStub();
404 if (softBusClientStub == nullptr) {
405 return false;
406 }
407 softBusClientStub->OnRemoteRequest(CLIENT_ON_HICHAIN_PROOF_EXCEPTION, datas, reply, option);
408 return true;
409 }
410
OnNodeOnlineStateChangedInnerTest(const uint8_t * data,size_t size)411 bool OnNodeOnlineStateChangedInnerTest(const uint8_t *data, size_t size)
412 {
413 constexpr uint32_t infoTypeLen = 10;
414 bool boolNum = true;
415 const char *test = "test";
416 constexpr size_t len = 4;
417 MessageParcel datas;
418 datas.WriteInterfaceToken(SOFTBUS_CLIENT_STUB_INTERFACE_TOKEN);
419 datas.WriteBuffer(data, size);
420 datas.WriteBool(boolNum);
421 datas.WriteUint32(infoTypeLen);
422 datas.WriteRawData(test, len);
423 datas.RewindRead(0);
424 MessageParcel reply;
425 MessageOption option;
426 sptr<OHOS::SoftBusClientStub> softBusClientStub = new OHOS::SoftBusClientStub();
427 if (softBusClientStub == nullptr) {
428 return false;
429 }
430 softBusClientStub->OnRemoteRequest(CLIENT_ON_NODE_ONLINE_STATE_CHANGED, datas, reply, option);
431 return true;
432 }
433
OnNodeBasicInfoChangedInnerTest(const uint8_t * data,size_t size)434 bool OnNodeBasicInfoChangedInnerTest(const uint8_t *data, size_t size)
435 {
436 constexpr int32_t type = 2;
437 constexpr uint32_t infoTypeLen = 10;
438 const char *test = "test";
439 constexpr size_t len = 4;
440 MessageParcel datas;
441 datas.WriteInterfaceToken(SOFTBUS_CLIENT_STUB_INTERFACE_TOKEN);
442 datas.WriteBuffer(data, size);
443 datas.WriteRawData(test, len);
444 datas.WriteInt32(type);
445 datas.WriteUint32(infoTypeLen);
446 datas.RewindRead(0);
447 MessageParcel reply;
448 MessageOption option;
449 sptr<OHOS::SoftBusClientStub> softBusClientStub = new OHOS::SoftBusClientStub();
450 if (softBusClientStub == nullptr) {
451 return false;
452 }
453 softBusClientStub->OnRemoteRequest(CLIENT_ON_NODE_BASIC_INFO_CHANGED, datas, reply, option);
454 return true;
455 }
456
OnTimeSyncResultInnerTest(const uint8_t * data,size_t size)457 bool OnTimeSyncResultInnerTest(const uint8_t *data, size_t size)
458 {
459 constexpr uint32_t infoTypeLen = 10;
460 int32_t retCode = *(reinterpret_cast<const char *>(data));
461 const char *test = "test";
462 constexpr size_t len = 4;
463 MessageParcel datas;
464 datas.WriteInterfaceToken(SOFTBUS_CLIENT_STUB_INTERFACE_TOKEN);
465 datas.WriteInt32(retCode);
466 datas.WriteUint32(infoTypeLen);
467 datas.WriteRawData(test, len);
468 datas.RewindRead(0);
469 MessageParcel reply;
470 MessageOption option;
471 sptr<OHOS::SoftBusClientStub> softBusClientStub = new OHOS::SoftBusClientStub();
472 if (softBusClientStub == nullptr) {
473 return false;
474 }
475 softBusClientStub->OnRemoteRequest(CLIENT_ON_TIME_SYNC_RESULT, datas, reply, option);
476 return true;
477 }
478
OnClientEventByReasonAndCode(const uint8_t * data,size_t size,int32_t reason,uint32_t code)479 static bool OnClientEventByReasonAndCode(const uint8_t *data, size_t size, int32_t reason, uint32_t code)
480 {
481 (void)size;
482 int32_t intNum = *(reinterpret_cast<const int32_t *>(data));
483 MessageParcel datas;
484 datas.WriteInterfaceToken(SOFTBUS_CLIENT_STUB_INTERFACE_TOKEN);
485 datas.WriteInt32(intNum);
486 datas.WriteInt32(reason);
487 datas.RewindRead(0);
488 MessageParcel reply;
489 MessageOption option;
490 sptr<OHOS::SoftBusClientStub> softBusClientStub = new OHOS::SoftBusClientStub();
491 if (softBusClientStub == nullptr) {
492 return false;
493 }
494 softBusClientStub->OnRemoteRequest(code, datas, reply, option);
495 return true;
496 }
497
OnPublishLNNResultInnerTest(const uint8_t * data,size_t size)498 bool OnPublishLNNResultInnerTest(const uint8_t *data, size_t size)
499 {
500 constexpr int32_t reason = 2;
501 return OnClientEventByReasonAndCode(data, size, reason, CLIENT_ON_PUBLISH_LNN_RESULT);
502 }
503
OnRefreshLNNResultInnerTest(const uint8_t * data,size_t size)504 bool OnRefreshLNNResultInnerTest(const uint8_t *data, size_t size)
505 {
506 constexpr int32_t reason = 8;
507 return OnClientEventByReasonAndCode(data, size, reason, CLIENT_ON_REFRESH_LNN_RESULT);
508 }
509
OnRefreshDeviceFoundInnerTest(const uint8_t * data,size_t size)510 bool OnRefreshDeviceFoundInnerTest(const uint8_t *data, size_t size)
511 {
512 uint32_t deviceLen = *(reinterpret_cast<const int32_t *>(data));
513 const char *test = "test";
514 MessageParcel datas;
515 constexpr size_t len = 4;
516 datas.WriteInterfaceToken(SOFTBUS_CLIENT_STUB_INTERFACE_TOKEN);
517 datas.WriteUint32(deviceLen);
518 datas.WriteRawData(test, len);
519 datas.RewindRead(0);
520 MessageParcel reply;
521 MessageOption option;
522 sptr<OHOS::SoftBusClientStub> softBusClientStub = new OHOS::SoftBusClientStub();
523 if (softBusClientStub == nullptr) {
524 return false;
525 }
526 softBusClientStub->OnRemoteRequest(CLIENT_ON_REFRESH_DEVICE_FOUND, datas, reply, option);
527 return true;
528 }
529
OnClientPermissonChangeInnerTest(const uint8_t * data,size_t size)530 bool OnClientPermissonChangeInnerTest(const uint8_t *data, size_t size)
531 {
532 sptr<OHOS::SoftBusClientStub> softBusClientStub = new OHOS::SoftBusClientStub();
533 if (softBusClientStub == nullptr) {
534 return false;
535 }
536
537 int32_t state = *(reinterpret_cast<const int32_t *>(data));
538 char *pkgName = const_cast<char *>(reinterpret_cast<const char *>(data));
539
540 MessageParcel datas;
541 MessageParcel reply;
542 MessageOption option;
543 datas.WriteInterfaceToken(SOFTBUS_CLIENT_STUB_INTERFACE_TOKEN);
544 datas.WriteInt32(state);
545 datas.WriteCString(pkgName);
546 softBusClientStub->OnRemoteRequest(CLIENT_ON_PERMISSION_CHANGE, datas, reply, option);
547
548 return true;
549 }
550
OnDiscoverFailedInnerTest(const uint8_t * data,size_t size)551 bool OnDiscoverFailedInnerTest(const uint8_t *data, size_t size)
552 {
553 constexpr int32_t reason = 2;
554 return OnClientEventByReasonAndCode(data, size, reason, CLIENT_ON_PUBLISH_LNN_RESULT);
555 }
556
OnDiscoverySuccessInnerTest(const uint8_t * data,size_t size)557 bool OnDiscoverySuccessInnerTest(const uint8_t *data, size_t size)
558 {
559 constexpr int32_t reason = 2;
560 return OnClientEventByReasonAndCode(data, size, reason, CLIENT_ON_PUBLISH_LNN_RESULT);
561 }
562
OnPublishSuccessInnerTest(const uint8_t * data,size_t size)563 bool OnPublishSuccessInnerTest(const uint8_t *data, size_t size)
564 {
565 constexpr int32_t reason = 2;
566 return OnClientEventByReasonAndCode(data, size, reason, CLIENT_ON_PUBLISH_LNN_RESULT);
567 }
568
OnPublishFailInnerTest(const uint8_t * data,size_t size)569 bool OnPublishFailInnerTest(const uint8_t *data, size_t size)
570 {
571 constexpr int32_t reason = 2;
572 return OnClientEventByReasonAndCode(data, size, reason, CLIENT_ON_PUBLISH_LNN_RESULT);
573 }
574
OnClientTransLimitChangeInnerTest(const uint8_t * data,size_t size)575 bool OnClientTransLimitChangeInnerTest(const uint8_t *data, size_t size)
576 {
577 sptr<OHOS::SoftBusClientStub> softBusClientStub = new OHOS::SoftBusClientStub();
578 if (softBusClientStub == nullptr) {
579 return false;
580 }
581
582 int32_t channelId = *(reinterpret_cast<const int32_t *>(data));
583 uint8_t tos = *(reinterpret_cast<const uint8_t *>(data));
584
585 MessageParcel datas;
586 MessageParcel reply;
587 MessageOption option;
588 datas.WriteInterfaceToken(SOFTBUS_CLIENT_STUB_INTERFACE_TOKEN);
589 datas.WriteInt32(channelId);
590 datas.WriteUint8(tos);
591 softBusClientStub->OnRemoteRequest(CLIENT_ON_TRANS_LIMIT_CHANGE, datas, reply, option);
592
593 return true;
594 }
595
SetChannelInfoInnerTest(const uint8_t * data,size_t size)596 bool SetChannelInfoInnerTest(const uint8_t *data, size_t size)
597 {
598 sptr<OHOS::SoftBusClientStub> softBusClientStub = new OHOS::SoftBusClientStub();
599 if (softBusClientStub == nullptr) {
600 return false;
601 }
602
603 char *sessionName = const_cast<char *>(reinterpret_cast<const char *>(data));
604 int32_t sessionId = *(reinterpret_cast<const int32_t *>(data));
605 int32_t channelId = *(reinterpret_cast<const int32_t *>(data));
606 int32_t channelType = *(reinterpret_cast<const int32_t *>(data));
607
608 MessageParcel datas;
609 MessageParcel reply;
610 MessageOption option;
611 datas.WriteInterfaceToken(SOFTBUS_CLIENT_STUB_INTERFACE_TOKEN);
612 datas.WriteCString(sessionName);
613 datas.WriteInt32(sessionId);
614 datas.WriteInt32(channelId);
615 datas.WriteInt32(channelType);
616 softBusClientStub->OnRemoteRequest(CLIENT_SET_CHANNEL_INFO, datas, reply, option);
617
618 return true;
619 }
620
OnChannelBindInnerTest(const uint8_t * data,size_t size)621 bool OnChannelBindInnerTest(const uint8_t *data, size_t size)
622 {
623 sptr<OHOS::SoftBusClientStub> softBusClientStub = new OHOS::SoftBusClientStub();
624 if (softBusClientStub == nullptr) {
625 return false;
626 }
627
628 int32_t channelId = *(reinterpret_cast<const int32_t *>(data));
629 int32_t channelType = *(reinterpret_cast<const int32_t *>(data));
630
631 MessageParcel datas;
632 MessageParcel reply;
633 MessageOption option;
634 datas.WriteInterfaceToken(SOFTBUS_CLIENT_STUB_INTERFACE_TOKEN);
635 datas.WriteInt32(channelId);
636 datas.WriteInt32(channelType);
637 softBusClientStub->OnRemoteRequest(CLIENT_ON_CHANNEL_BIND, datas, reply, option);
638
639 return true;
640 }
641 } // namespace OHOS
642
643 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)644 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
645 {
646 /* Run your code on data */
647 if (data == nullptr || size < OHOS::U32_AT_SIZE || size > OHOS::FOO_MAX_LEN) {
648 return 0;
649 }
650 uint8_t *dataWithEndCharacter = static_cast<uint8_t *>(SoftBusCalloc(size + 1));
651 if (dataWithEndCharacter == nullptr) {
652 return 0;
653 }
654 if (memcpy_s(dataWithEndCharacter, size, data, size) != EOK) {
655 SoftBusFree(dataWithEndCharacter);
656 return 0;
657 }
658 static OHOS::TestEnv env;
659 if (!env.IsInited()) {
660 return 0;
661 }
662 OHOS::OnChannelOpenedInnerTest(dataWithEndCharacter, size);
663 OHOS::OnChannelOpenFailedInnerTest(data, size);
664 OHOS::OnChannelLinkDownInnerTest(dataWithEndCharacter, size);
665 OHOS::OnChannelClosedInnerTest(data, size);
666 OHOS::OnChannelMsgReceivedInnerTest(data, size);
667 OHOS::OnChannelQosEventInnerTest(data, size);
668 OHOS::OnDeviceFoundInnerTest(data, size);
669 OHOS::OnJoinLNNResultInnerTest(data, size);
670 OHOS::OnJoinMetaNodeResultInnerTest(data, size);
671 OHOS::OnLeaveLNNResultInnerTest(data, size);
672 OHOS::OnLeaveMetaNodeResultInnerTest(data, size);
673 OHOS::OnHichainProofExceptionInnerTest(data, size);
674 OHOS::OnNodeOnlineStateChangedInnerTest(data, size);
675 OHOS::OnNodeBasicInfoChangedInnerTest(data, size);
676 OHOS::OnTimeSyncResultInnerTest(data, size);
677 OHOS::OnPublishLNNResultInnerTest(data, size);
678 OHOS::OnRefreshLNNResultInnerTest(data, size);
679 OHOS::OnRefreshDeviceFoundInnerTest(data, size);
680 OHOS::OnClientPermissonChangeInnerTest(dataWithEndCharacter, size);
681 OHOS::OnDiscoverFailedInnerTest(data, size);
682 OHOS::OnDiscoverySuccessInnerTest(data, size);
683 OHOS::OnPublishSuccessInnerTest(data, size);
684 OHOS::OnPublishFailInnerTest(data, size);
685 OHOS::OnClientTransLimitChangeInnerTest(data, size);
686 OHOS::SetChannelInfoInnerTest(dataWithEndCharacter, size);
687 OHOS::OnChannelBindInnerTest(data, size);
688 SoftBusFree(dataWithEndCharacter);
689 return 0;
690 }
691