From 9b7232b6baca38d1f823a942dc5082f1fb3f26bd Mon Sep 17 00:00:00 2001 From: wujianlin Date: Wed, 8 May 2024 17:37:14 +0800 Subject: [PATCH] Add fuzz test for directory_ex Issue:https://gitee.com/openharmony/commonlibrary_c_utils/issues/I9NKIJ?from=project-issue Signed-off-by: wujianlin --- base/test/fuzztest/BUILD.gn | 1 + base/test/fuzztest/directory_fuzzer/BUILD.gn | 33 +++++++ .../fuzztest/directory_fuzzer/corpus/init | 14 +++ .../directory_fuzzer/directory_fuzzer.cpp | 96 +++++++++++++++++++ .../directory_fuzzer/directory_fuzzer.h | 21 ++++ .../fuzztest/directory_fuzzer/project.xml | 25 +++++ 6 files changed, 190 insertions(+) create mode 100644 base/test/fuzztest/directory_fuzzer/BUILD.gn create mode 100644 base/test/fuzztest/directory_fuzzer/corpus/init create mode 100644 base/test/fuzztest/directory_fuzzer/directory_fuzzer.cpp create mode 100644 base/test/fuzztest/directory_fuzzer/directory_fuzzer.h create mode 100644 base/test/fuzztest/directory_fuzzer/project.xml diff --git a/base/test/fuzztest/BUILD.gn b/base/test/fuzztest/BUILD.gn index 61243ac..dcca95f 100644 --- a/base/test/fuzztest/BUILD.gn +++ b/base/test/fuzztest/BUILD.gn @@ -18,6 +18,7 @@ group("fuzztest") { deps = [] deps += [ # deps file + "directory_fuzzer:DirectoryFuzzTest", "parcel_fuzzer:ParcelFuzzTest", "refbase_fuzzer:RefbaseFuzzTest", "timer_fuzzer:TimerFuzzTest", diff --git a/base/test/fuzztest/directory_fuzzer/BUILD.gn b/base/test/fuzztest/directory_fuzzer/BUILD.gn new file mode 100644 index 0000000..1486ab9 --- /dev/null +++ b/base/test/fuzztest/directory_fuzzer/BUILD.gn @@ -0,0 +1,33 @@ +# 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. +import("//build/test.gni") + +##############################fuzztest########################################## +ohos_fuzztest("DirectoryFuzzTest") { + 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 = [ "directory_fuzzer.cpp" ] +} +############################################################################### diff --git a/base/test/fuzztest/directory_fuzzer/corpus/init b/base/test/fuzztest/directory_fuzzer/corpus/init new file mode 100644 index 0000000..e4ceac1 --- /dev/null +++ b/base/test/fuzztest/directory_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# 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. + +FUZZ \ No newline at end of file diff --git a/base/test/fuzztest/directory_fuzzer/directory_fuzzer.cpp b/base/test/fuzztest/directory_fuzzer/directory_fuzzer.cpp new file mode 100644 index 0000000..c478422 --- /dev/null +++ b/base/test/fuzztest/directory_fuzzer/directory_fuzzer.cpp @@ -0,0 +1,96 @@ +/* + * 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 "directory_fuzzer.h" + +#include "fuzz_log.h" +#include "fuzzer/FuzzedDataProvider.h" +#include "directory_ex.h" +#include + +using namespace std; + +namespace OHOS { +const uint8_t MAX_DIR_LENGTH = 15; + +void DirectoryTestFunc(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider) +{ + FUZZ_LOGI("DirectoryTestFunc start"); + GetCurrentProcPath(); + + string testDir = "/data/test_dir/"; + ForceCreateDirectory(testDir); + + string dirs[6] = { + "/data/test_dir/level1_1", + "/data/test_dir/level1_2", + "/data/test_dir/level1_2/level2_1", + "/data/test_dir/level1_2/level2_2", + "/data/test_dir/level1_2/level2_2/level3_1", + "/data/test_dir/level1_3", + }; + + string resultfiles[9] = { + "/data/test_dir/level1_1/test_file", + "/data/test_dir/level1_2/level2_2/level3_1/test_file_1", + "/data/test_dir/level1_2/level2_2/level3_1/test_file_2", + "/data/test_dir/level1_2/level2_2/test_file_1", + "/data/test_dir/level1_2/level2_2/test_file_2", + "/data/test_dir/level1_2/level2_2/test_file_3", + "/data/test_dir/level1_2/level2_2/test_file_4", + "/data/test_dir/level1_2/test_file", + "/data/test_dir/level1_3/test_file", + }; + + for (auto &path : dirs) { + ForceCreateDirectory(path); + } + + for (auto &filepath : resultfiles) { + ofstream(filepath, fstream::out); + } + + string dirName = dataProvider->ConsumeRandomLengthString(MAX_DIR_LENGTH); + string dirPath = testDir + dirName; + ForceCreateDirectory(dirPath); + + string realPath1; + string realPath2; + string overLongePath = testDir + dataProvider->ConsumeRandomLengthString(PATH_MAX); + PathToRealPath(overLongePath, realPath1); + PathToRealPath(dirPath, realPath2); + + string fileName = dataProvider->ConsumeRandomLengthString(MAX_DIR_LENGTH); + ExtractFileExt(fileName); + + string fileFullPath = testDir + fileName; + mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO; + ChangeModeFile(fileFullPath, mode); + + IsEmptyFolder(testDir); + ForceRemoveDirectory(testDir); + + FUZZ_LOGI("DirectoryTestFunc end"); +} + +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + FuzzedDataProvider dataProvider(data, size); + OHOS::DirectoryTestFunc(data, size, &dataProvider); + return 0; +} diff --git a/base/test/fuzztest/directory_fuzzer/directory_fuzzer.h b/base/test/fuzztest/directory_fuzzer/directory_fuzzer.h new file mode 100644 index 0000000..5fd95e0 --- /dev/null +++ b/base/test/fuzztest/directory_fuzzer/directory_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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. + */ + +#ifndef DIRECTORY_FUZZER_H +#define DIRECTORY_FUZZER_H + +#define FUZZ_PROJECT_NAME "directory_fuzzer" + +#endif // DIRECTORY_FUZZER_H diff --git a/base/test/fuzztest/directory_fuzzer/project.xml b/base/test/fuzztest/directory_fuzzer/project.xml new file mode 100644 index 0000000..9b0a87e --- /dev/null +++ b/base/test/fuzztest/directory_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 10000 + + 300 + + 4096 + + -- Gitee