crypto_stream_manager_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package quic
  2. import (
  3. "errors"
  4. "github.com/golang/mock/gomock"
  5. "github.com/lucas-clemente/quic-go/internal/protocol"
  6. "github.com/lucas-clemente/quic-go/internal/wire"
  7. . "github.com/onsi/ginkgo"
  8. . "github.com/onsi/gomega"
  9. )
  10. var _ = Describe("Crypto Stream Manager", func() {
  11. var (
  12. csm *cryptoStreamManager
  13. cs *MockCryptoDataHandler
  14. initialStream *MockCryptoStream
  15. handshakeStream *MockCryptoStream
  16. oneRTTStream *MockCryptoStream
  17. )
  18. BeforeEach(func() {
  19. initialStream = NewMockCryptoStream(mockCtrl)
  20. handshakeStream = NewMockCryptoStream(mockCtrl)
  21. oneRTTStream = NewMockCryptoStream(mockCtrl)
  22. cs = NewMockCryptoDataHandler(mockCtrl)
  23. csm = newCryptoStreamManager(cs, initialStream, handshakeStream, oneRTTStream)
  24. })
  25. It("passes messages to the initial stream", func() {
  26. cf := &wire.CryptoFrame{Data: []byte("foobar")}
  27. initialStream.EXPECT().HandleCryptoFrame(cf)
  28. initialStream.EXPECT().GetCryptoData().Return([]byte("foobar"))
  29. initialStream.EXPECT().GetCryptoData()
  30. cs.EXPECT().HandleMessage([]byte("foobar"), protocol.EncryptionInitial)
  31. encLevelChanged, err := csm.HandleCryptoFrame(cf, protocol.EncryptionInitial)
  32. Expect(err).ToNot(HaveOccurred())
  33. Expect(encLevelChanged).To(BeFalse())
  34. })
  35. It("passes messages to the handshake stream", func() {
  36. cf := &wire.CryptoFrame{Data: []byte("foobar")}
  37. handshakeStream.EXPECT().HandleCryptoFrame(cf)
  38. handshakeStream.EXPECT().GetCryptoData().Return([]byte("foobar"))
  39. handshakeStream.EXPECT().GetCryptoData()
  40. cs.EXPECT().HandleMessage([]byte("foobar"), protocol.EncryptionHandshake)
  41. encLevelChanged, err := csm.HandleCryptoFrame(cf, protocol.EncryptionHandshake)
  42. Expect(err).ToNot(HaveOccurred())
  43. Expect(encLevelChanged).To(BeFalse())
  44. })
  45. It("passes messages to the 1-RTT stream", func() {
  46. cf := &wire.CryptoFrame{Data: []byte("foobar")}
  47. oneRTTStream.EXPECT().HandleCryptoFrame(cf)
  48. oneRTTStream.EXPECT().GetCryptoData().Return([]byte("foobar"))
  49. oneRTTStream.EXPECT().GetCryptoData()
  50. cs.EXPECT().HandleMessage([]byte("foobar"), protocol.Encryption1RTT)
  51. encLevelChanged, err := csm.HandleCryptoFrame(cf, protocol.Encryption1RTT)
  52. Expect(err).ToNot(HaveOccurred())
  53. Expect(encLevelChanged).To(BeFalse())
  54. })
  55. It("doesn't call the message handler, if there's no message", func() {
  56. cf := &wire.CryptoFrame{Data: []byte("foobar")}
  57. handshakeStream.EXPECT().HandleCryptoFrame(cf)
  58. handshakeStream.EXPECT().GetCryptoData() // don't return any data to handle
  59. // don't EXPECT any calls to HandleMessage()
  60. encLevelChanged, err := csm.HandleCryptoFrame(cf, protocol.EncryptionHandshake)
  61. Expect(err).ToNot(HaveOccurred())
  62. Expect(encLevelChanged).To(BeFalse())
  63. })
  64. It("processes all messages", func() {
  65. cf := &wire.CryptoFrame{Data: []byte("foobar")}
  66. handshakeStream.EXPECT().HandleCryptoFrame(cf)
  67. handshakeStream.EXPECT().GetCryptoData().Return([]byte("foo"))
  68. handshakeStream.EXPECT().GetCryptoData().Return([]byte("bar"))
  69. handshakeStream.EXPECT().GetCryptoData()
  70. cs.EXPECT().HandleMessage([]byte("foo"), protocol.EncryptionHandshake)
  71. cs.EXPECT().HandleMessage([]byte("bar"), protocol.EncryptionHandshake)
  72. encLevelChanged, err := csm.HandleCryptoFrame(cf, protocol.EncryptionHandshake)
  73. Expect(err).ToNot(HaveOccurred())
  74. Expect(encLevelChanged).To(BeFalse())
  75. })
  76. It("finishes the crypto stream, when the crypto setup is done with this encryption level", func() {
  77. cf := &wire.CryptoFrame{Data: []byte("foobar")}
  78. gomock.InOrder(
  79. handshakeStream.EXPECT().HandleCryptoFrame(cf),
  80. handshakeStream.EXPECT().GetCryptoData().Return([]byte("foobar")),
  81. cs.EXPECT().HandleMessage([]byte("foobar"), protocol.EncryptionHandshake).Return(true),
  82. handshakeStream.EXPECT().Finish(),
  83. )
  84. encLevelChanged, err := csm.HandleCryptoFrame(cf, protocol.EncryptionHandshake)
  85. Expect(err).ToNot(HaveOccurred())
  86. Expect(encLevelChanged).To(BeTrue())
  87. })
  88. It("returns errors that occur when finishing a stream", func() {
  89. testErr := errors.New("test error")
  90. cf := &wire.CryptoFrame{Data: []byte("foobar")}
  91. gomock.InOrder(
  92. handshakeStream.EXPECT().HandleCryptoFrame(cf),
  93. handshakeStream.EXPECT().GetCryptoData().Return([]byte("foobar")),
  94. cs.EXPECT().HandleMessage([]byte("foobar"), protocol.EncryptionHandshake).Return(true),
  95. handshakeStream.EXPECT().Finish().Return(testErr),
  96. )
  97. _, err := csm.HandleCryptoFrame(cf, protocol.EncryptionHandshake)
  98. Expect(err).To(MatchError(err))
  99. })
  100. It("errors for unknown encryption levels", func() {
  101. _, err := csm.HandleCryptoFrame(&wire.CryptoFrame{}, 42)
  102. Expect(err).To(HaveOccurred())
  103. Expect(err.Error()).To(ContainSubstring("received CRYPTO frame with unexpected encryption level"))
  104. })
  105. })