use a mock crypto stream in the crypto stream manager tests

This commit is contained in:
Marten Seemann 2018-10-20 11:13:17 +09:00
parent 19e5feef57
commit fe442e4d19

View file

@ -12,40 +12,53 @@ var _ = Describe("Crypto Stream Manager", func() {
var ( var (
csm *cryptoStreamManager csm *cryptoStreamManager
cs *MockCryptoDataHandler cs *MockCryptoDataHandler
initialStream *MockCryptoStream
handshakeStream *MockCryptoStream
) )
BeforeEach(func() { BeforeEach(func() {
initialStream := newCryptoStream() initialStream = NewMockCryptoStream(mockCtrl)
handshakeStream := newCryptoStream() handshakeStream = NewMockCryptoStream(mockCtrl)
cs = NewMockCryptoDataHandler(mockCtrl) cs = NewMockCryptoDataHandler(mockCtrl)
csm = newCryptoStreamManager(cs, initialStream, handshakeStream) csm = newCryptoStreamManager(cs, initialStream, handshakeStream)
}) })
It("passes messages to the right stream", func() { It("passes messages to the initial stream", func() {
initialMsg := createHandshakeMessage(10) cf := &wire.CryptoFrame{Data: []byte("foobar")}
handshakeMsg := createHandshakeMessage(20) initialStream.EXPECT().HandleCryptoFrame(cf)
initialStream.EXPECT().GetCryptoData().Return([]byte("foobar"))
initialStream.EXPECT().GetCryptoData()
cs.EXPECT().HandleMessage([]byte("foobar"), protocol.EncryptionInitial)
Expect(csm.HandleCryptoFrame(cf, protocol.EncryptionInitial)).To(Succeed())
})
// only pass in a part of the message, to make sure they get assembled in the right crypto stream It("passes messages to the handshake stream", func() {
Expect(csm.HandleCryptoFrame(&wire.CryptoFrame{ cf := &wire.CryptoFrame{Data: []byte("foobar")}
Data: initialMsg[:5], handshakeStream.EXPECT().HandleCryptoFrame(cf)
}, protocol.EncryptionInitial)).To(Succeed()) handshakeStream.EXPECT().GetCryptoData().Return([]byte("foobar"))
Expect(csm.HandleCryptoFrame(&wire.CryptoFrame{ handshakeStream.EXPECT().GetCryptoData()
Data: handshakeMsg[:5], cs.EXPECT().HandleMessage([]byte("foobar"), protocol.EncryptionHandshake)
}, protocol.EncryptionHandshake)).To(Succeed()) Expect(csm.HandleCryptoFrame(cf, protocol.EncryptionHandshake)).To(Succeed())
})
// now pass in the rest of the initial message It("doesn't call the message handler, if there's no message", func() {
cs.EXPECT().HandleMessage(initialMsg, protocol.EncryptionInitial) cf := &wire.CryptoFrame{Data: []byte("foobar")}
Expect(csm.HandleCryptoFrame(&wire.CryptoFrame{ handshakeStream.EXPECT().HandleCryptoFrame(cf)
Data: initialMsg[5:], handshakeStream.EXPECT().GetCryptoData() // don't return any data to handle
Offset: 5, // don't EXPECT any calls to HandleMessage()
}, protocol.EncryptionInitial)).To(Succeed()) Expect(csm.HandleCryptoFrame(cf, protocol.EncryptionHandshake)).To(Succeed())
})
// now pass in the rest of the handshake message It("processes all messages", func() {
cs.EXPECT().HandleMessage(handshakeMsg, protocol.EncryptionHandshake) cf := &wire.CryptoFrame{Data: []byte("foobar")}
Expect(csm.HandleCryptoFrame(&wire.CryptoFrame{ handshakeStream.EXPECT().HandleCryptoFrame(cf)
Data: handshakeMsg[5:], handshakeStream.EXPECT().GetCryptoData().Return([]byte("foo"))
Offset: 5, handshakeStream.EXPECT().GetCryptoData().Return([]byte("bar"))
}, protocol.EncryptionHandshake)).To(Succeed()) handshakeStream.EXPECT().GetCryptoData()
cs.EXPECT().HandleMessage([]byte("foo"), protocol.EncryptionHandshake)
cs.EXPECT().HandleMessage([]byte("bar"), protocol.EncryptionHandshake)
Expect(csm.HandleCryptoFrame(cf, protocol.EncryptionHandshake)).To(Succeed())
}) })
It("errors for unknown encryption levels", func() { It("errors for unknown encryption levels", func() {