Skip to content

Commit

Permalink
fix: normalize timestamps before testing roundtrip (#11734)
Browse files Browse the repository at this point in the history
FuzzTypesParseTimeBytes checks that roundtripping timestamps doens't
change them. However, that property only holds for timestamps returned
from FormatTimeBytes, not arbitrary timestamps.
  • Loading branch information
elias-orijtech authored and odeke-em committed Jun 4, 2022
1 parent 33fcf54 commit ac1584f
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions fuzz/tests/types_parsetimebytes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,20 @@ import (

func FuzzTypesParseTimeBytes(f *testing.F) {
f.Fuzz(func(t *testing.T, bin []byte) {
// Normalize input, reject invalid timestamps.
ti, err := types.ParseTimeBytes(bin)
if err != nil {
return
}
brt := types.FormatTimeBytes(ti)
if !bytes.Equal(brt, bin) {
panic(fmt.Sprintf("Roundtrip failure, got\n%s", brt))
// Check that roundtripping a normalized timestamp doesn't change it.
ti2, err := types.ParseTimeBytes(brt)
if err != nil {
panic(fmt.Errorf("failed to parse formatted time %q: %w", brt, err))
}
brt2 := types.FormatTimeBytes(ti2)
if !bytes.Equal(brt, brt2) {
panic(fmt.Sprintf("Roundtrip failure, got\n%q\nwant\n%q", brt, brt2))
}
})
}

0 comments on commit ac1584f

Please sign in to comment.