1 /*
2 * Copyright (c) 2022-2025 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_fuzzer.h"
17
18 #include <fuzzer/FuzzedDataProvider.h>
19
20 #include "dm_device_info.h"
21 #include "dm_publish_info.h"
22 #include "dm_subscribe_info.h"
23 #include "softbus_bus_center.h"
24 #include "softbus_connector.h"
25 #include "softbus_session.h"
26
27 namespace OHOS {
28 namespace DistributedHardware {
29
SoftBusConnectorFuzzTest(const uint8_t * data,size_t size)30 void SoftBusConnectorFuzzTest(const uint8_t* data, size_t size)
31 {
32 if ((data == nullptr) || (size < sizeof(int32_t) + sizeof(int) + sizeof(int32_t))) {
33 return;
34 }
35 FuzzedDataProvider fdp(data, size);
36 ConnectionAddr *addr = nullptr;
37 const char *networkId = reinterpret_cast<const char*>(data);
38 int32_t result = fdp.ConsumeIntegral<int32_t>();
39 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
40 softbusConnector->OnSoftbusJoinLNNResult(addr, networkId, result);
41 }
42
SoftBusConnectorFirstFuzzTest(const uint8_t * data,size_t size)43 void SoftBusConnectorFirstFuzzTest(const uint8_t* data, size_t size)
44 {
45 if ((data == nullptr) || (size < sizeof(int32_t))) {
46 return;
47 }
48 FuzzedDataProvider fdp(data, size);
49 int32_t userId = fdp.ConsumeIntegral<int32_t>();
50 std::string credId = fdp.ConsumeRandomLengthString();
51 int32_t sessionKeyId = fdp.ConsumeIntegral<int32_t>();
52 int32_t aclId = fdp.ConsumeIntegral<int32_t>();
53 std::string localUdid = "localDeviceId";
54 std::string remoteUdid = "remoteDeviceId";
55 DistributedDeviceProfile::AccessControlProfile localAcl;
56 std::vector<std::string> acLStrList;
57 std::string jsonString = fdp.ConsumeRandomLengthString();
58 std::vector<AclHashItem> remoteAllAclList;
59 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
60 softbusConnector->SyncAclList(userId, credId, sessionKeyId, aclId);
61 DistributedDeviceProfile::Accesser acer;
62 DistributedDeviceProfile::Accessee acee;
63 acee.SetAccesseeDeviceId("localDeviceId");
64 acer.SetAccesserDeviceId("remoteDeviceId");
65 localAcl.SetTrustDeviceId("remoteDeviceId");
66 localAcl.SetAccesser(acer);
67 localAcl.SetAccessee(acee);
68 softbusConnector->SyncLocalAclList5_1_0(localUdid, remoteUdid, localAcl, acLStrList);
69 acer.SetAccesserDeviceId("localDeviceId");
70 acee.SetAccesseeDeviceId("remoteDeviceId");
71 localAcl.SetTrustDeviceId("remoteDeviceId");
72 localAcl.SetAccesser(acer);
73 localAcl.SetAccessee(acee);
74 softbusConnector->SyncLocalAclList5_1_0(localUdid, remoteUdid, localAcl, acLStrList);
75 softbusConnector->ParaseAclChecksumList(jsonString, remoteAllAclList);
76 std::string localVersion = fdp.ConsumeRandomLengthString();
77 softbusConnector->GetLocalVersion(localUdid, remoteUdid, localVersion, localAcl);
78 std::vector<std::string> aclVerDesc;
79 std::map<std::string, AclHashItem> remoteAllAclMap;
80 softbusConnector->SortAclListDesc(remoteAllAclList, aclVerDesc, remoteAllAclMap);
81 std::vector<std::string> remoteVerDesc;
82 softbusConnector->MatchTargetVersion(localVersion, remoteVerDesc);
83 remoteVerDesc.push_back(fdp.ConsumeRandomLengthString());
84 softbusConnector->MatchTargetVersion(localVersion, remoteVerDesc);
85 std::string deviceId = fdp.ConsumeRandomLengthString();
86 std::string remoteUdidHash = fdp.ConsumeRandomLengthString();
87 softbusConnector->JoinLnn(deviceId, remoteUdidHash);
88 }
89
SoftBusConnectorSecondFuzzTest(const uint8_t * data,size_t size)90 void SoftBusConnectorSecondFuzzTest(const uint8_t* data, size_t size)
91 {
92 if ((data == nullptr) || (size < sizeof(int32_t))) {
93 return;
94 }
95 FuzzedDataProvider fdp(data, size);
96 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
97 int32_t sessionId = fdp.ConsumeIntegral<int32_t>();
98 int32_t sessionKeyId = fdp.ConsumeIntegral<int32_t>();
99 std::string udid = fdp.ConsumeRandomLengthString();
100 std::string udidHash = fdp.ConsumeRandomLengthString();
101 int32_t remoteSessionKeyId = fdp.ConsumeIntegral<int32_t>();
102 std::string deviceId = fdp.ConsumeRandomLengthString();
103 softbusConnector->JoinLNNBySkId(sessionId, sessionKeyId, remoteSessionKeyId, udid, udidHash);
104 softbusConnector->JoinLnnByHml(sessionId, sessionKeyId, remoteSessionKeyId);
105 int32_t tempSessionKeyId = 1;
106 int32_t tempRemoteSessionKeyId = 2;
107 sessionKeyId = tempSessionKeyId;
108 remoteSessionKeyId = tempRemoteSessionKeyId;
109 softbusConnector->JoinLnnByHml(sessionId, sessionKeyId, remoteSessionKeyId);
110 DeviceInfo deviceInfo;
111 DmDeviceInfo dmDeviceInfo;
112 memcpy_s(deviceInfo.devId, sizeof(deviceInfo.devId), "deviceId", sizeof("deviceId"));
113 memcpy_s(deviceInfo.devName, sizeof(deviceInfo.devName), "deviceName", sizeof("deviceName"));
114 softbusConnector->ConvertDeviceInfoToDmDevice(deviceInfo, dmDeviceInfo);
115 DmDeviceBasicInfo dmDeviceBasicInfo;
116 softbusConnector->ConvertDeviceInfoToDmDevice(deviceInfo, dmDeviceBasicInfo);
117 softbusConnector->GetLocalDeviceNetworkId();
118 ProcessInfo processInfo;
119 softbusConnector->SetProcessInfo(processInfo);
120 std::vector<ProcessInfo> processInfoVec;
121 softbusConnector->SetProcessInfoVec(processInfoVec);
122 softbusConnector->GetProcessInfo();
123 softbusConnector->ClearProcessInfo();
124 softbusConnector->SetChangeProcessInfo(processInfo);
125 softbusConnector->GetChangeProcessInfo();
126 softbusConnector->ClearChangeProcessInfo();
127 softbusConnector->DeleteOffLineTimer(udidHash);
128 softbusConnector->CheckIsNeedJoinLnn(udid, deviceId);
129 softbusConnector->CheckIsNeedJoinLnn("udid", "deviceId");
130 NodeBasicInfo nodeBasicInfo;
131 memcpy_s(nodeBasicInfo.networkId, sizeof(nodeBasicInfo.networkId), "networkId", sizeof("networkId"));
132 memcpy_s(nodeBasicInfo.deviceName, sizeof(nodeBasicInfo.deviceName), "deviceName", sizeof("deviceName"));
133 softbusConnector->ConvertNodeBasicInfoToDmDevice(nodeBasicInfo, dmDeviceInfo);
134 DevUserInfo localUserInfo;
135 DevUserInfo remoteUserInfo;
136 std::string remoteAclList = fdp.ConsumeRandomLengthString();
137 softbusConnector->SyncLocalAclListProcess(localUserInfo, remoteUserInfo, remoteAclList);
138 softbusConnector->GetAclListHash(localUserInfo, remoteUserInfo, remoteAclList);
139 }
140
AddMemberToDiscoverMapFuzzTest(const uint8_t * data,size_t size)141 void AddMemberToDiscoverMapFuzzTest(const uint8_t* data, size_t size)
142 {
143 if ((data == nullptr) || (size < sizeof(int32_t))) {
144 return;
145 }
146
147 FuzzedDataProvider fdp(data, size);
148 int32_t maxStringLength = 64;
149 std::string deviceId = fdp.ConsumeRandomLengthString(maxStringLength);
150
151 auto deviceInfo = std::make_shared<DeviceInfo>();
152 deviceInfo->addrNum = fdp.ConsumeIntegral<int32_t>();
153 deviceInfo->devType = static_cast<DeviceType>(fdp.ConsumeIntegral<int32_t>());
154 fdp.ConsumeData(deviceInfo->devId, sizeof(deviceInfo->devId));
155 fdp.ConsumeData(deviceInfo->devName, sizeof(deviceInfo->devName));
156
157 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
158 softbusConnector->AddMemberToDiscoverMap(deviceId, deviceInfo);
159 softbusConnector->AddMemberToDiscoverMap("", deviceInfo);
160 }
161 }
162 }
163
164 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)165 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
166 {
167 /* Run your code on data */
168 OHOS::DistributedHardware::SoftBusConnectorFuzzTest(data, size);
169 OHOS::DistributedHardware::SoftBusConnectorFirstFuzzTest(data, size);
170 OHOS::DistributedHardware::SoftBusConnectorSecondFuzzTest(data, size);
171 OHOS::DistributedHardware::AddMemberToDiscoverMapFuzzTest(data, size);
172 return 0;
173 }
174