• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #![cfg(feature = "derive")]
2 
3 use arbitrary::*;
4 
arbitrary_from<'a, T: Arbitrary<'a>>(input: &'a [u8]) -> T5 fn arbitrary_from<'a, T: Arbitrary<'a>>(input: &'a [u8]) -> T {
6     let mut buf = Unstructured::new(input);
7     T::arbitrary(&mut buf).expect("can create arbitrary instance OK")
8 }
9 
10 #[derive(Copy, Clone, Debug, Eq, PartialEq, Arbitrary)]
11 pub struct Rgb {
12     pub r: u8,
13     pub g: u8,
14     pub b: u8,
15 }
16 
17 #[test]
struct_with_named_fields()18 fn struct_with_named_fields() {
19     let rgb: Rgb = arbitrary_from(&[4, 5, 6]);
20     assert_eq!(rgb.r, 4);
21     assert_eq!(rgb.g, 5);
22     assert_eq!(rgb.b, 6);
23 
24     assert_eq!((3, Some(3)), <Rgb as Arbitrary>::size_hint(0));
25 }
26 
27 #[derive(Copy, Clone, Debug, Arbitrary)]
28 struct MyTupleStruct(u8, bool);
29 
30 #[test]
tuple_struct()31 fn tuple_struct() {
32     let s: MyTupleStruct = arbitrary_from(&[43, 42]);
33     assert_eq!(s.0, 43);
34     assert_eq!(s.1, false);
35 
36     let s: MyTupleStruct = arbitrary_from(&[42, 43]);
37     assert_eq!(s.0, 42);
38     assert_eq!(s.1, true);
39 
40     assert_eq!((2, Some(2)), <MyTupleStruct as Arbitrary>::size_hint(0));
41 }
42 
43 #[derive(Clone, Debug, Arbitrary)]
44 struct EndingInVec(u8, bool, u32, Vec<u16>);
45 #[derive(Clone, Debug, Arbitrary)]
46 struct EndingInString(u8, bool, u32, String);
47 
48 #[test]
test_take_rest()49 fn test_take_rest() {
50     let bytes = [1, 1, 1, 2, 3, 4, 5, 6, 7, 8];
51     let s1 = EndingInVec::arbitrary_take_rest(Unstructured::new(&bytes)).unwrap();
52     let s2 = EndingInString::arbitrary_take_rest(Unstructured::new(&bytes)).unwrap();
53     assert_eq!(s1.0, 1);
54     assert_eq!(s2.0, 1);
55     assert_eq!(s1.1, true);
56     assert_eq!(s2.1, true);
57     assert_eq!(s1.2, 0x4030201);
58     assert_eq!(s2.2, 0x4030201);
59     assert_eq!(s1.3, vec![0x605, 0x807]);
60     assert_eq!(s2.3, "\x05\x06\x07\x08");
61 }
62 
63 #[derive(Copy, Clone, Debug, Arbitrary)]
64 enum MyEnum {
65     Unit,
66     Tuple(u8, u16),
67     Struct { a: u32, b: (bool, u64) },
68 }
69 
70 #[test]
derive_enum()71 fn derive_enum() {
72     let mut raw = vec![
73         // The choice of which enum variant takes 4 bytes.
74         1, 2, 3, 4,
75         // And then we need up to 13 bytes for creating `MyEnum::Struct`, the
76         // largest variant.
77         1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
78     ];
79 
80     let mut saw_unit = false;
81     let mut saw_tuple = false;
82     let mut saw_struct = false;
83 
84     for i in 0..=255 {
85         // Choose different variants each iteration.
86         for el in &mut raw[..4] {
87             *el = i;
88         }
89 
90         let e: MyEnum = arbitrary_from(&raw);
91 
92         match e {
93             MyEnum::Unit => {
94                 saw_unit = true;
95             }
96             MyEnum::Tuple(a, b) => {
97                 saw_tuple = true;
98                 assert_eq!(a, arbitrary_from(&raw[4..5]));
99                 assert_eq!(b, arbitrary_from(&raw[5..]));
100             }
101             MyEnum::Struct { a, b } => {
102                 saw_struct = true;
103                 assert_eq!(a, arbitrary_from(&raw[4..8]));
104                 assert_eq!(b, arbitrary_from(&raw[8..]));
105             }
106         }
107     }
108 
109     assert!(saw_unit);
110     assert!(saw_tuple);
111     assert!(saw_struct);
112 
113     assert_eq!((4, Some(17)), <MyEnum as Arbitrary>::size_hint(0));
114 }
115 
116 #[derive(Arbitrary, Debug)]
117 enum RecursiveTree {
118     Leaf,
119     Node {
120         left: Box<RecursiveTree>,
121         right: Box<RecursiveTree>,
122     },
123 }
124 
125 #[test]
recursive()126 fn recursive() {
127     let raw = vec![1, 2, 3, 4, 5, 6, 7, 8, 9];
128     let _rec: RecursiveTree = arbitrary_from(&raw);
129 
130     let (lower, upper) = <RecursiveTree as Arbitrary>::size_hint(0);
131     assert_eq!(lower, 4, "need a u32 for the discriminant at minimum");
132     assert!(
133         upper.is_none(),
134         "potentially infinitely recursive, so no upper bound"
135     );
136 }
137 
138 #[derive(Arbitrary, Debug)]
139 struct Generic<T> {
140     inner: T,
141 }
142 
143 #[test]
generics()144 fn generics() {
145     let raw = vec![1, 2, 3, 4, 5, 6, 7, 8, 9];
146     let gen: Generic<bool> = arbitrary_from(&raw);
147     assert!(gen.inner);
148 
149     let (lower, upper) = <Generic<u32> as Arbitrary>::size_hint(0);
150     assert_eq!(lower, 4);
151     assert_eq!(upper, Some(4));
152 }
153 
154 #[derive(Arbitrary, Debug)]
155 struct OneLifetime<'a> {
156     alpha: &'a str,
157 }
158 
159 #[test]
one_lifetime()160 fn one_lifetime() {
161     // Last byte is used for length
162     let raw: Vec<u8> = vec![97, 98, 99, 100, 3];
163     let lifetime: OneLifetime = arbitrary_from(&raw);
164     assert_eq!("abc", lifetime.alpha);
165 
166     let (lower, upper) = <OneLifetime as Arbitrary>::size_hint(0);
167     assert_eq!(lower, 8);
168     assert_eq!(upper, None);
169 }
170 
171 #[derive(Arbitrary, Debug)]
172 struct TwoLifetimes<'a, 'b> {
173     alpha: &'a str,
174     beta: &'b str,
175 }
176 
177 #[test]
two_lifetimes()178 fn two_lifetimes() {
179     // Last byte is used for length
180     let raw: Vec<u8> = vec![97, 98, 99, 100, 101, 102, 103, 3];
181     let lifetime: TwoLifetimes = arbitrary_from(&raw);
182     assert_eq!("abc", lifetime.alpha);
183     assert_eq!("def", lifetime.beta);
184 
185     let (lower, upper) = <TwoLifetimes as Arbitrary>::size_hint(0);
186     assert_eq!(lower, 16);
187     assert_eq!(upper, None);
188 }
189