#![allow(missing_docs)]
use crate::packet::{Packet, marshal, unmarshal, unmarshal_lenient};
pub fn packet_unmarshal(data: &[u8]) {
let mut buf = data;
let _ = unmarshal(&mut buf);
}
pub fn packet_unmarshal_lenient(data: &[u8]) {
let mut buf = data;
let lenient = unmarshal_lenient(&mut buf);
let mut buf = data;
if unmarshal(&mut buf).is_ok() {
assert!(
lenient.is_ok(),
"the lenient reader accepts what the strict one does"
);
}
if let Ok(packets) = lenient {
for packet in &packets {
let _ = packet.header();
let _ = packet.destination_ssrc();
}
if let Ok(raw) = marshal(&packets) {
unmarshal_lenient(&mut raw.freeze()).expect("what was written is read again");
}
}
}
fn sizes_are_what_is_written(packets: &[Box<dyn Packet>]) {
for packet in packets {
let mut buf = vec![0u8; packet.marshal_size()];
if let Ok(n) = packet.marshal_to(&mut buf) {
assert_eq!(
n,
buf.len(),
"marshal_to writes marshal_size octets: {packet:?}"
);
}
}
}
pub fn packet_roundtrip(data: &[u8]) {
let mut buf = data;
if let Ok(packets) = unmarshal(&mut buf) {
sizes_are_what_is_written(&packets);
if let Ok(raw) = marshal(&packets) {
let again = unmarshal(&mut raw.clone().freeze()).expect("what was written is read");
assert_eq!(packets.len(), again.len());
sizes_are_what_is_written(&again);
let rewritten = marshal(&again).expect("and written again");
assert_eq!(raw, rewritten, "marshal(parse(.)) is a fixed point");
let third = unmarshal(&mut rewritten.freeze()).expect("and read again");
for (a, b) in again.iter().zip(third.iter()) {
assert!(
a.equal(&**b),
"the same bytes read as the same packet: {a:?} {b:?}"
);
}
}
}
}