github.com/metacubex/quic-go@v0.44.1-0.20240520163451-20b689a59136/fuzzing/transportparameters/cmd/corpus.go (about)

     1  package main
     2  
     3  import (
     4  	"log"
     5  	"math"
     6  	"net/netip"
     7  	"time"
     8  
     9  	"golang.org/x/exp/rand"
    10  
    11  	"github.com/metacubex/quic-go/fuzzing/internal/helper"
    12  	"github.com/metacubex/quic-go/fuzzing/transportparameters"
    13  	"github.com/metacubex/quic-go/internal/protocol"
    14  	"github.com/metacubex/quic-go/internal/wire"
    15  )
    16  
    17  func getRandomData(l int) []byte {
    18  	b := make([]byte, l)
    19  	rand.Read(b)
    20  	return b
    21  }
    22  
    23  func getRandomValue() uint64 {
    24  	maxVals := []int64{math.MaxUint8 / 4, math.MaxUint16 / 4, math.MaxUint32 / 4, math.MaxUint64 / 4}
    25  	return uint64(rand.Int63n(maxVals[int(rand.Int31n(4))]))
    26  }
    27  
    28  func main() {
    29  	for i := 0; i < 30; i++ {
    30  		tp := &wire.TransportParameters{
    31  			InitialMaxStreamDataBidiLocal:  protocol.ByteCount(getRandomValue()),
    32  			InitialMaxStreamDataBidiRemote: protocol.ByteCount(getRandomValue()),
    33  			InitialMaxStreamDataUni:        protocol.ByteCount(getRandomValue()),
    34  			InitialMaxData:                 protocol.ByteCount(getRandomValue()),
    35  			MaxAckDelay:                    time.Duration(getRandomValue()),
    36  			AckDelayExponent:               uint8(getRandomValue()),
    37  			DisableActiveMigration:         getRandomValue()%2 == 0,
    38  			MaxUDPPayloadSize:              protocol.ByteCount(getRandomValue()),
    39  			MaxUniStreamNum:                protocol.StreamNum(getRandomValue()),
    40  			MaxBidiStreamNum:               protocol.StreamNum(getRandomValue()),
    41  			MaxIdleTimeout:                 time.Duration(getRandomValue()),
    42  			ActiveConnectionIDLimit:        getRandomValue() + 2,
    43  		}
    44  		if rand.Int()%2 == 0 {
    45  			tp.OriginalDestinationConnectionID = protocol.ParseConnectionID(getRandomData(rand.Intn(21)))
    46  		}
    47  		if rand.Int()%2 == 0 {
    48  			tp.InitialSourceConnectionID = protocol.ParseConnectionID(getRandomData(rand.Intn(21)))
    49  		}
    50  		if rand.Int()%2 == 0 {
    51  			connID := protocol.ParseConnectionID(getRandomData(rand.Intn(21)))
    52  			tp.RetrySourceConnectionID = &connID
    53  		}
    54  		if rand.Int()%2 == 0 {
    55  			var token protocol.StatelessResetToken
    56  			rand.Read(token[:])
    57  			tp.StatelessResetToken = &token
    58  		}
    59  		if rand.Int()%2 == 0 {
    60  			var token protocol.StatelessResetToken
    61  			rand.Read(token[:])
    62  			var ip4 [4]byte
    63  			rand.Read(ip4[:])
    64  			var ip6 [16]byte
    65  			rand.Read(ip6[:])
    66  			tp.PreferredAddress = &wire.PreferredAddress{
    67  				IPv4:                netip.AddrPortFrom(netip.AddrFrom4(ip4), uint16(rand.Int())),
    68  				IPv6:                netip.AddrPortFrom(netip.AddrFrom16(ip6), uint16(rand.Int())),
    69  				ConnectionID:        protocol.ParseConnectionID(getRandomData(rand.Intn(21))),
    70  				StatelessResetToken: token,
    71  			}
    72  		}
    73  
    74  		var data []byte
    75  		if rand.Int()%2 == 0 {
    76  			pers := protocol.PerspectiveServer
    77  			if rand.Int()%2 == 0 {
    78  				pers = protocol.PerspectiveClient
    79  			}
    80  			data = tp.Marshal(pers)
    81  		} else {
    82  			data = tp.MarshalForSessionTicket(nil)
    83  		}
    84  		if err := helper.WriteCorpusFileWithPrefix("corpus", data, transportparameters.PrefixLen); err != nil {
    85  			log.Fatal(err)
    86  		}
    87  	}
    88  }