pheon/tests/writing.rs
Jan Petykiewicz 31afef0529 Reject invalid record sizes and return malformed-input errors
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.
2026-11-16 21:28:54 -08:00

192 lines
6.6 KiB
Rust

use arrow_array::Array;
use pheon::{
basic::{pack_bitarray, pack_int2, pack_int4, pack_real8},
library::{FileHeader, read_library},
record::{Int2Array, Int4Array, Record, RecordData, RecordHeader},
records,
};
use std::io::{self, Write};
#[test]
fn record_headers_include_their_own_size() {
let mut bytes = Vec::new();
assert_eq!(records::HEADER::write(&mut bytes, &600).unwrap(), 6);
assert_eq!(bytes, [0, 6, 0, 2, 2, 88]);
let (remaining, value) = records::HEADER::read(&bytes).unwrap();
assert!(remaining.is_empty());
assert_eq!(value, 600);
let maximum = RecordHeader {
data_size: u16::MAX - 4,
tag: 0x1003,
};
assert_eq!(maximum.pack_into().unwrap(), [0xff, 0xff, 0x10, 0x03]);
}
#[test]
fn integer_array_records_round_trip() {
let mut bytes = Vec::new();
records::COLROW::write(&mut bytes, &[2, 3]).unwrap();
let (remaining, values) = records::COLROW::read(&bytes).unwrap();
assert!(remaining.is_empty());
assert_eq!(values.collect::<Vec<_>>(), [2, 3]);
let mut bytes = Vec::new();
let values = [i32::MIN, -1, 0, i32::MAX];
records::XY::write(&mut bytes, &values).unwrap();
let (remaining, parsed) = records::XY::read(&bytes).unwrap();
assert!(remaining.is_empty());
assert_eq!(parsed.collect::<Vec<_>>(), values);
}
#[test]
fn scalar_packers_write_only_their_prefix_and_check_short_buffers() {
type Case = (fn(&mut [u8]), &'static [u8]);
let cases: &[Case] = &[
(|bytes| pack_bitarray(bytes, &[true; 16]), &[0xff, 0xff]),
(|bytes| pack_int2(bytes, -2), &[0xff, 0xfe]),
(|bytes| pack_int4(bytes, -2), &[0xff, 0xff, 0xff, 0xfe]),
(
|bytes| pack_real8(bytes, 1.0).unwrap(),
&[0x41, 0x10, 0, 0, 0, 0, 0, 0],
),
];
for (pack, expected) in cases {
let mut bytes = [0xa5; 16];
pack(&mut bytes);
assert_eq!(&bytes[..expected.len()], *expected);
assert!(bytes[expected.len()..].iter().all(|&byte| byte == 0xa5));
let mut short = vec![0xa5; expected.len() - 1];
assert!(
std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| pack(&mut short))).is_err()
);
assert!(short.iter().all(|&byte| byte == 0xa5));
}
// Invalid REAL8 values are rejected before accessing the output buffer.
assert!(pack_real8(&mut [], f64::INFINITY).is_err());
}
#[test]
fn array_packers_reject_nonexact_buffers_before_writing() {
for width in [2, 4] {
let required = 2 * width;
for size in [required - 1, required + 1, required + width] {
let mut bytes = vec![0xa5; size];
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
if width == 2 {
Int2Array::pack_into(&mut bytes, &[-1, 1]);
} else {
Int4Array::pack_into(&mut bytes, &[-1, 1]);
}
}));
assert!(result.is_err());
assert!(bytes.iter().all(|&byte| byte == 0xa5));
}
}
}
#[test]
fn string_records_pad_only_when_needed_and_support_empty_strings() {
for value in [b"".as_slice(), b"odd", b"even"] {
let mut bytes = Vec::new();
let expected_size = 4 + value.len() + value.len() % 2;
assert_eq!(
records::LIBNAME::write(&mut bytes, value).unwrap(),
expected_size
);
assert_eq!(bytes.len(), expected_size);
let (remaining, parsed) = records::LIBNAME::read(&bytes).unwrap();
assert!(remaining.is_empty());
assert_eq!(parsed, value);
if value.len() % 2 == 1 {
assert_eq!(bytes.last(), Some(&0));
}
}
}
struct ShortWriter(Vec<u8>);
impl Write for ShortWriter {
fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
let size = bytes.len().min(1);
self.0.extend_from_slice(&bytes[..size]);
Ok(size)
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
#[test]
fn library_header_round_trips_through_a_short_writer() {
let header = FileHeader::new(b"odd", 1e-9, 0.001);
let mut writer = ShortWriter(Vec::new());
let mut written = header.write(&mut writer).unwrap();
written += records::ENDLIB::write(&mut writer, &()).unwrap();
assert_eq!(written, writer.0.len());
let (remaining, library) = read_library(&writer.0).unwrap();
assert!(remaining.is_empty());
assert_eq!(library.len(), 1);
}
#[test]
fn oversized_records_fail_before_writing() {
let mut bytes = Vec::new();
let error = records::XY::write(&mut bytes, &vec![0; 16383]).unwrap_err();
assert_eq!(error.kind(), io::ErrorKind::InvalidInput);
assert!(bytes.is_empty());
// Header packing and direct header writes reject every overflowing size too.
for data_size in u16::MAX - 3..=u16::MAX {
let header = RecordHeader {
tag: records::RTAG_XY,
data_size,
};
assert_eq!(
header.pack_into().unwrap_err().kind(),
io::ErrorKind::InvalidInput
);
assert_eq!(
header.write(&mut bytes).unwrap_err().kind(),
io::ErrorKind::InvalidInput
);
assert!(bytes.is_empty());
}
}
#[test]
fn invalid_record_sizes_preserve_the_output() {
let mut bytes = vec![0xa5];
for count in [0, 1, 3] {
let error = records::COLROW::write(&mut bytes, &vec![2; count]).unwrap_err();
assert_eq!(error.kind(), io::ErrorKind::InvalidInput);
assert_eq!(bytes, [0xa5]);
}
let error = records::COLROW::write_header(&mut bytes, 2).unwrap_err();
assert_eq!(error.kind(), io::ErrorKind::InvalidInput);
assert_eq!(bytes, [0xa5]);
macro_rules! check_metadata {
($record:ident) => {
for count in [43, 44, 45] {
let payload = vec![b'x'; count];
let mut bytes = vec![0xa5];
let result = records::$record::write(&mut bytes, &payload);
if count == 45 {
assert_eq!(result.unwrap_err().kind(), io::ErrorKind::InvalidInput);
assert_eq!(bytes, [0xa5]);
} else {
// Validation uses the padded record size, not the input length.
assert_eq!(result.unwrap(), 48);
assert_eq!(records::$record::read(&bytes[1..]).unwrap().1, payload);
}
}
let error = records::$record::write_header(&mut bytes, 46).unwrap_err();
assert_eq!(error.kind(), io::ErrorKind::InvalidInput);
assert_eq!(bytes, [0xa5]);
};
}
check_metadata!(REFLIBS);
check_metadata!(FONTS);
check_metadata!(ATTRTABLE);
}