1 #![cfg(feature = "derive")]
2 // Various structs/fields that we are deriving `Arbitrary` for aren't actually
3 // used except to exercise the derive.
4 #![allow(dead_code)]
5
6 use arbitrary::*;
7
arbitrary_from<'a, T: Arbitrary<'a>>(input: &'a [u8]) -> T8 fn arbitrary_from<'a, T: Arbitrary<'a>>(input: &'a [u8]) -> T {
9 let mut buf = Unstructured::new(input);
10 T::arbitrary(&mut buf).expect("can create arbitrary instance OK")
11 }
12
13 #[derive(Copy, Clone, Debug, Eq, PartialEq, Arbitrary)]
14 pub struct Rgb {
15 pub r: u8,
16 pub g: u8,
17 pub b: u8,
18 }
19
20 #[test]
struct_with_named_fields()21 fn struct_with_named_fields() {
22 let rgb: Rgb = arbitrary_from(&[4, 5, 6]);
23 assert_eq!(rgb.r, 4);
24 assert_eq!(rgb.g, 5);
25 assert_eq!(rgb.b, 6);
26
27 assert_eq!((3, Some(3)), <Rgb as Arbitrary>::size_hint(0));
28 }
29
30 #[derive(Copy, Clone, Debug, Arbitrary)]
31 struct MyTupleStruct(u8, bool);
32
33 #[test]
tuple_struct()34 fn tuple_struct() {
35 let s: MyTupleStruct = arbitrary_from(&[43, 42]);
36 assert_eq!(s.0, 43);
37 assert_eq!(s.1, false);
38
39 let s: MyTupleStruct = arbitrary_from(&[42, 43]);
40 assert_eq!(s.0, 42);
41 assert_eq!(s.1, true);
42
43 assert_eq!((2, Some(2)), <MyTupleStruct as Arbitrary>::size_hint(0));
44 }
45
46 #[derive(Clone, Debug, Arbitrary)]
47 struct EndingInVec(u8, bool, u32, Vec<u16>);
48 #[derive(Clone, Debug, Arbitrary)]
49 struct EndingInString(u8, bool, u32, String);
50
51 #[test]
test_take_rest()52 fn test_take_rest() {
53 let bytes = [1, 1, 1, 2, 3, 4, 5, 6, 7, 8];
54 let s1 = EndingInVec::arbitrary_take_rest(Unstructured::new(&bytes)).unwrap();
55 let s2 = EndingInString::arbitrary_take_rest(Unstructured::new(&bytes)).unwrap();
56 assert_eq!(s1.0, 1);
57 assert_eq!(s2.0, 1);
58 assert_eq!(s1.1, true);
59 assert_eq!(s2.1, true);
60 assert_eq!(s1.2, 0x4030201);
61 assert_eq!(s2.2, 0x4030201);
62 assert_eq!(s1.3, vec![0x605, 0x807]);
63 assert_eq!(s2.3, "\x05\x06\x07\x08");
64 }
65
66 #[derive(Copy, Clone, Debug, Arbitrary)]
67 enum MyEnum {
68 Unit,
69 Tuple(u8, u16),
70 Struct { a: u32, b: (bool, u64) },
71 }
72
73 #[test]
derive_enum()74 fn derive_enum() {
75 let mut raw = vec![
76 // The choice of which enum variant takes 4 bytes.
77 1, 2, 3, 4,
78 // And then we need up to 13 bytes for creating `MyEnum::Struct`, the
79 // largest variant.
80 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
81 ];
82
83 let mut saw_unit = false;
84 let mut saw_tuple = false;
85 let mut saw_struct = false;
86
87 for i in 0..=255 {
88 // Choose different variants each iteration.
89 for el in &mut raw[..4] {
90 *el = i;
91 }
92
93 let e: MyEnum = arbitrary_from(&raw);
94
95 match e {
96 MyEnum::Unit => {
97 saw_unit = true;
98 }
99 MyEnum::Tuple(a, b) => {
100 saw_tuple = true;
101 assert_eq!(a, arbitrary_from(&raw[4..5]));
102 assert_eq!(b, arbitrary_from(&raw[5..]));
103 }
104 MyEnum::Struct { a, b } => {
105 saw_struct = true;
106 assert_eq!(a, arbitrary_from(&raw[4..8]));
107 assert_eq!(b, arbitrary_from(&raw[8..]));
108 }
109 }
110 }
111
112 assert!(saw_unit);
113 assert!(saw_tuple);
114 assert!(saw_struct);
115
116 assert_eq!((4, Some(17)), <MyEnum as Arbitrary>::size_hint(0));
117 }
118
119 #[derive(Arbitrary, Debug)]
120 enum RecursiveTree {
121 Leaf,
122 Node {
123 left: Box<RecursiveTree>,
124 right: Box<RecursiveTree>,
125 },
126 }
127
128 #[test]
recursive()129 fn recursive() {
130 let raw = vec![1, 2, 3, 4, 5, 6, 7, 8, 9];
131 let _rec: RecursiveTree = arbitrary_from(&raw);
132
133 let (lower, upper) = <RecursiveTree as Arbitrary>::size_hint(0);
134 assert_eq!(lower, 4, "need a u32 for the discriminant at minimum");
135 assert!(
136 upper.is_none(),
137 "potentially infinitely recursive, so no upper bound"
138 );
139 }
140
141 #[derive(Arbitrary, Debug)]
142 struct Generic<T> {
143 inner: T,
144 }
145
146 #[test]
generics()147 fn generics() {
148 let raw = vec![1, 2, 3, 4, 5, 6, 7, 8, 9];
149 let gen: Generic<bool> = arbitrary_from(&raw);
150 assert!(gen.inner);
151
152 let (lower, upper) = <Generic<u32> as Arbitrary>::size_hint(0);
153 assert_eq!(lower, 4);
154 assert_eq!(upper, Some(4));
155 }
156
157 #[derive(Arbitrary, Debug)]
158 struct OneLifetime<'a> {
159 alpha: &'a str,
160 }
161
162 #[test]
one_lifetime()163 fn one_lifetime() {
164 // Last byte is used for length
165 let raw: Vec<u8> = vec![97, 98, 99, 100, 3];
166 let lifetime: OneLifetime = arbitrary_from(&raw);
167 assert_eq!("abc", lifetime.alpha);
168
169 let (lower, upper) = <OneLifetime as Arbitrary>::size_hint(0);
170 assert_eq!(lower, 0);
171 assert_eq!(upper, None);
172 }
173
174 #[derive(Arbitrary, Debug)]
175 struct TwoLifetimes<'a, 'b> {
176 alpha: &'a str,
177 beta: &'b str,
178 }
179
180 #[test]
two_lifetimes()181 fn two_lifetimes() {
182 // Last byte is used for length
183 let raw: Vec<u8> = vec![97, 98, 99, 100, 101, 102, 103, 3];
184 let lifetime: TwoLifetimes = arbitrary_from(&raw);
185 assert_eq!("abc", lifetime.alpha);
186 assert_eq!("def", lifetime.beta);
187
188 let (lower, upper) = <TwoLifetimes as Arbitrary>::size_hint(0);
189 assert_eq!(lower, 0);
190 assert_eq!(upper, None);
191 }
192
193 #[test]
recursive_and_empty_input()194 fn recursive_and_empty_input() {
195 // None of the following derives should result in a stack overflow. See
196 // https://github.com/rust-fuzz/arbitrary/issues/107 for details.
197
198 #[derive(Debug, Arbitrary)]
199 enum Nat {
200 Succ(Box<Nat>),
201 Zero,
202 }
203
204 let _ = Nat::arbitrary(&mut Unstructured::new(&[]));
205
206 #[derive(Debug, Arbitrary)]
207 enum Nat2 {
208 Zero,
209 Succ(Box<Nat2>),
210 }
211
212 let _ = Nat2::arbitrary(&mut Unstructured::new(&[]));
213
214 #[derive(Debug, Arbitrary)]
215 struct Nat3 {
216 f: Option<Box<Nat3>>,
217 }
218
219 let _ = Nat3::arbitrary(&mut Unstructured::new(&[]));
220
221 #[derive(Debug, Arbitrary)]
222 struct Nat4(Option<Box<Nat4>>);
223
224 let _ = Nat4::arbitrary(&mut Unstructured::new(&[]));
225
226 #[derive(Debug, Arbitrary)]
227 enum Nat5 {
228 Zero,
229 Succ { f: Box<Nat5> },
230 }
231
232 let _ = Nat5::arbitrary(&mut Unstructured::new(&[]));
233 }
234
235 #[test]
test_field_attributes()236 fn test_field_attributes() {
237 // A type that DOES NOT implement Arbitrary
238 #[derive(Debug)]
239 struct Weight(u8);
240
241 #[derive(Debug, Arbitrary)]
242 struct Parcel {
243 #[arbitrary(with = arbitrary_weight)]
244 weight: Weight,
245
246 #[arbitrary(default)]
247 width: u8,
248
249 #[arbitrary(value = 2 + 2)]
250 length: u8,
251
252 height: u8,
253
254 #[arbitrary(with = |u: &mut Unstructured| u.int_in_range(0..=100))]
255 price: u8,
256 }
257
258 fn arbitrary_weight(u: &mut Unstructured) -> arbitrary::Result<Weight> {
259 u.int_in_range(45..=56).map(Weight)
260 }
261
262 let parcel: Parcel = arbitrary_from(&[6, 199, 17]);
263
264 // 45 + 6 = 51
265 assert_eq!(parcel.weight.0, 51);
266
267 // u8::default()
268 assert_eq!(parcel.width, 0);
269
270 // 2 + 2 = 4
271 assert_eq!(parcel.length, 4);
272
273 // 199 is the 2nd byte used by arbitrary
274 assert_eq!(parcel.height, 199);
275
276 // 17 is the 3rd byte used by arbitrary
277 assert_eq!(parcel.price, 17);
278 }
279