Skip to content

Commit 46379a7

Browse files
committed
Add FuzzHash
1 parent 7c8cdd1 commit 46379a7

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module github.com/gohugoio/hashstructure
22

33
go 1.18
4+
5+
require github.com/AdaLogics/go-fuzz-headers v0.0.0-20240806141605-e8a1dd7889d6 // indirect

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/AdaLogics/go-fuzz-headers v0.0.0-20240806141605-e8a1dd7889d6 h1:He8afgbRMd7mFxO99hRNu+6tazq8nFF9lIwo9JFroBk=
2+
github.com/AdaLogics/go-fuzz-headers v0.0.0-20240806141605-e8a1dd7889d6/go.mod h1:8o94RPi1/7XTJvwPpRSzSUedZrtlirdB3r9Z20bi2f8=

hashstructure_fuzz_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package hashstructure
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
fuzz "github.com/AdaLogics/go-fuzz-headers"
8+
)
9+
10+
func FuzzHash(f *testing.F) {
11+
type Test struct {
12+
Str string
13+
Int int
14+
In64 int64
15+
Float64 float64
16+
MapStr map[string]string
17+
MapStruct map[string]Test
18+
SliceStr []string
19+
SliceStrSet []string `hash:"set"`
20+
SliceByte []byte
21+
StrPtr *string
22+
IntPtr *int
23+
Time time.Time
24+
Duration time.Duration
25+
UUID string `hash:"ignore"`
26+
}
27+
28+
f.Fuzz(func(t *testing.T, data []byte) {
29+
fuzzConsumer := fuzz.NewConsumer(data)
30+
targetStruct := &Test{}
31+
err := fuzzConsumer.GenerateStruct(targetStruct)
32+
if err != nil {
33+
return
34+
}
35+
_, err = Hash(targetStruct, nil)
36+
if err != nil {
37+
t.Fatal(err)
38+
}
39+
})
40+
}

0 commit comments

Comments
 (0)