pre-filling fields

This commit is contained in:
Sergey Fedorov 2024-06-05 19:25:20 +03:00
parent 5f28280180
commit e9c39aef25
2 changed files with 46 additions and 2 deletions

View file

@ -33,3 +33,34 @@ func FuzzTest(f *testing.F) {
}
})
}
type MyStruct struct {
Field1 string
Field2 float32
Field3 int
Field4 [5]int
}
func FuzzTest2(f *testing.F) {
f.Fuzz(func(t *testing.T, genData []byte) {
if len(genData) < 15 {
return
}
fuzz2 := NewGenerator(genData)
fieldInfo := map[string]interface{}{
"MyStruct": MyStruct{Field1: "Value1", Field3: 100},
}
fuzz2.SetPreFilled(fieldInfo)
var newStruct MyStruct
err := fuzz2.GenerateStruct(&newStruct)
t.Log(newStruct.Field1)
t.Log(newStruct.Field2)
if err != nil {
}
})
}

View file

@ -14,16 +14,29 @@ type Generator struct {
position uint32
mapStruct map[string]interface{}
arrayStruct []interface{}
fieldInfo map[string]interface{}
}
func NewGenerator(fuzzData []byte) *Generator {
return &Generator{
data: fuzzData,
dataSize: uint32(len(fuzzData)),
data: fuzzData,
dataSize: uint32(len(fuzzData)),
fieldInfo: make(map[string]interface{}),
}
}
func (g *Generator) SetPreFilled(fieldInfo map[string]interface{}) {
g.fieldInfo = fieldInfo
}
func (g *Generator) fillAny(any reflect.Value) error {
if fieldInfoValue, ok := g.fieldInfo[any.Type().String()]; ok {
fieldInfoData := reflect.ValueOf(fieldInfoValue)
any.Set(fieldInfoData)
return nil
}
switch any.Kind() {
case reflect.Struct:
for i := 0; i < any.NumField(); i++ {