From 4c5fb3a8e943d03b57fe32405d3c375aaa35eb69 Mon Sep 17 00:00:00 2001 From: Vasiliy Kiryanov Date: Sat, 2 Nov 2024 15:40:21 -0400 Subject: [PATCH] made code Win and Unix/Linux/MacOS compatible --- include/test/helper/fixture.hpp | 3 +- test/corpora/invalid_param_name/1 | 2 +- test/corpora/invalid_param_name/2 | 2 +- test/corpora/invalid_param_name/3 | 2 +- test/helper/fixture.cpp | 81 ++++++++++++++++++++++--------- 5 files changed, 63 insertions(+), 27 deletions(-) diff --git a/include/test/helper/fixture.hpp b/include/test/helper/fixture.hpp index f7885147..01879210 100644 --- a/include/test/helper/fixture.hpp +++ b/include/test/helper/fixture.hpp @@ -20,7 +20,6 @@ # define __STUMPLESS_TEST_HELPER_FIXTURE_HPP # include -# include # include # include # include @@ -107,6 +106,6 @@ struct stumpless_test_data { * directory as an array and corresponding size stored as length. */ stumpless_test_data -load_corpus_folder( const std::string& name ); +load_corpus_folder( const char* name ); #endif /* __STUMPLESS_TEST_HELPER_FIXTURE_HPP */ diff --git a/test/corpora/invalid_param_name/1 b/test/corpora/invalid_param_name/1 index e6675258..a5124030 100644 --- a/test/corpora/invalid_param_name/1 +++ b/test/corpora/invalid_param_name/1 @@ -1 +1 @@ -par=am +par=am \ No newline at end of file diff --git a/test/corpora/invalid_param_name/2 b/test/corpora/invalid_param_name/2 index 9cc6a849..40430b86 100644 --- a/test/corpora/invalid_param_name/2 +++ b/test/corpora/invalid_param_name/2 @@ -1 +1 @@ -par]am +par]am \ No newline at end of file diff --git a/test/corpora/invalid_param_name/3 b/test/corpora/invalid_param_name/3 index 546514e0..60a83811 100644 --- a/test/corpora/invalid_param_name/3 +++ b/test/corpora/invalid_param_name/3 @@ -1 +1 @@ -par\"a +par\"am \ No newline at end of file diff --git a/test/helper/fixture.cpp b/test/helper/fixture.cpp index 38f65b8c..b74b32b4 100644 --- a/test/helper/fixture.cpp +++ b/test/helper/fixture.cpp @@ -16,6 +16,10 @@ * limitations under the License. */ +#ifndef HAVE_WINDOWS_H + #include +#endif + #include #include #include @@ -90,29 +94,62 @@ load_corpus( const string& name ) { } stumpless_test_data -load_corpus_folder( const string& name){ +load_corpus_folder( const char* name ) { + char** test_strings = NULL; + int fileIndex = 0; + +#if defined(HAVE_WINDOWS_H) + // Windows directory traversal + char corpora_dir[MAX_PATH]; + snprintf( corpora_dir, sizeof( corpora_dir ), "%s\\%s\\*", FUZZ_CORPORA_DIR, name ); + + WIN32_FIND_DATA findFileData; + HANDLE hFind = FindFirstFile( corpora_dir, &findFileData ); + + if ( hFind != INVALID_HANDLE_VALUE ) { + do { + if ( !( findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) ) { + char filePath[MAX_PATH]; + snprintf( filePath, MAX_PATH, "%s\\%s\\%s", FUZZ_CORPORA_DIR, name, findFileData.cFileName ); + + const char* test_string = load_corpus( filePath ); + if (test_string != NULL) { + test_strings = (char**) realloc( test_strings, sizeof(char*) * ( fileIndex + 1 ) ) ; + test_strings[fileIndex] = (char*) test_string; + ++fileIndex; + } + } + } while ( FindNextFile( hFind, &findFileData ) != 0 ); + FindClose(hFind); + } - DIR *dir; - struct dirent *ent; - string corpora_dir ( FUZZ_CORPORA_DIR ); - int fileIndex = 0; - - char **test_strings; - if((dir = opendir((corpora_dir + "/" + name).c_str())) != NULL){ - while((ent = readdir(dir)) != NULL){ - const char *test_string = load_corpus(name + "/" + ent->d_name); - if(test_string != NULL){ - if(fileIndex > 0){ - test_strings = (char **) realloc(test_strings, sizeof(char *) * (fileIndex + 1)); +#else + // POSIX (macOS Unix/Linux) directory traversal + char corpora_dir[PATH_MAX]; + snprintf( corpora_dir, sizeof( corpora_dir ), "%s/%s", FUZZ_CORPORA_DIR, name ); + + DIR* dir = opendir( corpora_dir ); + dirent* ent; + if (dir != NULL) { + while ( ( ent = readdir(dir) ) != NULL ) { + if ( ent->d_type == DT_REG ) { // Only process regular files + char filePath[1024]; + snprintf( filePath, sizeof(filePath), "%s/%s", corpora_dir, ent->d_name ); + + const char* test_string = load_corpus( filePath ); + if ( test_string != NULL ) { + test_strings = (char**) realloc( test_strings, sizeof(char*) * ( fileIndex + 1 ) ); + test_strings[fileIndex] = (char*) test_string; + ++fileIndex; + } + } } - else{ - test_strings = (char **) malloc(sizeof(char *)); - } - test_strings[fileIndex] = (char *)test_string; - ++fileIndex; - } + closedir( dir ); } - closedir(dir); - } - return {fileIndex, test_strings}; +#endif + + stumpless_test_data result; + result.length = fileIndex; + result.test_strings = test_strings; + return result; }