//! Fuzzes the ENet client peer: a connecting peer fed a sequence of datagrams. //! Asserts invariants, not only "no panic": never panics; delivered packets are within MAX_PACKET_BYTES; outgoing datagrams parse and stay within the MTU. //! How to run: see the comment at the top of `fuzz/Cargo.toml`. #![no_main] use libfuzzer_sys::fuzz_target; use core::time::Duration; use domistream::enet::protocol::parse_datagram; use domistream::enet::{EnetClient, EnetConfig, EnetEvent, MAX_PACKET_BYTES}; use domistream::{Instant, RandomSource}; struct Fixed; impl RandomSource for Fixed { fn fill(&mut self, dest: &mut [u8]) { dest.fill(0x5A); } } fuzz_target!(|data: &[u8]| { let cfg = EnetConfig { channel_count: 2, mtu: 1400, connect_data: 0, ping_interval: Duration::from_millis(500), timeout: Duration::from_secs(10), }; let Ok(mut c) = EnetClient::connect(cfg, Instant::ZERO, &mut Fixed) else { return }; // Datagrams separated by a 2-byte big-endian length; time advances 1 ms per datagram. let mut rest = data; let mut ms = 0u64; while rest.len() >= 2 { let len = usize::from(u16::from_be_bytes([rest[0], rest[1]])).min(rest.len() - 2); let (d, tail) = rest[2..].split_at(len); rest = tail; ms += 1; let now = Instant::from_micros(ms * 1000); let _ = c.handle_datagram(now, d); let _ = c.handle_timeout(now); while let Some(out) = c.poll_transmit(now) { assert!(out.len() <= usize::from(cfg.mtu)); parse_datagram(&out).expect("the client's own datagrams parse"); } while let Some(e) = c.poll_event() { if let EnetEvent::Received { data, .. } = e { assert!(data.len() <= MAX_PACKET_BYTES); } } } });