Files
faunosteo/crates/core/tests/pdf_robustness.rs
T

159 lines
4.9 KiB
Rust

//! R4.2: the reader never panics on content. Seeded mutations (byte flips, truncations,
//! splices of PDF tokens) of the reference and hostile PDFs go through
//! `read_attached_document` and `import_pdf`; a panic (an index out of range, an overflow
//! in an offset sum) would be a trap in the browser and fails here. The seed is fixed so a
//! failure is reproducible; the count keeps the suite under a few seconds in debug.
#![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)]
mod common;
use common::r4::{r4_input, repo_root};
use faunosteo_core::domain::js::parse_json;
use faunosteo_core::domain::sheet::{BundledSheetData, LoadedSheet};
use faunosteo_core::pdf::import::import_pdf;
use faunosteo_core::pdf::read::probe::{ProbeLimits, read_attached_document};
use serde_json::{Value, json};
/// xorshift64*, enough for mutation choices.
struct Rng(u64);
impl Rng {
fn next(&mut self) -> u64 {
self.0 ^= self.0 >> 12;
self.0 ^= self.0 << 25;
self.0 ^= self.0 >> 27;
self.0.wrapping_mul(0x2545_F491_4F6C_DD1D)
}
fn below(&mut self, n: usize) -> usize {
(self.next() % (n.max(1) as u64)) as usize
}
}
const TOKENS: &[&[u8]] = &[
b"stream\n",
b"endstream",
b"/Type /EmbeddedFile",
b"/Type/Filespec",
b"<<",
b">>",
b"/Length 0",
b"/Length 99999999999",
b"/Filter /FlateDecode",
b"/EF << /F 1 0 R >>",
b"/Keywords (faunosteo-archive/1)",
b"\xFE\xFF",
b"(",
b")",
b"<",
b"\\",
b"%PDF-",
b"\r\n",
b"\x00",
b"obj",
b"0 0 obj",
b"\xEF\xBB\xBF",
b"{\"version\":3",
b"[",
b"\"",
];
fn mutate(rng: &mut Rng, base: &[u8]) -> Vec<u8> {
let mut bytes = base.to_vec();
for _ in 0..1 + rng.below(4) {
match rng.below(4) {
0 if !bytes.is_empty() => {
let at = rng.below(bytes.len());
bytes[at] = rng.next() as u8;
}
1 if !bytes.is_empty() => {
let at = rng.below(bytes.len());
bytes.truncate(at);
}
2 => {
let at = rng.below(bytes.len() + 1);
let token = TOKENS[rng.below(TOKENS.len())];
bytes.splice(at..at, token.iter().copied());
}
_ if bytes.len() > 2 => {
let from = rng.below(bytes.len());
let to = from + rng.below(bytes.len() - from);
bytes.drain(from..to);
}
_ => {}
}
}
bytes
}
fn draft_sheet() -> LoadedSheet {
let seed = repo_root().join("supabase/seed/anatomical-sheets/equine/draft");
let read = |name: &str| std::fs::read_to_string(seed.join(name)).unwrap();
let payload = json!({
"status": "draft",
"ref": parse_json(&read("sheet-ref.json")).unwrap(),
"document": parse_json(&read("document.json")).unwrap(),
"migrationMap": parse_json(&read("report-migration-map.json")).unwrap(),
"assets": {},
"assetHashes": {},
});
let data: BundledSheetData = serde_json::from_value(payload).unwrap();
LoadedSheet::from_bundle(data).unwrap()
}
fn bases() -> Vec<(String, Vec<u8>)> {
let fixtures = repo_root().join("apps/app/tests/fixtures");
let mut out = vec![
(
"report-v3.bare.pdf".to_owned(),
std::fs::read(fixtures.join("report-v3.bare.pdf")).unwrap(),
),
(
"report-v2.full.pdf".to_owned(),
std::fs::read(fixtures.join("report-v2.full.pdf")).unwrap(),
),
(
"demo.fr.nologos.pdf".to_owned(),
std::fs::read(fixtures.join("pdf/r3/demo.fr.nologos.pdf")).unwrap(),
),
];
let oracle: Value = serde_json::from_str(
&std::fs::read_to_string(repo_root().join("crates/core/tests/oracles/r4/read.json"))
.unwrap(),
)
.unwrap();
for row in oracle["inputs"].as_array().unwrap() {
let file = row["file"].as_str().unwrap();
if row["stored"] == "bytes"
&& row["kind"] == "pdf"
&& row["length"].as_u64().unwrap() < 200_000
{
out.push((file.to_owned(), r4_input("read", file)));
}
}
out
}
#[test]
fn mutated_pdfs_never_panic_the_reader() {
let sheet = draft_sheet();
let bases = bases();
let mut rng = Rng(0x2026_1018_5EED);
let (mut reads, mut imports) = (0u32, 0u32);
for (name, base) in &bases {
for round in 0..20 {
let bytes = mutate(&mut rng, base);
let outcome = std::panic::catch_unwind(|| {
let _ = read_attached_document(&bytes, ProbeLimits::PRODUCTION);
let _ = import_pdf(&sheet, &bytes, ProbeLimits::PRODUCTION);
});
assert!(outcome.is_ok(), "panic on {name} mutation {round}");
reads += 1;
imports += 1;
}
}
assert!(
reads > 2_000,
"{reads} reads, {imports} imports: too few bases"
);
}