github.com/MerlinKodo/quic-go@v0.39.2/internal/wire/new_connection_id_frame_test.go (about) 1 package wire 2 3 import ( 4 "bytes" 5 "io" 6 7 "github.com/MerlinKodo/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 an invalid length", func() { 42 data := encodeVarInt(0xdeadbeef) // sequence number 43 data = append(data, encodeVarInt(0xcafe)...) // retire prior to 44 data = append(data, 21) // connection ID length 45 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 46 data = append(data, []byte("deadbeefdecafbad")...) // stateless reset token 47 _, err := parseNewConnectionIDFrame(bytes.NewReader(data), protocol.Version1) 48 Expect(err).To(MatchError(protocol.ErrInvalidConnectionIDLen)) 49 }) 50 51 It("errors on EOFs", func() { 52 data := encodeVarInt(0xdeadbeef) // sequence number 53 data = append(data, encodeVarInt(0xcafe1234)...) // retire prior to 54 data = append(data, 10) // connection ID length 55 data = append(data, []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}...) // connection ID 56 data = append(data, []byte("deadbeefdecafbad")...) // stateless reset token 57 _, err := parseNewConnectionIDFrame(bytes.NewReader(data), protocol.Version1) 58 Expect(err).NotTo(HaveOccurred()) 59 for i := range data { 60 _, err := parseNewConnectionIDFrame(bytes.NewReader(data[:i]), protocol.Version1) 61 Expect(err).To(MatchError(io.EOF)) 62 } 63 }) 64 }) 65 66 Context("when writing", func() { 67 It("writes a sample frame", func() { 68 token := protocol.StatelessResetToken{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15} 69 frame := &NewConnectionIDFrame{ 70 SequenceNumber: 0x1337, 71 RetirePriorTo: 0x42, 72 ConnectionID: protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6}), 73 StatelessResetToken: token, 74 } 75 b, err := frame.Append(nil, protocol.Version1) 76 Expect(err).ToNot(HaveOccurred()) 77 expected := []byte{newConnectionIDFrameType} 78 expected = append(expected, encodeVarInt(0x1337)...) 79 expected = append(expected, encodeVarInt(0x42)...) 80 expected = append(expected, 6) 81 expected = append(expected, []byte{1, 2, 3, 4, 5, 6}...) 82 expected = append(expected, token[:]...) 83 Expect(b).To(Equal(expected)) 84 }) 85 86 It("has the correct length", func() { 87 token := protocol.StatelessResetToken{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15} 88 frame := &NewConnectionIDFrame{ 89 SequenceNumber: 0xdecafbad, 90 RetirePriorTo: 0xdeadbeefcafe, 91 ConnectionID: protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6, 7, 8}), 92 StatelessResetToken: token, 93 } 94 b, err := frame.Append(nil, protocol.Version1) 95 Expect(err).ToNot(HaveOccurred()) 96 Expect(b).To(HaveLen(int(frame.Length(protocol.Version1)))) 97 }) 98 }) 99 })