Skip to content

Commit

Permalink
Add an initial cjson fuzz test
Browse files Browse the repository at this point in the history
Signed-off-by: Geoff Hutchison <[email protected]>
  • Loading branch information
ghutchis committed Jan 8, 2025
1 parent 77e93ad commit 07a67f6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
15 changes: 15 additions & 0 deletions tests/io/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,18 @@ foreach(TestName ${tests})
add_test(NAME "Io-${TestName}"
COMMAND AvogadroIOTests "--gtest_filter=${TestName}Test.*")
endforeach()

option(ENABLE_FUZZ "Enable fuzz testing." OFF)

if(ENABLE_FUZZ)
add_definitions(-DFUZZTEST_FUZZING_MODE=on)
# fuzztest uses abseil-cpp
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_VISIBILITY_PRESET default)
add_subdirectory(fuzztest)

fuzztest_setup_fuzzing_flags()
add_executable(AvogadroFuzzTests fuzztest.cpp)
target_link_libraries(AvogadroFuzzTests PRIVATE Avogadro::IO)
link_fuzztest(AvogadroFuzzTests)
endif()
32 changes: 32 additions & 0 deletions tests/io/fuzztest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/******************************************************************************
This source file is part of the Avogadro project.
This source code is released under the 3-Clause BSD License, (see "LICENSE").
******************************************************************************/

#include "fuzztest/fuzztest.h"
#include "iotests.h"

#include <gtest/gtest.h>

#include <avogadro/core/matrix.h>
#include <avogadro/core/molecule.h>
#include <avogadro/core/unitcell.h>

#include <avogadro/io/cjsonformat.h>

using Avogadro::Core::Molecule;
using Avogadro::Io::CjsonFormat;

static const std::string cjsonDir = std::string(AVOGADRO_DATA) + "/data/cjson/";

void readCjson(const std::string& data)
{
CjsonFormat cjson;
Molecule molecule;
bool success = cjson.readString(data, molecule);
EXPECT_TRUE(success);
EXPECT_EQ(cjson.error(), "");
}

FUZZ_TEST(AvogadroFuzzTests, readCjson)
.WithSeeds(fuzztest::ReadFilesFromDirectory(cjsonDir));

0 comments on commit 07a67f6

Please sign in to comment.