13f4cbf05Sopenharmony_ci/* 23f4cbf05Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 33f4cbf05Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 43f4cbf05Sopenharmony_ci * you may not use this file except in compliance with the License. 53f4cbf05Sopenharmony_ci * You may obtain a copy of the License at 63f4cbf05Sopenharmony_ci * 73f4cbf05Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 83f4cbf05Sopenharmony_ci * 93f4cbf05Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 103f4cbf05Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 113f4cbf05Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 123f4cbf05Sopenharmony_ci * See the License for the specific language governing permissions and 133f4cbf05Sopenharmony_ci * limitations under the License. 143f4cbf05Sopenharmony_ci */ 153f4cbf05Sopenharmony_ci 163f4cbf05Sopenharmony_ci#include "string_fuzzer.h" 173f4cbf05Sopenharmony_ci 183f4cbf05Sopenharmony_ci#include <cstdint> 193f4cbf05Sopenharmony_ci#include <thread> 203f4cbf05Sopenharmony_ci#include <vector> 213f4cbf05Sopenharmony_ci 223f4cbf05Sopenharmony_ci#include "fuzz_log.h" 233f4cbf05Sopenharmony_ci#include "fuzzer/FuzzedDataProvider.h" 243f4cbf05Sopenharmony_ci#include "string_ex.h" 253f4cbf05Sopenharmony_ci 263f4cbf05Sopenharmony_ciusing namespace std; 273f4cbf05Sopenharmony_ci 283f4cbf05Sopenharmony_cinamespace OHOS { 293f4cbf05Sopenharmony_ciconst uint8_t MAX_STRING_LENGTH = 255; 303f4cbf05Sopenharmony_ci 313f4cbf05Sopenharmony_civoid StringTestFunc(FuzzedDataProvider* dataProvider) 323f4cbf05Sopenharmony_ci{ 333f4cbf05Sopenharmony_ci FUZZ_LOGI("StringTestFunc start"); 343f4cbf05Sopenharmony_ci string str = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); 353f4cbf05Sopenharmony_ci string src = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); 363f4cbf05Sopenharmony_ci string dst = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); 373f4cbf05Sopenharmony_ci ReplaceStr(str, src, dst); 383f4cbf05Sopenharmony_ci 393f4cbf05Sopenharmony_ci int value = dataProvider->ConsumeIntegral<int>(); 403f4cbf05Sopenharmony_ci bool upper = dataProvider->ConsumeBool(); 413f4cbf05Sopenharmony_ci DexToHexString(value, upper); 423f4cbf05Sopenharmony_ci 433f4cbf05Sopenharmony_ci string newStr = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); 443f4cbf05Sopenharmony_ci IsAlphaStr(newStr); 453f4cbf05Sopenharmony_ci IsUpperStr(newStr); 463f4cbf05Sopenharmony_ci IsLowerStr(newStr); 473f4cbf05Sopenharmony_ci IsNumericStr(newStr); 483f4cbf05Sopenharmony_ci 493f4cbf05Sopenharmony_ci string sub = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); 503f4cbf05Sopenharmony_ci IsSubStr(newStr, sub); 513f4cbf05Sopenharmony_ci 523f4cbf05Sopenharmony_ci string left = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); 533f4cbf05Sopenharmony_ci string right = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); 543f4cbf05Sopenharmony_ci string emptySubStr; 553f4cbf05Sopenharmony_ci GetFirstSubStrBetween(newStr, left, right, emptySubStr); 563f4cbf05Sopenharmony_ci FUZZ_LOGI("StringTestFunc end"); 573f4cbf05Sopenharmony_ci} 583f4cbf05Sopenharmony_ci} // namespace OHOS 593f4cbf05Sopenharmony_ci 603f4cbf05Sopenharmony_ci/* Fuzzer entry point */ 613f4cbf05Sopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) 623f4cbf05Sopenharmony_ci{ 633f4cbf05Sopenharmony_ci FuzzedDataProvider dataProvider(data, size); 643f4cbf05Sopenharmony_ci OHOS::StringTestFunc(&dataProvider); 653f4cbf05Sopenharmony_ci return 0; 663f4cbf05Sopenharmony_ci} 67