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() {}
29 void OnSessionOpened(int32_t sessionId, int32_t sessionSide, int32_t result) override
30 {
31 (void)sessionId;
32 (void)sessionSide;
33 (void)result;
34 }
35 void OnSessionClosed(int32_t sessionId) override
36 {
37 (void)sessionId;
38 }
39 void OnDataReceived(int32_t sessionId, std::string message) override
40 {
41 (void)sessionId;
42 (void)message;
43 }
44 bool GetIsCryptoSupport() override
45 {
46 return true;
47 }
48 void OnAuthDeviceDataReceived(int32_t sessionId, std::string message) override
49 {
50 (void)sessionId;
51 (void)message;
52 }
53 };
54
SoftBusSessionFuzzTest(const uint8_t* data, size_t size)55 void SoftBusSessionFuzzTest(const uint8_t* data, size_t size)
56 {
57 if ((data == nullptr) || (size < sizeof(int32_t))) {
58 return;
59 }
60
61 int32_t socket = *(reinterpret_cast<const int*>(data));
62 QoSEvent eventId = static_cast<QoSEvent>(1);
63 uint32_t qosCount = 3;
64 QosTV qos[] = {
65 { .qos = QOS_TYPE_MIN_BW, .value = 64 * 1024 },
66 { .qos = QOS_TYPE_MAX_LATENCY, .value = 19000},
67 { .qos = QOS_TYPE_MIN_LATENCY, .value = 500 },
68 };
69 ShutdownReason reason = ShutdownReason::SHUTDOWN_REASON_UNKNOWN;
70
71 std::shared_ptr<SoftbusSession> softbusSession = std::make_shared<SoftbusSession>();
72 softbusSession->RegisterSessionCallback(std::make_shared<SoftbusSessionCallbackTest>());
73 softbusSession->iSocketListener_.OnBytes(socket, data, size);
74 softbusSession->iSocketListener_.OnShutdown(socket, reason);
75 softbusSession->iSocketListener_.OnQos(socket, eventId, qos, qosCount);
76 }
77 }
78 }
79
80 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)81 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
82 {
83 /* Run your code on data */
84 OHOS::DistributedHardware::SoftBusSessionFuzzTest(data, size);
85
86 return 0;
87 }
88