From c027909accbe8dbe9ba54ff0d3cf6ffaad53b60c Mon Sep 17 00:00:00 2001 From: Chris Wolfe Date: Sat, 19 May 2018 10:58:45 -0500 Subject: [PATCH] add a test file for regression tests, cmakelist.txt to get it build correctly --- fuzz/CMakeLists.txt | 43 ++++++++++++++++++++++++++++++++ fuzz/regression_runner.cpp | 50 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 fuzz/CMakeLists.txt create mode 100644 fuzz/regression_runner.cpp diff --git a/fuzz/CMakeLists.txt b/fuzz/CMakeLists.txt new file mode 100644 index 00000000..efa036f3 --- /dev/null +++ b/fuzz/CMakeLists.txt @@ -0,0 +1,43 @@ +FIND_PACKAGE (GTest REQUIRED) +FIND_PACKAGE (ZLIB REQUIRED) +FIND_PACKAGE (Boost REQUIRED COMPONENTS system filesystem) + +INCLUDE_DIRECTORIES ( + ${GTEST_INCLUDE_DIRS} + ${ZLIB_INCLUDE_DIRS} + ${Boost_INCLUDE_DIRS} +) + +LIST (APPEND check_PROGRAMS + regression_runner.cpp +) + +LINK_DIRECTORIES ( + ${Boost_LIBRARY_DIRS} +) + +FOREACH (source_file ${check_PROGRAMS}) + GET_FILENAME_COMPONENT (source_file_we ${source_file} NAME_WE) + ADD_EXECUTABLE ( + ${source_file_we} + ${source_file} + ) + TARGET_LINK_LIBRARIES (${source_file_we} + msgpackc + ${GTEST_BOTH_LIBRARIES} + ${ZLIB_LIBRARIES} + ${CMAKE_THREAD_LIBS_INIT} + ${Boost_LIBRARIES} + ) + ADD_TEST (${source_file_we} ${source_file_we}) + IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") + SET_PROPERTY (TARGET ${source_file_we} APPEND_STRING PROPERTY COMPILE_FLAGS "-Wall -Wextra -Wno-mismatched-tags -g") + IF ("${MSGPACK_SAN}" STREQUAL "ASAN") + SET (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-omit-frame-pointer -fsanitize=address") + SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-omit-frame-pointer -fsanitize=address") + ELSEIF ("${MSGPACK_SAN}" STREQUAL "UBSAN") + SET (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined") + SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined") + ENDIF() + ENDIF() +ENDFOREACH () diff --git a/fuzz/regression_runner.cpp b/fuzz/regression_runner.cpp new file mode 100644 index 00000000..7b67222c --- /dev/null +++ b/fuzz/regression_runner.cpp @@ -0,0 +1,50 @@ +#include +#include + +#include +#include +#include + +#include "unpack_pack_fuzzer.cpp" + +using ::testing::TestWithParam; +using ::testing::ValuesIn; + + +std::vector ListDirectory(const std::string& path) { + std::vector v; + + boost::filesystem::path p(path); + boost::filesystem::directory_iterator f{p}; + + if(boost::filesystem::is_directory(p)) { + while (f != boost::filesystem::directory_iterator{}) { + v.push_back((*f++).path().string()); + } + } + return v; +} + +class UnpackPackFuzzerRegressionTest : public ::testing::TestWithParam { +public: +}; + +TEST_P(UnpackPackFuzzerRegressionTest, Returns0) { + auto fpath = GetParam(); + std::ifstream in(fpath, std::ifstream::binary); + if (!in) { + FAIL() << fpath << " not found"; + } + in.seekg(0, in.end); + size_t length = in.tellg(); + in.seekg(0, in.beg); + std::vector bytes(length); + in.read(bytes.data(), bytes.size()); + assert(in); + EXPECT_EQ(0, LLVMFuzzerTestOneInput(reinterpret_cast(bytes.data()), + bytes.size())); +} + +INSTANTIATE_TEST_CASE_P(UnpackPackFuzzerRegressions, + UnpackPackFuzzerRegressionTest, + ::testing::ValuesIn(ListDirectory("../../fuzz/unpack_pack_fuzzer_regressions")));