use anyhow::Result;
use crate::ffi::nlattr;
use crate::sys::Cursor;
#[test]
fn cursor_invariants() -> Result<()> {
let mut buf = Vec::new();
buf.extend_from_slice(&32u16.to_ne_bytes());
buf.extend_from_slice(&64u16.to_ne_bytes());
let mut c = Cursor::from(&buf[..3]);
assert_eq!(None, c.next::<u16>());
let mut c = Cursor::from(&buf[..]);
let tail = Cursor::from(&buf[4..]);
let attr = nlattr { nla_len: 32, nla_type: 64 };
assert_eq!(Some((attr, tail)), c.next::<nlattr>());
let mut buf = Vec::new();
buf.extend_from_slice(&3u16.to_ne_bytes());
buf.extend_from_slice(&4u16.to_ne_bytes());
buf.extend_from_slice(&[0, 1, 2]);
let mut c = Cursor::from(&buf[..]);
let tail = Cursor::from(&buf[4..]);
let attr = nlattr { nla_len: 3, nla_type: 4 };
assert_eq!(Some((attr, tail)), c.next::<nlattr>());
let mut buf = Vec::new();
buf.extend_from_slice(&2u16.to_ne_bytes());
buf.extend_from_slice(&4u16.to_ne_bytes());
buf.extend_from_slice(&8u64.to_ne_bytes());
let mut c = Cursor::from(&buf[0..]);
let tail = Cursor::from(&buf[4..]);
let attr = nlattr { nla_len: 2, nla_type: 4 };
assert_eq!(Some((attr, tail)), c.next());
Ok(())
}
#[test]
fn cursor_asciiz() -> Result<()> {
let mut buf = Vec::new();
buf.extend_from_slice(&8u16.to_ne_bytes());
buf.extend_from_slice(&2u16.to_ne_bytes());
buf.extend_from_slice(b"foo\0");
let mut c = Cursor::from(&buf[..]);
let tail = c.next::<nlattr>().unwrap().1;
assert_eq!(Ok("foo"), tail.asciiz());
Ok(())
}
#[test]
fn cursor_bytes() -> Result<()> {
let mut buf = Vec::new();
buf.extend_from_slice(&8u16.to_ne_bytes());
buf.extend_from_slice(&2u16.to_ne_bytes());
buf.extend_from_slice(b"quux");
let mut c = Cursor::from(&buf[..]);
let tail = c.next::<nlattr>().unwrap().1;
assert_eq!(b"quux", tail.bytes());
Ok(())
}
#[test]
fn cursor_copy() -> Result<()> {
let mut buf = Vec::new();
buf.extend_from_slice(&8u16.to_ne_bytes());
buf.extend_from_slice(&2u16.to_ne_bytes());
buf.extend_from_slice(&4u32.to_ne_bytes());
let mut c = Cursor::from(&buf[..]);
let tail = c.next::<nlattr>().unwrap().1;
assert_eq!(4u32, tail.copy());
Ok(())
}