Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c1d8de1

Browse files
committed
test(templates): use existing template tests for execution fuzzing
1 parent 0487c96 commit c1d8de1

4 files changed

+40
-2
lines changed

pongo2_test.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package pongo2_test
22

33
import (
4+
"errors"
5+
"io"
6+
"io/ioutil"
7+
"path/filepath"
48
"testing"
59

610
"github.com/flosch/pongo2/v6"
@@ -99,10 +103,35 @@ func (s *TestSuite) TestImplicitExecCtx(c *C) {
99103
c.Check(res, Equals, val)
100104
}
101105

106+
type DummyLoader struct{}
107+
108+
func (l *DummyLoader) Abs(base, name string) string {
109+
return filepath.Join(filepath.Dir(base), name)
110+
}
111+
112+
func (l *DummyLoader) Get(path string) (io.Reader, error) {
113+
return nil, errors.New("dummy not found")
114+
}
115+
102116
func FuzzSimpleExecution(f *testing.F) {
103-
f.Add("{{ foobar }}", "test123")
117+
tpls, err := filepath.Glob("template_tests/*.tpl")
118+
if err != nil {
119+
f.Fatalf("glob: %v", err)
120+
}
121+
files := []string{"README.md"}
122+
files = append(files, tpls...)
123+
124+
for _, tplPath := range files {
125+
buf, err := ioutil.ReadFile(tplPath)
126+
if err != nil {
127+
f.Fatalf("could not read file '%v': %v", tplPath, err)
128+
}
129+
f.Add(string(buf), "test-value")
130+
}
131+
104132
f.Fuzz(func(t *testing.T, tpl, contextValue string) {
105-
out, err := pongo2.FromString(tpl)
133+
ts := pongo2.NewSet("fuzz-test", &DummyLoader{})
134+
out, err := ts.FromString(tpl)
106135
if err != nil && out != nil {
107136
t.Errorf("%v", err)
108137
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
go test fuzz v1
2+
string("{%macro A()%}{%if(0)%}{%endif%}0{{0}}0{{A}}0{%if 0%}{%endif%}0{%endmacro%}{%for A in A%}{%endfor%}")
3+
string("0")
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
go test fuzz v1
2+
string("empty single line comment\n{# #}\n\nfilled single line comment\n{# testing single line comment #}\n\nfilled single line comment with valid tags\n{# testing single line comment {% if thing %}{% endif %} #}\n\nfilled single line comment with invalid tags\n{# testing single line comment {% if thing %} #}\n\nfilled single line comment with invalid syntax\n{# testing single line comment {% if thing('') %}wow{% endif %} #}\n\nempty block comment\n{% comment %}{% endcomment %}\n\nfilled text single line block comment\n{% comment %}filled block comment {% endcomment %}\n\nempty multi line block comment\n{% comment %}\n\n\n{% endcomment %}\n\nblock comment with other tags inside of it\n{% comment %}\n {{ thing_goes_here }}\n {% if stuff %}do stuff{% endif %}\n{% endcomment %}\n\nblock comment with invalid tags inside of it\n{% comment %}\n {% if thing %}\n{% endcomment %}\n\nblock comment with invalid syntax inside of it\n{% comment %}\n {% thing('') %}\n{% endcomment \x00\n\nRegular tags between comments to verify it doesn't break in the lexer\n{% if hello %}\n{% endif %}\nafter if\n{% comment %}All done{% endcomment %}\n\nend of file\n")
3+
string("test-vKlue")
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
go test fuzz v1
2+
string("empty single line comment\n{# #}\n\nfilled single line comment\n{# testing single line comment #}\n\nfilled single line comment with valid tags\n{# testing single line comment {% if thing %}{% endif %} #}\n\nfilled single line comment with invalid tags\n{# testing single line comment {% if thing %} #}\n\nfilled single line comment with invalid syntax\n{# testing single line comment {% if thing('') %}wow{% endif %} #}\n\nempty block comment\n{% comment %}{% endcommenbl")
3+
string("test-value")

0 commit comments

Comments
 (0)