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 "selectcertrequest_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_js_ssl_select_cert_result.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 = 1000;
33 constexpr int MAX_VECTOR_SIZE = 10;
SelectCertRequestFuzzTest(const uint8_t * data,size_t size)34 bool SelectCertRequestFuzzTest(const uint8_t* data, size_t size)
35 {
36 if ((data == nullptr) || (size < sizeof(int))) {
37 return false;
38 }
39 FuzzedDataProvider dataProvider(data, size);
40 OHOS::NWeb::NWebHandler handler;
41 std::shared_ptr<NWebJSSslSelectCertResult> result = nullptr;
42 std::string host = dataProvider.ConsumeRandomLengthString(MAX_STRING_LENGTH);
43 int port = dataProvider.ConsumeIntegralInRange<int>(0, MAX_SET_NUMBER);
44 std::vector<std::string> keyTypes;
45 std::vector<std::string> issuers;
46 int vec_size = dataProvider.ConsumeIntegralInRange<int>(0, MAX_VECTOR_SIZE);
47 for (size_t i = 0; i < vec_size; i++) {
48 std::string item = dataProvider.ConsumeRandomLengthString(MAX_STRING_LENGTH);
49 keyTypes.push_back(item);
50 issuers.push_back(item);
51 }
52 handler.OnSslSelectCertRequestByJS(result, host, port, keyTypes, issuers);
53 return true;
54 }
55 } // namespace OHOS
56
57 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)58 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
59 {
60 /* Run your code on data */
61 OHOS::SelectCertRequestFuzzTest(data, size);
62 return 0;
63 }
64