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 "commonutil_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <unistd.h>
21 #include "securec.h"
22 #include "common_util.h"
23 
24 namespace OHOS {
25 namespace Wifi {
26 constexpr size_t DHCP_SLEEP_1 = 2;
27 
CommonUtilFuzzTest(const uint8_t * data,size_t size)28 void CommonUtilFuzzTest(const uint8_t* data, size_t size)
29 {
30     uint32_t index = 0;
31     char buf[] = {"aabbcc"};
32     size_t bufSize = static_cast<uint32_t>(data[index++]);
33     Tmspsec();
34     Tmspusec();
35     LeftTrim(buf);
36     RightTrim(buf);
37     TrimString(buf);
38     RemoveSpaceCharacters(buf, bufSize);
39 }
40 
GetFilePathTest(const uint8_t * data,size_t size)41 void GetFilePathTest(const uint8_t* data, size_t size)
42 {
43     char fileName;
44     if (size > 0) {
45         fileName = static_cast<char>(data[0]);
46     }
47     (void)GetFilePath(&fileName);
48 }
49 
GetLeaseFileTest(const uint8_t * data,size_t size)50 void GetLeaseFileTest(const uint8_t* data, size_t size)
51 {
52     char fileName;
53     char ifname;
54     if (size > 0) {
55         fileName = static_cast<char>(data[0]);
56         ifname = static_cast<char>(data[0]);
57     }
58     (void)GetLeaseFile(&fileName, &ifname);
59 }
60 
CreatePathTest(const uint8_t * data,size_t size)61 void CreatePathTest(const uint8_t* data, size_t size)
62 {
63     char fileName;
64     if (size > 0) {
65         fileName = static_cast<char>(data[0]);
66     }
67     (void)CreatePath(&fileName);
68 }
69 
70 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)71 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
72 {
73     if (data == nullptr) {
74         return 0;
75     }
76     sleep(DHCP_SLEEP_1);
77     OHOS::Wifi::CommonUtilFuzzTest(data, size);
78     OHOS::Wifi::GetFilePathTest(data, size);
79     OHOS::Wifi::GetLeaseFileTest(data, size);
80     OHOS::Wifi::CreatePathTest(data, size);
81     return 0;
82 }
83 }  // namespace Wifi
84 }  // namespace OHOS
85