Skip to content

Commit

Permalink
Add Proptest and a couple of tests. (#56)
Browse files Browse the repository at this point in the history
* Add proptest and a couple of initial tests to confirm fixes for prs 53 and 54

* Move proptests to separate mod

* proptests behind std feature

* put dev dep in the right place
  • Loading branch information
jsdw authored May 8, 2024
1 parent c9fcc18 commit b65e4cb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
3 changes: 2 additions & 1 deletion scale-decode/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ trybuild = "1.0.72"
# Enable the scale-info feature for testing.
scale-bits = { version = "0.6.0", default-features = false, features = ["scale-info"] }
primitive-types = { version = "0.12.0", default-features = false, features = ["scale-info"] }
scale-type-resolver = { version = "0.2.0", default-features = false, features = ["scale-info"] }
scale-type-resolver = { version = "0.2.0", default-features = false, features = ["scale-info"] }
proptest = "1.4.0"
36 changes: 36 additions & 0 deletions scale-decode/src/visitor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1119,4 +1119,40 @@ mod test {
.unwrap();
assert_eq!(decoded, ("hello".to_string(), "world".to_string()));
}

// A couple of tests to check that invalid input doesn't lead to panics
// when we attempt to decode it to certain types.
#[cfg(feature = "std")]
mod proptests {
use super::*;
use proptest::prelude::*;

proptest! {
#[test]
fn invalid_strings_dont_panic(bytes in any::<Vec<u8>>()) {
let (id, types) = make_type::<String>();
let _ = decode_with_visitor(&mut &*bytes, id, &types, ValueVisitor::new());
}

#[test]
fn invalid_bitvecs_dont_panic(bytes in any::<Vec<u8>>()) {
use bitvec::{
vec::BitVec,
order::{Lsb0, Msb0},
};

let (id, types) = make_type::<BitVec<u8,Lsb0>>();
let _ = decode_with_visitor(&mut &*bytes, id, &types, ValueVisitor::new());

let (id, types) = make_type::<BitVec<u8,Msb0>>();
let _ = decode_with_visitor(&mut &*bytes, id, &types, ValueVisitor::new());

let (id, types) = make_type::<BitVec<u32,Lsb0>>();
let _ = decode_with_visitor(&mut &*bytes, id, &types, ValueVisitor::new());

let (id, types) = make_type::<BitVec<u32,Msb0>>();
let _ = decode_with_visitor(&mut &*bytes, id, &types, ValueVisitor::new());
}
}
}
}

0 comments on commit b65e4cb

Please sign in to comment.