From fec912125764c10377e2e2bb49310a2fb87a70c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E9=9B=B7?= Date: Wed, 30 Jul 2025 09:14:07 +0800 Subject: [PATCH 1/2] add fuzz MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张雷 --- .../deviceprofile/dp_inited_callback.cpp | 2 + .../service/src/hichain/hichain_listener.cpp | 2 + .../dm_datashare_common_event.cpp | 2 + .../service/src/softbus/softbus_listener.cpp | 2 + .../hichain_connector_fuzzer.cpp | 17 +++ .../hichain_listener_fuzzer.cpp | 51 +++++++- .../softbus_connector_fuzzer.cpp | 26 ++++ .../softbus_listener_fuzzer.cpp | 112 ++++++++++++++++++ 8 files changed, 213 insertions(+), 1 deletion(-) diff --git a/services/implementation/src/dependency/deviceprofile/dp_inited_callback.cpp b/services/implementation/src/dependency/deviceprofile/dp_inited_callback.cpp index 8b788d908..7a95da084 100644 --- a/services/implementation/src/dependency/deviceprofile/dp_inited_callback.cpp +++ b/services/implementation/src/dependency/deviceprofile/dp_inited_callback.cpp @@ -59,6 +59,7 @@ int32_t DpInitedCallback::OnDpInited() return DM_OK; } +//LCOV_EXCL_START void DpInitedCallback::PutAllTrustedDevices() { LOGE("In."); @@ -95,6 +96,7 @@ void DpInitedCallback::PutAllTrustedDevices() ret = DeviceProfileConnector::GetInstance().PutAllTrustedDevices(deviceInfos); LOGI("ret:%{public}d", ret); } +//LCOV_EXCL_STOP bool DpInitedCallback::ConvertToTrustedDeviceInfo(const std::unordered_map &authFormMap, const DmDeviceInfo &deviceInfo, DistributedDeviceProfile::TrustedDeviceInfo &trustedDeviceInfo) diff --git a/services/service/src/hichain/hichain_listener.cpp b/services/service/src/hichain/hichain_listener.cpp index ebb72bea9..25e2124dd 100644 --- a/services/service/src/hichain/hichain_listener.cpp +++ b/services/service/src/hichain/hichain_listener.cpp @@ -284,10 +284,12 @@ int32_t HichainListener::GetRelatedGroupsCommon(int32_t userId, const std::strin return DM_OK; } +//LCOV_EXCL_START int64_t HichainListener::GenRequestId() { return GenRandLongLong(MIN_REQUEST_ID, MAX_REQUEST_ID); } +//LCOV_EXCL_STOP int32_t HichainListener::DeleteGroup(const int32_t userId, std::string &groupId) { diff --git a/services/service/src/publishcommonevent/dm_datashare_common_event.cpp b/services/service/src/publishcommonevent/dm_datashare_common_event.cpp index a18715aa2..fbf0fc835 100644 --- a/services/service/src/publishcommonevent/dm_datashare_common_event.cpp +++ b/services/service/src/publishcommonevent/dm_datashare_common_event.cpp @@ -95,6 +95,7 @@ bool DmDataShareCommonEventManager::SubscribeDataShareCommonEvent(const std::vec return true; } +//LCOV_EXCL_START bool DmDataShareCommonEventManager::UnsubscribeDataShareCommonEvent() { std::lock_guard locker(evenSubscriberMutex_); @@ -130,6 +131,7 @@ bool DmDataShareCommonEventManager::UnsubscribeDataShareCommonEvent() eventValidFlag_ = false; return true; } +//LCOV_EXCL_STOP void DmDataShareEventSubscriber::OnReceiveEvent(const CommonEventData &data) { diff --git a/services/service/src/softbus/softbus_listener.cpp b/services/service/src/softbus/softbus_listener.cpp index f834a08e3..9e5643097 100644 --- a/services/service/src/softbus/softbus_listener.cpp +++ b/services/service/src/softbus/softbus_listener.cpp @@ -1261,6 +1261,7 @@ int32_t SoftbusListener::SetForegroundUserIdsToDSoftBus(const std::string &remot return DM_OK; } +//LCOV_EXCL_START void SoftbusListener::DeleteCacheDeviceInfo() { LOGI("start."); @@ -1276,6 +1277,7 @@ void SoftbusListener::DeleteCacheDeviceInfo() DeviceOffLine(it); } } +//LCOV_EXCL_STOP int32_t SoftbusListener::SetLocalDisplayName(const std::string &displayName) { diff --git a/test/commonfuzztest/hichainconnector_fuzzer/hichain_connector_fuzzer.cpp b/test/commonfuzztest/hichainconnector_fuzzer/hichain_connector_fuzzer.cpp index 1fc177894..1a572554a 100644 --- a/test/commonfuzztest/hichainconnector_fuzzer/hichain_connector_fuzzer.cpp +++ b/test/commonfuzztest/hichainconnector_fuzzer/hichain_connector_fuzzer.cpp @@ -405,6 +405,22 @@ void HiChainConnectorSevenhFuzzTest(const uint8_t* data, size_t size) JsonObject jsonDeviceList; hichainConnector->deleteMultiMembers(groupType, usersId, jsonDeviceList); } + +void GetGroupInfoExtFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size < sizeof(int32_t))) { + return; + } + FuzzedDataProvider fdp(data, size); + + int32_t userId = fdp.ConsumeIntegral(); + int32_t maxStringLength = 64; + std::string queryParams = fdp.ConsumeRandomLengthString(maxStringLength); + std::vector groupList; + + std::shared_ptr hichainConnector = std::make_shared(); + hichainConnector->GetGroupInfoExt(userId, queryParams, groupList); +} } } @@ -419,5 +435,6 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) OHOS::DistributedHardware::HiChainConnectorFifthFuzzTest(data, size); OHOS::DistributedHardware::HiChainConnectorSixthFuzzTest(data, size); OHOS::DistributedHardware::HiChainConnectorSevenhFuzzTest(data, size); + OHOS::DistributedHardware::GetGroupInfoExtFuzzTest(data, size); return 0; } \ No newline at end of file diff --git a/test/servicesfuzztest/hichainlistener_fuzzer/hichain_listener_fuzzer.cpp b/test/servicesfuzztest/hichainlistener_fuzzer/hichain_listener_fuzzer.cpp index 0f5936a35..6d91465c2 100644 --- a/test/servicesfuzztest/hichainlistener_fuzzer/hichain_listener_fuzzer.cpp +++ b/test/servicesfuzztest/hichainlistener_fuzzer/hichain_listener_fuzzer.cpp @@ -117,7 +117,7 @@ void DeleteAllGroupFuzzTest(const uint8_t* data, size_t size) std::string localUdid = fdp.ConsumeRandomLengthString(MAX_STRING_LENGTH); - size_t minUserIdCount = 0; + size_t minUserIdCount = 1; size_t maxUserIdCount = 10; size_t userIdCount = fdp.ConsumeIntegralInRange(minUserIdCount, maxUserIdCount); std::vector backgroundUserIds; @@ -170,9 +170,55 @@ void DeleteCredentialFuzzTest(const uint8_t* data, size_t size) std::string credId = fdp.ConsumeRandomLengthString(MAX_STRING_LENGTH); std::shared_ptr hiChainListener = std::make_shared(); + hiChainListener->DeleteCredential(osAccountId, credId); hiChainListener->credManager_ = nullptr; hiChainListener->DeleteCredential(osAccountId, credId); } + +void DeleteGroupExtFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size < sizeof(int32_t))) { + return; + } + FuzzedDataProvider fdp(data, size); + int32_t userId = fdp.ConsumeIntegral(); + std::string groupId = fdp.ConsumeRandomLengthString(MAX_STRING_LENGTH); + + std::shared_ptr hiChainListener = std::make_shared(); + hiChainListener->DeleteGroupExt(userId, groupId); +} + +void DeleteGroupFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size < sizeof(int32_t))) { + return; + } + FuzzedDataProvider fdp(data, size); + int32_t userId = -1; + std::string groupId = fdp.ConsumeRandomLengthString(MAX_STRING_LENGTH); + + std::shared_ptr hiChainListener = std::make_shared(); + hiChainListener->DeleteGroup(userId, groupId); + userId = 1; + hiChainListener->DeleteGroup(userId, groupId); +} + +void GetRelatedGroupsCommonFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size < sizeof(int32_t))) { + return; + } + FuzzedDataProvider fdp(data, size); + int32_t userId = 1; + std::string deviceId = fdp.ConsumeRandomLengthString(MAX_STRING_LENGTH); + std::string pkgName = fdp.ConsumeRandomLengthString(MAX_STRING_LENGTH); + + std::vector groupList; + std::shared_ptr hiChainListener = std::make_shared(); + hiChainListener->GetRelatedGroupsCommon(userId, deviceId, pkgName.c_str(), groupList); + userId = -1; + hiChainListener->GetRelatedGroupsCommon(userId, deviceId, pkgName.c_str(), groupList); +} } } @@ -188,5 +234,8 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) OHOS::DistributedHardware::GetRelatedGroupsFuzzTest(data, size); OHOS::DistributedHardware::GetRelatedGroupsExtFuzzTest(data, size); OHOS::DistributedHardware::DeleteCredentialFuzzTest(data, size); + OHOS::DistributedHardware::DeleteGroupExtFuzzTest(data, size); + OHOS::DistributedHardware::DeleteGroupFuzzTest(data, size); + OHOS::DistributedHardware::GetRelatedGroupsCommonFuzzTest(data, size); return 0; } diff --git a/test/softbusfuzztest/softbusconnector_fuzzer/softbus_connector_fuzzer.cpp b/test/softbusfuzztest/softbusconnector_fuzzer/softbus_connector_fuzzer.cpp index f1d294c57..4c76e5a91 100644 --- a/test/softbusfuzztest/softbusconnector_fuzzer/softbus_connector_fuzzer.cpp +++ b/test/softbusfuzztest/softbusconnector_fuzzer/softbus_connector_fuzzer.cpp @@ -102,6 +102,9 @@ void SoftBusConnectorSecondFuzzTest(const uint8_t* data, size_t size) std::string deviceId = fdp.ConsumeRandomLengthString(); softbusConnector->JoinLNNBySkId(sessionId, sessionKeyId, remoteSessionKeyId, udid, udidHash); softbusConnector->JoinLnnByHml(sessionId, sessionKeyId, remoteSessionKeyId); + sessionKeyId = 1; + remoteSessionKeyId = 2; + softbusConnector->JoinLnnByHml(sessionId, sessionKeyId, remoteSessionKeyId); DeviceInfo deviceInfo; DmDeviceInfo dmDeviceInfo; memcpy_s(deviceInfo.devId, sizeof(deviceInfo.devId), "deviceId", sizeof("deviceId")); @@ -121,6 +124,7 @@ void SoftBusConnectorSecondFuzzTest(const uint8_t* data, size_t size) softbusConnector->ClearChangeProcessInfo(); softbusConnector->DeleteOffLineTimer(udidHash); softbusConnector->CheckIsNeedJoinLnn(udid, deviceId); + softbusConnector->CheckIsNeedJoinLnn("udid", "deviceId"); NodeBasicInfo nodeBasicInfo; memcpy_s(nodeBasicInfo.networkId, sizeof(nodeBasicInfo.networkId), "networkId", sizeof("networkId")); memcpy_s(nodeBasicInfo.deviceName, sizeof(nodeBasicInfo.deviceName), "deviceName", sizeof("deviceName")); @@ -131,6 +135,27 @@ void SoftBusConnectorSecondFuzzTest(const uint8_t* data, size_t size) softbusConnector->SyncLocalAclListProcess(localUserInfo, remoteUserInfo, remoteAclList); softbusConnector->GetAclListHash(localUserInfo, remoteUserInfo, remoteAclList); } + +void AddMemberToDiscoverMapFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size < sizeof(int32_t))) { + return; + } + + FuzzedDataProvider fdp(data, size); + int32_t maxStringLength = 64; + std::string deviceId = fdp.ConsumeRandomLengthString(maxStringLength); + + auto deviceInfo = std::make_shared(); + deviceInfo->addrNum = fdp.ConsumeIntegral(); + deviceInfo->devType = static_cast(fdp.ConsumeIntegral()); + fdp.ConsumeData(deviceInfo->devId, sizeof(deviceInfo->devId)); + fdp.ConsumeData(deviceInfo->devName, sizeof(deviceInfo->devName)); + + std::shared_ptr softbusConnector = std::make_shared(); + softbusConnector->AddMemberToDiscoverMap(deviceId, deviceInfo); + softbusConnector->AddMemberToDiscoverMap("", deviceInfo); +} } } @@ -141,5 +166,6 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) OHOS::DistributedHardware::SoftBusConnectorFuzzTest(data, size); OHOS::DistributedHardware::SoftBusConnectorFirstFuzzTest(data, size); OHOS::DistributedHardware::SoftBusConnectorSecondFuzzTest(data, size); + OHOS::DistributedHardware::AddMemberToDiscoverMapFuzzTest(data, size); return 0; } diff --git a/test/softbusfuzztest/softbuslistener_fuzzer/softbus_listener_fuzzer.cpp b/test/softbusfuzztest/softbuslistener_fuzzer/softbus_listener_fuzzer.cpp index bdce67ed3..dee32cd05 100644 --- a/test/softbusfuzztest/softbuslistener_fuzzer/softbus_listener_fuzzer.cpp +++ b/test/softbusfuzztest/softbuslistener_fuzzer/softbus_listener_fuzzer.cpp @@ -282,6 +282,10 @@ void SoftBusListenerForthFuzzTest(const uint8_t* data, size_t size) std::string extra(reinterpret_cast(data), size); std::vector deviceList; softbusListener_->GetAllTrustedDeviceList(pkgName, extra, deviceList); + TrustChangeType changeType = TrustChangeType::DEVICE_NOT_TRUSTED; + uint32_t msgLen = 0; + char* msg = nullptr; + softbusListener_->OnDeviceTrustedChange(changeType, msg, msgLen); } void SoftBusListenerFifthFuzzTest(const uint8_t* data, size_t size) @@ -310,6 +314,8 @@ void SoftBusListenerFifthFuzzTest(const uint8_t* data, size_t size) softbusListener_->OnGetAclListHash(localDevUserInfo, remoteDevUserInfo, aclList); uint32_t deviceListLen = static_cast(deviceList.length()); softbusListener_->OnCredentialAuthStatus(deviceList.data(), deviceListLen, deviceTypeId, errcode); + deviceListLen = 2001; + softbusListener_->OnCredentialAuthStatus(deviceList.data(), deviceListLen, deviceTypeId, errcode); std::shared_ptr info = nullptr; softbusListener_->UpdateDeviceName(info.get()); info = std::make_shared(); @@ -394,6 +400,106 @@ void SoftBusListenerSeventhFuzzTest(const uint8_t* data, size_t size) DmDeviceBasicInfo dmdevInfo; softbusListener_->ConvertNodeBasicInfoToDmDevice(nodeBasicInfo, dmdevInfo); } + +void PutOstypeDataFuzzTest(const uint8_t* data, size_t size) +{ + int32_t maxStringLength = 64; + if ((data == nullptr) || (size < sizeof(int32_t) + maxStringLength)) { + return; + } + + FuzzedDataProvider fdp(data, size); + std::string peerUdid = fdp.ConsumeRandomLengthString(maxStringLength); + int32_t osType = fdp.ConsumeIntegral(); + softbusListener_->PutOstypeData(peerUdid, osType); +} + +void CacheDeviceInfoFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size < sizeof(int32_t))) { + return; + } + + FuzzedDataProvider fdp(data, size); + + std::string deviceId = "deviceId"; + auto deviceInfo = std::make_shared(); + deviceInfo->addrNum = 1; + softbusListener_->CacheDeviceInfo(deviceId, deviceInfo); + + deviceInfo->addr[0].type = static_cast(fdp.ConsumeIntegral()); + fdp.ConsumeData(deviceInfo->addr[0].info.ip.ip, sizeof(deviceInfo->addr[0].info.ip.ip)); + deviceInfo->addr[0].info.ip.port = fdp.ConsumeIntegral(); + softbusListener_->CacheDeviceInfo(deviceId, deviceInfo); + deviceInfo->addrNum = 0; + softbusListener_->CacheDeviceInfo(deviceId, deviceInfo); + softbusListener_->CacheDeviceInfo("", deviceInfo); +} + +void GetUuidByNetworkIdFuzzTest(const uint8_t* data, size_t size) +{ + int32_t maxStringLength = 64; + if ((data == nullptr) || (size < maxStringLength)) { + return; + } + + FuzzedDataProvider fdp(data, size); + std::string networkId = fdp.ConsumeRandomLengthString(maxStringLength); + std::string uuid = ""; + softbusListener_->GetUuidByNetworkId(networkId.c_str(), uuid); +} + +void SetHostPkgNameFuzzTest(const uint8_t* data, size_t size) +{ + int32_t maxStringLength = 64; + if ((data == nullptr) || (size < maxStringLength)) { + return; + } + FuzzedDataProvider fdp(data, size); + + std::string hostName = fdp.ConsumeRandomLengthString(maxStringLength); + softbusListener_->SetHostPkgName(hostName); +} + +void GetAttrFromCustomDataFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size < sizeof(int32_t))) { + return; + } + FuzzedDataProvider fdp(data, size); + + const char* jsonString = R"({ + "MsgType": "0", + "userId": "12345", + "accountId": "a******3", + "peerUdids": ["u******1", "u******2"], + "peerUdid": "p******d", + "accountName": "t******t", + "syncUserIdFlag": 1, + "userIds": [ + {"type": 1, "userId": 111}, + {"type": 0, "userId": 222} + ] + })"; + cJSON *jsonObject = cJSON_Parse(jsonString); + DmDeviceInfo dmDevInfo; + int32_t actionId = fdp.ConsumeIntegral(); + + softbusListener_->GetAttrFromCustomData(jsonObject, dmDevInfo, actionId); + cJSON_Delete(jsonObject); +} + +void ConvertOsTypeToJsonFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size < sizeof(int32_t))) { + return; + } + FuzzedDataProvider fdp(data, size); + + int32_t osType = fdp.ConsumeIntegral(); + std::string osTypeStr = ""; + softbusListener_->ConvertOsTypeToJson(osType, osTypeStr); +} } } @@ -409,5 +515,11 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) OHOS::DistributedHardware::SoftBusListenerFifthFuzzTest(data, size); OHOS::DistributedHardware::SoftBusListenerSixthFuzzTest(data, size); OHOS::DistributedHardware::SoftBusListenerSeventhFuzzTest(data, size); + OHOS::DistributedHardware::PutOstypeDataFuzzTest(data, size); + OHOS::DistributedHardware::CacheDeviceInfoFuzzTest(data, size); + OHOS::DistributedHardware::GetUuidByNetworkIdFuzzTest(data, size); + OHOS::DistributedHardware::SetHostPkgNameFuzzTest(data, size); + OHOS::DistributedHardware::GetAttrFromCustomDataFuzzTest(data, size); + OHOS::DistributedHardware::ConvertOsTypeToJsonFuzzTest(data, size); return 0; } -- Gitee From 6e3d1d2c084373a36120bb755d1746bab6c29164 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E9=9B=B7?= Date: Wed, 30 Jul 2025 09:35:23 +0800 Subject: [PATCH 2/2] modify fuzz MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张雷 --- .../softbusconnector_fuzzer/softbus_connector_fuzzer.cpp | 6 ++++-- .../softbuslistener_fuzzer/softbus_listener_fuzzer.cpp | 4 +++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/test/softbusfuzztest/softbusconnector_fuzzer/softbus_connector_fuzzer.cpp b/test/softbusfuzztest/softbusconnector_fuzzer/softbus_connector_fuzzer.cpp index 4c76e5a91..42008ec69 100644 --- a/test/softbusfuzztest/softbusconnector_fuzzer/softbus_connector_fuzzer.cpp +++ b/test/softbusfuzztest/softbusconnector_fuzzer/softbus_connector_fuzzer.cpp @@ -102,8 +102,10 @@ void SoftBusConnectorSecondFuzzTest(const uint8_t* data, size_t size) std::string deviceId = fdp.ConsumeRandomLengthString(); softbusConnector->JoinLNNBySkId(sessionId, sessionKeyId, remoteSessionKeyId, udid, udidHash); softbusConnector->JoinLnnByHml(sessionId, sessionKeyId, remoteSessionKeyId); - sessionKeyId = 1; - remoteSessionKeyId = 2; + int32_t tempSessionKeyId = 1; + int32_t tempRemoteSessionKeyId = 2; + sessionKeyId = tempSessionKeyId; + remoteSessionKeyId = tempRemoteSessionKeyId; softbusConnector->JoinLnnByHml(sessionId, sessionKeyId, remoteSessionKeyId); DeviceInfo deviceInfo; DmDeviceInfo dmDeviceInfo; diff --git a/test/softbusfuzztest/softbuslistener_fuzzer/softbus_listener_fuzzer.cpp b/test/softbusfuzztest/softbuslistener_fuzzer/softbus_listener_fuzzer.cpp index dee32cd05..d44b25b5f 100644 --- a/test/softbusfuzztest/softbuslistener_fuzzer/softbus_listener_fuzzer.cpp +++ b/test/softbusfuzztest/softbuslistener_fuzzer/softbus_listener_fuzzer.cpp @@ -31,6 +31,7 @@ namespace { constexpr int32_t INT32NUM = 3; constexpr int32_t DATA_LEN = 20; constexpr int32_t CONNECTION_ADDR_USB_VALUE = 5; + constexpr uint32_t MAX_DEVICE_LIST_LENGTH = 2001; } class ISoftbusDiscoveringCallbackTest : public ISoftbusDiscoveringCallback { @@ -314,7 +315,8 @@ void SoftBusListenerFifthFuzzTest(const uint8_t* data, size_t size) softbusListener_->OnGetAclListHash(localDevUserInfo, remoteDevUserInfo, aclList); uint32_t deviceListLen = static_cast(deviceList.length()); softbusListener_->OnCredentialAuthStatus(deviceList.data(), deviceListLen, deviceTypeId, errcode); - deviceListLen = 2001; + + deviceListLen = MAX_DEVICE_LIST_LENGTH; softbusListener_->OnCredentialAuthStatus(deviceList.data(), deviceListLen, deviceTypeId, errcode); std::shared_ptr info = nullptr; softbusListener_->UpdateDeviceName(info.get()); -- Gitee