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 <cstddef>
19 #include "securec.h"
20 #include <cstdint>
21 
22 #include "distributed_mission_broadcast_listener.h"
23 #include "softbus_adapter/softbus_adapter.h"
24 
25 namespace OHOS {
26 namespace DistributedSchedule {
FuzzSendSoftbusEvent(const uint8_t * data,size_t size)27 void FuzzSendSoftbusEvent(const uint8_t* data, size_t size)
28 {
29     if ((data == nullptr) || (size < sizeof(uint32_t))) {
30         return;
31     }
32     std::shared_ptr<DSchedDataBuffer> buffer = std::make_shared<DSchedDataBuffer>(size);
33     if (memcpy_s(buffer->Data(), buffer->Capacity(), data, size) != ERR_OK) {
34         return;
35     }
36     SoftbusAdapter::GetInstance().SendSoftbusEvent(buffer);
37     SoftbusAdapter::GetInstance().StopSoftbusEvent();
38 }
39 
FuzzOnBroadCastRecv(const uint8_t * data,size_t size)40 void FuzzOnBroadCastRecv(const uint8_t* data, size_t size)
41 {
42     if ((data == nullptr) || (size < sizeof(uint32_t))) {
43         return;
44     }
45     std::string networkId(reinterpret_cast<const char*>(data), size);
46     uint8_t* newdata = const_cast<uint8_t*>(data);
47     uint32_t dataLen = *(reinterpret_cast<const uint32_t*>(data));
48     SoftbusAdapter::GetInstance().OnBroadCastRecv(networkId, newdata, dataLen);
49     std::shared_ptr<SoftbusAdapterListener> listener = std::make_shared<DistributedMissionBroadcastListener>();
50     SoftbusAdapter::GetInstance().RegisterSoftbusEventListener(listener);
51     SoftbusAdapter::GetInstance().UnregisterSoftbusEventListener(listener);
52 }
53 }
54 }
55 
56 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)57 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
58 {
59     OHOS::DistributedSchedule::FuzzSendSoftbusEvent(data, size);
60     OHOS::DistributedSchedule::FuzzOnBroadCastRecv(data, size);
61     return 0;
62 }
63