github.com/quic-go/quic-go@v0.44.0/internal/wire/new_connection_id_frame_test.go (about)

     1  package wire
     2  
     3  import (
     4  	"io"
     5  
     6  	"github.com/quic-go/quic-go/internal/protocol"
     7  
     8  	. "github.com/onsi/ginkgo/v2"
     9  	. "github.com/onsi/gomega"
    10  )
    11  
    12  var _ = Describe("NEW_CONNECTION_ID frame", func() {
    13  	Context("when parsing", func() {
    14  		It("accepts a sample frame", func() {
    15  			data := encodeVarInt(0xdeadbeef)                              // sequence number
    16  			data = append(data, encodeVarInt(0xcafe)...)                  // retire prior to
    17  			data = append(data, 10)                                       // connection ID length
    18  			data = append(data, []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}...) // connection ID
    19  			data = append(data, []byte("deadbeefdecafbad")...)            // stateless reset token
    20  			frame, l, err := parseNewConnectionIDFrame(data, protocol.Version1)
    21  			Expect(err).ToNot(HaveOccurred())
    22  			Expect(frame.SequenceNumber).To(Equal(uint64(0xdeadbeef)))
    23  			Expect(frame.RetirePriorTo).To(Equal(uint64(0xcafe)))
    24  			Expect(frame.ConnectionID).To(Equal(protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})))
    25  			Expect(string(frame.StatelessResetToken[:])).To(Equal("deadbeefdecafbad"))
    26  			Expect(l).To(Equal(len(data)))
    27  		})
    28  
    29  		It("errors when the Retire Prior To value is larger than the Sequence Number", func() {
    30  			data := encodeVarInt(1000)                 // sequence number
    31  			data = append(data, encodeVarInt(1001)...) // retire prior to
    32  			data = append(data, 3)
    33  			data = append(data, []byte{1, 2, 3}...)
    34  			data = append(data, []byte("deadbeefdecafbad")...) // stateless reset token
    35  			_, _, err := parseNewConnectionIDFrame(data, protocol.Version1)
    36  			Expect(err).To(MatchError("Retire Prior To value (1001) larger than Sequence Number (1000)"))
    37  		})
    38  
    39  		It("errors when the connection ID has a zero-length connection ID", func() {
    40  			data := encodeVarInt(42)                 // sequence number
    41  			data = append(data, encodeVarInt(12)...) // retire prior to
    42  			data = append(data, 0)                   // connection ID length
    43  			_, _, err := parseNewConnectionIDFrame(data, protocol.Version1)
    44  			Expect(err).To(MatchError("invalid zero-length connection ID"))
    45  		})
    46  
    47  		It("errors when the connection ID has an invalid length (too long)", func() {
    48  			data := encodeVarInt(0xdeadbeef)                                                                          // sequence number
    49  			data = append(data, encodeVarInt(0xcafe)...)                                                              // retire prior to
    50  			data = append(data, 21)                                                                                   // connection ID length
    51  			data = append(data, []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21}...) // connection ID
    52  			data = append(data, []byte("deadbeefdecafbad")...)                                                        // stateless reset token
    53  			_, _, err := parseNewConnectionIDFrame(data, protocol.Version1)
    54  			Expect(err).To(MatchError(protocol.ErrInvalidConnectionIDLen))
    55  		})
    56  
    57  		It("errors on EOFs", func() {
    58  			data := encodeVarInt(0xdeadbeef)                              // sequence number
    59  			data = append(data, encodeVarInt(0xcafe1234)...)              // retire prior to
    60  			data = append(data, 10)                                       // connection ID length
    61  			data = append(data, []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}...) // connection ID
    62  			data = append(data, []byte("deadbeefdecafbad")...)            // stateless reset token
    63  			_, l, err := parseNewConnectionIDFrame(data, protocol.Version1)
    64  			Expect(err).NotTo(HaveOccurred())
    65  			Expect(l).To(Equal(len(data)))
    66  			for i := range data {
    67  				_, _, err := parseNewConnectionIDFrame(data[:i], protocol.Version1)
    68  				Expect(err).To(MatchError(io.EOF))
    69  			}
    70  		})
    71  	})
    72  
    73  	Context("when writing", func() {
    74  		It("writes a sample frame", func() {
    75  			token := protocol.StatelessResetToken{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
    76  			frame := &NewConnectionIDFrame{
    77  				SequenceNumber:      0x1337,
    78  				RetirePriorTo:       0x42,
    79  				ConnectionID:        protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6}),
    80  				StatelessResetToken: token,
    81  			}
    82  			b, err := frame.Append(nil, protocol.Version1)
    83  			Expect(err).ToNot(HaveOccurred())
    84  			expected := []byte{newConnectionIDFrameType}
    85  			expected = append(expected, encodeVarInt(0x1337)...)
    86  			expected = append(expected, encodeVarInt(0x42)...)
    87  			expected = append(expected, 6)
    88  			expected = append(expected, []byte{1, 2, 3, 4, 5, 6}...)
    89  			expected = append(expected, token[:]...)
    90  			Expect(b).To(Equal(expected))
    91  		})
    92  
    93  		It("has the correct length", func() {
    94  			token := protocol.StatelessResetToken{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
    95  			frame := &NewConnectionIDFrame{
    96  				SequenceNumber:      0xdecafbad,
    97  				RetirePriorTo:       0xdeadbeefcafe,
    98  				ConnectionID:        protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6, 7, 8}),
    99  				StatelessResetToken: token,
   100  			}
   101  			b, err := frame.Append(nil, protocol.Version1)
   102  			Expect(err).ToNot(HaveOccurred())
   103  			Expect(b).To(HaveLen(int(frame.Length(protocol.Version1))))
   104  		})
   105  	})
   106  })