use super::*;
#[test]
fn a_continuation_packet_of_two_or_three_octets_is_accepted() -> Result<()> {
let mut packet = Vp8Packet::default();
assert_eq!(
packet.depacketize(&Bytes::from_static(&[0x00, 0xAB]))?,
Bytes::from_static(&[0xAB])
);
assert_eq!(
packet.depacketize(&Bytes::from_static(&[0x00, 0xAB, 0xCD]))?,
Bytes::from_static(&[0xAB, 0xCD])
);
assert!(packet.depacketize(&Bytes::from_static(&[0x00])).is_err());
assert!(
packet
.depacketize(&Bytes::from_static(&[0x80, 0x80, 0x11]))
.is_err()
);
Ok(())
}
#[test]
fn every_packet_the_payloader_emits_depacketizes() -> Result<()> {
let frame = Bytes::from_static(&[0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96]);
for enable_picture_id in [false, true] {
for mtu in 2..=12 {
let mut payloader = Vp8Payloader {
enable_picture_id,
..Default::default()
};
let payloads = payloader.payload(mtu, &frame)?;
if payloads.is_empty() {
continue;
}
let mut packet = Vp8Packet::default();
let mut out = vec![];
for payload in &payloads {
out.extend_from_slice(&packet.depacketize(payload)?);
}
assert_eq!(out, frame, "mtu {mtu}, picture id {enable_picture_id}");
}
}
Ok(())
}
#[test]
fn a_packet_without_extensions_after_one_with_them_has_none() -> Result<()> {
let mut packet = Vp8Packet::default();
assert_eq!(
packet.depacketize(&Bytes::from_static(&[
0x90, 0xF0, 0x05, 0x09, 0xA3, 0xAA, 0xBB
]))?,
Bytes::from_static(&[0xAA, 0xBB])
);
assert_eq!(
(
packet.picture_id,
packet.tl0_pic_idx,
packet.tid,
packet.y,
packet.key_idx
),
(5, 9, 2, 1, 3)
);
assert_eq!(
packet.depacketize(&Bytes::from_static(&[0x00, 0x11, 0x22, 0x33, 0x44]))?,
Bytes::from_static(&[0x11, 0x22, 0x33, 0x44])
);
assert_eq!(
(packet.x, packet.i, packet.l, packet.t, packet.k),
(0, 0, 0, 0, 0)
);
assert_eq!(
(
packet.picture_id,
packet.tl0_pic_idx,
packet.tid,
packet.y,
packet.key_idx
),
(0, 0, 0, 0, 0)
);
Ok(())
}
#[test]
fn tid_is_ignored_without_t_and_keyidx_without_k() -> Result<()> {
let mut packet = Vp8Packet::default();
packet.depacketize(&Bytes::from_static(&[0x80, 0x30, 0xFF, 0xAA]))?;
assert_eq!((packet.tid, packet.y, packet.key_idx), (3, 1, 31));
packet.depacketize(&Bytes::from_static(&[0x80, 0x10, 0xE7, 0xAA]))?;
assert_eq!((packet.tid, packet.y, packet.key_idx), (0, 0, 7));
packet.depacketize(&Bytes::from_static(&[0x80, 0x20, 0x5F, 0xAA]))?;
assert_eq!((packet.tid, packet.y, packet.key_idx), (1, 0, 0));
Ok(())
}
#[test]
fn reserved_bits_are_ignored() -> Result<()> {
let mut packet = Vp8Packet::default();
assert_eq!(
packet.depacketize(&Bytes::from_static(&[0xD8, 0x8F, 0x7F, 0xAA]))?,
Bytes::from_static(&[0xAA])
);
assert_eq!((packet.x, packet.n, packet.s, packet.pid), (1, 0, 1, 0));
assert_eq!((packet.i, packet.l, packet.t, packet.k), (1, 0, 0, 0));
assert_eq!(packet.picture_id, 0x7F);
Ok(())
}
#[test]
fn only_the_first_partition_starts_a_frame() {
let packet = Vp8Packet::default();
assert!(packet.is_partition_head(&Bytes::from_static(&[0x10, 0x00])));
for pid in 1..=7u8 {
assert!(
!packet.is_partition_head(&Bytes::copy_from_slice(&[0x10 | pid, 0x00])),
"S=1 with PID {pid} is not a frame start"
);
}
assert!(!packet.is_partition_head(&Bytes::from_static(&[0x00, 0x00])));
}