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 "dhcpfunction_fuzzer.h" 17518678f8Sopenharmony_ci 18518678f8Sopenharmony_ci#include <cstddef> 19518678f8Sopenharmony_ci#include <cstdint> 20518678f8Sopenharmony_ci#include <unistd.h> 21518678f8Sopenharmony_ci#include "securec.h" 22518678f8Sopenharmony_ci#include "dhcp_function.h" 23518678f8Sopenharmony_ci 24518678f8Sopenharmony_cinamespace OHOS { 25518678f8Sopenharmony_cinamespace DHCP { 26518678f8Sopenharmony_ciconstexpr size_t DHCP_SLEEP_1 = 2; 27518678f8Sopenharmony_ciconstexpr int TWO = 2; 28518678f8Sopenharmony_ci 29518678f8Sopenharmony_cistd::shared_ptr<DhcpFunction> pDhcpFunction = std::make_shared<DhcpFunction>(); 30518678f8Sopenharmony_ci 31518678f8Sopenharmony_civoid Ip4StrConToIntTest(const uint8_t* data, size_t size) 32518678f8Sopenharmony_ci{ 33518678f8Sopenharmony_ci uint32_t index = 0; 34518678f8Sopenharmony_ci uint32_t uIp = static_cast<uint32_t>(data[index++]); 35518678f8Sopenharmony_ci std::string strIp = std::string(reinterpret_cast<const char*>(data), size); 36518678f8Sopenharmony_ci bool bHost = (static_cast<int>(data[0]) % TWO) ? true : false; 37518678f8Sopenharmony_ci pDhcpFunction->Ip4StrConToInt(strIp, uIp, bHost); 38518678f8Sopenharmony_ci} 39518678f8Sopenharmony_ci 40518678f8Sopenharmony_civoid Ip4IntConvertToStrTest(const uint8_t* data, size_t size) 41518678f8Sopenharmony_ci{ 42518678f8Sopenharmony_ci uint32_t index = 0; 43518678f8Sopenharmony_ci uint32_t uIp = static_cast<uint32_t>(data[index++]); 44518678f8Sopenharmony_ci bool bHost = (static_cast<int>(data[0]) % TWO) ? true : false; 45518678f8Sopenharmony_ci pDhcpFunction->Ip4IntConvertToStr(uIp, bHost); 46518678f8Sopenharmony_ci} 47518678f8Sopenharmony_ci 48518678f8Sopenharmony_civoid Ip6StrConToCharTest(const uint8_t* data, size_t size) 49518678f8Sopenharmony_ci{ 50518678f8Sopenharmony_ci std::string strIp = std::string(reinterpret_cast<const char*>(data), size); 51518678f8Sopenharmony_ci uint8_t chIp[sizeof(struct in6_addr)] = {0}; 52518678f8Sopenharmony_ci pDhcpFunction->Ip6StrConToChar(strIp, chIp, sizeof(struct in6_addr)); 53518678f8Sopenharmony_ci} 54518678f8Sopenharmony_ci 55518678f8Sopenharmony_civoid CheckIpStrTest(const uint8_t* data, size_t size) 56518678f8Sopenharmony_ci{ 57518678f8Sopenharmony_ci std::string strIp = std::string(reinterpret_cast<const char*>(data), size); 58518678f8Sopenharmony_ci pDhcpFunction->CheckIpStr(strIp); 59518678f8Sopenharmony_ci} 60518678f8Sopenharmony_ci 61518678f8Sopenharmony_civoid IsExistFileTest(const uint8_t* data, size_t size) 62518678f8Sopenharmony_ci{ 63518678f8Sopenharmony_ci std::string filename = std::string(reinterpret_cast<const char*>(data), size); 64518678f8Sopenharmony_ci pDhcpFunction->IsExistFile(filename); 65518678f8Sopenharmony_ci} 66518678f8Sopenharmony_ci 67518678f8Sopenharmony_civoid CreateFileTest(const uint8_t* data, size_t size) 68518678f8Sopenharmony_ci{ 69518678f8Sopenharmony_ci std::string filename = std::string(reinterpret_cast<const char*>(data), size); 70518678f8Sopenharmony_ci std::string filedata = std::string(reinterpret_cast<const char*>(data), size); 71518678f8Sopenharmony_ci pDhcpFunction->CreateFile(filename, filedata); 72518678f8Sopenharmony_ci} 73518678f8Sopenharmony_ci 74518678f8Sopenharmony_civoid RemoveFileTest(const uint8_t* data, size_t size) 75518678f8Sopenharmony_ci{ 76518678f8Sopenharmony_ci std::string filename = std::string(reinterpret_cast<const char*>(data), size); 77518678f8Sopenharmony_ci pDhcpFunction->RemoveFile(filename); 78518678f8Sopenharmony_ci} 79518678f8Sopenharmony_ci 80518678f8Sopenharmony_civoid AddFileLineDataTest(const uint8_t* data, size_t size) 81518678f8Sopenharmony_ci{ 82518678f8Sopenharmony_ci std::string filename = std::string(reinterpret_cast<const char*>(data), size); 83518678f8Sopenharmony_ci std::string prevdata = std::string(reinterpret_cast<const char*>(data), size); 84518678f8Sopenharmony_ci std::string linedata = std::string(reinterpret_cast<const char*>(data), size); 85518678f8Sopenharmony_ci pDhcpFunction->AddFileLineData(filename, prevdata, linedata); 86518678f8Sopenharmony_ci} 87518678f8Sopenharmony_ci 88518678f8Sopenharmony_civoid DelFileLineDataTest(const uint8_t* data, size_t size) 89518678f8Sopenharmony_ci{ 90518678f8Sopenharmony_ci std::string filename = std::string(reinterpret_cast<const char*>(data), size); 91518678f8Sopenharmony_ci std::string linedata = std::string(reinterpret_cast<const char*>(data), size); 92518678f8Sopenharmony_ci pDhcpFunction->DelFileLineData(filename, linedata); 93518678f8Sopenharmony_ci} 94518678f8Sopenharmony_ci 95518678f8Sopenharmony_civoid ModifyFileLineDataTest(const uint8_t* data, size_t size) 96518678f8Sopenharmony_ci{ 97518678f8Sopenharmony_ci std::string filename = std::string(reinterpret_cast<const char*>(data), size); 98518678f8Sopenharmony_ci std::string srcdata = std::string(reinterpret_cast<const char*>(data), size); 99518678f8Sopenharmony_ci std::string dstdata = std::string(reinterpret_cast<const char*>(data), size); 100518678f8Sopenharmony_ci pDhcpFunction->ModifyFileLineData(filename, srcdata, dstdata); 101518678f8Sopenharmony_ci} 102518678f8Sopenharmony_ci 103518678f8Sopenharmony_civoid FormatStringTest(const uint8_t* data, size_t size) 104518678f8Sopenharmony_ci{ 105518678f8Sopenharmony_ci struct DhcpPacketResult result; 106518678f8Sopenharmony_ci memset_s(&result, sizeof(result), 0, sizeof(result)); 107518678f8Sopenharmony_ci strncpy_s(result.strYiaddr, INET_ADDRSTRLEN, "*", INET_ADDRSTRLEN - 1); 108518678f8Sopenharmony_ci strncpy_s(result.strOptServerId, INET_ADDRSTRLEN, "*", INET_ADDRSTRLEN - 1); 109518678f8Sopenharmony_ci strncpy_s(result.strOptSubnet, INET_ADDRSTRLEN, "*", INET_ADDRSTRLEN - 1); 110518678f8Sopenharmony_ci strncpy_s(result.strOptDns1, INET_ADDRSTRLEN, "*", INET_ADDRSTRLEN - 1); 111518678f8Sopenharmony_ci strncpy_s(result.strOptDns2, INET_ADDRSTRLEN, "*", INET_ADDRSTRLEN - 1); 112518678f8Sopenharmony_ci strncpy_s(result.strOptRouter1, INET_ADDRSTRLEN, "*", INET_ADDRSTRLEN - 1); 113518678f8Sopenharmony_ci strncpy_s(result.strOptRouter2, INET_ADDRSTRLEN, "*", INET_ADDRSTRLEN - 1); 114518678f8Sopenharmony_ci strncpy_s(result.strOptVendor, DHCP_FILE_MAX_BYTES, "*", DHCP_FILE_MAX_BYTES - 1); 115518678f8Sopenharmony_ci pDhcpFunction->FormatString(result); 116518678f8Sopenharmony_ci} 117518678f8Sopenharmony_ci 118518678f8Sopenharmony_civoid InitPidfileTest(const uint8_t* data, size_t size) 119518678f8Sopenharmony_ci{ 120518678f8Sopenharmony_ci std::string piddir = std::string(reinterpret_cast<const char*>(data), size); 121518678f8Sopenharmony_ci std::string pidfile = std::string(reinterpret_cast<const char*>(data), size); 122518678f8Sopenharmony_ci pDhcpFunction->InitPidfile(piddir, pidfile); 123518678f8Sopenharmony_ci} 124518678f8Sopenharmony_ci 125518678f8Sopenharmony_civoid GetPIDTest(const uint8_t* data, size_t size) 126518678f8Sopenharmony_ci{ 127518678f8Sopenharmony_ci std::string pidfile = std::string(reinterpret_cast<const char*>(data), size); 128518678f8Sopenharmony_ci pDhcpFunction->GetPID(pidfile); 129518678f8Sopenharmony_ci} 130518678f8Sopenharmony_ci 131518678f8Sopenharmony_civoid CheckProRunningTest(const uint8_t* data, size_t size) 132518678f8Sopenharmony_ci{ 133518678f8Sopenharmony_ci pid_t index = 0; 134518678f8Sopenharmony_ci pid_t proPid = static_cast<pid_t>(data[index++]); 135518678f8Sopenharmony_ci std::string proName = std::string(reinterpret_cast<const char*>(data), size); 136518678f8Sopenharmony_ci pDhcpFunction->CheckProRunning(proPid, proName); 137518678f8Sopenharmony_ci} 138518678f8Sopenharmony_ci 139518678f8Sopenharmony_civoid CreateDirsTest(const uint8_t* data, size_t size) 140518678f8Sopenharmony_ci{ 141518678f8Sopenharmony_ci int mode = DIR_DEFAULT_MODE; 142518678f8Sopenharmony_ci std::string dirs = std::string(reinterpret_cast<const char*>(data), size); 143518678f8Sopenharmony_ci pDhcpFunction->CreateDirs(dirs, mode); 144518678f8Sopenharmony_ci} 145518678f8Sopenharmony_ci 146518678f8Sopenharmony_civoid SplitStringTest(const uint8_t* data, size_t size) 147518678f8Sopenharmony_ci{ 148518678f8Sopenharmony_ci int index = 0; 149518678f8Sopenharmony_ci int count = static_cast<int>(data[index++]); 150518678f8Sopenharmony_ci std::vector<std::string> splits {}; 151518678f8Sopenharmony_ci std::string src = std::string(reinterpret_cast<const char*>(data), size); 152518678f8Sopenharmony_ci std::string delim = std::string(reinterpret_cast<const char*>(data), size); 153518678f8Sopenharmony_ci pDhcpFunction->SplitString(src, delim, count, splits); 154518678f8Sopenharmony_ci} 155518678f8Sopenharmony_ci 156518678f8Sopenharmony_civoid WaitProcessExitTest(const uint8_t* data, size_t size) 157518678f8Sopenharmony_ci{ 158518678f8Sopenharmony_ci pid_t index = 0; 159518678f8Sopenharmony_ci pid_t serverPid = static_cast<pid_t>(data[index++]); 160518678f8Sopenharmony_ci pDhcpFunction->WaitProcessExit(serverPid); 161518678f8Sopenharmony_ci} 162518678f8Sopenharmony_ci 163518678f8Sopenharmony_ci/* Fuzzer entry point */ 164518678f8Sopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) 165518678f8Sopenharmony_ci{ 166518678f8Sopenharmony_ci if (data == nullptr) { 167518678f8Sopenharmony_ci return 0; 168518678f8Sopenharmony_ci } 169518678f8Sopenharmony_ci sleep(DHCP_SLEEP_1); 170518678f8Sopenharmony_ci OHOS::DHCP::Ip4StrConToIntTest(data, size); 171518678f8Sopenharmony_ci OHOS::DHCP::Ip4IntConvertToStrTest(data, size); 172518678f8Sopenharmony_ci OHOS::DHCP::Ip6StrConToCharTest(data, size); 173518678f8Sopenharmony_ci OHOS::DHCP::CheckIpStrTest(data, size); 174518678f8Sopenharmony_ci OHOS::DHCP::IsExistFileTest(data, size); 175518678f8Sopenharmony_ci OHOS::DHCP::CreateFileTest(data, size); 176518678f8Sopenharmony_ci OHOS::DHCP::RemoveFileTest(data, size); 177518678f8Sopenharmony_ci OHOS::DHCP::AddFileLineDataTest(data, size); 178518678f8Sopenharmony_ci OHOS::DHCP::DelFileLineDataTest(data, size); 179518678f8Sopenharmony_ci OHOS::DHCP::ModifyFileLineDataTest(data, size); 180518678f8Sopenharmony_ci OHOS::DHCP::FormatStringTest(data, size); 181518678f8Sopenharmony_ci OHOS::DHCP::InitPidfileTest(data, size); 182518678f8Sopenharmony_ci OHOS::DHCP::GetPIDTest(data, size); 183518678f8Sopenharmony_ci OHOS::DHCP::CheckProRunningTest(data, size); 184518678f8Sopenharmony_ci OHOS::DHCP::CreateDirsTest(data, size); 185518678f8Sopenharmony_ci OHOS::DHCP::SplitStringTest(data, size); 186518678f8Sopenharmony_ci OHOS::DHCP::WaitProcessExitTest(data, size); 187518678f8Sopenharmony_ci return 0; 188518678f8Sopenharmony_ci} 189518678f8Sopenharmony_ci} // namespace DHCP 190518678f8Sopenharmony_ci} // namespace OHOS 191