github.com/metacubex/quic-go@v0.44.1-0.20240520163451-20b689a59136/internal/wire/new_token_frame_test.go (about) 1 package wire 2 3 import ( 4 "io" 5 6 "github.com/metacubex/quic-go/internal/protocol" 7 "github.com/metacubex/quic-go/quicvarint" 8 9 . "github.com/onsi/ginkgo/v2" 10 . "github.com/onsi/gomega" 11 ) 12 13 var _ = Describe("NEW_TOKEN frame", func() { 14 Context("parsing", func() { 15 It("accepts a sample frame", func() { 16 token := "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." 17 data := encodeVarInt(uint64(len(token))) 18 data = append(data, token...) 19 f, l, err := parseNewTokenFrame(data, protocol.Version1) 20 Expect(err).ToNot(HaveOccurred()) 21 Expect(string(f.Token)).To(Equal(token)) 22 Expect(l).To(Equal(len(data))) 23 }) 24 25 It("rejects empty tokens", func() { 26 data := encodeVarInt(0) 27 _, _, err := parseNewTokenFrame(data, protocol.Version1) 28 Expect(err).To(MatchError("token must not be empty")) 29 }) 30 31 It("errors on EOFs", func() { 32 token := "Lorem ipsum dolor sit amet, consectetur adipiscing elit" 33 data := encodeVarInt(uint64(len(token))) 34 data = append(data, token...) 35 _, l, err := parseNewTokenFrame(data, protocol.Version1) 36 Expect(err).NotTo(HaveOccurred()) 37 Expect(l).To(Equal(len(data))) 38 for i := range data { 39 _, _, err := parseNewTokenFrame(data[:i], protocol.Version1) 40 Expect(err).To(MatchError(io.EOF)) 41 } 42 }) 43 }) 44 45 Context("writing", func() { 46 It("writes a sample frame", func() { 47 token := "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat." 48 f := &NewTokenFrame{Token: []byte(token)} 49 b, err := f.Append(nil, protocol.Version1) 50 Expect(err).ToNot(HaveOccurred()) 51 expected := []byte{newTokenFrameType} 52 expected = append(expected, encodeVarInt(uint64(len(token)))...) 53 expected = append(expected, token...) 54 Expect(b).To(Equal(expected)) 55 }) 56 57 It("has the correct min length", func() { 58 frame := &NewTokenFrame{Token: []byte("foobar")} 59 Expect(frame.Length(protocol.Version1)).To(BeEquivalentTo(1 + quicvarint.Len(6) + 6)) 60 }) 61 }) 62 })