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 "device_manager_service_listener.h"
17 #include "softbus_bus_center.h"
18 #include "softbus_adapter.h"
19 #include "softbus_connector.h"
20 #include "softbus_adapter_object_fuzzer.h"
21 
22 namespace OHOS {
23 namespace DistributedHardware {
SoftbusAdapterFuzzTest(const uint8_t * data,size_t size)24 void SoftbusAdapterFuzzTest(const uint8_t* data, size_t size)
25 {
26     if ((data == nullptr) || (size < sizeof(uint32_t))) {
27         return;
28     }
29 
30     int32_t socket =  *(reinterpret_cast<const int*>(data));
31     std::string str(reinterpret_cast<const char*>(data), size);
32     uint32_t qosCount = 3;
33     StreamData streamData;
34     StreamData ext;
35     StreamFrameInfo frameInfo;
36     QoSEvent eventId = static_cast<QoSEvent>(1);
37     QosTV qos[] = {
38         { .qos = QOS_TYPE_MIN_BW, .value = 64 * 1024 },
39         { .qos = QOS_TYPE_MAX_LATENCY, .value = 19000 },
40         { .qos = QOS_TYPE_MIN_LATENCY, .value = 500 },
41     };
42     ShutdownReason reason = ShutdownReason::SHUTDOWN_REASON_UNKNOWN;
43     PeerSocketInfo info = {
44         .name = const_cast<char*>(str.c_str()),
45         .pkgName = const_cast<char*>(str.c_str()),
46         .networkId = const_cast<char*>(str.c_str()),
47     };
48 
49     SoftbusAdapter::GetInstance().iSocketListener_.OnBind(socket, info);
50     SoftbusAdapter::GetInstance().iSocketListener_.OnShutdown(socket, reason);
51     SoftbusAdapter::GetInstance().iSocketListener_.OnBytes(socket, data, size);
52     SoftbusAdapter::GetInstance().iSocketListener_.OnMessage(socket, data, size);
53     SoftbusAdapter::GetInstance().iSocketListener_.OnStream(socket, &streamData, &ext, &frameInfo);
54     SoftbusAdapter::GetInstance().iSocketListener_.OnQos(socket, eventId, qos, qosCount);
55 }
56 }
57 }
58 
59 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)60 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
61 {
62     /* Run your code on data */
63     OHOS::DistributedHardware::SoftbusAdapterFuzzTest(data, size);
64 
65     return 0;
66 }
67