github.com/TugasAkhir-QUIC/quic-go@v0.0.2-0.20240215011318-d20e25a9054c/fuzzing/header/cmd/corpus.go (about)

     1  package main
     2  
     3  import (
     4  	"log"
     5  
     6  	"golang.org/x/exp/rand"
     7  
     8  	"github.com/TugasAkhir-QUIC/quic-go/fuzzing/header"
     9  	"github.com/TugasAkhir-QUIC/quic-go/fuzzing/internal/helper"
    10  	"github.com/TugasAkhir-QUIC/quic-go/internal/protocol"
    11  	"github.com/TugasAkhir-QUIC/quic-go/internal/wire"
    12  )
    13  
    14  const version = protocol.Version1
    15  
    16  func getRandomData(l int) []byte {
    17  	b := make([]byte, l)
    18  	rand.Read(b)
    19  	return b
    20  }
    21  
    22  func getVNP(src, dest protocol.ArbitraryLenConnectionID, numVersions int) []byte {
    23  	versions := make([]protocol.Version, numVersions)
    24  	for i := 0; i < numVersions; i++ {
    25  		versions[i] = protocol.Version(rand.Uint32())
    26  	}
    27  	return wire.ComposeVersionNegotiation(src, dest, versions)
    28  }
    29  
    30  func main() {
    31  	headers := []wire.Header{
    32  		{ // Initial without token
    33  			SrcConnectionID:  protocol.ParseConnectionID(getRandomData(3)),
    34  			DestConnectionID: protocol.ParseConnectionID(getRandomData(8)),
    35  			Type:             protocol.PacketTypeInitial,
    36  			Length:           protocol.ByteCount(rand.Intn(1000)),
    37  			Version:          version,
    38  		},
    39  		{ // Initial without token, with zero-length src conn id
    40  			DestConnectionID: protocol.ParseConnectionID(getRandomData(8)),
    41  			Type:             protocol.PacketTypeInitial,
    42  			Length:           protocol.ByteCount(rand.Intn(1000)),
    43  			Version:          version,
    44  		},
    45  		{ // Initial with Token
    46  			SrcConnectionID:  protocol.ParseConnectionID(getRandomData(10)),
    47  			DestConnectionID: protocol.ParseConnectionID(getRandomData(19)),
    48  			Type:             protocol.PacketTypeInitial,
    49  			Length:           protocol.ByteCount(rand.Intn(1000)),
    50  			Version:          version,
    51  			Token:            getRandomData(25),
    52  		},
    53  		{ // Handshake packet
    54  			SrcConnectionID:  protocol.ParseConnectionID(getRandomData(5)),
    55  			DestConnectionID: protocol.ParseConnectionID(getRandomData(10)),
    56  			Type:             protocol.PacketTypeHandshake,
    57  			Length:           protocol.ByteCount(rand.Intn(1000)),
    58  			Version:          version,
    59  		},
    60  		{ // Handshake packet, with zero-length src conn id
    61  			DestConnectionID: protocol.ParseConnectionID(getRandomData(12)),
    62  			Type:             protocol.PacketTypeHandshake,
    63  			Length:           protocol.ByteCount(rand.Intn(1000)),
    64  			Version:          version,
    65  		},
    66  		{ // 0-RTT packet
    67  			SrcConnectionID:  protocol.ParseConnectionID(getRandomData(8)),
    68  			DestConnectionID: protocol.ParseConnectionID(getRandomData(9)),
    69  			Type:             protocol.PacketType0RTT,
    70  			Length:           protocol.ByteCount(rand.Intn(1000)),
    71  			Version:          version,
    72  		},
    73  		{ // Retry Packet, with empty orig dest conn id
    74  			SrcConnectionID:  protocol.ParseConnectionID(getRandomData(8)),
    75  			DestConnectionID: protocol.ParseConnectionID(getRandomData(9)),
    76  			Type:             protocol.PacketTypeRetry,
    77  			Token:            getRandomData(1000),
    78  			Version:          version,
    79  		},
    80  	}
    81  
    82  	for _, h := range headers {
    83  		extHdr := &wire.ExtendedHeader{
    84  			Header:          h,
    85  			PacketNumberLen: protocol.PacketNumberLen(rand.Intn(4) + 1),
    86  			PacketNumber:    protocol.PacketNumber(rand.Uint64()),
    87  		}
    88  		b, err := extHdr.Append(nil, version)
    89  		if err != nil {
    90  			log.Fatal(err)
    91  		}
    92  		if h.Type == protocol.PacketTypeRetry {
    93  			b = append(b, []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}...)
    94  		}
    95  		if h.Length > 0 {
    96  			b = append(b, make([]byte, h.Length)...)
    97  		}
    98  
    99  		if err := helper.WriteCorpusFileWithPrefix("corpus", b, header.PrefixLen); err != nil {
   100  			log.Fatal(err)
   101  		}
   102  	}
   103  
   104  	// short header
   105  	b, err := wire.AppendShortHeader(nil, protocol.ParseConnectionID(getRandomData(8)), 1337, protocol.PacketNumberLen2, protocol.KeyPhaseOne)
   106  	if err != nil {
   107  		log.Fatal(err)
   108  	}
   109  	if err := helper.WriteCorpusFileWithPrefix("corpus", b, header.PrefixLen); err != nil {
   110  		log.Fatal(err)
   111  	}
   112  
   113  	vnps := [][]byte{
   114  		getVNP(
   115  			protocol.ArbitraryLenConnectionID(getRandomData(8)),
   116  			protocol.ArbitraryLenConnectionID(getRandomData(10)),
   117  			4,
   118  		),
   119  		getVNP(
   120  			protocol.ArbitraryLenConnectionID(getRandomData(10)),
   121  			protocol.ArbitraryLenConnectionID(getRandomData(5)),
   122  			0,
   123  		),
   124  		getVNP(
   125  			protocol.ArbitraryLenConnectionID(getRandomData(3)),
   126  			protocol.ArbitraryLenConnectionID(getRandomData(19)),
   127  			100,
   128  		),
   129  		getVNP(
   130  			protocol.ArbitraryLenConnectionID(getRandomData(3)),
   131  			nil,
   132  			20,
   133  		),
   134  		getVNP(
   135  			nil,
   136  			protocol.ArbitraryLenConnectionID(getRandomData(10)),
   137  			5,
   138  		),
   139  	}
   140  
   141  	for _, vnp := range vnps {
   142  		if err := helper.WriteCorpusFileWithPrefix("corpus", vnp, header.PrefixLen); err != nil {
   143  			log.Fatal(err)
   144  		}
   145  	}
   146  }