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 "dhcpclient_fuzzer.h"
17 #include "dhcp_client.h"
18 #include "dhcp_event.h"
19 
20 namespace OHOS {
21 namespace DHCP {
22     std::shared_ptr<DhcpClient> dhcpClient = DhcpClient::GetInstance(DHCP_CLIENT_ABILITY_ID);
23     static OHOS::sptr<DhcpClientCallBack> dhcpClientCallBack =
24         OHOS::sptr<DhcpClientCallBack>(new (std::nothrow)DhcpClientCallBack());
25     static OHOS::sptr<DhcpServerCallBack> dhcpServerCallBack =
26         OHOS::sptr<DhcpServerCallBack>(new (std::nothrow)DhcpServerCallBack());
DhcpClientFuzzerTest(const uint8_t * data,size_t size)27     bool DhcpClientFuzzerTest(const uint8_t* data, size_t size)
28     {
29         if (dhcpClient == nullptr) {
30             return false;
31         }
32         std::string ifname = std::string(reinterpret_cast<const char*>(data), size);
33         RouterConfig routerConfig;
34         routerConfig.bssid = std::string(reinterpret_cast<const char*>(data), size);
35         dhcpClient->SetConfiguration(ifname, routerConfig);
36         dhcpClient->StartDhcpClient(ifname, true);
37         dhcpClient->StopDhcpClient(ifname, true);
38         dhcpClient->RegisterDhcpClientCallBack(ifname, dhcpClientCallBack);
39 
40         return true;
41     }
42 }  // namespace DHCP
43 }  // namespace OHOS
44 
45 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)46 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
47 {
48     OHOS::DHCP::DhcpClientFuzzerTest(data, size);
49     return 0;
50 }
51