Skip to content

Commit

Permalink
testing: Add basic fuzz test harnesses
Browse files Browse the repository at this point in the history
  • Loading branch information
nathaniel-brough committed Oct 9, 2023
1 parent 2d161c5 commit 333c79c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
9 changes: 8 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
cmake_minimum_required(VERSION 3.15.0)
cmake_minimum_required(VERSION 3.22.0)

# For MSVC RUNTIME LIBRARY, need CMP0091=NEW and cmake 3.15+
cmake_policy(SET CMP0091 NEW)

include(CMakeDependentOption)

# Version info
set(QUANTLIB_VERSION_MAJOR 1)
set(QUANTLIB_VERSION_MINOR 32)
Expand Down Expand Up @@ -42,6 +44,7 @@ set(QL_INSTALL_CMAKEDIR "lib/cmake/${PACKAGE_NAME}" CACHE STRING
option(QL_BUILD_BENCHMARK "Build benchmark" ON)
option(QL_BUILD_EXAMPLES "Build examples" ON)
option(QL_BUILD_TEST_SUITE "Build test suite" ON)
cmake_dependent_option(QL_BUILD_FUZZ_TEST_SUITE "Build fuzz test suite" ON "'${CMAKE_CXX_COMPILER_ID}' MATCHES 'Clang'" OFF)
option(QL_ENABLE_OPENMP "Detect and use OpenMP" OFF)
option(QL_ENABLE_PARALLEL_UNIT_TEST_RUNNER "Enable the parallel unit test runner" OFF)
option(QL_ENABLE_SESSIONS "Singletons return different instances for different sessions" OFF)
Expand Down Expand Up @@ -269,6 +272,10 @@ if (QL_BUILD_TEST_SUITE OR QL_BUILD_BENCHMARK)
add_subdirectory(test-suite)
endif()

if (QL_BUILD_FUZZ_TEST_SUITE)
add_subdirectory(fuzz-test-suite)
endif()

# CPack support (make package, make package_source)
set(CPACK_PACKAGE_VERSION_MAJOR ${QUANTLIB_VERSION_MAJOR})
set(CPACK_PACKAGE_VERSION_MINOR ${QUANTLIB_VERSION_MINOR})
Expand Down
21 changes: 21 additions & 0 deletions fuzz-test-suite/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Determine the flags for fuzzing. Use OSS-Fuzz's configuration if available, otherwise fall back to defaults.
if(DEFINED ENV{LIB_FUZZING_ENGINE})
set(FUZZING_ENGINE $ENV{LIB_FUZZING_ENGINE})
set(FUZZING_COMPILE_FLAGS "")
set(FUZZING_LINK_FLAGS "${FUZZING_ENGINE}")
else()
set(FUZZING_COMPILE_FLAGS "-fsanitize=fuzzer")
set(FUZZING_LINK_FLAGS "-fsanitize=fuzzer")
endif()

# Define the fuzz target
add_executable(DateParserFuzzer dateparserfuzzer.cpp)

# Apply the determined flags
set_target_properties(DateParserFuzzer PROPERTIES
COMPILE_FLAGS "${FUZZING_COMPILE_FLAGS}"
LINK_FLAGS "${FUZZING_LINK_FLAGS}"
)

# Link QuantLib and any other necessary libraries
target_link_libraries(DateParserFuzzer ql_library ${QL_THREAD_LIBRARIES})
24 changes: 24 additions & 0 deletions fuzz-test-suite/dateparserfuzzer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <ql/time/asx.hpp>
#include <ql/time/date.hpp>
#include <ql/time/ecb.hpp>
#include <ql/time/imm.hpp>
#include <ql/time/timeunit.hpp>
#include <ql/utilities/dataparsers.hpp>
#include <string>

#ifndef __clang__
#pragma message("Fuzzer headers are available from clang, other compilers have not been tested.")
#endif
#include <fuzzer/FuzzedDataProvider.h>

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
FuzzedDataProvider fdp(data, size);
constexpr int kMaxString = 100;
try {
(void)QuantLib::DateParser::parseFormatted(fdp.ConsumeRandomLengthString(kMaxString), "%Y-%m-%d");
(void)QuantLib::DateParser::parseISO(fdp.ConsumeRandomLengthString(kMaxString));
} catch (const std::exception& e) {
// Handle or ignore exceptions
}
return 0;
}

0 comments on commit 333c79c

Please sign in to comment.