route/vendor/github.com/lucas-clemente/quic-go/session_test.go

1525 lines
52 KiB
Go
Raw Normal View History

2017-12-12 02:51:45 +00:00
package quic
import (
"bytes"
2018-01-03 19:19:49 +00:00
"context"
"crypto/tls"
2017-12-12 02:51:45 +00:00
"errors"
"io"
"net"
"runtime/pprof"
"strings"
"time"
2018-01-03 19:19:49 +00:00
"github.com/golang/mock/gomock"
2017-12-12 02:51:45 +00:00
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/lucas-clemente/quic-go/ackhandler"
2018-01-03 19:19:49 +00:00
"github.com/lucas-clemente/quic-go/internal/crypto"
"github.com/lucas-clemente/quic-go/internal/handshake"
"github.com/lucas-clemente/quic-go/internal/mocks"
2018-01-20 18:07:01 +00:00
"github.com/lucas-clemente/quic-go/internal/mocks/ackhandler"
2018-01-03 19:19:49 +00:00
"github.com/lucas-clemente/quic-go/internal/protocol"
"github.com/lucas-clemente/quic-go/internal/testdata"
"github.com/lucas-clemente/quic-go/internal/wire"
2017-12-12 02:51:45 +00:00
"github.com/lucas-clemente/quic-go/qerr"
)
type mockConnection struct {
remoteAddr net.Addr
localAddr net.Addr
2018-01-03 19:19:49 +00:00
written chan []byte
}
func newMockConnection() *mockConnection {
return &mockConnection{
remoteAddr: &net.UDPAddr{},
written: make(chan []byte, 100),
}
2017-12-12 02:51:45 +00:00
}
func (m *mockConnection) Write(p []byte) error {
b := make([]byte, len(p))
copy(b, p)
2018-01-03 19:19:49 +00:00
select {
case m.written <- b:
default:
panic("mockConnection channel full")
}
2017-12-12 02:51:45 +00:00
return nil
}
func (m *mockConnection) Read([]byte) (int, net.Addr, error) { panic("not implemented") }
func (m *mockConnection) SetCurrentRemoteAddr(addr net.Addr) {
m.remoteAddr = addr
}
func (m *mockConnection) LocalAddr() net.Addr { return m.localAddr }
func (m *mockConnection) RemoteAddr() net.Addr { return m.remoteAddr }
func (*mockConnection) Close() error { panic("not implemented") }
type mockUnpacker struct {
unpackErr error
}
2018-01-03 19:19:49 +00:00
func (m *mockUnpacker) Unpack(headerBinary []byte, hdr *wire.Header, data []byte) (*unpackedPacket, error) {
2017-12-12 02:51:45 +00:00
if m.unpackErr != nil {
return nil, m.unpackErr
}
return &unpackedPacket{
frames: nil,
}, nil
}
func areSessionsRunning() bool {
var b bytes.Buffer
pprof.Lookup("goroutine").WriteTo(&b, 1)
return strings.Contains(b.String(), "quic-go.(*session).run")
}
var _ = Describe("Session", func() {
var (
2018-01-20 18:07:01 +00:00
sess *session
scfg *handshake.ServerConfig
mconn *mockConnection
cryptoSetup *mockCryptoSetup
streamManager *MockStreamManager
handshakeChan chan<- struct{}
2017-12-12 02:51:45 +00:00
)
BeforeEach(func() {
Eventually(areSessionsRunning).Should(BeFalse())
2018-01-03 19:19:49 +00:00
cryptoSetup = &mockCryptoSetup{}
newCryptoSetup = func(
_ io.ReadWriter,
_ protocol.ConnectionID,
_ net.Addr,
_ protocol.VersionNumber,
_ *handshake.ServerConfig,
_ *handshake.TransportParameters,
_ []protocol.VersionNumber,
_ func(net.Addr, *Cookie) bool,
_ chan<- handshake.TransportParameters,
2018-01-20 18:07:01 +00:00
handshakeChanP chan<- struct{},
2018-01-03 19:19:49 +00:00
) (handshake.CryptoSetup, error) {
2018-01-20 18:07:01 +00:00
handshakeChan = handshakeChanP
2018-01-03 19:19:49 +00:00
return cryptoSetup, nil
2017-12-12 02:51:45 +00:00
}
2018-01-03 19:19:49 +00:00
mconn = newMockConnection()
2017-12-12 02:51:45 +00:00
certChain := crypto.NewCertChain(testdata.GetTLSConfig())
kex, err := crypto.NewCurve25519KEX()
Expect(err).NotTo(HaveOccurred())
scfg, err = handshake.NewServerConfig(kex, certChain)
Expect(err).NotTo(HaveOccurred())
2018-01-03 19:19:49 +00:00
var pSess Session
pSess, err = newSession(
2017-12-12 02:51:45 +00:00
mconn,
2018-01-03 19:19:49 +00:00
protocol.Version39,
2017-12-12 02:51:45 +00:00
0,
scfg,
2018-01-03 19:19:49 +00:00
nil,
populateServerConfig(&Config{}),
2017-12-12 02:51:45 +00:00
)
Expect(err).NotTo(HaveOccurred())
sess = pSess.(*session)
2018-01-20 18:07:01 +00:00
streamManager = NewMockStreamManager(mockCtrl)
sess.streamsMap = streamManager
2017-12-12 02:51:45 +00:00
})
AfterEach(func() {
2018-01-03 19:19:49 +00:00
newCryptoSetup = handshake.NewCryptoSetup
2017-12-12 02:51:45 +00:00
Eventually(areSessionsRunning).Should(BeFalse())
})
2018-01-03 19:19:49 +00:00
Context("source address validation", func() {
var (
cookieVerify func(net.Addr, *Cookie) bool
paramClientAddr net.Addr
paramCookie *Cookie
)
remoteAddr := &net.UDPAddr{IP: net.IPv4(192, 168, 13, 37), Port: 1000}
2017-12-12 02:51:45 +00:00
2018-01-03 19:19:49 +00:00
BeforeEach(func() {
newCryptoSetup = func(
_ io.ReadWriter,
_ protocol.ConnectionID,
_ net.Addr,
_ protocol.VersionNumber,
_ *handshake.ServerConfig,
_ *handshake.TransportParameters,
_ []protocol.VersionNumber,
cookieFunc func(net.Addr, *Cookie) bool,
_ chan<- handshake.TransportParameters,
2018-01-20 18:07:01 +00:00
_ chan<- struct{},
2018-01-03 19:19:49 +00:00
) (handshake.CryptoSetup, error) {
cookieVerify = cookieFunc
return cryptoSetup, nil
}
conf := populateServerConfig(&Config{})
conf.AcceptCookie = func(clientAddr net.Addr, cookie *Cookie) bool {
paramClientAddr = clientAddr
paramCookie = cookie
return false
2017-12-12 02:51:45 +00:00
}
2018-01-03 19:19:49 +00:00
pSess, err := newSession(
mconn,
protocol.Version39,
2017-12-12 02:51:45 +00:00
0,
scfg,
2018-01-03 19:19:49 +00:00
nil,
conf,
2017-12-12 02:51:45 +00:00
)
2018-01-03 19:19:49 +00:00
Expect(err).NotTo(HaveOccurred())
sess = pSess.(*session)
2017-12-12 02:51:45 +00:00
})
2018-01-03 19:19:49 +00:00
It("calls the callback with the right parameters when the client didn't send an STK", func() {
cookieVerify(remoteAddr, nil)
Expect(paramClientAddr).To(Equal(remoteAddr))
Expect(paramCookie).To(BeNil())
2017-12-12 02:51:45 +00:00
})
2018-01-03 19:19:49 +00:00
It("calls the callback with the STK when the client sent an STK", func() {
cookieAddr := &net.UDPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 1337}
sentTime := time.Now().Add(-time.Hour)
cookieVerify(remoteAddr, &Cookie{SentTime: sentTime, RemoteAddr: cookieAddr.String()})
Expect(paramClientAddr).To(Equal(remoteAddr))
Expect(paramCookie).ToNot(BeNil())
Expect(paramCookie.RemoteAddr).To(Equal(cookieAddr.String()))
Expect(paramCookie.SentTime).To(Equal(sentTime))
2017-12-12 02:51:45 +00:00
})
2018-01-03 19:19:49 +00:00
})
2017-12-12 02:51:45 +00:00
2018-01-03 19:19:49 +00:00
Context("frame handling", func() {
2018-01-20 18:07:01 +00:00
Context("handling STREAM frames", func() {
It("passes STREAM frames to the stream", func() {
2018-01-03 19:19:49 +00:00
f := &wire.StreamFrame{
StreamID: 5,
Data: []byte{0xde, 0xca, 0xfb, 0xad},
}
2018-01-20 18:07:01 +00:00
str := NewMockReceiveStreamI(mockCtrl)
str.EXPECT().handleStreamFrame(f)
streamManager.EXPECT().GetOrOpenReceiveStream(protocol.StreamID(5)).Return(str, nil)
2018-01-03 19:19:49 +00:00
err := sess.handleStreamFrame(f)
Expect(err).ToNot(HaveOccurred())
2017-12-12 02:51:45 +00:00
})
2018-01-20 18:07:01 +00:00
It("returns errors", func() {
testErr := errors.New("test err")
f := &wire.StreamFrame{
2018-01-03 19:19:49 +00:00
StreamID: 5,
2018-01-20 18:07:01 +00:00
Data: []byte{0xde, 0xca, 0xfb, 0xad},
2018-01-03 19:19:49 +00:00
}
2018-01-20 18:07:01 +00:00
str := NewMockReceiveStreamI(mockCtrl)
str.EXPECT().handleStreamFrame(f).Return(testErr)
streamManager.EXPECT().GetOrOpenReceiveStream(protocol.StreamID(5)).Return(str, nil)
err := sess.handleStreamFrame(f)
Expect(err).To(MatchError(testErr))
2017-12-12 02:51:45 +00:00
})
2018-01-03 19:19:49 +00:00
It("ignores STREAM frames for closed streams", func() {
2018-01-20 18:07:01 +00:00
streamManager.EXPECT().GetOrOpenReceiveStream(protocol.StreamID(5)).Return(nil, nil) // for closed streams, the streamManager returns nil
err := sess.handleStreamFrame(&wire.StreamFrame{
2018-01-03 19:19:49 +00:00
StreamID: 5,
Data: []byte("foobar"),
})
Expect(err).ToNot(HaveOccurred())
})
2018-01-20 18:07:01 +00:00
It("errors on a STREAM frame that would close the crypto stream", func() {
err := sess.handleStreamFrame(&wire.StreamFrame{
StreamID: sess.version.CryptoStreamID(),
Offset: 0x1337,
FinBit: true,
})
Expect(err).To(MatchError("Received STREAM frame with FIN bit for the crypto stream"))
})
2017-12-12 02:51:45 +00:00
})
2018-01-20 18:07:01 +00:00
Context("handling ACK frames", func() {
It("informs the SentPacketHandler about ACKs", func() {
f := &wire.AckFrame{LargestAcked: 3, LowestAcked: 2}
sph := mockackhandler.NewMockSentPacketHandler(mockCtrl)
sph.EXPECT().ReceivedAck(f, protocol.PacketNumber(42), protocol.EncryptionSecure, gomock.Any())
sph.EXPECT().GetLowestPacketNotConfirmedAcked()
sess.sentPacketHandler = sph
sess.lastRcvdPacketNumber = 42
err := sess.handleAckFrame(f, protocol.EncryptionSecure)
2018-01-03 19:19:49 +00:00
Expect(err).ToNot(HaveOccurred())
2018-01-20 18:07:01 +00:00
})
It("tells the ReceivedPacketHandler to ignore low ranges", func() {
sph := mockackhandler.NewMockSentPacketHandler(mockCtrl)
sph.EXPECT().ReceivedAck(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any())
sph.EXPECT().GetLowestPacketNotConfirmedAcked().Return(protocol.PacketNumber(0x42))
sess.sentPacketHandler = sph
rph := mockackhandler.NewMockReceivedPacketHandler(mockCtrl)
rph.EXPECT().IgnoreBelow(protocol.PacketNumber(0x42))
sess.receivedPacketHandler = rph
err := sess.handleAckFrame(&wire.AckFrame{LargestAcked: 3, LowestAcked: 2}, protocol.EncryptionUnencrypted)
2018-01-03 19:19:49 +00:00
Expect(err).ToNot(HaveOccurred())
})
2018-01-20 18:07:01 +00:00
})
2017-12-12 02:51:45 +00:00
2018-01-20 18:07:01 +00:00
Context("handling RST_STREAM frames", func() {
It("closes the streams for writing", func() {
f := &wire.RstStreamFrame{
StreamID: 555,
ErrorCode: 42,
2018-01-03 19:19:49 +00:00
ByteOffset: 0x1337,
2018-01-20 18:07:01 +00:00
}
str := NewMockReceiveStreamI(mockCtrl)
streamManager.EXPECT().GetOrOpenReceiveStream(protocol.StreamID(555)).Return(str, nil)
str.EXPECT().handleRstStreamFrame(f)
err := sess.handleRstStreamFrame(f)
Expect(err).ToNot(HaveOccurred())
2017-12-12 02:51:45 +00:00
})
2018-01-03 19:19:49 +00:00
It("returns errors", func() {
2018-01-20 18:07:01 +00:00
f := &wire.RstStreamFrame{
StreamID: 7,
2018-01-03 19:19:49 +00:00
ByteOffset: 0x1337,
2018-01-20 18:07:01 +00:00
}
testErr := errors.New("flow control violation")
str := NewMockReceiveStreamI(mockCtrl)
streamManager.EXPECT().GetOrOpenReceiveStream(protocol.StreamID(7)).Return(str, nil)
str.EXPECT().handleRstStreamFrame(f).Return(testErr)
err := sess.handleRstStreamFrame(f)
2018-01-03 19:19:49 +00:00
Expect(err).To(MatchError(testErr))
2017-12-12 02:51:45 +00:00
})
2018-01-20 18:07:01 +00:00
It("ignores RST_STREAM frames for closed streams", func() {
streamManager.EXPECT().GetOrOpenReceiveStream(protocol.StreamID(3)).Return(nil, nil)
err := sess.handleFrames([]wire.Frame{&wire.RstStreamFrame{
2018-01-03 19:19:49 +00:00
StreamID: 3,
ErrorCode: 42,
}}, protocol.EncryptionUnspecified)
Expect(err).NotTo(HaveOccurred())
2017-12-12 02:51:45 +00:00
})
2018-01-20 18:07:01 +00:00
It("erros when a RST_STREAM frame would reset the crypto stream", func() {
err := sess.handleRstStreamFrame(&wire.RstStreamFrame{
StreamID: sess.version.CryptoStreamID(),
ErrorCode: 123,
})
Expect(err).To(MatchError("Received RST_STREAM frame for the crypto stream"))
})
2017-12-12 02:51:45 +00:00
})
2018-01-03 19:19:49 +00:00
Context("handling MAX_DATA and MAX_STREAM_DATA frames", func() {
var connFC *mocks.MockConnectionFlowController
BeforeEach(func() {
connFC = mocks.NewMockConnectionFlowController(mockCtrl)
sess.connFlowController = connFC
2017-12-12 02:51:45 +00:00
})
2018-01-20 18:07:01 +00:00
It("updates the flow control window of the crypto stream", func() {
fc := mocks.NewMockStreamFlowController(mockCtrl)
offset := protocol.ByteCount(0x4321)
fc.EXPECT().UpdateSendWindow(offset)
sess.cryptoStream.(*cryptoStream).sendStream.flowController = fc
err := sess.handleMaxStreamDataFrame(&wire.MaxStreamDataFrame{
StreamID: sess.version.CryptoStreamID(),
2018-01-03 19:19:49 +00:00
ByteOffset: offset,
})
Expect(err).ToNot(HaveOccurred())
2017-12-12 02:51:45 +00:00
})
2018-01-20 18:07:01 +00:00
It("updates the flow control window of a stream", func() {
f := &wire.MaxStreamDataFrame{
StreamID: 12345,
ByteOffset: 0x1337,
}
str := NewMockSendStreamI(mockCtrl)
streamManager.EXPECT().GetOrOpenSendStream(protocol.StreamID(12345)).Return(str, nil)
str.EXPECT().handleMaxStreamDataFrame(f)
err := sess.handleMaxStreamDataFrame(f)
Expect(err).ToNot(HaveOccurred())
})
2018-01-03 19:19:49 +00:00
It("updates the flow control window of the connection", func() {
offset := protocol.ByteCount(0x800000)
connFC.EXPECT().UpdateSendWindow(offset)
sess.handleMaxDataFrame(&wire.MaxDataFrame{ByteOffset: offset})
2017-12-12 02:51:45 +00:00
})
2018-01-20 18:07:01 +00:00
It("ignores MAX_STREAM_DATA frames for a closed stream", func() {
streamManager.EXPECT().GetOrOpenSendStream(protocol.StreamID(10)).Return(nil, nil)
err := sess.handleFrames([]wire.Frame{&wire.MaxStreamDataFrame{
StreamID: 10,
ByteOffset: 1337,
}}, protocol.EncryptionUnspecified)
2018-01-03 19:19:49 +00:00
Expect(err).NotTo(HaveOccurred())
2017-12-12 02:51:45 +00:00
})
2018-01-20 18:07:01 +00:00
})
2017-12-12 02:51:45 +00:00
2018-01-20 18:07:01 +00:00
Context("handling STOP_SENDING frames", func() {
It("passes the frame to the stream", func() {
f := &wire.StopSendingFrame{
StreamID: 5,
ErrorCode: 10,
}
str := NewMockSendStreamI(mockCtrl)
streamManager.EXPECT().GetOrOpenSendStream(protocol.StreamID(5)).Return(str, nil)
str.EXPECT().handleStopSendingFrame(f)
err := sess.handleStopSendingFrame(f)
2018-01-03 19:19:49 +00:00
Expect(err).ToNot(HaveOccurred())
2018-01-20 18:07:01 +00:00
})
It("errors when receiving a STOP_SENDING for the crypto stream", func() {
err := sess.handleStopSendingFrame(&wire.StopSendingFrame{
StreamID: sess.version.CryptoStreamID(),
ErrorCode: 10,
})
Expect(err).To(MatchError("Received a STOP_SENDING frame for the crypto stream"))
})
It("ignores STOP_SENDING frames for a closed stream", func() {
streamManager.EXPECT().GetOrOpenSendStream(protocol.StreamID(3)).Return(nil, nil)
err := sess.handleFrames([]wire.Frame{&wire.StopSendingFrame{
StreamID: 3,
ErrorCode: 1337,
2018-01-03 19:19:49 +00:00
}}, protocol.EncryptionUnspecified)
Expect(err).NotTo(HaveOccurred())
2017-12-12 02:51:45 +00:00
})
})
2018-01-03 19:19:49 +00:00
It("handles PING frames", func() {
err := sess.handleFrames([]wire.Frame{&wire.PingFrame{}}, protocol.EncryptionUnspecified)
2017-12-12 02:51:45 +00:00
Expect(err).NotTo(HaveOccurred())
})
2018-01-03 19:19:49 +00:00
It("handles BLOCKED frames", func() {
err := sess.handleFrames([]wire.Frame{&wire.BlockedFrame{}}, protocol.EncryptionUnspecified)
Expect(err).NotTo(HaveOccurred())
2017-12-12 02:51:45 +00:00
})
2018-01-03 19:19:49 +00:00
It("errors on GOAWAY frames", func() {
err := sess.handleFrames([]wire.Frame{&wire.GoawayFrame{}}, protocol.EncryptionUnspecified)
Expect(err).To(MatchError("unimplemented: handling GOAWAY frames"))
2017-12-12 02:51:45 +00:00
})
2018-01-03 19:19:49 +00:00
It("handles STOP_WAITING frames", func() {
err := sess.handleFrames([]wire.Frame{&wire.StopWaitingFrame{LeastUnacked: 10}}, protocol.EncryptionUnspecified)
2017-12-12 02:51:45 +00:00
Expect(err).NotTo(HaveOccurred())
})
2018-01-03 19:19:49 +00:00
It("handles CONNECTION_CLOSE frames", func() {
2018-01-20 18:07:01 +00:00
testErr := qerr.Error(qerr.ProofInvalid, "foobar")
streamManager.EXPECT().CloseWithError(testErr)
2018-01-03 19:19:49 +00:00
done := make(chan struct{})
go func() {
defer GinkgoRecover()
err := sess.run()
2018-01-20 18:07:01 +00:00
Expect(err).To(MatchError(testErr))
2018-01-03 19:19:49 +00:00
close(done)
}()
2018-01-20 18:07:01 +00:00
err := sess.handleFrames([]wire.Frame{&wire.ConnectionCloseFrame{ErrorCode: qerr.ProofInvalid, ReasonPhrase: "foobar"}}, protocol.EncryptionUnspecified)
2017-12-12 02:51:45 +00:00
Expect(err).NotTo(HaveOccurred())
2018-01-03 19:19:49 +00:00
Eventually(sess.Context().Done()).Should(BeClosed())
Eventually(done).Should(BeClosed())
2017-12-12 02:51:45 +00:00
})
})
2018-01-03 19:19:49 +00:00
It("tells its versions", func() {
sess.version = 4242
Expect(sess.GetVersion()).To(Equal(protocol.VersionNumber(4242)))
2017-12-12 02:51:45 +00:00
})
2018-01-20 18:07:01 +00:00
It("accepts new streams", func() {
mstr := NewMockStreamI(mockCtrl)
streamManager.EXPECT().AcceptStream().Return(mstr, nil)
str, err := sess.AcceptStream()
Expect(err).ToNot(HaveOccurred())
Expect(str).To(Equal(mstr))
2017-12-12 02:51:45 +00:00
})
2018-01-20 18:07:01 +00:00
Context("closing", func() {
2018-01-03 19:19:49 +00:00
BeforeEach(func() {
2018-01-20 18:07:01 +00:00
Eventually(areSessionsRunning).Should(BeFalse())
2017-12-12 02:51:45 +00:00
go func() {
2018-01-03 19:19:49 +00:00
defer GinkgoRecover()
2018-01-20 18:07:01 +00:00
sess.run()
2017-12-12 02:51:45 +00:00
}()
Eventually(areSessionsRunning).Should(BeTrue())
})
It("shuts down without error", func() {
2018-01-20 18:07:01 +00:00
streamManager.EXPECT().CloseWithError(qerr.Error(qerr.PeerGoingAway, ""))
2017-12-12 02:51:45 +00:00
sess.Close(nil)
Eventually(areSessionsRunning).Should(BeFalse())
Expect(mconn.written).To(HaveLen(1))
2018-01-03 19:19:49 +00:00
buf := &bytes.Buffer{}
err := (&wire.ConnectionCloseFrame{ErrorCode: qerr.PeerGoingAway}).Write(buf, sess.version)
Expect(err).ToNot(HaveOccurred())
Expect(mconn.written).To(Receive(ContainSubstring(string(buf.Bytes()))))
Expect(sess.Context().Done()).To(BeClosed())
2017-12-12 02:51:45 +00:00
})
It("only closes once", func() {
2018-01-20 18:07:01 +00:00
streamManager.EXPECT().CloseWithError(qerr.Error(qerr.PeerGoingAway, ""))
2017-12-12 02:51:45 +00:00
sess.Close(nil)
sess.Close(nil)
Eventually(areSessionsRunning).Should(BeFalse())
Expect(mconn.written).To(HaveLen(1))
2018-01-03 19:19:49 +00:00
Expect(sess.Context().Done()).To(BeClosed())
2017-12-12 02:51:45 +00:00
})
It("closes streams with proper error", func() {
testErr := errors.New("test error")
2018-01-20 18:07:01 +00:00
streamManager.EXPECT().CloseWithError(qerr.Error(qerr.InternalError, testErr.Error()))
2017-12-12 02:51:45 +00:00
sess.Close(testErr)
Eventually(areSessionsRunning).Should(BeFalse())
2018-01-03 19:19:49 +00:00
Expect(sess.Context().Done()).To(BeClosed())
2017-12-12 02:51:45 +00:00
})
It("closes the session in order to replace it with another QUIC version", func() {
2018-01-20 18:07:01 +00:00
streamManager.EXPECT().CloseWithError(gomock.Any())
2017-12-12 02:51:45 +00:00
sess.Close(errCloseSessionForNewVersion)
Eventually(areSessionsRunning).Should(BeFalse())
Expect(mconn.written).To(BeEmpty()) // no CONNECTION_CLOSE or PUBLIC_RESET sent
})
It("sends a Public Reset if the client is initiating the head-of-line blocking experiment", func() {
2018-01-20 18:07:01 +00:00
streamManager.EXPECT().CloseWithError(gomock.Any())
2017-12-12 02:51:45 +00:00
sess.Close(handshake.ErrHOLExperiment)
Expect(mconn.written).To(HaveLen(1))
2018-01-03 19:19:49 +00:00
Expect((<-mconn.written)[0] & 0x02).ToNot(BeZero()) // Public Reset
Expect(sess.Context().Done()).To(BeClosed())
})
It("sends a Public Reset if the client is initiating the no STOP_WAITING experiment", func() {
2018-01-20 18:07:01 +00:00
streamManager.EXPECT().CloseWithError(gomock.Any())
2018-01-03 19:19:49 +00:00
sess.Close(handshake.ErrHOLExperiment)
Expect(mconn.written).To(HaveLen(1))
Expect((<-mconn.written)[0] & 0x02).ToNot(BeZero()) // Public Reset
Expect(sess.Context().Done()).To(BeClosed())
})
It("cancels the context when the run loop exists", func() {
2018-01-20 18:07:01 +00:00
streamManager.EXPECT().CloseWithError(gomock.Any())
2018-01-03 19:19:49 +00:00
returned := make(chan struct{})
go func() {
defer GinkgoRecover()
ctx := sess.Context()
<-ctx.Done()
Expect(ctx.Err()).To(MatchError(context.Canceled))
close(returned)
}()
Consistently(returned).ShouldNot(BeClosed())
sess.Close(nil)
Eventually(returned).Should(BeClosed())
2017-12-12 02:51:45 +00:00
})
})
Context("receiving packets", func() {
2018-01-03 19:19:49 +00:00
var hdr *wire.Header
2017-12-12 02:51:45 +00:00
BeforeEach(func() {
sess.unpacker = &mockUnpacker{}
2018-01-03 19:19:49 +00:00
hdr = &wire.Header{PacketNumberLen: protocol.PacketNumberLen6}
2017-12-12 02:51:45 +00:00
})
It("sets the {last,largest}RcvdPacketNumber", func() {
hdr.PacketNumber = 5
2018-01-03 19:19:49 +00:00
err := sess.handlePacketImpl(&receivedPacket{header: hdr})
2017-12-12 02:51:45 +00:00
Expect(err).ToNot(HaveOccurred())
Expect(sess.lastRcvdPacketNumber).To(Equal(protocol.PacketNumber(5)))
Expect(sess.largestRcvdPacketNumber).To(Equal(protocol.PacketNumber(5)))
})
2018-01-20 18:07:01 +00:00
It("informs the ReceivedPacketHandler", func() {
now := time.Now().Add(time.Hour)
rph := mockackhandler.NewMockReceivedPacketHandler(mockCtrl)
rph.EXPECT().ReceivedPacket(protocol.PacketNumber(5), now, false)
sess.receivedPacketHandler = rph
hdr.PacketNumber = 5
err := sess.handlePacketImpl(&receivedPacket{header: hdr, rcvTime: now})
Expect(err).ToNot(HaveOccurred())
})
2018-01-03 19:19:49 +00:00
It("closes when handling a packet fails", func(done Done) {
2018-01-20 18:07:01 +00:00
streamManager.EXPECT().CloseWithError(gomock.Any())
2018-01-03 19:19:49 +00:00
testErr := errors.New("unpack error")
hdr.PacketNumber = 5
var runErr error
go func() {
2018-01-20 18:07:01 +00:00
defer GinkgoRecover()
2018-01-03 19:19:49 +00:00
runErr = sess.run()
}()
sess.unpacker.(*mockUnpacker).unpackErr = testErr
sess.handlePacket(&receivedPacket{header: hdr})
Eventually(func() error { return runErr }).Should(MatchError(testErr))
Expect(sess.Context().Done()).To(BeClosed())
close(done)
})
2017-12-12 02:51:45 +00:00
It("sets the {last,largest}RcvdPacketNumber, for an out-of-order packet", func() {
hdr.PacketNumber = 5
2018-01-03 19:19:49 +00:00
err := sess.handlePacketImpl(&receivedPacket{header: hdr})
2017-12-12 02:51:45 +00:00
Expect(err).ToNot(HaveOccurred())
Expect(sess.lastRcvdPacketNumber).To(Equal(protocol.PacketNumber(5)))
Expect(sess.largestRcvdPacketNumber).To(Equal(protocol.PacketNumber(5)))
hdr.PacketNumber = 3
2018-01-03 19:19:49 +00:00
err = sess.handlePacketImpl(&receivedPacket{header: hdr})
2017-12-12 02:51:45 +00:00
Expect(err).ToNot(HaveOccurred())
Expect(sess.lastRcvdPacketNumber).To(Equal(protocol.PacketNumber(3)))
Expect(sess.largestRcvdPacketNumber).To(Equal(protocol.PacketNumber(5)))
})
2018-01-03 19:19:49 +00:00
It("handles duplicate packets", func() {
2017-12-12 02:51:45 +00:00
hdr.PacketNumber = 5
2018-01-03 19:19:49 +00:00
err := sess.handlePacketImpl(&receivedPacket{header: hdr})
2017-12-12 02:51:45 +00:00
Expect(err).ToNot(HaveOccurred())
2018-01-03 19:19:49 +00:00
err = sess.handlePacketImpl(&receivedPacket{header: hdr})
2017-12-12 02:51:45 +00:00
Expect(err).ToNot(HaveOccurred())
})
Context("updating the remote address", func() {
2018-01-03 19:19:49 +00:00
It("doesn't support connection migration", func() {
origAddr := sess.conn.(*mockConnection).remoteAddr
2017-12-12 02:51:45 +00:00
remoteIP := &net.IPAddr{IP: net.IPv4(192, 168, 0, 100)}
2018-01-03 19:19:49 +00:00
Expect(origAddr).ToNot(Equal(remoteIP))
2017-12-12 02:51:45 +00:00
p := receivedPacket{
2018-01-03 19:19:49 +00:00
remoteAddr: remoteIP,
header: &wire.Header{PacketNumber: 1337},
2017-12-12 02:51:45 +00:00
}
err := sess.handlePacketImpl(&p)
Expect(err).ToNot(HaveOccurred())
2018-01-03 19:19:49 +00:00
Expect(sess.conn.(*mockConnection).remoteAddr).To(Equal(origAddr))
2017-12-12 02:51:45 +00:00
})
It("doesn't change the remote address if authenticating the packet fails", func() {
remoteIP := &net.IPAddr{IP: net.IPv4(192, 168, 0, 100)}
attackerIP := &net.IPAddr{IP: net.IPv4(192, 168, 0, 102)}
sess.conn.(*mockConnection).remoteAddr = remoteIP
// use the real packetUnpacker here, to make sure this test fails if the error code for failed decryption changes
sess.unpacker = &packetUnpacker{}
sess.unpacker.(*packetUnpacker).aead = &mockAEAD{}
p := receivedPacket{
2018-01-03 19:19:49 +00:00
remoteAddr: attackerIP,
header: &wire.Header{PacketNumber: 1337},
2017-12-12 02:51:45 +00:00
}
err := sess.handlePacketImpl(&p)
quicErr := err.(*qerr.QuicError)
Expect(quicErr.ErrorCode).To(Equal(qerr.DecryptionFailure))
Expect(sess.conn.(*mockConnection).remoteAddr).To(Equal(remoteIP))
})
})
})
Context("sending packets", func() {
2018-01-03 19:19:49 +00:00
BeforeEach(func() {
sess.packer.hasSentPacket = true // make sure this is not the first packet the packer sends
})
It("sends ACK frames", func() {
packetNumber := protocol.PacketNumber(0x035e)
2018-01-20 18:07:01 +00:00
err := sess.receivedPacketHandler.ReceivedPacket(packetNumber, time.Now(), true)
2018-01-03 19:19:49 +00:00
Expect(err).ToNot(HaveOccurred())
2018-01-20 18:07:01 +00:00
sent, err := sess.sendPacket()
2018-01-03 19:19:49 +00:00
Expect(err).NotTo(HaveOccurred())
2018-01-20 18:07:01 +00:00
Expect(sent).To(BeTrue())
2018-01-03 19:19:49 +00:00
Expect(mconn.written).To(HaveLen(1))
Expect(mconn.written).To(Receive(ContainSubstring(string([]byte{0x03, 0x5e}))))
})
2018-01-20 18:07:01 +00:00
It("sends a retransmittable packet when required by the SentPacketHandler", func() {
ack := &wire.AckFrame{LargestAcked: 1000}
sess.packer.QueueControlFrame(ack)
sph := mockackhandler.NewMockSentPacketHandler(mockCtrl)
sph.EXPECT().GetLeastUnacked().AnyTimes()
sph.EXPECT().DequeuePacketForRetransmission()
sph.EXPECT().ShouldSendRetransmittablePacket().Return(true)
sph.EXPECT().SentPacket(gomock.Any()).Do(func(p *ackhandler.Packet) {
Expect(p.Frames).To(HaveLen(2))
Expect(p.Frames).To(ContainElement(ack))
})
sess.sentPacketHandler = sph
sent, err := sess.sendPacket()
2017-12-12 02:51:45 +00:00
Expect(err).NotTo(HaveOccurred())
2018-01-20 18:07:01 +00:00
Expect(sent).To(BeTrue())
2017-12-12 02:51:45 +00:00
Expect(mconn.written).To(HaveLen(1))
})
2018-01-20 18:07:01 +00:00
It("adds a MAX_DATA frames", func() {
fc := mocks.NewMockConnectionFlowController(mockCtrl)
fc.EXPECT().GetWindowUpdate().Return(protocol.ByteCount(0x1337))
fc.EXPECT().IsNewlyBlocked()
sess.connFlowController = fc
sph := mockackhandler.NewMockSentPacketHandler(mockCtrl)
sph.EXPECT().GetLeastUnacked().AnyTimes()
sph.EXPECT().DequeuePacketForRetransmission()
sph.EXPECT().ShouldSendRetransmittablePacket()
sph.EXPECT().SentPacket(gomock.Any()).Do(func(p *ackhandler.Packet) {
Expect(p.Frames).To(Equal([]wire.Frame{
&wire.MaxDataFrame{ByteOffset: 0x1337},
}))
})
sess.sentPacketHandler = sph
sent, err := sess.sendPacket()
Expect(err).NotTo(HaveOccurred())
Expect(sent).To(BeTrue())
2018-01-03 19:19:49 +00:00
})
2018-01-20 18:07:01 +00:00
It("adds MAX_STREAM_DATA frames", func() {
sess.windowUpdateQueue.callback(&wire.MaxStreamDataFrame{
StreamID: 2,
ByteOffset: 20,
})
sph := mockackhandler.NewMockSentPacketHandler(mockCtrl)
sph.EXPECT().GetLeastUnacked().AnyTimes()
sph.EXPECT().DequeuePacketForRetransmission()
sph.EXPECT().ShouldSendRetransmittablePacket()
sph.EXPECT().SentPacket(gomock.Any()).Do(func(p *ackhandler.Packet) {
Expect(p.Frames).To(ContainElement(&wire.MaxStreamDataFrame{StreamID: 2, ByteOffset: 20}))
})
sess.sentPacketHandler = sph
sent, err := sess.sendPacket()
2017-12-12 02:51:45 +00:00
Expect(err).NotTo(HaveOccurred())
2018-01-20 18:07:01 +00:00
Expect(sent).To(BeTrue())
})
It("adds a BLOCKED frame when it is connection-level flow control blocked", func() {
fc := mocks.NewMockConnectionFlowController(mockCtrl)
fc.EXPECT().GetWindowUpdate()
fc.EXPECT().IsNewlyBlocked().Return(true, protocol.ByteCount(1337))
sess.connFlowController = fc
sph := mockackhandler.NewMockSentPacketHandler(mockCtrl)
sph.EXPECT().GetLeastUnacked().AnyTimes()
sph.EXPECT().DequeuePacketForRetransmission()
sph.EXPECT().ShouldSendRetransmittablePacket()
sph.EXPECT().SentPacket(gomock.Any()).Do(func(p *ackhandler.Packet) {
Expect(p.Frames).To(Equal([]wire.Frame{
&wire.BlockedFrame{Offset: 1337},
}))
})
sess.sentPacketHandler = sph
sent, err := sess.sendPacket()
2017-12-12 02:51:45 +00:00
Expect(err).NotTo(HaveOccurred())
2018-01-20 18:07:01 +00:00
Expect(sent).To(BeTrue())
})
It("sends multiple packets", func() {
sess.queueControlFrame(&wire.MaxDataFrame{ByteOffset: 1})
sph := mockackhandler.NewMockSentPacketHandler(mockCtrl)
sph.EXPECT().DequeuePacketForRetransmission().Times(2)
sph.EXPECT().GetAlarmTimeout().AnyTimes()
sph.EXPECT().GetLeastUnacked().AnyTimes()
sph.EXPECT().ShouldSendRetransmittablePacket().Times(2)
sph.EXPECT().SentPacket(gomock.Any()).Times(2)
sph.EXPECT().SendingAllowed().Do(func() { // after sending the first packet
// make sure there's something to send
sess.packer.QueueControlFrame(&wire.MaxDataFrame{ByteOffset: 2})
}).Return(true).Times(2) // allow 2 packets...
sph.EXPECT().SendingAllowed() // ...then report that we're congestion limited
sess.sentPacketHandler = sph
done := make(chan struct{})
go func() {
defer GinkgoRecover()
sess.run()
close(done)
}()
sess.scheduleSending()
Eventually(mconn.written).Should(HaveLen(2))
// make the go routine return
streamManager.EXPECT().CloseWithError(gomock.Any())
sess.Close(nil)
Eventually(done).Should(BeClosed())
2017-12-12 02:51:45 +00:00
})
It("sends public reset", func() {
err := sess.sendPublicReset(1)
Expect(err).NotTo(HaveOccurred())
Expect(mconn.written).To(HaveLen(1))
2018-01-03 19:19:49 +00:00
Expect(mconn.written).To(Receive(ContainSubstring("PRST")))
2017-12-12 02:51:45 +00:00
})
It("informs the SentPacketHandler about sent packets", func() {
2018-01-03 19:19:49 +00:00
f := &wire.StreamFrame{
2017-12-12 02:51:45 +00:00
StreamID: 5,
Data: []byte("foobar"),
}
2018-01-20 18:07:01 +00:00
var sentPacket *ackhandler.Packet
sph := mockackhandler.NewMockSentPacketHandler(mockCtrl)
sph.EXPECT().GetLeastUnacked().AnyTimes()
sph.EXPECT().GetStopWaitingFrame(gomock.Any())
sph.EXPECT().DequeuePacketForRetransmission()
sph.EXPECT().ShouldSendRetransmittablePacket()
sph.EXPECT().SentPacket(gomock.Any()).Do(func(p *ackhandler.Packet) {
sentPacket = p
})
sess.sentPacketHandler = sph
sess.packer.packetNumberGenerator.next = 0x1337 + 9
sess.packer.cryptoSetup = &mockCryptoSetup{encLevelSeal: protocol.EncryptionForwardSecure}
2017-12-12 02:51:45 +00:00
sess.streamFramer.AddFrameForRetransmission(f)
2018-01-20 18:07:01 +00:00
sent, err := sess.sendPacket()
2017-12-12 02:51:45 +00:00
Expect(err).NotTo(HaveOccurred())
2018-01-20 18:07:01 +00:00
Expect(sent).To(BeTrue())
2017-12-12 02:51:45 +00:00
Expect(mconn.written).To(HaveLen(1))
2018-01-20 18:07:01 +00:00
Expect(sentPacket.PacketNumber).To(Equal(protocol.PacketNumber(0x1337 + 9)))
Expect(sentPacket.Frames).To(ContainElement(f))
Expect(sentPacket.EncryptionLevel).To(Equal(protocol.EncryptionForwardSecure))
Expect(sentPacket.Length).To(BeEquivalentTo(len(<-mconn.written)))
})
})
Context("sending ACK only packets", func() {
It("doesn't do anything if there's no ACK to be sent", func() {
sph := mockackhandler.NewMockSentPacketHandler(mockCtrl)
sess.sentPacketHandler = sph
err := sess.maybeSendAckOnlyPacket()
Expect(err).ToNot(HaveOccurred())
Expect(mconn.written).To(BeEmpty())
})
It("sends ACK only packets", func() {
swf := &wire.StopWaitingFrame{LeastUnacked: 10}
sph := mockackhandler.NewMockSentPacketHandler(mockCtrl)
sph.EXPECT().GetLeastUnacked()
sph.EXPECT().GetAlarmTimeout().AnyTimes()
sph.EXPECT().SendingAllowed()
sph.EXPECT().GetStopWaitingFrame(false).Return(swf)
sph.EXPECT().SentPacket(gomock.Any()).Do(func(p *ackhandler.Packet) {
Expect(p.Frames).To(HaveLen(2))
Expect(p.Frames[0]).To(BeAssignableToTypeOf(&wire.AckFrame{}))
Expect(p.Frames[1]).To(Equal(swf))
})
sess.sentPacketHandler = sph
sess.packer.packetNumberGenerator.next = 0x1338
sess.receivedPacketHandler.ReceivedPacket(1, time.Now(), true)
done := make(chan struct{})
go func() {
defer GinkgoRecover()
sess.run()
close(done)
}()
sess.scheduleSending()
Eventually(mconn.written).Should(HaveLen(1))
// make sure that the go routine returns
streamManager.EXPECT().CloseWithError(gomock.Any())
sess.Close(nil)
Eventually(done).Should(BeClosed())
})
It("doesn't include a STOP_WAITING for an ACK-only packet for IETF QUIC", func() {
sess.version = versionIETFFrames
done := make(chan struct{})
sph := mockackhandler.NewMockSentPacketHandler(mockCtrl)
sph.EXPECT().GetLeastUnacked()
sph.EXPECT().GetAlarmTimeout().AnyTimes()
sph.EXPECT().SendingAllowed()
sph.EXPECT().SentPacket(gomock.Any()).Do(func(p *ackhandler.Packet) {
Expect(p.Frames).To(HaveLen(1))
Expect(p.Frames[0]).To(BeAssignableToTypeOf(&wire.AckFrame{}))
})
sess.sentPacketHandler = sph
sess.packer.packetNumberGenerator.next = 0x1338
sess.receivedPacketHandler.ReceivedPacket(1, time.Now(), true)
go func() {
defer GinkgoRecover()
sess.run()
close(done)
}()
sess.scheduleSending()
Eventually(mconn.written).Should(HaveLen(1))
// make sure that the go routine returns
streamManager.EXPECT().CloseWithError(gomock.Any())
sess.Close(nil)
Eventually(done).Should(BeClosed())
2017-12-12 02:51:45 +00:00
})
})
Context("retransmissions", func() {
2018-01-20 18:07:01 +00:00
var sph *mockackhandler.MockSentPacketHandler
2017-12-12 02:51:45 +00:00
BeforeEach(func() {
2018-01-20 18:07:01 +00:00
// a STOP_WAITING frame is added, so make sure the packet number of the new package is higher than the packet number of the retransmitted packet
2017-12-12 02:51:45 +00:00
sess.packer.packetNumberGenerator.next = 0x1337 + 10
2018-01-03 19:19:49 +00:00
sess.packer.hasSentPacket = true // make sure this is not the first packet the packer sends
2018-01-20 18:07:01 +00:00
sph = mockackhandler.NewMockSentPacketHandler(mockCtrl)
sph.EXPECT().GetLeastUnacked().AnyTimes()
2017-12-12 02:51:45 +00:00
sess.sentPacketHandler = sph
sess.packer.cryptoSetup = &mockCryptoSetup{encLevelSeal: protocol.EncryptionForwardSecure}
})
Context("for handshake packets", func() {
2018-01-20 18:07:01 +00:00
It("retransmits an unencrypted packet, and adds a STOP_WAITING frame (for gQUIC)", func() {
2018-01-03 19:19:49 +00:00
sf := &wire.StreamFrame{StreamID: 1, Data: []byte("foobar")}
2018-01-20 18:07:01 +00:00
swf := &wire.StopWaitingFrame{LeastUnacked: 0x1337}
sph.EXPECT().GetStopWaitingFrame(true).Return(swf)
sph.EXPECT().DequeuePacketForRetransmission().Return(&ackhandler.Packet{
2018-01-03 19:19:49 +00:00
Frames: []wire.Frame{sf},
2017-12-12 02:51:45 +00:00
EncryptionLevel: protocol.EncryptionUnencrypted,
2018-01-20 18:07:01 +00:00
})
sph.EXPECT().SentPacket(gomock.Any()).Do(func(p *ackhandler.Packet) {
Expect(p.EncryptionLevel).To(Equal(protocol.EncryptionUnencrypted))
Expect(p.Frames).To(Equal([]wire.Frame{swf, sf}))
})
sent, err := sess.sendPacket()
Expect(err).NotTo(HaveOccurred())
Expect(sent).To(BeTrue())
2017-12-12 02:51:45 +00:00
Expect(mconn.written).To(HaveLen(1))
})
2018-01-20 18:07:01 +00:00
It("retransmits an unencrypted packet, and doesn't add a STOP_WAITING frame (for IETF QUIC)", func() {
sess.version = versionIETFFrames
sess.packer.version = versionIETFFrames
2018-01-03 19:19:49 +00:00
sf := &wire.StreamFrame{StreamID: 1, Data: []byte("foobar")}
2018-01-20 18:07:01 +00:00
sph.EXPECT().DequeuePacketForRetransmission().Return(&ackhandler.Packet{
2018-01-03 19:19:49 +00:00
Frames: []wire.Frame{sf},
2018-01-20 18:07:01 +00:00
EncryptionLevel: protocol.EncryptionUnencrypted,
})
sph.EXPECT().SentPacket(gomock.Any()).Do(func(p *ackhandler.Packet) {
Expect(p.EncryptionLevel).To(Equal(protocol.EncryptionUnencrypted))
Expect(p.Frames).To(Equal([]wire.Frame{sf}))
})
sent, err := sess.sendPacket()
Expect(err).NotTo(HaveOccurred())
Expect(sent).To(BeTrue())
2017-12-12 02:51:45 +00:00
Expect(mconn.written).To(HaveLen(1))
})
2018-01-03 19:19:49 +00:00
})
2017-12-12 02:51:45 +00:00
2018-01-03 19:19:49 +00:00
Context("for packets after the handshake", func() {
2018-01-20 18:07:01 +00:00
It("sends a STREAM frame from a packet queued for retransmission, and adds a STOP_WAITING (for gQUIC)", func() {
f := &wire.StreamFrame{
2017-12-12 02:51:45 +00:00
StreamID: 0x5,
2018-01-20 18:07:01 +00:00
Data: []byte("foobar"),
2017-12-12 02:51:45 +00:00
}
2018-01-20 18:07:01 +00:00
swf := &wire.StopWaitingFrame{LeastUnacked: 10}
sph.EXPECT().GetStopWaitingFrame(true).Return(swf)
sph.EXPECT().DequeuePacketForRetransmission().Return(&ackhandler.Packet{
2017-12-12 02:51:45 +00:00
PacketNumber: 0x1337,
2018-01-20 18:07:01 +00:00
Frames: []wire.Frame{f},
2017-12-12 02:51:45 +00:00
EncryptionLevel: protocol.EncryptionForwardSecure,
2018-01-20 18:07:01 +00:00
})
sph.EXPECT().DequeuePacketForRetransmission()
sph.EXPECT().ShouldSendRetransmittablePacket()
sph.EXPECT().SentPacket(gomock.Any()).Do(func(p *ackhandler.Packet) {
Expect(p.Frames).To(Equal([]wire.Frame{swf, f}))
Expect(p.EncryptionLevel).To(Equal(protocol.EncryptionForwardSecure))
})
sent, err := sess.sendPacket()
Expect(err).NotTo(HaveOccurred())
Expect(sent).To(BeTrue())
Expect(mconn.written).To(HaveLen(1))
})
2017-12-12 02:51:45 +00:00
2018-01-20 18:07:01 +00:00
It("sends a STREAM frame from a packet queued for retransmission, and doesn't add a STOP_WAITING (for IETF QUIC)", func() {
sess.version = versionIETFFrames
sess.packer.version = versionIETFFrames
f := &wire.StreamFrame{
StreamID: 0x5,
Data: []byte("foobar"),
}
sph.EXPECT().DequeuePacketForRetransmission().Return(&ackhandler.Packet{
Frames: []wire.Frame{f},
EncryptionLevel: protocol.EncryptionForwardSecure,
})
sph.EXPECT().DequeuePacketForRetransmission()
sph.EXPECT().ShouldSendRetransmittablePacket()
sph.EXPECT().SentPacket(gomock.Any()).Do(func(p *ackhandler.Packet) {
Expect(p.Frames).To(Equal([]wire.Frame{f}))
Expect(p.EncryptionLevel).To(Equal(protocol.EncryptionForwardSecure))
})
sent, err := sess.sendPacket()
2017-12-12 02:51:45 +00:00
Expect(err).NotTo(HaveOccurred())
2018-01-20 18:07:01 +00:00
Expect(sent).To(BeTrue())
2017-12-12 02:51:45 +00:00
Expect(mconn.written).To(HaveLen(1))
})
2018-01-20 18:07:01 +00:00
It("sends a STREAM frame from a packet queued for retransmission", func() {
2018-01-03 19:19:49 +00:00
f1 := wire.StreamFrame{
2017-12-12 02:51:45 +00:00
StreamID: 0x5,
Data: []byte("foobar"),
}
2018-01-03 19:19:49 +00:00
f2 := wire.StreamFrame{
2017-12-12 02:51:45 +00:00
StreamID: 0x7,
Data: []byte("loremipsum"),
}
2018-01-20 18:07:01 +00:00
p1 := &ackhandler.Packet{
2017-12-12 02:51:45 +00:00
PacketNumber: 0x1337,
2018-01-03 19:19:49 +00:00
Frames: []wire.Frame{&f1},
2017-12-12 02:51:45 +00:00
EncryptionLevel: protocol.EncryptionForwardSecure,
}
2018-01-20 18:07:01 +00:00
p2 := &ackhandler.Packet{
2017-12-12 02:51:45 +00:00
PacketNumber: 0x1338,
2018-01-03 19:19:49 +00:00
Frames: []wire.Frame{&f2},
2017-12-12 02:51:45 +00:00
EncryptionLevel: protocol.EncryptionForwardSecure,
}
2018-01-20 18:07:01 +00:00
sph.EXPECT().DequeuePacketForRetransmission().Return(p1)
sph.EXPECT().DequeuePacketForRetransmission().Return(p2)
sph.EXPECT().DequeuePacketForRetransmission()
sph.EXPECT().GetStopWaitingFrame(true).Return(&wire.StopWaitingFrame{})
sph.EXPECT().ShouldSendRetransmittablePacket()
sph.EXPECT().SentPacket(gomock.Any()).Do(func(p *ackhandler.Packet) {
Expect(p.Frames).To(HaveLen(3))
})
sent, err := sess.sendPacket()
2017-12-12 02:51:45 +00:00
Expect(err).NotTo(HaveOccurred())
2018-01-20 18:07:01 +00:00
Expect(sent).To(BeTrue())
2017-12-12 02:51:45 +00:00
Expect(mconn.written).To(HaveLen(1))
2018-01-03 19:19:49 +00:00
packet := <-mconn.written
Expect(packet).To(ContainSubstring("foobar"))
Expect(packet).To(ContainSubstring("loremipsum"))
2017-12-12 02:51:45 +00:00
})
})
})
It("retransmits RTO packets", func() {
2018-01-03 19:19:49 +00:00
sess.packer.hasSentPacket = true // make sure this is not the first packet the packer sends
sess.sentPacketHandler.SetHandshakeComplete()
n := protocol.PacketNumber(10)
2017-12-12 02:51:45 +00:00
sess.packer.cryptoSetup = &mockCryptoSetup{encLevelSeal: protocol.EncryptionForwardSecure}
// We simulate consistently low RTTs, so that the test works faster
2018-01-03 19:19:49 +00:00
rtt := time.Millisecond
sess.rttStats.UpdateRTT(rtt, 0, time.Now())
Expect(sess.rttStats.SmoothedRTT()).To(Equal(rtt)) // make sure it worked
2017-12-12 02:51:45 +00:00
sess.packer.packetNumberGenerator.next = n + 1
// Now, we send a single packet, and expect that it was retransmitted later
err := sess.sentPacketHandler.SentPacket(&ackhandler.Packet{
PacketNumber: n,
Length: 1,
2018-01-03 19:19:49 +00:00
Frames: []wire.Frame{&wire.StreamFrame{
2017-12-12 02:51:45 +00:00
Data: []byte("foobar"),
}},
EncryptionLevel: protocol.EncryptionForwardSecure,
})
Expect(err).NotTo(HaveOccurred())
go sess.run()
defer sess.Close(nil)
sess.scheduleSending()
2018-01-03 19:19:49 +00:00
Eventually(func() int { return len(mconn.written) }).ShouldNot(BeZero())
Expect(mconn.written).To(Receive(ContainSubstring("foobar")))
2018-01-20 18:07:01 +00:00
streamManager.EXPECT().CloseWithError(gomock.Any())
2017-12-12 02:51:45 +00:00
})
Context("scheduling sending", func() {
BeforeEach(func() {
2018-01-03 19:19:49 +00:00
sess.packer.hasSentPacket = true // make sure this is not the first packet the packer sends
2017-12-12 02:51:45 +00:00
sess.packer.cryptoSetup = &mockCryptoSetup{encLevelSeal: protocol.EncryptionForwardSecure}
})
2018-01-20 18:07:01 +00:00
It("sends when scheduleSending is called", func() {
done := make(chan struct{})
2017-12-12 02:51:45 +00:00
go func() {
2018-01-20 18:07:01 +00:00
defer GinkgoRecover()
sess.run()
2017-12-12 02:51:45 +00:00
close(done)
}()
2018-01-20 18:07:01 +00:00
sess.streamFramer.AddFrameForRetransmission(&wire.StreamFrame{
StreamID: 5,
Data: []byte("foobar"),
})
Consistently(mconn.written).ShouldNot(Receive())
sess.scheduleSending()
Eventually(mconn.written).Should(Receive())
// make the go routine return
streamManager.EXPECT().CloseWithError(gomock.Any())
sess.Close(nil)
Eventually(done).Should(BeClosed())
2017-12-12 02:51:45 +00:00
})
It("sets the timer to the ack timer", func() {
2018-01-20 18:07:01 +00:00
rph := mockackhandler.NewMockReceivedPacketHandler(mockCtrl)
rph.EXPECT().GetAckFrame().Return(&wire.AckFrame{LargestAcked: 0x1337})
rph.EXPECT().GetAckFrame()
rph.EXPECT().GetAlarmTimeout().Return(time.Now().Add(10 * time.Millisecond)).MinTimes(1)
2017-12-12 02:51:45 +00:00
sess.receivedPacketHandler = rph
2018-01-20 18:07:01 +00:00
done := make(chan struct{})
go func() {
defer GinkgoRecover()
sess.run()
close(done)
}()
Eventually(mconn.written).Should(Receive(ContainSubstring(string([]byte{0x13, 0x37}))))
// make the go routine return
streamManager.EXPECT().CloseWithError(gomock.Any())
sess.Close(nil)
Eventually(done).Should(BeClosed())
2017-12-12 02:51:45 +00:00
})
})
It("closes when crypto stream errors", func() {
2018-01-03 19:19:49 +00:00
testErr := errors.New("crypto setup error")
2018-01-20 18:07:01 +00:00
streamManager.EXPECT().CloseWithError(qerr.Error(qerr.InternalError, testErr.Error()))
2018-01-03 19:19:49 +00:00
cryptoSetup.handleErr = testErr
2018-01-20 18:07:01 +00:00
done := make(chan struct{})
2018-01-03 19:19:49 +00:00
go func() {
2018-01-20 18:07:01 +00:00
defer GinkgoRecover()
err := sess.run()
Expect(err).To(MatchError(testErr))
close(done)
2018-01-03 19:19:49 +00:00
}()
2018-01-20 18:07:01 +00:00
Eventually(done).Should(BeClosed())
2017-12-12 02:51:45 +00:00
})
Context("sending a Public Reset when receiving undecryptable packets during the handshake", func() {
// sends protocol.MaxUndecryptablePackets+1 undecrytable packets
// this completely fills up the undecryptable packets queue and triggers the public reset timer
sendUndecryptablePackets := func() {
for i := 0; i < protocol.MaxUndecryptablePackets+1; i++ {
2018-01-03 19:19:49 +00:00
hdr := &wire.Header{
2017-12-12 02:51:45 +00:00
PacketNumber: protocol.PacketNumber(i + 1),
}
2018-01-03 19:19:49 +00:00
sess.handlePacket(&receivedPacket{
header: hdr,
remoteAddr: &net.UDPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 1234},
data: []byte("foobar"),
})
2017-12-12 02:51:45 +00:00
}
}
BeforeEach(func() {
sess.unpacker = &mockUnpacker{unpackErr: qerr.Error(qerr.DecryptionFailure, "")}
sess.cryptoSetup = &mockCryptoSetup{}
2018-01-20 18:07:01 +00:00
streamManager.EXPECT().CloseWithError(gomock.Any()).MaxTimes(1)
2017-12-12 02:51:45 +00:00
})
It("doesn't immediately send a Public Reset after receiving too many undecryptable packets", func() {
2018-01-20 18:07:01 +00:00
go func() {
defer GinkgoRecover()
sess.run()
}()
2017-12-12 02:51:45 +00:00
sendUndecryptablePackets()
sess.scheduleSending()
2018-01-03 19:19:49 +00:00
Consistently(mconn.written).Should(HaveLen(0))
Expect(sess.Close(nil)).To(Succeed())
Eventually(sess.Context().Done()).Should(BeClosed())
2017-12-12 02:51:45 +00:00
})
It("sets a deadline to send a Public Reset after receiving too many undecryptable packets", func() {
2018-01-20 18:07:01 +00:00
go func() {
defer GinkgoRecover()
sess.run()
}()
2017-12-12 02:51:45 +00:00
sendUndecryptablePackets()
2018-01-03 19:19:49 +00:00
Eventually(func() time.Time { return sess.receivedTooManyUndecrytablePacketsTime }).Should(BeTemporally("~", time.Now(), 20*time.Millisecond))
2017-12-12 02:51:45 +00:00
sess.Close(nil)
2018-01-03 19:19:49 +00:00
Eventually(sess.Context().Done()).Should(BeClosed())
2017-12-12 02:51:45 +00:00
})
It("drops undecryptable packets when the undecrytable packet queue is full", func() {
2018-01-20 18:07:01 +00:00
go func() {
defer GinkgoRecover()
sess.run()
}()
2017-12-12 02:51:45 +00:00
sendUndecryptablePackets()
Eventually(func() []*receivedPacket { return sess.undecryptablePackets }).Should(HaveLen(protocol.MaxUndecryptablePackets))
// check that old packets are kept, and the new packets are dropped
2018-01-03 19:19:49 +00:00
Expect(sess.undecryptablePackets[0].header.PacketNumber).To(Equal(protocol.PacketNumber(1)))
Expect(sess.Close(nil)).To(Succeed())
Eventually(sess.Context().Done()).Should(BeClosed())
2017-12-12 02:51:45 +00:00
})
It("sends a Public Reset after a timeout", func() {
2018-01-03 19:19:49 +00:00
Expect(sess.receivedTooManyUndecrytablePacketsTime).To(BeZero())
2018-01-20 18:07:01 +00:00
go func() {
defer GinkgoRecover()
sess.run()
}()
2017-12-12 02:51:45 +00:00
sendUndecryptablePackets()
2018-01-03 19:19:49 +00:00
Eventually(func() time.Time { return sess.receivedTooManyUndecrytablePacketsTime }).Should(BeTemporally("~", time.Now(), time.Second))
2017-12-12 02:51:45 +00:00
// speed up this test by manually setting back the time when too many packets were received
sess.receivedTooManyUndecrytablePacketsTime = time.Now().Add(-protocol.PublicResetTimeout)
time.Sleep(10 * time.Millisecond) // wait for the run loop to spin up
sess.scheduleSending() // wake up the run loop
2018-01-03 19:19:49 +00:00
Eventually(mconn.written).Should(HaveLen(1))
Expect(mconn.written).To(Receive(ContainSubstring("PRST")))
Eventually(sess.Context().Done()).Should(BeClosed())
2017-12-12 02:51:45 +00:00
})
It("doesn't send a Public Reset if decrypting them suceeded during the timeout", func() {
2018-01-20 18:07:01 +00:00
go func() {
defer GinkgoRecover()
sess.run()
}()
2017-12-12 02:51:45 +00:00
sess.receivedTooManyUndecrytablePacketsTime = time.Now().Add(-protocol.PublicResetTimeout).Add(-time.Millisecond)
sess.scheduleSending() // wake up the run loop
// there are no packets in the undecryptable packet queue
// in reality, this happens when the trial decryption succeeded during the Public Reset timeout
2018-01-03 19:19:49 +00:00
Consistently(mconn.written).ShouldNot(HaveLen(1))
Expect(sess.Context().Done()).ToNot(Receive())
Expect(sess.Close(nil)).To(Succeed())
Eventually(sess.Context().Done()).Should(BeClosed())
2017-12-12 02:51:45 +00:00
})
It("ignores undecryptable packets after the handshake is complete", func() {
2018-01-03 19:19:49 +00:00
sess.handshakeComplete = true
2018-01-20 18:07:01 +00:00
go func() {
defer GinkgoRecover()
sess.run()
}()
2017-12-12 02:51:45 +00:00
sendUndecryptablePackets()
Consistently(sess.undecryptablePackets).Should(BeEmpty())
2018-01-03 19:19:49 +00:00
Expect(sess.Close(nil)).To(Succeed())
Eventually(sess.Context().Done()).Should(BeClosed())
2017-12-12 02:51:45 +00:00
})
It("unqueues undecryptable packets for later decryption", func() {
sess.undecryptablePackets = []*receivedPacket{{
2018-01-03 19:19:49 +00:00
header: &wire.Header{PacketNumber: protocol.PacketNumber(42)},
2017-12-12 02:51:45 +00:00
}}
Expect(sess.receivedPackets).NotTo(Receive())
sess.tryDecryptingQueuedPackets()
Expect(sess.undecryptablePackets).To(BeEmpty())
Expect(sess.receivedPackets).To(Receive())
})
})
2018-01-20 18:07:01 +00:00
It("doesn't do anything when the crypto setup says to decrypt undecryptable packets", func() {
done := make(chan struct{})
go func() {
defer GinkgoRecover()
err := sess.run()
Expect(err).ToNot(HaveOccurred())
close(done)
}()
handshakeChan <- struct{}{}
Consistently(sess.handshakeStatus()).ShouldNot(Receive())
// make sure the go routine returns
streamManager.EXPECT().CloseWithError(gomock.Any())
2018-01-03 19:19:49 +00:00
Expect(sess.Close(nil)).To(Succeed())
2018-01-20 18:07:01 +00:00
Eventually(done).Should(BeClosed())
2018-01-03 19:19:49 +00:00
})
2018-01-20 18:07:01 +00:00
It("closes the handshakeChan when the handshake completes", func() {
done := make(chan struct{})
go func() {
defer GinkgoRecover()
err := sess.run()
Expect(err).ToNot(HaveOccurred())
close(done)
}()
close(handshakeChan)
2018-01-03 19:19:49 +00:00
Eventually(sess.handshakeStatus()).Should(BeClosed())
2018-01-20 18:07:01 +00:00
// make sure the go routine returns
streamManager.EXPECT().CloseWithError(gomock.Any())
2018-01-03 19:19:49 +00:00
Expect(sess.Close(nil)).To(Succeed())
2018-01-20 18:07:01 +00:00
Eventually(done).Should(BeClosed())
2018-01-03 19:19:49 +00:00
})
2018-01-20 18:07:01 +00:00
It("passes errors to the handshakeChan", func() {
2018-01-03 19:19:49 +00:00
testErr := errors.New("handshake error")
2018-01-20 18:07:01 +00:00
done := make(chan struct{})
go func() {
defer GinkgoRecover()
err := sess.run()
Expect(err).To(MatchError(testErr))
close(done)
}()
streamManager.EXPECT().CloseWithError(gomock.Any())
sess.Close(testErr)
Expect(sess.handshakeStatus()).To(Receive(Equal(testErr)))
Eventually(done).Should(BeClosed())
2018-01-03 19:19:49 +00:00
})
It("process transport parameters received from the peer", func() {
paramsChan := make(chan handshake.TransportParameters)
sess.paramsChan = paramsChan
2018-01-20 18:07:01 +00:00
done := make(chan struct{})
go func() {
defer GinkgoRecover()
sess.run()
close(done)
}()
2018-01-03 19:19:49 +00:00
params := handshake.TransportParameters{
MaxStreams: 123,
IdleTimeout: 90 * time.Second,
StreamFlowControlWindow: 0x5000,
ConnectionFlowControlWindow: 0x5000,
OmitConnectionID: true,
}
2018-01-20 18:07:01 +00:00
streamManager.EXPECT().UpdateLimits(&params)
2018-01-03 19:19:49 +00:00
paramsChan <- params
Eventually(func() *handshake.TransportParameters { return sess.peerParams }).Should(Equal(&params))
Eventually(func() bool { return sess.packer.omitConnectionID }).Should(BeTrue())
2018-01-20 18:07:01 +00:00
// make the go routine return
streamManager.EXPECT().CloseWithError(gomock.Any())
2018-01-03 19:19:49 +00:00
Expect(sess.Close(nil)).To(Succeed())
2018-01-20 18:07:01 +00:00
Eventually(done).Should(BeClosed())
2018-01-03 19:19:49 +00:00
})
Context("keep-alives", func() {
// should be shorter than the local timeout for these tests
// otherwise we'd send a CONNECTION_CLOSE in the tests where we're testing that no PING is sent
remoteIdleTimeout := 20 * time.Second
BeforeEach(func() {
sess.peerParams = &handshake.TransportParameters{IdleTimeout: remoteIdleTimeout}
})
It("sends a PING", func() {
sess.handshakeComplete = true
sess.config.KeepAlive = true
sess.lastNetworkActivityTime = time.Now().Add(-remoteIdleTimeout / 2)
sess.packer.hasSentPacket = true // make sure this is not the first packet the packer sends
2018-01-20 18:07:01 +00:00
done := make(chan struct{})
go func() {
defer GinkgoRecover()
sess.run()
close(done)
}()
2018-01-03 19:19:49 +00:00
var data []byte
Eventually(mconn.written).Should(Receive(&data))
// -12 because of the crypto tag. This should be 7 (the frame id for a ping frame).
Expect(data[len(data)-12-1 : len(data)-12]).To(Equal([]byte{0x07}))
2018-01-20 18:07:01 +00:00
// make the go routine return
streamManager.EXPECT().CloseWithError(gomock.Any())
sess.Close(nil)
Eventually(done).Should(BeClosed())
2018-01-03 19:19:49 +00:00
})
It("doesn't send a PING packet if keep-alive is disabled", func() {
sess.handshakeComplete = true
sess.config.KeepAlive = false
sess.lastNetworkActivityTime = time.Now().Add(-remoteIdleTimeout / 2)
2018-01-20 18:07:01 +00:00
done := make(chan struct{})
go func() {
defer GinkgoRecover()
sess.run()
close(done)
}()
2018-01-03 19:19:49 +00:00
Consistently(mconn.written).ShouldNot(Receive())
2018-01-20 18:07:01 +00:00
// make the go routine return
streamManager.EXPECT().CloseWithError(gomock.Any())
sess.Close(nil)
Eventually(done).Should(BeClosed())
2018-01-03 19:19:49 +00:00
})
It("doesn't send a PING if the handshake isn't completed yet", func() {
sess.handshakeComplete = false
sess.config.KeepAlive = true
sess.lastNetworkActivityTime = time.Now().Add(-remoteIdleTimeout / 2)
2018-01-20 18:07:01 +00:00
done := make(chan struct{})
go func() {
defer GinkgoRecover()
sess.run()
close(done)
}()
2018-01-03 19:19:49 +00:00
Consistently(mconn.written).ShouldNot(Receive())
2018-01-20 18:07:01 +00:00
// make the go routine return
streamManager.EXPECT().CloseWithError(gomock.Any())
sess.Close(nil)
Eventually(done).Should(BeClosed())
2018-01-03 19:19:49 +00:00
})
})
2017-12-12 02:51:45 +00:00
Context("timeouts", func() {
2018-01-20 18:07:01 +00:00
BeforeEach(func() {
streamManager.EXPECT().CloseWithError(gomock.Any())
})
2017-12-12 02:51:45 +00:00
It("times out due to no network activity", func(done Done) {
2018-01-03 19:19:49 +00:00
sess.handshakeComplete = true
2017-12-12 02:51:45 +00:00
sess.lastNetworkActivityTime = time.Now().Add(-time.Hour)
2018-01-03 19:19:49 +00:00
err := sess.run() // Would normally not return
Expect(err.(*qerr.QuicError).ErrorCode).To(Equal(qerr.NetworkIdleTimeout))
Expect(mconn.written).To(Receive(ContainSubstring("No recent network activity.")))
Expect(sess.Context().Done()).To(BeClosed())
2017-12-12 02:51:45 +00:00
close(done)
})
2018-01-03 19:19:49 +00:00
It("times out due to non-completed handshake", func(done Done) {
sess.sessionCreationTime = time.Now().Add(-protocol.DefaultHandshakeTimeout).Add(-time.Second)
err := sess.run() // Would normally not return
Expect(err.(*qerr.QuicError).ErrorCode).To(Equal(qerr.HandshakeTimeout))
Expect(mconn.written).To(Receive(ContainSubstring("Crypto handshake did not complete in time.")))
Expect(sess.Context().Done()).To(BeClosed())
2017-12-12 02:51:45 +00:00
close(done)
})
2018-01-03 19:19:49 +00:00
It("does not use the idle timeout before the handshake complete", func() {
sess.config.IdleTimeout = 9999 * time.Second
defer sess.Close(nil)
2017-12-12 02:51:45 +00:00
sess.lastNetworkActivityTime = time.Now().Add(-time.Minute)
2018-01-03 19:19:49 +00:00
// the handshake timeout is irrelevant here, since it depends on the time the session was created,
// and not on the last network activity
done := make(chan struct{})
go func() {
defer GinkgoRecover()
_ = sess.run()
close(done)
}()
Consistently(done).ShouldNot(BeClosed())
2017-12-12 02:51:45 +00:00
})
2018-01-03 19:19:49 +00:00
It("closes the session due to the idle timeout after handshake", func() {
sess.config.IdleTimeout = 0
2018-01-20 18:07:01 +00:00
close(handshakeChan)
2018-01-03 19:19:49 +00:00
errChan := make(chan error)
go func() {
defer GinkgoRecover()
errChan <- sess.run() // Would normally not return
}()
var err error
Eventually(errChan).Should(Receive(&err))
Expect(err.(*qerr.QuicError).ErrorCode).To(Equal(qerr.NetworkIdleTimeout))
Expect(mconn.written).To(Receive(ContainSubstring("No recent network activity.")))
Expect(sess.Context().Done()).To(BeClosed())
2017-12-12 02:51:45 +00:00
})
})
It("stores up to MaxSessionUnprocessedPackets packets", func(done Done) {
// Nothing here should block
for i := protocol.PacketNumber(0); i < protocol.MaxSessionUnprocessedPackets+10; i++ {
sess.handlePacket(&receivedPacket{})
}
close(done)
}, 0.5)
Context("getting streams", func() {
It("returns a new stream", func() {
2018-01-20 18:07:01 +00:00
mstr := NewMockStreamI(mockCtrl)
streamManager.EXPECT().GetOrOpenStream(protocol.StreamID(11)).Return(mstr, nil)
2017-12-12 02:51:45 +00:00
str, err := sess.GetOrOpenStream(11)
Expect(err).ToNot(HaveOccurred())
2018-01-20 18:07:01 +00:00
Expect(str).To(Equal(mstr))
2017-12-12 02:51:45 +00:00
})
It("returns a nil-value (not an interface with value nil) for closed streams", func() {
2018-01-20 18:07:01 +00:00
strI := Stream(nil)
streamManager.EXPECT().GetOrOpenStream(protocol.StreamID(1337)).Return(strI, nil)
str, err := sess.GetOrOpenStream(1337)
2017-12-12 02:51:45 +00:00
Expect(err).ToNot(HaveOccurred())
// make sure that the returned value is a plain nil, not an Stream with value nil
_, ok := str.(Stream)
Expect(ok).To(BeFalse())
})
// all relevant tests for this are in the streamsMap
It("opens streams synchronously", func() {
2018-01-20 18:07:01 +00:00
mstr := NewMockStreamI(mockCtrl)
streamManager.EXPECT().OpenStreamSync().Return(mstr, nil)
2017-12-12 02:51:45 +00:00
str, err := sess.OpenStreamSync()
Expect(err).ToNot(HaveOccurred())
2018-01-20 18:07:01 +00:00
Expect(str).To(Equal(mstr))
2017-12-12 02:51:45 +00:00
})
})
Context("ignoring errors", func() {
It("ignores duplicate acks", func() {
sess.sentPacketHandler.SentPacket(&ackhandler.Packet{
PacketNumber: 1,
Length: 1,
})
2018-01-03 19:19:49 +00:00
err := sess.handleFrames([]wire.Frame{&wire.AckFrame{
2017-12-12 02:51:45 +00:00
LargestAcked: 1,
2018-01-03 19:19:49 +00:00
}}, protocol.EncryptionUnspecified)
2017-12-12 02:51:45 +00:00
Expect(err).NotTo(HaveOccurred())
2018-01-03 19:19:49 +00:00
err = sess.handleFrames([]wire.Frame{&wire.AckFrame{
2017-12-12 02:51:45 +00:00
LargestAcked: 1,
2018-01-03 19:19:49 +00:00
}}, protocol.EncryptionUnspecified)
2017-12-12 02:51:45 +00:00
Expect(err).NotTo(HaveOccurred())
})
})
It("returns the local address", func() {
addr := &net.UDPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 1337}
mconn.localAddr = addr
Expect(sess.LocalAddr()).To(Equal(addr))
})
It("returns the remote address", func() {
addr := &net.UDPAddr{IP: net.IPv4(1, 2, 7, 1), Port: 7331}
mconn.remoteAddr = addr
Expect(sess.RemoteAddr()).To(Equal(addr))
})
})
2018-01-03 19:19:49 +00:00
var _ = Describe("Client Session", func() {
var (
2018-01-20 18:07:01 +00:00
sess *session
mconn *mockConnection
handshakeChan chan<- struct{}
2018-01-03 19:19:49 +00:00
cryptoSetup *mockCryptoSetup
)
BeforeEach(func() {
Eventually(areSessionsRunning).Should(BeFalse())
cryptoSetup = &mockCryptoSetup{}
newCryptoSetupClient = func(
_ io.ReadWriter,
_ string,
_ protocol.ConnectionID,
_ protocol.VersionNumber,
_ *tls.Config,
_ *handshake.TransportParameters,
_ chan<- handshake.TransportParameters,
2018-01-20 18:07:01 +00:00
handshakeChanP chan<- struct{},
2018-01-03 19:19:49 +00:00
_ protocol.VersionNumber,
_ []protocol.VersionNumber,
) (handshake.CryptoSetup, error) {
2018-01-20 18:07:01 +00:00
handshakeChan = handshakeChanP
2018-01-03 19:19:49 +00:00
return cryptoSetup, nil
}
mconn = newMockConnection()
sessP, err := newClientSession(
mconn,
"hostname",
protocol.Version39,
0,
nil,
populateClientConfig(&Config{}),
protocol.VersionWhatever,
nil,
)
sess = sessP.(*session)
Expect(err).ToNot(HaveOccurred())
})
AfterEach(func() {
newCryptoSetupClient = handshake.NewCryptoSetupClient
})
2018-01-20 18:07:01 +00:00
It("sends a forward-secure packet when the handshake completes", func() {
sess.packer.hasSentPacket = true
done := make(chan struct{})
go func() {
defer GinkgoRecover()
err := sess.run()
Expect(err).ToNot(HaveOccurred())
close(done)
}()
close(handshakeChan)
Eventually(mconn.written).Should(Receive())
//make sure the go routine returns
Expect(sess.Close(nil)).To(Succeed())
Eventually(done).Should(BeClosed())
})
2018-01-03 19:19:49 +00:00
Context("receiving packets", func() {
var hdr *wire.Header
BeforeEach(func() {
hdr = &wire.Header{PacketNumberLen: protocol.PacketNumberLen6}
sess.unpacker = &mockUnpacker{}
})
2018-01-20 18:07:01 +00:00
It("passes the diversification nonce to the crypto setup", func() {
done := make(chan struct{})
2018-01-03 19:19:49 +00:00
go func() {
defer GinkgoRecover()
2018-01-20 18:07:01 +00:00
err := sess.run()
Expect(err).ToNot(HaveOccurred())
close(done)
2018-01-03 19:19:49 +00:00
}()
hdr.PacketNumber = 5
hdr.DiversificationNonce = []byte("foobar")
err := sess.handlePacketImpl(&receivedPacket{header: hdr})
Expect(err).ToNot(HaveOccurred())
Eventually(func() []byte { return cryptoSetup.divNonce }).Should(Equal(hdr.DiversificationNonce))
Expect(sess.Close(nil)).To(Succeed())
2018-01-20 18:07:01 +00:00
Eventually(done).Should(BeClosed())
2018-01-03 19:19:49 +00:00
})
})
})