About
The Arbitrary
crate lets you construct arbitrary instances of a type.
This crate is primarily intended to be combined with a fuzzer like libFuzzer
and cargo-fuzz
or
AFL, and to help you turn the raw,
untyped byte buffers that they produce into well-typed, valid, structured
values. This allows you to combine structure-aware test case generation with
coverage-guided, mutation-based fuzzers.
Documentation
Read the API documentation on docs.rs
!
Example
Say you're writing a color conversion library, and you have an Rgb
struct to
represent RGB colors. You might want to implement Arbitrary
for Rgb
so that
you could take arbitrary Rgb
instances in a test function that asserts some
property (for example, asserting that RGB converted to HSL and converted back to
RGB always ends up exactly where we started).
Automatically Deriving Arbitrary
Automatically deriving the Arbitrary
trait is the recommended way to implement
Arbitrary
for your types.
Automatically deriving Arbitrary
requires you to enable the "derive"
cargo
feature:
# Cargo.toml
[]
= { = "1", = ["derive"] }
And then you can simply add #[derive(Arbitrary)]
annotations to your types:
// rgb.rs
use Arbitrary;
Customizing single fields
This can be particular handy if your structure uses a type that does not implement Arbitrary
or you want to have more customization for particular fields.
Implementing Arbitrary
By Hand
Alternatively, you can write an Arbitrary
implementation by hand:
// rgb.rs
use ;
License
Licensed under dual MIT or Apache-2.0 at your choice.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.