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