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 Apr 22, 2022
1 parent a06db67 commit 3a117e2
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 3a117e2

Please sign in to comment.