#proptest #bitcoin #testing

bin+lib bitcoin-proptest

Proptest strategies for Bitcoin-related code

2 releases

0.0.1-alpha.2 Apr 28, 2024
0.0.1-alpha.1 Apr 27, 2024

#9 in #proptest

Download history 312/week @ 2024-04-25 15/week @ 2024-05-02

327 downloads per month

Apache-2.0 OR MIT

40KB
719 lines

Rust Bitcoin Proptest Strategies

Proptest strategies for Bitcoin-related code.

This collection of generators (called strategies in proptest's terminology) helps developers of Bitcoin-related software written in Rust supply random data – both valid and invalid – to their property-based tests.

Visit documentation for details.

Example

#[cfg(test)]
mod tests {
  proptest! {
     #[test]
     // generates valid hex-encoded public keys, both compressed and uncompressed
     fn pubkey_parsing(s in prop::secp256k1::public_key::valid::hex()) {
        prop_assert!(s.len() == 66 || s.len() == 130);
        prop_assert!(my_parser(s).is_ok());
     }
  }
}

lib.rs:

Rust Bitcoin Proptest Strategies

Proptest strategies for Bitcoin-related code.

This collection of generators (called strategies in proptest's terminology) helps developers of Bitcoin-related software written in Rust supply random data – both valid and invalid – to their property-based tests.

Basic rules

  1. unless documentation specifies, the strategy generates anything, i. e. all possible variants, both valid and invalid, as long as type permits, for example:
  • “Generates descriptors” means any type, both valid and invalid
  • “Generates valid descriptors” means any type, all valid
  1. every strategy also has an adjacent strategy called build where inputs (in form of strategies) can be specified, for example:

Organization

All strategies are placed in module prop sorted by category. Inside each category, there is typically a nested set of modules that further specify properties of the strategies. The modules are usually organized as prop::category::what::what::what::how() where how() is the strategy returning a specific format of what, for example: prop::secp256k1::public_key::invalid::compressed::vec().

If a strategy (in form of function) is placed at the same level as other modules, they typically generate mix of all modules at that level. For example prop::secp256k1::public_key::valid::vec() will generate byte vectors of both compressed an uncompressed public keys and prop::secp256k1::public_key::vec() will generate valid and invalid, compressed as well as uncompressed public keys.

Example

#[cfg(test)]
mod tests {
proptest! {
#[test]
// generates valid hex-encoded public keys, both compressed and uncompressed
fn pubkey_parsing(s in prop::secp256k1::public_key::valid::hex()) {
prop_assert!(s.len() == 66 || s.len() == 130);
prop_assert!(my_parser(s).is_ok());
}
}
}

Dependencies

~13MB
~166K SLoC