#![cfg(test)]
mod common;
use einvoice_xpath_eval::{DEFAULT_MAX_DEPTH, LimitExceeded};
use einvoice_xslt_exec::{
DEFAULT_MAX_NESTING, DEFAULT_MAX_STACK, DEFAULT_MAX_TAIL_CALLS, DocumentResolver, Executable,
TransformError, TransformOptions, max_nesting_for_depth,
};
const STACK: usize = 2 << 20;
fn on_a_default_stack<T: Send + 'static>(f: impl FnOnce() -> T + Send + 'static) -> T {
std::thread::Builder::new()
.stack_size(STACK)
.spawn(f)
.unwrap()
.join()
.unwrap()
}
const HEAD: &str = r#"<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:f="urn:f">
<xsl:output omit-xml-declaration="yes"/>"#;
const NS: &str = r#" xmlns:f="urn:f" xmlns:xs="http://www.w3.org/2001/XMLSchema""#;
fn r(text: &str) -> String {
format!("<r{NS}>{text}</r>")
}
fn prepare(body: &str) -> Executable {
let xslt = format!("{HEAD}\n{body}\n</xsl:stylesheet>");
Executable::new(&einvoice_xslt::load(&xslt).unwrap_or_else(|e| panic!("{e}")))
.unwrap_or_else(|e| panic!("{e}"))
}
fn nested(depth: usize, inner: &str) -> String {
format!("{}{inner}{}", "<x>".repeat(depth), "</x>".repeat(depth))
}
fn position(text: &str, offset: usize) -> (u32, u32) {
let before = &text[..offset];
let line = before.matches('\n').count() + 1;
let column = before[before.rfind('\n').map_or(0, |i| i + 1)..]
.chars()
.count()
+ 1;
(u32::try_from(line).unwrap(), u32::try_from(column).unwrap())
}
fn deep_invoice(depth: usize, at: usize) -> (String, (u32, u32)) {
let root = common::workspace_root();
let text = common::read(&common::join(
&root,
"artefacts/cen-en16931-ubl-1.3.17/ubl/examples/ubl-tc434-example1.xml",
))
.unwrap();
let start = text.find("<Invoice").unwrap();
let end = start + text[start..].find('>').unwrap() + 1;
let n = depth - 4;
let head = format!(
"{}<ext:UBLExtensions xmlns:ext=\"urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2\"><ext:UBLExtension><ext:ExtensionContent><y:x xmlns:y=\"urn:y\">",
&text[..end]
);
let document = format!(
"{head}{}{}</ext:ExtensionContent></ext:UBLExtension></ext:UBLExtensions>{}",
"<y:x>".repeat(n - 1),
"</y:x>".repeat(n),
&text[end..]
);
let offset = if at <= 4 {
0
} else {
head.len() - "<y:x xmlns:y=\"urn:y\">".len()
+ if at == 5 {
0
} else {
"<y:x xmlns:y=\"urn:y\">".len() + (at - 6) * "<y:x>".len()
}
};
let at = position(&document, offset);
(document, at)
}
fn limit<T: std::fmt::Debug>(result: Result<T, TransformError>) -> LimitExceeded {
let error = result.unwrap_err();
assert_eq!(error.code(), "LIMIT", "{error}");
error.limit().unwrap_or_else(|| panic!("no limit: {error}"))
}
#[test]
fn the_defaults() {
assert_eq!(DEFAULT_MAX_DEPTH, 256);
assert_eq!(DEFAULT_MAX_NESTING, 2 * 256 + 64);
assert_eq!(
max_nesting_for_depth(DEFAULT_MAX_DEPTH),
DEFAULT_MAX_NESTING
);
assert_eq!(max_nesting_for_depth(u32::MAX), u32::MAX);
let options = TransformOptions::default();
assert_eq!(
(options.max_depth, options.max_nesting),
(DEFAULT_MAX_DEPTH, DEFAULT_MAX_NESTING)
);
}
#[test]
fn the_cen_stylesheet_at_the_maximum_depth() {
on_a_default_stack(|| {
let root = common::workspace_root();
let executable = common::executable(&root).unwrap();
let (document, _) = deep_invoice(256, 256);
let svrl = executable
.transform(document.as_str(), None)
.unwrap()
.output;
let canonical = einvoice_svrl::canonicalize(&svrl, &|_| false).unwrap();
assert!(!canonical.contains("flag=\"fatal\""), "{canonical}");
assert!(canonical.contains("fired-rule"), "{canonical}");
for depth in [257, 1500] {
let (document, at) = deep_invoice(depth, 257);
assert_eq!(
limit(executable.transform(document.as_str(), None)),
LimitExceeded::Depth {
max: 256,
line: at.0,
column: at.1
}
);
}
let (document, _) = deep_invoice(300, 300);
let options = TransformOptions {
max_depth: 300,
max_nesting: max_nesting_for_depth(300),
..TransformOptions::default()
};
executable
.transform_with(document.as_str(), None, &options)
.unwrap();
});
}
#[test]
fn the_peppol_stylesheets_at_the_maximum_depth() {
let root = common::workspace_root();
let Some(corpus) = common::peppol::corpus(&root).unwrap_or_else(|e| panic!("{e}")) else {
assert!(
!common::peppol::required(),
"no Peppol corpus; {} is set",
common::peppol::REQUIRED_VAR
);
eprintln!("SKIPPED: no Peppol corpus in {}", common::peppol::BASE);
return;
};
for stylesheet in corpus.stylesheets {
on_a_default_stack(move || {
let executable = common::prepare(&stylesheet.xslt).unwrap();
let (document, _) = deep_invoice(256, 256);
let svrl = executable
.transform(document.as_str(), None)
.unwrap()
.output;
einvoice_svrl::canonicalize(&svrl, &|_| false).unwrap();
let (document, at) = deep_invoice(257, 257);
assert_eq!(
limit(executable.transform(document.as_str(), None)),
LimitExceeded::Depth {
max: 256,
line: at.0,
column: at.1
},
"{}",
stylesheet.name
);
});
}
}
#[test]
fn unbounded_recursion_is_a_limit() {
let stylesheets = [
r#"<xsl:template match="/"><xsl:apply-templates select="*"/></xsl:template>
<xsl:template match="*"><r><xsl:apply-templates select="."/></r></xsl:template>"#,
r#"<xsl:template match="/"><xsl:call-template name="t"/></xsl:template>
<xsl:template name="t"><xsl:param name="n" select="0"/><xsl:call-template name="t"><xsl:with-param name="n" select="$n + 1"/></xsl:call-template></xsl:template>"#,
r#"<xsl:function name="f:r" as="xs:integer"><xsl:param name="n" as="xs:integer"/><xsl:sequence select="if ($n lt 0) then 0 else 1 + f:r($n + 1)"/></xsl:function>
<xsl:template match="/"><r><xsl:value-of select="f:r(0)"/></r></xsl:template>"#,
r#"<xsl:function name="f:r"><xsl:param name="n"/><xsl:choose><xsl:when test="$n lt 0"><xsl:sequence select="0"/></xsl:when><xsl:otherwise><xsl:for-each select="1"><xsl:variable name="m" select="f:r($n + 1)"/><xsl:sequence select="$m"/></xsl:for-each></xsl:otherwise></xsl:choose></xsl:function>
<xsl:template match="/"><r><xsl:value-of select="f:r(0)"/></r></xsl:template>"#,
r#"<xsl:template match="node()"><xsl:variable name="v"><xsl:apply-templates select="."/></xsl:variable><xsl:value-of select="$v"/></xsl:template>"#,
r#"<xsl:template match="node()"><xsl:value-of><xsl:apply-templates select="."/></xsl:value-of></xsl:template>"#,
r#"<xsl:template match="/"><r><xsl:apply-templates select="*"/></r></xsl:template>
<xsl:template match="*"><xsl:attribute name="a"><xsl:apply-templates select="."/></xsl:attribute></xsl:template>"#,
r#"<xsl:template match="node()"><xsl:comment><xsl:apply-templates select="."/></xsl:comment></xsl:template>"#,
r#"<xsl:template match="/"><xsl:next-match/></xsl:template>
<xsl:template match="node()" priority="-1"><xsl:apply-templates select="/"/></xsl:template>"#,
r#"<xsl:function name="f:r" as="xs:boolean"><xsl:param name="n" as="xs:integer"/><xsl:sequence select="f:r($n + 1)"/></xsl:function>
<xsl:template match="x[f:r(0)]"><m/></xsl:template>"#,
];
for (i, body) in stylesheets.into_iter().enumerate() {
let outcome = on_a_default_stack(move || {
let executable = prepare(body);
executable.transform("<x><x/></x>", None).map(|t| t.output)
});
let error = outcome.unwrap_err();
assert_eq!(error.code(), "LIMIT", "{body}: {error}");
let message = error.to_string();
match error.limit() {
Some(LimitExceeded::Nesting { max: 576 }) => assert!(
message.contains(
"LIMIT: templates and functions invoked one inside another more than 576 deep"
),
"{message}"
),
Some(LimitExceeded::Stack { max }) if max == DEFAULT_MAX_STACK && i != 1 => assert!(
message.contains("use more than 1572864 bytes of stack"),
"{message}"
),
Some(LimitExceeded::TailCalls { max }) if max == DEFAULT_MAX_TAIL_CALLS && i == 1 => {
assert!(
message.contains(
"ending with a call of another template more than 65536 times in a row"
),
"{message}"
);
}
other => panic!("{body}: {other:?}"),
}
}
}
#[test]
fn bounded_recursion_and_the_configurable_limits() {
let template = prepare(
r#"<xsl:template match="/"><r><xsl:call-template name="sum"><xsl:with-param name="n" select="xs:integer(/n)"/></xsl:call-template></r></xsl:template>
<xsl:template name="sum"><xsl:param name="n"/><xsl:param name="total" select="0"/><xsl:choose><xsl:when test="$n eq 0 or $total lt 0"><xsl:value-of select="$total"/></xsl:when><xsl:otherwise><xsl:call-template name="sum"><xsl:with-param name="n" select="$n - 1"/><xsl:with-param name="total" select="$total + $n"/></xsl:call-template><xsl:sequence select="()"/></xsl:otherwise></xsl:choose></xsl:template>"#,
);
let tail = prepare(
r#"<xsl:template match="/"><r><xsl:call-template name="sum"><xsl:with-param name="n" select="xs:integer(/n)"/></xsl:call-template></r></xsl:template>
<xsl:template name="sum"><xsl:param name="n"/><xsl:param name="total" select="0"/><xsl:choose><xsl:when test="$n eq 0 or $total lt 0"><xsl:value-of select="$total"/></xsl:when><xsl:otherwise><xsl:call-template name="sum"><xsl:with-param name="n" select="$n - 1"/><xsl:with-param name="total" select="$total + $n"/></xsl:call-template></xsl:otherwise></xsl:choose></xsl:template>"#,
);
let function = prepare(
r#"<xsl:function name="f:sum" as="xs:integer"><xsl:param name="n" as="xs:integer"/><xsl:sequence select="if ($n eq 0) then 0 else $n + f:sum($n - 1)"/></xsl:function>
<xsl:template match="/"><r><xsl:value-of select="f:sum(xs:integer(/n))"/></r></xsl:template>"#,
);
on_a_default_stack(move || {
let run = |e: &Executable, n: u32, options: &TransformOptions<'_>| {
e.transform_with(format!("<n>{n}</n>").as_str(), None, options)
.map(|t| t.output)
};
let defaults = TransformOptions::default();
assert_eq!(run(&template, 550, &defaults).unwrap(), r("151525"));
assert_eq!(run(&function, 40, &defaults).unwrap(), r("820"));
let nesting = TransformOptions {
max_nesting: 10,
..TransformOptions::default()
};
for e in [&template, &function] {
assert_eq!(run(e, 8, &nesting).unwrap(), r("36"));
assert_eq!(
limit(run(e, 9, &nesting)),
LimitExceeded::Nesting { max: 10 }
);
}
let stack = TransformOptions {
max_stack: 64 << 10,
..TransformOptions::default()
};
assert_eq!(run(&function, 0, &stack).unwrap(), r("0"));
for e in [&template, &function] {
assert_eq!(
limit(run(e, 200, &stack)),
LimitExceeded::Stack { max: 64 << 10 }
);
}
assert_eq!(run(&tail, 60_000, &defaults).unwrap(), r("1800030000"));
assert_eq!(run(&tail, 200, &nesting).unwrap(), r("20100"));
assert_eq!(run(&tail, 200, &stack).unwrap(), r("20100"));
let tail_calls = TransformOptions {
max_tail_calls: 8,
..TransformOptions::default()
};
assert_eq!(run(&tail, 8, &tail_calls).unwrap(), r("36"));
assert_eq!(
limit(run(&tail, 9, &tail_calls)),
LimitExceeded::TailCalls { max: 8 }
);
});
}
fn least_stack(executable: &Executable, input: &str) -> u32 {
let runs = |max_stack: u32| {
let options = TransformOptions {
max_depth: u32::MAX,
max_nesting: u32::MAX,
max_stack,
..TransformOptions::default()
};
match executable.transform_with(input, None, &options) {
Ok(_) => true,
Err(e) if e.limit() == Some(LimitExceeded::Stack { max: max_stack }) => false,
Err(e) => panic!("{e}"),
}
};
let (mut low, mut high) = (0, DEFAULT_MAX_STACK);
assert!(runs(high), "{input:.80}: more than the default stack");
while high - low > 16 {
let middle = low + (high - low) / 2;
if runs(middle) {
high = middle;
} else {
low = middle;
}
}
high
}
#[test]
fn the_stack_of_a_level_of_recursion() {
on_a_default_stack(|| {
let named = prepare(
r#"<xsl:template match="/"><r><xsl:call-template name="down"><xsl:with-param name="n" select="xs:integer(/n)"/></xsl:call-template></r></xsl:template>
<xsl:template name="down"><xsl:param name="n"/><xsl:choose><xsl:when test="$n eq 0"><xsl:value-of select="$n"/></xsl:when><xsl:otherwise><xsl:call-template name="down"><xsl:with-param name="n" select="$n - 1"/></xsl:call-template><xsl:sequence select="()"/></xsl:otherwise></xsl:choose></xsl:template>"#,
);
let rules = prepare(
r#"<xsl:template match="x"><xsl:apply-templates select="@*|*"/></xsl:template>"#,
);
let built_in =
prepare(r#"<xsl:template match="/"><r><xsl:apply-templates/></r></xsl:template>"#);
let cen = common::executable(&common::workspace_root()).unwrap();
let number = |n: usize| format!("<n>{n}</n>");
let elements = |n: usize| nested(n, "");
let invoice = |n: usize| deep_invoice(n, n).0;
let level = |executable: &Executable, input: &dyn Fn(usize) -> String, deepest: u32| {
let half = deepest / 2;
let low = least_stack(executable, &input(half as usize));
let high = least_stack(executable, &input(deepest as usize));
(high - low) / half
};
let levels = [
("named template", level(&named, &number, 512)),
("template rule", level(&rules, &elements, 512)),
("built-in rule", level(&built_in, &elements, 512)),
("CEN stylesheet", level(&cen, &invoice, DEFAULT_MAX_DEPTH)),
];
assert!(
levels.iter().all(|&(_, bytes)| bytes < 3 << 10),
"{levels:?}"
);
});
}
#[test]
fn chains_of_lazy_values_are_bounded() {
let template = prepare(
r#"<xsl:template match="/"><r><xsl:call-template name="sum"><xsl:with-param name="n" select="xs:integer(/n)"/></xsl:call-template></r></xsl:template>
<xsl:template name="sum"><xsl:param name="n"/><xsl:param name="total" select="0"/><xsl:choose><xsl:when test="$n eq 0"><xsl:value-of select="$total"/></xsl:when><xsl:otherwise><xsl:call-template name="sum"><xsl:with-param name="n" select="$n - 1"/><xsl:with-param name="total" select="$total + $n"/></xsl:call-template></xsl:otherwise></xsl:choose></xsl:template>"#,
);
on_a_default_stack(move || {
let run = |n: u32| {
template
.transform(format!("<n>{n}</n>").as_str(), None)
.map(|t| t.output)
};
assert_eq!(run(40).unwrap(), r("820"));
assert_eq!(
limit(run(550)),
LimitExceeded::Stack {
max: DEFAULT_MAX_STACK
}
);
});
}
#[test]
fn built_in_rules_are_counted() {
let executable =
prepare(r#"<xsl:template match="/"><r><xsl:apply-templates/></r></xsl:template>"#);
on_a_default_stack(move || {
let options = TransformOptions {
max_depth: 100_000,
..TransformOptions::default()
};
let text = nested(575, "t");
assert_eq!(
executable
.transform_with(text.as_str(), None, &options)
.unwrap()
.output,
r("t")
);
let text = nested(576, "t");
assert_eq!(
limit(executable.transform_with(text.as_str(), None, &options)),
LimitExceeded::Nesting { max: 576 }
);
});
}
#[test]
fn deep_trees_are_walked_without_recursion() {
const DEPTH: usize = 100_000;
let options = || TransformOptions {
max_depth: 100_000,
..TransformOptions::default()
};
let copy = prepare(r#"<xsl:template match="/"><xsl:sequence select="."/></xsl:template>"#);
let tree = prepare(
r#"<xsl:template match="/"><xsl:variable name="t"><xsl:sequence select="/"/></xsl:variable><r><xsl:value-of select="count($t//x), string($t), count(($t//x)[last()]/ancestor::*)"/></r></xsl:template>"#,
);
let ambiguous = prepare(
r#"<xsl:template match="/"><r><xsl:apply-templates select="//x[not(*)]"/></r></xsl:template>
<xsl:template match="x"><a/></xsl:template>
<xsl:template match="x"><b/></xsl:template>"#,
);
on_a_default_stack(move || {
let text = nested(DEPTH, "t");
assert_eq!(
copy.transform_with(text.as_str(), None, &options())
.unwrap()
.output,
text
);
assert_eq!(
tree.transform_with(text.as_str(), None, &options())
.unwrap()
.output,
r(&format!("{DEPTH} t {}", DEPTH - 1))
);
let result = ambiguous
.transform_with(text.as_str(), None, &options())
.unwrap();
assert_eq!(result.output, r("<b/>"));
assert_eq!(result.warnings.len(), 1, "{:?}", result.warnings);
let path = format!("/x{}", "/x[1]".repeat(DEPTH - 1));
assert!(
result.warnings[0].contains(&format!("Ambiguous rule match for {path} in mode")),
"{}",
result.warnings[0].chars().take(200).collect::<String>()
);
});
}
struct Deep;
impl DocumentResolver for Deep {
fn resolve(&self, uri: &str) -> Option<String> {
let depth = uri.strip_prefix("urn:deep:")?.parse().ok()?;
Some(nested(depth, ""))
}
}
#[test]
fn documents_loaded_by_document_have_the_maximum_depth() {
let executable = prepare(
r#"<xsl:template match="/"><r><xsl:value-of select="count(document(string(/d))//x)"/></r></xsl:template>"#,
);
let in_pattern = prepare(
r#"<xsl:template match="d[count(document(string(.))//x) gt 0]"><m/></xsl:template>"#,
);
on_a_default_stack(move || {
let options = TransformOptions {
resolver: Some(&Deep),
..TransformOptions::default()
};
let ok = executable
.transform_with("<d>urn:deep:256</d>", None, &options)
.unwrap();
assert_eq!(ok.output, r("256"));
for sheet in [&executable, &in_pattern] {
let error = sheet
.transform_with("<d>urn:deep:257</d>", None, &options)
.unwrap_err();
assert_eq!(
error.limit(),
Some(LimitExceeded::Depth {
max: 256,
line: 1,
column: 1 + 3 * 256
}),
"{error}"
);
assert!(error.message().starts_with("urn:deep:257: "), "{error}");
}
});
}