/* * Copyright (c) 2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include #include #include #include #include #include "dm_comm_tool_fuzzer.h" #include "dm_comm_tool.h" namespace OHOS { namespace DistributedHardware { namespace { constexpr uint32_t DATA_LEN = 10; } std::shared_ptr dmCommToolPtr_ = std::make_shared(); void DmCommToolFuzzTest(const uint8_t* data, size_t size) { if ((data == nullptr) || (size < sizeof(int32_t))) { return; } FuzzedDataProvider fdp(data, size); int32_t socketId = fdp.ConsumeIntegral(); std::string rmtNetworkId(reinterpret_cast(data), size); dmCommToolPtr_->Init(); dmCommToolPtr_->GetInstance(); dmCommToolPtr_->UnInit(); std::vector foregroundUserIds; foregroundUserIds.push_back(DATA_LEN); std::vector backgroundUserIds; dmCommToolPtr_->SendUserIds(rmtNetworkId, foregroundUserIds, backgroundUserIds); dmCommToolPtr_->RspLocalFrontOrBackUserIds(rmtNetworkId, foregroundUserIds, backgroundUserIds, socketId); rmtNetworkId = ""; dmCommToolPtr_->SendUserIds(rmtNetworkId, foregroundUserIds, backgroundUserIds); std::string remoteNetworkId(reinterpret_cast(data), size); std::shared_ptr commMsg = std::make_shared(); std::shared_ptr innerCommMsg = std::make_shared(remoteNetworkId, commMsg, socketId); dmCommToolPtr_->ProcessReceiveUserIdsEvent(innerCommMsg); dmCommToolPtr_->ProcessResponseUserIdsEvent(innerCommMsg); dmCommToolPtr_->ProcessReceiveCommonEvent(innerCommMsg); dmCommToolPtr_->ProcessResponseCommonEvent(innerCommMsg); dmCommToolPtr_->ProcessReceiveUninstAppEvent(innerCommMsg); dmCommToolPtr_->ProcessReceiveUnBindAppEvent(innerCommMsg); dmCommToolPtr_->ProcessReceiveRspAppUninstallEvent(innerCommMsg); dmCommToolPtr_->ProcessReceiveRspAppUnbindEvent(innerCommMsg); dmCommToolPtr_->GetDMTransportPtr(); dmCommToolPtr_->GetEventHandler(); } void DmCommToolFirstFuzzTest(const uint8_t* data, size_t size) { if ((data == nullptr) || (size < sizeof(int32_t))) { return; } FuzzedDataProvider fdp(data, size); DMCommTool::DMCommToolEventHandler dmCommToolEventHandler( AppExecFwk::EventRunner::Create(FUZZ_PROJECT_NAME), dmCommToolPtr_); int32_t socketId = fdp.ConsumeIntegral(); std::string rmtNetworkId = fdp.ConsumeRandomLengthString(); int32_t code = fdp.ConsumeIntegral(); std::string msg = fdp.ConsumeRandomLengthString(); std::shared_ptr commMsg = std::make_shared(code, msg); std::shared_ptr innrCommMsg = std::make_shared(rmtNetworkId, commMsg, socketId); UserIdsMsg userIdsMsg; userIdsMsg.foregroundUserIds.push_back(DATA_LEN); dmCommToolEventHandler.ParseUserIdsMsg(innrCommMsg, userIdsMsg); AppExecFwk::InnerEvent::Pointer event = AppExecFwk::InnerEvent::Get(commMsg->code, innrCommMsg); dmCommToolEventHandler.ProcessEvent(event); dmCommToolPtr_->ProcessResponseUserStopEvent(innrCommMsg); std::string commonEventType; EventCallback eventCallback; dmCommToolPtr_->StartCommonEvent(commonEventType, eventCallback); dmCommToolPtr_->Init(); int32_t userId = fdp.ConsumeIntegral(); int32_t tokenId = fdp.ConsumeIntegral(); dmCommToolPtr_->SendUninstAppObj(userId, tokenId, rmtNetworkId); std::string emptyNetworkId = ""; dmCommToolPtr_->SendUninstAppObj(userId, tokenId, emptyNetworkId); dmCommToolPtr_->RspAppUninstall(rmtNetworkId, socketId); dmCommToolPtr_->RspAppUnbind(rmtNetworkId, socketId); std::string udid = fdp.ConsumeRandomLengthString(); dmCommToolPtr_->SendUnBindAppObj(userId, tokenId, msg, rmtNetworkId, udid); dmCommToolPtr_->SendUnBindAppObj(userId, tokenId, msg, emptyNetworkId, udid); dmCommToolPtr_->StopSocket(rmtNetworkId); } } } /* Fuzzer entry point */ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { /* Run your code on data */ OHOS::DistributedHardware::DmCommToolFuzzTest(data, size); OHOS::DistributedHardware::DmCommToolFirstFuzzTest(data, size); return 0; }