1 /*
2  * Copyright (c) 2023-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_connector.h"
19 #include "softbus_session.h"
20 #include "onbytesreceived_fuzzer.h"
21 
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 
OnBytesReceivedFuzzTest(const uint8_t* data, size_t size)55 void OnBytesReceivedFuzzTest(const uint8_t* data, size_t size)
56 {
57     if ((data == nullptr) || (size < sizeof(int))) {
58         return;
59     }
60     int sessionId = *(reinterpret_cast<const int*>(data));
61 
62     std::shared_ptr<SoftbusSession> softbusSession = std::make_shared<SoftbusSession>();
63     softbusSession->RegisterSessionCallback(std::make_shared<SoftbusSessionCallbackTest>());
64     softbusSession->OnBytesReceived(sessionId, data, size);
65 }
66 }
67 }
68 
69 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)70 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
71 {
72     /* Run your code on data */
73     OHOS::DistributedHardware::OnBytesReceivedFuzzTest(data, size);
74 
75     return 0;
76 }
77