Add support for afl-fuzz testing

"make fuzz" creates a simple executable that de-serialises stdin
and re-serialises to stdout.
"make fuzz_testcases" extracts the smaller json test cases into
a testcases directory.

The library can then be fuzzed as follows:
    CC=afl-clang-fast make fuzz
    make fuzz_testcases
    mkdir out
    afl-fuzz -i testcases -o out ./fuzz
This commit is contained in:
Michael Macnair 2016-02-12 09:35:08 +00:00
parent 61fe90f998
commit 9e500b49ac
2 changed files with 54 additions and 2 deletions

42
test/fuzz.cpp Normal file
View file

@ -0,0 +1,42 @@
/*
__ _____ _____ _____
__| | __| | | | JSON for Modern C++ (fuzz test support)
| | |__ | | | | | | version 2.0.0
|_____|_____|_____|_|___| https://github.com/nlohmann/json
To run under afl:
afl-fuzz -i testcases -o output ./fuzz
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
*/
#include <json.hpp>
using json = nlohmann::json;
int main()
{
json *jp;
#ifdef __AFL_HAVE_MANUAL_CONTROL
while (__AFL_LOOP(1000)) {
#endif
jp = new json();
json j = *jp;
try {
j << std::cin;
} catch (std::invalid_argument e) {
std::cout << "Invalid argument in parsing" << e.what() << '\n';
}
if (j.find("foo") != j.end()) {
std::cout << "Found a foo";
}
std::cout << j.type() << j << std::endl;
delete jp;
#ifdef __AFL_HAVE_MANUAL_CONTROL
}
#endif
}