1 /*
2 * Copyright (c) 2022-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
17 #include "device_manager_service_listener.h"
18 #include "softbus_bus_center.h"
19 #include "softbus_session.h"
20 #include "softbus_connector.h"
21 #include "softbus_session_object_fuzzer.h"
22 namespace OHOS {
23 namespace DistributedHardware {
24
25 class SoftbusSessionCallbackTest : public ISoftbusSessionCallback {
26 public:
SoftbusSessionCallbackTest()27 SoftbusSessionCallbackTest() {}
~SoftbusSessionCallbackTest()28 virtual ~SoftbusSessionCallbackTest() {}
OnSessionOpened(int32_t sessionId,int32_t sessionSide,int32_t result)29 void OnSessionOpened(int32_t sessionId, int32_t sessionSide, int32_t result) override
30 {
31 (void)sessionId;
32 (void)sessionSide;
33 (void)result;
34 }
OnSessionClosed(int32_t sessionId)35 void OnSessionClosed(int32_t sessionId) override
36 {
37 (void)sessionId;
38 }
OnDataReceived(int32_t sessionId,std::string message)39 void OnDataReceived(int32_t sessionId, std::string message) override
40 {
41 (void)sessionId;
42 (void)message;
43 }
GetIsCryptoSupport()44 bool GetIsCryptoSupport() override
45 {
46 return true;
47 }
OnUnbindSessionOpened(int32_t socket,PeerSocketInfo info)48 void OnUnbindSessionOpened(int32_t socket, PeerSocketInfo info) override
49 {
50 (void)socket;
51 (void)info;
52 }
OnAuthDeviceDataReceived(int32_t sessionId,std::string message)53 void OnAuthDeviceDataReceived(int32_t sessionId, std::string message) override
54 {
55 (void)sessionId;
56 (void)message;
57 }
BindSocketSuccess(int32_t socket)58 void BindSocketSuccess(int32_t socket) override
59 {
60 (void)socket;
61 }
BindSocketFail()62 void BindSocketFail() override {}
63 };
64
SoftBusSessionFuzzTest(const uint8_t * data,size_t size)65 void SoftBusSessionFuzzTest(const uint8_t* data, size_t size)
66 {
67 if ((data == nullptr) || (size < sizeof(int32_t))) {
68 return;
69 }
70
71 int32_t socket = *(reinterpret_cast<const int*>(data));
72 QoSEvent eventId = static_cast<QoSEvent>(1);
73 uint32_t qosCount = 3;
74 QosTV qos[] = {
75 { .qos = QOS_TYPE_MIN_BW, .value = 64 * 1024 },
76 { .qos = QOS_TYPE_MAX_LATENCY, .value = 19000},
77 { .qos = QOS_TYPE_MIN_LATENCY, .value = 500 },
78 };
79 ShutdownReason reason = ShutdownReason::SHUTDOWN_REASON_UNKNOWN;
80
81 std::shared_ptr<SoftbusSession> softbusSession = std::make_shared<SoftbusSession>();
82 softbusSession->RegisterSessionCallback(std::make_shared<SoftbusSessionCallbackTest>());
83 softbusSession->iSocketListener_.OnBytes(socket, data, size);
84 softbusSession->iSocketListener_.OnShutdown(socket, reason);
85 softbusSession->iSocketListener_.OnQos(socket, eventId, qos, qosCount);
86 }
87 }
88 }
89
90 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)91 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
92 {
93 /* Run your code on data */
94 OHOS::DistributedHardware::SoftBusSessionFuzzTest(data, size);
95
96 return 0;
97 }
98