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 "clientstub_fuzzer.h"
20518678f8Sopenharmony_ci#include "message_parcel.h"
21518678f8Sopenharmony_ci#include "securec.h"
22518678f8Sopenharmony_ci#include "dhcp_client_service_impl.h"
23518678f8Sopenharmony_ci#include "dhcp_client_stub.h"
24518678f8Sopenharmony_ci#include "dhcp_manager_service_ipc_interface_code.h"
25518678f8Sopenharmony_ci#include "dhcp_fuzz_common_func.h"
26518678f8Sopenharmony_ci
27518678f8Sopenharmony_cinamespace OHOS {
28518678f8Sopenharmony_cinamespace DHCP {
29518678f8Sopenharmony_ciconstexpr size_t U32_AT_SIZE_ZERO = 4;
30518678f8Sopenharmony_ciconstexpr size_t DHCP_SLEEP_1 = 1;
31518678f8Sopenharmony_ciconst std::u16string FORMMGR_INTERFACE_TOKEN = u"ohos.wifi.IDhcpClient";
32518678f8Sopenharmony_cisptr<DhcpClientStub> pDhcpClientStub = DhcpClientServiceImpl::GetInstance();
33518678f8Sopenharmony_ci
34518678f8Sopenharmony_civoid OnRegisterCallBackTest(const std::string& ifname, size_t size)
35518678f8Sopenharmony_ci{
36518678f8Sopenharmony_ci    uint32_t code = static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_REG_CALL_BACK);
37518678f8Sopenharmony_ci    MessageParcel datas;
38518678f8Sopenharmony_ci    datas.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN);
39518678f8Sopenharmony_ci    datas.WriteInt32(0);
40518678f8Sopenharmony_ci    datas.WriteString(ifname);
41518678f8Sopenharmony_ci    datas.RewindRead(0);
42518678f8Sopenharmony_ci    MessageParcel reply;
43518678f8Sopenharmony_ci    MessageOption option;
44518678f8Sopenharmony_ci    pDhcpClientStub->OnRemoteRequest(code, datas, reply, option);
45518678f8Sopenharmony_ci}
46518678f8Sopenharmony_ci
47518678f8Sopenharmony_civoid OnStartDhcpClientTest(const std::string& ifname, size_t size, bool ipv6)
48518678f8Sopenharmony_ci{
49518678f8Sopenharmony_ci    uint32_t code = static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_START_DHCP_CLIENT);
50518678f8Sopenharmony_ci    MessageParcel datas;
51518678f8Sopenharmony_ci    datas.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN);
52518678f8Sopenharmony_ci    datas.WriteInt32(0);
53518678f8Sopenharmony_ci    datas.WriteString("");
54518678f8Sopenharmony_ci    datas.WriteBool(ipv6);
55518678f8Sopenharmony_ci    datas.RewindRead(0);
56518678f8Sopenharmony_ci    MessageParcel reply;
57518678f8Sopenharmony_ci    MessageOption option;
58518678f8Sopenharmony_ci    pDhcpClientStub->OnRemoteRequest(code, datas, reply, option);
59518678f8Sopenharmony_ci}
60518678f8Sopenharmony_ci
61518678f8Sopenharmony_civoid OnStopDhcpClientTest(const std::string& ifname, size_t size, bool ipv6)
62518678f8Sopenharmony_ci{
63518678f8Sopenharmony_ci    uint32_t code = static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_STOP_DHCP_CLIENT);
64518678f8Sopenharmony_ci    MessageParcel datas;
65518678f8Sopenharmony_ci    datas.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN);
66518678f8Sopenharmony_ci    datas.WriteInt32(0);
67518678f8Sopenharmony_ci    datas.WriteString(ifname);
68518678f8Sopenharmony_ci    datas.WriteBool(ipv6);
69518678f8Sopenharmony_ci    MessageParcel reply;
70518678f8Sopenharmony_ci    MessageOption option;
71518678f8Sopenharmony_ci    pDhcpClientStub->OnRemoteRequest(code, datas, reply, option);
72518678f8Sopenharmony_ci}
73518678f8Sopenharmony_ci
74518678f8Sopenharmony_civoid OnSetConfigurationTest(const std::string& ifname)
75518678f8Sopenharmony_ci{
76518678f8Sopenharmony_ci    uint32_t code = static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_SET_CONFIG);
77518678f8Sopenharmony_ci    MessageParcel datas;
78518678f8Sopenharmony_ci    datas.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN);
79518678f8Sopenharmony_ci    datas.WriteInt32(0);
80518678f8Sopenharmony_ci    datas.WriteString(ifname);
81518678f8Sopenharmony_ci    datas.WriteString("bssid");
82518678f8Sopenharmony_ci    MessageParcel reply;
83518678f8Sopenharmony_ci    MessageOption option;
84518678f8Sopenharmony_ci    pDhcpClientStub->OnRemoteRequest(code, datas, reply, option);
85518678f8Sopenharmony_ci}
86518678f8Sopenharmony_ci
87518678f8Sopenharmony_ci/* Fuzzer entry point */
88518678f8Sopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
89518678f8Sopenharmony_ci{
90518678f8Sopenharmony_ci    if ((data == nullptr) || (size <= OHOS::DHCP::U32_AT_SIZE_ZERO)) {
91518678f8Sopenharmony_ci        return 0;
92518678f8Sopenharmony_ci    }
93518678f8Sopenharmony_ci    std::string ifname = "wlan0";
94518678f8Sopenharmony_ci    OnRegisterCallBackTest(ifname, size);
95518678f8Sopenharmony_ci    sleep(DHCP_SLEEP_1);
96518678f8Sopenharmony_ci    OnSetConfigurationTest(ifname);
97518678f8Sopenharmony_ci    sleep(DHCP_SLEEP_1);
98518678f8Sopenharmony_ci    OnStartDhcpClientTest(ifname, size, false);
99518678f8Sopenharmony_ci    sleep(DHCP_SLEEP_1);
100518678f8Sopenharmony_ci    OnStopDhcpClientTest(ifname, size, false);
101518678f8Sopenharmony_ci    sleep(DHCP_SLEEP_1);
102518678f8Sopenharmony_ci    return 0;
103518678f8Sopenharmony_ci}
104518678f8Sopenharmony_ci}  // namespace DHCP
105518678f8Sopenharmony_ci}  // namespace OHOS
106