diff --git a/fuzz_test.go b/fuzz_test.go index 79d3f2b..8ec0c92 100644 --- a/fuzz_test.go +++ b/fuzz_test.go @@ -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 { + } + }) +} diff --git a/generator.go b/generator.go index 0a213ee..55068fc 100644 --- a/generator.go +++ b/generator.go @@ -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++ {