github.com/tumi8/quic-go@v0.37.4-tum/noninternal/wire/connection_close_frame_test.go (about) 1 package wire 2 3 import ( 4 "bytes" 5 "io" 6 7 8 "github.com/tumi8/quic-go/noninternal/protocol" 9 10 . "github.com/onsi/ginkgo/v2" 11 . "github.com/onsi/gomega" 12 ) 13 14 var _ = Describe("CONNECTION_CLOSE Frame", func() { 15 Context("when parsing", func() { 16 It("accepts sample frame containing a QUIC error code", func() { 17 reason := "No recent network activity." 18 data := encodeVarInt(0x19) 19 data = append(data, encodeVarInt(0x1337)...) // frame type 20 data = append(data, encodeVarInt(uint64(len(reason)))...) // reason phrase length 21 data = append(data, []byte(reason)...) 22 b := bytes.NewReader(data) 23 frame, err := parseConnectionCloseFrame(b, connectionCloseFrameType, protocol.Version1) 24 Expect(err).ToNot(HaveOccurred()) 25 Expect(frame.IsApplicationError).To(BeFalse()) 26 Expect(frame.ErrorCode).To(BeEquivalentTo(0x19)) 27 Expect(frame.FrameType).To(BeEquivalentTo(0x1337)) 28 Expect(frame.ReasonPhrase).To(Equal(reason)) 29 Expect(b.Len()).To(BeZero()) 30 }) 31 32 It("accepts sample frame containing an application error code", func() { 33 reason := "The application messed things up." 34 data := encodeVarInt(0xcafe) 35 data = append(data, encodeVarInt(uint64(len(reason)))...) // reason phrase length 36 data = append(data, reason...) 37 b := bytes.NewReader(data) 38 frame, err := parseConnectionCloseFrame(b, applicationCloseFrameType, protocol.Version1) 39 Expect(err).ToNot(HaveOccurred()) 40 Expect(frame.IsApplicationError).To(BeTrue()) 41 Expect(frame.ErrorCode).To(BeEquivalentTo(0xcafe)) 42 Expect(frame.ReasonPhrase).To(Equal(reason)) 43 Expect(b.Len()).To(BeZero()) 44 }) 45 46 It("rejects long reason phrases", func() { 47 data := encodeVarInt(0xcafe) 48 data = append(data, encodeVarInt(0x42)...) // frame type 49 data = append(data, encodeVarInt(0xffff)...) // reason phrase length 50 _, err := parseConnectionCloseFrame(bytes.NewReader(data), connectionCloseFrameType, protocol.Version1) 51 Expect(err).To(MatchError(io.EOF)) 52 }) 53 54 It("errors on EOFs", func() { 55 reason := "No recent network activity." 56 data := encodeVarInt(0x19) 57 data = append(data, encodeVarInt(0x1337)...) // frame type 58 data = append(data, encodeVarInt(uint64(len(reason)))...) // reason phrase length 59 data = append(data, []byte(reason)...) 60 b := bytes.NewReader(data) 61 _, err := parseConnectionCloseFrame(b, connectionCloseFrameType, protocol.Version1) 62 Expect(err).NotTo(HaveOccurred()) 63 for i := range data { 64 b := bytes.NewReader(data[:i]) 65 _, err = parseConnectionCloseFrame(b, connectionCloseFrameType, protocol.Version1) 66 Expect(err).To(MatchError(io.EOF)) 67 } 68 }) 69 70 It("parses a frame without a reason phrase", func() { 71 data := encodeVarInt(0xcafe) 72 data = append(data, encodeVarInt(0x42)...) // frame type 73 data = append(data, encodeVarInt(0)...) 74 b := bytes.NewReader(data) 75 frame, err := parseConnectionCloseFrame(b, connectionCloseFrameType, protocol.Version1) 76 Expect(err).ToNot(HaveOccurred()) 77 Expect(frame.ReasonPhrase).To(BeEmpty()) 78 Expect(b.Len()).To(BeZero()) 79 }) 80 }) 81 82 Context("when writing", func() { 83 It("writes a frame without a reason phrase", func() { 84 frame := &ConnectionCloseFrame{ 85 ErrorCode: 0xbeef, 86 FrameType: 0x12345, 87 } 88 b, err := frame.Append(nil, protocol.Version1) 89 Expect(err).ToNot(HaveOccurred()) 90 expected := []byte{connectionCloseFrameType} 91 expected = append(expected, encodeVarInt(0xbeef)...) 92 expected = append(expected, encodeVarInt(0x12345)...) // frame type 93 expected = append(expected, encodeVarInt(0)...) // reason phrase length 94 Expect(b).To(Equal(expected)) 95 }) 96 97 It("writes a frame with a reason phrase", func() { 98 frame := &ConnectionCloseFrame{ 99 ErrorCode: 0xdead, 100 ReasonPhrase: "foobar", 101 } 102 b, err := frame.Append(nil, protocol.Version1) 103 Expect(err).ToNot(HaveOccurred()) 104 expected := []byte{connectionCloseFrameType} 105 expected = append(expected, encodeVarInt(0xdead)...) 106 expected = append(expected, encodeVarInt(0)...) // frame type 107 expected = append(expected, encodeVarInt(6)...) // reason phrase length 108 expected = append(expected, []byte("foobar")...) 109 Expect(b).To(Equal(expected)) 110 }) 111 112 It("writes a frame with an application error code", func() { 113 frame := &ConnectionCloseFrame{ 114 IsApplicationError: true, 115 ErrorCode: 0xdead, 116 ReasonPhrase: "foobar", 117 } 118 b, err := frame.Append(nil, protocol.Version1) 119 Expect(err).ToNot(HaveOccurred()) 120 expected := []byte{applicationCloseFrameType} 121 expected = append(expected, encodeVarInt(0xdead)...) 122 expected = append(expected, encodeVarInt(6)...) // reason phrase length 123 expected = append(expected, []byte("foobar")...) 124 Expect(b).To(Equal(expected)) 125 }) 126 127 It("has proper min length, for a frame containing a QUIC error code", func() { 128 f := &ConnectionCloseFrame{ 129 ErrorCode: 0xcafe, 130 FrameType: 0xdeadbeef, 131 ReasonPhrase: "foobar", 132 } 133 b, err := f.Append(nil, protocol.Version1) 134 Expect(err).ToNot(HaveOccurred()) 135 Expect(b).To(HaveLen(int(f.Length(protocol.Version1)))) 136 }) 137 138 It("has proper min length, for a frame containing an application error code", func() { 139 f := &ConnectionCloseFrame{ 140 IsApplicationError: true, 141 ErrorCode: 0xcafe, 142 ReasonPhrase: "foobar", 143 } 144 b, err := f.Append(nil, protocol.Version1) 145 Expect(err).ToNot(HaveOccurred()) 146 Expect(b).To(HaveLen(int(f.Length(protocol.Version1)))) 147 }) 148 }) 149 })