diff --git a/base/test/fuzztest/BUILD.gn b/base/test/fuzztest/BUILD.gn
index 61243ac948c902c7337294755121eddbd931b952..97ca64680145194de07b4254820f05377b3c7d80 100644
--- a/base/test/fuzztest/BUILD.gn
+++ b/base/test/fuzztest/BUILD.gn
@@ -20,6 +20,7 @@ group("fuzztest") {
# deps file
"parcel_fuzzer:ParcelFuzzTest",
"refbase_fuzzer:RefbaseFuzzTest",
+ "string_fuzzer:StringFuzzTest",
"timer_fuzzer:TimerFuzzTest",
]
}
diff --git a/base/test/fuzztest/string_fuzzer/BUILD.gn b/base/test/fuzztest/string_fuzzer/BUILD.gn
new file mode 100644
index 0000000000000000000000000000000000000000..758e4499ef84081f170705f241236408c60312b3
--- /dev/null
+++ b/base/test/fuzztest/string_fuzzer/BUILD.gn
@@ -0,0 +1,33 @@
+# Copyright (c) 2024 Huawei Device Co., Ltd.
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+import("//build/test.gni")
+
+##############################fuzztest##########################################
+ohos_fuzztest("StringFuzzTest") {
+ module_out_path = "c_utils/c_utils"
+ fuzz_config_file = "."
+ include_dirs = [ "../" ]
+ cflags = [
+ "-g",
+ "-O0",
+ "-Wno-unused-variable",
+ "-fno-omit-frame-pointer",
+ ]
+ defines = [ "DEBUG_FUZZ" ]
+ external_deps = [
+ "c_utils:utils",
+ "hilog:libhilog_base",
+ ]
+ sources = [ "string_fuzzer.cpp" ]
+}
+###############################################################################
diff --git a/base/test/fuzztest/string_fuzzer/corpus/init b/base/test/fuzztest/string_fuzzer/corpus/init
new file mode 100644
index 0000000000000000000000000000000000000000..e7c3fecd8d4d4816e40088113a2316bb9eb2e13f
--- /dev/null
+++ b/base/test/fuzztest/string_fuzzer/corpus/init
@@ -0,0 +1,14 @@
+# Copyright (c) 2024 Huawei Device Co., Ltd.
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+FUZZ
\ No newline at end of file
diff --git a/base/test/fuzztest/string_fuzzer/project.xml b/base/test/fuzztest/string_fuzzer/project.xml
new file mode 100644
index 0000000000000000000000000000000000000000..d6679ccc2ec868b072c934ed45102d545f31f91d
--- /dev/null
+++ b/base/test/fuzztest/string_fuzzer/project.xml
@@ -0,0 +1,25 @@
+
+
+
+
+
+ 1000
+
+ 300
+
+ 4096
+
+
diff --git a/base/test/fuzztest/string_fuzzer/string_fuzzer.cpp b/base/test/fuzztest/string_fuzzer/string_fuzzer.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..3f7fab098b24f6fa93dfb0b82bd4db8b236cbb7f
--- /dev/null
+++ b/base/test/fuzztest/string_fuzzer/string_fuzzer.cpp
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2023 Huawei Device Co., Ltd.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "string_fuzzer.h"
+
+#include
+#include
+#include
+
+#include "fuzz_log.h"
+#include "fuzzer/FuzzedDataProvider.h"
+#include "string_ex.h"
+
+using namespace std;
+
+namespace OHOS {
+const uint8_t MAX_STRING_LENGTH = 255;
+
+void StringTestFunc(FuzzedDataProvider* dataProvider)
+{
+ FUZZ_LOGI("StringTestFunc start");
+ string str = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
+ string src = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
+ string dst = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
+ ReplaceStr(str, src, dst);
+
+ int value = dataProvider->ConsumeIntegral();
+ bool upper = dataProvider->ConsumeBool();
+ DexToHexString(value, upper);
+
+ string newStr = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
+ IsAlphaStr(newStr);
+ IsUpperStr(newStr);
+ IsLowerStr(newStr);
+ IsNumericStr(newStr);
+
+ string sub = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
+ IsSubStr(newStr, sub);
+
+ string left = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
+ string right = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
+ string emptySubStr;
+ GetFirstSubStrBetween(newStr, left, right, emptySubStr);
+ FUZZ_LOGI("StringTestFunc end");
+}
+} // namespace OHOS
+
+/* Fuzzer entry point */
+extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
+{
+ FuzzedDataProvider dataProvider(data, size);
+ OHOS::StringTestFunc(&dataProvider);
+ return 0;
+}
diff --git a/base/test/fuzztest/string_fuzzer/string_fuzzer.h b/base/test/fuzztest/string_fuzzer/string_fuzzer.h
new file mode 100644
index 0000000000000000000000000000000000000000..cdb04a89b66768cd37c8792b717883cd7da66edb
--- /dev/null
+++ b/base/test/fuzztest/string_fuzzer/string_fuzzer.h
@@ -0,0 +1,21 @@
+/*
+ * Copyright (c) 2024 Huawei Device Co., Ltd.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef STRING_FUZZER_H
+#define STRING_FUZZER_H
+
+#define FUZZ_PROJECT_NAME "string_fuzzer"
+
+#endif // STRING_FUZZER_H