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 <cstddef>
17#include <cstdint>
18#include <unistd.h>
19#include "clientstub_fuzzer.h"
20#include "message_parcel.h"
21#include "securec.h"
22#include "dhcp_client_service_impl.h"
23#include "dhcp_client_stub.h"
24#include "dhcp_manager_service_ipc_interface_code.h"
25#include "dhcp_fuzz_common_func.h"
26
27namespace OHOS {
28namespace DHCP {
29constexpr size_t U32_AT_SIZE_ZERO = 4;
30constexpr size_t DHCP_SLEEP_1 = 1;
31const std::u16string FORMMGR_INTERFACE_TOKEN = u"ohos.wifi.IDhcpClient";
32sptr<DhcpClientStub> pDhcpClientStub = DhcpClientServiceImpl::GetInstance();
33
34void OnRegisterCallBackTest(const std::string& ifname, size_t size)
35{
36    uint32_t code = static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_REG_CALL_BACK);
37    MessageParcel datas;
38    datas.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN);
39    datas.WriteInt32(0);
40    datas.WriteString(ifname);
41    datas.RewindRead(0);
42    MessageParcel reply;
43    MessageOption option;
44    pDhcpClientStub->OnRemoteRequest(code, datas, reply, option);
45}
46
47void OnStartDhcpClientTest(const std::string& ifname, size_t size, bool ipv6)
48{
49    uint32_t code = static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_START_DHCP_CLIENT);
50    MessageParcel datas;
51    datas.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN);
52    datas.WriteInt32(0);
53    datas.WriteString("");
54    datas.WriteBool(ipv6);
55    datas.RewindRead(0);
56    MessageParcel reply;
57    MessageOption option;
58    pDhcpClientStub->OnRemoteRequest(code, datas, reply, option);
59}
60
61void OnStopDhcpClientTest(const std::string& ifname, size_t size, bool ipv6)
62{
63    uint32_t code = static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_STOP_DHCP_CLIENT);
64    MessageParcel datas;
65    datas.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN);
66    datas.WriteInt32(0);
67    datas.WriteString(ifname);
68    datas.WriteBool(ipv6);
69    MessageParcel reply;
70    MessageOption option;
71    pDhcpClientStub->OnRemoteRequest(code, datas, reply, option);
72}
73
74void OnSetConfigurationTest(const std::string& ifname)
75{
76    uint32_t code = static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_SET_CONFIG);
77    MessageParcel datas;
78    datas.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN);
79    datas.WriteInt32(0);
80    datas.WriteString(ifname);
81    datas.WriteString("bssid");
82    MessageParcel reply;
83    MessageOption option;
84    pDhcpClientStub->OnRemoteRequest(code, datas, reply, option);
85}
86
87/* Fuzzer entry point */
88extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
89{
90    if ((data == nullptr) || (size <= OHOS::DHCP::U32_AT_SIZE_ZERO)) {
91        return 0;
92    }
93    std::string ifname = "wlan0";
94    OnRegisterCallBackTest(ifname, size);
95    sleep(DHCP_SLEEP_1);
96    OnSetConfigurationTest(ifname);
97    sleep(DHCP_SLEEP_1);
98    OnStartDhcpClientTest(ifname, size, false);
99    sleep(DHCP_SLEEP_1);
100    OnStopDhcpClientTest(ifname, size, false);
101    sleep(DHCP_SLEEP_1);
102    return 0;
103}
104}  // namespace DHCP
105}  // namespace OHOS
106