-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy patharbitrary.rs
122 lines (108 loc) · 4.48 KB
/
arbitrary.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use proc_macro::TokenStream;
use proc_macro2::{Ident, TokenStream as TokenStream2};
use quote::{quote, ToTokens};
/// If `compact` or `rlp` is passed to `derive_arbitrary`, this function will generate the
/// corresponding proptest roundtrip tests.
///
/// It accepts an optional integer number for the number of proptest cases. Otherwise, it will set
/// it at 1000.
pub fn maybe_generate_tests(
args: TokenStream,
type_ident: &impl ToTokens,
mod_tests: &Ident,
) -> TokenStream2 {
// Same as proptest
let mut default_cases = 256;
let mut traits = vec![];
let mut roundtrips = vec![];
let mut additional_tests = vec![];
let mut is_crate = false;
let mut iter = args.into_iter().peekable();
// we check if there's a crate argument which is used from inside the codecs crate directly
if let Some(arg) = iter.peek() {
if arg.to_string() == "crate" {
is_crate = true;
iter.next();
}
}
for arg in iter {
if arg.to_string() == "compact" {
let path = if is_crate {
quote! { use crate::Compact; }
} else {
quote! { use reth_codecs::Compact; }
};
traits.push(path);
roundtrips.push(quote! {
{
let mut buf = vec![];
let len = field.clone().to_compact(&mut buf);
let (decoded, _): (super::#type_ident, _) = Compact::from_compact(&buf, len);
assert_eq!(field, decoded, "maybe_generate_tests::compact");
}
});
} else if arg.to_string() == "rlp" {
traits.push(quote! { use alloy_rlp::{Encodable, Decodable}; });
roundtrips.push(quote! {
{
let mut buf = vec![];
let len = field.encode(&mut buf);
let mut b = &mut buf.as_slice();
let decoded: super::#type_ident = Decodable::decode(b).unwrap();
assert_eq!(field, decoded, "maybe_generate_tests::rlp");
// ensure buffer is fully consumed by decode
assert!(b.is_empty(), "buffer was not consumed entirely");
}
});
additional_tests.push(quote! {
#[test]
fn malformed_rlp_header_check() {
use rand::RngCore;
// get random instance of type
let mut raw = [0u8; 1024];
rand::thread_rng().fill_bytes(&mut raw);
let mut unstructured = arbitrary::Unstructured::new(&raw[..]);
let val: Result<super::#type_ident, _> = arbitrary::Arbitrary::arbitrary(&mut unstructured);
if val.is_err() {
// this can be flaky sometimes due to not enough data for iterator based types like Vec
return
}
let val = val.unwrap();
let mut buf = vec![];
let len = val.encode(&mut buf);
// malformed rlp-header check
let mut decode_buf = &mut buf.as_slice();
let mut header = alloy_rlp::Header::decode(decode_buf).expect("failed to decode header");
header.payload_length+=1;
let mut b = Vec::with_capacity(decode_buf.len());
header.encode(&mut b);
b.extend_from_slice(decode_buf);
let res: Result<super::#type_ident, _> = Decodable::decode(&mut b.as_ref());
assert!(res.is_err(), "malformed header was decoded");
}
});
} else if let Ok(num) = arg.to_string().parse() {
default_cases = num;
}
}
let mut tests = TokenStream2::default();
if !roundtrips.is_empty() {
tests = quote! {
#[allow(non_snake_case)]
#[cfg(test)]
mod #mod_tests {
#(#traits)*
use proptest_arbitrary_interop::arb;
#[test]
fn proptest() {
let mut config = proptest::prelude::ProptestConfig::with_cases(#default_cases as u32);
proptest::proptest!(config, |(field in arb::<super::#type_ident>())| {
#(#roundtrips)*
});
}
#(#additional_tests)*
}
}
}
tests
}