diff --git a/base/test/fuzztest/BUILD.gn b/base/test/fuzztest/BUILD.gn index 61243ac948c902c7337294755121eddbd931b952..dcca95f7f4ba46553bdc18bd6141cb5c4d57df10 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 0000000000000000000000000000000000000000..1486ab99fa0ae9d7608ee9573f8669896d539ad2 --- /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 0000000000000000000000000000000000000000..e4ceac1bcd4e3b3427eb63cea0c28304064333cc --- /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 0000000000000000000000000000000000000000..c478422236333c81ee6ea627d9a2fe062fd2f5dd --- /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 0000000000000000000000000000000000000000..5fd95e0a5d695d6422054fd2abfb5acdf6baba81 --- /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 0000000000000000000000000000000000000000..9b0a87e51ab3e28c253b9d33a726e98f6a9465a8 --- /dev/null +++ b/base/test/fuzztest/directory_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 10000 + + 300 + + 4096 + +