Skip to content

Commit

Permalink
add more fuzzing
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverpool committed Oct 23, 2024
1 parent fc9f538 commit deb971e
Showing 1 changed file with 49 additions and 4 deletions.
53 changes: 49 additions & 4 deletions card_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,13 +391,58 @@ func TestCard_Revision(t *testing.T) {
}

func TestCard_FieldValue(t *testing.T) {
got := NewFieldValue("geo:1.23,4.56")
if expected := FieldValue("geo:1.23\\,4.56"); got != expected {
t.Errorf("Expected FieldValue to be %q but got %q", expected, got)
fieldValues := map[FieldValue][]string{
"": {""},
",": {"", ""},
"\\,": {","},
",,\\,,": {"", "", ",", ""},
"geo:1.23\\,4.56": {"geo:1.23,4.56"},
"geo:1.23,4.56": {"geo:1.23", "4.56"},
"geo:1\\,23,4\\,56": {"geo:1,23", "4,56"},
}
for fv, parts := range fieldValues {
t.Run(fv.String(), func(t *testing.T) {
gotParts := fv.Values()
if !reflect.DeepEqual(parts, gotParts) {
t.Errorf("Expected parts to be %+v but got %+v", parts, gotParts)
}

gotFV := NewFieldValue(parts...)
if gotFV != fv {
t.Errorf("Expected FieldValue to be %+v but got %+v", fv, gotFV)
}
})
}
}

func FuzzCard_FieldValue(f *testing.F) {
// go test -fuzztime=10s -fuzz=Card_FieldValueRaw
func FuzzCard_FieldValueRaw(f *testing.F) {
f.Add(``)
f.Add(`123`)
f.Add(`1,2,3`)
f.Add(`1\abc`) // missing escaping of "\"
f.Add(`1\,2,3`)
f.Add(`1\\,2,3`)
f.Fuzz(func(t *testing.T, raw string) {
fv := FieldValue(raw)
parts := fv.Values()
got1 := NewFieldValue(parts...)
if got1 != fv {
// the raw value was wrongly escaped:
// "got" should now be correctly escaped
if len(got1) <= len(fv) {
t.Errorf("Expected a larger (escaped) string than %q, got %q", fv, got1)
}
// encode again and check that we get back the same (correctly escaped) raw value
got2 := NewFieldValue(got1.Values()...)
if got1 != got2 {
t.Errorf("Expected %q, got %q", got1, got2)
}
}
})
}

func FuzzCard_FieldValueParts(f *testing.F) {
f.Add("p0", "p1")
f.Add("1,2", "3")
f.Add("1\\,2", "3")
Expand Down

0 comments on commit deb971e

Please sign in to comment.