extern crate carrier; extern crate proptest; use carrier::packet::*; use proptest::prelude::*; fn arb_frame() -> impl Strategy { prop_oneof![ (any::(), any::>()).prop_map(|(a, b)| { Frame::Header { stream: a, payload: b } }), (any::(), any::(), any::>()).prop_map(|(a, b, c)| { Frame::Stream { stream: a, order: b, payload: c, } }), (any::(), any::>()).prop_map(|(a, mut b)| { b.sort_unstable(); Frame::Ack { delay: a as u64, acked: b, } }), any::().prop_map(|_| Frame::Ping), any::().prop_map(|_| Frame::Disconnect { reason: DisconnectReason::None, }), (any::(), any::()).prop_map(|(a, b)| { Frame::Close { stream: a, order: b, reason: CloseReason::None, } }), (any::>(), any::()).prop_map(|(a, b)| { Frame::Config { timeout: a, sleeping: b, } }), ] } proptest! { #[test] fn proptest_decode_frames(frames in prop::collection::vec(arb_frame(), 0..100)) { let mut w = Vec::new(); for f in &frames { f.encode(LATEST_VERSION, &mut w).unwrap(); } let frames2 = Frame::decode(LATEST_VERSION, &w[..]).unwrap(); assert_eq!(frames, frames2); } #[test] fn proptest_decode_junk(r: Vec) { Frame::decode(LATEST_VERSION, &r[..]).ok(); } #[test] fn proptest_re_packets(mut route: u64, dir: bool, counter: u64, payload: Vec) { route -= route % 2; let pkt = EncryptedPacket { version: 0x08, route, direction: if dir { RoutingDirection::Initiator2Responder } else { RoutingDirection::Responder2Initiator}, counter, payload, }; let pkt2 = EncryptedPacket::decode(&pkt.clone().encode()).unwrap(); println!("{:?} <> {:?}", pkt, pkt2.0); assert_eq!(pkt, pkt2.0); } }