Skip to content

koushik-ms/fuzzcover

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fuzzcover - test suite generation for C++

You write the code, we write the tests.

About

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.

Process

  1. Implement the fuzzcover_interface defined in include/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
  2. 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 function
    • tester to execute your test function with the generated test inputs, usually to check for coverage
    • dumper to dump the test inputs in JSON format
  3. Run the targets to do the actual jobs:
    • run_fuzzer runs the fuzzer and populates a directory "corpus" with test inputs
    • check_coverage generates a coverage report for the generated test inputs in the "corpus" directory
    • minimize_corpus_files removes test inputs from the "corpus" directory which do no contribute to coverage; the result is usually a much smaller test suite
    • minimize_corpus_length tries to reduce the size of each test input; this is usually necessary, because libFuzzer may generate long byte sequences from which function value_from_bytes only consumes a small prefix

Examples

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.

Dependencies

Roadmap

  • 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

Support

You can sponsor my work at GitHub Sponsors.

License

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.

About

test suite generation for C++

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 98.9%
  • Other 1.1%