use std::io::Cursor;
use std::io::ErrorKind;
use qubit_codec::ByteOrder;
use qubit_io_binary::StringReadExt;
#[test]
fn test_string_read_ext_reuses_payload_buffer() {
let mut input = Cursor::new(b"hello world".to_vec());
let mut payload = Vec::with_capacity(32);
input
.read_utf8_payload_into(&mut payload, 5, 32)
.expect("first payload should be read");
let allocation = payload.as_ptr();
assert_eq!(b"hello", payload.as_slice());
input
.read_utf8_payload_into(&mut payload, 6, 32)
.expect("second payload should be read");
assert_eq!(allocation, payload.as_ptr());
assert_eq!(b" world", payload.as_slice());
let error = input
.read_utf8_payload_into(&mut payload, 1, 0)
.expect_err("payload over the configured limit should fail");
assert_eq!(ErrorKind::InvalidData, error.kind());
assert_eq!(b" world", payload.as_slice());
let mut input = Cursor::new(vec![0xff]);
let error = input
.read_utf8_payload_into(&mut payload, 1, 1)
.expect_err("invalid UTF-8 should fail");
assert_eq!(ErrorKind::InvalidData, error.kind());
assert_eq!([0xff], payload.as_slice());
}
#[test]
fn test_reusable_payload_eof_then_independent_input() {
let mut input = Cursor::new(b"ab".to_vec());
let mut payload = Vec::with_capacity(16);
payload.extend_from_slice(b"old");
let error = input
.read_utf8_payload_into(&mut payload, 5, 5)
.expect_err("partial payload must report EOF");
assert_eq!(ErrorKind::UnexpectedEof, error.kind());
assert_eq!(2, input.position());
assert!(payload.starts_with(b"ab"), "received prefix must be observable");
let mut next_frame = Cursor::new(b"new".to_vec());
next_frame
.read_utf8_payload_into(&mut payload, 3, 5)
.expect("storage must be reusable on an independent frame");
assert_eq!(b"new", payload.as_slice());
assert_eq!(3, next_frame.position());
}
#[test]
fn test_string_read_ext_reads_all_length_prefix_kinds() {
let mut input = Cursor::new(b"raw".to_vec());
assert_eq!(
"raw",
input
.read_utf8_payload(3, 8)
.expect("known-length payload should be read")
);
let mut input = Cursor::new(vec![5, b'h', b'e', b'l', b'l', b'o']);
assert_eq!(
"hello",
input
.read_utf8_string_uleb_usize(8)
.expect("ULEB string should be read")
);
let mut input = Cursor::new(vec![5, b'h', b'e', b'l', b'l', b'o']);
assert_eq!(
"hello",
input
.read_utf8_string_uleb_u64(8)
.expect("u64 ULEB string should be read")
);
let mut input = Cursor::new(vec![5, b'h', b'e', b'l', b'l', b'o']);
assert_eq!(
"hello",
input
.read_utf8_string_uleb_u64_strict(8)
.expect("strict u64 ULEB string should be read")
);
let mut input = Cursor::new(vec![5, b'h', b'e', b'l', b'l', b'o']);
assert_eq!(
"hello",
input
.read_utf8_string_uleb_usize_strict(8)
.expect("strict ULEB string should be read")
);
let mut input = Cursor::new(vec![0, 2, b'h', b'i']);
assert_eq!(
"hi",
input
.read_string_with_u16_len_be(8)
.expect("u16 BE string should be read")
);
let mut input = Cursor::new(vec![0, 2, b'h', b'i']);
assert_eq!(
"hi",
input
.read_string_with_u16_len(ByteOrder::BigEndian, 8)
.expect("runtime u16 BE string should be read")
);
let mut input = Cursor::new(vec![2, 0, b'h', b'i']);
assert_eq!(
"hi",
input
.read_string_with_u16_len_le(8)
.expect("u16 LE string should be read")
);
let mut input = Cursor::new(vec![2, 0, b'h', b'i']);
assert_eq!(
"hi",
input
.read_string_with_u16_len(ByteOrder::LittleEndian, 8)
.expect("runtime u16 LE string should be read")
);
let mut input = Cursor::new(vec![0, 0, 0, 2, b'o', b'k']);
assert_eq!(
"ok",
input
.read_string_with_u32_len_be(8)
.expect("u32 BE string should be read")
);
let mut input = Cursor::new(vec![0, 0, 0, 2, b'o', b'k']);
assert_eq!(
"ok",
input
.read_string_with_u32_len(ByteOrder::BigEndian, 8)
.expect("runtime u32 BE string should be read")
);
let mut input = Cursor::new(vec![2, 0, 0, 0, b'o', b'k']);
assert_eq!(
"ok",
input
.read_string_with_u32_len_le(8)
.expect("u32 LE string should be read")
);
let mut input = Cursor::new(vec![2, 0, 0, 0, b'o', b'k']);
assert_eq!(
"ok",
input
.read_string_with_u32_len(ByteOrder::LittleEndian, 8)
.expect("runtime u32 LE string should be read")
);
}
#[test]
fn test_string_read_ext_reports_length_and_utf8_errors() {
let mut input = Cursor::new(vec![b'a', b'b', b'c']);
assert_eq!(
ErrorKind::InvalidData,
input
.read_utf8_payload(3, 2)
.expect_err("oversized known-length payload should fail")
.kind()
);
let mut input = Cursor::new(vec![3, b'a', b'b', b'c']);
assert_eq!(
ErrorKind::InvalidData,
input
.read_utf8_string_uleb_usize(2)
.expect_err("oversized ULEB string should fail")
.kind()
);
let mut input = Cursor::new(vec![3, b'a', b'b', b'c']);
assert_eq!(
ErrorKind::InvalidData,
input
.read_utf8_string_uleb_u64(2)
.expect_err("oversized u64 ULEB string should fail")
.kind()
);
let mut input = Cursor::new(vec![0x80, 0x00, b'a']);
assert_eq!(
ErrorKind::InvalidData,
input
.read_utf8_string_uleb_usize_strict(8)
.expect_err("non-canonical ULEB length should fail")
.kind()
);
let mut input = Cursor::new(vec![0x80, 0x00, b'a']);
assert_eq!(
ErrorKind::InvalidData,
input
.read_utf8_string_uleb_u64_strict(8)
.expect_err("non-canonical u64 ULEB length should fail")
.kind()
);
let mut input = Cursor::new(vec![0, 2, 0xff, 0xff]);
assert_eq!(
ErrorKind::InvalidData,
input
.read_string_with_u16_len_be(8)
.expect_err("invalid UTF-8 should fail")
.kind()
);
let mut input = Cursor::new(vec![2, 0, 0xff, 0xff]);
assert_eq!(
ErrorKind::InvalidData,
input
.read_string_with_u16_len_le(8)
.expect_err("invalid UTF-8 should fail")
.kind()
);
let mut input = Cursor::new(vec![0, 0, 0, 3, b'a', b'b', b'c']);
assert_eq!(
ErrorKind::InvalidData,
input
.read_string_with_u32_len_be(2)
.expect_err("oversized u32 BE string should fail")
.kind()
);
let mut input = Cursor::new(vec![3, 0, 0, 0, b'a', b'b', b'c']);
assert_eq!(
ErrorKind::InvalidData,
input
.read_string_with_u32_len_le(2)
.expect_err("oversized u32 LE string should fail")
.kind()
);
let mut input = Cursor::new(vec![0x00, 0x03, b'a', b'b', b'c']);
assert_eq!(
ErrorKind::InvalidData,
input
.read_string_with_u16_len(ByteOrder::BigEndian, 2)
.expect_err("oversized runtime u16 string should fail")
.kind()
);
let mut input = Cursor::new(vec![0x03, 0x00, b'a', b'b', b'c']);
assert_eq!(
ErrorKind::InvalidData,
input
.read_string_with_u16_len(ByteOrder::LittleEndian, 2)
.expect_err("oversized runtime little-endian u16 string should fail")
.kind()
);
let mut input = Cursor::new(vec![0x00, 0x00, 0x00, 0x03, b'a', b'b', b'c']);
assert_eq!(
ErrorKind::InvalidData,
input
.read_string_with_u32_len(ByteOrder::BigEndian, 2)
.expect_err("oversized runtime u32 string should fail")
.kind()
);
let mut input = Cursor::new(vec![0x03, 0x00, 0x00, 0x00, b'a', b'b', b'c']);
assert_eq!(
ErrorKind::InvalidData,
input
.read_string_with_u32_len(ByteOrder::LittleEndian, 2)
.expect_err("oversized runtime little-endian u32 string should fail")
.kind()
);
}
#[test]
fn test_string_read_ext_returns_payload_read_error() {
let mut input = Cursor::new(vec![2, b'a']);
assert_eq!(
ErrorKind::UnexpectedEof,
input
.read_utf8_string_uleb_usize(8)
.expect_err("truncated payload should fail")
.kind()
);
let mut reader = Cursor::new(Vec::<u8>::new());
assert_eq!(
ErrorKind::UnexpectedEof,
reader
.read_utf8_string_uleb_usize(8)
.expect_err("length read error should be returned")
.kind()
);
let mut reader = Cursor::new(vec![0x00]);
assert_eq!(
ErrorKind::UnexpectedEof,
reader
.read_string_with_u16_len(ByteOrder::BigEndian, 8)
.expect_err("runtime u16 length read error should be returned")
.kind()
);
let mut reader = Cursor::new(vec![0x00]);
assert_eq!(
ErrorKind::UnexpectedEof,
reader
.read_string_with_u16_len(ByteOrder::LittleEndian, 8)
.expect_err("runtime little-endian u16 length read error should be returned")
.kind()
);
let mut reader = Cursor::new(vec![0x00]);
assert_eq!(
ErrorKind::UnexpectedEof,
reader
.read_string_with_u16_len_be(8)
.expect_err("u16 BE length read error should be returned")
.kind()
);
let mut reader = Cursor::new(vec![0x00]);
assert_eq!(
ErrorKind::UnexpectedEof,
reader
.read_string_with_u16_len_le(8)
.expect_err("u16 LE length read error should be returned")
.kind()
);
let mut reader = Cursor::new(vec![0x00, 0x00, 0x00]);
assert_eq!(
ErrorKind::UnexpectedEof,
reader
.read_string_with_u32_len(ByteOrder::BigEndian, 8)
.expect_err("runtime u32 length read error should be returned")
.kind()
);
let mut reader = Cursor::new(vec![0x00, 0x00, 0x00]);
assert_eq!(
ErrorKind::UnexpectedEof,
reader
.read_string_with_u32_len(ByteOrder::LittleEndian, 8)
.expect_err("runtime little-endian u32 length read error should be returned")
.kind()
);
let mut reader = Cursor::new(vec![0x00, 0x00, 0x00]);
assert_eq!(
ErrorKind::UnexpectedEof,
reader
.read_string_with_u32_len_be(8)
.expect_err("u32 BE length read error should be returned")
.kind()
);
let mut reader = Cursor::new(vec![0x00, 0x00, 0x00]);
assert_eq!(
ErrorKind::UnexpectedEof,
reader
.read_string_with_u32_len_le(8)
.expect_err("u32 LE length read error should be returned")
.kind()
);
let mut input = Cursor::new(Vec::<u8>::new());
assert_eq!(
ErrorKind::OutOfMemory,
input
.read_utf8_payload(usize::MAX, usize::MAX)
.expect_err("payload allocation error should be returned")
.kind()
);
}
#[test]
fn test_string_read_ext_preserves_failure_stage_positions() {
let mut oversized = Cursor::new(vec![3, b'a', b'b', b'c', 1, b'z']);
let error = oversized
.read_utf8_string_uleb_u64(2)
.expect_err("length exceeds limit");
assert_eq!(ErrorKind::InvalidData, error.kind());
assert_eq!(1, oversized.position());
assert_eq!(&oversized.get_ref()[1..], b"abc\x01z");
let mut invalid = Cursor::new(vec![1, 0xff, 1, b'z']);
let error = invalid
.read_utf8_string_uleb_u64(2)
.expect_err("payload is invalid UTF-8");
assert_eq!(ErrorKind::InvalidData, error.kind());
assert_eq!(2, invalid.position());
assert_eq!("z", invalid.read_utf8_string_uleb_u64(2).expect("next frame is intact"));
assert_eq!(4, invalid.position());
}