99 lines
3.6 KiB
Rust
99 lines
3.6 KiB
Rust
|
|
mod common;
|
||
|
|
|
||
|
|
use pheon::{
|
||
|
|
basic::ErrType,
|
||
|
|
record::{
|
||
|
|
ASCII, BitArray, DateTimePair, Empty, Int2, Int2Array, Int4, Int4Array, Real8, Real8Pair,
|
||
|
|
Record, RecordData,
|
||
|
|
},
|
||
|
|
records,
|
||
|
|
};
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn fixed_record_sizes_are_checked_by_every_read_entry_point() {
|
||
|
|
let payload = [0; 4];
|
||
|
|
let mut bytes = Vec::new();
|
||
|
|
common::record(&mut bytes, records::RTAG_HEADER, &payload);
|
||
|
|
let mut prefixed = Vec::new();
|
||
|
|
common::record(&mut prefixed, records::RTAG_LIBNAME, b"skip");
|
||
|
|
prefixed.extend_from_slice(&bytes);
|
||
|
|
|
||
|
|
for result in [
|
||
|
|
records::HEADER::read(&bytes),
|
||
|
|
records::HEADER::skip_and_read(&prefixed),
|
||
|
|
records::HEADER::read_data(&payload, 4),
|
||
|
|
] {
|
||
|
|
let (remaining, reason) = result.unwrap_err();
|
||
|
|
assert_eq!(remaining, payload);
|
||
|
|
assert!(matches!(reason, ErrType::Failed(message)
|
||
|
|
if message == "Expected record size 2, got 4"));
|
||
|
|
}
|
||
|
|
|
||
|
|
// The underlying integer-array payload is aligned, but COLROW needs two values.
|
||
|
|
let payload = [0; 2];
|
||
|
|
let mut bytes = Vec::new();
|
||
|
|
common::record(&mut bytes, records::RTAG_COLROW, &payload);
|
||
|
|
assert!(records::COLROW::read(&bytes).is_err());
|
||
|
|
assert!(records::COLROW::skip_and_read(&bytes).is_err());
|
||
|
|
assert!(records::COLROW::read_data(&payload, 2).is_err());
|
||
|
|
}
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn metadata_size_constraints_apply_to_every_read_entry_point() {
|
||
|
|
macro_rules! check {
|
||
|
|
($record:ident, $tag:ident) => {{
|
||
|
|
let payload = [b'x'; 44];
|
||
|
|
let mut bytes = Vec::new();
|
||
|
|
common::record(&mut bytes, records::$tag, &payload);
|
||
|
|
let mut prefixed = common::library_header();
|
||
|
|
prefixed.extend_from_slice(&bytes);
|
||
|
|
assert_eq!(records::$record::read(&bytes).unwrap().1, payload);
|
||
|
|
assert_eq!(
|
||
|
|
records::$record::skip_and_read(&prefixed).unwrap().1,
|
||
|
|
payload
|
||
|
|
);
|
||
|
|
assert_eq!(
|
||
|
|
records::$record::read_data(&payload, 44).unwrap().1,
|
||
|
|
payload
|
||
|
|
);
|
||
|
|
|
||
|
|
let payload = [b'x'; 46];
|
||
|
|
let mut bytes = Vec::new();
|
||
|
|
common::record(&mut bytes, records::$tag, &payload);
|
||
|
|
assert!(records::$record::read(&bytes).is_err());
|
||
|
|
assert!(records::$record::skip_and_read(&bytes).is_err());
|
||
|
|
assert!(records::$record::read_data(&payload, 46).is_err());
|
||
|
|
}};
|
||
|
|
}
|
||
|
|
|
||
|
|
check!(REFLIBS, RTAG_REFLIBS);
|
||
|
|
check!(FONTS, RTAG_FONTS);
|
||
|
|
check!(ATTRTABLE, RTAG_ATTRTABLE);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn malformed_payload_sizes_return_errors_without_panicking() {
|
||
|
|
let payload = [0; 32];
|
||
|
|
assert!(BitArray::read(&payload, 4).is_err());
|
||
|
|
assert!(Int2::read(&payload, 4).is_err());
|
||
|
|
assert!(Int4::read(&payload, 2).is_err());
|
||
|
|
assert!(Real8::read(&payload, 4).is_err());
|
||
|
|
assert!(Real8Pair::read(&payload, 8).is_err());
|
||
|
|
assert!(DateTimePair::read(&payload, 12).is_err());
|
||
|
|
assert!(Empty::read(&payload, 2).is_err());
|
||
|
|
assert!(Int2Array::read(&payload, 3).is_err());
|
||
|
|
assert!(Int4Array::read(&payload, 6).is_err());
|
||
|
|
|
||
|
|
let mut bytes = Vec::new();
|
||
|
|
common::record(&mut bytes, records::RTAG_XY, &payload[..6]);
|
||
|
|
assert!(records::XY::read(&bytes).is_err());
|
||
|
|
assert!(records::XY::skip_and_read(&bytes).is_err());
|
||
|
|
assert!(records::XY::read_data(&payload, 6).is_err());
|
||
|
|
|
||
|
|
// Valid sizes still reject a truncated buffer; an empty string remains valid.
|
||
|
|
assert!(Int4::read(&payload[..2], 4).is_err());
|
||
|
|
assert!(Real8Pair::read(&payload[..8], 16).is_err());
|
||
|
|
assert!(DateTimePair::read(&payload[..12], 24).is_err());
|
||
|
|
assert!(Int4Array::read(&payload[..4], 8).is_err());
|
||
|
|
assert_eq!(ASCII::read(&[], 0).unwrap(), (&[][..], Vec::<u8>::new()));
|
||
|
|
}
|