From 5d5904d5d94b5d1abd2e67e6798c76b8120e25bd Mon Sep 17 00:00:00 2001 From: y30045862 Date: Tue, 22 Jul 2025 16:42:46 +0800 Subject: [PATCH] file_api add fuzztest Signed-off-by: yangjingbo10 Change-Id: Ibec087b97d64b84be2ee72d710cb0c4e45ebd104 --- bundle.json | 3 +- interfaces/test/fuzztest/BUILD.gn | 28 +++++ .../test/fuzztest/environment_fuzzer/BUILD.gn | 47 ++++++++ .../fuzztest/environment_fuzzer/corpus/init | 16 +++ .../environment_fuzzer/environment_fuzzer.cpp | 58 ++++++++++ .../environment_fuzzer/environment_fuzzer.h | 21 ++++ .../fuzztest/environment_fuzzer/project.xml | 25 +++++ .../test/fuzztest/fileio_fuzzer/BUILD.gn | 47 ++++++++ .../test/fuzztest/fileio_fuzzer/corpus/init | 16 +++ .../fuzztest/fileio_fuzzer/fileio_fuzzer.cpp | 79 +++++++++++++ .../fuzztest/fileio_fuzzer/fileio_fuzzer.h | 21 ++++ .../test/fuzztest/fileio_fuzzer/project.xml | 25 +++++ .../fuzz_common/fileapi_fuzzer_helper.h | 86 ++++++++++++++ .../test/fuzztest/hyperaio_fuzzer/BUILD.gn | 60 ++++++++++ .../test/fuzztest/hyperaio_fuzzer/corpus/init | 16 +++ .../hyperaio_fuzzer/hyperaio_fuzzer.cpp | 88 +++++++++++++++ .../hyperaio_fuzzer/hyperaio_fuzzer.h | 21 ++++ .../test/fuzztest/hyperaio_fuzzer/project.xml | 25 +++++ .../test/fuzztest/resource/ohos_test.xml | 32 ++++++ .../test/fuzztest/resource/rust_test.txt | 30 +++++ .../fuzztest/rustcutfilename_fuzzer/BUILD.gn | 43 +++++++ .../rustcutfilename_fuzzer/corpus/init | 16 +++ .../rustcutfilename_fuzzer/project.xml | 25 +++++ .../rustcutfilename_fuzzer.cpp | 64 +++++++++++ .../rustcutfilename_fuzzer.h | 21 ++++ .../fuzztest/rustgetparentstr_fuzzer/BUILD.gn | 43 +++++++ .../rustgetparentstr_fuzzer/corpus/init | 16 +++ .../rustgetparentstr_fuzzer/project.xml | 25 +++++ .../rustgetparentstr_fuzzer.cpp | 57 ++++++++++ .../rustgetparentstr_fuzzer.h | 21 ++++ .../test/fuzztest/rustmkdirs_fuzzer/BUILD.gn | 43 +++++++ .../fuzztest/rustmkdirs_fuzzer/corpus/init | 16 +++ .../fuzztest/rustmkdirs_fuzzer/project.xml | 25 +++++ .../rustmkdirs_fuzzer/rustmkdirs_fuzzer.cpp | 105 ++++++++++++++++++ .../rustmkdirs_fuzzer/rustmkdirs_fuzzer.h | 21 ++++ .../rustreaderiterator_fuzzer/BUILD.gn | 43 +++++++ .../rustreaderiterator_fuzzer/corpus/init | 16 +++ .../rustreaderiterator_fuzzer/project.xml | 25 +++++ .../rustreaderiterator_fuzzer.cpp | 62 +++++++++++ .../rustreaderiterator_fuzzer.h | 21 ++++ .../test/fuzztest/rustseek_fuzzer/BUILD.gn | 43 +++++++ .../test/fuzztest/rustseek_fuzzer/corpus/init | 16 +++ .../test/fuzztest/rustseek_fuzzer/project.xml | 25 +++++ .../rustseek_fuzzer/rustseek_fuzzer.cpp | 92 +++++++++++++++ .../rustseek_fuzzer/rustseek_fuzzer.h | 21 ++++ 45 files changed, 1648 insertions(+), 1 deletion(-) create mode 100644 interfaces/test/fuzztest/BUILD.gn create mode 100644 interfaces/test/fuzztest/environment_fuzzer/BUILD.gn create mode 100644 interfaces/test/fuzztest/environment_fuzzer/corpus/init create mode 100644 interfaces/test/fuzztest/environment_fuzzer/environment_fuzzer.cpp create mode 100644 interfaces/test/fuzztest/environment_fuzzer/environment_fuzzer.h create mode 100644 interfaces/test/fuzztest/environment_fuzzer/project.xml create mode 100644 interfaces/test/fuzztest/fileio_fuzzer/BUILD.gn create mode 100644 interfaces/test/fuzztest/fileio_fuzzer/corpus/init create mode 100644 interfaces/test/fuzztest/fileio_fuzzer/fileio_fuzzer.cpp create mode 100644 interfaces/test/fuzztest/fileio_fuzzer/fileio_fuzzer.h create mode 100644 interfaces/test/fuzztest/fileio_fuzzer/project.xml create mode 100644 interfaces/test/fuzztest/fuzz_common/fileapi_fuzzer_helper.h create mode 100644 interfaces/test/fuzztest/hyperaio_fuzzer/BUILD.gn create mode 100644 interfaces/test/fuzztest/hyperaio_fuzzer/corpus/init create mode 100644 interfaces/test/fuzztest/hyperaio_fuzzer/hyperaio_fuzzer.cpp create mode 100644 interfaces/test/fuzztest/hyperaio_fuzzer/hyperaio_fuzzer.h create mode 100644 interfaces/test/fuzztest/hyperaio_fuzzer/project.xml create mode 100644 interfaces/test/fuzztest/resource/ohos_test.xml create mode 100644 interfaces/test/fuzztest/resource/rust_test.txt create mode 100644 interfaces/test/fuzztest/rustcutfilename_fuzzer/BUILD.gn create mode 100644 interfaces/test/fuzztest/rustcutfilename_fuzzer/corpus/init create mode 100644 interfaces/test/fuzztest/rustcutfilename_fuzzer/project.xml create mode 100644 interfaces/test/fuzztest/rustcutfilename_fuzzer/rustcutfilename_fuzzer.cpp create mode 100644 interfaces/test/fuzztest/rustcutfilename_fuzzer/rustcutfilename_fuzzer.h create mode 100644 interfaces/test/fuzztest/rustgetparentstr_fuzzer/BUILD.gn create mode 100644 interfaces/test/fuzztest/rustgetparentstr_fuzzer/corpus/init create mode 100644 interfaces/test/fuzztest/rustgetparentstr_fuzzer/project.xml create mode 100644 interfaces/test/fuzztest/rustgetparentstr_fuzzer/rustgetparentstr_fuzzer.cpp create mode 100644 interfaces/test/fuzztest/rustgetparentstr_fuzzer/rustgetparentstr_fuzzer.h create mode 100644 interfaces/test/fuzztest/rustmkdirs_fuzzer/BUILD.gn create mode 100644 interfaces/test/fuzztest/rustmkdirs_fuzzer/corpus/init create mode 100644 interfaces/test/fuzztest/rustmkdirs_fuzzer/project.xml create mode 100644 interfaces/test/fuzztest/rustmkdirs_fuzzer/rustmkdirs_fuzzer.cpp create mode 100644 interfaces/test/fuzztest/rustmkdirs_fuzzer/rustmkdirs_fuzzer.h create mode 100644 interfaces/test/fuzztest/rustreaderiterator_fuzzer/BUILD.gn create mode 100644 interfaces/test/fuzztest/rustreaderiterator_fuzzer/corpus/init create mode 100644 interfaces/test/fuzztest/rustreaderiterator_fuzzer/project.xml create mode 100644 interfaces/test/fuzztest/rustreaderiterator_fuzzer/rustreaderiterator_fuzzer.cpp create mode 100644 interfaces/test/fuzztest/rustreaderiterator_fuzzer/rustreaderiterator_fuzzer.h create mode 100644 interfaces/test/fuzztest/rustseek_fuzzer/BUILD.gn create mode 100644 interfaces/test/fuzztest/rustseek_fuzzer/corpus/init create mode 100644 interfaces/test/fuzztest/rustseek_fuzzer/project.xml create mode 100644 interfaces/test/fuzztest/rustseek_fuzzer/rustseek_fuzzer.cpp create mode 100644 interfaces/test/fuzztest/rustseek_fuzzer/rustseek_fuzzer.h diff --git a/bundle.json b/bundle.json index b0c965533..4b7dae035 100644 --- a/bundle.json +++ b/bundle.json @@ -212,7 +212,8 @@ } ], "test": [ - "//foundation/filemanagement/file_api/interfaces/test/unittest:file_api_unittest" + "//foundation/filemanagement/file_api/interfaces/test/unittest:file_api_unittest", + "//foundation/filemanagement/file_api/interfaces/test/fuzztest:file_api_fuzztest" ] } } diff --git a/interfaces/test/fuzztest/BUILD.gn b/interfaces/test/fuzztest/BUILD.gn new file mode 100644 index 000000000..aab21d9a5 --- /dev/null +++ b/interfaces/test/fuzztest/BUILD.gn @@ -0,0 +1,28 @@ +# 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("//foundation/filemanagement/file_api/file_api.gni") + +group("file_api_fuzztest") { + testonly = true + + deps = [ + "${file_api_path}/interfaces/test/fuzztest/environment_fuzzer:EnvironmentFuzzTest", + "${file_api_path}/interfaces/test/fuzztest/fileio_fuzzer:FileioFuzzTest", + "${file_api_path}/interfaces/test/fuzztest/rustcutfilename_fuzzer:RustCutfilenameFuzzTest", + "${file_api_path}/interfaces/test/fuzztest/rustmkdirs_fuzzer:RustMkdirsFuzzTest", + "${file_api_path}/interfaces/test/fuzztest/rustgetparentstr_fuzzer:RustGetParentStrFuzzTest", + "${file_api_path}/interfaces/test/fuzztest/rustreaderiterator_fuzzer:RustReadIteratorFuzzTest", + "${file_api_path}/interfaces/test/fuzztest/rustseek_fuzzer:RustSeekFuzzTest", + "${file_api_path}/interfaces/test/fuzztest/hyperaio_fuzzer:HyperaioFuzzTest", + ] +} diff --git a/interfaces/test/fuzztest/environment_fuzzer/BUILD.gn b/interfaces/test/fuzztest/environment_fuzzer/BUILD.gn new file mode 100644 index 000000000..26567c1c0 --- /dev/null +++ b/interfaces/test/fuzztest/environment_fuzzer/BUILD.gn @@ -0,0 +1,47 @@ +# Copyright (c) 2025 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/config/features.gni") +import("//build/test.gni") +import("//foundation/filemanagement/file_api/file_api.gni") + +ohos_fuzztest("EnvironmentFuzzTest") { + module_out_path = "file_api/file_api" + fuzz_config_file = "${file_api_path}/interfaces/test/fuzztest/environment_fuzzer" + + include_dirs = [ + "${file_api_path}/interfaces/test/fuzztest/fuzz_common", + "${file_api_path}/interfaces/kits/c/environment", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ + "${file_api_path}/interfaces/test/fuzztest/environment_fuzzer/environment_fuzzer.cpp", + ] + defines = [] + deps = [ + "${file_api_path}/interfaces/kits/c/environment:ohenvironment", + "${file_api_path}/interfaces/kits/native:fileio_native", + ] + external_deps = [ "c_utils:utils" ] +} + +group("fuzztest") { + testonly = true + deps = [ ":EnvironmentFuzzTest" ] +} diff --git a/interfaces/test/fuzztest/environment_fuzzer/corpus/init b/interfaces/test/fuzztest/environment_fuzzer/corpus/init new file mode 100644 index 000000000..7ade8a0fa --- /dev/null +++ b/interfaces/test/fuzztest/environment_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 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/interfaces/test/fuzztest/environment_fuzzer/environment_fuzzer.cpp b/interfaces/test/fuzztest/environment_fuzzer/environment_fuzzer.cpp new file mode 100644 index 000000000..b75cd04b6 --- /dev/null +++ b/interfaces/test/fuzztest/environment_fuzzer/environment_fuzzer.cpp @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2025 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 "environment_fuzzer.h" + +#include "environment.h" +#include "fileapi_fuzzer_helper.h" + +namespace OHOS { +bool GetUserDownloadDirFuzzTest(const uint8_t *data, size_t size) +{ + (void)data; + char *res = nullptr; + OH_Environment_GetUserDownloadDir(&res); + return true; +} + +bool GetUserDocumentDirFuzzTest(const uint8_t *data, size_t size) +{ + (void)data; + char *res = nullptr; + OH_Environment_GetUserDocumentDir(&res); + return true; +} + +bool GetUserDesktopDirFuzzTest(const uint8_t *data, size_t size) +{ + (void)data; + char *res = nullptr; + OH_Environment_GetUserDesktopDir(&res); + return true; +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + if (data == nullptr) { + return 0; + } + + OHOS::GetUserDownloadDirFuzzTest(data, size); + OHOS::GetUserDocumentDirFuzzTest(data, size); + OHOS::GetUserDesktopDirFuzzTest(data, size); + return 0; +} diff --git a/interfaces/test/fuzztest/environment_fuzzer/environment_fuzzer.h b/interfaces/test/fuzztest/environment_fuzzer/environment_fuzzer.h new file mode 100644 index 000000000..f3419b194 --- /dev/null +++ b/interfaces/test/fuzztest/environment_fuzzer/environment_fuzzer.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 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 ENVIRONMENT_FUZZER_H +#define ENVIRONMENT_FUZZER_H + +#define FUZZ_PROJECT_NAME "environment_fuzzer" + +#endif \ No newline at end of file diff --git a/interfaces/test/fuzztest/environment_fuzzer/project.xml b/interfaces/test/fuzztest/environment_fuzzer/project.xml new file mode 100644 index 000000000..8cd958170 --- /dev/null +++ b/interfaces/test/fuzztest/environment_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/interfaces/test/fuzztest/fileio_fuzzer/BUILD.gn b/interfaces/test/fuzztest/fileio_fuzzer/BUILD.gn new file mode 100644 index 000000000..0b4ea42c9 --- /dev/null +++ b/interfaces/test/fuzztest/fileio_fuzzer/BUILD.gn @@ -0,0 +1,47 @@ +# Copyright (c) 2025 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/config/features.gni") +import("//build/test.gni") +import("//foundation/filemanagement/file_api/file_api.gni") + +ohos_fuzztest("FileioFuzzTest") { + module_out_path = "file_api/file_api" + fuzz_config_file = "${file_api_path}/interfaces/test/fuzztest/fileio_fuzzer" + + include_dirs = [ + "${file_api_path}/interfaces/test/fuzztest/fuzz_common", + "${file_api_path}/interfaces/kits/c/fileio", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ + "${file_api_path}/interfaces/test/fuzztest/fileio_fuzzer/fileio_fuzzer.cpp", + ] + defines = [] + deps = [ + "${file_api_path}/interfaces/kits/c/fileio:ohfileio", + "${file_api_path}/interfaces/kits/native:fileio_native", + ] + external_deps = [ "c_utils:utils" ] +} + +group("fuzztest") { + testonly = true + deps = [ ":FileioFuzzTest" ] +} diff --git a/interfaces/test/fuzztest/fileio_fuzzer/corpus/init b/interfaces/test/fuzztest/fileio_fuzzer/corpus/init new file mode 100644 index 000000000..7ade8a0fa --- /dev/null +++ b/interfaces/test/fuzztest/fileio_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 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/interfaces/test/fuzztest/fileio_fuzzer/fileio_fuzzer.cpp b/interfaces/test/fuzztest/fileio_fuzzer/fileio_fuzzer.cpp new file mode 100644 index 000000000..b221ef2c3 --- /dev/null +++ b/interfaces/test/fuzztest/fileio_fuzzer/fileio_fuzzer.cpp @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2025 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 "fileio_fuzzer.h" + +#include "fileio.h" +#include "fileapi_fuzzer_helper.h" + +namespace OHOS { +constexpr size_t U32_AT_SIZE = 4; +constexpr uint32_t PATH_MAX_LEN = 4096; +const std::string TEST_PATH = "/data/test/"; + +bool FileioGetFileLocationRandomFuzzTest(FuzzData &fuzzData, size_t size) +{ + fuzzData.ResetData(size); + // uriLength + uint32_t uriLength = fuzzData.GetData(); + uriLength = uriLength % PATH_MAX_LEN; + // uri + uint32_t len = fuzzData.GetData(); + len = len % PATH_MAX_LEN; + + std::string uriStr = fuzzData.GetStringFromData(len); + if (uriLength > uriStr.size()) { + uriLength = uriStr.size(); + } + // location + int location = 0; + + OH_FileIO_GetFileLocation(const_cast(uriStr.c_str()), uriLength, &location); + return true; +} + +bool FileioGetFileLocationRealFuzzTest(FuzzData &fuzzData, size_t size) +{ + fuzzData.ResetData(size); + bool boolData = fuzzData.GetData(); + // uri + std::string uriStr; + // uriLength + uint32_t uriLength = 0; + if (boolData) { + uriStr = TEST_PATH; + uriLength = uriStr.size(); + } + + // location + int location = 0; + + OH_FileIO_GetFileLocation(const_cast(uriStr.c_str()), uriLength, &location); + return true; +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + if ((data == nullptr) || (size < (OHOS::U32_AT_SIZE << 1))) { + return 0; + } + + OHOS::FuzzData fuzzData(data, size); + OHOS::FileioGetFileLocationRandomFuzzTest(fuzzData, size); + OHOS::FileioGetFileLocationRealFuzzTest(fuzzData, size); + return 0; +} diff --git a/interfaces/test/fuzztest/fileio_fuzzer/fileio_fuzzer.h b/interfaces/test/fuzztest/fileio_fuzzer/fileio_fuzzer.h new file mode 100644 index 000000000..40606a76e --- /dev/null +++ b/interfaces/test/fuzztest/fileio_fuzzer/fileio_fuzzer.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 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 FILEIO_FUZZER_H +#define FILEIO_FUZZER_H + +#define FUZZ_PROJECT_NAME "fileio_fuzzer" + +#endif \ No newline at end of file diff --git a/interfaces/test/fuzztest/fileio_fuzzer/project.xml b/interfaces/test/fuzztest/fileio_fuzzer/project.xml new file mode 100644 index 000000000..8cd958170 --- /dev/null +++ b/interfaces/test/fuzztest/fileio_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/interfaces/test/fuzztest/fuzz_common/fileapi_fuzzer_helper.h b/interfaces/test/fuzztest/fuzz_common/fileapi_fuzzer_helper.h new file mode 100644 index 000000000..c82491be9 --- /dev/null +++ b/interfaces/test/fuzztest/fuzz_common/fileapi_fuzzer_helper.h @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2025 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 FILEAPI_FUZZER_HELPER_H +#define FILEAPI_FUZZER_HELPER_H + +#include "securec.h" +#include + +namespace OHOS { +class FuzzData { +public: + FuzzData(const uint8_t *data, size_t size) + { + baseData = data; + baseSize = size; + } + + void ResetData(size_t newSize) + { + if (newSize <= baseSize) { + baseSize = newSize; + } + basePos = 0; + } + + template + T GetData() + { + T object{}; + size_t objectSize = sizeof(object); + if (baseData == nullptr || objectSize > baseSize - basePos) { + return object; + } + if (memcpy_s(&object, objectSize, baseData + basePos, objectSize) != EOK) { + return {}; + } + basePos = basePos + objectSize; + return object; + } + + size_t GetRemainSize() const + { + return baseSize - basePos; + } + + std::string GetStringFromData(int len) + { + if (len <= 0 || baseData == nullptr || basePos >= baseSize) { + return ""; + } + + std::string cstr; + for (int i = 0; i < len; ++i) { + if (basePos >= baseSize) { + basePos = 0; + } + char tmp = GetData(); + if (tmp == '\0') { + cstr.push_back('1'); + } else { + cstr.push_back(tmp); + } + } + return cstr; + } + +private: + const uint8_t *baseData{nullptr}; + size_t baseSize{0}; + size_t basePos{0}; +}; +} // namespace OHOS +#endif // FILEAPI_FUZZER_HELPER_H diff --git a/interfaces/test/fuzztest/hyperaio_fuzzer/BUILD.gn b/interfaces/test/fuzztest/hyperaio_fuzzer/BUILD.gn new file mode 100644 index 000000000..73f9f489b --- /dev/null +++ b/interfaces/test/fuzztest/hyperaio_fuzzer/BUILD.gn @@ -0,0 +1,60 @@ +# Copyright (c) 2025 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/config/features.gni") +import("//build/test.gni") +import("//foundation/filemanagement/file_api/file_api.gni") + +ohos_fuzztest("HyperaioFuzzTest") { + module_out_path = "file_api/file_api" + fuzz_config_file = "${file_api_path}/interfaces/test/fuzztest/hyperaio_fuzzer" + file_api_feature_hyperaio = true + + include_dirs = [ + "${file_api_path}/interfaces/test/fuzztest/fuzz_common", + "${file_api_path}/interfaces/kits/hyperaio/include", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = + [ "${file_api_path}/interfaces/test/fuzztest/hyperaio_fuzzer/hyperaio_fuzzer.cpp" ] + defines = [] + deps = [ + "${file_api_path}/interfaces/kits/hyperaio:HyperAio", + ] + + external_deps = [ + "c_utils:utils", + "hilog:libhilog", + "ipc:ipc_core", + "access_token:libaccesstoken_sdk", + "hitrace:hitrace_meter", + ] + if (file_api_feature_hyperaio) { + external_deps += [ "liburing:liburing" ] + } + + if (file_api_feature_hyperaio) { + defines += [ "HYPERAIO_USE_LIBURING" ] + } +} + +group("fuzztest") { + testonly = true + deps = [ ":HyperaioFuzzTest" ] +} diff --git a/interfaces/test/fuzztest/hyperaio_fuzzer/corpus/init b/interfaces/test/fuzztest/hyperaio_fuzzer/corpus/init new file mode 100644 index 000000000..7ade8a0fa --- /dev/null +++ b/interfaces/test/fuzztest/hyperaio_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 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/interfaces/test/fuzztest/hyperaio_fuzzer/hyperaio_fuzzer.cpp b/interfaces/test/fuzztest/hyperaio_fuzzer/hyperaio_fuzzer.cpp new file mode 100644 index 000000000..aafaeb3ae --- /dev/null +++ b/interfaces/test/fuzztest/hyperaio_fuzzer/hyperaio_fuzzer.cpp @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2025 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 "hyperaio_fuzzer.h" + +#include +#include "hyperaio.h" +#include "fileapi_fuzzer_helper.h" + +namespace OHOS { +constexpr size_t U32_AT_SIZE = 4; +constexpr size_t U64_AT_SIZE = 8; +constexpr uint32_t URING_QUEUE_SIZE = 512; +std::function)> callBack = + [](std::unique_ptr response) {}; + +bool HyperaioStartOpenReqsFuzzTest(FuzzData &fuzzData, size_t size) +{ + fuzzData.ResetData(size); + std::unique_ptr hyperAio_ = std::make_unique(); + hyperAio_->CtxInit(&callBack); + + // userData + uint64_t userData = fuzzData.GetData(); + + OHOS::HyperAio::OpenInfo openInfo = {0, O_RDWR, 0, nullptr, userData}; + OHOS::HyperAio::OpenReqs openReqs = {1, &openInfo}; + + hyperAio_->StartOpenReqs(&openReqs); + hyperAio_->DestroyCtx(); + + return true; +} + +bool HyperaioStartOpenReqsBatchFuzzTest(FuzzData &fuzzData, size_t size) +{ + fuzzData.ResetData(size); + std::unique_ptr hyperAio_ = std::make_unique(); + hyperAio_->CtxInit(&callBack); + + // userData + uint64_t userData = fuzzData.GetData(); + uint32_t batchSize = fuzzData.GetData(); + batchSize = batchSize % URING_QUEUE_SIZE; + + auto openInfos = std::make_unique(batchSize); + for (uint32_t i = 0; i < batchSize; ++i) { + openInfos[i].dfd = 0; + openInfos[i].flags = O_RDWR; + openInfos[i].mode = 0; + openInfos[i].path = nullptr; + openInfos[i].userData = userData + i; + } + OHOS::HyperAio::OpenReqs openReqs = {batchSize, openInfos.get()}; + + hyperAio_->StartOpenReqs(&openReqs); + hyperAio_->DestroyCtx(); + + return true; +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ +#ifdef HYPERAIO_USE_LIBURING + /* Run your code on data */ + if (data == nullptr || size <= (OHOS::U32_AT_SIZE + OHOS::U64_AT_SIZE)) { + return 0; + } + + OHOS::FuzzData fuzzData(data, size); + OHOS::HyperaioStartOpenReqsFuzzTest(fuzzData, size); + OHOS::HyperaioStartOpenReqsBatchFuzzTest(fuzzData, size); +#endif + return 0; +} diff --git a/interfaces/test/fuzztest/hyperaio_fuzzer/hyperaio_fuzzer.h b/interfaces/test/fuzztest/hyperaio_fuzzer/hyperaio_fuzzer.h new file mode 100644 index 000000000..990c437c6 --- /dev/null +++ b/interfaces/test/fuzztest/hyperaio_fuzzer/hyperaio_fuzzer.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 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 HYPERAIO_FUZZER_H +#define HYPERAIO_FUZZER_H + +#define FUZZ_PROJECT_NAME "hyperaio_fuzzer" + +#endif \ No newline at end of file diff --git a/interfaces/test/fuzztest/hyperaio_fuzzer/project.xml b/interfaces/test/fuzztest/hyperaio_fuzzer/project.xml new file mode 100644 index 000000000..8cd958170 --- /dev/null +++ b/interfaces/test/fuzztest/hyperaio_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/interfaces/test/fuzztest/resource/ohos_test.xml b/interfaces/test/fuzztest/resource/ohos_test.xml new file mode 100644 index 000000000..10c699790 --- /dev/null +++ b/interfaces/test/fuzztest/resource/ohos_test.xml @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + diff --git a/interfaces/test/fuzztest/resource/rust_test.txt b/interfaces/test/fuzztest/resource/rust_test.txt new file mode 100644 index 000000000..2c1f3e490 --- /dev/null +++ b/interfaces/test/fuzztest/resource/rust_test.txt @@ -0,0 +1,30 @@ +abc +222 +333 +444 +555 +666 +777 +888 +999 +000 +abc +222 +333 +444 +555 +666 +777 +888 +999 +000 +abc +222 +333 +444 +555 +666 +777 +888 +999 +000 diff --git a/interfaces/test/fuzztest/rustcutfilename_fuzzer/BUILD.gn b/interfaces/test/fuzztest/rustcutfilename_fuzzer/BUILD.gn new file mode 100644 index 000000000..41cf17bac --- /dev/null +++ b/interfaces/test/fuzztest/rustcutfilename_fuzzer/BUILD.gn @@ -0,0 +1,43 @@ +# Copyright (c) 2025 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/config/features.gni") +import("//build/test.gni") +import("//foundation/filemanagement/file_api/file_api.gni") + +ohos_fuzztest("RustCutfilenameFuzzTest") { + module_out_path = "file_api/file_api" + fuzz_config_file = "${file_api_path}/interfaces/test/fuzztest/rustcutfilename_fuzzer" + + include_dirs = [ + "${file_api_path}/interfaces/test/fuzztest/fuzz_common", + "${file_api_path}/interfaces/kits/rust/include", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "${file_api_path}/interfaces/test/fuzztest/rustcutfilename_fuzzer/rustcutfilename_fuzzer.cpp" ] + defines = [] + deps = [ "${file_api_path}/interfaces/kits/rust:rust_file" ] + + external_deps = [ "c_utils:utils" ] +} + +group("fuzztest") { + testonly = true + deps = [ ":RustCutfilenameFuzzTest" ] +} diff --git a/interfaces/test/fuzztest/rustcutfilename_fuzzer/corpus/init b/interfaces/test/fuzztest/rustcutfilename_fuzzer/corpus/init new file mode 100644 index 000000000..7ade8a0fa --- /dev/null +++ b/interfaces/test/fuzztest/rustcutfilename_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 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/interfaces/test/fuzztest/rustcutfilename_fuzzer/project.xml b/interfaces/test/fuzztest/rustcutfilename_fuzzer/project.xml new file mode 100644 index 000000000..8cd958170 --- /dev/null +++ b/interfaces/test/fuzztest/rustcutfilename_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/interfaces/test/fuzztest/rustcutfilename_fuzzer/rustcutfilename_fuzzer.cpp b/interfaces/test/fuzztest/rustcutfilename_fuzzer/rustcutfilename_fuzzer.cpp new file mode 100644 index 000000000..a5d394afe --- /dev/null +++ b/interfaces/test/fuzztest/rustcutfilename_fuzzer/rustcutfilename_fuzzer.cpp @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2025 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 "rustcutfilename_fuzzer.h" + +#include +#include +#include "fileapi_fuzzer_helper.h" +#include "rust_file.h" + +namespace OHOS { +constexpr uint32_t PATH_MAX_LEN = 4096; +constexpr size_t U32_AT_SIZE = 4; +const std::string FILE_PATH = "/data/test/rust_test.txt"; +bool RustCutFilenameFuzzTest(FuzzData &fuzzData, size_t size) +{ + fuzzData.ResetData(size); + uint32_t strLen = fuzzData.GetData(); + strLen = strLen % PATH_MAX_LEN; + std::string fileStr = fuzzData.GetStringFromData(strLen); + uint32_t cutLen = fuzzData.GetData(); + Str *str = CutFileName(fileStr.c_str(), cutLen); + StrFree(str); + return true; +} + +bool RustCutFilenameRealFuzzTest(FuzzData &fuzzData, size_t size) +{ + fuzzData.ResetData(size); + uint32_t strLen = fuzzData.GetData(); + strLen = strLen % PATH_MAX_LEN; + std::string fileStr = fuzzData.GetStringFromData(strLen); + uint32_t cutLen = fuzzData.GetData(); + cutLen = cutLen % PATH_MAX_LEN; + Str *str = CutFileName(fileStr.c_str(), cutLen); + StrFree(str); + return true; +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + if (data == nullptr || size <= (OHOS::U32_AT_SIZE << 1)) { + return 0; + } + + OHOS::FuzzData fuzzData(data, size); + OHOS::RustCutFilenameFuzzTest(fuzzData, size); + OHOS::RustCutFilenameRealFuzzTest(fuzzData, size); + return 0; +} diff --git a/interfaces/test/fuzztest/rustcutfilename_fuzzer/rustcutfilename_fuzzer.h b/interfaces/test/fuzztest/rustcutfilename_fuzzer/rustcutfilename_fuzzer.h new file mode 100644 index 000000000..a506e56a7 --- /dev/null +++ b/interfaces/test/fuzztest/rustcutfilename_fuzzer/rustcutfilename_fuzzer.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 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 RUSTCUTFILENAME_FUZZER_H +#define RUSTCUTFILENAME_FUZZER_H + +#define FUZZ_PROJECT_NAME "rustcutfilename_fuzzer" + +#endif \ No newline at end of file diff --git a/interfaces/test/fuzztest/rustgetparentstr_fuzzer/BUILD.gn b/interfaces/test/fuzztest/rustgetparentstr_fuzzer/BUILD.gn new file mode 100644 index 000000000..0ad35c65e --- /dev/null +++ b/interfaces/test/fuzztest/rustgetparentstr_fuzzer/BUILD.gn @@ -0,0 +1,43 @@ +# Copyright (c) 2025 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/config/features.gni") +import("//build/test.gni") +import("//foundation/filemanagement/file_api/file_api.gni") + +ohos_fuzztest("RustGetParentStrFuzzTest") { + module_out_path = "file_api/file_api" + fuzz_config_file = "${file_api_path}/interfaces/test/fuzztest/rustgetparentstr_fuzzer" + + include_dirs = [ + "${file_api_path}/interfaces/test/fuzztest/fuzz_common", + "${file_api_path}/interfaces/kits/rust/include", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "${file_api_path}/interfaces/test/fuzztest/rustgetparentstr_fuzzer/rustgetparentstr_fuzzer.cpp" ] + defines = [] + deps = [ "${file_api_path}/interfaces/kits/rust:rust_file" ] + + external_deps = [ "c_utils:utils" ] +} + +group("fuzztest") { + testonly = true + deps = [ ":RustGetParentStrFuzzTest" ] +} diff --git a/interfaces/test/fuzztest/rustgetparentstr_fuzzer/corpus/init b/interfaces/test/fuzztest/rustgetparentstr_fuzzer/corpus/init new file mode 100644 index 000000000..7ade8a0fa --- /dev/null +++ b/interfaces/test/fuzztest/rustgetparentstr_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 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/interfaces/test/fuzztest/rustgetparentstr_fuzzer/project.xml b/interfaces/test/fuzztest/rustgetparentstr_fuzzer/project.xml new file mode 100644 index 000000000..8cd958170 --- /dev/null +++ b/interfaces/test/fuzztest/rustgetparentstr_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/interfaces/test/fuzztest/rustgetparentstr_fuzzer/rustgetparentstr_fuzzer.cpp b/interfaces/test/fuzztest/rustgetparentstr_fuzzer/rustgetparentstr_fuzzer.cpp new file mode 100644 index 000000000..2845cb74c --- /dev/null +++ b/interfaces/test/fuzztest/rustgetparentstr_fuzzer/rustgetparentstr_fuzzer.cpp @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 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 "rustgetparentstr_fuzzer.h" + +#include +#include +#include "fileapi_fuzzer_helper.h" +#include "rust_file.h" + +namespace OHOS { +constexpr size_t U32_AT_SIZE = 4; +const char FILE_STR[] = "/data/test/rust_test.txt"; +bool RustGetParentStrRandomFuzzTest(FuzzData &fuzzData, size_t size) +{ + fuzzData.ResetData(size); + int fd = fuzzData.GetData(); + Str *str = GetParent(fd); + StrFree(str); + return true; +} + +bool RustGetParentStrRealFuzzTest(FuzzData &fuzzData, size_t size) +{ + fuzzData.ResetData(size); + int fd = open(FILE_STR, O_RDWR); + Str *str = GetParent(fd); + StrFree(str); + close(fd); + return true; +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + if (data == nullptr || size <= OHOS::U32_AT_SIZE) { + return 0; + } + + OHOS::FuzzData fuzzData(data, size); + OHOS::RustGetParentStrRandomFuzzTest(fuzzData, size); + OHOS::RustGetParentStrRealFuzzTest(fuzzData, size); + return 0; +} diff --git a/interfaces/test/fuzztest/rustgetparentstr_fuzzer/rustgetparentstr_fuzzer.h b/interfaces/test/fuzztest/rustgetparentstr_fuzzer/rustgetparentstr_fuzzer.h new file mode 100644 index 000000000..d18b42eb8 --- /dev/null +++ b/interfaces/test/fuzztest/rustgetparentstr_fuzzer/rustgetparentstr_fuzzer.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 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 RUSTGETPARENTSTR_FUZZER_H +#define RUSTGETPARENTSTR_FUZZER_H + +#define FUZZ_PROJECT_NAME "rustgetparentstr_fuzzer" + +#endif \ No newline at end of file diff --git a/interfaces/test/fuzztest/rustmkdirs_fuzzer/BUILD.gn b/interfaces/test/fuzztest/rustmkdirs_fuzzer/BUILD.gn new file mode 100644 index 000000000..8520f824b --- /dev/null +++ b/interfaces/test/fuzztest/rustmkdirs_fuzzer/BUILD.gn @@ -0,0 +1,43 @@ +# Copyright (c) 2025 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/config/features.gni") +import("//build/test.gni") +import("//foundation/filemanagement/file_api/file_api.gni") + +ohos_fuzztest("RustMkdirsFuzzTest") { + module_out_path = "file_api/file_api" + fuzz_config_file = "${file_api_path}/interfaces/test/fuzztest/rustmkdirs_fuzzer" + + include_dirs = [ + "${file_api_path}/interfaces/test/fuzztest/fuzz_common", + "${file_api_path}/interfaces/kits/rust/include", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "${file_api_path}/interfaces/test/fuzztest/rustmkdirs_fuzzer/rustmkdirs_fuzzer.cpp" ] + defines = [] + deps = [ "${file_api_path}/interfaces/kits/rust:rust_file" ] + + external_deps = [ "c_utils:utils" ] +} + +group("fuzztest") { + testonly = true + deps = [ ":RustMkdirsFuzzTest" ] +} diff --git a/interfaces/test/fuzztest/rustmkdirs_fuzzer/corpus/init b/interfaces/test/fuzztest/rustmkdirs_fuzzer/corpus/init new file mode 100644 index 000000000..7ade8a0fa --- /dev/null +++ b/interfaces/test/fuzztest/rustmkdirs_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 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/interfaces/test/fuzztest/rustmkdirs_fuzzer/project.xml b/interfaces/test/fuzztest/rustmkdirs_fuzzer/project.xml new file mode 100644 index 000000000..8cd958170 --- /dev/null +++ b/interfaces/test/fuzztest/rustmkdirs_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/interfaces/test/fuzztest/rustmkdirs_fuzzer/rustmkdirs_fuzzer.cpp b/interfaces/test/fuzztest/rustmkdirs_fuzzer/rustmkdirs_fuzzer.cpp new file mode 100644 index 000000000..ca05537eb --- /dev/null +++ b/interfaces/test/fuzztest/rustmkdirs_fuzzer/rustmkdirs_fuzzer.cpp @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2025 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 "rustmkdirs_fuzzer.h" + +#include +#include +#include "fileapi_fuzzer_helper.h" +#include "rust_file.h" + +namespace OHOS { +constexpr size_t U16_AT_SIZE = 2; +constexpr int MAX_LEN = 255; +const std::string FILE_PATH = "/data/test/test"; +const std::string SLASH = "/"; +bool RustMkdirsFuzzTest(FuzzData &fuzzData, size_t size) +{ + fuzzData.ResetData(size); + bool isSingle = fuzzData.GetData(); + uint8_t len = fuzzData.GetData(); + if (len > MAX_LEN - FILE_PATH.length()) { + len = MAX_LEN - FILE_PATH.length(); + } + std::string fileStr = FILE_PATH + fuzzData.GetStringFromData(len); + int realLen = fileStr.length(); + char filePath[realLen + 1]; + for (uint8_t i = 0; i < realLen; i++) { + filePath[i] = fileStr[i]; + } + filePath[realLen] = '\0'; + Mkdirs(filePath, isSingle ? SINGLE : MULTIPLE); + int ret = remove(filePath); + if (ret) { + return false; + } + return true; +} + +bool RustMkdirsMultipleFuzzTest(FuzzData &fuzzData, size_t size) +{ + fuzzData.ResetData(size); + bool isSingle = fuzzData.GetData(); + + uint8_t slashTimes = fuzzData.GetData(); + int maxSlash = (MAX_LEN - FILE_PATH.length()) >> 1; + if (slashTimes > maxSlash) { + slashTimes = maxSlash; + } + if (slashTimes == 0) { + slashTimes++; + } + int randomPartLen = (MAX_LEN - FILE_PATH.length()) / slashTimes - SLASH.length(); + std::string randomPart = fuzzData.GetStringFromData(randomPartLen); + std::string fileStr = FILE_PATH; + for (size_t i = 0; i < slashTimes; i++) { + fileStr += randomPart + SLASH; + } + + int realLen = fileStr.length(); + char filePath[realLen + 1]; + for (uint8_t i = 0; i < realLen; i++) { + filePath[i] = fileStr[i]; + } + filePath[realLen] = '\0'; + Mkdirs(filePath, isSingle ? SINGLE : MULTIPLE); + int ret = remove(filePath); + if (ret) { + return false; + } + return true; +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + if (data == nullptr || size <= OHOS::U16_AT_SIZE) { + return 0; + } + + int prefixLen = OHOS::FILE_PATH.length(); + char filePathPrefix[prefixLen + 1]; + for (uint8_t i = 0; i < prefixLen; i++) { + filePathPrefix[i] = OHOS::FILE_PATH[i]; + } + filePathPrefix[prefixLen] = '\0'; + Mkdirs(filePathPrefix, SINGLE); + + OHOS::FuzzData fuzzData(data, size); + OHOS::RustMkdirsFuzzTest(fuzzData, size); + OHOS::RustMkdirsMultipleFuzzTest(fuzzData, size); + return 0; +} diff --git a/interfaces/test/fuzztest/rustmkdirs_fuzzer/rustmkdirs_fuzzer.h b/interfaces/test/fuzztest/rustmkdirs_fuzzer/rustmkdirs_fuzzer.h new file mode 100644 index 000000000..b92866116 --- /dev/null +++ b/interfaces/test/fuzztest/rustmkdirs_fuzzer/rustmkdirs_fuzzer.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 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 RUSTMKDIRS_FUZZER_H +#define RUSTMKDIRS_FUZZER_H + +#define FUZZ_PROJECT_NAME "rustmkdirs_fuzzer" + +#endif \ No newline at end of file diff --git a/interfaces/test/fuzztest/rustreaderiterator_fuzzer/BUILD.gn b/interfaces/test/fuzztest/rustreaderiterator_fuzzer/BUILD.gn new file mode 100644 index 000000000..71e4b1b7d --- /dev/null +++ b/interfaces/test/fuzztest/rustreaderiterator_fuzzer/BUILD.gn @@ -0,0 +1,43 @@ +# Copyright (c) 2025 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/config/features.gni") +import("//build/test.gni") +import("//foundation/filemanagement/file_api/file_api.gni") + +ohos_fuzztest("RustReadIteratorFuzzTest") { + module_out_path = "file_api/file_api" + fuzz_config_file = "${file_api_path}/interfaces/test/fuzztest/rustreaderiterator_fuzzer" + + include_dirs = [ + "${file_api_path}/interfaces/test/fuzztest/fuzz_common", + "${file_api_path}/interfaces/kits/rust/include", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "${file_api_path}/interfaces/test/fuzztest/rustreaderiterator_fuzzer/rustreaderiterator_fuzzer.cpp" ] + defines = [] + deps = [ "${file_api_path}/interfaces/kits/rust:rust_file" ] + + external_deps = [ "c_utils:utils" ] +} + +group("fuzztest") { + testonly = true + deps = [ ":RustReadIteratorFuzzTest" ] +} diff --git a/interfaces/test/fuzztest/rustreaderiterator_fuzzer/corpus/init b/interfaces/test/fuzztest/rustreaderiterator_fuzzer/corpus/init new file mode 100644 index 000000000..7ade8a0fa --- /dev/null +++ b/interfaces/test/fuzztest/rustreaderiterator_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 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/interfaces/test/fuzztest/rustreaderiterator_fuzzer/project.xml b/interfaces/test/fuzztest/rustreaderiterator_fuzzer/project.xml new file mode 100644 index 000000000..8cd958170 --- /dev/null +++ b/interfaces/test/fuzztest/rustreaderiterator_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/interfaces/test/fuzztest/rustreaderiterator_fuzzer/rustreaderiterator_fuzzer.cpp b/interfaces/test/fuzztest/rustreaderiterator_fuzzer/rustreaderiterator_fuzzer.cpp new file mode 100644 index 000000000..fbf833a64 --- /dev/null +++ b/interfaces/test/fuzztest/rustreaderiterator_fuzzer/rustreaderiterator_fuzzer.cpp @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2025 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 "rustreaderiterator_fuzzer.h" + +#include +#include "fileapi_fuzzer_helper.h" +#include "rust_file.h" + +namespace OHOS { +constexpr size_t U32_AT_SIZE = 4; +const std::string FILE_PATH = "/data/test/rust_test.txt"; +bool RustReaderIteratorFuzzTest(FuzzData &fuzzData, size_t size) +{ + fuzzData.ResetData(size); + void *iter = ReaderIterator(const_cast(FILE_PATH.c_str())); + uint32_t lines = fuzzData.GetData(); + Str *ret = NextLine(iter); + while (ret != nullptr && lines-- > 0) { + StrFree(ret); + ret = NextLine(iter); + } + StrFree(ret); + DropReaderIterator(iter); + return true; +} + +bool RustReaderIteratorRandomFuzzTest(FuzzData &fuzzData, size_t size) +{ + fuzzData.ResetData(size); + uint32_t len = fuzzData.GetData(); + std::string path = fuzzData.GetStringFromData(len); + void *iter = ReaderIterator(const_cast(path.c_str())); + DropReaderIterator(iter); + return true; +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + if (data == nullptr || size <= (OHOS::U32_AT_SIZE)) { + return false; + } + + OHOS::FuzzData fuzzData(data, size); + OHOS::RustReaderIteratorFuzzTest(fuzzData, size); + OHOS::RustReaderIteratorRandomFuzzTest(fuzzData, size); + return 0; +} diff --git a/interfaces/test/fuzztest/rustreaderiterator_fuzzer/rustreaderiterator_fuzzer.h b/interfaces/test/fuzztest/rustreaderiterator_fuzzer/rustreaderiterator_fuzzer.h new file mode 100644 index 000000000..aae3c383e --- /dev/null +++ b/interfaces/test/fuzztest/rustreaderiterator_fuzzer/rustreaderiterator_fuzzer.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 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 RUSTREADERITERATOR_FUZZER_H +#define RUSTREADERITERATOR_FUZZER_H + +#define FUZZ_PROJECT_NAME "rustreaderiterator_fuzzer" + +#endif \ No newline at end of file diff --git a/interfaces/test/fuzztest/rustseek_fuzzer/BUILD.gn b/interfaces/test/fuzztest/rustseek_fuzzer/BUILD.gn new file mode 100644 index 000000000..6d62368f0 --- /dev/null +++ b/interfaces/test/fuzztest/rustseek_fuzzer/BUILD.gn @@ -0,0 +1,43 @@ +# Copyright (c) 2025 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/config/features.gni") +import("//build/test.gni") +import("//foundation/filemanagement/file_api/file_api.gni") + +ohos_fuzztest("RustSeekFuzzTest") { + module_out_path = "file_api/file_api" + fuzz_config_file = "${file_api_path}/interfaces/test/fuzztest/rustseek_fuzzer" + + include_dirs = [ + "${file_api_path}/interfaces/test/fuzztest/fuzz_common", + "${file_api_path}/interfaces/kits/rust/include", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "${file_api_path}/interfaces/test/fuzztest/rustseek_fuzzer/rustseek_fuzzer.cpp" ] + defines = [] + deps = [ "${file_api_path}/interfaces/kits/rust:rust_file" ] + + external_deps = [ "c_utils:utils" ] +} + +group("fuzztest") { + testonly = true + deps = [ ":RustSeekFuzzTest" ] +} diff --git a/interfaces/test/fuzztest/rustseek_fuzzer/corpus/init b/interfaces/test/fuzztest/rustseek_fuzzer/corpus/init new file mode 100644 index 000000000..7ade8a0fa --- /dev/null +++ b/interfaces/test/fuzztest/rustseek_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 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/interfaces/test/fuzztest/rustseek_fuzzer/project.xml b/interfaces/test/fuzztest/rustseek_fuzzer/project.xml new file mode 100644 index 000000000..8cd958170 --- /dev/null +++ b/interfaces/test/fuzztest/rustseek_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/interfaces/test/fuzztest/rustseek_fuzzer/rustseek_fuzzer.cpp b/interfaces/test/fuzztest/rustseek_fuzzer/rustseek_fuzzer.cpp new file mode 100644 index 000000000..aa46eb9de --- /dev/null +++ b/interfaces/test/fuzztest/rustseek_fuzzer/rustseek_fuzzer.cpp @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2025 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 "rustseek_fuzzer.h" + +#include +#include +#include "fileapi_fuzzer_helper.h" +#include "rust_file.h" + +namespace OHOS { +constexpr size_t U32_AT_SIZE = 4; +constexpr size_t U64_AT_SIZE = 8; +const char FILE_STR[] = "/data/test/rust_test.txt"; +bool RustSeekRandomFuzzTest(FuzzData &fuzzData, size_t size) +{ + fuzzData.ResetData(size); + int fd = fuzzData.GetData(); + if (fd < 0) { + return true; + } + long long offset = fuzzData.GetData(); + Lseek(fd, offset, START); + return true; +} + +bool RustSeekStartFuzzTest(FuzzData &fuzzData, size_t size) +{ + fuzzData.ResetData(size); + int fd = open(FILE_STR, O_RDWR); + if (fd < 0) { + return true; + } + long long offset = fuzzData.GetData(); + Lseek(fd, offset, START); + close(fd); + return true; +} + +bool RustSeekCurrentFuzzTest(FuzzData &fuzzData, size_t size) +{ + fuzzData.ResetData(size); + int fd = open(FILE_STR, O_RDWR); + if (fd < 0) { + return true; + } + long long offset = fuzzData.GetData(); + Lseek(fd, offset, CURRENT); + close(fd); + return true; +} + +bool RustSeekEndFuzzTest(FuzzData &fuzzData, size_t size) +{ + fuzzData.ResetData(size); + int fd = open(FILE_STR, O_RDWR); + if (fd < 0) { + return true; + } + long long offset = fuzzData.GetData(); + Lseek(fd, offset, END); + close(fd); + return true; +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + if (data == nullptr || size <= OHOS::U32_AT_SIZE + OHOS::U64_AT_SIZE) { + return 0; + } + + OHOS::FuzzData fuzzData(data, size); + OHOS::RustSeekRandomFuzzTest(fuzzData, size); + OHOS::RustSeekStartFuzzTest(fuzzData, size); + OHOS::RustSeekCurrentFuzzTest(fuzzData, size); + OHOS::RustSeekEndFuzzTest(fuzzData, size); + return 0; +} diff --git a/interfaces/test/fuzztest/rustseek_fuzzer/rustseek_fuzzer.h b/interfaces/test/fuzztest/rustseek_fuzzer/rustseek_fuzzer.h new file mode 100644 index 000000000..c4610720e --- /dev/null +++ b/interfaces/test/fuzztest/rustseek_fuzzer/rustseek_fuzzer.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 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 RUSTSEEK_FUZZER_H +#define RUSTSEEK_FUZZER_H + +#define FUZZ_PROJECT_NAME "rustseek_fuzzer" + +#endif \ No newline at end of file -- Gitee