• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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_publish_manager.h"
21 #include "dm_subscribe_info.h"
22 #include "softbus_discovery_callback.h"
23 #include "softbus_publish_callback.h"
24 #include "softbus_session.h"
25 #include "softbus_state_callback.h"
26 #include "softbus_connector_register_fuzzer.h"
27 
28 namespace OHOS {
29 namespace DistributedHardware {
30 class SoftbusStateCallbackFuzzTest : public ISoftbusStateCallback {
31 public:
~SoftbusStateCallbackFuzzTest()32     virtual ~SoftbusStateCallbackFuzzTest() {}
33 
OnDeviceOnline(const std::string & pkgName,DmDeviceInfo & info)34     void OnDeviceOnline(const std::string &pkgName, DmDeviceInfo &info) override {}
OnDeviceOffline(const std::string & pkgName,const DmDeviceInfo & info)35     void OnDeviceOffline(const std::string &pkgName, const DmDeviceInfo &info) override {}
OnDeviceChanged(const std::string & pkgName,const DmDeviceInfo & info)36     void OnDeviceChanged(const std::string &pkgName, const DmDeviceInfo &info) override {}
OnDeviceReady(const std::string & pkgName,const DmDeviceInfo & info)37     void OnDeviceReady(const std::string &pkgName, const DmDeviceInfo &info) override {}
38 };
39 
40 class SoftbusDiscoveryCallbackFuzzTest : public ISoftbusDiscoveryCallback {
41 public:
~SoftbusDiscoveryCallbackFuzzTest()42     virtual ~SoftbusDiscoveryCallbackFuzzTest() {}
43 
OnDeviceFound(const std::string & pkgName,DmDeviceInfo & info,bool isOnline)44     void OnDeviceFound(const std::string &pkgName, DmDeviceInfo &info, bool isOnline) override {}
OnDiscoverySuccess(const std::string & pkgName,int32_t subscribeId)45     void OnDiscoverySuccess(const std::string &pkgName, int32_t subscribeId) override {}
OnDiscoveryFailed(const std::string & pkgName,int32_t subscribeId,int32_t failedReason)46     void OnDiscoveryFailed(const std::string &pkgName, int32_t subscribeId, int32_t failedReason) override {}
47 };
48 
49 class SoftbusPublishCallbackFuzzTest : public ISoftbusPublishCallback {
50 public:
~SoftbusPublishCallbackFuzzTest()51     virtual ~SoftbusPublishCallbackFuzzTest() {}
52 
OnPublishResult(const std::string & pkgName,int32_t publishId,int32_t publishResult)53     void OnPublishResult(const std::string &pkgName, int32_t publishId, int32_t publishResult) override {}
54 };
55 
SoftBusConnectorRegisterFuzzTest(const uint8_t * data,size_t size)56 void SoftBusConnectorRegisterFuzzTest(const uint8_t* data, size_t size)
57 {
58     if ((data == nullptr) || (size == 0)) {
59         return;
60     }
61 
62     std::string pkgName(reinterpret_cast<const char*>(data), size);
63     std::shared_ptr<ISoftbusPublishCallback> pubishCallback = std::make_shared<SoftbusPublishCallbackFuzzTest>();
64     std::shared_ptr<ISoftbusDiscoveryCallback> discoveryCallback = std::make_shared<SoftbusDiscoveryCallbackFuzzTest>();
65     std::shared_ptr<ISoftbusStateCallback> stateCallback = std::make_shared<SoftbusStateCallbackFuzzTest>();
66     std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
67 
68     softbusConnector->RegisterSoftbusStateCallback(pkgName, stateCallback);
69     softbusConnector->UnRegisterSoftbusStateCallback(pkgName);
70     softbusConnector->RegisterSoftbusDiscoveryCallback(pkgName, discoveryCallback);
71     softbusConnector->UnRegisterSoftbusDiscoveryCallback(pkgName);
72     softbusConnector->RegisterSoftbusPublishCallback(pkgName, pubishCallback);
73     softbusConnector->UnRegisterSoftbusPublishCallback(pkgName);
74 }
75 }
76 }
77 
78 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)79 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
80 {
81     /* Run your code on data */
82     OHOS::DistributedHardware::SoftBusConnectorRegisterFuzzTest(data, size);
83 
84     return 0;
85 }
86