1use quickcheck::{Arbitrary, Gen};
2use triomphe::Arc;
3
4use super::{
5 Ack, Alive, Dead, DelegateVersion, ErrorResponse, IndirectPing, Label, MaybeResolvedAddress,
6 Meta, Nack, NodeState, Ping, ProtocolVersion, PushNodeState, PushPull, State, Suspect,
7 proto::{Message, MessageType},
8};
9
10impl Arbitrary for Ack {
11 fn arbitrary(g: &mut Gen) -> Self {
12 Self {
13 sequence_number: u32::arbitrary(g),
14 payload: Vec::<u8>::arbitrary(g).into(),
15 }
16 }
17}
18
19impl Arbitrary for Nack {
20 fn arbitrary(g: &mut Gen) -> Self {
21 Self {
22 sequence_number: u32::arbitrary(g),
23 }
24 }
25}
26
27impl<A, R> Arbitrary for MaybeResolvedAddress<A, R>
28where
29 A: Arbitrary,
30 R: Arbitrary,
31{
32 fn arbitrary(g: &mut Gen) -> Self {
33 if bool::arbitrary(g) {
34 Self::Resolved(Arbitrary::arbitrary(g))
35 } else {
36 Self::Unresolved(Arbitrary::arbitrary(g))
37 }
38 }
39}
40
41impl<I, A> Arbitrary for Alive<I, A>
42where
43 I: Arbitrary,
44 A: Arbitrary,
45{
46 fn arbitrary(g: &mut quickcheck::Gen) -> Self {
47 Self {
48 incarnation: Arbitrary::arbitrary(g),
49 meta: Arbitrary::arbitrary(g),
50 node: Arbitrary::arbitrary(g),
51 protocol_version: Arbitrary::arbitrary(g),
52 delegate_version: Arbitrary::arbitrary(g),
53 }
54 }
55}
56
57impl<I> Arbitrary for Suspect<I>
58where
59 I: Arbitrary,
60{
61 fn arbitrary(g: &mut Gen) -> Self {
62 Self {
63 incarnation: Arbitrary::arbitrary(g),
64 node: Arbitrary::arbitrary(g),
65 from: Arbitrary::arbitrary(g),
66 }
67 }
68}
69
70impl<I> Arbitrary for Dead<I>
71where
72 I: Arbitrary,
73{
74 fn arbitrary(g: &mut Gen) -> Self {
75 Self {
76 incarnation: Arbitrary::arbitrary(g),
77 node: Arbitrary::arbitrary(g),
78 from: Arbitrary::arbitrary(g),
79 }
80 }
81}
82
83impl Arbitrary for ErrorResponse {
84 fn arbitrary(g: &mut Gen) -> Self {
85 Self::new(String::arbitrary(g))
86 }
87}
88
89impl<I, A> Arbitrary for PushNodeState<I, A>
90where
91 I: Arbitrary,
92 A: Arbitrary,
93{
94 fn arbitrary(g: &mut Gen) -> Self {
95 Self {
96 id: Arbitrary::arbitrary(g),
97 addr: Arbitrary::arbitrary(g),
98 meta: Arbitrary::arbitrary(g),
99 incarnation: Arbitrary::arbitrary(g),
100 state: Arbitrary::arbitrary(g),
101 protocol_version: Arbitrary::arbitrary(g),
102 delegate_version: Arbitrary::arbitrary(g),
103 }
104 }
105}
106
107impl<I, A> Arbitrary for PushPull<I, A>
108where
109 I: Arbitrary,
110 A: Arbitrary,
111{
112 fn arbitrary(g: &mut Gen) -> Self {
113 let states = Vec::<PushNodeState<I, A>>::arbitrary(g);
114 let user_data = Vec::<u8>::arbitrary(g).into();
115 Self {
116 join: Arbitrary::arbitrary(g),
117 states: Arc::from(states),
118 user_data,
119 }
120 }
121}
122
123impl Arbitrary for Label {
124 fn arbitrary(g: &mut Gen) -> Self {
125 let mut s = String::new();
126 while s.len() < 253 {
127 let c = char::arbitrary(g);
128 let char_len = c.len_utf8();
129
130 if s.len() + char_len > 253 {
131 break;
132 }
133 s.push(c);
134 }
135
136 Label(s.into())
137 }
138}
139
140impl Arbitrary for Meta {
141 fn arbitrary(g: &mut quickcheck::Gen) -> Self {
142 let len = usize::arbitrary(g) % Self::MAX_SIZE;
143 let mut buf = Vec::with_capacity(len);
144 for _ in 0..len {
145 buf.push(u8::arbitrary(g));
146 }
147 Meta::try_from(buf).unwrap()
148 }
149}
150
151impl Arbitrary for DelegateVersion {
152 fn arbitrary(g: &mut quickcheck::Gen) -> Self {
153 u8::arbitrary(g).into()
154 }
155}
156
157impl Arbitrary for ProtocolVersion {
158 fn arbitrary(g: &mut quickcheck::Gen) -> Self {
159 u8::arbitrary(g).into()
160 }
161}
162
163impl<I, A> Arbitrary for NodeState<I, A>
164where
165 I: Arbitrary,
166 A: Arbitrary,
167{
168 fn arbitrary(g: &mut Gen) -> Self {
169 Self {
170 id: I::arbitrary(g),
171 addr: A::arbitrary(g),
172 meta: Meta::arbitrary(g),
173 state: State::arbitrary(g),
174 protocol_version: ProtocolVersion::arbitrary(g),
175 delegate_version: DelegateVersion::arbitrary(g),
176 }
177 }
178}
179
180impl Arbitrary for State {
181 fn arbitrary(g: &mut Gen) -> Self {
182 u8::arbitrary(g).into()
183 }
184}
185
186impl<I: Arbitrary, A: Arbitrary> Arbitrary for Ping<I, A> {
187 fn arbitrary(g: &mut Gen) -> Self {
188 Self::new(
189 Arbitrary::arbitrary(g),
190 Arbitrary::arbitrary(g),
191 Arbitrary::arbitrary(g),
192 )
193 }
194}
195
196impl<I: Arbitrary, A: Arbitrary> Arbitrary for IndirectPing<I, A> {
197 fn arbitrary(g: &mut Gen) -> Self {
198 Self::new(
199 Arbitrary::arbitrary(g),
200 Arbitrary::arbitrary(g),
201 Arbitrary::arbitrary(g),
202 )
203 }
204}
205
206#[cfg(feature = "encryption")]
207impl Arbitrary for super::SecretKey {
208 fn arbitrary(g: &mut Gen) -> Self {
209 macro_rules! random {
210 ($lit:literal) => {{
211 let mut buf = [0; $lit];
212 for i in 0..$lit {
213 buf[i] = u8::arbitrary(g);
214 }
215 buf
216 }};
217 }
218
219 match u8::arbitrary(g) % 3 {
220 0 => Self::Aes128(random!(16)),
221 1 => Self::Aes192(random!(24)),
222 2 => Self::Aes256(random!(32)),
223 _ => unreachable!(),
224 }
225 }
226}
227
228impl<I, A> Arbitrary for Message<I, A>
229where
230 I: Arbitrary,
231 A: Arbitrary,
232{
233 fn arbitrary(g: &mut Gen) -> Self {
234 let ty = MessageType::arbitrary(g);
235 match ty {
236 MessageType::Ping => {
237 let ping = Ping::<I, A>::arbitrary(g);
238 Self::Ping(ping)
239 }
240 MessageType::IndirectPing => {
241 let indirect_ping = IndirectPing::<I, A>::arbitrary(g);
242 Self::IndirectPing(indirect_ping)
243 }
244 MessageType::Ack => {
245 let ack = Ack::arbitrary(g);
246 Self::Ack(ack)
247 }
248 MessageType::Suspect => {
249 let suspect = Suspect::<I>::arbitrary(g);
250 Self::Suspect(suspect)
251 }
252 MessageType::Alive => {
253 let alive = Alive::<I, A>::arbitrary(g);
254 Self::Alive(alive)
255 }
256 MessageType::Dead => {
257 let dead = Dead::<I>::arbitrary(g);
258 Self::Dead(dead)
259 }
260 MessageType::PushPull => {
261 let push_pull = PushPull::<I, A>::arbitrary(g);
262 Self::PushPull(push_pull)
263 }
264 MessageType::UserData => {
265 let bytes = Vec::<u8>::arbitrary(g).into();
266 Self::UserData(bytes)
267 }
268 MessageType::Nack => {
269 let nack = Nack::arbitrary(g);
270 Self::Nack(nack)
271 }
272 MessageType::ErrorResponse => {
273 let error_response = ErrorResponse::arbitrary(g);
274 Self::ErrorResponse(error_response)
275 }
276 }
277 }
278}
279
280impl Arbitrary for MessageType {
281 fn arbitrary(g: &mut Gen) -> Self {
282 *g.choose(Self::POSSIBLE_VALUES).unwrap()
283 }
284}