• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 "dhcpfunction_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <unistd.h>
21 #include "securec.h"
22 #include "dhcp_function.h"
23 
24 namespace OHOS {
25 namespace DHCP {
26 constexpr size_t DHCP_SLEEP_1 = 2;
27 constexpr int TWO = 2;
28 
29 std::shared_ptr<DhcpFunction> pDhcpFunction = std::make_shared<DhcpFunction>();
30 
Ip4StrConToIntTest(const uint8_t * data,size_t size)31 void Ip4StrConToIntTest(const uint8_t* data, size_t size)
32 {
33     uint32_t index = 0;
34     uint32_t uIp = static_cast<uint32_t>(data[index++]);
35     std::string strIp = std::string(reinterpret_cast<const char*>(data), size);
36     bool bHost = (static_cast<int>(data[0]) % TWO) ? true : false;
37     pDhcpFunction->Ip4StrConToInt(strIp, uIp, bHost);
38 }
39 
Ip6StrConToCharTest(const uint8_t * data,size_t size)40 void Ip6StrConToCharTest(const uint8_t* data, size_t size)
41 {
42     std::string strIp = std::string(reinterpret_cast<const char*>(data), size);
43     uint8_t	chIp[sizeof(struct in6_addr)] = {0};
44     pDhcpFunction->Ip6StrConToChar(strIp, chIp, sizeof(struct in6_addr));
45 }
46 
CheckIpStrTest(const uint8_t * data,size_t size)47 void CheckIpStrTest(const uint8_t* data, size_t size)
48 {
49     std::string strIp = std::string(reinterpret_cast<const char*>(data), size);
50     pDhcpFunction->CheckIpStr(strIp);
51 }
52 
IsExistFileTest(const uint8_t * data,size_t size)53 void IsExistFileTest(const uint8_t* data, size_t size)
54 {
55     std::string filename = std::string(reinterpret_cast<const char*>(data), size);
56     pDhcpFunction->IsExistFile(filename);
57 }
58 
CreateFileTest(const uint8_t * data,size_t size)59 void CreateFileTest(const uint8_t* data, size_t size)
60 {
61     std::string filename = std::string(reinterpret_cast<const char*>(data), size);
62     std::string filedata = std::string(reinterpret_cast<const char*>(data), size);
63     pDhcpFunction->CreateFile(filename, filedata);
64 }
65 
RemoveFileTest(const uint8_t * data,size_t size)66 void RemoveFileTest(const uint8_t* data, size_t size)
67 {
68     std::string filename = std::string(reinterpret_cast<const char*>(data), size);
69     pDhcpFunction->RemoveFile(filename);
70 }
71 
FormatStringTest(const uint8_t * data,size_t size)72 void FormatStringTest(const uint8_t* data, size_t size)
73 {
74     struct DhcpPacketResult result;
75     memset_s(&result, sizeof(result), 0, sizeof(result));
76     strncpy_s(result.strYiaddr, INET_ADDRSTRLEN, "*", INET_ADDRSTRLEN - 1);
77     strncpy_s(result.strOptServerId, INET_ADDRSTRLEN, "*", INET_ADDRSTRLEN - 1);
78     strncpy_s(result.strOptSubnet, INET_ADDRSTRLEN, "*", INET_ADDRSTRLEN - 1);
79     strncpy_s(result.strOptDns1, INET_ADDRSTRLEN, "*", INET_ADDRSTRLEN - 1);
80     strncpy_s(result.strOptDns2, INET_ADDRSTRLEN, "*", INET_ADDRSTRLEN - 1);
81     strncpy_s(result.strOptRouter1, INET_ADDRSTRLEN, "*", INET_ADDRSTRLEN - 1);
82     strncpy_s(result.strOptRouter2, INET_ADDRSTRLEN, "*", INET_ADDRSTRLEN - 1);
83     strncpy_s(result.strOptVendor, DHCP_FILE_MAX_BYTES, "*", DHCP_FILE_MAX_BYTES - 1);
84     pDhcpFunction->FormatString(result);
85 }
86 
CreateDirsTest(const uint8_t * data,size_t size)87 void CreateDirsTest(const uint8_t* data, size_t size)
88 {
89     int mode = DIR_DEFAULT_MODE;
90     std::string dirs = std::string(reinterpret_cast<const char*>(data), size);
91     pDhcpFunction->CreateDirs(dirs, mode);
92 }
93 
94 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)95 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
96 {
97     if (data == nullptr) {
98         return 0;
99     }
100     sleep(DHCP_SLEEP_1);
101     OHOS::DHCP::Ip4StrConToIntTest(data, size);
102     OHOS::DHCP::Ip6StrConToCharTest(data, size);
103     OHOS::DHCP::CheckIpStrTest(data, size);
104     OHOS::DHCP::IsExistFileTest(data, size);
105     OHOS::DHCP::CreateFileTest(data, size);
106     OHOS::DHCP::RemoveFileTest(data, size);
107     OHOS::DHCP::FormatStringTest(data, size);
108     OHOS::DHCP::CreateDirsTest(data, size);
109     return 0;
110 }
111 }  // namespace DHCP
112 }  // namespace OHOS
113