github.com/tumi8/quic-go@v0.37.4-tum/crypto_stream_manager_test.go (about)

     1  package quic
     2  
     3  import (
     4  
     5  	"github.com/tumi8/quic-go/noninternal/protocol"
     6  	"github.com/tumi8/quic-go/noninternal/wire"
     7  
     8  	. "github.com/onsi/ginkgo/v2"
     9  	. "github.com/onsi/gomega"
    10  )
    11  
    12  var _ = Describe("Crypto Stream Manager", func() {
    13  	var (
    14  		csm *cryptoStreamManager
    15  		cs  *MockCryptoDataHandler
    16  
    17  		initialStream   *MockCryptoStream
    18  		handshakeStream *MockCryptoStream
    19  		oneRTTStream    *MockCryptoStream
    20  	)
    21  
    22  	BeforeEach(func() {
    23  		initialStream = NewMockCryptoStream(mockCtrl)
    24  		handshakeStream = NewMockCryptoStream(mockCtrl)
    25  		oneRTTStream = NewMockCryptoStream(mockCtrl)
    26  		cs = NewMockCryptoDataHandler(mockCtrl)
    27  		csm = newCryptoStreamManager(cs, initialStream, handshakeStream, oneRTTStream)
    28  	})
    29  
    30  	It("passes messages to the initial stream", func() {
    31  		cf := &wire.CryptoFrame{Data: []byte("foobar")}
    32  		initialStream.EXPECT().HandleCryptoFrame(cf)
    33  		initialStream.EXPECT().GetCryptoData().Return([]byte("foobar"))
    34  		initialStream.EXPECT().GetCryptoData()
    35  		cs.EXPECT().HandleMessage([]byte("foobar"), protocol.EncryptionInitial)
    36  		Expect(csm.HandleCryptoFrame(cf, protocol.EncryptionInitial)).To(Succeed())
    37  	})
    38  
    39  	It("passes messages to the handshake stream", func() {
    40  		cf := &wire.CryptoFrame{Data: []byte("foobar")}
    41  		handshakeStream.EXPECT().HandleCryptoFrame(cf)
    42  		handshakeStream.EXPECT().GetCryptoData().Return([]byte("foobar"))
    43  		handshakeStream.EXPECT().GetCryptoData()
    44  		cs.EXPECT().HandleMessage([]byte("foobar"), protocol.EncryptionHandshake)
    45  		Expect(csm.HandleCryptoFrame(cf, protocol.EncryptionHandshake)).To(Succeed())
    46  	})
    47  
    48  	It("passes messages to the 1-RTT stream", func() {
    49  		cf := &wire.CryptoFrame{Data: []byte("foobar")}
    50  		oneRTTStream.EXPECT().HandleCryptoFrame(cf)
    51  		oneRTTStream.EXPECT().GetCryptoData().Return([]byte("foobar"))
    52  		oneRTTStream.EXPECT().GetCryptoData()
    53  		cs.EXPECT().HandleMessage([]byte("foobar"), protocol.Encryption1RTT)
    54  		Expect(csm.HandleCryptoFrame(cf, protocol.Encryption1RTT)).To(Succeed())
    55  	})
    56  
    57  	It("doesn't call the message handler, if there's no message", func() {
    58  		cf := &wire.CryptoFrame{Data: []byte("foobar")}
    59  		handshakeStream.EXPECT().HandleCryptoFrame(cf)
    60  		handshakeStream.EXPECT().GetCryptoData() // don't return any data to handle
    61  		// don't EXPECT any calls to HandleMessage()
    62  		Expect(csm.HandleCryptoFrame(cf, protocol.EncryptionHandshake)).To(Succeed())
    63  	})
    64  
    65  	It("processes all messages", func() {
    66  		cf := &wire.CryptoFrame{Data: []byte("foobar")}
    67  		handshakeStream.EXPECT().HandleCryptoFrame(cf)
    68  		handshakeStream.EXPECT().GetCryptoData().Return([]byte("foo"))
    69  		handshakeStream.EXPECT().GetCryptoData().Return([]byte("bar"))
    70  		handshakeStream.EXPECT().GetCryptoData()
    71  		cs.EXPECT().HandleMessage([]byte("foo"), protocol.EncryptionHandshake)
    72  		cs.EXPECT().HandleMessage([]byte("bar"), protocol.EncryptionHandshake)
    73  		Expect(csm.HandleCryptoFrame(cf, protocol.EncryptionHandshake)).To(Succeed())
    74  	})
    75  
    76  	It("errors for unknown encryption levels", func() {
    77  		err := csm.HandleCryptoFrame(&wire.CryptoFrame{}, 42)
    78  		Expect(err).To(HaveOccurred())
    79  		Expect(err.Error()).To(ContainSubstring("received CRYPTO frame with unexpected encryption level"))
    80  	})
    81  
    82  	It("drops Initial", func() {
    83  		initialStream.EXPECT().Finish()
    84  		Expect(csm.Drop(protocol.EncryptionInitial)).To(Succeed())
    85  	})
    86  
    87  	It("drops Handshake", func() {
    88  		handshakeStream.EXPECT().Finish()
    89  		Expect(csm.Drop(protocol.EncryptionHandshake)).To(Succeed())
    90  	})
    91  })