Skip to content

Commit

Permalink
Merge pull request #1927 from ghutchis/fuzz-testing
Browse files Browse the repository at this point in the history
Add some initial fuzz IO tests
  • Loading branch information
ghutchis authored Jan 20, 2025
2 parents 103f9e5 + 55060be commit 126e6d0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
24 changes: 24 additions & 0 deletions tests/io/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,27 @@ foreach(TestName ${tests})
add_test(NAME "Io-${TestName}"
COMMAND AvogadroIOTests "--gtest_filter=${TestName}Test.*")
endforeach()

if (DEFINED ENV{LIB_FUZZING_ENGINE})
option(ENABLE_FUZZ "Enable fuzz testing." ON)
else()
option(ENABLE_FUZZ "Enable fuzz testing." OFF)
endif()

if(ENABLE_FUZZ OR DEFINED ENV{LIB_FUZZING_ENGINE})
# todo - make this into a loop for multiple formats
set(fuzz
cjson
cml
sdf
pdb
xyz
)
foreach(fuzzName ${fuzz})
add_executable(fuzz_${fuzzName} fuzztest.cpp)
target_compile_definitions(fuzz_${fuzzName} PRIVATE -DFUZZ_INPUT_FORMAT="${fuzzName}")
target_compile_options(fuzz_${fuzzName} PRIVATE -g -fsanitize=fuzzer)
target_link_options(fuzz_${fuzzName} PRIVATE -fsanitize=fuzzer)
target_link_libraries(fuzz_${fuzzName} PRIVATE Avogadro::IO $ENV{LIB_FUZZING_ENGINE})
endforeach()
endif()
23 changes: 23 additions & 0 deletions tests/io/fuzztest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/******************************************************************************
This source file is part of the Avogadro project.
This source code is released under the 3-Clause BSD License, (see "LICENSE").
******************************************************************************/

#include <avogadro/core/molecule.h>
#include <avogadro/io/fileformat.h>
#include <avogadro/io/fileformatmanager.h>

using Avogadro::Core::Molecule;
using Avogadro::Io::FileFormatManager;

// FUZZ_INPUT_FORMAT is defined in the build system
// e.g., "cjson", "sdf", "xyz", etc.
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, size_t Size)
{
std::string input(reinterpret_cast<const char*>(Data), Size);

Molecule molecule;
FileFormatManager::instance().readString(molecule, input, FUZZ_INPUT_FORMAT);

return 0;
}

0 comments on commit 126e6d0

Please sign in to comment.