Skip to content

Commit

Permalink
add fuzzing test
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg committed May 27, 2024
1 parent e0430e6 commit a0b8085
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions hash_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package header_test
package header

import (
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"testing"

"github.com/celestiaorg/go-header"
"github.com/stretchr/testify/require"
)

Expand All @@ -16,7 +15,7 @@ func TestHash(t *testing.T) {
buf, err := h.MarshalJSON()
require.NoError(t, err)

var h2 header.Hash
var h2 Hash
err = h2.UnmarshalJSON(buf)
require.NoError(t, err)

Expand Down Expand Up @@ -49,7 +48,7 @@ func BenchmarkHashMarshaling(b *testing.B) {
})

b.Run("Unmarshal", func(b *testing.B) {
var h2 header.Hash
var h2 Hash

for i := 0; i < b.N; i++ {
err := h2.UnmarshalJSON(golden)
Expand All @@ -58,10 +57,18 @@ func BenchmarkHashMarshaling(b *testing.B) {
})
}

func randHash() header.Hash {
func Fuzz_hexToUpper(f *testing.F) {
f.Add([]byte("48656c6c6f20476f7068657221"))

f.Fuzz(func(t *testing.T, buf []byte) {
hexToUpper(buf)
})
}

func randHash() Hash {
var buf [sha256.Size]byte
if _, err := rand.Read(buf[:]); err != nil {
panic(err)
}
return header.Hash(buf[:])
return Hash(buf[:])
}

0 comments on commit a0b8085

Please sign in to comment.