refactor(scanner): take seed from fuzzer

This commit is contained in:
sentriz
2022-03-02 09:30:51 +00:00
parent d6492c1521
commit 9d406afb1f
5 changed files with 11 additions and 14 deletions

View File

@@ -4,7 +4,6 @@
package scanner_test package scanner_test
import ( import (
"encoding/binary"
"fmt" "fmt"
"math/rand" "math/rand"
"reflect" "reflect"
@@ -28,7 +27,7 @@ func FuzzScanner(f *testing.F) {
is.Equal(ctx.GenresMissing(), 0) is.Equal(ctx.GenresMissing(), 0)
} }
f.Fuzz(func(t *testing.T, data []byte) { f.Fuzz(func(t *testing.T, data []byte, seed int64) {
is := is.NewRelaxed(t) is := is.NewRelaxed(t)
m := mockfs.New(t) m := mockfs.New(t)
defer m.CleanUp() defer m.CleanUp()
@@ -38,7 +37,7 @@ func FuzzScanner(f *testing.F) {
path := fmt.Sprintf("artist-%d/album-%d/track-%d.flac", i/6, i/3, i) path := fmt.Sprintf("artist-%d/album-%d/track-%d.flac", i/6, i/3, i)
m.AddTrack(path) m.AddTrack(path)
m.SetTags(path, func(tags *mockfs.Tags) error { m.SetTags(path, func(tags *mockfs.Tags) error {
fuzzStruct(i, data, tags) fuzzStruct(i, data, seed, tags)
return nil return nil
}) })
} }
@@ -48,12 +47,12 @@ func FuzzScanner(f *testing.F) {
}) })
} }
func fuzzStruct(taken int, data []byte, dest interface{}) { func fuzzStruct(taken int, data []byte, seed int64, dest interface{}) {
if len(data) == 0 { if len(data) == 0 {
return return
} }
r := seedRand(data) r := rand.New(rand.NewSource(seed))
v := reflect.ValueOf(dest) v := reflect.ValueOf(dest)
for i := 0; i < v.Elem().NumField(); i++ { for i := 0; i < v.Elem().NumField(); i++ {
if r.Float64() < 0.1 { if r.Float64() < 0.1 {
@@ -79,15 +78,7 @@ func fuzzStruct(taken int, data []byte, dest interface{}) {
case reflect.Float32, reflect.Float64: case reflect.Float32, reflect.Float64:
f.SetFloat(float64(b[0])) f.SetFloat(float64(b[0]))
case reflect.Struct: case reflect.Struct:
fuzzStruct(taken, data, f.Addr().Interface()) fuzzStruct(taken, data, seed, f.Addr().Interface())
} }
} }
} }
func seedRand(seed []byte) *rand.Rand {
b := make([]byte, 8)
for i := range b {
b[i] = seed[i%len(seed)]
}
return rand.New(rand.NewSource(int64(binary.BigEndian.Uint64(b))))
}

View File

@@ -1,2 +1,3 @@
go test fuzz v1 go test fuzz v1
[]byte("001") []byte("001")
int64(3472329395739373616)

View File

@@ -0,0 +1,3 @@
go test fuzz v1
[]byte("001")
int64(3472329395739373616)

View File

@@ -1,2 +1,3 @@
go test fuzz v1 go test fuzz v1
[]byte("") []byte("")
int64(0)