Skip to content

Commit

Permalink
made code Win and Unix/Linux/MacOS compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
vasiliyk committed Nov 2, 2024
1 parent 6bf8e73 commit 4c5fb3a
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 27 deletions.
3 changes: 1 addition & 2 deletions include/test/helper/fixture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
# define __STUMPLESS_TEST_HELPER_FIXTURE_HPP

# include <cstdlib>
# include <dirent.h>
# include <gtest/gtest.h>
# include <string>
# include <stumpless.h>
Expand Down Expand Up @@ -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 */
2 changes: 1 addition & 1 deletion test/corpora/invalid_param_name/1
Original file line number Diff line number Diff line change
@@ -1 +1 @@
par=am
par=am
2 changes: 1 addition & 1 deletion test/corpora/invalid_param_name/2
Original file line number Diff line number Diff line change
@@ -1 +1 @@
par]am
par]am
2 changes: 1 addition & 1 deletion test/corpora/invalid_param_name/3
Original file line number Diff line number Diff line change
@@ -1 +1 @@
par\"a
par\"am
81 changes: 59 additions & 22 deletions test/helper/fixture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
* limitations under the License.
*/

#ifndef HAVE_WINDOWS_H
#include <dirent.h>
#endif

#include <cstddef>
#include <fstream>
#include <string>
Expand Down Expand Up @@ -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;
}

0 comments on commit 4c5fb3a

Please sign in to comment.