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 "dhcpservercbkstub_fuzzer.h"
17 #include "dhcp_client.h"
18 #include "dhcp_event.h"
19 #include "dhcp_manager_service_ipc_interface_code.h"
20 #include "dhcp_server_callback_stub.h"
21 #include "dhcp_fuzz_common_func.h"
22 
23 
24 namespace OHOS {
25 namespace DHCP {
26 constexpr size_t U32_AT_SIZE_ZERO = 4;
27 constexpr size_t MAP_SCAN_NUMS = 5;
28 const std::u16string FORMMGR_INTERFACE_TOKEN = u"ohos.wifi.IDhcpServerCallBack";
29 std::shared_ptr<DhcpServreCallBackStub> pDhcpServerCbkStub = std::make_shared<DhcpServreCallBackStub>();
30 
OnGetSupportedFeaturesTest(const uint8_t * data,size_t size)31 void OnGetSupportedFeaturesTest(const uint8_t* data, size_t size)
32 {
33     uint32_t code = U32_AT(data) % MAP_SCAN_NUMS + static_cast<uint32_t>
34     (DhcpServerInterfaceCode::DHCP_SERVER_CBK_SERVER_STATUS_CHANGE);
35     MessageParcel datas;
36     datas.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN);
37     datas.WriteInt32(0);
38     datas.WriteBuffer(data, size);
39     datas.RewindRead(0);
40     MessageParcel reply;
41     MessageOption option;
42     pDhcpServerCbkStub->OnRemoteRequest(code, datas, reply, option);
43 }
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     if ((data == nullptr) || (size <= OHOS::DHCP::U32_AT_SIZE_ZERO)) {
49         return 0;
50     }
51     OHOS::DHCP::OnGetSupportedFeaturesTest(data, size);
52     return 0;
53 }
54 }  // namespace DHCP
55 }  // namespace OHOS
56 
57