Validate payload sizes before record/header writes and make header packing return io::Result when the total size overflows the format limit. Return located parse errors for invalid UTF-8 and timestamp year overflow. Validate cell timestamps consistently in full reads, selected reads, and hierarchy scans while retaining scan behavior for skipped text geometry. Add regression coverage for preserved writer outputs, padded metadata sizes, malformed strings, timestamp boundaries, bounded errors, and Python bindings. Document the fallible header packing API. Validation: all 97 Rust tests pass in debug and release; all 16 Python tests pass against the rebuilt native library.
122 lines
4.6 KiB
Rust
122 lines
4.6 KiB
Rust
mod common;
|
|
|
|
use pheon::{
|
|
basic::{ErrType, parse_datetime},
|
|
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 timestamp_year_conversion_checks_the_int16_boundaries() {
|
|
for stored_year in [i16::MIN, -1900, 0, i16::MAX - 1900] {
|
|
let mut payload = [0; 12];
|
|
payload[..2].copy_from_slice(&stored_year.to_be_bytes());
|
|
let (remaining, date) = parse_datetime(&payload).unwrap();
|
|
assert!(remaining.is_empty());
|
|
assert_eq!(i32::from(date[0]), i32::from(stored_year) + 1900);
|
|
}
|
|
for stored_year in [i16::MAX - 1899, i16::MAX] {
|
|
for timestamp in [0, 1] {
|
|
let mut payload = [0; 24];
|
|
let offset = timestamp * 12;
|
|
payload[offset..offset + 2].copy_from_slice(&stored_year.to_be_bytes());
|
|
let (remaining, error) = DateTimePair::read(&payload, 24).unwrap_err();
|
|
assert_eq!(remaining.as_ptr(), payload[offset..].as_ptr());
|
|
assert!(
|
|
matches!(error, ErrType::Failed(message) if message.contains("Timestamp year"))
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[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()));
|
|
}
|