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

807 lines
29 KiB
Rust

//! R4.5: the Rust PDF against the R3 build's PDFs (O2, `apps/app/tests/fixtures/pdf/r3/`),
//! with the difference classes of plan §B.2.3 applied: content streams compared after the
//! resource names are resolved (B3), image planes decompressed, links and Info by value,
//! the attachment byte for byte (the store-path export differs by the D-R11 report order
//! only). Then the digests of the R4 outputs (`goldens/r4/pdf.*.sha256`, written once with
//! `MAKE_R4_ORACLES=1`), the external checks (`qpdf --check`, `pdfdetach -list`, poppler at
//! 150 dpi against the R3 render) when the tools are installed, and the restoration round
//! trip through the R4 reader.
#![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)]
mod common;
use std::collections::BTreeMap;
use std::process::Command;
use common::r4::{repo_root, sha256_hex};
use faunosteo_core::domain::archive::Locale;
use faunosteo_core::domain::js::parse_json;
use faunosteo_core::domain::sheet::{BundledSheetData, LoadedSheet};
use faunosteo_core::json::to_js_compact;
use faunosteo_core::pdf::import::import_pdf;
use faunosteo_core::pdf::read::probe::{ProbeLimits, read_attached_document};
use faunosteo_core::pdf::write::export::{export_from_envelope, export_pdf};
use faunosteo_core::pdf::write::input::{ExportInput, Rasters};
use lopdf::{Dictionary, Document, Object, ObjectId};
use serde_json::{Value, json};
const REPORTS: [&str; 3] = ["demo", "v2full", "v2unresolved"];
const LOCALES: [&str; 2] = ["fr", "en"];
const MODES: [&str; 2] = ["nologos", "logos"];
const ONE_PX_PNG: &str = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==";
const TWO_BY_ONE_PNG: &str = "iVBORw0KGgoAAAANSUhEUgAAAAIAAAABCAIAAAB7QOjdAAAAD0lEQVR4nGPYEWX///9/AA0PBE/1T7nxAAAAAElFTkSuQmCC";
fn base64(text: &str) -> Vec<u8> {
let table: Vec<u8> = (b'A'..=b'Z')
.chain(b'a'..=b'z')
.chain(b'0'..=b'9')
.chain(*b"+/")
.collect();
let mut out = Vec::new();
let (mut acc, mut bits) = (0u32, 0u32);
for c in text.bytes().filter(|c| *c != b'=') {
acc = (acc << 6) | table.iter().position(|t| *t == c).unwrap() as u32;
bits += 6;
if bits >= 8 {
bits -= 8;
out.push((acc >> bits) as u8);
acc &= (1 << bits) - 1;
}
}
out
}
fn fixture(path: &str) -> Vec<u8> {
std::fs::read(repo_root().join("apps/app/tests/fixtures").join(path)).unwrap()
}
/// The envelope an R3 PDF carries, as JSON.
fn envelope_of(pdf: &[u8]) -> Value {
let located = read_attached_document(pdf, ProbeLimits::UNBOUNDED).unwrap();
parse_json(&located.text).unwrap()
}
fn input_from(envelope: &Value) -> ExportInput {
serde_json::from_value(json!({
"report": envelope["report"],
"sheetSnapshot": envelope["sheetSnapshot"],
"branding": envelope["branding"],
"labels": envelope["labels"],
"locale": envelope["locale"],
"exportedAt": envelope["exportedAt"],
}))
.unwrap()
}
fn stub_rasters(input: &ExportInput, png: &[u8]) -> Rasters {
let mut rasters = Rasters {
header_logo: Some(png.to_vec()),
footer_logo: Some(png.to_vec()),
powered_by: Some(png.to_vec()),
partner: Some(png.to_vec()),
views: Default::default(),
};
for observation in &input.report.observations {
for zone in &observation.zones {
rasters
.views
.entry((observation.id.clone(), zone.view_id.clone()))
.or_insert_with(|| png.to_vec());
}
}
rasters
}
/// One O2 run: the R3 PDF and the Rust export of the same input and rasters.
struct Run {
name: String,
r3: Vec<u8>,
r4: Vec<u8>,
envelope: String,
}
fn stub_run(report: &str, locale: &str, mode: &str) -> Run {
let r3 = fixture(&format!("pdf/r3/{report}.{locale}.{mode}.pdf"));
let input = input_from(&envelope_of(&r3));
let png = base64(if mode == "logos" {
TWO_BY_ONE_PNG
} else {
ONE_PX_PNG
});
let rasters = stub_rasters(&input, &png);
let result = export_pdf(input, &rasters).unwrap();
assert!(result.warnings.is_empty());
Run {
name: format!("{report}.{locale}.{mode}"),
r3,
r4: result.pdf,
envelope: result.envelope,
}
}
/// The real-raster run: v2full's first observation, the PNGs the browser made (manifest).
fn real_run() -> Run {
let r3 = fixture("pdf/r3/real/v2full-obs1.fr.logos.pdf");
let input = input_from(&envelope_of(&r3));
let manifest: Value = serde_json::from_slice(&fixture("pdf/r3/real/manifest.json")).unwrap();
let mut rasters = stub_rasters(&input, &base64(TWO_BY_ONE_PNG));
for call in manifest["calls"].as_array().unwrap() {
let png = fixture(&format!("pdf/r3/real/{}", call["png"].as_str().unwrap()));
assert_eq!(sha256_hex(&png), call["pngSha256"]);
let subject = &call["subject"];
if subject["kind"] == "logo" {
assert_eq!(subject["slot"], "partner");
rasters.partner = Some(png);
} else {
let key = (
subject["observationId"].as_str().unwrap().to_owned(),
subject["viewId"].as_str().unwrap().to_owned(),
);
rasters.views.insert(key, png);
}
}
let result = export_pdf(input, &rasters).unwrap();
assert!(result.warnings.is_empty());
Run {
name: "real.v2full-obs1.fr.logos".to_owned(),
r3,
r4: result.pdf,
envelope: result.envelope,
}
}
/// The store-path run: the report as the editor's store ordered it (D-R11: the attachment's
/// report is in contract order in R4, values identical).
fn store_path_run() -> Run {
let r3 = fixture("pdf/r3/store-path.fr.nologos.pdf");
let envelope = envelope_of(&r3);
let mut input = input_from(&envelope);
let report: Value = serde_json::from_slice(&fixture("pdf/r3/store-path.report.json")).unwrap();
input.report = serde_json::from_value(report).unwrap();
let rasters = stub_rasters(&input, &base64(ONE_PX_PNG));
let result = export_pdf(input, &rasters).unwrap();
Run {
name: "store-path.fr.nologos".to_owned(),
r3,
r4: result.pdf,
envelope: result.envelope,
}
}
fn all_runs() -> Vec<Run> {
let mut runs = Vec::new();
for report in REPORTS {
for locale in LOCALES {
for mode in MODES {
runs.push(stub_run(report, locale, mode));
}
}
}
runs.push(real_run());
runs.push(store_path_run());
runs
}
// ---------------------------------------------------------------------------
// Reading both files
fn load(bytes: &[u8]) -> Document {
Document::load_mem(bytes).unwrap()
}
fn pages(doc: &Document) -> Vec<ObjectId> {
doc.get_pages().into_values().collect()
}
/// Every image XObject of the document, by object number: the embed order in both files
/// (pdf-lib registered the images as it embedded them; ours are written in embed order).
fn image_order(doc: &Document) -> BTreeMap<ObjectId, usize> {
let mut ids: Vec<ObjectId> = doc
.objects
.iter()
.filter(|(_, object)| {
object.as_stream().is_ok_and(|s| {
s.dict.get(b"Subtype").and_then(Object::as_name).ok() == Some(b"Image")
&& s.dict.get(b"ColorSpace").and_then(Object::as_name).ok()
== Some(b"DeviceRGB")
})
})
.map(|(id, _)| *id)
.collect();
ids.sort();
ids.into_iter().enumerate().map(|(k, id)| (id, k)).collect()
}
/// `name → canonical token` for a page's fonts (by BaseFont) and images (by embed order).
fn resource_map(doc: &Document, page: ObjectId) -> BTreeMap<Vec<u8>, String> {
let (resources, _) = doc.get_page_resources(page).unwrap();
let resources = resources.unwrap();
let order = image_order(doc);
let mut map = BTreeMap::new();
if let Ok(fonts) = resources.get(b"Font").and_then(Object::as_dict) {
for (name, value) in fonts.iter() {
let font = doc.get_dictionary(value.as_reference().unwrap()).unwrap();
let base = String::from_utf8_lossy(font.get(b"BaseFont").unwrap().as_name().unwrap())
.into_owned();
map.insert(name.clone(), format!("/Font:{base}"));
}
}
if let Ok(xobjects) = resources.get(b"XObject").and_then(Object::as_dict) {
for (name, value) in xobjects.iter() {
let k = order[&value.as_reference().unwrap()];
map.insert(name.clone(), format!("/Image:{k}"));
}
}
map
}
/// The content stream with every resource name replaced by its canonical token.
fn canonical_content(doc: &Document, page: ObjectId) -> Vec<u8> {
let content = doc.get_page_content(page);
let map = resource_map(doc, page);
let mut out = Vec::with_capacity(content.len());
let mut i = 0;
while i < content.len() {
if content[i] == b'/' {
let end = content[i..]
.iter()
.position(|b| b.is_ascii_whitespace())
.map_or(content.len(), |p| i + p);
let name = &content[i + 1..end];
if let Some(token) = map.get(name) {
out.extend_from_slice(token.as_bytes());
i = end;
continue;
}
}
out.push(content[i]);
i += 1;
}
out
}
/// Every image XObject of the document in embed order: dictionary facts and planes.
fn images(doc: &Document) -> Vec<Value> {
let mut xobjects: Vec<(ObjectId, &Dictionary)> = Vec::new();
for (id, object) in doc.objects.iter() {
if let Ok(stream) = object.as_stream()
&& stream.dict.get(b"Subtype").and_then(Object::as_name).ok() == Some(b"Image")
&& stream
.dict
.get(b"ColorSpace")
.and_then(Object::as_name)
.ok()
== Some(b"DeviceRGB")
{
xobjects.push((*id, &stream.dict));
}
}
xobjects.sort_by_key(|(id, _)| *id);
xobjects
.into_iter()
.map(|(id, dict)| {
let stream = doc.get_object(id).unwrap().as_stream().unwrap();
let rgb = stream.decompressed_content().unwrap();
let smask = dict.get(b"SMask").ok().map(|r| {
let mask = doc.get_object(r.as_reference().unwrap()).unwrap().as_stream().unwrap();
json!({
"colorSpace": String::from_utf8_lossy(mask.dict.get(b"ColorSpace").unwrap().as_name().unwrap()),
"decode": format!("{:?}", mask.dict.get(b"Decode").unwrap()),
"bits": mask.dict.get(b"BitsPerComponent").unwrap().as_i64().unwrap(),
"width": mask.dict.get(b"Width").unwrap().as_i64().unwrap(),
"height": mask.dict.get(b"Height").unwrap().as_i64().unwrap(),
"sha256": sha256_hex(&mask.decompressed_content().unwrap()),
})
});
json!({
"width": dict.get(b"Width").unwrap().as_i64().unwrap(),
"height": dict.get(b"Height").unwrap().as_i64().unwrap(),
"bits": dict.get(b"BitsPerComponent").unwrap().as_i64().unwrap(),
"rgbSha256": sha256_hex(&rgb),
"smask": smask,
})
})
.collect()
}
/// `[x1 y1 x2 y2]` and URI of every Link annotation, per page, from the raw bytes (the
/// numbers' spelling is compared, lopdf would read them as f32).
fn links(doc: &Document, bytes: &[u8]) -> Vec<Value> {
let text = String::from_utf8_lossy(bytes);
let mut out = Vec::new();
for (p, page) in pages(doc).iter().enumerate() {
for annot in doc.get_page_annotations(*page).unwrap() {
let uri = annot
.get(b"A")
.unwrap()
.as_dict()
.unwrap()
.get(b"URI")
.unwrap()
.as_str()
.unwrap()
.to_vec();
let rect_f32: Vec<f32> = annot
.get(b"Rect")
.unwrap()
.as_array()
.unwrap()
.iter()
.map(|o| o.as_float().unwrap())
.collect();
out.push(
json!({ "page": p, "rect": rect_f32, "uri": String::from_utf8(uri).unwrap() }),
);
}
}
// The spellings: every `/Rect [...]` of the file in order.
let mut rects = Vec::new();
let mut rest = text.as_ref();
while let Some(at) = rest.find("/Rect") {
let after = &rest[at + 5..];
let open = after.find('[').unwrap();
let close = after.find(']').unwrap();
rects.push(
after[open + 1..close]
.split_whitespace()
.collect::<Vec<_>>()
.join(" "),
);
rest = &after[close..];
}
// Object order is free (B2): the spellings are compared as a sorted multiset.
rects.sort();
out.push(json!({ "rectSpellings": rects }));
out
}
fn info(doc: &Document) -> BTreeMap<String, Vec<u8>> {
let id = doc.trailer.get(b"Info").unwrap().as_reference().unwrap();
let dict = doc.get_dictionary(id).unwrap();
dict.iter()
.map(|(k, v)| {
(
String::from_utf8_lossy(k).into_owned(),
v.as_str().unwrap().to_vec(),
)
})
.collect()
}
/// The embedded file stream and the Filespec, by walking the catalog's names tree.
fn attachment(doc: &Document) -> (Dictionary, Vec<u8>, Dictionary) {
let root = doc.trailer.get(b"Root").unwrap().as_reference().unwrap();
let catalog = doc.get_dictionary(root).unwrap();
let names = catalog
.get(b"Names")
.unwrap()
.as_dict()
.unwrap()
.get(b"EmbeddedFiles")
.unwrap()
.as_dict()
.unwrap()
.get(b"Names")
.unwrap()
.as_array()
.unwrap();
let filespec = doc
.get_dictionary(names[1].as_reference().unwrap())
.unwrap()
.clone();
let ef = filespec
.get(b"EF")
.unwrap()
.as_dict()
.unwrap()
.get(b"F")
.unwrap()
.as_reference()
.unwrap();
let stream = doc.get_object(ef).unwrap().as_stream().unwrap();
assert_eq!(
catalog.get(b"AF").unwrap().as_array().unwrap()[0]
.as_reference()
.unwrap(),
names[1].as_reference().unwrap()
);
(
stream.dict.clone(),
stream.decompressed_content().unwrap(),
filespec,
)
}
fn dict_facts(dict: &Dictionary, skip: &[&[u8]]) -> BTreeMap<String, String> {
dict.iter()
.filter(|(k, _)| !skip.contains(&k.as_slice()))
.map(|(k, v)| (String::from_utf8_lossy(k).into_owned(), format!("{v:?}")))
.collect()
}
// ---------------------------------------------------------------------------
// Tests
#[test]
fn o2_streams_images_links_info_and_attachment_match_the_r3_build() {
let mut mismatches = Vec::new();
for run in all_runs() {
let (r3, r4) = (load(&run.r3), load(&run.r4));
let (p3, p4) = (pages(&r3), pages(&r4));
if p3.len() != p4.len() {
mismatches.push(format!(
"{}: {} pages, expected {}",
run.name,
p4.len(),
p3.len()
));
continue;
}
for (i, (a, b)) in p3.iter().zip(&p4).enumerate() {
let (c3, c4) = (canonical_content(&r3, *a), canonical_content(&r4, *b));
if c3 != c4 {
let at = c3
.iter()
.zip(&c4)
.position(|(x, y)| x != y)
.unwrap_or(c3.len().min(c4.len()));
mismatches.push(format!(
"{} page {i}: content differs at byte {at}: r3 …{:?}… r4 …{:?}…",
run.name,
String::from_utf8_lossy(&c3[at.saturating_sub(40)..(at + 40).min(c3.len())]),
String::from_utf8_lossy(&c4[at.saturating_sub(40)..(at + 40).min(c4.len())])
));
}
}
let (i3, i4) = (images(&r3), images(&r4));
if i3 != i4 {
mismatches.push(format!(
"{}: images\n r3 {}\n r4 {}",
run.name,
Value::Array(i3),
Value::Array(i4)
));
}
let (l3, l4) = (links(&r3, &run.r3), links(&r4, &run.r4));
if l3 != l4 {
let at = l3
.iter()
.zip(&l4)
.position(|(a, b)| a != b)
.unwrap_or(l3.len().min(l4.len()));
mismatches.push(format!(
"{}: links differ at entry {at} ({} vs {} entries)\n r3 {}\n r4 {}",
run.name,
l3.len(),
l4.len(),
l3.get(at).cloned().unwrap_or(Value::Null),
l4.get(at).cloned().unwrap_or(Value::Null)
));
}
let (mut n3, mut n4) = (info(&r3), info(&r4));
for key in ["Producer", "Creator"] {
assert!(n3.remove(key).is_some(), "{}: r3 has {key}", run.name);
assert_eq!(
n4.remove(key).unwrap(),
"\u{feff}Faunosteo"
.encode_utf16()
.flat_map(u16::to_be_bytes)
.collect::<Vec<u8>>(),
"{}: {key}",
run.name
);
}
if n3 != n4 {
mismatches.push(format!(
"{}: info differs: r3 {:?} r4 {:?}",
run.name,
n3.keys().collect::<Vec<_>>(),
n4
));
}
let (d3, a3, f3) = attachment(&r3);
let (d4, a4, f4) = attachment(&r4);
if run.name.starts_with("store-path") {
// D-R11: the report in contract order; the values identical.
assert_eq!(
to_js_compact(&parse_json(std::str::from_utf8(&a3).unwrap()).unwrap()["report"])
.unwrap()
.len(),
to_js_compact(&parse_json(&run.envelope).unwrap()["report"])
.unwrap()
.len()
);
let (v3, v4) = (
parse_json(std::str::from_utf8(&a3).unwrap()).unwrap(),
parse_json(std::str::from_utf8(&a4).unwrap()).unwrap(),
);
assert_eq!(v3, v4, "{}: attachment values", run.name);
} else if a3 != a4 {
let at = a3
.iter()
.zip(&a4)
.position(|(x, y)| x != y)
.unwrap_or(a3.len().min(a4.len()));
mismatches.push(format!(
"{}: attachment differs at byte {at} of {} / {}: …{}… vs …{}…",
run.name,
a3.len(),
a4.len(),
String::from_utf8_lossy(&a3[at.saturating_sub(60)..(at + 60).min(a3.len())]),
String::from_utf8_lossy(&a4[at.saturating_sub(60)..(at + 60).min(a4.len())])
));
}
assert_eq!(
a4,
run.envelope.as_bytes(),
"{}: the attachment is the envelope",
run.name
);
let skip: &[&[u8]] = &[b"Length", b"Filter"];
let (mut e3, mut e4) = (dict_facts(&d3, skip), dict_facts(&d4, skip));
// Params/Size: the attachment's own length (equal when the bytes are).
if run.name.starts_with("store-path") {
e3.remove("Params");
e4.remove("Params");
}
if e3 != e4 {
mismatches.push(format!(
"{}: embedded file dict r3 {e3:?} r4 {e4:?}",
run.name
));
}
let (s3, s4) = (dict_facts(&f3, &[b"EF"]), dict_facts(&f4, &[b"EF"]));
if s3 != s4 {
mismatches.push(format!("{}: filespec r3 {s3:?} r4 {s4:?}", run.name));
}
}
assert!(
mismatches.is_empty(),
"{} mismatches:\n{}",
mismatches.len(),
mismatches.join("\n")
);
}
/// The digests of the R4 outputs: written once (`MAKE_R4_ORACLES=1`), then asserted; the
/// wasm build must give the same bytes (`packages/core-wasm/tests`).
#[test]
fn r4_output_digests_are_frozen() {
let dir = repo_root().join("apps/app/tests/fixtures/goldens/r4");
let make = std::env::var("MAKE_R4_ORACLES").is_ok_and(|v| v == "1");
for run in all_runs() {
let path = dir.join(format!("pdf.{}.sha256", run.name));
let digest = sha256_hex(&run.r4);
if make {
std::fs::write(&path, format!("{digest}\n")).unwrap();
} else {
let expected = std::fs::read_to_string(&path)
.unwrap_or_else(|_| panic!("{path:?} missing: MAKE_R4_ORACLES=1"));
assert_eq!(expected.trim(), digest, "{}: output changed", run.name);
}
}
}
fn tool(name: &str) -> bool {
Command::new("which")
.arg(name)
.output()
.is_ok_and(|o| o.status.success())
}
/// `qpdf --check` and `pdfdetach -list` on every R4 output; poppler's render at 150 dpi
/// against the R3 PDF's, pixel for pixel (same renderer and fonts for both files).
#[test]
fn external_tools_accept_the_files_and_render_them_like_the_r3_build() {
if !(tool("qpdf") && tool("pdfdetach") && tool("pdftoppm")) {
println!("qpdf / poppler not installed: external checks skipped here");
return;
}
let dir = std::env::temp_dir().join(format!("faunosteo-r4-serialize-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let mut report = Vec::new();
for run in all_runs() {
let r4 = dir.join(format!("{}.r4.pdf", run.name));
let r3 = dir.join(format!("{}.r3.pdf", run.name));
std::fs::write(&r4, &run.r4).unwrap();
std::fs::write(&r3, &run.r3).unwrap();
let check = Command::new("qpdf")
.arg("--check")
.arg(&r4)
.output()
.unwrap();
assert!(
check.status.success(),
"{}: qpdf --check\n{}{}",
run.name,
String::from_utf8_lossy(&check.stdout),
String::from_utf8_lossy(&check.stderr)
);
let list = Command::new("pdfdetach")
.arg("-list")
.arg(&r4)
.output()
.unwrap();
let listed = String::from_utf8_lossy(&list.stdout);
assert!(
listed.contains("osteo-report.json"),
"{}: pdfdetach -list: {listed}",
run.name
);
let render = |pdf: &std::path::Path, stem: &str| {
let out = dir.join(stem);
let status = Command::new("pdftoppm")
.args(["-r", "150"])
.arg(pdf)
.arg(&out)
.status()
.unwrap();
assert!(status.success());
let mut pages: Vec<_> = std::fs::read_dir(&dir)
.unwrap()
.map(|e| e.unwrap().path())
.filter(|p| {
p.file_name()
.unwrap()
.to_string_lossy()
.starts_with(&format!("{stem}-"))
&& p.extension().is_some_and(|e| e == "ppm")
})
.collect();
pages.sort();
pages
.into_iter()
.map(|p| std::fs::read(p).unwrap())
.collect::<Vec<_>>()
};
let (img3, img4) = (
render(&r3, &format!("{}.r3", run.name)),
render(&r4, &format!("{}.r4", run.name)),
);
assert_eq!(img3.len(), img4.len(), "{}: rendered page count", run.name);
let mut differing = 0usize;
let mut total = 0usize;
for (a, b) in img3.iter().zip(&img4) {
assert_eq!(a.len(), b.len(), "{}: render size", run.name);
total += a.len() / 3;
differing += a.chunks(3).zip(b.chunks(3)).filter(|(x, y)| x != y).count();
}
report.push(format!(
"{}: {} pages rendered, {differing} of {total} pixels differ",
run.name,
img3.len()
));
assert_eq!(differing, 0, "{}: poppler render differs", run.name);
}
println!("{}", report.join("\n"));
let _ = std::fs::remove_dir_all(&dir);
}
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 manifest: Value = serde_json::from_str(&read("assets.json")).unwrap();
let mut assets = serde_json::Map::new();
let mut hashes = serde_json::Map::new();
for asset in manifest.as_array().unwrap() {
let id = asset["svg_asset_id"].as_str().unwrap().to_owned();
assets.insert(
id.clone(),
Value::String(read(&format!("assets/{}", asset["file"].as_str().unwrap()))),
);
hashes.insert(id, asset["sha256"].clone());
}
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": assets, "assetHashes": hashes,
});
LoadedSheet::from_bundle(serde_json::from_value::<BundledSheetData>(payload).unwrap()).unwrap()
}
/// Export → import on the R4 reader gives the report back without loss, and the same
/// outcome as importing the R3 PDF; a re-export from the envelope gives the same file.
#[test]
fn restoration_round_trip_loses_nothing() {
let sheet = draft_sheet();
for run in all_runs() {
let from_r4 = import_pdf(&sheet, &run.r4, ProbeLimits::PRODUCTION).unwrap();
let from_r3 = import_pdf(&sheet, &run.r3, ProbeLimits::PRODUCTION).unwrap();
assert_eq!(from_r4, from_r3, "{}: import outcome", run.name);
let envelope = parse_json(&run.envelope).unwrap();
assert_eq!(
to_js_compact(&from_r4.read.report).unwrap(),
to_js_compact(&envelope["report"]).unwrap(),
"{}: report",
run.name
);
if !run.name.starts_with("store-path") {
let locale = if envelope["locale"] == "en" {
Locale::En
} else {
Locale::Fr
};
let input = input_from(&envelope);
let png = base64(
if run.name.contains("logos") && !run.name.contains("nologos") {
TWO_BY_ONE_PNG
} else {
ONE_PX_PNG
},
);
let rasters = if run.name.starts_with("real") {
continue;
} else {
stub_rasters(&input, &png)
};
let again = export_from_envelope(
&envelope,
locale,
envelope["exportedAt"].as_str().unwrap(),
&rasters,
)
.unwrap();
assert_eq!(
again.pdf, run.r4,
"{}: re-export from the envelope",
run.name
);
}
}
}
/// The export-side security cases of the TypeScript suite: a hostile SVG asset in the
/// snapshot is `snapshot_invalid` before any page; a URL that would close a literal string
/// never reaches the file (the schema refuses it, and the link sink writes hex ASCII only);
/// a PNG logo the gate refuses fails alone with `logo_failed`.
#[test]
fn export_side_security_cases_hold() {
use faunosteo_core::pdf::write::input::{ExportWarning, LogoSlot, check_input};
let r3 = fixture("pdf/r3/demo.fr.logos.pdf");
let envelope = envelope_of(&r3);
// Hostile asset: an SVG with a doctype (the reader refuses it; the TS sanitiser did too).
let mut hostile = envelope.clone();
let doctype = std::fs::read_to_string(
repo_root().join("crates/core/tests/fixtures/svg/hostile/doctype-entities.svg"),
)
.unwrap();
hostile["sheetSnapshot"]["assets"][0]["svg"] = Value::String(doctype);
let err = check_input(input_from(&hostile)).err().unwrap();
assert_eq!(err.kind.as_str(), "snapshot_invalid");
assert!(err.detail.starts_with("asset "), "{}", err.detail);
// URL injection: refused by the schema; the sink is hex ASCII for the accepted ones.
let mut injected = envelope.clone();
injected["branding"]["siteUrl"] =
Value::String("https://a.b/) /Next << /S /JavaScript >>".to_owned());
assert_eq!(
check_input(input_from(&injected)).err().unwrap().detail,
"branding"
);
let text = String::from_utf8_lossy(&stub_run("demo", "fr", "logos").r4).into_owned();
assert!(!text.contains("/URI ("), "URIs are written as hex strings");
assert!(
text.contains(&format!(
"/URI <{}>",
"https://faunosteo.com"
.bytes()
.map(|b| format!("{b:02x}"))
.collect::<String>()
))
);
// A refused PNG logo: the slot fails alone, the export completes with the warning.
let mut bad_logo = envelope.clone();
bad_logo["branding"]["headerLogo"] = json!({ "kind": "png", "base64": "iVBORw0KGgo=" });
let input = input_from(&bad_logo);
let rasters = stub_rasters(&input, &base64(TWO_BY_ONE_PNG));
let result = export_pdf(input, &rasters).unwrap();
assert_eq!(
result.warnings,
vec![ExportWarning::LogoFailed {
slot: LogoSlot::HeaderLogo
}]
);
assert!(read_attached_document(&result.pdf, ProbeLimits::PRODUCTION).is_ok());
}