Add page about creating unit tests from crash reproduction data.#50
Add page about creating unit tests from crash reproduction data.#50julianharty wants to merge 1 commit intorust-fuzz:masterfrom
Conversation
These are based on the approach used for holo-routing#82 - this time Claude Code was instructed to create the unit tests and test them on interesting commits in the project's recent history after cargo fuzz was added. The approach is now documented in rust-fuzz/book#50 .
fitzgen
left a comment
There was a problem hiding this comment.
Thanks!
Another thing you can do is check the crashes into your repo (or a submodule) and write a test that does something like
// my_crate/src/lib.rs
pub fn my_fuzz(data: &[u8]) {
// ...
}
#[test]
fn crash_regression_tests() -> anyhow::Result<()> {
for entry in fs::read_dir("path/to/crashes")? {
let entry = entry?;
let data = fs::read(entry.path())?;
my_fuzz(data);
}
Ok(())
}
// fuzz/fuzz_targets/my_fuzz.rs
libfuzzer_sys::fuzz_target!(|data| {
my_crate::my_fuzz(data);
});Better yet, use libtest_mimic to make each file its own test so that they can run in parallel, be ran one at a time, etc.
Mind expanding the docs to include this technique?
|
@fitzgen Thanks these great suggestions which complement the approach I've described. I'm happy to incorporate them into this PR. Big pictureI envisage 3 options devs could use:
Have I captured your suggestions adequately? And what else would be useful for readers of this topic in the book? Implementation details[Background context] [Questions for you]
I'm unfamiliar with |
I realised it's feasible to create unit tests based on the reproduction tests and the contents of the relevant fuzz target. I've documented the process based on the tutorial in this book. You're welcome to keep whatever's useful and modify it to suit the rest of the book. I'm also happy to revise the contents based on your feedback.