github.com/cilium/cilium@v1.16.2/pkg/monitor/format/fuzz_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package format
     5  
     6  import (
     7  	"runtime"
     8  	"testing"
     9  
    10  	fuzz "github.com/AdaLogics/go-fuzz-headers"
    11  
    12  	"github.com/cilium/cilium/pkg/monitor/payload"
    13  )
    14  
    15  /*
    16  This fuzzer invokes the garbage collector, because
    17  fmt.Sprintf() seemingly will not free memory between
    18  runs. The GC slows down the fuzzer significantly.
    19  */
    20  func FuzzFormatEvent(f *testing.F) {
    21  	f.Fuzz(func(t *testing.T, data []byte) {
    22  		f := fuzz.NewConsumer(data)
    23  		pl := &payload.Payload{}
    24  		err := f.GenerateStruct(pl)
    25  		if err != nil {
    26  			return
    27  		}
    28  
    29  		// Invalid pl.Data. Leave here to avoid
    30  		// invoking the GC.
    31  		if len(pl.Data) == 0 {
    32  			return
    33  		}
    34  
    35  		defer func() {
    36  			if r := recover(); r != nil {
    37  			}
    38  			runtime.GC()
    39  		}()
    40  
    41  		mf := NewMonitorFormatter(0, nil)
    42  
    43  		mf.FormatEvent(pl)
    44  	})
    45  }