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 #include "softbus_connector.h"
17 #include "softbus_bus_center.h"
18 #include "dm_device_info.h"
19 #include "dm_publish_info.h"
20 #include "dm_subscribe_info.h"
21 #include "softbus_session.h"
22 #include "softbus_session_fuzzer.h"
23
24 namespace OHOS {
25 namespace DistributedHardware {
26
27 class SoftbusSessionCallbackTest : public ISoftbusSessionCallback {
28 public:
SoftbusSessionCallbackTest()29 SoftbusSessionCallbackTest() {}
~SoftbusSessionCallbackTest()30 virtual ~SoftbusSessionCallbackTest() {}
OnSessionOpened(int32_t sessionId,int32_t sessionSide,int32_t result)31 void OnSessionOpened(int32_t sessionId, int32_t sessionSide, int32_t result) override
32 {
33 (void)sessionId;
34 (void)sessionSide;
35 (void)result;
36 }
OnSessionClosed(int32_t sessionId)37 void OnSessionClosed(int32_t sessionId) override
38 {
39 (void)sessionId;
40 }
OnDataReceived(int32_t sessionId,std::string message)41 void OnDataReceived(int32_t sessionId, std::string message) override
42 {
43 (void)sessionId;
44 (void)message;
45 }
GetIsCryptoSupport()46 bool GetIsCryptoSupport() override
47 {
48 return true;
49 }
OnUnbindSessionOpened(int32_t socket,PeerSocketInfo info)50 void OnUnbindSessionOpened(int32_t socket, PeerSocketInfo info) override
51 {
52 (void)socket;
53 (void)info;
54 }
OnAuthDeviceDataReceived(int32_t sessionId,std::string message)55 void OnAuthDeviceDataReceived(int32_t sessionId, std::string message) override
56 {
57 (void)sessionId;
58 (void)message;
59 }
BindSocketSuccess(int32_t socket)60 void BindSocketSuccess(int32_t socket) override
61 {
62 (void)socket;
63 }
BindSocketFail()64 void BindSocketFail() override {}
65 };
66
SoftBusSessionFuzzTest(const uint8_t * data,size_t size)67 void SoftBusSessionFuzzTest(const uint8_t* data, size_t size)
68 {
69 if ((data == nullptr) || (size < sizeof(int32_t))) {
70 return;
71 }
72
73 int result = *(reinterpret_cast<const int*>(data));
74 int32_t sessionId = *(reinterpret_cast<const int32_t*>(data));
75 std::string str(reinterpret_cast<const char*>(data), size);
76 PeerSocketInfo info;
77 std::shared_ptr<SoftbusSession> softbusSession = std::make_shared<SoftbusSession>();
78
79 softbusSession->RegisterSessionCallback(std::make_shared<SoftbusSessionCallbackTest>());
80 softbusSession->OnSessionOpened(result, result);
81 softbusSession->OpenAuthSession(str);
82 softbusSession->CloseAuthSession(sessionId);
83 softbusSession->OnBytesReceived(result, str.c_str(), str.size());
84 softbusSession->OnUnbindSessionOpened(sessionId, info);
85 softbusSession->OpenUnbindSession(str);
86 softbusSession->CloseUnbindSession(sessionId);
87 softbusSession->GetPeerDeviceId(sessionId, str);
88 softbusSession->SendData(sessionId, str);
89 softbusSession->SendHeartbeatData(sessionId, str);
90 softbusSession->OnSessionClosed(result);
91 softbusSession->UnRegisterSessionCallback();
92 }
93 }
94 }
95
96 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)97 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
98 {
99 /* Run your code on data */
100 OHOS::DistributedHardware::SoftBusSessionFuzzTest(data, size);
101
102 return 0;
103 }
104