2014-11-20 12:32:21 -05:00
|
|
|
package tpl
|
2013-11-10 15:04:51 -05:00
|
|
|
|
2015-04-30 04:51:01 -04:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Test for bugs discovered by https://github.com/dvyukov/go-fuzz
|
|
|
|
func TestTplGoFuzzReports(t *testing.T) {
|
2015-04-30 07:25:45 -04:00
|
|
|
|
|
|
|
// The following test case(s) also fail
|
|
|
|
// See https://github.com/golang/go/issues/10634
|
|
|
|
//{"{{ seq 433937734937734969526500969526500 }}", 2}}
|
|
|
|
|
2015-04-30 04:51:01 -04:00
|
|
|
for i, this := range []struct {
|
|
|
|
data string
|
|
|
|
expectErr int
|
2015-04-30 05:26:34 -04:00
|
|
|
}{
|
|
|
|
// Issue #1089
|
2015-05-02 05:32:38 -04:00
|
|
|
//{"{{apply .C \"first\" }}", 2},
|
2015-04-30 05:26:34 -04:00
|
|
|
// Issue #1090
|
2015-04-30 05:41:25 -04:00
|
|
|
{"{{ slicestr \"000000\" 10}}", 2},
|
|
|
|
// Issue #1091
|
2015-05-02 05:32:38 -04:00
|
|
|
//{"{{apply .C \"first\" 0 0 0}}", 2},
|
2015-05-01 11:00:22 -04:00
|
|
|
{"{{seq 3e80}}", 2},
|
|
|
|
// Issue #1095
|
|
|
|
{"{{apply .C \"urlize\" " +
|
|
|
|
"\".\"}}", 2}} {
|
2015-04-30 04:51:01 -04:00
|
|
|
templ := New()
|
|
|
|
|
|
|
|
d := &Data{
|
|
|
|
A: 42,
|
|
|
|
B: "foo",
|
|
|
|
C: []int{1, 2, 3},
|
|
|
|
D: map[int]string{1: "foo", 2: "bar"},
|
|
|
|
E: Data1{42, "foo"},
|
2015-04-30 07:25:45 -04:00
|
|
|
F: []string{"a", "b", "c"},
|
|
|
|
G: []string{"a", "b", "c", "d", "e"},
|
|
|
|
H: "a,b,c,d,e,f",
|
2015-04-30 04:51:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
err := templ.AddTemplate("fuzz", this.data)
|
|
|
|
|
|
|
|
if err != nil && this.expectErr == 0 {
|
|
|
|
t.Fatalf("Test %d errored: %s", i, err)
|
|
|
|
} else if err == nil && this.expectErr == 1 {
|
|
|
|
t.Fatalf("#1 Test %d should have errored", i)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = templ.ExecuteTemplate(ioutil.Discard, "fuzz", d)
|
|
|
|
|
|
|
|
if err != nil && this.expectErr == 0 {
|
|
|
|
t.Fatalf("Test %d errored: %s", i, err)
|
|
|
|
} else if err == nil && this.expectErr == 2 {
|
|
|
|
t.Fatalf("#2 Test %d should have errored", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Data struct {
|
|
|
|
A int
|
|
|
|
B string
|
|
|
|
C []int
|
|
|
|
D map[int]string
|
|
|
|
E Data1
|
2015-04-30 07:25:45 -04:00
|
|
|
F []string
|
|
|
|
G []string
|
|
|
|
H string
|
2015-04-30 04:51:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type Data1 struct {
|
|
|
|
A int
|
|
|
|
B string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (Data1) Q() string {
|
|
|
|
return "foo"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (Data1) W() (string, error) {
|
|
|
|
return "foo", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (Data1) E() (string, error) {
|
|
|
|
return "foo", errors.New("Data.E error")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (Data1) R(v int) (string, error) {
|
|
|
|
return "foo", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (Data1) T(s string) (string, error) {
|
|
|
|
return s, nil
|
|
|
|
}
|