github.com/danielpfeifer02/quic-go-prio-packs@v0.41.0-28/qlog/packet_header_test.go (about)

     1  package qlog
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  
     7  	"github.com/francoispqt/gojay"
     8  
     9  	"github.com/danielpfeifer02/quic-go-prio-packs/internal/protocol"
    10  	"github.com/danielpfeifer02/quic-go-prio-packs/internal/wire"
    11  	"github.com/danielpfeifer02/quic-go-prio-packs/logging"
    12  
    13  	. "github.com/onsi/ginkgo/v2"
    14  	. "github.com/onsi/gomega"
    15  )
    16  
    17  var _ = Describe("Packet Header", func() {
    18  	It("determines the packet type from the encryption level", func() {
    19  		Expect(getPacketTypeFromEncryptionLevel(protocol.EncryptionInitial)).To(BeEquivalentTo(logging.PacketTypeInitial))
    20  		Expect(getPacketTypeFromEncryptionLevel(protocol.EncryptionHandshake)).To(BeEquivalentTo(logging.PacketTypeHandshake))
    21  		Expect(getPacketTypeFromEncryptionLevel(protocol.Encryption0RTT)).To(BeEquivalentTo(logging.PacketType0RTT))
    22  		Expect(getPacketTypeFromEncryptionLevel(protocol.Encryption1RTT)).To(BeEquivalentTo(logging.PacketType1RTT))
    23  	})
    24  
    25  	Context("marshalling", func() {
    26  		check := func(hdr *wire.ExtendedHeader, expected map[string]interface{}) {
    27  			buf := &bytes.Buffer{}
    28  			enc := gojay.NewEncoder(buf)
    29  			ExpectWithOffset(1, enc.Encode(transformLongHeader(hdr))).To(Succeed())
    30  			data := buf.Bytes()
    31  			ExpectWithOffset(1, json.Valid(data)).To(BeTrue())
    32  			checkEncoding(data, expected)
    33  		}
    34  
    35  		It("marshals a header with a payload length", func() {
    36  			check(
    37  				&wire.ExtendedHeader{
    38  					PacketNumber: 42,
    39  					Header: wire.Header{
    40  						Type:    protocol.PacketTypeInitial,
    41  						Length:  123,
    42  						Version: protocol.Version(0xdecafbad),
    43  					},
    44  				},
    45  				map[string]interface{}{
    46  					"packet_type":   "initial",
    47  					"packet_number": 42,
    48  					"dcil":          0,
    49  					"scil":          0,
    50  					"version":       "decafbad",
    51  				},
    52  			)
    53  		})
    54  
    55  		It("marshals an Initial with a token", func() {
    56  			check(
    57  				&wire.ExtendedHeader{
    58  					PacketNumber: 4242,
    59  					Header: wire.Header{
    60  						Type:    protocol.PacketTypeInitial,
    61  						Length:  123,
    62  						Version: protocol.Version(0xdecafbad),
    63  						Token:   []byte{0xde, 0xad, 0xbe, 0xef},
    64  					},
    65  				},
    66  				map[string]interface{}{
    67  					"packet_type":   "initial",
    68  					"packet_number": 4242,
    69  					"dcil":          0,
    70  					"scil":          0,
    71  					"version":       "decafbad",
    72  					"token":         map[string]interface{}{"data": "deadbeef"},
    73  				},
    74  			)
    75  		})
    76  
    77  		It("marshals a Retry packet", func() {
    78  			check(
    79  				&wire.ExtendedHeader{
    80  					Header: wire.Header{
    81  						Type:            protocol.PacketTypeRetry,
    82  						SrcConnectionID: protocol.ParseConnectionID([]byte{0x11, 0x22, 0x33, 0x44}),
    83  						Version:         protocol.Version(0xdecafbad),
    84  						Token:           []byte{0xde, 0xad, 0xbe, 0xef},
    85  					},
    86  				},
    87  				map[string]interface{}{
    88  					"packet_type": "retry",
    89  					"dcil":        0,
    90  					"scil":        4,
    91  					"scid":        "11223344",
    92  					"token":       map[string]interface{}{"data": "deadbeef"},
    93  					"version":     "decafbad",
    94  				},
    95  			)
    96  		})
    97  
    98  		It("marshals a packet with packet number 0", func() {
    99  			check(
   100  				&wire.ExtendedHeader{
   101  					PacketNumber: 0,
   102  					Header: wire.Header{
   103  						Type:    protocol.PacketTypeHandshake,
   104  						Version: protocol.Version(0xdecafbad),
   105  					},
   106  				},
   107  				map[string]interface{}{
   108  					"packet_type":   "handshake",
   109  					"packet_number": 0,
   110  					"dcil":          0,
   111  					"scil":          0,
   112  					"version":       "decafbad",
   113  				},
   114  			)
   115  		})
   116  
   117  		It("marshals a header with a source connection ID", func() {
   118  			check(
   119  				&wire.ExtendedHeader{
   120  					PacketNumber: 42,
   121  					Header: wire.Header{
   122  						Type:            protocol.PacketTypeHandshake,
   123  						SrcConnectionID: protocol.ParseConnectionID([]byte{0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}),
   124  						Version:         protocol.Version(0xdecafbad),
   125  					},
   126  				},
   127  				map[string]interface{}{
   128  					"packet_type":   "handshake",
   129  					"packet_number": 42,
   130  					"dcil":          0,
   131  					"scil":          16,
   132  					"scid":          "00112233445566778899aabbccddeeff",
   133  					"version":       "decafbad",
   134  				},
   135  			)
   136  		})
   137  	})
   138  })