Skip to content

Commit

Permalink
Fuzz consistency must also check number of bytes consumed (#56)
Browse files Browse the repository at this point in the history
* Fuzz consistency must also check number of bytes consumed

* Check if encode returns the correct number of bytes encoded
  • Loading branch information
fasmat authored May 15, 2023
1 parent 8e485e9 commit 843115a
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions tester/fuzz.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,20 @@ func FuzzConsistency[T any, H scale.TypePtr[T]](f *testing.F, fuzzFuncs ...any)

buf := bytes.NewBuffer(nil)
enc := scale.NewEncoder(buf)
_, err := H(&object).EncodeScale(enc)
n1, err := H(&object).EncodeScale(enc)
if errors.Is(err, scale.ErrEncodeTooManyElements) {
return
}
require.NoError(t, err)
require.Equal(t, n1, buf.Len())

dec := scale.NewDecoder(buf)
var decoded T
_, err = H(&decoded).DecodeScale(dec)
n2, err := H(&decoded).DecodeScale(dec)
require.NoError(t, err)

require.Equal(t, n1, n2)

if !cmp.Equal(object, decoded, cmpopts.EquateEmpty()) {
t.Errorf("decoded didn't match original: %s", cmp.Diff(object, decoded))
}
Expand Down

0 comments on commit 843115a

Please sign in to comment.