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 "string_fuzzer.h"
17
18#include <cstdint>
19#include <thread>
20#include <vector>
21
22#include "fuzz_log.h"
23#include "fuzzer/FuzzedDataProvider.h"
24#include "string_ex.h"
25
26using namespace std;
27
28namespace OHOS {
29const uint8_t MAX_STRING_LENGTH = 255;
30
31void StringTestFunc(FuzzedDataProvider* dataProvider)
32{
33    FUZZ_LOGI("StringTestFunc start");
34    string str = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
35    string src = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
36    string dst = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
37    ReplaceStr(str, src, dst);
38
39    int value = dataProvider->ConsumeIntegral<int>();
40    bool upper = dataProvider->ConsumeBool();
41    DexToHexString(value, upper);
42
43    string newStr = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
44    IsAlphaStr(newStr);
45    IsUpperStr(newStr);
46    IsLowerStr(newStr);
47    IsNumericStr(newStr);
48
49    string sub = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
50    IsSubStr(newStr, sub);
51
52    string left = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
53    string right = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
54    string emptySubStr;
55    GetFirstSubStrBetween(newStr, left, right, emptySubStr);
56    FUZZ_LOGI("StringTestFunc end");
57}
58} // namespace OHOS
59
60/* Fuzzer entry point */
61extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
62{
63    FuzzedDataProvider dataProvider(data, size);
64    OHOS::StringTestFunc(&dataProvider);
65    return 0;
66}
67