...

Source file src/github.com/AdaLogics/go-fuzz-headers/funcs.go

Documentation: github.com/AdaLogics/go-fuzz-headers

     1  // Copyright 2023 The go-fuzz-headers Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package gofuzzheaders
    16  
    17  import (
    18  	"fmt"
    19  	"reflect"
    20  )
    21  
    22  type Continue struct {
    23  	F *ConsumeFuzzer
    24  }
    25  
    26  func (f *ConsumeFuzzer) AddFuncs(fuzzFuncs []interface{}) {
    27  	for i := range fuzzFuncs {
    28  		v := reflect.ValueOf(fuzzFuncs[i])
    29  		if v.Kind() != reflect.Func {
    30  			panic("Need only funcs!")
    31  		}
    32  		t := v.Type()
    33  		if t.NumIn() != 2 || t.NumOut() != 1 {
    34  			fmt.Println(t.NumIn(), t.NumOut())
    35  
    36  			panic("Need 2 in and 1 out params. In must be the type. Out must be an error")
    37  		}
    38  		argT := t.In(0)
    39  		switch argT.Kind() {
    40  		case reflect.Ptr, reflect.Map:
    41  		default:
    42  			panic("fuzzFunc must take pointer or map type")
    43  		}
    44  		if t.In(1) != reflect.TypeOf(Continue{}) {
    45  			panic("fuzzFunc's second parameter must be type Continue")
    46  		}
    47  		f.Funcs[argT] = v
    48  	}
    49  }
    50  
    51  func (f *ConsumeFuzzer) GenerateWithCustom(targetStruct interface{}) error {
    52  	e := reflect.ValueOf(targetStruct).Elem()
    53  	return f.fuzzStruct(e, true)
    54  }
    55  
    56  func (c Continue) GenerateStruct(targetStruct interface{}) error {
    57  	return c.F.GenerateStruct(targetStruct)
    58  }
    59  
    60  func (c Continue) GenerateStructWithCustom(targetStruct interface{}) error {
    61  	return c.F.GenerateWithCustom(targetStruct)
    62  }
    63  

View as plain text