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