test(parsers.json): add fuzz test (#12368)

This commit is contained in:
Joshua Powers 2022-12-13 06:41:08 -07:00 committed by GitHub
parent 1050d35f33
commit 42640b1def
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 0 deletions

42
internal/fuzz/json.go Normal file
View File

@ -0,0 +1,42 @@
package fuzz
// https://github.com/google/AFL/blob/master/dictionaries/json.dict
var JSONDictionary = []string{
"0",
",0",
":0",
"0:",
"-1.2e+3",
"true",
"false",
"null",
"\"\"",
",\"\"",
":\"\"",
"\"\":",
"{}",
",{}",
":{}",
"{\"\":0}",
"{{}}",
"[]",
",[]",
":[]",
"[0]",
"[[]]",
"''",
"\\",
"\\b",
"\\f",
"\\n",
"\\r",
"\\t",
"\\u0000",
"\\x00",
"\\0",
"\\uD800\\uDC00",
"\\uDBFF\\uDFFF",
"\"\":0",
"//",
"/**/",
}

View File

@ -6,6 +6,7 @@ import (
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal/fuzz"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/require"
)
@ -1356,3 +1357,23 @@ func TestParseArrayWithWildcardTagKeys(t *testing.T) {
})
}
}
func FuzzParserJSON(f *testing.F) {
for _, value := range fuzz.JSONDictionary {
f.Add([]byte(value))
}
f.Add([]byte(validJSON))
f.Add([]byte(validJSONArray))
f.Add([]byte(validJSONArrayMultiple))
f.Add([]byte(validJSONArrayTags))
f.Add([]byte(validJSONNewline))
f.Add([]byte(validJSONTags))
parser := &Parser{MetricName: "testing"}
require.NoError(f, parser.Init())
f.Fuzz(func(t *testing.T, input []byte) {
_, _ = parser.Parse(input)
})
}