463 lines
15 KiB
Rust
463 lines
15 KiB
Rust
//! Scanner snapshots and basic show commands versus independent real INITEX.
|
|
#[path = "support/font_world.rs"]
|
|
mod font_world;
|
|
use font_world::FontFiles;
|
|
use std::{
|
|
collections::BTreeMap,
|
|
fs,
|
|
path::PathBuf,
|
|
process::Command,
|
|
sync::atomic::{AtomicU64, Ordering},
|
|
};
|
|
use texrra_tex_core::execution::*;
|
|
const HEADER: &str = "\\catcode123=1\\catcode125=2\\catcode35=6\\catcode94=7\\catcode0=12\\catcode127=12\\errorcontextlines=100 ";
|
|
#[derive(Default)]
|
|
struct Host {
|
|
files: BTreeMap<String, String>,
|
|
diagnostics: Vec<(WriteDestination, String)>,
|
|
}
|
|
impl SourceProvider for Host {
|
|
font_world::font_resources!();
|
|
fn read(&mut self, name: &str, _: &str) -> Result<SourceFile, String> {
|
|
self.files
|
|
.get(name)
|
|
.map(|s| SourceFile {
|
|
name: name.into(),
|
|
data: s.as_bytes().to_vec(),
|
|
})
|
|
.ok_or_else(|| format!("missing {name}"))
|
|
}
|
|
fn observe_diagnostic(&mut self, d: WriteDestination, t: &str) -> Result<(), String> {
|
|
self.diagnostics.push((d, t.into()));
|
|
Ok(())
|
|
}
|
|
}
|
|
fn execute(s: &str, h: &mut impl SourceProvider) -> Result<Execution, Error> {
|
|
Executor::new(State::initex().with_etex(), Limits::default()).run(
|
|
SourceFile {
|
|
name: "probe.tex".into(),
|
|
data: s.as_bytes().to_vec(),
|
|
},
|
|
h,
|
|
)
|
|
}
|
|
fn directory() -> PathBuf {
|
|
static ID: AtomicU64 = AtomicU64::new(0);
|
|
let dir = std::env::temp_dir().join(format!(
|
|
"texrra-context-{}-{}",
|
|
std::process::id(),
|
|
ID.fetch_add(1, Ordering::Relaxed)
|
|
));
|
|
fs::create_dir_all(&dir).unwrap();
|
|
dir
|
|
}
|
|
fn reference(s: &str, files: &BTreeMap<String, String>, fatal: bool) -> String {
|
|
let dir = directory();
|
|
fs::write(dir.join("probe.tex"), s).unwrap();
|
|
for (name, text) in files {
|
|
fs::write(dir.join(name), text).unwrap();
|
|
}
|
|
let mut c = Command::new("pdftex");
|
|
c.args(["-ini", "-etex", "-interaction=nonstopmode"]);
|
|
if fatal {
|
|
c.arg("-halt-on-error");
|
|
}
|
|
let o = c
|
|
.arg("probe.tex")
|
|
.current_dir(&dir)
|
|
.output()
|
|
.expect("real INITEX oracle required");
|
|
assert_eq!(
|
|
o.status.code(),
|
|
Some(1),
|
|
"{}",
|
|
String::from_utf8_lossy(&o.stdout)
|
|
);
|
|
let text = String::from_utf8(o.stdout).unwrap();
|
|
fs::remove_dir_all(dir).unwrap();
|
|
text
|
|
}
|
|
fn fatal(s: &str, files: BTreeMap<String, String>) -> Error {
|
|
let e = execute(
|
|
s,
|
|
&mut Host {
|
|
files: files.clone(),
|
|
..Host::default()
|
|
},
|
|
)
|
|
.unwrap_err();
|
|
assert!(
|
|
e.message.contains("undefined native control sequence"),
|
|
"{}",
|
|
e.message
|
|
);
|
|
let r = reference(s, &files, true);
|
|
let context = r
|
|
.split_once("Undefined control sequence.\n")
|
|
.expect(&r)
|
|
.1
|
|
.split_once("No pages of output.")
|
|
.expect(&r)
|
|
.0;
|
|
assert_eq!(e.context.text, context, "source:\n{s}\nreference:\n{r}");
|
|
let copy: Error = serde_json::from_slice(&serde_json::to_vec(&e).unwrap()).unwrap();
|
|
assert_eq!(e, copy);
|
|
e
|
|
}
|
|
#[test]
|
|
fn actual_context_limits_macro_arguments_aliases_active_tail_and_sources_match_initex() {
|
|
let body = r"\def\A{\B\relax}\def\B{\C\relax}\def\C{\bad\relax}\A\end";
|
|
for n in [-2, -1, 0, 1, 2, 100] {
|
|
fatal(
|
|
&format!("{HEADER}\\errorcontextlines={n}\n{body}\n"),
|
|
BTreeMap::new(),
|
|
);
|
|
}
|
|
for body in [
|
|
r"\def\A{\B}\def\B{\bad}\A\end",
|
|
r"\def\A#1{#1\relax}\A{\bad x}\end",
|
|
r"\def\A#1{#1\relax}\def\B{\A{\bad x}\relax}\B\end",
|
|
r"\def\A#1x{#1\relax}\let\alias\A\alias\bad x\end",
|
|
r"\catcode33=13\def!{\bad\relax}!\end",
|
|
r"\def\A{\bad x}\message{\A z}\end",
|
|
r"\def\A{\bad x}\edef\B{\A z}\end",
|
|
r"\def\A{\bad x}\expanded{\A z}\end",
|
|
r"\def\A{\bad x}\count0=\A\end",
|
|
r"\def\A{\bad x}\def\B#1{\message{#1z}}\B{\A}\end",
|
|
r"\bad\end",
|
|
r"\catcode33=6\def\A!1{!1\relax}\A{\bad x}\end",
|
|
r"\expanded{\noexpand\bad}\end",
|
|
r"\unexpanded{\bad}\end",
|
|
r"\toks0={\bad}\the\toks0\end",
|
|
r"\expandafter\bad\relax\end",
|
|
r"\immediate\write16{\bad}\end",
|
|
] {
|
|
fatal(&format!("{HEADER}\n{body}\n"), BTreeMap::new());
|
|
}
|
|
let nested = fatal(
|
|
&format!("{HEADER}\n\\def\\A{{\\input child }}\\A\\end\n"),
|
|
BTreeMap::from([("child".into(), "\\def\\B{\\bad x}\n\\B\\endinput\n".into())]),
|
|
);
|
|
assert!(
|
|
nested
|
|
.context
|
|
.frames
|
|
.iter()
|
|
.any(|f| matches!(&f.kind, ContextKind::Source { name, .. } if name == "child"))
|
|
);
|
|
assert!(
|
|
nested
|
|
.context
|
|
.reads
|
|
.contains(&Dependency::Parameter(Parameter::Errorcontextlines))
|
|
);
|
|
}
|
|
#[test]
|
|
fn actual_long_line_crops_and_transformed_byte_positions_match_initex() {
|
|
for before in [0, 20, 70, 120] {
|
|
for after in [0, 10, 80, 160] {
|
|
let prefix = "\\relax ".repeat(before);
|
|
let suffix = "\\relax ".repeat(after);
|
|
fatal(
|
|
&format!("{HEADER}\n{prefix}\\bad {suffix}\\end\n"),
|
|
BTreeMap::new(),
|
|
);
|
|
fatal(
|
|
&format!("{HEADER}\n\\def\\A{{{prefix}\\bad {suffix}}}\\A\\end\n"),
|
|
BTreeMap::new(),
|
|
);
|
|
}
|
|
}
|
|
for body in [
|
|
r"\^^62ad ^^a \end",
|
|
r"\def\A{\^^62ad ^^00 ^^7f ^^c8}\A\end",
|
|
r"\bad^^00 \end",
|
|
r"\bad^^7f \end",
|
|
r"\def\A{\bad}\^^41\end",
|
|
r"\def\A{\bad}^^5cA\end",
|
|
r"\def\A{\bad}\^^5e^41\end",
|
|
r"\def\A{\bad}^^5e^5cA\end",
|
|
r"\endlinechar=-1 \bad",
|
|
r"\endlinechar=65 \bad",
|
|
] {
|
|
fatal(&format!("{HEADER}\n{body}\n"), BTreeMap::new());
|
|
}
|
|
}
|
|
#[test]
|
|
fn shows_use_raw_meanings_internal_quantities_toks_and_actual_context() {
|
|
for body in [
|
|
r"\show a\show\relax\show\count\show\unknown\end",
|
|
r#"\show\-\show\meaning\show\eTeXversion\show\nullfont\chardef\x=65\show\x\mathchardef\m="1234\show\m\end"#,
|
|
r"\long\def\A#1x{hello #1}\show\A\let\alias\A\show\alias\end",
|
|
r"\catcode33=6\def\A#1!2{#1!2}\show\A\end",
|
|
r"\protected\long\def\A#1{#1##}\show\A\end",
|
|
r"\countdef\c=0\count0=-7\dimen0=1.25pt\skip0=2pt plus3fil minus1pt\muskip0=3mu\toks0={\relax ## x}\show\c\showthe\c\showthe\dimen0\showthe\skip0\showthe\muskip0\showthe\toks0\end",
|
|
r"\count0=13\showthe\count0\relax\end",
|
|
r"\def\A#1{\show#1\relax}\A{\unknown}\end",
|
|
r"\setbox0=\hbox{}\showthe\wd0\showthe\ht0\showthe\dp0\showthe\prevdepth\end",
|
|
r"\showthe\errorcontextlines\showthe\currentgrouplevel\showthe\catcode65\end",
|
|
] {
|
|
let s = format!("{HEADER}\n{body}\n");
|
|
let mut host = Host::default();
|
|
let e = execute(&s, &mut host).unwrap();
|
|
let r = reference(&s, &BTreeMap::new(), false);
|
|
let mut count = 0;
|
|
for event in &e.events {
|
|
if let Event::Show(report) = event {
|
|
let exact = report.output.clone();
|
|
assert!(r.contains(&exact), "native:\n{exact}\nreference:\n{r}");
|
|
count += 1;
|
|
}
|
|
}
|
|
assert!(count > 0);
|
|
assert_eq!(host.diagnostics.len(), count * 2);
|
|
let copy: Execution = serde_json::from_slice(&serde_json::to_vec(&e).unwrap()).unwrap();
|
|
assert_eq!(copy, e);
|
|
}
|
|
}
|
|
#[test]
|
|
fn errorcontextlines_has_scope_arithmetic_the_and_real_read_dependencies() {
|
|
let s = format!(
|
|
r"{HEADER}\message{{\the\errorcontextlines}} {{\errorcontextlines=2\advance\errorcontextlines by3\multiply\errorcontextlines by2\divide\errorcontextlines by3\message{{\the\errorcontextlines}}}}\globaldefs=1 {{\errorcontextlines=-2}}\globaldefs=0\message{{\the\errorcontextlines}}\end"
|
|
);
|
|
let e = execute(&s, &mut Host::default()).unwrap();
|
|
let messages: Vec<_> = e
|
|
.events
|
|
.iter()
|
|
.filter_map(|e| {
|
|
if let Event::Message { text, .. } = e {
|
|
Some(text.as_str())
|
|
} else {
|
|
None
|
|
}
|
|
})
|
|
.collect();
|
|
assert_eq!(messages, ["100", "3", "-2"]);
|
|
assert!(
|
|
e.reads
|
|
.contains(&Dependency::Parameter(Parameter::Errorcontextlines))
|
|
);
|
|
assert_eq!(
|
|
State::initex().parameter(Parameter::Errorcontextlines),
|
|
ParameterValue::Integer(0)
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn context_printer_escape_newline_and_large_names_match_actual_initex() {
|
|
for escape in [-1, 92, 64, 200] {
|
|
for newline in [-1, 0, 33, 65, 92] {
|
|
fatal(
|
|
&format!(
|
|
"{HEADER}\\escapechar={escape}\\newlinechar={newline}\n\\def\\A{{\\bad x}}\\A\\end\n"
|
|
),
|
|
BTreeMap::new(),
|
|
);
|
|
}
|
|
}
|
|
let name = "LongMacro".repeat(10);
|
|
fatal(
|
|
&format!("{HEADER}\n\\def\\{name}{{\\bad x}}\\{name}\\end\n"),
|
|
BTreeMap::new(),
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn shows_print_real_bytes_newline_escape_and_wrap_like_initex() {
|
|
for escape in [-1, 92, 64, 200] {
|
|
for newline in [-1, 0, 33, 65, 92] {
|
|
let s = format!(
|
|
"{HEADER}\\escapechar={escape}\\newlinechar={newline}\n\\long\\def\\A#1{{#1^^00^^7f^^c8}}\\show\\A\\showthe\\errorcontextlines\\end\n"
|
|
);
|
|
let e = execute(&s, &mut Host::default()).unwrap();
|
|
let r = reference(&s, &BTreeMap::new(), false);
|
|
for event in e.events {
|
|
if let Event::Show(report) = event {
|
|
let exact = report.output.clone();
|
|
assert!(
|
|
r.contains(&exact),
|
|
"source:\n{s}\nnative:\n{exact}\nreference:\n{r}"
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
let s = format!("{HEADER}\n\\def\\A{{{}}}\\show\\A\\end\n", "x".repeat(210));
|
|
let e = execute(&s, &mut Host::default()).unwrap();
|
|
let r = reference(&s, &BTreeMap::new(), false);
|
|
for event in e.events {
|
|
if let Event::Show(report) = event {
|
|
assert!(r.contains(&report.output), "{}\n{r}", report.text);
|
|
}
|
|
}
|
|
}
|
|
#[test]
|
|
fn snapshots_shared_macros_observer_failures_and_execution_capacity_stay_bounded() {
|
|
struct Broken {
|
|
fatal: bool,
|
|
}
|
|
impl SourceProvider for Broken {
|
|
font_world::font_resources!();
|
|
fn read(&mut self, _: &str, _: &str) -> Result<SourceFile, String> {
|
|
Err("no source".into())
|
|
}
|
|
fn observe_diagnostic(&mut self, _: WriteDestination, _: &str) -> Result<(), String> {
|
|
Err("diagnostic sink full".into())
|
|
}
|
|
fn observe_error(&mut self, _: &Error) -> Result<(), String> {
|
|
if self.fatal {
|
|
Err("fatal sink full".into())
|
|
} else {
|
|
Ok(())
|
|
}
|
|
}
|
|
}
|
|
let e = execute(
|
|
&format!("{HEADER}\\show\\relax"),
|
|
&mut Broken { fatal: false },
|
|
)
|
|
.unwrap_err();
|
|
assert!(e.message.contains("diagnostic sink full"));
|
|
let e = execute(&format!("{HEADER}\\bad"), &mut Broken { fatal: true }).unwrap_err();
|
|
assert!(
|
|
e.message.contains("undefined native control sequence")
|
|
&& e.message.contains("fatal sink full")
|
|
);
|
|
let s = format!(
|
|
"{HEADER}\\def\\A#1{{\\A{{#1}}\\relax}}\\A{{{}}}",
|
|
"x".repeat(100)
|
|
);
|
|
let e = Executor::new(
|
|
State::initex().with_etex(),
|
|
Limits {
|
|
steps: 100_000,
|
|
tokens: 2000,
|
|
input_depth: 40,
|
|
},
|
|
)
|
|
.run(
|
|
SourceFile {
|
|
name: "deep.tex".into(),
|
|
data: s.as_bytes().to_vec(),
|
|
},
|
|
&mut FontFiles,
|
|
)
|
|
.unwrap_err();
|
|
assert!(e.message.contains("input stack limit"), "{}", e.message);
|
|
assert!(e.context.frames.len() <= 40);
|
|
assert!(serde_json::to_vec(&e).unwrap().len() < 20_000);
|
|
let e = Executor::new(
|
|
State::initex().with_etex(),
|
|
Limits {
|
|
steps: 3000,
|
|
tokens: 1000,
|
|
input_depth: 10,
|
|
},
|
|
)
|
|
.run(
|
|
SourceFile {
|
|
name: "tail.tex".into(),
|
|
data: (format!("{HEADER}\\def\\A{{\\A}}\\A")).as_bytes().to_vec(),
|
|
},
|
|
&mut FontFiles,
|
|
)
|
|
.unwrap_err();
|
|
assert!(e.message.contains("step limit"), "{}", e.message);
|
|
let s = format!("{HEADER}\\def\\A{{{}}}\\let\\B\\A\\end", "x".repeat(1500));
|
|
let bootstrap = execute(&s, &mut FontFiles).unwrap();
|
|
let Meaning::Macro(a) = bootstrap.state.meaning(&CommandName::Control("A".into())) else {
|
|
panic!()
|
|
};
|
|
let Meaning::Macro(b) = bootstrap.state.meaning(&CommandName::Control("B".into())) else {
|
|
panic!()
|
|
};
|
|
assert!(std::sync::Arc::ptr_eq(&a, &b));
|
|
let e = Executor::from_execution(
|
|
bootstrap,
|
|
Limits {
|
|
steps: 100_000,
|
|
tokens: 2000,
|
|
input_depth: 40,
|
|
},
|
|
)
|
|
.run(
|
|
SourceFile {
|
|
name: "show.tex".into(),
|
|
data: (r"\show\A").as_bytes().to_vec(),
|
|
},
|
|
&mut FontFiles,
|
|
)
|
|
.unwrap();
|
|
assert!(e.events.iter().any(|e| matches!(e, Event::Show(_))));
|
|
let bootstrap = execute(&s, &mut FontFiles).unwrap();
|
|
let e = Executor::from_execution(
|
|
bootstrap,
|
|
Limits {
|
|
steps: 100_000,
|
|
tokens: 1000,
|
|
input_depth: 40,
|
|
},
|
|
)
|
|
.run(
|
|
SourceFile {
|
|
name: "show.tex".into(),
|
|
data: (r"\show\A").as_bytes().to_vec(),
|
|
},
|
|
&mut FontFiles,
|
|
)
|
|
.unwrap_err();
|
|
assert!(
|
|
e.message.contains("diagnostic output limit"),
|
|
"{}",
|
|
e.message
|
|
);
|
|
let bootstrap = execute(
|
|
&format!("{HEADER}\\def\\A{{{}}}\\end", "x".repeat(800)),
|
|
&mut FontFiles,
|
|
)
|
|
.unwrap();
|
|
let mut host = Host::default();
|
|
let error = Executor::from_execution(
|
|
bootstrap,
|
|
Limits {
|
|
tokens: 1600,
|
|
..Limits::default()
|
|
},
|
|
)
|
|
.run(
|
|
SourceFile {
|
|
name: "repeated-shows.tex".into(),
|
|
data: (r"\show\A\show\A\show\A\end").as_bytes().to_vec(),
|
|
},
|
|
&mut host,
|
|
)
|
|
.unwrap_err();
|
|
assert!(error.message.contains("diagnostic output limit"));
|
|
assert_eq!(
|
|
host.diagnostics.len(),
|
|
2,
|
|
"per-job capacity must stop later output"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn show_body_wrapping_at_the_exact_column_boundary_preserves_real_context_spacing() {
|
|
for length in 70..=82 {
|
|
let s = format!(
|
|
"{HEADER}\n\\def\\A{{{}}}\\show\\A\\end\n",
|
|
"x".repeat(length)
|
|
);
|
|
let e = execute(&s, &mut Host::default()).unwrap();
|
|
let r = reference(&s, &BTreeMap::new(), false);
|
|
for event in e.events {
|
|
if let Event::Show(report) = event {
|
|
assert!(
|
|
r.contains(&report.output),
|
|
"length={length}\nnative:\n{}\nreference:\n{r}",
|
|
report.output
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|