Introduce Pheon GDSII parsing and layout review tools
Implement native GDSII reading and writing with Arrow interchange, hierarchy scans and selective cell loading. Validate numeric packing and array pitches, and preserve normalized REAL8 encodings exactly within the supported range. Add standalone dump, structural diff, hierarchy, layer-name annotation and Git diff commands. Provide scoped text and JSON reports, deterministic comparison, and record-preserving metadata edits. Package the shared library and optional Python Arrow readers as pheon using CFFI ABI mode and Python-independent platform wheels. Include locked source builds, native API checks, cross-platform artifact workflows, documentation and regression coverage. Require Rust 1.88 or newer and Python 3.11 or newer. Validation: 87 native tests, 14 binding tests on Python 3.11/3.13/3.14, and 71 installed companion integration tests pass. Full, scanned and selected Arrow layouts match the previous schemas.
This commit is contained in:
parent
e0a634f476
commit
b3346b40a4
1 changed files with 12956 additions and 1 deletions
157
tests/ffi_errors.rs
Normal file
157
tests/ffi_errors.rs
Normal file
|
|
@ -0,0 +1,157 @@
|
|||
mod common;
|
||||
|
||||
use arrow_array::ffi::{FFI_ArrowArray, FFI_ArrowSchema};
|
||||
use pheon::{last_error_message, read_path, read_cells_bytes, scan_bytes, records};
|
||||
use std::{ffi::{CStr, CString}, fs, path::PathBuf, ptr};
|
||||
|
||||
struct TestFile(PathBuf);
|
||||
|
||||
impl TestFile {
|
||||
fn new(name: &str, contents: &[u8]) -> Self {
|
||||
let path = std::env::temp_dir().join(format!("pheon-{name}-{}.gds", std::process::id()));
|
||||
fs::write(&path, contents).unwrap();
|
||||
Self(path)
|
||||
}
|
||||
|
||||
fn cpath(&self) -> CString {
|
||||
CString::new(self.0.to_str().unwrap()).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for TestFile {
|
||||
fn drop(&mut self) {
|
||||
let _ = fs::remove_file(&self.0);
|
||||
}
|
||||
}
|
||||
|
||||
fn error_message() -> String {
|
||||
let message = last_error_message();
|
||||
assert!(!message.is_null());
|
||||
unsafe { CStr::from_ptr(message) }.to_str().unwrap().to_owned()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn path_errors_preserve_outputs_and_convert_parser_panics() {
|
||||
let mut array = FFI_ArrowArray::empty();
|
||||
let mut schema = FFI_ArrowSchema::empty();
|
||||
assert_eq!(unsafe { read_path(ptr::null(), &mut array, &mut schema) }, -1);
|
||||
assert!(error_message().contains("path pointer was null"));
|
||||
assert!(array.is_released());
|
||||
|
||||
// A mismatched timestamp size currently makes Record::read panic.
|
||||
let mut bytes = Vec::new();
|
||||
common::record(&mut bytes, records::RTAG_HEADER, &600_i16.to_be_bytes());
|
||||
common::record(&mut bytes, records::RTAG_BGNLIB, &[0; 12]);
|
||||
let file = TestFile::new("invalid-timestamps", &bytes);
|
||||
assert_eq!(unsafe { read_path(file.cpath().as_ptr(), &mut array, &mut schema) }, -1);
|
||||
assert!(error_message().contains("Expected record size"));
|
||||
assert!(array.is_released());
|
||||
|
||||
let mut bytes = common::library_header();
|
||||
common::record(&mut bytes, records::RTAG_ENDLIB, &[]);
|
||||
let file = TestFile::new("valid-empty", &bytes);
|
||||
assert_eq!(unsafe { read_path(file.cpath().as_ptr(), &mut array, &mut schema) }, 0);
|
||||
assert!(!array.is_released());
|
||||
assert!(last_error_message().is_null());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn other_threads_do_not_clear_or_free_error_messages() {
|
||||
let mut array = FFI_ArrowArray::empty();
|
||||
let mut schema = FFI_ArrowSchema::empty();
|
||||
assert_eq!(unsafe { read_path(ptr::null(), &mut array, &mut schema) }, -1);
|
||||
let pointer = last_error_message();
|
||||
let message = error_message();
|
||||
|
||||
std::thread::spawn(|| {
|
||||
assert!(last_error_message().is_null());
|
||||
let mut bytes = common::library_header();
|
||||
common::record(&mut bytes, records::RTAG_ENDLIB, &[]);
|
||||
let file = TestFile::new("thread-empty", &bytes);
|
||||
let mut array = FFI_ArrowArray::empty();
|
||||
let mut schema = FFI_ArrowSchema::empty();
|
||||
assert_eq!(unsafe { read_path(ptr::null(), &mut array, &mut schema) }, -1);
|
||||
assert_eq!(unsafe { read_path(file.cpath().as_ptr(), &mut array, &mut schema) }, 0);
|
||||
assert!(last_error_message().is_null());
|
||||
}).join().unwrap();
|
||||
|
||||
assert_eq!(last_error_message(), pointer);
|
||||
assert_eq!(unsafe { CStr::from_ptr(pointer) }.to_str().unwrap(), message);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn null_arrow_outputs_return_an_error() {
|
||||
assert_eq!(unsafe { read_path(ptr::null(), ptr::null_mut(), ptr::null_mut()) }, -1);
|
||||
assert!(error_message().contains("Arrow output pointer was null"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn large_malformed_inputs_report_offsets_without_dumping_contents() {
|
||||
let mut bytes = vec![0; 1024 * 1024 + 4];
|
||||
bytes[..4].copy_from_slice(&[0, 4, 0xff, 0xff]);
|
||||
let mut array = FFI_ArrowArray::empty();
|
||||
let mut schema = FFI_ArrowSchema::empty();
|
||||
let file = TestFile::new("large-malformed", &bytes);
|
||||
assert_eq!(
|
||||
unsafe { scan_bytes(bytes.as_ptr(), bytes.len(), &mut array, &mut schema) },
|
||||
-1
|
||||
);
|
||||
let scan_message = error_message();
|
||||
assert_eq!(
|
||||
unsafe { read_path(file.cpath().as_ptr(), &mut array, &mut schema) },
|
||||
-1
|
||||
);
|
||||
let read_message = error_message();
|
||||
assert_eq!(
|
||||
unsafe {
|
||||
read_cells_bytes(
|
||||
bytes.as_ptr(),
|
||||
bytes.len(),
|
||||
ptr::null(),
|
||||
0,
|
||||
&mut array,
|
||||
&mut schema,
|
||||
)
|
||||
},
|
||||
-1
|
||||
);
|
||||
let selected_message = error_message();
|
||||
for message in [scan_message, read_message, selected_message] {
|
||||
assert!(message.contains("at byte 4"), "{message}");
|
||||
assert!(message.contains("Unexpected record"), "{message}");
|
||||
assert!(message.len() < 256);
|
||||
}
|
||||
assert!(array.is_released());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn selected_cell_errors_use_file_offsets_for_bounded_ranges() {
|
||||
let mut bytes = common::library_header();
|
||||
let start = bytes.len() as u64;
|
||||
common::record(&mut bytes, records::RTAG_BGNSTR, &[0; 24]);
|
||||
common::record(&mut bytes, records::RTAG_STRNAME, b"top\0");
|
||||
common::record(&mut bytes, records::RTAG_ENDSTR, &[]);
|
||||
let cell_end = bytes.len();
|
||||
common::record(&mut bytes, records::RTAG_ENDLIB, &[]);
|
||||
let range_end = bytes.len() as u64;
|
||||
bytes.resize(bytes.len() + 1024, 0);
|
||||
let ranges = [start, range_end];
|
||||
let mut array = FFI_ArrowArray::empty();
|
||||
let mut schema = FFI_ArrowSchema::empty();
|
||||
assert_eq!(
|
||||
unsafe {
|
||||
read_cells_bytes(
|
||||
bytes.as_ptr(),
|
||||
bytes.len(),
|
||||
ranges.as_ptr(),
|
||||
1,
|
||||
&mut array,
|
||||
&mut schema,
|
||||
)
|
||||
},
|
||||
-1
|
||||
);
|
||||
assert!(error_message().contains(&format!(
|
||||
"at byte {cell_end}: Cell byte range extends past ENDSTR"
|
||||
)));
|
||||
}
|
||||
Loading…
Reference in a new issue