Skip to content

Commit

Permalink
Add test that shows an infinite loop with an invalid mustache template
Browse files Browse the repository at this point in the history
  • Loading branch information
paulharris committed Oct 18, 2024
1 parent aab0a15 commit 3ea04db
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
15 changes: 15 additions & 0 deletions tests/template/crow_extra_mustache_tests.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"overview": "Check Crow's behaviour when given invalid mustache templates",
"tests": [
{
"name": "Missing end-tags",
"desc": "Missing end-tags should fail to render ... and not enter infinite loops or other undefined behaviour",
"data": {
"boolean": true
},
"template": "\"{{#boolean}}{{^boolean}}\"",
"expected": "COMPILE EXCEPTION: crow::mustache error: open tag has no matching end tag {{# {{/ pair: boolean"
}
],
"__ATTN__": "This file was hand-written"
}
30 changes: 18 additions & 12 deletions tests/template/mustachetest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,23 @@ string read_all(const string& filename)

int main()
{
auto data = json::load(read_all("data"));
auto templ = compile(read_all("template"));
auto partials = json::load(read_all("partials"));
set_loader([&](std::string name) -> std::string {
if (partials.count(name))
{
return partials[name].s();
}
return "";
});
context ctx(data);
cout << templ.render_string(ctx);
try {
auto data = json::load(read_all("data"));
auto templ = compile(read_all("template"));
auto partials = json::load(read_all("partials"));
set_loader([&](std::string name) -> std::string {
if (partials.count(name))
{
return partials[name].s();
}
return "";
});
context ctx(data);
cout << templ.render_string(ctx);
}
// catch and return compile errors as text, for the python test to compare
catch (invalid_template_exception & err) {
cout << "COMPILE EXCEPTION: " << err.what();
}
return 0;
}
2 changes: 1 addition & 1 deletion tests/template/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
print('Data: ', json.dumps(test["data"]))
print('Template: ', test["template"])
print('Expected:', repr(test["expected"]))
print('Actual:', repr(ret))
print('Actual: ', repr(ret))
assert ret == test["expected"]

os.unlink('data')
Expand Down

0 comments on commit 3ea04db

Please sign in to comment.