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 "serverstub_fuzzer.h"
20 #include "message_parcel.h"
21 #include "securec.h"
22 #include "dhcp_server_service_impl.h"
23 #include "dhcp_server_stub.h"
24 #include "dhcp_manager_service_ipc_interface_code.h"
25 #include "dhcp_fuzz_common_func.h"
26 
27 namespace OHOS {
28 namespace DHCP {
29 constexpr size_t U32_AT_SIZE_ZERO = 4;
30 constexpr size_t MAP_SCAN_NUMS = 12;
31 constexpr size_t DHCP_SLEEP_1 = 2;
32 const std::u16string FORMMGR_INTERFACE_TOKEN = u"ohos.wifi.IDhcpServer";
33 sptr<DhcpServerStub> pDhcpServerStub = DhcpServerServiceImpl::GetInstance();
34 
OnGetSupportedFeaturesTest(const uint8_t * data,size_t size)35 void OnGetSupportedFeaturesTest(const uint8_t* data, size_t size)
36 {
37     uint32_t code = U32_AT(data) % MAP_SCAN_NUMS + static_cast<uint32_t>
38     (DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_REG_CALL_BACK);
39     if (code == static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_GET_SERVER_STATUS) ||
40         code ==  static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_SVR_CMD_GET_IP_LIST)) {
41         return;
42     }
43     MessageParcel datas;
44     datas.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN);
45     datas.WriteInt32(0);
46     datas.WriteBuffer(data, size);
47     datas.RewindRead(0);
48     MessageParcel reply;
49     MessageOption option;
50     pDhcpServerStub->OnRemoteRequest(code, datas, reply, option);
51 }
52 
53 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)54 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
55 {
56     if ((data == nullptr) || (size <= OHOS::DHCP::U32_AT_SIZE_ZERO)) {
57         return 0;
58     }
59     sleep(DHCP_SLEEP_1);
60     OHOS::DHCP::OnGetSupportedFeaturesTest(data, size);
61     return 0;
62 }
63 }  // namespace DHCP
64 }  // namespace OHOS
65