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 "dhcpbinding_fuzzer.h"
17518678f8Sopenharmony_ci
18518678f8Sopenharmony_ci#include <cstddef>
19518678f8Sopenharmony_ci#include <cstdint>
20518678f8Sopenharmony_ci#include <unistd.h>
21518678f8Sopenharmony_ci#include "securec.h"
22518678f8Sopenharmony_ci#include "dhcp_binding.h"
23518678f8Sopenharmony_ci
24518678f8Sopenharmony_cinamespace OHOS {
25518678f8Sopenharmony_cinamespace Wifi {
26518678f8Sopenharmony_ciconstexpr size_t DHCP_SLEEP_1 = 2;
27518678f8Sopenharmony_ciconstexpr size_t U32_AT_SIZE_ZERO = 4;
28518678f8Sopenharmony_ciconstexpr int CFG_DATA_MAX_BYTES = 20;
29518678f8Sopenharmony_ci
30518678f8Sopenharmony_civoid NextPendingIntervalTest(const uint8_t* data, size_t size)
31518678f8Sopenharmony_ci{
32518678f8Sopenharmony_ci    uint64_t pendingInterval = static_cast<uint64_t>(data[0]);
33518678f8Sopenharmony_ci    NextPendingInterval(pendingInterval);
34518678f8Sopenharmony_ci}
35518678f8Sopenharmony_ci
36518678f8Sopenharmony_civoid IsExpireTest(const uint8_t* data, size_t size)
37518678f8Sopenharmony_ci{
38518678f8Sopenharmony_ci    AddressBinding binding;
39518678f8Sopenharmony_ci    binding.ipAddress = static_cast<uint32_t>(data[0]);
40518678f8Sopenharmony_ci    binding.clientId = static_cast<uint32_t>(data[0]);
41518678f8Sopenharmony_ci    binding.bindingTime = static_cast<uint64_t>(data[0]);
42518678f8Sopenharmony_ci    binding.pendingTime = static_cast<uint64_t>(data[0]);
43518678f8Sopenharmony_ci    binding.expireIn = static_cast<uint64_t>(data[0]);
44518678f8Sopenharmony_ci    binding.leaseTime = static_cast<uint64_t>(data[0]);
45518678f8Sopenharmony_ci    binding.pendingInterval = static_cast<uint64_t>(data[0]);
46518678f8Sopenharmony_ci    IsExpire(&binding);
47518678f8Sopenharmony_ci}
48518678f8Sopenharmony_ci
49518678f8Sopenharmony_civoid WriteAddressBindingTest(const uint8_t* data, size_t size)
50518678f8Sopenharmony_ci{
51518678f8Sopenharmony_ci    AddressBinding binding;
52518678f8Sopenharmony_ci    uint32_t size_t = static_cast<uint64_t>(data[0]);
53518678f8Sopenharmony_ci    binding.ipAddress = static_cast<uint32_t>(data[0]);
54518678f8Sopenharmony_ci    binding.clientId = static_cast<uint32_t>(data[0]);
55518678f8Sopenharmony_ci    binding.bindingTime = static_cast<uint64_t>(data[0]);
56518678f8Sopenharmony_ci    binding.pendingTime = static_cast<uint64_t>(data[0]);
57518678f8Sopenharmony_ci    binding.expireIn = static_cast<uint64_t>(data[0]);
58518678f8Sopenharmony_ci    binding.leaseTime = static_cast<uint64_t>(data[0]);
59518678f8Sopenharmony_ci    binding.pendingInterval = static_cast<uint64_t>(data[0]);
60518678f8Sopenharmony_ci    char *out = nullptr;
61518678f8Sopenharmony_ci    if (memcpy_s(out, CFG_DATA_MAX_BYTES, data, CFG_DATA_MAX_BYTES - 1) != EOK) {
62518678f8Sopenharmony_ci        return;
63518678f8Sopenharmony_ci    }
64518678f8Sopenharmony_ci    WriteAddressBinding(&binding, out, size_t);
65518678f8Sopenharmony_ci}
66518678f8Sopenharmony_ci
67518678f8Sopenharmony_civoid ParseAddressBindingTest(const uint8_t* data, size_t size)
68518678f8Sopenharmony_ci{
69518678f8Sopenharmony_ci    AddressBinding binding;
70518678f8Sopenharmony_ci    binding.ipAddress = static_cast<uint32_t>(data[0]);
71518678f8Sopenharmony_ci    binding.clientId = static_cast<uint32_t>(data[0]);
72518678f8Sopenharmony_ci    binding.bindingTime = static_cast<uint64_t>(data[0]);
73518678f8Sopenharmony_ci    binding.pendingTime = static_cast<uint64_t>(data[0]);
74518678f8Sopenharmony_ci    binding.expireIn = static_cast<uint64_t>(data[0]);
75518678f8Sopenharmony_ci    binding.leaseTime = static_cast<uint64_t>(data[0]);
76518678f8Sopenharmony_ci    binding.pendingInterval = static_cast<uint64_t>(data[0]);
77518678f8Sopenharmony_ci    const char *buf = "Text";
78518678f8Sopenharmony_ci    ParseAddressBinding(&binding, buf);
79518678f8Sopenharmony_ci}
80518678f8Sopenharmony_ci
81518678f8Sopenharmony_ci/* Fuzzer entry point */
82518678f8Sopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
83518678f8Sopenharmony_ci{
84518678f8Sopenharmony_ci    if ((data == nullptr) || (size <= OHOS::Wifi::U32_AT_SIZE_ZERO)) {
85518678f8Sopenharmony_ci        return 0;
86518678f8Sopenharmony_ci    }
87518678f8Sopenharmony_ci    sleep(DHCP_SLEEP_1);
88518678f8Sopenharmony_ci    OHOS::Wifi::NextPendingIntervalTest(data, size);
89518678f8Sopenharmony_ci    OHOS::Wifi::IsExpireTest(data, size);
90518678f8Sopenharmony_ci    OHOS::Wifi::WriteAddressBindingTest(data, size);
91518678f8Sopenharmony_ci    OHOS::Wifi::ParseAddressBindingTest(data, size);
92518678f8Sopenharmony_ci    return 0;
93518678f8Sopenharmony_ci}
94518678f8Sopenharmony_ci}  // namespace Wifi
95518678f8Sopenharmony_ci}  // namespace OHOS
96