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