use mqtt_protocol_core::mqtt;
mod common;
#[test]
fn test_cursor_new() {
common::init_tracing();
let data = &b"hello"[..];
let cursor = mqtt::common::Cursor::new(data);
assert_eq!(cursor.position(), 0);
assert_eq!(cursor.get_ref(), &data);
}
#[test]
fn test_cursor_position_and_set_position() {
common::init_tracing();
let mut cursor = mqtt::common::Cursor::new(&b"hello world"[..]);
assert_eq!(cursor.position(), 0);
cursor.set_position(5);
assert_eq!(cursor.position(), 5);
cursor.set_position(100);
assert_eq!(cursor.position(), 100);
}
#[test]
fn test_cursor_get_ref() {
common::init_tracing();
let data = &b"test data"[..];
let cursor = mqtt::common::Cursor::new(data);
assert_eq!(cursor.get_ref(), &data);
}
#[test]
fn test_remaining_slice() {
common::init_tracing();
let mut cursor = mqtt::common::Cursor::new(&b"hello world"[..]);
assert_eq!(cursor.remaining_slice(), b"hello world");
cursor.set_position(6);
assert_eq!(cursor.remaining_slice(), b"world");
cursor.set_position(11);
assert_eq!(cursor.remaining_slice(), b"");
cursor.set_position(20);
assert_eq!(cursor.remaining_slice(), b"");
}
#[test]
fn test_read_u8() {
common::init_tracing();
let mut cursor = mqtt::common::Cursor::new(&b"abc"[..]);
assert_eq!(cursor.read_u8(), Some(b'a'));
assert_eq!(cursor.position(), 1);
assert_eq!(cursor.read_u8(), Some(b'b'));
assert_eq!(cursor.position(), 2);
assert_eq!(cursor.read_u8(), Some(b'c'));
assert_eq!(cursor.position(), 3);
assert_eq!(cursor.read_u8(), None);
assert_eq!(cursor.position(), 3);
}
#[test]
fn test_read_bytes() {
common::init_tracing();
let mut cursor = mqtt::common::Cursor::new(&b"hello world"[..]);
let chunk = cursor.read_bytes(5);
assert_eq!(chunk, Some(&b"hello"[..]));
assert_eq!(cursor.position(), 5);
let chunk = cursor.read_bytes(1);
assert_eq!(chunk, Some(&b" "[..]));
assert_eq!(cursor.position(), 6);
let chunk = cursor.read_bytes(5);
assert_eq!(chunk, Some(&b"world"[..]));
assert_eq!(cursor.position(), 11);
let chunk = cursor.read_bytes(1);
assert_eq!(chunk, None);
assert_eq!(cursor.position(), 11); }
#[test]
fn test_read_bytes_zero_length() {
common::init_tracing();
let mut cursor = mqtt::common::Cursor::new(&b"hello"[..]);
let chunk = cursor.read_bytes(0);
assert_eq!(chunk, Some(&b""[..]));
assert_eq!(cursor.position(), 0); }
#[test]
fn test_read_bytes_exact_length() {
common::init_tracing();
let mut cursor = mqtt::common::Cursor::new(&b"test"[..]);
let chunk = cursor.read_bytes(4);
assert_eq!(chunk, Some(&b"test"[..]));
assert_eq!(cursor.position(), 4);
let chunk = cursor.read_bytes(1);
assert_eq!(chunk, None);
}
#[test]
fn test_read() {
common::init_tracing();
let mut cursor = mqtt::common::Cursor::new(&b"hello world"[..]);
let mut buf = [0u8; 5];
let n = cursor.read(&mut buf).unwrap();
assert_eq!(n, 5);
assert_eq!(&buf, b"hello");
assert_eq!(cursor.position(), 5);
let mut buf2 = [0u8; 3];
let n = cursor.read(&mut buf2).unwrap();
assert_eq!(n, 3);
assert_eq!(&buf2, b" wo");
assert_eq!(cursor.position(), 8);
let mut buf3 = [0u8; 10];
let n = cursor.read(&mut buf3).unwrap();
assert_eq!(n, 3); assert_eq!(&buf3[..3], b"rld");
assert_eq!(cursor.position(), 11);
let n = cursor.read(&mut buf3).unwrap();
assert_eq!(n, 0);
assert_eq!(cursor.position(), 11);
}
#[test]
fn test_read_exact() {
common::init_tracing();
let mut cursor = mqtt::common::Cursor::new(&b"hello world"[..]);
let mut buf = [0u8; 5];
cursor.read_exact(&mut buf).unwrap();
assert_eq!(&buf, b"hello");
assert_eq!(cursor.position(), 5);
let mut buf2 = [0u8; 1];
cursor.read_exact(&mut buf2).unwrap();
assert_eq!(&buf2, b" ");
assert_eq!(cursor.position(), 6);
let mut buf3 = [0u8; 10];
let result = cursor.read_exact(&mut buf3);
assert_eq!(result, Err(mqtt::common::CursorError::UnexpectedEof));
assert_eq!(cursor.position(), 6); }
#[test]
fn test_read_exact_zero_length() {
common::init_tracing();
let mut cursor = mqtt::common::Cursor::new(&b"hello"[..]);
let mut buf = [0u8; 0];
cursor.read_exact(&mut buf).unwrap();
assert_eq!(cursor.position(), 0); }
#[test]
fn test_read_exact_at_end() {
common::init_tracing();
let mut cursor = mqtt::common::Cursor::new(&b"ab"[..]);
cursor.set_position(2);
let mut buf = [0u8; 1];
let result = cursor.read_exact(&mut buf);
assert_eq!(result, Err(mqtt::common::CursorError::UnexpectedEof));
}
#[test]
fn test_cursor_with_different_types() {
common::init_tracing();
let data_vec = vec![1, 2, 3, 4, 5];
let mut cursor_vec = mqtt::common::Cursor::new(data_vec);
let mut buf = [0u8; 3];
cursor_vec.read_exact(&mut buf).unwrap();
assert_eq!(buf, [1, 2, 3]);
let data_string = "hello".to_string();
let mut cursor_string = mqtt::common::Cursor::new(data_string);
let mut buf2 = [0u8; 2];
cursor_string.read_exact(&mut buf2).unwrap();
assert_eq!(&buf2, b"he");
}
#[test]
fn test_cursor_error_debug() {
common::init_tracing();
let error = mqtt::common::CursorError::UnexpectedEof;
let debug_str = format!("{error:?}");
assert_eq!(debug_str, "UnexpectedEof");
}
#[test]
fn test_cursor_error_equality() {
common::init_tracing();
let error1 = mqtt::common::CursorError::UnexpectedEof;
let error2 = mqtt::common::CursorError::UnexpectedEof;
assert_eq!(error1, error2);
assert_eq!(error1.clone(), error2);
}
#[test]
fn test_mixed_operations() {
common::init_tracing();
let mut cursor = mqtt::common::Cursor::new(&b"abcdefghij"[..]);
assert_eq!(cursor.read_u8(), Some(b'a'));
assert_eq!(cursor.position(), 1);
let chunk = cursor.read_bytes(3).unwrap();
assert_eq!(chunk, b"bcd");
assert_eq!(cursor.position(), 4);
let mut buf = [0u8; 2];
cursor.read_exact(&mut buf).unwrap();
assert_eq!(&buf, b"ef");
assert_eq!(cursor.position(), 6);
assert_eq!(cursor.remaining_slice(), b"ghij");
let mut buf2 = [0u8; 5];
let n = cursor.read(&mut buf2).unwrap();
assert_eq!(n, 4);
assert_eq!(&buf2[..4], b"ghij");
assert_eq!(cursor.position(), 10);
}
#[test]
fn test_position_overflow_safety() {
common::init_tracing();
let mut cursor = mqtt::common::Cursor::new(&b"hello"[..]);
cursor.set_position(u64::MAX);
assert_eq!(cursor.position(), u64::MAX);
assert_eq!(cursor.remaining_slice(), b"");
assert_eq!(cursor.read_u8(), None);
assert_eq!(cursor.read_bytes(1), None);
let mut buf = [0u8; 1];
let result = cursor.read_exact(&mut buf);
assert_eq!(result, Err(mqtt::common::CursorError::UnexpectedEof));
let n = cursor.read(&mut buf).unwrap();
assert_eq!(n, 0);
}