You write the code, we write the tests.
Fuzzcover is a set of utilities to use libFuzzer to generate tests to achieve a high function coverage of code of your choice. It bases on the ideas presented in the talk Testing Legacy Code - Fuzzing for Better Input Data by Tina Ulbrich and Niel Waldren on Meeting C++ 2019.
While the talk presents the overall framework of how fuzz testing can be used to generate test cases, fuzzcover aims to automate as many steps as possible.
- Implement the
fuzzcover_interface
defined ininclude/fuzzcover/fuzzcover.hpp
:- choose the test input type (e.g.,
double
,std::string
, or any other type from which you can feed the test function - implement function
value_from_bytes
which generates a test input from some bytes generated by libFuzzer - implement function
test_function
which takes a test input and executes whatever code you want to improve the test coverage for
- choose the test input type (e.g.,
- Implement three binaries defined in
CMakeLists.txt
, which boils down to replacing the include and class name to fit your implementation from step 1:fuzzer
to plug libFuzzer to your test functiontester
to execute your test function with the generated test inputs, usually to check for coveragedumper
to dump the test inputs in JSON format
- Run the targets to do the actual jobs:
run_fuzzer
runs the fuzzer and populates a directory "corpus" with test inputscheck_coverage
generates a coverage report for the generated test inputs in the "corpus" directoryminimize_corpus_files
removes test inputs from the "corpus" directory which do no contribute to coverage; the result is usually a much smaller test suiteminimize_corpus_length
tries to reduce the size of each test input; this is usually necessary, because libFuzzer may generate long byte sequences from which functionvalue_from_bytes
only consumes a small prefix
Folder examples
contains several example implementations that generate tests for various functions from JSON for Modern C++. If you do not change anything in the code, examples/json/fuzzer_lexer_scan_number.hpp
is used and ready to go. Running the targets in step 3 above will generate enough strings to cover all lines of the number parser of the JSON library.
- generate an actual test suite (e.g., for Catch or ApprovalTests.cpp
- improve scripts to accelerate corpus reduction (e.g., using binary search)
- remove lcov dependency
- add CI
- add tutorial
You can sponsor my work at GitHub Sponsors.
The code is licensed under the MIT License:
Copyright © 2019 Niels Lohmann
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.