github.com/tumi8/quic-go@v0.37.4-tum/fuzzing/transportparameters/fuzz.go (about)

     1  package transportparameters
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  
     7  	"github.com/tumi8/quic-go/fuzzing/internal/helper"
     8  	"github.com/tumi8/quic-go/noninternal/protocol"
     9  	"github.com/tumi8/quic-go/noninternal/wire"
    10  )
    11  
    12  // PrefixLen is the number of bytes used for configuration
    13  const PrefixLen = 1
    14  
    15  // Fuzz fuzzes the QUIC transport parameters.
    16  //
    17  //go:generate go run ./cmd/corpus.go
    18  func Fuzz(data []byte) int {
    19  	if len(data) <= PrefixLen {
    20  		return 0
    21  	}
    22  
    23  	if helper.NthBit(data[0], 0) {
    24  		return fuzzTransportParametersForSessionTicket(data[PrefixLen:])
    25  	}
    26  	return fuzzTransportParameters(data[PrefixLen:], helper.NthBit(data[0], 1))
    27  }
    28  
    29  func fuzzTransportParameters(data []byte, isServer bool) int {
    30  	perspective := protocol.PerspectiveClient
    31  	if isServer {
    32  		perspective = protocol.PerspectiveServer
    33  	}
    34  
    35  	tp := &wire.TransportParameters{}
    36  	if err := tp.Unmarshal(data, perspective); err != nil {
    37  		return 0
    38  	}
    39  	_ = tp.String()
    40  
    41  	tp2 := &wire.TransportParameters{}
    42  	if err := tp2.Unmarshal(tp.Marshal(perspective), perspective); err != nil {
    43  		fmt.Printf("%#v\n", tp)
    44  		panic(err)
    45  	}
    46  	return 1
    47  }
    48  
    49  func fuzzTransportParametersForSessionTicket(data []byte) int {
    50  	tp := &wire.TransportParameters{}
    51  	if err := tp.UnmarshalFromSessionTicket(bytes.NewReader(data)); err != nil {
    52  		return 0
    53  	}
    54  	b := tp.MarshalForSessionTicket(nil)
    55  	tp2 := &wire.TransportParameters{}
    56  	if err := tp2.UnmarshalFromSessionTicket(bytes.NewReader(b)); err != nil {
    57  		panic(err)
    58  	}
    59  	return 1
    60  }