github.com/mikelsr/quic-go@v0.36.1-0.20230701132136-1d9415b66898/fuzzing/transportparameters/cmd/corpus.go (about)

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