use super::*;
#[test]
fn test_vp8_unmarshal() -> Result<()> {
let mut pck = Vp8Packet::default();
let empty_bytes = Bytes::from_static(&[]);
let result = pck.depacketize(&empty_bytes);
assert!(result.is_err(), "Result should be err in case of error");
let small_bytes = Bytes::from_static(&[0x00]);
let result = pck.depacketize(&small_bytes);
assert!(result.is_err(), "Result should be err in case of error");
let small_bytes = Bytes::from_static(&[0x80, 0x80]);
let result = pck.depacketize(&small_bytes);
assert!(result.is_err(), "Result should be err in case of error");
struct Case {
name: &'static str,
raw: &'static [u8],
payload: &'static [u8],
flags: [u8; 5], picture_id: u16,
picture_id_15_bits: bool,
tl0_pic_idx: u8,
tid: u8,
y: u8,
key_idx: u8,
}
let cases = [
Case {
name: "no extension",
raw: &[0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x90],
payload: &[0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x90],
flags: [0, 0, 0, 0, 0],
picture_id: 0,
picture_id_15_bits: false,
tl0_pic_idx: 0,
tid: 0,
y: 0,
key_idx: 0,
},
Case {
name: "only X",
raw: &[0x80, 0x00, 0xaa, 0xbb],
payload: &[0xaa, 0xbb],
flags: [1, 0, 0, 0, 0],
picture_id: 0,
picture_id_15_bits: false,
tl0_pic_idx: 0,
tid: 0,
y: 0,
key_idx: 0,
},
Case {
name: "X and I, 7-bit PictureID",
raw: &[0x80, 0x80, 0x45, 0xcc],
payload: &[0xcc],
flags: [1, 1, 0, 0, 0],
picture_id: 0x45,
picture_id_15_bits: false,
tl0_pic_idx: 0,
tid: 0,
y: 0,
key_idx: 0,
},
Case {
name: "X and I, 15-bit PictureID",
raw: &[0x80, 0x80, 0x81, 0x23, 0xcc],
payload: &[0xcc],
flags: [1, 1, 0, 0, 0],
picture_id: 0x0123,
picture_id_15_bits: true,
tl0_pic_idx: 0,
tid: 0,
y: 0,
key_idx: 0,
},
Case {
name: "X and L",
raw: &[0x80, 0x40, 0x37, 0xdd],
payload: &[0xdd],
flags: [1, 0, 1, 0, 0],
picture_id: 0,
picture_id_15_bits: false,
tl0_pic_idx: 0x37,
tid: 0,
y: 0,
key_idx: 0,
},
Case {
name: "X and T",
raw: &[0x80, 0x20, 0xbf, 0xee],
payload: &[0xee],
flags: [1, 0, 0, 1, 0],
picture_id: 0,
picture_id_15_bits: false,
tl0_pic_idx: 0,
tid: 2,
y: 1,
key_idx: 0,
},
Case {
name: "X and K",
raw: &[0x80, 0x10, 0xf5, 0xee],
payload: &[0xee],
flags: [1, 0, 0, 0, 1],
picture_id: 0,
picture_id_15_bits: false,
tl0_pic_idx: 0,
tid: 0,
y: 0,
key_idx: 0x15,
},
Case {
name: "all flags, 7-bit PictureID",
raw: &[0xff, 0xff, 0x7e, 0x12, 0x6b, 0x99, 0x98],
payload: &[0x99, 0x98],
flags: [1, 1, 1, 1, 1],
picture_id: 0x7e,
picture_id_15_bits: false,
tl0_pic_idx: 0x12,
tid: 1,
y: 1,
key_idx: 0x0b,
},
Case {
name: "all flags, 15-bit PictureID",
raw: &[0xff, 0xff, 0xff, 0xfe, 0x12, 0x6b, 0x99],
payload: &[0x99],
flags: [1, 1, 1, 1, 1],
picture_id: 0x7ffe,
picture_id_15_bits: true,
tl0_pic_idx: 0x12,
tid: 1,
y: 1,
key_idx: 0x0b,
},
];
for case in cases {
let name = case.name;
let payload = pck
.depacketize(&Bytes::from_static(case.raw))
.unwrap_or_else(|err| panic!("{name}: {err}"));
assert_eq!(&payload[..], case.payload, "{name}: payload offset");
assert_eq!(
[pck.x, pck.i, pck.l, pck.t, pck.k],
case.flags,
"{name}: X I L T K"
);
assert_eq!(pck.picture_id, case.picture_id, "{name}: PictureID");
assert_eq!(pck.picture_id_15_bits, case.picture_id_15_bits, "{name}: M");
assert_eq!(pck.tl0_pic_idx, case.tl0_pic_idx, "{name}: TL0PICIDX");
assert_eq!(pck.tid, case.tid, "{name}: TID");
assert_eq!(pck.y, case.y, "{name}: Y");
assert_eq!(pck.key_idx, case.key_idx, "{name}: KEYIDX");
}
assert_eq!((pck.n, pck.s, pck.pid), (1, 1, 7));
for truncated in [
&[0x80, 0x80, 0x81][..], &[0x80, 0x40], &[0x80, 0x10], &[0x80, 0x10, 0x05], ] {
assert!(
pck.depacketize(&Bytes::copy_from_slice(truncated)).is_err(),
"{truncated:02x?}"
);
}
Ok(())
}
#[test]
fn test_vp8_payload() -> Result<()> {
let tests = vec![
(
"WithoutPictureID",
Vp8Payloader::default(),
2,
vec![
Bytes::from_static(&[0x90, 0x90, 0x90]),
Bytes::from_static(&[0x91, 0x91]),
],
vec![
vec![
Bytes::from_static(&[0x10, 0x90]),
Bytes::from_static(&[0x00, 0x90]),
Bytes::from_static(&[0x00, 0x90]),
],
vec![
Bytes::from_static(&[0x10, 0x91]),
Bytes::from_static(&[0x00, 0x91]),
],
],
),
(
"WithPictureID_1byte",
Vp8Payloader {
enable_picture_id: true,
picture_id: 0x20,
..Default::default()
},
5,
vec![
Bytes::from_static(&[0x90, 0x90, 0x90]),
Bytes::from_static(&[0x91, 0x91]),
],
vec![
vec![
Bytes::from_static(&[0x90, 0x80, 0x20, 0x90, 0x90]),
Bytes::from_static(&[0x80, 0x80, 0x20, 0x90]),
],
vec![Bytes::from_static(&[0x90, 0x80, 0x21, 0x91, 0x91])],
],
),
(
"WithPictureID_2bytes",
Vp8Payloader {
enable_picture_id: true,
picture_id: 0x120,
..Default::default()
},
6,
vec![
Bytes::from_static(&[0x90, 0x90, 0x90]),
Bytes::from_static(&[0x91, 0x91]),
],
vec![
vec![
Bytes::from_static(&[0x90, 0x80, 0x81, 0x20, 0x90, 0x90]),
Bytes::from_static(&[0x80, 0x80, 0x81, 0x20, 0x90]),
],
vec![Bytes::from_static(&[0x90, 0x80, 0x81, 0x21, 0x91, 0x91])],
],
),
];
for (name, mut pck, mtu, payloads, expected) in tests {
for (i, payload) in payloads.iter().enumerate() {
let actual = pck.payload(mtu, payload)?;
assert_eq!(expected[i], actual, "{name}: Generated packet[{i}] differs");
}
}
Ok(())
}
#[test]
fn test_vp8_payload_error() -> Result<()> {
let mut pck = Vp8Payloader::default();
let empty = Bytes::from_static(&[]);
let payload = Bytes::from_static(&[0x90, 0x90, 0x90]);
let result = pck.payload(1, &empty)?;
assert!(result.is_empty(), "Generated payload should be empty");
let result = pck.payload(1, &payload)?;
assert_eq!(result.len(), 0, "Generated payload should be empty");
let result = pck.payload(2, &payload)?;
assert_eq!(
result.len(),
payload.len(),
"Generated payload should be the same size as original payload size"
);
Ok(())
}
#[test]
fn test_vp8_partition_head_checker_is_partition_head() -> Result<()> {
let vp8 = Vp8Packet::default();
assert!(
!vp8.is_partition_head(&Bytes::from_static(&[0x00])),
"Small packet should not be the head of a new partition"
);
assert!(
vp8.is_partition_head(&Bytes::from_static(&[0x10, 0x00, 0x00, 0x00])),
"Packet with S flag should be the head of a new partition"
);
assert!(
!vp8.is_partition_head(&Bytes::from_static(&[0x00, 0x00, 0x00, 0x00])),
"Packet without S flag should not be the head of a new partition"
);
Ok(())
}