find_embedded_files, decode_pdf_string, has_archive_stamp on the frozen O7 oracle: 144 PDF inputs, 178 candidates, 19 string rows, all equal to the TypeScript.
133 lines
4.4 KiB
Rust
133 lines
4.4 KiB
Rust
//! O7 scan suite (R4.2a): `find_embedded_files`, `has_archive_stamp` and `decode_pdf_string`
|
|
//! against the outcomes the TypeScript recorded (`oracles/r4/read.json`), on every `pdf`
|
|
//! input of the oracle and every `decodePdfString` row.
|
|
#![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)]
|
|
|
|
mod common;
|
|
|
|
use common::r4::{latin1_bytes, r4_input, r4_oracle, sha256_hex};
|
|
use faunosteo_core::pdf::read::scan::find_embedded_files;
|
|
use faunosteo_core::pdf::read::stamp::has_archive_stamp;
|
|
use faunosteo_core::pdf::read::strings::decode_pdf_string;
|
|
use faunosteo_core::pdf::read::{EmbeddedFileCandidate, StreamFilter};
|
|
use serde_json::{Value, json};
|
|
|
|
/// The bytes of an oracle input, checked against the row's length and digest. A generator
|
|
/// row names its `<slug>.gen.json` description, which `r4_input` expands from any file name
|
|
/// of that slug (the description itself is stored under the row's name).
|
|
fn input_bytes(row: &Value) -> Vec<u8> {
|
|
let file = row["file"].as_str().unwrap();
|
|
if row["stored"] == "generator" {
|
|
let slug = file.strip_suffix(".gen.json").unwrap();
|
|
return r4_input("read", &format!("{slug}.pdf"));
|
|
}
|
|
let bytes = r4_input("read", file);
|
|
assert_eq!(
|
|
bytes.len() as u64,
|
|
row["length"].as_u64().unwrap(),
|
|
"{file}: length"
|
|
);
|
|
assert_eq!(
|
|
sha256_hex(&bytes),
|
|
row["sha256"].as_str().unwrap(),
|
|
"{file}: digest"
|
|
);
|
|
bytes
|
|
}
|
|
|
|
fn candidate_json(candidate: &EmbeddedFileCandidate) -> Value {
|
|
let filter = match candidate.filter {
|
|
StreamFilter::FlateDecode => json!("FlateDecode"),
|
|
StreamFilter::None => Value::Null,
|
|
};
|
|
json!({
|
|
"name": candidate.name,
|
|
"offset": candidate.offset,
|
|
"length": candidate.length,
|
|
"filter": filter,
|
|
})
|
|
}
|
|
|
|
#[derive(Default)]
|
|
struct Tally {
|
|
inputs: usize,
|
|
candidates: usize,
|
|
stamps: usize,
|
|
failures: Vec<String>,
|
|
}
|
|
|
|
impl Tally {
|
|
fn check(&mut self, file: &str, field: &str, actual: Value, expected: &Value) {
|
|
if actual != *expected {
|
|
self.failures
|
|
.push(format!("{file}: {field}: got {actual}, oracle {expected}"));
|
|
}
|
|
}
|
|
|
|
fn scan(&mut self, row: &Value) {
|
|
let file = row["file"].as_str().unwrap();
|
|
let bytes = input_bytes(row);
|
|
let outcomes = &row["outcomes"];
|
|
self.inputs += 1;
|
|
if let Some(expected) = outcomes.get("findEmbeddedFiles") {
|
|
let found: Vec<Value> = find_embedded_files(&bytes)
|
|
.iter()
|
|
.map(candidate_json)
|
|
.collect();
|
|
self.candidates += found.len();
|
|
self.check(file, "findEmbeddedFiles", Value::Array(found), expected);
|
|
}
|
|
if let Some(expected) = outcomes.get("hasArchiveStamp") {
|
|
self.stamps += 1;
|
|
self.check(
|
|
file,
|
|
"hasArchiveStamp",
|
|
json!(has_archive_stamp(&bytes)),
|
|
expected,
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn every_pdf_input_scans_as_the_typescript_did() {
|
|
let oracle = r4_oracle("read.json");
|
|
assert_eq!(oracle["oracle"], "O7");
|
|
let mut tally = Tally::default();
|
|
let rows = oracle["inputs"].as_array().unwrap();
|
|
for row in rows.iter().filter(|row| row["kind"] == "pdf") {
|
|
tally.scan(row);
|
|
}
|
|
assert!(tally.failures.is_empty(), "{}", tally.failures.join("\n"));
|
|
assert_eq!(tally.inputs, 144, "pdf inputs");
|
|
assert!(tally.candidates > 0 && tally.stamps == tally.inputs);
|
|
println!(
|
|
"O7 scan: {} pdf inputs of {}, {} candidates, {} stamp outcomes",
|
|
tally.inputs,
|
|
rows.len(),
|
|
tally.candidates,
|
|
tally.stamps
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn decode_pdf_string_rows_match() {
|
|
let oracle = r4_oracle("read.json");
|
|
let rows = oracle["decodePdfString"].as_array().unwrap();
|
|
let failures: Vec<String> = rows
|
|
.iter()
|
|
.filter_map(|row| {
|
|
let token = row["token"].as_str().unwrap();
|
|
let actual = json!(decode_pdf_string(&latin1_bytes(token)));
|
|
(actual != row["result"]).then(|| {
|
|
format!(
|
|
"decodePdfString({token:?}): got {actual}, oracle {}",
|
|
row["result"]
|
|
)
|
|
})
|
|
})
|
|
.collect();
|
|
assert!(failures.is_empty(), "{}", failures.join("\n"));
|
|
assert_eq!(rows.len(), 19, "decodePdfString rows");
|
|
}
|