add fuzz tests (#234)

This commit is contained in:
Alessandro Ros
2023-04-09 18:02:36 +02:00
committed by GitHub
parent 5859c9cb29
commit b6727c07ef
31 changed files with 112 additions and 277 deletions

View File

@@ -116,63 +116,6 @@ func TestHeaderUnmarshal(t *testing.T) {
}
}
func TestHeaderUnmarshalErrors(t *testing.T) {
for _, ca := range []struct {
name string
dec []byte
err string
}{
{
"empty",
[]byte{},
"EOF",
},
{
"missing value",
[]byte("Testing:"),
"EOF",
},
{
"missing eol",
[]byte("Testing: val"),
"EOF",
},
{
"r without n",
[]byte("Testing: val\rTesting: val\r\n"),
"expected '\n', got 'T'",
},
{
"final r without n",
[]byte("Testing: val\r\nTesting: val\r\n\r"),
"EOF",
},
{
"missing value",
[]byte("Testing\r\n"),
"value is missing",
},
{
"too many entries",
func() []byte {
var ret []byte
for i := 0; i < headerMaxEntryCount+2; i++ {
ret = append(ret, []byte("Testing: val\r\n")...)
}
ret = append(ret, []byte("\r\n")...)
return ret
}(),
"headers count exceeds 255",
},
} {
t.Run(ca.name, func(t *testing.T) {
h := make(Header)
err := h.unmarshal(bufio.NewReader(bytes.NewBuffer(ca.dec)))
require.EqualError(t, err, ca.err)
})
}
}
func TestHeaderWrite(t *testing.T) {
for _, ca := range cases {
t.Run(ca.name, func(t *testing.T) {
@@ -181,3 +124,16 @@ func TestHeaderWrite(t *testing.T) {
})
}
}
func FuzzHeaderUnmarshal(f *testing.F) {
str := ""
for i := 0; i < 300; i++ {
str += "Key: val\r\n"
}
f.Add([]byte(str))
f.Fuzz(func(t *testing.T, b []byte) {
var h Header
h.unmarshal(bufio.NewReader(bytes.NewBuffer(b)))
})
}