1 /*
2 * Copyright (c) 2023 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 "sethttpdns_fuzzer.h"
17 #include <fuzzer/FuzzedDataProvider.h>
18
19 #include <cstring>
20 #include <securec.h>
21
22 #include "nweb.h"
23 #include "nweb_adapter_helper.h"
24 #include "nweb_handler.h"
25 #include "nweb_helper.h"
26 #include "nweb_init_params.h"
27
28 using namespace OHOS::NWeb;
29
30 namespace OHOS {
31 constexpr uint8_t MAX_STRING_LENGTH = 125;
32 constexpr int MAX_SET_NUMBER = 10;
SetHttpDnsFuzzTest(const uint8_t * data,size_t size)33 bool SetHttpDnsFuzzTest(const uint8_t* data, size_t size)
34 {
35 if ((data == nullptr) || (size < sizeof(int))) {
36 return false;
37 }
38 FuzzedDataProvider dataProvider(data, size);
39 std::string dohConfig = dataProvider.ConsumeRandomLengthString(MAX_STRING_LENGTH);
40 int mode = dataProvider.ConsumeIntegralInRange<int>(0, MAX_SET_NUMBER);
41
42 std::shared_ptr<NWebDOHConfigImpl> config = std::make_shared<NWebDOHConfigImpl>();
43 config->SetMode(mode);
44 config->SetConfig(dohConfig);
45 NWebHelper::Instance().SetHttpDns(config);
46 return true;
47 }
48 } // namespace OHOS
49
50 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)51 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
52 {
53 /* Run your code on data */
54 OHOS::SetHttpDnsFuzzTest(data, size);
55 return 0;
56 }
57