Skip to content

Commit a8fabfe

Browse files
skirtan1kehoecj
andauthored
adding fuzz tests for validators (#248)
* add fuzz tests for validators Signed-off-by: Shreyas Kirtane <[email protected]> * fixing lint issues Signed-off-by: Shreyas Kirtane <[email protected]> --------- Signed-off-by: Shreyas Kirtane <[email protected]> Co-authored-by: kehoecj <[email protected]>
1 parent 266811f commit a8fabfe

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

pkg/validator/validator_test.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,22 @@ var (
3535
</dict> <!-- Missing value for the key 'NSAllowsArbitraryLoads' -->
3636
</dict>
3737
</plist>`)
38+
39+
fuzzbank = [][]byte{
40+
[]byte(`{test": "test"}`), []byte(`{"test": "test"}`),
41+
[]byte(`{}`), []byte(`[]`), []byte(`{]'{}}`), []byte("no_rizz"),
42+
[]byte(`{"hows_the_market": "full_of_crabs"}`), []byte("a: 1\nb: 2"),
43+
[]byte("a: b\nc: d:::::::::::::::"),
44+
[]byte("<test>\n</test>"), []byte("<xml\n"), []byte("name = 123__456"),
45+
[]byte("name = 123"), []byte(`{[Version]\nCatalog=hidden\n}`),
46+
[]byte(`\nCatalog hidden\n`), []byte("key=value\nkey2=${key}"),
47+
[]byte("key=${key}"), []byte(`key = "value"`),
48+
[]byte(`"key" = "value"`), []byte(`"key1" = "value1"\n"key2"="value2"`),
49+
[]byte(`first_name,last_name,username\nRob,Pike,rob\n`),
50+
[]byte(`This string has a \" in it`), validPlistBytes, invalidPlistBytes,
51+
[]byte(`test = [1, 2, 3]`), []byte(`test = [1, 2,, 3]`), []byte("KEY=VALUE"),
52+
[]byte("=TEST"), []byte("working = true"), []byte("[*.md\nworking=false"),
53+
}
3854
)
3955

4056
var testData = []struct {
@@ -94,3 +110,76 @@ func Test_ValidationInput(t *testing.T) {
94110
})
95111
}
96112
}
113+
114+
func addFuzzCases(f *testing.F) {
115+
f.Helper()
116+
for _, tc := range fuzzbank {
117+
f.Add(tc)
118+
}
119+
}
120+
121+
func fuzzFunction(v Validator) func(*testing.T, []byte) {
122+
return func(_ *testing.T, a []byte) {
123+
_, _ = v.Validate(a)
124+
}
125+
}
126+
127+
func FuzzJsonValidator(f *testing.F) {
128+
addFuzzCases(f)
129+
f.Fuzz(fuzzFunction(JSONValidator{}))
130+
}
131+
132+
func FuzzYamlValidator(f *testing.F) {
133+
addFuzzCases(f)
134+
f.Fuzz(fuzzFunction(YAMLValidator{}))
135+
}
136+
137+
func FuzzXMLValidator(f *testing.F) {
138+
addFuzzCases(f)
139+
f.Fuzz(fuzzFunction(XMLValidator{}))
140+
}
141+
142+
func FuzzTomlValidator(f *testing.F) {
143+
addFuzzCases(f)
144+
f.Fuzz(fuzzFunction(TomlValidator{}))
145+
}
146+
147+
func FuzzIniValidator(f *testing.F) {
148+
addFuzzCases(f)
149+
f.Fuzz(fuzzFunction(IniValidator{}))
150+
}
151+
152+
func FuzzPropValidator(f *testing.F) {
153+
addFuzzCases(f)
154+
f.Fuzz(fuzzFunction(PropValidator{}))
155+
}
156+
157+
func FuzzHclValidator(f *testing.F) {
158+
addFuzzCases(f)
159+
f.Fuzz(fuzzFunction(HclValidator{}))
160+
}
161+
162+
func FuzzCsvValidator(f *testing.F) {
163+
addFuzzCases(f)
164+
f.Fuzz(fuzzFunction(CsvValidator{}))
165+
}
166+
167+
func FuzzPlistValidator(f *testing.F) {
168+
addFuzzCases(f)
169+
f.Fuzz(fuzzFunction(PlistValidator{}))
170+
}
171+
172+
func FuzzHoconValidator(f *testing.F) {
173+
addFuzzCases(f)
174+
f.Fuzz(fuzzFunction(HoconValidator{}))
175+
}
176+
177+
func FuzzEnvValidator(f *testing.F) {
178+
addFuzzCases(f)
179+
f.Fuzz(fuzzFunction(EnvValidator{}))
180+
}
181+
182+
func FuzzEditorConfigValidator(f *testing.F) {
183+
addFuzzCases(f)
184+
f.Fuzz(fuzzFunction(EditorConfigValidator{}))
185+
}

0 commit comments

Comments
 (0)