//! O9: `inflate_flate` against the frozen `DecompressionStream('deflate')` verdicts of //! `inflateFlate` (`tests/oracles/r4/inflate.json`, its browser columns //! `inflate.browsers.json`) and the `flate` inputs of O7 (`read.json`). #![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)] mod common; use common::r4::{r4_input, r4_oracle, sha256_hex}; use faunosteo_core::pdf::read::inflate::inflate_flate; use serde_json::{Value, json}; /// The oracle form of an outcome: null, or `{ length, sha256 }` of the output. fn verdict(output: Option>) -> Value { output.map_or( Value::Null, |bytes| json!({ "length": bytes.len(), "sha256": sha256_hex(&bytes) }), ) } /// The bytes of an O9 input, checked against the length and digest the oracle recorded. fn o9_input(oracle: &Value, file: &str) -> Vec { let bytes = r4_input("inflate", file); let recorded = oracle["inputs"] .as_array() .unwrap() .iter() .find(|input| input["file"] == file) .unwrap_or_else(|| panic!("{file}: not listed in inputs")); assert_eq!(recorded["length"], json!(bytes.len()), "{file}: length"); assert_eq!( recorded["sha256"], json!(sha256_hex(&bytes)), "{file}: digest" ); bytes } fn mismatch(file: &str, case: &str, limit: u64, expected: &Value, got: &Value) -> String { format!("{file} ({case}), limit {limit}: node {expected}, rust {got}") } #[test] fn every_o9_row_matches_node() { let oracle = r4_oracle("inflate.json"); let rows = oracle["rows"].as_array().unwrap(); assert_eq!(rows.len(), 26, "O9 row count"); let mut failures = Vec::new(); for row in rows { let (file, case) = (row["file"].as_str().unwrap(), row["case"].as_str().unwrap()); let limit = row["limit"].as_u64().unwrap(); let bytes = o9_input(&oracle, file); let got = verdict(inflate_flate(&bytes, limit as usize)); let expected = &row["verdicts"]["node"]; if got != *expected { failures.push(mismatch(file, case, limit, expected, &got)); } } assert!( failures.is_empty(), "O9 rows differ:\n{}", failures.join("\n") ); } #[test] fn every_o7_flate_input_matches_node() { let oracle = r4_oracle("read.json"); let inputs: Vec<&Value> = oracle["inputs"] .as_array() .unwrap() .iter() .filter(|input| input["kind"] == "flate") .collect(); assert_eq!(inputs.len(), 11, "O7 flate input count"); let mut checked = 0; let mut failures = Vec::new(); for input in inputs { let file = input["file"].as_str().unwrap(); let bytes = r4_input("read", file); for outcome in input["outcomes"]["inflateFlate"].as_array().unwrap() { let limit = outcome["limit"].as_u64().unwrap(); let got = verdict(inflate_flate(&bytes, limit as usize)); if got != outcome["verdict"] { failures.push(mismatch(file, "O7", limit, &outcome["verdict"], &got)); } checked += 1; } } assert_eq!(checked, 12, "O7 inflateFlate verdict count"); assert!( failures.is_empty(), "O7 flate rows differ:\n{}", failures.join("\n") ); } #[test] fn every_browser_agrees_with_node_on_every_o9_row() { let browsers = r4_oracle("inflate.browsers.json"); let node_rows = r4_oracle("inflate.json")["rows"].clone(); assert_eq!( browsers["disagreements"], json!([]), "recorded disagreements" ); let rows = browsers["rows"].as_array().unwrap(); assert_eq!( rows.len(), node_rows.as_array().unwrap().len(), "browser row count" ); for (row, node_row) in rows.iter().zip(node_rows.as_array().unwrap()) { let label = format!("{} ({})", row["file"], row["case"]); assert_eq!(row["file"], node_row["file"], "{label}: file"); assert_eq!(row["limit"], node_row["limit"], "{label}: limit"); let node = &node_row["verdicts"]["node"]; assert_eq!(row["verdicts"]["node"], *node, "{label}: node column"); for engine in ["chromium", "firefox", "webkit"] { assert_eq!(row["verdicts"][engine], *node, "{label}: {engine} vs node"); } } } /// Not an oracle row: the zlib rule the non-wrapping output buffer exists for. A match whose /// distance reaches before the first output byte is "invalid distance too far back" for zlib /// (checked with the system zlib); the Adler-32 is the one of the three zero bytes a decoder /// reading a zeroed window would produce, so only the distance check can refuse it. #[test] fn a_match_before_the_output_start_is_refused() { let before_start = [0x78, 0x01, 0x03, 0x02, 0x00, 0x00, 0x03, 0x00, 0x01]; assert_eq!(inflate_flate(&before_start, 1024), None); let control = [0x78, 0x01, 0x4b, 0x04, 0x02, 0x00, 0x03, 0xce, 0x01, 0x85]; assert_eq!(inflate_flate(&control, 1024).as_deref(), Some(&b"aaaa"[..])); }