github.com/daeuniverse/quic-go@v0.0.0-20240413031024-943f218e0810/internal/handshake/session_ticket_test.go (about)

     1  package handshake
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/daeuniverse/quic-go/internal/wire"
     7  	"github.com/daeuniverse/quic-go/quicvarint"
     8  
     9  	. "github.com/onsi/ginkgo/v2"
    10  	. "github.com/onsi/gomega"
    11  )
    12  
    13  var _ = Describe("Session Ticket", func() {
    14  	It("marshals and unmarshals a 0-RTT session ticket", func() {
    15  		ticket := &sessionTicket{
    16  			Parameters: &wire.TransportParameters{
    17  				InitialMaxStreamDataBidiLocal:  1,
    18  				InitialMaxStreamDataBidiRemote: 2,
    19  				ActiveConnectionIDLimit:        10,
    20  				MaxDatagramFrameSize:           20,
    21  			},
    22  			RTT: 1337 * time.Microsecond,
    23  		}
    24  		var t sessionTicket
    25  		Expect(t.Unmarshal(ticket.Marshal(), true)).To(Succeed())
    26  		Expect(t.Parameters.InitialMaxStreamDataBidiLocal).To(BeEquivalentTo(1))
    27  		Expect(t.Parameters.InitialMaxStreamDataBidiRemote).To(BeEquivalentTo(2))
    28  		Expect(t.Parameters.ActiveConnectionIDLimit).To(BeEquivalentTo(10))
    29  		Expect(t.Parameters.MaxDatagramFrameSize).To(BeEquivalentTo(20))
    30  		Expect(t.RTT).To(Equal(1337 * time.Microsecond))
    31  		// fails to unmarshal the ticket as a non-0-RTT ticket
    32  		Expect(t.Unmarshal(ticket.Marshal(), false)).To(MatchError("the session ticket has more bytes than expected"))
    33  	})
    34  
    35  	It("marshals and unmarshals a non-0-RTT session ticket", func() {
    36  		ticket := &sessionTicket{
    37  			RTT: 1337 * time.Microsecond,
    38  		}
    39  		var t sessionTicket
    40  		Expect(t.Unmarshal(ticket.Marshal(), false)).To(Succeed())
    41  		Expect(t.Parameters).To(BeNil())
    42  		Expect(t.RTT).To(Equal(1337 * time.Microsecond))
    43  		// fails to unmarshal the ticket as a 0-RTT ticket
    44  		Expect(t.Unmarshal(ticket.Marshal(), true)).To(MatchError(ContainSubstring("unmarshaling transport parameters from session ticket failed")))
    45  	})
    46  
    47  	It("refuses to unmarshal if the ticket is too short for the revision", func() {
    48  		Expect((&sessionTicket{}).Unmarshal([]byte{}, true)).To(MatchError("failed to read session ticket revision"))
    49  		Expect((&sessionTicket{}).Unmarshal([]byte{}, false)).To(MatchError("failed to read session ticket revision"))
    50  	})
    51  
    52  	It("refuses to unmarshal if the revision doesn't match", func() {
    53  		b := quicvarint.Append(nil, 1337)
    54  		Expect((&sessionTicket{}).Unmarshal(b, true)).To(MatchError("unknown session ticket revision: 1337"))
    55  		Expect((&sessionTicket{}).Unmarshal(b, false)).To(MatchError("unknown session ticket revision: 1337"))
    56  	})
    57  
    58  	It("refuses to unmarshal if the RTT cannot be read", func() {
    59  		b := quicvarint.Append(nil, sessionTicketRevision)
    60  		Expect((&sessionTicket{}).Unmarshal(b, true)).To(MatchError("failed to read RTT"))
    61  		Expect((&sessionTicket{}).Unmarshal(b, false)).To(MatchError("failed to read RTT"))
    62  	})
    63  
    64  	It("refuses to unmarshal a 0-RTT session ticket if unmarshaling the transport parameters fails", func() {
    65  		b := quicvarint.Append(nil, sessionTicketRevision)
    66  		b = append(b, []byte("foobar")...)
    67  		err := (&sessionTicket{}).Unmarshal(b, true)
    68  		Expect(err).To(HaveOccurred())
    69  		Expect(err.Error()).To(ContainSubstring("unmarshaling transport parameters from session ticket failed"))
    70  	})
    71  
    72  	It("refuses to unmarshal if the non-0-RTT session ticket has more bytes than expected", func() {
    73  		ticket := &sessionTicket{
    74  			Parameters: &wire.TransportParameters{
    75  				InitialMaxStreamDataBidiLocal:  1,
    76  				InitialMaxStreamDataBidiRemote: 2,
    77  				ActiveConnectionIDLimit:        10,
    78  				MaxDatagramFrameSize:           20,
    79  			},
    80  			RTT: 1234 * time.Microsecond,
    81  		}
    82  		err := (&sessionTicket{}).Unmarshal(ticket.Marshal(), false)
    83  		Expect(err).To(HaveOccurred())
    84  		Expect(err.Error()).To(ContainSubstring("the session ticket has more bytes than expected"))
    85  	})
    86  })