1518678f8Sopenharmony_ci/* 2518678f8Sopenharmony_ci * Copyright (C) 2024 Huawei Device Co., Ltd. 3518678f8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4518678f8Sopenharmony_ci * you may not use this file except in compliance with the License. 5518678f8Sopenharmony_ci * You may obtain a copy of the License at 6518678f8Sopenharmony_ci * 7518678f8Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8518678f8Sopenharmony_ci * 9518678f8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10518678f8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11518678f8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12518678f8Sopenharmony_ci * See the License for the specific language governing permissions and 13518678f8Sopenharmony_ci * limitations under the License. 14518678f8Sopenharmony_ci */ 15518678f8Sopenharmony_ci 16518678f8Sopenharmony_ci#include <cstddef> 17518678f8Sopenharmony_ci#include <cstdint> 18518678f8Sopenharmony_ci#include <unistd.h> 19518678f8Sopenharmony_ci#include "dhcpserverimpl_fuzzer.h" 20518678f8Sopenharmony_ci#include "securec.h" 21518678f8Sopenharmony_ci#include "dhcp_server_service_impl.h" 22518678f8Sopenharmony_ci#include "dhcp_server_death_recipient.h" 23518678f8Sopenharmony_ci#include "iremote_object.h" 24518678f8Sopenharmony_ci 25518678f8Sopenharmony_cinamespace OHOS { 26518678f8Sopenharmony_cinamespace DHCP { 27518678f8Sopenharmony_ciconstexpr size_t U32_AT_SIZE_ZERO = 4; 28518678f8Sopenharmony_ciconstexpr size_t DHCP_SLEEP_1 = 2; 29518678f8Sopenharmony_cisptr<DhcpServerServiceImpl> pDhcpServerServiceImpl = DhcpServerServiceImpl::GetInstance(); 30518678f8Sopenharmony_ci 31518678f8Sopenharmony_civoid OnStartTest(const uint8_t* data, size_t size) 32518678f8Sopenharmony_ci{ 33518678f8Sopenharmony_ci pDhcpServerServiceImpl->OnStart(); 34518678f8Sopenharmony_ci} 35518678f8Sopenharmony_ci 36518678f8Sopenharmony_civoid OnStopTest(const uint8_t* data, size_t size) 37518678f8Sopenharmony_ci{ 38518678f8Sopenharmony_ci pDhcpServerServiceImpl->OnStop(); 39518678f8Sopenharmony_ci} 40518678f8Sopenharmony_ci 41518678f8Sopenharmony_civoid StartDhcpServerTest(const uint8_t* data, size_t size) 42518678f8Sopenharmony_ci{ 43518678f8Sopenharmony_ci std::string ifname = ""; 44518678f8Sopenharmony_ci pDhcpServerServiceImpl->StartDhcpServer(ifname); 45518678f8Sopenharmony_ci} 46518678f8Sopenharmony_ci 47518678f8Sopenharmony_civoid StopDhcpServerTest(const uint8_t* data, size_t size) 48518678f8Sopenharmony_ci{ 49518678f8Sopenharmony_ci std::string ifname1 = "wlan0"; 50518678f8Sopenharmony_ci std::string ifname2 = ""; 51518678f8Sopenharmony_ci pDhcpServerServiceImpl->StopDhcpServer(ifname1); 52518678f8Sopenharmony_ci pDhcpServerServiceImpl->StopDhcpServer(ifname2); 53518678f8Sopenharmony_ci} 54518678f8Sopenharmony_ci 55518678f8Sopenharmony_civoid PutDhcpRangeTest(const uint8_t* data, size_t size) 56518678f8Sopenharmony_ci{ 57518678f8Sopenharmony_ci std::string tagName = "sta"; 58518678f8Sopenharmony_ci DhcpRange range; 59518678f8Sopenharmony_ci range.iptype = 0; 60518678f8Sopenharmony_ci pDhcpServerServiceImpl->PutDhcpRange(tagName, range); 61518678f8Sopenharmony_ci pDhcpServerServiceImpl->PutDhcpRange("", range); 62518678f8Sopenharmony_ci} 63518678f8Sopenharmony_ci 64518678f8Sopenharmony_civoid RemoveDhcpRangeTest(const uint8_t* data, size_t size) 65518678f8Sopenharmony_ci{ 66518678f8Sopenharmony_ci std::string tagName = "sta"; 67518678f8Sopenharmony_ci DhcpRange range; 68518678f8Sopenharmony_ci range.iptype = 0; 69518678f8Sopenharmony_ci pDhcpServerServiceImpl->RemoveDhcpRange(tagName, range); 70518678f8Sopenharmony_ci pDhcpServerServiceImpl->RemoveDhcpRange("", range); 71518678f8Sopenharmony_ci} 72518678f8Sopenharmony_ci 73518678f8Sopenharmony_civoid RemoveAllDhcpRangeTest(const uint8_t* data, size_t size) 74518678f8Sopenharmony_ci{ 75518678f8Sopenharmony_ci std::string tagName1 = "sta"; 76518678f8Sopenharmony_ci std::string tagName2 = ""; 77518678f8Sopenharmony_ci pDhcpServerServiceImpl->RemoveAllDhcpRange(tagName1); 78518678f8Sopenharmony_ci pDhcpServerServiceImpl->RemoveAllDhcpRange(tagName2); 79518678f8Sopenharmony_ci} 80518678f8Sopenharmony_ci 81518678f8Sopenharmony_civoid SetDhcpRangeTest(const uint8_t* data, size_t size) 82518678f8Sopenharmony_ci{ 83518678f8Sopenharmony_ci std::string ifname = "wlan0"; 84518678f8Sopenharmony_ci DhcpRange range; 85518678f8Sopenharmony_ci range.iptype = 0; 86518678f8Sopenharmony_ci pDhcpServerServiceImpl->SetDhcpRange(ifname, range); 87518678f8Sopenharmony_ci} 88518678f8Sopenharmony_ci 89518678f8Sopenharmony_civoid SetDhcpNameTest(const uint8_t* data, size_t size) 90518678f8Sopenharmony_ci{ 91518678f8Sopenharmony_ci std::string ifname = "wlan0"; 92518678f8Sopenharmony_ci std::string tagName = "sta"; 93518678f8Sopenharmony_ci pDhcpServerServiceImpl->SetDhcpName(ifname, tagName); 94518678f8Sopenharmony_ci pDhcpServerServiceImpl->SetDhcpName("", tagName); 95518678f8Sopenharmony_ci pDhcpServerServiceImpl->SetDhcpName(ifname, ""); 96518678f8Sopenharmony_ci pDhcpServerServiceImpl->SetDhcpName("", ""); 97518678f8Sopenharmony_ci} 98518678f8Sopenharmony_ci 99518678f8Sopenharmony_civoid GetDhcpClientInfosTest(const uint8_t* data, size_t size) 100518678f8Sopenharmony_ci{ 101518678f8Sopenharmony_ci std::string ifname1 = ""; 102518678f8Sopenharmony_ci std::string ifname2 = "wlan0"; 103518678f8Sopenharmony_ci std::vector<std::string> leases; 104518678f8Sopenharmony_ci leases.push_back(std::string(reinterpret_cast<const char*>(data), size)); 105518678f8Sopenharmony_ci pDhcpServerServiceImpl->GetDhcpClientInfos(ifname1, leases); 106518678f8Sopenharmony_ci pDhcpServerServiceImpl->GetDhcpClientInfos(ifname2, leases); 107518678f8Sopenharmony_ci} 108518678f8Sopenharmony_ci 109518678f8Sopenharmony_civoid UpdateLeasesTimeTest(const uint8_t* data, size_t size) 110518678f8Sopenharmony_ci{ 111518678f8Sopenharmony_ci std::string leaseTime = std::string(reinterpret_cast<const char*>(data), size); 112518678f8Sopenharmony_ci pDhcpServerServiceImpl->UpdateLeasesTime(leaseTime); 113518678f8Sopenharmony_ci} 114518678f8Sopenharmony_ci 115518678f8Sopenharmony_civoid IsRemoteDiedTest(const uint8_t* data, size_t size) 116518678f8Sopenharmony_ci{ 117518678f8Sopenharmony_ci pDhcpServerServiceImpl->IsRemoteDied(); 118518678f8Sopenharmony_ci} 119518678f8Sopenharmony_ci 120518678f8Sopenharmony_civoid DeleteLeaseFileTest(const uint8_t* data, size_t size) 121518678f8Sopenharmony_ci{ 122518678f8Sopenharmony_ci std::string ifname = "wlan0"; 123518678f8Sopenharmony_ci pDhcpServerServiceImpl->DeleteLeaseFile(ifname); 124518678f8Sopenharmony_ci} 125518678f8Sopenharmony_ci 126518678f8Sopenharmony_civoid CheckAndUpdateConfTest(const uint8_t* data, size_t size) 127518678f8Sopenharmony_ci{ 128518678f8Sopenharmony_ci std::string ifname1 = ""; 129518678f8Sopenharmony_ci std::string ifname2 = "wlan0"; 130518678f8Sopenharmony_ci pDhcpServerServiceImpl->CheckAndUpdateConf(ifname1); 131518678f8Sopenharmony_ci pDhcpServerServiceImpl->CheckAndUpdateConf(ifname2); 132518678f8Sopenharmony_ci} 133518678f8Sopenharmony_ci 134518678f8Sopenharmony_civoid CheckIpAddrRangeTest(const uint8_t* data, size_t size) 135518678f8Sopenharmony_ci{ 136518678f8Sopenharmony_ci DhcpRange range; 137518678f8Sopenharmony_ci int call = 2; 138518678f8Sopenharmony_ci range.strTagName = std::string(reinterpret_cast<const char*>(data), size); 139518678f8Sopenharmony_ci range.strStartip = std::string(reinterpret_cast<const char*>(data), size); 140518678f8Sopenharmony_ci range.strEndip = std::string(reinterpret_cast<const char*>(data), size); 141518678f8Sopenharmony_ci range.strSubnet = std::string(reinterpret_cast<const char*>(data), size); 142518678f8Sopenharmony_ci range.iptype = static_cast<int>(data[0]) % call; 143518678f8Sopenharmony_ci range.leaseHours = static_cast<int>(data[0]); 144518678f8Sopenharmony_ci pDhcpServerServiceImpl->CheckIpAddrRange(range); 145518678f8Sopenharmony_ci} 146518678f8Sopenharmony_ci 147518678f8Sopenharmony_civoid AddSpecifiedInterfaceTest(const uint8_t* data, size_t size) 148518678f8Sopenharmony_ci{ 149518678f8Sopenharmony_ci std::string ifname = "wlan0"; 150518678f8Sopenharmony_ci pDhcpServerServiceImpl->AddSpecifiedInterface(ifname); 151518678f8Sopenharmony_ci} 152518678f8Sopenharmony_ci 153518678f8Sopenharmony_civoid GetUsingIpRangeTest(const uint8_t* data, size_t size) 154518678f8Sopenharmony_ci{ 155518678f8Sopenharmony_ci std::string ifname1 = ""; 156518678f8Sopenharmony_ci std::string ifname2 = "ww"; 157518678f8Sopenharmony_ci std::string ifname3 = "wlan0"; 158518678f8Sopenharmony_ci std::string ipRange = std::string(reinterpret_cast<const char*>(data), size); 159518678f8Sopenharmony_ci pDhcpServerServiceImpl->GetUsingIpRange(ifname1, ipRange); 160518678f8Sopenharmony_ci pDhcpServerServiceImpl->GetUsingIpRange(ifname2, ipRange); 161518678f8Sopenharmony_ci pDhcpServerServiceImpl->GetUsingIpRange(ifname3, ipRange); 162518678f8Sopenharmony_ci} 163518678f8Sopenharmony_ci 164518678f8Sopenharmony_civoid CreateDefaultConfigFileTest(const uint8_t* data, size_t size) 165518678f8Sopenharmony_ci{ 166518678f8Sopenharmony_ci std::string strFile = std::string(reinterpret_cast<const char*>(data), size); 167518678f8Sopenharmony_ci pDhcpServerServiceImpl->CreateDefaultConfigFile(strFile); 168518678f8Sopenharmony_ci} 169518678f8Sopenharmony_ci 170518678f8Sopenharmony_civoid UnregisterSignalTest(const uint8_t* data, size_t size) 171518678f8Sopenharmony_ci{ 172518678f8Sopenharmony_ci pDhcpServerServiceImpl->UnregisterSignal(); 173518678f8Sopenharmony_ci} 174518678f8Sopenharmony_ci 175518678f8Sopenharmony_civoid DelSpecifiedInterfaceTest(const uint8_t* data, size_t size) 176518678f8Sopenharmony_ci{ 177518678f8Sopenharmony_ci std::string ifname = std::string(reinterpret_cast<const char*>(data), size); 178518678f8Sopenharmony_ci pDhcpServerServiceImpl->DelSpecifiedInterface(ifname); 179518678f8Sopenharmony_ci} 180518678f8Sopenharmony_ci 181518678f8Sopenharmony_civoid RegisterDhcpServerCallBackTest(const uint8_t* data, size_t size) 182518678f8Sopenharmony_ci{ 183518678f8Sopenharmony_ci std::string ifname = std::string(reinterpret_cast<const char*>(data), size); 184518678f8Sopenharmony_ci sptr<IDhcpServerCallBack> serverCallback; 185518678f8Sopenharmony_ci pDhcpServerServiceImpl->RegisterDhcpServerCallBack(ifname, serverCallback); 186518678f8Sopenharmony_ci} 187518678f8Sopenharmony_ci 188518678f8Sopenharmony_civoid StartServiceAbilityTest(const uint8_t* data, size_t size) 189518678f8Sopenharmony_ci{ 190518678f8Sopenharmony_ci int index = 0; 191518678f8Sopenharmony_ci int sleepS = static_cast<int>(index++); 192518678f8Sopenharmony_ci pDhcpServerServiceImpl->StartServiceAbility(sleepS); 193518678f8Sopenharmony_ci} 194518678f8Sopenharmony_ci 195518678f8Sopenharmony_ci/* Fuzzer entry point */ 196518678f8Sopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) 197518678f8Sopenharmony_ci{ 198518678f8Sopenharmony_ci if ((data == nullptr) || (size <= OHOS::DHCP::U32_AT_SIZE_ZERO)) { 199518678f8Sopenharmony_ci return 0; 200518678f8Sopenharmony_ci } 201518678f8Sopenharmony_ci sleep(DHCP_SLEEP_1); 202518678f8Sopenharmony_ci OHOS::DHCP::OnStartTest(data, size); 203518678f8Sopenharmony_ci OHOS::DHCP::OnStopTest(data, size); 204518678f8Sopenharmony_ci OHOS::DHCP::StartDhcpServerTest(data, size); 205518678f8Sopenharmony_ci OHOS::DHCP::StopDhcpServerTest(data, size); 206518678f8Sopenharmony_ci OHOS::DHCP::PutDhcpRangeTest(data, size); 207518678f8Sopenharmony_ci OHOS::DHCP::RemoveDhcpRangeTest(data, size); 208518678f8Sopenharmony_ci OHOS::DHCP::RemoveAllDhcpRangeTest(data, size); 209518678f8Sopenharmony_ci OHOS::DHCP::SetDhcpRangeTest(data, size); 210518678f8Sopenharmony_ci OHOS::DHCP::SetDhcpNameTest(data, size); 211518678f8Sopenharmony_ci OHOS::DHCP::GetDhcpClientInfosTest(data, size); 212518678f8Sopenharmony_ci OHOS::DHCP::UpdateLeasesTimeTest(data, size); 213518678f8Sopenharmony_ci OHOS::DHCP::IsRemoteDiedTest(data, size); 214518678f8Sopenharmony_ci OHOS::DHCP::DeleteLeaseFileTest(data, size); 215518678f8Sopenharmony_ci OHOS::DHCP::CheckAndUpdateConfTest(data, size); 216518678f8Sopenharmony_ci OHOS::DHCP::CheckIpAddrRangeTest(data, size); 217518678f8Sopenharmony_ci OHOS::DHCP::AddSpecifiedInterfaceTest(data, size); 218518678f8Sopenharmony_ci OHOS::DHCP::GetUsingIpRangeTest(data, size); 219518678f8Sopenharmony_ci OHOS::DHCP::CreateDefaultConfigFileTest(data, size); 220518678f8Sopenharmony_ci OHOS::DHCP::UnregisterSignalTest(data, size); 221518678f8Sopenharmony_ci OHOS::DHCP::DelSpecifiedInterfaceTest(data, size); 222518678f8Sopenharmony_ci OHOS::DHCP::RegisterDhcpServerCallBackTest(data, size); 223518678f8Sopenharmony_ci OHOS::DHCP::StartServiceAbilityTest(data, size); 224518678f8Sopenharmony_ci return 0; 225518678f8Sopenharmony_ci} 226518678f8Sopenharmony_ci} // namespace DHCP 227518678f8Sopenharmony_ci} // namespace OHOS 228